]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs3.patch
- 3.1.101
[packages/kernel.git] / kernel-aufs3.patch
1 aufs3.10 kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index c229f82..397b473 100644
5 --- a/fs/Kconfig
6 +++ b/fs/Kconfig
7 @@ -212,6 +212,7 @@ source "fs/ufs/Kconfig"
8  source "fs/exofs/Kconfig"
9  source "fs/f2fs/Kconfig"
10  source "fs/efivarfs/Kconfig"
11 +source "fs/aufs/Kconfig"
12  
13  endif # MISC_FILESYSTEMS
14  
15 diff --git a/fs/Makefile b/fs/Makefile
16 index 4fe6df3..4a57676 100644
17 --- a/fs/Makefile
18 +++ b/fs/Makefile
19 @@ -126,3 +126,4 @@ obj-y                               += exofs/ # Multiple modules
20  obj-$(CONFIG_CEPH_FS)          += ceph/
21  obj-$(CONFIG_PSTORE)           += pstore/
22  obj-$(CONFIG_EFIVAR_FS)                += efivarfs/
23 +obj-$(CONFIG_AUFS_FS)           += aufs/
24 diff --git a/include/uapi/linux/Kbuild b/include/uapi/linux/Kbuild
25 index bdc6e87..349600c 100644
26 --- a/include/uapi/linux/Kbuild
27 +++ b/include/uapi/linux/Kbuild
28 @@ -56,6 +56,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.10 base patch
37
38 diff --git a/fs/inode.c b/fs/inode.c
39 index 00d5fc3..f324521 100644
40 --- a/fs/inode.c
41 +++ b/fs/inode.c
42 @@ -1498,7 +1498,7 @@ static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
43   * This does the actual work of updating an inodes time or version.  Must have
44   * had called mnt_want_write() before calling this.
45   */
46 -static int update_time(struct inode *inode, struct timespec *time, int flags)
47 +int update_time(struct inode *inode, struct timespec *time, int flags)
48  {
49         if (inode->i_op->update_time)
50                 return inode->i_op->update_time(inode, time, flags);
51 diff --git a/fs/splice.c b/fs/splice.c
52 index d37431d..987346f 100644
53 --- a/fs/splice.c
54 +++ b/fs/splice.c
55 @@ -1093,8 +1093,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 @@ -1124,9 +1124,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/fs.h b/include/linux/fs.h
80 index 65c2be2..0148214 100644
81 --- a/include/linux/fs.h
82 +++ b/include/linux/fs.h
83 @@ -2574,6 +2574,7 @@ extern int inode_change_ok(const struct inode *, struct iattr *);
84  extern int inode_newsize_ok(const struct inode *, loff_t offset);
85  extern void setattr_copy(struct inode *inode, const struct iattr *attr);
86  
87 +extern int update_time(struct inode *, struct timespec *, int);
88  extern int file_update_time(struct file *file);
89  
90  extern int generic_show_options(struct seq_file *m, struct dentry *root);
91 diff --git a/include/linux/splice.h b/include/linux/splice.h
92 index 74575cb..bfc6fb6 100644
93 --- a/include/linux/splice.h
94 +++ b/include/linux/splice.h
95 @@ -92,4 +92,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
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.10 standalone patch
107
108 diff --git a/fs/inode.c b/fs/inode.c
109 index f324521..bff7670 100644
110 --- a/fs/inode.c
111 +++ b/fs/inode.c
112 @@ -56,6 +56,7 @@ static struct hlist_head *inode_hashtable __read_mostly;
113  static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
114  
115  __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
116 +EXPORT_SYMBOL(inode_sb_list_lock);
117  
118  /*
119   * Empty aops. Can be used for the cases where the user does not
120 @@ -1514,6 +1515,7 @@ int update_time(struct inode *inode, struct timespec *time, int flags)
121         mark_inode_dirty_sync(inode);
122         return 0;
123  }
124 +EXPORT_SYMBOL(update_time);
125  
126  /**
127   *     touch_atime     -       update the access time
128 diff --git a/fs/namespace.c b/fs/namespace.c
129 index 7b1ca9b..51db6ad 100644
130 --- a/fs/namespace.c
131 +++ b/fs/namespace.c
132 @@ -54,6 +54,7 @@ EXPORT_SYMBOL_GPL(fs_kobj);
133   * tree or hash is modified or when a vfsmount structure is modified.
134   */
135  DEFINE_BRLOCK(vfsmount_lock);
136 +EXPORT_SYMBOL(vfsmount_lock);
137  
138  static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
139  {
140 @@ -427,6 +428,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
141         mnt_dec_writers(real_mount(mnt));
142         preempt_enable();
143  }
144 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
145  
146  /**
147   * mnt_drop_write - give up write access to a mount
148 @@ -1456,6 +1458,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
149         }
150         return 0;
151  }
152 +EXPORT_SYMBOL(iterate_mounts);
153  
154  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
155  {
156 diff --git a/fs/notify/group.c b/fs/notify/group.c
157 index bd2625b..2ff2a0f 100644
158 --- a/fs/notify/group.c
159 +++ b/fs/notify/group.c
160 @@ -22,6 +22,7 @@
161  #include <linux/srcu.h>
162  #include <linux/rculist.h>
163  #include <linux/wait.h>
164 +#include <linux/module.h>
165  
166  #include <linux/fsnotify_backend.h>
167  #include "fsnotify.h"
168 @@ -65,6 +66,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
169  {
170         atomic_inc(&group->refcnt);
171  }
172 +EXPORT_SYMBOL(fsnotify_get_group);
173  
174  /*
175   * Drop a reference to a group.  Free it if it's through.
176 @@ -74,6 +76,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
177         if (atomic_dec_and_test(&group->refcnt))
178                 fsnotify_final_destroy_group(group);
179  }
180 +EXPORT_SYMBOL(fsnotify_put_group);
181  
182  /*
183   * Create a new fsnotify_group and hold a reference for the group returned.
184 @@ -102,6 +105,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
185  
186         return group;
187  }
188 +EXPORT_SYMBOL(fsnotify_alloc_group);
189  
190  int fsnotify_fasync(int fd, struct file *file, int on)
191  {
192 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
193 index fc6b49b..a6bb87d 100644
194 --- a/fs/notify/mark.c
195 +++ b/fs/notify/mark.c
196 @@ -115,6 +115,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
197                 mark->free_mark(mark);
198         }
199  }
200 +EXPORT_SYMBOL(fsnotify_put_mark);
201  
202  /*
203   * Any time a mark is getting freed we end up here.
204 @@ -197,6 +198,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
205         fsnotify_destroy_mark_locked(mark, group);
206         mutex_unlock(&group->mark_mutex);
207  }
208 +EXPORT_SYMBOL(fsnotify_destroy_mark);
209  
210  void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
211  {
212 @@ -281,6 +283,7 @@ err:
213  
214         return ret;
215  }
216 +EXPORT_SYMBOL(fsnotify_add_mark);
217  
218  int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
219                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
220 @@ -342,6 +345,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
221         atomic_set(&mark->refcnt, 1);
222         mark->free_mark = free_mark;
223  }
224 +EXPORT_SYMBOL(fsnotify_init_mark);
225  
226  static int fsnotify_mark_destroy(void *ignored)
227  {
228 diff --git a/fs/open.c b/fs/open.c
229 index 8c74100..be563cd 100644
230 --- a/fs/open.c
231 +++ b/fs/open.c
232 @@ -61,6 +61,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
233         mutex_unlock(&dentry->d_inode->i_mutex);
234         return ret;
235  }
236 +EXPORT_SYMBOL(do_truncate);
237  
238  long vfs_truncate(struct path *path, loff_t length)
239  {
240 diff --git a/fs/splice.c b/fs/splice.c
241 index 987346f..8d6a045 100644
242 --- a/fs/splice.c
243 +++ b/fs/splice.c
244 @@ -1120,6 +1120,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
245         file_end_write(out);
246         return ret;
247  }
248 +EXPORT_SYMBOL(do_splice_from);
249  
250  /*
251   * Attempt to initiate a splice from a file to a pipe.
252 @@ -1146,6 +1147,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
253  
254         return splice_read(in, ppos, pipe, len, flags);
255  }
256 +EXPORT_SYMBOL(do_splice_to);
257  
258  /**
259   * splice_direct_to_actor - splices data directly between two non-pipes
260 diff --git a/security/commoncap.c b/security/commoncap.c
261 index c44b6fe..d78b003 100644
262 --- a/security/commoncap.c
263 +++ b/security/commoncap.c
264 @@ -988,9 +988,11 @@ int cap_mmap_addr(unsigned long addr)
265         }
266         return ret;
267  }
268 +EXPORT_SYMBOL(cap_mmap_addr);
269  
270  int cap_mmap_file(struct file *file, unsigned long reqprot,
271                   unsigned long prot, unsigned long flags)
272  {
273         return 0;
274  }
275 +EXPORT_SYMBOL(cap_mmap_file);
276 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
277 index dd0dc57..9760ecb6 100644
278 --- a/security/device_cgroup.c
279 +++ b/security/device_cgroup.c
280 @@ -7,6 +7,7 @@
281  #include <linux/device_cgroup.h>
282  #include <linux/cgroup.h>
283  #include <linux/ctype.h>
284 +#include <linux/export.h>
285  #include <linux/list.h>
286  #include <linux/uaccess.h>
287  #include <linux/seq_file.h>
288 @@ -789,6 +790,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
289         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
290                         access);
291  }
292 +EXPORT_SYMBOL(__devcgroup_inode_permission);
293  
294  int devcgroup_inode_mknod(int mode, dev_t dev)
295  {
296 diff --git a/security/security.c b/security/security.c
297 index a3dce87..06a6ea6 100644
298 --- a/security/security.c
299 +++ b/security/security.c
300 @@ -396,6 +396,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
301                 return 0;
302         return security_ops->path_rmdir(dir, dentry);
303  }
304 +EXPORT_SYMBOL(security_path_rmdir);
305  
306  int security_path_unlink(struct path *dir, struct dentry *dentry)
307  {
308 @@ -412,6 +413,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
309                 return 0;
310         return security_ops->path_symlink(dir, dentry, old_name);
311  }
312 +EXPORT_SYMBOL(security_path_symlink);
313  
314  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
315                        struct dentry *new_dentry)
316 @@ -420,6 +422,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
317                 return 0;
318         return security_ops->path_link(old_dentry, new_dir, new_dentry);
319  }
320 +EXPORT_SYMBOL(security_path_link);
321  
322  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
323                          struct path *new_dir, struct dentry *new_dentry)
324 @@ -438,6 +441,7 @@ int security_path_truncate(struct path *path)
325                 return 0;
326         return security_ops->path_truncate(path);
327  }
328 +EXPORT_SYMBOL(security_path_truncate);
329  
330  int security_path_chmod(struct path *path, umode_t mode)
331  {
332 @@ -445,6 +449,7 @@ int security_path_chmod(struct path *path, umode_t mode)
333                 return 0;
334         return security_ops->path_chmod(path, mode);
335  }
336 +EXPORT_SYMBOL(security_path_chmod);
337  
338  int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
339  {
340 @@ -452,6 +457,7 @@ int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
341                 return 0;
342         return security_ops->path_chown(path, uid, gid);
343  }
344 +EXPORT_SYMBOL(security_path_chown);
345  
346  int security_path_chroot(struct path *path)
347  {
348 @@ -528,6 +534,7 @@ int security_inode_readlink(struct dentry *dentry)
349                 return 0;
350         return security_ops->inode_readlink(dentry);
351  }
352 +EXPORT_SYMBOL(security_inode_readlink);
353  
354  int security_inode_follow_link(struct dentry *dentry, struct nameidata *nd)
355  {
356 @@ -542,6 +549,7 @@ int security_inode_permission(struct inode *inode, int mask)
357                 return 0;
358         return security_ops->inode_permission(inode, mask);
359  }
360 +EXPORT_SYMBOL(security_inode_permission);
361  
362  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
363  {
364 @@ -663,6 +671,7 @@ int security_file_permission(struct file *file, int mask)
365  
366         return fsnotify_perm(file, mask);
367  }
368 +EXPORT_SYMBOL(security_file_permission);
369  
370  int security_file_alloc(struct file *file)
371  {
372 @@ -723,6 +732,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
373                 return ret;
374         return ima_file_mmap(file, prot);
375  }
376 +EXPORT_SYMBOL(security_mmap_file);
377  
378  int security_mmap_addr(unsigned long addr)
379  {
380 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
381 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
382 +++ linux/Documentation/ABI/testing/debugfs-aufs        2013-07-06 13:20:47.716863966 +0200
383 @@ -0,0 +1,50 @@
384 +What:          /debug/aufs/si_<id>/
385 +Date:          March 2009
386 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
387 +Description:
388 +               Under /debug/aufs, a directory named si_<id> is created
389 +               per aufs mount, where <id> is a unique id generated
390 +               internally.
391 +
392 +What:          /debug/aufs/si_<id>/plink
393 +Date:          Apr 2013
394 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
395 +Description:
396 +               It has three lines and shows the information about the
397 +               pseudo-link. The first line is a single number
398 +               representing a number of buckets. The second line is a
399 +               number of pseudo-links per buckets (separated by a
400 +               blank). The last line is a single number representing a
401 +               total number of psedo-links.
402 +               When the aufs mount option 'noplink' is specified, it
403 +               will show "1\n0\n0\n".
404 +
405 +What:          /debug/aufs/si_<id>/xib
406 +Date:          March 2009
407 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
408 +Description:
409 +               It shows the consumed blocks by xib (External Inode Number
410 +               Bitmap), its block size and file size.
411 +               When the aufs mount option 'noxino' is specified, it
412 +               will be empty. About XINO files, see the aufs manual.
413 +
414 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
415 +Date:          March 2009
416 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
417 +Description:
418 +               It shows the consumed blocks by xino (External Inode Number
419 +               Translation Table), its link count, block size and file
420 +               size.
421 +               When the aufs mount option 'noxino' is specified, it
422 +               will be empty. About XINO files, see the aufs manual.
423 +
424 +What:          /debug/aufs/si_<id>/xigen
425 +Date:          March 2009
426 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
427 +Description:
428 +               It shows the consumed blocks by xigen (External Inode
429 +               Generation Table), its block size and file size.
430 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
431 +               be created.
432 +               When the aufs mount option 'noxino' is specified, it
433 +               will be empty. About XINO files, see the aufs manual.
434 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
435 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
436 +++ linux/Documentation/ABI/testing/sysfs-aufs  2013-07-06 13:20:47.730197761 +0200
437 @@ -0,0 +1,24 @@
438 +What:          /sys/fs/aufs/si_<id>/
439 +Date:          March 2009
440 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
441 +Description:
442 +               Under /sys/fs/aufs, a directory named si_<id> is created
443 +               per aufs mount, where <id> is a unique id generated
444 +               internally.
445 +
446 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
447 +Date:          March 2009
448 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
449 +Description:
450 +               It shows the abolute path of a member directory (which
451 +               is called branch) in aufs, and its permission.
452 +
453 +What:          /sys/fs/aufs/si_<id>/xi_path
454 +Date:          March 2009
455 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
456 +Description:
457 +               It shows the abolute path of XINO (External Inode Number
458 +               Bitmap, Translation Table and Generation Table) file
459 +               even if it is the default path.
460 +               When the aufs mount option 'noxino' is specified, it
461 +               will be empty. About XINO files, see the aufs manual.
462 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
463 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
464 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2013-07-06 13:20:47.730197761 +0200
465 @@ -0,0 +1,162 @@
466 +
467 +# Copyright (C) 2005-2013 Junjiro R. Okajima
468 +# 
469 +# This program is free software; you can redistribute it and/or modify
470 +# it under the terms of the GNU General Public License as published by
471 +# the Free Software Foundation; either version 2 of the License, or
472 +# (at your option) any later version.
473 +# 
474 +# This program is distributed in the hope that it will be useful,
475 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
476 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
477 +# GNU General Public License for more details.
478 +# 
479 +# You should have received a copy of the GNU General Public License
480 +# along with this program; if not, write to the Free Software
481 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
482 +
483 +Introduction
484 +----------------------------------------
485 +
486 +aufs [ei ju: ef es] | [a u f s]
487 +1. abbrev. for "advanced multi-layered unification filesystem".
488 +2. abbrev. for "another unionfs".
489 +3. abbrev. for "auf das" in German which means "on the" in English.
490 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
491 +   But "Filesystem aufs Filesystem" is hard to understand.
492 +
493 +AUFS is a filesystem with features:
494 +- multi layered stackable unification filesystem, the member directory
495 +  is called as a branch.
496 +- branch permission and attribute, 'readonly', 'real-readonly',
497 +  'readwrite', 'whiteout-able', 'link-able whiteout' and their
498 +  combination.
499 +- internal "file copy-on-write".
500 +- logical deletion, whiteout.
501 +- dynamic branch manipulation, adding, deleting and changing permission.
502 +- allow bypassing aufs, user's direct branch access.
503 +- external inode number translation table and bitmap which maintains the
504 +  persistent aufs inode number.
505 +- seekable directory, including NFS readdir.
506 +- file mapping, mmap and sharing pages.
507 +- pseudo-link, hardlink over branches.
508 +- loopback mounted filesystem as a branch.
509 +- several policies to select one among multiple writable branches.
510 +- revert a single systemcall when an error occurs in aufs.
511 +- and more...
512 +
513 +
514 +Multi Layered Stackable Unification Filesystem
515 +----------------------------------------------------------------------
516 +Most people already knows what it is.
517 +It is a filesystem which unifies several directories and provides a
518 +merged single directory. When users access a file, the access will be
519 +passed/re-directed/converted (sorry, I am not sure which English word is
520 +correct) to the real file on the member filesystem. The member
521 +filesystem is called 'lower filesystem' or 'branch' and has a mode
522 +'readonly' and 'readwrite.' And the deletion for a file on the lower
523 +readonly branch is handled by creating 'whiteout' on the upper writable
524 +branch.
525 +
526 +On LKML, there have been discussions about UnionMount (Jan Blunck,
527 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
528 +different approaches to implement the merged-view.
529 +The former tries putting it into VFS, and the latter implements as a
530 +separate filesystem.
531 +(If I misunderstand about these implementations, please let me know and
532 +I shall correct it. Because it is a long time ago when I read their
533 +source files last time).
534 +
535 +UnionMount's approach will be able to small, but may be hard to share
536 +branches between several UnionMount since the whiteout in it is
537 +implemented in the inode on branch filesystem and always
538 +shared. According to Bharata's post, readdir does not seems to be
539 +finished yet.
540 +There are several missing features known in this implementations such as
541 +- for users, the inode number may change silently. eg. copy-up.
542 +- link(2) may break by copy-up.
543 +- read(2) may get an obsoleted filedata (fstat(2) too).
544 +- fcntl(F_SETLK) may be broken by copy-up.
545 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
546 +  open(O_RDWR).
547 +
548 +Unionfs has a longer history. When I started implementing a stacking filesystem
549 +(Aug 2005), it already existed. It has virtual super_block, inode,
550 +dentry and file objects and they have an array pointing lower same kind
551 +objects. After contributing many patches for Unionfs, I re-started my
552 +project AUFS (Jun 2006).
553 +
554 +In AUFS, the structure of filesystem resembles to Unionfs, but I
555 +implemented my own ideas, approaches and enhancements and it became
556 +totally different one.
557 +
558 +Comparing DM snapshot and fs based implementation
559 +- the number of bytes to be copied between devices is much smaller.
560 +- the type of filesystem must be one and only.
561 +- the fs must be writable, no readonly fs, even for the lower original
562 +  device. so the compression fs will not be usable. but if we use
563 +  loopback mount, we may address this issue.
564 +  for instance,
565 +       mount /cdrom/squashfs.img /sq
566 +       losetup /sq/ext2.img
567 +       losetup /somewhere/cow
568 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
569 +- it will be difficult (or needs more operations) to extract the
570 +  difference between the original device and COW.
571 +- DM snapshot-merge may help a lot when users try merging. in the
572 +  fs-layer union, users will use rsync(1).
573 +
574 +
575 +Several characters/aspects of aufs
576 +----------------------------------------------------------------------
577 +
578 +Aufs has several characters or aspects.
579 +1. a filesystem, callee of VFS helper
580 +2. sub-VFS, caller of VFS helper for branches
581 +3. a virtual filesystem which maintains persistent inode number
582 +4. reader/writer of files on branches such like an application
583 +
584 +1. Callee of VFS Helper
585 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
586 +unlink(2) from an application reaches sys_unlink() kernel function and
587 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
588 +calls filesystem specific unlink operation. Actually aufs implements the
589 +unlink operation but it behaves like a redirector.
590 +
591 +2. Caller of VFS Helper for Branches
592 +aufs_unlink() passes the unlink request to the branch filesystem as if
593 +it were called from VFS. So the called unlink operation of the branch
594 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
595 +every necessary pre/post operation for the branch filesystem.
596 +- acquire the lock for the parent dir on a branch
597 +- lookup in a branch
598 +- revalidate dentry on a branch
599 +- mnt_want_write() for a branch
600 +- vfs_unlink() for a branch
601 +- mnt_drop_write() for a branch
602 +- release the lock on a branch
603 +
604 +3. Persistent Inode Number
605 +One of the most important issue for a filesystem is to maintain inode
606 +numbers. This is particularly important to support exporting a
607 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
608 +backend block device for its own. But some storage is necessary to
609 +maintain inode number. It may be a large space and may not suit to keep
610 +in memory. Aufs rents some space from its first writable branch
611 +filesystem (by default) and creates file(s) on it. These files are
612 +created by aufs internally and removed soon (currently) keeping opened.
613 +Note: Because these files are removed, they are totally gone after
614 +      unmounting aufs. It means the inode numbers are not persistent
615 +      across unmount or reboot. I have a plan to make them really
616 +      persistent which will be important for aufs on NFS server.
617 +
618 +4. Read/Write Files Internally (copy-on-write)
619 +Because a branch can be readonly, when you write a file on it, aufs will
620 +"copy-up" it to the upper writable branch internally. And then write the
621 +originally requested thing to the file. Generally kernel doesn't
622 +open/read/write file actively. In aufs, even a single write may cause a
623 +internal "file copy". This behaviour is very similar to cp(1) command.
624 +
625 +Some people may think it is better to pass such work to user space
626 +helper, instead of doing in kernel space. Actually I am still thinking
627 +about it. But currently I have implemented it in kernel space.
628 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
629 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
630 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2013-08-23 23:59:39.631583456 +0200
631 @@ -0,0 +1,243 @@
632 +
633 +# Copyright (C) 2005-2013 Junjiro R. Okajima
634 +# 
635 +# This program is free software; you can redistribute it and/or modify
636 +# it under the terms of the GNU General Public License as published by
637 +# the Free Software Foundation; either version 2 of the License, or
638 +# (at your option) any later version.
639 +# 
640 +# This program is distributed in the hope that it will be useful,
641 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
642 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
643 +# GNU General Public License for more details.
644 +# 
645 +# You should have received a copy of the GNU General Public License
646 +# along with this program; if not, write to the Free Software
647 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
648 +
649 +Basic Aufs Internal Structure
650 +
651 +Superblock/Inode/Dentry/File Objects
652 +----------------------------------------------------------------------
653 +As like an ordinary filesystem, aufs has its own
654 +superblock/inode/dentry/file objects. All these objects have a
655 +dynamically allocated array and store the same kind of pointers to the
656 +lower filesystem, branch.
657 +For example, when you build a union with one readwrite branch and one
658 +readonly, mounted /au, /rw and /ro respectively.
659 +- /au = /rw + /ro
660 +- /ro/fileA exists but /rw/fileA
661 +
662 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
663 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
664 +- [0] = NULL
665 +- [1] = /ro/fileA
666 +
667 +This style of an array is essentially same to the aufs
668 +superblock/inode/dentry/file objects.
669 +
670 +Because aufs supports manipulating branches, ie. add/delete/change
671 +dynamically, these objects has its own generation. When branches are
672 +changed, the generation in aufs superblock is incremented. And a
673 +generation in other object are compared when it is accessed.
674 +When a generation in other objects are obsoleted, aufs refreshes the
675 +internal array.
676 +
677 +
678 +Superblock
679 +----------------------------------------------------------------------
680 +Additionally aufs superblock has some data for policies to select one
681 +among multiple writable branches, XIB files, pseudo-links and kobject.
682 +See below in detail.
683 +About the policies which supports copy-down a directory, see policy.txt
684 +too.
685 +
686 +
687 +Branch and XINO(External Inode Number Translation Table)
688 +----------------------------------------------------------------------
689 +Every branch has its own xino (external inode number translation table)
690 +file. The xino file is created and unlinked by aufs internally. When two
691 +members of a union exist on the same filesystem, they share the single
692 +xino file.
693 +The struct of a xino file is simple, just a sequence of aufs inode
694 +numbers which is indexed by the lower inode number.
695 +In the above sample, assume the inode number of /ro/fileA is i111 and
696 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
697 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
698 +
699 +When the inode numbers are not contiguous, the xino file will be sparse
700 +which has a hole in it and doesn't consume as much disk space as it
701 +might appear. If your branch filesystem consumes disk space for such
702 +holes, then you should specify 'xino=' option at mounting aufs.
703 +
704 +Also a writable branch has three kinds of "whiteout bases". All these
705 +are existed when the branch is joined to aufs and the names are
706 +whiteout-ed doubly, so that users will never see their names in aufs
707 +hierarchy.
708 +1. a regular file which will be linked to all whiteouts.
709 +2. a directory to store a pseudo-link.
710 +3. a directory to store an "orphan-ed" file temporary.
711 +
712 +1. Whiteout Base
713 +   When you remove a file on a readonly branch, aufs handles it as a
714 +   logical deletion and creates a whiteout on the upper writable branch
715 +   as a hardlink of this file in order not to consume inode on the
716 +   writable branch.
717 +2. Pseudo-link Dir
718 +   See below, Pseudo-link.
719 +3. Step-Parent Dir
720 +   When "fileC" exists on the lower readonly branch only and it is
721 +   opened and removed with its parent dir, and then user writes
722 +   something into it, then aufs copies-up fileC to this
723 +   directory. Because there is no other dir to store fileC. After
724 +   creating a file under this dir, the file is unlinked.
725 +
726 +Because aufs supports manipulating branches, ie. add/delete/change
727 +dynamically, a branch has its own id. When the branch order changes, aufs
728 +finds the new index by searching the branch id.
729 +
730 +
731 +Pseudo-link
732 +----------------------------------------------------------------------
733 +Assume "fileA" exists on the lower readonly branch only and it is
734 +hardlinked to "fileB" on the branch. When you write something to fileA,
735 +aufs copies-up it to the upper writable branch. Additionally aufs
736 +creates a hardlink under the Pseudo-link Directory of the writable
737 +branch. The inode of a pseudo-link is kept in aufs super_block as a
738 +simple list. If fileB is read after unlinking fileA, aufs returns
739 +filedata from the pseudo-link instead of the lower readonly
740 +branch. Because the pseudo-link is based upon the inode, to keep the
741 +inode number by xino (see above) is important.
742 +
743 +All the hardlinks under the Pseudo-link Directory of the writable branch
744 +should be restored in a proper location later. Aufs provides a utility
745 +to do this. The userspace helpers executed at remounting and unmounting
746 +aufs by default.
747 +During this utility is running, it puts aufs into the pseudo-link
748 +maintenance mode. In this mode, only the process which began the
749 +maintenance mode (and its child processes) is allowed to operate in
750 +aufs. Some other processes which are not related to the pseudo-link will
751 +be allowed to run too, but the rest have to return an error or wait
752 +until the maintenance mode ends. If a process already acquires an inode
753 +mutex (in VFS), it has to return an error.
754 +
755 +
756 +XIB(external inode number bitmap)
757 +----------------------------------------------------------------------
758 +Addition to the xino file per a branch, aufs has an external inode number
759 +bitmap in a superblock object. It is also a file such like a xino file.
760 +It is a simple bitmap to mark whether the aufs inode number is in-use or
761 +not.
762 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
763 +
764 +Aufs implements a feature to truncate/refresh both of xino and xib to
765 +reduce the number of consumed disk blocks for these files.
766 +
767 +
768 +Virtual or Vertical Dir, and Readdir in Userspace
769 +----------------------------------------------------------------------
770 +In order to support multiple layers (branches), aufs readdir operation
771 +constructs a virtual dir block on memory. For readdir, aufs calls
772 +vfs_readdir() internally for each dir on branches, merges their entries
773 +with eliminating the whiteout-ed ones, and sets it to file (dir)
774 +object. So the file object has its entry list until it is closed. The
775 +entry list will be updated when the file position is zero and becomes
776 +old. This decision is made in aufs automatically.
777 +
778 +The dynamically allocated memory block for the name of entries has a
779 +unit of 512 bytes (by default) and stores the names contiguously (no
780 +padding). Another block for each entry is handled by kmem_cache too.
781 +During building dir blocks, aufs creates hash list and judging whether
782 +the entry is whiteouted by its upper branch or already listed.
783 +The merged result is cached in the corresponding inode object and
784 +maintained by a customizable life-time option.
785 +
786 +Some people may call it can be a security hole or invite DoS attack
787 +since the opened and once readdir-ed dir (file object) holds its entry
788 +list and becomes a pressure for system memory. But I'd say it is similar
789 +to files under /proc or /sys. The virtual files in them also holds a
790 +memory page (generally) while they are opened. When an idea to reduce
791 +memory for them is introduced, it will be applied to aufs too.
792 +For those who really hate this situation, I've developed readdir(3)
793 +library which operates this merging in userspace. You just need to set
794 +LD_PRELOAD environment variable, and aufs will not consume no memory in
795 +kernel space for readdir(3).
796 +
797 +
798 +Workqueue
799 +----------------------------------------------------------------------
800 +Aufs sometimes requires privilege access to a branch. For instance,
801 +in copy-up/down operation. When a user process is going to make changes
802 +to a file which exists in the lower readonly branch only, and the mode
803 +of one of ancestor directories may not be writable by a user
804 +process. Here aufs copy-up the file with its ancestors and they may
805 +require privilege to set its owner/group/mode/etc.
806 +This is a typical case of a application character of aufs (see
807 +Introduction).
808 +
809 +Aufs uses workqueue synchronously for this case. It creates its own
810 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
811 +passes the request to call mkdir or write (for example), and wait for
812 +its completion. This approach solves a problem of a signal handler
813 +simply.
814 +If aufs didn't adopt the workqueue and changed the privilege of the
815 +process, and if the mkdir/write call arises SIGXFSZ or other signal,
816 +then the user process might gain a privilege or the generated core file
817 +was owned by a superuser.
818 +
819 +Also aufs uses the system global workqueue ("events" kernel thread) too
820 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
821 +whiteout base and etc. This is unrelated to a privilege.
822 +Most of aufs operation tries acquiring a rw_semaphore for aufs
823 +superblock at the beginning, at the same time waits for the completion
824 +of all queued asynchronous tasks.
825 +
826 +
827 +Whiteout
828 +----------------------------------------------------------------------
829 +The whiteout in aufs is very similar to Unionfs's. That is represented
830 +by its filename. UnionMount takes an approach of a file mode, but I am
831 +afraid several utilities (find(1) or something) will have to support it.
832 +
833 +Basically the whiteout represents "logical deletion" which stops aufs to
834 +lookup further, but also it represents "dir is opaque" which also stop
835 +lookup.
836 +
837 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
838 +In order to make several functions in a single systemcall to be
839 +revertible, aufs adopts an approach to rename a directory to a temporary
840 +unique whiteouted name.
841 +For example, in rename(2) dir where the target dir already existed, aufs
842 +renames the target dir to a temporary unique whiteouted name before the
843 +actual rename on a branch and then handles other actions (make it opaque,
844 +update the attributes, etc). If an error happens in these actions, aufs
845 +simply renames the whiteouted name back and returns an error. If all are
846 +succeeded, aufs registers a function to remove the whiteouted unique
847 +temporary name completely and asynchronously to the system global
848 +workqueue.
849 +
850 +
851 +Copy-up
852 +----------------------------------------------------------------------
853 +It is a well-known feature or concept.
854 +When user modifies a file on a readonly branch, aufs operate "copy-up"
855 +internally and makes change to the new file on the upper writable branch.
856 +When the trigger systemcall does not update the timestamps of the parent
857 +dir, aufs reverts it after copy-up.
858 +
859 +
860 +Move-down (aufs3.9 and later)
861 +----------------------------------------------------------------------
862 +"Copy-up" is one of the essential feature in aufs. It copies a file from
863 +the lower readonly branch to the upper writable branch when a user
864 +changes something about the file.
865 +"Move-down" is an opposite action of copy-up. Basically this action is
866 +ran manually instead of automatically and internally.
867 +
868 +Sometimes users want to move-down a file from the upper writable branch
869 +to the lower readonly or writable branch. For instance,
870 +- the free space of the upper writable branch is going to run out.
871 +- create a new intermediate branch between the upper and lower branch.
872 +- etc.
873 +
874 +For this purpose, use "aumvdown" command in aufs-util.git.
875 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
876 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
877 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2013-07-06 13:20:47.730197761 +0200
878 @@ -0,0 +1,106 @@
879 +
880 +# Copyright (C) 2005-2013 Junjiro R. Okajima
881 +# 
882 +# This program is free software; you can redistribute it and/or modify
883 +# it under the terms of the GNU General Public License as published by
884 +# the Free Software Foundation; either version 2 of the License, or
885 +# (at your option) any later version.
886 +# 
887 +# This program is distributed in the hope that it will be useful,
888 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
889 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
890 +# GNU General Public License for more details.
891 +# 
892 +# You should have received a copy of the GNU General Public License
893 +# along with this program; if not, write to the Free Software
894 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
895 +
896 +Lookup in a Branch
897 +----------------------------------------------------------------------
898 +Since aufs has a character of sub-VFS (see Introduction), it operates
899 +lookup for branches as VFS does. It may be a heavy work. Generally
900 +speaking struct nameidata is a bigger structure and includes many
901 +information. But almost all lookup operation in aufs is the simplest
902 +case, ie. lookup only an entry directly connected to its parent. Digging
903 +down the directory hierarchy is unnecessary.
904 +
905 +VFS has a function lookup_one_len() for that use, but it is not usable
906 +for a branch filesystem which requires struct nameidata. So aufs
907 +implements a simple lookup wrapper function. When a branch filesystem
908 +allows NULL as nameidata, it calls lookup_one_len(). Otherwise it builds
909 +a simplest nameidata and calls lookup_hash().
910 +Here aufs applies "a principle in NFSD", ie. if the filesystem supports
911 +NFS-export, then it has to support NULL as a nameidata parameter for
912 +->create(), ->lookup() and ->d_revalidate(). So the lookup wrapper in
913 +aufs tests if ->s_export_op in the branch is NULL or not.
914 +
915 +When a branch is a remote filesystem, aufs basically trusts its
916 +->d_revalidate(), also aufs forces the hardest revalidate tests for
917 +them.
918 +For d_revalidate, aufs implements three levels of revalidate tests. See
919 +"Revalidate Dentry and UDBA" in detail.
920 +
921 +
922 +Loopback Mount
923 +----------------------------------------------------------------------
924 +Basically aufs supports any type of filesystem and block device for a
925 +branch (actually there are some exceptions). But it is prohibited to add
926 +a loopback mounted one whose backend file exists in a filesystem which is
927 +already added to aufs. The reason is to protect aufs from a recursive
928 +lookup. If it was allowed, the aufs lookup operation might re-enter a
929 +lookup for the loopback mounted branch in the same context, and will
930 +cause a deadlock.
931 +
932 +
933 +Revalidate Dentry and UDBA (User's Direct Branch Access)
934 +----------------------------------------------------------------------
935 +Generally VFS helpers re-validate a dentry as a part of lookup.
936 +0. digging down the directory hierarchy.
937 +1. lock the parent dir by its i_mutex.
938 +2. lookup the final (child) entry.
939 +3. revalidate it.
940 +4. call the actual operation (create, unlink, etc.)
941 +5. unlock the parent dir
942 +
943 +If the filesystem implements its ->d_revalidate() (step 3), then it is
944 +called. Actually aufs implements it and checks the dentry on a branch is
945 +still valid.
946 +But it is not enough. Because aufs has to release the lock for the
947 +parent dir on a branch at the end of ->lookup() (step 2) and
948 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
949 +held by VFS.
950 +If the file on a branch is changed directly, eg. bypassing aufs, after
951 +aufs released the lock, then the subsequent operation may cause
952 +something unpleasant result.
953 +
954 +This situation is a result of VFS architecture, ->lookup() and
955 +->d_revalidate() is separated. But I never say it is wrong. It is a good
956 +design from VFS's point of view. It is just not suitable for sub-VFS
957 +character in aufs.
958 +
959 +Aufs supports such case by three level of revalidation which is
960 +selectable by user.
961 +1. Simple Revalidate
962 +   Addition to the native flow in VFS's, confirm the child-parent
963 +   relationship on the branch just after locking the parent dir on the
964 +   branch in the "actual operation" (step 4). When this validation
965 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
966 +   checks the validation of the dentry on branches.
967 +2. Monitor Changes Internally by Inotify/Fsnotify
968 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
969 +   the dentry on the branch, and returns EBUSY if it finds different
970 +   dentry.
971 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
972 +   during it is in cache. When the event is notified, aufs registers a
973 +   function to kernel 'events' thread by schedule_work(). And the
974 +   function sets some special status to the cached aufs dentry and inode
975 +   private data. If they are not cached, then aufs has nothing to
976 +   do. When the same file is accessed through aufs (step 0-3) later,
977 +   aufs will detect the status and refresh all necessary data.
978 +   In this mode, aufs has to ignore the event which is fired by aufs
979 +   itself.
980 +3. No Extra Validation
981 +   This is the simplest test and doesn't add any additional revalidation
982 +   test, and skip therevalidatin in step 4. It is useful and improves
983 +   aufs performance when system surely hide the aufs branches from user,
984 +   by over-mounting something (or another method).
985 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
986 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
987 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2013-07-06 13:20:47.730197761 +0200
988 @@ -0,0 +1,76 @@
989 +
990 +# Copyright (C) 2005-2013 Junjiro R. Okajima
991 +# 
992 +# This program is free software; you can redistribute it and/or modify
993 +# it under the terms of the GNU General Public License as published by
994 +# the Free Software Foundation; either version 2 of the License, or
995 +# (at your option) any later version.
996 +# 
997 +# This program is distributed in the hope that it will be useful,
998 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
999 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1000 +# GNU General Public License for more details.
1001 +# 
1002 +# You should have received a copy of the GNU General Public License
1003 +# along with this program; if not, write to the Free Software
1004 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1005 +
1006 +Branch Manipulation
1007 +
1008 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1009 +and changing its permission/attribute, there are a lot of works to do.
1010 +
1011 +
1012 +Add a Branch
1013 +----------------------------------------------------------------------
1014 +o Confirm the adding dir exists outside of aufs, including loopback
1015 +  mount.
1016 +- and other various attributes...
1017 +o Initialize the xino file and whiteout bases if necessary.
1018 +  See struct.txt.
1019 +
1020 +o Check the owner/group/mode of the directory
1021 +  When the owner/group/mode of the adding directory differs from the
1022 +  existing branch, aufs issues a warning because it may impose a
1023 +  security risk.
1024 +  For example, when a upper writable branch has a world writable empty
1025 +  top directory, a malicious user can create any files on the writable
1026 +  branch directly, like copy-up and modify manually. If something like
1027 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1028 +  writable branch, and the writable branch is world-writable, then a
1029 +  malicious guy may create /etc/passwd on the writable branch directly
1030 +  and the infected file will be valid in aufs.
1031 +  I am afraid it can be a security issue, but nothing to do except
1032 +  producing a warning.
1033 +
1034 +
1035 +Delete a Branch
1036 +----------------------------------------------------------------------
1037 +o Confirm the deleting branch is not busy
1038 +  To be general, there is one merit to adopt "remount" interface to
1039 +  manipulate branches. It is to discard caches. At deleting a branch,
1040 +  aufs checks the still cached (and connected) dentries and inodes. If
1041 +  there are any, then they are all in-use. An inode without its
1042 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1043 +
1044 +  For the cached one, aufs checks whether the same named entry exists on
1045 +  other branches.
1046 +  If the cached one is a directory, because aufs provides a merged view
1047 +  to users, as long as one dir is left on any branch aufs can show the
1048 +  dir to users. In this case, the branch can be removed from aufs.
1049 +  Otherwise aufs rejects deleting the branch.
1050 +
1051 +  If any file on the deleting branch is opened by aufs, then aufs
1052 +  rejects deleting.
1053 +
1054 +
1055 +Modify the Permission of a Branch
1056 +----------------------------------------------------------------------
1057 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1058 +  See struct.txt.
1059 +
1060 +o rw --> ro: Confirm the modifying branch is not busy
1061 +  Aufs rejects the request if any of these conditions are true.
1062 +  - a file on the branch is mmap-ed.
1063 +  - a regular file on the branch is opened for write and there is no
1064 +    same named entry on the upper branch.
1065 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1066 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1067 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2013-07-06 13:20:47.730197761 +0200
1068 @@ -0,0 +1,65 @@
1069 +
1070 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1071 +# 
1072 +# This program is free software; you can redistribute it and/or modify
1073 +# it under the terms of the GNU General Public License as published by
1074 +# the Free Software Foundation; either version 2 of the License, or
1075 +# (at your option) any later version.
1076 +# 
1077 +# This program is distributed in the hope that it will be useful,
1078 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1079 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1080 +# GNU General Public License for more details.
1081 +# 
1082 +# You should have received a copy of the GNU General Public License
1083 +# along with this program; if not, write to the Free Software
1084 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1085 +
1086 +Policies to Select One among Multiple Writable Branches
1087 +----------------------------------------------------------------------
1088 +When the number of writable branch is more than one, aufs has to decide
1089 +the target branch for file creation or copy-up. By default, the highest
1090 +writable branch which has the parent (or ancestor) dir of the target
1091 +file is chosen (top-down-parent policy).
1092 +By user's request, aufs implements some other policies to select the
1093 +writable branch, for file creation two policies, round-robin and
1094 +most-free-space policies. For copy-up three policies, top-down-parent,
1095 +bottom-up-parent and bottom-up policies.
1096 +
1097 +As expected, the round-robin policy selects the branch in circular. When
1098 +you have two writable branches and creates 10 new files, 5 files will be
1099 +created for each branch. mkdir(2) systemcall is an exception. When you
1100 +create 10 new directories, all will be created on the same branch.
1101 +And the most-free-space policy selects the one which has most free
1102 +space among the writable branches. The amount of free space will be
1103 +checked by aufs internally, and users can specify its time interval.
1104 +
1105 +The policies for copy-up is more simple,
1106 +top-down-parent is equivalent to the same named on in create policy,
1107 +bottom-up-parent selects the writable branch where the parent dir
1108 +exists and the nearest upper one from the copyup-source,
1109 +bottom-up selects the nearest upper writable branch from the
1110 +copyup-source, regardless the existence of the parent dir.
1111 +
1112 +There are some rules or exceptions to apply these policies.
1113 +- If there is a readonly branch above the policy-selected branch and
1114 +  the parent dir is marked as opaque (a variation of whiteout), or the
1115 +  target (creating) file is whiteout-ed on the upper readonly branch,
1116 +  then the result of the policy is ignored and the target file will be
1117 +  created on the nearest upper writable branch than the readonly branch.
1118 +- If there is a writable branch above the policy-selected branch and
1119 +  the parent dir is marked as opaque or the target file is whiteouted
1120 +  on the branch, then the result of the policy is ignored and the target
1121 +  file will be created on the highest one among the upper writable
1122 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1123 +  it as usual.
1124 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1125 +  They try selecting the branch where the source exists as possible
1126 +  since copyup a large file will take long time. If it can't be,
1127 +  ie. the branch where the source exists is readonly, then they will
1128 +  follow the copyup policy.
1129 +- There is an exception for rename(2) when the target exists.
1130 +  If the rename target exists, aufs compares the index of the branches
1131 +  where the source and the target exists and selects the higher
1132 +  one. If the selected branch is readonly, then aufs follows the
1133 +  copyup policy.
1134 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
1135 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
1136 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2013-07-06 13:20:47.730197761 +0200
1137 @@ -0,0 +1,47 @@
1138 +
1139 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1140 +# 
1141 +# This program is free software; you can redistribute it and/or modify
1142 +# it under the terms of the GNU General Public License as published by
1143 +# the Free Software Foundation; either version 2 of the License, or
1144 +# (at your option) any later version.
1145 +# 
1146 +# This program is distributed in the hope that it will be useful,
1147 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1148 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1149 +# GNU General Public License for more details.
1150 +# 
1151 +# You should have received a copy of the GNU General Public License
1152 +# along with this program; if not, write to the Free Software
1153 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1154 +
1155 +mmap(2) -- File Memory Mapping
1156 +----------------------------------------------------------------------
1157 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1158 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1159 +->mmap().
1160 +This approach is simple and good, but there is one problem.
1161 +Under /proc, several entries show the mmap-ped files by its path (with
1162 +device and inode number), and the printed path will be the path on the
1163 +branch fs's instead of virtual aufs's.
1164 +This is not a problem in most cases, but some utilities lsof(1) (and its
1165 +user) may expect the path on aufs.
1166 +
1167 +To address this issue, aufs adds a new member called vm_prfile in struct
1168 +vm_area_struct (and struct vm_region). The original vm_file points to
1169 +the file on the branch fs in order to handle everything correctly as
1170 +usual. The new vm_prfile points to a virtual file in aufs, and the
1171 +show-functions in procfs refers to vm_prfile if it is set.
1172 +Also we need to maintain several other places where touching vm_file
1173 +such like
1174 +- fork()/clone() copies vma and the reference count of vm_file is
1175 +  incremented.
1176 +- merging vma maintains the ref count too.
1177 +
1178 +This is not a good approach. It just faking the printed path. But it
1179 +leaves all behaviour around f_mapping unchanged. This is surely an
1180 +advantage.
1181 +Actually aufs had adopted another complicated approach which calls
1182 +generic_file_mmap() and handles struct vm_operations_struct. In this
1183 +approach, aufs met a hard problem and I could not solve it without
1184 +switching the approach.
1185 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
1186 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
1187 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2013-07-06 13:20:47.736864659 +0200
1188 @@ -0,0 +1,59 @@
1189 +
1190 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1191 +# 
1192 +# This program is free software; you can redistribute it and/or modify
1193 +# it under the terms of the GNU General Public License as published by
1194 +# the Free Software Foundation; either version 2 of the License, or
1195 +# (at your option) any later version.
1196 +# 
1197 +# This program is distributed in the hope that it will be useful,
1198 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1199 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1200 +# GNU General Public License for more details.
1201 +# 
1202 +# You should have received a copy of the GNU General Public License
1203 +# along with this program; if not, write to the Free Software
1204 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1205 +
1206 +Export Aufs via NFS
1207 +----------------------------------------------------------------------
1208 +Here is an approach.
1209 +- like xino/xib, add a new file 'xigen' which stores aufs inode
1210 +  generation.
1211 +- iget_locked(): initialize aufs inode generation for a new inode, and
1212 +  store it in xigen file.
1213 +- destroy_inode(): increment aufs inode generation and store it in xigen
1214 +  file. it is necessary even if it is not unlinked, because any data of
1215 +  inode may be changed by UDBA.
1216 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
1217 +  build file handle by
1218 +  + branch id (4 bytes)
1219 +  + superblock generation (4 bytes)
1220 +  + inode number (4 or 8 bytes)
1221 +  + parent dir inode number (4 or 8 bytes)
1222 +  + inode generation (4 bytes))
1223 +  + return value of exportfs_encode_fh() for the parent on a branch (4
1224 +    bytes)
1225 +  + file handle for a branch (by exportfs_encode_fh())
1226 +- fh_to_dentry():
1227 +  + find the index of a branch from its id in handle, and check it is
1228 +    still exist in aufs.
1229 +  + 1st level: get the inode number from handle and search it in cache.
1230 +  + 2nd level: if not found, get the parent inode number from handle and
1231 +    search it in cache. and then open the parent dir, find the matching
1232 +    inode number by vfs_readdir() and get its name, and call
1233 +    lookup_one_len() for the target dentry.
1234 +  + 3rd level: if the parent dir is not cached, call
1235 +    exportfs_decode_fh() for a branch and get the parent on a branch,
1236 +    build a pathname of it, convert it a pathname in aufs, call
1237 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
1238 +    the 2nd level.
1239 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
1240 +    for every branch, but not itself. to get this, (currently) aufs
1241 +    searches in current->nsproxy->mnt_ns list. it may not be a good
1242 +    idea, but I didn't get other approach.
1243 +  + test the generation of the gotten inode.
1244 +- every inode operation: they may get EBUSY due to UDBA. in this case,
1245 +  convert it into ESTALE for NFSD.
1246 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
1247 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
1248 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
1249 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
1250 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2013-07-06 13:20:47.736864659 +0200
1251 @@ -0,0 +1,53 @@
1252 +
1253 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1254 +# 
1255 +# This program is free software; you can redistribute it and/or modify
1256 +# it under the terms of the GNU General Public License as published by
1257 +# the Free Software Foundation; either version 2 of the License, or
1258 +# (at your option) any later version.
1259 +# 
1260 +# This program is distributed in the hope that it will be useful,
1261 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1262 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1263 +# GNU General Public License for more details.
1264 +# 
1265 +# You should have received a copy of the GNU General Public License
1266 +# along with this program; if not, write to the Free Software
1267 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1268 +
1269 +Show Whiteout Mode (shwh)
1270 +----------------------------------------------------------------------
1271 +Generally aufs hides the name of whiteouts. But in some cases, to show
1272 +them is very useful for users. For instance, creating a new middle layer
1273 +(branch) by merging existing layers.
1274 +
1275 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
1276 +When you have three branches,
1277 +- Bottom: 'system', squashfs (underlying base system), read-only
1278 +- Middle: 'mods', squashfs, read-only
1279 +- Top: 'overlay', ram (tmpfs), read-write
1280 +
1281 +The top layer is loaded at boot time and saved at shutdown, to preserve
1282 +the changes made to the system during the session.
1283 +When larger changes have been made, or smaller changes have accumulated,
1284 +the size of the saved top layer data grows. At this point, it would be
1285 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
1286 +and rewrite the 'mods' squashfs, clearing the top layer and thus
1287 +restoring save and load speed.
1288 +
1289 +This merging is simplified by the use of another aufs mount, of just the
1290 +two overlay branches using the 'shwh' option.
1291 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
1292 +       aufs /livesys/merge_union
1293 +
1294 +A merged view of these two branches is then available at
1295 +/livesys/merge_union, and the new feature is that the whiteouts are
1296 +visible!
1297 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
1298 +writing to all branches. Also the default mode for all branches is 'ro'.
1299 +It is now possible to save the combined contents of the two overlay
1300 +branches to a new squashfs, e.g.:
1301 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
1302 +
1303 +This new squashfs archive can be stored on the boot device and the
1304 +initramfs will use it to replace the old one at the next boot.
1305 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
1306 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
1307 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2013-07-06 13:20:47.736864659 +0200
1308 @@ -0,0 +1,47 @@
1309 +
1310 +# Copyright (C) 2010-2013 Junjiro R. Okajima
1311 +#
1312 +# This program is free software; you can redistribute it and/or modify
1313 +# it under the terms of the GNU General Public License as published by
1314 +# the Free Software Foundation; either version 2 of the License, or
1315 +# (at your option) any later version.
1316 +#
1317 +# This program is distributed in the hope that it will be useful,
1318 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1319 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1320 +# GNU General Public License for more details.
1321 +#
1322 +# You should have received a copy of the GNU General Public License
1323 +# along with this program; if not, write to the Free Software
1324 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1325 +
1326 +Dynamically customizable FS operations
1327 +----------------------------------------------------------------------
1328 +Generally FS operations (struct inode_operations, struct
1329 +address_space_operations, struct file_operations, etc.) are defined as
1330 +"static const", but it never means that FS have only one set of
1331 +operation. Some FS have multiple sets of them. For instance, ext2 has
1332 +three sets, one for XIP, for NOBH, and for normal.
1333 +Since aufs overrides and redirects these operations, sometimes aufs has
1334 +to change its behaviour according to the branch FS type. More imporantly
1335 +VFS acts differently if a function (member in the struct) is set or
1336 +not. It means aufs should have several sets of operations and select one
1337 +among them according to the branch FS definition.
1338 +
1339 +In order to solve this problem and not to affect the behavour of VFS,
1340 +aufs defines these operations dynamically. For instance, aufs defines
1341 +aio_read function for struct file_operations, but it may not be set to
1342 +the file_operations. When the branch FS doesn't have it, aufs doesn't
1343 +set it to its file_operations while the function definition itself is
1344 +still alive. So the behaviour of io_submit(2) will not change, and it
1345 +will return an error when aio_read is not defined.
1346 +
1347 +The lifetime of these dynamically generated operation object is
1348 +maintained by aufs branch object. When the branch is removed from aufs,
1349 +the reference counter of the object is decremented. When it reaches
1350 +zero, the dynamically generated operation object will be freed.
1351 +
1352 +This approach is designed to support AIO (io_submit), Direcit I/O and
1353 +XIP mainly.
1354 +Currently this approach is applied to file_operations and
1355 +vm_operations_struct for regular files only.
1356 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt linux/Documentation/filesystems/aufs/design/99plan.txt
1357 --- /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt   1970-01-01 01:00:00.000000000 +0100
1358 +++ linux/Documentation/filesystems/aufs/design/99plan.txt      2013-07-06 13:20:47.736864659 +0200
1359 @@ -0,0 +1,96 @@
1360 +
1361 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1362 +# 
1363 +# This program is free software; you can redistribute it and/or modify
1364 +# it under the terms of the GNU General Public License as published by
1365 +# the Free Software Foundation; either version 2 of the License, or
1366 +# (at your option) any later version.
1367 +# 
1368 +# This program is distributed in the hope that it will be useful,
1369 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1370 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1371 +# GNU General Public License for more details.
1372 +# 
1373 +# You should have received a copy of the GNU General Public License
1374 +# along with this program; if not, write to the Free Software
1375 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1376 +
1377 +Plan
1378 +
1379 +Restoring some features which was implemented in aufs1.
1380 +They were dropped in aufs2 in order to make source files simpler and
1381 +easier to be reviewed.
1382 +
1383 +
1384 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1385 +----------------------------------------------------------------------
1386 +Let's try case study.
1387 +- aufs has two branches, upper readwrite and lower readonly.
1388 +  /au = /rw + /ro
1389 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1390 +- user invoked "chmod a+rx /au/dirA"
1391 +- then "dirA" becomes world readable?
1392 +
1393 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1394 +or it may be a natively readonly filesystem. If aufs respects the lower
1395 +branch, it should not respond readdir request from other users. But user
1396 +allowed it by chmod. Should really aufs rejects showing the entries
1397 +under /ro/dirA?
1398 +
1399 +To be honest, I don't have a best solution for this case. So I
1400 +implemented 'dirperm1' and 'nodirperm1' option in aufs1, and leave it to
1401 +users.
1402 +When dirperm1 is specified, aufs checks only the highest one for the
1403 +directory permission, and shows the entries. Otherwise, as usual, checks
1404 +every dir existing on all branches and rejects the request.
1405 +
1406 +As a side effect, dirperm1 option improves the performance of aufs
1407 +because the number of permission check is reduced.
1408 +
1409 +
1410 +Being Another Aufs's Readonly Branch (robr)
1411 +----------------------------------------------------------------------
1412 +Aufs1 allows aufs to be another aufs's readonly branch.
1413 +This feature was developed by a user's request. But it may not be used
1414 +currecnly.
1415 +
1416 +
1417 +Copy-up on Open (coo=)
1418 +----------------------------------------------------------------------
1419 +By default the internal copy-up is executed when it is really necessary.
1420 +It is not done when a file is opened for writing, but when write(2) is
1421 +done. Users who have many (over 100) branches want to know and analyse
1422 +when and what file is copied-up. To insert a new upper branch which
1423 +contains such files only may improve the performance of aufs.
1424 +
1425 +Aufs1 implemented "coo=none | leaf | all" option.
1426 +
1427 +
1428 +Refresh the Opened File (refrof)
1429 +----------------------------------------------------------------------
1430 +This option is implemented in aufs1 but incomplete.
1431 +
1432 +When user reads from a file, he expects to get its latest filedata
1433 +generally. If the file is removed and a new same named file is created,
1434 +the content he gets is unchanged, ie. the unlinked filedata.
1435 +
1436 +Let's try case study again.
1437 +- aufs has two branches.
1438 +  /au = /rw + /ro
1439 +- "fileA" exists under /ro, but /rw.
1440 +- user opened "/au/fileA".
1441 +- he or someone else inserts a branch (/new) between /rw and /ro.
1442 +  /au = /rw + /new + /ro
1443 +- the new branch has "fileA".
1444 +- user reads from the opened "fileA"
1445 +- which filedata should aufs return, from /ro or /new?
1446 +
1447 +Some people says it has to be "from /ro" and it is a semantics of Unix.
1448 +The others say it should be "from /new" because the file is not removed
1449 +and it is equivalent to the case of someone else modifies the file.
1450 +
1451 +Here again I don't have a best and final answer. I got an idea to
1452 +implement 'refrof' and 'norefrof' option. When 'refrof' (REFResh the
1453 +Opened File) is specified (by default), aufs returns the filedata from
1454 +/new.
1455 +Otherwise from /new.
1456 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
1457 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
1458 +++ linux/Documentation/filesystems/aufs/README 2013-07-30 22:42:46.229279157 +0200
1459 @@ -0,0 +1,346 @@
1460 +
1461 +Aufs3 -- advanced multi layered unification filesystem version 3.x
1462 +http://aufs.sf.net
1463 +Junjiro R. Okajima
1464 +
1465 +
1466 +0. Introduction
1467 +----------------------------------------
1468 +In the early days, aufs was entirely re-designed and re-implemented
1469 +Unionfs Version 1.x series. After many original ideas, approaches,
1470 +improvements and implementations, it becomes totally different from
1471 +Unionfs while keeping the basic features.
1472 +Recently, Unionfs Version 2.x series begin taking some of the same
1473 +approaches to aufs1's.
1474 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
1475 +University and his team.
1476 +
1477 +Aufs3 supports linux-3.0 and later.
1478 +If you want older kernel version support, try aufs2-2.6.git or
1479 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
1480 +
1481 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
1482 +According to Christoph Hellwig, linux rejects all union-type filesystems
1483 +but UnionMount.
1484 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
1485 +
1486 +
1487 +1. Features
1488 +----------------------------------------
1489 +- unite several directories into a single virtual filesystem. The member
1490 +  directory is called as a branch.
1491 +- you can specify the permission flags to the branch, which are 'readonly',
1492 +  'readwrite' and 'whiteout-able.'
1493 +- by upper writable branch, internal copyup and whiteout, files/dirs on
1494 +  readonly branch are modifiable logically.
1495 +- dynamic branch manipulation, add, del.
1496 +- etc...
1497 +
1498 +Also there are many enhancements in aufs1, such as:
1499 +- readdir(3) in userspace.
1500 +- keep inode number by external inode number table
1501 +- keep the timestamps of file/dir in internal copyup operation
1502 +- seekable directory, supporting NFS readdir.
1503 +- whiteout is hardlinked in order to reduce the consumption of inodes
1504 +  on branch
1505 +- do not copyup, nor create a whiteout when it is unnecessary
1506 +- revert a single systemcall when an error occurs in aufs
1507 +- remount interface instead of ioctl
1508 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
1509 +- loopback mounted filesystem as a branch
1510 +- kernel thread for removing the dir who has a plenty of whiteouts
1511 +- support copyup sparse file (a file which has a 'hole' in it)
1512 +- default permission flags for branches
1513 +- selectable permission flags for ro branch, whether whiteout can
1514 +  exist or not
1515 +- export via NFS.
1516 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
1517 +- support multiple writable branches, some policies to select one
1518 +  among multiple writable branches.
1519 +- a new semantics for link(2) and rename(2) to support multiple
1520 +  writable branches.
1521 +- no glibc changes are required.
1522 +- pseudo hardlink (hardlink over branches)
1523 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
1524 +  including NFS or remote filesystem branch.
1525 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
1526 +- and more...
1527 +
1528 +Currently these features are dropped temporary from aufs3.
1529 +See design/08plan.txt in detail.
1530 +- test only the highest one for the directory permission (dirperm1)
1531 +- copyup on open (coo=)
1532 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
1533 +  (robr)
1534 +- statistics of aufs thread (/sys/fs/aufs/stat)
1535 +- delegation mode (dlgt)
1536 +  a delegation of the internal branch access to support task I/O
1537 +  accounting, which also supports Linux Security Modules (LSM) mainly
1538 +  for Suse AppArmor.
1539 +- intent.open/create (file open in a single lookup)
1540 +
1541 +Features or just an idea in the future (see also design/*.txt),
1542 +- reorder the branch index without del/re-add.
1543 +- permanent xino files for NFSD
1544 +- an option for refreshing the opened files after add/del branches
1545 +- 'move' policy for copy-up between two writable branches, after
1546 +  checking free space.
1547 +- light version, without branch manipulation. (unnecessary?)
1548 +- copyup in userspace
1549 +- inotify in userspace
1550 +- readv/writev
1551 +- xattr, acl
1552 +
1553 +
1554 +2. Download
1555 +----------------------------------------
1556 +There were three GIT trees for aufs3, aufs3-linux.git,
1557 +aufs3-standalone.git, and aufs-util.git. Note that there is no "3" in
1558 +"aufs-util.git."
1559 +While the aufs-util is always necessary, you need either of aufs3-linux
1560 +or aufs3-standalone.
1561 +
1562 +The aufs3-linux tree includes the whole linux mainline GIT tree,
1563 +git://git.kernel.org/.../torvalds/linux.git.
1564 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
1565 +build aufs3 as an external kernel module.
1566 +
1567 +On the other hand, the aufs3-standalone tree has only aufs source files
1568 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
1569 +
1570 +You will find GIT branches whose name is in form of "aufs3.x" where "x"
1571 +represents the linux kernel version, "linux-3.x". For instance,
1572 +"aufs3.0" is for linux-3.0. For latest "linux-3.x-rcN", use
1573 +"aufs3.x-rcN" branch.
1574 +
1575 +o aufs3-linux tree
1576 +$ git clone --reference /your/linux/git/tree \
1577 +       git://git.code.sf.net/p/aufs/aufs3-linux aufs-aufs3-linux \
1578 +       aufs3-linux.git
1579 +- if you don't have linux GIT tree, then remove "--reference ..."
1580 +$ cd aufs3-linux.git
1581 +$ git checkout origin/aufs3.0
1582 +
1583 +o aufs3-standalone tree
1584 +$ git clone git://git.code.sf.net/p/aufs/aufs3-standalone \
1585 +       aufs3-standalone.git
1586 +$ cd aufs3-standalone.git
1587 +$ git checkout origin/aufs3.0
1588 +
1589 +o aufs-util tree
1590 +$ git clone git://git.code.sf.net/p/aufs/aufs-util \
1591 +       aufs-util.git
1592 +$ cd aufs-util.git
1593 +$ git checkout origin/aufs3.0
1594 +
1595 +Note: The 3.x-rcN branch is to be used with `rc' kernel versions ONLY.
1596 +The minor version number, 'x' in '3.x', of aufs may not always
1597 +follow the minor version number of the kernel.
1598 +Because changes in the kernel that cause the use of a new
1599 +minor version number do not always require changes to aufs-util.
1600 +
1601 +Since aufs-util has its own minor version number, you may not be
1602 +able to find a GIT branch in aufs-util for your kernel's
1603 +exact minor version number.
1604 +In this case, you should git-checkout the branch for the
1605 +nearest lower number.
1606 +
1607 +For (an unreleased) example:
1608 +If you are using "linux-3.10" and the "aufs3.10" branch
1609 +does not exist in aufs-util repository, then "aufs3.9", "aufs3.8"
1610 +or something numerically smaller is the branch for your kernel.
1611 +
1612 +Also you can view all branches by
1613 +       $ git branch -a
1614 +
1615 +
1616 +3. Configuration and Compilation
1617 +----------------------------------------
1618 +Make sure you have git-checkout'ed the correct branch.
1619 +
1620 +For aufs3-linux tree,
1621 +- enable CONFIG_AUFS_FS.
1622 +- set other aufs configurations if necessary.
1623 +
1624 +For aufs3-standalone tree,
1625 +There are several ways to build.
1626 +
1627 +1.
1628 +- apply ./aufs3-kbuild.patch to your kernel source files.
1629 +- apply ./aufs3-base.patch too.
1630 +- apply ./aufs3-proc_map.patch too, if you want to make /proc/PID/maps (and
1631 +  others including lsof(1)) show the file path on aufs instead of the
1632 +  path on the branch fs.
1633 +- apply ./aufs3-standalone.patch too, if you have a plan to set
1634 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs3-standalone.patch.
1635 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
1636 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
1637 +- enable CONFIG_AUFS_FS, you can select either
1638 +  =m or =y.
1639 +- and build your kernel as usual.
1640 +- install the built kernel.
1641 +  Note: Since linux-3.9, every filesystem module requires an alias
1642 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
1643 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
1644 +- install the header files too by "make headers_install" to the
1645 +  directory where you specify. By default, it is $PWD/usr.
1646 +  "make help" shows a brief note for headers_install.
1647 +- and reboot your system.
1648 +
1649 +2.
1650 +- module only (CONFIG_AUFS_FS=m).
1651 +- apply ./aufs3-base.patch to your kernel source files.
1652 +- apply ./aufs3-proc_map.patch too to your kernel source files,
1653 +  if you want to make /proc/PID/maps (and others including lsof(1)) show
1654 +  the file path on aufs instead of the path on the branch fs.
1655 +- apply ./aufs3-standalone.patch too.
1656 +- build your kernel, don't forget "make headers_install", and reboot.
1657 +- edit ./config.mk and set other aufs configurations if necessary.
1658 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
1659 +  every aufs configurations.
1660 +- build the module by simple "make".
1661 +  Note: Since linux-3.9, every filesystem module requires an alias
1662 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
1663 +  modules.aliases file.
1664 +- you can specify ${KDIR} make variable which points to your kernel
1665 +  source tree.
1666 +- install the files
1667 +  + run "make install" to install the aufs module, or copy the built
1668 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
1669 +  + run "make install_headers" (instead of headers_install) to install
1670 +    the modified aufs header file (you can specify DESTDIR which is
1671 +    available in aufs standalone version's Makefile only), or copy
1672 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
1673 +    you like manually. By default, the target directory is $PWD/usr.
1674 +- no need to apply aufs3-kbuild.patch, nor copying source files to your
1675 +  kernel source tree.
1676 +
1677 +Note: The header file aufs_type.h is necessary to build aufs-util
1678 +      as well as "make headers_install" in the kernel source tree.
1679 +      headers_install is subject to be forgotten, but it is essentially
1680 +      necessary, not only for building aufs-util.
1681 +      You may not meet problems without headers_install in some older
1682 +      version though.
1683 +
1684 +And then,
1685 +- read README in aufs-util, build and install it
1686 +- note that your distribution may contain an obsoleted version of
1687 +  aufs_type.h in /usr/include/linux or something. When you build aufs
1688 +  utilities, make sure that your compiler refers the correct aufs header
1689 +  file which is built by "make headers_install."
1690 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
1691 +  then run "make install_ulib" too. And refer to the aufs manual in
1692 +  detail.
1693 +
1694 +
1695 +4. Usage
1696 +----------------------------------------
1697 +At first, make sure aufs-util are installed, and please read the aufs
1698 +manual, aufs.5 in aufs-util.git tree.
1699 +$ man -l aufs.5
1700 +
1701 +And then,
1702 +$ mkdir /tmp/rw /tmp/aufs
1703 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
1704 +
1705 +Here is another example. The result is equivalent.
1706 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
1707 +  Or
1708 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
1709 +# mount -o remount,append:${HOME} /tmp/aufs
1710 +
1711 +Then, you can see whole tree of your home dir through /tmp/aufs. If
1712 +you modify a file under /tmp/aufs, the one on your home directory is
1713 +not affected, instead the same named file will be newly created under
1714 +/tmp/rw. And all of your modification to a file will be applied to
1715 +the one under /tmp/rw. This is called the file based Copy on Write
1716 +(COW) method.
1717 +Aufs mount options are described in aufs.5.
1718 +If you run chroot or something and make your aufs as a root directory,
1719 +then you need to customize the shutdown script. See the aufs manual in
1720 +detail.
1721 +
1722 +Additionally, there are some sample usages of aufs which are a
1723 +diskless system with network booting, and LiveCD over NFS.
1724 +See sample dir in CVS tree on SourceForge.
1725 +
1726 +
1727 +5. Contact
1728 +----------------------------------------
1729 +When you have any problems or strange behaviour in aufs, please let me
1730 +know with:
1731 +- /proc/mounts (instead of the output of mount(8))
1732 +- /sys/module/aufs/*
1733 +- /sys/fs/aufs/* (if you have them)
1734 +- /debug/aufs/* (if you have them)
1735 +- linux kernel version
1736 +  if your kernel is not plain, for example modified by distributor,
1737 +  the url where i can download its source is necessary too.
1738 +- aufs version which was printed at loading the module or booting the
1739 +  system, instead of the date you downloaded.
1740 +- configuration (define/undefine CONFIG_AUFS_xxx)
1741 +- kernel configuration or /proc/config.gz (if you have it)
1742 +- behaviour which you think to be incorrect
1743 +- actual operation, reproducible one is better
1744 +- mailto: aufs-users at lists.sourceforge.net
1745 +
1746 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
1747 +and Feature Requests) on SourceForge. Please join and write to
1748 +aufs-users ML.
1749 +
1750 +
1751 +6. Acknowledgements
1752 +----------------------------------------
1753 +Thanks to everyone who have tried and are using aufs, whoever
1754 +have reported a bug or any feedback.
1755 +
1756 +Especially donators:
1757 +Tomas Matejicek(slax.org) made a donation (much more than once).
1758 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
1759 +       scripts) is making "doubling" donations.
1760 +       Unfortunately I cannot list all of the donators, but I really
1761 +       appreciate.
1762 +       It ends Aug 2010, but the ordinary donation URL is still available.
1763 +       <http://sourceforge.net/donate/index.php?group_id=167503>
1764 +Dai Itasaka made a donation (2007/8).
1765 +Chuck Smith made a donation (2008/4, 10 and 12).
1766 +Henk Schoneveld made a donation (2008/9).
1767 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
1768 +Francois Dupoux made a donation (2008/11).
1769 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
1770 +       aufs2 GIT tree (2009/2).
1771 +William Grant made a donation (2009/3).
1772 +Patrick Lane made a donation (2009/4).
1773 +The Mail Archive (mail-archive.com) made donations (2009/5).
1774 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
1775 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
1776 +Pavel Pronskiy made a donation (2011/2).
1777 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
1778 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
1779 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
1780 +11).
1781 +Sam Liddicott made a donation (2011/9).
1782 +Era Scarecrow made a donation (2013/4).
1783 +Bor Ratajc made a donation (2013/4).
1784 +Alessandro Gorreta made a donation (2013/4).
1785 +POIRETTE Marc made a donation (2013/4).
1786 +Alessandro Gorreta made a donation (2013/4).
1787 +lauri kasvandik made a donation (2013/5).
1788 +pemasu from Finland made a donation (2013/7).
1789 +
1790 +Thank you very much.
1791 +Donations are always, including future donations, very important and
1792 +helpful for me to keep on developing aufs.
1793 +
1794 +
1795 +7.
1796 +----------------------------------------
1797 +If you are an experienced user, no explanation is needed. Aufs is
1798 +just a linux filesystem.
1799 +
1800 +
1801 +Enjoy!
1802 +
1803 +# Local variables: ;
1804 +# mode: text;
1805 +# End: ;
1806 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
1807 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
1808 +++ linux/fs/aufs/aufs.h        2013-07-06 13:20:47.736864659 +0200
1809 @@ -0,0 +1,60 @@
1810 +/*
1811 + * Copyright (C) 2005-2013 Junjiro R. Okajima
1812 + *
1813 + * This program, aufs is free software; you can redistribute it and/or modify
1814 + * it under the terms of the GNU General Public License as published by
1815 + * the Free Software Foundation; either version 2 of the License, or
1816 + * (at your option) any later version.
1817 + *
1818 + * This program is distributed in the hope that it will be useful,
1819 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1820 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1821 + * GNU General Public License for more details.
1822 + *
1823 + * You should have received a copy of the GNU General Public License
1824 + * along with this program; if not, write to the Free Software
1825 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1826 + */
1827 +
1828 +/*
1829 + * all header files
1830 + */
1831 +
1832 +#ifndef __AUFS_H__
1833 +#define __AUFS_H__
1834 +
1835 +#ifdef __KERNEL__
1836 +
1837 +#define AuStub(type, name, body, ...) \
1838 +       static inline type name(__VA_ARGS__) { body; }
1839 +
1840 +#define AuStubVoid(name, ...) \
1841 +       AuStub(void, name, , __VA_ARGS__)
1842 +#define AuStubInt0(name, ...) \
1843 +       AuStub(int, name, return 0, __VA_ARGS__)
1844 +
1845 +#include "debug.h"
1846 +
1847 +#include "branch.h"
1848 +#include "cpup.h"
1849 +#include "dcsub.h"
1850 +#include "dbgaufs.h"
1851 +#include "dentry.h"
1852 +#include "dir.h"
1853 +#include "dynop.h"
1854 +#include "file.h"
1855 +#include "fstype.h"
1856 +#include "inode.h"
1857 +#include "loop.h"
1858 +#include "module.h"
1859 +#include "opts.h"
1860 +#include "rwsem.h"
1861 +#include "spl.h"
1862 +#include "super.h"
1863 +#include "sysaufs.h"
1864 +#include "vfsub.h"
1865 +#include "whout.h"
1866 +#include "wkq.h"
1867 +
1868 +#endif /* __KERNEL__ */
1869 +#endif /* __AUFS_H__ */
1870 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
1871 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
1872 +++ linux/fs/aufs/branch.c      2013-07-30 22:42:55.839613269 +0200
1873 @@ -0,0 +1,1222 @@
1874 +/*
1875 + * Copyright (C) 2005-2013 Junjiro R. Okajima
1876 + *
1877 + * This program, aufs is free software; you can redistribute it and/or modify
1878 + * it under the terms of the GNU General Public License as published by
1879 + * the Free Software Foundation; either version 2 of the License, or
1880 + * (at your option) any later version.
1881 + *
1882 + * This program is distributed in the hope that it will be useful,
1883 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1884 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1885 + * GNU General Public License for more details.
1886 + *
1887 + * You should have received a copy of the GNU General Public License
1888 + * along with this program; if not, write to the Free Software
1889 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1890 + */
1891 +
1892 +/*
1893 + * branch management
1894 + */
1895 +
1896 +#include <linux/compat.h>
1897 +#include <linux/statfs.h>
1898 +#include "aufs.h"
1899 +
1900 +/*
1901 + * free a single branch
1902 + */
1903 +
1904 +/* prohibit rmdir to the root of the branch */
1905 +/* todo: another new flag? */
1906 +static void au_br_dflags_force(struct au_branch *br)
1907 +{
1908 +       struct dentry *h_dentry;
1909 +
1910 +       h_dentry = au_br_dentry(br);
1911 +       spin_lock(&h_dentry->d_lock);
1912 +       br->br_dflags = h_dentry->d_flags & DCACHE_MOUNTED;
1913 +       h_dentry->d_flags |= DCACHE_MOUNTED;
1914 +       spin_unlock(&h_dentry->d_lock);
1915 +}
1916 +
1917 +/* restore its d_flags */
1918 +static void au_br_dflags_restore(struct au_branch *br)
1919 +{
1920 +       struct dentry *h_dentry;
1921 +
1922 +       if (br->br_dflags)
1923 +               return;
1924 +
1925 +       h_dentry = au_br_dentry(br);
1926 +       spin_lock(&h_dentry->d_lock);
1927 +       h_dentry->d_flags &= ~DCACHE_MOUNTED;
1928 +       spin_unlock(&h_dentry->d_lock);
1929 +}
1930 +
1931 +static void au_br_do_free(struct au_branch *br)
1932 +{
1933 +       int i;
1934 +       struct au_wbr *wbr;
1935 +       struct au_dykey **key;
1936 +
1937 +       au_hnotify_fin_br(br);
1938 +
1939 +       if (br->br_xino.xi_file)
1940 +               fput(br->br_xino.xi_file);
1941 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
1942 +
1943 +       AuDebugOn(atomic_read(&br->br_count));
1944 +
1945 +       wbr = br->br_wbr;
1946 +       if (wbr) {
1947 +               for (i = 0; i < AuBrWh_Last; i++)
1948 +                       dput(wbr->wbr_wh[i]);
1949 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
1950 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
1951 +       }
1952 +
1953 +       key = br->br_dykey;
1954 +       for (i = 0; i < AuBrDynOp; i++, key++)
1955 +               if (*key)
1956 +                       au_dy_put(*key);
1957 +               else
1958 +                       break;
1959 +
1960 +       au_br_dflags_restore(br);
1961 +
1962 +       /* recursive lock, s_umount of branch's */
1963 +       lockdep_off();
1964 +       path_put(&br->br_path);
1965 +       lockdep_on();
1966 +       kfree(wbr);
1967 +       kfree(br);
1968 +}
1969 +
1970 +/*
1971 + * frees all branches
1972 + */
1973 +void au_br_free(struct au_sbinfo *sbinfo)
1974 +{
1975 +       aufs_bindex_t bmax;
1976 +       struct au_branch **br;
1977 +
1978 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
1979 +
1980 +       bmax = sbinfo->si_bend + 1;
1981 +       br = sbinfo->si_branch;
1982 +       while (bmax--)
1983 +               au_br_do_free(*br++);
1984 +}
1985 +
1986 +/*
1987 + * find the index of a branch which is specified by @br_id.
1988 + */
1989 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
1990 +{
1991 +       aufs_bindex_t bindex, bend;
1992 +
1993 +       bend = au_sbend(sb);
1994 +       for (bindex = 0; bindex <= bend; bindex++)
1995 +               if (au_sbr_id(sb, bindex) == br_id)
1996 +                       return bindex;
1997 +       return -1;
1998 +}
1999 +
2000 +/* ---------------------------------------------------------------------- */
2001 +
2002 +/*
2003 + * add a branch
2004 + */
2005 +
2006 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2007 +                       struct dentry *h_root)
2008 +{
2009 +       if (unlikely(h_adding == h_root
2010 +                    || au_test_loopback_overlap(sb, h_adding)))
2011 +               return 1;
2012 +       if (h_adding->d_sb != h_root->d_sb)
2013 +               return 0;
2014 +       return au_test_subdir(h_adding, h_root)
2015 +               || au_test_subdir(h_root, h_adding);
2016 +}
2017 +
2018 +/*
2019 + * returns a newly allocated branch. @new_nbranch is a number of branches
2020 + * after adding a branch.
2021 + */
2022 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2023 +                                    int perm)
2024 +{
2025 +       struct au_branch *add_branch;
2026 +       struct dentry *root;
2027 +       int err;
2028 +
2029 +       err = -ENOMEM;
2030 +       root = sb->s_root;
2031 +       add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
2032 +       if (unlikely(!add_branch))
2033 +               goto out;
2034 +
2035 +       err = au_hnotify_init_br(add_branch, perm);
2036 +       if (unlikely(err))
2037 +               goto out_br;
2038 +
2039 +       add_branch->br_wbr = NULL;
2040 +       if (au_br_writable(perm)) {
2041 +               /* may be freed separately at changing the branch permission */
2042 +               add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
2043 +                                            GFP_NOFS);
2044 +               if (unlikely(!add_branch->br_wbr))
2045 +                       goto out_hnotify;
2046 +       }
2047 +
2048 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
2049 +       if (!err)
2050 +               err = au_di_realloc(au_di(root), new_nbranch);
2051 +       if (!err)
2052 +               err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
2053 +       if (!err)
2054 +               return add_branch; /* success */
2055 +
2056 +       kfree(add_branch->br_wbr);
2057 +
2058 +out_hnotify:
2059 +       au_hnotify_fin_br(add_branch);
2060 +out_br:
2061 +       kfree(add_branch);
2062 +out:
2063 +       return ERR_PTR(err);
2064 +}
2065 +
2066 +/*
2067 + * test if the branch permission is legal or not.
2068 + */
2069 +static int test_br(struct inode *inode, int brperm, char *path)
2070 +{
2071 +       int err;
2072 +
2073 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
2074 +       if (!err)
2075 +               goto out;
2076 +
2077 +       err = -EINVAL;
2078 +       pr_err("write permission for readonly mount or inode, %s\n", path);
2079 +
2080 +out:
2081 +       return err;
2082 +}
2083 +
2084 +/*
2085 + * returns:
2086 + * 0: success, the caller will add it
2087 + * plus: success, it is already unified, the caller should ignore it
2088 + * minus: error
2089 + */
2090 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
2091 +{
2092 +       int err;
2093 +       aufs_bindex_t bend, bindex;
2094 +       struct dentry *root;
2095 +       struct inode *inode, *h_inode;
2096 +
2097 +       root = sb->s_root;
2098 +       bend = au_sbend(sb);
2099 +       if (unlikely(bend >= 0
2100 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
2101 +               err = 1;
2102 +               if (!remount) {
2103 +                       err = -EINVAL;
2104 +                       pr_err("%s duplicated\n", add->pathname);
2105 +               }
2106 +               goto out;
2107 +       }
2108 +
2109 +       err = -ENOSPC; /* -E2BIG; */
2110 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2111 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
2112 +               pr_err("number of branches exceeded %s\n", add->pathname);
2113 +               goto out;
2114 +       }
2115 +
2116 +       err = -EDOM;
2117 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
2118 +               pr_err("bad index %d\n", add->bindex);
2119 +               goto out;
2120 +       }
2121 +
2122 +       inode = add->path.dentry->d_inode;
2123 +       err = -ENOENT;
2124 +       if (unlikely(!inode->i_nlink)) {
2125 +               pr_err("no existence %s\n", add->pathname);
2126 +               goto out;
2127 +       }
2128 +
2129 +       err = -EINVAL;
2130 +       if (unlikely(inode->i_sb == sb)) {
2131 +               pr_err("%s must be outside\n", add->pathname);
2132 +               goto out;
2133 +       }
2134 +
2135 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
2136 +               pr_err("unsupported filesystem, %s (%s)\n",
2137 +                      add->pathname, au_sbtype(inode->i_sb));
2138 +               goto out;
2139 +       }
2140 +
2141 +       err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
2142 +       if (unlikely(err))
2143 +               goto out;
2144 +
2145 +       if (bend < 0)
2146 +               return 0; /* success */
2147 +
2148 +       err = -EINVAL;
2149 +       for (bindex = 0; bindex <= bend; bindex++)
2150 +               if (unlikely(test_overlap(sb, add->path.dentry,
2151 +                                         au_h_dptr(root, bindex)))) {
2152 +                       pr_err("%s is overlapped\n", add->pathname);
2153 +                       goto out;
2154 +               }
2155 +
2156 +       err = 0;
2157 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
2158 +               h_inode = au_h_dptr(root, 0)->d_inode;
2159 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
2160 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
2161 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
2162 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
2163 +                               add->pathname,
2164 +                               i_uid_read(inode), i_gid_read(inode),
2165 +                               (inode->i_mode & S_IALLUGO),
2166 +                               i_uid_read(h_inode), i_gid_read(h_inode),
2167 +                               (h_inode->i_mode & S_IALLUGO));
2168 +       }
2169 +
2170 +out:
2171 +       return err;
2172 +}
2173 +
2174 +/*
2175 + * initialize or clean the whiteouts for an adding branch
2176 + */
2177 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
2178 +                        int new_perm)
2179 +{
2180 +       int err, old_perm;
2181 +       aufs_bindex_t bindex;
2182 +       struct mutex *h_mtx;
2183 +       struct au_wbr *wbr;
2184 +       struct au_hinode *hdir;
2185 +
2186 +       err = vfsub_mnt_want_write(au_br_mnt(br));
2187 +       if (unlikely(err))
2188 +               goto out;
2189 +
2190 +       wbr = br->br_wbr;
2191 +       old_perm = br->br_perm;
2192 +       br->br_perm = new_perm;
2193 +       hdir = NULL;
2194 +       h_mtx = NULL;
2195 +       bindex = au_br_index(sb, br->br_id);
2196 +       if (0 <= bindex) {
2197 +               hdir = au_hi(sb->s_root->d_inode, bindex);
2198 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
2199 +       } else {
2200 +               h_mtx = &au_br_dentry(br)->d_inode->i_mutex;
2201 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
2202 +       }
2203 +       if (!wbr)
2204 +               err = au_wh_init(br, sb);
2205 +       else {
2206 +               wbr_wh_write_lock(wbr);
2207 +               err = au_wh_init(br, sb);
2208 +               wbr_wh_write_unlock(wbr);
2209 +       }
2210 +       if (hdir)
2211 +               au_hn_imtx_unlock(hdir);
2212 +       else
2213 +               mutex_unlock(h_mtx);
2214 +       vfsub_mnt_drop_write(au_br_mnt(br));
2215 +       br->br_perm = old_perm;
2216 +
2217 +       if (!err && wbr && !au_br_writable(new_perm)) {
2218 +               kfree(wbr);
2219 +               br->br_wbr = NULL;
2220 +       }
2221 +
2222 +out:
2223 +       return err;
2224 +}
2225 +
2226 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
2227 +                      int perm)
2228 +{
2229 +       int err;
2230 +       struct kstatfs kst;
2231 +       struct au_wbr *wbr;
2232 +
2233 +       wbr = br->br_wbr;
2234 +       au_rw_init(&wbr->wbr_wh_rwsem);
2235 +       memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
2236 +       atomic_set(&wbr->wbr_wh_running, 0);
2237 +       wbr->wbr_bytes = 0;
2238 +
2239 +       /*
2240 +        * a limit for rmdir/rename a dir
2241 +        * cf. AUFS_MAX_NAMELEN in include/linux/aufs_type.h
2242 +        */
2243 +       err = vfs_statfs(&br->br_path, &kst);
2244 +       if (unlikely(err))
2245 +               goto out;
2246 +       err = -EINVAL;
2247 +       if (kst.f_namelen >= NAME_MAX)
2248 +               err = au_br_init_wh(sb, br, perm);
2249 +       else
2250 +               pr_err("%.*s(%s), unsupported namelen %ld\n",
2251 +                      AuDLNPair(au_br_dentry(br)),
2252 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
2253 +
2254 +out:
2255 +       return err;
2256 +}
2257 +
2258 +/* intialize a new branch */
2259 +static int au_br_init(struct au_branch *br, struct super_block *sb,
2260 +                     struct au_opt_add *add)
2261 +{
2262 +       int err;
2263 +
2264 +       err = 0;
2265 +       memset(&br->br_xino, 0, sizeof(br->br_xino));
2266 +       mutex_init(&br->br_xino.xi_nondir_mtx);
2267 +       br->br_perm = add->perm;
2268 +       BUILD_BUG_ON(sizeof(br->br_dflags)
2269 +                    != sizeof(br->br_path.dentry->d_flags));
2270 +       br->br_dflags = DCACHE_MOUNTED;
2271 +       br->br_path = add->path; /* set first, path_get() later */
2272 +       spin_lock_init(&br->br_dykey_lock);
2273 +       memset(br->br_dykey, 0, sizeof(br->br_dykey));
2274 +       atomic_set(&br->br_count, 0);
2275 +       br->br_xino_upper = AUFS_XINO_TRUNC_INIT;
2276 +       atomic_set(&br->br_xino_running, 0);
2277 +       br->br_id = au_new_br_id(sb);
2278 +       AuDebugOn(br->br_id < 0);
2279 +
2280 +       if (au_br_writable(add->perm)) {
2281 +               err = au_wbr_init(br, sb, add->perm);
2282 +               if (unlikely(err))
2283 +                       goto out_err;
2284 +       }
2285 +
2286 +       if (au_opt_test(au_mntflags(sb), XINO)) {
2287 +               err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
2288 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
2289 +               if (unlikely(err)) {
2290 +                       AuDebugOn(br->br_xino.xi_file);
2291 +                       goto out_err;
2292 +               }
2293 +       }
2294 +
2295 +       sysaufs_br_init(br);
2296 +       path_get(&br->br_path);
2297 +       goto out; /* success */
2298 +
2299 +out_err:
2300 +       memset(&br->br_path, 0, sizeof(br->br_path));
2301 +out:
2302 +       return err;
2303 +}
2304 +
2305 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
2306 +                            struct au_branch *br, aufs_bindex_t bend,
2307 +                            aufs_bindex_t amount)
2308 +{
2309 +       struct au_branch **brp;
2310 +
2311 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2312 +
2313 +       brp = sbinfo->si_branch + bindex;
2314 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
2315 +       *brp = br;
2316 +       sbinfo->si_bend++;
2317 +       if (unlikely(bend < 0))
2318 +               sbinfo->si_bend = 0;
2319 +}
2320 +
2321 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
2322 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2323 +{
2324 +       struct au_hdentry *hdp;
2325 +
2326 +       AuRwMustWriteLock(&dinfo->di_rwsem);
2327 +
2328 +       hdp = dinfo->di_hdentry + bindex;
2329 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
2330 +       au_h_dentry_init(hdp);
2331 +       dinfo->di_bend++;
2332 +       if (unlikely(bend < 0))
2333 +               dinfo->di_bstart = 0;
2334 +}
2335 +
2336 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
2337 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2338 +{
2339 +       struct au_hinode *hip;
2340 +
2341 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
2342 +
2343 +       hip = iinfo->ii_hinode + bindex;
2344 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
2345 +       hip->hi_inode = NULL;
2346 +       au_hn_init(hip);
2347 +       iinfo->ii_bend++;
2348 +       if (unlikely(bend < 0))
2349 +               iinfo->ii_bstart = 0;
2350 +}
2351 +
2352 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
2353 +                        aufs_bindex_t bindex)
2354 +{
2355 +       struct dentry *root, *h_dentry;
2356 +       struct inode *root_inode;
2357 +       aufs_bindex_t bend, amount;
2358 +
2359 +       au_br_dflags_force(br);
2360 +
2361 +       root = sb->s_root;
2362 +       root_inode = root->d_inode;
2363 +       bend = au_sbend(sb);
2364 +       amount = bend + 1 - bindex;
2365 +       h_dentry = au_br_dentry(br);
2366 +       au_sbilist_lock();
2367 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
2368 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
2369 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
2370 +       au_set_h_dptr(root, bindex, dget(h_dentry));
2371 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
2372 +                     /*flags*/0);
2373 +       au_sbilist_unlock();
2374 +}
2375 +
2376 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
2377 +{
2378 +       int err;
2379 +       aufs_bindex_t bend, add_bindex;
2380 +       struct dentry *root, *h_dentry;
2381 +       struct inode *root_inode;
2382 +       struct au_branch *add_branch;
2383 +
2384 +       root = sb->s_root;
2385 +       root_inode = root->d_inode;
2386 +       IMustLock(root_inode);
2387 +       err = test_add(sb, add, remount);
2388 +       if (unlikely(err < 0))
2389 +               goto out;
2390 +       if (err) {
2391 +               err = 0;
2392 +               goto out; /* success */
2393 +       }
2394 +
2395 +       bend = au_sbend(sb);
2396 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
2397 +       err = PTR_ERR(add_branch);
2398 +       if (IS_ERR(add_branch))
2399 +               goto out;
2400 +
2401 +       err = au_br_init(add_branch, sb, add);
2402 +       if (unlikely(err)) {
2403 +               au_br_do_free(add_branch);
2404 +               goto out;
2405 +       }
2406 +
2407 +       add_bindex = add->bindex;
2408 +       if (!remount)
2409 +               au_br_do_add(sb, add_branch, add_bindex);
2410 +       else {
2411 +               sysaufs_brs_del(sb, add_bindex);
2412 +               au_br_do_add(sb, add_branch, add_bindex);
2413 +               sysaufs_brs_add(sb, add_bindex);
2414 +       }
2415 +
2416 +       h_dentry = add->path.dentry;
2417 +       if (!add_bindex) {
2418 +               au_cpup_attr_all(root_inode, /*force*/1);
2419 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
2420 +       } else
2421 +               au_add_nlink(root_inode, h_dentry->d_inode);
2422 +
2423 +       /*
2424 +        * this test/set prevents aufs from handling unnecesary notify events
2425 +        * of xino files, in case of re-adding a writable branch which was
2426 +        * once detached from aufs.
2427 +        */
2428 +       if (au_xino_brid(sb) < 0
2429 +           && au_br_writable(add_branch->br_perm)
2430 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
2431 +           && add_branch->br_xino.xi_file
2432 +           && add_branch->br_xino.xi_file->f_dentry->d_parent == h_dentry)
2433 +               au_xino_brid_set(sb, add_branch->br_id);
2434 +
2435 +out:
2436 +       return err;
2437 +}
2438 +
2439 +/* ---------------------------------------------------------------------- */
2440 +
2441 +/*
2442 + * delete a branch
2443 + */
2444 +
2445 +/* to show the line number, do not make it inlined function */
2446 +#define AuVerbose(do_info, fmt, ...) do { \
2447 +       if (do_info) \
2448 +               pr_info(fmt, ##__VA_ARGS__); \
2449 +} while (0)
2450 +
2451 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
2452 +                        aufs_bindex_t bend)
2453 +{
2454 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
2455 +}
2456 +
2457 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
2458 +                        aufs_bindex_t bend)
2459 +{
2460 +       return au_test_ibusy(dentry->d_inode, bstart, bend);
2461 +}
2462 +
2463 +/*
2464 + * test if the branch is deletable or not.
2465 + */
2466 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
2467 +                           unsigned int sigen, const unsigned int verbose)
2468 +{
2469 +       int err, i, j, ndentry;
2470 +       aufs_bindex_t bstart, bend;
2471 +       struct au_dcsub_pages dpages;
2472 +       struct au_dpage *dpage;
2473 +       struct dentry *d;
2474 +
2475 +       err = au_dpages_init(&dpages, GFP_NOFS);
2476 +       if (unlikely(err))
2477 +               goto out;
2478 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
2479 +       if (unlikely(err))
2480 +               goto out_dpages;
2481 +
2482 +       for (i = 0; !err && i < dpages.ndpage; i++) {
2483 +               dpage = dpages.dpages + i;
2484 +               ndentry = dpage->ndentry;
2485 +               for (j = 0; !err && j < ndentry; j++) {
2486 +                       d = dpage->dentries[j];
2487 +                       AuDebugOn(!d->d_count);
2488 +                       if (!au_digen_test(d, sigen)) {
2489 +                               di_read_lock_child(d, AuLock_IR);
2490 +                               if (unlikely(au_dbrange_test(d))) {
2491 +                                       di_read_unlock(d, AuLock_IR);
2492 +                                       continue;
2493 +                               }
2494 +                       } else {
2495 +                               di_write_lock_child(d);
2496 +                               if (unlikely(au_dbrange_test(d))) {
2497 +                                       di_write_unlock(d);
2498 +                                       continue;
2499 +                               }
2500 +                               err = au_reval_dpath(d, sigen);
2501 +                               if (!err)
2502 +                                       di_downgrade_lock(d, AuLock_IR);
2503 +                               else {
2504 +                                       di_write_unlock(d);
2505 +                                       break;
2506 +                               }
2507 +                       }
2508 +
2509 +                       /* AuDbgDentry(d); */
2510 +                       bstart = au_dbstart(d);
2511 +                       bend = au_dbend(d);
2512 +                       if (bstart <= bindex
2513 +                           && bindex <= bend
2514 +                           && au_h_dptr(d, bindex)
2515 +                           && au_test_dbusy(d, bstart, bend)) {
2516 +                               err = -EBUSY;
2517 +                               AuVerbose(verbose, "busy %.*s\n", AuDLNPair(d));
2518 +                               AuDbgDentry(d);
2519 +                       }
2520 +                       di_read_unlock(d, AuLock_IR);
2521 +               }
2522 +       }
2523 +
2524 +out_dpages:
2525 +       au_dpages_free(&dpages);
2526 +out:
2527 +       return err;
2528 +}
2529 +
2530 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
2531 +                          unsigned int sigen, const unsigned int verbose)
2532 +{
2533 +       int err;
2534 +       unsigned long long max, ull;
2535 +       struct inode *i, **array;
2536 +       aufs_bindex_t bstart, bend;
2537 +
2538 +       array = au_iarray_alloc(sb, &max);
2539 +       err = PTR_ERR(array);
2540 +       if (IS_ERR(array))
2541 +               goto out;
2542 +
2543 +       err = 0;
2544 +       AuDbg("b%d\n", bindex);
2545 +       for (ull = 0; !err && ull < max; ull++) {
2546 +               i = array[ull];
2547 +               if (i->i_ino == AUFS_ROOT_INO)
2548 +                       continue;
2549 +
2550 +               /* AuDbgInode(i); */
2551 +               if (au_iigen(i, NULL) == sigen)
2552 +                       ii_read_lock_child(i);
2553 +               else {
2554 +                       ii_write_lock_child(i);
2555 +                       err = au_refresh_hinode_self(i);
2556 +                       au_iigen_dec(i);
2557 +                       if (!err)
2558 +                               ii_downgrade_lock(i);
2559 +                       else {
2560 +                               ii_write_unlock(i);
2561 +                               break;
2562 +                       }
2563 +               }
2564 +
2565 +               bstart = au_ibstart(i);
2566 +               bend = au_ibend(i);
2567 +               if (bstart <= bindex
2568 +                   && bindex <= bend
2569 +                   && au_h_iptr(i, bindex)
2570 +                   && au_test_ibusy(i, bstart, bend)) {
2571 +                       err = -EBUSY;
2572 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
2573 +                       AuDbgInode(i);
2574 +               }
2575 +               ii_read_unlock(i);
2576 +       }
2577 +       au_iarray_free(array, max);
2578 +
2579 +out:
2580 +       return err;
2581 +}
2582 +
2583 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
2584 +                             const unsigned int verbose)
2585 +{
2586 +       int err;
2587 +       unsigned int sigen;
2588 +
2589 +       sigen = au_sigen(root->d_sb);
2590 +       DiMustNoWaiters(root);
2591 +       IiMustNoWaiters(root->d_inode);
2592 +       di_write_unlock(root);
2593 +       err = test_dentry_busy(root, bindex, sigen, verbose);
2594 +       if (!err)
2595 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
2596 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
2597 +
2598 +       return err;
2599 +}
2600 +
2601 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
2602 +                            const aufs_bindex_t bindex,
2603 +                            const aufs_bindex_t bend)
2604 +{
2605 +       struct au_branch **brp, **p;
2606 +
2607 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2608 +
2609 +       brp = sbinfo->si_branch + bindex;
2610 +       if (bindex < bend)
2611 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
2612 +       sbinfo->si_branch[0 + bend] = NULL;
2613 +       sbinfo->si_bend--;
2614 +
2615 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
2616 +       if (p)
2617 +               sbinfo->si_branch = p;
2618 +       /* harmless error */
2619 +}
2620 +
2621 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
2622 +                            const aufs_bindex_t bend)
2623 +{
2624 +       struct au_hdentry *hdp, *p;
2625 +
2626 +       AuRwMustWriteLock(&dinfo->di_rwsem);
2627 +
2628 +       hdp = dinfo->di_hdentry;
2629 +       if (bindex < bend)
2630 +               memmove(hdp + bindex, hdp + bindex + 1,
2631 +                       sizeof(*hdp) * (bend - bindex));
2632 +       hdp[0 + bend].hd_dentry = NULL;
2633 +       dinfo->di_bend--;
2634 +
2635 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
2636 +       if (p)
2637 +               dinfo->di_hdentry = p;
2638 +       /* harmless error */
2639 +}
2640 +
2641 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
2642 +                            const aufs_bindex_t bend)
2643 +{
2644 +       struct au_hinode *hip, *p;
2645 +
2646 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
2647 +
2648 +       hip = iinfo->ii_hinode + bindex;
2649 +       if (bindex < bend)
2650 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
2651 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
2652 +       au_hn_init(iinfo->ii_hinode + bend);
2653 +       iinfo->ii_bend--;
2654 +
2655 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
2656 +       if (p)
2657 +               iinfo->ii_hinode = p;
2658 +       /* harmless error */
2659 +}
2660 +
2661 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
2662 +                        struct au_branch *br)
2663 +{
2664 +       aufs_bindex_t bend;
2665 +       struct au_sbinfo *sbinfo;
2666 +       struct dentry *root, *h_root;
2667 +       struct inode *inode, *h_inode;
2668 +       struct au_hinode *hinode;
2669 +
2670 +       SiMustWriteLock(sb);
2671 +
2672 +       root = sb->s_root;
2673 +       inode = root->d_inode;
2674 +       sbinfo = au_sbi(sb);
2675 +       bend = sbinfo->si_bend;
2676 +
2677 +       h_root = au_h_dptr(root, bindex);
2678 +       hinode = au_hi(inode, bindex);
2679 +       h_inode = au_igrab(hinode->hi_inode);
2680 +       au_hiput(hinode);
2681 +
2682 +       au_sbilist_lock();
2683 +       au_br_do_del_brp(sbinfo, bindex, bend);
2684 +       au_br_do_del_hdp(au_di(root), bindex, bend);
2685 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
2686 +       au_sbilist_unlock();
2687 +
2688 +       dput(h_root);
2689 +       iput(h_inode);
2690 +       au_br_do_free(br);
2691 +}
2692 +
2693 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
2694 +{
2695 +       int err, rerr, i;
2696 +       unsigned int mnt_flags;
2697 +       aufs_bindex_t bindex, bend, br_id;
2698 +       unsigned char do_wh, verbose;
2699 +       struct au_branch *br;
2700 +       struct au_wbr *wbr;
2701 +
2702 +       err = 0;
2703 +       bindex = au_find_dbindex(sb->s_root, del->h_path.dentry);
2704 +       if (bindex < 0) {
2705 +               if (remount)
2706 +                       goto out; /* success */
2707 +               err = -ENOENT;
2708 +               pr_err("%s no such branch\n", del->pathname);
2709 +               goto out;
2710 +       }
2711 +       AuDbg("bindex b%d\n", bindex);
2712 +
2713 +       err = -EBUSY;
2714 +       mnt_flags = au_mntflags(sb);
2715 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
2716 +       bend = au_sbend(sb);
2717 +       if (unlikely(!bend)) {
2718 +               AuVerbose(verbose, "no more branches left\n");
2719 +               goto out;
2720 +       }
2721 +       br = au_sbr(sb, bindex);
2722 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
2723 +       i = atomic_read(&br->br_count);
2724 +       if (unlikely(i)) {
2725 +               AuVerbose(verbose, "%d file(s) opened\n", i);
2726 +               goto out;
2727 +       }
2728 +
2729 +       wbr = br->br_wbr;
2730 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
2731 +       if (do_wh) {
2732 +               /* instead of WbrWhMustWriteLock(wbr) */
2733 +               SiMustWriteLock(sb);
2734 +               for (i = 0; i < AuBrWh_Last; i++) {
2735 +                       dput(wbr->wbr_wh[i]);
2736 +                       wbr->wbr_wh[i] = NULL;
2737 +               }
2738 +       }
2739 +
2740 +       err = test_children_busy(sb->s_root, bindex, verbose);
2741 +       if (unlikely(err)) {
2742 +               if (do_wh)
2743 +                       goto out_wh;
2744 +               goto out;
2745 +       }
2746 +
2747 +       err = 0;
2748 +       br_id = br->br_id;
2749 +       if (!remount)
2750 +               au_br_do_del(sb, bindex, br);
2751 +       else {
2752 +               sysaufs_brs_del(sb, bindex);
2753 +               au_br_do_del(sb, bindex, br);
2754 +               sysaufs_brs_add(sb, bindex);
2755 +       }
2756 +
2757 +       if (!bindex) {
2758 +               au_cpup_attr_all(sb->s_root->d_inode, /*force*/1);
2759 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
2760 +       } else
2761 +               au_sub_nlink(sb->s_root->d_inode, del->h_path.dentry->d_inode);
2762 +       if (au_opt_test(mnt_flags, PLINK))
2763 +               au_plink_half_refresh(sb, br_id);
2764 +
2765 +       if (au_xino_brid(sb) == br_id)
2766 +               au_xino_brid_set(sb, -1);
2767 +       goto out; /* success */
2768 +
2769 +out_wh:
2770 +       /* revert */
2771 +       rerr = au_br_init_wh(sb, br, br->br_perm);
2772 +       if (rerr)
2773 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
2774 +                       del->pathname, rerr);
2775 +out:
2776 +       return err;
2777 +}
2778 +
2779 +/* ---------------------------------------------------------------------- */
2780 +
2781 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
2782 +{
2783 +       int err;
2784 +       aufs_bindex_t bstart, bend;
2785 +       struct aufs_ibusy ibusy;
2786 +       struct inode *inode, *h_inode;
2787 +
2788 +       err = -EPERM;
2789 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
2790 +               goto out;
2791 +
2792 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
2793 +       if (!err)
2794 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
2795 +       if (unlikely(err)) {
2796 +               err = -EFAULT;
2797 +               AuTraceErr(err);
2798 +               goto out;
2799 +       }
2800 +
2801 +       err = -EINVAL;
2802 +       si_read_lock(sb, AuLock_FLUSH);
2803 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
2804 +               goto out_unlock;
2805 +
2806 +       err = 0;
2807 +       ibusy.h_ino = 0; /* invalid */
2808 +       inode = ilookup(sb, ibusy.ino);
2809 +       if (!inode
2810 +           || inode->i_ino == AUFS_ROOT_INO
2811 +           || is_bad_inode(inode))
2812 +               goto out_unlock;
2813 +
2814 +       ii_read_lock_child(inode);
2815 +       bstart = au_ibstart(inode);
2816 +       bend = au_ibend(inode);
2817 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
2818 +               h_inode = au_h_iptr(inode, ibusy.bindex);
2819 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
2820 +                       ibusy.h_ino = h_inode->i_ino;
2821 +       }
2822 +       ii_read_unlock(inode);
2823 +       iput(inode);
2824 +
2825 +out_unlock:
2826 +       si_read_unlock(sb);
2827 +       if (!err) {
2828 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
2829 +               if (unlikely(err)) {
2830 +                       err = -EFAULT;
2831 +                       AuTraceErr(err);
2832 +               }
2833 +       }
2834 +out:
2835 +       return err;
2836 +}
2837 +
2838 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
2839 +{
2840 +       return au_ibusy(file->f_dentry->d_sb, (void __user *)arg);
2841 +}
2842 +
2843 +#ifdef CONFIG_COMPAT
2844 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
2845 +{
2846 +       return au_ibusy(file->f_dentry->d_sb, compat_ptr(arg));
2847 +}
2848 +#endif
2849 +
2850 +/* ---------------------------------------------------------------------- */
2851 +
2852 +/*
2853 + * change a branch permission
2854 + */
2855 +
2856 +static void au_warn_ima(void)
2857 +{
2858 +#ifdef CONFIG_IMA
2859 +       /* since it doesn't support mark_files_ro() */
2860 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
2861 +#endif
2862 +}
2863 +
2864 +static int do_need_sigen_inc(int a, int b)
2865 +{
2866 +       return au_br_whable(a) && !au_br_whable(b);
2867 +}
2868 +
2869 +static int need_sigen_inc(int old, int new)
2870 +{
2871 +       return do_need_sigen_inc(old, new)
2872 +               || do_need_sigen_inc(new, old);
2873 +}
2874 +
2875 +static unsigned long long au_farray_cb(void *a,
2876 +                                      unsigned long long max __maybe_unused,
2877 +                                      void *arg)
2878 +{
2879 +       unsigned long long n;
2880 +       struct file **p, *f;
2881 +       struct au_sphlhead *files;
2882 +       struct au_finfo *finfo;
2883 +       struct super_block *sb = arg;
2884 +
2885 +       n = 0;
2886 +       p = a;
2887 +       files = &au_sbi(sb)->si_files;
2888 +       spin_lock(&files->spin);
2889 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
2890 +               f = finfo->fi_file;
2891 +               if (file_count(f)
2892 +                   && !special_file(file_inode(f)->i_mode)) {
2893 +                       get_file(f);
2894 +                       *p++ = f;
2895 +                       n++;
2896 +                       AuDebugOn(n > max);
2897 +               }
2898 +       }
2899 +       spin_unlock(&files->spin);
2900 +
2901 +       return n;
2902 +}
2903 +
2904 +static struct file **au_farray_alloc(struct super_block *sb,
2905 +                                    unsigned long long *max)
2906 +{
2907 +       *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
2908 +       return au_array_alloc(max, au_farray_cb, sb);
2909 +}
2910 +
2911 +static void au_farray_free(struct file **a, unsigned long long max)
2912 +{
2913 +       unsigned long long ull;
2914 +
2915 +       for (ull = 0; ull < max; ull++)
2916 +               if (a[ull])
2917 +                       fput(a[ull]);
2918 +       au_array_free(a);
2919 +}
2920 +
2921 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
2922 +{
2923 +       int err, do_warn;
2924 +       unsigned int mnt_flags;
2925 +       unsigned long long ull, max;
2926 +       aufs_bindex_t br_id;
2927 +       unsigned char verbose;
2928 +       struct file *file, *hf, **array;
2929 +       struct inode *inode;
2930 +       struct au_hfile *hfile;
2931 +
2932 +       mnt_flags = au_mntflags(sb);
2933 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
2934 +
2935 +       array = au_farray_alloc(sb, &max);
2936 +       err = PTR_ERR(array);
2937 +       if (IS_ERR(array))
2938 +               goto out;
2939 +
2940 +       do_warn = 0;
2941 +       br_id = au_sbr_id(sb, bindex);
2942 +       for (ull = 0; ull < max; ull++) {
2943 +               file = array[ull];
2944 +
2945 +               /* AuDbg("%.*s\n", AuDLNPair(file->f_dentry)); */
2946 +               fi_read_lock(file);
2947 +               if (unlikely(au_test_mmapped(file))) {
2948 +                       err = -EBUSY;
2949 +                       AuVerbose(verbose, "mmapped %.*s\n",
2950 +                                 AuDLNPair(file->f_dentry));
2951 +                       AuDbgFile(file);
2952 +                       FiMustNoWaiters(file);
2953 +                       fi_read_unlock(file);
2954 +                       goto out_array;
2955 +               }
2956 +
2957 +               inode = file_inode(file);
2958 +               hfile = &au_fi(file)->fi_htop;
2959 +               hf = hfile->hf_file;
2960 +               if (!S_ISREG(inode->i_mode)
2961 +                   || !(file->f_mode & FMODE_WRITE)
2962 +                   || hfile->hf_br->br_id != br_id
2963 +                   || !(hf->f_mode & FMODE_WRITE))
2964 +                       array[ull] = NULL;
2965 +               else {
2966 +                       do_warn = 1;
2967 +                       get_file(file);
2968 +               }
2969 +
2970 +               FiMustNoWaiters(file);
2971 +               fi_read_unlock(file);
2972 +               fput(file);
2973 +       }
2974 +
2975 +       err = 0;
2976 +       if (do_warn)
2977 +               au_warn_ima();
2978 +
2979 +       for (ull = 0; ull < max; ull++) {
2980 +               file = array[ull];
2981 +               if (!file)
2982 +                       continue;
2983 +
2984 +               /* todo: already flushed? */
2985 +               /*
2986 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
2987 +                * approach which resets f_mode and calls mnt_drop_write() and
2988 +                * file_release_write() for each file, because the branch
2989 +                * attribute in aufs world is totally different from the native
2990 +                * fs rw/ro mode.
2991 +               */
2992 +               /* fi_read_lock(file); */
2993 +               hfile = &au_fi(file)->fi_htop;
2994 +               hf = hfile->hf_file;
2995 +               /* fi_read_unlock(file); */
2996 +               spin_lock(&hf->f_lock);
2997 +               hf->f_mode &= ~FMODE_WRITE;
2998 +               spin_unlock(&hf->f_lock);
2999 +               if (!file_check_writeable(hf)) {
3000 +                       __mnt_drop_write(hf->f_path.mnt);
3001 +                       file_release_write(hf);
3002 +               }
3003 +       }
3004 +
3005 +out_array:
3006 +       au_farray_free(array, max);
3007 +out:
3008 +       AuTraceErr(err);
3009 +       return err;
3010 +}
3011 +
3012 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3013 +             int *do_refresh)
3014 +{
3015 +       int err, rerr;
3016 +       aufs_bindex_t bindex;
3017 +       struct dentry *root;
3018 +       struct au_branch *br;
3019 +
3020 +       root = sb->s_root;
3021 +       bindex = au_find_dbindex(root, mod->h_root);
3022 +       if (bindex < 0) {
3023 +               if (remount)
3024 +                       return 0; /* success */
3025 +               err = -ENOENT;
3026 +               pr_err("%s no such branch\n", mod->path);
3027 +               goto out;
3028 +       }
3029 +       AuDbg("bindex b%d\n", bindex);
3030 +
3031 +       err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
3032 +       if (unlikely(err))
3033 +               goto out;
3034 +
3035 +       br = au_sbr(sb, bindex);
3036 +       AuDebugOn(mod->h_root != au_br_dentry(br));
3037 +       if (br->br_perm == mod->perm)
3038 +               return 0; /* success */
3039 +
3040 +       if (au_br_writable(br->br_perm)) {
3041 +               /* remove whiteout base */
3042 +               err = au_br_init_wh(sb, br, mod->perm);
3043 +               if (unlikely(err))
3044 +                       goto out;
3045 +
3046 +               if (!au_br_writable(mod->perm)) {
3047 +                       /* rw --> ro, file might be mmapped */
3048 +                       DiMustNoWaiters(root);
3049 +                       IiMustNoWaiters(root->d_inode);
3050 +                       di_write_unlock(root);
3051 +                       err = au_br_mod_files_ro(sb, bindex);
3052 +                       /* aufs_write_lock() calls ..._child() */
3053 +                       di_write_lock_child(root);
3054 +
3055 +                       if (unlikely(err)) {
3056 +                               rerr = -ENOMEM;
3057 +                               br->br_wbr = kmalloc(sizeof(*br->br_wbr),
3058 +                                                    GFP_NOFS);
3059 +                               if (br->br_wbr)
3060 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
3061 +                               if (unlikely(rerr)) {
3062 +                                       AuIOErr("nested error %d (%d)\n",
3063 +                                               rerr, err);
3064 +                                       br->br_perm = mod->perm;
3065 +                               }
3066 +                       }
3067 +               }
3068 +       } else if (au_br_writable(mod->perm)) {
3069 +               /* ro --> rw */
3070 +               err = -ENOMEM;
3071 +               br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
3072 +               if (br->br_wbr) {
3073 +                       err = au_wbr_init(br, sb, mod->perm);
3074 +                       if (unlikely(err)) {
3075 +                               kfree(br->br_wbr);
3076 +                               br->br_wbr = NULL;
3077 +                       }
3078 +               }
3079 +       }
3080 +
3081 +       if (!err) {
3082 +               if ((br->br_perm & AuBrAttr_UNPIN)
3083 +                   && !(mod->perm & AuBrAttr_UNPIN))
3084 +                       au_br_dflags_force(br);
3085 +               else if (!(br->br_perm & AuBrAttr_UNPIN)
3086 +                        && (mod->perm & AuBrAttr_UNPIN))
3087 +                       au_br_dflags_restore(br);
3088 +               *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
3089 +               br->br_perm = mod->perm;
3090 +       }
3091 +
3092 +out:
3093 +       AuTraceErr(err);
3094 +       return err;
3095 +}
3096 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
3097 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
3098 +++ linux/fs/aufs/branch.h      2013-07-06 13:20:47.740198107 +0200
3099 @@ -0,0 +1,255 @@
3100 +/*
3101 + * Copyright (C) 2005-2013 Junjiro R. Okajima
3102 + *
3103 + * This program, aufs is free software; you can redistribute it and/or modify
3104 + * it under the terms of the GNU General Public License as published by
3105 + * the Free Software Foundation; either version 2 of the License, or
3106 + * (at your option) any later version.
3107 + *
3108 + * This program is distributed in the hope that it will be useful,
3109 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3110 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3111 + * GNU General Public License for more details.
3112 + *
3113 + * You should have received a copy of the GNU General Public License
3114 + * along with this program; if not, write to the Free Software
3115 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
3116 + */
3117 +
3118 +/*
3119 + * branch filesystems and xino for them
3120 + */
3121 +
3122 +#ifndef __AUFS_BRANCH_H__
3123 +#define __AUFS_BRANCH_H__
3124 +
3125 +#ifdef __KERNEL__
3126 +
3127 +#include <linux/mount.h>
3128 +#include "dynop.h"
3129 +#include "rwsem.h"
3130 +#include "super.h"
3131 +
3132 +/* ---------------------------------------------------------------------- */
3133 +
3134 +/* a xino file */
3135 +struct au_xino_file {
3136 +       struct file             *xi_file;
3137 +       struct mutex            xi_nondir_mtx;
3138 +
3139 +       /* todo: make xino files an array to support huge inode number */
3140 +
3141 +#ifdef CONFIG_DEBUG_FS
3142 +       struct dentry            *xi_dbgaufs;
3143 +#endif
3144 +};
3145 +
3146 +/* members for writable branch only */
3147 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
3148 +struct au_wbr {
3149 +       struct au_rwsem         wbr_wh_rwsem;
3150 +       struct dentry           *wbr_wh[AuBrWh_Last];
3151 +       atomic_t                wbr_wh_running;
3152 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
3153 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
3154 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
3155 +
3156 +       /* mfs mode */
3157 +       unsigned long long      wbr_bytes;
3158 +};
3159 +
3160 +/* ext2 has 3 types of operations at least, ext3 has 4 */
3161 +#define AuBrDynOp (AuDyLast * 4)
3162 +
3163 +#ifdef CONFIG_AUFS_HFSNOTIFY
3164 +/* support for asynchronous destruction */
3165 +struct au_br_hfsnotify {
3166 +       struct fsnotify_group   *hfsn_group;
3167 +};
3168 +#endif
3169 +
3170 +/* protected by superblock rwsem */
3171 +struct au_branch {
3172 +       struct au_xino_file     br_xino;
3173 +
3174 +       aufs_bindex_t           br_id;
3175 +
3176 +       int                     br_perm;
3177 +       unsigned int            br_dflags;
3178 +       struct path             br_path;
3179 +       spinlock_t              br_dykey_lock;
3180 +       struct au_dykey         *br_dykey[AuBrDynOp];
3181 +       atomic_t                br_count;
3182 +
3183 +       struct au_wbr           *br_wbr;
3184 +
3185 +       /* xino truncation */
3186 +       blkcnt_t                br_xino_upper;  /* watermark in blocks */
3187 +       atomic_t                br_xino_running;
3188 +
3189 +#ifdef CONFIG_AUFS_HFSNOTIFY
3190 +       struct au_br_hfsnotify  *br_hfsn;
3191 +#endif
3192 +
3193 +#ifdef CONFIG_SYSFS
3194 +       /* an entry under sysfs per mount-point */
3195 +       char                    br_name[8];
3196 +       struct attribute        br_attr;
3197 +#endif
3198 +};
3199 +
3200 +/* ---------------------------------------------------------------------- */
3201 +
3202 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
3203 +{
3204 +       return br->br_path.mnt;
3205 +}
3206 +
3207 +static inline struct dentry *au_br_dentry(struct au_branch *br)
3208 +{
3209 +       return br->br_path.dentry;
3210 +}
3211 +
3212 +static inline struct super_block *au_br_sb(struct au_branch *br)
3213 +{
3214 +       return au_br_mnt(br)->mnt_sb;
3215 +}
3216 +
3217 +/* branch permissions and attributes */
3218 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
3219 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
3220 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
3221 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
3222 +
3223 +#define AuBrRAttr_WH           (1 << 3)        /* whiteout-able */
3224 +
3225 +#define AuBrWAttr_NoLinkWH     (1 << 4)        /* un-hardlinkable whiteouts */
3226 +
3227 +#define AuBrAttr_UNPIN         (1 << 5)        /* rename-able top dir of
3228 +                                                  branch */
3229 +
3230 +static inline int au_br_writable(int brperm)
3231 +{
3232 +       return brperm & AuBrPerm_RW;
3233 +}
3234 +
3235 +static inline int au_br_whable(int brperm)
3236 +{
3237 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
3238 +}
3239 +
3240 +static inline int au_br_wh_linkable(int brperm)
3241 +{
3242 +       return !(brperm & AuBrWAttr_NoLinkWH);
3243 +}
3244 +
3245 +static inline int au_br_rdonly(struct au_branch *br)
3246 +{
3247 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
3248 +               || !au_br_writable(br->br_perm))
3249 +               ? -EROFS : 0;
3250 +}
3251 +
3252 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
3253 +{
3254 +#ifdef CONFIG_AUFS_HNOTIFY
3255 +       return !(brperm & AuBrPerm_RR);
3256 +#else
3257 +       return 0;
3258 +#endif
3259 +}
3260 +
3261 +/* ---------------------------------------------------------------------- */
3262 +
3263 +/* branch.c */
3264 +struct au_sbinfo;
3265 +void au_br_free(struct au_sbinfo *sinfo);
3266 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
3267 +struct au_opt_add;
3268 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
3269 +struct au_opt_del;
3270 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
3271 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
3272 +#ifdef CONFIG_COMPAT
3273 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
3274 +#endif
3275 +struct au_opt_mod;
3276 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3277 +             int *do_refresh);
3278 +
3279 +/* xino.c */
3280 +static const loff_t au_loff_max = LLONG_MAX;
3281 +
3282 +int au_xib_trunc(struct super_block *sb);
3283 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
3284 +                  loff_t *pos);
3285 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
3286 +                   loff_t *pos);
3287 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
3288 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
3289 +ino_t au_xino_new_ino(struct super_block *sb);
3290 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
3291 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3292 +                 ino_t ino);
3293 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3294 +                ino_t *ino);
3295 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
3296 +              struct file *base_file, int do_test);
3297 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
3298 +
3299 +struct au_opt_xino;
3300 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
3301 +void au_xino_clr(struct super_block *sb);
3302 +struct file *au_xino_def(struct super_block *sb);
3303 +int au_xino_path(struct seq_file *seq, struct file *file);
3304 +
3305 +/* ---------------------------------------------------------------------- */
3306 +
3307 +/* Superblock to branch */
3308 +static inline
3309 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
3310 +{
3311 +       return au_sbr(sb, bindex)->br_id;
3312 +}
3313 +
3314 +static inline
3315 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
3316 +{
3317 +       return au_br_mnt(au_sbr(sb, bindex));
3318 +}
3319 +
3320 +static inline
3321 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
3322 +{
3323 +       return au_br_sb(au_sbr(sb, bindex));
3324 +}
3325 +
3326 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
3327 +{
3328 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
3329 +}
3330 +
3331 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
3332 +{
3333 +       return au_sbr(sb, bindex)->br_perm;
3334 +}
3335 +
3336 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
3337 +{
3338 +       return au_br_whable(au_sbr_perm(sb, bindex));
3339 +}
3340 +
3341 +/* ---------------------------------------------------------------------- */
3342 +
3343 +/*
3344 + * wbr_wh_read_lock, wbr_wh_write_lock
3345 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
3346 + */
3347 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
3348 +
3349 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
3350 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
3351 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
3352 +
3353 +#endif /* __KERNEL__ */
3354 +#endif /* __AUFS_BRANCH_H__ */
3355 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
3356 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
3357 +++ linux/fs/aufs/conf.mk       2013-07-06 13:20:47.740198107 +0200
3358 @@ -0,0 +1,38 @@
3359 +
3360 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
3361 +
3362 +define AuConf
3363 +ifdef ${1}
3364 +AuConfStr += ${1}=${${1}}
3365 +endif
3366 +endef
3367 +
3368 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
3369 +       SBILIST \
3370 +       HNOTIFY HFSNOTIFY \
3371 +       EXPORT INO_T_64 \
3372 +       RDU \
3373 +       PROC_MAP \
3374 +       SP_IATTR \
3375 +       SHWH \
3376 +       BR_RAMFS \
3377 +       BR_FUSE POLL \
3378 +       BR_HFSPLUS \
3379 +       BDEV_LOOP \
3380 +       DEBUG MAGIC_SYSRQ
3381 +$(foreach i, ${AuConfAll}, \
3382 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
3383 +
3384 +AuConfName = ${obj}/conf.str
3385 +${AuConfName}.tmp: FORCE
3386 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
3387 +${AuConfName}: ${AuConfName}.tmp
3388 +       @diff -q $< $@ > /dev/null 2>&1 || { \
3389 +       echo '  GEN    ' $@; \
3390 +       cp -p $< $@; \
3391 +       }
3392 +FORCE:
3393 +clean-files += ${AuConfName} ${AuConfName}.tmp
3394 +${obj}/sysfs.o: ${AuConfName}
3395 +
3396 +-include ${srctree}/${src}/conf_priv.mk
3397 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
3398 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
3399 +++ linux/fs/aufs/cpup.c        2013-08-23 23:59:39.631583456 +0200
3400 @@ -0,0 +1,1255 @@
3401 +/*
3402 + * Copyright (C) 2005-2013 Junjiro R. Okajima
3403 + *
3404 + * This program, aufs is free software; you can redistribute it and/or modify
3405 + * it under the terms of the GNU General Public License as published by
3406 + * the Free Software Foundation; either version 2 of the License, or
3407 + * (at your option) any later version.
3408 + *
3409 + * This program is distributed in the hope that it will be useful,
3410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3412 + * GNU General Public License for more details.
3413 + *
3414 + * You should have received a copy of the GNU General Public License
3415 + * along with this program; if not, write to the Free Software
3416 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
3417 + */
3418 +
3419 +/*
3420 + * copy-up functions, see wbr_policy.c for copy-down
3421 + */
3422 +
3423 +#include <linux/fs_stack.h>
3424 +#include <linux/mm.h>
3425 +#include "aufs.h"
3426 +
3427 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
3428 +{
3429 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
3430 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
3431 +
3432 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
3433 +
3434 +       dst->i_flags |= iflags & ~mask;
3435 +       if (au_test_fs_notime(dst->i_sb))
3436 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
3437 +}
3438 +
3439 +void au_cpup_attr_timesizes(struct inode *inode)
3440 +{
3441 +       struct inode *h_inode;
3442 +
3443 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3444 +       fsstack_copy_attr_times(inode, h_inode);
3445 +       fsstack_copy_inode_size(inode, h_inode);
3446 +}
3447 +
3448 +void au_cpup_attr_nlink(struct inode *inode, int force)
3449 +{
3450 +       struct inode *h_inode;
3451 +       struct super_block *sb;
3452 +       aufs_bindex_t bindex, bend;
3453 +
3454 +       sb = inode->i_sb;
3455 +       bindex = au_ibstart(inode);
3456 +       h_inode = au_h_iptr(inode, bindex);
3457 +       if (!force
3458 +           && !S_ISDIR(h_inode->i_mode)
3459 +           && au_opt_test(au_mntflags(sb), PLINK)
3460 +           && au_plink_test(inode))
3461 +               return;
3462 +
3463 +       /*
3464 +        * 0 can happen in revalidating.
3465 +        * h_inode->i_mutex is not held, but it is harmless since once i_nlink
3466 +        * reaches 0, it will never become positive.
3467 +        */
3468 +       set_nlink(inode, h_inode->i_nlink);
3469 +
3470 +       /*
3471 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
3472 +        * it may includes whplink directory.
3473 +        */
3474 +       if (S_ISDIR(h_inode->i_mode)) {
3475 +               bend = au_ibend(inode);
3476 +               for (bindex++; bindex <= bend; bindex++) {
3477 +                       h_inode = au_h_iptr(inode, bindex);
3478 +                       if (h_inode)
3479 +                               au_add_nlink(inode, h_inode);
3480 +               }
3481 +       }
3482 +}
3483 +
3484 +void au_cpup_attr_changeable(struct inode *inode)
3485 +{
3486 +       struct inode *h_inode;
3487 +
3488 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3489 +       inode->i_mode = h_inode->i_mode;
3490 +       inode->i_uid = h_inode->i_uid;
3491 +       inode->i_gid = h_inode->i_gid;
3492 +       au_cpup_attr_timesizes(inode);
3493 +       au_cpup_attr_flags(inode, h_inode->i_flags);
3494 +}
3495 +
3496 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
3497 +{
3498 +       struct au_iinfo *iinfo = au_ii(inode);
3499 +
3500 +       IiMustWriteLock(inode);
3501 +
3502 +       iinfo->ii_higen = h_inode->i_generation;
3503 +       iinfo->ii_hsb1 = h_inode->i_sb;
3504 +}
3505 +
3506 +void au_cpup_attr_all(struct inode *inode, int force)
3507 +{
3508 +       struct inode *h_inode;
3509 +
3510 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3511 +       au_cpup_attr_changeable(inode);
3512 +       if (inode->i_nlink > 0)
3513 +               au_cpup_attr_nlink(inode, force);
3514 +       inode->i_rdev = h_inode->i_rdev;
3515 +       inode->i_blkbits = h_inode->i_blkbits;
3516 +       au_cpup_igen(inode, h_inode);
3517 +}
3518 +
3519 +/* ---------------------------------------------------------------------- */
3520 +
3521 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
3522 +
3523 +/* keep the timestamps of the parent dir when cpup */
3524 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
3525 +                   struct path *h_path)
3526 +{
3527 +       struct inode *h_inode;
3528 +
3529 +       dt->dt_dentry = dentry;
3530 +       dt->dt_h_path = *h_path;
3531 +       h_inode = h_path->dentry->d_inode;
3532 +       dt->dt_atime = h_inode->i_atime;
3533 +       dt->dt_mtime = h_inode->i_mtime;
3534 +       /* smp_mb(); */
3535 +}
3536 +
3537 +void au_dtime_revert(struct au_dtime *dt)
3538 +{
3539 +       struct iattr attr;
3540 +       int err;
3541 +
3542 +       attr.ia_atime = dt->dt_atime;
3543 +       attr.ia_mtime = dt->dt_mtime;
3544 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
3545 +               | ATTR_ATIME | ATTR_ATIME_SET;
3546 +
3547 +       err = vfsub_notify_change(&dt->dt_h_path, &attr);
3548 +       if (unlikely(err))
3549 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
3550 +}
3551 +
3552 +/* ---------------------------------------------------------------------- */
3553 +
3554 +/* internal use only */
3555 +struct au_cpup_reg_attr {
3556 +       int             valid;
3557 +       struct kstat    st;
3558 +       unsigned int    iflags; /* inode->i_flags */
3559 +};
3560 +
3561 +static noinline_for_stack
3562 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
3563 +              struct au_cpup_reg_attr *h_src_attr)
3564 +{
3565 +       int err, sbits;
3566 +       struct iattr ia;
3567 +       struct path h_path;
3568 +       struct inode *h_isrc, *h_idst;
3569 +       struct kstat *h_st;
3570 +
3571 +       h_path.dentry = au_h_dptr(dst, bindex);
3572 +       h_idst = h_path.dentry->d_inode;
3573 +       h_path.mnt = au_sbr_mnt(dst->d_sb, bindex);
3574 +       h_isrc = h_src->d_inode;
3575 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
3576 +               | ATTR_ATIME | ATTR_MTIME
3577 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
3578 +       if (h_src_attr && h_src_attr->valid) {
3579 +               h_st = &h_src_attr->st;
3580 +               ia.ia_uid = h_st->uid;
3581 +               ia.ia_gid = h_st->gid;
3582 +               ia.ia_atime = h_st->atime;
3583 +               ia.ia_mtime = h_st->mtime;
3584 +               if (h_idst->i_mode != h_st->mode
3585 +                   && !S_ISLNK(h_idst->i_mode)) {
3586 +                       ia.ia_valid |= ATTR_MODE;
3587 +                       ia.ia_mode = h_st->mode;
3588 +               }
3589 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
3590 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
3591 +       } else {
3592 +               ia.ia_uid = h_isrc->i_uid;
3593 +               ia.ia_gid = h_isrc->i_gid;
3594 +               ia.ia_atime = h_isrc->i_atime;
3595 +               ia.ia_mtime = h_isrc->i_mtime;
3596 +               if (h_idst->i_mode != h_isrc->i_mode
3597 +                   && !S_ISLNK(h_idst->i_mode)) {
3598 +                       ia.ia_valid |= ATTR_MODE;
3599 +                       ia.ia_mode = h_isrc->i_mode;
3600 +               }
3601 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
3602 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
3603 +       }
3604 +       err = vfsub_notify_change(&h_path, &ia);
3605 +
3606 +       /* is this nfs only? */
3607 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
3608 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
3609 +               ia.ia_mode = h_isrc->i_mode;
3610 +               err = vfsub_notify_change(&h_path, &ia);
3611 +       }
3612 +
3613 +       return err;
3614 +}
3615 +
3616 +/* ---------------------------------------------------------------------- */
3617 +
3618 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
3619 +                          char *buf, unsigned long blksize)
3620 +{
3621 +       int err;
3622 +       size_t sz, rbytes, wbytes;
3623 +       unsigned char all_zero;
3624 +       char *p, *zp;
3625 +       struct mutex *h_mtx;
3626 +       /* reduce stack usage */
3627 +       struct iattr *ia;
3628 +
3629 +       zp = page_address(ZERO_PAGE(0));
3630 +       if (unlikely(!zp))
3631 +               return -ENOMEM; /* possible? */
3632 +
3633 +       err = 0;
3634 +       all_zero = 0;
3635 +       while (len) {
3636 +               AuDbg("len %lld\n", len);
3637 +               sz = blksize;
3638 +               if (len < blksize)
3639 +                       sz = len;
3640 +
3641 +               rbytes = 0;
3642 +               /* todo: signal_pending? */
3643 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
3644 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
3645 +                       err = rbytes;
3646 +               }
3647 +               if (unlikely(err < 0))
3648 +                       break;
3649 +
3650 +               all_zero = 0;
3651 +               if (len >= rbytes && rbytes == blksize)
3652 +                       all_zero = !memcmp(buf, zp, rbytes);
3653 +               if (!all_zero) {
3654 +                       wbytes = rbytes;
3655 +                       p = buf;
3656 +                       while (wbytes) {
3657 +                               size_t b;
3658 +
3659 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
3660 +                               err = b;
3661 +                               /* todo: signal_pending? */
3662 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
3663 +                                       continue;
3664 +                               if (unlikely(err < 0))
3665 +                                       break;
3666 +                               wbytes -= b;
3667 +                               p += b;
3668 +                       }
3669 +               } else {
3670 +                       loff_t res;
3671 +
3672 +                       AuLabel(hole);
3673 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
3674 +                       err = res;
3675 +                       if (unlikely(res < 0))
3676 +                               break;
3677 +               }
3678 +               len -= rbytes;
3679 +               err = 0;
3680 +       }
3681 +
3682 +       /* the last block may be a hole */
3683 +       if (!err && all_zero) {
3684 +               AuLabel(last hole);
3685 +
3686 +               err = 1;
3687 +               if (au_test_nfs(dst->f_dentry->d_sb)) {
3688 +                       /* nfs requires this step to make last hole */
3689 +                       /* is this only nfs? */
3690 +                       do {
3691 +                               /* todo: signal_pending? */
3692 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
3693 +                       } while (err == -EAGAIN || err == -EINTR);
3694 +                       if (err == 1)
3695 +                               dst->f_pos--;
3696 +               }
3697 +
3698 +               if (err == 1) {
3699 +                       ia = (void *)buf;
3700 +                       ia->ia_size = dst->f_pos;
3701 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
3702 +                       ia->ia_file = dst;
3703 +                       h_mtx = &file_inode(dst)->i_mutex;
3704 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
3705 +                       err = vfsub_notify_change(&dst->f_path, ia);
3706 +                       mutex_unlock(h_mtx);
3707 +               }
3708 +       }
3709 +
3710 +       return err;
3711 +}
3712 +
3713 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
3714 +{
3715 +       int err;
3716 +       unsigned long blksize;
3717 +       unsigned char do_kfree;
3718 +       char *buf;
3719 +
3720 +       err = -ENOMEM;
3721 +       blksize = dst->f_dentry->d_sb->s_blocksize;
3722 +       if (!blksize || PAGE_SIZE < blksize)
3723 +               blksize = PAGE_SIZE;
3724 +       AuDbg("blksize %lu\n", blksize);
3725 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
3726 +       if (do_kfree)
3727 +               buf = kmalloc(blksize, GFP_NOFS);
3728 +       else
3729 +               buf = (void *)__get_free_page(GFP_NOFS);
3730 +       if (unlikely(!buf))
3731 +               goto out;
3732 +
3733 +       if (len > (1 << 22))
3734 +               AuDbg("copying a large file %lld\n", (long long)len);
3735 +
3736 +       src->f_pos = 0;
3737 +       dst->f_pos = 0;
3738 +       err = au_do_copy_file(dst, src, len, buf, blksize);
3739 +       if (do_kfree)
3740 +               kfree(buf);
3741 +       else
3742 +               free_page((unsigned long)buf);
3743 +
3744 +out:
3745 +       return err;
3746 +}
3747 +
3748 +/*
3749 + * to support a sparse file which is opened with O_APPEND,
3750 + * we need to close the file.
3751 + */
3752 +static int au_cp_regular(struct au_cp_generic *cpg)
3753 +{
3754 +       int err, i;
3755 +       enum { SRC, DST };
3756 +       struct {
3757 +               aufs_bindex_t bindex;
3758 +               unsigned int flags;
3759 +               struct dentry *dentry;
3760 +               struct file *file;
3761 +               void *label, *label_file;
3762 +       } *f, file[] = {
3763 +               {
3764 +                       .bindex = cpg->bsrc,
3765 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
3766 +                       .file = NULL,
3767 +                       .label = &&out,
3768 +                       .label_file = &&out_src
3769 +               },
3770 +               {
3771 +                       .bindex = cpg->bdst,
3772 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
3773 +                       .file = NULL,
3774 +                       .label = &&out_src,
3775 +                       .label_file = &&out_dst
3776 +               }
3777 +       };
3778 +       struct super_block *sb;
3779 +
3780 +       /* bsrc branch can be ro/rw. */
3781 +       sb = cpg->dentry->d_sb;
3782 +       f = file;
3783 +       for (i = 0; i < 2; i++, f++) {
3784 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
3785 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
3786 +                                   /*file*/NULL);
3787 +               err = PTR_ERR(f->file);
3788 +               if (IS_ERR(f->file))
3789 +                       goto *f->label;
3790 +               err = -EINVAL;
3791 +               if (unlikely(!f->file->f_op))
3792 +                       goto *f->label_file;
3793 +       }
3794 +
3795 +       /* try stopping to update while we copyup */
3796 +       IMustLock(file[SRC].dentry->d_inode);
3797 +       err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
3798 +
3799 +out_dst:
3800 +       fput(file[DST].file);
3801 +       au_sbr_put(sb, file[DST].bindex);
3802 +out_src:
3803 +       fput(file[SRC].file);
3804 +       au_sbr_put(sb, file[SRC].bindex);
3805 +out:
3806 +       return err;
3807 +}
3808 +
3809 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
3810 +                             struct au_cpup_reg_attr *h_src_attr)
3811 +{
3812 +       int err, rerr;
3813 +       loff_t l;
3814 +       struct path h_path;
3815 +       struct inode *h_src_inode;
3816 +
3817 +       err = 0;
3818 +       h_src_inode = au_h_iptr(cpg->dentry->d_inode, cpg->bsrc);
3819 +       l = i_size_read(h_src_inode);
3820 +       if (cpg->len == -1 || l < cpg->len)
3821 +               cpg->len = l;
3822 +       if (cpg->len) {
3823 +               /* try stopping to update while we are referencing */
3824 +               mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
3825 +               au_pin_hdir_unlock(cpg->pin);
3826 +
3827 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
3828 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
3829 +               h_src_attr->iflags = h_src_inode->i_flags;
3830 +               err = vfs_getattr(&h_path, &h_src_attr->st);
3831 +               if (unlikely(err)) {
3832 +                       mutex_unlock(&h_src_inode->i_mutex);
3833 +                       goto out;
3834 +               }
3835 +               h_src_attr->valid = 1;
3836 +               err = au_cp_regular(cpg);
3837 +               mutex_unlock(&h_src_inode->i_mutex);
3838 +               rerr = au_pin_hdir_relock(cpg->pin);
3839 +               if (!err && rerr)
3840 +                       err = rerr;
3841 +       }
3842 +
3843 +out:
3844 +       return err;
3845 +}
3846 +
3847 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
3848 +                             struct inode *h_dir)
3849 +{
3850 +       int err, symlen;
3851 +       mm_segment_t old_fs;
3852 +       union {
3853 +               char *k;
3854 +               char __user *u;
3855 +       } sym;
3856 +
3857 +       err = -ENOSYS;
3858 +       if (unlikely(!h_src->d_inode->i_op->readlink))
3859 +               goto out;
3860 +
3861 +       err = -ENOMEM;
3862 +       sym.k = (void *)__get_free_page(GFP_NOFS);
3863 +       if (unlikely(!sym.k))
3864 +               goto out;
3865 +
3866 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
3867 +       old_fs = get_fs();
3868 +       set_fs(KERNEL_DS);
3869 +       symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
3870 +       err = symlen;
3871 +       set_fs(old_fs);
3872 +
3873 +       if (symlen > 0) {
3874 +               sym.k[symlen] = 0;
3875 +               err = vfsub_symlink(h_dir, h_path, sym.k);
3876 +       }
3877 +       free_page((unsigned long)sym.k);
3878 +
3879 +out:
3880 +       return err;
3881 +}
3882 +
3883 +static noinline_for_stack
3884 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
3885 +              struct au_cpup_reg_attr *h_src_attr)
3886 +{
3887 +       int err;
3888 +       umode_t mode;
3889 +       unsigned int mnt_flags;
3890 +       unsigned char isdir;
3891 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
3892 +       struct au_dtime dt;
3893 +       struct path h_path;
3894 +       struct dentry *h_src, *h_dst, *h_parent;
3895 +       struct inode *h_inode, *h_dir;
3896 +       struct super_block *sb;
3897 +
3898 +       /* bsrc branch can be ro/rw. */
3899 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
3900 +       h_inode = h_src->d_inode;
3901 +       AuDebugOn(h_inode != au_h_iptr(cpg->dentry->d_inode, cpg->bsrc));
3902 +
3903 +       /* try stopping to be referenced while we are creating */
3904 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
3905 +       if (au_ftest_cpup(cpg->flags, RENAME))
3906 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
3907 +                                 AUFS_WH_PFX_LEN));
3908 +       h_parent = h_dst->d_parent; /* dir inode is locked */
3909 +       h_dir = h_parent->d_inode;
3910 +       IMustLock(h_dir);
3911 +       AuDebugOn(h_parent != h_dst->d_parent);
3912 +
3913 +       sb = cpg->dentry->d_sb;
3914 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
3915 +       if (do_dt) {
3916 +               h_path.dentry = h_parent;
3917 +               au_dtime_store(&dt, dst_parent, &h_path);
3918 +       }
3919 +       h_path.dentry = h_dst;
3920 +
3921 +       isdir = 0;
3922 +       mode = h_inode->i_mode;
3923 +       switch (mode & S_IFMT) {
3924 +       case S_IFREG:
3925 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
3926 +                                  /*want_excl*/true);
3927 +               if (!err)
3928 +                       err = au_do_cpup_regular(cpg, h_src_attr);
3929 +               break;
3930 +       case S_IFDIR:
3931 +               isdir = 1;
3932 +               err = vfsub_mkdir(h_dir, &h_path, mode);
3933 +               if (!err) {
3934 +                       /*
3935 +                        * strange behaviour from the users view,
3936 +                        * particularry setattr case
3937 +                        */
3938 +                       if (au_ibstart(dst_parent->d_inode) == cpg->bdst)
3939 +                               au_cpup_attr_nlink(dst_parent->d_inode,
3940 +                                                  /*force*/1);
3941 +                       au_cpup_attr_nlink(cpg->dentry->d_inode, /*force*/1);
3942 +               }
3943 +               break;
3944 +       case S_IFLNK:
3945 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
3946 +               break;
3947 +       case S_IFCHR:
3948 +       case S_IFBLK:
3949 +               AuDebugOn(!capable(CAP_MKNOD));
3950 +               /*FALLTHROUGH*/
3951 +       case S_IFIFO:
3952 +       case S_IFSOCK:
3953 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
3954 +               break;
3955 +       default:
3956 +               AuIOErr("Unknown inode type 0%o\n", mode);
3957 +               err = -EIO;
3958 +       }
3959 +
3960 +       mnt_flags = au_mntflags(sb);
3961 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
3962 +           && !isdir
3963 +           && au_opt_test(mnt_flags, XINO)
3964 +           && h_inode->i_nlink == 1
3965 +           /* todo: unnecessary? */
3966 +           /* && cpg->dentry->d_inode->i_nlink == 1 */
3967 +           && cpg->bdst < cpg->bsrc
3968 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
3969 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
3970 +               /* ignore this error */
3971 +
3972 +       if (do_dt)
3973 +               au_dtime_revert(&dt);
3974 +       return err;
3975 +}
3976 +
3977 +static int au_do_ren_after_cpup(struct dentry *dentry, aufs_bindex_t bdst,
3978 +                               struct path *h_path)
3979 +{
3980 +       int err;
3981 +       struct dentry *h_dentry, *h_parent;
3982 +       struct inode *h_dir;
3983 +
3984 +       h_dentry = dget(au_h_dptr(dentry, bdst));
3985 +       au_set_h_dptr(dentry, bdst, NULL);
3986 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
3987 +       if (unlikely(err)) {
3988 +               au_set_h_dptr(dentry, bdst, h_dentry);
3989 +               goto out;
3990 +       }
3991 +
3992 +       h_path->dentry = dget(au_h_dptr(dentry, bdst));
3993 +       au_set_h_dptr(dentry, bdst, h_dentry);
3994 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
3995 +       h_dir = h_parent->d_inode;
3996 +       IMustLock(h_dir);
3997 +       AuDbg("%.*s %.*s\n", AuDLNPair(h_dentry), AuDLNPair(h_path->dentry));
3998 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path);
3999 +       dput(h_path->dentry);
4000 +
4001 +out:
4002 +       return err;
4003 +}
4004 +
4005 +/*
4006 + * copyup the @dentry from @bsrc to @bdst.
4007 + * the caller must set the both of lower dentries.
4008 + * @len is for truncating when it is -1 copyup the entire file.
4009 + * in link/rename cases, @dst_parent may be different from the real one.
4010 + * basic->bsrc can be larger than basic->bdst.
4011 + */
4012 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
4013 +{
4014 +       int err, rerr;
4015 +       aufs_bindex_t old_ibstart;
4016 +       unsigned char isdir, plink;
4017 +       struct dentry *h_src, *h_dst, *h_parent;
4018 +       struct inode *dst_inode, *h_dir, *inode;
4019 +       struct super_block *sb;
4020 +       struct au_branch *br;
4021 +       /* to reuduce stack size */
4022 +       struct {
4023 +               struct au_dtime dt;
4024 +               struct path h_path;
4025 +               struct au_cpup_reg_attr h_src_attr;
4026 +       } *a;
4027 +
4028 +       err = -ENOMEM;
4029 +       a = kmalloc(sizeof(*a), GFP_NOFS);
4030 +       if (unlikely(!a))
4031 +               goto out;
4032 +       a->h_src_attr.valid = 0;
4033 +
4034 +       sb = cpg->dentry->d_sb;
4035 +       br = au_sbr(sb, cpg->bdst);
4036 +       a->h_path.mnt = au_br_mnt(br);
4037 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
4038 +       h_parent = h_dst->d_parent; /* dir inode is locked */
4039 +       h_dir = h_parent->d_inode;
4040 +       IMustLock(h_dir);
4041 +
4042 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
4043 +       inode = cpg->dentry->d_inode;
4044 +
4045 +       if (!dst_parent)
4046 +               dst_parent = dget_parent(cpg->dentry);
4047 +       else
4048 +               dget(dst_parent);
4049 +
4050 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
4051 +       dst_inode = au_h_iptr(inode, cpg->bdst);
4052 +       if (dst_inode) {
4053 +               if (unlikely(!plink)) {
4054 +                       err = -EIO;
4055 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
4056 +                               "but plink is disabled\n",
4057 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
4058 +                       goto out_parent;
4059 +               }
4060 +
4061 +               if (dst_inode->i_nlink) {
4062 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
4063 +
4064 +                       h_src = au_plink_lkup(inode, cpg->bdst);
4065 +                       err = PTR_ERR(h_src);
4066 +                       if (IS_ERR(h_src))
4067 +                               goto out_parent;
4068 +                       if (unlikely(!h_src->d_inode)) {
4069 +                               err = -EIO;
4070 +                               AuIOErr("i%lu exists on a upper branch "
4071 +                                       "but not pseudo-linked\n",
4072 +                                       inode->i_ino);
4073 +                               dput(h_src);
4074 +                               goto out_parent;
4075 +                       }
4076 +
4077 +                       if (do_dt) {
4078 +                               a->h_path.dentry = h_parent;
4079 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
4080 +                       }
4081 +
4082 +                       a->h_path.dentry = h_dst;
4083 +                       err = vfsub_link(h_src, h_dir, &a->h_path);
4084 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
4085 +                               err = au_do_ren_after_cpup
4086 +                                       (cpg->dentry, cpg->bdst, &a->h_path);
4087 +                       if (do_dt)
4088 +                               au_dtime_revert(&a->dt);
4089 +                       dput(h_src);
4090 +                       goto out_parent;
4091 +               } else
4092 +                       /* todo: cpup_wh_file? */
4093 +                       /* udba work */
4094 +                       au_update_ibrange(inode, /*do_put_zero*/1);
4095 +       }
4096 +
4097 +       isdir = S_ISDIR(inode->i_mode);
4098 +       old_ibstart = au_ibstart(inode);
4099 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
4100 +       if (unlikely(err))
4101 +               goto out_rev;
4102 +       dst_inode = h_dst->d_inode;
4103 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
4104 +       /* todo: necessary? */
4105 +       /* au_pin_hdir_unlock(cpg->pin); */
4106 +
4107 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
4108 +       if (unlikely(err)) {
4109 +               /* todo: necessary? */
4110 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
4111 +               mutex_unlock(&dst_inode->i_mutex);
4112 +               goto out_rev;
4113 +       }
4114 +
4115 +       if (cpg->bdst < old_ibstart) {
4116 +               if (S_ISREG(inode->i_mode)) {
4117 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
4118 +                       if (unlikely(err)) {
4119 +                               /* ignore an error */
4120 +                               /* au_pin_hdir_relock(cpg->pin); */
4121 +                               mutex_unlock(&dst_inode->i_mutex);
4122 +                               goto out_rev;
4123 +                       }
4124 +               }
4125 +               au_set_ibstart(inode, cpg->bdst);
4126 +       } else
4127 +               au_set_ibend(inode, cpg->bdst);
4128 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
4129 +                     au_hi_flags(inode, isdir));
4130 +
4131 +       /* todo: necessary? */
4132 +       /* err = au_pin_hdir_relock(cpg->pin); */
4133 +       mutex_unlock(&dst_inode->i_mutex);
4134 +       if (unlikely(err))
4135 +               goto out_rev;
4136 +
4137 +       if (!isdir
4138 +           && h_src->d_inode->i_nlink > 1
4139 +           && plink)
4140 +               au_plink_append(inode, cpg->bdst, h_dst);
4141 +
4142 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
4143 +               a->h_path.dentry = h_dst;
4144 +               err = au_do_ren_after_cpup(cpg->dentry, cpg->bdst, &a->h_path);
4145 +       }
4146 +       if (!err)
4147 +               goto out_parent; /* success */
4148 +
4149 +       /* revert */
4150 +out_rev:
4151 +       a->h_path.dentry = h_parent;
4152 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
4153 +       a->h_path.dentry = h_dst;
4154 +       rerr = 0;
4155 +       if (h_dst->d_inode) {
4156 +               if (!isdir)
4157 +                       rerr = vfsub_unlink(h_dir, &a->h_path, /*force*/0);
4158 +               else
4159 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
4160 +       }
4161 +       au_dtime_revert(&a->dt);
4162 +       if (rerr) {
4163 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
4164 +               err = -EIO;
4165 +       }
4166 +out_parent:
4167 +       dput(dst_parent);
4168 +       kfree(a);
4169 +out:
4170 +       return err;
4171 +}
4172 +
4173 +#if 0 /* unused */
4174 +struct au_cpup_single_args {
4175 +       int *errp;
4176 +       struct au_cp_generic *cpg;
4177 +       struct dentry *dst_parent;
4178 +};
4179 +
4180 +static void au_call_cpup_single(void *args)
4181 +{
4182 +       struct au_cpup_single_args *a = args;
4183 +
4184 +       au_pin_hdir_acquire_nest(a->cpg->pin);
4185 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
4186 +       au_pin_hdir_release(a->cpg->pin);
4187 +}
4188 +#endif
4189 +
4190 +/*
4191 + * prevent SIGXFSZ in copy-up.
4192 + * testing CAP_MKNOD is for generic fs,
4193 + * but CAP_FSETID is for xfs only, currently.
4194 + */
4195 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
4196 +{
4197 +       int do_sio;
4198 +       struct super_block *sb;
4199 +       struct inode *h_dir;
4200 +
4201 +       do_sio = 0;
4202 +       sb = au_pinned_parent(pin)->d_sb;
4203 +       if (!au_wkq_test()
4204 +           && (!au_sbi(sb)->si_plink_maint_pid
4205 +               || au_plink_maint(sb, AuLock_NOPLM))) {
4206 +               switch (mode & S_IFMT) {
4207 +               case S_IFREG:
4208 +                       /* no condition about RLIMIT_FSIZE and the file size */
4209 +                       do_sio = 1;
4210 +                       break;
4211 +               case S_IFCHR:
4212 +               case S_IFBLK:
4213 +                       do_sio = !capable(CAP_MKNOD);
4214 +                       break;
4215 +               }
4216 +               if (!do_sio)
4217 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
4218 +                                 && !capable(CAP_FSETID));
4219 +               /* this workaround may be removed in the future */
4220 +               if (!do_sio) {
4221 +                       h_dir = au_pinned_h_dir(pin);
4222 +                       do_sio = h_dir->i_mode & S_ISVTX;
4223 +               }
4224 +       }
4225 +
4226 +       return do_sio;
4227 +}
4228 +
4229 +#if 0 /* unused */
4230 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
4231 +{
4232 +       int err, wkq_err;
4233 +       struct dentry *h_dentry;
4234 +
4235 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
4236 +       if (!au_cpup_sio_test(pin, h_dentry->d_inode->i_mode))
4237 +               err = au_cpup_single(cpg, dst_parent);
4238 +       else {
4239 +               struct au_cpup_single_args args = {
4240 +                       .errp           = &err,
4241 +                       .cpg            = cpg,
4242 +                       .dst_parent     = dst_parent
4243 +               };
4244 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
4245 +               if (unlikely(wkq_err))
4246 +                       err = wkq_err;
4247 +       }
4248 +
4249 +       return err;
4250 +}
4251 +#endif
4252 +
4253 +/*
4254 + * copyup the @dentry from the first active lower branch to @bdst,
4255 + * using au_cpup_single().
4256 + */
4257 +static int au_cpup_simple(struct au_cp_generic *cpg)
4258 +{
4259 +       int err;
4260 +       unsigned int flags_orig;
4261 +       struct dentry *dentry;
4262 +
4263 +       AuDebugOn(cpg->bsrc < 0);
4264 +
4265 +       dentry = cpg->dentry;
4266 +       DiMustWriteLock(dentry);
4267 +
4268 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
4269 +       if (!err) {
4270 +               flags_orig = cpg->flags;
4271 +               au_fset_cpup(cpg->flags, RENAME);
4272 +               err = au_cpup_single(cpg, NULL);
4273 +               cpg->flags = flags_orig;
4274 +               if (!err)
4275 +                       return 0; /* success */
4276 +
4277 +               /* revert */
4278 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
4279 +               au_set_dbstart(dentry, cpg->bsrc);
4280 +       }
4281 +
4282 +       return err;
4283 +}
4284 +
4285 +struct au_cpup_simple_args {
4286 +       int *errp;
4287 +       struct au_cp_generic *cpg;
4288 +};
4289 +
4290 +static void au_call_cpup_simple(void *args)
4291 +{
4292 +       struct au_cpup_simple_args *a = args;
4293 +
4294 +       au_pin_hdir_acquire_nest(a->cpg->pin);
4295 +       *a->errp = au_cpup_simple(a->cpg);
4296 +       au_pin_hdir_release(a->cpg->pin);
4297 +}
4298 +
4299 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
4300 +{
4301 +       int err, wkq_err;
4302 +       struct dentry *dentry, *parent;
4303 +       struct file *h_file;
4304 +       struct inode *h_dir;
4305 +
4306 +       dentry = cpg->dentry;
4307 +       h_file = NULL;
4308 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
4309 +               AuDebugOn(cpg->bsrc < 0);
4310 +               h_file = au_h_open_pre(dentry, cpg->bsrc);
4311 +               err = PTR_ERR(h_file);
4312 +               if (IS_ERR(h_file))
4313 +                       goto out;
4314 +       }
4315 +
4316 +       parent = dget_parent(dentry);
4317 +       h_dir = au_h_iptr(parent->d_inode, cpg->bdst);
4318 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
4319 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
4320 +               err = au_cpup_simple(cpg);
4321 +       else {
4322 +               struct au_cpup_simple_args args = {
4323 +                       .errp           = &err,
4324 +                       .cpg            = cpg
4325 +               };
4326 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
4327 +               if (unlikely(wkq_err))
4328 +                       err = wkq_err;
4329 +       }
4330 +
4331 +       dput(parent);
4332 +       if (h_file)
4333 +               au_h_open_post(dentry, cpg->bsrc, h_file);
4334 +
4335 +out:
4336 +       return err;
4337 +}
4338 +
4339 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
4340 +{
4341 +       aufs_bindex_t bsrc, bend;
4342 +       struct dentry *dentry, *h_dentry;
4343 +
4344 +       if (cpg->bsrc < 0) {
4345 +               dentry = cpg->dentry;
4346 +               bend = au_dbend(dentry);
4347 +               for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
4348 +                       h_dentry = au_h_dptr(dentry, bsrc);
4349 +                       if (h_dentry) {
4350 +                               AuDebugOn(!h_dentry->d_inode);
4351 +                               break;
4352 +                       }
4353 +               }
4354 +               AuDebugOn(bsrc > bend);
4355 +               cpg->bsrc = bsrc;
4356 +       }
4357 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
4358 +       return au_do_sio_cpup_simple(cpg);
4359 +}
4360 +
4361 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
4362 +{
4363 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
4364 +       return au_do_sio_cpup_simple(cpg);
4365 +}
4366 +
4367 +/* ---------------------------------------------------------------------- */
4368 +
4369 +/*
4370 + * copyup the deleted file for writing.
4371 + */
4372 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
4373 +                        struct file *file)
4374 +{
4375 +       int err;
4376 +       unsigned int flags_orig;
4377 +       aufs_bindex_t bsrc_orig;
4378 +       struct dentry *h_d_dst, *h_d_start;
4379 +       struct au_dinfo *dinfo;
4380 +       struct au_hdentry *hdp;
4381 +
4382 +       dinfo = au_di(cpg->dentry);
4383 +       AuRwMustWriteLock(&dinfo->di_rwsem);
4384 +
4385 +       bsrc_orig = cpg->bsrc;
4386 +       cpg->bsrc = dinfo->di_bstart;
4387 +       hdp = dinfo->di_hdentry;
4388 +       h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
4389 +       dinfo->di_bstart = cpg->bdst;
4390 +       hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
4391 +       h_d_start = NULL;
4392 +       if (file) {
4393 +               h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
4394 +               hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_dentry;
4395 +       }
4396 +       flags_orig = cpg->flags;
4397 +       cpg->flags = !AuCpup_DTIME;
4398 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
4399 +       cpg->flags = flags_orig;
4400 +       if (file) {
4401 +               if (!err)
4402 +                       err = au_reopen_nondir(file);
4403 +               hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
4404 +       }
4405 +       hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
4406 +       dinfo->di_bstart = cpg->bsrc;
4407 +       cpg->bsrc = bsrc_orig;
4408 +
4409 +       return err;
4410 +}
4411 +
4412 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
4413 +{
4414 +       int err;
4415 +       aufs_bindex_t bdst;
4416 +       struct au_dtime dt;
4417 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
4418 +       struct au_branch *br;
4419 +       struct path h_path;
4420 +
4421 +       dentry = cpg->dentry;
4422 +       bdst = cpg->bdst;
4423 +       br = au_sbr(dentry->d_sb, bdst);
4424 +       parent = dget_parent(dentry);
4425 +       h_parent = au_h_dptr(parent, bdst);
4426 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
4427 +       err = PTR_ERR(wh_dentry);
4428 +       if (IS_ERR(wh_dentry))
4429 +               goto out;
4430 +
4431 +       h_path.dentry = h_parent;
4432 +       h_path.mnt = au_br_mnt(br);
4433 +       au_dtime_store(&dt, parent, &h_path);
4434 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
4435 +       if (unlikely(err))
4436 +               goto out_wh;
4437 +
4438 +       dget(wh_dentry);
4439 +       h_path.dentry = wh_dentry;
4440 +       if (!S_ISDIR(wh_dentry->d_inode->i_mode))
4441 +               err = vfsub_unlink(h_parent->d_inode, &h_path, /*force*/0);
4442 +       else
4443 +               err = vfsub_rmdir(h_parent->d_inode, &h_path);
4444 +       if (unlikely(err)) {
4445 +               AuIOErr("failed remove copied-up tmp file %.*s(%d)\n",
4446 +                       AuDLNPair(wh_dentry), err);
4447 +               err = -EIO;
4448 +       }
4449 +       au_dtime_revert(&dt);
4450 +       au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
4451 +
4452 +out_wh:
4453 +       dput(wh_dentry);
4454 +out:
4455 +       dput(parent);
4456 +       return err;
4457 +}
4458 +
4459 +struct au_cpup_wh_args {
4460 +       int *errp;
4461 +       struct au_cp_generic *cpg;
4462 +       struct file *file;
4463 +};
4464 +
4465 +static void au_call_cpup_wh(void *args)
4466 +{
4467 +       struct au_cpup_wh_args *a = args;
4468 +
4469 +       au_pin_hdir_acquire_nest(a->cpg->pin);
4470 +       *a->errp = au_cpup_wh(a->cpg, a->file);
4471 +       au_pin_hdir_release(a->cpg->pin);
4472 +}
4473 +
4474 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
4475 +{
4476 +       int err, wkq_err;
4477 +       aufs_bindex_t bdst;
4478 +       struct dentry *dentry, *parent, *h_orph, *h_parent, *h_dentry;
4479 +       struct inode *dir, *h_dir, *h_tmpdir;
4480 +       struct au_wbr *wbr;
4481 +       struct au_pin wh_pin, *pin_orig;
4482 +
4483 +       dentry = cpg->dentry;
4484 +       bdst = cpg->bdst;
4485 +       parent = dget_parent(dentry);
4486 +       dir = parent->d_inode;
4487 +       h_orph = NULL;
4488 +       h_parent = NULL;
4489 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
4490 +       h_tmpdir = h_dir;
4491 +       pin_orig = NULL;
4492 +       if (!h_dir->i_nlink) {
4493 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
4494 +               h_orph = wbr->wbr_orph;
4495 +
4496 +               h_parent = dget(au_h_dptr(parent, bdst));
4497 +               au_set_h_dptr(parent, bdst, dget(h_orph));
4498 +               h_tmpdir = h_orph->d_inode;
4499 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
4500 +
4501 +               if (file)
4502 +                       h_dentry = au_hf_top(file)->f_dentry;
4503 +               else
4504 +                       h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
4505 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
4506 +               /* todo: au_h_open_pre()? */
4507 +
4508 +               pin_orig = cpg->pin;
4509 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
4510 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
4511 +               cpg->pin = &wh_pin;
4512 +       }
4513 +
4514 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
4515 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
4516 +               err = au_cpup_wh(cpg, file);
4517 +       else {
4518 +               struct au_cpup_wh_args args = {
4519 +                       .errp   = &err,
4520 +                       .cpg    = cpg,
4521 +                       .file   = file
4522 +               };
4523 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
4524 +               if (unlikely(wkq_err))
4525 +                       err = wkq_err;
4526 +       }
4527 +
4528 +       if (h_orph) {
4529 +               mutex_unlock(&h_tmpdir->i_mutex);
4530 +               /* todo: au_h_open_post()? */
4531 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
4532 +               au_set_h_dptr(parent, bdst, h_parent);
4533 +               AuDebugOn(!pin_orig);
4534 +               cpg->pin = pin_orig;
4535 +       }
4536 +       iput(h_dir);
4537 +       dput(parent);
4538 +
4539 +       return err;
4540 +}
4541 +
4542 +/* ---------------------------------------------------------------------- */
4543 +
4544 +/*
4545 + * generic routine for both of copy-up and copy-down.
4546 + */
4547 +/* cf. revalidate function in file.c */
4548 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
4549 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
4550 +                        struct au_pin *pin,
4551 +                        struct dentry *h_parent, void *arg),
4552 +              void *arg)
4553 +{
4554 +       int err;
4555 +       struct au_pin pin;
4556 +       struct dentry *d, *parent, *h_parent, *real_parent;
4557 +
4558 +       err = 0;
4559 +       parent = dget_parent(dentry);
4560 +       if (IS_ROOT(parent))
4561 +               goto out;
4562 +
4563 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
4564 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
4565 +
4566 +       /* do not use au_dpage */
4567 +       real_parent = parent;
4568 +       while (1) {
4569 +               dput(parent);
4570 +               parent = dget_parent(dentry);
4571 +               h_parent = au_h_dptr(parent, bdst);
4572 +               if (h_parent)
4573 +                       goto out; /* success */
4574 +
4575 +               /* find top dir which is necessary to cpup */
4576 +               do {
4577 +                       d = parent;
4578 +                       dput(parent);
4579 +                       parent = dget_parent(d);
4580 +                       di_read_lock_parent3(parent, !AuLock_IR);
4581 +                       h_parent = au_h_dptr(parent, bdst);
4582 +                       di_read_unlock(parent, !AuLock_IR);
4583 +               } while (!h_parent);
4584 +
4585 +               if (d != real_parent)
4586 +                       di_write_lock_child3(d);
4587 +
4588 +               /* somebody else might create while we were sleeping */
4589 +               if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
4590 +                       if (au_h_dptr(d, bdst))
4591 +                               au_update_dbstart(d);
4592 +
4593 +                       au_pin_set_dentry(&pin, d);
4594 +                       err = au_do_pin(&pin);
4595 +                       if (!err) {
4596 +                               err = cp(d, bdst, &pin, h_parent, arg);
4597 +                               au_unpin(&pin);
4598 +                       }
4599 +               }
4600 +
4601 +               if (d != real_parent)
4602 +                       di_write_unlock(d);
4603 +               if (unlikely(err))
4604 +                       break;
4605 +       }
4606 +
4607 +out:
4608 +       dput(parent);
4609 +       return err;
4610 +}
4611 +
4612 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
4613 +                      struct au_pin *pin,
4614 +                      struct dentry *h_parent __maybe_unused ,
4615 +                      void *arg __maybe_unused)
4616 +{
4617 +       struct au_cp_generic cpg = {
4618 +               .dentry = dentry,
4619 +               .bdst   = bdst,
4620 +               .bsrc   = -1,
4621 +               .len    = 0,
4622 +               .pin    = pin,
4623 +               .flags  = AuCpup_DTIME
4624 +       };
4625 +       return au_sio_cpup_simple(&cpg);
4626 +}
4627 +
4628 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
4629 +{
4630 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
4631 +}
4632 +
4633 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
4634 +{
4635 +       int err;
4636 +       struct dentry *parent;
4637 +       struct inode *dir;
4638 +
4639 +       parent = dget_parent(dentry);
4640 +       dir = parent->d_inode;
4641 +       err = 0;
4642 +       if (au_h_iptr(dir, bdst))
4643 +               goto out;
4644 +
4645 +       di_read_unlock(parent, AuLock_IR);
4646 +       di_write_lock_parent(parent);
4647 +       /* someone else might change our inode while we were sleeping */
4648 +       if (!au_h_iptr(dir, bdst))
4649 +               err = au_cpup_dirs(dentry, bdst);
4650 +       di_downgrade_lock(parent, AuLock_IR);
4651 +
4652 +out:
4653 +       dput(parent);
4654 +       return err;
4655 +}
4656 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
4657 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
4658 +++ linux/fs/aufs/cpup.h        2013-08-23 23:59:39.634916914 +0200
4659 @@ -0,0 +1,90 @@
4660 +/*
4661 + * Copyright (C) 2005-2013 Junjiro R. Okajima
4662 + *
4663 + * This program, aufs is free software; you can redistribute it and/or modify
4664 + * it under the terms of the GNU General Public License as published by
4665 + * the Free Software Foundation; either version 2 of the License, or
4666 + * (at your option) any later version.
4667 + *
4668 + * This program is distributed in the hope that it will be useful,
4669 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4670 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4671 + * GNU General Public License for more details.
4672 + *
4673 + * You should have received a copy of the GNU General Public License
4674 + * along with this program; if not, write to the Free Software
4675 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4676 + */
4677 +
4678 +/*
4679 + * copy-up/down functions
4680 + */
4681 +
4682 +#ifndef __AUFS_CPUP_H__
4683 +#define __AUFS_CPUP_H__
4684 +
4685 +#ifdef __KERNEL__
4686 +
4687 +#include <linux/path.h>
4688 +
4689 +struct inode;
4690 +struct file;
4691 +struct au_pin;
4692 +
4693 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
4694 +void au_cpup_attr_timesizes(struct inode *inode);
4695 +void au_cpup_attr_nlink(struct inode *inode, int force);
4696 +void au_cpup_attr_changeable(struct inode *inode);
4697 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
4698 +void au_cpup_attr_all(struct inode *inode, int force);
4699 +
4700 +/* ---------------------------------------------------------------------- */
4701 +
4702 +struct au_cp_generic {
4703 +       struct dentry   *dentry;
4704 +       aufs_bindex_t   bdst, bsrc;
4705 +       loff_t          len;
4706 +       struct au_pin   *pin;
4707 +       unsigned int    flags;
4708 +};
4709 +
4710 +/* cpup flags */
4711 +#define AuCpup_DTIME   1               /* do dtime_store/revert */
4712 +#define AuCpup_KEEPLINO        (1 << 1)        /* do not clear the lower xino,
4713 +                                          for link(2) */
4714 +#define AuCpup_RENAME  (1 << 2)        /* rename after cpup */
4715 +#define AuCpup_HOPEN   (1 << 3)        /* call h_open_pre/post() in cpup */
4716 +
4717 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
4718 +#define au_fset_cpup(flags, name) \
4719 +       do { (flags) |= AuCpup_##name; } while (0)
4720 +#define au_fclr_cpup(flags, name) \
4721 +       do { (flags) &= ~AuCpup_##name; } while (0)
4722 +
4723 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
4724 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
4725 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
4726 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
4727 +
4728 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
4729 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
4730 +                        struct au_pin *pin,
4731 +                        struct dentry *h_parent, void *arg),
4732 +              void *arg);
4733 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
4734 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
4735 +
4736 +/* ---------------------------------------------------------------------- */
4737 +
4738 +/* keep timestamps when copyup */
4739 +struct au_dtime {
4740 +       struct dentry *dt_dentry;
4741 +       struct path dt_h_path;
4742 +       struct timespec dt_atime, dt_mtime;
4743 +};
4744 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4745 +                   struct path *h_path);
4746 +void au_dtime_revert(struct au_dtime *dt);
4747 +
4748 +#endif /* __KERNEL__ */
4749 +#endif /* __AUFS_CPUP_H__ */
4750 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
4751 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
4752 +++ linux/fs/aufs/dbgaufs.c     2013-07-06 13:20:47.740198107 +0200
4753 @@ -0,0 +1,433 @@
4754 +/*
4755 + * Copyright (C) 2005-2013 Junjiro R. Okajima
4756 + *
4757 + * This program, aufs is free software; you can redistribute it and/or modify
4758 + * it under the terms of the GNU General Public License as published by
4759 + * the Free Software Foundation; either version 2 of the License, or
4760 + * (at your option) any later version.
4761 + *
4762 + * This program is distributed in the hope that it will be useful,
4763 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4764 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4765 + * GNU General Public License for more details.
4766 + *
4767 + * You should have received a copy of the GNU General Public License
4768 + * along with this program; if not, write to the Free Software
4769 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4770 + */
4771 +
4772 +/*
4773 + * debugfs interface
4774 + */
4775 +
4776 +#include <linux/debugfs.h>
4777 +#include "aufs.h"
4778 +
4779 +#ifndef CONFIG_SYSFS
4780 +#error DEBUG_FS depends upon SYSFS
4781 +#endif
4782 +
4783 +static struct dentry *dbgaufs;
4784 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
4785 +
4786 +/* 20 is max digits length of ulong 64 */
4787 +struct dbgaufs_arg {
4788 +       int n;
4789 +       char a[20 * 4];
4790 +};
4791 +
4792 +/*
4793 + * common function for all XINO files
4794 + */
4795 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
4796 +                             struct file *file)
4797 +{
4798 +       kfree(file->private_data);
4799 +       return 0;
4800 +}
4801 +
4802 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
4803 +{
4804 +       int err;
4805 +       struct kstat st;
4806 +       struct dbgaufs_arg *p;
4807 +
4808 +       err = -ENOMEM;
4809 +       p = kmalloc(sizeof(*p), GFP_NOFS);
4810 +       if (unlikely(!p))
4811 +               goto out;
4812 +
4813 +       err = 0;
4814 +       p->n = 0;
4815 +       file->private_data = p;
4816 +       if (!xf)
4817 +               goto out;
4818 +
4819 +       err = vfs_getattr(&xf->f_path, &st);
4820 +       if (!err) {
4821 +               if (do_fcnt)
4822 +                       p->n = snprintf
4823 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
4824 +                                (long)file_count(xf), st.blocks, st.blksize,
4825 +                                (long long)st.size);
4826 +               else
4827 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
4828 +                                       st.blocks, st.blksize,
4829 +                                       (long long)st.size);
4830 +               AuDebugOn(p->n >= sizeof(p->a));
4831 +       } else {
4832 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
4833 +               err = 0;
4834 +       }
4835 +
4836 +out:
4837 +       return err;
4838 +
4839 +}
4840 +
4841 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
4842 +                              size_t count, loff_t *ppos)
4843 +{
4844 +       struct dbgaufs_arg *p;
4845 +
4846 +       p = file->private_data;
4847 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
4848 +}
4849 +
4850 +/* ---------------------------------------------------------------------- */
4851 +
4852 +struct dbgaufs_plink_arg {
4853 +       int n;
4854 +       char a[];
4855 +};
4856 +
4857 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
4858 +                                struct file *file)
4859 +{
4860 +       free_page((unsigned long)file->private_data);
4861 +       return 0;
4862 +}
4863 +
4864 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
4865 +{
4866 +       int err, i, limit;
4867 +       unsigned long n, sum;
4868 +       struct dbgaufs_plink_arg *p;
4869 +       struct au_sbinfo *sbinfo;
4870 +       struct super_block *sb;
4871 +       struct au_sphlhead *sphl;
4872 +
4873 +       err = -ENOMEM;
4874 +       p = (void *)get_zeroed_page(GFP_NOFS);
4875 +       if (unlikely(!p))
4876 +               goto out;
4877 +
4878 +       err = -EFBIG;
4879 +       sbinfo = inode->i_private;
4880 +       sb = sbinfo->si_sb;
4881 +       si_noflush_read_lock(sb);
4882 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
4883 +               limit = PAGE_SIZE - sizeof(p->n);
4884 +
4885 +               /* the number of buckets */
4886 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
4887 +               p->n += n;
4888 +               limit -= n;
4889 +
4890 +               sum = 0;
4891 +               for (i = 0, sphl = sbinfo->si_plink;
4892 +                    i < AuPlink_NHASH;
4893 +                    i++, sphl++) {
4894 +                       n = au_sphl_count(sphl);
4895 +                       sum += n;
4896 +
4897 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
4898 +                       p->n += n;
4899 +                       limit -= n;
4900 +                       if (unlikely(limit <= 0))
4901 +                               goto out_free;
4902 +               }
4903 +               p->a[p->n - 1] = '\n';
4904 +
4905 +               /* the sum of plinks */
4906 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
4907 +               p->n += n;
4908 +               limit -= n;
4909 +               if (unlikely(limit <= 0))
4910 +                       goto out_free;
4911 +       } else {
4912 +#define str "1\n0\n0\n"
4913 +               p->n = sizeof(str) - 1;
4914 +               strcpy(p->a, str);
4915 +#undef str
4916 +       }
4917 +       si_read_unlock(sb);
4918 +
4919 +       err = 0;
4920 +       file->private_data = p;
4921 +       goto out; /* success */
4922 +
4923 +out_free:
4924 +       free_page((unsigned long)p);
4925 +out:
4926 +       return err;
4927 +}
4928 +
4929 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
4930 +                                 size_t count, loff_t *ppos)
4931 +{
4932 +       struct dbgaufs_plink_arg *p;
4933 +
4934 +       p = file->private_data;
4935 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
4936 +}
4937 +
4938 +static const struct file_operations dbgaufs_plink_fop = {
4939 +       .owner          = THIS_MODULE,
4940 +       .open           = dbgaufs_plink_open,
4941 +       .release        = dbgaufs_plink_release,
4942 +       .read           = dbgaufs_plink_read
4943 +};
4944 +
4945 +/* ---------------------------------------------------------------------- */
4946 +
4947 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
4948 +{
4949 +       int err;
4950 +       struct au_sbinfo *sbinfo;
4951 +       struct super_block *sb;
4952 +
4953 +       sbinfo = inode->i_private;
4954 +       sb = sbinfo->si_sb;
4955 +       si_noflush_read_lock(sb);
4956 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
4957 +       si_read_unlock(sb);
4958 +       return err;
4959 +}
4960 +
4961 +static const struct file_operations dbgaufs_xib_fop = {
4962 +       .owner          = THIS_MODULE,
4963 +       .open           = dbgaufs_xib_open,
4964 +       .release        = dbgaufs_xi_release,
4965 +       .read           = dbgaufs_xi_read
4966 +};
4967 +
4968 +/* ---------------------------------------------------------------------- */
4969 +
4970 +#define DbgaufsXi_PREFIX "xi"
4971 +
4972 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
4973 +{
4974 +       int err;
4975 +       long l;
4976 +       struct au_sbinfo *sbinfo;
4977 +       struct super_block *sb;
4978 +       struct file *xf;
4979 +       struct qstr *name;
4980 +
4981 +       err = -ENOENT;
4982 +       xf = NULL;
4983 +       name = &file->f_dentry->d_name;
4984 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
4985 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
4986 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
4987 +               goto out;
4988 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
4989 +       if (unlikely(err))
4990 +               goto out;
4991 +
4992 +       sbinfo = inode->i_private;
4993 +       sb = sbinfo->si_sb;
4994 +       si_noflush_read_lock(sb);
4995 +       if (l <= au_sbend(sb)) {
4996 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
4997 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
4998 +       } else
4999 +               err = -ENOENT;
5000 +       si_read_unlock(sb);
5001 +
5002 +out:
5003 +       return err;
5004 +}
5005 +
5006 +static const struct file_operations dbgaufs_xino_fop = {
5007 +       .owner          = THIS_MODULE,
5008 +       .open           = dbgaufs_xino_open,
5009 +       .release        = dbgaufs_xi_release,
5010 +       .read           = dbgaufs_xi_read
5011 +};
5012 +
5013 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
5014 +{
5015 +       aufs_bindex_t bend;
5016 +       struct au_branch *br;
5017 +       struct au_xino_file *xi;
5018 +
5019 +       if (!au_sbi(sb)->si_dbgaufs)
5020 +               return;
5021 +
5022 +       bend = au_sbend(sb);
5023 +       for (; bindex <= bend; bindex++) {
5024 +               br = au_sbr(sb, bindex);
5025 +               xi = &br->br_xino;
5026 +               debugfs_remove(xi->xi_dbgaufs);
5027 +               xi->xi_dbgaufs = NULL;
5028 +       }
5029 +}
5030 +
5031 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
5032 +{
5033 +       struct au_sbinfo *sbinfo;
5034 +       struct dentry *parent;
5035 +       struct au_branch *br;
5036 +       struct au_xino_file *xi;
5037 +       aufs_bindex_t bend;
5038 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
5039 +
5040 +       sbinfo = au_sbi(sb);
5041 +       parent = sbinfo->si_dbgaufs;
5042 +       if (!parent)
5043 +               return;
5044 +
5045 +       bend = au_sbend(sb);
5046 +       for (; bindex <= bend; bindex++) {
5047 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
5048 +               br = au_sbr(sb, bindex);
5049 +               xi = &br->br_xino;
5050 +               AuDebugOn(xi->xi_dbgaufs);
5051 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
5052 +                                                    sbinfo, &dbgaufs_xino_fop);
5053 +               /* ignore an error */
5054 +               if (unlikely(!xi->xi_dbgaufs))
5055 +                       AuWarn1("failed %s under debugfs\n", name);
5056 +       }
5057 +}
5058 +
5059 +/* ---------------------------------------------------------------------- */
5060 +
5061 +#ifdef CONFIG_AUFS_EXPORT
5062 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
5063 +{
5064 +       int err;
5065 +       struct au_sbinfo *sbinfo;
5066 +       struct super_block *sb;
5067 +
5068 +       sbinfo = inode->i_private;
5069 +       sb = sbinfo->si_sb;
5070 +       si_noflush_read_lock(sb);
5071 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
5072 +       si_read_unlock(sb);
5073 +       return err;
5074 +}
5075 +
5076 +static const struct file_operations dbgaufs_xigen_fop = {
5077 +       .owner          = THIS_MODULE,
5078 +       .open           = dbgaufs_xigen_open,
5079 +       .release        = dbgaufs_xi_release,
5080 +       .read           = dbgaufs_xi_read
5081 +};
5082 +
5083 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
5084 +{
5085 +       int err;
5086 +
5087 +       /*
5088 +        * This function is a dynamic '__init' fucntion actually,
5089 +        * so the tiny check for si_rwsem is unnecessary.
5090 +        */
5091 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
5092 +
5093 +       err = -EIO;
5094 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
5095 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5096 +                &dbgaufs_xigen_fop);
5097 +       if (sbinfo->si_dbgaufs_xigen)
5098 +               err = 0;
5099 +
5100 +       return err;
5101 +}
5102 +#else
5103 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
5104 +{
5105 +       return 0;
5106 +}
5107 +#endif /* CONFIG_AUFS_EXPORT */
5108 +
5109 +/* ---------------------------------------------------------------------- */
5110 +
5111 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
5112 +{
5113 +       /*
5114 +        * This function is a dynamic '__init' fucntion actually,
5115 +        * so the tiny check for si_rwsem is unnecessary.
5116 +        */
5117 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
5118 +
5119 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
5120 +       sbinfo->si_dbgaufs = NULL;
5121 +       kobject_put(&sbinfo->si_kobj);
5122 +}
5123 +
5124 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
5125 +{
5126 +       int err;
5127 +       char name[SysaufsSiNameLen];
5128 +
5129 +       /*
5130 +        * This function is a dynamic '__init' fucntion actually,
5131 +        * so the tiny check for si_rwsem is unnecessary.
5132 +        */
5133 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
5134 +
5135 +       err = -ENOENT;
5136 +       if (!dbgaufs) {
5137 +               AuErr1("/debug/aufs is uninitialized\n");
5138 +               goto out;
5139 +       }
5140 +
5141 +       err = -EIO;
5142 +       sysaufs_name(sbinfo, name);
5143 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
5144 +       if (unlikely(!sbinfo->si_dbgaufs))
5145 +               goto out;
5146 +       kobject_get(&sbinfo->si_kobj);
5147 +
5148 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
5149 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5150 +                &dbgaufs_xib_fop);
5151 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
5152 +               goto out_dir;
5153 +
5154 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
5155 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5156 +                &dbgaufs_plink_fop);
5157 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
5158 +               goto out_dir;
5159 +
5160 +       err = dbgaufs_xigen_init(sbinfo);
5161 +       if (!err)
5162 +               goto out; /* success */
5163 +
5164 +out_dir:
5165 +       dbgaufs_si_fin(sbinfo);
5166 +out:
5167 +       return err;
5168 +}
5169 +
5170 +/* ---------------------------------------------------------------------- */
5171 +
5172 +void dbgaufs_fin(void)
5173 +{
5174 +       debugfs_remove(dbgaufs);
5175 +}
5176 +
5177 +int __init dbgaufs_init(void)
5178 +{
5179 +       int err;
5180 +
5181 +       err = -EIO;
5182 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
5183 +       if (dbgaufs)
5184 +               err = 0;
5185 +       return err;
5186 +}
5187 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
5188 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
5189 +++ linux/fs/aufs/dbgaufs.h     2013-07-06 13:20:47.740198107 +0200
5190 @@ -0,0 +1,49 @@
5191 +/*
5192 + * Copyright (C) 2005-2013 Junjiro R. Okajima
5193 + *
5194 + * This program, aufs is free software; you can redistribute it and/or modify
5195 + * it under the terms of the GNU General Public License as published by
5196 + * the Free Software Foundation; either version 2 of the License, or
5197 + * (at your option) any later version.
5198 + *
5199 + * This program is distributed in the hope that it will be useful,
5200 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5201 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5202 + * GNU General Public License for more details.
5203 + *
5204 + * You should have received a copy of the GNU General Public License
5205 + * along with this program; if not, write to the Free Software
5206 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5207 + */
5208 +
5209 +/*
5210 + * debugfs interface
5211 + */
5212 +
5213 +#ifndef __DBGAUFS_H__
5214 +#define __DBGAUFS_H__
5215 +
5216 +#ifdef __KERNEL__
5217 +
5218 +struct super_block;
5219 +struct au_sbinfo;
5220 +
5221 +#ifdef CONFIG_DEBUG_FS
5222 +/* dbgaufs.c */
5223 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
5224 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
5225 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
5226 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
5227 +void dbgaufs_fin(void);
5228 +int __init dbgaufs_init(void);
5229 +#else
5230 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
5231 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
5232 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
5233 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
5234 +AuStubVoid(dbgaufs_fin, void)
5235 +AuStubInt0(__init dbgaufs_init, void)
5236 +#endif /* CONFIG_DEBUG_FS */
5237 +
5238 +#endif /* __KERNEL__ */
5239 +#endif /* __DBGAUFS_H__ */
5240 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
5241 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
5242 +++ linux/fs/aufs/dcsub.c       2013-07-30 22:42:55.839613269 +0200
5243 @@ -0,0 +1,243 @@
5244 +/*
5245 + * Copyright (C) 2005-2013 Junjiro R. Okajima
5246 + *
5247 + * This program, aufs is free software; you can redistribute it and/or modify
5248 + * it under the terms of the GNU General Public License as published by
5249 + * the Free Software Foundation; either version 2 of the License, or
5250 + * (at your option) any later version.
5251 + *
5252 + * This program is distributed in the hope that it will be useful,
5253 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5254 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5255 + * GNU General Public License for more details.
5256 + *
5257 + * You should have received a copy of the GNU General Public License
5258 + * along with this program; if not, write to the Free Software
5259 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5260 + */
5261 +
5262 +/*
5263 + * sub-routines for dentry cache
5264 + */
5265 +
5266 +#include "aufs.h"
5267 +
5268 +static void au_dpage_free(struct au_dpage *dpage)
5269 +{
5270 +       int i;
5271 +       struct dentry **p;
5272 +
5273 +       p = dpage->dentries;
5274 +       for (i = 0; i < dpage->ndentry; i++)
5275 +               dput(*p++);
5276 +       free_page((unsigned long)dpage->dentries);
5277 +}
5278 +
5279 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
5280 +{
5281 +       int err;
5282 +       void *p;
5283 +
5284 +       err = -ENOMEM;
5285 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
5286 +       if (unlikely(!dpages->dpages))
5287 +               goto out;
5288 +
5289 +       p = (void *)__get_free_page(gfp);
5290 +       if (unlikely(!p))
5291 +               goto out_dpages;
5292 +
5293 +       dpages->dpages[0].ndentry = 0;
5294 +       dpages->dpages[0].dentries = p;
5295 +       dpages->ndpage = 1;
5296 +       return 0; /* success */
5297 +
5298 +out_dpages:
5299 +       kfree(dpages->dpages);
5300 +out:
5301 +       return err;
5302 +}
5303 +
5304 +void au_dpages_free(struct au_dcsub_pages *dpages)
5305 +{
5306 +       int i;
5307 +       struct au_dpage *p;
5308 +
5309 +       p = dpages->dpages;
5310 +       for (i = 0; i < dpages->ndpage; i++)
5311 +               au_dpage_free(p++);
5312 +       kfree(dpages->dpages);
5313 +}
5314 +
5315 +static int au_dpages_append(struct au_dcsub_pages *dpages,
5316 +                           struct dentry *dentry, gfp_t gfp)
5317 +{
5318 +       int err, sz;
5319 +       struct au_dpage *dpage;
5320 +       void *p;
5321 +
5322 +       dpage = dpages->dpages + dpages->ndpage - 1;
5323 +       sz = PAGE_SIZE / sizeof(dentry);
5324 +       if (unlikely(dpage->ndentry >= sz)) {
5325 +               AuLabel(new dpage);
5326 +               err = -ENOMEM;
5327 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
5328 +               p = au_kzrealloc(dpages->dpages, sz,
5329 +                                sz + sizeof(*dpages->dpages), gfp);
5330 +               if (unlikely(!p))
5331 +                       goto out;
5332 +
5333 +               dpages->dpages = p;
5334 +               dpage = dpages->dpages + dpages->ndpage;
5335 +               p = (void *)__get_free_page(gfp);
5336 +               if (unlikely(!p))
5337 +                       goto out;
5338 +
5339 +               dpage->ndentry = 0;
5340 +               dpage->dentries = p;
5341 +               dpages->ndpage++;
5342 +       }
5343 +
5344 +       AuDebugOn(!dentry->d_count);
5345 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
5346 +       return 0; /* success */
5347 +
5348 +out:
5349 +       return err;
5350 +}
5351 +
5352 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
5353 +                  au_dpages_test test, void *arg)
5354 +{
5355 +       int err;
5356 +       struct dentry *this_parent;
5357 +       struct list_head *next;
5358 +       struct super_block *sb = root->d_sb;
5359 +
5360 +       err = 0;
5361 +       write_seqlock(&rename_lock);
5362 +       this_parent = root;
5363 +       spin_lock(&this_parent->d_lock);
5364 +repeat:
5365 +       next = this_parent->d_subdirs.next;
5366 +resume:
5367 +       if (this_parent->d_sb == sb
5368 +           && !IS_ROOT(this_parent)
5369 +           && au_di(this_parent)
5370 +           && this_parent->d_count
5371 +           && (!test || test(this_parent, arg))) {
5372 +               err = au_dpages_append(dpages, this_parent, GFP_ATOMIC);
5373 +               if (unlikely(err))
5374 +                       goto out;
5375 +       }
5376 +
5377 +       while (next != &this_parent->d_subdirs) {
5378 +               struct list_head *tmp = next;
5379 +               struct dentry *dentry = list_entry(tmp, struct dentry,
5380 +                                                  d_child);
5381 +
5382 +               next = tmp->next;
5383 +               spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
5384 +               if (dentry->d_count) {
5385 +                       if (!list_empty(&dentry->d_subdirs)) {
5386 +                               spin_unlock(&this_parent->d_lock);
5387 +                               spin_release(&dentry->d_lock.dep_map, 1,
5388 +                                            _RET_IP_);
5389 +                               this_parent = dentry;
5390 +                               spin_acquire(&this_parent->d_lock.dep_map, 0, 1,
5391 +                                            _RET_IP_);
5392 +                               goto repeat;
5393 +                       }
5394 +                       if (dentry->d_sb == sb
5395 +                           && au_di(dentry)
5396 +                           && (!test || test(dentry, arg)))
5397 +                               err = au_dpages_append(dpages, dentry,
5398 +                                                      GFP_ATOMIC);
5399 +               }
5400 +               spin_unlock(&dentry->d_lock);
5401 +               if (unlikely(err))
5402 +                       goto out;
5403 +       }
5404 +
5405 +       if (this_parent != root) {
5406 +               struct dentry *tmp;
5407 +               struct dentry *child;
5408 +
5409 +               tmp = this_parent->d_parent;
5410 +               rcu_read_lock();
5411 +               spin_unlock(&this_parent->d_lock);
5412 +               child = this_parent;
5413 +               this_parent = tmp;
5414 +               spin_lock(&this_parent->d_lock);
5415 +               rcu_read_unlock();
5416 +               next = child->d_child.next;
5417 +               goto resume;
5418 +       }
5419 +
5420 +out:
5421 +       spin_unlock(&this_parent->d_lock);
5422 +       write_sequnlock(&rename_lock);
5423 +       return err;
5424 +}
5425 +
5426 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5427 +                      int do_include, au_dpages_test test, void *arg)
5428 +{
5429 +       int err;
5430 +
5431 +       err = 0;
5432 +       write_seqlock(&rename_lock);
5433 +       spin_lock(&dentry->d_lock);
5434 +       if (do_include
5435 +           && dentry->d_count
5436 +           && (!test || test(dentry, arg)))
5437 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5438 +       spin_unlock(&dentry->d_lock);
5439 +       if (unlikely(err))
5440 +               goto out;
5441 +
5442 +       /*
5443 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
5444 +        * mount
5445 +        */
5446 +       while (!IS_ROOT(dentry)) {
5447 +               dentry = dentry->d_parent; /* rename_lock is locked */
5448 +               spin_lock(&dentry->d_lock);
5449 +               if (dentry->d_count
5450 +                   && (!test || test(dentry, arg)))
5451 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5452 +               spin_unlock(&dentry->d_lock);
5453 +               if (unlikely(err))
5454 +                       break;
5455 +       }
5456 +
5457 +out:
5458 +       write_sequnlock(&rename_lock);
5459 +       return err;
5460 +}
5461 +
5462 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
5463 +{
5464 +       return au_di(dentry) && dentry->d_sb == arg;
5465 +}
5466 +
5467 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
5468 +                           struct dentry *dentry, int do_include)
5469 +{
5470 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
5471 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
5472 +}
5473 +
5474 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
5475 +{
5476 +       struct path path[2] = {
5477 +               {
5478 +                       .dentry = d1
5479 +               },
5480 +               {
5481 +                       .dentry = d2
5482 +               }
5483 +       };
5484 +
5485 +       return path_is_under(path + 0, path + 1);
5486 +}
5487 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
5488 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
5489 +++ linux/fs/aufs/dcsub.h       2013-07-06 13:20:47.740198107 +0200
5490 @@ -0,0 +1,94 @@
5491 +/*
5492 + * Copyright (C) 2005-2013 Junjiro R. Okajima
5493 + *
5494 + * This program, aufs is free software; you can redistribute it and/or modify
5495 + * it under the terms of the GNU General Public License as published by
5496 + * the Free Software Foundation; either version 2 of the License, or
5497 + * (at your option) any later version.
5498 + *
5499 + * This program is distributed in the hope that it will be useful,
5500 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5501 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5502 + * GNU General Public License for more details.
5503 + *
5504 + * You should have received a copy of the GNU General Public License
5505 + * along with this program; if not, write to the Free Software
5506 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5507 + */
5508 +
5509 +/*
5510 + * sub-routines for dentry cache
5511 + */
5512 +
5513 +#ifndef __AUFS_DCSUB_H__
5514 +#define __AUFS_DCSUB_H__
5515 +
5516 +#ifdef __KERNEL__
5517 +
5518 +#include <linux/dcache.h>
5519 +#include <linux/fs.h>
5520 +
5521 +struct dentry;
5522 +
5523 +struct au_dpage {
5524 +       int ndentry;
5525 +       struct dentry **dentries;
5526 +};
5527 +
5528 +struct au_dcsub_pages {
5529 +       int ndpage;
5530 +       struct au_dpage *dpages;
5531 +};
5532 +
5533 +/* ---------------------------------------------------------------------- */
5534 +
5535 +/* dcsub.c */
5536 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
5537 +void au_dpages_free(struct au_dcsub_pages *dpages);
5538 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
5539 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
5540 +                  au_dpages_test test, void *arg);
5541 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5542 +                      int do_include, au_dpages_test test, void *arg);
5543 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
5544 +                           struct dentry *dentry, int do_include);
5545 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
5546 +
5547 +/* ---------------------------------------------------------------------- */
5548 +
5549 +static inline int au_d_hashed_positive(struct dentry *d)
5550 +{
5551 +       int err;
5552 +       struct inode *inode = d->d_inode;
5553 +       err = 0;
5554 +       if (unlikely(d_unhashed(d) || !inode || !inode->i_nlink))
5555 +               err = -ENOENT;
5556 +       return err;
5557 +}
5558 +
5559 +static inline int au_d_alive(struct dentry *d)
5560 +{
5561 +       int err;
5562 +       struct inode *inode;
5563 +       err = 0;
5564 +       if (!IS_ROOT(d))
5565 +               err = au_d_hashed_positive(d);
5566 +       else {
5567 +               inode = d->d_inode;
5568 +               if (unlikely(d_unlinked(d) || !inode || !inode->i_nlink))
5569 +                       err = -ENOENT;
5570 +       }
5571 +       return err;
5572 +}
5573 +
5574 +static inline int au_alive_dir(struct dentry *d)
5575 +{
5576 +       int err;
5577 +       err = au_d_alive(d);
5578 +       if (unlikely(err || IS_DEADDIR(d->d_inode)))
5579 +               err = -ENOENT;
5580 +       return err;
5581 +}
5582 +
5583 +#endif /* __KERNEL__ */
5584 +#endif /* __AUFS_DCSUB_H__ */
5585 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
5586 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
5587 +++ linux/fs/aufs/debug.c       2013-08-23 23:59:39.634916914 +0200
5588 @@ -0,0 +1,491 @@
5589 +/*
5590 + * Copyright (C) 2005-2013 Junjiro R. Okajima
5591 + *
5592 + * This program, aufs is free software; you can redistribute it and/or modify
5593 + * it under the terms of the GNU General Public License as published by
5594 + * the Free Software Foundation; either version 2 of the License, or
5595 + * (at your option) any later version.
5596 + *
5597 + * This program is distributed in the hope that it will be useful,
5598 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5599 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5600 + * GNU General Public License for more details.
5601 + *
5602 + * You should have received a copy of the GNU General Public License
5603 + * along with this program; if not, write to the Free Software
5604 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5605 + */
5606 +
5607 +/*
5608 + * debug print functions
5609 + */
5610 +
5611 +#include <linux/vt_kern.h>
5612 +#include "aufs.h"
5613 +
5614 +int aufs_debug;
5615 +MODULE_PARM_DESC(debug, "debug print");
5616 +module_param_named(debug, aufs_debug, int, S_IRUGO | S_IWUSR | S_IWGRP);
5617 +
5618 +char *au_plevel = KERN_DEBUG;
5619 +#define dpri(fmt, ...) do {                                    \
5620 +       if ((au_plevel                                          \
5621 +            && strcmp(au_plevel, KERN_DEBUG))                  \
5622 +           || au_debug_test())                                 \
5623 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
5624 +} while (0)
5625 +
5626 +/* ---------------------------------------------------------------------- */
5627 +
5628 +void au_dpri_whlist(struct au_nhash *whlist)
5629 +{
5630 +       unsigned long ul, n;
5631 +       struct hlist_head *head;
5632 +       struct au_vdir_wh *pos;
5633 +
5634 +       n = whlist->nh_num;
5635 +       head = whlist->nh_head;
5636 +       for (ul = 0; ul < n; ul++) {
5637 +               hlist_for_each_entry(pos, head, wh_hash)
5638 +                       dpri("b%d, %.*s, %d\n",
5639 +                            pos->wh_bindex,
5640 +                            pos->wh_str.len, pos->wh_str.name,
5641 +                            pos->wh_str.len);
5642 +               head++;
5643 +       }
5644 +}
5645 +
5646 +void au_dpri_vdir(struct au_vdir *vdir)
5647 +{
5648 +       unsigned long ul;
5649 +       union au_vdir_deblk_p p;
5650 +       unsigned char *o;
5651 +
5652 +       if (!vdir || IS_ERR(vdir)) {
5653 +               dpri("err %ld\n", PTR_ERR(vdir));
5654 +               return;
5655 +       }
5656 +
5657 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
5658 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
5659 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
5660 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
5661 +               p.deblk = vdir->vd_deblk[ul];
5662 +               o = p.deblk;
5663 +               dpri("[%lu]: %p\n", ul, o);
5664 +       }
5665 +}
5666 +
5667 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
5668 +                       struct dentry *wh)
5669 +{
5670 +       char *n = NULL;
5671 +       int l = 0;
5672 +
5673 +       if (!inode || IS_ERR(inode)) {
5674 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
5675 +               return -1;
5676 +       }
5677 +
5678 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
5679 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
5680 +                    && sizeof(inode->i_blocks) != sizeof(u64));
5681 +       if (wh) {
5682 +               n = (void *)wh->d_name.name;
5683 +               l = wh->d_name.len;
5684 +       }
5685 +
5686 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
5687 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
5688 +            bindex, inode,
5689 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
5690 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
5691 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
5692 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
5693 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
5694 +            inode->i_state, inode->i_flags, inode->i_version,
5695 +            inode->i_generation,
5696 +            l ? ", wh " : "", l, n);
5697 +       return 0;
5698 +}
5699 +
5700 +void au_dpri_inode(struct inode *inode)
5701 +{
5702 +       struct au_iinfo *iinfo;
5703 +       aufs_bindex_t bindex;
5704 +       int err, hn;
5705 +
5706 +       err = do_pri_inode(-1, inode, -1, NULL);
5707 +       if (err || !au_test_aufs(inode->i_sb))
5708 +               return;
5709 +
5710 +       iinfo = au_ii(inode);
5711 +       if (!iinfo)
5712 +               return;
5713 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
5714 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
5715 +       if (iinfo->ii_bstart < 0)
5716 +               return;
5717 +       hn = 0;
5718 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
5719 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
5720 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
5721 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
5722 +       }
5723 +}
5724 +
5725 +void au_dpri_dalias(struct inode *inode)
5726 +{
5727 +       struct dentry *d;
5728 +
5729 +       spin_lock(&inode->i_lock);
5730 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
5731 +               au_dpri_dentry(d);
5732 +       spin_unlock(&inode->i_lock);
5733 +}
5734 +
5735 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
5736 +{
5737 +       struct dentry *wh = NULL;
5738 +       int hn;
5739 +
5740 +       if (!dentry || IS_ERR(dentry)) {
5741 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
5742 +               return -1;
5743 +       }
5744 +       /* do not call dget_parent() here */
5745 +       /* note: access d_xxx without d_lock */
5746 +       dpri("d%d: %.*s?/%.*s, %s, cnt %d, flags 0x%x\n",
5747 +            bindex,
5748 +            AuDLNPair(dentry->d_parent), AuDLNPair(dentry),
5749 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
5750 +            dentry->d_count, dentry->d_flags);
5751 +       hn = -1;
5752 +       if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
5753 +               struct au_iinfo *iinfo = au_ii(dentry->d_inode);
5754 +               if (iinfo) {
5755 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
5756 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
5757 +               }
5758 +       }
5759 +       do_pri_inode(bindex, dentry->d_inode, hn, wh);
5760 +       return 0;
5761 +}
5762 +
5763 +void au_dpri_dentry(struct dentry *dentry)
5764 +{
5765 +       struct au_dinfo *dinfo;
5766 +       aufs_bindex_t bindex;
5767 +       int err;
5768 +       struct au_hdentry *hdp;
5769 +
5770 +       err = do_pri_dentry(-1, dentry);
5771 +       if (err || !au_test_aufs(dentry->d_sb))
5772 +               return;
5773 +
5774 +       dinfo = au_di(dentry);
5775 +       if (!dinfo)
5776 +               return;
5777 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d\n",
5778 +            dinfo->di_bstart, dinfo->di_bend,
5779 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry));
5780 +       if (dinfo->di_bstart < 0)
5781 +               return;
5782 +       hdp = dinfo->di_hdentry;
5783 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
5784 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
5785 +}
5786 +
5787 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
5788 +{
5789 +       char a[32];
5790 +
5791 +       if (!file || IS_ERR(file)) {
5792 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
5793 +               return -1;
5794 +       }
5795 +       a[0] = 0;
5796 +       if (bindex < 0
5797 +           && file->f_dentry
5798 +           && au_test_aufs(file->f_dentry->d_sb)
5799 +           && au_fi(file))
5800 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
5801 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
5802 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
5803 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
5804 +            file->f_version, file->f_pos, a);
5805 +       if (file->f_dentry)
5806 +               do_pri_dentry(bindex, file->f_dentry);
5807 +       return 0;
5808 +}
5809 +
5810 +void au_dpri_file(struct file *file)
5811 +{
5812 +       struct au_finfo *finfo;
5813 +       struct au_fidir *fidir;
5814 +       struct au_hfile *hfile;
5815 +       aufs_bindex_t bindex;
5816 +       int err;
5817 +
5818 +       err = do_pri_file(-1, file);
5819 +       if (err || !file->f_dentry || !au_test_aufs(file->f_dentry->d_sb))
5820 +               return;
5821 +
5822 +       finfo = au_fi(file);
5823 +       if (!finfo)
5824 +               return;
5825 +       if (finfo->fi_btop < 0)
5826 +               return;
5827 +       fidir = finfo->fi_hdir;
5828 +       if (!fidir)
5829 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
5830 +       else
5831 +               for (bindex = finfo->fi_btop;
5832 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
5833 +                    bindex++) {
5834 +                       hfile = fidir->fd_hfile + bindex;
5835 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
5836 +               }
5837 +}
5838 +
5839 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
5840 +{
5841 +       struct vfsmount *mnt;
5842 +       struct super_block *sb;
5843 +
5844 +       if (!br || IS_ERR(br))
5845 +               goto out;
5846 +       mnt = au_br_mnt(br);
5847 +       if (!mnt || IS_ERR(mnt))
5848 +               goto out;
5849 +       sb = mnt->mnt_sb;
5850 +       if (!sb || IS_ERR(sb))
5851 +               goto out;
5852 +
5853 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
5854 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
5855 +            "xino %d\n",
5856 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
5857 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
5858 +            sb->s_flags, sb->s_count,
5859 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
5860 +       return 0;
5861 +
5862 +out:
5863 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
5864 +       return -1;
5865 +}
5866 +
5867 +void au_dpri_sb(struct super_block *sb)
5868 +{
5869 +       struct au_sbinfo *sbinfo;
5870 +       aufs_bindex_t bindex;
5871 +       int err;
5872 +       /* to reuduce stack size */
5873 +       struct {
5874 +               struct vfsmount mnt;
5875 +               struct au_branch fake;
5876 +       } *a;
5877 +
5878 +       /* this function can be called from magic sysrq */
5879 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
5880 +       if (unlikely(!a)) {
5881 +               dpri("no memory\n");
5882 +               return;
5883 +       }
5884 +
5885 +       a->mnt.mnt_sb = sb;
5886 +       a->fake.br_perm = 0;
5887 +       a->fake.br_path.mnt = &a->mnt;
5888 +       a->fake.br_xino.xi_file = NULL;
5889 +       atomic_set(&a->fake.br_count, 0);
5890 +       smp_mb(); /* atomic_set */
5891 +       err = do_pri_br(-1, &a->fake);
5892 +       kfree(a);
5893 +       dpri("dev 0x%x\n", sb->s_dev);
5894 +       if (err || !au_test_aufs(sb))
5895 +               return;
5896 +
5897 +       sbinfo = au_sbi(sb);
5898 +       if (!sbinfo)
5899 +               return;
5900 +       dpri("nw %d, gen %u, kobj %d\n",
5901 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
5902 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
5903 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
5904 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
5905 +}
5906 +
5907 +/* ---------------------------------------------------------------------- */
5908 +
5909 +void au_dbg_sleep_jiffy(int jiffy)
5910 +{
5911 +       while (jiffy)
5912 +               jiffy = schedule_timeout_uninterruptible(jiffy);
5913 +}
5914 +
5915 +void au_dbg_iattr(struct iattr *ia)
5916 +{
5917 +#define AuBit(name)                                    \
5918 +       do {                                            \
5919 +               if (ia->ia_valid & ATTR_ ## name)       \
5920 +                       dpri(#name "\n");               \
5921 +       } while (0)
5922 +       AuBit(MODE);
5923 +       AuBit(UID);
5924 +       AuBit(GID);
5925 +       AuBit(SIZE);
5926 +       AuBit(ATIME);
5927 +       AuBit(MTIME);
5928 +       AuBit(CTIME);
5929 +       AuBit(ATIME_SET);
5930 +       AuBit(MTIME_SET);
5931 +       AuBit(FORCE);
5932 +       AuBit(ATTR_FLAG);
5933 +       AuBit(KILL_SUID);
5934 +       AuBit(KILL_SGID);
5935 +       AuBit(FILE);
5936 +       AuBit(KILL_PRIV);
5937 +       AuBit(OPEN);
5938 +       AuBit(TIMES_SET);
5939 +#undef AuBit
5940 +       dpri("ia_file %p\n", ia->ia_file);
5941 +}
5942 +
5943 +/* ---------------------------------------------------------------------- */
5944 +
5945 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
5946 +{
5947 +       struct inode *h_inode, *inode = dentry->d_inode;
5948 +       struct dentry *h_dentry;
5949 +       aufs_bindex_t bindex, bend, bi;
5950 +
5951 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
5952 +               return;
5953 +
5954 +       bend = au_dbend(dentry);
5955 +       bi = au_ibend(inode);
5956 +       if (bi < bend)
5957 +               bend = bi;
5958 +       bindex = au_dbstart(dentry);
5959 +       bi = au_ibstart(inode);
5960 +       if (bi > bindex)
5961 +               bindex = bi;
5962 +
5963 +       for (; bindex <= bend; bindex++) {
5964 +               h_dentry = au_h_dptr(dentry, bindex);
5965 +               if (!h_dentry)
5966 +                       continue;
5967 +               h_inode = au_h_iptr(inode, bindex);
5968 +               if (unlikely(h_inode != h_dentry->d_inode)) {
5969 +                       int old = au_debug_test();
5970 +                       if (!old)
5971 +                               au_debug(1);
5972 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
5973 +                       AuDbgDentry(dentry);
5974 +                       AuDbgInode(inode);
5975 +                       if (!old)
5976 +                               au_debug(0);
5977 +                       BUG();
5978 +               }
5979 +       }
5980 +}
5981 +
5982 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen)
5983 +{
5984 +       struct dentry *parent;
5985 +
5986 +       parent = dget_parent(dentry);
5987 +       AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
5988 +       AuDebugOn(IS_ROOT(dentry));
5989 +       AuDebugOn(au_digen_test(parent, sigen));
5990 +       dput(parent);
5991 +}
5992 +
5993 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen)
5994 +{
5995 +       struct dentry *parent;
5996 +       struct inode *inode;
5997 +
5998 +       parent = dget_parent(dentry);
5999 +       inode = dentry->d_inode;
6000 +       AuDebugOn(inode && S_ISDIR(dentry->d_inode->i_mode));
6001 +       AuDebugOn(au_digen_test(parent, sigen));
6002 +       dput(parent);
6003 +}
6004 +
6005 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
6006 +{
6007 +       int err, i, j;
6008 +       struct au_dcsub_pages dpages;
6009 +       struct au_dpage *dpage;
6010 +       struct dentry **dentries;
6011 +
6012 +       err = au_dpages_init(&dpages, GFP_NOFS);
6013 +       AuDebugOn(err);
6014 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
6015 +       AuDebugOn(err);
6016 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
6017 +               dpage = dpages.dpages + i;
6018 +               dentries = dpage->dentries;
6019 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
6020 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
6021 +       }
6022 +       au_dpages_free(&dpages);
6023 +}
6024 +
6025 +void au_dbg_verify_kthread(void)
6026 +{
6027 +       if (au_wkq_test()) {
6028 +               au_dbg_blocked();
6029 +               /*
6030 +                * It may be recursive, but udba=notify between two aufs mounts,
6031 +                * where a single ro branch is shared, is not a problem.
6032 +                */
6033 +               /* WARN_ON(1); */
6034 +       }
6035 +}
6036 +
6037 +/* ---------------------------------------------------------------------- */
6038 +
6039 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo __maybe_unused)
6040 +{
6041 +#ifdef AuForceNoPlink
6042 +       au_opt_clr(sbinfo->si_mntflags, PLINK);
6043 +#endif
6044 +#ifdef AuForceNoXino
6045 +       au_opt_clr(sbinfo->si_mntflags, XINO);
6046 +#endif
6047 +#ifdef AuForceNoRefrof
6048 +       au_opt_clr(sbinfo->si_mntflags, REFROF);
6049 +#endif
6050 +#ifdef AuForceHnotify
6051 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_HNOTIFY);
6052 +#endif
6053 +#ifdef AuForceRd0
6054 +       sbinfo->si_rdblk = 0;
6055 +       sbinfo->si_rdhash = 0;
6056 +#endif
6057 +}
6058 +
6059 +int __init au_debug_init(void)
6060 +{
6061 +       aufs_bindex_t bindex;
6062 +       struct au_vdir_destr destr;
6063 +
6064 +       bindex = -1;
6065 +       AuDebugOn(bindex >= 0);
6066 +
6067 +       destr.len = -1;
6068 +       AuDebugOn(destr.len < NAME_MAX);
6069 +
6070 +#ifdef CONFIG_4KSTACKS
6071 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
6072 +#endif
6073 +
6074 +#ifdef AuForceNoBrs
6075 +       sysaufs_brs = 0;
6076 +#endif
6077 +
6078 +       return 0;
6079 +}
6080 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
6081 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
6082 +++ linux/fs/aufs/debug.h       2013-07-06 13:20:47.740198107 +0200
6083 @@ -0,0 +1,242 @@
6084 +/*
6085 + * Copyright (C) 2005-2013 Junjiro R. Okajima
6086 + *
6087 + * This program, aufs is free software; you can redistribute it and/or modify
6088 + * it under the terms of the GNU General Public License as published by
6089 + * the Free Software Foundation; either version 2 of the License, or
6090 + * (at your option) any later version.
6091 + *
6092 + * This program is distributed in the hope that it will be useful,
6093 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6094 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6095 + * GNU General Public License for more details.
6096 + *
6097 + * You should have received a copy of the GNU General Public License
6098 + * along with this program; if not, write to the Free Software
6099 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
6100 + */
6101 +
6102 +/*
6103 + * debug print functions
6104 + */
6105 +
6106 +#ifndef __AUFS_DEBUG_H__
6107 +#define __AUFS_DEBUG_H__
6108 +
6109 +#ifdef __KERNEL__
6110 +
6111 +#include <linux/module.h>
6112 +#include <linux/kallsyms.h>
6113 +#include <linux/sysrq.h>
6114 +
6115 +#ifdef CONFIG_AUFS_DEBUG
6116 +#define AuDebugOn(a)           BUG_ON(a)
6117 +
6118 +/* module parameter */
6119 +extern int aufs_debug;
6120 +static inline void au_debug(int n)
6121 +{
6122 +       aufs_debug = n;
6123 +       smp_mb();
6124 +}
6125 +
6126 +static inline int au_debug_test(void)
6127 +{
6128 +       return aufs_debug;
6129 +}
6130 +#else
6131 +#define AuDebugOn(a)           do {} while (0)
6132 +AuStubVoid(au_debug, int n)
6133 +AuStubInt0(au_debug_test, void)
6134 +#endif /* CONFIG_AUFS_DEBUG */
6135 +
6136 +/* ---------------------------------------------------------------------- */
6137 +
6138 +/* debug print */
6139 +
6140 +#define AuDbg(fmt, ...) do { \
6141 +       if (au_debug_test()) \
6142 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
6143 +} while (0)
6144 +#define AuLabel(l)             AuDbg(#l "\n")
6145 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
6146 +#define AuWarn1(fmt, ...) do { \
6147 +       static unsigned char _c; \
6148 +       if (!_c++) \
6149 +               pr_warn(fmt, ##__VA_ARGS__); \
6150 +} while (0)
6151 +
6152 +#define AuErr1(fmt, ...) do { \
6153 +       static unsigned char _c; \
6154 +       if (!_c++) \
6155 +               pr_err(fmt, ##__VA_ARGS__); \
6156 +} while (0)
6157 +
6158 +#define AuIOErr1(fmt, ...) do { \
6159 +       static unsigned char _c; \
6160 +       if (!_c++) \
6161 +               AuIOErr(fmt, ##__VA_ARGS__); \
6162 +} while (0)
6163 +
6164 +#define AuUnsupportMsg "This operation is not supported." \
6165 +                       " Please report this application to aufs-users ML."
6166 +#define AuUnsupport(fmt, ...) do { \
6167 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
6168 +       dump_stack(); \
6169 +} while (0)
6170 +
6171 +#define AuTraceErr(e) do { \
6172 +       if (unlikely((e) < 0)) \
6173 +               AuDbg("err %d\n", (int)(e)); \
6174 +} while (0)
6175 +
6176 +#define AuTraceErrPtr(p) do { \
6177 +       if (IS_ERR(p)) \
6178 +               AuDbg("err %ld\n", PTR_ERR(p)); \
6179 +} while (0)
6180 +
6181 +/* dirty macros for debug print, use with "%.*s" and caution */
6182 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
6183 +#define AuDLNPair(d)           AuLNPair(&(d)->d_name)
6184 +
6185 +/* ---------------------------------------------------------------------- */
6186 +
6187 +struct au_sbinfo;
6188 +struct au_finfo;
6189 +struct dentry;
6190 +#ifdef CONFIG_AUFS_DEBUG
6191 +extern char *au_plevel;
6192 +struct au_nhash;
6193 +void au_dpri_whlist(struct au_nhash *whlist);
6194 +struct au_vdir;
6195 +void au_dpri_vdir(struct au_vdir *vdir);
6196 +struct inode;
6197 +void au_dpri_inode(struct inode *inode);
6198 +void au_dpri_dalias(struct inode *inode);
6199 +void au_dpri_dentry(struct dentry *dentry);
6200 +struct file;
6201 +void au_dpri_file(struct file *filp);
6202 +struct super_block;
6203 +void au_dpri_sb(struct super_block *sb);
6204 +
6205 +void au_dbg_sleep_jiffy(int jiffy);
6206 +struct iattr;
6207 +void au_dbg_iattr(struct iattr *ia);
6208 +
6209 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
6210 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
6211 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen);
6212 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen);
6213 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
6214 +void au_dbg_verify_kthread(void);
6215 +
6216 +int __init au_debug_init(void);
6217 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo);
6218 +#define AuDbgWhlist(w) do { \
6219 +       AuDbg(#w "\n"); \
6220 +       au_dpri_whlist(w); \
6221 +} while (0)
6222 +
6223 +#define AuDbgVdir(v) do { \
6224 +       AuDbg(#v "\n"); \
6225 +       au_dpri_vdir(v); \
6226 +} while (0)
6227 +
6228 +#define AuDbgInode(i) do { \
6229 +       AuDbg(#i "\n"); \
6230 +       au_dpri_inode(i); \
6231 +} while (0)
6232 +
6233 +#define AuDbgDAlias(i) do { \
6234 +       AuDbg(#i "\n"); \
6235 +       au_dpri_dalias(i); \
6236 +} while (0)
6237 +
6238 +#define AuDbgDentry(d) do { \
6239 +       AuDbg(#d "\n"); \
6240 +       au_dpri_dentry(d); \
6241 +} while (0)
6242 +
6243 +#define AuDbgFile(f) do { \
6244 +       AuDbg(#f "\n"); \
6245 +       au_dpri_file(f); \
6246 +} while (0)
6247 +
6248 +#define AuDbgSb(sb) do { \
6249 +       AuDbg(#sb "\n"); \
6250 +       au_dpri_sb(sb); \
6251 +} while (0)
6252 +
6253 +#define AuDbgSleep(sec) do { \
6254 +       AuDbg("sleep %d sec\n", sec); \
6255 +       ssleep(sec); \
6256 +} while (0)
6257 +
6258 +#define AuDbgSleepJiffy(jiffy) do { \
6259 +       AuDbg("sleep %d jiffies\n", jiffy); \
6260 +       au_dbg_sleep_jiffy(jiffy); \
6261 +} while (0)
6262 +
6263 +#define AuDbgIAttr(ia) do { \
6264 +       AuDbg("ia_valid 0x%x\n", (ia)->ia_valid); \
6265 +       au_dbg_iattr(ia); \
6266 +} while (0)
6267 +
6268 +#define AuDbgSym(addr) do {                            \
6269 +       char sym[KSYM_SYMBOL_LEN];                      \
6270 +       sprint_symbol(sym, (unsigned long)addr);        \
6271 +       AuDbg("%s\n", sym);                             \
6272 +} while (0)
6273 +
6274 +#define AuInfoSym(addr) do {                           \
6275 +       char sym[KSYM_SYMBOL_LEN];                      \
6276 +       sprint_symbol(sym, (unsigned long)addr);        \
6277 +       AuInfo("%s\n", sym);                            \
6278 +} while (0)
6279 +#else
6280 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
6281 +AuStubVoid(au_dbg_verify_dir_parent, struct dentry *dentry, unsigned int sigen)
6282 +AuStubVoid(au_dbg_verify_nondir_parent, struct dentry *dentry,
6283 +          unsigned int sigen)
6284 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
6285 +AuStubVoid(au_dbg_verify_kthread, void)
6286 +AuStubInt0(__init au_debug_init, void)
6287 +AuStubVoid(au_debug_sbinfo_init, struct au_sbinfo *sbinfo)
6288 +
6289 +#define AuDbgWhlist(w)         do {} while (0)
6290 +#define AuDbgVdir(v)           do {} while (0)
6291 +#define AuDbgInode(i)          do {} while (0)
6292 +#define AuDbgDAlias(i)         do {} while (0)
6293 +#define AuDbgDentry(d)         do {} while (0)
6294 +#define AuDbgFile(f)           do {} while (0)
6295 +#define AuDbgSb(sb)            do {} while (0)
6296 +#define AuDbgSleep(sec)                do {} while (0)
6297 +#define AuDbgSleepJiffy(jiffy) do {} while (0)
6298 +#define AuDbgIAttr(ia)         do {} while (0)
6299 +#define AuDbgSym(addr)         do {} while (0)
6300 +#define AuInfoSym(addr)                do {} while (0)
6301 +#endif /* CONFIG_AUFS_DEBUG */
6302 +
6303 +/* ---------------------------------------------------------------------- */
6304 +
6305 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
6306 +int __init au_sysrq_init(void);
6307 +void au_sysrq_fin(void);
6308 +
6309 +#ifdef CONFIG_HW_CONSOLE
6310 +#define au_dbg_blocked() do { \
6311 +       WARN_ON(1); \
6312 +       handle_sysrq('w'); \
6313 +} while (0)
6314 +#else
6315 +AuStubVoid(au_dbg_blocked, void)
6316 +#endif
6317 +
6318 +#else
6319 +AuStubInt0(__init au_sysrq_init, void)
6320 +AuStubVoid(au_sysrq_fin, void)
6321 +AuStubVoid(au_dbg_blocked, void)
6322 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
6323 +
6324 +#endif /* __KERNEL__ */
6325 +#endif /* __AUFS_DEBUG_H__ */
6326 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
6327 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
6328 +++ linux/fs/aufs/dentry.c      2013-07-06 13:20:47.740198107 +0200
6329 @@ -0,0 +1,1065 @@
6330 +/*
6331 + * Copyright (C) 2005-2013 Junjiro R. Okajima
6332 + *
6333 + * This program, aufs is free software; you can redistribute it and/or modify
6334 + * it under the terms of the GNU General Public License as published by
6335 + * the Free Software Foundation; either version 2 of the License, or
6336 + * (at your option) any later version.
6337 + *
6338 + * This program is distributed in the hope that it will be useful,
6339 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6340 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6341 + * GNU General Public License for more details.
6342 + *
6343 + * You should have received a copy of the GNU General Public License
6344 + * along with this program; if not, write to the Free Software
6345 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
6346 + */
6347 +
6348 +/*
6349 + * lookup and dentry operations
6350 + */
6351 +
6352 +#include <linux/namei.h>
6353 +#include "aufs.h"
6354 +
6355 +#define AuLkup_ALLOW_NEG       1
6356 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
6357 +#define au_fset_lkup(flags, name) \
6358 +       do { (flags) |= AuLkup_##name; } while (0)
6359 +#define au_fclr_lkup(flags, name) \
6360 +       do { (flags) &= ~AuLkup_##name; } while (0)
6361 +
6362 +struct au_do_lookup_args {
6363 +       unsigned int            flags;
6364 +       mode_t                  type;
6365 +};
6366 +
6367 +/*
6368 + * returns positive/negative dentry, NULL or an error.
6369 + * NULL means whiteout-ed or not-found.
6370 + */
6371 +static struct dentry*
6372 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
6373 +            aufs_bindex_t bindex, struct qstr *wh_name,
6374 +            struct au_do_lookup_args *args)
6375 +{
6376 +       struct dentry *h_dentry;
6377 +       struct inode *h_inode, *inode;
6378 +       struct au_branch *br;
6379 +       int wh_found, opq;
6380 +       unsigned char wh_able;
6381 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
6382 +
6383 +       wh_found = 0;
6384 +       br = au_sbr(dentry->d_sb, bindex);
6385 +       wh_able = !!au_br_whable(br->br_perm);
6386 +       if (wh_able)
6387 +               wh_found = au_wh_test(h_parent, wh_name, br, /*try_sio*/0);
6388 +       h_dentry = ERR_PTR(wh_found);
6389 +       if (!wh_found)
6390 +               goto real_lookup;
6391 +       if (unlikely(wh_found < 0))
6392 +               goto out;
6393 +
6394 +       /* We found a whiteout */
6395 +       /* au_set_dbend(dentry, bindex); */
6396 +       au_set_dbwh(dentry, bindex);
6397 +       if (!allow_neg)
6398 +               return NULL; /* success */
6399 +
6400 +real_lookup:
6401 +       h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
6402 +       if (IS_ERR(h_dentry))
6403 +               goto out;
6404 +
6405 +       h_inode = h_dentry->d_inode;
6406 +       if (!h_inode) {
6407 +               if (!allow_neg)
6408 +                       goto out_neg;
6409 +       } else if (wh_found
6410 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
6411 +               goto out_neg;
6412 +
6413 +       if (au_dbend(dentry) <= bindex)
6414 +               au_set_dbend(dentry, bindex);
6415 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
6416 +               au_set_dbstart(dentry, bindex);
6417 +       au_set_h_dptr(dentry, bindex, h_dentry);
6418 +
6419 +       inode = dentry->d_inode;
6420 +       if (!h_inode || !S_ISDIR(h_inode->i_mode) || !wh_able
6421 +           || (inode && !S_ISDIR(inode->i_mode)))
6422 +               goto out; /* success */
6423 +
6424 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
6425 +       opq = au_diropq_test(h_dentry, br);
6426 +       mutex_unlock(&h_inode->i_mutex);
6427 +       if (opq > 0)
6428 +               au_set_dbdiropq(dentry, bindex);
6429 +       else if (unlikely(opq < 0)) {
6430 +               au_set_h_dptr(dentry, bindex, NULL);
6431 +               h_dentry = ERR_PTR(opq);
6432 +       }
6433 +       goto out;
6434 +
6435 +out_neg:
6436 +       dput(h_dentry);
6437 +       h_dentry = NULL;
6438 +out:
6439 +       return h_dentry;
6440 +}
6441 +
6442 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
6443 +{
6444 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
6445 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
6446 +               return -EPERM;
6447 +       return 0;
6448 +}
6449 +
6450 +/*
6451 + * returns the number of lower positive dentries,
6452 + * otherwise an error.
6453 + * can be called at unlinking with @type is zero.
6454 + */
6455 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
6456 +{
6457 +       int npositive, err;
6458 +       aufs_bindex_t bindex, btail, bdiropq;
6459 +       unsigned char isdir;
6460 +       struct qstr whname;
6461 +       struct au_do_lookup_args args = {
6462 +               .flags          = 0,
6463 +               .type           = type
6464 +       };
6465 +       const struct qstr *name = &dentry->d_name;
6466 +       struct dentry *parent;
6467 +       struct inode *inode;
6468 +
6469 +       err = au_test_shwh(dentry->d_sb, name);
6470 +       if (unlikely(err))
6471 +               goto out;
6472 +
6473 +       err = au_wh_name_alloc(&whname, name);
6474 +       if (unlikely(err))
6475 +               goto out;
6476 +
6477 +       inode = dentry->d_inode;
6478 +       isdir = !!(inode && S_ISDIR(inode->i_mode));
6479 +       if (!type)
6480 +               au_fset_lkup(args.flags, ALLOW_NEG);
6481 +
6482 +       npositive = 0;
6483 +       parent = dget_parent(dentry);
6484 +       btail = au_dbtaildir(parent);
6485 +       for (bindex = bstart; bindex <= btail; bindex++) {
6486 +               struct dentry *h_parent, *h_dentry;
6487 +               struct inode *h_inode, *h_dir;
6488 +
6489 +               h_dentry = au_h_dptr(dentry, bindex);
6490 +               if (h_dentry) {
6491 +                       if (h_dentry->d_inode)
6492 +                               npositive++;
6493 +                       if (type != S_IFDIR)
6494 +                               break;
6495 +                       continue;
6496 +               }
6497 +               h_parent = au_h_dptr(parent, bindex);
6498 +               if (!h_parent)
6499 +                       continue;
6500 +               h_dir = h_parent->d_inode;
6501 +               if (!h_dir || !S_ISDIR(h_dir->i_mode))
6502 +                       continue;
6503 +
6504 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
6505 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
6506 +                                       &args);
6507 +               mutex_unlock(&h_dir->i_mutex);
6508 +               err = PTR_ERR(h_dentry);
6509 +               if (IS_ERR(h_dentry))
6510 +                       goto out_parent;
6511 +               au_fclr_lkup(args.flags, ALLOW_NEG);
6512 +
6513 +               if (au_dbwh(dentry) >= 0)
6514 +                       break;
6515 +               if (!h_dentry)
6516 +                       continue;
6517 +               h_inode = h_dentry->d_inode;
6518 +               if (!h_inode)
6519 +                       continue;
6520 +               npositive++;
6521 +               if (!args.type)
6522 +                       args.type = h_inode->i_mode & S_IFMT;
6523 +               if (args.type != S_IFDIR)
6524 +                       break;
6525 +               else if (isdir) {
6526 +                       /* the type of lower may be different */
6527 +                       bdiropq = au_dbdiropq(dentry);
6528 +                       if (bdiropq >= 0 && bdiropq <= bindex)
6529 +                               break;
6530 +               }
6531 +       }
6532 +
6533 +       if (npositive) {
6534 +               AuLabel(positive);
6535 +               au_update_dbstart(dentry);
6536 +       }
6537 +       err = npositive;
6538 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
6539 +                    && au_dbstart(dentry) < 0)) {
6540 +               err = -EIO;
6541 +               AuIOErr("both of real entry and whiteout found, %.*s, err %d\n",
6542 +                       AuDLNPair(dentry), err);
6543 +       }
6544 +
6545 +out_parent:
6546 +       dput(parent);
6547 +       kfree(whname.name);
6548 +out:
6549 +       return err;
6550 +}
6551 +
6552 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
6553 +                              struct au_branch *br)
6554 +{
6555 +       struct dentry *dentry;
6556 +       int wkq_err;
6557 +
6558 +       if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
6559 +               dentry = vfsub_lkup_one(name, parent);
6560 +       else {
6561 +               struct vfsub_lkup_one_args args = {
6562 +                       .errp   = &dentry,
6563 +                       .name   = name,
6564 +                       .parent = parent
6565 +               };
6566 +
6567 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
6568 +               if (unlikely(wkq_err))
6569 +                       dentry = ERR_PTR(wkq_err);
6570 +       }
6571 +
6572 +       return dentry;
6573 +}
6574 +
6575 +/*
6576 + * lookup @dentry on @bindex which should be negative.
6577 + */
6578 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
6579 +{
6580 +       int err;
6581 +       struct dentry *parent, *h_parent, *h_dentry;
6582 +       struct au_branch *br;
6583 +
6584 +       parent = dget_parent(dentry);
6585 +       h_parent = au_h_dptr(parent, bindex);
6586 +       br = au_sbr(dentry->d_sb, bindex);
6587 +       if (wh)
6588 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6589 +       else
6590 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent, br);
6591 +       err = PTR_ERR(h_dentry);
6592 +       if (IS_ERR(h_dentry))
6593 +               goto out;
6594 +       if (unlikely(h_dentry->d_inode)) {
6595 +               err = -EIO;
6596 +               AuIOErr("%.*s should be negative on b%d.\n",
6597 +                       AuDLNPair(h_dentry), bindex);
6598 +               dput(h_dentry);
6599 +               goto out;
6600 +       }
6601 +
6602 +       err = 0;
6603 +       if (bindex < au_dbstart(dentry))
6604 +               au_set_dbstart(dentry, bindex);
6605 +       if (au_dbend(dentry) < bindex)
6606 +               au_set_dbend(dentry, bindex);
6607 +       au_set_h_dptr(dentry, bindex, h_dentry);
6608 +
6609 +out:
6610 +       dput(parent);
6611 +       return err;
6612 +}
6613 +
6614 +/* ---------------------------------------------------------------------- */
6615 +
6616 +/* subset of struct inode */
6617 +struct au_iattr {
6618 +       unsigned long           i_ino;
6619 +       /* unsigned int         i_nlink; */
6620 +       kuid_t                  i_uid;
6621 +       kgid_t                  i_gid;
6622 +       u64                     i_version;
6623 +/*
6624 +       loff_t                  i_size;
6625 +       blkcnt_t                i_blocks;
6626 +*/
6627 +       umode_t                 i_mode;
6628 +};
6629 +
6630 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
6631 +{
6632 +       ia->i_ino = h_inode->i_ino;
6633 +       /* ia->i_nlink = h_inode->i_nlink; */
6634 +       ia->i_uid = h_inode->i_uid;
6635 +       ia->i_gid = h_inode->i_gid;
6636 +       ia->i_version = h_inode->i_version;
6637 +/*
6638 +       ia->i_size = h_inode->i_size;
6639 +       ia->i_blocks = h_inode->i_blocks;
6640 +*/
6641 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
6642 +}
6643 +
6644 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
6645 +{
6646 +       return ia->i_ino != h_inode->i_ino
6647 +               /* || ia->i_nlink != h_inode->i_nlink */
6648 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
6649 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
6650 +               || ia->i_version != h_inode->i_version
6651 +/*
6652 +               || ia->i_size != h_inode->i_size
6653 +               || ia->i_blocks != h_inode->i_blocks
6654 +*/
6655 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
6656 +}
6657 +
6658 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
6659 +                             struct au_branch *br)
6660 +{
6661 +       int err;
6662 +       struct au_iattr ia;
6663 +       struct inode *h_inode;
6664 +       struct dentry *h_d;
6665 +       struct super_block *h_sb;
6666 +
6667 +       err = 0;
6668 +       memset(&ia, -1, sizeof(ia));
6669 +       h_sb = h_dentry->d_sb;
6670 +       h_inode = h_dentry->d_inode;
6671 +       if (h_inode)
6672 +               au_iattr_save(&ia, h_inode);
6673 +       else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
6674 +               /* nfs d_revalidate may return 0 for negative dentry */
6675 +               /* fuse d_revalidate always return 0 for negative dentry */
6676 +               goto out;
6677 +
6678 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
6679 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
6680 +       err = PTR_ERR(h_d);
6681 +       if (IS_ERR(h_d))
6682 +               goto out;
6683 +
6684 +       err = 0;
6685 +       if (unlikely(h_d != h_dentry
6686 +                    || h_d->d_inode != h_inode
6687 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
6688 +               err = au_busy_or_stale();
6689 +       dput(h_d);
6690 +
6691 +out:
6692 +       AuTraceErr(err);
6693 +       return err;
6694 +}
6695 +
6696 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
6697 +               struct dentry *h_parent, struct au_branch *br)
6698 +{
6699 +       int err;
6700 +
6701 +       err = 0;
6702 +       if (udba == AuOpt_UDBA_REVAL
6703 +           && !au_test_fs_remote(h_dentry->d_sb)) {
6704 +               IMustLock(h_dir);
6705 +               err = (h_dentry->d_parent->d_inode != h_dir);
6706 +       } else if (udba != AuOpt_UDBA_NONE)
6707 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
6708 +
6709 +       return err;
6710 +}
6711 +
6712 +/* ---------------------------------------------------------------------- */
6713 +
6714 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
6715 +{
6716 +       int err;
6717 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
6718 +       struct au_hdentry tmp, *p, *q;
6719 +       struct au_dinfo *dinfo;
6720 +       struct super_block *sb;
6721 +
6722 +       DiMustWriteLock(dentry);
6723 +
6724 +       sb = dentry->d_sb;
6725 +       dinfo = au_di(dentry);
6726 +       bend = dinfo->di_bend;
6727 +       bwh = dinfo->di_bwh;
6728 +       bdiropq = dinfo->di_bdiropq;
6729 +       p = dinfo->di_hdentry + dinfo->di_bstart;
6730 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
6731 +               if (!p->hd_dentry)
6732 +                       continue;
6733 +
6734 +               new_bindex = au_br_index(sb, p->hd_id);
6735 +               if (new_bindex == bindex)
6736 +                       continue;
6737 +
6738 +               if (dinfo->di_bwh == bindex)
6739 +                       bwh = new_bindex;
6740 +               if (dinfo->di_bdiropq == bindex)
6741 +                       bdiropq = new_bindex;
6742 +               if (new_bindex < 0) {
6743 +                       au_hdput(p);
6744 +                       p->hd_dentry = NULL;
6745 +                       continue;
6746 +               }
6747 +
6748 +               /* swap two lower dentries, and loop again */
6749 +               q = dinfo->di_hdentry + new_bindex;
6750 +               tmp = *q;
6751 +               *q = *p;
6752 +               *p = tmp;
6753 +               if (tmp.hd_dentry) {
6754 +                       bindex--;
6755 +                       p--;
6756 +               }
6757 +       }
6758 +
6759 +       dinfo->di_bwh = -1;
6760 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
6761 +               dinfo->di_bwh = bwh;
6762 +
6763 +       dinfo->di_bdiropq = -1;
6764 +       if (bdiropq >= 0
6765 +           && bdiropq <= au_sbend(sb)
6766 +           && au_sbr_whable(sb, bdiropq))
6767 +               dinfo->di_bdiropq = bdiropq;
6768 +
6769 +       err = -EIO;
6770 +       dinfo->di_bstart = -1;
6771 +       dinfo->di_bend = -1;
6772 +       bend = au_dbend(parent);
6773 +       p = dinfo->di_hdentry;
6774 +       for (bindex = 0; bindex <= bend; bindex++, p++)
6775 +               if (p->hd_dentry) {
6776 +                       dinfo->di_bstart = bindex;
6777 +                       break;
6778 +               }
6779 +
6780 +       if (dinfo->di_bstart >= 0) {
6781 +               p = dinfo->di_hdentry + bend;
6782 +               for (bindex = bend; bindex >= 0; bindex--, p--)
6783 +                       if (p->hd_dentry) {
6784 +                               dinfo->di_bend = bindex;
6785 +                               err = 0;
6786 +                               break;
6787 +                       }
6788 +       }
6789 +
6790 +       return err;
6791 +}
6792 +
6793 +static void au_do_hide(struct dentry *dentry)
6794 +{
6795 +       struct inode *inode;
6796 +
6797 +       inode = dentry->d_inode;
6798 +       if (inode) {
6799 +               if (!S_ISDIR(inode->i_mode)) {
6800 +                       if (inode->i_nlink && !d_unhashed(dentry))
6801 +                               drop_nlink(inode);
6802 +               } else {
6803 +                       clear_nlink(inode);
6804 +                       /* stop next lookup */
6805 +                       inode->i_flags |= S_DEAD;
6806 +               }
6807 +               smp_mb(); /* necessary? */
6808 +       }
6809 +       d_drop(dentry);
6810 +}
6811 +
6812 +static int au_hide_children(struct dentry *parent)
6813 +{
6814 +       int err, i, j, ndentry;
6815 +       struct au_dcsub_pages dpages;
6816 +       struct au_dpage *dpage;
6817 +       struct dentry *dentry;
6818 +
6819 +       err = au_dpages_init(&dpages, GFP_NOFS);
6820 +       if (unlikely(err))
6821 +               goto out;
6822 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
6823 +       if (unlikely(err))
6824 +               goto out_dpages;
6825 +
6826 +       /* in reverse order */
6827 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
6828 +               dpage = dpages.dpages + i;
6829 +               ndentry = dpage->ndentry;
6830 +               for (j = ndentry - 1; j >= 0; j--) {
6831 +                       dentry = dpage->dentries[j];
6832 +                       if (dentry != parent)
6833 +                               au_do_hide(dentry);
6834 +               }
6835 +       }
6836 +
6837 +out_dpages:
6838 +       au_dpages_free(&dpages);
6839 +out:
6840 +       return err;
6841 +}
6842 +
6843 +static void au_hide(struct dentry *dentry)
6844 +{
6845 +       int err;
6846 +       struct inode *inode;
6847 +
6848 +       AuDbgDentry(dentry);
6849 +       inode = dentry->d_inode;
6850 +       if (inode && S_ISDIR(inode->i_mode)) {
6851 +               /* shrink_dcache_parent(dentry); */
6852 +               err = au_hide_children(dentry);
6853 +               if (unlikely(err))
6854 +                       AuIOErr("%.*s, failed hiding children, ignored %d\n",
6855 +                               AuDLNPair(dentry), err);
6856 +       }
6857 +       au_do_hide(dentry);
6858 +}
6859 +
6860 +/*
6861 + * By adding a dirty branch, a cached dentry may be affected in various ways.
6862 + *
6863 + * a dirty branch is added
6864 + * - on the top of layers
6865 + * - in the middle of layers
6866 + * - to the bottom of layers
6867 + *
6868 + * on the added branch there exists
6869 + * - a whiteout
6870 + * - a diropq
6871 + * - a same named entry
6872 + *   + exist
6873 + *     * negative --> positive
6874 + *     * positive --> positive
6875 + *      - type is unchanged
6876 + *      - type is changed
6877 + *   + doesn't exist
6878 + *     * negative --> negative
6879 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
6880 + * - none
6881 + */
6882 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
6883 +                              struct au_dinfo *tmp)
6884 +{
6885 +       int err;
6886 +       aufs_bindex_t bindex, bend;
6887 +       struct {
6888 +               struct dentry *dentry;
6889 +               struct inode *inode;
6890 +               mode_t mode;
6891 +       } orig_h, tmp_h;
6892 +       struct au_hdentry *hd;
6893 +       struct inode *inode, *h_inode;
6894 +       struct dentry *h_dentry;
6895 +
6896 +       err = 0;
6897 +       AuDebugOn(dinfo->di_bstart < 0);
6898 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
6899 +       orig_h.inode = orig_h.dentry->d_inode;
6900 +       orig_h.mode = 0;
6901 +       if (orig_h.inode)
6902 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
6903 +       memset(&tmp_h, 0, sizeof(tmp_h));
6904 +       if (tmp->di_bstart >= 0) {
6905 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
6906 +               tmp_h.inode = tmp_h.dentry->d_inode;
6907 +               if (tmp_h.inode)
6908 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
6909 +       }
6910 +
6911 +       inode = dentry->d_inode;
6912 +       if (!orig_h.inode) {
6913 +               AuDbg("nagative originally\n");
6914 +               if (inode) {
6915 +                       au_hide(dentry);
6916 +                       goto out;
6917 +               }
6918 +               AuDebugOn(inode);
6919 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
6920 +               AuDebugOn(dinfo->di_bdiropq != -1);
6921 +
6922 +               if (!tmp_h.inode) {
6923 +                       AuDbg("negative --> negative\n");
6924 +                       /* should have only one negative lower */
6925 +                       if (tmp->di_bstart >= 0
6926 +                           && tmp->di_bstart < dinfo->di_bstart) {
6927 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
6928 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
6929 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
6930 +                               au_di_cp(dinfo, tmp);
6931 +                               hd = tmp->di_hdentry + tmp->di_bstart;
6932 +                               au_set_h_dptr(dentry, tmp->di_bstart,
6933 +                                             dget(hd->hd_dentry));
6934 +                       }
6935 +                       au_dbg_verify_dinode(dentry);
6936 +               } else {
6937 +                       AuDbg("negative --> positive\n");
6938 +                       /*
6939 +                        * similar to the behaviour of creating with bypassing
6940 +                        * aufs.
6941 +                        * unhash it in order to force an error in the
6942 +                        * succeeding create operation.
6943 +                        * we should not set S_DEAD here.
6944 +                        */
6945 +                       d_drop(dentry);
6946 +                       /* au_di_swap(tmp, dinfo); */
6947 +                       au_dbg_verify_dinode(dentry);
6948 +               }
6949 +       } else {
6950 +               AuDbg("positive originally\n");
6951 +               /* inode may be NULL */
6952 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
6953 +               if (!tmp_h.inode) {
6954 +                       AuDbg("positive --> negative\n");
6955 +                       /* or bypassing aufs */
6956 +                       au_hide(dentry);
6957 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
6958 +                               dinfo->di_bwh = tmp->di_bwh;
6959 +                       if (inode)
6960 +                               err = au_refresh_hinode_self(inode);
6961 +                       au_dbg_verify_dinode(dentry);
6962 +               } else if (orig_h.mode == tmp_h.mode) {
6963 +                       AuDbg("positive --> positive, same type\n");
6964 +                       if (!S_ISDIR(orig_h.mode)
6965 +                           && dinfo->di_bstart > tmp->di_bstart) {
6966 +                               /*
6967 +                                * similar to the behaviour of removing and
6968 +                                * creating.
6969 +                                */
6970 +                               au_hide(dentry);
6971 +                               if (inode)
6972 +                                       err = au_refresh_hinode_self(inode);
6973 +                               au_dbg_verify_dinode(dentry);
6974 +                       } else {
6975 +                               /* fill empty slots */
6976 +                               if (dinfo->di_bstart > tmp->di_bstart)
6977 +                                       dinfo->di_bstart = tmp->di_bstart;
6978 +                               if (dinfo->di_bend < tmp->di_bend)
6979 +                                       dinfo->di_bend = tmp->di_bend;
6980 +                               dinfo->di_bwh = tmp->di_bwh;
6981 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
6982 +                               hd = tmp->di_hdentry;
6983 +                               bend = dinfo->di_bend;
6984 +                               for (bindex = tmp->di_bstart; bindex <= bend;
6985 +                                    bindex++) {
6986 +                                       if (au_h_dptr(dentry, bindex))
6987 +                                               continue;
6988 +                                       h_dentry = hd[bindex].hd_dentry;
6989 +                                       if (!h_dentry)
6990 +                                               continue;
6991 +                                       h_inode = h_dentry->d_inode;
6992 +                                       AuDebugOn(!h_inode);
6993 +                                       AuDebugOn(orig_h.mode
6994 +                                                 != (h_inode->i_mode
6995 +                                                     & S_IFMT));
6996 +                                       au_set_h_dptr(dentry, bindex,
6997 +                                                     dget(h_dentry));
6998 +                               }
6999 +                               err = au_refresh_hinode(inode, dentry);
7000 +                               au_dbg_verify_dinode(dentry);
7001 +                       }
7002 +               } else {
7003 +                       AuDbg("positive --> positive, different type\n");
7004 +                       /* similar to the behaviour of removing and creating */
7005 +                       au_hide(dentry);
7006 +                       if (inode)
7007 +                               err = au_refresh_hinode_self(inode);
7008 +                       au_dbg_verify_dinode(dentry);
7009 +               }
7010 +       }
7011 +
7012 +out:
7013 +       return err;
7014 +}
7015 +
7016 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
7017 +{
7018 +       int err, ebrange;
7019 +       unsigned int sigen;
7020 +       struct au_dinfo *dinfo, *tmp;
7021 +       struct super_block *sb;
7022 +       struct inode *inode;
7023 +
7024 +       DiMustWriteLock(dentry);
7025 +       AuDebugOn(IS_ROOT(dentry));
7026 +       AuDebugOn(!parent->d_inode);
7027 +
7028 +       sb = dentry->d_sb;
7029 +       inode = dentry->d_inode;
7030 +       sigen = au_sigen(sb);
7031 +       err = au_digen_test(parent, sigen);
7032 +       if (unlikely(err))
7033 +               goto out;
7034 +
7035 +       dinfo = au_di(dentry);
7036 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
7037 +       if (unlikely(err))
7038 +               goto out;
7039 +       ebrange = au_dbrange_test(dentry);
7040 +       if (!ebrange)
7041 +               ebrange = au_do_refresh_hdentry(dentry, parent);
7042 +
7043 +       if (d_unhashed(dentry) || ebrange) {
7044 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
7045 +               if (inode)
7046 +                       err = au_refresh_hinode_self(inode);
7047 +               au_dbg_verify_dinode(dentry);
7048 +               if (!err)
7049 +                       goto out_dgen; /* success */
7050 +               goto out;
7051 +       }
7052 +
7053 +       /* temporary dinfo */
7054 +       AuDbgDentry(dentry);
7055 +       err = -ENOMEM;
7056 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
7057 +       if (unlikely(!tmp))
7058 +               goto out;
7059 +       au_di_swap(tmp, dinfo);
7060 +       /* returns the number of positive dentries */
7061 +       /*
7062 +        * if current working dir is removed, it returns an error.
7063 +        * but the dentry is legal.
7064 +        */
7065 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
7066 +       AuDbgDentry(dentry);
7067 +       au_di_swap(tmp, dinfo);
7068 +       if (err == -ENOENT)
7069 +               err = 0;
7070 +       if (err >= 0) {
7071 +               /* compare/refresh by dinfo */
7072 +               AuDbgDentry(dentry);
7073 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
7074 +               au_dbg_verify_dinode(dentry);
7075 +               AuTraceErr(err);
7076 +       }
7077 +       au_rw_write_unlock(&tmp->di_rwsem);
7078 +       au_di_free(tmp);
7079 +       if (unlikely(err))
7080 +               goto out;
7081 +
7082 +out_dgen:
7083 +       au_update_digen(dentry);
7084 +out:
7085 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
7086 +               AuIOErr("failed refreshing %.*s, %d\n",
7087 +                       AuDLNPair(dentry), err);
7088 +               AuDbgDentry(dentry);
7089 +       }
7090 +       AuTraceErr(err);
7091 +       return err;
7092 +}
7093 +
7094 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
7095 +                          struct dentry *dentry, aufs_bindex_t bindex)
7096 +{
7097 +       int err, valid;
7098 +
7099 +       err = 0;
7100 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
7101 +               goto out;
7102 +
7103 +       AuDbg("b%d\n", bindex);
7104 +       /*
7105 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
7106 +        * due to whiteout and branch permission.
7107 +        */
7108 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
7109 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
7110 +       /* it may return tri-state */
7111 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
7112 +
7113 +       if (unlikely(valid < 0))
7114 +               err = valid;
7115 +       else if (!valid)
7116 +               err = -EINVAL;
7117 +
7118 +out:
7119 +       AuTraceErr(err);
7120 +       return err;
7121 +}
7122 +
7123 +/* todo: remove this */
7124 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
7125 +                         unsigned int flags, int do_udba)
7126 +{
7127 +       int err;
7128 +       umode_t mode, h_mode;
7129 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
7130 +       unsigned char plus, unhashed, is_root, h_plus;
7131 +       struct inode *h_inode, *h_cached_inode;
7132 +       struct dentry *h_dentry;
7133 +       struct qstr *name, *h_name;
7134 +
7135 +       err = 0;
7136 +       plus = 0;
7137 +       mode = 0;
7138 +       ibs = -1;
7139 +       ibe = -1;
7140 +       unhashed = !!d_unhashed(dentry);
7141 +       is_root = !!IS_ROOT(dentry);
7142 +       name = &dentry->d_name;
7143 +
7144 +       /*
7145 +        * Theoretically, REVAL test should be unnecessary in case of
7146 +        * {FS,I}NOTIFY.
7147 +        * But {fs,i}notify doesn't fire some necessary events,
7148 +        *      IN_ATTRIB for atime/nlink/pageio
7149 +        *      IN_DELETE for NFS dentry
7150 +        * Let's do REVAL test too.
7151 +        */
7152 +       if (do_udba && inode) {
7153 +               mode = (inode->i_mode & S_IFMT);
7154 +               plus = (inode->i_nlink > 0);
7155 +               ibs = au_ibstart(inode);
7156 +               ibe = au_ibend(inode);
7157 +       }
7158 +
7159 +       bstart = au_dbstart(dentry);
7160 +       btail = bstart;
7161 +       if (inode && S_ISDIR(inode->i_mode))
7162 +               btail = au_dbtaildir(dentry);
7163 +       for (bindex = bstart; bindex <= btail; bindex++) {
7164 +               h_dentry = au_h_dptr(dentry, bindex);
7165 +               if (!h_dentry)
7166 +                       continue;
7167 +
7168 +               AuDbg("b%d, %.*s\n", bindex, AuDLNPair(h_dentry));
7169 +               spin_lock(&h_dentry->d_lock);
7170 +               h_name = &h_dentry->d_name;
7171 +               if (unlikely(do_udba
7172 +                            && !is_root
7173 +                            && (unhashed != !!d_unhashed(h_dentry)
7174 +                                || name->len != h_name->len
7175 +                                || memcmp(name->name, h_name->name, name->len))
7176 +                           )) {
7177 +                       AuDbg("unhash 0x%x 0x%x, %.*s %.*s\n",
7178 +                                 unhashed, d_unhashed(h_dentry),
7179 +                                 AuDLNPair(dentry), AuDLNPair(h_dentry));
7180 +                       spin_unlock(&h_dentry->d_lock);
7181 +                       goto err;
7182 +               }
7183 +               spin_unlock(&h_dentry->d_lock);
7184 +
7185 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
7186 +               if (unlikely(err))
7187 +                       /* do not goto err, to keep the errno */
7188 +                       break;
7189 +
7190 +               /* todo: plink too? */
7191 +               if (!do_udba)
7192 +                       continue;
7193 +
7194 +               /* UDBA tests */
7195 +               h_inode = h_dentry->d_inode;
7196 +               if (unlikely(!!inode != !!h_inode))
7197 +                       goto err;
7198 +
7199 +               h_plus = plus;
7200 +               h_mode = mode;
7201 +               h_cached_inode = h_inode;
7202 +               if (h_inode) {
7203 +                       h_mode = (h_inode->i_mode & S_IFMT);
7204 +                       h_plus = (h_inode->i_nlink > 0);
7205 +               }
7206 +               if (inode && ibs <= bindex && bindex <= ibe)
7207 +                       h_cached_inode = au_h_iptr(inode, bindex);
7208 +
7209 +               if (unlikely(plus != h_plus
7210 +                            || mode != h_mode
7211 +                            || h_cached_inode != h_inode))
7212 +                       goto err;
7213 +               continue;
7214 +
7215 +       err:
7216 +               err = -EINVAL;
7217 +               break;
7218 +       }
7219 +
7220 +       return err;
7221 +}
7222 +
7223 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
7224 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
7225 +{
7226 +       int err;
7227 +       struct dentry *parent;
7228 +
7229 +       if (!au_digen_test(dentry, sigen))
7230 +               return 0;
7231 +
7232 +       parent = dget_parent(dentry);
7233 +       di_read_lock_parent(parent, AuLock_IR);
7234 +       AuDebugOn(au_digen_test(parent, sigen));
7235 +       au_dbg_verify_gen(parent, sigen);
7236 +       err = au_refresh_dentry(dentry, parent);
7237 +       di_read_unlock(parent, AuLock_IR);
7238 +       dput(parent);
7239 +       AuTraceErr(err);
7240 +       return err;
7241 +}
7242 +
7243 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
7244 +{
7245 +       int err;
7246 +       struct dentry *d, *parent;
7247 +       struct inode *inode;
7248 +
7249 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
7250 +               return simple_reval_dpath(dentry, sigen);
7251 +
7252 +       /* slow loop, keep it simple and stupid */
7253 +       /* cf: au_cpup_dirs() */
7254 +       err = 0;
7255 +       parent = NULL;
7256 +       while (au_digen_test(dentry, sigen)) {
7257 +               d = dentry;
7258 +               while (1) {
7259 +                       dput(parent);
7260 +                       parent = dget_parent(d);
7261 +                       if (!au_digen_test(parent, sigen))
7262 +                               break;
7263 +                       d = parent;
7264 +               }
7265 +
7266 +               inode = d->d_inode;
7267 +               if (d != dentry)
7268 +                       di_write_lock_child2(d);
7269 +
7270 +               /* someone might update our dentry while we were sleeping */
7271 +               if (au_digen_test(d, sigen)) {
7272 +                       /*
7273 +                        * todo: consolidate with simple_reval_dpath(),
7274 +                        * do_refresh() and au_reval_for_attr().
7275 +                        */
7276 +                       di_read_lock_parent(parent, AuLock_IR);
7277 +                       err = au_refresh_dentry(d, parent);
7278 +                       di_read_unlock(parent, AuLock_IR);
7279 +               }
7280 +
7281 +               if (d != dentry)
7282 +                       di_write_unlock(d);
7283 +               dput(parent);
7284 +               if (unlikely(err))
7285 +                       break;
7286 +       }
7287 +
7288 +       return err;
7289 +}
7290 +
7291 +/*
7292 + * if valid returns 1, otherwise 0.
7293 + */
7294 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
7295 +{
7296 +       int valid, err;
7297 +       unsigned int sigen;
7298 +       unsigned char do_udba;
7299 +       struct super_block *sb;
7300 +       struct inode *inode;
7301 +
7302 +       /* todo: support rcu-walk? */
7303 +       if (flags & LOOKUP_RCU)
7304 +               return -ECHILD;
7305 +
7306 +       valid = 0;
7307 +       if (unlikely(!au_di(dentry)))
7308 +               goto out;
7309 +
7310 +       inode = dentry->d_inode;
7311 +       if (inode && is_bad_inode(inode))
7312 +               goto out;
7313 +
7314 +       valid = 1;
7315 +       sb = dentry->d_sb;
7316 +       /*
7317 +        * todo: very ugly
7318 +        * i_mutex of parent dir may be held,
7319 +        * but we should not return 'invalid' due to busy.
7320 +        */
7321 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
7322 +       if (unlikely(err)) {
7323 +               valid = err;
7324 +               AuTraceErr(err);
7325 +               goto out;
7326 +       }
7327 +       if (unlikely(au_dbrange_test(dentry))) {
7328 +               err = -EINVAL;
7329 +               AuTraceErr(err);
7330 +               goto out_dgrade;
7331 +       }
7332 +
7333 +       sigen = au_sigen(sb);
7334 +       if (au_digen_test(dentry, sigen)) {
7335 +               AuDebugOn(IS_ROOT(dentry));
7336 +               err = au_reval_dpath(dentry, sigen);
7337 +               if (unlikely(err)) {
7338 +                       AuTraceErr(err);
7339 +                       goto out_dgrade;
7340 +               }
7341 +       }
7342 +       di_downgrade_lock(dentry, AuLock_IR);
7343 +
7344 +       err = -EINVAL;
7345 +       if (inode && (IS_DEADDIR(inode) || !inode->i_nlink))
7346 +               goto out_inval;
7347 +
7348 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
7349 +       if (do_udba && inode) {
7350 +               aufs_bindex_t bstart = au_ibstart(inode);
7351 +               struct inode *h_inode;
7352 +
7353 +               if (bstart >= 0) {
7354 +                       h_inode = au_h_iptr(inode, bstart);
7355 +                       if (h_inode && au_test_higen(inode, h_inode))
7356 +                               goto out_inval;
7357 +               }
7358 +       }
7359 +
7360 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
7361 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
7362 +               err = -EIO;
7363 +               AuDbg("both of real entry and whiteout found, %.*s, err %d\n",
7364 +                     AuDLNPair(dentry), err);
7365 +       }
7366 +       goto out_inval;
7367 +
7368 +out_dgrade:
7369 +       di_downgrade_lock(dentry, AuLock_IR);
7370 +out_inval:
7371 +       aufs_read_unlock(dentry, AuLock_IR);
7372 +       AuTraceErr(err);
7373 +       valid = !err;
7374 +out:
7375 +       if (!valid) {
7376 +               AuDbg("%.*s invalid, %d\n", AuDLNPair(dentry), valid);
7377 +               d_drop(dentry);
7378 +       }
7379 +       return valid;
7380 +}
7381 +
7382 +static void aufs_d_release(struct dentry *dentry)
7383 +{
7384 +       if (au_di(dentry)) {
7385 +               au_di_fin(dentry);
7386 +               au_hn_di_reinit(dentry);
7387 +       }
7388 +}
7389 +
7390 +const struct dentry_operations aufs_dop = {
7391 +       .d_revalidate           = aufs_d_revalidate,
7392 +       .d_weak_revalidate      = aufs_d_revalidate,
7393 +       .d_release              = aufs_d_release
7394 +};
7395 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
7396 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
7397 +++ linux/fs/aufs/dentry.h      2013-07-06 13:20:47.740198107 +0200
7398 @@ -0,0 +1,234 @@
7399 +/*
7400 + * Copyright (C) 2005-2013 Junjiro R. Okajima
7401 + *
7402 + * This program, aufs is free software; you can redistribute it and/or modify
7403 + * it under the terms of the GNU General Public License as published by
7404 + * the Free Software Foundation; either version 2 of the License, or
7405 + * (at your option) any later version.
7406 + *
7407 + * This program is distributed in the hope that it will be useful,
7408 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7409 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7410 + * GNU General Public License for more details.
7411 + *
7412 + * You should have received a copy of the GNU General Public License
7413 + * along with this program; if not, write to the Free Software
7414 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7415 + */
7416 +
7417 +/*
7418 + * lookup and dentry operations
7419 + */
7420 +
7421 +#ifndef __AUFS_DENTRY_H__
7422 +#define __AUFS_DENTRY_H__
7423 +
7424 +#ifdef __KERNEL__
7425 +
7426 +#include <linux/dcache.h>
7427 +#include "rwsem.h"
7428 +
7429 +struct au_hdentry {
7430 +       struct dentry           *hd_dentry;
7431 +       aufs_bindex_t           hd_id;
7432 +};
7433 +
7434 +struct au_dinfo {
7435 +       atomic_t                di_generation;
7436 +
7437 +       struct au_rwsem         di_rwsem;
7438 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
7439 +       struct au_hdentry       *di_hdentry;
7440 +} ____cacheline_aligned_in_smp;
7441 +
7442 +/* ---------------------------------------------------------------------- */
7443 +
7444 +/* dentry.c */
7445 +extern const struct dentry_operations aufs_dop;
7446 +struct au_branch;
7447 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
7448 +                              struct au_branch *br);
7449 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7450 +               struct dentry *h_parent, struct au_branch *br);
7451 +
7452 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
7453 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
7454 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
7455 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
7456 +
7457 +/* dinfo.c */
7458 +void au_di_init_once(void *_di);
7459 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
7460 +void au_di_free(struct au_dinfo *dinfo);
7461 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
7462 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
7463 +int au_di_init(struct dentry *dentry);
7464 +void au_di_fin(struct dentry *dentry);
7465 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
7466 +
7467 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
7468 +void di_read_unlock(struct dentry *d, int flags);
7469 +void di_downgrade_lock(struct dentry *d, int flags);
7470 +void di_write_lock(struct dentry *d, unsigned int lsc);
7471 +void di_write_unlock(struct dentry *d);
7472 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
7473 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
7474 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
7475 +
7476 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
7477 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
7478 +aufs_bindex_t au_dbtail(struct dentry *dentry);
7479 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
7480 +
7481 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
7482 +                  struct dentry *h_dentry);
7483 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
7484 +int au_dbrange_test(struct dentry *dentry);
7485 +void au_update_digen(struct dentry *dentry);
7486 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
7487 +void au_update_dbstart(struct dentry *dentry);
7488 +void au_update_dbend(struct dentry *dentry);
7489 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
7490 +
7491 +/* ---------------------------------------------------------------------- */
7492 +
7493 +static inline struct au_dinfo *au_di(struct dentry *dentry)
7494 +{
7495 +       return dentry->d_fsdata;
7496 +}
7497 +
7498 +/* ---------------------------------------------------------------------- */
7499 +
7500 +/* lock subclass for dinfo */
7501 +enum {
7502 +       AuLsc_DI_CHILD,         /* child first */
7503 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
7504 +       AuLsc_DI_CHILD3,        /* copyup dirs */
7505 +       AuLsc_DI_PARENT,
7506 +       AuLsc_DI_PARENT2,
7507 +       AuLsc_DI_PARENT3,
7508 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
7509 +};
7510 +
7511 +/*
7512 + * di_read_lock_child, di_write_lock_child,
7513 + * di_read_lock_child2, di_write_lock_child2,
7514 + * di_read_lock_child3, di_write_lock_child3,
7515 + * di_read_lock_parent, di_write_lock_parent,
7516 + * di_read_lock_parent2, di_write_lock_parent2,
7517 + * di_read_lock_parent3, di_write_lock_parent3,
7518 + */
7519 +#define AuReadLockFunc(name, lsc) \
7520 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
7521 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
7522 +
7523 +#define AuWriteLockFunc(name, lsc) \
7524 +static inline void di_write_lock_##name(struct dentry *d) \
7525 +{ di_write_lock(d, AuLsc_DI_##lsc); }
7526 +
7527 +#define AuRWLockFuncs(name, lsc) \
7528 +       AuReadLockFunc(name, lsc) \
7529 +       AuWriteLockFunc(name, lsc)
7530 +
7531 +AuRWLockFuncs(child, CHILD);
7532 +AuRWLockFuncs(child2, CHILD2);
7533 +AuRWLockFuncs(child3, CHILD3);
7534 +AuRWLockFuncs(parent, PARENT);
7535 +AuRWLockFuncs(parent2, PARENT2);
7536 +AuRWLockFuncs(parent3, PARENT3);
7537 +
7538 +#undef AuReadLockFunc
7539 +#undef AuWriteLockFunc
7540 +#undef AuRWLockFuncs
7541 +
7542 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
7543 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
7544 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
7545 +
7546 +/* ---------------------------------------------------------------------- */
7547 +
7548 +/* todo: memory barrier? */
7549 +static inline unsigned int au_digen(struct dentry *d)
7550 +{
7551 +       return atomic_read(&au_di(d)->di_generation);
7552 +}
7553 +
7554 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
7555 +{
7556 +       hdentry->hd_dentry = NULL;
7557 +}
7558 +
7559 +static inline void au_hdput(struct au_hdentry *hd)
7560 +{
7561 +       if (hd)
7562 +               dput(hd->hd_dentry);
7563 +}
7564 +
7565 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
7566 +{
7567 +       DiMustAnyLock(dentry);
7568 +       return au_di(dentry)->di_bstart;
7569 +}
7570 +
7571 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
7572 +{
7573 +       DiMustAnyLock(dentry);
7574 +       return au_di(dentry)->di_bend;
7575 +}
7576 +
7577 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
7578 +{
7579 +       DiMustAnyLock(dentry);
7580 +       return au_di(dentry)->di_bwh;
7581 +}
7582 +
7583 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
7584 +{
7585 +       DiMustAnyLock(dentry);
7586 +       return au_di(dentry)->di_bdiropq;
7587 +}
7588 +
7589 +/* todo: hard/soft set? */
7590 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
7591 +{
7592 +       DiMustWriteLock(dentry);
7593 +       au_di(dentry)->di_bstart = bindex;
7594 +}
7595 +
7596 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
7597 +{
7598 +       DiMustWriteLock(dentry);
7599 +       au_di(dentry)->di_bend = bindex;
7600 +}
7601 +
7602 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
7603 +{
7604 +       DiMustWriteLock(dentry);
7605 +       /* dbwh can be outside of bstart - bend range */
7606 +       au_di(dentry)->di_bwh = bindex;
7607 +}
7608 +
7609 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
7610 +{
7611 +       DiMustWriteLock(dentry);
7612 +       au_di(dentry)->di_bdiropq = bindex;
7613 +}
7614 +
7615 +/* ---------------------------------------------------------------------- */
7616 +
7617 +#ifdef CONFIG_AUFS_HNOTIFY
7618 +static inline void au_digen_dec(struct dentry *d)
7619 +{
7620 +       atomic_dec(&au_di(d)->di_generation);
7621 +}
7622 +
7623 +static inline void au_hn_di_reinit(struct dentry *dentry)
7624 +{
7625 +       dentry->d_fsdata = NULL;
7626 +}
7627 +#else
7628 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
7629 +#endif /* CONFIG_AUFS_HNOTIFY */
7630 +
7631 +#endif /* __KERNEL__ */
7632 +#endif /* __AUFS_DENTRY_H__ */
7633 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
7634 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
7635 +++ linux/fs/aufs/dinfo.c       2013-07-30 22:42:55.839613269 +0200
7636 @@ -0,0 +1,543 @@
7637 +/*
7638 + * Copyright (C) 2005-2013 Junjiro R. Okajima
7639 + *
7640 + * This program, aufs is free software; you can redistribute it and/or modify
7641 + * it under the terms of the GNU General Public License as published by
7642 + * the Free Software Foundation; either version 2 of the License, or
7643 + * (at your option) any later version.
7644 + *
7645 + * This program is distributed in the hope that it will be useful,
7646 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7647 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7648 + * GNU General Public License for more details.
7649 + *
7650 + * You should have received a copy of the GNU General Public License
7651 + * along with this program; if not, write to the Free Software
7652 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7653 + */
7654 +
7655 +/*
7656 + * dentry private data
7657 + */
7658 +
7659 +#include "aufs.h"
7660 +
7661 +void au_di_init_once(void *_dinfo)
7662 +{
7663 +       struct au_dinfo *dinfo = _dinfo;
7664 +       static struct lock_class_key aufs_di;
7665 +
7666 +       au_rw_init(&dinfo->di_rwsem);
7667 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
7668 +}
7669 +
7670 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
7671 +{
7672 +       struct au_dinfo *dinfo;
7673 +       int nbr, i;
7674 +
7675 +       dinfo = au_cache_alloc_dinfo();
7676 +       if (unlikely(!dinfo))
7677 +               goto out;
7678 +
7679 +       nbr = au_sbend(sb) + 1;
7680 +       if (nbr <= 0)
7681 +               nbr = 1;
7682 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
7683 +       if (dinfo->di_hdentry) {
7684 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
7685 +               dinfo->di_bstart = -1;
7686 +               dinfo->di_bend = -1;
7687 +               dinfo->di_bwh = -1;
7688 +               dinfo->di_bdiropq = -1;
7689 +               for (i = 0; i < nbr; i++)
7690 +                       dinfo->di_hdentry[i].hd_id = -1;
7691 +               goto out;
7692 +       }
7693 +
7694 +       au_cache_free_dinfo(dinfo);
7695 +       dinfo = NULL;
7696 +
7697 +out:
7698 +       return dinfo;
7699 +}
7700 +
7701 +void au_di_free(struct au_dinfo *dinfo)
7702 +{
7703 +       struct au_hdentry *p;
7704 +       aufs_bindex_t bend, bindex;
7705 +
7706 +       /* dentry may not be revalidated */
7707 +       bindex = dinfo->di_bstart;
7708 +       if (bindex >= 0) {
7709 +               bend = dinfo->di_bend;
7710 +               p = dinfo->di_hdentry + bindex;
7711 +               while (bindex++ <= bend)
7712 +                       au_hdput(p++);
7713 +       }
7714 +       kfree(dinfo->di_hdentry);
7715 +       au_cache_free_dinfo(dinfo);
7716 +}
7717 +
7718 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
7719 +{
7720 +       struct au_hdentry *p;
7721 +       aufs_bindex_t bi;
7722 +
7723 +       AuRwMustWriteLock(&a->di_rwsem);
7724 +       AuRwMustWriteLock(&b->di_rwsem);
7725 +
7726 +#define DiSwap(v, name)                                \
7727 +       do {                                    \
7728 +               v = a->di_##name;               \
7729 +               a->di_##name = b->di_##name;    \
7730 +               b->di_##name = v;               \
7731 +       } while (0)
7732 +
7733 +       DiSwap(p, hdentry);
7734 +       DiSwap(bi, bstart);
7735 +       DiSwap(bi, bend);
7736 +       DiSwap(bi, bwh);
7737 +       DiSwap(bi, bdiropq);
7738 +       /* smp_mb(); */
7739 +
7740 +#undef DiSwap
7741 +}
7742 +
7743 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
7744 +{
7745 +       AuRwMustWriteLock(&dst->di_rwsem);
7746 +       AuRwMustWriteLock(&src->di_rwsem);
7747 +
7748 +       dst->di_bstart = src->di_bstart;
7749 +       dst->di_bend = src->di_bend;
7750 +       dst->di_bwh = src->di_bwh;
7751 +       dst->di_bdiropq = src->di_bdiropq;
7752 +       /* smp_mb(); */
7753 +}
7754 +
7755 +int au_di_init(struct dentry *dentry)
7756 +{
7757 +       int err;
7758 +       struct super_block *sb;
7759 +       struct au_dinfo *dinfo;
7760 +
7761 +       err = 0;
7762 +       sb = dentry->d_sb;
7763 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
7764 +       if (dinfo) {
7765 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
7766 +               /* smp_mb(); */ /* atomic_set */
7767 +               dentry->d_fsdata = dinfo;
7768 +       } else
7769 +               err = -ENOMEM;
7770 +
7771 +       return err;
7772 +}
7773 +
7774 +void au_di_fin(struct dentry *dentry)
7775 +{
7776 +       struct au_dinfo *dinfo;
7777 +
7778 +       dinfo = au_di(dentry);
7779 +       AuRwDestroy(&dinfo->di_rwsem);
7780 +       au_di_free(dinfo);
7781 +}
7782 +
7783 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
7784 +{
7785 +       int err, sz;
7786 +       struct au_hdentry *hdp;
7787 +
7788 +       AuRwMustWriteLock(&dinfo->di_rwsem);
7789 +
7790 +       err = -ENOMEM;
7791 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
7792 +       if (!sz)
7793 +               sz = sizeof(*hdp);
7794 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
7795 +       if (hdp) {
7796 +               dinfo->di_hdentry = hdp;
7797 +               err = 0;
7798 +       }
7799 +
7800 +       return err;
7801 +}
7802 +
7803 +/* ---------------------------------------------------------------------- */
7804 +
7805 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
7806 +{
7807 +       switch (lsc) {
7808 +       case AuLsc_DI_CHILD:
7809 +               ii_write_lock_child(inode);
7810 +               break;
7811 +       case AuLsc_DI_CHILD2:
7812 +               ii_write_lock_child2(inode);
7813 +               break;
7814 +       case AuLsc_DI_CHILD3:
7815 +               ii_write_lock_child3(inode);
7816 +               break;
7817 +       case AuLsc_DI_PARENT:
7818 +               ii_write_lock_parent(inode);
7819 +               break;
7820 +       case AuLsc_DI_PARENT2:
7821 +               ii_write_lock_parent2(inode);
7822 +               break;
7823 +       case AuLsc_DI_PARENT3:
7824 +               ii_write_lock_parent3(inode);
7825 +               break;
7826 +       default:
7827 +               BUG();
7828 +       }
7829 +}
7830 +
7831 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
7832 +{
7833 +       switch (lsc) {
7834 +       case AuLsc_DI_CHILD:
7835 +               ii_read_lock_child(inode);
7836 +               break;
7837 +       case AuLsc_DI_CHILD2:
7838 +               ii_read_lock_child2(inode);
7839 +               break;
7840 +       case AuLsc_DI_CHILD3:
7841 +               ii_read_lock_child3(inode);
7842 +               break;
7843 +       case AuLsc_DI_PARENT:
7844 +               ii_read_lock_parent(inode);
7845 +               break;
7846 +       case AuLsc_DI_PARENT2:
7847 +               ii_read_lock_parent2(inode);
7848 +               break;
7849 +       case AuLsc_DI_PARENT3:
7850 +               ii_read_lock_parent3(inode);
7851 +               break;
7852 +       default:
7853 +               BUG();
7854 +       }
7855 +}
7856 +
7857 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
7858 +{
7859 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
7860 +       if (d->d_inode) {
7861 +               if (au_ftest_lock(flags, IW))
7862 +                       do_ii_write_lock(d->d_inode, lsc);
7863 +               else if (au_ftest_lock(flags, IR))
7864 +                       do_ii_read_lock(d->d_inode, lsc);
7865 +       }
7866 +}
7867 +
7868 +void di_read_unlock(struct dentry *d, int flags)
7869 +{
7870 +       if (d->d_inode) {
7871 +               if (au_ftest_lock(flags, IW)) {
7872 +                       au_dbg_verify_dinode(d);
7873 +                       ii_write_unlock(d->d_inode);
7874 +               } else if (au_ftest_lock(flags, IR)) {
7875 +                       au_dbg_verify_dinode(d);
7876 +                       ii_read_unlock(d->d_inode);
7877 +               }
7878 +       }
7879 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
7880 +}
7881 +
7882 +void di_downgrade_lock(struct dentry *d, int flags)
7883 +{
7884 +       if (d->d_inode && au_ftest_lock(flags, IR))
7885 +               ii_downgrade_lock(d->d_inode);
7886 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
7887 +}
7888 +
7889 +void di_write_lock(struct dentry *d, unsigned int lsc)
7890 +{
7891 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
7892 +       if (d->d_inode)
7893 +               do_ii_write_lock(d->d_inode, lsc);
7894 +}
7895 +
7896 +void di_write_unlock(struct dentry *d)
7897 +{
7898 +       au_dbg_verify_dinode(d);
7899 +       if (d->d_inode)
7900 +               ii_write_unlock(d->d_inode);
7901 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
7902 +}
7903 +
7904 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
7905 +{
7906 +       AuDebugOn(d1 == d2
7907 +                 || d1->d_inode == d2->d_inode
7908 +                 || d1->d_sb != d2->d_sb);
7909 +
7910 +       if (isdir && au_test_subdir(d1, d2)) {
7911 +               di_write_lock_child(d1);
7912 +               di_write_lock_child2(d2);
7913 +       } else {
7914 +               /* there should be no races */
7915 +               di_write_lock_child(d2);
7916 +               di_write_lock_child2(d1);
7917 +       }
7918 +}
7919 +
7920 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
7921 +{
7922 +       AuDebugOn(d1 == d2
7923 +                 || d1->d_inode == d2->d_inode
7924 +                 || d1->d_sb != d2->d_sb);
7925 +
7926 +       if (isdir && au_test_subdir(d1, d2)) {
7927 +               di_write_lock_parent(d1);
7928 +               di_write_lock_parent2(d2);
7929 +       } else {
7930 +               /* there should be no races */
7931 +               di_write_lock_parent(d2);
7932 +               di_write_lock_parent2(d1);
7933 +       }
7934 +}
7935 +
7936 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
7937 +{
7938 +       di_write_unlock(d1);
7939 +       if (d1->d_inode == d2->d_inode)
7940 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
7941 +       else
7942 +               di_write_unlock(d2);
7943 +}
7944 +
7945 +/* ---------------------------------------------------------------------- */
7946 +
7947 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
7948 +{
7949 +       struct dentry *d;
7950 +
7951 +       DiMustAnyLock(dentry);
7952 +
7953 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7954 +               return NULL;
7955 +       AuDebugOn(bindex < 0);
7956 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
7957 +       AuDebugOn(d && d->d_count <= 0);
7958 +       return d;
7959 +}
7960 +
7961 +/*
7962 + * extended version of au_h_dptr().
7963 + * returns a hashed and positive h_dentry in bindex, NULL, or error.
7964 + */
7965 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
7966 +{
7967 +       struct dentry *h_dentry;
7968 +       struct inode *inode, *h_inode;
7969 +
7970 +       inode = dentry->d_inode;
7971 +       AuDebugOn(!inode);
7972 +
7973 +       h_dentry = NULL;
7974 +       if (au_dbstart(dentry) <= bindex
7975 +           && bindex <= au_dbend(dentry))
7976 +               h_dentry = au_h_dptr(dentry, bindex);
7977 +       if (h_dentry && !au_d_hashed_positive(h_dentry)) {
7978 +               dget(h_dentry);
7979 +               goto out; /* success */
7980 +       }
7981 +
7982 +       AuDebugOn(bindex < au_ibstart(inode));
7983 +       AuDebugOn(au_ibend(inode) < bindex);
7984 +       h_inode = au_h_iptr(inode, bindex);
7985 +       h_dentry = d_find_alias(h_inode);
7986 +       if (h_dentry) {
7987 +               if (!IS_ERR(h_dentry)) {
7988 +                       if (!au_d_hashed_positive(h_dentry))
7989 +                               goto out; /* success */
7990 +                       dput(h_dentry);
7991 +               } else
7992 +                       goto out;
7993 +       }
7994 +
7995 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
7996 +               h_dentry = au_plink_lkup(inode, bindex);
7997 +               AuDebugOn(!h_dentry);
7998 +               if (!IS_ERR(h_dentry)) {
7999 +                       if (!au_d_hashed_positive(h_dentry))
8000 +                               goto out; /* success */
8001 +                       dput(h_dentry);
8002 +                       h_dentry = NULL;
8003 +               }
8004 +       }
8005 +
8006 +out:
8007 +       AuDbgDentry(h_dentry);
8008 +       return h_dentry;
8009 +}
8010 +
8011 +aufs_bindex_t au_dbtail(struct dentry *dentry)
8012 +{
8013 +       aufs_bindex_t bend, bwh;
8014 +
8015 +       bend = au_dbend(dentry);
8016 +       if (0 <= bend) {
8017 +               bwh = au_dbwh(dentry);
8018 +               if (!bwh)
8019 +                       return bwh;
8020 +               if (0 < bwh && bwh < bend)
8021 +                       return bwh - 1;
8022 +       }
8023 +       return bend;
8024 +}
8025 +
8026 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
8027 +{
8028 +       aufs_bindex_t bend, bopq;
8029 +
8030 +       bend = au_dbtail(dentry);
8031 +       if (0 <= bend) {
8032 +               bopq = au_dbdiropq(dentry);
8033 +               if (0 <= bopq && bopq < bend)
8034 +                       bend = bopq;
8035 +       }
8036 +       return bend;
8037 +}
8038 +
8039 +/* ---------------------------------------------------------------------- */
8040 +
8041 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8042 +                  struct dentry *h_dentry)
8043 +{
8044 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
8045 +       struct au_branch *br;
8046 +
8047 +       DiMustWriteLock(dentry);
8048 +
8049 +       au_hdput(hd);
8050 +       hd->hd_dentry = h_dentry;
8051 +       if (h_dentry) {
8052 +               br = au_sbr(dentry->d_sb, bindex);
8053 +               hd->hd_id = br->br_id;
8054 +       }
8055 +}
8056 +
8057 +int au_dbrange_test(struct dentry *dentry)
8058 +{
8059 +       int err;
8060 +       aufs_bindex_t bstart, bend;
8061 +
8062 +       err = 0;
8063 +       bstart = au_dbstart(dentry);
8064 +       bend = au_dbend(dentry);
8065 +       if (bstart >= 0)
8066 +               AuDebugOn(bend < 0 && bstart > bend);
8067 +       else {
8068 +               err = -EIO;
8069 +               AuDebugOn(bend >= 0);
8070 +       }
8071 +
8072 +       return err;
8073 +}
8074 +
8075 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
8076 +{
8077 +       int err;
8078 +
8079 +       err = 0;
8080 +       if (unlikely(au_digen(dentry) != sigen
8081 +                    || au_iigen_test(dentry->d_inode, sigen)))
8082 +               err = -EIO;
8083 +
8084 +       return err;
8085 +}
8086 +
8087 +void au_update_digen(struct dentry *dentry)
8088 +{
8089 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
8090 +       /* smp_mb(); */ /* atomic_set */
8091 +}
8092 +
8093 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
8094 +{
8095 +       struct au_dinfo *dinfo;
8096 +       struct dentry *h_d;
8097 +       struct au_hdentry *hdp;
8098 +
8099 +       DiMustWriteLock(dentry);
8100 +
8101 +       dinfo = au_di(dentry);
8102 +       if (!dinfo || dinfo->di_bstart < 0)
8103 +               return;
8104 +
8105 +       hdp = dinfo->di_hdentry;
8106 +       if (do_put_zero) {
8107 +               aufs_bindex_t bindex, bend;
8108 +
8109 +               bend = dinfo->di_bend;
8110 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
8111 +                       h_d = hdp[0 + bindex].hd_dentry;
8112 +                       if (h_d && !h_d->d_inode)
8113 +                               au_set_h_dptr(dentry, bindex, NULL);
8114 +               }
8115 +       }
8116 +
8117 +       dinfo->di_bstart = -1;
8118 +       while (++dinfo->di_bstart <= dinfo->di_bend)
8119 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
8120 +                       break;
8121 +       if (dinfo->di_bstart > dinfo->di_bend) {
8122 +               dinfo->di_bstart = -1;
8123 +               dinfo->di_bend = -1;
8124 +               return;
8125 +       }
8126 +
8127 +       dinfo->di_bend++;
8128 +       while (0 <= --dinfo->di_bend)
8129 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
8130 +                       break;
8131 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
8132 +}
8133 +
8134 +void au_update_dbstart(struct dentry *dentry)
8135 +{
8136 +       aufs_bindex_t bindex, bend;
8137 +       struct dentry *h_dentry;
8138 +
8139 +       bend = au_dbend(dentry);
8140 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
8141 +               h_dentry = au_h_dptr(dentry, bindex);
8142 +               if (!h_dentry)
8143 +                       continue;
8144 +               if (h_dentry->d_inode) {
8145 +                       au_set_dbstart(dentry, bindex);
8146 +                       return;
8147 +               }
8148 +               au_set_h_dptr(dentry, bindex, NULL);
8149 +       }
8150 +}
8151 +
8152 +void au_update_dbend(struct dentry *dentry)
8153 +{
8154 +       aufs_bindex_t bindex, bstart;
8155 +       struct dentry *h_dentry;
8156 +
8157 +       bstart = au_dbstart(dentry);
8158 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
8159 +               h_dentry = au_h_dptr(dentry, bindex);
8160 +               if (!h_dentry)
8161 +                       continue;
8162 +               if (h_dentry->d_inode) {
8163 +                       au_set_dbend(dentry, bindex);
8164 +                       return;
8165 +               }
8166 +               au_set_h_dptr(dentry, bindex, NULL);
8167 +       }
8168 +}
8169 +
8170 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
8171 +{
8172 +       aufs_bindex_t bindex, bend;
8173 +
8174 +       bend = au_dbend(dentry);
8175 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
8176 +               if (au_h_dptr(dentry, bindex) == h_dentry)
8177 +                       return bindex;
8178 +       return -1;
8179 +}
8180 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
8181 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
8182 +++ linux/fs/aufs/dir.c 2013-07-30 22:42:55.839613269 +0200
8183 @@ -0,0 +1,632 @@
8184 +/*
8185 + * Copyright (C) 2005-2013 Junjiro R. Okajima
8186 + *
8187 + * This program, aufs is free software; you can redistribute it and/or modify
8188 + * it under the terms of the GNU General Public License as published by
8189 + * the Free Software Foundation; either version 2 of the License, or
8190 + * (at your option) any later version.
8191 + *
8192 + * This program is distributed in the hope that it will be useful,
8193 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8194 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8195 + * GNU General Public License for more details.
8196 + *
8197 + * You should have received a copy of the GNU General Public License
8198 + * along with this program; if not, write to the Free Software
8199 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
8200 + */
8201 +
8202 +/*
8203 + * directory operations
8204 + */
8205 +
8206 +#include <linux/fs_stack.h>
8207 +#include "aufs.h"
8208 +
8209 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
8210 +{
8211 +       unsigned int nlink;
8212 +
8213 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8214 +
8215 +       nlink = dir->i_nlink;
8216 +       nlink += h_dir->i_nlink - 2;
8217 +       if (h_dir->i_nlink < 2)
8218 +               nlink += 2;
8219 +       smp_mb();
8220 +       /* 0 can happen in revaliding */
8221 +       set_nlink(dir, nlink);
8222 +}
8223 +
8224 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
8225 +{
8226 +       unsigned int nlink;
8227 +
8228 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8229 +
8230 +       nlink = dir->i_nlink;
8231 +       nlink -= h_dir->i_nlink - 2;
8232 +       if (h_dir->i_nlink < 2)
8233 +               nlink -= 2;
8234 +       smp_mb();
8235 +       /* nlink == 0 means the branch-fs is broken */
8236 +       set_nlink(dir, nlink);
8237 +}
8238 +
8239 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
8240 +{
8241 +       loff_t sz;
8242 +       aufs_bindex_t bindex, bend;
8243 +       struct file *h_file;
8244 +       struct dentry *h_dentry;
8245 +
8246 +       sz = 0;
8247 +       if (file) {
8248 +               AuDebugOn(!file_inode(file));
8249 +               AuDebugOn(!S_ISDIR(file_inode(file)->i_mode));
8250 +
8251 +               bend = au_fbend_dir(file);
8252 +               for (bindex = au_fbstart(file);
8253 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
8254 +                    bindex++) {
8255 +                       h_file = au_hf_dir(file, bindex);
8256 +                       if (h_file && file_inode(h_file))
8257 +                               sz += vfsub_f_size_read(h_file);
8258 +               }
8259 +       } else {
8260 +               AuDebugOn(!dentry);
8261 +               AuDebugOn(!dentry->d_inode);
8262 +               AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
8263 +
8264 +               bend = au_dbtaildir(dentry);
8265 +               for (bindex = au_dbstart(dentry);
8266 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
8267 +                    bindex++) {
8268 +                       h_dentry = au_h_dptr(dentry, bindex);
8269 +                       if (h_dentry && h_dentry->d_inode)
8270 +                               sz += i_size_read(h_dentry->d_inode);
8271 +               }
8272 +       }
8273 +       if (sz < KMALLOC_MAX_SIZE)
8274 +               sz = roundup_pow_of_two(sz);
8275 +       if (sz > KMALLOC_MAX_SIZE)
8276 +               sz = KMALLOC_MAX_SIZE;
8277 +       else if (sz < NAME_MAX) {
8278 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
8279 +               sz = AUFS_RDBLK_DEF;
8280 +       }
8281 +       return sz;
8282 +}
8283 +
8284 +/* ---------------------------------------------------------------------- */
8285 +
8286 +static int reopen_dir(struct file *file)
8287 +{
8288 +       int err;
8289 +       unsigned int flags;
8290 +       aufs_bindex_t bindex, btail, bstart;
8291 +       struct dentry *dentry, *h_dentry;
8292 +       struct file *h_file;
8293 +
8294 +       /* open all lower dirs */
8295 +       dentry = file->f_dentry;
8296 +       bstart = au_dbstart(dentry);
8297 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
8298 +               au_set_h_fptr(file, bindex, NULL);
8299 +       au_set_fbstart(file, bstart);
8300 +
8301 +       btail = au_dbtaildir(dentry);
8302 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
8303 +               au_set_h_fptr(file, bindex, NULL);
8304 +       au_set_fbend_dir(file, btail);
8305 +
8306 +       flags = vfsub_file_flags(file);
8307 +       for (bindex = bstart; bindex <= btail; bindex++) {
8308 +               h_dentry = au_h_dptr(dentry, bindex);
8309 +               if (!h_dentry)
8310 +                       continue;
8311 +               h_file = au_hf_dir(file, bindex);
8312 +               if (h_file)
8313 +                       continue;
8314 +
8315 +               h_file = au_h_open(dentry, bindex, flags, file);
8316 +               err = PTR_ERR(h_file);
8317 +               if (IS_ERR(h_file))
8318 +                       goto out; /* close all? */
8319 +               au_set_h_fptr(file, bindex, h_file);
8320 +       }
8321 +       au_update_figen(file);
8322 +       /* todo: necessary? */
8323 +       /* file->f_ra = h_file->f_ra; */
8324 +       err = 0;
8325 +
8326 +out:
8327 +       return err;
8328 +}
8329 +
8330 +static int do_open_dir(struct file *file, int flags)
8331 +{
8332 +       int err;
8333 +       aufs_bindex_t bindex, btail;
8334 +       struct dentry *dentry, *h_dentry;
8335 +       struct file *h_file;
8336 +
8337 +       FiMustWriteLock(file);
8338 +
8339 +       dentry = file->f_dentry;
8340 +       err = au_alive_dir(dentry);
8341 +       if (unlikely(err))
8342 +               goto out;
8343 +
8344 +       file->f_version = dentry->d_inode->i_version;
8345 +       bindex = au_dbstart(dentry);
8346 +       au_set_fbstart(file, bindex);
8347 +       btail = au_dbtaildir(dentry);
8348 +       au_set_fbend_dir(file, btail);
8349 +       for (; !err && bindex <= btail; bindex++) {
8350 +               h_dentry = au_h_dptr(dentry, bindex);
8351 +               if (!h_dentry)
8352 +                       continue;
8353 +
8354 +               h_file = au_h_open(dentry, bindex, flags, file);
8355 +               if (IS_ERR(h_file)) {
8356 +                       err = PTR_ERR(h_file);
8357 +                       break;
8358 +               }
8359 +               au_set_h_fptr(file, bindex, h_file);
8360 +       }
8361 +       au_update_figen(file);
8362 +       /* todo: necessary? */
8363 +       /* file->f_ra = h_file->f_ra; */
8364 +       if (!err)
8365 +               return 0; /* success */
8366 +
8367 +       /* close all */
8368 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
8369 +               au_set_h_fptr(file, bindex, NULL);
8370 +       au_set_fbstart(file, -1);
8371 +       au_set_fbend_dir(file, -1);
8372 +
8373 +out:
8374 +       return err;
8375 +}
8376 +
8377 +static int aufs_open_dir(struct inode *inode __maybe_unused,
8378 +                        struct file *file)
8379 +{
8380 +       int err;
8381 +       struct super_block *sb;
8382 +       struct au_fidir *fidir;
8383 +
8384 +       err = -ENOMEM;
8385 +       sb = file->f_dentry->d_sb;
8386 +       si_read_lock(sb, AuLock_FLUSH);
8387 +       fidir = au_fidir_alloc(sb);
8388 +       if (fidir) {
8389 +               err = au_do_open(file, do_open_dir, fidir);
8390 +               if (unlikely(err))
8391 +                       kfree(fidir);
8392 +       }
8393 +       si_read_unlock(sb);
8394 +       return err;
8395 +}
8396 +
8397 +static int aufs_release_dir(struct inode *inode __maybe_unused,
8398 +                           struct file *file)
8399 +{
8400 +       struct au_vdir *vdir_cache;
8401 +       struct au_finfo *finfo;
8402 +       struct au_fidir *fidir;
8403 +       aufs_bindex_t bindex, bend;
8404 +
8405 +       finfo = au_fi(file);
8406 +       fidir = finfo->fi_hdir;
8407 +       if (fidir) {
8408 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
8409 +               if (vdir_cache)
8410 +                       au_vdir_free(vdir_cache);
8411 +
8412 +               bindex = finfo->fi_btop;
8413 +               if (bindex >= 0) {
8414 +                       /*
8415 +                        * calls fput() instead of filp_close(),
8416 +                        * since no dnotify or lock for the lower file.
8417 +                        */
8418 +                       bend = fidir->fd_bbot;
8419 +                       for (; bindex <= bend; bindex++)
8420 +                               au_set_h_fptr(file, bindex, NULL);
8421 +               }
8422 +               kfree(fidir);
8423 +               finfo->fi_hdir = NULL;
8424 +       }
8425 +       au_finfo_fin(file);
8426 +       return 0;
8427 +}
8428 +
8429 +/* ---------------------------------------------------------------------- */
8430 +
8431 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
8432 +{
8433 +       int err;
8434 +       aufs_bindex_t bindex, bend;
8435 +       struct file *h_file;
8436 +
8437 +       err = 0;
8438 +       bend = au_fbend_dir(file);
8439 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8440 +               h_file = au_hf_dir(file, bindex);
8441 +               if (h_file)
8442 +                       err = vfsub_flush(h_file, id);
8443 +       }
8444 +       return err;
8445 +}
8446 +
8447 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
8448 +{
8449 +       return au_do_flush(file, id, au_do_flush_dir);
8450 +}
8451 +
8452 +/* ---------------------------------------------------------------------- */
8453 +
8454 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
8455 +{
8456 +       int err;
8457 +       aufs_bindex_t bend, bindex;
8458 +       struct inode *inode;
8459 +       struct super_block *sb;
8460 +
8461 +       err = 0;
8462 +       sb = dentry->d_sb;
8463 +       inode = dentry->d_inode;
8464 +       IMustLock(inode);
8465 +       bend = au_dbend(dentry);
8466 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
8467 +               struct path h_path;
8468 +
8469 +               if (au_test_ro(sb, bindex, inode))
8470 +                       continue;
8471 +               h_path.dentry = au_h_dptr(dentry, bindex);
8472 +               if (!h_path.dentry)
8473 +                       continue;
8474 +
8475 +               h_path.mnt = au_sbr_mnt(sb, bindex);
8476 +               err = vfsub_fsync(NULL, &h_path, datasync);
8477 +       }
8478 +
8479 +       return err;
8480 +}
8481 +
8482 +static int au_do_fsync_dir(struct file *file, int datasync)
8483 +{
8484 +       int err;
8485 +       aufs_bindex_t bend, bindex;
8486 +       struct file *h_file;
8487 +       struct super_block *sb;
8488 +       struct inode *inode;
8489 +
8490 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8491 +       if (unlikely(err))
8492 +               goto out;
8493 +
8494 +       sb = file->f_dentry->d_sb;
8495 +       inode = file_inode(file);
8496 +       bend = au_fbend_dir(file);
8497 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8498 +               h_file = au_hf_dir(file, bindex);
8499 +               if (!h_file || au_test_ro(sb, bindex, inode))
8500 +                       continue;
8501 +
8502 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
8503 +       }
8504 +
8505 +out:
8506 +       return err;
8507 +}
8508 +
8509 +/*
8510 + * @file may be NULL
8511 + */
8512 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
8513 +                         int datasync)
8514 +{
8515 +       int err;
8516 +       struct dentry *dentry;
8517 +       struct super_block *sb;
8518 +       struct mutex *mtx;
8519 +
8520 +       err = 0;
8521 +       dentry = file->f_dentry;
8522 +       mtx = &dentry->d_inode->i_mutex;
8523 +       mutex_lock(mtx);
8524 +       sb = dentry->d_sb;
8525 +       si_noflush_read_lock(sb);
8526 +       if (file)
8527 +               err = au_do_fsync_dir(file, datasync);
8528 +       else {
8529 +               di_write_lock_child(dentry);
8530 +               err = au_do_fsync_dir_no_file(dentry, datasync);
8531 +       }
8532 +       au_cpup_attr_timesizes(dentry->d_inode);
8533 +       di_write_unlock(dentry);
8534 +       if (file)
8535 +               fi_write_unlock(file);
8536 +
8537 +       si_read_unlock(sb);
8538 +       mutex_unlock(mtx);
8539 +       return err;
8540 +}
8541 +
8542 +/* ---------------------------------------------------------------------- */
8543 +
8544 +static int aufs_readdir(struct file *file, void *dirent, filldir_t filldir)
8545 +{
8546 +       int err;
8547 +       struct dentry *dentry;
8548 +       struct inode *inode, *h_inode;
8549 +       struct super_block *sb;
8550 +
8551 +       dentry = file->f_dentry;
8552 +       inode = dentry->d_inode;
8553 +       IMustLock(inode);
8554 +
8555 +       sb = dentry->d_sb;
8556 +       si_read_lock(sb, AuLock_FLUSH);
8557 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8558 +       if (unlikely(err))
8559 +               goto out;
8560 +       err = au_alive_dir(dentry);
8561 +       if (!err)
8562 +               err = au_vdir_init(file);
8563 +       di_downgrade_lock(dentry, AuLock_IR);
8564 +       if (unlikely(err))
8565 +               goto out_unlock;
8566 +
8567 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
8568 +       if (!au_test_nfsd()) {
8569 +               err = au_vdir_fill_de(file, dirent, filldir);
8570 +               fsstack_copy_attr_atime(inode, h_inode);
8571 +       } else {
8572 +               /*
8573 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
8574 +                * encode_fh() and others.
8575 +                */
8576 +               atomic_inc(&h_inode->i_count);
8577 +               di_read_unlock(dentry, AuLock_IR);
8578 +               si_read_unlock(sb);
8579 +               err = au_vdir_fill_de(file, dirent, filldir);
8580 +               fsstack_copy_attr_atime(inode, h_inode);
8581 +               fi_write_unlock(file);
8582 +               iput(h_inode);
8583 +
8584 +               AuTraceErr(err);
8585 +               return err;
8586 +       }
8587 +
8588 +out_unlock:
8589 +       di_read_unlock(dentry, AuLock_IR);
8590 +       fi_write_unlock(file);
8591 +out:
8592 +       si_read_unlock(sb);
8593 +       return err;
8594 +}
8595 +
8596 +/* ---------------------------------------------------------------------- */
8597 +
8598 +#define AuTestEmpty_WHONLY     1
8599 +#define AuTestEmpty_CALLED     (1 << 1)
8600 +#define AuTestEmpty_SHWH       (1 << 2)
8601 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
8602 +#define au_fset_testempty(flags, name) \
8603 +       do { (flags) |= AuTestEmpty_##name; } while (0)
8604 +#define au_fclr_testempty(flags, name) \
8605 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
8606 +
8607 +#ifndef CONFIG_AUFS_SHWH
8608 +#undef AuTestEmpty_SHWH
8609 +#define AuTestEmpty_SHWH       0
8610 +#endif
8611 +
8612 +struct test_empty_arg {
8613 +       struct au_nhash *whlist;
8614 +       unsigned int flags;
8615 +       int err;
8616 +       aufs_bindex_t bindex;
8617 +};
8618 +
8619 +static int test_empty_cb(void *__arg, const char *__name, int namelen,
8620 +                        loff_t offset __maybe_unused, u64 ino,
8621 +                        unsigned int d_type)
8622 +{
8623 +       struct test_empty_arg *arg = __arg;
8624 +       char *name = (void *)__name;
8625 +
8626 +       arg->err = 0;
8627 +       au_fset_testempty(arg->flags, CALLED);
8628 +       /* smp_mb(); */
8629 +       if (name[0] == '.'
8630 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
8631 +               goto out; /* success */
8632 +
8633 +       if (namelen <= AUFS_WH_PFX_LEN
8634 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
8635 +               if (au_ftest_testempty(arg->flags, WHONLY)
8636 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
8637 +                       arg->err = -ENOTEMPTY;
8638 +               goto out;
8639 +       }
8640 +
8641 +       name += AUFS_WH_PFX_LEN;
8642 +       namelen -= AUFS_WH_PFX_LEN;
8643 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
8644 +               arg->err = au_nhash_append_wh
8645 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
8646 +                        au_ftest_testempty(arg->flags, SHWH));
8647 +
8648 +out:
8649 +       /* smp_mb(); */
8650 +       AuTraceErr(arg->err);
8651 +       return arg->err;
8652 +}
8653 +
8654 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8655 +{
8656 +       int err;
8657 +       struct file *h_file;
8658 +
8659 +       h_file = au_h_open(dentry, arg->bindex,
8660 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
8661 +                          /*file*/NULL);
8662 +       err = PTR_ERR(h_file);
8663 +       if (IS_ERR(h_file))
8664 +               goto out;
8665 +
8666 +       err = 0;
8667 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
8668 +           && !file_inode(h_file)->i_nlink)
8669 +               goto out_put;
8670 +
8671 +       do {
8672 +               arg->err = 0;
8673 +               au_fclr_testempty(arg->flags, CALLED);
8674 +               /* smp_mb(); */
8675 +               err = vfsub_readdir(h_file, test_empty_cb, arg);
8676 +               if (err >= 0)
8677 +                       err = arg->err;
8678 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
8679 +
8680 +out_put:
8681 +       fput(h_file);
8682 +       au_sbr_put(dentry->d_sb, arg->bindex);
8683 +out:
8684 +       return err;
8685 +}
8686 +
8687 +struct do_test_empty_args {
8688 +       int *errp;
8689 +       struct dentry *dentry;
8690 +       struct test_empty_arg *arg;
8691 +};
8692 +
8693 +static void call_do_test_empty(void *args)
8694 +{
8695 +       struct do_test_empty_args *a = args;
8696 +       *a->errp = do_test_empty(a->dentry, a->arg);
8697 +}
8698 +
8699 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8700 +{
8701 +       int err, wkq_err;
8702 +       struct dentry *h_dentry;
8703 +       struct inode *h_inode;
8704 +
8705 +       h_dentry = au_h_dptr(dentry, arg->bindex);
8706 +       h_inode = h_dentry->d_inode;
8707 +       /* todo: i_mode changes anytime? */
8708 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
8709 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
8710 +       mutex_unlock(&h_inode->i_mutex);
8711 +       if (!err)
8712 +               err = do_test_empty(dentry, arg);
8713 +       else {
8714 +               struct do_test_empty_args args = {
8715 +                       .errp   = &err,
8716 +                       .dentry = dentry,
8717 +                       .arg    = arg
8718 +               };
8719 +               unsigned int flags = arg->flags;
8720 +
8721 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
8722 +               if (unlikely(wkq_err))
8723 +                       err = wkq_err;
8724 +               arg->flags = flags;
8725 +       }
8726 +
8727 +       return err;
8728 +}
8729 +
8730 +int au_test_empty_lower(struct dentry *dentry)
8731 +{
8732 +       int err;
8733 +       unsigned int rdhash;
8734 +       aufs_bindex_t bindex, bstart, btail;
8735 +       struct au_nhash whlist;
8736 +       struct test_empty_arg arg;
8737 +
8738 +       SiMustAnyLock(dentry->d_sb);
8739 +
8740 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
8741 +       if (!rdhash)
8742 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
8743 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
8744 +       if (unlikely(err))
8745 +               goto out;
8746 +
8747 +       arg.flags = 0;
8748 +       arg.whlist = &whlist;
8749 +       bstart = au_dbstart(dentry);
8750 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
8751 +               au_fset_testempty(arg.flags, SHWH);
8752 +       arg.bindex = bstart;
8753 +       err = do_test_empty(dentry, &arg);
8754 +       if (unlikely(err))
8755 +               goto out_whlist;
8756 +
8757 +       au_fset_testempty(arg.flags, WHONLY);
8758 +       btail = au_dbtaildir(dentry);
8759 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
8760 +               struct dentry *h_dentry;
8761 +
8762 +               h_dentry = au_h_dptr(dentry, bindex);
8763 +               if (h_dentry && h_dentry->d_inode) {
8764 +                       arg.bindex = bindex;
8765 +                       err = do_test_empty(dentry, &arg);
8766 +               }
8767 +       }
8768 +
8769 +out_whlist:
8770 +       au_nhash_wh_free(&whlist);
8771 +out:
8772 +       return err;
8773 +}
8774 +
8775 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
8776 +{
8777 +       int err;
8778 +       struct test_empty_arg arg;
8779 +       aufs_bindex_t bindex, btail;
8780 +
8781 +       err = 0;
8782 +       arg.whlist = whlist;
8783 +       arg.flags = AuTestEmpty_WHONLY;
8784 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
8785 +               au_fset_testempty(arg.flags, SHWH);
8786 +       btail = au_dbtaildir(dentry);
8787 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
8788 +               struct dentry *h_dentry;
8789 +
8790 +               h_dentry = au_h_dptr(dentry, bindex);
8791 +               if (h_dentry && h_dentry->d_inode) {
8792 +                       arg.bindex = bindex;
8793 +                       err = sio_test_empty(dentry, &arg);
8794 +               }
8795 +       }
8796 +
8797 +       return err;
8798 +}
8799 +
8800 +/* ---------------------------------------------------------------------- */
8801 +
8802 +const struct file_operations aufs_dir_fop = {
8803 +       .owner          = THIS_MODULE,
8804 +       .llseek         = default_llseek,
8805 +       .read           = generic_read_dir,
8806 +       .readdir        = aufs_readdir,
8807 +       .unlocked_ioctl = aufs_ioctl_dir,
8808 +#ifdef CONFIG_COMPAT
8809 +       .compat_ioctl   = aufs_compat_ioctl_dir,
8810 +#endif
8811 +       .open           = aufs_open_dir,
8812 +       .release        = aufs_release_dir,
8813 +       .flush          = aufs_flush_dir,
8814 +       .fsync          = aufs_fsync_dir
8815 +};
8816 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
8817 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
8818 +++ linux/fs/aufs/dir.h 2013-07-30 22:42:55.839613269 +0200
8819 @@ -0,0 +1,137 @@
8820 +/*
8821 + * Copyright (C) 2005-2013 Junjiro R. Okajima
8822 + *
8823 + * This program, aufs is free software; you can redistribute it and/or modify
8824 + * it under the terms of the GNU General Public License as published by
8825 + * the Free Software Foundation; either version 2 of the License, or
8826 + * (at your option) any later version.
8827 + *
8828 + * This program is distributed in the hope that it will be useful,
8829 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8830 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8831 + * GNU General Public License for more details.
8832 + *
8833 + * You should have received a copy of the GNU General Public License
8834 + * along with this program; if not, write to the Free Software
8835 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
8836 + */
8837 +
8838 +/*
8839 + * directory operations
8840 + */
8841 +
8842 +#ifndef __AUFS_DIR_H__
8843 +#define __AUFS_DIR_H__
8844 +
8845 +#ifdef __KERNEL__
8846 +
8847 +#include <linux/fs.h>
8848 +
8849 +/* ---------------------------------------------------------------------- */
8850 +
8851 +/* need to be faster and smaller */
8852 +
8853 +struct au_nhash {
8854 +       unsigned int            nh_num;
8855 +       struct hlist_head       *nh_head;
8856 +};
8857 +
8858 +struct au_vdir_destr {
8859 +       unsigned char   len;
8860 +       unsigned char   name[0];
8861 +} __packed;
8862 +
8863 +struct au_vdir_dehstr {
8864 +       struct hlist_node       hash;
8865 +       struct au_vdir_destr    *str;
8866 +} ____cacheline_aligned_in_smp;
8867 +
8868 +struct au_vdir_de {
8869 +       ino_t                   de_ino;
8870 +       unsigned char           de_type;
8871 +       /* caution: packed */
8872 +       struct au_vdir_destr    de_str;
8873 +} __packed;
8874 +
8875 +struct au_vdir_wh {
8876 +       struct hlist_node       wh_hash;
8877 +#ifdef CONFIG_AUFS_SHWH
8878 +       ino_t                   wh_ino;
8879 +       aufs_bindex_t           wh_bindex;
8880 +       unsigned char           wh_type;
8881 +#else
8882 +       aufs_bindex_t           wh_bindex;
8883 +#endif
8884 +       /* caution: packed */
8885 +       struct au_vdir_destr    wh_str;
8886 +} __packed;
8887 +
8888 +union au_vdir_deblk_p {
8889 +       unsigned char           *deblk;
8890 +       struct au_vdir_de       *de;
8891 +};
8892 +
8893 +struct au_vdir {
8894 +       unsigned char   **vd_deblk;
8895 +       unsigned long   vd_nblk;
8896 +       struct {
8897 +               unsigned long           ul;
8898 +               union au_vdir_deblk_p   p;
8899 +       } vd_last;
8900 +
8901 +       unsigned long   vd_version;
8902 +       unsigned int    vd_deblk_sz;
8903 +       unsigned long   vd_jiffy;
8904 +} ____cacheline_aligned_in_smp;
8905 +
8906 +/* ---------------------------------------------------------------------- */
8907 +
8908 +/* dir.c */
8909 +extern const struct file_operations aufs_dir_fop;
8910 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
8911 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
8912 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
8913 +int au_test_empty_lower(struct dentry *dentry);
8914 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
8915 +
8916 +/* vdir.c */
8917 +unsigned int au_rdhash_est(loff_t sz);
8918 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
8919 +void au_nhash_wh_free(struct au_nhash *whlist);
8920 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
8921 +                           int limit);
8922 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
8923 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
8924 +                      unsigned int d_type, aufs_bindex_t bindex,
8925 +                      unsigned char shwh);
8926 +void au_vdir_free(struct au_vdir *vdir);
8927 +int au_vdir_init(struct file *file);
8928 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir);
8929 +
8930 +/* ioctl.c */
8931 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
8932 +
8933 +#ifdef CONFIG_AUFS_RDU
8934 +/* rdu.c */
8935 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
8936 +#ifdef CONFIG_COMPAT
8937 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
8938 +                        unsigned long arg);
8939 +#endif
8940 +#else
8941 +static inline long au_rdu_ioctl(struct file *file, unsigned int cmd,
8942 +                               unsigned long arg)
8943 +{
8944 +       return -EINVAL;
8945 +}
8946 +#ifdef CONFIG_COMPAT
8947 +static inline long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
8948 +                                      unsigned long arg)
8949 +{
8950 +       return -EINVAL;
8951 +}
8952 +#endif
8953 +#endif
8954 +
8955 +#endif /* __KERNEL__ */
8956 +#endif /* __AUFS_DIR_H__ */
8957 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
8958 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
8959 +++ linux/fs/aufs/dynop.c       2013-07-30 22:42:55.839613269 +0200
8960 @@ -0,0 +1,379 @@
8961 +/*
8962 + * Copyright (C) 2010-2013 Junjiro R. Okajima
8963 + *
8964 + * This program, aufs is free software; you can redistribute it and/or modify
8965 + * it under the terms of the GNU General Public License as published by
8966 + * the Free Software Foundation; either version 2 of the License, or
8967 + * (at your option) any later version.
8968 + *
8969 + * This program is distributed in the hope that it will be useful,
8970 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8971 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8972 + * GNU General Public License for more details.
8973 + *
8974 + * You should have received a copy of the GNU General Public License
8975 + * along with this program; if not, write to the Free Software
8976 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
8977 + */
8978 +
8979 +/*
8980 + * dynamically customizable operations for regular files
8981 + */
8982 +
8983 +#include "aufs.h"
8984 +
8985 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
8986 +
8987 +/*
8988 + * How large will these lists be?
8989 + * Usually just a few elements, 20-30 at most for each, I guess.
8990 + */
8991 +static struct au_splhead dynop[AuDyLast];
8992 +
8993 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
8994 +{
8995 +       struct au_dykey *key, *tmp;
8996 +       struct list_head *head;
8997 +
8998 +       key = NULL;
8999 +       head = &spl->head;
9000 +       rcu_read_lock();
9001 +       list_for_each_entry_rcu(tmp, head, dk_list)
9002 +               if (tmp->dk_op.dy_hop == h_op) {
9003 +                       key = tmp;
9004 +                       kref_get(&key->dk_kref);
9005 +                       break;
9006 +               }
9007 +       rcu_read_unlock();
9008 +
9009 +       return key;
9010 +}
9011 +
9012 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
9013 +{
9014 +       struct au_dykey **k, *found;
9015 +       const void *h_op = key->dk_op.dy_hop;
9016 +       int i;
9017 +
9018 +       found = NULL;
9019 +       k = br->br_dykey;
9020 +       for (i = 0; i < AuBrDynOp; i++)
9021 +               if (k[i]) {
9022 +                       if (k[i]->dk_op.dy_hop == h_op) {
9023 +                               found = k[i];
9024 +                               break;
9025 +                       }
9026 +               } else
9027 +                       break;
9028 +       if (!found) {
9029 +               spin_lock(&br->br_dykey_lock);
9030 +               for (; i < AuBrDynOp; i++)
9031 +                       if (k[i]) {
9032 +                               if (k[i]->dk_op.dy_hop == h_op) {
9033 +                                       found = k[i];
9034 +                                       break;
9035 +                               }
9036 +                       } else {
9037 +                               k[i] = key;
9038 +                               break;
9039 +                       }
9040 +               spin_unlock(&br->br_dykey_lock);
9041 +               BUG_ON(i == AuBrDynOp); /* expand the array */
9042 +       }
9043 +
9044 +       return found;
9045 +}
9046 +
9047 +/* kref_get() if @key is already added */
9048 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
9049 +{
9050 +       struct au_dykey *tmp, *found;
9051 +       struct list_head *head;
9052 +       const void *h_op = key->dk_op.dy_hop;
9053 +
9054 +       found = NULL;
9055 +       head = &spl->head;
9056 +       spin_lock(&spl->spin);
9057 +       list_for_each_entry(tmp, head, dk_list)
9058 +               if (tmp->dk_op.dy_hop == h_op) {
9059 +                       kref_get(&tmp->dk_kref);
9060 +                       found = tmp;
9061 +                       break;
9062 +               }
9063 +       if (!found)
9064 +               list_add_rcu(&key->dk_list, head);
9065 +       spin_unlock(&spl->spin);
9066 +
9067 +       if (!found)
9068 +               DyPrSym(key);
9069 +       return found;
9070 +}
9071 +
9072 +static void dy_free_rcu(struct rcu_head *rcu)
9073 +{
9074 +       struct au_dykey *key;
9075 +
9076 +       key = container_of(rcu, struct au_dykey, dk_rcu);
9077 +       DyPrSym(key);
9078 +       kfree(key);
9079 +}
9080 +
9081 +static void dy_free(struct kref *kref)
9082 +{
9083 +       struct au_dykey *key;
9084 +       struct au_splhead *spl;
9085 +
9086 +       key = container_of(kref, struct au_dykey, dk_kref);
9087 +       spl = dynop + key->dk_op.dy_type;
9088 +       au_spl_del_rcu(&key->dk_list, spl);
9089 +       call_rcu(&key->dk_rcu, dy_free_rcu);
9090 +}
9091 +
9092 +void au_dy_put(struct au_dykey *key)
9093 +{
9094 +       kref_put(&key->dk_kref, dy_free);
9095 +}
9096 +
9097 +/* ---------------------------------------------------------------------- */
9098 +
9099 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
9100 +
9101 +#ifdef CONFIG_AUFS_DEBUG
9102 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
9103 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
9104 +#else
9105 +#define DyDbgDeclare(cnt)      do {} while (0)
9106 +#define DyDbgInc(cnt)          do {} while (0)
9107 +#endif
9108 +
9109 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
9110 +       DyDbgInc(cnt);                                                  \
9111 +       if (h_op->func) {                                               \
9112 +               if (src.func)                                           \
9113 +                       dst.func = src.func;                            \
9114 +               else                                                    \
9115 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
9116 +       }                                                               \
9117 +} while (0)
9118 +
9119 +#define DySetForce(func, dst, src) do {                \
9120 +       AuDebugOn(!src.func);                   \
9121 +       DyDbgInc(cnt);                          \
9122 +       dst.func = src.func;                    \
9123 +} while (0)
9124 +
9125 +#define DySetAop(func) \
9126 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
9127 +#define DySetAopForce(func) \
9128 +       DySetForce(func, dyaop->da_op, aufs_aop)
9129 +
9130 +static void dy_aop(struct au_dykey *key, const void *h_op,
9131 +                  struct super_block *h_sb __maybe_unused)
9132 +{
9133 +       struct au_dyaop *dyaop = (void *)key;
9134 +       const struct address_space_operations *h_aop = h_op;
9135 +       DyDbgDeclare(cnt);
9136 +
9137 +       AuDbg("%s\n", au_sbtype(h_sb));
9138 +
9139 +       DySetAop(writepage);
9140 +       DySetAopForce(readpage);        /* force */
9141 +       DySetAop(writepages);
9142 +       DySetAop(set_page_dirty);
9143 +       DySetAop(readpages);
9144 +       DySetAop(write_begin);
9145 +       DySetAop(write_end);
9146 +       DySetAop(bmap);
9147 +       DySetAop(invalidatepage);
9148 +       DySetAop(releasepage);
9149 +       DySetAop(freepage);
9150 +       /* these two will be changed according to an aufs mount option */
9151 +       DySetAop(direct_IO);
9152 +       DySetAop(get_xip_mem);
9153 +       DySetAop(migratepage);
9154 +       DySetAop(launder_page);
9155 +       DySetAop(is_partially_uptodate);
9156 +       DySetAop(error_remove_page);
9157 +       DySetAop(swap_activate);
9158 +       DySetAop(swap_deactivate);
9159 +
9160 +       DyDbgSize(cnt, *h_aop);
9161 +       dyaop->da_get_xip_mem = h_aop->get_xip_mem;
9162 +}
9163 +
9164 +/* ---------------------------------------------------------------------- */
9165 +
9166 +static void dy_bug(struct kref *kref)
9167 +{
9168 +       BUG();
9169 +}
9170 +
9171 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
9172 +{
9173 +       struct au_dykey *key, *old;
9174 +       struct au_splhead *spl;
9175 +       struct op {
9176 +               unsigned int sz;
9177 +               void (*set)(struct au_dykey *key, const void *h_op,
9178 +                           struct super_block *h_sb __maybe_unused);
9179 +       };
9180 +       static const struct op a[] = {
9181 +               [AuDy_AOP] = {
9182 +                       .sz     = sizeof(struct au_dyaop),
9183 +                       .set    = dy_aop
9184 +               }
9185 +       };
9186 +       const struct op *p;
9187 +
9188 +       spl = dynop + op->dy_type;
9189 +       key = dy_gfind_get(spl, op->dy_hop);
9190 +       if (key)
9191 +               goto out_add; /* success */
9192 +
9193 +       p = a + op->dy_type;
9194 +       key = kzalloc(p->sz, GFP_NOFS);
9195 +       if (unlikely(!key)) {
9196 +               key = ERR_PTR(-ENOMEM);
9197 +               goto out;
9198 +       }
9199 +
9200 +       key->dk_op.dy_hop = op->dy_hop;
9201 +       kref_init(&key->dk_kref);
9202 +       p->set(key, op->dy_hop, au_br_sb(br));
9203 +       old = dy_gadd(spl, key);
9204 +       if (old) {
9205 +               kfree(key);
9206 +               key = old;
9207 +       }
9208 +
9209 +out_add:
9210 +       old = dy_bradd(br, key);
9211 +       if (old)
9212 +               /* its ref-count should never be zero here */
9213 +               kref_put(&key->dk_kref, dy_bug);
9214 +out:
9215 +       return key;
9216 +}
9217 +
9218 +/* ---------------------------------------------------------------------- */
9219 +/*
9220 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
9221 + * This behaviour is neccessary to return an error from open(O_DIRECT) instead
9222 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
9223 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
9224 + * See the aufs manual in detail.
9225 + *
9226 + * To keep this behaviour, aufs has to set NULL to ->get_xip_mem too, and the
9227 + * performance of fadvise() and madvise() may be affected.
9228 + */
9229 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
9230 +{
9231 +       if (!do_dx) {
9232 +               dyaop->da_op.direct_IO = NULL;
9233 +               dyaop->da_op.get_xip_mem = NULL;
9234 +       } else {
9235 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
9236 +               dyaop->da_op.get_xip_mem = aufs_aop.get_xip_mem;
9237 +               if (!dyaop->da_get_xip_mem)
9238 +                       dyaop->da_op.get_xip_mem = NULL;
9239 +       }
9240 +}
9241 +
9242 +static struct au_dyaop *dy_aget(struct au_branch *br,
9243 +                               const struct address_space_operations *h_aop,
9244 +                               int do_dx)
9245 +{
9246 +       struct au_dyaop *dyaop;
9247 +       struct au_dynop op;
9248 +
9249 +       op.dy_type = AuDy_AOP;
9250 +       op.dy_haop = h_aop;
9251 +       dyaop = (void *)dy_get(&op, br);
9252 +       if (IS_ERR(dyaop))
9253 +               goto out;
9254 +       dy_adx(dyaop, do_dx);
9255 +
9256 +out:
9257 +       return dyaop;
9258 +}
9259 +
9260 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
9261 +               struct inode *h_inode)
9262 +{
9263 +       int err, do_dx;
9264 +       struct super_block *sb;
9265 +       struct au_branch *br;
9266 +       struct au_dyaop *dyaop;
9267 +
9268 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
9269 +       IiMustWriteLock(inode);
9270 +
9271 +       sb = inode->i_sb;
9272 +       br = au_sbr(sb, bindex);
9273 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
9274 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
9275 +       err = PTR_ERR(dyaop);
9276 +       if (IS_ERR(dyaop))
9277 +               /* unnecessary to call dy_fput() */
9278 +               goto out;
9279 +
9280 +       err = 0;
9281 +       inode->i_mapping->a_ops = &dyaop->da_op;
9282 +
9283 +out:
9284 +       return err;
9285 +}
9286 +
9287 +/*
9288 + * Is it safe to replace a_ops during the inode/file is in operation?
9289 + * Yes, I hope so.
9290 + */
9291 +int au_dy_irefresh(struct inode *inode)
9292 +{
9293 +       int err;
9294 +       aufs_bindex_t bstart;
9295 +       struct inode *h_inode;
9296 +
9297 +       err = 0;
9298 +       if (S_ISREG(inode->i_mode)) {
9299 +               bstart = au_ibstart(inode);
9300 +               h_inode = au_h_iptr(inode, bstart);
9301 +               err = au_dy_iaop(inode, bstart, h_inode);
9302 +       }
9303 +       return err;
9304 +}
9305 +
9306 +void au_dy_arefresh(int do_dx)
9307 +{
9308 +       struct au_splhead *spl;
9309 +       struct list_head *head;
9310 +       struct au_dykey *key;
9311 +
9312 +       spl = dynop + AuDy_AOP;
9313 +       head = &spl->head;
9314 +       spin_lock(&spl->spin);
9315 +       list_for_each_entry(key, head, dk_list)
9316 +               dy_adx((void *)key, do_dx);
9317 +       spin_unlock(&spl->spin);
9318 +}
9319 +
9320 +/* ---------------------------------------------------------------------- */
9321 +
9322 +void __init au_dy_init(void)
9323 +{
9324 +       int i;
9325 +
9326 +       /* make sure that 'struct au_dykey *' can be any type */
9327 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
9328 +
9329 +       for (i = 0; i < AuDyLast; i++)
9330 +               au_spl_init(dynop + i);
9331 +}
9332 +
9333 +void au_dy_fin(void)
9334 +{
9335 +       int i;
9336 +
9337 +       for (i = 0; i < AuDyLast; i++)
9338 +               WARN_ON(!list_empty(&dynop[i].head));
9339 +}
9340 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
9341 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
9342 +++ linux/fs/aufs/dynop.h       2013-07-06 13:20:47.740198107 +0200
9343 @@ -0,0 +1,76 @@
9344 +/*
9345 + * Copyright (C) 2010-2013 Junjiro R. Okajima
9346 + *
9347 + * This program, aufs is free software; you can redistribute it and/or modify
9348 + * it under the terms of the GNU General Public License as published by
9349 + * the Free Software Foundation; either version 2 of the License, or
9350 + * (at your option) any later version.
9351 + *
9352 + * This program is distributed in the hope that it will be useful,
9353 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9354 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9355 + * GNU General Public License for more details.
9356 + *
9357 + * You should have received a copy of the GNU General Public License
9358 + * along with this program; if not, write to the Free Software
9359 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9360 + */
9361 +
9362 +/*
9363 + * dynamically customizable operations (for regular files only)
9364 + */
9365 +
9366 +#ifndef __AUFS_DYNOP_H__
9367 +#define __AUFS_DYNOP_H__
9368 +
9369 +#ifdef __KERNEL__
9370 +
9371 +#include "inode.h"
9372 +
9373 +enum {AuDy_AOP, AuDyLast};
9374 +
9375 +struct au_dynop {
9376 +       int                                             dy_type;
9377 +       union {
9378 +               const void                              *dy_hop;
9379 +               const struct address_space_operations   *dy_haop;
9380 +       };
9381 +};
9382 +
9383 +struct au_dykey {
9384 +       union {
9385 +               struct list_head        dk_list;
9386 +               struct rcu_head         dk_rcu;
9387 +       };
9388 +       struct au_dynop         dk_op;
9389 +
9390 +       /*
9391 +        * during I am in the branch local array, kref is gotten. when the
9392 +        * branch is removed, kref is put.
9393 +        */
9394 +       struct kref             dk_kref;
9395 +};
9396 +
9397 +/* stop unioning since their sizes are very different from each other */
9398 +struct au_dyaop {
9399 +       struct au_dykey                 da_key;
9400 +       struct address_space_operations da_op; /* not const */
9401 +       int (*da_get_xip_mem)(struct address_space *, pgoff_t, int,
9402 +                             void **, unsigned long *);
9403 +};
9404 +
9405 +/* ---------------------------------------------------------------------- */
9406 +
9407 +/* dynop.c */
9408 +struct au_branch;
9409 +void au_dy_put(struct au_dykey *key);
9410 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
9411 +               struct inode *h_inode);
9412 +int au_dy_irefresh(struct inode *inode);
9413 +void au_dy_arefresh(int do_dio);
9414 +
9415 +void __init au_dy_init(void);
9416 +void au_dy_fin(void);
9417 +
9418 +#endif /* __KERNEL__ */
9419 +#endif /* __AUFS_DYNOP_H__ */
9420 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
9421 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
9422 +++ linux/fs/aufs/export.c      2013-07-30 22:42:55.839613269 +0200
9423 @@ -0,0 +1,826 @@
9424 +/*
9425 + * Copyright (C) 2005-2013 Junjiro R. Okajima
9426 + *
9427 + * This program, aufs is free software; you can redistribute it and/or modify
9428 + * it under the terms of the GNU General Public License as published by
9429 + * the Free Software Foundation; either version 2 of the License, or
9430 + * (at your option) any later version.
9431 + *
9432 + * This program is distributed in the hope that it will be useful,
9433 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9434 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9435 + * GNU General Public License for more details.
9436 + *
9437 + * You should have received a copy of the GNU General Public License
9438 + * along with this program; if not, write to the Free Software
9439 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9440 + */
9441 +
9442 +/*
9443 + * export via nfs
9444 + */
9445 +
9446 +#include <linux/exportfs.h>
9447 +#include <linux/fs_struct.h>
9448 +#include <linux/namei.h>
9449 +#include <linux/nsproxy.h>
9450 +#include <linux/random.h>
9451 +#include <linux/writeback.h>
9452 +#include "../fs/mount.h"
9453 +#include "aufs.h"
9454 +
9455 +union conv {
9456 +#ifdef CONFIG_AUFS_INO_T_64
9457 +       __u32 a[2];
9458 +#else
9459 +       __u32 a[1];
9460 +#endif
9461 +       ino_t ino;
9462 +};
9463 +
9464 +static ino_t decode_ino(__u32 *a)
9465 +{
9466 +       union conv u;
9467 +
9468 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
9469 +       u.a[0] = a[0];
9470 +#ifdef CONFIG_AUFS_INO_T_64
9471 +       u.a[1] = a[1];
9472 +#endif
9473 +       return u.ino;
9474 +}
9475 +
9476 +static void encode_ino(__u32 *a, ino_t ino)
9477 +{
9478 +       union conv u;
9479 +
9480 +       u.ino = ino;
9481 +       a[0] = u.a[0];
9482 +#ifdef CONFIG_AUFS_INO_T_64
9483 +       a[1] = u.a[1];
9484 +#endif
9485 +}
9486 +
9487 +/* NFS file handle */
9488 +enum {
9489 +       Fh_br_id,
9490 +       Fh_sigen,
9491 +#ifdef CONFIG_AUFS_INO_T_64
9492 +       /* support 64bit inode number */
9493 +       Fh_ino1,
9494 +       Fh_ino2,
9495 +       Fh_dir_ino1,
9496 +       Fh_dir_ino2,
9497 +#else
9498 +       Fh_ino1,
9499 +       Fh_dir_ino1,
9500 +#endif
9501 +       Fh_igen,
9502 +       Fh_h_type,
9503 +       Fh_tail,
9504 +
9505 +       Fh_ino = Fh_ino1,
9506 +       Fh_dir_ino = Fh_dir_ino1
9507 +};
9508 +
9509 +static int au_test_anon(struct dentry *dentry)
9510 +{
9511 +       /* note: read d_flags without d_lock */
9512 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
9513 +}
9514 +
9515 +int au_test_nfsd(void)
9516 +{
9517 +       int ret;
9518 +       struct task_struct *tsk = current;
9519 +       char comm[sizeof(tsk->comm)];
9520 +
9521 +       ret = 0;
9522 +       if (tsk->flags & PF_KTHREAD) {
9523 +               get_task_comm(comm, tsk);
9524 +               ret = !strcmp(comm, "nfsd");
9525 +       }
9526 +
9527 +       return ret;
9528 +}
9529 +
9530 +/* ---------------------------------------------------------------------- */
9531 +/* inode generation external table */
9532 +
9533 +void au_xigen_inc(struct inode *inode)
9534 +{
9535 +       loff_t pos;
9536 +       ssize_t sz;
9537 +       __u32 igen;
9538 +       struct super_block *sb;
9539 +       struct au_sbinfo *sbinfo;
9540 +
9541 +       sb = inode->i_sb;
9542 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
9543 +
9544 +       sbinfo = au_sbi(sb);
9545 +       pos = inode->i_ino;
9546 +       pos *= sizeof(igen);
9547 +       igen = inode->i_generation + 1;
9548 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
9549 +                        sizeof(igen), &pos);
9550 +       if (sz == sizeof(igen))
9551 +               return; /* success */
9552 +
9553 +       if (unlikely(sz >= 0))
9554 +               AuIOErr("xigen error (%zd)\n", sz);
9555 +}
9556 +
9557 +int au_xigen_new(struct inode *inode)
9558 +{
9559 +       int err;
9560 +       loff_t pos;
9561 +       ssize_t sz;
9562 +       struct super_block *sb;
9563 +       struct au_sbinfo *sbinfo;
9564 +       struct file *file;
9565 +
9566 +       err = 0;
9567 +       /* todo: dirty, at mount time */
9568 +       if (inode->i_ino == AUFS_ROOT_INO)
9569 +               goto out;
9570 +       sb = inode->i_sb;
9571 +       SiMustAnyLock(sb);
9572 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
9573 +               goto out;
9574 +
9575 +       err = -EFBIG;
9576 +       pos = inode->i_ino;
9577 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
9578 +               AuIOErr1("too large i%lld\n", pos);
9579 +               goto out;
9580 +       }
9581 +       pos *= sizeof(inode->i_generation);
9582 +
9583 +       err = 0;
9584 +       sbinfo = au_sbi(sb);
9585 +       file = sbinfo->si_xigen;
9586 +       BUG_ON(!file);
9587 +
9588 +       if (vfsub_f_size_read(file)
9589 +           < pos + sizeof(inode->i_generation)) {
9590 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
9591 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
9592 +                                sizeof(inode->i_generation), &pos);
9593 +       } else
9594 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
9595 +                               sizeof(inode->i_generation), &pos);
9596 +       if (sz == sizeof(inode->i_generation))
9597 +               goto out; /* success */
9598 +
9599 +       err = sz;
9600 +       if (unlikely(sz >= 0)) {
9601 +               err = -EIO;
9602 +               AuIOErr("xigen error (%zd)\n", sz);
9603 +       }
9604 +
9605 +out:
9606 +       return err;
9607 +}
9608 +
9609 +int au_xigen_set(struct super_block *sb, struct file *base)
9610 +{
9611 +       int err;
9612 +       struct au_sbinfo *sbinfo;
9613 +       struct file *file;
9614 +
9615 +       SiMustWriteLock(sb);
9616 +
9617 +       sbinfo = au_sbi(sb);
9618 +       file = au_xino_create2(base, sbinfo->si_xigen);
9619 +       err = PTR_ERR(file);
9620 +       if (IS_ERR(file))
9621 +               goto out;
9622 +       err = 0;
9623 +       if (sbinfo->si_xigen)
9624 +               fput(sbinfo->si_xigen);
9625 +       sbinfo->si_xigen = file;
9626 +
9627 +out:
9628 +       return err;
9629 +}
9630 +
9631 +void au_xigen_clr(struct super_block *sb)
9632 +{
9633 +       struct au_sbinfo *sbinfo;
9634 +
9635 +       SiMustWriteLock(sb);
9636 +
9637 +       sbinfo = au_sbi(sb);
9638 +       if (sbinfo->si_xigen) {
9639 +               fput(sbinfo->si_xigen);
9640 +               sbinfo->si_xigen = NULL;
9641 +       }
9642 +}
9643 +
9644 +/* ---------------------------------------------------------------------- */
9645 +
9646 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
9647 +                                   ino_t dir_ino)
9648 +{
9649 +       struct dentry *dentry, *d;
9650 +       struct inode *inode;
9651 +       unsigned int sigen;
9652 +
9653 +       dentry = NULL;
9654 +       inode = ilookup(sb, ino);
9655 +       if (!inode)
9656 +               goto out;
9657 +
9658 +       dentry = ERR_PTR(-ESTALE);
9659 +       sigen = au_sigen(sb);
9660 +       if (unlikely(is_bad_inode(inode)
9661 +                    || IS_DEADDIR(inode)
9662 +                    || sigen != au_iigen(inode, NULL)))
9663 +               goto out_iput;
9664 +
9665 +       dentry = NULL;
9666 +       if (!dir_ino || S_ISDIR(inode->i_mode))
9667 +               dentry = d_find_alias(inode);
9668 +       else {
9669 +               spin_lock(&inode->i_lock);
9670 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
9671 +                       spin_lock(&d->d_lock);
9672 +                       if (!au_test_anon(d)
9673 +                           && d->d_parent->d_inode->i_ino == dir_ino) {
9674 +                               dentry = dget_dlock(d);
9675 +                               spin_unlock(&d->d_lock);
9676 +                               break;
9677 +                       }
9678 +                       spin_unlock(&d->d_lock);
9679 +               }
9680 +               spin_unlock(&inode->i_lock);
9681 +       }
9682 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
9683 +               /* need to refresh */
9684 +               dput(dentry);
9685 +               dentry = NULL;
9686 +       }
9687 +
9688 +out_iput:
9689 +       iput(inode);
9690 +out:
9691 +       AuTraceErrPtr(dentry);
9692 +       return dentry;
9693 +}
9694 +
9695 +/* ---------------------------------------------------------------------- */
9696 +
9697 +/* todo: dirty? */
9698 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
9699 +
9700 +struct au_compare_mnt_args {
9701 +       /* input */
9702 +       struct super_block *sb;
9703 +
9704 +       /* output */
9705 +       struct vfsmount *mnt;
9706 +};
9707 +
9708 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
9709 +{
9710 +       struct au_compare_mnt_args *a = arg;
9711 +
9712 +       if (mnt->mnt_sb != a->sb)
9713 +               return 0;
9714 +       a->mnt = mntget(mnt);
9715 +       return 1;
9716 +}
9717 +
9718 +static struct vfsmount *au_mnt_get(struct super_block *sb)
9719 +{
9720 +       int err;
9721 +       struct path root;
9722 +       struct au_compare_mnt_args args = {
9723 +               .sb = sb
9724 +       };
9725 +
9726 +       get_fs_root(current->fs, &root);
9727 +       rcu_read_lock();
9728 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
9729 +       rcu_read_unlock();
9730 +       path_put(&root);
9731 +       AuDebugOn(!err);
9732 +       AuDebugOn(!args.mnt);
9733 +       return args.mnt;
9734 +}
9735 +
9736 +struct au_nfsd_si_lock {
9737 +       unsigned int sigen;
9738 +       aufs_bindex_t bindex, br_id;
9739 +       unsigned char force_lock;
9740 +};
9741 +
9742 +static int si_nfsd_read_lock(struct super_block *sb,
9743 +                            struct au_nfsd_si_lock *nsi_lock)
9744 +{
9745 +       int err;
9746 +       aufs_bindex_t bindex;
9747 +
9748 +       si_read_lock(sb, AuLock_FLUSH);
9749 +
9750 +       /* branch id may be wrapped around */
9751 +       err = 0;
9752 +       bindex = au_br_index(sb, nsi_lock->br_id);
9753 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
9754 +               goto out; /* success */
9755 +
9756 +       err = -ESTALE;
9757 +       bindex = -1;
9758 +       if (!nsi_lock->force_lock)
9759 +               si_read_unlock(sb);
9760 +
9761 +out:
9762 +       nsi_lock->bindex = bindex;
9763 +       return err;
9764 +}
9765 +
9766 +struct find_name_by_ino {
9767 +       int called, found;
9768 +       ino_t ino;
9769 +       char *name;
9770 +       int namelen;
9771 +};
9772 +
9773 +static int
9774 +find_name_by_ino(void *arg, const char *name, int namelen, loff_t offset,
9775 +                u64 ino, unsigned int d_type)
9776 +{
9777 +       struct find_name_by_ino *a = arg;
9778 +
9779 +       a->called++;
9780 +       if (a->ino != ino)
9781 +               return 0;
9782 +
9783 +       memcpy(a->name, name, namelen);
9784 +       a->namelen = namelen;
9785 +       a->found = 1;
9786 +       return 1;
9787 +}
9788 +
9789 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
9790 +                                    struct au_nfsd_si_lock *nsi_lock)
9791 +{
9792 +       struct dentry *dentry, *parent;
9793 +       struct file *file;
9794 +       struct inode *dir;
9795 +       struct find_name_by_ino arg;
9796 +       int err;
9797 +
9798 +       parent = path->dentry;
9799 +       if (nsi_lock)
9800 +               si_read_unlock(parent->d_sb);
9801 +       file = vfsub_dentry_open(path, au_dir_roflags);
9802 +       dentry = (void *)file;
9803 +       if (IS_ERR(file))
9804 +               goto out;
9805 +
9806 +       dentry = ERR_PTR(-ENOMEM);
9807 +       arg.name = (void *)__get_free_page(GFP_NOFS);
9808 +       if (unlikely(!arg.name))
9809 +               goto out_file;
9810 +       arg.ino = ino;
9811 +       arg.found = 0;
9812 +       do {
9813 +               arg.called = 0;
9814 +               /* smp_mb(); */
9815 +               err = vfsub_readdir(file, find_name_by_ino, &arg);
9816 +       } while (!err && !arg.found && arg.called);
9817 +       dentry = ERR_PTR(err);
9818 +       if (unlikely(err))
9819 +               goto out_name;
9820 +       /* instead of ENOENT */
9821 +       dentry = ERR_PTR(-ESTALE);
9822 +       if (!arg.found)
9823 +               goto out_name;
9824 +
9825 +       /* do not call vfsub_lkup_one() */
9826 +       dir = parent->d_inode;
9827 +       mutex_lock(&dir->i_mutex);
9828 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
9829 +       mutex_unlock(&dir->i_mutex);
9830 +       AuTraceErrPtr(dentry);
9831 +       if (IS_ERR(dentry))
9832 +               goto out_name;
9833 +       AuDebugOn(au_test_anon(dentry));
9834 +       if (unlikely(!dentry->d_inode)) {
9835 +               dput(dentry);
9836 +               dentry = ERR_PTR(-ENOENT);
9837 +       }
9838 +
9839 +out_name:
9840 +       free_page((unsigned long)arg.name);
9841 +out_file:
9842 +       fput(file);
9843 +out:
9844 +       if (unlikely(nsi_lock
9845 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
9846 +               if (!IS_ERR(dentry)) {
9847 +                       dput(dentry);
9848 +                       dentry = ERR_PTR(-ESTALE);
9849 +               }
9850 +       AuTraceErrPtr(dentry);
9851 +       return dentry;
9852 +}
9853 +
9854 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
9855 +                                       ino_t dir_ino,
9856 +                                       struct au_nfsd_si_lock *nsi_lock)
9857 +{
9858 +       struct dentry *dentry;
9859 +       struct path path;
9860 +
9861 +       if (dir_ino != AUFS_ROOT_INO) {
9862 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
9863 +               dentry = path.dentry;
9864 +               if (!path.dentry || IS_ERR(path.dentry))
9865 +                       goto out;
9866 +               AuDebugOn(au_test_anon(path.dentry));
9867 +       } else
9868 +               path.dentry = dget(sb->s_root);
9869 +
9870 +       path.mnt = au_mnt_get(sb);
9871 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
9872 +       path_put(&path);
9873 +
9874 +out:
9875 +       AuTraceErrPtr(dentry);
9876 +       return dentry;
9877 +}
9878 +
9879 +/* ---------------------------------------------------------------------- */
9880 +
9881 +static int h_acceptable(void *expv, struct dentry *dentry)
9882 +{
9883 +       return 1;
9884 +}
9885 +
9886 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
9887 +                          char *buf, int len, struct super_block *sb)
9888 +{
9889 +       char *p;
9890 +       int n;
9891 +       struct path path;
9892 +
9893 +       p = d_path(h_rootpath, buf, len);
9894 +       if (IS_ERR(p))
9895 +               goto out;
9896 +       n = strlen(p);
9897 +
9898 +       path.mnt = h_rootpath->mnt;
9899 +       path.dentry = h_parent;
9900 +       p = d_path(&path, buf, len);
9901 +       if (IS_ERR(p))
9902 +               goto out;
9903 +       if (n != 1)
9904 +               p += n;
9905 +
9906 +       path.mnt = au_mnt_get(sb);
9907 +       path.dentry = sb->s_root;
9908 +       p = d_path(&path, buf, len - strlen(p));
9909 +       mntput(path.mnt);
9910 +       if (IS_ERR(p))
9911 +               goto out;
9912 +       if (n != 1)
9913 +               p[strlen(p)] = '/';
9914 +
9915 +out:
9916 +       AuTraceErrPtr(p);
9917 +       return p;
9918 +}
9919 +
9920 +static
9921 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
9922 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
9923 +{
9924 +       struct dentry *dentry, *h_parent, *root;
9925 +       struct super_block *h_sb;
9926 +       char *pathname, *p;
9927 +       struct vfsmount *h_mnt;
9928 +       struct au_branch *br;
9929 +       int err;
9930 +       struct path path;
9931 +
9932 +       br = au_sbr(sb, nsi_lock->bindex);
9933 +       h_mnt = au_br_mnt(br);
9934 +       h_sb = h_mnt->mnt_sb;
9935 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
9936 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
9937 +                                     fh_len - Fh_tail, fh[Fh_h_type],
9938 +                                     h_acceptable, /*context*/NULL);
9939 +       dentry = h_parent;
9940 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
9941 +               AuWarn1("%s decode_fh failed, %ld\n",
9942 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
9943 +               goto out;
9944 +       }
9945 +       dentry = NULL;
9946 +       if (unlikely(au_test_anon(h_parent))) {
9947 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
9948 +                       au_sbtype(h_sb));
9949 +               goto out_h_parent;
9950 +       }
9951 +
9952 +       dentry = ERR_PTR(-ENOMEM);
9953 +       pathname = (void *)__get_free_page(GFP_NOFS);
9954 +       if (unlikely(!pathname))
9955 +               goto out_h_parent;
9956 +
9957 +       root = sb->s_root;
9958 +       path.mnt = h_mnt;
9959 +       di_read_lock_parent(root, !AuLock_IR);
9960 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
9961 +       di_read_unlock(root, !AuLock_IR);
9962 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
9963 +       dentry = (void *)p;
9964 +       if (IS_ERR(p))
9965 +               goto out_pathname;
9966 +
9967 +       si_read_unlock(sb);
9968 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
9969 +       dentry = ERR_PTR(err);
9970 +       if (unlikely(err))
9971 +               goto out_relock;
9972 +
9973 +       dentry = ERR_PTR(-ENOENT);
9974 +       AuDebugOn(au_test_anon(path.dentry));
9975 +       if (unlikely(!path.dentry->d_inode))
9976 +               goto out_path;
9977 +
9978 +       if (ino != path.dentry->d_inode->i_ino)
9979 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
9980 +       else
9981 +               dentry = dget(path.dentry);
9982 +
9983 +out_path:
9984 +       path_put(&path);
9985 +out_relock:
9986 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
9987 +               if (!IS_ERR(dentry)) {
9988 +                       dput(dentry);
9989 +                       dentry = ERR_PTR(-ESTALE);
9990 +               }
9991 +out_pathname:
9992 +       free_page((unsigned long)pathname);
9993 +out_h_parent:
9994 +       dput(h_parent);
9995 +out:
9996 +       AuTraceErrPtr(dentry);
9997 +       return dentry;
9998 +}
9999 +
10000 +/* ---------------------------------------------------------------------- */
10001 +
10002 +static struct dentry *
10003 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
10004 +                 int fh_type)
10005 +{
10006 +       struct dentry *dentry;
10007 +       __u32 *fh = fid->raw;
10008 +       struct au_branch *br;
10009 +       ino_t ino, dir_ino;
10010 +       struct au_nfsd_si_lock nsi_lock = {
10011 +               .force_lock     = 0
10012 +       };
10013 +
10014 +       dentry = ERR_PTR(-ESTALE);
10015 +       /* it should never happen, but the file handle is unreliable */
10016 +       if (unlikely(fh_len < Fh_tail))
10017 +               goto out;
10018 +       nsi_lock.sigen = fh[Fh_sigen];
10019 +       nsi_lock.br_id = fh[Fh_br_id];
10020 +
10021 +       /* branch id may be wrapped around */
10022 +       br = NULL;
10023 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
10024 +               goto out;
10025 +       nsi_lock.force_lock = 1;
10026 +
10027 +       /* is this inode still cached? */
10028 +       ino = decode_ino(fh + Fh_ino);
10029 +       /* it should never happen */
10030 +       if (unlikely(ino == AUFS_ROOT_INO))
10031 +               goto out;
10032 +
10033 +       dir_ino = decode_ino(fh + Fh_dir_ino);
10034 +       dentry = decode_by_ino(sb, ino, dir_ino);
10035 +       if (IS_ERR(dentry))
10036 +               goto out_unlock;
10037 +       if (dentry)
10038 +               goto accept;
10039 +
10040 +       /* is the parent dir cached? */
10041 +       br = au_sbr(sb, nsi_lock.bindex);
10042 +       atomic_inc(&br->br_count);
10043 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
10044 +       if (IS_ERR(dentry))
10045 +               goto out_unlock;
10046 +       if (dentry)
10047 +               goto accept;
10048 +
10049 +       /* lookup path */
10050 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
10051 +       if (IS_ERR(dentry))
10052 +               goto out_unlock;
10053 +       if (unlikely(!dentry))
10054 +               /* todo?: make it ESTALE */
10055 +               goto out_unlock;
10056 +
10057 +accept:
10058 +       if (!au_digen_test(dentry, au_sigen(sb))
10059 +           && dentry->d_inode->i_generation == fh[Fh_igen])
10060 +               goto out_unlock; /* success */
10061 +
10062 +       dput(dentry);
10063 +       dentry = ERR_PTR(-ESTALE);
10064 +out_unlock:
10065 +       if (br)
10066 +               atomic_dec(&br->br_count);
10067 +       si_read_unlock(sb);
10068 +out:
10069 +       AuTraceErrPtr(dentry);
10070 +       return dentry;
10071 +}
10072 +
10073 +#if 0 /* reserved for future use */
10074 +/* support subtreecheck option */
10075 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
10076 +                                       int fh_len, int fh_type)
10077 +{
10078 +       struct dentry *parent;
10079 +       __u32 *fh = fid->raw;
10080 +       ino_t dir_ino;
10081 +
10082 +       dir_ino = decode_ino(fh + Fh_dir_ino);
10083 +       parent = decode_by_ino(sb, dir_ino, 0);
10084 +       if (IS_ERR(parent))
10085 +               goto out;
10086 +       if (!parent)
10087 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
10088 +                                       dir_ino, fh, fh_len);
10089 +
10090 +out:
10091 +       AuTraceErrPtr(parent);
10092 +       return parent;
10093 +}
10094 +#endif
10095 +
10096 +/* ---------------------------------------------------------------------- */
10097 +
10098 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
10099 +                         struct inode *dir)
10100 +{
10101 +       int err;
10102 +       aufs_bindex_t bindex;
10103 +       struct super_block *sb, *h_sb;
10104 +       struct dentry *dentry, *parent, *h_parent;
10105 +       struct inode *h_dir;
10106 +       struct au_branch *br;
10107 +
10108 +       err = -ENOSPC;
10109 +       if (unlikely(*max_len <= Fh_tail)) {
10110 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
10111 +               goto out;
10112 +       }
10113 +
10114 +       err = FILEID_ROOT;
10115 +       if (inode->i_ino == AUFS_ROOT_INO) {
10116 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
10117 +               goto out;
10118 +       }
10119 +
10120 +       h_parent = NULL;
10121 +       sb = inode->i_sb;
10122 +       err = si_read_lock(sb, AuLock_FLUSH);
10123 +       if (unlikely(err))
10124 +               goto out;
10125 +
10126 +#ifdef CONFIG_AUFS_DEBUG
10127 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
10128 +               AuWarn1("NFS-exporting requires xino\n");
10129 +#endif
10130 +       err = -EIO;
10131 +       parent = NULL;
10132 +       ii_read_lock_child(inode);
10133 +       bindex = au_ibstart(inode);
10134 +       if (!dir) {
10135 +               dentry = d_find_alias(inode);
10136 +               if (unlikely(!dentry))
10137 +                       goto out_unlock;
10138 +               AuDebugOn(au_test_anon(dentry));
10139 +               parent = dget_parent(dentry);
10140 +               dput(dentry);
10141 +               if (unlikely(!parent))
10142 +                       goto out_unlock;
10143 +               dir = parent->d_inode;
10144 +       }
10145 +
10146 +       ii_read_lock_parent(dir);
10147 +       h_dir = au_h_iptr(dir, bindex);
10148 +       ii_read_unlock(dir);
10149 +       if (unlikely(!h_dir))
10150 +               goto out_parent;
10151 +       h_parent = d_find_alias(h_dir);
10152 +       if (unlikely(!h_parent))
10153 +               goto out_hparent;
10154 +
10155 +       err = -EPERM;
10156 +       br = au_sbr(sb, bindex);
10157 +       h_sb = au_br_sb(br);
10158 +       if (unlikely(!h_sb->s_export_op)) {
10159 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
10160 +               goto out_hparent;
10161 +       }
10162 +
10163 +       fh[Fh_br_id] = br->br_id;
10164 +       fh[Fh_sigen] = au_sigen(sb);
10165 +       encode_ino(fh + Fh_ino, inode->i_ino);
10166 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
10167 +       fh[Fh_igen] = inode->i_generation;
10168 +
10169 +       *max_len -= Fh_tail;
10170 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
10171 +                                          max_len,
10172 +                                          /*connectable or subtreecheck*/0);
10173 +       err = fh[Fh_h_type];
10174 +       *max_len += Fh_tail;
10175 +       /* todo: macros? */
10176 +       if (err != FILEID_INVALID)
10177 +               err = 99;
10178 +       else
10179 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
10180 +
10181 +out_hparent:
10182 +       dput(h_parent);
10183 +out_parent:
10184 +       dput(parent);
10185 +out_unlock:
10186 +       ii_read_unlock(inode);
10187 +       si_read_unlock(sb);
10188 +out:
10189 +       if (unlikely(err < 0))
10190 +               err = FILEID_INVALID;
10191 +       return err;
10192 +}
10193 +
10194 +/* ---------------------------------------------------------------------- */
10195 +
10196 +static int aufs_commit_metadata(struct inode *inode)
10197 +{
10198 +       int err;
10199 +       aufs_bindex_t bindex;
10200 +       struct super_block *sb;
10201 +       struct inode *h_inode;
10202 +       int (*f)(struct inode *inode);
10203 +
10204 +       sb = inode->i_sb;
10205 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
10206 +       ii_write_lock_child(inode);
10207 +       bindex = au_ibstart(inode);
10208 +       AuDebugOn(bindex < 0);
10209 +       h_inode = au_h_iptr(inode, bindex);
10210 +
10211 +       f = h_inode->i_sb->s_export_op->commit_metadata;
10212 +       if (f)
10213 +               err = f(h_inode);
10214 +       else {
10215 +               struct writeback_control wbc = {
10216 +                       .sync_mode      = WB_SYNC_ALL,
10217 +                       .nr_to_write    = 0 /* metadata only */
10218 +               };
10219 +
10220 +               err = sync_inode(h_inode, &wbc);
10221 +       }
10222 +
10223 +       au_cpup_attr_timesizes(inode);
10224 +       ii_write_unlock(inode);
10225 +       si_read_unlock(sb);
10226 +       return err;
10227 +}
10228 +
10229 +/* ---------------------------------------------------------------------- */
10230 +
10231 +static struct export_operations aufs_export_op = {
10232 +       .fh_to_dentry           = aufs_fh_to_dentry,
10233 +       /* .fh_to_parent        = aufs_fh_to_parent, */
10234 +       .encode_fh              = aufs_encode_fh,
10235 +       .commit_metadata        = aufs_commit_metadata
10236 +};
10237 +
10238 +void au_export_init(struct super_block *sb)
10239 +{
10240 +       struct au_sbinfo *sbinfo;
10241 +       __u32 u;
10242 +
10243 +       sb->s_export_op = &aufs_export_op;
10244 +       sbinfo = au_sbi(sb);
10245 +       sbinfo->si_xigen = NULL;
10246 +       get_random_bytes(&u, sizeof(u));
10247 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
10248 +       atomic_set(&sbinfo->si_xigen_next, u);
10249 +}
10250 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
10251 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
10252 +++ linux/fs/aufs/file.c        2013-08-23 23:59:39.634916914 +0200
10253 @@ -0,0 +1,708 @@
10254 +/*
10255 + * Copyright (C) 2005-2013 Junjiro R. Okajima
10256 + *
10257 + * This program, aufs is free software; you can redistribute it and/or modify
10258 + * it under the terms of the GNU General Public License as published by
10259 + * the Free Software Foundation; either version 2 of the License, or
10260 + * (at your option) any later version.
10261 + *
10262 + * This program is distributed in the hope that it will be useful,
10263 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10264 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10265 + * GNU General Public License for more details.
10266 + *
10267 + * You should have received a copy of the GNU General Public License
10268 + * along with this program; if not, write to the Free Software
10269 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
10270 + */
10271 +
10272 +/*
10273 + * handling file/dir, and address_space operation
10274 + */
10275 +
10276 +#ifdef CONFIG_AUFS_DEBUG
10277 +#include <linux/migrate.h>
10278 +#endif
10279 +#include <linux/pagemap.h>
10280 +#include "aufs.h"
10281 +
10282 +/* drop flags for writing */
10283 +unsigned int au_file_roflags(unsigned int flags)
10284 +{
10285 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
10286 +       flags |= O_RDONLY | O_NOATIME;
10287 +       return flags;
10288 +}
10289 +
10290 +/* common functions to regular file and dir */
10291 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
10292 +                      struct file *file)
10293 +{
10294 +       struct file *h_file;
10295 +       struct dentry *h_dentry;
10296 +       struct inode *h_inode;
10297 +       struct super_block *sb;
10298 +       struct au_branch *br;
10299 +       struct path h_path;
10300 +       int err, exec_flag;
10301 +
10302 +       /* a race condition can happen between open and unlink/rmdir */
10303 +       h_file = ERR_PTR(-ENOENT);
10304 +       h_dentry = au_h_dptr(dentry, bindex);
10305 +       if (au_test_nfsd() && !h_dentry)
10306 +               goto out;
10307 +       h_inode = h_dentry->d_inode;
10308 +       if (au_test_nfsd() && !h_inode)
10309 +               goto out;
10310 +       spin_lock(&h_dentry->d_lock);
10311 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
10312 +               || !h_inode
10313 +               /* || !dentry->d_inode->i_nlink */
10314 +               ;
10315 +       spin_unlock(&h_dentry->d_lock);
10316 +       if (unlikely(err))
10317 +               goto out;
10318 +
10319 +       sb = dentry->d_sb;
10320 +       br = au_sbr(sb, bindex);
10321 +       h_file = ERR_PTR(-EACCES);
10322 +       exec_flag = flags & __FMODE_EXEC;
10323 +       if (exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC))
10324 +               goto out;
10325 +
10326 +       /* drop flags for writing */
10327 +       if (au_test_ro(sb, bindex, dentry->d_inode))
10328 +               flags = au_file_roflags(flags);
10329 +       flags &= ~O_CREAT;
10330 +       atomic_inc(&br->br_count);
10331 +       h_path.dentry = h_dentry;
10332 +       h_path.mnt = au_br_mnt(br);
10333 +       if (!au_special_file(h_inode->i_mode))
10334 +               h_file = vfsub_dentry_open(&h_path, flags);
10335 +       else {
10336 +               /* this block depends upon the configuration */
10337 +               di_read_unlock(dentry, AuLock_IR);
10338 +               fi_write_unlock(file);
10339 +               si_read_unlock(sb);
10340 +               h_file = vfsub_dentry_open(&h_path, flags);
10341 +               si_noflush_read_lock(sb);
10342 +               fi_write_lock(file);
10343 +               di_read_lock_child(dentry, AuLock_IR);
10344 +       }
10345 +       if (IS_ERR(h_file))
10346 +               goto out_br;
10347 +
10348 +       if (exec_flag) {
10349 +               err = deny_write_access(h_file);
10350 +               if (unlikely(err)) {
10351 +                       fput(h_file);
10352 +                       h_file = ERR_PTR(err);
10353 +                       goto out_br;
10354 +               }
10355 +       }
10356 +       fsnotify_open(h_file);
10357 +       goto out; /* success */
10358 +
10359 +out_br:
10360 +       atomic_dec(&br->br_count);
10361 +out:
10362 +       return h_file;
10363 +}
10364 +
10365 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
10366 +              struct au_fidir *fidir)
10367 +{
10368 +       int err;
10369 +       struct dentry *dentry;
10370 +
10371 +       err = au_finfo_init(file, fidir);
10372 +       if (unlikely(err))
10373 +               goto out;
10374 +
10375 +       dentry = file->f_dentry;
10376 +       di_read_lock_child(dentry, AuLock_IR);
10377 +       err = open(file, vfsub_file_flags(file));
10378 +       di_read_unlock(dentry, AuLock_IR);
10379 +
10380 +       fi_write_unlock(file);
10381 +       if (unlikely(err)) {
10382 +               au_fi(file)->fi_hdir = NULL;
10383 +               au_finfo_fin(file);
10384 +       }
10385 +
10386 +out:
10387 +       return err;
10388 +}
10389 +
10390 +int au_reopen_nondir(struct file *file)
10391 +{
10392 +       int err;
10393 +       aufs_bindex_t bstart;
10394 +       struct dentry *dentry;
10395 +       struct file *h_file, *h_file_tmp;
10396 +
10397 +       dentry = file->f_dentry;
10398 +       AuDebugOn(au_special_file(dentry->d_inode->i_mode));
10399 +       bstart = au_dbstart(dentry);
10400 +       h_file_tmp = NULL;
10401 +       if (au_fbstart(file) == bstart) {
10402 +               h_file = au_hf_top(file);
10403 +               if (file->f_mode == h_file->f_mode)
10404 +                       return 0; /* success */
10405 +               h_file_tmp = h_file;
10406 +               get_file(h_file_tmp);
10407 +               au_set_h_fptr(file, bstart, NULL);
10408 +       }
10409 +       AuDebugOn(au_fi(file)->fi_hdir);
10410 +       /*
10411 +        * it can happen
10412 +        * file exists on both of rw and ro
10413 +        * open --> dbstart and fbstart are both 0
10414 +        * prepend a branch as rw, "rw" become ro
10415 +        * remove rw/file
10416 +        * delete the top branch, "rw" becomes rw again
10417 +        *      --> dbstart is 1, fbstart is still 0
10418 +        * write --> fbstart is 0 but dbstart is 1
10419 +        */
10420 +       /* AuDebugOn(au_fbstart(file) < bstart); */
10421 +
10422 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
10423 +                          file);
10424 +       err = PTR_ERR(h_file);
10425 +       if (IS_ERR(h_file)) {
10426 +               if (h_file_tmp) {
10427 +                       atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
10428 +                       au_set_h_fptr(file, bstart, h_file_tmp);
10429 +                       h_file_tmp = NULL;
10430 +               }
10431 +               goto out; /* todo: close all? */
10432 +       }
10433 +
10434 +       err = 0;
10435 +       au_set_fbstart(file, bstart);
10436 +       au_set_h_fptr(file, bstart, h_file);
10437 +       au_update_figen(file);
10438 +       /* todo: necessary? */
10439 +       /* file->f_ra = h_file->f_ra; */
10440 +
10441 +out:
10442 +       if (h_file_tmp)
10443 +               fput(h_file_tmp);
10444 +       return err;
10445 +}
10446 +
10447 +/* ---------------------------------------------------------------------- */
10448 +
10449 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
10450 +                       struct dentry *hi_wh)
10451 +{
10452 +       int err;
10453 +       aufs_bindex_t bstart;
10454 +       struct au_dinfo *dinfo;
10455 +       struct dentry *h_dentry;
10456 +       struct au_hdentry *hdp;
10457 +
10458 +       dinfo = au_di(file->f_dentry);
10459 +       AuRwMustWriteLock(&dinfo->di_rwsem);
10460 +
10461 +       bstart = dinfo->di_bstart;
10462 +       dinfo->di_bstart = btgt;
10463 +       hdp = dinfo->di_hdentry;
10464 +       h_dentry = hdp[0 + btgt].hd_dentry;
10465 +       hdp[0 + btgt].hd_dentry = hi_wh;
10466 +       err = au_reopen_nondir(file);
10467 +       hdp[0 + btgt].hd_dentry = h_dentry;
10468 +       dinfo->di_bstart = bstart;
10469 +
10470 +       return err;
10471 +}
10472 +
10473 +static int au_ready_to_write_wh(struct file *file, loff_t len,
10474 +                               aufs_bindex_t bcpup, struct au_pin *pin)
10475 +{
10476 +       int err;
10477 +       struct inode *inode, *h_inode;
10478 +       struct dentry *h_dentry, *hi_wh;
10479 +       struct au_cp_generic cpg = {
10480 +               .dentry = file->f_dentry,
10481 +               .bdst   = bcpup,
10482 +               .bsrc   = -1,
10483 +               .len    = len,
10484 +               .pin    = pin
10485 +       };
10486 +
10487 +       au_update_dbstart(cpg.dentry);
10488 +       inode = cpg.dentry->d_inode;
10489 +       h_inode = NULL;
10490 +       if (au_dbstart(cpg.dentry) <= bcpup
10491 +           && au_dbend(cpg.dentry) >= bcpup) {
10492 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
10493 +               if (h_dentry)
10494 +                       h_inode = h_dentry->d_inode;
10495 +       }
10496 +       hi_wh = au_hi_wh(inode, bcpup);
10497 +       if (!hi_wh && !h_inode)
10498 +               err = au_sio_cpup_wh(&cpg, file);
10499 +       else
10500 +               /* already copied-up after unlink */
10501 +               err = au_reopen_wh(file, bcpup, hi_wh);
10502 +
10503 +       if (!err
10504 +           && inode->i_nlink > 1
10505 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
10506 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
10507 +
10508 +       return err;
10509 +}
10510 +
10511 +/*
10512 + * prepare the @file for writing.
10513 + */
10514 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
10515 +{
10516 +       int err;
10517 +       aufs_bindex_t dbstart;
10518 +       struct dentry *parent, *h_dentry;
10519 +       struct inode *inode;
10520 +       struct super_block *sb;
10521 +       struct file *h_file;
10522 +       struct au_cp_generic cpg = {
10523 +               .dentry = file->f_dentry,
10524 +               .bdst   = -1,
10525 +               .bsrc   = -1,
10526 +               .len    = len,
10527 +               .pin    = pin,
10528 +               .flags  = AuCpup_DTIME
10529 +       };
10530 +
10531 +       sb = cpg.dentry->d_sb;
10532 +       inode = cpg.dentry->d_inode;
10533 +       AuDebugOn(au_special_file(inode->i_mode));
10534 +       cpg.bsrc = au_fbstart(file);
10535 +       err = au_test_ro(sb, cpg.bsrc, inode);
10536 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
10537 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
10538 +                            /*flags*/0);
10539 +               goto out;
10540 +       }
10541 +
10542 +       /* need to cpup or reopen */
10543 +       parent = dget_parent(cpg.dentry);
10544 +       di_write_lock_parent(parent);
10545 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
10546 +       cpg.bdst = err;
10547 +       if (unlikely(err < 0))
10548 +               goto out_dgrade;
10549 +       err = 0;
10550 +
10551 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
10552 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
10553 +               if (unlikely(err))
10554 +                       goto out_dgrade;
10555 +       }
10556 +
10557 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
10558 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10559 +       if (unlikely(err))
10560 +               goto out_dgrade;
10561 +
10562 +       h_dentry = au_hf_top(file)->f_dentry;
10563 +       dbstart = au_dbstart(cpg.dentry);
10564 +       if (dbstart <= cpg.bdst) {
10565 +               h_dentry = au_h_dptr(cpg.dentry, cpg.bdst);
10566 +               AuDebugOn(!h_dentry);
10567 +               cpg.bsrc = cpg.bdst;
10568 +       }
10569 +
10570 +       if (dbstart <= cpg.bdst         /* just reopen */
10571 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
10572 +               ) {
10573 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc);
10574 +               if (IS_ERR(h_file))
10575 +                       err = PTR_ERR(h_file);
10576 +               else {
10577 +                       di_downgrade_lock(parent, AuLock_IR);
10578 +                       if (dbstart > cpg.bdst)
10579 +                               err = au_sio_cpup_simple(&cpg);
10580 +                       if (!err)
10581 +                               err = au_reopen_nondir(file);
10582 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
10583 +               }
10584 +       } else {                        /* copyup as wh and reopen */
10585 +               /*
10586 +                * since writable hfsplus branch is not supported,
10587 +                * h_open_pre/post() are unnecessary.
10588 +                */
10589 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
10590 +               di_downgrade_lock(parent, AuLock_IR);
10591 +       }
10592 +
10593 +       if (!err) {
10594 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
10595 +               goto out_dput; /* success */
10596 +       }
10597 +       au_unpin(pin);
10598 +       goto out_unlock;
10599 +
10600 +out_dgrade:
10601 +       di_downgrade_lock(parent, AuLock_IR);
10602 +out_unlock:
10603 +       di_read_unlock(parent, AuLock_IR);
10604 +out_dput:
10605 +       dput(parent);
10606 +out:
10607 +       return err;
10608 +}
10609 +
10610 +/* ---------------------------------------------------------------------- */
10611 +
10612 +int au_do_flush(struct file *file, fl_owner_t id,
10613 +               int (*flush)(struct file *file, fl_owner_t id))
10614 +{
10615 +       int err;
10616 +       struct super_block *sb;
10617 +       struct inode *inode;
10618 +
10619 +       inode = file_inode(file);
10620 +       sb = inode->i_sb;
10621 +       si_noflush_read_lock(sb);
10622 +       fi_read_lock(file);
10623 +       ii_read_lock_child(inode);
10624 +
10625 +       err = flush(file, id);
10626 +       au_cpup_attr_timesizes(inode);
10627 +
10628 +       ii_read_unlock(inode);
10629 +       fi_read_unlock(file);
10630 +       si_read_unlock(sb);
10631 +       return err;
10632 +}
10633 +
10634 +/* ---------------------------------------------------------------------- */
10635 +
10636 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
10637 +{
10638 +       int err;
10639 +       struct au_pin pin;
10640 +       struct au_finfo *finfo;
10641 +       struct dentry *parent, *hi_wh;
10642 +       struct inode *inode;
10643 +       struct super_block *sb;
10644 +       struct au_cp_generic cpg = {
10645 +               .dentry = file->f_dentry,
10646 +               .bdst   = -1,
10647 +               .bsrc   = -1,
10648 +               .len    = -1,
10649 +               .pin    = &pin,
10650 +               .flags  = AuCpup_DTIME
10651 +       };
10652 +
10653 +       FiMustWriteLock(file);
10654 +
10655 +       err = 0;
10656 +       finfo = au_fi(file);
10657 +       sb = cpg.dentry->d_sb;
10658 +       inode = cpg.dentry->d_inode;
10659 +       cpg.bdst = au_ibstart(inode);
10660 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
10661 +               goto out;
10662 +
10663 +       parent = dget_parent(cpg.dentry);
10664 +       if (au_test_ro(sb, cpg.bdst, inode)) {
10665 +               di_read_lock_parent(parent, !AuLock_IR);
10666 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
10667 +               cpg.bdst = err;
10668 +               di_read_unlock(parent, !AuLock_IR);
10669 +               if (unlikely(err < 0))
10670 +                       goto out_parent;
10671 +               err = 0;
10672 +       }
10673 +
10674 +       di_read_lock_parent(parent, AuLock_IR);
10675 +       hi_wh = au_hi_wh(inode, cpg.bdst);
10676 +       if (!S_ISDIR(inode->i_mode)
10677 +           && au_opt_test(au_mntflags(sb), PLINK)
10678 +           && au_plink_test(inode)
10679 +           && !d_unhashed(cpg.dentry)
10680 +           && cpg.bdst < au_dbstart(cpg.dentry)) {
10681 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
10682 +               if (unlikely(err))
10683 +                       goto out_unlock;
10684 +
10685 +               /* always superio. */
10686 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
10687 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10688 +               if (!err) {
10689 +                       err = au_sio_cpup_simple(&cpg);
10690 +                       au_unpin(&pin);
10691 +               }
10692 +       } else if (hi_wh) {
10693 +               /* already copied-up after unlink */
10694 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
10695 +               *need_reopen = 0;
10696 +       }
10697 +
10698 +out_unlock:
10699 +       di_read_unlock(parent, AuLock_IR);
10700 +out_parent:
10701 +       dput(parent);
10702 +out:
10703 +       return err;
10704 +}
10705 +
10706 +static void au_do_refresh_dir(struct file *file)
10707 +{
10708 +       aufs_bindex_t bindex, bend, new_bindex, brid;
10709 +       struct au_hfile *p, tmp, *q;
10710 +       struct au_finfo *finfo;
10711 +       struct super_block *sb;
10712 +       struct au_fidir *fidir;
10713 +
10714 +       FiMustWriteLock(file);
10715 +
10716 +       sb = file->f_dentry->d_sb;
10717 +       finfo = au_fi(file);
10718 +       fidir = finfo->fi_hdir;
10719 +       AuDebugOn(!fidir);
10720 +       p = fidir->fd_hfile + finfo->fi_btop;
10721 +       brid = p->hf_br->br_id;
10722 +       bend = fidir->fd_bbot;
10723 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
10724 +               if (!p->hf_file)
10725 +                       continue;
10726 +
10727 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
10728 +               if (new_bindex == bindex)
10729 +                       continue;
10730 +               if (new_bindex < 0) {
10731 +                       au_set_h_fptr(file, bindex, NULL);
10732 +                       continue;
10733 +               }
10734 +
10735 +               /* swap two lower inode, and loop again */
10736 +               q = fidir->fd_hfile + new_bindex;
10737 +               tmp = *q;
10738 +               *q = *p;
10739 +               *p = tmp;
10740 +               if (tmp.hf_file) {
10741 +                       bindex--;
10742 +                       p--;
10743 +               }
10744 +       }
10745 +
10746 +       p = fidir->fd_hfile;
10747 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_dentry)) {
10748 +               bend = au_sbend(sb);
10749 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
10750 +                    finfo->fi_btop++, p++)
10751 +                       if (p->hf_file) {
10752 +                               if (file_inode(p->hf_file))
10753 +                                       break;
10754 +                               else
10755 +                                       au_hfput(p, file);
10756 +                       }
10757 +       } else {
10758 +               bend = au_br_index(sb, brid);
10759 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
10760 +                    finfo->fi_btop++, p++)
10761 +                       if (p->hf_file)
10762 +                               au_hfput(p, file);
10763 +               bend = au_sbend(sb);
10764 +       }
10765 +
10766 +       p = fidir->fd_hfile + bend;
10767 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
10768 +            fidir->fd_bbot--, p--)
10769 +               if (p->hf_file) {
10770 +                       if (file_inode(p->hf_file))
10771 +                               break;
10772 +                       else
10773 +                               au_hfput(p, file);
10774 +               }
10775 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
10776 +}
10777 +
10778 +/*
10779 + * after branch manipulating, refresh the file.
10780 + */
10781 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
10782 +{
10783 +       int err, need_reopen;
10784 +       aufs_bindex_t bend, bindex;
10785 +       struct dentry *dentry;
10786 +       struct au_finfo *finfo;
10787 +       struct au_hfile *hfile;
10788 +
10789 +       dentry = file->f_dentry;
10790 +       finfo = au_fi(file);
10791 +       if (!finfo->fi_hdir) {
10792 +               hfile = &finfo->fi_htop;
10793 +               AuDebugOn(!hfile->hf_file);
10794 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
10795 +               AuDebugOn(bindex < 0);
10796 +               if (bindex != finfo->fi_btop)
10797 +                       au_set_fbstart(file, bindex);
10798 +       } else {
10799 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
10800 +               if (unlikely(err))
10801 +                       goto out;
10802 +               au_do_refresh_dir(file);
10803 +       }
10804 +
10805 +       err = 0;
10806 +       need_reopen = 1;
10807 +       if (!au_test_mmapped(file))
10808 +               err = au_file_refresh_by_inode(file, &need_reopen);
10809 +       if (!err && need_reopen && !d_unlinked(dentry))
10810 +               err = reopen(file);
10811 +       if (!err) {
10812 +               au_update_figen(file);
10813 +               goto out; /* success */
10814 +       }
10815 +
10816 +       /* error, close all lower files */
10817 +       if (finfo->fi_hdir) {
10818 +               bend = au_fbend_dir(file);
10819 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
10820 +                       au_set_h_fptr(file, bindex, NULL);
10821 +       }
10822 +
10823 +out:
10824 +       return err;
10825 +}
10826 +
10827 +/* common function to regular file and dir */
10828 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
10829 +                         int wlock)
10830 +{
10831 +       int err;
10832 +       unsigned int sigen, figen;
10833 +       aufs_bindex_t bstart;
10834 +       unsigned char pseudo_link;
10835 +       struct dentry *dentry;
10836 +       struct inode *inode;
10837 +
10838 +       err = 0;
10839 +       dentry = file->f_dentry;
10840 +       inode = dentry->d_inode;
10841 +       AuDebugOn(au_special_file(inode->i_mode));
10842 +       sigen = au_sigen(dentry->d_sb);
10843 +       fi_write_lock(file);
10844 +       figen = au_figen(file);
10845 +       di_write_lock_child(dentry);
10846 +       bstart = au_dbstart(dentry);
10847 +       pseudo_link = (bstart != au_ibstart(inode));
10848 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
10849 +               if (!wlock) {
10850 +                       di_downgrade_lock(dentry, AuLock_IR);
10851 +                       fi_downgrade_lock(file);
10852 +               }
10853 +               goto out; /* success */
10854 +       }
10855 +
10856 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
10857 +       if (au_digen_test(dentry, sigen)) {
10858 +               err = au_reval_dpath(dentry, sigen);
10859 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
10860 +       }
10861 +
10862 +       if (!err)
10863 +               err = refresh_file(file, reopen);
10864 +       if (!err) {
10865 +               if (!wlock) {
10866 +                       di_downgrade_lock(dentry, AuLock_IR);
10867 +                       fi_downgrade_lock(file);
10868 +               }
10869 +       } else {
10870 +               di_write_unlock(dentry);
10871 +               fi_write_unlock(file);
10872 +       }
10873 +
10874 +out:
10875 +       return err;
10876 +}
10877 +
10878 +/* ---------------------------------------------------------------------- */
10879 +
10880 +/* cf. aufs_nopage() */
10881 +/* for madvise(2) */
10882 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
10883 +{
10884 +       unlock_page(page);
10885 +       return 0;
10886 +}
10887 +
10888 +/* it will never be called, but necessary to support O_DIRECT */
10889 +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
10890 +                             const struct iovec *iov, loff_t offset,
10891 +                             unsigned long nr_segs)
10892 +{ BUG(); return 0; }
10893 +
10894 +/*
10895 + * it will never be called, but madvise and fadvise behaves differently
10896 + * when get_xip_mem is defined
10897 + */
10898 +static int aufs_get_xip_mem(struct address_space *mapping, pgoff_t pgoff,
10899 +                           int create, void **kmem, unsigned long *pfn)
10900 +{ BUG(); return 0; }
10901 +
10902 +/* they will never be called. */
10903 +#ifdef CONFIG_AUFS_DEBUG
10904 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
10905 +                           loff_t pos, unsigned len, unsigned flags,
10906 +                           struct page **pagep, void **fsdata)
10907 +{ AuUnsupport(); return 0; }
10908 +static int aufs_write_end(struct file *file, struct address_space *mapping,
10909 +                         loff_t pos, unsigned len, unsigned copied,
10910 +                         struct page *page, void *fsdata)
10911 +{ AuUnsupport(); return 0; }
10912 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
10913 +{ AuUnsupport(); return 0; }
10914 +
10915 +static int aufs_set_page_dirty(struct page *page)
10916 +{ AuUnsupport(); return 0; }
10917 +static void aufs_invalidatepage(struct page *page, unsigned long offset)
10918 +{ AuUnsupport(); }
10919 +static int aufs_releasepage(struct page *page, gfp_t gfp)
10920 +{ AuUnsupport(); return 0; }
10921 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
10922 +                           struct page *page, enum migrate_mode mode)
10923 +{ AuUnsupport(); return 0; }
10924 +static int aufs_launder_page(struct page *page)
10925 +{ AuUnsupport(); return 0; }
10926 +static int aufs_is_partially_uptodate(struct page *page,
10927 +                                     read_descriptor_t *desc,
10928 +                                     unsigned long from)
10929 +{ AuUnsupport(); return 0; }
10930 +static int aufs_error_remove_page(struct address_space *mapping,
10931 +                                 struct page *page)
10932 +{ AuUnsupport(); return 0; }
10933 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
10934 +                             sector_t *span)
10935 +{ AuUnsupport(); return 0; }
10936 +static void aufs_swap_deactivate(struct file *file)
10937 +{ AuUnsupport(); }
10938 +#endif /* CONFIG_AUFS_DEBUG */
10939 +
10940 +const struct address_space_operations aufs_aop = {
10941 +       .readpage               = aufs_readpage,
10942 +       .direct_IO              = aufs_direct_IO,
10943 +       .get_xip_mem            = aufs_get_xip_mem,
10944 +#ifdef CONFIG_AUFS_DEBUG
10945 +       .writepage              = aufs_writepage,
10946 +       /* no writepages, because of writepage */
10947 +       .set_page_dirty         = aufs_set_page_dirty,
10948 +       /* no readpages, because of readpage */
10949 +       .write_begin            = aufs_write_begin,
10950 +       .write_end              = aufs_write_end,
10951 +       /* no bmap, no block device */
10952 +       .invalidatepage         = aufs_invalidatepage,
10953 +       .releasepage            = aufs_releasepage,
10954 +       .migratepage            = aufs_migratepage,
10955 +       .launder_page           = aufs_launder_page,
10956 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
10957 +       .error_remove_page      = aufs_error_remove_page,
10958 +       .swap_activate          = aufs_swap_activate,
10959 +       .swap_deactivate        = aufs_swap_deactivate
10960 +#endif /* CONFIG_AUFS_DEBUG */
10961 +};
10962 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
10963 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
10964 +++ linux/fs/aufs/file.h        2013-08-23 23:59:39.634916914 +0200
10965 @@ -0,0 +1,313 @@
10966 +/*
10967 + * Copyright (C) 2005-2013 Junjiro R. Okajima
10968 + *
10969 + * This program, aufs is free software; you can redistribute it and/or modify
10970 + * it under the terms of the GNU General Public License as published by
10971 + * the Free Software Foundation; either version 2 of the License, or
10972 + * (at your option) any later version.
10973 + *
10974 + * This program is distributed in the hope that it will be useful,
10975 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10976 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10977 + * GNU General Public License for more details.
10978 + *
10979 + * You should have received a copy of the GNU General Public License
10980 + * along with this program; if not, write to the Free Software
10981 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
10982 + */
10983 +
10984 +/*
10985 + * file operations
10986 + */
10987 +
10988 +#ifndef __AUFS_FILE_H__
10989 +#define __AUFS_FILE_H__
10990 +
10991 +#ifdef __KERNEL__
10992 +
10993 +#include <linux/file.h>
10994 +#include <linux/fs.h>
10995 +#include <linux/poll.h>
10996 +#include "rwsem.h"
10997 +
10998 +struct au_branch;
10999 +struct au_hfile {
11000 +       struct file             *hf_file;
11001 +       struct au_branch        *hf_br;
11002 +};
11003 +
11004 +struct au_vdir;
11005 +struct au_fidir {
11006 +       aufs_bindex_t           fd_bbot;
11007 +       aufs_bindex_t           fd_nent;
11008 +       struct au_vdir          *fd_vdir_cache;
11009 +       struct au_hfile         fd_hfile[];
11010 +};
11011 +
11012 +static inline int au_fidir_sz(int nent)
11013 +{
11014 +       AuDebugOn(nent < 0);
11015 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
11016 +}
11017 +
11018 +struct au_finfo {
11019 +       atomic_t                fi_generation;
11020 +
11021 +       struct au_rwsem         fi_rwsem;
11022 +       aufs_bindex_t           fi_btop;
11023 +
11024 +       /* do not union them */
11025 +       struct {                                /* for non-dir */
11026 +               struct au_hfile                 fi_htop;
11027 +               atomic_t                        fi_mmapped;
11028 +       };
11029 +       struct au_fidir         *fi_hdir;       /* for dir only */
11030 +
11031 +       struct hlist_node       fi_hlist;
11032 +       struct file             *fi_file;       /* very ugly */
11033 +} ____cacheline_aligned_in_smp;
11034 +
11035 +/* ---------------------------------------------------------------------- */
11036 +
11037 +/* file.c */
11038 +extern const struct address_space_operations aufs_aop;
11039 +unsigned int au_file_roflags(unsigned int flags);
11040 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
11041 +                      struct file *file);
11042 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
11043 +              struct au_fidir *fidir);
11044 +int au_reopen_nondir(struct file *file);
11045 +struct au_pin;
11046 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
11047 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
11048 +                         int wlock);
11049 +int au_do_flush(struct file *file, fl_owner_t id,
11050 +               int (*flush)(struct file *file, fl_owner_t id));
11051 +
11052 +/* poll.c */
11053 +#ifdef CONFIG_AUFS_POLL
11054 +unsigned int aufs_poll(struct file *file, poll_table *wait);
11055 +#endif
11056 +
11057 +#ifdef CONFIG_AUFS_BR_HFSPLUS
11058 +/* hfsplus.c */
11059 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex);
11060 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
11061 +                   struct file *h_file);
11062 +#else
11063 +static inline
11064 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex)
11065 +{
11066 +       return NULL;
11067 +}
11068 +
11069 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
11070 +          struct file *h_file);
11071 +#endif
11072 +
11073 +/* f_op.c */
11074 +extern const struct file_operations aufs_file_fop;
11075 +int au_do_open_nondir(struct file *file, int flags);
11076 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
11077 +
11078 +#ifdef CONFIG_AUFS_SP_IATTR
11079 +/* f_op_sp.c */
11080 +struct au_finfo *au_fi_sp(struct file *file);
11081 +int au_special_file(umode_t mode);
11082 +void au_init_special_fop(struct inode *inode, umode_t mode, dev_t rdev);
11083 +#else
11084 +static inline struct au_finfo *au_fi_sp(struct file *file)
11085 +{
11086 +       return NULL;
11087 +}
11088 +AuStubInt0(au_special_file, umode_t mode)
11089 +static inline void au_init_special_fop(struct inode *inode, umode_t mode,
11090 +                                      dev_t rdev)
11091 +{
11092 +       init_special_inode(inode, mode, rdev);
11093 +}
11094 +#endif
11095 +
11096 +/* finfo.c */
11097 +void au_hfput(struct au_hfile *hf, struct file *file);
11098 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
11099 +                  struct file *h_file);
11100 +
11101 +void au_update_figen(struct file *file);
11102 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
11103 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
11104 +
11105 +void au_fi_init_once(void *_fi);
11106 +void au_finfo_fin(struct file *file);
11107 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
11108 +
11109 +/* ioctl.c */
11110 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
11111 +#ifdef CONFIG_COMPAT
11112 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
11113 +                          unsigned long arg);
11114 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
11115 +                             unsigned long arg);
11116 +#endif
11117 +
11118 +/* ---------------------------------------------------------------------- */
11119 +
11120 +static inline struct au_finfo *au_fi(struct file *file)
11121 +{
11122 +       struct au_finfo *finfo;
11123 +
11124 +       finfo = au_fi_sp(file);
11125 +       if (!finfo)
11126 +               finfo = file->private_data;
11127 +       return finfo;
11128 +}
11129 +
11130 +/* ---------------------------------------------------------------------- */
11131 +
11132 +/*
11133 + * fi_read_lock, fi_write_lock,
11134 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
11135 + */
11136 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
11137 +
11138 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
11139 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
11140 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
11141 +
11142 +/* ---------------------------------------------------------------------- */
11143 +
11144 +/* todo: hard/soft set? */
11145 +static inline aufs_bindex_t au_fbstart(struct file *file)
11146 +{
11147 +       FiMustAnyLock(file);
11148 +       return au_fi(file)->fi_btop;
11149 +}
11150 +
11151 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
11152 +{
11153 +       FiMustAnyLock(file);
11154 +       AuDebugOn(!au_fi(file)->fi_hdir);
11155 +       return au_fi(file)->fi_hdir->fd_bbot;
11156 +}
11157 +
11158 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
11159 +{
11160 +       FiMustAnyLock(file);
11161 +       AuDebugOn(!au_fi(file)->fi_hdir);
11162 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
11163 +}
11164 +
11165 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
11166 +{
11167 +       FiMustWriteLock(file);
11168 +       au_fi(file)->fi_btop = bindex;
11169 +}
11170 +
11171 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
11172 +{
11173 +       FiMustWriteLock(file);
11174 +       AuDebugOn(!au_fi(file)->fi_hdir);
11175 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
11176 +}
11177 +
11178 +static inline void au_set_fvdir_cache(struct file *file,
11179 +                                     struct au_vdir *vdir_cache)
11180 +{
11181 +       FiMustWriteLock(file);
11182 +       AuDebugOn(!au_fi(file)->fi_hdir);
11183 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
11184 +}
11185 +
11186 +static inline struct file *au_hf_top(struct file *file)
11187 +{
11188 +       FiMustAnyLock(file);
11189 +       AuDebugOn(au_fi(file)->fi_hdir);
11190 +       return au_fi(file)->fi_htop.hf_file;
11191 +}
11192 +
11193 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
11194 +{
11195 +       FiMustAnyLock(file);
11196 +       AuDebugOn(!au_fi(file)->fi_hdir);
11197 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
11198 +}
11199 +
11200 +/* todo: memory barrier? */
11201 +static inline unsigned int au_figen(struct file *f)
11202 +{
11203 +       return atomic_read(&au_fi(f)->fi_generation);
11204 +}
11205 +
11206 +static inline void au_set_mmapped(struct file *f)
11207 +{
11208 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
11209 +               return;
11210 +       pr_warn("fi_mmapped wrapped around\n");
11211 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
11212 +               ;
11213 +}
11214 +
11215 +static inline void au_unset_mmapped(struct file *f)
11216 +{
11217 +       atomic_dec(&au_fi(f)->fi_mmapped);
11218 +}
11219 +
11220 +static inline int au_test_mmapped(struct file *f)
11221 +{
11222 +       return atomic_read(&au_fi(f)->fi_mmapped);
11223 +}
11224 +
11225 +/* customize vma->vm_file */
11226 +
11227 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
11228 +                                      struct file *file)
11229 +{
11230 +       struct file *f;
11231 +
11232 +       f = vma->vm_file;
11233 +       get_file(file);
11234 +       vma->vm_file = file;
11235 +       fput(f);
11236 +}
11237 +
11238 +#ifdef CONFIG_MMU
11239 +#define AuDbgVmRegion(file, vma) do {} while (0)
11240 +
11241 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
11242 +                                   struct file *file)
11243 +{
11244 +       au_do_vm_file_reset(vma, file);
11245 +}
11246 +#else
11247 +#define AuDbgVmRegion(file, vma) \
11248 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
11249 +
11250 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
11251 +                                   struct file *file)
11252 +{
11253 +       struct file *f;
11254 +
11255 +       au_do_vm_file_reset(vma, file);
11256 +       f = vma->vm_region->vm_file;
11257 +       get_file(file);
11258 +       vma->vm_region->vm_file = file;
11259 +       fput(f);
11260 +}
11261 +#endif /* CONFIG_MMU */
11262 +
11263 +/* handle vma->vm_prfile */
11264 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
11265 +                                   struct file *file)
11266 +{
11267 +#ifdef CONFIG_AUFS_PROC_MAP
11268 +       get_file(file);
11269 +       vma->vm_prfile = file;
11270 +#ifndef CONFIG_MMU
11271 +       get_file(file);
11272 +       vma->vm_region->vm_prfile = file;
11273 +#endif
11274 +#endif
11275 +}
11276 +
11277 +#endif /* __KERNEL__ */
11278 +#endif /* __AUFS_FILE_H__ */
11279 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
11280 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
11281 +++ linux/fs/aufs/finfo.c       2013-07-06 13:20:47.750198454 +0200
11282 @@ -0,0 +1,157 @@
11283 +/*
11284 + * Copyright (C) 2005-2013 Junjiro R. Okajima
11285 + *
11286 + * This program, aufs is free software; you can redistribute it and/or modify
11287 + * it under the terms of the GNU General Public License as published by
11288 + * the Free Software Foundation; either version 2 of the License, or
11289 + * (at your option) any later version.
11290 + *
11291 + * This program is distributed in the hope that it will be useful,
11292 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11293 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11294 + * GNU General Public License for more details.
11295 + *
11296 + * You should have received a copy of the GNU General Public License
11297 + * along with this program; if not, write to the Free Software
11298 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11299 + */
11300 +
11301 +/*
11302 + * file private data
11303 + */
11304 +
11305 +#include "aufs.h"
11306 +
11307 +void au_hfput(struct au_hfile *hf, struct file *file)
11308 +{
11309 +       /* todo: direct access f_flags */
11310 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
11311 +               allow_write_access(hf->hf_file);
11312 +       fput(hf->hf_file);
11313 +       hf->hf_file = NULL;
11314 +       atomic_dec(&hf->hf_br->br_count);
11315 +       hf->hf_br = NULL;
11316 +}
11317 +
11318 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
11319 +{
11320 +       struct au_finfo *finfo = au_fi(file);
11321 +       struct au_hfile *hf;
11322 +       struct au_fidir *fidir;
11323 +
11324 +       fidir = finfo->fi_hdir;
11325 +       if (!fidir) {
11326 +               AuDebugOn(finfo->fi_btop != bindex);
11327 +               hf = &finfo->fi_htop;
11328 +       } else
11329 +               hf = fidir->fd_hfile + bindex;
11330 +
11331 +       if (hf && hf->hf_file)
11332 +               au_hfput(hf, file);
11333 +       if (val) {
11334 +               FiMustWriteLock(file);
11335 +               hf->hf_file = val;
11336 +               hf->hf_br = au_sbr(file->f_dentry->d_sb, bindex);
11337 +       }
11338 +}
11339 +
11340 +void au_update_figen(struct file *file)
11341 +{
11342 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_dentry));
11343 +       /* smp_mb(); */ /* atomic_set */
11344 +}
11345 +
11346 +/* ---------------------------------------------------------------------- */
11347 +
11348 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
11349 +{
11350 +       struct au_fidir *fidir;
11351 +       int nbr;
11352 +
11353 +       nbr = au_sbend(sb) + 1;
11354 +       if (nbr < 2)
11355 +               nbr = 2; /* initial allocate for 2 branches */
11356 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
11357 +       if (fidir) {
11358 +               fidir->fd_bbot = -1;
11359 +               fidir->fd_nent = nbr;
11360 +               fidir->fd_vdir_cache = NULL;
11361 +       }
11362 +
11363 +       return fidir;
11364 +}
11365 +
11366 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
11367 +{
11368 +       int err;
11369 +       struct au_fidir *fidir, *p;
11370 +
11371 +       AuRwMustWriteLock(&finfo->fi_rwsem);
11372 +       fidir = finfo->fi_hdir;
11373 +       AuDebugOn(!fidir);
11374 +
11375 +       err = -ENOMEM;
11376 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
11377 +                        GFP_NOFS);
11378 +       if (p) {
11379 +               p->fd_nent = nbr;
11380 +               finfo->fi_hdir = p;
11381 +               err = 0;
11382 +       }
11383 +
11384 +       return err;
11385 +}
11386 +
11387 +/* ---------------------------------------------------------------------- */
11388 +
11389 +void au_finfo_fin(struct file *file)
11390 +{
11391 +       struct au_finfo *finfo;
11392 +
11393 +       au_nfiles_dec(file->f_dentry->d_sb);
11394 +
11395 +       finfo = au_fi(file);
11396 +       AuDebugOn(finfo->fi_hdir);
11397 +       AuRwDestroy(&finfo->fi_rwsem);
11398 +       au_cache_free_finfo(finfo);
11399 +}
11400 +
11401 +void au_fi_init_once(void *_finfo)
11402 +{
11403 +       struct au_finfo *finfo = _finfo;
11404 +       static struct lock_class_key aufs_fi;
11405 +
11406 +       au_rw_init(&finfo->fi_rwsem);
11407 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
11408 +}
11409 +
11410 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
11411 +{
11412 +       int err;
11413 +       struct au_finfo *finfo;
11414 +       struct dentry *dentry;
11415 +
11416 +       err = -ENOMEM;
11417 +       dentry = file->f_dentry;
11418 +       finfo = au_cache_alloc_finfo();
11419 +       if (unlikely(!finfo))
11420 +               goto out;
11421 +
11422 +       err = 0;
11423 +       au_nfiles_inc(dentry->d_sb);
11424 +       /* verbose coding for lock class name */
11425 +       if (!fidir)
11426 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
11427 +       else
11428 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
11429 +       au_rw_write_lock(&finfo->fi_rwsem);
11430 +       finfo->fi_btop = -1;
11431 +       finfo->fi_hdir = fidir;
11432 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
11433 +       /* smp_mb(); */ /* atomic_set */
11434 +
11435 +       file->private_data = finfo;
11436 +
11437 +out:
11438 +       return err;
11439 +}
11440 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
11441 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
11442 +++ linux/fs/aufs/f_op.c        2013-08-23 23:59:39.634916914 +0200
11443 @@ -0,0 +1,721 @@
11444 +/*
11445 + * Copyright (C) 2005-2013 Junjiro R. Okajima
11446 + *
11447 + * This program, aufs is free software; you can redistribute it and/or modify
11448 + * it under the terms of the GNU General Public License as published by
11449 + * the Free Software Foundation; either version 2 of the License, or
11450 + * (at your option) any later version.
11451 + *
11452 + * This program is distributed in the hope that it will be useful,
11453 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11454 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11455 + * GNU General Public License for more details.
11456 + *
11457 + * You should have received a copy of the GNU General Public License
11458 + * along with this program; if not, write to the Free Software
11459 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11460 + */
11461 +
11462 +/*
11463 + * file and vm operations
11464 + */
11465 +
11466 +#include <linux/aio.h>
11467 +#include <linux/fs_stack.h>
11468 +#include <linux/mman.h>
11469 +#include <linux/security.h>
11470 +#include "aufs.h"
11471 +
11472 +int au_do_open_nondir(struct file *file, int flags)
11473 +{
11474 +       int err;
11475 +       aufs_bindex_t bindex;
11476 +       struct file *h_file;
11477 +       struct dentry *dentry;
11478 +       struct au_finfo *finfo;
11479 +
11480 +       FiMustWriteLock(file);
11481 +
11482 +       dentry = file->f_dentry;
11483 +       err = au_d_alive(dentry);
11484 +       if (unlikely(err))
11485 +               goto out;
11486 +
11487 +       finfo = au_fi(file);
11488 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
11489 +       atomic_set(&finfo->fi_mmapped, 0);
11490 +       bindex = au_dbstart(dentry);
11491 +       h_file = au_h_open(dentry, bindex, flags, file);
11492 +       if (IS_ERR(h_file))
11493 +               err = PTR_ERR(h_file);
11494 +       else {
11495 +               au_set_fbstart(file, bindex);
11496 +               au_set_h_fptr(file, bindex, h_file);
11497 +               au_update_figen(file);
11498 +               /* todo: necessary? */
11499 +               /* file->f_ra = h_file->f_ra; */
11500 +       }
11501 +
11502 +out:
11503 +       return err;
11504 +}
11505 +
11506 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
11507 +                           struct file *file)
11508 +{
11509 +       int err;
11510 +       struct super_block *sb;
11511 +
11512 +       AuDbg("%.*s, f_flags 0x%x, f_mode 0x%x\n",
11513 +             AuDLNPair(file->f_dentry), vfsub_file_flags(file),
11514 +             file->f_mode);
11515 +
11516 +       sb = file->f_dentry->d_sb;
11517 +       si_read_lock(sb, AuLock_FLUSH);
11518 +       err = au_do_open(file, au_do_open_nondir, /*fidir*/NULL);
11519 +       si_read_unlock(sb);
11520 +       return err;
11521 +}
11522 +
11523 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
11524 +{
11525 +       struct au_finfo *finfo;
11526 +       aufs_bindex_t bindex;
11527 +
11528 +       finfo = au_fi(file);
11529 +       bindex = finfo->fi_btop;
11530 +       if (bindex >= 0)
11531 +               au_set_h_fptr(file, bindex, NULL);
11532 +
11533 +       au_finfo_fin(file);
11534 +       return 0;
11535 +}
11536 +
11537 +/* ---------------------------------------------------------------------- */
11538 +
11539 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
11540 +{
11541 +       int err;
11542 +       struct file *h_file;
11543 +
11544 +       err = 0;
11545 +       h_file = au_hf_top(file);
11546 +       if (h_file)
11547 +               err = vfsub_flush(h_file, id);
11548 +       return err;
11549 +}
11550 +
11551 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
11552 +{
11553 +       return au_do_flush(file, id, au_do_flush_nondir);
11554 +}
11555 +
11556 +/* ---------------------------------------------------------------------- */
11557 +/*
11558 + * read and write functions acquire [fdi]_rwsem once, but release before
11559 + * mmap_sem. This is because to stop a race condition between mmap(2).
11560 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
11561 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
11562 + * read functions after [fdi]_rwsem are released, but it should be harmless.
11563 + */
11564 +
11565 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
11566 +                        loff_t *ppos)
11567 +{
11568 +       ssize_t err;
11569 +       struct dentry *dentry;
11570 +       struct file *h_file;
11571 +       struct super_block *sb;
11572 +
11573 +       dentry = file->f_dentry;
11574 +       sb = dentry->d_sb;
11575 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11576 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11577 +       if (unlikely(err))
11578 +               goto out;
11579 +
11580 +       h_file = au_hf_top(file);
11581 +       get_file(h_file);
11582 +       di_read_unlock(dentry, AuLock_IR);
11583 +       fi_read_unlock(file);
11584 +
11585 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
11586 +       err = vfsub_read_u(h_file, buf, count, ppos);
11587 +       /* todo: necessary? */
11588 +       /* file->f_ra = h_file->f_ra; */
11589 +       /* update without lock, I don't think it a problem */
11590 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
11591 +       fput(h_file);
11592 +
11593 +out:
11594 +       si_read_unlock(sb);
11595 +       return err;
11596 +}
11597 +
11598 +/*
11599 + * todo: very ugly
11600 + * it locks both of i_mutex and si_rwsem for read in safe.
11601 + * if the plink maintenance mode continues forever (that is the problem),
11602 + * may loop forever.
11603 + */
11604 +static void au_mtx_and_read_lock(struct inode *inode)
11605 +{
11606 +       int err;
11607 +       struct super_block *sb = inode->i_sb;
11608 +
11609 +       while (1) {
11610 +               mutex_lock(&inode->i_mutex);
11611 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
11612 +               if (!err)
11613 +                       break;
11614 +               mutex_unlock(&inode->i_mutex);
11615 +               si_read_lock(sb, AuLock_NOPLMW);
11616 +               si_read_unlock(sb);
11617 +       }
11618 +}
11619 +
11620 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
11621 +                         size_t count, loff_t *ppos)
11622 +{
11623 +       ssize_t err;
11624 +       struct au_pin pin;
11625 +       struct dentry *dentry;
11626 +       struct super_block *sb;
11627 +       struct inode *inode;
11628 +       struct file *h_file;
11629 +       char __user *buf = (char __user *)ubuf;
11630 +
11631 +       dentry = file->f_dentry;
11632 +       sb = dentry->d_sb;
11633 +       inode = dentry->d_inode;
11634 +       au_mtx_and_read_lock(inode);
11635 +
11636 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11637 +       if (unlikely(err))
11638 +               goto out;
11639 +
11640 +       err = au_ready_to_write(file, -1, &pin);
11641 +       di_downgrade_lock(dentry, AuLock_IR);
11642 +       if (unlikely(err)) {
11643 +               di_read_unlock(dentry, AuLock_IR);
11644 +               fi_write_unlock(file);
11645 +               goto out;
11646 +       }
11647 +
11648 +       h_file = au_hf_top(file);
11649 +       get_file(h_file);
11650 +       au_unpin(&pin);
11651 +       di_read_unlock(dentry, AuLock_IR);
11652 +       fi_write_unlock(file);
11653 +
11654 +       err = vfsub_write_u(h_file, buf, count, ppos);
11655 +       ii_write_lock_child(inode);
11656 +       au_cpup_attr_timesizes(inode);
11657 +       inode->i_mode = file_inode(h_file)->i_mode;
11658 +       ii_write_unlock(inode);
11659 +       fput(h_file);
11660 +
11661 +out:
11662 +       si_read_unlock(sb);
11663 +       mutex_unlock(&inode->i_mutex);
11664 +       return err;
11665 +}
11666 +
11667 +static ssize_t au_do_aio(struct file *h_file, int rw, struct kiocb *kio,
11668 +                        const struct iovec *iov, unsigned long nv, loff_t pos)
11669 +{
11670 +       ssize_t err;
11671 +       struct file *file;
11672 +       ssize_t (*func)(struct kiocb *, const struct iovec *, unsigned long,
11673 +                       loff_t);
11674 +
11675 +       err = security_file_permission(h_file, rw);
11676 +       if (unlikely(err))
11677 +               goto out;
11678 +
11679 +       err = -ENOSYS;
11680 +       func = NULL;
11681 +       if (rw == MAY_READ)
11682 +               func = h_file->f_op->aio_read;
11683 +       else if (rw == MAY_WRITE)
11684 +               func = h_file->f_op->aio_write;
11685 +       if (func) {
11686 +               file = kio->ki_filp;
11687 +               kio->ki_filp = h_file;
11688 +               lockdep_off();
11689 +               err = func(kio, iov, nv, pos);
11690 +               lockdep_on();
11691 +               kio->ki_filp = file;
11692 +       } else
11693 +               /* currently there is no such fs */
11694 +               WARN_ON_ONCE(1);
11695 +
11696 +out:
11697 +       return err;
11698 +}
11699 +
11700 +static ssize_t aufs_aio_read(struct kiocb *kio, const struct iovec *iov,
11701 +                            unsigned long nv, loff_t pos)
11702 +{
11703 +       ssize_t err;
11704 +       struct file *file, *h_file;
11705 +       struct dentry *dentry;
11706 +       struct super_block *sb;
11707 +
11708 +       file = kio->ki_filp;
11709 +       dentry = file->f_dentry;
11710 +       sb = dentry->d_sb;
11711 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11712 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11713 +       if (unlikely(err))
11714 +               goto out;
11715 +
11716 +       h_file = au_hf_top(file);
11717 +       get_file(h_file);
11718 +       di_read_unlock(dentry, AuLock_IR);
11719 +       fi_read_unlock(file);
11720 +
11721 +       err = au_do_aio(h_file, MAY_READ, kio, iov, nv, pos);
11722 +       /* todo: necessary? */
11723 +       /* file->f_ra = h_file->f_ra; */
11724 +       /* update without lock, I don't think it a problem */
11725 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
11726 +       fput(h_file);
11727 +
11728 +out:
11729 +       si_read_unlock(sb);
11730 +       return err;
11731 +}
11732 +
11733 +static ssize_t aufs_aio_write(struct kiocb *kio, const struct iovec *iov,
11734 +                             unsigned long nv, loff_t pos)
11735 +{
11736 +       ssize_t err;
11737 +       struct au_pin pin;
11738 +       struct dentry *dentry;
11739 +       struct inode *inode;
11740 +       struct file *file, *h_file;
11741 +       struct super_block *sb;
11742 +
11743 +       file = kio->ki_filp;
11744 +       dentry = file->f_dentry;
11745 +       sb = dentry->d_sb;
11746 +       inode = dentry->d_inode;
11747 +       au_mtx_and_read_lock(inode);
11748 +
11749 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11750 +       if (unlikely(err))
11751 +               goto out;
11752 +
11753 +       err = au_ready_to_write(file, -1, &pin);
11754 +       di_downgrade_lock(dentry, AuLock_IR);
11755 +       if (unlikely(err)) {
11756 +               di_read_unlock(dentry, AuLock_IR);
11757 +               fi_write_unlock(file);
11758 +               goto out;
11759 +       }
11760 +
11761 +       h_file = au_hf_top(file);
11762 +       get_file(h_file);
11763 +       au_unpin(&pin);
11764 +       di_read_unlock(dentry, AuLock_IR);
11765 +       fi_write_unlock(file);
11766 +
11767 +       err = au_do_aio(h_file, MAY_WRITE, kio, iov, nv, pos);
11768 +       ii_write_lock_child(inode);
11769 +       au_cpup_attr_timesizes(inode);
11770 +       inode->i_mode = file_inode(h_file)->i_mode;
11771 +       ii_write_unlock(inode);
11772 +       fput(h_file);
11773 +
11774 +out:
11775 +       si_read_unlock(sb);
11776 +       mutex_unlock(&inode->i_mutex);
11777 +       return err;
11778 +}
11779 +
11780 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
11781 +                               struct pipe_inode_info *pipe, size_t len,
11782 +                               unsigned int flags)
11783 +{
11784 +       ssize_t err;
11785 +       struct file *h_file;
11786 +       struct dentry *dentry;
11787 +       struct super_block *sb;
11788 +
11789 +       dentry = file->f_dentry;
11790 +       sb = dentry->d_sb;
11791 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11792 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11793 +       if (unlikely(err))
11794 +               goto out;
11795 +
11796 +       err = -EINVAL;
11797 +       h_file = au_hf_top(file);
11798 +       get_file(h_file);
11799 +       if (au_test_loopback_kthread()) {
11800 +               au_warn_loopback(h_file->f_dentry->d_sb);
11801 +               if (file->f_mapping != h_file->f_mapping) {
11802 +                       file->f_mapping = h_file->f_mapping;
11803 +                       smp_mb(); /* unnecessary? */
11804 +               }
11805 +       }
11806 +       di_read_unlock(dentry, AuLock_IR);
11807 +       fi_read_unlock(file);
11808 +
11809 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
11810 +       /* todo: necessasry? */
11811 +       /* file->f_ra = h_file->f_ra; */
11812 +       /* update without lock, I don't think it a problem */
11813 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
11814 +       fput(h_file);
11815 +
11816 +out:
11817 +       si_read_unlock(sb);
11818 +       return err;
11819 +}
11820 +
11821 +static ssize_t
11822 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
11823 +                 size_t len, unsigned int flags)
11824 +{
11825 +       ssize_t err;
11826 +       struct au_pin pin;
11827 +       struct dentry *dentry;
11828 +       struct inode *inode;
11829 +       struct file *h_file;
11830 +       struct super_block *sb;
11831 +
11832 +       dentry = file->f_dentry;
11833 +       sb = dentry->d_sb;
11834 +       inode = dentry->d_inode;
11835 +       au_mtx_and_read_lock(inode);
11836 +
11837 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11838 +       if (unlikely(err))
11839 +               goto out;
11840 +
11841 +       err = au_ready_to_write(file, -1, &pin);
11842 +       di_downgrade_lock(dentry, AuLock_IR);
11843 +       if (unlikely(err)) {
11844 +               di_read_unlock(dentry, AuLock_IR);
11845 +               fi_write_unlock(file);
11846 +               goto out;
11847 +       }
11848 +
11849 +       h_file = au_hf_top(file);
11850 +       get_file(h_file);
11851 +       au_unpin(&pin);
11852 +       di_read_unlock(dentry, AuLock_IR);
11853 +       fi_write_unlock(file);
11854 +
11855 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
11856 +       ii_write_lock_child(inode);
11857 +       au_cpup_attr_timesizes(inode);
11858 +       inode->i_mode = file_inode(h_file)->i_mode;
11859 +       ii_write_unlock(inode);
11860 +       fput(h_file);
11861 +
11862 +out:
11863 +       si_read_unlock(sb);
11864 +       mutex_unlock(&inode->i_mutex);
11865 +       return err;
11866 +}
11867 +
11868 +/* ---------------------------------------------------------------------- */
11869 +
11870 +/*
11871 + * The locking order around current->mmap_sem.
11872 + * - in most and regular cases
11873 + *   file I/O syscall -- aufs_read() or something
11874 + *     -- si_rwsem for read -- mmap_sem
11875 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
11876 + * - in mmap case
11877 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
11878 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
11879 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
11880 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
11881 + * It means that when aufs acquires si_rwsem for write, the process should never
11882 + * acquire mmap_sem.
11883 + *
11884 + * Actually aufs_readdir() holds [fdi]i_rwsem before mmap_sem, but this is not a
11885 + * problem either since any directory is not able to be mmap-ed.
11886 + * The similar scenario is applied to aufs_readlink() too.
11887 + */
11888 +
11889 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
11890 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
11891 +
11892 +static unsigned long au_arch_prot_conv(unsigned long flags)
11893 +{
11894 +       /* currently ppc64 only */
11895 +#ifdef CONFIG_PPC64
11896 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
11897 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
11898 +       return AuConv_VM_PROT(flags, SAO);
11899 +#else
11900 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
11901 +       return 0;
11902 +#endif
11903 +}
11904 +
11905 +static unsigned long au_prot_conv(unsigned long flags)
11906 +{
11907 +       return AuConv_VM_PROT(flags, READ)
11908 +               | AuConv_VM_PROT(flags, WRITE)
11909 +               | AuConv_VM_PROT(flags, EXEC)
11910 +               | au_arch_prot_conv(flags);
11911 +}
11912 +
11913 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
11914 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
11915 +
11916 +static unsigned long au_flag_conv(unsigned long flags)
11917 +{
11918 +       return AuConv_VM_MAP(flags, GROWSDOWN)
11919 +               | AuConv_VM_MAP(flags, DENYWRITE)
11920 +               | AuConv_VM_MAP(flags, LOCKED);
11921 +}
11922 +
11923 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
11924 +{
11925 +       int err;
11926 +       aufs_bindex_t bstart;
11927 +       const unsigned char wlock
11928 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
11929 +       struct dentry *dentry;
11930 +       struct super_block *sb;
11931 +       struct file *h_file;
11932 +       struct au_branch *br;
11933 +       struct au_pin pin;
11934 +
11935 +       AuDbgVmRegion(file, vma);
11936 +
11937 +       dentry = file->f_dentry;
11938 +       sb = dentry->d_sb;
11939 +       lockdep_off();
11940 +       si_read_lock(sb, AuLock_NOPLMW);
11941 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11942 +       if (unlikely(err))
11943 +               goto out;
11944 +
11945 +       if (wlock) {
11946 +               err = au_ready_to_write(file, -1, &pin);
11947 +               di_write_unlock(dentry);
11948 +               if (unlikely(err)) {
11949 +                       fi_write_unlock(file);
11950 +                       goto out;
11951 +               }
11952 +               au_unpin(&pin);
11953 +       } else
11954 +               di_write_unlock(dentry);
11955 +
11956 +       bstart = au_fbstart(file);
11957 +       br = au_sbr(sb, bstart);
11958 +       h_file = au_hf_top(file);
11959 +       get_file(h_file);
11960 +       au_set_mmapped(file);
11961 +       fi_write_unlock(file);
11962 +       lockdep_on();
11963 +
11964 +       au_vm_file_reset(vma, h_file);
11965 +       err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
11966 +                                au_flag_conv(vma->vm_flags));
11967 +       if (!err)
11968 +               err = h_file->f_op->mmap(h_file, vma);
11969 +       if (unlikely(err))
11970 +               goto out_reset;
11971 +
11972 +       au_vm_prfile_set(vma, file);
11973 +       /* update without lock, I don't think it a problem */
11974 +       fsstack_copy_attr_atime(file_inode(file), file_inode(h_file));
11975 +       goto out_fput; /* success */
11976 +
11977 +out_reset:
11978 +       au_unset_mmapped(file);
11979 +       au_vm_file_reset(vma, file);
11980 +out_fput:
11981 +       fput(h_file);
11982 +       lockdep_off();
11983 +out:
11984 +       si_read_unlock(sb);
11985 +       lockdep_on();
11986 +       AuTraceErr(err);
11987 +       return err;
11988 +}
11989 +
11990 +/* ---------------------------------------------------------------------- */
11991 +
11992 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
11993 +                            int datasync)
11994 +{
11995 +       int err;
11996 +       struct au_pin pin;
11997 +       struct dentry *dentry;
11998 +       struct inode *inode;
11999 +       struct file *h_file;
12000 +       struct super_block *sb;
12001 +
12002 +       dentry = file->f_dentry;
12003 +       inode = dentry->d_inode;
12004 +       sb = dentry->d_sb;
12005 +       mutex_lock(&inode->i_mutex);
12006 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
12007 +       if (unlikely(err))
12008 +               goto out;
12009 +
12010 +       err = 0; /* -EBADF; */ /* posix? */
12011 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
12012 +               goto out_si;
12013 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12014 +       if (unlikely(err))
12015 +               goto out_si;
12016 +
12017 +       err = au_ready_to_write(file, -1, &pin);
12018 +       di_downgrade_lock(dentry, AuLock_IR);
12019 +       if (unlikely(err))
12020 +               goto out_unlock;
12021 +       au_unpin(&pin);
12022 +
12023 +       err = -EINVAL;
12024 +       h_file = au_hf_top(file);
12025 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
12026 +       au_cpup_attr_timesizes(inode);
12027 +
12028 +out_unlock:
12029 +       di_read_unlock(dentry, AuLock_IR);
12030 +       fi_write_unlock(file);
12031 +out_si:
12032 +       si_read_unlock(sb);
12033 +out:
12034 +       mutex_unlock(&inode->i_mutex);
12035 +       return err;
12036 +}
12037 +
12038 +/* no one supports this operation, currently */
12039 +#if 0
12040 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
12041 +{
12042 +       int err;
12043 +       struct au_pin pin;
12044 +       struct dentry *dentry;
12045 +       struct inode *inode;
12046 +       struct file *file, *h_file;
12047 +
12048 +       file = kio->ki_filp;
12049 +       dentry = file->f_dentry;
12050 +       inode = dentry->d_inode;
12051 +       au_mtx_and_read_lock(inode);
12052 +
12053 +       err = 0; /* -EBADF; */ /* posix? */
12054 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
12055 +               goto out;
12056 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12057 +       if (unlikely(err))
12058 +               goto out;
12059 +
12060 +       err = au_ready_to_write(file, -1, &pin);
12061 +       di_downgrade_lock(dentry, AuLock_IR);
12062 +       if (unlikely(err))
12063 +               goto out_unlock;
12064 +       au_unpin(&pin);
12065 +
12066 +       err = -ENOSYS;
12067 +       h_file = au_hf_top(file);
12068 +       if (h_file->f_op && h_file->f_op->aio_fsync) {
12069 +               struct mutex *h_mtx;
12070 +
12071 +               h_mtx = &file_inode(h_file)->i_mutex;
12072 +               if (!is_sync_kiocb(kio)) {
12073 +                       get_file(h_file);
12074 +                       fput(file);
12075 +               }
12076 +               kio->ki_filp = h_file;
12077 +               err = h_file->f_op->aio_fsync(kio, datasync);
12078 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
12079 +               if (!err)
12080 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
12081 +               /*ignore*/
12082 +               au_cpup_attr_timesizes(inode);
12083 +               mutex_unlock(h_mtx);
12084 +       }
12085 +
12086 +out_unlock:
12087 +       di_read_unlock(dentry, AuLock_IR);
12088 +       fi_write_unlock(file);
12089 +out:
12090 +       si_read_unlock(inode->sb);
12091 +       mutex_unlock(&inode->i_mutex);
12092 +       return err;
12093 +}
12094 +#endif
12095 +
12096 +static int aufs_fasync(int fd, struct file *file, int flag)
12097 +{
12098 +       int err;
12099 +       struct file *h_file;
12100 +       struct dentry *dentry;
12101 +       struct super_block *sb;
12102 +
12103 +       dentry = file->f_dentry;
12104 +       sb = dentry->d_sb;
12105 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
12106 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
12107 +       if (unlikely(err))
12108 +               goto out;
12109 +
12110 +       h_file = au_hf_top(file);
12111 +       if (h_file->f_op && h_file->f_op->fasync)
12112 +               err = h_file->f_op->fasync(fd, h_file, flag);
12113 +
12114 +       di_read_unlock(dentry, AuLock_IR);
12115 +       fi_read_unlock(file);
12116 +
12117 +out:
12118 +       si_read_unlock(sb);
12119 +       return err;
12120 +}
12121 +
12122 +/* ---------------------------------------------------------------------- */
12123 +
12124 +/* no one supports this operation, currently */
12125 +#if 0
12126 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
12127 +                            size_t len, loff_t *pos , int more)
12128 +{
12129 +}
12130 +#endif
12131 +
12132 +/* ---------------------------------------------------------------------- */
12133 +
12134 +const struct file_operations aufs_file_fop = {
12135 +       .owner          = THIS_MODULE,
12136 +
12137 +       .llseek         = default_llseek,
12138 +
12139 +       .read           = aufs_read,
12140 +       .write          = aufs_write,
12141 +       .aio_read       = aufs_aio_read,
12142 +       .aio_write      = aufs_aio_write,
12143 +#ifdef CONFIG_AUFS_POLL
12144 +       .poll           = aufs_poll,
12145 +#endif
12146 +       .unlocked_ioctl = aufs_ioctl_nondir,
12147 +#ifdef CONFIG_COMPAT
12148 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
12149 +#endif
12150 +       .mmap           = aufs_mmap,
12151 +       .open           = aufs_open_nondir,
12152 +       .flush          = aufs_flush_nondir,
12153 +       .release        = aufs_release_nondir,
12154 +       .fsync          = aufs_fsync_nondir,
12155 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
12156 +       .fasync         = aufs_fasync,
12157 +       /* .sendpage    = aufs_sendpage, */
12158 +       .splice_write   = aufs_splice_write,
12159 +       .splice_read    = aufs_splice_read,
12160 +#if 0
12161 +       .aio_splice_write = aufs_aio_splice_write,
12162 +       .aio_splice_read  = aufs_aio_splice_read
12163 +#endif
12164 +};
12165 diff -urN /usr/share/empty/fs/aufs/f_op_sp.c linux/fs/aufs/f_op_sp.c
12166 --- /usr/share/empty/fs/aufs/f_op_sp.c  1970-01-01 01:00:00.000000000 +0100
12167 +++ linux/fs/aufs/f_op_sp.c     2013-08-23 23:59:39.634916914 +0200
12168 @@ -0,0 +1,383 @@
12169 +/*
12170 + * Copyright (C) 2005-2013 Junjiro R. Okajima
12171 + *
12172 + * This program, aufs is free software; you can redistribute it and/or modify
12173 + * it under the terms of the GNU General Public License as published by
12174 + * the Free Software Foundation; either version 2 of the License, or
12175 + * (at your option) any later version.
12176 + *
12177 + * This program is distributed in the hope that it will be useful,
12178 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12179 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12180 + * GNU General Public License for more details.
12181 + *
12182 + * You should have received a copy of the GNU General Public License
12183 + * along with this program; if not, write to the Free Software
12184 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12185 + */
12186 +
12187 +/*
12188 + * file operations for special files.
12189 + * while they exist in aufs virtually,
12190 + * their file I/O is handled out of aufs.
12191 + */
12192 +
12193 +#include <linux/aio.h>
12194 +#include "aufs.h"
12195 +
12196 +/*
12197 + * I don't think the size of this list grows much.
12198 + * so here is a very simple list implemented in order to find finfo matching a
12199 + * given file.
12200 + */
12201 +static struct au_sphlhead au_finfo_sp = {
12202 +       .spin   = __SPIN_LOCK_INITIALIZER(au_finfo_sp.spin),
12203 +       .head   = HLIST_HEAD_INIT
12204 +};
12205 +
12206 +struct au_finfo_sp {
12207 +       struct hlist_node       hlist;
12208 +       struct file             *file;
12209 +       struct au_finfo         *finfo;
12210 +};
12211 +
12212 +struct au_finfo *au_fi_sp(struct file *file)
12213 +{
12214 +       struct au_finfo *finfo;
12215 +       struct au_finfo_sp *sp;
12216 +
12217 +       finfo = NULL;
12218 +       spin_lock(&au_finfo_sp.spin);
12219 +       hlist_for_each_entry(sp, &au_finfo_sp.head, hlist) {
12220 +               if (sp->file != file)
12221 +                       continue;
12222 +               finfo = sp->finfo;
12223 +               break;
12224 +       }
12225 +       spin_unlock(&au_finfo_sp.spin);
12226 +
12227 +       return finfo;
12228 +}
12229 +
12230 +static int au_fi_sp_add(struct file *file)
12231 +{
12232 +       int err;
12233 +       struct au_finfo_sp *sp;
12234 +
12235 +       err = -ENOMEM;
12236 +       sp = kmalloc(sizeof(*sp), GFP_NOFS);
12237 +       if (sp) {
12238 +               err = 0;
12239 +               sp->file = file;
12240 +               sp->finfo = file->private_data;
12241 +               spin_lock(&au_finfo_sp.spin);
12242 +               hlist_add_head(&sp->hlist, &au_finfo_sp.head);
12243 +               spin_unlock(&au_finfo_sp.spin);
12244 +       }
12245 +       return err;
12246 +}
12247 +
12248 +static void au_fi_sp_del(struct file *file)
12249 +{
12250 +       struct au_finfo_sp *sp, *do_free;
12251 +
12252 +       do_free = NULL;
12253 +       spin_lock(&au_finfo_sp.spin);
12254 +       hlist_for_each_entry(sp, &au_finfo_sp.head, hlist) {
12255 +               if (sp->file != file)
12256 +                       continue;
12257 +               hlist_del(&sp->hlist);
12258 +               do_free = sp;
12259 +               break;
12260 +       }
12261 +       spin_unlock(&au_finfo_sp.spin);
12262 +       kfree(do_free);
12263 +}
12264 +
12265 +/* ---------------------------------------------------------------------- */
12266 +
12267 +static ssize_t aufs_aio_read_sp(struct kiocb *kio, const struct iovec *iov,
12268 +                               unsigned long nv, loff_t pos)
12269 +{
12270 +       ssize_t err;
12271 +       aufs_bindex_t bstart;
12272 +       unsigned char wbr;
12273 +       struct file *file, *h_file;
12274 +       struct super_block *sb;
12275 +
12276 +       file = kio->ki_filp;
12277 +       sb = file->f_dentry->d_sb;
12278 +       si_read_lock(sb, AuLock_FLUSH);
12279 +       fi_read_lock(file);
12280 +       bstart = au_fbstart(file);
12281 +       h_file = au_hf_top(file);
12282 +       fi_read_unlock(file);
12283 +       wbr = !!au_br_writable(au_sbr(sb, bstart)->br_perm);
12284 +       si_read_unlock(sb);
12285 +
12286 +       /* do not change the file in kio */
12287 +       AuDebugOn(!h_file->f_op || !h_file->f_op->aio_read);
12288 +       err = h_file->f_op->aio_read(kio, iov, nv, pos);
12289 +       if (err > 0 && wbr)
12290 +               file_accessed(h_file);
12291 +
12292 +       return err;
12293 +}
12294 +
12295 +static ssize_t aufs_aio_write_sp(struct kiocb *kio, const struct iovec *iov,
12296 +                                unsigned long nv, loff_t pos)
12297 +{
12298 +       ssize_t err;
12299 +       aufs_bindex_t bstart;
12300 +       unsigned char wbr;
12301 +       struct super_block *sb;
12302 +       struct file *file, *h_file;
12303 +
12304 +       file = kio->ki_filp;
12305 +       sb = file->f_dentry->d_sb;
12306 +       si_read_lock(sb, AuLock_FLUSH);
12307 +       fi_read_lock(file);
12308 +       bstart = au_fbstart(file);
12309 +       h_file = au_hf_top(file);
12310 +       fi_read_unlock(file);
12311 +       wbr = !!au_br_writable(au_sbr(sb, bstart)->br_perm);
12312 +       si_read_unlock(sb);
12313 +
12314 +       /* do not change the file in kio */
12315 +       AuDebugOn(!h_file->f_op || !h_file->f_op->aio_write);
12316 +       err = h_file->f_op->aio_write(kio, iov, nv, pos);
12317 +       return err;
12318 +}
12319 +
12320 +/* ---------------------------------------------------------------------- */
12321 +
12322 +static int aufs_release_sp(struct inode *inode, struct file *file)
12323 +{
12324 +       int err;
12325 +       struct file *h_file;
12326 +
12327 +       fi_read_lock(file);
12328 +       h_file = au_hf_top(file);
12329 +       fi_read_unlock(file);
12330 +       /* close this fifo in aufs */
12331 +       err = h_file->f_op->release(inode, file); /* ignore */
12332 +       aufs_release_nondir(inode, file); /* ignore */
12333 +       au_fi_sp_del(file);
12334 +       return err;
12335 +}
12336 +
12337 +/* ---------------------------------------------------------------------- */
12338 +
12339 +/* currently, support only FIFO */
12340 +enum {
12341 +       AuSp_FIFO, AuSp_FIFO_R, AuSp_FIFO_W, AuSp_FIFO_RW,
12342 +       /* AuSp_SOCK, AuSp_CHR, AuSp_BLK, */
12343 +       AuSp_Last
12344 +};
12345 +static int aufs_open_sp(struct inode *inode, struct file *file);
12346 +static struct au_sp_fop {
12347 +       int                     done;
12348 +       struct file_operations  fop;    /* not 'const' */
12349 +       spinlock_t              spin;
12350 +} au_sp_fop[AuSp_Last] = {
12351 +       [AuSp_FIFO] = {
12352 +               .fop    = {
12353 +                       .owner  = THIS_MODULE,
12354 +                       .open   = aufs_open_sp
12355 +               }
12356 +       }
12357 +};
12358 +
12359 +static void au_init_fop_sp(struct file *file)
12360 +{
12361 +       struct au_sp_fop *p;
12362 +       int i;
12363 +       struct file *h_file;
12364 +
12365 +       p = au_sp_fop;
12366 +       if (unlikely(!p->done)) {
12367 +               /* initialize first time only */
12368 +               static DEFINE_SPINLOCK(spin);
12369 +
12370 +               spin_lock(&spin);
12371 +               if (!p->done) {
12372 +                       BUILD_BUG_ON(sizeof(au_sp_fop)/sizeof(*au_sp_fop)
12373 +                                    != AuSp_Last);
12374 +                       for (i = 0; i < AuSp_Last; i++)
12375 +                               spin_lock_init(&p[i].spin);
12376 +                       p->done = 1;
12377 +               }
12378 +               spin_unlock(&spin);
12379 +       }
12380 +
12381 +       switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
12382 +       case FMODE_READ:
12383 +               i = AuSp_FIFO_R;
12384 +               break;
12385 +       case FMODE_WRITE:
12386 +               i = AuSp_FIFO_W;
12387 +               break;
12388 +       case FMODE_READ | FMODE_WRITE:
12389 +               i = AuSp_FIFO_RW;
12390 +               break;
12391 +       default:
12392 +               BUG();
12393 +       }
12394 +
12395 +       p += i;
12396 +       if (unlikely(!p->done)) {
12397 +               /* initialize first time only */
12398 +               h_file = au_hf_top(file);
12399 +               spin_lock(&p->spin);
12400 +               if (!p->done) {
12401 +                       p->fop = *h_file->f_op;
12402 +                       p->fop.owner = THIS_MODULE;
12403 +                       if (p->fop.aio_read)
12404 +                               p->fop.aio_read = aufs_aio_read_sp;
12405 +                       if (p->fop.aio_write)
12406 +                               p->fop.aio_write = aufs_aio_write_sp;
12407 +                       p->fop.release = aufs_release_sp;
12408 +                       p->done = 1;
12409 +               }
12410 +               spin_unlock(&p->spin);
12411 +       }
12412 +       file->f_op = &p->fop;
12413 +}
12414 +
12415 +static int au_cpup_sp(struct dentry *dentry)
12416 +{
12417 +       int err;
12418 +       struct au_pin pin;
12419 +       struct au_wr_dir_args wr_dir_args = {
12420 +               .force_btgt     = -1,
12421 +               .flags          = 0
12422 +       };
12423 +       struct au_cp_generic cpg = {
12424 +               .dentry = dentry,
12425 +               .bdst   = -1,
12426 +               .bsrc   = -1,
12427 +               .len    = -1,
12428 +               .pin    = &pin,
12429 +               .flags  = AuCpup_DTIME
12430 +       };
12431 +
12432 +       AuDbg("%.*s\n", AuDLNPair(dentry));
12433 +
12434 +       di_read_unlock(dentry, AuLock_IR);
12435 +       di_write_lock_child(dentry);
12436 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
12437 +       if (unlikely(err < 0))
12438 +               goto out;
12439 +       cpg.bdst = err;
12440 +       err = 0;
12441 +       if (cpg.bdst == au_dbstart(dentry))
12442 +               goto out; /* success */
12443 +
12444 +       err = au_pin(&pin, dentry, cpg.bdst, au_opt_udba(dentry->d_sb),
12445 +                    AuPin_MNT_WRITE);
12446 +       if (!err) {
12447 +               err = au_sio_cpup_simple(&cpg);
12448 +               au_unpin(&pin);
12449 +       }
12450 +
12451 +out:
12452 +       di_downgrade_lock(dentry, AuLock_IR);
12453 +       return err;
12454 +}
12455 +
12456 +static int au_do_open_sp(struct file *file, int flags)
12457 +{
12458 +       int err;
12459 +       struct dentry *dentry;
12460 +       struct super_block *sb;
12461 +       struct file *h_file;
12462 +       struct inode *h_inode;
12463 +
12464 +       err = au_fi_sp_add(file);
12465 +       if (unlikely(err))
12466 +               goto out;
12467 +
12468 +       dentry = file->f_dentry;
12469 +       AuDbg("%.*s\n", AuDLNPair(dentry));
12470 +
12471 +       /*
12472 +        * try copying-up.
12473 +        * operate on the ro branch is not an error.
12474 +        */
12475 +       au_cpup_sp(dentry); /* ignore */
12476 +
12477 +       /* prepare h_file */
12478 +       err = au_do_open_nondir(file, vfsub_file_flags(file));
12479 +       if (unlikely(err))
12480 +               goto out_del;
12481 +
12482 +       sb = dentry->d_sb;
12483 +       h_file = au_hf_top(file);
12484 +       h_inode = file_inode(h_file);
12485 +       di_read_unlock(dentry, AuLock_IR);
12486 +       fi_write_unlock(file);
12487 +       si_read_unlock(sb);
12488 +       /* open this fifo in aufs */
12489 +       err = h_inode->i_fop->open(file_inode(file), file);
12490 +       si_noflush_read_lock(sb);
12491 +       fi_write_lock(file);
12492 +       di_read_lock_child(dentry, AuLock_IR);
12493 +       if (!err) {
12494 +               au_init_fop_sp(file);
12495 +               goto out; /* success */
12496 +       }
12497 +
12498 +out_del:
12499 +       au_fi_sp_del(file);
12500 +out:
12501 +       return err;
12502 +}
12503 +
12504 +static int aufs_open_sp(struct inode *inode, struct file *file)
12505 +{
12506 +       int err;
12507 +       struct super_block *sb;
12508 +
12509 +       sb = file->f_dentry->d_sb;
12510 +       si_read_lock(sb, AuLock_FLUSH);
12511 +       err = au_do_open(file, au_do_open_sp, /*fidir*/NULL);
12512 +       si_read_unlock(sb);
12513 +       return err;
12514 +}
12515 +
12516 +/* ---------------------------------------------------------------------- */
12517 +
12518 +void au_init_special_fop(struct inode *inode, umode_t mode, dev_t rdev)
12519 +{
12520 +       init_special_inode(inode, mode, rdev);
12521 +
12522 +       switch (mode & S_IFMT) {
12523 +       case S_IFIFO:
12524 +               inode->i_fop = &au_sp_fop[AuSp_FIFO].fop;
12525 +               /*FALLTHROUGH*/
12526 +       case S_IFCHR:
12527 +       case S_IFBLK:
12528 +       case S_IFSOCK:
12529 +               break;
12530 +       default:
12531 +               AuDebugOn(1);
12532 +       }
12533 +}
12534 +
12535 +int au_special_file(umode_t mode)
12536 +{
12537 +       int ret;
12538 +
12539 +       ret = 0;
12540 +       switch (mode & S_IFMT) {
12541 +       case S_IFIFO:
12542 +#if 0
12543 +       case S_IFCHR:
12544 +       case S_IFBLK:
12545 +       case S_IFSOCK:
12546 +#endif
12547 +               ret = 1;
12548 +       }
12549 +
12550 +       return ret;
12551 +}
12552 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
12553 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
12554 +++ linux/fs/aufs/fstype.h      2013-08-23 23:59:39.634916914 +0200
12555 @@ -0,0 +1,470 @@
12556 +/*
12557 + * Copyright (C) 2005-2013 Junjiro R. Okajima
12558 + *
12559 + * This program, aufs is free software; you can redistribute it and/or modify
12560 + * it under the terms of the GNU General Public License as published by
12561 + * the Free Software Foundation; either version 2 of the License, or
12562 + * (at your option) any later version.
12563 + *
12564 + * This program is distributed in the hope that it will be useful,
12565 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12566 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12567 + * GNU General Public License for more details.
12568 + *
12569 + * You should have received a copy of the GNU General Public License
12570 + * along with this program; if not, write to the Free Software
12571 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12572 + */
12573 +
12574 +/*
12575 + * judging filesystem type
12576 + */
12577 +
12578 +#ifndef __AUFS_FSTYPE_H__
12579 +#define __AUFS_FSTYPE_H__
12580 +
12581 +#ifdef __KERNEL__
12582 +
12583 +#include <linux/fs.h>
12584 +#include <linux/magic.h>
12585 +#include <linux/romfs_fs.h>
12586 +
12587 +static inline int au_test_aufs(struct super_block *sb)
12588 +{
12589 +       return sb->s_magic == AUFS_SUPER_MAGIC;
12590 +}
12591 +
12592 +static inline const char *au_sbtype(struct super_block *sb)
12593 +{
12594 +       return sb->s_type->name;
12595 +}
12596 +
12597 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
12598 +{
12599 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
12600 +       return sb->s_magic == ROMFS_MAGIC;
12601 +#else
12602 +       return 0;
12603 +#endif
12604 +}
12605 +
12606 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
12607 +{
12608 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
12609 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
12610 +#else
12611 +       return 0;
12612 +#endif
12613 +}
12614 +
12615 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
12616 +{
12617 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
12618 +       return sb->s_magic == CRAMFS_MAGIC;
12619 +#endif
12620 +       return 0;
12621 +}
12622 +
12623 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
12624 +{
12625 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
12626 +       return sb->s_magic == NFS_SUPER_MAGIC;
12627 +#else
12628 +       return 0;
12629 +#endif
12630 +}
12631 +
12632 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
12633 +{
12634 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
12635 +       return sb->s_magic == FUSE_SUPER_MAGIC;
12636 +#else
12637 +       return 0;
12638 +#endif
12639 +}
12640 +
12641 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
12642 +{
12643 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
12644 +       return sb->s_magic == XFS_SB_MAGIC;
12645 +#else
12646 +       return 0;
12647 +#endif
12648 +}
12649 +
12650 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
12651 +{
12652 +#ifdef CONFIG_TMPFS
12653 +       return sb->s_magic == TMPFS_MAGIC;
12654 +#else
12655 +       return 0;
12656 +#endif
12657 +}
12658 +
12659 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
12660 +{
12661 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
12662 +       return !strcmp(au_sbtype(sb), "ecryptfs");
12663 +#else
12664 +       return 0;
12665 +#endif
12666 +}
12667 +
12668 +static inline int au_test_ocfs2(struct super_block *sb __maybe_unused)
12669 +{
12670 +#if defined(CONFIG_OCFS2_FS) || defined(CONFIG_OCFS2_FS_MODULE)
12671 +       return sb->s_magic == OCFS2_SUPER_MAGIC;
12672 +#else
12673 +       return 0;
12674 +#endif
12675 +}
12676 +
12677 +static inline int au_test_ocfs2_dlmfs(struct super_block *sb __maybe_unused)
12678 +{
12679 +#if defined(CONFIG_OCFS2_FS_O2CB) || defined(CONFIG_OCFS2_FS_O2CB_MODULE)
12680 +       return sb->s_magic == DLMFS_MAGIC;
12681 +#else
12682 +       return 0;
12683 +#endif
12684 +}
12685 +
12686 +static inline int au_test_coda(struct super_block *sb __maybe_unused)
12687 +{
12688 +#if defined(CONFIG_CODA_FS) || defined(CONFIG_CODA_FS_MODULE)
12689 +       return sb->s_magic == CODA_SUPER_MAGIC;
12690 +#else
12691 +       return 0;
12692 +#endif
12693 +}
12694 +
12695 +static inline int au_test_v9fs(struct super_block *sb __maybe_unused)
12696 +{
12697 +#if defined(CONFIG_9P_FS) || defined(CONFIG_9P_FS_MODULE)
12698 +       return sb->s_magic == V9FS_MAGIC;
12699 +#else
12700 +       return 0;
12701 +#endif
12702 +}
12703 +
12704 +static inline int au_test_ext4(struct super_block *sb __maybe_unused)
12705 +{
12706 +#if defined(CONFIG_EXT4_FS) || defined(CONFIG_EXT4_FS_MODULE)
12707 +       return sb->s_magic == EXT4_SUPER_MAGIC;
12708 +#else
12709 +       return 0;
12710 +#endif
12711 +}
12712 +
12713 +static inline int au_test_sysv(struct super_block *sb __maybe_unused)
12714 +{
12715 +#if defined(CONFIG_SYSV_FS) || defined(CONFIG_SYSV_FS_MODULE)
12716 +       return !strcmp(au_sbtype(sb), "sysv");
12717 +#else
12718 +       return 0;
12719 +#endif
12720 +}
12721 +
12722 +static inline int au_test_ramfs(struct super_block *sb)
12723 +{
12724 +       return sb->s_magic == RAMFS_MAGIC;
12725 +}
12726 +
12727 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
12728 +{
12729 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
12730 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
12731 +#else
12732 +       return 0;
12733 +#endif
12734 +}
12735 +
12736 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
12737 +{
12738 +#ifdef CONFIG_PROC_FS
12739 +       return sb->s_magic == PROC_SUPER_MAGIC;
12740 +#else
12741 +       return 0;
12742 +#endif
12743 +}
12744 +
12745 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
12746 +{
12747 +#ifdef CONFIG_SYSFS
12748 +       return sb->s_magic == SYSFS_MAGIC;
12749 +#else
12750 +       return 0;
12751 +#endif
12752 +}
12753 +
12754 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
12755 +{
12756 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
12757 +       return sb->s_magic == CONFIGFS_MAGIC;
12758 +#else
12759 +       return 0;
12760 +#endif
12761 +}
12762 +
12763 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
12764 +{
12765 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
12766 +       return sb->s_magic == MINIX3_SUPER_MAGIC
12767 +               || sb->s_magic == MINIX2_SUPER_MAGIC
12768 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
12769 +               || sb->s_magic == MINIX_SUPER_MAGIC
12770 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
12771 +#else
12772 +       return 0;
12773 +#endif
12774 +}
12775 +
12776 +static inline int au_test_cifs(struct super_block *sb __maybe_unused)
12777 +{
12778 +#if defined(CONFIG_CIFS_FS) || defined(CONFIGCIFS_FS_MODULE)
12779 +       return sb->s_magic == CIFS_MAGIC_NUMBER;
12780 +#else
12781 +       return 0;
12782 +#endif
12783 +}
12784 +
12785 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
12786 +{
12787 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
12788 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
12789 +#else
12790 +       return 0;
12791 +#endif
12792 +}
12793 +
12794 +static inline int au_test_msdos(struct super_block *sb)
12795 +{
12796 +       return au_test_fat(sb);
12797 +}
12798 +
12799 +static inline int au_test_vfat(struct super_block *sb)
12800 +{
12801 +       return au_test_fat(sb);
12802 +}
12803 +
12804 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
12805 +{
12806 +#ifdef CONFIG_SECURITYFS
12807 +       return sb->s_magic == SECURITYFS_MAGIC;
12808 +#else
12809 +       return 0;
12810 +#endif
12811 +}
12812 +
12813 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
12814 +{
12815 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
12816 +       return sb->s_magic == SQUASHFS_MAGIC;
12817 +#else
12818 +       return 0;
12819 +#endif
12820 +}
12821 +
12822 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
12823 +{
12824 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
12825 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
12826 +#else
12827 +       return 0;
12828 +#endif
12829 +}
12830 +
12831 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
12832 +{
12833 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
12834 +       return sb->s_magic == XENFS_SUPER_MAGIC;
12835 +#else
12836 +       return 0;
12837 +#endif
12838 +}
12839 +
12840 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
12841 +{
12842 +#ifdef CONFIG_DEBUG_FS
12843 +       return sb->s_magic == DEBUGFS_MAGIC;
12844 +#else
12845 +       return 0;
12846 +#endif
12847 +}
12848 +
12849 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
12850 +{
12851 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
12852 +       return sb->s_magic == NILFS_SUPER_MAGIC;
12853 +#else
12854 +       return 0;
12855 +#endif
12856 +}
12857 +
12858 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
12859 +{
12860 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
12861 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
12862 +#else
12863 +       return 0;
12864 +#endif
12865 +}
12866 +
12867 +/* ---------------------------------------------------------------------- */
12868 +/*
12869 + * they can't be an aufs branch.
12870 + */
12871 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
12872 +{
12873 +       return
12874 +#ifndef CONFIG_AUFS_BR_RAMFS
12875 +               au_test_ramfs(sb) ||
12876 +#endif
12877 +               au_test_procfs(sb)
12878 +               || au_test_sysfs(sb)
12879 +               || au_test_configfs(sb)
12880 +               || au_test_debugfs(sb)
12881 +               || au_test_securityfs(sb)
12882 +               || au_test_xenfs(sb)
12883 +               || au_test_ecryptfs(sb)
12884 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
12885 +               || au_test_aufs(sb); /* will be supported in next version */
12886 +}
12887 +
12888 +static inline int au_test_fs_remote(struct super_block *sb)
12889 +{
12890 +       return !au_test_tmpfs(sb)
12891 +#ifdef CONFIG_AUFS_BR_RAMFS
12892 +               && !au_test_ramfs(sb)
12893 +#endif
12894 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
12895 +}
12896 +
12897 +/* ---------------------------------------------------------------------- */
12898 +
12899 +/*
12900 + * Note: these functions (below) are created after reading ->getattr() in all
12901 + * filesystems under linux/fs. it means we have to do so in every update...
12902 + */
12903 +
12904 +/*
12905 + * some filesystems require getattr to refresh the inode attributes before
12906 + * referencing.
12907 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
12908 + * and leave the work for d_revalidate()
12909 + */
12910 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
12911 +{
12912 +       return au_test_nfs(sb)
12913 +               || au_test_fuse(sb)
12914 +               /* || au_test_ocfs2(sb) */      /* untested */
12915 +               /* || au_test_btrfs(sb) */      /* untested */
12916 +               /* || au_test_coda(sb) */       /* untested */
12917 +               /* || au_test_v9fs(sb) */       /* untested */
12918 +               ;
12919 +}
12920 +
12921 +/*
12922 + * filesystems which don't maintain i_size or i_blocks.
12923 + */
12924 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
12925 +{
12926 +       return au_test_xfs(sb)
12927 +               || au_test_btrfs(sb)
12928 +               || au_test_ubifs(sb)
12929 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
12930 +               /* || au_test_ext4(sb) */       /* untested */
12931 +               /* || au_test_ocfs2(sb) */      /* untested */
12932 +               /* || au_test_ocfs2_dlmfs(sb) */ /* untested */
12933 +               /* || au_test_sysv(sb) */       /* untested */
12934 +               /* || au_test_minix(sb) */      /* untested */
12935 +               ;
12936 +}
12937 +
12938 +/*
12939 + * filesystems which don't store the correct value in some of their inode
12940 + * attributes.
12941 + */
12942 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
12943 +{
12944 +       return au_test_fs_bad_iattr_size(sb)
12945 +               /* || au_test_cifs(sb) */       /* untested */
12946 +               || au_test_fat(sb)
12947 +               || au_test_msdos(sb)
12948 +               || au_test_vfat(sb);
12949 +}
12950 +
12951 +/* they don't check i_nlink in link(2) */
12952 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
12953 +{
12954 +       return au_test_tmpfs(sb)
12955 +#ifdef CONFIG_AUFS_BR_RAMFS
12956 +               || au_test_ramfs(sb)
12957 +#endif
12958 +               || au_test_ubifs(sb)
12959 +               || au_test_hfsplus(sb);
12960 +}
12961 +
12962 +/*
12963 + * filesystems which sets S_NOATIME and S_NOCMTIME.
12964 + */
12965 +static inline int au_test_fs_notime(struct super_block *sb)
12966 +{
12967 +       return au_test_nfs(sb)
12968 +               || au_test_fuse(sb)
12969 +               || au_test_ubifs(sb)
12970 +               /* || au_test_cifs(sb) */       /* untested */
12971 +               ;
12972 +}
12973 +
12974 +/*
12975 + * filesystems which requires replacing i_mapping.
12976 + */
12977 +static inline int au_test_fs_bad_mapping(struct super_block *sb)
12978 +{
12979 +       return au_test_fuse(sb)
12980 +               || au_test_ubifs(sb);
12981 +}
12982 +
12983 +/* temporary support for i#1 in cramfs */
12984 +static inline int au_test_fs_unique_ino(struct inode *inode)
12985 +{
12986 +       if (au_test_cramfs(inode->i_sb))
12987 +               return inode->i_ino != 1;
12988 +       return 1;
12989 +}
12990 +
12991 +/* ---------------------------------------------------------------------- */
12992 +
12993 +/*
12994 + * the filesystem where the xino files placed must support i/o after unlink and
12995 + * maintain i_size and i_blocks.
12996 + */
12997 +static inline int au_test_fs_bad_xino(struct super_block *sb)
12998 +{
12999 +       return au_test_fs_remote(sb)
13000 +               || au_test_fs_bad_iattr_size(sb)
13001 +               /* don't want unnecessary work for xino */
13002 +               || au_test_aufs(sb)
13003 +               || au_test_ecryptfs(sb)
13004 +               || au_test_nilfs(sb);
13005 +}
13006 +
13007 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
13008 +{
13009 +       return au_test_tmpfs(sb)
13010 +               || au_test_ramfs(sb);
13011 +}
13012 +
13013 +/*
13014 + * test if the @sb is real-readonly.
13015 + */
13016 +static inline int au_test_fs_rr(struct super_block *sb)
13017 +{
13018 +       return au_test_squashfs(sb)
13019 +               || au_test_iso9660(sb)
13020 +               || au_test_cramfs(sb)
13021 +               || au_test_romfs(sb);
13022 +}
13023 +
13024 +#endif /* __KERNEL__ */
13025 +#endif /* __AUFS_FSTYPE_H__ */
13026 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
13027 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
13028 +++ linux/fs/aufs/hfsnotify.c   2013-07-06 13:20:47.750198454 +0200
13029 @@ -0,0 +1,296 @@
13030 +/*
13031 + * Copyright (C) 2005-2013 Junjiro R. Okajima
13032 + *
13033 + * This program, aufs is free software; you can redistribute it and/or modify
13034 + * it under the terms of the GNU General Public License as published by
13035 + * the Free Software Foundation; either version 2 of the License, or
13036 + * (at your option) any later version.
13037 + *
13038 + * This program is distributed in the hope that it will be useful,
13039 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13040 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13041 + * GNU General Public License for more details.
13042 + *
13043 + * You should have received a copy of the GNU General Public License
13044 + * along with this program; if not, write to the Free Software
13045 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13046 + */
13047 +
13048 +/*
13049 + * fsnotify for the lower directories
13050 + */
13051 +
13052 +#include "aufs.h"
13053 +
13054 +/* FS_IN_IGNORED is unnecessary */
13055 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
13056 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
13057 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
13058 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
13059 +
13060 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
13061 +{
13062 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
13063 +                                            hn_mark);
13064 +       AuDbg("here\n");
13065 +       au_cache_free_hnotify(hn);
13066 +       smp_mb__before_atomic_dec();
13067 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
13068 +               wake_up(&au_hfsn_wq);
13069 +}
13070 +
13071 +static int au_hfsn_alloc(struct au_hinode *hinode)
13072 +{
13073 +       int err;
13074 +       struct au_hnotify *hn;
13075 +       struct super_block *sb;
13076 +       struct au_branch *br;
13077 +       struct fsnotify_mark *mark;
13078 +       aufs_bindex_t bindex;
13079 +
13080 +       hn = hinode->hi_notify;
13081 +       sb = hn->hn_aufs_inode->i_sb;
13082 +       bindex = au_br_index(sb, hinode->hi_id);
13083 +       br = au_sbr(sb, bindex);
13084 +       AuDebugOn(!br->br_hfsn);
13085 +
13086 +       mark = &hn->hn_mark;
13087 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
13088 +       mark->mask = AuHfsnMask;
13089 +       /*
13090 +        * by udba rename or rmdir, aufs assign a new inode to the known
13091 +        * h_inode, so specify 1 to allow dups.
13092 +        */
13093 +       err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
13094 +                                /*mnt*/NULL, /*allow_dups*/1);
13095 +       /* even if err */
13096 +       fsnotify_put_mark(mark);
13097 +
13098 +       return err;
13099 +}
13100 +
13101 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
13102 +{
13103 +       struct fsnotify_mark *mark;
13104 +       unsigned long long ull;
13105 +       struct fsnotify_group *group;
13106 +
13107 +       ull = atomic64_inc_return(&au_hfsn_ifree);
13108 +       BUG_ON(!ull);
13109 +
13110 +       mark = &hn->hn_mark;
13111 +       spin_lock(&mark->lock);
13112 +       group = mark->group;
13113 +       fsnotify_get_group(group);
13114 +       spin_unlock(&mark->lock);
13115 +       fsnotify_destroy_mark(mark, group);
13116 +       fsnotify_put_group(group);
13117 +
13118 +       /* free hn by myself */
13119 +       return 0;
13120 +}
13121 +
13122 +/* ---------------------------------------------------------------------- */
13123 +
13124 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
13125 +{
13126 +       struct fsnotify_mark *mark;
13127 +
13128 +       mark = &hinode->hi_notify->hn_mark;
13129 +       spin_lock(&mark->lock);
13130 +       if (do_set) {
13131 +               AuDebugOn(mark->mask & AuHfsnMask);
13132 +               mark->mask |= AuHfsnMask;
13133 +       } else {
13134 +               AuDebugOn(!(mark->mask & AuHfsnMask));
13135 +               mark->mask &= ~AuHfsnMask;
13136 +       }
13137 +       spin_unlock(&mark->lock);
13138 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
13139 +}
13140 +
13141 +/* ---------------------------------------------------------------------- */
13142 +
13143 +/* #define AuDbgHnotify */
13144 +#ifdef AuDbgHnotify
13145 +static char *au_hfsn_name(u32 mask)
13146 +{
13147 +#ifdef CONFIG_AUFS_DEBUG
13148 +#define test_ret(flag)                         \
13149 +       do {                                    \
13150 +               if (mask & flag)                \
13151 +                       return #flag;           \
13152 +       } while (0)
13153 +       test_ret(FS_ACCESS);
13154 +       test_ret(FS_MODIFY);
13155 +       test_ret(FS_ATTRIB);
13156 +       test_ret(FS_CLOSE_WRITE);
13157 +       test_ret(FS_CLOSE_NOWRITE);
13158 +       test_ret(FS_OPEN);
13159 +       test_ret(FS_MOVED_FROM);
13160 +       test_ret(FS_MOVED_TO);
13161 +       test_ret(FS_CREATE);
13162 +       test_ret(FS_DELETE);
13163 +       test_ret(FS_DELETE_SELF);
13164 +       test_ret(FS_MOVE_SELF);
13165 +       test_ret(FS_UNMOUNT);
13166 +       test_ret(FS_Q_OVERFLOW);
13167 +       test_ret(FS_IN_IGNORED);
13168 +       test_ret(FS_IN_ISDIR);
13169 +       test_ret(FS_IN_ONESHOT);
13170 +       test_ret(FS_EVENT_ON_CHILD);
13171 +       return "";
13172 +#undef test_ret
13173 +#else
13174 +       return "??";
13175 +#endif
13176 +}
13177 +#endif
13178 +
13179 +/* ---------------------------------------------------------------------- */
13180 +
13181 +static void au_hfsn_free_group(struct fsnotify_group *group)
13182 +{
13183 +       struct au_br_hfsnotify *hfsn = group->private;
13184 +
13185 +       AuDbg("here\n");
13186 +       kfree(hfsn);
13187 +}
13188 +
13189 +static int au_hfsn_handle_event(struct fsnotify_group *group,
13190 +                               struct fsnotify_mark *inode_mark,
13191 +                               struct fsnotify_mark *vfsmount_mark,
13192 +                               struct fsnotify_event *event)
13193 +{
13194 +       int err;
13195 +       struct au_hnotify *hnotify;
13196 +       struct inode *h_dir, *h_inode;
13197 +       __u32 mask;
13198 +       struct qstr h_child_qstr = QSTR_INIT(event->file_name, event->name_len);
13199 +
13200 +       AuDebugOn(event->data_type != FSNOTIFY_EVENT_INODE);
13201 +
13202 +       err = 0;
13203 +       /* if FS_UNMOUNT happens, there must be another bug */
13204 +       mask = event->mask;
13205 +       AuDebugOn(mask & FS_UNMOUNT);
13206 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
13207 +               goto out;
13208 +
13209 +       h_dir = event->to_tell;
13210 +       h_inode = event->inode;
13211 +#ifdef AuDbgHnotify
13212 +       au_debug(1);
13213 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
13214 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
13215 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
13216 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
13217 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
13218 +               /* WARN_ON(1); */
13219 +       }
13220 +       au_debug(0);
13221 +#endif
13222 +
13223 +       AuDebugOn(!inode_mark);
13224 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
13225 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
13226 +
13227 +out:
13228 +       return err;
13229 +}
13230 +
13231 +/* isn't it waste to ask every registered 'group'? */
13232 +/* copied from linux/fs/notify/inotify/inotify_fsnotiry.c */
13233 +/* it should be exported to modules */
13234 +static bool au_hfsn_should_send_event(struct fsnotify_group *group,
13235 +                                     struct inode *h_inode,
13236 +                                     struct fsnotify_mark *inode_mark,
13237 +                                     struct fsnotify_mark *vfsmount_mark,
13238 +                                     __u32 mask, void *data, int data_type)
13239 +{
13240 +       mask = (mask & ~FS_EVENT_ON_CHILD);
13241 +       return inode_mark->mask & mask;
13242 +}
13243 +
13244 +static struct fsnotify_ops au_hfsn_ops = {
13245 +       .should_send_event      = au_hfsn_should_send_event,
13246 +       .handle_event           = au_hfsn_handle_event,
13247 +       .free_group_priv        = au_hfsn_free_group
13248 +};
13249 +
13250 +/* ---------------------------------------------------------------------- */
13251 +
13252 +static void au_hfsn_fin_br(struct au_branch *br)
13253 +{
13254 +       struct au_br_hfsnotify *hfsn;
13255 +
13256 +       hfsn = br->br_hfsn;
13257 +       if (hfsn)
13258 +               fsnotify_put_group(hfsn->hfsn_group);
13259 +}
13260 +
13261 +static int au_hfsn_init_br(struct au_branch *br, int perm)
13262 +{
13263 +       int err;
13264 +       struct fsnotify_group *group;
13265 +       struct au_br_hfsnotify *hfsn;
13266 +
13267 +       err = 0;
13268 +       br->br_hfsn = NULL;
13269 +       if (!au_br_hnotifyable(perm))
13270 +               goto out;
13271 +
13272 +       err = -ENOMEM;
13273 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
13274 +       if (unlikely(!hfsn))
13275 +               goto out;
13276 +
13277 +       err = 0;
13278 +       group = fsnotify_alloc_group(&au_hfsn_ops);
13279 +       if (IS_ERR(group)) {
13280 +               err = PTR_ERR(group);
13281 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
13282 +               goto out_hfsn;
13283 +       }
13284 +
13285 +       group->private = hfsn;
13286 +       hfsn->hfsn_group = group;
13287 +       br->br_hfsn = hfsn;
13288 +       goto out; /* success */
13289 +
13290 +out_hfsn:
13291 +       kfree(hfsn);
13292 +out:
13293 +       return err;
13294 +}
13295 +
13296 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
13297 +{
13298 +       int err;
13299 +
13300 +       err = 0;
13301 +       if (!br->br_hfsn)
13302 +               err = au_hfsn_init_br(br, perm);
13303 +
13304 +       return err;
13305 +}
13306 +
13307 +/* ---------------------------------------------------------------------- */
13308 +
13309 +static void au_hfsn_fin(void)
13310 +{
13311 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
13312 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
13313 +}
13314 +
13315 +const struct au_hnotify_op au_hnotify_op = {
13316 +       .ctl            = au_hfsn_ctl,
13317 +       .alloc          = au_hfsn_alloc,
13318 +       .free           = au_hfsn_free,
13319 +
13320 +       .fin            = au_hfsn_fin,
13321 +
13322 +       .reset_br       = au_hfsn_reset_br,
13323 +       .fin_br         = au_hfsn_fin_br,
13324 +       .init_br        = au_hfsn_init_br
13325 +};
13326 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
13327 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
13328 +++ linux/fs/aufs/hfsplus.c     2013-07-06 13:20:47.750198454 +0200
13329 @@ -0,0 +1,56 @@
13330 +/*
13331 + * Copyright (C) 2010-2013 Junjiro R. Okajima
13332 + *
13333 + * This program, aufs is free software; you can redistribute it and/or modify
13334 + * it under the terms of the GNU General Public License as published by
13335 + * the Free Software Foundation; either version 2 of the License, or
13336 + * (at your option) any later version.
13337 + *
13338 + * This program is distributed in the hope that it will be useful,
13339 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13340 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13341 + * GNU General Public License for more details.
13342 + *
13343 + * You should have received a copy of the GNU General Public License
13344 + * along with this program; if not, write to the Free Software
13345 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13346 + */
13347 +
13348 +/*
13349 + * special support for filesystems which aqucires an inode mutex
13350 + * at final closing a file, eg, hfsplus.
13351 + *
13352 + * This trick is very simple and stupid, just to open the file before really
13353 + * neceeary open to tell hfsplus that this is not the final closing.
13354 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
13355 + * and au_h_open_post() after releasing it.
13356 + */
13357 +
13358 +#include "aufs.h"
13359 +
13360 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex)
13361 +{
13362 +       struct file *h_file;
13363 +       struct dentry *h_dentry;
13364 +
13365 +       h_dentry = au_h_dptr(dentry, bindex);
13366 +       AuDebugOn(!h_dentry);
13367 +       AuDebugOn(!h_dentry->d_inode);
13368 +
13369 +       h_file = NULL;
13370 +       if (au_test_hfsplus(h_dentry->d_sb)
13371 +           && S_ISREG(h_dentry->d_inode->i_mode))
13372 +               h_file = au_h_open(dentry, bindex,
13373 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
13374 +                                  /*file*/NULL);
13375 +       return h_file;
13376 +}
13377 +
13378 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
13379 +                   struct file *h_file)
13380 +{
13381 +       if (h_file) {
13382 +               fput(h_file);
13383 +               au_sbr_put(dentry->d_sb, bindex);
13384 +       }
13385 +}
13386 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
13387 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
13388 +++ linux/fs/aufs/hnotify.c     2013-07-30 22:42:55.842946719 +0200
13389 @@ -0,0 +1,712 @@
13390 +/*
13391 + * Copyright (C) 2005-2013 Junjiro R. Okajima
13392 + *
13393 + * This program, aufs is free software; you can redistribute it and/or modify
13394 + * it under the terms of the GNU General Public License as published by
13395 + * the Free Software Foundation; either version 2 of the License, or
13396 + * (at your option) any later version.
13397 + *
13398 + * This program is distributed in the hope that it will be useful,
13399 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13400 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13401 + * GNU General Public License for more details.
13402 + *
13403 + * You should have received a copy of the GNU General Public License
13404 + * along with this program; if not, write to the Free Software
13405 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13406 + */
13407 +
13408 +/*
13409 + * abstraction to notify the direct changes on lower directories
13410 + */
13411 +
13412 +#include "aufs.h"
13413 +
13414 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
13415 +{
13416 +       int err;
13417 +       struct au_hnotify *hn;
13418 +
13419 +       err = -ENOMEM;
13420 +       hn = au_cache_alloc_hnotify();
13421 +       if (hn) {
13422 +               hn->hn_aufs_inode = inode;
13423 +               hinode->hi_notify = hn;
13424 +               err = au_hnotify_op.alloc(hinode);
13425 +               AuTraceErr(err);
13426 +               if (unlikely(err)) {
13427 +                       hinode->hi_notify = NULL;
13428 +                       au_cache_free_hnotify(hn);
13429 +                       /*
13430 +                        * The upper dir was removed by udba, but the same named
13431 +                        * dir left. In this case, aufs assignes a new inode
13432 +                        * number and set the monitor again.
13433 +                        * For the lower dir, the old monitnor is still left.
13434 +                        */
13435 +                       if (err == -EEXIST)
13436 +                               err = 0;
13437 +               }
13438 +       }
13439 +
13440 +       AuTraceErr(err);
13441 +       return err;
13442 +}
13443 +
13444 +void au_hn_free(struct au_hinode *hinode)
13445 +{
13446 +       struct au_hnotify *hn;
13447 +
13448 +       hn = hinode->hi_notify;
13449 +       if (hn) {
13450 +               hinode->hi_notify = NULL;
13451 +               if (au_hnotify_op.free(hinode, hn))
13452 +                       au_cache_free_hnotify(hn);
13453 +       }
13454 +}
13455 +
13456 +/* ---------------------------------------------------------------------- */
13457 +
13458 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
13459 +{
13460 +       if (hinode->hi_notify)
13461 +               au_hnotify_op.ctl(hinode, do_set);
13462 +}
13463 +
13464 +void au_hn_reset(struct inode *inode, unsigned int flags)
13465 +{
13466 +       aufs_bindex_t bindex, bend;
13467 +       struct inode *hi;
13468 +       struct dentry *iwhdentry;
13469 +
13470 +       bend = au_ibend(inode);
13471 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
13472 +               hi = au_h_iptr(inode, bindex);
13473 +               if (!hi)
13474 +                       continue;
13475 +
13476 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
13477 +               iwhdentry = au_hi_wh(inode, bindex);
13478 +               if (iwhdentry)
13479 +                       dget(iwhdentry);
13480 +               au_igrab(hi);
13481 +               au_set_h_iptr(inode, bindex, NULL, 0);
13482 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
13483 +                             flags & ~AuHi_XINO);
13484 +               iput(hi);
13485 +               dput(iwhdentry);
13486 +               /* mutex_unlock(&hi->i_mutex); */
13487 +       }
13488 +}
13489 +
13490 +/* ---------------------------------------------------------------------- */
13491 +
13492 +static int hn_xino(struct inode *inode, struct inode *h_inode)
13493 +{
13494 +       int err;
13495 +       aufs_bindex_t bindex, bend, bfound, bstart;
13496 +       struct inode *h_i;
13497 +
13498 +       err = 0;
13499 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13500 +               pr_warn("branch root dir was changed\n");
13501 +               goto out;
13502 +       }
13503 +
13504 +       bfound = -1;
13505 +       bend = au_ibend(inode);
13506 +       bstart = au_ibstart(inode);
13507 +#if 0 /* reserved for future use */
13508 +       if (bindex == bend) {
13509 +               /* keep this ino in rename case */
13510 +               goto out;
13511 +       }
13512 +#endif
13513 +       for (bindex = bstart; bindex <= bend; bindex++)
13514 +               if (au_h_iptr(inode, bindex) == h_inode) {
13515 +                       bfound = bindex;
13516 +                       break;
13517 +               }
13518 +       if (bfound < 0)
13519 +               goto out;
13520 +
13521 +       for (bindex = bstart; bindex <= bend; bindex++) {
13522 +               h_i = au_h_iptr(inode, bindex);
13523 +               if (!h_i)
13524 +                       continue;
13525 +
13526 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
13527 +               /* ignore this error */
13528 +               /* bad action? */
13529 +       }
13530 +
13531 +       /* children inode number will be broken */
13532 +
13533 +out:
13534 +       AuTraceErr(err);
13535 +       return err;
13536 +}
13537 +
13538 +static int hn_gen_tree(struct dentry *dentry)
13539 +{
13540 +       int err, i, j, ndentry;
13541 +       struct au_dcsub_pages dpages;
13542 +       struct au_dpage *dpage;
13543 +       struct dentry **dentries;
13544 +
13545 +       err = au_dpages_init(&dpages, GFP_NOFS);
13546 +       if (unlikely(err))
13547 +               goto out;
13548 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
13549 +       if (unlikely(err))
13550 +               goto out_dpages;
13551 +
13552 +       for (i = 0; i < dpages.ndpage; i++) {
13553 +               dpage = dpages.dpages + i;
13554 +               dentries = dpage->dentries;
13555 +               ndentry = dpage->ndentry;
13556 +               for (j = 0; j < ndentry; j++) {
13557 +                       struct dentry *d;
13558 +
13559 +                       d = dentries[j];
13560 +                       if (IS_ROOT(d))
13561 +                               continue;
13562 +
13563 +                       au_digen_dec(d);
13564 +                       if (d->d_inode)
13565 +                               /* todo: reset children xino?
13566 +                                  cached children only? */
13567 +                               au_iigen_dec(d->d_inode);
13568 +               }
13569 +       }
13570 +
13571 +out_dpages:
13572 +       au_dpages_free(&dpages);
13573 +
13574 +#if 0
13575 +       /* discard children */
13576 +       dentry_unhash(dentry);
13577 +       dput(dentry);
13578 +#endif
13579 +out:
13580 +       return err;
13581 +}
13582 +
13583 +/*
13584 + * return 0 if processed.
13585 + */
13586 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
13587 +                          const unsigned int isdir)
13588 +{
13589 +       int err;
13590 +       struct dentry *d;
13591 +       struct qstr *dname;
13592 +
13593 +       err = 1;
13594 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13595 +               pr_warn("branch root dir was changed\n");
13596 +               err = 0;
13597 +               goto out;
13598 +       }
13599 +
13600 +       if (!isdir) {
13601 +               AuDebugOn(!name);
13602 +               au_iigen_dec(inode);
13603 +               spin_lock(&inode->i_lock);
13604 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13605 +                       spin_lock(&d->d_lock);
13606 +                       dname = &d->d_name;
13607 +                       if (dname->len != nlen
13608 +                           && memcmp(dname->name, name, nlen)) {
13609 +                               spin_unlock(&d->d_lock);
13610 +                               continue;
13611 +                       }
13612 +                       err = 0;
13613 +                       au_digen_dec(d);
13614 +                       spin_unlock(&d->d_lock);
13615 +                       break;
13616 +               }
13617 +               spin_unlock(&inode->i_lock);
13618 +       } else {
13619 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
13620 +               d = d_find_alias(inode);
13621 +               if (!d) {
13622 +                       au_iigen_dec(inode);
13623 +                       goto out;
13624 +               }
13625 +
13626 +               spin_lock(&d->d_lock);
13627 +               dname = &d->d_name;
13628 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
13629 +                       spin_unlock(&d->d_lock);
13630 +                       err = hn_gen_tree(d);
13631 +                       spin_lock(&d->d_lock);
13632 +               }
13633 +               spin_unlock(&d->d_lock);
13634 +               dput(d);
13635 +       }
13636 +
13637 +out:
13638 +       AuTraceErr(err);
13639 +       return err;
13640 +}
13641 +
13642 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
13643 +{
13644 +       int err;
13645 +       struct inode *inode;
13646 +
13647 +       inode = dentry->d_inode;
13648 +       if (IS_ROOT(dentry)
13649 +           /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
13650 +               ) {
13651 +               pr_warn("branch root dir was changed\n");
13652 +               return 0;
13653 +       }
13654 +
13655 +       err = 0;
13656 +       if (!isdir) {
13657 +               au_digen_dec(dentry);
13658 +               if (inode)
13659 +                       au_iigen_dec(inode);
13660 +       } else {
13661 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
13662 +               if (inode)
13663 +                       err = hn_gen_tree(dentry);
13664 +       }
13665 +
13666 +       AuTraceErr(err);
13667 +       return err;
13668 +}
13669 +
13670 +/* ---------------------------------------------------------------------- */
13671 +
13672 +/* hnotify job flags */
13673 +#define AuHnJob_XINO0          1
13674 +#define AuHnJob_GEN            (1 << 1)
13675 +#define AuHnJob_DIRENT         (1 << 2)
13676 +#define AuHnJob_ISDIR          (1 << 3)
13677 +#define AuHnJob_TRYXINO0       (1 << 4)
13678 +#define AuHnJob_MNTPNT         (1 << 5)
13679 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
13680 +#define au_fset_hnjob(flags, name) \
13681 +       do { (flags) |= AuHnJob_##name; } while (0)
13682 +#define au_fclr_hnjob(flags, name) \
13683 +       do { (flags) &= ~AuHnJob_##name; } while (0)
13684 +
13685 +enum {
13686 +       AuHn_CHILD,
13687 +       AuHn_PARENT,
13688 +       AuHnLast
13689 +};
13690 +
13691 +struct au_hnotify_args {
13692 +       struct inode *h_dir, *dir, *h_child_inode;
13693 +       u32 mask;
13694 +       unsigned int flags[AuHnLast];
13695 +       unsigned int h_child_nlen;
13696 +       char h_child_name[];
13697 +};
13698 +
13699 +struct hn_job_args {
13700 +       unsigned int flags;
13701 +       struct inode *inode, *h_inode, *dir, *h_dir;
13702 +       struct dentry *dentry;
13703 +       char *h_name;
13704 +       int h_nlen;
13705 +};
13706 +
13707 +static int hn_job(struct hn_job_args *a)
13708 +{
13709 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
13710 +
13711 +       /* reset xino */
13712 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
13713 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
13714 +
13715 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
13716 +           && a->inode
13717 +           && a->h_inode) {
13718 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
13719 +               if (!a->h_inode->i_nlink)
13720 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
13721 +               mutex_unlock(&a->h_inode->i_mutex);
13722 +       }
13723 +
13724 +       /* make the generation obsolete */
13725 +       if (au_ftest_hnjob(a->flags, GEN)) {
13726 +               int err = -1;
13727 +               if (a->inode)
13728 +                       err = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
13729 +                                             isdir);
13730 +               if (err && a->dentry)
13731 +                       hn_gen_by_name(a->dentry, isdir);
13732 +               /* ignore this error */
13733 +       }
13734 +
13735 +       /* make dir entries obsolete */
13736 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
13737 +               struct au_vdir *vdir;
13738 +
13739 +               vdir = au_ivdir(a->inode);
13740 +               if (vdir)
13741 +                       vdir->vd_jiffy = 0;
13742 +               /* IMustLock(a->inode); */
13743 +               /* a->inode->i_version++; */
13744 +       }
13745 +
13746 +       /* can do nothing but warn */
13747 +       if (au_ftest_hnjob(a->flags, MNTPNT)
13748 +           && a->dentry
13749 +           && d_mountpoint(a->dentry))
13750 +               pr_warn("mount-point %.*s is removed or renamed\n",
13751 +                       AuDLNPair(a->dentry));
13752 +
13753 +       return 0;
13754 +}
13755 +
13756 +/* ---------------------------------------------------------------------- */
13757 +
13758 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
13759 +                                          struct inode *dir)
13760 +{
13761 +       struct dentry *dentry, *d, *parent;
13762 +       struct qstr *dname;
13763 +
13764 +       parent = d_find_alias(dir);
13765 +       if (!parent)
13766 +               return NULL;
13767 +
13768 +       dentry = NULL;
13769 +       spin_lock(&parent->d_lock);
13770 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
13771 +               /* AuDbg("%.*s\n", AuDLNPair(d)); */
13772 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
13773 +               dname = &d->d_name;
13774 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
13775 +                       goto cont_unlock;
13776 +               if (au_di(d))
13777 +                       au_digen_dec(d);
13778 +               else
13779 +                       goto cont_unlock;
13780 +               if (d->d_count) {
13781 +                       dentry = dget_dlock(d);
13782 +                       spin_unlock(&d->d_lock);
13783 +                       break;
13784 +               }
13785 +
13786 +       cont_unlock:
13787 +               spin_unlock(&d->d_lock);
13788 +       }
13789 +       spin_unlock(&parent->d_lock);
13790 +       dput(parent);
13791 +
13792 +       if (dentry)
13793 +               di_write_lock_child(dentry);
13794 +
13795 +       return dentry;
13796 +}
13797 +
13798 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
13799 +                                        aufs_bindex_t bindex, ino_t h_ino)
13800 +{
13801 +       struct inode *inode;
13802 +       ino_t ino;
13803 +       int err;
13804 +
13805 +       inode = NULL;
13806 +       err = au_xino_read(sb, bindex, h_ino, &ino);
13807 +       if (!err && ino)
13808 +               inode = ilookup(sb, ino);
13809 +       if (!inode)
13810 +               goto out;
13811 +
13812 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13813 +               pr_warn("wrong root branch\n");
13814 +               iput(inode);
13815 +               inode = NULL;
13816 +               goto out;
13817 +       }
13818 +
13819 +       ii_write_lock_child(inode);
13820 +
13821 +out:
13822 +       return inode;
13823 +}
13824 +
13825 +static void au_hn_bh(void *_args)
13826 +{
13827 +       struct au_hnotify_args *a = _args;
13828 +       struct super_block *sb;
13829 +       aufs_bindex_t bindex, bend, bfound;
13830 +       unsigned char xino, try_iput;
13831 +       int err;
13832 +       struct inode *inode;
13833 +       ino_t h_ino;
13834 +       struct hn_job_args args;
13835 +       struct dentry *dentry;
13836 +       struct au_sbinfo *sbinfo;
13837 +
13838 +       AuDebugOn(!_args);
13839 +       AuDebugOn(!a->h_dir);
13840 +       AuDebugOn(!a->dir);
13841 +       AuDebugOn(!a->mask);
13842 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
13843 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
13844 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
13845 +
13846 +       inode = NULL;
13847 +       dentry = NULL;
13848 +       /*
13849 +        * do not lock a->dir->i_mutex here
13850 +        * because of d_revalidate() may cause a deadlock.
13851 +        */
13852 +       sb = a->dir->i_sb;
13853 +       AuDebugOn(!sb);
13854 +       sbinfo = au_sbi(sb);
13855 +       AuDebugOn(!sbinfo);
13856 +       si_write_lock(sb, AuLock_NOPLMW);
13857 +
13858 +       ii_read_lock_parent(a->dir);
13859 +       bfound = -1;
13860 +       bend = au_ibend(a->dir);
13861 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
13862 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
13863 +                       bfound = bindex;
13864 +                       break;
13865 +               }
13866 +       ii_read_unlock(a->dir);
13867 +       if (unlikely(bfound < 0))
13868 +               goto out;
13869 +
13870 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
13871 +       h_ino = 0;
13872 +       if (a->h_child_inode)
13873 +               h_ino = a->h_child_inode->i_ino;
13874 +
13875 +       if (a->h_child_nlen
13876 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
13877 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
13878 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
13879 +                                             a->dir);
13880 +       try_iput = 0;
13881 +       if (dentry)
13882 +               inode = dentry->d_inode;
13883 +       if (xino && !inode && h_ino
13884 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
13885 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
13886 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
13887 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
13888 +               try_iput = 1;
13889 +           }
13890 +
13891 +       args.flags = a->flags[AuHn_CHILD];
13892 +       args.dentry = dentry;
13893 +       args.inode = inode;
13894 +       args.h_inode = a->h_child_inode;
13895 +       args.dir = a->dir;
13896 +       args.h_dir = a->h_dir;
13897 +       args.h_name = a->h_child_name;
13898 +       args.h_nlen = a->h_child_nlen;
13899 +       err = hn_job(&args);
13900 +       if (dentry) {
13901 +               if (au_di(dentry))
13902 +                       di_write_unlock(dentry);
13903 +               dput(dentry);
13904 +       }
13905 +       if (inode && try_iput) {
13906 +               ii_write_unlock(inode);
13907 +               iput(inode);
13908 +       }
13909 +
13910 +       ii_write_lock_parent(a->dir);
13911 +       args.flags = a->flags[AuHn_PARENT];
13912 +       args.dentry = NULL;
13913 +       args.inode = a->dir;
13914 +       args.h_inode = a->h_dir;
13915 +       args.dir = NULL;
13916 +       args.h_dir = NULL;
13917 +       args.h_name = NULL;
13918 +       args.h_nlen = 0;
13919 +       err = hn_job(&args);
13920 +       ii_write_unlock(a->dir);
13921 +
13922 +out:
13923 +       iput(a->h_child_inode);
13924 +       iput(a->h_dir);
13925 +       iput(a->dir);
13926 +       si_write_unlock(sb);
13927 +       au_nwt_done(&sbinfo->si_nowait);
13928 +       kfree(a);
13929 +}
13930 +
13931 +/* ---------------------------------------------------------------------- */
13932 +
13933 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
13934 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
13935 +{
13936 +       int err, len;
13937 +       unsigned int flags[AuHnLast], f;
13938 +       unsigned char isdir, isroot, wh;
13939 +       struct inode *dir;
13940 +       struct au_hnotify_args *args;
13941 +       char *p, *h_child_name;
13942 +
13943 +       err = 0;
13944 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
13945 +       dir = igrab(hnotify->hn_aufs_inode);
13946 +       if (!dir)
13947 +               goto out;
13948 +
13949 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
13950 +       wh = 0;
13951 +       h_child_name = (void *)h_child_qstr->name;
13952 +       len = h_child_qstr->len;
13953 +       if (h_child_name) {
13954 +               if (len > AUFS_WH_PFX_LEN
13955 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
13956 +                       h_child_name += AUFS_WH_PFX_LEN;
13957 +                       len -= AUFS_WH_PFX_LEN;
13958 +                       wh = 1;
13959 +               }
13960 +       }
13961 +
13962 +       isdir = 0;
13963 +       if (h_child_inode)
13964 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
13965 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
13966 +       flags[AuHn_CHILD] = 0;
13967 +       if (isdir)
13968 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
13969 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
13970 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
13971 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
13972 +       case FS_MOVED_FROM:
13973 +       case FS_MOVED_TO:
13974 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
13975 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
13976 +               /*FALLTHROUGH*/
13977 +       case FS_CREATE:
13978 +               AuDebugOn(!h_child_name || !h_child_inode);
13979 +               break;
13980 +
13981 +       case FS_DELETE:
13982 +               /*
13983 +                * aufs never be able to get this child inode.
13984 +                * revalidation should be in d_revalidate()
13985 +                * by checking i_nlink, i_generation or d_unhashed().
13986 +                */
13987 +               AuDebugOn(!h_child_name);
13988 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
13989 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
13990 +               break;
13991 +
13992 +       default:
13993 +               AuDebugOn(1);
13994 +       }
13995 +
13996 +       if (wh)
13997 +               h_child_inode = NULL;
13998 +
13999 +       err = -ENOMEM;
14000 +       /* iput() and kfree() will be called in au_hnotify() */
14001 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
14002 +       if (unlikely(!args)) {
14003 +               AuErr1("no memory\n");
14004 +               iput(dir);
14005 +               goto out;
14006 +       }
14007 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
14008 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
14009 +       args->mask = mask;
14010 +       args->dir = dir;
14011 +       args->h_dir = igrab(h_dir);
14012 +       if (h_child_inode)
14013 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
14014 +       args->h_child_inode = h_child_inode;
14015 +       args->h_child_nlen = len;
14016 +       if (len) {
14017 +               p = (void *)args;
14018 +               p += sizeof(*args);
14019 +               memcpy(p, h_child_name, len);
14020 +               p[len] = 0;
14021 +       }
14022 +
14023 +       f = 0;
14024 +       if (!dir->i_nlink)
14025 +               f = AuWkq_NEST;
14026 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
14027 +       if (unlikely(err)) {
14028 +               pr_err("wkq %d\n", err);
14029 +               iput(args->h_child_inode);
14030 +               iput(args->h_dir);
14031 +               iput(args->dir);
14032 +               kfree(args);
14033 +       }
14034 +
14035 +out:
14036 +       return err;
14037 +}
14038 +
14039 +/* ---------------------------------------------------------------------- */
14040 +
14041 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
14042 +{
14043 +       int err;
14044 +
14045 +       AuDebugOn(!(udba & AuOptMask_UDBA));
14046 +
14047 +       err = 0;
14048 +       if (au_hnotify_op.reset_br)
14049 +               err = au_hnotify_op.reset_br(udba, br, perm);
14050 +
14051 +       return err;
14052 +}
14053 +
14054 +int au_hnotify_init_br(struct au_branch *br, int perm)
14055 +{
14056 +       int err;
14057 +
14058 +       err = 0;
14059 +       if (au_hnotify_op.init_br)
14060 +               err = au_hnotify_op.init_br(br, perm);
14061 +
14062 +       return err;
14063 +}
14064 +
14065 +void au_hnotify_fin_br(struct au_branch *br)
14066 +{
14067 +       if (au_hnotify_op.fin_br)
14068 +               au_hnotify_op.fin_br(br);
14069 +}
14070 +
14071 +static void au_hn_destroy_cache(void)
14072 +{
14073 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
14074 +       au_cachep[AuCache_HNOTIFY] = NULL;
14075 +}
14076 +
14077 +int __init au_hnotify_init(void)
14078 +{
14079 +       int err;
14080 +
14081 +       err = -ENOMEM;
14082 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
14083 +       if (au_cachep[AuCache_HNOTIFY]) {
14084 +               err = 0;
14085 +               if (au_hnotify_op.init)
14086 +                       err = au_hnotify_op.init();
14087 +               if (unlikely(err))
14088 +                       au_hn_destroy_cache();
14089 +       }
14090 +       AuTraceErr(err);
14091 +       return err;
14092 +}
14093 +
14094 +void au_hnotify_fin(void)
14095 +{
14096 +       if (au_hnotify_op.fin)
14097 +               au_hnotify_op.fin();
14098 +       /* cf. au_cache_fin() */
14099 +       if (au_cachep[AuCache_HNOTIFY])
14100 +               au_hn_destroy_cache();
14101 +}
14102 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
14103 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
14104 +++ linux/fs/aufs/iinfo.c       2013-07-06 13:20:47.750198454 +0200
14105 @@ -0,0 +1,276 @@
14106 +/*
14107 + * Copyright (C) 2005-2013 Junjiro R. Okajima
14108 + *
14109 + * This program, aufs is free software; you can redistribute it and/or modify
14110 + * it under the terms of the GNU General Public License as published by
14111 + * the Free Software Foundation; either version 2 of the License, or
14112 + * (at your option) any later version.
14113 + *
14114 + * This program is distributed in the hope that it will be useful,
14115 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14116 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14117 + * GNU General Public License for more details.
14118 + *
14119 + * You should have received a copy of the GNU General Public License
14120 + * along with this program; if not, write to the Free Software
14121 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14122 + */
14123 +
14124 +/*
14125 + * inode private data
14126 + */
14127 +
14128 +#include "aufs.h"
14129 +
14130 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
14131 +{
14132 +       struct inode *h_inode;
14133 +
14134 +       IiMustAnyLock(inode);
14135 +
14136 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
14137 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
14138 +       return h_inode;
14139 +}
14140 +
14141 +/* todo: hard/soft set? */
14142 +void au_hiput(struct au_hinode *hinode)
14143 +{
14144 +       au_hn_free(hinode);
14145 +       dput(hinode->hi_whdentry);
14146 +       iput(hinode->hi_inode);
14147 +}
14148 +
14149 +unsigned int au_hi_flags(struct inode *inode, int isdir)
14150 +{
14151 +       unsigned int flags;
14152 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
14153 +
14154 +       flags = 0;
14155 +       if (au_opt_test(mnt_flags, XINO))
14156 +               au_fset_hi(flags, XINO);
14157 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
14158 +               au_fset_hi(flags, HNOTIFY);
14159 +       return flags;
14160 +}
14161 +
14162 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
14163 +                  struct inode *h_inode, unsigned int flags)
14164 +{
14165 +       struct au_hinode *hinode;
14166 +       struct inode *hi;
14167 +       struct au_iinfo *iinfo = au_ii(inode);
14168 +
14169 +       IiMustWriteLock(inode);
14170 +
14171 +       hinode = iinfo->ii_hinode + bindex;
14172 +       hi = hinode->hi_inode;
14173 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
14174 +
14175 +       if (hi)
14176 +               au_hiput(hinode);
14177 +       hinode->hi_inode = h_inode;
14178 +       if (h_inode) {
14179 +               int err;
14180 +               struct super_block *sb = inode->i_sb;
14181 +               struct au_branch *br;
14182 +
14183 +               AuDebugOn(inode->i_mode
14184 +                         && (h_inode->i_mode & S_IFMT)
14185 +                         != (inode->i_mode & S_IFMT));
14186 +               if (bindex == iinfo->ii_bstart)
14187 +                       au_cpup_igen(inode, h_inode);
14188 +               br = au_sbr(sb, bindex);
14189 +               hinode->hi_id = br->br_id;
14190 +               if (au_ftest_hi(flags, XINO)) {
14191 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
14192 +                                           inode->i_ino);
14193 +                       if (unlikely(err))
14194 +                               AuIOErr1("failed au_xino_write() %d\n", err);
14195 +               }
14196 +
14197 +               if (au_ftest_hi(flags, HNOTIFY)
14198 +                   && au_br_hnotifyable(br->br_perm)) {
14199 +                       err = au_hn_alloc(hinode, inode);
14200 +                       if (unlikely(err))
14201 +                               AuIOErr1("au_hn_alloc() %d\n", err);
14202 +               }
14203 +       }
14204 +}
14205 +
14206 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
14207 +                 struct dentry *h_wh)
14208 +{
14209 +       struct au_hinode *hinode;
14210 +
14211 +       IiMustWriteLock(inode);
14212 +
14213 +       hinode = au_ii(inode)->ii_hinode + bindex;
14214 +       AuDebugOn(hinode->hi_whdentry);
14215 +       hinode->hi_whdentry = h_wh;
14216 +}
14217 +
14218 +void au_update_iigen(struct inode *inode, int half)
14219 +{
14220 +       struct au_iinfo *iinfo;
14221 +       struct au_iigen *iigen;
14222 +       unsigned int sigen;
14223 +
14224 +       sigen = au_sigen(inode->i_sb);
14225 +       iinfo = au_ii(inode);
14226 +       iigen = &iinfo->ii_generation;
14227 +       spin_lock(&iinfo->ii_genspin);
14228 +       iigen->ig_generation = sigen;
14229 +       if (half)
14230 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
14231 +       else
14232 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
14233 +       spin_unlock(&iinfo->ii_genspin);
14234 +}
14235 +
14236 +/* it may be called at remount time, too */
14237 +void au_update_ibrange(struct inode *inode, int do_put_zero)
14238 +{
14239 +       struct au_iinfo *iinfo;
14240 +       aufs_bindex_t bindex, bend;
14241 +
14242 +       iinfo = au_ii(inode);
14243 +       if (!iinfo)
14244 +               return;
14245 +
14246 +       IiMustWriteLock(inode);
14247 +
14248 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
14249 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
14250 +                    bindex++) {
14251 +                       struct inode *h_i;
14252 +
14253 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
14254 +                       if (h_i && !h_i->i_nlink)
14255 +                               au_set_h_iptr(inode, bindex, NULL, 0);
14256 +               }
14257 +       }
14258 +
14259 +       iinfo->ii_bstart = -1;
14260 +       iinfo->ii_bend = -1;
14261 +       bend = au_sbend(inode->i_sb);
14262 +       for (bindex = 0; bindex <= bend; bindex++)
14263 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
14264 +                       iinfo->ii_bstart = bindex;
14265 +                       break;
14266 +               }
14267 +       if (iinfo->ii_bstart >= 0)
14268 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
14269 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
14270 +                               iinfo->ii_bend = bindex;
14271 +                               break;
14272 +                       }
14273 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
14274 +}
14275 +
14276 +/* ---------------------------------------------------------------------- */
14277 +
14278 +void au_icntnr_init_once(void *_c)
14279 +{
14280 +       struct au_icntnr *c = _c;
14281 +       struct au_iinfo *iinfo = &c->iinfo;
14282 +       static struct lock_class_key aufs_ii;
14283 +
14284 +       spin_lock_init(&iinfo->ii_genspin);
14285 +       au_rw_init(&iinfo->ii_rwsem);
14286 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
14287 +       inode_init_once(&c->vfs_inode);
14288 +}
14289 +
14290 +int au_iinfo_init(struct inode *inode)
14291 +{
14292 +       struct au_iinfo *iinfo;
14293 +       struct super_block *sb;
14294 +       int nbr, i;
14295 +
14296 +       sb = inode->i_sb;
14297 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
14298 +       nbr = au_sbend(sb) + 1;
14299 +       if (unlikely(nbr <= 0))
14300 +               nbr = 1;
14301 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
14302 +       if (iinfo->ii_hinode) {
14303 +               au_ninodes_inc(sb);
14304 +               for (i = 0; i < nbr; i++)
14305 +                       iinfo->ii_hinode[i].hi_id = -1;
14306 +
14307 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
14308 +               iinfo->ii_bstart = -1;
14309 +               iinfo->ii_bend = -1;
14310 +               iinfo->ii_vdir = NULL;
14311 +               return 0;
14312 +       }
14313 +       return -ENOMEM;
14314 +}
14315 +
14316 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
14317 +{
14318 +       int err, sz;
14319 +       struct au_hinode *hip;
14320 +
14321 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
14322 +
14323 +       err = -ENOMEM;
14324 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
14325 +       if (!sz)
14326 +               sz = sizeof(*hip);
14327 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
14328 +       if (hip) {
14329 +               iinfo->ii_hinode = hip;
14330 +               err = 0;
14331 +       }
14332 +
14333 +       return err;
14334 +}
14335 +
14336 +void au_iinfo_fin(struct inode *inode)
14337 +{
14338 +       struct au_iinfo *iinfo;
14339 +       struct au_hinode *hi;
14340 +       struct super_block *sb;
14341 +       aufs_bindex_t bindex, bend;
14342 +       const unsigned char unlinked = !inode->i_nlink;
14343 +
14344 +       iinfo = au_ii(inode);
14345 +       /* bad_inode case */
14346 +       if (!iinfo)
14347 +               return;
14348 +
14349 +       sb = inode->i_sb;
14350 +       au_ninodes_dec(sb);
14351 +       if (si_pid_test(sb))
14352 +               au_xino_delete_inode(inode, unlinked);
14353 +       else {
14354 +               /*
14355 +                * it is safe to hide the dependency between sbinfo and
14356 +                * sb->s_umount.
14357 +                */
14358 +               lockdep_off();
14359 +               si_noflush_read_lock(sb);
14360 +               au_xino_delete_inode(inode, unlinked);
14361 +               si_read_unlock(sb);
14362 +               lockdep_on();
14363 +       }
14364 +
14365 +       if (iinfo->ii_vdir)
14366 +               au_vdir_free(iinfo->ii_vdir);
14367 +
14368 +       bindex = iinfo->ii_bstart;
14369 +       if (bindex >= 0) {
14370 +               hi = iinfo->ii_hinode + bindex;
14371 +               bend = iinfo->ii_bend;
14372 +               while (bindex++ <= bend) {
14373 +                       if (hi->hi_inode)
14374 +                               au_hiput(hi);
14375 +                       hi++;
14376 +               }
14377 +       }
14378 +       kfree(iinfo->ii_hinode);
14379 +       iinfo->ii_hinode = NULL;
14380 +       AuRwDestroy(&iinfo->ii_rwsem);
14381 +}
14382 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
14383 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
14384 +++ linux/fs/aufs/inode.c       2013-07-06 13:20:47.750198454 +0200
14385 @@ -0,0 +1,492 @@
14386 +/*
14387 + * Copyright (C) 2005-2013 Junjiro R. Okajima
14388 + *
14389 + * This program, aufs is free software; you can redistribute it and/or modify
14390 + * it under the terms of the GNU General Public License as published by
14391 + * the Free Software Foundation; either version 2 of the License, or
14392 + * (at your option) any later version.
14393 + *
14394 + * This program is distributed in the hope that it will be useful,
14395 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14396 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14397 + * GNU General Public License for more details.
14398 + *
14399 + * You should have received a copy of the GNU General Public License
14400 + * along with this program; if not, write to the Free Software
14401 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14402 + */
14403 +
14404 +/*
14405 + * inode functions
14406 + */
14407 +
14408 +#include "aufs.h"
14409 +
14410 +struct inode *au_igrab(struct inode *inode)
14411 +{
14412 +       if (inode) {
14413 +               AuDebugOn(!atomic_read(&inode->i_count));
14414 +               ihold(inode);
14415 +       }
14416 +       return inode;
14417 +}
14418 +
14419 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
14420 +{
14421 +       au_cpup_attr_all(inode, /*force*/0);
14422 +       au_update_iigen(inode, /*half*/1);
14423 +       if (do_version)
14424 +               inode->i_version++;
14425 +}
14426 +
14427 +static int au_ii_refresh(struct inode *inode, int *update)
14428 +{
14429 +       int err, e;
14430 +       umode_t type;
14431 +       aufs_bindex_t bindex, new_bindex;
14432 +       struct super_block *sb;
14433 +       struct au_iinfo *iinfo;
14434 +       struct au_hinode *p, *q, tmp;
14435 +
14436 +       IiMustWriteLock(inode);
14437 +
14438 +       *update = 0;
14439 +       sb = inode->i_sb;
14440 +       type = inode->i_mode & S_IFMT;
14441 +       iinfo = au_ii(inode);
14442 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
14443 +       if (unlikely(err))
14444 +               goto out;
14445 +
14446 +       AuDebugOn(iinfo->ii_bstart < 0);
14447 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
14448 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
14449 +            bindex++, p++) {
14450 +               if (!p->hi_inode)
14451 +                       continue;
14452 +
14453 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
14454 +               new_bindex = au_br_index(sb, p->hi_id);
14455 +               if (new_bindex == bindex)
14456 +                       continue;
14457 +
14458 +               if (new_bindex < 0) {
14459 +                       *update = 1;
14460 +                       au_hiput(p);
14461 +                       p->hi_inode = NULL;
14462 +                       continue;
14463 +               }
14464 +
14465 +               if (new_bindex < iinfo->ii_bstart)
14466 +                       iinfo->ii_bstart = new_bindex;
14467 +               if (iinfo->ii_bend < new_bindex)
14468 +                       iinfo->ii_bend = new_bindex;
14469 +               /* swap two lower inode, and loop again */
14470 +               q = iinfo->ii_hinode + new_bindex;
14471 +               tmp = *q;
14472 +               *q = *p;
14473 +               *p = tmp;
14474 +               if (tmp.hi_inode) {
14475 +                       bindex--;
14476 +                       p--;
14477 +               }
14478 +       }
14479 +       au_update_ibrange(inode, /*do_put_zero*/0);
14480 +       e = au_dy_irefresh(inode);
14481 +       if (unlikely(e && !err))
14482 +               err = e;
14483 +
14484 +out:
14485 +       AuTraceErr(err);
14486 +       return err;
14487 +}
14488 +
14489 +int au_refresh_hinode_self(struct inode *inode)
14490 +{
14491 +       int err, update;
14492 +
14493 +       err = au_ii_refresh(inode, &update);
14494 +       if (!err)
14495 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
14496 +
14497 +       AuTraceErr(err);
14498 +       return err;
14499 +}
14500 +
14501 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
14502 +{
14503 +       int err, e, update;
14504 +       unsigned int flags;
14505 +       umode_t mode;
14506 +       aufs_bindex_t bindex, bend;
14507 +       unsigned char isdir;
14508 +       struct au_hinode *p;
14509 +       struct au_iinfo *iinfo;
14510 +
14511 +       err = au_ii_refresh(inode, &update);
14512 +       if (unlikely(err))
14513 +               goto out;
14514 +
14515 +       update = 0;
14516 +       iinfo = au_ii(inode);
14517 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
14518 +       mode = (inode->i_mode & S_IFMT);
14519 +       isdir = S_ISDIR(mode);
14520 +       flags = au_hi_flags(inode, isdir);
14521 +       bend = au_dbend(dentry);
14522 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
14523 +               struct inode *h_i;
14524 +               struct dentry *h_d;
14525 +
14526 +               h_d = au_h_dptr(dentry, bindex);
14527 +               if (!h_d || !h_d->d_inode)
14528 +                       continue;
14529 +
14530 +               AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
14531 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
14532 +                       h_i = au_h_iptr(inode, bindex);
14533 +                       if (h_i) {
14534 +                               if (h_i == h_d->d_inode)
14535 +                                       continue;
14536 +                               err = -EIO;
14537 +                               break;
14538 +                       }
14539 +               }
14540 +               if (bindex < iinfo->ii_bstart)
14541 +                       iinfo->ii_bstart = bindex;
14542 +               if (iinfo->ii_bend < bindex)
14543 +                       iinfo->ii_bend = bindex;
14544 +               au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
14545 +               update = 1;
14546 +       }
14547 +       au_update_ibrange(inode, /*do_put_zero*/0);
14548 +       e = au_dy_irefresh(inode);
14549 +       if (unlikely(e && !err))
14550 +               err = e;
14551 +       if (!err)
14552 +               au_refresh_hinode_attr(inode, update && isdir);
14553 +
14554 +out:
14555 +       AuTraceErr(err);
14556 +       return err;
14557 +}
14558 +
14559 +static int set_inode(struct inode *inode, struct dentry *dentry)
14560 +{
14561 +       int err;
14562 +       unsigned int flags;
14563 +       umode_t mode;
14564 +       aufs_bindex_t bindex, bstart, btail;
14565 +       unsigned char isdir;
14566 +       struct dentry *h_dentry;
14567 +       struct inode *h_inode;
14568 +       struct au_iinfo *iinfo;
14569 +
14570 +       IiMustWriteLock(inode);
14571 +
14572 +       err = 0;
14573 +       isdir = 0;
14574 +       bstart = au_dbstart(dentry);
14575 +       h_inode = au_h_dptr(dentry, bstart)->d_inode;
14576 +       mode = h_inode->i_mode;
14577 +       switch (mode & S_IFMT) {
14578 +       case S_IFREG:
14579 +               btail = au_dbtail(dentry);
14580 +               inode->i_op = &aufs_iop;
14581 +               inode->i_fop = &aufs_file_fop;
14582 +               err = au_dy_iaop(inode, bstart, h_inode);
14583 +               if (unlikely(err))
14584 +                       goto out;
14585 +               break;
14586 +       case S_IFDIR:
14587 +               isdir = 1;
14588 +               btail = au_dbtaildir(dentry);
14589 +               inode->i_op = &aufs_dir_iop;
14590 +               inode->i_fop = &aufs_dir_fop;
14591 +               break;
14592 +       case S_IFLNK:
14593 +               btail = au_dbtail(dentry);
14594 +               inode->i_op = &aufs_symlink_iop;
14595 +               break;
14596 +       case S_IFBLK:
14597 +       case S_IFCHR:
14598 +       case S_IFIFO:
14599 +       case S_IFSOCK:
14600 +               btail = au_dbtail(dentry);
14601 +               inode->i_op = &aufs_iop;
14602 +               au_init_special_fop(inode, mode, h_inode->i_rdev);
14603 +               break;
14604 +       default:
14605 +               AuIOErr("Unknown file type 0%o\n", mode);
14606 +               err = -EIO;
14607 +               goto out;
14608 +       }
14609 +
14610 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
14611 +       flags = au_hi_flags(inode, isdir);
14612 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
14613 +           && au_ftest_hi(flags, HNOTIFY)
14614 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
14615 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
14616 +               au_fclr_hi(flags, HNOTIFY);
14617 +       iinfo = au_ii(inode);
14618 +       iinfo->ii_bstart = bstart;
14619 +       iinfo->ii_bend = btail;
14620 +       for (bindex = bstart; bindex <= btail; bindex++) {
14621 +               h_dentry = au_h_dptr(dentry, bindex);
14622 +               if (h_dentry)
14623 +                       au_set_h_iptr(inode, bindex,
14624 +                                     au_igrab(h_dentry->d_inode), flags);
14625 +       }
14626 +       au_cpup_attr_all(inode, /*force*/1);
14627 +
14628 +out:
14629 +       return err;
14630 +}
14631 +
14632 +/*
14633 + * successful returns with iinfo write_locked
14634 + * minus: errno
14635 + * zero: success, matched
14636 + * plus: no error, but unmatched
14637 + */
14638 +static int reval_inode(struct inode *inode, struct dentry *dentry)
14639 +{
14640 +       int err;
14641 +       unsigned int gen;
14642 +       struct au_iigen iigen;
14643 +       aufs_bindex_t bindex, bend;
14644 +       struct inode *h_inode, *h_dinode;
14645 +
14646 +       /*
14647 +        * before this function, if aufs got any iinfo lock, it must be only
14648 +        * one, the parent dir.
14649 +        * it can happen by UDBA and the obsoleted inode number.
14650 +        */
14651 +       err = -EIO;
14652 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
14653 +               goto out;
14654 +
14655 +       err = 1;
14656 +       ii_write_lock_new_child(inode);
14657 +       h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
14658 +       bend = au_ibend(inode);
14659 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
14660 +               h_inode = au_h_iptr(inode, bindex);
14661 +               if (!h_inode || h_inode != h_dinode)
14662 +                       continue;
14663 +
14664 +               err = 0;
14665 +               gen = au_iigen(inode, &iigen);
14666 +               if (gen == au_digen(dentry)
14667 +                   && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
14668 +                       break;
14669 +
14670 +               /* fully refresh inode using dentry */
14671 +               err = au_refresh_hinode(inode, dentry);
14672 +               if (!err)
14673 +                       au_update_iigen(inode, /*half*/0);
14674 +               break;
14675 +       }
14676 +
14677 +       if (unlikely(err))
14678 +               ii_write_unlock(inode);
14679 +out:
14680 +       return err;
14681 +}
14682 +
14683 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
14684 +          unsigned int d_type, ino_t *ino)
14685 +{
14686 +       int err;
14687 +       struct mutex *mtx;
14688 +
14689 +       /* prevent hardlinked inode number from race condition */
14690 +       mtx = NULL;
14691 +       if (d_type != DT_DIR) {
14692 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
14693 +               mutex_lock(mtx);
14694 +       }
14695 +       err = au_xino_read(sb, bindex, h_ino, ino);
14696 +       if (unlikely(err))
14697 +               goto out;
14698 +
14699 +       if (!*ino) {
14700 +               err = -EIO;
14701 +               *ino = au_xino_new_ino(sb);
14702 +               if (unlikely(!*ino))
14703 +                       goto out;
14704 +               err = au_xino_write(sb, bindex, h_ino, *ino);
14705 +               if (unlikely(err))
14706 +                       goto out;
14707 +       }
14708 +
14709 +out:
14710 +       if (mtx)
14711 +               mutex_unlock(mtx);
14712 +       return err;
14713 +}
14714 +
14715 +/* successful returns with iinfo write_locked */
14716 +/* todo: return with unlocked? */
14717 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
14718 +{
14719 +       struct inode *inode, *h_inode;
14720 +       struct dentry *h_dentry;
14721 +       struct super_block *sb;
14722 +       struct mutex *mtx;
14723 +       ino_t h_ino, ino;
14724 +       int err;
14725 +       aufs_bindex_t bstart;
14726 +
14727 +       sb = dentry->d_sb;
14728 +       bstart = au_dbstart(dentry);
14729 +       h_dentry = au_h_dptr(dentry, bstart);
14730 +       h_inode = h_dentry->d_inode;
14731 +       h_ino = h_inode->i_ino;
14732 +
14733 +       /*
14734 +        * stop 'race'-ing between hardlinks under different
14735 +        * parents.
14736 +        */
14737 +       mtx = NULL;
14738 +       if (!S_ISDIR(h_inode->i_mode))
14739 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
14740 +
14741 +new_ino:
14742 +       if (mtx)
14743 +               mutex_lock(mtx);
14744 +       err = au_xino_read(sb, bstart, h_ino, &ino);
14745 +       inode = ERR_PTR(err);
14746 +       if (unlikely(err))
14747 +               goto out;
14748 +
14749 +       if (!ino) {
14750 +               ino = au_xino_new_ino(sb);
14751 +               if (unlikely(!ino)) {
14752 +                       inode = ERR_PTR(-EIO);
14753 +                       goto out;
14754 +               }
14755 +       }
14756 +
14757 +       AuDbg("i%lu\n", (unsigned long)ino);
14758 +       inode = au_iget_locked(sb, ino);
14759 +       err = PTR_ERR(inode);
14760 +       if (IS_ERR(inode))
14761 +               goto out;
14762 +
14763 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
14764 +       if (inode->i_state & I_NEW) {
14765 +               /* verbose coding for lock class name */
14766 +               if (unlikely(S_ISLNK(h_inode->i_mode)))
14767 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
14768 +                                   au_lc_key + AuLcSymlink_IIINFO);
14769 +               else if (unlikely(S_ISDIR(h_inode->i_mode)))
14770 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
14771 +                                   au_lc_key + AuLcDir_IIINFO);
14772 +               else /* likely */
14773 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
14774 +                                   au_lc_key + AuLcNonDir_IIINFO);
14775 +
14776 +               ii_write_lock_new_child(inode);
14777 +               err = set_inode(inode, dentry);
14778 +               if (!err) {
14779 +                       unlock_new_inode(inode);
14780 +                       goto out; /* success */
14781 +               }
14782 +
14783 +               /*
14784 +                * iget_failed() calls iput(), but we need to call
14785 +                * ii_write_unlock() after iget_failed(). so dirty hack for
14786 +                * i_count.
14787 +                */
14788 +               atomic_inc(&inode->i_count);
14789 +               iget_failed(inode);
14790 +               ii_write_unlock(inode);
14791 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
14792 +               /* ignore this error */
14793 +               goto out_iput;
14794 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
14795 +               /*
14796 +                * horrible race condition between lookup, readdir and copyup
14797 +                * (or something).
14798 +                */
14799 +               if (mtx)
14800 +                       mutex_unlock(mtx);
14801 +               err = reval_inode(inode, dentry);
14802 +               if (unlikely(err < 0)) {
14803 +                       mtx = NULL;
14804 +                       goto out_iput;
14805 +               }
14806 +
14807 +               if (!err) {
14808 +                       mtx = NULL;
14809 +                       goto out; /* success */
14810 +               } else if (mtx)
14811 +                       mutex_lock(mtx);
14812 +       }
14813 +
14814 +       if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
14815 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
14816 +                       " b%d, %s, %.*s, hi%lu, i%lu.\n",
14817 +                       bstart, au_sbtype(h_dentry->d_sb), AuDLNPair(dentry),
14818 +                       (unsigned long)h_ino, (unsigned long)ino);
14819 +       ino = 0;
14820 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
14821 +       if (!err) {
14822 +               iput(inode);
14823 +               if (mtx)
14824 +                       mutex_unlock(mtx);
14825 +               goto new_ino;
14826 +       }
14827 +
14828 +out_iput:
14829 +       iput(inode);
14830 +       inode = ERR_PTR(err);
14831 +out:
14832 +       if (mtx)
14833 +               mutex_unlock(mtx);
14834 +       return inode;
14835 +}
14836 +
14837 +/* ---------------------------------------------------------------------- */
14838 +
14839 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
14840 +              struct inode *inode)
14841 +{
14842 +       int err;
14843 +
14844 +       err = au_br_rdonly(au_sbr(sb, bindex));
14845 +
14846 +       /* pseudo-link after flushed may happen out of bounds */
14847 +       if (!err
14848 +           && inode
14849 +           && au_ibstart(inode) <= bindex
14850 +           && bindex <= au_ibend(inode)) {
14851 +               /*
14852 +                * permission check is unnecessary since vfsub routine
14853 +                * will be called later
14854 +                */
14855 +               struct inode *hi = au_h_iptr(inode, bindex);
14856 +               if (hi)
14857 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
14858 +       }
14859 +
14860 +       return err;
14861 +}
14862 +
14863 +int au_test_h_perm(struct inode *h_inode, int mask)
14864 +{
14865 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
14866 +               return 0;
14867 +       return inode_permission(h_inode, mask);
14868 +}
14869 +
14870 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
14871 +{
14872 +       if (au_test_nfs(h_inode->i_sb)
14873 +           && (mask & MAY_WRITE)
14874 +           && S_ISDIR(h_inode->i_mode))
14875 +               mask |= MAY_READ; /* force permission check */
14876 +       return au_test_h_perm(h_inode, mask);
14877 +}
14878 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
14879 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
14880 +++ linux/fs/aufs/inode.h       2013-07-06 13:20:47.750198454 +0200
14881 @@ -0,0 +1,600 @@
14882 +/*
14883 + * Copyright (C) 2005-2013 Junjiro R. Okajima
14884 + *
14885 + * This program, aufs is free software; you can redistribute it and/or modify
14886 + * it under the terms of the GNU General Public License as published by
14887 + * the Free Software Foundation; either version 2 of the License, or
14888 + * (at your option) any later version.
14889 + *
14890 + * This program is distributed in the hope that it will be useful,
14891 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14892 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14893 + * GNU General Public License for more details.
14894 + *
14895 + * You should have received a copy of the GNU General Public License
14896 + * along with this program; if not, write to the Free Software
14897 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14898 + */
14899 +
14900 +/*
14901 + * inode operations
14902 + */
14903 +
14904 +#ifndef __AUFS_INODE_H__
14905 +#define __AUFS_INODE_H__
14906 +
14907 +#ifdef __KERNEL__
14908 +
14909 +#include <linux/fsnotify.h>
14910 +#include "rwsem.h"
14911 +
14912 +struct vfsmount;
14913 +
14914 +struct au_hnotify {
14915 +#ifdef CONFIG_AUFS_HNOTIFY
14916 +#ifdef CONFIG_AUFS_HFSNOTIFY
14917 +       /* never use fsnotify_add_vfsmount_mark() */
14918 +       struct fsnotify_mark            hn_mark;
14919 +#endif
14920 +       struct inode                    *hn_aufs_inode; /* no get/put */
14921 +#endif
14922 +} ____cacheline_aligned_in_smp;
14923 +
14924 +struct au_hinode {
14925 +       struct inode            *hi_inode;
14926 +       aufs_bindex_t           hi_id;
14927 +#ifdef CONFIG_AUFS_HNOTIFY
14928 +       struct au_hnotify       *hi_notify;
14929 +#endif
14930 +
14931 +       /* reference to the copied-up whiteout with get/put */
14932 +       struct dentry           *hi_whdentry;
14933 +};
14934 +
14935 +/* ig_flags */
14936 +#define AuIG_HALF_REFRESHED            1
14937 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
14938 +#define au_ig_fset(flags, name) \
14939 +       do { (flags) |= AuIG_##name; } while (0)
14940 +#define au_ig_fclr(flags, name) \
14941 +       do { (flags) &= ~AuIG_##name; } while (0)
14942 +
14943 +struct au_iigen {
14944 +       __u32           ig_generation, ig_flags;
14945 +};
14946 +
14947 +struct au_vdir;
14948 +struct au_iinfo {
14949 +       spinlock_t              ii_genspin;
14950 +       struct au_iigen         ii_generation;
14951 +       struct super_block      *ii_hsb1;       /* no get/put */
14952 +
14953 +       struct au_rwsem         ii_rwsem;
14954 +       aufs_bindex_t           ii_bstart, ii_bend;
14955 +       __u32                   ii_higen;
14956 +       struct au_hinode        *ii_hinode;
14957 +       struct au_vdir          *ii_vdir;
14958 +};
14959 +
14960 +struct au_icntnr {
14961 +       struct au_iinfo iinfo;
14962 +       struct inode vfs_inode;
14963 +} ____cacheline_aligned_in_smp;
14964 +
14965 +/* au_pin flags */
14966 +#define AuPin_DI_LOCKED                1
14967 +#define AuPin_MNT_WRITE                (1 << 1)
14968 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
14969 +#define au_fset_pin(flags, name) \
14970 +       do { (flags) |= AuPin_##name; } while (0)
14971 +#define au_fclr_pin(flags, name) \
14972 +       do { (flags) &= ~AuPin_##name; } while (0)
14973 +
14974 +struct au_pin {
14975 +       /* input */
14976 +       struct dentry *dentry;
14977 +       unsigned int udba;
14978 +       unsigned char lsc_di, lsc_hi, flags;
14979 +       aufs_bindex_t bindex;
14980 +
14981 +       /* output */
14982 +       struct dentry *parent;
14983 +       struct au_hinode *hdir;
14984 +       struct vfsmount *h_mnt;
14985 +
14986 +       /* temporary unlock/relock for copyup */
14987 +       struct dentry *h_dentry, *h_parent;
14988 +       struct au_branch *br;
14989 +       struct task_struct *task;
14990 +};
14991 +
14992 +void au_pin_hdir_unlock(struct au_pin *p);
14993 +int au_pin_hdir_relock(struct au_pin *p);
14994 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
14995 +void au_pin_hdir_acquire_nest(struct au_pin *p);
14996 +void au_pin_hdir_release(struct au_pin *p);
14997 +
14998 +/* ---------------------------------------------------------------------- */
14999 +
15000 +static inline struct au_iinfo *au_ii(struct inode *inode)
15001 +{
15002 +       struct au_iinfo *iinfo;
15003 +
15004 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15005 +       if (iinfo->ii_hinode)
15006 +               return iinfo;
15007 +       return NULL; /* debugging bad_inode case */
15008 +}
15009 +
15010 +/* ---------------------------------------------------------------------- */
15011 +
15012 +/* inode.c */
15013 +struct inode *au_igrab(struct inode *inode);
15014 +int au_refresh_hinode_self(struct inode *inode);
15015 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
15016 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
15017 +          unsigned int d_type, ino_t *ino);
15018 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
15019 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
15020 +              struct inode *inode);
15021 +int au_test_h_perm(struct inode *h_inode, int mask);
15022 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
15023 +
15024 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
15025 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
15026 +{
15027 +#ifdef CONFIG_AUFS_SHWH
15028 +       return au_ino(sb, bindex, h_ino, d_type, ino);
15029 +#else
15030 +       return 0;
15031 +#endif
15032 +}
15033 +
15034 +/* i_op.c */
15035 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
15036 +
15037 +/* au_wr_dir flags */
15038 +#define AuWrDir_ADD_ENTRY      1
15039 +#define AuWrDir_TMP_WHENTRY    (1 << 1)
15040 +#define AuWrDir_ISDIR          (1 << 2)
15041 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
15042 +#define au_fset_wrdir(flags, name) \
15043 +       do { (flags) |= AuWrDir_##name; } while (0)
15044 +#define au_fclr_wrdir(flags, name) \
15045 +       do { (flags) &= ~AuWrDir_##name; } while (0)
15046 +
15047 +struct au_wr_dir_args {
15048 +       aufs_bindex_t force_btgt;
15049 +       unsigned char flags;
15050 +};
15051 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
15052 +             struct au_wr_dir_args *args);
15053 +
15054 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
15055 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
15056 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
15057 +                unsigned int udba, unsigned char flags);
15058 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
15059 +          unsigned int udba, unsigned char flags) __must_check;
15060 +int au_do_pin(struct au_pin *pin) __must_check;
15061 +void au_unpin(struct au_pin *pin);
15062 +
15063 +/* i_op_add.c */
15064 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
15065 +              struct dentry *h_parent, int isdir);
15066 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
15067 +              dev_t dev);
15068 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
15069 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
15070 +               bool want_excl);
15071 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
15072 +             struct dentry *dentry);
15073 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
15074 +
15075 +/* i_op_del.c */
15076 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
15077 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
15078 +              struct dentry *h_parent, int isdir);
15079 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
15080 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
15081 +
15082 +/* i_op_ren.c */
15083 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
15084 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
15085 +               struct inode *dir, struct dentry *dentry);
15086 +
15087 +/* iinfo.c */
15088 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
15089 +void au_hiput(struct au_hinode *hinode);
15090 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15091 +                 struct dentry *h_wh);
15092 +unsigned int au_hi_flags(struct inode *inode, int isdir);
15093 +
15094 +/* hinode flags */
15095 +#define AuHi_XINO      1
15096 +#define AuHi_HNOTIFY   (1 << 1)
15097 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
15098 +#define au_fset_hi(flags, name) \
15099 +       do { (flags) |= AuHi_##name; } while (0)
15100 +#define au_fclr_hi(flags, name) \
15101 +       do { (flags) &= ~AuHi_##name; } while (0)
15102 +
15103 +#ifndef CONFIG_AUFS_HNOTIFY
15104 +#undef AuHi_HNOTIFY
15105 +#define AuHi_HNOTIFY   0
15106 +#endif
15107 +
15108 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15109 +                  struct inode *h_inode, unsigned int flags);
15110 +
15111 +void au_update_iigen(struct inode *inode, int half);
15112 +void au_update_ibrange(struct inode *inode, int do_put_zero);
15113 +
15114 +void au_icntnr_init_once(void *_c);
15115 +int au_iinfo_init(struct inode *inode);
15116 +void au_iinfo_fin(struct inode *inode);
15117 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
15118 +
15119 +#ifdef CONFIG_PROC_FS
15120 +/* plink.c */
15121 +int au_plink_maint(struct super_block *sb, int flags);
15122 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
15123 +int au_plink_maint_enter(struct super_block *sb);
15124 +#ifdef CONFIG_AUFS_DEBUG
15125 +void au_plink_list(struct super_block *sb);
15126 +#else
15127 +AuStubVoid(au_plink_list, struct super_block *sb)
15128 +#endif
15129 +int au_plink_test(struct inode *inode);
15130 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
15131 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
15132 +                    struct dentry *h_dentry);
15133 +void au_plink_put(struct super_block *sb, int verbose);
15134 +void au_plink_clean(struct super_block *sb, int verbose);
15135 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
15136 +#else
15137 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
15138 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
15139 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
15140 +AuStubVoid(au_plink_list, struct super_block *sb);
15141 +AuStubInt0(au_plink_test, struct inode *inode);
15142 +AuStub(struct dentry *, au_plink_lkup, return NULL,
15143 +       struct inode *inode, aufs_bindex_t bindex);
15144 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
15145 +          struct dentry *h_dentry);
15146 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
15147 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
15148 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
15149 +#endif /* CONFIG_PROC_FS */
15150 +
15151 +/* ---------------------------------------------------------------------- */
15152 +
15153 +/* lock subclass for iinfo */
15154 +enum {
15155 +       AuLsc_II_CHILD,         /* child first */
15156 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
15157 +       AuLsc_II_CHILD3,        /* copyup dirs */
15158 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
15159 +       AuLsc_II_PARENT2,
15160 +       AuLsc_II_PARENT3,       /* copyup dirs */
15161 +       AuLsc_II_NEW_CHILD
15162 +};
15163 +
15164 +/*
15165 + * ii_read_lock_child, ii_write_lock_child,
15166 + * ii_read_lock_child2, ii_write_lock_child2,
15167 + * ii_read_lock_child3, ii_write_lock_child3,
15168 + * ii_read_lock_parent, ii_write_lock_parent,
15169 + * ii_read_lock_parent2, ii_write_lock_parent2,
15170 + * ii_read_lock_parent3, ii_write_lock_parent3,
15171 + * ii_read_lock_new_child, ii_write_lock_new_child,
15172 + */
15173 +#define AuReadLockFunc(name, lsc) \
15174 +static inline void ii_read_lock_##name(struct inode *i) \
15175 +{ \
15176 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15177 +}
15178 +
15179 +#define AuWriteLockFunc(name, lsc) \
15180 +static inline void ii_write_lock_##name(struct inode *i) \
15181 +{ \
15182 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15183 +}
15184 +
15185 +#define AuRWLockFuncs(name, lsc) \
15186 +       AuReadLockFunc(name, lsc) \
15187 +       AuWriteLockFunc(name, lsc)
15188 +
15189 +AuRWLockFuncs(child, CHILD);
15190 +AuRWLockFuncs(child2, CHILD2);
15191 +AuRWLockFuncs(child3, CHILD3);
15192 +AuRWLockFuncs(parent, PARENT);
15193 +AuRWLockFuncs(parent2, PARENT2);
15194 +AuRWLockFuncs(parent3, PARENT3);
15195 +AuRWLockFuncs(new_child, NEW_CHILD);
15196 +
15197 +#undef AuReadLockFunc
15198 +#undef AuWriteLockFunc
15199 +#undef AuRWLockFuncs
15200 +
15201 +/*
15202 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
15203 + */
15204 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
15205 +
15206 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
15207 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
15208 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
15209 +
15210 +/* ---------------------------------------------------------------------- */
15211 +
15212 +static inline void au_icntnr_init(struct au_icntnr *c)
15213 +{
15214 +#ifdef CONFIG_AUFS_DEBUG
15215 +       c->vfs_inode.i_mode = 0;
15216 +#endif
15217 +}
15218 +
15219 +static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
15220 +{
15221 +       unsigned int gen;
15222 +       struct au_iinfo *iinfo;
15223 +
15224 +       iinfo = au_ii(inode);
15225 +       spin_lock(&iinfo->ii_genspin);
15226 +       if (iigen)
15227 +               *iigen = iinfo->ii_generation;
15228 +       gen = iinfo->ii_generation.ig_generation;
15229 +       spin_unlock(&iinfo->ii_genspin);
15230 +
15231 +       return gen;
15232 +}
15233 +
15234 +/* tiny test for inode number */
15235 +/* tmpfs generation is too rough */
15236 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
15237 +{
15238 +       struct au_iinfo *iinfo;
15239 +
15240 +       iinfo = au_ii(inode);
15241 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
15242 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
15243 +                && iinfo->ii_higen == h_inode->i_generation);
15244 +}
15245 +
15246 +static inline void au_iigen_dec(struct inode *inode)
15247 +{
15248 +       struct au_iinfo *iinfo;
15249 +
15250 +       iinfo = au_ii(inode);
15251 +       spin_lock(&iinfo->ii_genspin);
15252 +       iinfo->ii_generation.ig_generation--;
15253 +       spin_unlock(&iinfo->ii_genspin);
15254 +}
15255 +
15256 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
15257 +{
15258 +       int err;
15259 +
15260 +       err = 0;
15261 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
15262 +               err = -EIO;
15263 +
15264 +       return err;
15265 +}
15266 +
15267 +/* ---------------------------------------------------------------------- */
15268 +
15269 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
15270 +                                       aufs_bindex_t bindex)
15271 +{
15272 +       IiMustAnyLock(inode);
15273 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
15274 +}
15275 +
15276 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
15277 +{
15278 +       IiMustAnyLock(inode);
15279 +       return au_ii(inode)->ii_bstart;
15280 +}
15281 +
15282 +static inline aufs_bindex_t au_ibend(struct inode *inode)
15283 +{
15284 +       IiMustAnyLock(inode);
15285 +       return au_ii(inode)->ii_bend;
15286 +}
15287 +
15288 +static inline struct au_vdir *au_ivdir(struct inode *inode)
15289 +{
15290 +       IiMustAnyLock(inode);
15291 +       return au_ii(inode)->ii_vdir;
15292 +}
15293 +
15294 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
15295 +{
15296 +       IiMustAnyLock(inode);
15297 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
15298 +}
15299 +
15300 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
15301 +{
15302 +       IiMustWriteLock(inode);
15303 +       au_ii(inode)->ii_bstart = bindex;
15304 +}
15305 +
15306 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
15307 +{
15308 +       IiMustWriteLock(inode);
15309 +       au_ii(inode)->ii_bend = bindex;
15310 +}
15311 +
15312 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
15313 +{
15314 +       IiMustWriteLock(inode);
15315 +       au_ii(inode)->ii_vdir = vdir;
15316 +}
15317 +
15318 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
15319 +{
15320 +       IiMustAnyLock(inode);
15321 +       return au_ii(inode)->ii_hinode + bindex;
15322 +}
15323 +
15324 +/* ---------------------------------------------------------------------- */
15325 +
15326 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
15327 +{
15328 +       if (pin)
15329 +               return pin->parent;
15330 +       return NULL;
15331 +}
15332 +
15333 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
15334 +{
15335 +       if (pin && pin->hdir)
15336 +               return pin->hdir->hi_inode;
15337 +       return NULL;
15338 +}
15339 +
15340 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
15341 +{
15342 +       if (pin)
15343 +               return pin->hdir;
15344 +       return NULL;
15345 +}
15346 +
15347 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
15348 +{
15349 +       if (pin)
15350 +               pin->dentry = dentry;
15351 +}
15352 +
15353 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
15354 +                                          unsigned char lflag)
15355 +{
15356 +       if (pin) {
15357 +               if (lflag)
15358 +                       au_fset_pin(pin->flags, DI_LOCKED);
15359 +               else
15360 +                       au_fclr_pin(pin->flags, DI_LOCKED);
15361 +       }
15362 +}
15363 +
15364 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
15365 +{
15366 +       if (pin) {
15367 +               dput(pin->parent);
15368 +               pin->parent = dget(parent);
15369 +       }
15370 +}
15371 +
15372 +/* ---------------------------------------------------------------------- */
15373 +
15374 +struct au_branch;
15375 +#ifdef CONFIG_AUFS_HNOTIFY
15376 +struct au_hnotify_op {
15377 +       void (*ctl)(struct au_hinode *hinode, int do_set);
15378 +       int (*alloc)(struct au_hinode *hinode);
15379 +
15380 +       /*
15381 +        * if it returns true, the the caller should free hinode->hi_notify,
15382 +        * otherwise ->free() frees it.
15383 +        */
15384 +       int (*free)(struct au_hinode *hinode,
15385 +                   struct au_hnotify *hn) __must_check;
15386 +
15387 +       void (*fin)(void);
15388 +       int (*init)(void);
15389 +
15390 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
15391 +       void (*fin_br)(struct au_branch *br);
15392 +       int (*init_br)(struct au_branch *br, int perm);
15393 +};
15394 +
15395 +/* hnotify.c */
15396 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
15397 +void au_hn_free(struct au_hinode *hinode);
15398 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
15399 +void au_hn_reset(struct inode *inode, unsigned int flags);
15400 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15401 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
15402 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
15403 +int au_hnotify_init_br(struct au_branch *br, int perm);
15404 +void au_hnotify_fin_br(struct au_branch *br);
15405 +int __init au_hnotify_init(void);
15406 +void au_hnotify_fin(void);
15407 +
15408 +/* hfsnotify.c */
15409 +extern const struct au_hnotify_op au_hnotify_op;
15410 +
15411 +static inline
15412 +void au_hn_init(struct au_hinode *hinode)
15413 +{
15414 +       hinode->hi_notify = NULL;
15415 +}
15416 +
15417 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
15418 +{
15419 +       return hinode->hi_notify;
15420 +}
15421 +
15422 +#else
15423 +static inline
15424 +int au_hn_alloc(struct au_hinode *hinode __maybe_unused,
15425 +               struct inode *inode __maybe_unused)
15426 +{
15427 +       return -EOPNOTSUPP;
15428 +}
15429 +
15430 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
15431 +{
15432 +       return NULL;
15433 +}
15434 +
15435 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
15436 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
15437 +          int do_set __maybe_unused)
15438 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
15439 +          unsigned int flags __maybe_unused)
15440 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
15441 +          struct au_branch *br __maybe_unused,
15442 +          int perm __maybe_unused)
15443 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
15444 +          int perm __maybe_unused)
15445 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
15446 +AuStubInt0(__init au_hnotify_init, void)
15447 +AuStubVoid(au_hnotify_fin, void)
15448 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
15449 +#endif /* CONFIG_AUFS_HNOTIFY */
15450 +
15451 +static inline void au_hn_suspend(struct au_hinode *hdir)
15452 +{
15453 +       au_hn_ctl(hdir, /*do_set*/0);
15454 +}
15455 +
15456 +static inline void au_hn_resume(struct au_hinode *hdir)
15457 +{
15458 +       au_hn_ctl(hdir, /*do_set*/1);
15459 +}
15460 +
15461 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
15462 +{
15463 +       mutex_lock(&hdir->hi_inode->i_mutex);
15464 +       au_hn_suspend(hdir);
15465 +}
15466 +
15467 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
15468 +                                         unsigned int sc __maybe_unused)
15469 +{
15470 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
15471 +       au_hn_suspend(hdir);
15472 +}
15473 +
15474 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
15475 +{
15476 +       au_hn_resume(hdir);
15477 +       mutex_unlock(&hdir->hi_inode->i_mutex);
15478 +}
15479 +
15480 +#endif /* __KERNEL__ */
15481 +#endif /* __AUFS_INODE_H__ */
15482 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
15483 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
15484 +++ linux/fs/aufs/ioctl.c       2013-08-23 23:59:39.634916914 +0200
15485 @@ -0,0 +1,202 @@
15486 +/*
15487 + * Copyright (C) 2005-2013 Junjiro R. Okajima
15488 + *
15489 + * This program, aufs is free software; you can redistribute it and/or modify
15490 + * it under the terms of the GNU General Public License as published by
15491 + * the Free Software Foundation; either version 2 of the License, or
15492 + * (at your option) any later version.
15493 + *
15494 + * This program is distributed in the hope that it will be useful,
15495 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15496 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15497 + * GNU General Public License for more details.
15498 + *
15499 + * You should have received a copy of the GNU General Public License
15500 + * along with this program; if not, write to the Free Software
15501 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15502 + */
15503 +
15504 +/*
15505 + * ioctl
15506 + * plink-management and readdir in userspace.
15507 + * assist the pathconf(3) wrapper library.
15508 + * move-down
15509 + */
15510 +
15511 +#include <linux/compat.h>
15512 +#include <linux/file.h>
15513 +#include "aufs.h"
15514 +
15515 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
15516 +{
15517 +       int err, fd;
15518 +       aufs_bindex_t wbi, bindex, bend;
15519 +       struct file *h_file;
15520 +       struct super_block *sb;
15521 +       struct dentry *root;
15522 +       struct au_branch *br;
15523 +       struct aufs_wbr_fd wbrfd = {
15524 +               .oflags = au_dir_roflags,
15525 +               .brid   = -1
15526 +       };
15527 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
15528 +               | O_NOATIME | O_CLOEXEC;
15529 +
15530 +       AuDebugOn(wbrfd.oflags & ~valid);
15531 +
15532 +       if (arg) {
15533 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
15534 +               if (unlikely(err)) {
15535 +                       err = -EFAULT;
15536 +                       goto out;
15537 +               }
15538 +
15539 +               err = -EINVAL;
15540 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
15541 +               wbrfd.oflags |= au_dir_roflags;
15542 +               AuDbg("0%o\n", wbrfd.oflags);
15543 +               if (unlikely(wbrfd.oflags & ~valid))
15544 +                       goto out;
15545 +       }
15546 +
15547 +       fd = get_unused_fd();
15548 +       err = fd;
15549 +       if (unlikely(fd < 0))
15550 +               goto out;
15551 +
15552 +       h_file = ERR_PTR(-EINVAL);
15553 +       wbi = 0;
15554 +       br = NULL;
15555 +       sb = path->dentry->d_sb;
15556 +       root = sb->s_root;
15557 +       aufs_read_lock(root, AuLock_IR);
15558 +       bend = au_sbend(sb);
15559 +       if (wbrfd.brid >= 0) {
15560 +               wbi = au_br_index(sb, wbrfd.brid);
15561 +               if (unlikely(wbi < 0 || wbi > bend))
15562 +                       goto out_unlock;
15563 +       }
15564 +
15565 +       h_file = ERR_PTR(-ENOENT);
15566 +       br = au_sbr(sb, wbi);
15567 +       if (!au_br_writable(br->br_perm)) {
15568 +               if (arg)
15569 +                       goto out_unlock;
15570 +
15571 +               bindex = wbi + 1;
15572 +               wbi = -1;
15573 +               for (; bindex <= bend; bindex++) {
15574 +                       br = au_sbr(sb, bindex);
15575 +                       if (au_br_writable(br->br_perm)) {
15576 +                               wbi = bindex;
15577 +                               br = au_sbr(sb, wbi);
15578 +                               break;
15579 +                       }
15580 +               }
15581 +       }
15582 +       AuDbg("wbi %d\n", wbi);
15583 +       if (wbi >= 0)
15584 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL);
15585 +
15586 +out_unlock:
15587 +       aufs_read_unlock(root, AuLock_IR);
15588 +       err = PTR_ERR(h_file);
15589 +       if (IS_ERR(h_file))
15590 +               goto out_fd;
15591 +
15592 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
15593 +       fd_install(fd, h_file);
15594 +       err = fd;
15595 +       goto out; /* success */
15596 +
15597 +out_fd:
15598 +       put_unused_fd(fd);
15599 +out:
15600 +       AuTraceErr(err);
15601 +       return err;
15602 +}
15603 +
15604 +/* ---------------------------------------------------------------------- */
15605 +
15606 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
15607 +{
15608 +       long err;
15609 +
15610 +       switch (cmd) {
15611 +       case AUFS_CTL_RDU:
15612 +       case AUFS_CTL_RDU_INO:
15613 +               err = au_rdu_ioctl(file, cmd, arg);
15614 +               break;
15615 +
15616 +       case AUFS_CTL_WBR_FD:
15617 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15618 +               break;
15619 +
15620 +       case AUFS_CTL_IBUSY:
15621 +               err = au_ibusy_ioctl(file, arg);
15622 +               break;
15623 +
15624 +       default:
15625 +               /* do not call the lower */
15626 +               AuDbg("0x%x\n", cmd);
15627 +               err = -ENOTTY;
15628 +       }
15629 +
15630 +       AuTraceErr(err);
15631 +       return err;
15632 +}
15633 +
15634 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
15635 +{
15636 +       long err;
15637 +
15638 +       switch (cmd) {
15639 +       case AUFS_CTL_MVDOWN:
15640 +               WARN_ONCE(1, "move-down is still testing...\n");
15641 +               err = au_mvdown(file->f_dentry, (void __user *)arg);
15642 +               break;
15643 +
15644 +       case AUFS_CTL_WBR_FD:
15645 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15646 +               break;
15647 +
15648 +       default:
15649 +               /* do not call the lower */
15650 +               AuDbg("0x%x\n", cmd);
15651 +               err = -ENOTTY;
15652 +       }
15653 +
15654 +       AuTraceErr(err);
15655 +       return err;
15656 +}
15657 +
15658 +#ifdef CONFIG_COMPAT
15659 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15660 +                          unsigned long arg)
15661 +{
15662 +       long err;
15663 +
15664 +       switch (cmd) {
15665 +       case AUFS_CTL_RDU:
15666 +       case AUFS_CTL_RDU_INO:
15667 +               err = au_rdu_compat_ioctl(file, cmd, arg);
15668 +               break;
15669 +
15670 +       case AUFS_CTL_IBUSY:
15671 +               err = au_ibusy_compat_ioctl(file, arg);
15672 +               break;
15673 +
15674 +       default:
15675 +               err = aufs_ioctl_dir(file, cmd, arg);
15676 +       }
15677 +
15678 +       AuTraceErr(err);
15679 +       return err;
15680 +}
15681 +
15682 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15683 +                             unsigned long arg)
15684 +{
15685 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
15686 +}
15687 +#endif
15688 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
15689 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
15690 +++ linux/fs/aufs/i_op_add.c    2013-08-23 23:59:39.634916914 +0200
15691 @@ -0,0 +1,739 @@
15692 +/*
15693 + * Copyright (C) 2005-2013 Junjiro R. Okajima
15694 + *
15695 + * This program, aufs is free software; you can redistribute it and/or modify
15696 + * it under the terms of the GNU General Public License as published by
15697 + * the Free Software Foundation; either version 2 of the License, or
15698 + * (at your option) any later version.
15699 + *
15700 + * This program is distributed in the hope that it will be useful,
15701 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15702 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15703 + * GNU General Public License for more details.
15704 + *
15705 + * You should have received a copy of the GNU General Public License
15706 + * along with this program; if not, write to the Free Software
15707 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15708 + */
15709 +
15710 +/*
15711 + * inode operations (add entry)
15712 + */
15713 +
15714 +#include "aufs.h"
15715 +
15716 +/*
15717 + * final procedure of adding a new entry, except link(2).
15718 + * remove whiteout, instantiate, copyup the parent dir's times and size
15719 + * and update version.
15720 + * if it failed, re-create the removed whiteout.
15721 + */
15722 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
15723 +                 struct dentry *wh_dentry, struct dentry *dentry)
15724 +{
15725 +       int err, rerr;
15726 +       aufs_bindex_t bwh;
15727 +       struct path h_path;
15728 +       struct inode *inode, *h_dir;
15729 +       struct dentry *wh;
15730 +
15731 +       bwh = -1;
15732 +       if (wh_dentry) {
15733 +               h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
15734 +               IMustLock(h_dir);
15735 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
15736 +               bwh = au_dbwh(dentry);
15737 +               h_path.dentry = wh_dentry;
15738 +               h_path.mnt = au_sbr_mnt(dir->i_sb, bindex);
15739 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
15740 +                                         dentry);
15741 +               if (unlikely(err))
15742 +                       goto out;
15743 +       }
15744 +
15745 +       inode = au_new_inode(dentry, /*must_new*/1);
15746 +       if (!IS_ERR(inode)) {
15747 +               d_instantiate(dentry, inode);
15748 +               dir = dentry->d_parent->d_inode; /* dir inode is locked */
15749 +               IMustLock(dir);
15750 +               if (au_ibstart(dir) == au_dbstart(dentry))
15751 +                       au_cpup_attr_timesizes(dir);
15752 +               dir->i_version++;
15753 +               return 0; /* success */
15754 +       }
15755 +
15756 +       err = PTR_ERR(inode);
15757 +       if (!wh_dentry)
15758 +               goto out;
15759 +
15760 +       /* revert */
15761 +       /* dir inode is locked */
15762 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
15763 +       rerr = PTR_ERR(wh);
15764 +       if (IS_ERR(wh)) {
15765 +               AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
15766 +                       AuDLNPair(dentry), err, rerr);
15767 +               err = -EIO;
15768 +       } else
15769 +               dput(wh);
15770 +
15771 +out:
15772 +       return err;
15773 +}
15774 +
15775 +static int au_d_may_add(struct dentry *dentry)
15776 +{
15777 +       int err;
15778 +
15779 +       err = 0;
15780 +       if (unlikely(d_unhashed(dentry)))
15781 +               err = -ENOENT;
15782 +       if (unlikely(dentry->d_inode))
15783 +               err = -EEXIST;
15784 +       return err;
15785 +}
15786 +
15787 +/*
15788 + * simple tests for the adding inode operations.
15789 + * following the checks in vfs, plus the parent-child relationship.
15790 + */
15791 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
15792 +              struct dentry *h_parent, int isdir)
15793 +{
15794 +       int err;
15795 +       umode_t h_mode;
15796 +       struct dentry *h_dentry;
15797 +       struct inode *h_inode;
15798 +
15799 +       err = -ENAMETOOLONG;
15800 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
15801 +               goto out;
15802 +
15803 +       h_dentry = au_h_dptr(dentry, bindex);
15804 +       h_inode = h_dentry->d_inode;
15805 +       if (!dentry->d_inode) {
15806 +               err = -EEXIST;
15807 +               if (unlikely(h_inode))
15808 +                       goto out;
15809 +       } else {
15810 +               /* rename(2) case */
15811 +               err = -EIO;
15812 +               if (unlikely(!h_inode || !h_inode->i_nlink))
15813 +                       goto out;
15814 +
15815 +               h_mode = h_inode->i_mode;
15816 +               if (!isdir) {
15817 +                       err = -EISDIR;
15818 +                       if (unlikely(S_ISDIR(h_mode)))
15819 +                               goto out;
15820 +               } else if (unlikely(!S_ISDIR(h_mode))) {
15821 +                       err = -ENOTDIR;
15822 +                       goto out;
15823 +               }
15824 +       }
15825 +
15826 +       err = 0;
15827 +       /* expected parent dir is locked */
15828 +       if (unlikely(h_parent != h_dentry->d_parent))
15829 +               err = -EIO;
15830 +
15831 +out:
15832 +       AuTraceErr(err);
15833 +       return err;
15834 +}
15835 +
15836 +/*
15837 + * initial procedure of adding a new entry.
15838 + * prepare writable branch and the parent dir, lock it,
15839 + * and lookup whiteout for the new entry.
15840 + */
15841 +static struct dentry*
15842 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
15843 +                 struct dentry *src_dentry, struct au_pin *pin,
15844 +                 struct au_wr_dir_args *wr_dir_args)
15845 +{
15846 +       struct dentry *wh_dentry, *h_parent;
15847 +       struct super_block *sb;
15848 +       struct au_branch *br;
15849 +       int err;
15850 +       unsigned int udba;
15851 +       aufs_bindex_t bcpup;
15852 +
15853 +       AuDbg("%.*s\n", AuDLNPair(dentry));
15854 +
15855 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
15856 +       bcpup = err;
15857 +       wh_dentry = ERR_PTR(err);
15858 +       if (unlikely(err < 0))
15859 +               goto out;
15860 +
15861 +       sb = dentry->d_sb;
15862 +       udba = au_opt_udba(sb);
15863 +       err = au_pin(pin, dentry, bcpup, udba,
15864 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15865 +       wh_dentry = ERR_PTR(err);
15866 +       if (unlikely(err))
15867 +               goto out;
15868 +
15869 +       h_parent = au_pinned_h_parent(pin);
15870 +       if (udba != AuOpt_UDBA_NONE
15871 +           && au_dbstart(dentry) == bcpup)
15872 +               err = au_may_add(dentry, bcpup, h_parent,
15873 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
15874 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
15875 +               err = -ENAMETOOLONG;
15876 +       wh_dentry = ERR_PTR(err);
15877 +       if (unlikely(err))
15878 +               goto out_unpin;
15879 +
15880 +       br = au_sbr(sb, bcpup);
15881 +       if (dt) {
15882 +               struct path tmp = {
15883 +                       .dentry = h_parent,
15884 +                       .mnt    = au_br_mnt(br)
15885 +               };
15886 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
15887 +       }
15888 +
15889 +       wh_dentry = NULL;
15890 +       if (bcpup != au_dbwh(dentry))
15891 +               goto out; /* success */
15892 +
15893 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
15894 +
15895 +out_unpin:
15896 +       if (IS_ERR(wh_dentry))
15897 +               au_unpin(pin);
15898 +out:
15899 +       return wh_dentry;
15900 +}
15901 +
15902 +/* ---------------------------------------------------------------------- */
15903 +
15904 +enum { Mknod, Symlink, Creat };
15905 +struct simple_arg {
15906 +       int type;
15907 +       union {
15908 +               struct {
15909 +                       umode_t mode;
15910 +                       bool want_excl;
15911 +               } c;
15912 +               struct {
15913 +                       const char *symname;
15914 +               } s;
15915 +               struct {
15916 +                       umode_t mode;
15917 +                       dev_t dev;
15918 +               } m;
15919 +       } u;
15920 +};
15921 +
15922 +static int add_simple(struct inode *dir, struct dentry *dentry,
15923 +                     struct simple_arg *arg)
15924 +{
15925 +       int err;
15926 +       aufs_bindex_t bstart;
15927 +       unsigned char created;
15928 +       struct dentry *wh_dentry, *parent;
15929 +       struct inode *h_dir;
15930 +       /* to reuduce stack size */
15931 +       struct {
15932 +               struct au_dtime dt;
15933 +               struct au_pin pin;
15934 +               struct path h_path;
15935 +               struct au_wr_dir_args wr_dir_args;
15936 +       } *a;
15937 +
15938 +       AuDbg("%.*s\n", AuDLNPair(dentry));
15939 +       IMustLock(dir);
15940 +
15941 +       err = -ENOMEM;
15942 +       a = kmalloc(sizeof(*a), GFP_NOFS);
15943 +       if (unlikely(!a))
15944 +               goto out;
15945 +       a->wr_dir_args.force_btgt = -1;
15946 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
15947 +
15948 +       parent = dentry->d_parent; /* dir inode is locked */
15949 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
15950 +       if (unlikely(err))
15951 +               goto out_free;
15952 +       err = au_d_may_add(dentry);
15953 +       if (unlikely(err))
15954 +               goto out_unlock;
15955 +       di_write_lock_parent(parent);
15956 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
15957 +                                     &a->pin, &a->wr_dir_args);
15958 +       err = PTR_ERR(wh_dentry);
15959 +       if (IS_ERR(wh_dentry))
15960 +               goto out_parent;
15961 +
15962 +       bstart = au_dbstart(dentry);
15963 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
15964 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
15965 +       h_dir = au_pinned_h_dir(&a->pin);
15966 +       switch (arg->type) {
15967 +       case Creat:
15968 +               err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
15969 +                                  arg->u.c.want_excl);
15970 +               break;
15971 +       case Symlink:
15972 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
15973 +               break;
15974 +       case Mknod:
15975 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
15976 +                                 arg->u.m.dev);
15977 +               break;
15978 +       default:
15979 +               BUG();
15980 +       }
15981 +       created = !err;
15982 +       if (!err)
15983 +               err = epilog(dir, bstart, wh_dentry, dentry);
15984 +
15985 +       /* revert */
15986 +       if (unlikely(created && err && a->h_path.dentry->d_inode)) {
15987 +               int rerr;
15988 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*force*/0);
15989 +               if (rerr) {
15990 +                       AuIOErr("%.*s revert failure(%d, %d)\n",
15991 +                               AuDLNPair(dentry), err, rerr);
15992 +                       err = -EIO;
15993 +               }
15994 +               au_dtime_revert(&a->dt);
15995 +       }
15996 +
15997 +       au_unpin(&a->pin);
15998 +       dput(wh_dentry);
15999 +
16000 +out_parent:
16001 +       di_write_unlock(parent);
16002 +out_unlock:
16003 +       if (unlikely(err)) {
16004 +               au_update_dbstart(dentry);
16005 +               d_drop(dentry);
16006 +       }
16007 +       aufs_read_unlock(dentry, AuLock_DW);
16008 +out_free:
16009 +       kfree(a);
16010 +out:
16011 +       return err;
16012 +}
16013 +
16014 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16015 +              dev_t dev)
16016 +{
16017 +       struct simple_arg arg = {
16018 +               .type = Mknod,
16019 +               .u.m = {
16020 +                       .mode   = mode,
16021 +                       .dev    = dev
16022 +               }
16023 +       };
16024 +       return add_simple(dir, dentry, &arg);
16025 +}
16026 +
16027 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
16028 +{
16029 +       struct simple_arg arg = {
16030 +               .type = Symlink,
16031 +               .u.s.symname = symname
16032 +       };
16033 +       return add_simple(dir, dentry, &arg);
16034 +}
16035 +
16036 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
16037 +               bool want_excl)
16038 +{
16039 +       struct simple_arg arg = {
16040 +               .type = Creat,
16041 +               .u.c = {
16042 +                       .mode           = mode,
16043 +                       .want_excl      = want_excl
16044 +               }
16045 +       };
16046 +       return add_simple(dir, dentry, &arg);
16047 +}
16048 +
16049 +/* ---------------------------------------------------------------------- */
16050 +
16051 +struct au_link_args {
16052 +       aufs_bindex_t bdst, bsrc;
16053 +       struct au_pin pin;
16054 +       struct path h_path;
16055 +       struct dentry *src_parent, *parent;
16056 +};
16057 +
16058 +static int au_cpup_before_link(struct dentry *src_dentry,
16059 +                              struct au_link_args *a)
16060 +{
16061 +       int err;
16062 +       struct dentry *h_src_dentry;
16063 +       struct au_cp_generic cpg = {
16064 +               .dentry = src_dentry,
16065 +               .bdst   = a->bdst,
16066 +               .bsrc   = a->bsrc,
16067 +               .len    = -1,
16068 +               .pin    = &a->pin,
16069 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
16070 +       };
16071 +
16072 +       di_read_lock_parent(a->src_parent, AuLock_IR);
16073 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
16074 +       if (unlikely(err))
16075 +               goto out;
16076 +
16077 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
16078 +       err = au_pin(&a->pin, src_dentry, a->bdst,
16079 +                    au_opt_udba(src_dentry->d_sb),
16080 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
16081 +       if (unlikely(err))
16082 +               goto out;
16083 +
16084 +       err = au_sio_cpup_simple(&cpg);
16085 +       au_unpin(&a->pin);
16086 +
16087 +out:
16088 +       di_read_unlock(a->src_parent, AuLock_IR);
16089 +       return err;
16090 +}
16091 +
16092 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
16093 +                          struct au_link_args *a)
16094 +{
16095 +       int err;
16096 +       unsigned char plink;
16097 +       aufs_bindex_t bend;
16098 +       struct dentry *h_src_dentry;
16099 +       struct inode *h_inode, *inode;
16100 +       struct super_block *sb;
16101 +       struct file *h_file;
16102 +
16103 +       plink = 0;
16104 +       h_inode = NULL;
16105 +       sb = src_dentry->d_sb;
16106 +       inode = src_dentry->d_inode;
16107 +       if (au_ibstart(inode) <= a->bdst)
16108 +               h_inode = au_h_iptr(inode, a->bdst);
16109 +       if (!h_inode || !h_inode->i_nlink) {
16110 +               /* copyup src_dentry as the name of dentry. */
16111 +               bend = au_dbend(dentry);
16112 +               if (bend < a->bsrc)
16113 +                       au_set_dbend(dentry, a->bsrc);
16114 +               au_set_h_dptr(dentry, a->bsrc,
16115 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
16116 +               dget(a->h_path.dentry);
16117 +               au_set_h_dptr(dentry, a->bdst, NULL);
16118 +               dentry->d_inode = src_dentry->d_inode; /* tmp */
16119 +               h_file = au_h_open_pre(dentry, a->bsrc);
16120 +               if (IS_ERR(h_file))
16121 +                       err = PTR_ERR(h_file);
16122 +               else {
16123 +                       struct au_cp_generic cpg = {
16124 +                               .dentry = dentry,
16125 +                               .bdst   = a->bdst,
16126 +                               .bsrc   = -1,
16127 +                               .len    = -1,
16128 +                               .pin    = &a->pin,
16129 +                               .flags  = AuCpup_KEEPLINO
16130 +                       };
16131 +                       err = au_sio_cpup_simple(&cpg);
16132 +                       au_h_open_post(dentry, a->bsrc, h_file);
16133 +                       if (!err) {
16134 +                               dput(a->h_path.dentry);
16135 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
16136 +                       } else
16137 +                               au_set_h_dptr(dentry, a->bdst,
16138 +                                             a->h_path.dentry);
16139 +               }
16140 +               dentry->d_inode = NULL; /* restore */
16141 +               au_set_h_dptr(dentry, a->bsrc, NULL);
16142 +               au_set_dbend(dentry, bend);
16143 +       } else {
16144 +               /* the inode of src_dentry already exists on a.bdst branch */
16145 +               h_src_dentry = d_find_alias(h_inode);
16146 +               if (!h_src_dentry && au_plink_test(inode)) {
16147 +                       plink = 1;
16148 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
16149 +                       err = PTR_ERR(h_src_dentry);
16150 +                       if (IS_ERR(h_src_dentry))
16151 +                               goto out;
16152 +
16153 +                       if (unlikely(!h_src_dentry->d_inode)) {
16154 +                               dput(h_src_dentry);
16155 +                               h_src_dentry = NULL;
16156 +                       }
16157 +
16158 +               }
16159 +               if (h_src_dentry) {
16160 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
16161 +                                        &a->h_path);
16162 +                       dput(h_src_dentry);
16163 +               } else {
16164 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
16165 +                               h_inode->i_ino, a->bdst);
16166 +                       err = -EIO;
16167 +               }
16168 +       }
16169 +
16170 +       if (!err && !plink)
16171 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
16172 +
16173 +out:
16174 +       AuTraceErr(err);
16175 +       return err;
16176 +}
16177 +
16178 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
16179 +             struct dentry *dentry)
16180 +{
16181 +       int err, rerr;
16182 +       struct au_dtime dt;
16183 +       struct au_link_args *a;
16184 +       struct dentry *wh_dentry, *h_src_dentry;
16185 +       struct inode *inode;
16186 +       struct super_block *sb;
16187 +       struct au_wr_dir_args wr_dir_args = {
16188 +               /* .force_btgt  = -1, */
16189 +               .flags          = AuWrDir_ADD_ENTRY
16190 +       };
16191 +
16192 +       IMustLock(dir);
16193 +       inode = src_dentry->d_inode;
16194 +       IMustLock(inode);
16195 +
16196 +       err = -ENOMEM;
16197 +       a = kzalloc(sizeof(*a), GFP_NOFS);
16198 +       if (unlikely(!a))
16199 +               goto out;
16200 +
16201 +       a->parent = dentry->d_parent; /* dir inode is locked */
16202 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
16203 +                                       AuLock_NOPLM | AuLock_GEN);
16204 +       if (unlikely(err))
16205 +               goto out_kfree;
16206 +       err = au_d_hashed_positive(src_dentry);
16207 +       if (unlikely(err))
16208 +               goto out_unlock;
16209 +       err = au_d_may_add(dentry);
16210 +       if (unlikely(err))
16211 +               goto out_unlock;
16212 +
16213 +       a->src_parent = dget_parent(src_dentry);
16214 +       wr_dir_args.force_btgt = au_ibstart(inode);
16215 +
16216 +       di_write_lock_parent(a->parent);
16217 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
16218 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
16219 +                                     &wr_dir_args);
16220 +       err = PTR_ERR(wh_dentry);
16221 +       if (IS_ERR(wh_dentry))
16222 +               goto out_parent;
16223 +
16224 +       err = 0;
16225 +       sb = dentry->d_sb;
16226 +       a->bdst = au_dbstart(dentry);
16227 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
16228 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
16229 +       a->bsrc = au_ibstart(inode);
16230 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
16231 +       if (!h_src_dentry) {
16232 +               a->bsrc = au_dbstart(src_dentry);
16233 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
16234 +               AuDebugOn(!h_src_dentry);
16235 +       } else if (IS_ERR(h_src_dentry))
16236 +               goto out_parent;
16237 +
16238 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
16239 +               if (a->bdst < a->bsrc
16240 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
16241 +                       err = au_cpup_or_link(src_dentry, dentry, a);
16242 +               else
16243 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
16244 +                                        &a->h_path);
16245 +               dput(h_src_dentry);
16246 +       } else {
16247 +               /*
16248 +                * copyup src_dentry to the branch we process,
16249 +                * and then link(2) to it.
16250 +                */
16251 +               dput(h_src_dentry);
16252 +               if (a->bdst < a->bsrc
16253 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
16254 +                       au_unpin(&a->pin);
16255 +                       di_write_unlock(a->parent);
16256 +                       err = au_cpup_before_link(src_dentry, a);
16257 +                       di_write_lock_parent(a->parent);
16258 +                       if (!err)
16259 +                               err = au_pin(&a->pin, dentry, a->bdst,
16260 +                                            au_opt_udba(sb),
16261 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
16262 +                       if (unlikely(err))
16263 +                               goto out_wh;
16264 +               }
16265 +               if (!err) {
16266 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
16267 +                       err = -ENOENT;
16268 +                       if (h_src_dentry && h_src_dentry->d_inode)
16269 +                               err = vfsub_link(h_src_dentry,
16270 +                                                au_pinned_h_dir(&a->pin),
16271 +                                                &a->h_path);
16272 +               }
16273 +       }
16274 +       if (unlikely(err))
16275 +               goto out_unpin;
16276 +
16277 +       if (wh_dentry) {
16278 +               a->h_path.dentry = wh_dentry;
16279 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
16280 +                                         dentry);
16281 +               if (unlikely(err))
16282 +                       goto out_revert;
16283 +       }
16284 +
16285 +       dir->i_version++;
16286 +       if (au_ibstart(dir) == au_dbstart(dentry))
16287 +               au_cpup_attr_timesizes(dir);
16288 +       inc_nlink(inode);
16289 +       inode->i_ctime = dir->i_ctime;
16290 +       d_instantiate(dentry, au_igrab(inode));
16291 +       if (d_unhashed(a->h_path.dentry))
16292 +               /* some filesystem calls d_drop() */
16293 +               d_drop(dentry);
16294 +       goto out_unpin; /* success */
16295 +
16296 +out_revert:
16297 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, /*force*/0);
16298 +       if (unlikely(rerr)) {
16299 +               AuIOErr("%.*s reverting failed(%d, %d)\n",
16300 +                       AuDLNPair(dentry), err, rerr);
16301 +               err = -EIO;
16302 +       }
16303 +       au_dtime_revert(&dt);
16304 +out_unpin:
16305 +       au_unpin(&a->pin);
16306 +out_wh:
16307 +       dput(wh_dentry);
16308 +out_parent:
16309 +       di_write_unlock(a->parent);
16310 +       dput(a->src_parent);
16311 +out_unlock:
16312 +       if (unlikely(err)) {
16313 +               au_update_dbstart(dentry);
16314 +               d_drop(dentry);
16315 +       }
16316 +       aufs_read_and_write_unlock2(dentry, src_dentry);
16317 +out_kfree:
16318 +       kfree(a);
16319 +out:
16320 +       AuTraceErr(err);
16321 +       return err;
16322 +}
16323 +
16324 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16325 +{
16326 +       int err, rerr;
16327 +       aufs_bindex_t bindex;
16328 +       unsigned char diropq;
16329 +       struct path h_path;
16330 +       struct dentry *wh_dentry, *parent, *opq_dentry;
16331 +       struct mutex *h_mtx;
16332 +       struct super_block *sb;
16333 +       struct {
16334 +               struct au_pin pin;
16335 +               struct au_dtime dt;
16336 +       } *a; /* reduce the stack usage */
16337 +       struct au_wr_dir_args wr_dir_args = {
16338 +               .force_btgt     = -1,
16339 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
16340 +       };
16341 +
16342 +       IMustLock(dir);
16343 +
16344 +       err = -ENOMEM;
16345 +       a = kmalloc(sizeof(*a), GFP_NOFS);
16346 +       if (unlikely(!a))
16347 +               goto out;
16348 +
16349 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
16350 +       if (unlikely(err))
16351 +               goto out_free;
16352 +       err = au_d_may_add(dentry);
16353 +       if (unlikely(err))
16354 +               goto out_unlock;
16355 +
16356 +       parent = dentry->d_parent; /* dir inode is locked */
16357 +       di_write_lock_parent(parent);
16358 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
16359 +                                     &a->pin, &wr_dir_args);
16360 +       err = PTR_ERR(wh_dentry);
16361 +       if (IS_ERR(wh_dentry))
16362 +               goto out_parent;
16363 +
16364 +       sb = dentry->d_sb;
16365 +       bindex = au_dbstart(dentry);
16366 +       h_path.dentry = au_h_dptr(dentry, bindex);
16367 +       h_path.mnt = au_sbr_mnt(sb, bindex);
16368 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
16369 +       if (unlikely(err))
16370 +               goto out_unpin;
16371 +
16372 +       /* make the dir opaque */
16373 +       diropq = 0;
16374 +       h_mtx = &h_path.dentry->d_inode->i_mutex;
16375 +       if (wh_dentry
16376 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
16377 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
16378 +               opq_dentry = au_diropq_create(dentry, bindex);
16379 +               mutex_unlock(h_mtx);
16380 +               err = PTR_ERR(opq_dentry);
16381 +               if (IS_ERR(opq_dentry))
16382 +                       goto out_dir;
16383 +               dput(opq_dentry);
16384 +               diropq = 1;
16385 +       }
16386 +
16387 +       err = epilog(dir, bindex, wh_dentry, dentry);
16388 +       if (!err) {
16389 +               inc_nlink(dir);
16390 +               goto out_unpin; /* success */
16391 +       }
16392 +
16393 +       /* revert */
16394 +       if (diropq) {
16395 +               AuLabel(revert opq);
16396 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
16397 +               rerr = au_diropq_remove(dentry, bindex);
16398 +               mutex_unlock(h_mtx);
16399 +               if (rerr) {
16400 +                       AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
16401 +                               AuDLNPair(dentry), err, rerr);
16402 +                       err = -EIO;
16403 +               }
16404 +       }
16405 +
16406 +out_dir:
16407 +       AuLabel(revert dir);
16408 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
16409 +       if (rerr) {
16410 +               AuIOErr("%.*s reverting dir failed(%d, %d)\n",
16411 +                       AuDLNPair(dentry), err, rerr);
16412 +               err = -EIO;
16413 +       }
16414 +       au_dtime_revert(&a->dt);
16415 +out_unpin:
16416 +       au_unpin(&a->pin);
16417 +       dput(wh_dentry);
16418 +out_parent:
16419 +       di_write_unlock(parent);
16420 +out_unlock:
16421 +       if (unlikely(err)) {
16422 +               au_update_dbstart(dentry);
16423 +               d_drop(dentry);
16424 +       }
16425 +       aufs_read_unlock(dentry, AuLock_DW);
16426 +out_free:
16427 +       kfree(a);
16428 +out:
16429 +       return err;
16430 +}
16431 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
16432 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
16433 +++ linux/fs/aufs/i_op.c        2013-08-23 23:59:39.634916914 +0200
16434 @@ -0,0 +1,1115 @@
16435 +/*
16436 + * Copyright (C) 2005-2013 Junjiro R. Okajima
16437 + *
16438 + * This program, aufs is free software; you can redistribute it and/or modify
16439 + * it under the terms of the GNU General Public License as published by
16440 + * the Free Software Foundation; either version 2 of the License, or
16441 + * (at your option) any later version.
16442 + *
16443 + * This program is distributed in the hope that it will be useful,
16444 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16445 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16446 + * GNU General Public License for more details.
16447 + *
16448 + * You should have received a copy of the GNU General Public License
16449 + * along with this program; if not, write to the Free Software
16450 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16451 + */
16452 +
16453 +/*
16454 + * inode operations (except add/del/rename)
16455 + */
16456 +
16457 +#include <linux/device_cgroup.h>
16458 +#include <linux/fs_stack.h>
16459 +#include <linux/mm.h>
16460 +#include <linux/namei.h>
16461 +#include <linux/security.h>
16462 +#include "aufs.h"
16463 +
16464 +static int h_permission(struct inode *h_inode, int mask,
16465 +                       struct vfsmount *h_mnt, int brperm)
16466 +{
16467 +       int err;
16468 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
16469 +
16470 +       err = -EACCES;
16471 +       if ((write_mask && IS_IMMUTABLE(h_inode))
16472 +           || ((mask & MAY_EXEC)
16473 +               && S_ISREG(h_inode->i_mode)
16474 +               && ((h_mnt->mnt_flags & MNT_NOEXEC)
16475 +                   || !(h_inode->i_mode & S_IXUGO))))
16476 +               goto out;
16477 +
16478 +       /*
16479 +        * - skip the lower fs test in the case of write to ro branch.
16480 +        * - nfs dir permission write check is optimized, but a policy for
16481 +        *   link/rename requires a real check.
16482 +        */
16483 +       if ((write_mask && !au_br_writable(brperm))
16484 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
16485 +               && write_mask && !(mask & MAY_READ))
16486 +           || !h_inode->i_op->permission) {
16487 +               /* AuLabel(generic_permission); */
16488 +               err = generic_permission(h_inode, mask);
16489 +       } else {
16490 +               /* AuLabel(h_inode->permission); */
16491 +               err = h_inode->i_op->permission(h_inode, mask);
16492 +               AuTraceErr(err);
16493 +       }
16494 +
16495 +       if (!err)
16496 +               err = devcgroup_inode_permission(h_inode, mask);
16497 +       if (!err)
16498 +               err = security_inode_permission(h_inode, mask);
16499 +
16500 +#if 0
16501 +       if (!err) {
16502 +               /* todo: do we need to call ima_path_check()? */
16503 +               struct path h_path = {
16504 +                       .dentry =
16505 +                       .mnt    = h_mnt
16506 +               };
16507 +               err = ima_path_check(&h_path,
16508 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
16509 +                                    IMA_COUNT_LEAVE);
16510 +       }
16511 +#endif
16512 +
16513 +out:
16514 +       return err;
16515 +}
16516 +
16517 +static int aufs_permission(struct inode *inode, int mask)
16518 +{
16519 +       int err;
16520 +       aufs_bindex_t bindex, bend;
16521 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
16522 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
16523 +       struct inode *h_inode;
16524 +       struct super_block *sb;
16525 +       struct au_branch *br;
16526 +
16527 +       /* todo: support rcu-walk? */
16528 +       if (mask & MAY_NOT_BLOCK)
16529 +               return -ECHILD;
16530 +
16531 +       sb = inode->i_sb;
16532 +       si_read_lock(sb, AuLock_FLUSH);
16533 +       ii_read_lock_child(inode);
16534 +#if 0
16535 +       err = au_iigen_test(inode, au_sigen(sb));
16536 +       if (unlikely(err))
16537 +               goto out;
16538 +#endif
16539 +
16540 +       if (!isdir || write_mask) {
16541 +               err = au_busy_or_stale();
16542 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
16543 +               if (unlikely(!h_inode
16544 +                            || (h_inode->i_mode & S_IFMT)
16545 +                            != (inode->i_mode & S_IFMT)))
16546 +                       goto out;
16547 +
16548 +               err = 0;
16549 +               bindex = au_ibstart(inode);
16550 +               br = au_sbr(sb, bindex);
16551 +               err = h_permission(h_inode, mask, au_br_mnt(br), br->br_perm);
16552 +               if (write_mask
16553 +                   && !err
16554 +                   && !special_file(h_inode->i_mode)) {
16555 +                       /* test whether the upper writable branch exists */
16556 +                       err = -EROFS;
16557 +                       for (; bindex >= 0; bindex--)
16558 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
16559 +                                       err = 0;
16560 +                                       break;
16561 +                               }
16562 +               }
16563 +               goto out;
16564 +       }
16565 +
16566 +       /* non-write to dir */
16567 +       err = 0;
16568 +       bend = au_ibend(inode);
16569 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
16570 +               h_inode = au_h_iptr(inode, bindex);
16571 +               if (h_inode) {
16572 +                       err = au_busy_or_stale();
16573 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
16574 +                               break;
16575 +
16576 +                       br = au_sbr(sb, bindex);
16577 +                       err = h_permission(h_inode, mask, au_br_mnt(br),
16578 +                                          br->br_perm);
16579 +               }
16580 +       }
16581 +
16582 +out:
16583 +       ii_read_unlock(inode);
16584 +       si_read_unlock(sb);
16585 +       return err;
16586 +}
16587 +
16588 +/* ---------------------------------------------------------------------- */
16589 +
16590 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
16591 +                                 unsigned int flags)
16592 +{
16593 +       struct dentry *ret, *parent;
16594 +       struct inode *inode;
16595 +       struct super_block *sb;
16596 +       int err, npositive;
16597 +
16598 +       IMustLock(dir);
16599 +
16600 +       /* todo: support rcu-walk? */
16601 +       ret = ERR_PTR(-ECHILD);
16602 +       if (flags & LOOKUP_RCU)
16603 +               goto out;
16604 +
16605 +       ret = ERR_PTR(-ENAMETOOLONG);
16606 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
16607 +               goto out;
16608 +
16609 +       sb = dir->i_sb;
16610 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16611 +       ret = ERR_PTR(err);
16612 +       if (unlikely(err))
16613 +               goto out;
16614 +
16615 +       err = au_di_init(dentry);
16616 +       ret = ERR_PTR(err);
16617 +       if (unlikely(err))
16618 +               goto out_si;
16619 +
16620 +       inode = NULL;
16621 +       npositive = 0; /* suppress a warning */
16622 +       parent = dentry->d_parent; /* dir inode is locked */
16623 +       di_read_lock_parent(parent, AuLock_IR);
16624 +       err = au_alive_dir(parent);
16625 +       if (!err)
16626 +               err = au_digen_test(parent, au_sigen(sb));
16627 +       if (!err) {
16628 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
16629 +                                          /*type*/0);
16630 +               err = npositive;
16631 +       }
16632 +       di_read_unlock(parent, AuLock_IR);
16633 +       ret = ERR_PTR(err);
16634 +       if (unlikely(err < 0))
16635 +               goto out_unlock;
16636 +
16637 +       if (npositive) {
16638 +               inode = au_new_inode(dentry, /*must_new*/0);
16639 +               ret = (void *)inode;
16640 +       }
16641 +       if (IS_ERR(inode)) {
16642 +               inode = NULL;
16643 +               goto out_unlock;
16644 +       }
16645 +
16646 +       ret = d_splice_alias(inode, dentry);
16647 +#if 0
16648 +       if (unlikely(d_need_lookup(dentry))) {
16649 +               spin_lock(&dentry->d_lock);
16650 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
16651 +               spin_unlock(&dentry->d_lock);
16652 +       } else
16653 +#endif
16654 +       if (unlikely(IS_ERR(ret) && inode)) {
16655 +               ii_write_unlock(inode);
16656 +               iput(inode);
16657 +               inode = NULL;
16658 +       }
16659 +
16660 +out_unlock:
16661 +       di_write_unlock(dentry);
16662 +       if (inode) {
16663 +               /* verbose coding for lock class name */
16664 +               if (unlikely(S_ISLNK(inode->i_mode)))
16665 +                       au_rw_class(&au_di(dentry)->di_rwsem,
16666 +                                   au_lc_key + AuLcSymlink_DIINFO);
16667 +               else if (unlikely(S_ISDIR(inode->i_mode)))
16668 +                       au_rw_class(&au_di(dentry)->di_rwsem,
16669 +                                   au_lc_key + AuLcDir_DIINFO);
16670 +               else /* likely */
16671 +                       au_rw_class(&au_di(dentry)->di_rwsem,
16672 +                                   au_lc_key + AuLcNonDir_DIINFO);
16673 +       }
16674 +out_si:
16675 +       si_read_unlock(sb);
16676 +out:
16677 +       return ret;
16678 +}
16679 +
16680 +/* ---------------------------------------------------------------------- */
16681 +
16682 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
16683 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
16684 +                         aufs_bindex_t bstart)
16685 +{
16686 +       int err;
16687 +       struct dentry *h_parent;
16688 +       struct inode *h_dir;
16689 +
16690 +       if (add_entry)
16691 +               IMustLock(parent->d_inode);
16692 +       else
16693 +               di_write_lock_parent(parent);
16694 +
16695 +       err = 0;
16696 +       if (!au_h_dptr(parent, bcpup)) {
16697 +               if (bstart > bcpup)
16698 +                       err = au_cpup_dirs(dentry, bcpup);
16699 +               else if (bstart < bcpup)
16700 +                       err = au_cpdown_dirs(dentry, bcpup);
16701 +               else
16702 +                       BUG();
16703 +       }
16704 +       if (!err && add_entry) {
16705 +               h_parent = au_h_dptr(parent, bcpup);
16706 +               h_dir = h_parent->d_inode;
16707 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
16708 +               err = au_lkup_neg(dentry, bcpup,
16709 +                                 au_ftest_wrdir(add_entry, TMP_WHENTRY));
16710 +               /* todo: no unlock here */
16711 +               mutex_unlock(&h_dir->i_mutex);
16712 +
16713 +               AuDbg("bcpup %d\n", bcpup);
16714 +               if (!err) {
16715 +                       if (!dentry->d_inode)
16716 +                               au_set_h_dptr(dentry, bstart, NULL);
16717 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
16718 +               }
16719 +       }
16720 +
16721 +       if (!add_entry)
16722 +               di_write_unlock(parent);
16723 +       if (!err)
16724 +               err = bcpup; /* success */
16725 +
16726 +       AuTraceErr(err);
16727 +       return err;
16728 +}
16729 +
16730 +/*
16731 + * decide the branch and the parent dir where we will create a new entry.
16732 + * returns new bindex or an error.
16733 + * copyup the parent dir if needed.
16734 + */
16735 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16736 +             struct au_wr_dir_args *args)
16737 +{
16738 +       int err;
16739 +       aufs_bindex_t bcpup, bstart, src_bstart;
16740 +       const unsigned char add_entry
16741 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
16742 +               | au_ftest_wrdir(args->flags, TMP_WHENTRY);
16743 +       struct super_block *sb;
16744 +       struct dentry *parent;
16745 +       struct au_sbinfo *sbinfo;
16746 +
16747 +       sb = dentry->d_sb;
16748 +       sbinfo = au_sbi(sb);
16749 +       parent = dget_parent(dentry);
16750 +       bstart = au_dbstart(dentry);
16751 +       bcpup = bstart;
16752 +       if (args->force_btgt < 0) {
16753 +               if (src_dentry) {
16754 +                       src_bstart = au_dbstart(src_dentry);
16755 +                       if (src_bstart < bstart)
16756 +                               bcpup = src_bstart;
16757 +               } else if (add_entry) {
16758 +                       err = AuWbrCreate(sbinfo, dentry,
16759 +                                         au_ftest_wrdir(args->flags, ISDIR));
16760 +                       bcpup = err;
16761 +               }
16762 +
16763 +               if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
16764 +                       if (add_entry)
16765 +                               err = AuWbrCopyup(sbinfo, dentry);
16766 +                       else {
16767 +                               if (!IS_ROOT(dentry)) {
16768 +                                       di_read_lock_parent(parent, !AuLock_IR);
16769 +                                       err = AuWbrCopyup(sbinfo, dentry);
16770 +                                       di_read_unlock(parent, !AuLock_IR);
16771 +                               } else
16772 +                                       err = AuWbrCopyup(sbinfo, dentry);
16773 +                       }
16774 +                       bcpup = err;
16775 +                       if (unlikely(err < 0))
16776 +                               goto out;
16777 +               }
16778 +       } else {
16779 +               bcpup = args->force_btgt;
16780 +               AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
16781 +       }
16782 +
16783 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
16784 +       err = bcpup;
16785 +       if (bcpup == bstart)
16786 +               goto out; /* success */
16787 +
16788 +       /* copyup the new parent into the branch we process */
16789 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
16790 +       if (err >= 0) {
16791 +               if (!dentry->d_inode) {
16792 +                       au_set_h_dptr(dentry, bstart, NULL);
16793 +                       au_set_dbstart(dentry, bcpup);
16794 +                       au_set_dbend(dentry, bcpup);
16795 +               }
16796 +               AuDebugOn(add_entry && !au_h_dptr(dentry, bcpup));
16797 +       }
16798 +
16799 +out:
16800 +       dput(parent);
16801 +       return err;
16802 +}
16803 +
16804 +/* ---------------------------------------------------------------------- */
16805 +
16806 +void au_pin_hdir_unlock(struct au_pin *p)
16807 +{
16808 +       if (p->hdir)
16809 +               au_hn_imtx_unlock(p->hdir);
16810 +}
16811 +
16812 +static int au_pin_hdir_lock(struct au_pin *p)
16813 +{
16814 +       int err;
16815 +
16816 +       err = 0;
16817 +       if (!p->hdir)
16818 +               goto out;
16819 +
16820 +       /* even if an error happens later, keep this lock */
16821 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
16822 +
16823 +       err = -EBUSY;
16824 +       if (unlikely(p->hdir->hi_inode != p->h_parent->d_inode))
16825 +               goto out;
16826 +
16827 +       err = 0;
16828 +       if (p->h_dentry)
16829 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
16830 +                                 p->h_parent, p->br);
16831 +
16832 +out:
16833 +       return err;
16834 +}
16835 +
16836 +int au_pin_hdir_relock(struct au_pin *p)
16837 +{
16838 +       int err, i;
16839 +       struct inode *h_i;
16840 +       struct dentry *h_d[] = {
16841 +               p->h_dentry,
16842 +               p->h_parent
16843 +       };
16844 +
16845 +       err = au_pin_hdir_lock(p);
16846 +       if (unlikely(err))
16847 +               goto out;
16848 +
16849 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
16850 +               if (!h_d[i])
16851 +                       continue;
16852 +               h_i = h_d[i]->d_inode;
16853 +               if (h_i)
16854 +                       err = !h_i->i_nlink;
16855 +       }
16856 +
16857 +out:
16858 +       return err;
16859 +}
16860 +
16861 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
16862 +{
16863 +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
16864 +       p->hdir->hi_inode->i_mutex.owner = task;
16865 +#endif
16866 +}
16867 +
16868 +void au_pin_hdir_acquire_nest(struct au_pin *p)
16869 +{
16870 +       if (p->hdir) {
16871 +               mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
16872 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
16873 +               au_pin_hdir_set_owner(p, current);
16874 +       }
16875 +}
16876 +
16877 +void au_pin_hdir_release(struct au_pin *p)
16878 +{
16879 +       if (p->hdir) {
16880 +               au_pin_hdir_set_owner(p, p->task);
16881 +               mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
16882 +       }
16883 +}
16884 +
16885 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
16886 +{
16887 +       if (pin && pin->parent)
16888 +               return au_h_dptr(pin->parent, pin->bindex);
16889 +       return NULL;
16890 +}
16891 +
16892 +void au_unpin(struct au_pin *p)
16893 +{
16894 +       if (p->hdir)
16895 +               au_pin_hdir_unlock(p);
16896 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
16897 +               vfsub_mnt_drop_write(p->h_mnt);
16898 +       if (!p->hdir)
16899 +               return;
16900 +
16901 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
16902 +               di_read_unlock(p->parent, AuLock_IR);
16903 +       iput(p->hdir->hi_inode);
16904 +       dput(p->parent);
16905 +       p->parent = NULL;
16906 +       p->hdir = NULL;
16907 +       p->h_mnt = NULL;
16908 +       /* do not clear p->task */
16909 +}
16910 +
16911 +int au_do_pin(struct au_pin *p)
16912 +{
16913 +       int err;
16914 +       struct super_block *sb;
16915 +       struct inode *h_dir;
16916 +
16917 +       err = 0;
16918 +       sb = p->dentry->d_sb;
16919 +       p->br = au_sbr(sb, p->bindex);
16920 +       if (IS_ROOT(p->dentry)) {
16921 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
16922 +                       p->h_mnt = au_br_mnt(p->br);
16923 +                       err = vfsub_mnt_want_write(p->h_mnt);
16924 +                       if (unlikely(err)) {
16925 +                               au_fclr_pin(p->flags, MNT_WRITE);
16926 +                               goto out_err;
16927 +                       }
16928 +               }
16929 +               goto out;
16930 +       }
16931 +
16932 +       p->h_dentry = NULL;
16933 +       if (p->bindex <= au_dbend(p->dentry))
16934 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
16935 +
16936 +       p->parent = dget_parent(p->dentry);
16937 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
16938 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
16939 +
16940 +       h_dir = NULL;
16941 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
16942 +       p->hdir = au_hi(p->parent->d_inode, p->bindex);
16943 +       if (p->hdir)
16944 +               h_dir = p->hdir->hi_inode;
16945 +
16946 +       /*
16947 +        * udba case, or
16948 +        * if DI_LOCKED is not set, then p->parent may be different
16949 +        * and h_parent can be NULL.
16950 +        */
16951 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
16952 +               err = -EBUSY;
16953 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
16954 +                       di_read_unlock(p->parent, AuLock_IR);
16955 +               dput(p->parent);
16956 +               p->parent = NULL;
16957 +               goto out_err;
16958 +       }
16959 +
16960 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
16961 +               p->h_mnt = au_br_mnt(p->br);
16962 +               err = vfsub_mnt_want_write(p->h_mnt);
16963 +               if (unlikely(err)) {
16964 +                       au_fclr_pin(p->flags, MNT_WRITE);
16965 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
16966 +                               di_read_unlock(p->parent, AuLock_IR);
16967 +                       dput(p->parent);
16968 +                       p->parent = NULL;
16969 +                       goto out_err;
16970 +               }
16971 +       }
16972 +
16973 +       au_igrab(h_dir);
16974 +       err = au_pin_hdir_lock(p);
16975 +       if (!err)
16976 +               goto out; /* success */
16977 +
16978 +out_err:
16979 +       pr_err("err %d\n", err);
16980 +       err = au_busy_or_stale();
16981 +out:
16982 +       return err;
16983 +}
16984 +
16985 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
16986 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16987 +                unsigned int udba, unsigned char flags)
16988 +{
16989 +       p->dentry = dentry;
16990 +       p->udba = udba;
16991 +       p->lsc_di = lsc_di;
16992 +       p->lsc_hi = lsc_hi;
16993 +       p->flags = flags;
16994 +       p->bindex = bindex;
16995 +
16996 +       p->parent = NULL;
16997 +       p->hdir = NULL;
16998 +       p->h_mnt = NULL;
16999 +
17000 +       p->h_dentry = NULL;
17001 +       p->h_parent = NULL;
17002 +       p->br = NULL;
17003 +       p->task = current;
17004 +}
17005 +
17006 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
17007 +          unsigned int udba, unsigned char flags)
17008 +{
17009 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
17010 +                   udba, flags);
17011 +       return au_do_pin(pin);
17012 +}
17013 +
17014 +/* ---------------------------------------------------------------------- */
17015 +
17016 +/*
17017 + * ->setattr() and ->getattr() are called in various cases.
17018 + * chmod, stat: dentry is revalidated.
17019 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
17020 + *               unhashed.
17021 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
17022 + */
17023 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
17024 +static int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
17025 +{
17026 +       int err;
17027 +       struct inode *inode;
17028 +       struct dentry *parent;
17029 +
17030 +       err = 0;
17031 +       inode = dentry->d_inode;
17032 +       if (au_digen_test(dentry, sigen)) {
17033 +               parent = dget_parent(dentry);
17034 +               di_read_lock_parent(parent, AuLock_IR);
17035 +               err = au_refresh_dentry(dentry, parent);
17036 +               di_read_unlock(parent, AuLock_IR);
17037 +               dput(parent);
17038 +       }
17039 +
17040 +       AuTraceErr(err);
17041 +       return err;
17042 +}
17043 +
17044 +#define AuIcpup_DID_CPUP       1
17045 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
17046 +#define au_fset_icpup(flags, name) \
17047 +       do { (flags) |= AuIcpup_##name; } while (0)
17048 +#define au_fclr_icpup(flags, name) \
17049 +       do { (flags) &= ~AuIcpup_##name; } while (0)
17050 +
17051 +struct au_icpup_args {
17052 +       unsigned char flags;
17053 +       unsigned char pin_flags;
17054 +       aufs_bindex_t btgt;
17055 +       unsigned int udba;
17056 +       struct au_pin pin;
17057 +       struct path h_path;
17058 +       struct inode *h_inode;
17059 +};
17060 +
17061 +static int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
17062 +                           struct au_icpup_args *a)
17063 +{
17064 +       int err;
17065 +       loff_t sz;
17066 +       aufs_bindex_t bstart, ibstart;
17067 +       struct dentry *hi_wh, *parent;
17068 +       struct inode *inode;
17069 +       struct au_wr_dir_args wr_dir_args = {
17070 +               .force_btgt     = -1,
17071 +               .flags          = 0
17072 +       };
17073 +
17074 +       bstart = au_dbstart(dentry);
17075 +       inode = dentry->d_inode;
17076 +       if (S_ISDIR(inode->i_mode))
17077 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
17078 +       /* plink or hi_wh() case */
17079 +       ibstart = au_ibstart(inode);
17080 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
17081 +               wr_dir_args.force_btgt = ibstart;
17082 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17083 +       if (unlikely(err < 0))
17084 +               goto out;
17085 +       a->btgt = err;
17086 +       if (err != bstart)
17087 +               au_fset_icpup(a->flags, DID_CPUP);
17088 +
17089 +       err = 0;
17090 +       a->pin_flags = AuPin_MNT_WRITE;
17091 +       parent = NULL;
17092 +       if (!IS_ROOT(dentry)) {
17093 +               au_fset_pin(a->pin_flags, DI_LOCKED);
17094 +               parent = dget_parent(dentry);
17095 +               di_write_lock_parent(parent);
17096 +       }
17097 +
17098 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
17099 +       if (unlikely(err))
17100 +               goto out_parent;
17101 +
17102 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17103 +       a->h_inode = a->h_path.dentry->d_inode;
17104 +       mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
17105 +       sz = -1;
17106 +       if ((ia->ia_valid & ATTR_SIZE) && ia->ia_size < i_size_read(a->h_inode))
17107 +               sz = ia->ia_size;
17108 +       mutex_unlock(&a->h_inode->i_mutex);
17109 +
17110 +       hi_wh = NULL;
17111 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
17112 +               hi_wh = au_hi_wh(inode, a->btgt);
17113 +               if (!hi_wh) {
17114 +                       struct au_cp_generic cpg = {
17115 +                               .dentry = dentry,
17116 +                               .bdst   = a->btgt,
17117 +                               .bsrc   = -1,
17118 +                               .len    = sz,
17119 +                               .pin    = &a->pin
17120 +                       };
17121 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
17122 +                       if (unlikely(err))
17123 +                               goto out_unlock;
17124 +                       hi_wh = au_hi_wh(inode, a->btgt);
17125 +                       /* todo: revalidate hi_wh? */
17126 +               }
17127 +       }
17128 +
17129 +       if (parent) {
17130 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
17131 +               di_downgrade_lock(parent, AuLock_IR);
17132 +               dput(parent);
17133 +               parent = NULL;
17134 +       }
17135 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
17136 +               goto out; /* success */
17137 +
17138 +       if (!d_unhashed(dentry)) {
17139 +               struct au_cp_generic cpg = {
17140 +                       .dentry = dentry,
17141 +                       .bdst   = a->btgt,
17142 +                       .bsrc   = bstart,
17143 +                       .len    = sz,
17144 +                       .pin    = &a->pin,
17145 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
17146 +               };
17147 +               err = au_sio_cpup_simple(&cpg);
17148 +               if (!err)
17149 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
17150 +       } else if (!hi_wh)
17151 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
17152 +       else
17153 +               a->h_path.dentry = hi_wh; /* do not dget here */
17154 +
17155 +out_unlock:
17156 +       a->h_inode = a->h_path.dentry->d_inode;
17157 +       if (!err)
17158 +               goto out; /* success */
17159 +       au_unpin(&a->pin);
17160 +out_parent:
17161 +       if (parent) {
17162 +               di_write_unlock(parent);
17163 +               dput(parent);
17164 +       }
17165 +out:
17166 +       if (!err)
17167 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
17168 +       return err;
17169 +}
17170 +
17171 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
17172 +{
17173 +       int err;
17174 +       struct inode *inode;
17175 +       struct super_block *sb;
17176 +       struct file *file;
17177 +       struct au_icpup_args *a;
17178 +
17179 +       inode = dentry->d_inode;
17180 +       IMustLock(inode);
17181 +
17182 +       err = -ENOMEM;
17183 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17184 +       if (unlikely(!a))
17185 +               goto out;
17186 +
17187 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
17188 +               ia->ia_valid &= ~ATTR_MODE;
17189 +
17190 +       file = NULL;
17191 +       sb = dentry->d_sb;
17192 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17193 +       if (unlikely(err))
17194 +               goto out_kfree;
17195 +
17196 +       if (ia->ia_valid & ATTR_FILE) {
17197 +               /* currently ftruncate(2) only */
17198 +               AuDebugOn(!S_ISREG(inode->i_mode));
17199 +               file = ia->ia_file;
17200 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
17201 +               if (unlikely(err))
17202 +                       goto out_si;
17203 +               ia->ia_file = au_hf_top(file);
17204 +               a->udba = AuOpt_UDBA_NONE;
17205 +       } else {
17206 +               /* fchmod() doesn't pass ia_file */
17207 +               a->udba = au_opt_udba(sb);
17208 +               di_write_lock_child(dentry);
17209 +               /* no d_unlinked(), to set UDBA_NONE for root */
17210 +               if (d_unhashed(dentry))
17211 +                       a->udba = AuOpt_UDBA_NONE;
17212 +               if (a->udba != AuOpt_UDBA_NONE) {
17213 +                       AuDebugOn(IS_ROOT(dentry));
17214 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
17215 +                       if (unlikely(err))
17216 +                               goto out_dentry;
17217 +               }
17218 +       }
17219 +
17220 +       err = au_pin_and_icpup(dentry, ia, a);
17221 +       if (unlikely(err < 0))
17222 +               goto out_dentry;
17223 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
17224 +               ia->ia_file = NULL;
17225 +               ia->ia_valid &= ~ATTR_FILE;
17226 +       }
17227 +
17228 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
17229 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
17230 +           == (ATTR_MODE | ATTR_CTIME)) {
17231 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
17232 +               if (unlikely(err))
17233 +                       goto out_unlock;
17234 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
17235 +                  && (ia->ia_valid & ATTR_CTIME)) {
17236 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
17237 +               if (unlikely(err))
17238 +                       goto out_unlock;
17239 +       }
17240 +
17241 +       if (ia->ia_valid & ATTR_SIZE) {
17242 +               struct file *f;
17243 +
17244 +               if (ia->ia_size < i_size_read(inode))
17245 +                       /* unmap only */
17246 +                       truncate_setsize(inode, ia->ia_size);
17247 +
17248 +               f = NULL;
17249 +               if (ia->ia_valid & ATTR_FILE)
17250 +                       f = ia->ia_file;
17251 +               mutex_unlock(&a->h_inode->i_mutex);
17252 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
17253 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
17254 +       } else
17255 +               err = vfsub_notify_change(&a->h_path, ia);
17256 +       if (!err)
17257 +               au_cpup_attr_changeable(inode);
17258 +
17259 +out_unlock:
17260 +       mutex_unlock(&a->h_inode->i_mutex);
17261 +       au_unpin(&a->pin);
17262 +       if (unlikely(err))
17263 +               au_update_dbstart(dentry);
17264 +out_dentry:
17265 +       di_write_unlock(dentry);
17266 +       if (file) {
17267 +               fi_write_unlock(file);
17268 +               ia->ia_file = file;
17269 +               ia->ia_valid |= ATTR_FILE;
17270 +       }
17271 +out_si:
17272 +       si_read_unlock(sb);
17273 +out_kfree:
17274 +       kfree(a);
17275 +out:
17276 +       AuTraceErr(err);
17277 +       return err;
17278 +}
17279 +
17280 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
17281 +                            unsigned int nlink)
17282 +{
17283 +       unsigned int n;
17284 +
17285 +       inode->i_mode = st->mode;
17286 +       /* don't i_[ug]id_write() here */
17287 +       inode->i_uid = st->uid;
17288 +       inode->i_gid = st->gid;
17289 +       inode->i_atime = st->atime;
17290 +       inode->i_mtime = st->mtime;
17291 +       inode->i_ctime = st->ctime;
17292 +
17293 +       au_cpup_attr_nlink(inode, /*force*/0);
17294 +       if (S_ISDIR(inode->i_mode)) {
17295 +               n = inode->i_nlink;
17296 +               n -= nlink;
17297 +               n += st->nlink;
17298 +               smp_mb();
17299 +               /* 0 can happen */
17300 +               set_nlink(inode, n);
17301 +       }
17302 +
17303 +       spin_lock(&inode->i_lock);
17304 +       inode->i_blocks = st->blocks;
17305 +       i_size_write(inode, st->size);
17306 +       spin_unlock(&inode->i_lock);
17307 +}
17308 +
17309 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
17310 +                       struct dentry *dentry, struct kstat *st)
17311 +{
17312 +       int err;
17313 +       unsigned int mnt_flags;
17314 +       aufs_bindex_t bindex;
17315 +       unsigned char udba_none, positive;
17316 +       struct super_block *sb, *h_sb;
17317 +       struct inode *inode;
17318 +       struct path h_path;
17319 +
17320 +       sb = dentry->d_sb;
17321 +       inode = dentry->d_inode;
17322 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17323 +       if (unlikely(err))
17324 +               goto out;
17325 +       mnt_flags = au_mntflags(sb);
17326 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
17327 +
17328 +       /* support fstat(2) */
17329 +       if (!d_unlinked(dentry) && !udba_none) {
17330 +               unsigned int sigen = au_sigen(sb);
17331 +               err = au_digen_test(dentry, sigen);
17332 +               if (!err) {
17333 +                       di_read_lock_child(dentry, AuLock_IR);
17334 +                       err = au_dbrange_test(dentry);
17335 +                       if (unlikely(err))
17336 +                               goto out_unlock;
17337 +               } else {
17338 +                       AuDebugOn(IS_ROOT(dentry));
17339 +                       di_write_lock_child(dentry);
17340 +                       err = au_dbrange_test(dentry);
17341 +                       if (!err)
17342 +                               err = au_reval_for_attr(dentry, sigen);
17343 +                       di_downgrade_lock(dentry, AuLock_IR);
17344 +                       if (unlikely(err))
17345 +                               goto out_unlock;
17346 +               }
17347 +       } else
17348 +               di_read_lock_child(dentry, AuLock_IR);
17349 +
17350 +       bindex = au_ibstart(inode);
17351 +       h_path.mnt = au_sbr_mnt(sb, bindex);
17352 +       h_sb = h_path.mnt->mnt_sb;
17353 +       if (!au_test_fs_bad_iattr(h_sb) && udba_none)
17354 +               goto out_fill; /* success */
17355 +
17356 +       h_path.dentry = NULL;
17357 +       if (au_dbstart(dentry) == bindex)
17358 +               h_path.dentry = dget(au_h_dptr(dentry, bindex));
17359 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
17360 +               h_path.dentry = au_plink_lkup(inode, bindex);
17361 +               if (IS_ERR(h_path.dentry))
17362 +                       goto out_fill; /* pretending success */
17363 +       }
17364 +       /* illegally overlapped or something */
17365 +       if (unlikely(!h_path.dentry))
17366 +               goto out_fill; /* pretending success */
17367 +
17368 +       positive = !!h_path.dentry->d_inode;
17369 +       if (positive)
17370 +               err = vfs_getattr(&h_path, st);
17371 +       dput(h_path.dentry);
17372 +       if (!err) {
17373 +               if (positive)
17374 +                       au_refresh_iattr(inode, st,
17375 +                                        h_path.dentry->d_inode->i_nlink);
17376 +               goto out_fill; /* success */
17377 +       }
17378 +       AuTraceErr(err);
17379 +       goto out_unlock;
17380 +
17381 +out_fill:
17382 +       generic_fillattr(inode, st);
17383 +out_unlock:
17384 +       di_read_unlock(dentry, AuLock_IR);
17385 +       si_read_unlock(sb);
17386 +out:
17387 +       AuTraceErr(err);
17388 +       return err;
17389 +}
17390 +
17391 +/* ---------------------------------------------------------------------- */
17392 +
17393 +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
17394 +                     int bufsiz)
17395 +{
17396 +       int err;
17397 +       struct super_block *sb;
17398 +       struct dentry *h_dentry;
17399 +
17400 +       err = -EINVAL;
17401 +       h_dentry = au_h_dptr(dentry, bindex);
17402 +       if (unlikely(!h_dentry->d_inode->i_op->readlink))
17403 +               goto out;
17404 +
17405 +       err = security_inode_readlink(h_dentry);
17406 +       if (unlikely(err))
17407 +               goto out;
17408 +
17409 +       sb = dentry->d_sb;
17410 +       if (!au_test_ro(sb, bindex, dentry->d_inode)) {
17411 +               vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
17412 +               fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
17413 +       }
17414 +       err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
17415 +
17416 +out:
17417 +       return err;
17418 +}
17419 +
17420 +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
17421 +{
17422 +       int err;
17423 +
17424 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
17425 +       if (unlikely(err))
17426 +               goto out;
17427 +       err = au_d_hashed_positive(dentry);
17428 +       if (!err)
17429 +               err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
17430 +       aufs_read_unlock(dentry, AuLock_IR);
17431 +
17432 +out:
17433 +       return err;
17434 +}
17435 +
17436 +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
17437 +{
17438 +       int err;
17439 +       mm_segment_t old_fs;
17440 +       union {
17441 +               char *k;
17442 +               char __user *u;
17443 +       } buf;
17444 +
17445 +       err = -ENOMEM;
17446 +       buf.k = (void *)__get_free_page(GFP_NOFS);
17447 +       if (unlikely(!buf.k))
17448 +               goto out;
17449 +
17450 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
17451 +       if (unlikely(err))
17452 +               goto out_name;
17453 +
17454 +       err = au_d_hashed_positive(dentry);
17455 +       if (!err) {
17456 +               old_fs = get_fs();
17457 +               set_fs(KERNEL_DS);
17458 +               err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
17459 +               set_fs(old_fs);
17460 +       }
17461 +       aufs_read_unlock(dentry, AuLock_IR);
17462 +
17463 +       if (err >= 0) {
17464 +               buf.k[err] = 0;
17465 +               /* will be freed by put_link */
17466 +               nd_set_link(nd, buf.k);
17467 +               return NULL; /* success */
17468 +       }
17469 +
17470 +out_name:
17471 +       free_page((unsigned long)buf.k);
17472 +out:
17473 +       AuTraceErr(err);
17474 +       return ERR_PTR(err);
17475 +}
17476 +
17477 +static void aufs_put_link(struct dentry *dentry __maybe_unused,
17478 +                         struct nameidata *nd, void *cookie __maybe_unused)
17479 +{
17480 +       char *p;
17481 +
17482 +       p = nd_get_link(nd);
17483 +       if (!IS_ERR_OR_NULL(p))
17484 +               free_page((unsigned long)p);
17485 +}
17486 +
17487 +/* ---------------------------------------------------------------------- */
17488 +
17489 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
17490 +{
17491 +       int err;
17492 +       struct super_block *sb;
17493 +       struct inode *h_inode;
17494 +
17495 +       sb = inode->i_sb;
17496 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
17497 +       lockdep_off();
17498 +       si_read_lock(sb, AuLock_FLUSH);
17499 +       ii_write_lock_child(inode);
17500 +       lockdep_on();
17501 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
17502 +       err = vfsub_update_time(h_inode, ts, flags);
17503 +       lockdep_off();
17504 +       ii_write_unlock(inode);
17505 +       si_read_unlock(sb);
17506 +       lockdep_on();
17507 +       return err;
17508 +}
17509 +
17510 +/* ---------------------------------------------------------------------- */
17511 +
17512 +struct inode_operations aufs_symlink_iop = {
17513 +       .permission     = aufs_permission,
17514 +       .setattr        = aufs_setattr,
17515 +       .getattr        = aufs_getattr,
17516 +
17517 +       .readlink       = aufs_readlink,
17518 +       .follow_link    = aufs_follow_link,
17519 +       .put_link       = aufs_put_link,
17520 +
17521 +       /* .update_time = aufs_update_time */
17522 +};
17523 +
17524 +struct inode_operations aufs_dir_iop = {
17525 +       .create         = aufs_create,
17526 +       .lookup         = aufs_lookup,
17527 +       .link           = aufs_link,
17528 +       .unlink         = aufs_unlink,
17529 +       .symlink        = aufs_symlink,
17530 +       .mkdir          = aufs_mkdir,
17531 +       .rmdir          = aufs_rmdir,
17532 +       .mknod          = aufs_mknod,
17533 +       .rename         = aufs_rename,
17534 +
17535 +       .permission     = aufs_permission,
17536 +       .setattr        = aufs_setattr,
17537 +       .getattr        = aufs_getattr,
17538 +
17539 +       .update_time    = aufs_update_time
17540 +       /* no support for atomic_open() */
17541 +};
17542 +
17543 +struct inode_operations aufs_iop = {
17544 +       .permission     = aufs_permission,
17545 +       .setattr        = aufs_setattr,
17546 +       .getattr        = aufs_getattr,
17547 +
17548 +       .update_time    = aufs_update_time
17549 +};
17550 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
17551 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
17552 +++ linux/fs/aufs/i_op_del.c    2013-08-23 23:59:39.634916914 +0200
17553 @@ -0,0 +1,502 @@
17554 +/*
17555 + * Copyright (C) 2005-2013 Junjiro R. Okajima
17556 + *
17557 + * This program, aufs is free software; you can redistribute it and/or modify
17558 + * it under the terms of the GNU General Public License as published by
17559 + * the Free Software Foundation; either version 2 of the License, or
17560 + * (at your option) any later version.
17561 + *
17562 + * This program is distributed in the hope that it will be useful,
17563 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17564 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17565 + * GNU General Public License for more details.
17566 + *
17567 + * You should have received a copy of the GNU General Public License
17568 + * along with this program; if not, write to the Free Software
17569 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17570 + */
17571 +
17572 +/*
17573 + * inode operations (del entry)
17574 + */
17575 +
17576 +#include "aufs.h"
17577 +
17578 +/*
17579 + * decide if a new whiteout for @dentry is necessary or not.
17580 + * when it is necessary, prepare the parent dir for the upper branch whose
17581 + * branch index is @bcpup for creation. the actual creation of the whiteout will
17582 + * be done by caller.
17583 + * return value:
17584 + * 0: wh is unnecessary
17585 + * plus: wh is necessary
17586 + * minus: error
17587 + */
17588 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
17589 +{
17590 +       int need_wh, err;
17591 +       aufs_bindex_t bstart;
17592 +       struct super_block *sb;
17593 +
17594 +       sb = dentry->d_sb;
17595 +       bstart = au_dbstart(dentry);
17596 +       if (*bcpup < 0) {
17597 +               *bcpup = bstart;
17598 +               if (au_test_ro(sb, bstart, dentry->d_inode)) {
17599 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
17600 +                       *bcpup = err;
17601 +                       if (unlikely(err < 0))
17602 +                               goto out;
17603 +               }
17604 +       } else
17605 +               AuDebugOn(bstart < *bcpup
17606 +                         || au_test_ro(sb, *bcpup, dentry->d_inode));
17607 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
17608 +
17609 +       if (*bcpup != bstart) {
17610 +               err = au_cpup_dirs(dentry, *bcpup);
17611 +               if (unlikely(err))
17612 +                       goto out;
17613 +               need_wh = 1;
17614 +       } else {
17615 +               struct au_dinfo *dinfo, *tmp;
17616 +
17617 +               need_wh = -ENOMEM;
17618 +               dinfo = au_di(dentry);
17619 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
17620 +               if (tmp) {
17621 +                       au_di_cp(tmp, dinfo);
17622 +                       au_di_swap(tmp, dinfo);
17623 +                       /* returns the number of positive dentries */
17624 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
17625 +                       au_di_swap(tmp, dinfo);
17626 +                       au_rw_write_unlock(&tmp->di_rwsem);
17627 +                       au_di_free(tmp);
17628 +               }
17629 +       }
17630 +       AuDbg("need_wh %d\n", need_wh);
17631 +       err = need_wh;
17632 +
17633 +out:
17634 +       return err;
17635 +}
17636 +
17637 +/*
17638 + * simple tests for the del-entry operations.
17639 + * following the checks in vfs, plus the parent-child relationship.
17640 + */
17641 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
17642 +              struct dentry *h_parent, int isdir)
17643 +{
17644 +       int err;
17645 +       umode_t h_mode;
17646 +       struct dentry *h_dentry, *h_latest;
17647 +       struct inode *h_inode;
17648 +
17649 +       h_dentry = au_h_dptr(dentry, bindex);
17650 +       h_inode = h_dentry->d_inode;
17651 +       if (dentry->d_inode) {
17652 +               err = -ENOENT;
17653 +               if (unlikely(!h_inode || !h_inode->i_nlink))
17654 +                       goto out;
17655 +
17656 +               h_mode = h_inode->i_mode;
17657 +               if (!isdir) {
17658 +                       err = -EISDIR;
17659 +                       if (unlikely(S_ISDIR(h_mode)))
17660 +                               goto out;
17661 +               } else if (unlikely(!S_ISDIR(h_mode))) {
17662 +                       err = -ENOTDIR;
17663 +                       goto out;
17664 +               }
17665 +       } else {
17666 +               /* rename(2) case */
17667 +               err = -EIO;
17668 +               if (unlikely(h_inode))
17669 +                       goto out;
17670 +       }
17671 +
17672 +       err = -ENOENT;
17673 +       /* expected parent dir is locked */
17674 +       if (unlikely(h_parent != h_dentry->d_parent))
17675 +               goto out;
17676 +       err = 0;
17677 +
17678 +       /*
17679 +        * rmdir a dir may break the consistency on some filesystem.
17680 +        * let's try heavy test.
17681 +        */
17682 +       err = -EACCES;
17683 +       if (unlikely(au_test_h_perm(h_parent->d_inode, MAY_EXEC | MAY_WRITE)))
17684 +               goto out;
17685 +
17686 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent,
17687 +                                  au_sbr(dentry->d_sb, bindex));
17688 +       err = -EIO;
17689 +       if (IS_ERR(h_latest))
17690 +               goto out;
17691 +       if (h_latest == h_dentry)
17692 +               err = 0;
17693 +       dput(h_latest);
17694 +
17695 +out:
17696 +       return err;
17697 +}
17698 +
17699 +/*
17700 + * decide the branch where we operate for @dentry. the branch index will be set
17701 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
17702 + * dir for reverting.
17703 + * when a new whiteout is necessary, create it.
17704 + */
17705 +static struct dentry*
17706 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
17707 +                   struct au_dtime *dt, struct au_pin *pin)
17708 +{
17709 +       struct dentry *wh_dentry;
17710 +       struct super_block *sb;
17711 +       struct path h_path;
17712 +       int err, need_wh;
17713 +       unsigned int udba;
17714 +       aufs_bindex_t bcpup;
17715 +
17716 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
17717 +       wh_dentry = ERR_PTR(need_wh);
17718 +       if (unlikely(need_wh < 0))
17719 +               goto out;
17720 +
17721 +       sb = dentry->d_sb;
17722 +       udba = au_opt_udba(sb);
17723 +       bcpup = *rbcpup;
17724 +       err = au_pin(pin, dentry, bcpup, udba,
17725 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17726 +       wh_dentry = ERR_PTR(err);
17727 +       if (unlikely(err))
17728 +               goto out;
17729 +
17730 +       h_path.dentry = au_pinned_h_parent(pin);
17731 +       if (udba != AuOpt_UDBA_NONE
17732 +           && au_dbstart(dentry) == bcpup) {
17733 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
17734 +               wh_dentry = ERR_PTR(err);
17735 +               if (unlikely(err))
17736 +                       goto out_unpin;
17737 +       }
17738 +
17739 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
17740 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
17741 +       wh_dentry = NULL;
17742 +       if (!need_wh)
17743 +               goto out; /* success, no need to create whiteout */
17744 +
17745 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
17746 +       if (IS_ERR(wh_dentry))
17747 +               goto out_unpin;
17748 +
17749 +       /* returns with the parent is locked and wh_dentry is dget-ed */
17750 +       goto out; /* success */
17751 +
17752 +out_unpin:
17753 +       au_unpin(pin);
17754 +out:
17755 +       return wh_dentry;
17756 +}
17757 +
17758 +/*
17759 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
17760 + * in order to be revertible and save time for removing many child whiteouts
17761 + * under the dir.
17762 + * returns 1 when there are too many child whiteout and caller should remove
17763 + * them asynchronously. returns 0 when the number of children is enough small to
17764 + * remove now or the branch fs is a remote fs.
17765 + * otherwise return an error.
17766 + */
17767 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
17768 +                          struct au_nhash *whlist, struct inode *dir)
17769 +{
17770 +       int rmdir_later, err, dirwh;
17771 +       struct dentry *h_dentry;
17772 +       struct super_block *sb;
17773 +
17774 +       sb = dentry->d_sb;
17775 +       SiMustAnyLock(sb);
17776 +       h_dentry = au_h_dptr(dentry, bindex);
17777 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
17778 +       if (unlikely(err))
17779 +               goto out;
17780 +
17781 +       /* stop monitoring */
17782 +       au_hn_free(au_hi(dentry->d_inode, bindex));
17783 +
17784 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
17785 +               dirwh = au_sbi(sb)->si_dirwh;
17786 +               rmdir_later = (dirwh <= 1);
17787 +               if (!rmdir_later)
17788 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
17789 +                                                             dirwh);
17790 +               if (rmdir_later)
17791 +                       return rmdir_later;
17792 +       }
17793 +
17794 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
17795 +       if (unlikely(err)) {
17796 +               AuIOErr("rmdir %.*s, b%d failed, %d. ignored\n",
17797 +                       AuDLNPair(h_dentry), bindex, err);
17798 +               err = 0;
17799 +       }
17800 +
17801 +out:
17802 +       AuTraceErr(err);
17803 +       return err;
17804 +}
17805 +
17806 +/*
17807 + * final procedure for deleting a entry.
17808 + * maintain dentry and iattr.
17809 + */
17810 +static void epilog(struct inode *dir, struct dentry *dentry,
17811 +                  aufs_bindex_t bindex)
17812 +{
17813 +       struct inode *inode;
17814 +
17815 +       inode = dentry->d_inode;
17816 +       d_drop(dentry);
17817 +       inode->i_ctime = dir->i_ctime;
17818 +
17819 +       if (au_ibstart(dir) == bindex)
17820 +               au_cpup_attr_timesizes(dir);
17821 +       dir->i_version++;
17822 +}
17823 +
17824 +/*
17825 + * when an error happened, remove the created whiteout and revert everything.
17826 + */
17827 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
17828 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
17829 +                    struct dentry *dentry, struct au_dtime *dt)
17830 +{
17831 +       int rerr;
17832 +       struct path h_path = {
17833 +               .dentry = wh_dentry,
17834 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
17835 +       };
17836 +
17837 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
17838 +       if (!rerr) {
17839 +               au_set_dbwh(dentry, bwh);
17840 +               au_dtime_revert(dt);
17841 +               return 0;
17842 +       }
17843 +
17844 +       AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
17845 +               AuDLNPair(dentry), err, rerr);
17846 +       return -EIO;
17847 +}
17848 +
17849 +/* ---------------------------------------------------------------------- */
17850 +
17851 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
17852 +{
17853 +       int err;
17854 +       aufs_bindex_t bwh, bindex, bstart;
17855 +       struct inode *inode, *h_dir;
17856 +       struct dentry *parent, *wh_dentry;
17857 +       /* to reuduce stack size */
17858 +       struct {
17859 +               struct au_dtime dt;
17860 +               struct au_pin pin;
17861 +               struct path h_path;
17862 +       } *a;
17863 +
17864 +       IMustLock(dir);
17865 +
17866 +       err = -ENOMEM;
17867 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17868 +       if (unlikely(!a))
17869 +               goto out;
17870 +
17871 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17872 +       if (unlikely(err))
17873 +               goto out_free;
17874 +       err = au_d_hashed_positive(dentry);
17875 +       if (unlikely(err))
17876 +               goto out_unlock;
17877 +       inode = dentry->d_inode;
17878 +       IMustLock(inode);
17879 +       err = -EISDIR;
17880 +       if (unlikely(S_ISDIR(inode->i_mode)))
17881 +               goto out_unlock; /* possible? */
17882 +
17883 +       bstart = au_dbstart(dentry);
17884 +       bwh = au_dbwh(dentry);
17885 +       bindex = -1;
17886 +       parent = dentry->d_parent; /* dir inode is locked */
17887 +       di_write_lock_parent(parent);
17888 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
17889 +                                       &a->pin);
17890 +       err = PTR_ERR(wh_dentry);
17891 +       if (IS_ERR(wh_dentry))
17892 +               goto out_parent;
17893 +
17894 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
17895 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17896 +       dget(a->h_path.dentry);
17897 +       if (bindex == bstart) {
17898 +               h_dir = au_pinned_h_dir(&a->pin);
17899 +               err = vfsub_unlink(h_dir, &a->h_path, /*force*/0);
17900 +       } else {
17901 +               /* dir inode is locked */
17902 +               h_dir = wh_dentry->d_parent->d_inode;
17903 +               IMustLock(h_dir);
17904 +               err = 0;
17905 +       }
17906 +
17907 +       if (!err) {
17908 +               vfsub_drop_nlink(inode);
17909 +               epilog(dir, dentry, bindex);
17910 +
17911 +               /* update target timestamps */
17912 +               if (bindex == bstart) {
17913 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
17914 +                       /*ignore*/
17915 +                       inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
17916 +               } else
17917 +                       /* todo: this timestamp may be reverted later */
17918 +                       inode->i_ctime = h_dir->i_ctime;
17919 +               goto out_unpin; /* success */
17920 +       }
17921 +
17922 +       /* revert */
17923 +       if (wh_dentry) {
17924 +               int rerr;
17925 +
17926 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
17927 +                                &a->dt);
17928 +               if (rerr)
17929 +                       err = rerr;
17930 +       }
17931 +
17932 +out_unpin:
17933 +       au_unpin(&a->pin);
17934 +       dput(wh_dentry);
17935 +       dput(a->h_path.dentry);
17936 +out_parent:
17937 +       di_write_unlock(parent);
17938 +out_unlock:
17939 +       aufs_read_unlock(dentry, AuLock_DW);
17940 +out_free:
17941 +       kfree(a);
17942 +out:
17943 +       return err;
17944 +}
17945 +
17946 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
17947 +{
17948 +       int err, rmdir_later;
17949 +       aufs_bindex_t bwh, bindex, bstart;
17950 +       struct inode *inode;
17951 +       struct dentry *parent, *wh_dentry, *h_dentry;
17952 +       struct au_whtmp_rmdir *args;
17953 +       /* to reuduce stack size */
17954 +       struct {
17955 +               struct au_dtime dt;
17956 +               struct au_pin pin;
17957 +       } *a;
17958 +
17959 +       IMustLock(dir);
17960 +
17961 +       err = -ENOMEM;
17962 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17963 +       if (unlikely(!a))
17964 +               goto out;
17965 +
17966 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
17967 +       if (unlikely(err))
17968 +               goto out_free;
17969 +       err = au_alive_dir(dentry);
17970 +       if (unlikely(err))
17971 +               goto out_unlock;
17972 +       inode = dentry->d_inode;
17973 +       IMustLock(inode);
17974 +       err = -ENOTDIR;
17975 +       if (unlikely(!S_ISDIR(inode->i_mode)))
17976 +               goto out_unlock; /* possible? */
17977 +
17978 +       err = -ENOMEM;
17979 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
17980 +       if (unlikely(!args))
17981 +               goto out_unlock;
17982 +
17983 +       parent = dentry->d_parent; /* dir inode is locked */
17984 +       di_write_lock_parent(parent);
17985 +       err = au_test_empty(dentry, &args->whlist);
17986 +       if (unlikely(err))
17987 +               goto out_parent;
17988 +
17989 +       bstart = au_dbstart(dentry);
17990 +       bwh = au_dbwh(dentry);
17991 +       bindex = -1;
17992 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
17993 +                                       &a->pin);
17994 +       err = PTR_ERR(wh_dentry);
17995 +       if (IS_ERR(wh_dentry))
17996 +               goto out_parent;
17997 +
17998 +       h_dentry = au_h_dptr(dentry, bstart);
17999 +       dget(h_dentry);
18000 +       rmdir_later = 0;
18001 +       if (bindex == bstart) {
18002 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
18003 +               if (err > 0) {
18004 +                       rmdir_later = err;
18005 +                       err = 0;
18006 +               }
18007 +       } else {
18008 +               /* stop monitoring */
18009 +               au_hn_free(au_hi(inode, bstart));
18010 +
18011 +               /* dir inode is locked */
18012 +               IMustLock(wh_dentry->d_parent->d_inode);
18013 +               err = 0;
18014 +       }
18015 +
18016 +       if (!err) {
18017 +               vfsub_dead_dir(inode);
18018 +               au_set_dbdiropq(dentry, -1);
18019 +               epilog(dir, dentry, bindex);
18020 +
18021 +               if (rmdir_later) {
18022 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
18023 +                       args = NULL;
18024 +               }
18025 +
18026 +               goto out_unpin; /* success */
18027 +       }
18028 +
18029 +       /* revert */
18030 +       AuLabel(revert);
18031 +       if (wh_dentry) {
18032 +               int rerr;
18033 +
18034 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
18035 +                                &a->dt);
18036 +               if (rerr)
18037 +                       err = rerr;
18038 +       }
18039 +
18040 +out_unpin:
18041 +       au_unpin(&a->pin);
18042 +       dput(wh_dentry);
18043 +       dput(h_dentry);
18044 +out_parent:
18045 +       di_write_unlock(parent);
18046 +       if (args)
18047 +               au_whtmp_rmdir_free(args);
18048 +out_unlock:
18049 +       aufs_read_unlock(dentry, AuLock_DW);
18050 +out_free:
18051 +       kfree(a);
18052 +out:
18053 +       AuTraceErr(err);
18054 +       return err;
18055 +}
18056 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
18057 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
18058 +++ linux/fs/aufs/i_op_ren.c    2013-08-23 23:59:39.634916914 +0200
18059 @@ -0,0 +1,1009 @@
18060 +/*
18061 + * Copyright (C) 2005-2013 Junjiro R. Okajima
18062 + *
18063 + * This program, aufs is free software; you can redistribute it and/or modify
18064 + * it under the terms of the GNU General Public License as published by
18065 + * the Free Software Foundation; either version 2 of the License, or
18066 + * (at your option) any later version.
18067 + *
18068 + * This program is distributed in the hope that it will be useful,
18069 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18070 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18071 + * GNU General Public License for more details.
18072 + *
18073 + * You should have received a copy of the GNU General Public License
18074 + * along with this program; if not, write to the Free Software
18075 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18076 + */
18077 +
18078 +/*
18079 + * inode operation (rename entry)
18080 + * todo: this is crazy monster
18081 + */
18082 +
18083 +#include "aufs.h"
18084 +
18085 +enum { AuSRC, AuDST, AuSrcDst };
18086 +enum { AuPARENT, AuCHILD, AuParentChild };
18087 +
18088 +#define AuRen_ISDIR    1
18089 +#define AuRen_ISSAMEDIR        (1 << 1)
18090 +#define AuRen_WHSRC    (1 << 2)
18091 +#define AuRen_WHDST    (1 << 3)
18092 +#define AuRen_MNT_WRITE        (1 << 4)
18093 +#define AuRen_DT_DSTDIR        (1 << 5)
18094 +#define AuRen_DIROPQ   (1 << 6)
18095 +#define AuRen_CPUP     (1 << 7)
18096 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
18097 +#define au_fset_ren(flags, name) \
18098 +       do { (flags) |= AuRen_##name; } while (0)
18099 +#define au_fclr_ren(flags, name) \
18100 +       do { (flags) &= ~AuRen_##name; } while (0)
18101 +
18102 +struct au_ren_args {
18103 +       struct {
18104 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
18105 +                       *wh_dentry;
18106 +               struct inode *dir, *inode;
18107 +               struct au_hinode *hdir;
18108 +               struct au_dtime dt[AuParentChild];
18109 +               aufs_bindex_t bstart;
18110 +       } sd[AuSrcDst];
18111 +
18112 +#define src_dentry     sd[AuSRC].dentry
18113 +#define src_dir                sd[AuSRC].dir
18114 +#define src_inode      sd[AuSRC].inode
18115 +#define src_h_dentry   sd[AuSRC].h_dentry
18116 +#define src_parent     sd[AuSRC].parent
18117 +#define src_h_parent   sd[AuSRC].h_parent
18118 +#define src_wh_dentry  sd[AuSRC].wh_dentry
18119 +#define src_hdir       sd[AuSRC].hdir
18120 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
18121 +#define src_dt         sd[AuSRC].dt
18122 +#define src_bstart     sd[AuSRC].bstart
18123 +
18124 +#define dst_dentry     sd[AuDST].dentry
18125 +#define dst_dir                sd[AuDST].dir
18126 +#define dst_inode      sd[AuDST].inode
18127 +#define dst_h_dentry   sd[AuDST].h_dentry
18128 +#define dst_parent     sd[AuDST].parent
18129 +#define dst_h_parent   sd[AuDST].h_parent
18130 +#define dst_wh_dentry  sd[AuDST].wh_dentry
18131 +#define dst_hdir       sd[AuDST].hdir
18132 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
18133 +#define dst_dt         sd[AuDST].dt
18134 +#define dst_bstart     sd[AuDST].bstart
18135 +
18136 +       struct dentry *h_trap;
18137 +       struct au_branch *br;
18138 +       struct au_hinode *src_hinode;
18139 +       struct path h_path;
18140 +       struct au_nhash whlist;
18141 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
18142 +
18143 +       unsigned int flags;
18144 +
18145 +       struct au_whtmp_rmdir *thargs;
18146 +       struct dentry *h_dst;
18147 +};
18148 +
18149 +/* ---------------------------------------------------------------------- */
18150 +
18151 +/*
18152 + * functions for reverting.
18153 + * when an error happened in a single rename systemcall, we should revert
18154 + * everything as if nothing happend.
18155 + * we don't need to revert the copied-up/down the parent dir since they are
18156 + * harmless.
18157 + */
18158 +
18159 +#define RevertFailure(fmt, ...) do { \
18160 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
18161 +               ##__VA_ARGS__, err, rerr); \
18162 +       err = -EIO; \
18163 +} while (0)
18164 +
18165 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
18166 +{
18167 +       int rerr;
18168 +
18169 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
18170 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
18171 +       au_hn_imtx_unlock(a->src_hinode);
18172 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
18173 +       if (rerr)
18174 +               RevertFailure("remove diropq %.*s", AuDLNPair(a->src_dentry));
18175 +}
18176 +
18177 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
18178 +{
18179 +       int rerr;
18180 +
18181 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
18182 +                                         a->src_h_parent);
18183 +       rerr = PTR_ERR(a->h_path.dentry);
18184 +       if (IS_ERR(a->h_path.dentry)) {
18185 +               RevertFailure("lkup one %.*s", AuDLNPair(a->src_dentry));
18186 +               return;
18187 +       }
18188 +
18189 +       rerr = vfsub_rename(a->dst_h_dir,
18190 +                           au_h_dptr(a->src_dentry, a->btgt),
18191 +                           a->src_h_dir, &a->h_path);
18192 +       d_drop(a->h_path.dentry);
18193 +       dput(a->h_path.dentry);
18194 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
18195 +       if (rerr)
18196 +               RevertFailure("rename %.*s", AuDLNPair(a->src_dentry));
18197 +}
18198 +
18199 +static void au_ren_rev_cpup(int err, struct au_ren_args *a)
18200 +{
18201 +       int rerr;
18202 +
18203 +       a->h_path.dentry = a->dst_h_dentry;
18204 +       rerr = vfsub_unlink(a->dst_h_dir, &a->h_path, /*force*/0);
18205 +       au_set_h_dptr(a->src_dentry, a->btgt, NULL);
18206 +       au_set_dbstart(a->src_dentry, a->src_bstart);
18207 +       if (rerr)
18208 +               RevertFailure("unlink %.*s", AuDLNPair(a->dst_h_dentry));
18209 +}
18210 +
18211 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
18212 +{
18213 +       int rerr;
18214 +
18215 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
18216 +                                         a->dst_h_parent);
18217 +       rerr = PTR_ERR(a->h_path.dentry);
18218 +       if (IS_ERR(a->h_path.dentry)) {
18219 +               RevertFailure("lkup one %.*s", AuDLNPair(a->dst_dentry));
18220 +               return;
18221 +       }
18222 +       if (a->h_path.dentry->d_inode) {
18223 +               d_drop(a->h_path.dentry);
18224 +               dput(a->h_path.dentry);
18225 +               return;
18226 +       }
18227 +
18228 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path);
18229 +       d_drop(a->h_path.dentry);
18230 +       dput(a->h_path.dentry);
18231 +       if (!rerr)
18232 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
18233 +       else
18234 +               RevertFailure("rename %.*s", AuDLNPair(a->h_dst));
18235 +}
18236 +
18237 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
18238 +{
18239 +       int rerr;
18240 +
18241 +       a->h_path.dentry = a->src_wh_dentry;
18242 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
18243 +       au_set_dbwh(a->src_dentry, a->src_bwh);
18244 +       if (rerr)
18245 +               RevertFailure("unlink %.*s", AuDLNPair(a->src_wh_dentry));
18246 +}
18247 +#undef RevertFailure
18248 +
18249 +/* ---------------------------------------------------------------------- */
18250 +
18251 +/*
18252 + * when we have to copyup the renaming entry, do it with the rename-target name
18253 + * in order to minimize the cost (the later actual rename is unnecessary).
18254 + * otherwise rename it on the target branch.
18255 + */
18256 +static int au_ren_or_cpup(struct au_ren_args *a)
18257 +{
18258 +       int err;
18259 +       struct dentry *d;
18260 +
18261 +       d = a->src_dentry;
18262 +       if (au_dbstart(d) == a->btgt) {
18263 +               a->h_path.dentry = a->dst_h_dentry;
18264 +               if (au_ftest_ren(a->flags, DIROPQ)
18265 +                   && au_dbdiropq(d) == a->btgt)
18266 +                       au_fclr_ren(a->flags, DIROPQ);
18267 +               AuDebugOn(au_dbstart(d) != a->btgt);
18268 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
18269 +                                  a->dst_h_dir, &a->h_path);
18270 +       } else
18271 +               BUG();
18272 +
18273 +       if (!err && a->h_dst)
18274 +               /* it will be set to dinfo later */
18275 +               dget(a->h_dst);
18276 +
18277 +       return err;
18278 +}
18279 +
18280 +/* cf. aufs_rmdir() */
18281 +static int au_ren_del_whtmp(struct au_ren_args *a)
18282 +{
18283 +       int err;
18284 +       struct inode *dir;
18285 +
18286 +       dir = a->dst_dir;
18287 +       SiMustAnyLock(dir->i_sb);
18288 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
18289 +                                    au_sbi(dir->i_sb)->si_dirwh)
18290 +           || au_test_fs_remote(a->h_dst->d_sb)) {
18291 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
18292 +               if (unlikely(err))
18293 +                       pr_warn("failed removing whtmp dir %.*s (%d), "
18294 +                               "ignored.\n", AuDLNPair(a->h_dst), err);
18295 +       } else {
18296 +               au_nhash_wh_free(&a->thargs->whlist);
18297 +               a->thargs->whlist = a->whlist;
18298 +               a->whlist.nh_num = 0;
18299 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
18300 +               dput(a->h_dst);
18301 +               a->thargs = NULL;
18302 +       }
18303 +
18304 +       return 0;
18305 +}
18306 +
18307 +/* make it 'opaque' dir. */
18308 +static int au_ren_diropq(struct au_ren_args *a)
18309 +{
18310 +       int err;
18311 +       struct dentry *diropq;
18312 +
18313 +       err = 0;
18314 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
18315 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
18316 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
18317 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
18318 +       au_hn_imtx_unlock(a->src_hinode);
18319 +       if (IS_ERR(diropq))
18320 +               err = PTR_ERR(diropq);
18321 +       dput(diropq);
18322 +
18323 +       return err;
18324 +}
18325 +
18326 +static int do_rename(struct au_ren_args *a)
18327 +{
18328 +       int err;
18329 +       struct dentry *d, *h_d;
18330 +
18331 +       /* prepare workqueue args for asynchronous rmdir */
18332 +       h_d = a->dst_h_dentry;
18333 +       if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
18334 +               err = -ENOMEM;
18335 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
18336 +               if (unlikely(!a->thargs))
18337 +                       goto out;
18338 +               a->h_dst = dget(h_d);
18339 +       }
18340 +
18341 +       /* create whiteout for src_dentry */
18342 +       if (au_ftest_ren(a->flags, WHSRC)) {
18343 +               a->src_bwh = au_dbwh(a->src_dentry);
18344 +               AuDebugOn(a->src_bwh >= 0);
18345 +               a->src_wh_dentry
18346 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
18347 +               err = PTR_ERR(a->src_wh_dentry);
18348 +               if (IS_ERR(a->src_wh_dentry))
18349 +                       goto out_thargs;
18350 +       }
18351 +
18352 +       /* lookup whiteout for dentry */
18353 +       if (au_ftest_ren(a->flags, WHDST)) {
18354 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
18355 +                                a->br);
18356 +               err = PTR_ERR(h_d);
18357 +               if (IS_ERR(h_d))
18358 +                       goto out_whsrc;
18359 +               if (!h_d->d_inode)
18360 +                       dput(h_d);
18361 +               else
18362 +                       a->dst_wh_dentry = h_d;
18363 +       }
18364 +
18365 +       /* rename dentry to tmpwh */
18366 +       if (a->thargs) {
18367 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
18368 +               if (unlikely(err))
18369 +                       goto out_whdst;
18370 +
18371 +               d = a->dst_dentry;
18372 +               au_set_h_dptr(d, a->btgt, NULL);
18373 +               err = au_lkup_neg(d, a->btgt, /*wh*/0);
18374 +               if (unlikely(err))
18375 +                       goto out_whtmp;
18376 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
18377 +       }
18378 +
18379 +       BUG_ON(a->dst_h_dentry->d_inode && a->src_bstart != a->btgt);
18380 +
18381 +       /* rename by vfs_rename or cpup */
18382 +       d = a->dst_dentry;
18383 +       if (au_ftest_ren(a->flags, ISDIR)
18384 +           && (a->dst_wh_dentry
18385 +               || au_dbdiropq(d) == a->btgt
18386 +               /* hide the lower to keep xino */
18387 +               || a->btgt < au_dbend(d)
18388 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
18389 +               au_fset_ren(a->flags, DIROPQ);
18390 +       err = au_ren_or_cpup(a);
18391 +       if (unlikely(err))
18392 +               /* leave the copied-up one */
18393 +               goto out_whtmp;
18394 +
18395 +       /* make dir opaque */
18396 +       if (au_ftest_ren(a->flags, DIROPQ)) {
18397 +               err = au_ren_diropq(a);
18398 +               if (unlikely(err))
18399 +                       goto out_rename;
18400 +       }
18401 +
18402 +       /* update target timestamps */
18403 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
18404 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
18405 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
18406 +       a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
18407 +
18408 +       /* remove whiteout for dentry */
18409 +       if (a->dst_wh_dentry) {
18410 +               a->h_path.dentry = a->dst_wh_dentry;
18411 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
18412 +                                         a->dst_dentry);
18413 +               if (unlikely(err))
18414 +                       goto out_diropq;
18415 +       }
18416 +
18417 +       /* remove whtmp */
18418 +       if (a->thargs)
18419 +               au_ren_del_whtmp(a); /* ignore this error */
18420 +
18421 +       err = 0;
18422 +       goto out_success;
18423 +
18424 +out_diropq:
18425 +       if (au_ftest_ren(a->flags, DIROPQ))
18426 +               au_ren_rev_diropq(err, a);
18427 +out_rename:
18428 +       if (!au_ftest_ren(a->flags, CPUP))
18429 +               au_ren_rev_rename(err, a);
18430 +       else
18431 +               au_ren_rev_cpup(err, a);
18432 +       dput(a->h_dst);
18433 +out_whtmp:
18434 +       if (a->thargs)
18435 +               au_ren_rev_whtmp(err, a);
18436 +out_whdst:
18437 +       dput(a->dst_wh_dentry);
18438 +       a->dst_wh_dentry = NULL;
18439 +out_whsrc:
18440 +       if (a->src_wh_dentry)
18441 +               au_ren_rev_whsrc(err, a);
18442 +out_success:
18443 +       dput(a->src_wh_dentry);
18444 +       dput(a->dst_wh_dentry);
18445 +out_thargs:
18446 +       if (a->thargs) {
18447 +               dput(a->h_dst);
18448 +               au_whtmp_rmdir_free(a->thargs);
18449 +               a->thargs = NULL;
18450 +       }
18451 +out:
18452 +       return err;
18453 +}
18454 +
18455 +/* ---------------------------------------------------------------------- */
18456 +
18457 +/*
18458 + * test if @dentry dir can be rename destination or not.
18459 + * success means, it is a logically empty dir.
18460 + */
18461 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
18462 +{
18463 +       return au_test_empty(dentry, whlist);
18464 +}
18465 +
18466 +/*
18467 + * test if @dentry dir can be rename source or not.
18468 + * if it can, return 0 and @children is filled.
18469 + * success means,
18470 + * - it is a logically empty dir.
18471 + * - or, it exists on writable branch and has no children including whiteouts
18472 + *       on the lower branch.
18473 + */
18474 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
18475 +{
18476 +       int err;
18477 +       unsigned int rdhash;
18478 +       aufs_bindex_t bstart;
18479 +
18480 +       bstart = au_dbstart(dentry);
18481 +       if (bstart != btgt) {
18482 +               struct au_nhash whlist;
18483 +
18484 +               SiMustAnyLock(dentry->d_sb);
18485 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
18486 +               if (!rdhash)
18487 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
18488 +                                                          dentry));
18489 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
18490 +               if (unlikely(err))
18491 +                       goto out;
18492 +               err = au_test_empty(dentry, &whlist);
18493 +               au_nhash_wh_free(&whlist);
18494 +               goto out;
18495 +       }
18496 +
18497 +       if (bstart == au_dbtaildir(dentry))
18498 +               return 0; /* success */
18499 +
18500 +       err = au_test_empty_lower(dentry);
18501 +
18502 +out:
18503 +       if (err == -ENOTEMPTY) {
18504 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
18505 +                       " is not supported\n");
18506 +               err = -EXDEV;
18507 +       }
18508 +       return err;
18509 +}
18510 +
18511 +/* side effect: sets whlist and h_dentry */
18512 +static int au_ren_may_dir(struct au_ren_args *a)
18513 +{
18514 +       int err;
18515 +       unsigned int rdhash;
18516 +       struct dentry *d;
18517 +
18518 +       d = a->dst_dentry;
18519 +       SiMustAnyLock(d->d_sb);
18520 +
18521 +       err = 0;
18522 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
18523 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
18524 +               if (!rdhash)
18525 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
18526 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
18527 +               if (unlikely(err))
18528 +                       goto out;
18529 +
18530 +               au_set_dbstart(d, a->dst_bstart);
18531 +               err = may_rename_dstdir(d, &a->whlist);
18532 +               au_set_dbstart(d, a->btgt);
18533 +       }
18534 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
18535 +       if (unlikely(err))
18536 +               goto out;
18537 +
18538 +       d = a->src_dentry;
18539 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
18540 +       if (au_ftest_ren(a->flags, ISDIR)) {
18541 +               err = may_rename_srcdir(d, a->btgt);
18542 +               if (unlikely(err)) {
18543 +                       au_nhash_wh_free(&a->whlist);
18544 +                       a->whlist.nh_num = 0;
18545 +               }
18546 +       }
18547 +out:
18548 +       return err;
18549 +}
18550 +
18551 +/* ---------------------------------------------------------------------- */
18552 +
18553 +/*
18554 + * simple tests for rename.
18555 + * following the checks in vfs, plus the parent-child relationship.
18556 + */
18557 +static int au_may_ren(struct au_ren_args *a)
18558 +{
18559 +       int err, isdir;
18560 +       struct inode *h_inode;
18561 +
18562 +       if (a->src_bstart == a->btgt) {
18563 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
18564 +                                au_ftest_ren(a->flags, ISDIR));
18565 +               if (unlikely(err))
18566 +                       goto out;
18567 +               err = -EINVAL;
18568 +               if (unlikely(a->src_h_dentry == a->h_trap))
18569 +                       goto out;
18570 +       }
18571 +
18572 +       err = 0;
18573 +       if (a->dst_bstart != a->btgt)
18574 +               goto out;
18575 +
18576 +       err = -ENOTEMPTY;
18577 +       if (unlikely(a->dst_h_dentry == a->h_trap))
18578 +               goto out;
18579 +
18580 +       err = -EIO;
18581 +       h_inode = a->dst_h_dentry->d_inode;
18582 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
18583 +       if (!a->dst_dentry->d_inode) {
18584 +               if (unlikely(h_inode))
18585 +                       goto out;
18586 +               err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
18587 +                                isdir);
18588 +       } else {
18589 +               if (unlikely(!h_inode || !h_inode->i_nlink))
18590 +                       goto out;
18591 +               err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
18592 +                                isdir);
18593 +               if (unlikely(err))
18594 +                       goto out;
18595 +       }
18596 +
18597 +out:
18598 +       if (unlikely(err == -ENOENT || err == -EEXIST))
18599 +               err = -EIO;
18600 +       AuTraceErr(err);
18601 +       return err;
18602 +}
18603 +
18604 +/* ---------------------------------------------------------------------- */
18605 +
18606 +/*
18607 + * locking order
18608 + * (VFS)
18609 + * - src_dir and dir by lock_rename()
18610 + * - inode if exitsts
18611 + * (aufs)
18612 + * - lock all
18613 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
18614 + *     + si_read_lock
18615 + *     + di_write_lock2_child()
18616 + *       + di_write_lock_child()
18617 + *        + ii_write_lock_child()
18618 + *       + di_write_lock_child2()
18619 + *        + ii_write_lock_child2()
18620 + *     + src_parent and parent
18621 + *       + di_write_lock_parent()
18622 + *        + ii_write_lock_parent()
18623 + *       + di_write_lock_parent2()
18624 + *        + ii_write_lock_parent2()
18625 + *   + lower src_dir and dir by vfsub_lock_rename()
18626 + *   + verify the every relationships between child and parent. if any
18627 + *     of them failed, unlock all and return -EBUSY.
18628 + */
18629 +static void au_ren_unlock(struct au_ren_args *a)
18630 +{
18631 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
18632 +                           a->dst_h_parent, a->dst_hdir);
18633 +       if (au_ftest_ren(a->flags, MNT_WRITE))
18634 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
18635 +}
18636 +
18637 +static int au_ren_lock(struct au_ren_args *a)
18638 +{
18639 +       int err;
18640 +       unsigned int udba;
18641 +
18642 +       err = 0;
18643 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
18644 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
18645 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
18646 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
18647 +
18648 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
18649 +       if (unlikely(err))
18650 +               goto out;
18651 +       au_fset_ren(a->flags, MNT_WRITE);
18652 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
18653 +                                     a->dst_h_parent, a->dst_hdir);
18654 +       udba = au_opt_udba(a->src_dentry->d_sb);
18655 +       if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
18656 +                    || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
18657 +               err = au_busy_or_stale();
18658 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
18659 +               err = au_h_verify(a->src_h_dentry, udba,
18660 +                                 a->src_h_parent->d_inode, a->src_h_parent,
18661 +                                 a->br);
18662 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
18663 +               err = au_h_verify(a->dst_h_dentry, udba,
18664 +                                 a->dst_h_parent->d_inode, a->dst_h_parent,
18665 +                                 a->br);
18666 +       if (!err)
18667 +               goto out; /* success */
18668 +
18669 +       err = au_busy_or_stale();
18670 +       au_ren_unlock(a);
18671 +
18672 +out:
18673 +       return err;
18674 +}
18675 +
18676 +/* ---------------------------------------------------------------------- */
18677 +
18678 +static void au_ren_refresh_dir(struct au_ren_args *a)
18679 +{
18680 +       struct inode *dir;
18681 +
18682 +       dir = a->dst_dir;
18683 +       dir->i_version++;
18684 +       if (au_ftest_ren(a->flags, ISDIR)) {
18685 +               /* is this updating defined in POSIX? */
18686 +               au_cpup_attr_timesizes(a->src_inode);
18687 +               au_cpup_attr_nlink(dir, /*force*/1);
18688 +       }
18689 +
18690 +       if (au_ibstart(dir) == a->btgt)
18691 +               au_cpup_attr_timesizes(dir);
18692 +
18693 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
18694 +               return;
18695 +
18696 +       dir = a->src_dir;
18697 +       dir->i_version++;
18698 +       if (au_ftest_ren(a->flags, ISDIR))
18699 +               au_cpup_attr_nlink(dir, /*force*/1);
18700 +       if (au_ibstart(dir) == a->btgt)
18701 +               au_cpup_attr_timesizes(dir);
18702 +}
18703 +
18704 +static void au_ren_refresh(struct au_ren_args *a)
18705 +{
18706 +       aufs_bindex_t bend, bindex;
18707 +       struct dentry *d, *h_d;
18708 +       struct inode *i, *h_i;
18709 +       struct super_block *sb;
18710 +
18711 +       d = a->dst_dentry;
18712 +       d_drop(d);
18713 +       if (a->h_dst)
18714 +               /* already dget-ed by au_ren_or_cpup() */
18715 +               au_set_h_dptr(d, a->btgt, a->h_dst);
18716 +
18717 +       i = a->dst_inode;
18718 +       if (i) {
18719 +               if (!au_ftest_ren(a->flags, ISDIR))
18720 +                       vfsub_drop_nlink(i);
18721 +               else {
18722 +                       vfsub_dead_dir(i);
18723 +                       au_cpup_attr_timesizes(i);
18724 +               }
18725 +               au_update_dbrange(d, /*do_put_zero*/1);
18726 +       } else {
18727 +               bend = a->btgt;
18728 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
18729 +                       au_set_h_dptr(d, bindex, NULL);
18730 +               bend = au_dbend(d);
18731 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
18732 +                       au_set_h_dptr(d, bindex, NULL);
18733 +               au_update_dbrange(d, /*do_put_zero*/0);
18734 +       }
18735 +
18736 +       d = a->src_dentry;
18737 +       au_set_dbwh(d, -1);
18738 +       bend = au_dbend(d);
18739 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
18740 +               h_d = au_h_dptr(d, bindex);
18741 +               if (h_d)
18742 +                       au_set_h_dptr(d, bindex, NULL);
18743 +       }
18744 +       au_set_dbend(d, a->btgt);
18745 +
18746 +       sb = d->d_sb;
18747 +       i = a->src_inode;
18748 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
18749 +               return; /* success */
18750 +
18751 +       bend = au_ibend(i);
18752 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
18753 +               h_i = au_h_iptr(i, bindex);
18754 +               if (h_i) {
18755 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
18756 +                       /* ignore this error */
18757 +                       au_set_h_iptr(i, bindex, NULL, 0);
18758 +               }
18759 +       }
18760 +       au_set_ibend(i, a->btgt);
18761 +}
18762 +
18763 +/* ---------------------------------------------------------------------- */
18764 +
18765 +/* mainly for link(2) and rename(2) */
18766 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
18767 +{
18768 +       aufs_bindex_t bdiropq, bwh;
18769 +       struct dentry *parent;
18770 +       struct au_branch *br;
18771 +
18772 +       parent = dentry->d_parent;
18773 +       IMustLock(parent->d_inode); /* dir is locked */
18774 +
18775 +       bdiropq = au_dbdiropq(parent);
18776 +       bwh = au_dbwh(dentry);
18777 +       br = au_sbr(dentry->d_sb, btgt);
18778 +       if (au_br_rdonly(br)
18779 +           || (0 <= bdiropq && bdiropq < btgt)
18780 +           || (0 <= bwh && bwh < btgt))
18781 +               btgt = -1;
18782 +
18783 +       AuDbg("btgt %d\n", btgt);
18784 +       return btgt;
18785 +}
18786 +
18787 +/* sets src_bstart, dst_bstart and btgt */
18788 +static int au_ren_wbr(struct au_ren_args *a)
18789 +{
18790 +       int err;
18791 +       struct au_wr_dir_args wr_dir_args = {
18792 +               /* .force_btgt  = -1, */
18793 +               .flags          = AuWrDir_ADD_ENTRY
18794 +       };
18795 +
18796 +       a->src_bstart = au_dbstart(a->src_dentry);
18797 +       a->dst_bstart = au_dbstart(a->dst_dentry);
18798 +       if (au_ftest_ren(a->flags, ISDIR))
18799 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
18800 +       wr_dir_args.force_btgt = a->src_bstart;
18801 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
18802 +               wr_dir_args.force_btgt = a->dst_bstart;
18803 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
18804 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
18805 +       a->btgt = err;
18806 +
18807 +       return err;
18808 +}
18809 +
18810 +static void au_ren_dt(struct au_ren_args *a)
18811 +{
18812 +       a->h_path.dentry = a->src_h_parent;
18813 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
18814 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
18815 +               a->h_path.dentry = a->dst_h_parent;
18816 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
18817 +       }
18818 +
18819 +       au_fclr_ren(a->flags, DT_DSTDIR);
18820 +       if (!au_ftest_ren(a->flags, ISDIR))
18821 +               return;
18822 +
18823 +       a->h_path.dentry = a->src_h_dentry;
18824 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
18825 +       if (a->dst_h_dentry->d_inode) {
18826 +               au_fset_ren(a->flags, DT_DSTDIR);
18827 +               a->h_path.dentry = a->dst_h_dentry;
18828 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
18829 +       }
18830 +}
18831 +
18832 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
18833 +{
18834 +       struct dentry *h_d;
18835 +       struct mutex *h_mtx;
18836 +
18837 +       au_dtime_revert(a->src_dt + AuPARENT);
18838 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
18839 +               au_dtime_revert(a->dst_dt + AuPARENT);
18840 +
18841 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
18842 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
18843 +               h_mtx = &h_d->d_inode->i_mutex;
18844 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18845 +               au_dtime_revert(a->src_dt + AuCHILD);
18846 +               mutex_unlock(h_mtx);
18847 +
18848 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
18849 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
18850 +                       h_mtx = &h_d->d_inode->i_mutex;
18851 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18852 +                       au_dtime_revert(a->dst_dt + AuCHILD);
18853 +                       mutex_unlock(h_mtx);
18854 +               }
18855 +       }
18856 +}
18857 +
18858 +/* ---------------------------------------------------------------------- */
18859 +
18860 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
18861 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
18862 +{
18863 +       int err, flags;
18864 +       /* reduce stack space */
18865 +       struct au_ren_args *a;
18866 +
18867 +       AuDbg("%.*s, %.*s\n", AuDLNPair(_src_dentry), AuDLNPair(_dst_dentry));
18868 +       IMustLock(_src_dir);
18869 +       IMustLock(_dst_dir);
18870 +
18871 +       err = -ENOMEM;
18872 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
18873 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18874 +       if (unlikely(!a))
18875 +               goto out;
18876 +
18877 +       a->src_dir = _src_dir;
18878 +       a->src_dentry = _src_dentry;
18879 +       a->src_inode = a->src_dentry->d_inode;
18880 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
18881 +       a->dst_dir = _dst_dir;
18882 +       a->dst_dentry = _dst_dentry;
18883 +       a->dst_inode = a->dst_dentry->d_inode;
18884 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
18885 +       if (a->dst_inode) {
18886 +               IMustLock(a->dst_inode);
18887 +               au_igrab(a->dst_inode);
18888 +       }
18889 +
18890 +       err = -ENOTDIR;
18891 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
18892 +       if (S_ISDIR(a->src_inode->i_mode)) {
18893 +               au_fset_ren(a->flags, ISDIR);
18894 +               if (unlikely(a->dst_inode && !S_ISDIR(a->dst_inode->i_mode)))
18895 +                       goto out_free;
18896 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
18897 +                                               AuLock_DIR | flags);
18898 +       } else
18899 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
18900 +                                               flags);
18901 +       if (unlikely(err))
18902 +               goto out_free;
18903 +
18904 +       err = au_d_hashed_positive(a->src_dentry);
18905 +       if (unlikely(err))
18906 +               goto out_unlock;
18907 +       err = -ENOENT;
18908 +       if (a->dst_inode) {
18909 +               /*
18910 +                * If it is a dir, VFS unhash dst_dentry before this
18911 +                * function. It means we cannot rely upon d_unhashed().
18912 +                */
18913 +               if (unlikely(!a->dst_inode->i_nlink))
18914 +                       goto out_unlock;
18915 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
18916 +                       err = au_d_hashed_positive(a->dst_dentry);
18917 +                       if (unlikely(err))
18918 +                               goto out_unlock;
18919 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
18920 +                       goto out_unlock;
18921 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
18922 +               goto out_unlock;
18923 +
18924 +       /*
18925 +        * is it possible?
18926 +        * yes, it happend (in linux-3.3-rcN) but I don't know why.
18927 +        * there may exist a problem somewhere else.
18928 +        */
18929 +       err = -EINVAL;
18930 +       if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
18931 +               goto out_unlock;
18932 +
18933 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
18934 +       di_write_lock_parent(a->dst_parent);
18935 +
18936 +       /* which branch we process */
18937 +       err = au_ren_wbr(a);
18938 +       if (unlikely(err < 0))
18939 +               goto out_parent;
18940 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
18941 +       a->h_path.mnt = au_br_mnt(a->br);
18942 +
18943 +       /* are they available to be renamed */
18944 +       err = au_ren_may_dir(a);
18945 +       if (unlikely(err))
18946 +               goto out_children;
18947 +
18948 +       /* prepare the writable parent dir on the same branch */
18949 +       if (a->dst_bstart == a->btgt) {
18950 +               au_fset_ren(a->flags, WHDST);
18951 +       } else {
18952 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
18953 +               if (unlikely(err))
18954 +                       goto out_children;
18955 +       }
18956 +
18957 +       if (a->src_dir != a->dst_dir) {
18958 +               /*
18959 +                * this temporary unlock is safe,
18960 +                * because both dir->i_mutex are locked.
18961 +                */
18962 +               di_write_unlock(a->dst_parent);
18963 +               di_write_lock_parent(a->src_parent);
18964 +               err = au_wr_dir_need_wh(a->src_dentry,
18965 +                                       au_ftest_ren(a->flags, ISDIR),
18966 +                                       &a->btgt);
18967 +               di_write_unlock(a->src_parent);
18968 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
18969 +               au_fclr_ren(a->flags, ISSAMEDIR);
18970 +       } else
18971 +               err = au_wr_dir_need_wh(a->src_dentry,
18972 +                                       au_ftest_ren(a->flags, ISDIR),
18973 +                                       &a->btgt);
18974 +       if (unlikely(err < 0))
18975 +               goto out_children;
18976 +       if (err)
18977 +               au_fset_ren(a->flags, WHSRC);
18978 +
18979 +       /* cpup src */
18980 +       if (a->src_bstart != a->btgt) {
18981 +               struct au_pin pin;
18982 +
18983 +               err = au_pin(&pin, a->src_dentry, a->btgt,
18984 +                            au_opt_udba(a->src_dentry->d_sb),
18985 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18986 +               if (!err) {
18987 +                       struct au_cp_generic cpg = {
18988 +                               .dentry = a->src_dentry,
18989 +                               .bdst   = a->btgt,
18990 +                               .bsrc   = a->src_bstart,
18991 +                               .len    = -1,
18992 +                               .pin    = &pin,
18993 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
18994 +                       };
18995 +                       AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
18996 +                       err = au_sio_cpup_simple(&cpg);
18997 +                       au_unpin(&pin);
18998 +               }
18999 +               if (unlikely(err))
19000 +                       goto out_children;
19001 +               a->src_bstart = a->btgt;
19002 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
19003 +               au_fset_ren(a->flags, WHSRC);
19004 +       }
19005 +
19006 +       /* lock them all */
19007 +       err = au_ren_lock(a);
19008 +       if (unlikely(err))
19009 +               /* leave the copied-up one */
19010 +               goto out_children;
19011 +
19012 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
19013 +               err = au_may_ren(a);
19014 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
19015 +               err = -ENAMETOOLONG;
19016 +       if (unlikely(err))
19017 +               goto out_hdir;
19018 +
19019 +       /* store timestamps to be revertible */
19020 +       au_ren_dt(a);
19021 +
19022 +       /* here we go */
19023 +       err = do_rename(a);
19024 +       if (unlikely(err))
19025 +               goto out_dt;
19026 +
19027 +       /* update dir attributes */
19028 +       au_ren_refresh_dir(a);
19029 +
19030 +       /* dput/iput all lower dentries */
19031 +       au_ren_refresh(a);
19032 +
19033 +       goto out_hdir; /* success */
19034 +
19035 +out_dt:
19036 +       au_ren_rev_dt(err, a);
19037 +out_hdir:
19038 +       au_ren_unlock(a);
19039 +out_children:
19040 +       au_nhash_wh_free(&a->whlist);
19041 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
19042 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
19043 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
19044 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
19045 +       }
19046 +out_parent:
19047 +       if (!err)
19048 +               d_move(a->src_dentry, a->dst_dentry);
19049 +       else {
19050 +               au_update_dbstart(a->dst_dentry);
19051 +               if (!a->dst_inode)
19052 +                       d_drop(a->dst_dentry);
19053 +       }
19054 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
19055 +               di_write_unlock(a->dst_parent);
19056 +       else
19057 +               di_write_unlock2(a->src_parent, a->dst_parent);
19058 +out_unlock:
19059 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
19060 +out_free:
19061 +       iput(a->dst_inode);
19062 +       if (a->thargs)
19063 +               au_whtmp_rmdir_free(a->thargs);
19064 +       kfree(a);
19065 +out:
19066 +       AuTraceErr(err);
19067 +       return err;
19068 +}
19069 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
19070 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
19071 +++ linux/fs/aufs/Kconfig       2013-07-06 13:20:47.736864659 +0200
19072 @@ -0,0 +1,202 @@
19073 +config AUFS_FS
19074 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
19075 +       help
19076 +       Aufs is a stackable unification filesystem such as Unionfs,
19077 +       which unifies several directories and provides a merged single
19078 +       directory.
19079 +       In the early days, aufs was entirely re-designed and
19080 +       re-implemented Unionfs Version 1.x series. Introducing many
19081 +       original ideas, approaches and improvements, it becomes totally
19082 +       different from Unionfs while keeping the basic features.
19083 +
19084 +if AUFS_FS
19085 +choice
19086 +       prompt "Maximum number of branches"
19087 +       default AUFS_BRANCH_MAX_127
19088 +       help
19089 +       Specifies the maximum number of branches (or member directories)
19090 +       in a single aufs. The larger value consumes more system
19091 +       resources and has a minor impact to performance.
19092 +config AUFS_BRANCH_MAX_127
19093 +       bool "127"
19094 +       help
19095 +       Specifies the maximum number of branches (or member directories)
19096 +       in a single aufs. The larger value consumes more system
19097 +       resources and has a minor impact to performance.
19098 +config AUFS_BRANCH_MAX_511
19099 +       bool "511"
19100 +       help
19101 +       Specifies the maximum number of branches (or member directories)
19102 +       in a single aufs. The larger value consumes more system
19103 +       resources and has a minor impact to performance.
19104 +config AUFS_BRANCH_MAX_1023
19105 +       bool "1023"
19106 +       help
19107 +       Specifies the maximum number of branches (or member directories)
19108 +       in a single aufs. The larger value consumes more system
19109 +       resources and has a minor impact to performance.
19110 +config AUFS_BRANCH_MAX_32767
19111 +       bool "32767"
19112 +       help
19113 +       Specifies the maximum number of branches (or member directories)
19114 +       in a single aufs. The larger value consumes more system
19115 +       resources and has a minor impact to performance.
19116 +endchoice
19117 +
19118 +config AUFS_SBILIST
19119 +       bool
19120 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
19121 +       default y
19122 +       help
19123 +       Automatic configuration for internal use.
19124 +       When aufs supports Magic SysRq or /proc, enabled automatically.
19125 +
19126 +config AUFS_HNOTIFY
19127 +       bool "Detect direct branch access (bypassing aufs)"
19128 +       help
19129 +       If you want to modify files on branches directly, eg. bypassing aufs,
19130 +       and want aufs to detect the changes of them fully, then enable this
19131 +       option and use 'udba=notify' mount option.
19132 +       Currently there is only one available configuration, "fsnotify".
19133 +       It will have a negative impact to the performance.
19134 +       See detail in aufs.5.
19135 +
19136 +choice
19137 +       prompt "method" if AUFS_HNOTIFY
19138 +       default AUFS_HFSNOTIFY
19139 +config AUFS_HFSNOTIFY
19140 +       bool "fsnotify"
19141 +       select FSNOTIFY
19142 +endchoice
19143 +
19144 +config AUFS_EXPORT
19145 +       bool "NFS-exportable aufs"
19146 +       depends on EXPORTFS
19147 +       help
19148 +       If you want to export your mounted aufs via NFS, then enable this
19149 +       option. There are several requirements for this configuration.
19150 +       See detail in aufs.5.
19151 +
19152 +config AUFS_INO_T_64
19153 +       bool
19154 +       depends on AUFS_EXPORT
19155 +       depends on 64BIT && !(ALPHA || S390)
19156 +       default y
19157 +       help
19158 +       Automatic configuration for internal use.
19159 +       /* typedef unsigned long/int __kernel_ino_t */
19160 +       /* alpha and s390x are int */
19161 +
19162 +config AUFS_RDU
19163 +       bool "Readdir in userspace"
19164 +       help
19165 +       Aufs has two methods to provide a merged view for a directory,
19166 +       by a user-space library and by kernel-space natively. The latter
19167 +       is always enabled but sometimes large and slow.
19168 +       If you enable this option, install the library in aufs2-util
19169 +       package, and set some environment variables for your readdir(3),
19170 +       then the work will be handled in user-space which generally
19171 +       shows better performance in most cases.
19172 +       See detail in aufs.5.
19173 +
19174 +config AUFS_PROC_MAP
19175 +       bool "support for /proc/maps and lsof(1)"
19176 +       depends on PROC_FS
19177 +       help
19178 +       When you issue mmap(2) in aufs, it is actually a direct mmap(2)
19179 +       call to the file on the branch fs since the file in aufs is
19180 +       purely virtual. And the file path printed in /proc/maps (and
19181 +       others) will be the path on the branch fs. In most cases, it
19182 +       does no harm. But some utilities like lsof(1) may confuse since
19183 +       the utility or user may expect the file path in aufs to be
19184 +       printed.
19185 +       To address this issue, aufs provides a patch which introduces a
19186 +       new member called vm_prfile into struct vm_are_struct. The patch
19187 +       is meaningless without enabling this configuration since nobody
19188 +       sets the new vm_prfile member.
19189 +       If you don't apply the patch, then enabling this configuration
19190 +       will cause a compile error.
19191 +       This approach is fragile since if someone else make some changes
19192 +       around vm_file, then vm_prfile may not work anymore. As a
19193 +       workaround such case, aufs provides this configuration. If you
19194 +       disable it, then lsof(1) may produce incorrect result but the
19195 +       problem will be gone even if the aufs patch is applied (I hope).
19196 +
19197 +config AUFS_SP_IATTR
19198 +       bool "Respect the attributes (mtime/ctime mainly) of special files"
19199 +       help
19200 +       When you write something to a special file, some attributes of it
19201 +       (mtime/ctime mainly) may be updated. Generally such updates are
19202 +       less important (actually some device drivers and NFS ignore
19203 +       it). But some applications (such like test program) requires
19204 +       such updates. If you need these updates, then enable this
19205 +       configuration which introduces some overhead.
19206 +       Currently this configuration handles FIFO only.
19207 +
19208 +config AUFS_SHWH
19209 +       bool "Show whiteouts"
19210 +       help
19211 +       If you want to make the whiteouts in aufs visible, then enable
19212 +       this option and specify 'shwh' mount option. Although it may
19213 +       sounds like philosophy or something, but in technically it
19214 +       simply shows the name of whiteout with keeping its behaviour.
19215 +
19216 +config AUFS_BR_RAMFS
19217 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
19218 +       help
19219 +       If you want to use ramfs as an aufs branch fs, then enable this
19220 +       option. Generally tmpfs is recommended.
19221 +       Aufs prohibited them to be a branch fs by default, because
19222 +       initramfs becomes unusable after switch_root or something
19223 +       generally. If you sets initramfs as an aufs branch and boot your
19224 +       system by switch_root, you will meet a problem easily since the
19225 +       files in initramfs may be inaccessible.
19226 +       Unless you are going to use ramfs as an aufs branch fs without
19227 +       switch_root or something, leave it N.
19228 +
19229 +config AUFS_BR_FUSE
19230 +       bool "Fuse fs as an aufs branch"
19231 +       depends on FUSE_FS
19232 +       select AUFS_POLL
19233 +       help
19234 +       If you want to use fuse-based userspace filesystem as an aufs
19235 +       branch fs, then enable this option.
19236 +       It implements the internal poll(2) operation which is
19237 +       implemented by fuse only (curretnly).
19238 +
19239 +config AUFS_POLL
19240 +       bool
19241 +       help
19242 +       Automatic configuration for internal use.
19243 +
19244 +config AUFS_BR_HFSPLUS
19245 +       bool "Hfsplus as an aufs branch"
19246 +       depends on HFSPLUS_FS
19247 +       default y
19248 +       help
19249 +       If you want to use hfsplus fs as an aufs branch fs, then enable
19250 +       this option. This option introduces a small overhead at
19251 +       copying-up a file on hfsplus.
19252 +
19253 +config AUFS_BDEV_LOOP
19254 +       bool
19255 +       depends on BLK_DEV_LOOP
19256 +       default y
19257 +       help
19258 +       Automatic configuration for internal use.
19259 +       Convert =[ym] into =y.
19260 +
19261 +config AUFS_DEBUG
19262 +       bool "Debug aufs"
19263 +       help
19264 +       Enable this to compile aufs internal debug code.
19265 +       It will have a negative impact to the performance.
19266 +
19267 +config AUFS_MAGIC_SYSRQ
19268 +       bool
19269 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
19270 +       default y
19271 +       help
19272 +       Automatic configuration for internal use.
19273 +       When aufs supports Magic SysRq, enabled automatically.
19274 +endif
19275 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
19276 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
19277 +++ linux/fs/aufs/loop.c        2013-07-30 22:42:55.842946719 +0200
19278 @@ -0,0 +1,135 @@
19279 +/*
19280 + * Copyright (C) 2005-2013 Junjiro R. Okajima
19281 + *
19282 + * This program, aufs is free software; you can redistribute it and/or modify
19283 + * it under the terms of the GNU General Public License as published by
19284 + * the Free Software Foundation; either version 2 of the License, or
19285 + * (at your option) any later version.
19286 + *
19287 + * This program is distributed in the hope that it will be useful,
19288 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19289 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19290 + * GNU General Public License for more details.
19291 + *
19292 + * You should have received a copy of the GNU General Public License
19293 + * along with this program; if not, write to the Free Software
19294 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19295 + */
19296 +
19297 +/*
19298 + * support for loopback block device as a branch
19299 + */
19300 +
19301 +#include <linux/loop.h>
19302 +#include "aufs.h"
19303 +
19304 +/*
19305 + * test if two lower dentries have overlapping branches.
19306 + */
19307 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
19308 +{
19309 +       struct super_block *h_sb;
19310 +       struct loop_device *l;
19311 +
19312 +       h_sb = h_adding->d_sb;
19313 +       if (MAJOR(h_sb->s_dev) != LOOP_MAJOR)
19314 +               return 0;
19315 +
19316 +       l = h_sb->s_bdev->bd_disk->private_data;
19317 +       h_adding = l->lo_backing_file->f_dentry;
19318 +       /*
19319 +        * h_adding can be local NFS.
19320 +        * in this case aufs cannot detect the loop.
19321 +        */
19322 +       if (unlikely(h_adding->d_sb == sb))
19323 +               return 1;
19324 +       return !!au_test_subdir(h_adding, sb->s_root);
19325 +}
19326 +
19327 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
19328 +int au_test_loopback_kthread(void)
19329 +{
19330 +       int ret;
19331 +       struct task_struct *tsk = current;
19332 +       char c, comm[sizeof(tsk->comm)];
19333 +
19334 +       ret = 0;
19335 +       if (tsk->flags & PF_KTHREAD) {
19336 +               get_task_comm(comm, tsk);
19337 +               c = comm[4];
19338 +               ret = ('0' <= c && c <= '9'
19339 +                      && !strncmp(comm, "loop", 4));
19340 +       }
19341 +
19342 +       return ret;
19343 +}
19344 +
19345 +/* ---------------------------------------------------------------------- */
19346 +
19347 +#define au_warn_loopback_step  16
19348 +static int au_warn_loopback_nelem = au_warn_loopback_step;
19349 +static unsigned long *au_warn_loopback_array;
19350 +
19351 +void au_warn_loopback(struct super_block *h_sb)
19352 +{
19353 +       int i, new_nelem;
19354 +       unsigned long *a, magic;
19355 +       static DEFINE_SPINLOCK(spin);
19356 +
19357 +       magic = h_sb->s_magic;
19358 +       spin_lock(&spin);
19359 +       a = au_warn_loopback_array;
19360 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
19361 +               if (a[i] == magic) {
19362 +                       spin_unlock(&spin);
19363 +                       return;
19364 +               }
19365 +
19366 +       /* h_sb is new to us, print it */
19367 +       if (i < au_warn_loopback_nelem) {
19368 +               a[i] = magic;
19369 +               goto pr;
19370 +       }
19371 +
19372 +       /* expand the array */
19373 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
19374 +       a = au_kzrealloc(au_warn_loopback_array,
19375 +                        au_warn_loopback_nelem * sizeof(unsigned long),
19376 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
19377 +       if (a) {
19378 +               au_warn_loopback_nelem = new_nelem;
19379 +               au_warn_loopback_array = a;
19380 +               a[i] = magic;
19381 +               goto pr;
19382 +       }
19383 +
19384 +       spin_unlock(&spin);
19385 +       AuWarn1("realloc failed, ignored\n");
19386 +       return;
19387 +
19388 +pr:
19389 +       spin_unlock(&spin);
19390 +       pr_warn("you may want to try another patch for loopback file "
19391 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
19392 +}
19393 +
19394 +int au_loopback_init(void)
19395 +{
19396 +       int err;
19397 +       struct super_block *sb __maybe_unused;
19398 +
19399 +       AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
19400 +
19401 +       err = 0;
19402 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
19403 +                                        sizeof(unsigned long), GFP_NOFS);
19404 +       if (unlikely(!au_warn_loopback_array))
19405 +               err = -ENOMEM;
19406 +
19407 +       return err;
19408 +}
19409 +
19410 +void au_loopback_fin(void)
19411 +{
19412 +       kfree(au_warn_loopback_array);
19413 +}
19414 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
19415 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
19416 +++ linux/fs/aufs/loop.h        2013-07-30 22:42:55.842946719 +0200
19417 @@ -0,0 +1,50 @@
19418 +/*
19419 + * Copyright (C) 2005-2013 Junjiro R. Okajima
19420 + *
19421 + * This program, aufs is free software; you can redistribute it and/or modify
19422 + * it under the terms of the GNU General Public License as published by
19423 + * the Free Software Foundation; either version 2 of the License, or
19424 + * (at your option) any later version.
19425 + *
19426 + * This program is distributed in the hope that it will be useful,
19427 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19428 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19429 + * GNU General Public License for more details.
19430 + *
19431 + * You should have received a copy of the GNU General Public License
19432 + * along with this program; if not, write to the Free Software
19433 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19434 + */
19435 +
19436 +/*
19437 + * support for loopback mount as a branch
19438 + */
19439 +
19440 +#ifndef __AUFS_LOOP_H__
19441 +#define __AUFS_LOOP_H__
19442 +
19443 +#ifdef __KERNEL__
19444 +
19445 +struct dentry;
19446 +struct super_block;
19447 +
19448 +#ifdef CONFIG_AUFS_BDEV_LOOP
19449 +/* loop.c */
19450 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
19451 +int au_test_loopback_kthread(void);
19452 +void au_warn_loopback(struct super_block *h_sb);
19453 +
19454 +int au_loopback_init(void);
19455 +void au_loopback_fin(void);
19456 +#else
19457 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
19458 +          struct dentry *h_adding)
19459 +AuStubInt0(au_test_loopback_kthread, void)
19460 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
19461 +
19462 +AuStubInt0(au_loopback_init, void)
19463 +AuStubVoid(au_loopback_fin, void)
19464 +#endif /* BLK_DEV_LOOP */
19465 +
19466 +#endif /* __KERNEL__ */
19467 +#endif /* __AUFS_LOOP_H__ */
19468 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
19469 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
19470 +++ linux/fs/aufs/magic.mk      2013-07-06 13:20:47.750198454 +0200
19471 @@ -0,0 +1,54 @@
19472 +
19473 +# defined in ${srctree}/fs/fuse/inode.c
19474 +# tristate
19475 +ifdef CONFIG_FUSE_FS
19476 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
19477 +endif
19478 +
19479 +# defined in ${srctree}/fs/ocfs2/ocfs2_fs.h
19480 +# tristate
19481 +ifdef CONFIG_OCFS2_FS
19482 +ccflags-y += -DOCFS2_SUPER_MAGIC=0x7461636f
19483 +endif
19484 +
19485 +# defined in ${srctree}/fs/ocfs2/dlm/userdlm.h
19486 +# tristate
19487 +ifdef CONFIG_OCFS2_FS_O2CB
19488 +ccflags-y += -DDLMFS_MAGIC=0x76a9f425
19489 +endif
19490 +
19491 +# defined in ${srctree}/fs/cifs/cifsfs.c
19492 +# tristate
19493 +ifdef CONFIG_CIFS_FS
19494 +ccflags-y += -DCIFS_MAGIC_NUMBER=0xFF534D42
19495 +endif
19496 +
19497 +# defined in ${srctree}/fs/xfs/xfs_sb.h
19498 +# tristate
19499 +ifdef CONFIG_XFS_FS
19500 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
19501 +endif
19502 +
19503 +# defined in ${srctree}/fs/configfs/mount.c
19504 +# tristate
19505 +ifdef CONFIG_CONFIGFS_FS
19506 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
19507 +endif
19508 +
19509 +# defined in ${srctree}/fs/9p/v9fs.h
19510 +# tristate
19511 +ifdef CONFIG_9P_FS
19512 +ccflags-y += -DV9FS_MAGIC=0x01021997
19513 +endif
19514 +
19515 +# defined in ${srctree}/fs/ubifs/ubifs.h
19516 +# tristate
19517 +ifdef CONFIG_UBIFS_FS
19518 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
19519 +endif
19520 +
19521 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
19522 +# tristate
19523 +ifdef CONFIG_HFSPLUS_FS
19524 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
19525 +endif
19526 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
19527 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
19528 +++ linux/fs/aufs/Makefile      2013-08-23 23:59:39.631583456 +0200
19529 @@ -0,0 +1,42 @@
19530 +
19531 +include ${src}/magic.mk
19532 +ifeq (${CONFIG_AUFS_FS},m)
19533 +include ${src}/conf.mk
19534 +endif
19535 +-include ${src}/priv_def.mk
19536 +
19537 +# cf. include/linux/kernel.h
19538 +# enable pr_debug
19539 +ccflags-y += -DDEBUG
19540 +# sparse requires the full pathname
19541 +ifdef M
19542 +ccflags-y += -include ${M}/../../include/linux/aufs_type.h
19543 +else
19544 +ccflags-y += -include ${srctree}/include/linux/aufs_type.h
19545 +endif
19546 +
19547 +obj-$(CONFIG_AUFS_FS) += aufs.o
19548 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
19549 +       wkq.o vfsub.o dcsub.o \
19550 +       cpup.o whout.o wbr_policy.o \
19551 +       dinfo.o dentry.o \
19552 +       dynop.o \
19553 +       finfo.o file.o f_op.o \
19554 +       dir.o vdir.o \
19555 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
19556 +       mvdown.o ioctl.o
19557 +
19558 +# all are boolean
19559 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
19560 +aufs-$(CONFIG_SYSFS) += sysfs.o
19561 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
19562 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
19563 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
19564 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
19565 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
19566 +aufs-$(CONFIG_AUFS_POLL) += poll.o
19567 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
19568 +aufs-$(CONFIG_AUFS_SP_IATTR) += f_op_sp.o
19569 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
19570 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
19571 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
19572 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
19573 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
19574 +++ linux/fs/aufs/module.c      2013-07-06 13:20:47.750198454 +0200
19575 @@ -0,0 +1,203 @@
19576 +/*
19577 + * Copyright (C) 2005-2013 Junjiro R. Okajima
19578 + *
19579 + * This program, aufs is free software; you can redistribute it and/or modify
19580 + * it under the terms of the GNU General Public License as published by
19581 + * the Free Software Foundation; either version 2 of the License, or
19582 + * (at your option) any later version.
19583 + *
19584 + * This program is distributed in the hope that it will be useful,
19585 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19586 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19587 + * GNU General Public License for more details.
19588 + *
19589 + * You should have received a copy of the GNU General Public License
19590 + * along with this program; if not, write to the Free Software
19591 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19592 + */
19593 +
19594 +/*
19595 + * module global variables and operations
19596 + */
19597 +
19598 +#include <linux/module.h>
19599 +#include <linux/seq_file.h>
19600 +#include "aufs.h"
19601 +
19602 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
19603 +{
19604 +       if (new_sz <= nused)
19605 +               return p;
19606 +
19607 +       p = krealloc(p, new_sz, gfp);
19608 +       if (p)
19609 +               memset(p + nused, 0, new_sz - nused);
19610 +       return p;
19611 +}
19612 +
19613 +/* ---------------------------------------------------------------------- */
19614 +
19615 +/*
19616 + * aufs caches
19617 + */
19618 +struct kmem_cache *au_cachep[AuCache_Last];
19619 +static int __init au_cache_init(void)
19620 +{
19621 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
19622 +       if (au_cachep[AuCache_DINFO])
19623 +               /* SLAB_DESTROY_BY_RCU */
19624 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
19625 +                                                       au_icntnr_init_once);
19626 +       if (au_cachep[AuCache_ICNTNR])
19627 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
19628 +                                                      au_fi_init_once);
19629 +       if (au_cachep[AuCache_FINFO])
19630 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
19631 +       if (au_cachep[AuCache_VDIR])
19632 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
19633 +       if (au_cachep[AuCache_DEHSTR])
19634 +               return 0;
19635 +
19636 +       return -ENOMEM;
19637 +}
19638 +
19639 +static void au_cache_fin(void)
19640 +{
19641 +       int i;
19642 +
19643 +       /*
19644 +        * Make sure all delayed rcu free inodes are flushed before we
19645 +        * destroy cache.
19646 +        */
19647 +       rcu_barrier();
19648 +
19649 +       /* excluding AuCache_HNOTIFY */
19650 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
19651 +       for (i = 0; i < AuCache_HNOTIFY; i++)
19652 +               if (au_cachep[i]) {
19653 +                       kmem_cache_destroy(au_cachep[i]);
19654 +                       au_cachep[i] = NULL;
19655 +               }
19656 +}
19657 +
19658 +/* ---------------------------------------------------------------------- */
19659 +
19660 +int au_dir_roflags;
19661 +
19662 +#ifdef CONFIG_AUFS_SBILIST
19663 +/*
19664 + * iterate_supers_type() doesn't protect us from
19665 + * remounting (branch management)
19666 + */
19667 +struct au_splhead au_sbilist;
19668 +#endif
19669 +
19670 +struct lock_class_key au_lc_key[AuLcKey_Last];
19671 +
19672 +/*
19673 + * functions for module interface.
19674 + */
19675 +MODULE_LICENSE("GPL");
19676 +/* MODULE_LICENSE("GPL v2"); */
19677 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
19678 +MODULE_DESCRIPTION(AUFS_NAME
19679 +       " -- Advanced multi layered unification filesystem");
19680 +MODULE_VERSION(AUFS_VERSION);
19681 +MODULE_ALIAS_FS(AUFS_NAME);
19682 +
19683 +/* this module parameter has no meaning when SYSFS is disabled */
19684 +int sysaufs_brs = 1;
19685 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
19686 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
19687 +
19688 +/* ---------------------------------------------------------------------- */
19689 +
19690 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
19691 +
19692 +int au_seq_path(struct seq_file *seq, struct path *path)
19693 +{
19694 +       return seq_path(seq, path, au_esc_chars);
19695 +}
19696 +
19697 +/* ---------------------------------------------------------------------- */
19698 +
19699 +static int __init aufs_init(void)
19700 +{
19701 +       int err, i;
19702 +       char *p;
19703 +
19704 +       p = au_esc_chars;
19705 +       for (i = 1; i <= ' '; i++)
19706 +               *p++ = i;
19707 +       *p++ = '\\';
19708 +       *p++ = '\x7f';
19709 +       *p = 0;
19710 +
19711 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
19712 +
19713 +       au_sbilist_init();
19714 +       sysaufs_brs_init();
19715 +       au_debug_init();
19716 +       au_dy_init();
19717 +       err = sysaufs_init();
19718 +       if (unlikely(err))
19719 +               goto out;
19720 +       err = au_procfs_init();
19721 +       if (unlikely(err))
19722 +               goto out_sysaufs;
19723 +       err = au_wkq_init();
19724 +       if (unlikely(err))
19725 +               goto out_procfs;
19726 +       err = au_loopback_init();
19727 +       if (unlikely(err))
19728 +               goto out_wkq;
19729 +       err = au_hnotify_init();
19730 +       if (unlikely(err))
19731 +               goto out_loopback;
19732 +       err = au_sysrq_init();
19733 +       if (unlikely(err))
19734 +               goto out_hin;
19735 +       err = au_cache_init();
19736 +       if (unlikely(err))
19737 +               goto out_sysrq;
19738 +       err = register_filesystem(&aufs_fs_type);
19739 +       if (unlikely(err))
19740 +               goto out_cache;
19741 +       /* since we define pr_fmt, call printk directly */
19742 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
19743 +       goto out; /* success */
19744 +
19745 +out_cache:
19746 +       au_cache_fin();
19747 +out_sysrq:
19748 +       au_sysrq_fin();
19749 +out_hin:
19750 +       au_hnotify_fin();
19751 +out_loopback:
19752 +       au_loopback_fin();
19753 +out_wkq:
19754 +       au_wkq_fin();
19755 +out_procfs:
19756 +       au_procfs_fin();
19757 +out_sysaufs:
19758 +       sysaufs_fin();
19759 +       au_dy_fin();
19760 +out:
19761 +       return err;
19762 +}
19763 +
19764 +static void __exit aufs_exit(void)
19765 +{
19766 +       unregister_filesystem(&aufs_fs_type);
19767 +       au_cache_fin();
19768 +       au_sysrq_fin();
19769 +       au_hnotify_fin();
19770 +       au_loopback_fin();
19771 +       au_wkq_fin();
19772 +       au_procfs_fin();
19773 +       sysaufs_fin();
19774 +       au_dy_fin();
19775 +}
19776 +
19777 +module_init(aufs_init);
19778 +module_exit(aufs_exit);
19779 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
19780 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
19781 +++ linux/fs/aufs/module.h      2013-07-06 13:20:47.750198454 +0200
19782 @@ -0,0 +1,105 @@
19783 +/*
19784 + * Copyright (C) 2005-2013 Junjiro R. Okajima
19785 + *
19786 + * This program, aufs is free software; you can redistribute it and/or modify
19787 + * it under the terms of the GNU General Public License as published by
19788 + * the Free Software Foundation; either version 2 of the License, or
19789 + * (at your option) any later version.
19790 + *
19791 + * This program is distributed in the hope that it will be useful,
19792 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19793 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19794 + * GNU General Public License for more details.
19795 + *
19796 + * You should have received a copy of the GNU General Public License
19797 + * along with this program; if not, write to the Free Software
19798 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19799 + */
19800 +
19801 +/*
19802 + * module initialization and module-global
19803 + */
19804 +
19805 +#ifndef __AUFS_MODULE_H__
19806 +#define __AUFS_MODULE_H__
19807 +
19808 +#ifdef __KERNEL__
19809 +
19810 +#include <linux/slab.h>
19811 +
19812 +struct path;
19813 +struct seq_file;
19814 +
19815 +/* module parameters */
19816 +extern int sysaufs_brs;
19817 +
19818 +/* ---------------------------------------------------------------------- */
19819 +
19820 +extern int au_dir_roflags;
19821 +
19822 +enum {
19823 +       AuLcNonDir_FIINFO,
19824 +       AuLcNonDir_DIINFO,
19825 +       AuLcNonDir_IIINFO,
19826 +
19827 +       AuLcDir_FIINFO,
19828 +       AuLcDir_DIINFO,
19829 +       AuLcDir_IIINFO,
19830 +
19831 +       AuLcSymlink_DIINFO,
19832 +       AuLcSymlink_IIINFO,
19833 +
19834 +       AuLcKey_Last
19835 +};
19836 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
19837 +
19838 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
19839 +int au_seq_path(struct seq_file *seq, struct path *path);
19840 +
19841 +#ifdef CONFIG_PROC_FS
19842 +/* procfs.c */
19843 +int __init au_procfs_init(void);
19844 +void au_procfs_fin(void);
19845 +#else
19846 +AuStubInt0(au_procfs_init, void);
19847 +AuStubVoid(au_procfs_fin, void);
19848 +#endif
19849 +
19850 +/* ---------------------------------------------------------------------- */
19851 +
19852 +/* kmem cache */
19853 +enum {
19854 +       AuCache_DINFO,
19855 +       AuCache_ICNTNR,
19856 +       AuCache_FINFO,
19857 +       AuCache_VDIR,
19858 +       AuCache_DEHSTR,
19859 +       AuCache_HNOTIFY, /* must be last */
19860 +       AuCache_Last
19861 +};
19862 +
19863 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
19864 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
19865 +#define AuCacheCtor(type, ctor)        \
19866 +       kmem_cache_create(#type, sizeof(struct type), \
19867 +                         __alignof__(struct type), AuCacheFlags, ctor)
19868 +
19869 +extern struct kmem_cache *au_cachep[];
19870 +
19871 +#define AuCacheFuncs(name, index) \
19872 +static inline struct au_##name *au_cache_alloc_##name(void) \
19873 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
19874 +static inline void au_cache_free_##name(struct au_##name *p) \
19875 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
19876 +
19877 +AuCacheFuncs(dinfo, DINFO);
19878 +AuCacheFuncs(icntnr, ICNTNR);
19879 +AuCacheFuncs(finfo, FINFO);
19880 +AuCacheFuncs(vdir, VDIR);
19881 +AuCacheFuncs(vdir_dehstr, DEHSTR);
19882 +#ifdef CONFIG_AUFS_HNOTIFY
19883 +AuCacheFuncs(hnotify, HNOTIFY);
19884 +#endif
19885 +
19886 +#endif /* __KERNEL__ */
19887 +#endif /* __AUFS_MODULE_H__ */
19888 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
19889 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
19890 +++ linux/fs/aufs/mvdown.c      2013-08-23 23:59:39.634916914 +0200
19891 @@ -0,0 +1,533 @@
19892 +/*
19893 + * Copyright (C) 2011-2013 Junjiro R. Okajima
19894 + *
19895 + * This program, aufs is free software; you can redistribute it and/or modify
19896 + * it under the terms of the GNU General Public License as published by
19897 + * the Free Software Foundation; either version 2 of the License, or
19898 + * (at your option) any later version.
19899 + *
19900 + * This program is distributed in the hope that it will be useful,
19901 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19902 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19903 + * GNU General Public License for more details.
19904 + *
19905 + * You should have received a copy of the GNU General Public License
19906 + * along with this program; if not, write to the Free Software
19907 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19908 + */
19909 +
19910 +#include "aufs.h"
19911 +
19912 +enum {
19913 +       AUFS_MVDOWN_SRC,
19914 +       AUFS_MVDOWN_DST,
19915 +       AUFS_MVDOWN_NARRAY
19916 +};
19917 +
19918 +struct au_mvd_args {
19919 +       struct {
19920 +               aufs_bindex_t bindex;
19921 +               struct super_block *h_sb;
19922 +               struct dentry *h_parent;
19923 +               struct au_hinode *hdir;
19924 +               struct inode *h_dir;
19925 +       } info[AUFS_MVDOWN_NARRAY];
19926 +
19927 +       struct aufs_mvdown mvdown;
19928 +       struct dentry *dentry, *parent;
19929 +       struct inode *inode, *dir;
19930 +       struct super_block *sb;
19931 +       aufs_bindex_t bopq, bwh, bfound;
19932 +       unsigned char rename_lock;
19933 +       struct au_pin pin;
19934 +};
19935 +
19936 +#define mvd_bsrc               info[AUFS_MVDOWN_SRC].bindex
19937 +#define mvd_h_src_sb           info[AUFS_MVDOWN_SRC].h_sb
19938 +#define mvd_h_src_parent       info[AUFS_MVDOWN_SRC].h_parent
19939 +#define mvd_hdir_src           info[AUFS_MVDOWN_SRC].hdir
19940 +#define mvd_h_src_dir          info[AUFS_MVDOWN_SRC].h_dir
19941 +
19942 +#define mvd_bdst               info[AUFS_MVDOWN_DST].bindex
19943 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_DST].h_sb
19944 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_DST].h_parent
19945 +#define mvd_hdir_dst           info[AUFS_MVDOWN_DST].hdir
19946 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_DST].h_dir
19947 +
19948 +#define AU_MVD_PR(flag, ...) do {                      \
19949 +               if (flag)                               \
19950 +                       pr_err(__VA_ARGS__);            \
19951 +       } while (0)
19952 +
19953 +/* make the parent dir on bdst */
19954 +static int au_do_mkdir(const unsigned char verbose, struct au_mvd_args *a)
19955 +{
19956 +       int err;
19957 +
19958 +       err = 0;
19959 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
19960 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
19961 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
19962 +       a->mvd_h_dst_parent = NULL;
19963 +       if (au_dbend(a->parent) >= a->mvd_bdst)
19964 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
19965 +       if (!a->mvd_h_dst_parent) {
19966 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
19967 +               if (unlikely(err)) {
19968 +                       AU_MVD_PR(verbose, "cpdown_dirs failed\n");
19969 +                       goto out;
19970 +               }
19971 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
19972 +       }
19973 +
19974 +out:
19975 +       AuTraceErr(err);
19976 +       return err;
19977 +}
19978 +
19979 +/* lock them all */
19980 +static int au_do_lock(const unsigned char verbose, struct au_mvd_args *a)
19981 +{
19982 +       int err;
19983 +       struct dentry *h_trap;
19984 +
19985 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
19986 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
19987 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
19988 +               a->rename_lock = 0;
19989 +               err = au_pin(&a->pin, a->dentry, a->mvd_bdst, au_opt_udba(a->sb),
19990 +                            AuPin_MNT_WRITE | AuPin_DI_LOCKED);
19991 +               if (!err) {
19992 +                       a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
19993 +                       mutex_lock_nested(&a->mvd_h_src_dir->i_mutex,
19994 +                                         AuLsc_I_PARENT3);
19995 +               } else
19996 +                       AU_MVD_PR(verbose, "pin failed\n");
19997 +               goto out;
19998 +       }
19999 +
20000 +       err = 0;
20001 +       a->rename_lock = 1;
20002 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
20003 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
20004 +       if (h_trap) {
20005 +               err = (h_trap != a->mvd_h_src_parent);
20006 +               if (err)
20007 +                       err = (h_trap != a->mvd_h_dst_parent);
20008 +       }
20009 +       BUG_ON(err); /* it should never happen */
20010 +
20011 +out:
20012 +       AuTraceErr(err);
20013 +       return err;
20014 +}
20015 +
20016 +static void au_do_unlock(const unsigned char verbose, struct au_mvd_args *a)
20017 +{
20018 +       if (!a->rename_lock) {
20019 +               mutex_unlock(&a->mvd_h_src_dir->i_mutex);
20020 +               au_unpin(&a->pin);
20021 +       } else
20022 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
20023 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
20024 +}
20025 +
20026 +/* copy-down the file */
20027 +static int au_do_cpdown(const unsigned char verbose, struct au_mvd_args *a)
20028 +{
20029 +       int err;
20030 +       struct au_cp_generic cpg = {
20031 +               .dentry = a->dentry,
20032 +               .bdst   = a->mvd_bdst,
20033 +               .bsrc   = a->mvd_bsrc,
20034 +               .len    = -1,
20035 +               .pin    = &a->pin,
20036 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
20037 +       };
20038 +
20039 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
20040 +       err = au_sio_cpdown_simple(&cpg);
20041 +       if (unlikely(err))
20042 +               AU_MVD_PR(verbose, "cpdown failed\n");
20043 +
20044 +       AuTraceErr(err);
20045 +       return err;
20046 +}
20047 +
20048 +/*
20049 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
20050 + * were sleeping
20051 + */
20052 +static int au_do_unlink_wh(const unsigned char verbose, struct au_mvd_args *a)
20053 +{
20054 +       int err;
20055 +       struct path h_path;
20056 +       struct au_branch *br;
20057 +
20058 +       br = au_sbr(a->sb, a->mvd_bdst);
20059 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
20060 +       err = PTR_ERR(h_path.dentry);
20061 +       if (IS_ERR(h_path.dentry)) {
20062 +               AU_MVD_PR(verbose, "wh_lkup failed\n");
20063 +               goto out;
20064 +       }
20065 +
20066 +       err = 0;
20067 +       if (h_path.dentry->d_inode) {
20068 +               h_path.mnt = au_br_mnt(br);
20069 +               err = vfsub_unlink(a->mvd_h_dst_parent->d_inode, &h_path,
20070 +                                  /*force*/0);
20071 +               if (unlikely(err))
20072 +                       AU_MVD_PR(verbose, "wh_unlink failed\n");
20073 +       }
20074 +       dput(h_path.dentry);
20075 +
20076 +out:
20077 +       AuTraceErr(err);
20078 +       return err;
20079 +}
20080 +
20081 +/*
20082 + * unlink the topmost h_dentry
20083 + * Note: the target file MAY be modified by UDBA between this mutex_unlock() and
20084 + *     mutex_lock() in vfs_unlink(). in this case, such changes may be lost.
20085 + */
20086 +static int au_do_unlink(const unsigned char verbose, struct au_mvd_args *a)
20087 +{
20088 +       int err;
20089 +       struct path h_path;
20090 +
20091 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
20092 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
20093 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, /*force*/0);
20094 +       if (unlikely(err))
20095 +               AU_MVD_PR(verbose, "unlink failed\n");
20096 +
20097 +       AuTraceErr(err);
20098 +       return err;
20099 +}
20100 +
20101 +/*
20102 + * copy-down the file and unlink the bsrc file.
20103 + * - unlink the bdst whout if exist
20104 + * - copy-down the file (with whtmp name and rename)
20105 + * - unlink the bsrc file
20106 + */
20107 +static int au_do_mvdown(const unsigned char verbose, struct au_mvd_args *a)
20108 +{
20109 +       int err;
20110 +
20111 +       err = au_do_mkdir(verbose, a);
20112 +       if (!err)
20113 +               err = au_do_lock(verbose, a);
20114 +       if (unlikely(err))
20115 +               goto out;
20116 +
20117 +       /*
20118 +        * do not revert the activities we made on bdst since they should be
20119 +        * harmless in aufs.
20120 +        */
20121 +
20122 +       err = au_do_cpdown(verbose, a);
20123 +       if (!err)
20124 +               err = au_do_unlink_wh(verbose, a);
20125 +       if (!err)
20126 +               err = au_do_unlink(verbose, a);
20127 +       if (unlikely(err))
20128 +               goto out_unlock;
20129 +
20130 +       /* maintain internal array */
20131 +       au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
20132 +       au_set_dbstart(a->dentry, a->mvd_bdst);
20133 +       if (au_dbend(a->dentry) < a->mvd_bdst)
20134 +               au_set_dbend(a->dentry, a->mvd_bdst);
20135 +       au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
20136 +       au_set_ibstart(a->inode, a->mvd_bdst);
20137 +       if (au_ibend(a->inode) < a->mvd_bdst)
20138 +               au_set_ibend(a->inode, a->mvd_bdst);
20139 +
20140 +out_unlock:
20141 +       au_do_unlock(verbose, a);
20142 +out:
20143 +       AuTraceErr(err);
20144 +       return err;
20145 +}
20146 +
20147 +/* ---------------------------------------------------------------------- */
20148 +
20149 +static int find_lower_writable(struct super_block *sb, aufs_bindex_t bindex)
20150 +{
20151 +       aufs_bindex_t bend;
20152 +       struct au_branch *br;
20153 +
20154 +       bend = au_sbend(sb);
20155 +       for (bindex++; bindex <= bend; bindex++) {
20156 +               br = au_sbr(sb, bindex);
20157 +               if (!au_br_rdonly(br))
20158 +                       return bindex;
20159 +       }
20160 +
20161 +       return -1;
20162 +}
20163 +
20164 +/* make sure the file is idle */
20165 +static int au_mvd_args_busy(const unsigned char verbose, struct au_mvd_args *a)
20166 +{
20167 +       int err, plinked;
20168 +       struct inode *h_src_inode;
20169 +
20170 +       err = 0;
20171 +       h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
20172 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
20173 +       if (au_dbstart(a->dentry) == a->mvd_bsrc
20174 +           && a->dentry->d_count == 1
20175 +           && atomic_read(&a->inode->i_count) == 1
20176 +           /* && h_src_inode->i_nlink == 1 */
20177 +           && (!plinked || !au_plink_test(a->inode))
20178 +           && a->inode->i_nlink == 1)
20179 +               goto out;
20180 +
20181 +       err = -EBUSY;
20182 +       AU_MVD_PR(verbose,
20183 +                 "b%d, d{b%d, c%u?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
20184 +                 a->mvd_bsrc, au_dbstart(a->dentry), a->dentry->d_count,
20185 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
20186 +                 h_src_inode->i_nlink,
20187 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
20188 +
20189 +out:
20190 +       AuTraceErr(err);
20191 +       return err;
20192 +}
20193 +
20194 +/* make sure the parent dir is fine */
20195 +static int au_mvd_args_parent(const unsigned char verbose,
20196 +                             struct au_mvd_args *a)
20197 +{
20198 +       int err;
20199 +       aufs_bindex_t bindex;
20200 +
20201 +       err = 0;
20202 +       if (unlikely(au_alive_dir(a->parent))) {
20203 +               err = -ENOENT;
20204 +               AU_MVD_PR(verbose, "parent dir is dead\n");
20205 +               goto out;
20206 +       }
20207 +
20208 +       a->bopq = au_dbdiropq(a->parent);
20209 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
20210 +       AuDbg("b%d\n", bindex);
20211 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
20212 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
20213 +               err = -EINVAL;
20214 +               AU_MVD_PR(verbose, "parent dir is opaque b%d, b%d\n",
20215 +                         a->bopq, a->mvd_bdst);
20216 +       }
20217 +
20218 +out:
20219 +       AuTraceErr(err);
20220 +       return err;
20221 +}
20222 +
20223 +static int au_mvd_args_intermediate(const unsigned char verbose,
20224 +                                   struct au_mvd_args *a)
20225 +{
20226 +       int err;
20227 +       struct au_dinfo *dinfo, *tmp;
20228 +
20229 +       /* lookup the next lower positive entry */
20230 +       err = -ENOMEM;
20231 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
20232 +       if (unlikely(!tmp))
20233 +               goto out;
20234 +
20235 +       a->bfound = -1;
20236 +       a->bwh = -1;
20237 +       dinfo = au_di(a->dentry);
20238 +       au_di_cp(tmp, dinfo);
20239 +       au_di_swap(tmp, dinfo);
20240 +
20241 +       /* returns the number of positive dentries */
20242 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
20243 +       if (!err)
20244 +               a->bwh = au_dbwh(a->dentry);
20245 +       else if (err > 0)
20246 +               a->bfound = au_dbstart(a->dentry);
20247 +
20248 +       au_di_swap(tmp, dinfo);
20249 +       au_rw_write_unlock(&tmp->di_rwsem);
20250 +       au_di_free(tmp);
20251 +       if (unlikely(err < 0))
20252 +               AU_MVD_PR(verbose, "failed look-up lower\n");
20253 +
20254 +       /*
20255 +        * here, we have these cases.
20256 +        * bfound == -1
20257 +        *      no positive dentry under bsrc. there are more sub-cases.
20258 +        *      bwh < 0
20259 +        *              there no whiteout, we can safely move-down.
20260 +        *      bwh <= bsrc
20261 +        *              impossible
20262 +        *      bsrc < bwh && bwh < bdst
20263 +        *              there is a whiteout on RO branch. cannot proceed.
20264 +        *      bwh == bdst
20265 +        *              there is a whiteout on the RW target branch. it should
20266 +        *              be removed.
20267 +        *      bdst < bwh
20268 +        *              there is a whiteout somewhere unrelated branch.
20269 +        * -1 < bfound && bfound <= bsrc
20270 +        *      impossible.
20271 +        * bfound < bdst
20272 +        *      found, but it is on RO branch between bsrc and bdst. cannot
20273 +        *      proceed.
20274 +        * bfound == bdst
20275 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
20276 +        *      error.
20277 +        * bdst < bfound
20278 +        *      found, after we create the file on bdst, it will be hidden.
20279 +        */
20280 +
20281 +       AuDebugOn(a->bfound == -1
20282 +                 && a->bwh != -1
20283 +                 && a->bwh <= a->mvd_bsrc);
20284 +       AuDebugOn(-1 < a->bfound
20285 +                 && a->bfound <= a->mvd_bsrc);
20286 +
20287 +       err = -EINVAL;
20288 +       if (a->bfound == -1
20289 +           && a->mvd_bsrc < a->bwh
20290 +           && a->bwh != -1
20291 +           && a->bwh < a->mvd_bdst) {
20292 +               AU_MVD_PR(verbose, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
20293 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
20294 +               goto out;
20295 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
20296 +               AU_MVD_PR(verbose, "bdst %d, bfound %d\n",
20297 +                         a->mvd_bdst, a->bfound);
20298 +               goto out;
20299 +       }
20300 +
20301 +       err = 0; /* success */
20302 +
20303 +out:
20304 +       AuTraceErr(err);
20305 +       return err;
20306 +}
20307 +
20308 +static int au_mvd_args_exist(const unsigned char verbose, struct au_mvd_args *a)
20309 +{
20310 +       int err;
20311 +
20312 +       err = (a->bfound != a->mvd_bdst) ? 0 : -EEXIST;
20313 +       AuTraceErr(err);
20314 +       return err;
20315 +}
20316 +
20317 +static int au_mvd_args(const unsigned char verbose, struct au_mvd_args *a)
20318 +{
20319 +       int err;
20320 +       struct au_branch *br;
20321 +
20322 +       err = -EISDIR;
20323 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
20324 +               goto out;
20325 +
20326 +       err = -EINVAL;
20327 +       a->mvd_bsrc = au_ibstart(a->inode);
20328 +       if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
20329 +               AU_MVD_PR(verbose, "on the bottom\n");
20330 +               goto out;
20331 +       }
20332 +       br = au_sbr(a->sb, a->mvd_bsrc);
20333 +       err = au_br_rdonly(br);
20334 +       if (unlikely(err))
20335 +               goto out;
20336 +
20337 +       err = -EINVAL;
20338 +       a->mvd_bdst = find_lower_writable(a->sb, a->mvd_bsrc);
20339 +       if (unlikely(a->mvd_bdst < 0)) {
20340 +               AU_MVD_PR(verbose, "no writable lower branch\n");
20341 +               goto out;
20342 +       }
20343 +
20344 +       err = au_mvd_args_busy(verbose, a);
20345 +       if (!err)
20346 +               err = au_mvd_args_parent(verbose, a);
20347 +       if (!err)
20348 +               err = au_mvd_args_intermediate(verbose, a);
20349 +       if (!err)
20350 +               err = au_mvd_args_exist(verbose, a);
20351 +       if (!err)
20352 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
20353 +
20354 +out:
20355 +       AuTraceErr(err);
20356 +       return err;
20357 +}
20358 +
20359 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
20360 +{
20361 +       int err;
20362 +       unsigned char verbose;
20363 +       struct au_mvd_args args = {
20364 +               .dentry = dentry,
20365 +               .inode  = dentry->d_inode,
20366 +               .sb     = dentry->d_sb
20367 +       };
20368 +
20369 +       err = -EPERM;
20370 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
20371 +               goto out;
20372 +
20373 +       err = copy_from_user(&args.mvdown, uarg, sizeof(args.mvdown));
20374 +       if (unlikely(err)) {
20375 +               err = -EFAULT;
20376 +               goto out;
20377 +       }
20378 +       AuDbg("flags 0x%x\n", args.mvdown.flags);
20379 +
20380 +       err = -EBUSY;
20381 +       verbose = !!(args.mvdown.flags & AUFS_MVDOWN_VERBOSE);
20382 +       args.parent = dget_parent(dentry);
20383 +       args.dir = args.parent->d_inode;
20384 +       mutex_lock_nested(&args.dir->i_mutex, I_MUTEX_PARENT);
20385 +       dput(args.parent);
20386 +       if (unlikely(args.parent != dentry->d_parent)) {
20387 +               AU_MVD_PR(verbose, "parent dir is moved\n");
20388 +               goto out_dir;
20389 +       }
20390 +
20391 +       mutex_lock_nested(&args.inode->i_mutex, I_MUTEX_CHILD);
20392 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
20393 +       if (unlikely(err))
20394 +               goto out_inode;
20395 +
20396 +       di_write_lock_parent(args.parent);
20397 +       err = au_mvd_args(verbose, &args);
20398 +       if (unlikely(err))
20399 +               goto out_parent;
20400 +
20401 +       AuDbgDentry(dentry);
20402 +       AuDbgInode(args.inode);
20403 +       err = au_do_mvdown(verbose, &args);
20404 +       if (unlikely(err))
20405 +               goto out_parent;
20406 +       AuDbgDentry(dentry);
20407 +       AuDbgInode(args.inode);
20408 +
20409 +       au_cpup_attr_timesizes(args.dir);
20410 +       au_cpup_attr_timesizes(args.inode);
20411 +       au_cpup_igen(args.inode, au_h_iptr(args.inode, args.mvd_bdst));
20412 +       /* au_digen_dec(dentry); */
20413 +
20414 +out_parent:
20415 +       di_write_unlock(args.parent);
20416 +       aufs_read_unlock(dentry, AuLock_DW);
20417 +out_inode:
20418 +       mutex_unlock(&args.inode->i_mutex);
20419 +out_dir:
20420 +       mutex_unlock(&args.dir->i_mutex);
20421 +out:
20422 +       AuTraceErr(err);
20423 +       return err;
20424 +}
20425 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
20426 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
20427 +++ linux/fs/aufs/opts.c        2013-07-06 13:20:47.753531903 +0200
20428 @@ -0,0 +1,1697 @@
20429 +/*
20430 + * Copyright (C) 2005-2013 Junjiro R. Okajima
20431 + *
20432 + * This program, aufs is free software; you can redistribute it and/or modify
20433 + * it under the terms of the GNU General Public License as published by
20434 + * the Free Software Foundation; either version 2 of the License, or
20435 + * (at your option) any later version.
20436 + *
20437 + * This program is distributed in the hope that it will be useful,
20438 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20439 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20440 + * GNU General Public License for more details.
20441 + *
20442 + * You should have received a copy of the GNU General Public License
20443 + * along with this program; if not, write to the Free Software
20444 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20445 + */
20446 +
20447 +/*
20448 + * mount options/flags
20449 + */
20450 +
20451 +#include <linux/namei.h>
20452 +#include <linux/types.h> /* a distribution requires */
20453 +#include <linux/parser.h>
20454 +#include "aufs.h"
20455 +
20456 +/* ---------------------------------------------------------------------- */
20457 +
20458 +enum {
20459 +       Opt_br,
20460 +       Opt_add, Opt_del, Opt_mod, Opt_reorder, Opt_append, Opt_prepend,
20461 +       Opt_idel, Opt_imod, Opt_ireorder,
20462 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash, Opt_rendir,
20463 +       Opt_rdblk_def, Opt_rdhash_def,
20464 +       Opt_xino, Opt_zxino, Opt_noxino,
20465 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
20466 +       Opt_trunc_xino_path, Opt_itrunc_xino,
20467 +       Opt_trunc_xib, Opt_notrunc_xib,
20468 +       Opt_shwh, Opt_noshwh,
20469 +       Opt_plink, Opt_noplink, Opt_list_plink,
20470 +       Opt_udba,
20471 +       Opt_dio, Opt_nodio,
20472 +       /* Opt_lock, Opt_unlock, */
20473 +       Opt_cmd, Opt_cmd_args,
20474 +       Opt_diropq_a, Opt_diropq_w,
20475 +       Opt_warn_perm, Opt_nowarn_perm,
20476 +       Opt_wbr_copyup, Opt_wbr_create,
20477 +       Opt_refrof, Opt_norefrof,
20478 +       Opt_verbose, Opt_noverbose,
20479 +       Opt_sum, Opt_nosum, Opt_wsum,
20480 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
20481 +};
20482 +
20483 +static match_table_t options = {
20484 +       {Opt_br, "br=%s"},
20485 +       {Opt_br, "br:%s"},
20486 +
20487 +       {Opt_add, "add=%d:%s"},
20488 +       {Opt_add, "add:%d:%s"},
20489 +       {Opt_add, "ins=%d:%s"},
20490 +       {Opt_add, "ins:%d:%s"},
20491 +       {Opt_append, "append=%s"},
20492 +       {Opt_append, "append:%s"},
20493 +       {Opt_prepend, "prepend=%s"},
20494 +       {Opt_prepend, "prepend:%s"},
20495 +
20496 +       {Opt_del, "del=%s"},
20497 +       {Opt_del, "del:%s"},
20498 +       /* {Opt_idel, "idel:%d"}, */
20499 +       {Opt_mod, "mod=%s"},
20500 +       {Opt_mod, "mod:%s"},
20501 +       /* {Opt_imod, "imod:%d:%s"}, */
20502 +
20503 +       {Opt_dirwh, "dirwh=%d"},
20504 +
20505 +       {Opt_xino, "xino=%s"},
20506 +       {Opt_noxino, "noxino"},
20507 +       {Opt_trunc_xino, "trunc_xino"},
20508 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
20509 +       {Opt_notrunc_xino, "notrunc_xino"},
20510 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
20511 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
20512 +       /* {Opt_zxino, "zxino=%s"}, */
20513 +       {Opt_trunc_xib, "trunc_xib"},
20514 +       {Opt_notrunc_xib, "notrunc_xib"},
20515 +
20516 +#ifdef CONFIG_PROC_FS
20517 +       {Opt_plink, "plink"},
20518 +#else
20519 +       {Opt_ignore_silent, "plink"},
20520 +#endif
20521 +
20522 +       {Opt_noplink, "noplink"},
20523 +
20524 +#ifdef CONFIG_AUFS_DEBUG
20525 +       {Opt_list_plink, "list_plink"},
20526 +#endif
20527 +
20528 +       {Opt_udba, "udba=%s"},
20529 +
20530 +       {Opt_dio, "dio"},
20531 +       {Opt_nodio, "nodio"},
20532 +
20533 +       {Opt_diropq_a, "diropq=always"},
20534 +       {Opt_diropq_a, "diropq=a"},
20535 +       {Opt_diropq_w, "diropq=whiteouted"},
20536 +       {Opt_diropq_w, "diropq=w"},
20537 +
20538 +       {Opt_warn_perm, "warn_perm"},
20539 +       {Opt_nowarn_perm, "nowarn_perm"},
20540 +
20541 +       /* keep them temporary */
20542 +       {Opt_ignore_silent, "coo=%s"},
20543 +       {Opt_ignore_silent, "nodlgt"},
20544 +       {Opt_ignore_silent, "nodirperm1"},
20545 +       {Opt_ignore_silent, "clean_plink"},
20546 +
20547 +#ifdef CONFIG_AUFS_SHWH
20548 +       {Opt_shwh, "shwh"},
20549 +#endif
20550 +       {Opt_noshwh, "noshwh"},
20551 +
20552 +       {Opt_rendir, "rendir=%d"},
20553 +
20554 +       {Opt_refrof, "refrof"},
20555 +       {Opt_norefrof, "norefrof"},
20556 +
20557 +       {Opt_verbose, "verbose"},
20558 +       {Opt_verbose, "v"},
20559 +       {Opt_noverbose, "noverbose"},
20560 +       {Opt_noverbose, "quiet"},
20561 +       {Opt_noverbose, "q"},
20562 +       {Opt_noverbose, "silent"},
20563 +
20564 +       {Opt_sum, "sum"},
20565 +       {Opt_nosum, "nosum"},
20566 +       {Opt_wsum, "wsum"},
20567 +
20568 +       {Opt_rdcache, "rdcache=%d"},
20569 +       {Opt_rdblk, "rdblk=%d"},
20570 +       {Opt_rdblk_def, "rdblk=def"},
20571 +       {Opt_rdhash, "rdhash=%d"},
20572 +       {Opt_rdhash_def, "rdhash=def"},
20573 +
20574 +       {Opt_wbr_create, "create=%s"},
20575 +       {Opt_wbr_create, "create_policy=%s"},
20576 +       {Opt_wbr_copyup, "cpup=%s"},
20577 +       {Opt_wbr_copyup, "copyup=%s"},
20578 +       {Opt_wbr_copyup, "copyup_policy=%s"},
20579 +
20580 +       /* internal use for the scripts */
20581 +       {Opt_ignore_silent, "si=%s"},
20582 +
20583 +       {Opt_br, "dirs=%s"},
20584 +       {Opt_ignore, "debug=%d"},
20585 +       {Opt_ignore, "delete=whiteout"},
20586 +       {Opt_ignore, "delete=all"},
20587 +       {Opt_ignore, "imap=%s"},
20588 +
20589 +       /* temporary workaround, due to old mount(8)? */
20590 +       {Opt_ignore_silent, "relatime"},
20591 +
20592 +       {Opt_err, NULL}
20593 +};
20594 +
20595 +/* ---------------------------------------------------------------------- */
20596 +
20597 +static const char *au_parser_pattern(int val, struct match_token *token)
20598 +{
20599 +       while (token->pattern) {
20600 +               if (token->token == val)
20601 +                       return token->pattern;
20602 +               token++;
20603 +       }
20604 +       BUG();
20605 +       return "??";
20606 +}
20607 +
20608 +/* ---------------------------------------------------------------------- */
20609 +
20610 +static match_table_t brperm = {
20611 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
20612 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
20613 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
20614 +       {0, NULL}
20615 +};
20616 +
20617 +static match_table_t brattr = {
20618 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
20619 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
20620 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
20621 +       {0, NULL}
20622 +};
20623 +
20624 +#define AuBrStr_LONGEST        AUFS_BRPERM_RW \
20625 +       "+" AUFS_BRATTR_UNPIN \
20626 +       "+" AUFS_BRWATTR_NLWH
20627 +
20628 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
20629 +{
20630 +       int attr, v;
20631 +       char *p;
20632 +
20633 +       attr = 0;
20634 +       do {
20635 +               p = strchr(str, '+');
20636 +               if (p)
20637 +                       *p = 0;
20638 +               v = match_token(str, table, args);
20639 +               if (v)
20640 +                       attr |= v;
20641 +               else {
20642 +                       if (p)
20643 +                               *p = '+';
20644 +                       pr_warn("ignored branch attribute %s\n", str);
20645 +                       break;
20646 +               }
20647 +               if (p)
20648 +                       str = p + 1;
20649 +       } while (p);
20650 +
20651 +       return attr;
20652 +}
20653 +
20654 +static int noinline_for_stack br_perm_val(char *perm)
20655 +{
20656 +       int val;
20657 +       char *p, *q;
20658 +       substring_t args[MAX_OPT_ARGS];
20659 +
20660 +       p = strchr(perm, '+');
20661 +       if (p)
20662 +               *p = 0;
20663 +       val = match_token(perm, brperm, args);
20664 +       if (!val) {
20665 +               if (p)
20666 +                       *p = '+';
20667 +               pr_warn("ignored branch permission %s\n", perm);
20668 +               val = AuBrPerm_RO;
20669 +               goto out;
20670 +       }
20671 +       if (!p)
20672 +               goto out;
20673 +
20674 +       p++;
20675 +       while (1) {
20676 +               q = strchr(p, '+');
20677 +               if (q)
20678 +                       *q = 0;
20679 +               val |= br_attr_val(p, brattr, args);
20680 +               if (q) {
20681 +                       *q = '+';
20682 +                       p = q + 1;
20683 +               } else
20684 +                       break;
20685 +       }
20686 +       switch (val & AuBrPerm_Mask) {
20687 +       case AuBrPerm_RO:
20688 +       case AuBrPerm_RR:
20689 +               if (unlikely(val & AuBrWAttr_NoLinkWH)) {
20690 +                       pr_warn("ignored branch attribute %s\n",
20691 +                               AUFS_BRWATTR_NLWH);
20692 +                       val &= ~AuBrWAttr_NoLinkWH;
20693 +               }
20694 +               break;
20695 +       case AuBrPerm_RW:
20696 +               if (unlikely(val & AuBrRAttr_WH)) {
20697 +                       pr_warn("ignored branch attribute %s\n",
20698 +                               AUFS_BRRATTR_WH);
20699 +                       val &= ~AuBrRAttr_WH;
20700 +               }
20701 +               break;
20702 +       }
20703 +
20704 +out:
20705 +       return val;
20706 +}
20707 +
20708 +/* Caller should free the return value */
20709 +char *au_optstr_br_perm(int brperm)
20710 +{
20711 +       char *p, a[sizeof(AuBrStr_LONGEST)];
20712 +       int sz;
20713 +
20714 +#define SetPerm(str) do {                      \
20715 +               sz = sizeof(str);               \
20716 +               memcpy(a, str, sz);             \
20717 +               p = a + sz - 1;                 \
20718 +       } while (0)
20719 +
20720 +#define AppendAttr(flag, str) do {                     \
20721 +               if (brperm & flag) {            \
20722 +                       sz = sizeof(str);       \
20723 +                       *p++ = '+';             \
20724 +                       memcpy(p, str, sz);     \
20725 +                       p += sz - 1;            \
20726 +               }                               \
20727 +       } while (0)
20728 +
20729 +       switch (brperm & AuBrPerm_Mask) {
20730 +       case AuBrPerm_RO:
20731 +               SetPerm(AUFS_BRPERM_RO);
20732 +               break;
20733 +       case AuBrPerm_RR:
20734 +               SetPerm(AUFS_BRPERM_RR);
20735 +               break;
20736 +       case AuBrPerm_RW:
20737 +               SetPerm(AUFS_BRPERM_RW);
20738 +               break;
20739 +       default:
20740 +               AuDebugOn(1);
20741 +       }
20742 +
20743 +       AppendAttr(AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN);
20744 +       AppendAttr(AuBrRAttr_WH, AUFS_BRRATTR_WH);
20745 +       AppendAttr(AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH);
20746 +
20747 +       AuDebugOn(strlen(a) >= sizeof(a));
20748 +       return kstrdup(a, GFP_NOFS);
20749 +#undef SetPerm
20750 +#undef AppendAttr
20751 +}
20752 +
20753 +/* ---------------------------------------------------------------------- */
20754 +
20755 +static match_table_t udbalevel = {
20756 +       {AuOpt_UDBA_REVAL, "reval"},
20757 +       {AuOpt_UDBA_NONE, "none"},
20758 +#ifdef CONFIG_AUFS_HNOTIFY
20759 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
20760 +#ifdef CONFIG_AUFS_HFSNOTIFY
20761 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
20762 +#endif
20763 +#endif
20764 +       {-1, NULL}
20765 +};
20766 +
20767 +static int noinline_for_stack udba_val(char *str)
20768 +{
20769 +       substring_t args[MAX_OPT_ARGS];
20770 +
20771 +       return match_token(str, udbalevel, args);
20772 +}
20773 +
20774 +const char *au_optstr_udba(int udba)
20775 +{
20776 +       return au_parser_pattern(udba, (void *)udbalevel);
20777 +}
20778 +
20779 +/* ---------------------------------------------------------------------- */
20780 +
20781 +static match_table_t au_wbr_create_policy = {
20782 +       {AuWbrCreate_TDP, "tdp"},
20783 +       {AuWbrCreate_TDP, "top-down-parent"},
20784 +       {AuWbrCreate_RR, "rr"},
20785 +       {AuWbrCreate_RR, "round-robin"},
20786 +       {AuWbrCreate_MFS, "mfs"},
20787 +       {AuWbrCreate_MFS, "most-free-space"},
20788 +       {AuWbrCreate_MFSV, "mfs:%d"},
20789 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
20790 +
20791 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
20792 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
20793 +       {AuWbrCreate_PMFS, "pmfs"},
20794 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
20795 +
20796 +       {-1, NULL}
20797 +};
20798 +
20799 +/*
20800 + * cf. linux/lib/parser.c and cmdline.c
20801 + * gave up calling memparse() since it uses simple_strtoull() instead of
20802 + * kstrto...().
20803 + */
20804 +static int noinline_for_stack
20805 +au_match_ull(substring_t *s, unsigned long long *result)
20806 +{
20807 +       int err;
20808 +       unsigned int len;
20809 +       char a[32];
20810 +
20811 +       err = -ERANGE;
20812 +       len = s->to - s->from;
20813 +       if (len + 1 <= sizeof(a)) {
20814 +               memcpy(a, s->from, len);
20815 +               a[len] = '\0';
20816 +               err = kstrtoull(a, 0, result);
20817 +       }
20818 +       return err;
20819 +}
20820 +
20821 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
20822 +                           struct au_opt_wbr_create *create)
20823 +{
20824 +       int err;
20825 +       unsigned long long ull;
20826 +
20827 +       err = 0;
20828 +       if (!au_match_ull(arg, &ull))
20829 +               create->mfsrr_watermark = ull;
20830 +       else {
20831 +               pr_err("bad integer in %s\n", str);
20832 +               err = -EINVAL;
20833 +       }
20834 +
20835 +       return err;
20836 +}
20837 +
20838 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
20839 +                         struct au_opt_wbr_create *create)
20840 +{
20841 +       int n, err;
20842 +
20843 +       err = 0;
20844 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
20845 +               create->mfs_second = n;
20846 +       else {
20847 +               pr_err("bad integer in %s\n", str);
20848 +               err = -EINVAL;
20849 +       }
20850 +
20851 +       return err;
20852 +}
20853 +
20854 +static int noinline_for_stack
20855 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
20856 +{
20857 +       int err, e;
20858 +       substring_t args[MAX_OPT_ARGS];
20859 +
20860 +       err = match_token(str, au_wbr_create_policy, args);
20861 +       create->wbr_create = err;
20862 +       switch (err) {
20863 +       case AuWbrCreate_MFSRRV:
20864 +               e = au_wbr_mfs_wmark(&args[0], str, create);
20865 +               if (!e)
20866 +                       e = au_wbr_mfs_sec(&args[1], str, create);
20867 +               if (unlikely(e))
20868 +                       err = e;
20869 +               break;
20870 +       case AuWbrCreate_MFSRR:
20871 +               e = au_wbr_mfs_wmark(&args[0], str, create);
20872 +               if (unlikely(e)) {
20873 +                       err = e;
20874 +                       break;
20875 +               }
20876 +               /*FALLTHROUGH*/
20877 +       case AuWbrCreate_MFS:
20878 +       case AuWbrCreate_PMFS:
20879 +               create->mfs_second = AUFS_MFS_DEF_SEC;
20880 +               break;
20881 +       case AuWbrCreate_MFSV:
20882 +       case AuWbrCreate_PMFSV:
20883 +               e = au_wbr_mfs_sec(&args[0], str, create);
20884 +               if (unlikely(e))
20885 +                       err = e;
20886 +               break;
20887 +       }
20888 +
20889 +       return err;
20890 +}
20891 +
20892 +const char *au_optstr_wbr_create(int wbr_create)
20893 +{
20894 +       return au_parser_pattern(wbr_create, (void *)au_wbr_create_policy);
20895 +}
20896 +
20897 +static match_table_t au_wbr_copyup_policy = {
20898 +       {AuWbrCopyup_TDP, "tdp"},
20899 +       {AuWbrCopyup_TDP, "top-down-parent"},
20900 +       {AuWbrCopyup_BUP, "bup"},
20901 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
20902 +       {AuWbrCopyup_BU, "bu"},
20903 +       {AuWbrCopyup_BU, "bottom-up"},
20904 +       {-1, NULL}
20905 +};
20906 +
20907 +static int noinline_for_stack au_wbr_copyup_val(char *str)
20908 +{
20909 +       substring_t args[MAX_OPT_ARGS];
20910 +
20911 +       return match_token(str, au_wbr_copyup_policy, args);
20912 +}
20913 +
20914 +const char *au_optstr_wbr_copyup(int wbr_copyup)
20915 +{
20916 +       return au_parser_pattern(wbr_copyup, (void *)au_wbr_copyup_policy);
20917 +}
20918 +
20919 +/* ---------------------------------------------------------------------- */
20920 +
20921 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
20922 +
20923 +static void dump_opts(struct au_opts *opts)
20924 +{
20925 +#ifdef CONFIG_AUFS_DEBUG
20926 +       /* reduce stack space */
20927 +       union {
20928 +               struct au_opt_add *add;
20929 +               struct au_opt_del *del;
20930 +               struct au_opt_mod *mod;
20931 +               struct au_opt_xino *xino;
20932 +               struct au_opt_xino_itrunc *xino_itrunc;
20933 +               struct au_opt_wbr_create *create;
20934 +       } u;
20935 +       struct au_opt *opt;
20936 +
20937 +       opt = opts->opt;
20938 +       while (opt->type != Opt_tail) {
20939 +               switch (opt->type) {
20940 +               case Opt_add:
20941 +                       u.add = &opt->add;
20942 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
20943 +                                 u.add->bindex, u.add->pathname, u.add->perm,
20944 +                                 u.add->path.dentry);
20945 +                       break;
20946 +               case Opt_del:
20947 +               case Opt_idel:
20948 +                       u.del = &opt->del;
20949 +                       AuDbg("del {%s, %p}\n",
20950 +                             u.del->pathname, u.del->h_path.dentry);
20951 +                       break;
20952 +               case Opt_mod:
20953 +               case Opt_imod:
20954 +                       u.mod = &opt->mod;
20955 +                       AuDbg("mod {%s, 0x%x, %p}\n",
20956 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
20957 +                       break;
20958 +               case Opt_append:
20959 +                       u.add = &opt->add;
20960 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
20961 +                                 u.add->bindex, u.add->pathname, u.add->perm,
20962 +                                 u.add->path.dentry);
20963 +                       break;
20964 +               case Opt_prepend:
20965 +                       u.add = &opt->add;
20966 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
20967 +                                 u.add->bindex, u.add->pathname, u.add->perm,
20968 +                                 u.add->path.dentry);
20969 +                       break;
20970 +               case Opt_dirwh:
20971 +                       AuDbg("dirwh %d\n", opt->dirwh);
20972 +                       break;
20973 +               case Opt_rdcache:
20974 +                       AuDbg("rdcache %d\n", opt->rdcache);
20975 +                       break;
20976 +               case Opt_rdblk:
20977 +                       AuDbg("rdblk %u\n", opt->rdblk);
20978 +                       break;
20979 +               case Opt_rdblk_def:
20980 +                       AuDbg("rdblk_def\n");
20981 +                       break;
20982 +               case Opt_rdhash:
20983 +                       AuDbg("rdhash %u\n", opt->rdhash);
20984 +                       break;
20985 +               case Opt_rdhash_def:
20986 +                       AuDbg("rdhash_def\n");
20987 +                       break;
20988 +               case Opt_xino:
20989 +                       u.xino = &opt->xino;
20990 +                       AuDbg("xino {%s %.*s}\n",
20991 +                                 u.xino->path,
20992 +                                 AuDLNPair(u.xino->file->f_dentry));
20993 +                       break;
20994 +               case Opt_trunc_xino:
20995 +                       AuLabel(trunc_xino);
20996 +                       break;
20997 +               case Opt_notrunc_xino:
20998 +                       AuLabel(notrunc_xino);
20999 +                       break;
21000 +               case Opt_trunc_xino_path:
21001 +               case Opt_itrunc_xino:
21002 +                       u.xino_itrunc = &opt->xino_itrunc;
21003 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
21004 +                       break;
21005 +
21006 +               case Opt_noxino:
21007 +                       AuLabel(noxino);
21008 +                       break;
21009 +               case Opt_trunc_xib:
21010 +                       AuLabel(trunc_xib);
21011 +                       break;
21012 +               case Opt_notrunc_xib:
21013 +                       AuLabel(notrunc_xib);
21014 +                       break;
21015 +               case Opt_shwh:
21016 +                       AuLabel(shwh);
21017 +                       break;
21018 +               case Opt_noshwh:
21019 +                       AuLabel(noshwh);
21020 +                       break;
21021 +               case Opt_plink:
21022 +                       AuLabel(plink);
21023 +                       break;
21024 +               case Opt_noplink:
21025 +                       AuLabel(noplink);
21026 +                       break;
21027 +               case Opt_list_plink:
21028 +                       AuLabel(list_plink);
21029 +                       break;
21030 +               case Opt_udba:
21031 +                       AuDbg("udba %d, %s\n",
21032 +                                 opt->udba, au_optstr_udba(opt->udba));
21033 +                       break;
21034 +               case Opt_dio:
21035 +                       AuLabel(dio);
21036 +                       break;
21037 +               case Opt_nodio:
21038 +                       AuLabel(nodio);
21039 +                       break;
21040 +               case Opt_diropq_a:
21041 +                       AuLabel(diropq_a);
21042 +                       break;
21043 +               case Opt_diropq_w:
21044 +                       AuLabel(diropq_w);
21045 +                       break;
21046 +               case Opt_warn_perm:
21047 +                       AuLabel(warn_perm);
21048 +                       break;
21049 +               case Opt_nowarn_perm:
21050 +                       AuLabel(nowarn_perm);
21051 +                       break;
21052 +               case Opt_refrof:
21053 +                       AuLabel(refrof);
21054 +                       break;
21055 +               case Opt_norefrof:
21056 +                       AuLabel(norefrof);
21057 +                       break;
21058 +               case Opt_verbose:
21059 +                       AuLabel(verbose);
21060 +                       break;
21061 +               case Opt_noverbose:
21062 +                       AuLabel(noverbose);
21063 +                       break;
21064 +               case Opt_sum:
21065 +                       AuLabel(sum);
21066 +                       break;
21067 +               case Opt_nosum:
21068 +                       AuLabel(nosum);
21069 +                       break;
21070 +               case Opt_wsum:
21071 +                       AuLabel(wsum);
21072 +                       break;
21073 +               case Opt_wbr_create:
21074 +                       u.create = &opt->wbr_create;
21075 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
21076 +                                 au_optstr_wbr_create(u.create->wbr_create));
21077 +                       switch (u.create->wbr_create) {
21078 +                       case AuWbrCreate_MFSV:
21079 +                       case AuWbrCreate_PMFSV:
21080 +                               AuDbg("%d sec\n", u.create->mfs_second);
21081 +                               break;
21082 +                       case AuWbrCreate_MFSRR:
21083 +                               AuDbg("%llu watermark\n",
21084 +                                         u.create->mfsrr_watermark);
21085 +                               break;
21086 +                       case AuWbrCreate_MFSRRV:
21087 +                               AuDbg("%llu watermark, %d sec\n",
21088 +                                         u.create->mfsrr_watermark,
21089 +                                         u.create->mfs_second);
21090 +                               break;
21091 +                       }
21092 +                       break;
21093 +               case Opt_wbr_copyup:
21094 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
21095 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
21096 +                       break;
21097 +               default:
21098 +                       BUG();
21099 +               }
21100 +               opt++;
21101 +       }
21102 +#endif
21103 +}
21104 +
21105 +void au_opts_free(struct au_opts *opts)
21106 +{
21107 +       struct au_opt *opt;
21108 +
21109 +       opt = opts->opt;
21110 +       while (opt->type != Opt_tail) {
21111 +               switch (opt->type) {
21112 +               case Opt_add:
21113 +               case Opt_append:
21114 +               case Opt_prepend:
21115 +                       path_put(&opt->add.path);
21116 +                       break;
21117 +               case Opt_del:
21118 +               case Opt_idel:
21119 +                       path_put(&opt->del.h_path);
21120 +                       break;
21121 +               case Opt_mod:
21122 +               case Opt_imod:
21123 +                       dput(opt->mod.h_root);
21124 +                       break;
21125 +               case Opt_xino:
21126 +                       fput(opt->xino.file);
21127 +                       break;
21128 +               }
21129 +               opt++;
21130 +       }
21131 +}
21132 +
21133 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
21134 +                  aufs_bindex_t bindex)
21135 +{
21136 +       int err;
21137 +       struct au_opt_add *add = &opt->add;
21138 +       char *p;
21139 +
21140 +       add->bindex = bindex;
21141 +       add->perm = AuBrPerm_RO;
21142 +       add->pathname = opt_str;
21143 +       p = strchr(opt_str, '=');
21144 +       if (p) {
21145 +               *p++ = 0;
21146 +               if (*p)
21147 +                       add->perm = br_perm_val(p);
21148 +       }
21149 +
21150 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
21151 +       if (!err) {
21152 +               if (!p) {
21153 +                       add->perm = AuBrPerm_RO;
21154 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
21155 +                               add->perm = AuBrPerm_RR;
21156 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
21157 +                               add->perm = AuBrPerm_RW;
21158 +               }
21159 +               opt->type = Opt_add;
21160 +               goto out;
21161 +       }
21162 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
21163 +       err = -EINVAL;
21164 +
21165 +out:
21166 +       return err;
21167 +}
21168 +
21169 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
21170 +{
21171 +       int err;
21172 +
21173 +       del->pathname = args[0].from;
21174 +       AuDbg("del path %s\n", del->pathname);
21175 +
21176 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
21177 +       if (unlikely(err))
21178 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
21179 +
21180 +       return err;
21181 +}
21182 +
21183 +#if 0 /* reserved for future use */
21184 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
21185 +                             struct au_opt_del *del, substring_t args[])
21186 +{
21187 +       int err;
21188 +       struct dentry *root;
21189 +
21190 +       err = -EINVAL;
21191 +       root = sb->s_root;
21192 +       aufs_read_lock(root, AuLock_FLUSH);
21193 +       if (bindex < 0 || au_sbend(sb) < bindex) {
21194 +               pr_err("out of bounds, %d\n", bindex);
21195 +               goto out;
21196 +       }
21197 +
21198 +       err = 0;
21199 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
21200 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
21201 +
21202 +out:
21203 +       aufs_read_unlock(root, !AuLock_IR);
21204 +       return err;
21205 +}
21206 +#endif
21207 +
21208 +static int noinline_for_stack
21209 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
21210 +{
21211 +       int err;
21212 +       struct path path;
21213 +       char *p;
21214 +
21215 +       err = -EINVAL;
21216 +       mod->path = args[0].from;
21217 +       p = strchr(mod->path, '=');
21218 +       if (unlikely(!p)) {
21219 +               pr_err("no permssion %s\n", args[0].from);
21220 +               goto out;
21221 +       }
21222 +
21223 +       *p++ = 0;
21224 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
21225 +       if (unlikely(err)) {
21226 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
21227 +               goto out;
21228 +       }
21229 +
21230 +       mod->perm = br_perm_val(p);
21231 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
21232 +       mod->h_root = dget(path.dentry);
21233 +       path_put(&path);
21234 +
21235 +out:
21236 +       return err;
21237 +}
21238 +
21239 +#if 0 /* reserved for future use */
21240 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
21241 +                             struct au_opt_mod *mod, substring_t args[])
21242 +{
21243 +       int err;
21244 +       struct dentry *root;
21245 +
21246 +       err = -EINVAL;
21247 +       root = sb->s_root;
21248 +       aufs_read_lock(root, AuLock_FLUSH);
21249 +       if (bindex < 0 || au_sbend(sb) < bindex) {
21250 +               pr_err("out of bounds, %d\n", bindex);
21251 +               goto out;
21252 +       }
21253 +
21254 +       err = 0;
21255 +       mod->perm = br_perm_val(args[1].from);
21256 +       AuDbg("mod path %s, perm 0x%x, %s\n",
21257 +             mod->path, mod->perm, args[1].from);
21258 +       mod->h_root = dget(au_h_dptr(root, bindex));
21259 +
21260 +out:
21261 +       aufs_read_unlock(root, !AuLock_IR);
21262 +       return err;
21263 +}
21264 +#endif
21265 +
21266 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
21267 +                             substring_t args[])
21268 +{
21269 +       int err;
21270 +       struct file *file;
21271 +
21272 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
21273 +       err = PTR_ERR(file);
21274 +       if (IS_ERR(file))
21275 +               goto out;
21276 +
21277 +       err = -EINVAL;
21278 +       if (unlikely(file->f_dentry->d_sb == sb)) {
21279 +               fput(file);
21280 +               pr_err("%s must be outside\n", args[0].from);
21281 +               goto out;
21282 +       }
21283 +
21284 +       err = 0;
21285 +       xino->file = file;
21286 +       xino->path = args[0].from;
21287 +
21288 +out:
21289 +       return err;
21290 +}
21291 +
21292 +static int noinline_for_stack
21293 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
21294 +                              struct au_opt_xino_itrunc *xino_itrunc,
21295 +                              substring_t args[])
21296 +{
21297 +       int err;
21298 +       aufs_bindex_t bend, bindex;
21299 +       struct path path;
21300 +       struct dentry *root;
21301 +
21302 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
21303 +       if (unlikely(err)) {
21304 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
21305 +               goto out;
21306 +       }
21307 +
21308 +       xino_itrunc->bindex = -1;
21309 +       root = sb->s_root;
21310 +       aufs_read_lock(root, AuLock_FLUSH);
21311 +       bend = au_sbend(sb);
21312 +       for (bindex = 0; bindex <= bend; bindex++) {
21313 +               if (au_h_dptr(root, bindex) == path.dentry) {
21314 +                       xino_itrunc->bindex = bindex;
21315 +                       break;
21316 +               }
21317 +       }
21318 +       aufs_read_unlock(root, !AuLock_IR);
21319 +       path_put(&path);
21320 +
21321 +       if (unlikely(xino_itrunc->bindex < 0)) {
21322 +               pr_err("no such branch %s\n", args[0].from);
21323 +               err = -EINVAL;
21324 +       }
21325 +
21326 +out:
21327 +       return err;
21328 +}
21329 +
21330 +/* called without aufs lock */
21331 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
21332 +{
21333 +       int err, n, token;
21334 +       aufs_bindex_t bindex;
21335 +       unsigned char skipped;
21336 +       struct dentry *root;
21337 +       struct au_opt *opt, *opt_tail;
21338 +       char *opt_str;
21339 +       /* reduce the stack space */
21340 +       union {
21341 +               struct au_opt_xino_itrunc *xino_itrunc;
21342 +               struct au_opt_wbr_create *create;
21343 +       } u;
21344 +       struct {
21345 +               substring_t args[MAX_OPT_ARGS];
21346 +       } *a;
21347 +
21348 +       err = -ENOMEM;
21349 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21350 +       if (unlikely(!a))
21351 +               goto out;
21352 +
21353 +       root = sb->s_root;
21354 +       err = 0;
21355 +       bindex = 0;
21356 +       opt = opts->opt;
21357 +       opt_tail = opt + opts->max_opt - 1;
21358 +       opt->type = Opt_tail;
21359 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
21360 +               err = -EINVAL;
21361 +               skipped = 0;
21362 +               token = match_token(opt_str, options, a->args);
21363 +               switch (token) {
21364 +               case Opt_br:
21365 +                       err = 0;
21366 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
21367 +                              && *opt_str) {
21368 +                               err = opt_add(opt, opt_str, opts->sb_flags,
21369 +                                             bindex++);
21370 +                               if (unlikely(!err && ++opt > opt_tail)) {
21371 +                                       err = -E2BIG;
21372 +                                       break;
21373 +                               }
21374 +                               opt->type = Opt_tail;
21375 +                               skipped = 1;
21376 +                       }
21377 +                       break;
21378 +               case Opt_add:
21379 +                       if (unlikely(match_int(&a->args[0], &n))) {
21380 +                               pr_err("bad integer in %s\n", opt_str);
21381 +                               break;
21382 +                       }
21383 +                       bindex = n;
21384 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
21385 +                                     bindex);
21386 +                       if (!err)
21387 +                               opt->type = token;
21388 +                       break;
21389 +               case Opt_append:
21390 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
21391 +                                     /*dummy bindex*/1);
21392 +                       if (!err)
21393 +                               opt->type = token;
21394 +                       break;
21395 +               case Opt_prepend:
21396 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
21397 +                                     /*bindex*/0);
21398 +                       if (!err)
21399 +                               opt->type = token;
21400 +                       break;
21401 +               case Opt_del:
21402 +                       err = au_opts_parse_del(&opt->del, a->args);
21403 +                       if (!err)
21404 +                               opt->type = token;
21405 +                       break;
21406 +#if 0 /* reserved for future use */
21407 +               case Opt_idel:
21408 +                       del->pathname = "(indexed)";
21409 +                       if (unlikely(match_int(&args[0], &n))) {
21410 +                               pr_err("bad integer in %s\n", opt_str);
21411 +                               break;
21412 +                       }
21413 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
21414 +                       if (!err)
21415 +                               opt->type = token;
21416 +                       break;
21417 +#endif
21418 +               case Opt_mod:
21419 +                       err = au_opts_parse_mod(&opt->mod, a->args);
21420 +                       if (!err)
21421 +                               opt->type = token;
21422 +                       break;
21423 +#ifdef IMOD /* reserved for future use */
21424 +               case Opt_imod:
21425 +                       u.mod->path = "(indexed)";
21426 +                       if (unlikely(match_int(&a->args[0], &n))) {
21427 +                               pr_err("bad integer in %s\n", opt_str);
21428 +                               break;
21429 +                       }
21430 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
21431 +                       if (!err)
21432 +                               opt->type = token;
21433 +                       break;
21434 +#endif
21435 +               case Opt_xino:
21436 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
21437 +                       if (!err)
21438 +                               opt->type = token;
21439 +                       break;
21440 +
21441 +               case Opt_trunc_xino_path:
21442 +                       err = au_opts_parse_xino_itrunc_path
21443 +                               (sb, &opt->xino_itrunc, a->args);
21444 +                       if (!err)
21445 +                               opt->type = token;
21446 +                       break;
21447 +
21448 +               case Opt_itrunc_xino:
21449 +                       u.xino_itrunc = &opt->xino_itrunc;
21450 +                       if (unlikely(match_int(&a->args[0], &n))) {
21451 +                               pr_err("bad integer in %s\n", opt_str);
21452 +                               break;
21453 +                       }
21454 +                       u.xino_itrunc->bindex = n;
21455 +                       aufs_read_lock(root, AuLock_FLUSH);
21456 +                       if (n < 0 || au_sbend(sb) < n) {
21457 +                               pr_err("out of bounds, %d\n", n);
21458 +                               aufs_read_unlock(root, !AuLock_IR);
21459 +                               break;
21460 +                       }
21461 +                       aufs_read_unlock(root, !AuLock_IR);
21462 +                       err = 0;
21463 +                       opt->type = token;
21464 +                       break;
21465 +
21466 +               case Opt_dirwh:
21467 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
21468 +                               break;
21469 +                       err = 0;
21470 +                       opt->type = token;
21471 +                       break;
21472 +
21473 +               case Opt_rdcache:
21474 +                       if (unlikely(match_int(&a->args[0], &n))) {
21475 +                               pr_err("bad integer in %s\n", opt_str);
21476 +                               break;
21477 +                       }
21478 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
21479 +                               pr_err("rdcache must be smaller than %d\n",
21480 +                                      AUFS_RDCACHE_MAX);
21481 +                               break;
21482 +                       }
21483 +                       opt->rdcache = n;
21484 +                       err = 0;
21485 +                       opt->type = token;
21486 +                       break;
21487 +               case Opt_rdblk:
21488 +                       if (unlikely(match_int(&a->args[0], &n)
21489 +                                    || n < 0
21490 +                                    || n > KMALLOC_MAX_SIZE)) {
21491 +                               pr_err("bad integer in %s\n", opt_str);
21492 +                               break;
21493 +                       }
21494 +                       if (unlikely(n && n < NAME_MAX)) {
21495 +                               pr_err("rdblk must be larger than %d\n",
21496 +                                      NAME_MAX);
21497 +                               break;
21498 +                       }
21499 +                       opt->rdblk = n;
21500 +                       err = 0;
21501 +                       opt->type = token;
21502 +                       break;
21503 +               case Opt_rdhash:
21504 +                       if (unlikely(match_int(&a->args[0], &n)
21505 +                                    || n < 0
21506 +                                    || n * sizeof(struct hlist_head)
21507 +                                    > KMALLOC_MAX_SIZE)) {
21508 +                               pr_err("bad integer in %s\n", opt_str);
21509 +                               break;
21510 +                       }
21511 +                       opt->rdhash = n;
21512 +                       err = 0;
21513 +                       opt->type = token;
21514 +                       break;
21515 +
21516 +               case Opt_trunc_xino:
21517 +               case Opt_notrunc_xino:
21518 +               case Opt_noxino:
21519 +               case Opt_trunc_xib:
21520 +               case Opt_notrunc_xib:
21521 +               case Opt_shwh:
21522 +               case Opt_noshwh:
21523 +               case Opt_plink:
21524 +               case Opt_noplink:
21525 +               case Opt_list_plink:
21526 +               case Opt_dio:
21527 +               case Opt_nodio:
21528 +               case Opt_diropq_a:
21529 +               case Opt_diropq_w:
21530 +               case Opt_warn_perm:
21531 +               case Opt_nowarn_perm:
21532 +               case Opt_refrof:
21533 +               case Opt_norefrof:
21534 +               case Opt_verbose:
21535 +               case Opt_noverbose:
21536 +               case Opt_sum:
21537 +               case Opt_nosum:
21538 +               case Opt_wsum:
21539 +               case Opt_rdblk_def:
21540 +               case Opt_rdhash_def:
21541 +                       err = 0;
21542 +                       opt->type = token;
21543 +                       break;
21544 +
21545 +               case Opt_udba:
21546 +                       opt->udba = udba_val(a->args[0].from);
21547 +                       if (opt->udba >= 0) {
21548 +                               err = 0;
21549 +                               opt->type = token;
21550 +                       } else
21551 +                               pr_err("wrong value, %s\n", opt_str);
21552 +                       break;
21553 +
21554 +               case Opt_wbr_create:
21555 +                       u.create = &opt->wbr_create;
21556 +                       u.create->wbr_create
21557 +                               = au_wbr_create_val(a->args[0].from, u.create);
21558 +                       if (u.create->wbr_create >= 0) {
21559 +                               err = 0;
21560 +                               opt->type = token;
21561 +                       } else
21562 +                               pr_err("wrong value, %s\n", opt_str);
21563 +                       break;
21564 +               case Opt_wbr_copyup:
21565 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
21566 +                       if (opt->wbr_copyup >= 0) {
21567 +                               err = 0;
21568 +                               opt->type = token;
21569 +                       } else
21570 +                               pr_err("wrong value, %s\n", opt_str);
21571 +                       break;
21572 +
21573 +               case Opt_ignore:
21574 +                       pr_warn("ignored %s\n", opt_str);
21575 +                       /*FALLTHROUGH*/
21576 +               case Opt_ignore_silent:
21577 +                       skipped = 1;
21578 +                       err = 0;
21579 +                       break;
21580 +               case Opt_err:
21581 +                       pr_err("unknown option %s\n", opt_str);
21582 +                       break;
21583 +               }
21584 +
21585 +               if (!err && !skipped) {
21586 +                       if (unlikely(++opt > opt_tail)) {
21587 +                               err = -E2BIG;
21588 +                               opt--;
21589 +                               opt->type = Opt_tail;
21590 +                               break;
21591 +                       }
21592 +                       opt->type = Opt_tail;
21593 +               }
21594 +       }
21595 +
21596 +       kfree(a);
21597 +       dump_opts(opts);
21598 +       if (unlikely(err))
21599 +               au_opts_free(opts);
21600 +
21601 +out:
21602 +       return err;
21603 +}
21604 +
21605 +static int au_opt_wbr_create(struct super_block *sb,
21606 +                            struct au_opt_wbr_create *create)
21607 +{
21608 +       int err;
21609 +       struct au_sbinfo *sbinfo;
21610 +
21611 +       SiMustWriteLock(sb);
21612 +
21613 +       err = 1; /* handled */
21614 +       sbinfo = au_sbi(sb);
21615 +       if (sbinfo->si_wbr_create_ops->fin) {
21616 +               err = sbinfo->si_wbr_create_ops->fin(sb);
21617 +               if (!err)
21618 +                       err = 1;
21619 +       }
21620 +
21621 +       sbinfo->si_wbr_create = create->wbr_create;
21622 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
21623 +       switch (create->wbr_create) {
21624 +       case AuWbrCreate_MFSRRV:
21625 +       case AuWbrCreate_MFSRR:
21626 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
21627 +               /*FALLTHROUGH*/
21628 +       case AuWbrCreate_MFS:
21629 +       case AuWbrCreate_MFSV:
21630 +       case AuWbrCreate_PMFS:
21631 +       case AuWbrCreate_PMFSV:
21632 +               sbinfo->si_wbr_mfs.mfs_expire
21633 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
21634 +               break;
21635 +       }
21636 +
21637 +       if (sbinfo->si_wbr_create_ops->init)
21638 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
21639 +
21640 +       return err;
21641 +}
21642 +
21643 +/*
21644 + * returns,
21645 + * plus: processed without an error
21646 + * zero: unprocessed
21647 + */
21648 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
21649 +                        struct au_opts *opts)
21650 +{
21651 +       int err;
21652 +       struct au_sbinfo *sbinfo;
21653 +
21654 +       SiMustWriteLock(sb);
21655 +
21656 +       err = 1; /* handled */
21657 +       sbinfo = au_sbi(sb);
21658 +       switch (opt->type) {
21659 +       case Opt_udba:
21660 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
21661 +               sbinfo->si_mntflags |= opt->udba;
21662 +               opts->given_udba |= opt->udba;
21663 +               break;
21664 +
21665 +       case Opt_plink:
21666 +               au_opt_set(sbinfo->si_mntflags, PLINK);
21667 +               break;
21668 +       case Opt_noplink:
21669 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
21670 +                       au_plink_put(sb, /*verbose*/1);
21671 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
21672 +               break;
21673 +       case Opt_list_plink:
21674 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
21675 +                       au_plink_list(sb);
21676 +               break;
21677 +
21678 +       case Opt_dio:
21679 +               au_opt_set(sbinfo->si_mntflags, DIO);
21680 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
21681 +               break;
21682 +       case Opt_nodio:
21683 +               au_opt_clr(sbinfo->si_mntflags, DIO);
21684 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
21685 +               break;
21686 +
21687 +       case Opt_diropq_a:
21688 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
21689 +               break;
21690 +       case Opt_diropq_w:
21691 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
21692 +               break;
21693 +
21694 +       case Opt_warn_perm:
21695 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
21696 +               break;
21697 +       case Opt_nowarn_perm:
21698 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
21699 +               break;
21700 +
21701 +       case Opt_refrof:
21702 +               au_opt_set(sbinfo->si_mntflags, REFROF);
21703 +               break;
21704 +       case Opt_norefrof:
21705 +               au_opt_clr(sbinfo->si_mntflags, REFROF);
21706 +               break;
21707 +
21708 +       case Opt_verbose:
21709 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
21710 +               break;
21711 +       case Opt_noverbose:
21712 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
21713 +               break;
21714 +
21715 +       case Opt_sum:
21716 +               au_opt_set(sbinfo->si_mntflags, SUM);
21717 +               break;
21718 +       case Opt_wsum:
21719 +               au_opt_clr(sbinfo->si_mntflags, SUM);
21720 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
21721 +       case Opt_nosum:
21722 +               au_opt_clr(sbinfo->si_mntflags, SUM);
21723 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
21724 +               break;
21725 +
21726 +       case Opt_wbr_create:
21727 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
21728 +               break;
21729 +       case Opt_wbr_copyup:
21730 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
21731 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
21732 +               break;
21733 +
21734 +       case Opt_dirwh:
21735 +               sbinfo->si_dirwh = opt->dirwh;
21736 +               break;
21737 +
21738 +       case Opt_rdcache:
21739 +               sbinfo->si_rdcache
21740 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
21741 +               break;
21742 +       case Opt_rdblk:
21743 +               sbinfo->si_rdblk = opt->rdblk;
21744 +               break;
21745 +       case Opt_rdblk_def:
21746 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
21747 +               break;
21748 +       case Opt_rdhash:
21749 +               sbinfo->si_rdhash = opt->rdhash;
21750 +               break;
21751 +       case Opt_rdhash_def:
21752 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
21753 +               break;
21754 +
21755 +       case Opt_shwh:
21756 +               au_opt_set(sbinfo->si_mntflags, SHWH);
21757 +               break;
21758 +       case Opt_noshwh:
21759 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
21760 +               break;
21761 +
21762 +       case Opt_trunc_xino:
21763 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
21764 +               break;
21765 +       case Opt_notrunc_xino:
21766 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
21767 +               break;
21768 +
21769 +       case Opt_trunc_xino_path:
21770 +       case Opt_itrunc_xino:
21771 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
21772 +               if (!err)
21773 +                       err = 1;
21774 +               break;
21775 +
21776 +       case Opt_trunc_xib:
21777 +               au_fset_opts(opts->flags, TRUNC_XIB);
21778 +               break;
21779 +       case Opt_notrunc_xib:
21780 +               au_fclr_opts(opts->flags, TRUNC_XIB);
21781 +               break;
21782 +
21783 +       default:
21784 +               err = 0;
21785 +               break;
21786 +       }
21787 +
21788 +       return err;
21789 +}
21790 +
21791 +/*
21792 + * returns tri-state.
21793 + * plus: processed without an error
21794 + * zero: unprocessed
21795 + * minus: error
21796 + */
21797 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
21798 +                    struct au_opts *opts)
21799 +{
21800 +       int err, do_refresh;
21801 +
21802 +       err = 0;
21803 +       switch (opt->type) {
21804 +       case Opt_append:
21805 +               opt->add.bindex = au_sbend(sb) + 1;
21806 +               if (opt->add.bindex < 0)
21807 +                       opt->add.bindex = 0;
21808 +               goto add;
21809 +       case Opt_prepend:
21810 +               opt->add.bindex = 0;
21811 +       add:
21812 +       case Opt_add:
21813 +               err = au_br_add(sb, &opt->add,
21814 +                               au_ftest_opts(opts->flags, REMOUNT));
21815 +               if (!err) {
21816 +                       err = 1;
21817 +                       au_fset_opts(opts->flags, REFRESH);
21818 +               }
21819 +               break;
21820 +
21821 +       case Opt_del:
21822 +       case Opt_idel:
21823 +               err = au_br_del(sb, &opt->del,
21824 +                               au_ftest_opts(opts->flags, REMOUNT));
21825 +               if (!err) {
21826 +                       err = 1;
21827 +                       au_fset_opts(opts->flags, TRUNC_XIB);
21828 +                       au_fset_opts(opts->flags, REFRESH);
21829 +               }
21830 +               break;
21831 +
21832 +       case Opt_mod:
21833 +       case Opt_imod:
21834 +               err = au_br_mod(sb, &opt->mod,
21835 +                               au_ftest_opts(opts->flags, REMOUNT),
21836 +                               &do_refresh);
21837 +               if (!err) {
21838 +                       err = 1;
21839 +                       if (do_refresh)
21840 +                               au_fset_opts(opts->flags, REFRESH);
21841 +               }
21842 +               break;
21843 +       }
21844 +
21845 +       return err;
21846 +}
21847 +
21848 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
21849 +                      struct au_opt_xino **opt_xino,
21850 +                      struct au_opts *opts)
21851 +{
21852 +       int err;
21853 +       aufs_bindex_t bend, bindex;
21854 +       struct dentry *root, *parent, *h_root;
21855 +
21856 +       err = 0;
21857 +       switch (opt->type) {
21858 +       case Opt_xino:
21859 +               err = au_xino_set(sb, &opt->xino,
21860 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
21861 +               if (unlikely(err))
21862 +                       break;
21863 +
21864 +               *opt_xino = &opt->xino;
21865 +               au_xino_brid_set(sb, -1);
21866 +
21867 +               /* safe d_parent access */
21868 +               parent = opt->xino.file->f_dentry->d_parent;
21869 +               root = sb->s_root;
21870 +               bend = au_sbend(sb);
21871 +               for (bindex = 0; bindex <= bend; bindex++) {
21872 +                       h_root = au_h_dptr(root, bindex);
21873 +                       if (h_root == parent) {
21874 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
21875 +                               break;
21876 +                       }
21877 +               }
21878 +               break;
21879 +
21880 +       case Opt_noxino:
21881 +               au_xino_clr(sb);
21882 +               au_xino_brid_set(sb, -1);
21883 +               *opt_xino = (void *)-1;
21884 +               break;
21885 +       }
21886 +
21887 +       return err;
21888 +}
21889 +
21890 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
21891 +                  unsigned int pending)
21892 +{
21893 +       int err;
21894 +       aufs_bindex_t bindex, bend;
21895 +       unsigned char do_plink, skip, do_free;
21896 +       struct au_branch *br;
21897 +       struct au_wbr *wbr;
21898 +       struct dentry *root;
21899 +       struct inode *dir, *h_dir;
21900 +       struct au_sbinfo *sbinfo;
21901 +       struct au_hinode *hdir;
21902 +
21903 +       SiMustAnyLock(sb);
21904 +
21905 +       sbinfo = au_sbi(sb);
21906 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
21907 +
21908 +       if (!(sb_flags & MS_RDONLY)) {
21909 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
21910 +                       pr_warn("first branch should be rw\n");
21911 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
21912 +                       pr_warn("shwh should be used with ro\n");
21913 +       }
21914 +
21915 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
21916 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
21917 +               pr_warn("udba=*notify requires xino\n");
21918 +
21919 +       err = 0;
21920 +       root = sb->s_root;
21921 +       dir = root->d_inode;
21922 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
21923 +       bend = au_sbend(sb);
21924 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
21925 +               skip = 0;
21926 +               h_dir = au_h_iptr(dir, bindex);
21927 +               br = au_sbr(sb, bindex);
21928 +               do_free = 0;
21929 +
21930 +               wbr = br->br_wbr;
21931 +               if (wbr)
21932 +                       wbr_wh_read_lock(wbr);
21933 +
21934 +               if (!au_br_writable(br->br_perm)) {
21935 +                       do_free = !!wbr;
21936 +                       skip = (!wbr
21937 +                               || (!wbr->wbr_whbase
21938 +                                   && !wbr->wbr_plink
21939 +                                   && !wbr->wbr_orph));
21940 +               } else if (!au_br_wh_linkable(br->br_perm)) {
21941 +                       /* skip = (!br->br_whbase && !br->br_orph); */
21942 +                       skip = (!wbr || !wbr->wbr_whbase);
21943 +                       if (skip && wbr) {
21944 +                               if (do_plink)
21945 +                                       skip = !!wbr->wbr_plink;
21946 +                               else
21947 +                                       skip = !wbr->wbr_plink;
21948 +                       }
21949 +               } else {
21950 +                       /* skip = (br->br_whbase && br->br_ohph); */
21951 +                       skip = (wbr && wbr->wbr_whbase);
21952 +                       if (skip) {
21953 +                               if (do_plink)
21954 +                                       skip = !!wbr->wbr_plink;
21955 +                               else
21956 +                                       skip = !wbr->wbr_plink;
21957 +                       }
21958 +               }
21959 +               if (wbr)
21960 +                       wbr_wh_read_unlock(wbr);
21961 +
21962 +               if (skip)
21963 +                       continue;
21964 +
21965 +               hdir = au_hi(dir, bindex);
21966 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
21967 +               if (wbr)
21968 +                       wbr_wh_write_lock(wbr);
21969 +               err = au_wh_init(br, sb);
21970 +               if (wbr)
21971 +                       wbr_wh_write_unlock(wbr);
21972 +               au_hn_imtx_unlock(hdir);
21973 +
21974 +               if (!err && do_free) {
21975 +                       kfree(wbr);
21976 +                       br->br_wbr = NULL;
21977 +               }
21978 +       }
21979 +
21980 +       return err;
21981 +}
21982 +
21983 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
21984 +{
21985 +       int err;
21986 +       unsigned int tmp;
21987 +       aufs_bindex_t bindex, bend;
21988 +       struct au_opt *opt;
21989 +       struct au_opt_xino *opt_xino, xino;
21990 +       struct au_sbinfo *sbinfo;
21991 +       struct au_branch *br;
21992 +
21993 +       SiMustWriteLock(sb);
21994 +
21995 +       err = 0;
21996 +       opt_xino = NULL;
21997 +       opt = opts->opt;
21998 +       while (err >= 0 && opt->type != Opt_tail)
21999 +               err = au_opt_simple(sb, opt++, opts);
22000 +       if (err > 0)
22001 +               err = 0;
22002 +       else if (unlikely(err < 0))
22003 +               goto out;
22004 +
22005 +       /* disable xino and udba temporary */
22006 +       sbinfo = au_sbi(sb);
22007 +       tmp = sbinfo->si_mntflags;
22008 +       au_opt_clr(sbinfo->si_mntflags, XINO);
22009 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
22010 +
22011 +       opt = opts->opt;
22012 +       while (err >= 0 && opt->type != Opt_tail)
22013 +               err = au_opt_br(sb, opt++, opts);
22014 +       if (err > 0)
22015 +               err = 0;
22016 +       else if (unlikely(err < 0))
22017 +               goto out;
22018 +
22019 +       bend = au_sbend(sb);
22020 +       if (unlikely(bend < 0)) {
22021 +               err = -EINVAL;
22022 +               pr_err("no branches\n");
22023 +               goto out;
22024 +       }
22025 +
22026 +       if (au_opt_test(tmp, XINO))
22027 +               au_opt_set(sbinfo->si_mntflags, XINO);
22028 +       opt = opts->opt;
22029 +       while (!err && opt->type != Opt_tail)
22030 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
22031 +       if (unlikely(err))
22032 +               goto out;
22033 +
22034 +       err = au_opts_verify(sb, sb->s_flags, tmp);
22035 +       if (unlikely(err))
22036 +               goto out;
22037 +
22038 +       /* restore xino */
22039 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
22040 +               xino.file = au_xino_def(sb);
22041 +               err = PTR_ERR(xino.file);
22042 +               if (IS_ERR(xino.file))
22043 +                       goto out;
22044 +
22045 +               err = au_xino_set(sb, &xino, /*remount*/0);
22046 +               fput(xino.file);
22047 +               if (unlikely(err))
22048 +                       goto out;
22049 +       }
22050 +
22051 +       /* restore udba */
22052 +       tmp &= AuOptMask_UDBA;
22053 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
22054 +       sbinfo->si_mntflags |= tmp;
22055 +       bend = au_sbend(sb);
22056 +       for (bindex = 0; bindex <= bend; bindex++) {
22057 +               br = au_sbr(sb, bindex);
22058 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
22059 +               if (unlikely(err))
22060 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
22061 +                               bindex, err);
22062 +               /* go on even if err */
22063 +       }
22064 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
22065 +               struct inode *dir = sb->s_root->d_inode;
22066 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
22067 +       }
22068 +
22069 +out:
22070 +       return err;
22071 +}
22072 +
22073 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
22074 +{
22075 +       int err, rerr;
22076 +       struct inode *dir;
22077 +       struct au_opt_xino *opt_xino;
22078 +       struct au_opt *opt;
22079 +       struct au_sbinfo *sbinfo;
22080 +
22081 +       SiMustWriteLock(sb);
22082 +
22083 +       dir = sb->s_root->d_inode;
22084 +       sbinfo = au_sbi(sb);
22085 +       err = 0;
22086 +       opt_xino = NULL;
22087 +       opt = opts->opt;
22088 +       while (err >= 0 && opt->type != Opt_tail) {
22089 +               err = au_opt_simple(sb, opt, opts);
22090 +               if (!err)
22091 +                       err = au_opt_br(sb, opt, opts);
22092 +               if (!err)
22093 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
22094 +               opt++;
22095 +       }
22096 +       if (err > 0)
22097 +               err = 0;
22098 +       AuTraceErr(err);
22099 +       /* go on even err */
22100 +
22101 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
22102 +       if (unlikely(rerr && !err))
22103 +               err = rerr;
22104 +
22105 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
22106 +               rerr = au_xib_trunc(sb);
22107 +               if (unlikely(rerr && !err))
22108 +                       err = rerr;
22109 +       }
22110 +
22111 +       /* will be handled by the caller */
22112 +       if (!au_ftest_opts(opts->flags, REFRESH)
22113 +           && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
22114 +               au_fset_opts(opts->flags, REFRESH);
22115 +
22116 +       AuDbg("status 0x%x\n", opts->flags);
22117 +       return err;
22118 +}
22119 +
22120 +/* ---------------------------------------------------------------------- */
22121 +
22122 +unsigned int au_opt_udba(struct super_block *sb)
22123 +{
22124 +       return au_mntflags(sb) & AuOptMask_UDBA;
22125 +}
22126 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
22127 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
22128 +++ linux/fs/aufs/opts.h        2013-07-06 13:20:47.753531903 +0200
22129 @@ -0,0 +1,209 @@
22130 +/*
22131 + * Copyright (C) 2005-2013 Junjiro R. Okajima
22132 + *
22133 + * This program, aufs is free software; you can redistribute it and/or modify
22134 + * it under the terms of the GNU General Public License as published by
22135 + * the Free Software Foundation; either version 2 of the License, or
22136 + * (at your option) any later version.
22137 + *
22138 + * This program is distributed in the hope that it will be useful,
22139 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22140 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22141 + * GNU General Public License for more details.
22142 + *
22143 + * You should have received a copy of the GNU General Public License
22144 + * along with this program; if not, write to the Free Software
22145 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22146 + */
22147 +
22148 +/*
22149 + * mount options/flags
22150 + */
22151 +
22152 +#ifndef __AUFS_OPTS_H__
22153 +#define __AUFS_OPTS_H__
22154 +
22155 +#ifdef __KERNEL__
22156 +
22157 +#include <linux/path.h>
22158 +
22159 +struct file;
22160 +struct super_block;
22161 +
22162 +/* ---------------------------------------------------------------------- */
22163 +
22164 +/* mount flags */
22165 +#define AuOpt_XINO             1               /* external inode number bitmap
22166 +                                                  and translation table */
22167 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
22168 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
22169 +#define AuOpt_UDBA_REVAL       (1 << 3)
22170 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
22171 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
22172 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
22173 +#define AuOpt_DIRPERM1         (1 << 7)        /* unimplemented */
22174 +#define AuOpt_REFROF           (1 << 8)        /* unimplemented */
22175 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
22176 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
22177 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
22178 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
22179 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
22180 +#define AuOpt_DIO              (1 << 14)       /* direct io */
22181 +
22182 +#ifndef CONFIG_AUFS_HNOTIFY
22183 +#undef AuOpt_UDBA_HNOTIFY
22184 +#define AuOpt_UDBA_HNOTIFY     0
22185 +#endif
22186 +#ifndef CONFIG_AUFS_SHWH
22187 +#undef AuOpt_SHWH
22188 +#define AuOpt_SHWH             0
22189 +#endif
22190 +
22191 +#define AuOpt_Def      (AuOpt_XINO \
22192 +                        | AuOpt_UDBA_REVAL \
22193 +                        | AuOpt_PLINK \
22194 +                        /* | AuOpt_DIRPERM1 */ \
22195 +                        | AuOpt_WARN_PERM)
22196 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
22197 +                        | AuOpt_UDBA_REVAL \
22198 +                        | AuOpt_UDBA_HNOTIFY)
22199 +
22200 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
22201 +#define au_opt_set(flags, name) do { \
22202 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
22203 +       ((flags) |= AuOpt_##name); \
22204 +} while (0)
22205 +#define au_opt_set_udba(flags, name) do { \
22206 +       (flags) &= ~AuOptMask_UDBA; \
22207 +       ((flags) |= AuOpt_##name); \
22208 +} while (0)
22209 +#define au_opt_clr(flags, name) do { \
22210 +       ((flags) &= ~AuOpt_##name); \
22211 +} while (0)
22212 +
22213 +static inline unsigned int au_opts_plink(unsigned int mntflags)
22214 +{
22215 +#ifdef CONFIG_PROC_FS
22216 +       return mntflags;
22217 +#else
22218 +       return mntflags & ~AuOpt_PLINK;
22219 +#endif
22220 +}
22221 +
22222 +/* ---------------------------------------------------------------------- */
22223 +
22224 +/* policies to select one among multiple writable branches */
22225 +enum {
22226 +       AuWbrCreate_TDP,        /* top down parent */
22227 +       AuWbrCreate_RR,         /* round robin */
22228 +       AuWbrCreate_MFS,        /* most free space */
22229 +       AuWbrCreate_MFSV,       /* mfs with seconds */
22230 +       AuWbrCreate_MFSRR,      /* mfs then rr */
22231 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
22232 +       AuWbrCreate_PMFS,       /* parent and mfs */
22233 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
22234 +
22235 +       AuWbrCreate_Def = AuWbrCreate_TDP
22236 +};
22237 +
22238 +enum {
22239 +       AuWbrCopyup_TDP,        /* top down parent */
22240 +       AuWbrCopyup_BUP,        /* bottom up parent */
22241 +       AuWbrCopyup_BU,         /* bottom up */
22242 +
22243 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
22244 +};
22245 +
22246 +/* ---------------------------------------------------------------------- */
22247 +
22248 +struct au_opt_add {
22249 +       aufs_bindex_t   bindex;
22250 +       char            *pathname;
22251 +       int             perm;
22252 +       struct path     path;
22253 +};
22254 +
22255 +struct au_opt_del {
22256 +       char            *pathname;
22257 +       struct path     h_path;
22258 +};
22259 +
22260 +struct au_opt_mod {
22261 +       char            *path;
22262 +       int             perm;
22263 +       struct dentry   *h_root;
22264 +};
22265 +
22266 +struct au_opt_xino {
22267 +       char            *path;
22268 +       struct file     *file;
22269 +};
22270 +
22271 +struct au_opt_xino_itrunc {
22272 +       aufs_bindex_t   bindex;
22273 +};
22274 +
22275 +struct au_opt_wbr_create {
22276 +       int                     wbr_create;
22277 +       int                     mfs_second;
22278 +       unsigned long long      mfsrr_watermark;
22279 +};
22280 +
22281 +struct au_opt {
22282 +       int type;
22283 +       union {
22284 +               struct au_opt_xino      xino;
22285 +               struct au_opt_xino_itrunc xino_itrunc;
22286 +               struct au_opt_add       add;
22287 +               struct au_opt_del       del;
22288 +               struct au_opt_mod       mod;
22289 +               int                     dirwh;
22290 +               int                     rdcache;
22291 +               unsigned int            rdblk;
22292 +               unsigned int            rdhash;
22293 +               int                     udba;
22294 +               struct au_opt_wbr_create wbr_create;
22295 +               int                     wbr_copyup;
22296 +       };
22297 +};
22298 +
22299 +/* opts flags */
22300 +#define AuOpts_REMOUNT         1
22301 +#define AuOpts_REFRESH         (1 << 1)
22302 +#define AuOpts_TRUNC_XIB       (1 << 2)
22303 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
22304 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
22305 +#define au_fset_opts(flags, name) \
22306 +       do { (flags) |= AuOpts_##name; } while (0)
22307 +#define au_fclr_opts(flags, name) \
22308 +       do { (flags) &= ~AuOpts_##name; } while (0)
22309 +
22310 +struct au_opts {
22311 +       struct au_opt   *opt;
22312 +       int             max_opt;
22313 +
22314 +       unsigned int    given_udba;
22315 +       unsigned int    flags;
22316 +       unsigned long   sb_flags;
22317 +};
22318 +
22319 +/* ---------------------------------------------------------------------- */
22320 +
22321 +char *au_optstr_br_perm(int brperm);
22322 +const char *au_optstr_udba(int udba);
22323 +const char *au_optstr_wbr_copyup(int wbr_copyup);
22324 +const char *au_optstr_wbr_create(int wbr_create);
22325 +
22326 +void au_opts_free(struct au_opts *opts);
22327 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
22328 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
22329 +                  unsigned int pending);
22330 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
22331 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
22332 +
22333 +unsigned int au_opt_udba(struct super_block *sb);
22334 +
22335 +/* ---------------------------------------------------------------------- */
22336 +
22337 +#endif /* __KERNEL__ */
22338 +#endif /* __AUFS_OPTS_H__ */
22339 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
22340 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
22341 +++ linux/fs/aufs/plink.c       2013-07-06 13:20:47.753531903 +0200
22342 @@ -0,0 +1,520 @@
22343 +/*
22344 + * Copyright (C) 2005-2013 Junjiro R. Okajima
22345 + *
22346 + * This program, aufs is free software; you can redistribute it and/or modify
22347 + * it under the terms of the GNU General Public License as published by
22348 + * the Free Software Foundation; either version 2 of the License, or
22349 + * (at your option) any later version.
22350 + *
22351 + * This program is distributed in the hope that it will be useful,
22352 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22353 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22354 + * GNU General Public License for more details.
22355 + *
22356 + * You should have received a copy of the GNU General Public License
22357 + * along with this program; if not, write to the Free Software
22358 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22359 + */
22360 +
22361 +/*
22362 + * pseudo-link
22363 + */
22364 +
22365 +#include "aufs.h"
22366 +
22367 +/*
22368 + * the pseudo-link maintenance mode.
22369 + * during a user process maintains the pseudo-links,
22370 + * prohibit adding a new plink and branch manipulation.
22371 + *
22372 + * Flags
22373 + * NOPLM:
22374 + *     For entry functions which will handle plink, and i_mutex is already held
22375 + *     in VFS.
22376 + *     They cannot wait and should return an error at once.
22377 + *     Callers has to check the error.
22378 + * NOPLMW:
22379 + *     For entry functions which will handle plink, but i_mutex is not held
22380 + *     in VFS.
22381 + *     They can wait the plink maintenance mode to finish.
22382 + *
22383 + * They behave like F_SETLK and F_SETLKW.
22384 + * If the caller never handle plink, then both flags are unnecessary.
22385 + */
22386 +
22387 +int au_plink_maint(struct super_block *sb, int flags)
22388 +{
22389 +       int err;
22390 +       pid_t pid, ppid;
22391 +       struct au_sbinfo *sbi;
22392 +
22393 +       SiMustAnyLock(sb);
22394 +
22395 +       err = 0;
22396 +       if (!au_opt_test(au_mntflags(sb), PLINK))
22397 +               goto out;
22398 +
22399 +       sbi = au_sbi(sb);
22400 +       pid = sbi->si_plink_maint_pid;
22401 +       if (!pid || pid == current->pid)
22402 +               goto out;
22403 +
22404 +       /* todo: it highly depends upon /sbin/mount.aufs */
22405 +       rcu_read_lock();
22406 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
22407 +       rcu_read_unlock();
22408 +       if (pid == ppid)
22409 +               goto out;
22410 +
22411 +       if (au_ftest_lock(flags, NOPLMW)) {
22412 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
22413 +               /* AuDebugOn(!lockdep_depth(current)); */
22414 +               while (sbi->si_plink_maint_pid) {
22415 +                       si_read_unlock(sb);
22416 +                       /* gave up wake_up_bit() */
22417 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
22418 +
22419 +                       if (au_ftest_lock(flags, FLUSH))
22420 +                               au_nwt_flush(&sbi->si_nowait);
22421 +                       si_noflush_read_lock(sb);
22422 +               }
22423 +       } else if (au_ftest_lock(flags, NOPLM)) {
22424 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
22425 +               err = -EAGAIN;
22426 +       }
22427 +
22428 +out:
22429 +       return err;
22430 +}
22431 +
22432 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
22433 +{
22434 +       spin_lock(&sbinfo->si_plink_maint_lock);
22435 +       sbinfo->si_plink_maint_pid = 0;
22436 +       spin_unlock(&sbinfo->si_plink_maint_lock);
22437 +       wake_up_all(&sbinfo->si_plink_wq);
22438 +}
22439 +
22440 +int au_plink_maint_enter(struct super_block *sb)
22441 +{
22442 +       int err;
22443 +       struct au_sbinfo *sbinfo;
22444 +
22445 +       err = 0;
22446 +       sbinfo = au_sbi(sb);
22447 +       /* make sure i am the only one in this fs */
22448 +       si_write_lock(sb, AuLock_FLUSH);
22449 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
22450 +               spin_lock(&sbinfo->si_plink_maint_lock);
22451 +               if (!sbinfo->si_plink_maint_pid)
22452 +                       sbinfo->si_plink_maint_pid = current->pid;
22453 +               else
22454 +                       err = -EBUSY;
22455 +               spin_unlock(&sbinfo->si_plink_maint_lock);
22456 +       }
22457 +       si_write_unlock(sb);
22458 +
22459 +       return err;
22460 +}
22461 +
22462 +/* ---------------------------------------------------------------------- */
22463 +
22464 +#ifdef CONFIG_AUFS_DEBUG
22465 +void au_plink_list(struct super_block *sb)
22466 +{
22467 +       int i;
22468 +       struct au_sbinfo *sbinfo;
22469 +       struct hlist_head *plink_hlist;
22470 +       struct pseudo_link *plink;
22471 +
22472 +       SiMustAnyLock(sb);
22473 +
22474 +       sbinfo = au_sbi(sb);
22475 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
22476 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
22477 +
22478 +       for (i = 0; i < AuPlink_NHASH; i++) {
22479 +               plink_hlist = &sbinfo->si_plink[i].head;
22480 +               rcu_read_lock();
22481 +               hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
22482 +                       AuDbg("%lu\n", plink->inode->i_ino);
22483 +               rcu_read_unlock();
22484 +       }
22485 +}
22486 +#endif
22487 +
22488 +/* is the inode pseudo-linked? */
22489 +int au_plink_test(struct inode *inode)
22490 +{
22491 +       int found, i;
22492 +       struct au_sbinfo *sbinfo;
22493 +       struct hlist_head *plink_hlist;
22494 +       struct pseudo_link *plink;
22495 +
22496 +       sbinfo = au_sbi(inode->i_sb);
22497 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
22498 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
22499 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
22500 +
22501 +       found = 0;
22502 +       i = au_plink_hash(inode->i_ino);
22503 +       plink_hlist = &sbinfo->si_plink[i].head;
22504 +       rcu_read_lock();
22505 +       hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
22506 +               if (plink->inode == inode) {
22507 +                       found = 1;
22508 +                       break;
22509 +               }
22510 +       rcu_read_unlock();
22511 +       return found;
22512 +}
22513 +
22514 +/* ---------------------------------------------------------------------- */
22515 +
22516 +/*
22517 + * generate a name for plink.
22518 + * the file will be stored under AUFS_WH_PLINKDIR.
22519 + */
22520 +/* 20 is max digits length of ulong 64 */
22521 +#define PLINK_NAME_LEN ((20 + 1) * 2)
22522 +
22523 +static int plink_name(char *name, int len, struct inode *inode,
22524 +                     aufs_bindex_t bindex)
22525 +{
22526 +       int rlen;
22527 +       struct inode *h_inode;
22528 +
22529 +       h_inode = au_h_iptr(inode, bindex);
22530 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
22531 +       return rlen;
22532 +}
22533 +
22534 +struct au_do_plink_lkup_args {
22535 +       struct dentry **errp;
22536 +       struct qstr *tgtname;
22537 +       struct dentry *h_parent;
22538 +       struct au_branch *br;
22539 +};
22540 +
22541 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
22542 +                                      struct dentry *h_parent,
22543 +                                      struct au_branch *br)
22544 +{
22545 +       struct dentry *h_dentry;
22546 +       struct mutex *h_mtx;
22547 +
22548 +       h_mtx = &h_parent->d_inode->i_mutex;
22549 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
22550 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
22551 +       mutex_unlock(h_mtx);
22552 +       return h_dentry;
22553 +}
22554 +
22555 +static void au_call_do_plink_lkup(void *args)
22556 +{
22557 +       struct au_do_plink_lkup_args *a = args;
22558 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
22559 +}
22560 +
22561 +/* lookup the plink-ed @inode under the branch at @bindex */
22562 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
22563 +{
22564 +       struct dentry *h_dentry, *h_parent;
22565 +       struct au_branch *br;
22566 +       struct inode *h_dir;
22567 +       int wkq_err;
22568 +       char a[PLINK_NAME_LEN];
22569 +       struct qstr tgtname = QSTR_INIT(a, 0);
22570 +
22571 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
22572 +
22573 +       br = au_sbr(inode->i_sb, bindex);
22574 +       h_parent = br->br_wbr->wbr_plink;
22575 +       h_dir = h_parent->d_inode;
22576 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
22577 +
22578 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
22579 +               struct au_do_plink_lkup_args args = {
22580 +                       .errp           = &h_dentry,
22581 +                       .tgtname        = &tgtname,
22582 +                       .h_parent       = h_parent,
22583 +                       .br             = br
22584 +               };
22585 +
22586 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
22587 +               if (unlikely(wkq_err))
22588 +                       h_dentry = ERR_PTR(wkq_err);
22589 +       } else
22590 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
22591 +
22592 +       return h_dentry;
22593 +}
22594 +
22595 +/* create a pseudo-link */
22596 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
22597 +                     struct dentry *h_dentry, struct au_branch *br)
22598 +{
22599 +       int err;
22600 +       struct path h_path = {
22601 +               .mnt = au_br_mnt(br)
22602 +       };
22603 +       struct inode *h_dir;
22604 +
22605 +       h_dir = h_parent->d_inode;
22606 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
22607 +again:
22608 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
22609 +       err = PTR_ERR(h_path.dentry);
22610 +       if (IS_ERR(h_path.dentry))
22611 +               goto out;
22612 +
22613 +       err = 0;
22614 +       /* wh.plink dir is not monitored */
22615 +       /* todo: is it really safe? */
22616 +       if (h_path.dentry->d_inode
22617 +           && h_path.dentry->d_inode != h_dentry->d_inode) {
22618 +               err = vfsub_unlink(h_dir, &h_path, /*force*/0);
22619 +               dput(h_path.dentry);
22620 +               h_path.dentry = NULL;
22621 +               if (!err)
22622 +                       goto again;
22623 +       }
22624 +       if (!err && !h_path.dentry->d_inode)
22625 +               err = vfsub_link(h_dentry, h_dir, &h_path);
22626 +       dput(h_path.dentry);
22627 +
22628 +out:
22629 +       mutex_unlock(&h_dir->i_mutex);
22630 +       return err;
22631 +}
22632 +
22633 +struct do_whplink_args {
22634 +       int *errp;
22635 +       struct qstr *tgt;
22636 +       struct dentry *h_parent;
22637 +       struct dentry *h_dentry;
22638 +       struct au_branch *br;
22639 +};
22640 +
22641 +static void call_do_whplink(void *args)
22642 +{
22643 +       struct do_whplink_args *a = args;
22644 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
22645 +}
22646 +
22647 +static int whplink(struct dentry *h_dentry, struct inode *inode,
22648 +                  aufs_bindex_t bindex, struct au_branch *br)
22649 +{
22650 +       int err, wkq_err;
22651 +       struct au_wbr *wbr;
22652 +       struct dentry *h_parent;
22653 +       struct inode *h_dir;
22654 +       char a[PLINK_NAME_LEN];
22655 +       struct qstr tgtname = QSTR_INIT(a, 0);
22656 +
22657 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
22658 +       h_parent = wbr->wbr_plink;
22659 +       h_dir = h_parent->d_inode;
22660 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
22661 +
22662 +       /* always superio. */
22663 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
22664 +               struct do_whplink_args args = {
22665 +                       .errp           = &err,
22666 +                       .tgt            = &tgtname,
22667 +                       .h_parent       = h_parent,
22668 +                       .h_dentry       = h_dentry,
22669 +                       .br             = br
22670 +               };
22671 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
22672 +               if (unlikely(wkq_err))
22673 +                       err = wkq_err;
22674 +       } else
22675 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
22676 +
22677 +       return err;
22678 +}
22679 +
22680 +/* free a single plink */
22681 +static void do_put_plink(struct pseudo_link *plink, int do_del)
22682 +{
22683 +       if (do_del)
22684 +               hlist_del(&plink->hlist);
22685 +       iput(plink->inode);
22686 +       kfree(plink);
22687 +}
22688 +
22689 +static void do_put_plink_rcu(struct rcu_head *rcu)
22690 +{
22691 +       struct pseudo_link *plink;
22692 +
22693 +       plink = container_of(rcu, struct pseudo_link, rcu);
22694 +       iput(plink->inode);
22695 +       kfree(plink);
22696 +}
22697 +
22698 +/*
22699 + * create a new pseudo-link for @h_dentry on @bindex.
22700 + * the linked inode is held in aufs @inode.
22701 + */
22702 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
22703 +                    struct dentry *h_dentry)
22704 +{
22705 +       struct super_block *sb;
22706 +       struct au_sbinfo *sbinfo;
22707 +       struct hlist_head *plink_hlist;
22708 +       struct pseudo_link *plink, *tmp;
22709 +       struct au_sphlhead *sphl;
22710 +       int found, err, cnt, i;
22711 +
22712 +       sb = inode->i_sb;
22713 +       sbinfo = au_sbi(sb);
22714 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
22715 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
22716 +
22717 +       found = au_plink_test(inode);
22718 +       if (found)
22719 +               return;
22720 +
22721 +       i = au_plink_hash(inode->i_ino);
22722 +       sphl = sbinfo->si_plink + i;
22723 +       plink_hlist = &sphl->head;
22724 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
22725 +       if (tmp)
22726 +               tmp->inode = au_igrab(inode);
22727 +       else {
22728 +               err = -ENOMEM;
22729 +               goto out;
22730 +       }
22731 +
22732 +       spin_lock(&sphl->spin);
22733 +       hlist_for_each_entry(plink, plink_hlist, hlist) {
22734 +               if (plink->inode == inode) {
22735 +                       found = 1;
22736 +                       break;
22737 +               }
22738 +       }
22739 +       if (!found)
22740 +               hlist_add_head_rcu(&tmp->hlist, plink_hlist);
22741 +       spin_unlock(&sphl->spin);
22742 +       if (!found) {
22743 +               cnt = au_sphl_count(sphl);
22744 +#define msg "unexpectedly unblanced or too many pseudo-links"
22745 +               if (cnt > AUFS_PLINK_WARN)
22746 +                       AuWarn1(msg ", %d\n", cnt);
22747 +#undef msg
22748 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
22749 +       } else {
22750 +               do_put_plink(tmp, 0);
22751 +               return;
22752 +       }
22753 +
22754 +out:
22755 +       if (unlikely(err)) {
22756 +               pr_warn("err %d, damaged pseudo link.\n", err);
22757 +               if (tmp) {
22758 +                       au_sphl_del_rcu(&tmp->hlist, sphl);
22759 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
22760 +               }
22761 +       }
22762 +}
22763 +
22764 +/* free all plinks */
22765 +void au_plink_put(struct super_block *sb, int verbose)
22766 +{
22767 +       int i, warned;
22768 +       struct au_sbinfo *sbinfo;
22769 +       struct hlist_head *plink_hlist;
22770 +       struct hlist_node *tmp;
22771 +       struct pseudo_link *plink;
22772 +
22773 +       SiMustWriteLock(sb);
22774 +
22775 +       sbinfo = au_sbi(sb);
22776 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
22777 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
22778 +
22779 +       /* no spin_lock since sbinfo is write-locked */
22780 +       warned = 0;
22781 +       for (i = 0; i < AuPlink_NHASH; i++) {
22782 +               plink_hlist = &sbinfo->si_plink[i].head;
22783 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
22784 +                       pr_warn("pseudo-link is not flushed");
22785 +                       warned = 1;
22786 +               }
22787 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
22788 +                       do_put_plink(plink, 0);
22789 +               INIT_HLIST_HEAD(plink_hlist);
22790 +       }
22791 +}
22792 +
22793 +void au_plink_clean(struct super_block *sb, int verbose)
22794 +{
22795 +       struct dentry *root;
22796 +
22797 +       root = sb->s_root;
22798 +       aufs_write_lock(root);
22799 +       if (au_opt_test(au_mntflags(sb), PLINK))
22800 +               au_plink_put(sb, verbose);
22801 +       aufs_write_unlock(root);
22802 +}
22803 +
22804 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
22805 +{
22806 +       int do_put;
22807 +       aufs_bindex_t bstart, bend, bindex;
22808 +
22809 +       do_put = 0;
22810 +       bstart = au_ibstart(inode);
22811 +       bend = au_ibend(inode);
22812 +       if (bstart >= 0) {
22813 +               for (bindex = bstart; bindex <= bend; bindex++) {
22814 +                       if (!au_h_iptr(inode, bindex)
22815 +                           || au_ii_br_id(inode, bindex) != br_id)
22816 +                               continue;
22817 +                       au_set_h_iptr(inode, bindex, NULL, 0);
22818 +                       do_put = 1;
22819 +                       break;
22820 +               }
22821 +               if (do_put)
22822 +                       for (bindex = bstart; bindex <= bend; bindex++)
22823 +                               if (au_h_iptr(inode, bindex)) {
22824 +                                       do_put = 0;
22825 +                                       break;
22826 +                               }
22827 +       } else
22828 +               do_put = 1;
22829 +
22830 +       return do_put;
22831 +}
22832 +
22833 +/* free the plinks on a branch specified by @br_id */
22834 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
22835 +{
22836 +       struct au_sbinfo *sbinfo;
22837 +       struct hlist_head *plink_hlist;
22838 +       struct hlist_node *tmp;
22839 +       struct pseudo_link *plink;
22840 +       struct inode *inode;
22841 +       int i, do_put;
22842 +
22843 +       SiMustWriteLock(sb);
22844 +
22845 +       sbinfo = au_sbi(sb);
22846 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
22847 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
22848 +
22849 +       /* no spin_lock since sbinfo is write-locked */
22850 +       for (i = 0; i < AuPlink_NHASH; i++) {
22851 +               plink_hlist = &sbinfo->si_plink[i].head;
22852 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
22853 +                       inode = au_igrab(plink->inode);
22854 +                       ii_write_lock_child(inode);
22855 +                       do_put = au_plink_do_half_refresh(inode, br_id);
22856 +                       if (do_put)
22857 +                               do_put_plink(plink, 1);
22858 +                       ii_write_unlock(inode);
22859 +                       iput(inode);
22860 +               }
22861 +       }
22862 +}
22863 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
22864 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
22865 +++ linux/fs/aufs/poll.c        2013-07-06 13:20:47.753531903 +0200
22866 @@ -0,0 +1,56 @@
22867 +/*
22868 + * Copyright (C) 2005-2013 Junjiro R. Okajima
22869 + *
22870 + * This program, aufs is free software; you can redistribute it and/or modify
22871 + * it under the terms of the GNU General Public License as published by
22872 + * the Free Software Foundation; either version 2 of the License, or
22873 + * (at your option) any later version.
22874 + *
22875 + * This program is distributed in the hope that it will be useful,
22876 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22877 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22878 + * GNU General Public License for more details.
22879 + *
22880 + * You should have received a copy of the GNU General Public License
22881 + * along with this program; if not, write to the Free Software
22882 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22883 + */
22884 +
22885 +/*
22886 + * poll operation
22887 + * There is only one filesystem which implements ->poll operation, currently.
22888 + */
22889 +
22890 +#include "aufs.h"
22891 +
22892 +unsigned int aufs_poll(struct file *file, poll_table *wait)
22893 +{
22894 +       unsigned int mask;
22895 +       int err;
22896 +       struct file *h_file;
22897 +       struct dentry *dentry;
22898 +       struct super_block *sb;
22899 +
22900 +       /* We should pretend an error happened. */
22901 +       mask = POLLERR /* | POLLIN | POLLOUT */;
22902 +       dentry = file->f_dentry;
22903 +       sb = dentry->d_sb;
22904 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
22905 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
22906 +       if (unlikely(err))
22907 +               goto out;
22908 +
22909 +       /* it is not an error if h_file has no operation */
22910 +       mask = DEFAULT_POLLMASK;
22911 +       h_file = au_hf_top(file);
22912 +       if (h_file->f_op && h_file->f_op->poll)
22913 +               mask = h_file->f_op->poll(h_file, wait);
22914 +
22915 +       di_read_unlock(dentry, AuLock_IR);
22916 +       fi_read_unlock(file);
22917 +
22918 +out:
22919 +       si_read_unlock(sb);
22920 +       AuTraceErr((int)mask);
22921 +       return mask;
22922 +}
22923 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
22924 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
22925 +++ linux/fs/aufs/procfs.c      2013-07-06 13:20:47.753531903 +0200
22926 @@ -0,0 +1,170 @@
22927 +/*
22928 + * Copyright (C) 2010-2013 Junjiro R. Okajima
22929 + *
22930 + * This program, aufs is free software; you can redistribute it and/or modify
22931 + * it under the terms of the GNU General Public License as published by
22932 + * the Free Software Foundation; either version 2 of the License, or
22933 + * (at your option) any later version.
22934 + *
22935 + * This program is distributed in the hope that it will be useful,
22936 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22937 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22938 + * GNU General Public License for more details.
22939 + *
22940 + * You should have received a copy of the GNU General Public License
22941 + * along with this program; if not, write to the Free Software
22942 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22943 + */
22944 +
22945 +/*
22946 + * procfs interfaces
22947 + */
22948 +
22949 +#include <linux/proc_fs.h>
22950 +#include "aufs.h"
22951 +
22952 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
22953 +{
22954 +       struct au_sbinfo *sbinfo;
22955 +
22956 +       sbinfo = file->private_data;
22957 +       if (sbinfo) {
22958 +               au_plink_maint_leave(sbinfo);
22959 +               kobject_put(&sbinfo->si_kobj);
22960 +       }
22961 +
22962 +       return 0;
22963 +}
22964 +
22965 +static void au_procfs_plm_write_clean(struct file *file)
22966 +{
22967 +       struct au_sbinfo *sbinfo;
22968 +
22969 +       sbinfo = file->private_data;
22970 +       if (sbinfo)
22971 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
22972 +}
22973 +
22974 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
22975 +{
22976 +       int err;
22977 +       struct super_block *sb;
22978 +       struct au_sbinfo *sbinfo;
22979 +
22980 +       err = -EBUSY;
22981 +       if (unlikely(file->private_data))
22982 +               goto out;
22983 +
22984 +       sb = NULL;
22985 +       /* don't use au_sbilist_lock() here */
22986 +       spin_lock(&au_sbilist.spin);
22987 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
22988 +               if (id == sysaufs_si_id(sbinfo)) {
22989 +                       kobject_get(&sbinfo->si_kobj);
22990 +                       sb = sbinfo->si_sb;
22991 +                       break;
22992 +               }
22993 +       spin_unlock(&au_sbilist.spin);
22994 +
22995 +       err = -EINVAL;
22996 +       if (unlikely(!sb))
22997 +               goto out;
22998 +
22999 +       err = au_plink_maint_enter(sb);
23000 +       if (!err)
23001 +               /* keep kobject_get() */
23002 +               file->private_data = sbinfo;
23003 +       else
23004 +               kobject_put(&sbinfo->si_kobj);
23005 +out:
23006 +       return err;
23007 +}
23008 +
23009 +/*
23010 + * Accept a valid "si=xxxx" only.
23011 + * Once it is accepted successfully, accept "clean" too.
23012 + */
23013 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
23014 +                                  size_t count, loff_t *ppos)
23015 +{
23016 +       ssize_t err;
23017 +       unsigned long id;
23018 +       /* last newline is allowed */
23019 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
23020 +
23021 +       err = -EACCES;
23022 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
23023 +               goto out;
23024 +
23025 +       err = -EINVAL;
23026 +       if (unlikely(count > sizeof(buf)))
23027 +               goto out;
23028 +
23029 +       err = copy_from_user(buf, ubuf, count);
23030 +       if (unlikely(err)) {
23031 +               err = -EFAULT;
23032 +               goto out;
23033 +       }
23034 +       buf[count] = 0;
23035 +
23036 +       err = -EINVAL;
23037 +       if (!strcmp("clean", buf)) {
23038 +               au_procfs_plm_write_clean(file);
23039 +               goto out_success;
23040 +       } else if (unlikely(strncmp("si=", buf, 3)))
23041 +               goto out;
23042 +
23043 +       err = kstrtoul(buf + 3, 16, &id);
23044 +       if (unlikely(err))
23045 +               goto out;
23046 +
23047 +       err = au_procfs_plm_write_si(file, id);
23048 +       if (unlikely(err))
23049 +               goto out;
23050 +
23051 +out_success:
23052 +       err = count; /* success */
23053 +out:
23054 +       return err;
23055 +}
23056 +
23057 +static const struct file_operations au_procfs_plm_fop = {
23058 +       .write          = au_procfs_plm_write,
23059 +       .release        = au_procfs_plm_release,
23060 +       .owner          = THIS_MODULE
23061 +};
23062 +
23063 +/* ---------------------------------------------------------------------- */
23064 +
23065 +static struct proc_dir_entry *au_procfs_dir;
23066 +
23067 +void au_procfs_fin(void)
23068 +{
23069 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
23070 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
23071 +}
23072 +
23073 +int __init au_procfs_init(void)
23074 +{
23075 +       int err;
23076 +       struct proc_dir_entry *entry;
23077 +
23078 +       err = -ENOMEM;
23079 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
23080 +       if (unlikely(!au_procfs_dir))
23081 +               goto out;
23082 +
23083 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
23084 +                           au_procfs_dir, &au_procfs_plm_fop);
23085 +       if (unlikely(!entry))
23086 +               goto out_dir;
23087 +
23088 +       err = 0;
23089 +       goto out; /* success */
23090 +
23091 +
23092 +out_dir:
23093 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
23094 +out:
23095 +       return err;
23096 +}
23097 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
23098 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
23099 +++ linux/fs/aufs/rdu.c 2013-07-30 22:42:55.842946719 +0200
23100 @@ -0,0 +1,384 @@
23101 +/*
23102 + * Copyright (C) 2005-2013 Junjiro R. Okajima
23103 + *
23104 + * This program, aufs is free software; you can redistribute it and/or modify
23105 + * it under the terms of the GNU General Public License as published by
23106 + * the Free Software Foundation; either version 2 of the License, or
23107 + * (at your option) any later version.
23108 + *
23109 + * This program is distributed in the hope that it will be useful,
23110 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23111 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23112 + * GNU General Public License for more details.
23113 + *
23114 + * You should have received a copy of the GNU General Public License
23115 + * along with this program; if not, write to the Free Software
23116 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23117 + */
23118 +
23119 +/*
23120 + * readdir in userspace.
23121 + */
23122 +
23123 +#include <linux/compat.h>
23124 +#include <linux/fs_stack.h>
23125 +#include <linux/security.h>
23126 +#include "aufs.h"
23127 +
23128 +/* bits for struct aufs_rdu.flags */
23129 +#define        AuRdu_CALLED    1
23130 +#define        AuRdu_CONT      (1 << 1)
23131 +#define        AuRdu_FULL      (1 << 2)
23132 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
23133 +#define au_fset_rdu(flags, name) \
23134 +       do { (flags) |= AuRdu_##name; } while (0)
23135 +#define au_fclr_rdu(flags, name) \
23136 +       do { (flags) &= ~AuRdu_##name; } while (0)
23137 +
23138 +struct au_rdu_arg {
23139 +       struct aufs_rdu                 *rdu;
23140 +       union au_rdu_ent_ul             ent;
23141 +       unsigned long                   end;
23142 +
23143 +       struct super_block              *sb;
23144 +       int                             err;
23145 +};
23146 +
23147 +static int au_rdu_fill(void *__arg, const char *name, int nlen,
23148 +                      loff_t offset, u64 h_ino, unsigned int d_type)
23149 +{
23150 +       int err, len;
23151 +       struct au_rdu_arg *arg = __arg;
23152 +       struct aufs_rdu *rdu = arg->rdu;
23153 +       struct au_rdu_ent ent;
23154 +
23155 +       err = 0;
23156 +       arg->err = 0;
23157 +       au_fset_rdu(rdu->cookie.flags, CALLED);
23158 +       len = au_rdu_len(nlen);
23159 +       if (arg->ent.ul + len  < arg->end) {
23160 +               ent.ino = h_ino;
23161 +               ent.bindex = rdu->cookie.bindex;
23162 +               ent.type = d_type;
23163 +               ent.nlen = nlen;
23164 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
23165 +                       ent.type = DT_UNKNOWN;
23166 +
23167 +               /* unnecessary to support mmap_sem since this is a dir */
23168 +               err = -EFAULT;
23169 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
23170 +                       goto out;
23171 +               if (copy_to_user(arg->ent.e->name, name, nlen))
23172 +                       goto out;
23173 +               /* the terminating NULL */
23174 +               if (__put_user(0, arg->ent.e->name + nlen))
23175 +                       goto out;
23176 +               err = 0;
23177 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
23178 +               arg->ent.ul += len;
23179 +               rdu->rent++;
23180 +       } else {
23181 +               err = -EFAULT;
23182 +               au_fset_rdu(rdu->cookie.flags, FULL);
23183 +               rdu->full = 1;
23184 +               rdu->tail = arg->ent;
23185 +       }
23186 +
23187 +out:
23188 +       /* AuTraceErr(err); */
23189 +       return err;
23190 +}
23191 +
23192 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
23193 +{
23194 +       int err;
23195 +       loff_t offset;
23196 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
23197 +
23198 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
23199 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
23200 +       err = offset;
23201 +       if (unlikely(offset != cookie->h_pos))
23202 +               goto out;
23203 +
23204 +       err = 0;
23205 +       do {
23206 +               arg->err = 0;
23207 +               au_fclr_rdu(cookie->flags, CALLED);
23208 +               /* smp_mb(); */
23209 +               err = vfsub_readdir(h_file, au_rdu_fill, arg);
23210 +               if (err >= 0)
23211 +                       err = arg->err;
23212 +       } while (!err
23213 +                && au_ftest_rdu(cookie->flags, CALLED)
23214 +                && !au_ftest_rdu(cookie->flags, FULL));
23215 +       cookie->h_pos = h_file->f_pos;
23216 +
23217 +out:
23218 +       AuTraceErr(err);
23219 +       return err;
23220 +}
23221 +
23222 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
23223 +{
23224 +       int err;
23225 +       aufs_bindex_t bend;
23226 +       struct au_rdu_arg arg;
23227 +       struct dentry *dentry;
23228 +       struct inode *inode;
23229 +       struct file *h_file;
23230 +       struct au_rdu_cookie *cookie = &rdu->cookie;
23231 +
23232 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
23233 +       if (unlikely(err)) {
23234 +               err = -EFAULT;
23235 +               AuTraceErr(err);
23236 +               goto out;
23237 +       }
23238 +       rdu->rent = 0;
23239 +       rdu->tail = rdu->ent;
23240 +       rdu->full = 0;
23241 +       arg.rdu = rdu;
23242 +       arg.ent = rdu->ent;
23243 +       arg.end = arg.ent.ul;
23244 +       arg.end += rdu->sz;
23245 +
23246 +       err = -ENOTDIR;
23247 +       if (unlikely(!file->f_op || !file->f_op->readdir))
23248 +               goto out;
23249 +
23250 +       err = security_file_permission(file, MAY_READ);
23251 +       AuTraceErr(err);
23252 +       if (unlikely(err))
23253 +               goto out;
23254 +
23255 +       dentry = file->f_dentry;
23256 +       inode = dentry->d_inode;
23257 +#if 1
23258 +       mutex_lock(&inode->i_mutex);
23259 +#else
23260 +       err = mutex_lock_killable(&inode->i_mutex);
23261 +       AuTraceErr(err);
23262 +       if (unlikely(err))
23263 +               goto out;
23264 +#endif
23265 +
23266 +       arg.sb = inode->i_sb;
23267 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
23268 +       if (unlikely(err))
23269 +               goto out_mtx;
23270 +       err = au_alive_dir(dentry);
23271 +       if (unlikely(err))
23272 +               goto out_si;
23273 +       /* todo: reval? */
23274 +       fi_read_lock(file);
23275 +
23276 +       err = -EAGAIN;
23277 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
23278 +                    && cookie->generation != au_figen(file)))
23279 +               goto out_unlock;
23280 +
23281 +       err = 0;
23282 +       if (!rdu->blk) {
23283 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
23284 +               if (!rdu->blk)
23285 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
23286 +       }
23287 +       bend = au_fbstart(file);
23288 +       if (cookie->bindex < bend)
23289 +               cookie->bindex = bend;
23290 +       bend = au_fbend_dir(file);
23291 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
23292 +       for (; !err && cookie->bindex <= bend;
23293 +            cookie->bindex++, cookie->h_pos = 0) {
23294 +               h_file = au_hf_dir(file, cookie->bindex);
23295 +               if (!h_file)
23296 +                       continue;
23297 +
23298 +               au_fclr_rdu(cookie->flags, FULL);
23299 +               err = au_rdu_do(h_file, &arg);
23300 +               AuTraceErr(err);
23301 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
23302 +                       break;
23303 +       }
23304 +       AuDbg("rent %llu\n", rdu->rent);
23305 +
23306 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
23307 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
23308 +               au_fset_rdu(cookie->flags, CONT);
23309 +               cookie->generation = au_figen(file);
23310 +       }
23311 +
23312 +       ii_read_lock_child(inode);
23313 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
23314 +       ii_read_unlock(inode);
23315 +
23316 +out_unlock:
23317 +       fi_read_unlock(file);
23318 +out_si:
23319 +       si_read_unlock(arg.sb);
23320 +out_mtx:
23321 +       mutex_unlock(&inode->i_mutex);
23322 +out:
23323 +       AuTraceErr(err);
23324 +       return err;
23325 +}
23326 +
23327 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
23328 +{
23329 +       int err;
23330 +       ino_t ino;
23331 +       unsigned long long nent;
23332 +       union au_rdu_ent_ul *u;
23333 +       struct au_rdu_ent ent;
23334 +       struct super_block *sb;
23335 +
23336 +       err = 0;
23337 +       nent = rdu->nent;
23338 +       u = &rdu->ent;
23339 +       sb = file->f_dentry->d_sb;
23340 +       si_read_lock(sb, AuLock_FLUSH);
23341 +       while (nent-- > 0) {
23342 +               /* unnecessary to support mmap_sem since this is a dir */
23343 +               err = copy_from_user(&ent, u->e, sizeof(ent));
23344 +               if (!err)
23345 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
23346 +               if (unlikely(err)) {
23347 +                       err = -EFAULT;
23348 +                       AuTraceErr(err);
23349 +                       break;
23350 +               }
23351 +
23352 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
23353 +               if (!ent.wh)
23354 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
23355 +               else
23356 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
23357 +                                       &ino);
23358 +               if (unlikely(err)) {
23359 +                       AuTraceErr(err);
23360 +                       break;
23361 +               }
23362 +
23363 +               err = __put_user(ino, &u->e->ino);
23364 +               if (unlikely(err)) {
23365 +                       err = -EFAULT;
23366 +                       AuTraceErr(err);
23367 +                       break;
23368 +               }
23369 +               u->ul += au_rdu_len(ent.nlen);
23370 +       }
23371 +       si_read_unlock(sb);
23372 +
23373 +       return err;
23374 +}
23375 +
23376 +/* ---------------------------------------------------------------------- */
23377 +
23378 +static int au_rdu_verify(struct aufs_rdu *rdu)
23379 +{
23380 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
23381 +             "%llu, b%d, 0x%x, g%u}\n",
23382 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
23383 +             rdu->blk,
23384 +             rdu->rent, rdu->shwh, rdu->full,
23385 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
23386 +             rdu->cookie.generation);
23387 +
23388 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
23389 +               return 0;
23390 +
23391 +       AuDbg("%u:%u\n",
23392 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
23393 +       return -EINVAL;
23394 +}
23395 +
23396 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
23397 +{
23398 +       long err, e;
23399 +       struct aufs_rdu rdu;
23400 +       void __user *p = (void __user *)arg;
23401 +
23402 +       err = copy_from_user(&rdu, p, sizeof(rdu));
23403 +       if (unlikely(err)) {
23404 +               err = -EFAULT;
23405 +               AuTraceErr(err);
23406 +               goto out;
23407 +       }
23408 +       err = au_rdu_verify(&rdu);
23409 +       if (unlikely(err))
23410 +               goto out;
23411 +
23412 +       switch (cmd) {
23413 +       case AUFS_CTL_RDU:
23414 +               err = au_rdu(file, &rdu);
23415 +               if (unlikely(err))
23416 +                       break;
23417 +
23418 +               e = copy_to_user(p, &rdu, sizeof(rdu));
23419 +               if (unlikely(e)) {
23420 +                       err = -EFAULT;
23421 +                       AuTraceErr(err);
23422 +               }
23423 +               break;
23424 +       case AUFS_CTL_RDU_INO:
23425 +               err = au_rdu_ino(file, &rdu);
23426 +               break;
23427 +
23428 +       default:
23429 +               /* err = -ENOTTY; */
23430 +               err = -EINVAL;
23431 +       }
23432 +
23433 +out:
23434 +       AuTraceErr(err);
23435 +       return err;
23436 +}
23437 +
23438 +#ifdef CONFIG_COMPAT
23439 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
23440 +{
23441 +       long err, e;
23442 +       struct aufs_rdu rdu;
23443 +       void __user *p = compat_ptr(arg);
23444 +
23445 +       /* todo: get_user()? */
23446 +       err = copy_from_user(&rdu, p, sizeof(rdu));
23447 +       if (unlikely(err)) {
23448 +               err = -EFAULT;
23449 +               AuTraceErr(err);
23450 +               goto out;
23451 +       }
23452 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
23453 +       err = au_rdu_verify(&rdu);
23454 +       if (unlikely(err))
23455 +               goto out;
23456 +
23457 +       switch (cmd) {
23458 +       case AUFS_CTL_RDU:
23459 +               err = au_rdu(file, &rdu);
23460 +               if (unlikely(err))
23461 +                       break;
23462 +
23463 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
23464 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
23465 +               e = copy_to_user(p, &rdu, sizeof(rdu));
23466 +               if (unlikely(e)) {
23467 +                       err = -EFAULT;
23468 +                       AuTraceErr(err);
23469 +               }
23470 +               break;
23471 +       case AUFS_CTL_RDU_INO:
23472 +               err = au_rdu_ino(file, &rdu);
23473 +               break;
23474 +
23475 +       default:
23476 +               /* err = -ENOTTY; */
23477 +               err = -EINVAL;
23478 +       }
23479 +
23480 +out:
23481 +       AuTraceErr(err);
23482 +       return err;
23483 +}
23484 +#endif
23485 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
23486 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
23487 +++ linux/fs/aufs/rwsem.h       2013-07-06 13:20:47.753531903 +0200
23488 @@ -0,0 +1,188 @@
23489 +/*
23490 + * Copyright (C) 2005-2013 Junjiro R. Okajima
23491 + *
23492 + * This program, aufs is free software; you can redistribute it and/or modify
23493 + * it under the terms of the GNU General Public License as published by
23494 + * the Free Software Foundation; either version 2 of the License, or
23495 + * (at your option) any later version.
23496 + *
23497 + * This program is distributed in the hope that it will be useful,
23498 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23499 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23500 + * GNU General Public License for more details.
23501 + *
23502 + * You should have received a copy of the GNU General Public License
23503 + * along with this program; if not, write to the Free Software
23504 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23505 + */
23506 +
23507 +/*
23508 + * simple read-write semaphore wrappers
23509 + */
23510 +
23511 +#ifndef __AUFS_RWSEM_H__
23512 +#define __AUFS_RWSEM_H__
23513 +
23514 +#ifdef __KERNEL__
23515 +
23516 +#include "debug.h"
23517 +
23518 +struct au_rwsem {
23519 +       struct rw_semaphore     rwsem;
23520 +#ifdef CONFIG_AUFS_DEBUG
23521 +       /* just for debugging, not almighty counter */
23522 +       atomic_t                rcnt, wcnt;
23523 +#endif
23524 +};
23525 +
23526 +#ifdef CONFIG_AUFS_DEBUG
23527 +#define AuDbgCntInit(rw) do { \
23528 +       atomic_set(&(rw)->rcnt, 0); \
23529 +       atomic_set(&(rw)->wcnt, 0); \
23530 +       smp_mb(); /* atomic set */ \
23531 +} while (0)
23532 +
23533 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
23534 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
23535 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
23536 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
23537 +#else
23538 +#define AuDbgCntInit(rw)       do {} while (0)
23539 +#define AuDbgRcntInc(rw)       do {} while (0)
23540 +#define AuDbgRcntDec(rw)       do {} while (0)
23541 +#define AuDbgWcntInc(rw)       do {} while (0)
23542 +#define AuDbgWcntDec(rw)       do {} while (0)
23543 +#endif /* CONFIG_AUFS_DEBUG */
23544 +
23545 +/* to debug easier, do not make them inlined functions */
23546 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
23547 +/* rwsem_is_locked() is unusable */
23548 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
23549 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
23550 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
23551 +                                       && atomic_read(&(rw)->wcnt) <= 0)
23552 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
23553 +                                       || atomic_read(&(rw)->wcnt))
23554 +
23555 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
23556 +
23557 +static inline void au_rw_init(struct au_rwsem *rw)
23558 +{
23559 +       AuDbgCntInit(rw);
23560 +       init_rwsem(&rw->rwsem);
23561 +}
23562 +
23563 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
23564 +{
23565 +       au_rw_init(rw);
23566 +       down_write(&rw->rwsem);
23567 +       AuDbgWcntInc(rw);
23568 +}
23569 +
23570 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
23571 +                                          unsigned int lsc)
23572 +{
23573 +       au_rw_init(rw);
23574 +       down_write_nested(&rw->rwsem, lsc);
23575 +       AuDbgWcntInc(rw);
23576 +}
23577 +
23578 +static inline void au_rw_read_lock(struct au_rwsem *rw)
23579 +{
23580 +       down_read(&rw->rwsem);
23581 +       AuDbgRcntInc(rw);
23582 +}
23583 +
23584 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
23585 +{
23586 +       down_read_nested(&rw->rwsem, lsc);
23587 +       AuDbgRcntInc(rw);
23588 +}
23589 +
23590 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
23591 +{
23592 +       AuRwMustReadLock(rw);
23593 +       AuDbgRcntDec(rw);
23594 +       up_read(&rw->rwsem);
23595 +}
23596 +
23597 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
23598 +{
23599 +       AuRwMustWriteLock(rw);
23600 +       AuDbgRcntInc(rw);
23601 +       AuDbgWcntDec(rw);
23602 +       downgrade_write(&rw->rwsem);
23603 +}
23604 +
23605 +static inline void au_rw_write_lock(struct au_rwsem *rw)
23606 +{
23607 +       down_write(&rw->rwsem);
23608 +       AuDbgWcntInc(rw);
23609 +}
23610 +
23611 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
23612 +                                          unsigned int lsc)
23613 +{
23614 +       down_write_nested(&rw->rwsem, lsc);
23615 +       AuDbgWcntInc(rw);
23616 +}
23617 +
23618 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
23619 +{
23620 +       AuRwMustWriteLock(rw);
23621 +       AuDbgWcntDec(rw);
23622 +       up_write(&rw->rwsem);
23623 +}
23624 +
23625 +/* why is not _nested version defined */
23626 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
23627 +{
23628 +       int ret = down_read_trylock(&rw->rwsem);
23629 +       if (ret)
23630 +               AuDbgRcntInc(rw);
23631 +       return ret;
23632 +}
23633 +
23634 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
23635 +{
23636 +       int ret = down_write_trylock(&rw->rwsem);
23637 +       if (ret)
23638 +               AuDbgWcntInc(rw);
23639 +       return ret;
23640 +}
23641 +
23642 +#undef AuDbgCntInit
23643 +#undef AuDbgRcntInc
23644 +#undef AuDbgRcntDec
23645 +#undef AuDbgWcntInc
23646 +#undef AuDbgWcntDec
23647 +
23648 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
23649 +static inline void prefix##_read_lock(param) \
23650 +{ au_rw_read_lock(rwsem); } \
23651 +static inline void prefix##_write_lock(param) \
23652 +{ au_rw_write_lock(rwsem); } \
23653 +static inline int prefix##_read_trylock(param) \
23654 +{ return au_rw_read_trylock(rwsem); } \
23655 +static inline int prefix##_write_trylock(param) \
23656 +{ return au_rw_write_trylock(rwsem); }
23657 +/* why is not _nested version defined */
23658 +/* static inline void prefix##_read_trylock_nested(param, lsc)
23659 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
23660 +static inline void prefix##_write_trylock_nestd(param, lsc)
23661 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
23662 +
23663 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
23664 +static inline void prefix##_read_unlock(param) \
23665 +{ au_rw_read_unlock(rwsem); } \
23666 +static inline void prefix##_write_unlock(param) \
23667 +{ au_rw_write_unlock(rwsem); } \
23668 +static inline void prefix##_downgrade_lock(param) \
23669 +{ au_rw_dgrade_lock(rwsem); }
23670 +
23671 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
23672 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
23673 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
23674 +
23675 +#endif /* __KERNEL__ */
23676 +#endif /* __AUFS_RWSEM_H__ */
23677 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
23678 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
23679 +++ linux/fs/aufs/sbinfo.c      2013-07-06 13:20:47.753531903 +0200
23680 @@ -0,0 +1,348 @@
23681 +/*
23682 + * Copyright (C) 2005-2013 Junjiro R. Okajima
23683 + *
23684 + * This program, aufs is free software; you can redistribute it and/or modify
23685 + * it under the terms of the GNU General Public License as published by
23686 + * the Free Software Foundation; either version 2 of the License, or
23687 + * (at your option) any later version.
23688 + *
23689 + * This program is distributed in the hope that it will be useful,
23690 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23691 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23692 + * GNU General Public License for more details.
23693 + *
23694 + * You should have received a copy of the GNU General Public License
23695 + * along with this program; if not, write to the Free Software
23696 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23697 + */
23698 +
23699 +/*
23700 + * superblock private data
23701 + */
23702 +
23703 +#include "aufs.h"
23704 +
23705 +/*
23706 + * they are necessary regardless sysfs is disabled.
23707 + */
23708 +void au_si_free(struct kobject *kobj)
23709 +{
23710 +       int i;
23711 +       struct au_sbinfo *sbinfo;
23712 +       char *locked __maybe_unused; /* debug only */
23713 +
23714 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
23715 +       for (i = 0; i < AuPlink_NHASH; i++)
23716 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
23717 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
23718 +
23719 +       au_rw_write_lock(&sbinfo->si_rwsem);
23720 +       au_br_free(sbinfo);
23721 +       au_rw_write_unlock(&sbinfo->si_rwsem);
23722 +
23723 +       AuDebugOn(radix_tree_gang_lookup
23724 +                 (&sbinfo->au_si_pid.tree, (void **)&locked,
23725 +                  /*first_index*/PID_MAX_DEFAULT - 1,
23726 +                  /*max_items*/sizeof(locked)/sizeof(*locked)));
23727 +
23728 +       kfree(sbinfo->si_branch);
23729 +       kfree(sbinfo->au_si_pid.bitmap);
23730 +       mutex_destroy(&sbinfo->si_xib_mtx);
23731 +       AuRwDestroy(&sbinfo->si_rwsem);
23732 +
23733 +       kfree(sbinfo);
23734 +}
23735 +
23736 +int au_si_alloc(struct super_block *sb)
23737 +{
23738 +       int err, i;
23739 +       struct au_sbinfo *sbinfo;
23740 +       static struct lock_class_key aufs_si;
23741 +
23742 +       err = -ENOMEM;
23743 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
23744 +       if (unlikely(!sbinfo))
23745 +               goto out;
23746 +
23747 +       BUILD_BUG_ON(sizeof(unsigned long) !=
23748 +                    sizeof(*sbinfo->au_si_pid.bitmap));
23749 +       sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
23750 +                                       sizeof(*sbinfo->au_si_pid.bitmap),
23751 +                                       GFP_NOFS);
23752 +       if (unlikely(!sbinfo->au_si_pid.bitmap))
23753 +               goto out_sbinfo;
23754 +
23755 +       /* will be reallocated separately */
23756 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
23757 +       if (unlikely(!sbinfo->si_branch))
23758 +               goto out_pidmap;
23759 +
23760 +       err = sysaufs_si_init(sbinfo);
23761 +       if (unlikely(err))
23762 +               goto out_br;
23763 +
23764 +       au_nwt_init(&sbinfo->si_nowait);
23765 +       au_rw_init_wlock(&sbinfo->si_rwsem);
23766 +       au_rw_class(&sbinfo->si_rwsem, &aufs_si);
23767 +       spin_lock_init(&sbinfo->au_si_pid.tree_lock);
23768 +       INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
23769 +
23770 +       atomic_long_set(&sbinfo->si_ninodes, 0);
23771 +       atomic_long_set(&sbinfo->si_nfiles, 0);
23772 +
23773 +       sbinfo->si_bend = -1;
23774 +
23775 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
23776 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
23777 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
23778 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
23779 +
23780 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
23781 +
23782 +       mutex_init(&sbinfo->si_xib_mtx);
23783 +       sbinfo->si_xino_brid = -1;
23784 +       /* leave si_xib_last_pindex and si_xib_next_bit */
23785 +
23786 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
23787 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
23788 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
23789 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
23790 +
23791 +       for (i = 0; i < AuPlink_NHASH; i++)
23792 +               au_sphl_init(sbinfo->si_plink + i);
23793 +       init_waitqueue_head(&sbinfo->si_plink_wq);
23794 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
23795 +
23796 +       au_sphl_init(&sbinfo->si_files);
23797 +
23798 +       /* leave other members for sysaufs and si_mnt. */
23799 +       sbinfo->si_sb = sb;
23800 +       sb->s_fs_info = sbinfo;
23801 +       si_pid_set(sb);
23802 +       au_debug_sbinfo_init(sbinfo);
23803 +       return 0; /* success */
23804 +
23805 +out_br:
23806 +       kfree(sbinfo->si_branch);
23807 +out_pidmap:
23808 +       kfree(sbinfo->au_si_pid.bitmap);
23809 +out_sbinfo:
23810 +       kfree(sbinfo);
23811 +out:
23812 +       return err;
23813 +}
23814 +
23815 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
23816 +{
23817 +       int err, sz;
23818 +       struct au_branch **brp;
23819 +
23820 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
23821 +
23822 +       err = -ENOMEM;
23823 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
23824 +       if (unlikely(!sz))
23825 +               sz = sizeof(*brp);
23826 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
23827 +       if (brp) {
23828 +               sbinfo->si_branch = brp;
23829 +               err = 0;
23830 +       }
23831 +
23832 +       return err;
23833 +}
23834 +
23835 +/* ---------------------------------------------------------------------- */
23836 +
23837 +unsigned int au_sigen_inc(struct super_block *sb)
23838 +{
23839 +       unsigned int gen;
23840 +
23841 +       SiMustWriteLock(sb);
23842 +
23843 +       gen = ++au_sbi(sb)->si_generation;
23844 +       au_update_digen(sb->s_root);
23845 +       au_update_iigen(sb->s_root->d_inode, /*half*/0);
23846 +       sb->s_root->d_inode->i_version++;
23847 +       return gen;
23848 +}
23849 +
23850 +aufs_bindex_t au_new_br_id(struct super_block *sb)
23851 +{
23852 +       aufs_bindex_t br_id;
23853 +       int i;
23854 +       struct au_sbinfo *sbinfo;
23855 +
23856 +       SiMustWriteLock(sb);
23857 +
23858 +       sbinfo = au_sbi(sb);
23859 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
23860 +               br_id = ++sbinfo->si_last_br_id;
23861 +               AuDebugOn(br_id < 0);
23862 +               if (br_id && au_br_index(sb, br_id) < 0)
23863 +                       return br_id;
23864 +       }
23865 +
23866 +       return -1;
23867 +}
23868 +
23869 +/* ---------------------------------------------------------------------- */
23870 +
23871 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
23872 +int si_read_lock(struct super_block *sb, int flags)
23873 +{
23874 +       int err;
23875 +
23876 +       err = 0;
23877 +       if (au_ftest_lock(flags, FLUSH))
23878 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
23879 +
23880 +       si_noflush_read_lock(sb);
23881 +       err = au_plink_maint(sb, flags);
23882 +       if (unlikely(err))
23883 +               si_read_unlock(sb);
23884 +
23885 +       return err;
23886 +}
23887 +
23888 +int si_write_lock(struct super_block *sb, int flags)
23889 +{
23890 +       int err;
23891 +
23892 +       if (au_ftest_lock(flags, FLUSH))
23893 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
23894 +
23895 +       si_noflush_write_lock(sb);
23896 +       err = au_plink_maint(sb, flags);
23897 +       if (unlikely(err))
23898 +               si_write_unlock(sb);
23899 +
23900 +       return err;
23901 +}
23902 +
23903 +/* dentry and super_block lock. call at entry point */
23904 +int aufs_read_lock(struct dentry *dentry, int flags)
23905 +{
23906 +       int err;
23907 +       struct super_block *sb;
23908 +
23909 +       sb = dentry->d_sb;
23910 +       err = si_read_lock(sb, flags);
23911 +       if (unlikely(err))
23912 +               goto out;
23913 +
23914 +       if (au_ftest_lock(flags, DW))
23915 +               di_write_lock_child(dentry);
23916 +       else
23917 +               di_read_lock_child(dentry, flags);
23918 +
23919 +       if (au_ftest_lock(flags, GEN)) {
23920 +               err = au_digen_test(dentry, au_sigen(sb));
23921 +               AuDebugOn(!err && au_dbrange_test(dentry));
23922 +               if (unlikely(err))
23923 +                       aufs_read_unlock(dentry, flags);
23924 +       }
23925 +
23926 +out:
23927 +       return err;
23928 +}
23929 +
23930 +void aufs_read_unlock(struct dentry *dentry, int flags)
23931 +{
23932 +       if (au_ftest_lock(flags, DW))
23933 +               di_write_unlock(dentry);
23934 +       else
23935 +               di_read_unlock(dentry, flags);
23936 +       si_read_unlock(dentry->d_sb);
23937 +}
23938 +
23939 +void aufs_write_lock(struct dentry *dentry)
23940 +{
23941 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
23942 +       di_write_lock_child(dentry);
23943 +}
23944 +
23945 +void aufs_write_unlock(struct dentry *dentry)
23946 +{
23947 +       di_write_unlock(dentry);
23948 +       si_write_unlock(dentry->d_sb);
23949 +}
23950 +
23951 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
23952 +{
23953 +       int err;
23954 +       unsigned int sigen;
23955 +       struct super_block *sb;
23956 +
23957 +       sb = d1->d_sb;
23958 +       err = si_read_lock(sb, flags);
23959 +       if (unlikely(err))
23960 +               goto out;
23961 +
23962 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
23963 +
23964 +       if (au_ftest_lock(flags, GEN)) {
23965 +               sigen = au_sigen(sb);
23966 +               err = au_digen_test(d1, sigen);
23967 +               AuDebugOn(!err && au_dbrange_test(d1));
23968 +               if (!err) {
23969 +                       err = au_digen_test(d2, sigen);
23970 +                       AuDebugOn(!err && au_dbrange_test(d2));
23971 +               }
23972 +               if (unlikely(err))
23973 +                       aufs_read_and_write_unlock2(d1, d2);
23974 +       }
23975 +
23976 +out:
23977 +       return err;
23978 +}
23979 +
23980 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
23981 +{
23982 +       di_write_unlock2(d1, d2);
23983 +       si_read_unlock(d1->d_sb);
23984 +}
23985 +
23986 +/* ---------------------------------------------------------------------- */
23987 +
23988 +int si_pid_test_slow(struct super_block *sb)
23989 +{
23990 +       void *p;
23991 +
23992 +       rcu_read_lock();
23993 +       p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
23994 +       rcu_read_unlock();
23995 +
23996 +       return (long)!!p;
23997 +}
23998 +
23999 +void si_pid_set_slow(struct super_block *sb)
24000 +{
24001 +       int err;
24002 +       struct au_sbinfo *sbinfo;
24003 +
24004 +       AuDebugOn(si_pid_test_slow(sb));
24005 +
24006 +       sbinfo = au_sbi(sb);
24007 +       err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
24008 +       AuDebugOn(err);
24009 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
24010 +       err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
24011 +                               /*any valid ptr*/sb);
24012 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
24013 +       AuDebugOn(err);
24014 +       radix_tree_preload_end();
24015 +}
24016 +
24017 +void si_pid_clr_slow(struct super_block *sb)
24018 +{
24019 +       void *p;
24020 +       struct au_sbinfo *sbinfo;
24021 +
24022 +       AuDebugOn(!si_pid_test_slow(sb));
24023 +
24024 +       sbinfo = au_sbi(sb);
24025 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
24026 +       p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
24027 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
24028 +}
24029 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
24030 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
24031 +++ linux/fs/aufs/spl.h 2013-07-06 13:20:47.753531903 +0200
24032 @@ -0,0 +1,112 @@
24033 +/*
24034 + * Copyright (C) 2005-2013 Junjiro R. Okajima
24035 + *
24036 + * This program, aufs is free software; you can redistribute it and/or modify
24037 + * it under the terms of the GNU General Public License as published by
24038 + * the Free Software Foundation; either version 2 of the License, or
24039 + * (at your option) any later version.
24040 + *
24041 + * This program is distributed in the hope that it will be useful,
24042 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24043 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24044 + * GNU General Public License for more details.
24045 + *
24046 + * You should have received a copy of the GNU General Public License
24047 + * along with this program; if not, write to the Free Software
24048 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24049 + */
24050 +
24051 +/*
24052 + * simple list protected by a spinlock
24053 + */
24054 +
24055 +#ifndef __AUFS_SPL_H__
24056 +#define __AUFS_SPL_H__
24057 +
24058 +#ifdef __KERNEL__
24059 +
24060 +struct au_splhead {
24061 +       spinlock_t              spin;
24062 +       struct list_head        head;
24063 +};
24064 +
24065 +static inline void au_spl_init(struct au_splhead *spl)
24066 +{
24067 +       spin_lock_init(&spl->spin);
24068 +       INIT_LIST_HEAD(&spl->head);
24069 +}
24070 +
24071 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
24072 +{
24073 +       spin_lock(&spl->spin);
24074 +       list_add(list, &spl->head);
24075 +       spin_unlock(&spl->spin);
24076 +}
24077 +
24078 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
24079 +{
24080 +       spin_lock(&spl->spin);
24081 +       list_del(list);
24082 +       spin_unlock(&spl->spin);
24083 +}
24084 +
24085 +static inline void au_spl_del_rcu(struct list_head *list,
24086 +                                 struct au_splhead *spl)
24087 +{
24088 +       spin_lock(&spl->spin);
24089 +       list_del_rcu(list);
24090 +       spin_unlock(&spl->spin);
24091 +}
24092 +
24093 +/* ---------------------------------------------------------------------- */
24094 +
24095 +struct au_sphlhead {
24096 +       spinlock_t              spin;
24097 +       struct hlist_head       head;
24098 +};
24099 +
24100 +static inline void au_sphl_init(struct au_sphlhead *sphl)
24101 +{
24102 +       spin_lock_init(&sphl->spin);
24103 +       INIT_HLIST_HEAD(&sphl->head);
24104 +}
24105 +
24106 +static inline void au_sphl_add(struct hlist_node *hlist,
24107 +                              struct au_sphlhead *sphl)
24108 +{
24109 +       spin_lock(&sphl->spin);
24110 +       hlist_add_head(hlist, &sphl->head);
24111 +       spin_unlock(&sphl->spin);
24112 +}
24113 +
24114 +static inline void au_sphl_del(struct hlist_node *hlist,
24115 +                              struct au_sphlhead *sphl)
24116 +{
24117 +       spin_lock(&sphl->spin);
24118 +       hlist_del(hlist);
24119 +       spin_unlock(&sphl->spin);
24120 +}
24121 +
24122 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
24123 +                                  struct au_sphlhead *sphl)
24124 +{
24125 +       spin_lock(&sphl->spin);
24126 +       hlist_del_rcu(hlist);
24127 +       spin_unlock(&sphl->spin);
24128 +}
24129 +
24130 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
24131 +{
24132 +       unsigned long cnt;
24133 +       struct hlist_node *pos;
24134 +
24135 +       cnt = 0;
24136 +       spin_lock(&sphl->spin);
24137 +       hlist_for_each(pos, &sphl->head)
24138 +               cnt++;
24139 +       spin_unlock(&sphl->spin);
24140 +       return cnt;
24141 +}
24142 +
24143 +#endif /* __KERNEL__ */
24144 +#endif /* __AUFS_SPL_H__ */
24145 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
24146 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
24147 +++ linux/fs/aufs/super.c       2013-08-23 23:59:39.634916914 +0200
24148 @@ -0,0 +1,992 @@
24149 +/*
24150 + * Copyright (C) 2005-2013 Junjiro R. Okajima
24151 + *
24152 + * This program, aufs is free software; you can redistribute it and/or modify
24153 + * it under the terms of the GNU General Public License as published by
24154 + * the Free Software Foundation; either version 2 of the License, or
24155 + * (at your option) any later version.
24156 + *
24157 + * This program is distributed in the hope that it will be useful,
24158 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24159 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24160 + * GNU General Public License for more details.
24161 + *
24162 + * You should have received a copy of the GNU General Public License
24163 + * along with this program; if not, write to the Free Software
24164 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24165 + */
24166 +
24167 +/*
24168 + * mount and super_block operations
24169 + */
24170 +
24171 +#include <linux/mm.h>
24172 +#include <linux/module.h>
24173 +#include <linux/seq_file.h>
24174 +#include <linux/statfs.h>
24175 +#include <linux/vmalloc.h>
24176 +#include <linux/writeback.h>
24177 +#include "aufs.h"
24178 +
24179 +/*
24180 + * super_operations
24181 + */
24182 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
24183 +{
24184 +       struct au_icntnr *c;
24185 +
24186 +       c = au_cache_alloc_icntnr();
24187 +       if (c) {
24188 +               au_icntnr_init(c);
24189 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
24190 +               c->iinfo.ii_hinode = NULL;
24191 +               return &c->vfs_inode;
24192 +       }
24193 +       return NULL;
24194 +}
24195 +
24196 +static void aufs_destroy_inode_cb(struct rcu_head *head)
24197 +{
24198 +       struct inode *inode = container_of(head, struct inode, i_rcu);
24199 +
24200 +       INIT_HLIST_HEAD(&inode->i_dentry);
24201 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
24202 +}
24203 +
24204 +static void aufs_destroy_inode(struct inode *inode)
24205 +{
24206 +       au_iinfo_fin(inode);
24207 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
24208 +}
24209 +
24210 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
24211 +{
24212 +       struct inode *inode;
24213 +       int err;
24214 +
24215 +       inode = iget_locked(sb, ino);
24216 +       if (unlikely(!inode)) {
24217 +               inode = ERR_PTR(-ENOMEM);
24218 +               goto out;
24219 +       }
24220 +       if (!(inode->i_state & I_NEW))
24221 +               goto out;
24222 +
24223 +       err = au_xigen_new(inode);
24224 +       if (!err)
24225 +               err = au_iinfo_init(inode);
24226 +       if (!err)
24227 +               inode->i_version++;
24228 +       else {
24229 +               iget_failed(inode);
24230 +               inode = ERR_PTR(err);
24231 +       }
24232 +
24233 +out:
24234 +       /* never return NULL */
24235 +       AuDebugOn(!inode);
24236 +       AuTraceErrPtr(inode);
24237 +       return inode;
24238 +}
24239 +
24240 +/* lock free root dinfo */
24241 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
24242 +{
24243 +       int err;
24244 +       aufs_bindex_t bindex, bend;
24245 +       struct path path;
24246 +       struct au_hdentry *hdp;
24247 +       struct au_branch *br;
24248 +       char *perm;
24249 +
24250 +       err = 0;
24251 +       bend = au_sbend(sb);
24252 +       hdp = au_di(sb->s_root)->di_hdentry;
24253 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
24254 +               br = au_sbr(sb, bindex);
24255 +               path.mnt = au_br_mnt(br);
24256 +               path.dentry = hdp[bindex].hd_dentry;
24257 +               err = au_seq_path(seq, &path);
24258 +               if (err > 0) {
24259 +                       perm = au_optstr_br_perm(br->br_perm);
24260 +                       if (perm) {
24261 +                               err = seq_printf(seq, "=%s", perm);
24262 +                               kfree(perm);
24263 +                               if (err == -1)
24264 +                                       err = -E2BIG;
24265 +                       } else
24266 +                               err = -ENOMEM;
24267 +               }
24268 +               if (!err && bindex != bend)
24269 +                       err = seq_putc(seq, ':');
24270 +       }
24271 +
24272 +       return err;
24273 +}
24274 +
24275 +static void au_show_wbr_create(struct seq_file *m, int v,
24276 +                              struct au_sbinfo *sbinfo)
24277 +{
24278 +       const char *pat;
24279 +
24280 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24281 +
24282 +       seq_puts(m, ",create=");
24283 +       pat = au_optstr_wbr_create(v);
24284 +       switch (v) {
24285 +       case AuWbrCreate_TDP:
24286 +       case AuWbrCreate_RR:
24287 +       case AuWbrCreate_MFS:
24288 +       case AuWbrCreate_PMFS:
24289 +               seq_puts(m, pat);
24290 +               break;
24291 +       case AuWbrCreate_MFSV:
24292 +               seq_printf(m, /*pat*/"mfs:%lu",
24293 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24294 +                          / MSEC_PER_SEC);
24295 +               break;
24296 +       case AuWbrCreate_PMFSV:
24297 +               seq_printf(m, /*pat*/"pmfs:%lu",
24298 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24299 +                          / MSEC_PER_SEC);
24300 +               break;
24301 +       case AuWbrCreate_MFSRR:
24302 +               seq_printf(m, /*pat*/"mfsrr:%llu",
24303 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
24304 +               break;
24305 +       case AuWbrCreate_MFSRRV:
24306 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
24307 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
24308 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24309 +                          / MSEC_PER_SEC);
24310 +               break;
24311 +       }
24312 +}
24313 +
24314 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
24315 +{
24316 +#ifdef CONFIG_SYSFS
24317 +       return 0;
24318 +#else
24319 +       int err;
24320 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
24321 +       aufs_bindex_t bindex, brid;
24322 +       struct qstr *name;
24323 +       struct file *f;
24324 +       struct dentry *d, *h_root;
24325 +       struct au_hdentry *hdp;
24326 +
24327 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24328 +
24329 +       err = 0;
24330 +       f = au_sbi(sb)->si_xib;
24331 +       if (!f)
24332 +               goto out;
24333 +
24334 +       /* stop printing the default xino path on the first writable branch */
24335 +       h_root = NULL;
24336 +       brid = au_xino_brid(sb);
24337 +       if (brid >= 0) {
24338 +               bindex = au_br_index(sb, brid);
24339 +               hdp = au_di(sb->s_root)->di_hdentry;
24340 +               h_root = hdp[0 + bindex].hd_dentry;
24341 +       }
24342 +       d = f->f_dentry;
24343 +       name = &d->d_name;
24344 +       /* safe ->d_parent because the file is unlinked */
24345 +       if (d->d_parent == h_root
24346 +           && name->len == len
24347 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
24348 +               goto out;
24349 +
24350 +       seq_puts(seq, ",xino=");
24351 +       err = au_xino_path(seq, f);
24352 +
24353 +out:
24354 +       return err;
24355 +#endif
24356 +}
24357 +
24358 +/* seq_file will re-call me in case of too long string */
24359 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
24360 +{
24361 +       int err;
24362 +       unsigned int mnt_flags, v;
24363 +       struct super_block *sb;
24364 +       struct au_sbinfo *sbinfo;
24365 +
24366 +#define AuBool(name, str) do { \
24367 +       v = au_opt_test(mnt_flags, name); \
24368 +       if (v != au_opt_test(AuOpt_Def, name)) \
24369 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
24370 +} while (0)
24371 +
24372 +#define AuStr(name, str) do { \
24373 +       v = mnt_flags & AuOptMask_##name; \
24374 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
24375 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
24376 +} while (0)
24377 +
24378 +#define AuUInt(name, str, val) do { \
24379 +       if (val != AUFS_##name##_DEF) \
24380 +               seq_printf(m, "," #str "=%u", val); \
24381 +} while (0)
24382 +
24383 +       /* lock free root dinfo */
24384 +       sb = dentry->d_sb;
24385 +       si_noflush_read_lock(sb);
24386 +       sbinfo = au_sbi(sb);
24387 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
24388 +
24389 +       mnt_flags = au_mntflags(sb);
24390 +       if (au_opt_test(mnt_flags, XINO)) {
24391 +               err = au_show_xino(m, sb);
24392 +               if (unlikely(err))
24393 +                       goto out;
24394 +       } else
24395 +               seq_puts(m, ",noxino");
24396 +
24397 +       AuBool(TRUNC_XINO, trunc_xino);
24398 +       AuStr(UDBA, udba);
24399 +       AuBool(SHWH, shwh);
24400 +       AuBool(PLINK, plink);
24401 +       AuBool(DIO, dio);
24402 +       /* AuBool(DIRPERM1, dirperm1); */
24403 +       /* AuBool(REFROF, refrof); */
24404 +
24405 +       v = sbinfo->si_wbr_create;
24406 +       if (v != AuWbrCreate_Def)
24407 +               au_show_wbr_create(m, v, sbinfo);
24408 +
24409 +       v = sbinfo->si_wbr_copyup;
24410 +       if (v != AuWbrCopyup_Def)
24411 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
24412 +
24413 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
24414 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
24415 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
24416 +
24417 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
24418 +
24419 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
24420 +       AuUInt(RDCACHE, rdcache, v);
24421 +
24422 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
24423 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
24424 +
24425 +       AuBool(SUM, sum);
24426 +       /* AuBool(SUM_W, wsum); */
24427 +       AuBool(WARN_PERM, warn_perm);
24428 +       AuBool(VERBOSE, verbose);
24429 +
24430 +out:
24431 +       /* be sure to print "br:" last */
24432 +       if (!sysaufs_brs) {
24433 +               seq_puts(m, ",br:");
24434 +               au_show_brs(m, sb);
24435 +       }
24436 +       si_read_unlock(sb);
24437 +       return 0;
24438 +
24439 +#undef AuBool
24440 +#undef AuStr
24441 +#undef AuUInt
24442 +}
24443 +
24444 +/* ---------------------------------------------------------------------- */
24445 +
24446 +/* sum mode which returns the summation for statfs(2) */
24447 +
24448 +static u64 au_add_till_max(u64 a, u64 b)
24449 +{
24450 +       u64 old;
24451 +
24452 +       old = a;
24453 +       a += b;
24454 +       if (old <= a)
24455 +               return a;
24456 +       return ULLONG_MAX;
24457 +}
24458 +
24459 +static u64 au_mul_till_max(u64 a, long mul)
24460 +{
24461 +       u64 old;
24462 +
24463 +       old = a;
24464 +       a *= mul;
24465 +       if (old <= a)
24466 +               return a;
24467 +       return ULLONG_MAX;
24468 +}
24469 +
24470 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
24471 +{
24472 +       int err;
24473 +       long bsize, factor;
24474 +       u64 blocks, bfree, bavail, files, ffree;
24475 +       aufs_bindex_t bend, bindex, i;
24476 +       unsigned char shared;
24477 +       struct path h_path;
24478 +       struct super_block *h_sb;
24479 +
24480 +       err = 0;
24481 +       bsize = LONG_MAX;
24482 +       files = 0;
24483 +       ffree = 0;
24484 +       blocks = 0;
24485 +       bfree = 0;
24486 +       bavail = 0;
24487 +       bend = au_sbend(sb);
24488 +       for (bindex = 0; bindex <= bend; bindex++) {
24489 +               h_path.mnt = au_sbr_mnt(sb, bindex);
24490 +               h_sb = h_path.mnt->mnt_sb;
24491 +               shared = 0;
24492 +               for (i = 0; !shared && i < bindex; i++)
24493 +                       shared = (au_sbr_sb(sb, i) == h_sb);
24494 +               if (shared)
24495 +                       continue;
24496 +
24497 +               /* sb->s_root for NFS is unreliable */
24498 +               h_path.dentry = h_path.mnt->mnt_root;
24499 +               err = vfs_statfs(&h_path, buf);
24500 +               if (unlikely(err))
24501 +                       goto out;
24502 +
24503 +               if (bsize > buf->f_bsize) {
24504 +                       /*
24505 +                        * we will reduce bsize, so we have to expand blocks
24506 +                        * etc. to match them again
24507 +                        */
24508 +                       factor = (bsize / buf->f_bsize);
24509 +                       blocks = au_mul_till_max(blocks, factor);
24510 +                       bfree = au_mul_till_max(bfree, factor);
24511 +                       bavail = au_mul_till_max(bavail, factor);
24512 +                       bsize = buf->f_bsize;
24513 +               }
24514 +
24515 +               factor = (buf->f_bsize / bsize);
24516 +               blocks = au_add_till_max(blocks,
24517 +                               au_mul_till_max(buf->f_blocks, factor));
24518 +               bfree = au_add_till_max(bfree,
24519 +                               au_mul_till_max(buf->f_bfree, factor));
24520 +               bavail = au_add_till_max(bavail,
24521 +                               au_mul_till_max(buf->f_bavail, factor));
24522 +               files = au_add_till_max(files, buf->f_files);
24523 +               ffree = au_add_till_max(ffree, buf->f_ffree);
24524 +       }
24525 +
24526 +       buf->f_bsize = bsize;
24527 +       buf->f_blocks = blocks;
24528 +       buf->f_bfree = bfree;
24529 +       buf->f_bavail = bavail;
24530 +       buf->f_files = files;
24531 +       buf->f_ffree = ffree;
24532 +       buf->f_frsize = 0;
24533 +
24534 +out:
24535 +       return err;
24536 +}
24537 +
24538 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
24539 +{
24540 +       int err;
24541 +       struct path h_path;
24542 +       struct super_block *sb;
24543 +
24544 +       /* lock free root dinfo */
24545 +       sb = dentry->d_sb;
24546 +       si_noflush_read_lock(sb);
24547 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
24548 +               /* sb->s_root for NFS is unreliable */
24549 +               h_path.mnt = au_sbr_mnt(sb, 0);
24550 +               h_path.dentry = h_path.mnt->mnt_root;
24551 +               err = vfs_statfs(&h_path, buf);
24552 +       } else
24553 +               err = au_statfs_sum(sb, buf);
24554 +       si_read_unlock(sb);
24555 +
24556 +       if (!err) {
24557 +               buf->f_type = AUFS_SUPER_MAGIC;
24558 +               buf->f_namelen = AUFS_MAX_NAMELEN;
24559 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
24560 +       }
24561 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
24562 +
24563 +       return err;
24564 +}
24565 +
24566 +/* ---------------------------------------------------------------------- */
24567 +
24568 +static int aufs_sync_fs(struct super_block *sb, int wait)
24569 +{
24570 +       int err, e;
24571 +       aufs_bindex_t bend, bindex;
24572 +       struct au_branch *br;
24573 +       struct super_block *h_sb;
24574 +
24575 +       err = 0;
24576 +       si_noflush_read_lock(sb);
24577 +       bend = au_sbend(sb);
24578 +       for (bindex = 0; bindex <= bend; bindex++) {
24579 +               br = au_sbr(sb, bindex);
24580 +               if (!au_br_writable(br->br_perm))
24581 +                       continue;
24582 +
24583 +               h_sb = au_sbr_sb(sb, bindex);
24584 +               if (h_sb->s_op->sync_fs) {
24585 +                       e = h_sb->s_op->sync_fs(h_sb, wait);
24586 +                       if (unlikely(e && !err))
24587 +                               err = e;
24588 +                       /* go on even if an error happens */
24589 +               }
24590 +       }
24591 +       si_read_unlock(sb);
24592 +
24593 +       return err;
24594 +}
24595 +
24596 +/* ---------------------------------------------------------------------- */
24597 +
24598 +/* final actions when unmounting a file system */
24599 +static void aufs_put_super(struct super_block *sb)
24600 +{
24601 +       struct au_sbinfo *sbinfo;
24602 +
24603 +       sbinfo = au_sbi(sb);
24604 +       if (!sbinfo)
24605 +               return;
24606 +
24607 +       dbgaufs_si_fin(sbinfo);
24608 +       kobject_put(&sbinfo->si_kobj);
24609 +}
24610 +
24611 +/* ---------------------------------------------------------------------- */
24612 +
24613 +void au_array_free(void *array)
24614 +{
24615 +       if (array) {
24616 +               if (!is_vmalloc_addr(array))
24617 +                       kfree(array);
24618 +               else
24619 +                       vfree(array);
24620 +       }
24621 +}
24622 +
24623 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
24624 +{
24625 +       void *array;
24626 +       unsigned long long n;
24627 +
24628 +       array = NULL;
24629 +       n = 0;
24630 +       if (!*hint)
24631 +               goto out;
24632 +
24633 +       if (*hint > ULLONG_MAX / sizeof(array)) {
24634 +               array = ERR_PTR(-EMFILE);
24635 +               pr_err("hint %llu\n", *hint);
24636 +               goto out;
24637 +       }
24638 +
24639 +       array = kmalloc(sizeof(array) * *hint, GFP_NOFS);
24640 +       if (unlikely(!array))
24641 +               array = vmalloc(sizeof(array) * *hint);
24642 +       if (unlikely(!array)) {
24643 +               array = ERR_PTR(-ENOMEM);
24644 +               goto out;
24645 +       }
24646 +
24647 +       n = cb(array, *hint, arg);
24648 +       AuDebugOn(n > *hint);
24649 +
24650 +out:
24651 +       *hint = n;
24652 +       return array;
24653 +}
24654 +
24655 +static unsigned long long au_iarray_cb(void *a,
24656 +                                      unsigned long long max __maybe_unused,
24657 +                                      void *arg)
24658 +{
24659 +       unsigned long long n;
24660 +       struct inode **p, *inode;
24661 +       struct list_head *head;
24662 +
24663 +       n = 0;
24664 +       p = a;
24665 +       head = arg;
24666 +       spin_lock(&inode_sb_list_lock);
24667 +       list_for_each_entry(inode, head, i_sb_list) {
24668 +               if (!is_bad_inode(inode)
24669 +                   && au_ii(inode)->ii_bstart >= 0) {
24670 +                       spin_lock(&inode->i_lock);
24671 +                       if (atomic_read(&inode->i_count)) {
24672 +                               au_igrab(inode);
24673 +                               *p++ = inode;
24674 +                               n++;
24675 +                               AuDebugOn(n > max);
24676 +                       }
24677 +                       spin_unlock(&inode->i_lock);
24678 +               }
24679 +       }
24680 +       spin_unlock(&inode_sb_list_lock);
24681 +
24682 +       return n;
24683 +}
24684 +
24685 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
24686 +{
24687 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
24688 +       return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
24689 +}
24690 +
24691 +void au_iarray_free(struct inode **a, unsigned long long max)
24692 +{
24693 +       unsigned long long ull;
24694 +
24695 +       for (ull = 0; ull < max; ull++)
24696 +               iput(a[ull]);
24697 +       au_array_free(a);
24698 +}
24699 +
24700 +/* ---------------------------------------------------------------------- */
24701 +
24702 +/*
24703 + * refresh dentry and inode at remount time.
24704 + */
24705 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
24706 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
24707 +                     struct dentry *parent)
24708 +{
24709 +       int err;
24710 +
24711 +       di_write_lock_child(dentry);
24712 +       di_read_lock_parent(parent, AuLock_IR);
24713 +       err = au_refresh_dentry(dentry, parent);
24714 +       if (!err && dir_flags)
24715 +               au_hn_reset(dentry->d_inode, dir_flags);
24716 +       di_read_unlock(parent, AuLock_IR);
24717 +       di_write_unlock(dentry);
24718 +
24719 +       return err;
24720 +}
24721 +
24722 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
24723 +                          struct au_sbinfo *sbinfo,
24724 +                          const unsigned int dir_flags)
24725 +{
24726 +       int err;
24727 +       struct dentry *parent;
24728 +       struct inode *inode;
24729 +
24730 +       err = 0;
24731 +       parent = dget_parent(dentry);
24732 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
24733 +               inode = dentry->d_inode;
24734 +               if (inode) {
24735 +                       if (!S_ISDIR(inode->i_mode))
24736 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
24737 +                                                parent);
24738 +                       else {
24739 +                               err = au_do_refresh(dentry, dir_flags, parent);
24740 +                               if (unlikely(err))
24741 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
24742 +                       }
24743 +               } else
24744 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
24745 +               AuDbgDentry(dentry);
24746 +       }
24747 +       dput(parent);
24748 +
24749 +       AuTraceErr(err);
24750 +       return err;
24751 +}
24752 +
24753 +static int au_refresh_d(struct super_block *sb)
24754 +{
24755 +       int err, i, j, ndentry, e;
24756 +       unsigned int sigen;
24757 +       struct au_dcsub_pages dpages;
24758 +       struct au_dpage *dpage;
24759 +       struct dentry **dentries, *d;
24760 +       struct au_sbinfo *sbinfo;
24761 +       struct dentry *root = sb->s_root;
24762 +       const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
24763 +
24764 +       err = au_dpages_init(&dpages, GFP_NOFS);
24765 +       if (unlikely(err))
24766 +               goto out;
24767 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
24768 +       if (unlikely(err))
24769 +               goto out_dpages;
24770 +
24771 +       sigen = au_sigen(sb);
24772 +       sbinfo = au_sbi(sb);
24773 +       for (i = 0; i < dpages.ndpage; i++) {
24774 +               dpage = dpages.dpages + i;
24775 +               dentries = dpage->dentries;
24776 +               ndentry = dpage->ndentry;
24777 +               for (j = 0; j < ndentry; j++) {
24778 +                       d = dentries[j];
24779 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
24780 +                       if (unlikely(e && !err))
24781 +                               err = e;
24782 +                       /* go on even err */
24783 +               }
24784 +       }
24785 +
24786 +out_dpages:
24787 +       au_dpages_free(&dpages);
24788 +out:
24789 +       return err;
24790 +}
24791 +
24792 +static int au_refresh_i(struct super_block *sb)
24793 +{
24794 +       int err, e;
24795 +       unsigned int sigen;
24796 +       unsigned long long max, ull;
24797 +       struct inode *inode, **array;
24798 +
24799 +       array = au_iarray_alloc(sb, &max);
24800 +       err = PTR_ERR(array);
24801 +       if (IS_ERR(array))
24802 +               goto out;
24803 +
24804 +       err = 0;
24805 +       sigen = au_sigen(sb);
24806 +       for (ull = 0; ull < max; ull++) {
24807 +               inode = array[ull];
24808 +               if (au_iigen(inode, NULL) != sigen) {
24809 +                       ii_write_lock_child(inode);
24810 +                       e = au_refresh_hinode_self(inode);
24811 +                       ii_write_unlock(inode);
24812 +                       if (unlikely(e)) {
24813 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
24814 +                               if (!err)
24815 +                                       err = e;
24816 +                               /* go on even if err */
24817 +                       }
24818 +               }
24819 +       }
24820 +
24821 +       au_iarray_free(array, max);
24822 +
24823 +out:
24824 +       return err;
24825 +}
24826 +
24827 +static void au_remount_refresh(struct super_block *sb)
24828 +{
24829 +       int err, e;
24830 +       unsigned int udba;
24831 +       aufs_bindex_t bindex, bend;
24832 +       struct dentry *root;
24833 +       struct inode *inode;
24834 +       struct au_branch *br;
24835 +
24836 +       au_sigen_inc(sb);
24837 +       au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
24838 +
24839 +       root = sb->s_root;
24840 +       DiMustNoWaiters(root);
24841 +       inode = root->d_inode;
24842 +       IiMustNoWaiters(inode);
24843 +
24844 +       udba = au_opt_udba(sb);
24845 +       bend = au_sbend(sb);
24846 +       for (bindex = 0; bindex <= bend; bindex++) {
24847 +               br = au_sbr(sb, bindex);
24848 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
24849 +               if (unlikely(err))
24850 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
24851 +                               bindex, err);
24852 +               /* go on even if err */
24853 +       }
24854 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
24855 +
24856 +       di_write_unlock(root);
24857 +       err = au_refresh_d(sb);
24858 +       e = au_refresh_i(sb);
24859 +       if (unlikely(e && !err))
24860 +               err = e;
24861 +       /* aufs_write_lock() calls ..._child() */
24862 +       di_write_lock_child(root);
24863 +
24864 +       au_cpup_attr_all(inode, /*force*/1);
24865 +
24866 +       if (unlikely(err))
24867 +               AuIOErr("refresh failed, ignored, %d\n", err);
24868 +}
24869 +
24870 +/* stop extra interpretation of errno in mount(8), and strange error messages */
24871 +static int cvt_err(int err)
24872 +{
24873 +       AuTraceErr(err);
24874 +
24875 +       switch (err) {
24876 +       case -ENOENT:
24877 +       case -ENOTDIR:
24878 +       case -EEXIST:
24879 +       case -EIO:
24880 +               err = -EINVAL;
24881 +       }
24882 +       return err;
24883 +}
24884 +
24885 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
24886 +{
24887 +       int err, do_dx;
24888 +       unsigned int mntflags;
24889 +       struct au_opts opts;
24890 +       struct dentry *root;
24891 +       struct inode *inode;
24892 +       struct au_sbinfo *sbinfo;
24893 +
24894 +       err = 0;
24895 +       root = sb->s_root;
24896 +       if (!data || !*data) {
24897 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
24898 +               if (!err) {
24899 +                       di_write_lock_child(root);
24900 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
24901 +                       aufs_write_unlock(root);
24902 +               }
24903 +               goto out;
24904 +       }
24905 +
24906 +       err = -ENOMEM;
24907 +       memset(&opts, 0, sizeof(opts));
24908 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
24909 +       if (unlikely(!opts.opt))
24910 +               goto out;
24911 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
24912 +       opts.flags = AuOpts_REMOUNT;
24913 +       opts.sb_flags = *flags;
24914 +
24915 +       /* parse it before aufs lock */
24916 +       err = au_opts_parse(sb, data, &opts);
24917 +       if (unlikely(err))
24918 +               goto out_opts;
24919 +
24920 +       sbinfo = au_sbi(sb);
24921 +       inode = root->d_inode;
24922 +       mutex_lock(&inode->i_mutex);
24923 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
24924 +       if (unlikely(err))
24925 +               goto out_mtx;
24926 +       di_write_lock_child(root);
24927 +
24928 +       /* au_opts_remount() may return an error */
24929 +       err = au_opts_remount(sb, &opts);
24930 +       au_opts_free(&opts);
24931 +
24932 +       if (au_ftest_opts(opts.flags, REFRESH))
24933 +               au_remount_refresh(sb);
24934 +
24935 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
24936 +               mntflags = au_mntflags(sb);
24937 +               do_dx = !!au_opt_test(mntflags, DIO);
24938 +               au_dy_arefresh(do_dx);
24939 +       }
24940 +
24941 +       aufs_write_unlock(root);
24942 +
24943 +out_mtx:
24944 +       mutex_unlock(&inode->i_mutex);
24945 +out_opts:
24946 +       free_page((unsigned long)opts.opt);
24947 +out:
24948 +       err = cvt_err(err);
24949 +       AuTraceErr(err);
24950 +       return err;
24951 +}
24952 +
24953 +static const struct super_operations aufs_sop = {
24954 +       .alloc_inode    = aufs_alloc_inode,
24955 +       .destroy_inode  = aufs_destroy_inode,
24956 +       /* always deleting, no clearing */
24957 +       .drop_inode     = generic_delete_inode,
24958 +       .show_options   = aufs_show_options,
24959 +       .statfs         = aufs_statfs,
24960 +       .put_super      = aufs_put_super,
24961 +       .sync_fs        = aufs_sync_fs,
24962 +       .remount_fs     = aufs_remount_fs
24963 +};
24964 +
24965 +/* ---------------------------------------------------------------------- */
24966 +
24967 +static int alloc_root(struct super_block *sb)
24968 +{
24969 +       int err;
24970 +       struct inode *inode;
24971 +       struct dentry *root;
24972 +
24973 +       err = -ENOMEM;
24974 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
24975 +       err = PTR_ERR(inode);
24976 +       if (IS_ERR(inode))
24977 +               goto out;
24978 +
24979 +       inode->i_op = &aufs_dir_iop;
24980 +       inode->i_fop = &aufs_dir_fop;
24981 +       inode->i_mode = S_IFDIR;
24982 +       set_nlink(inode, 2);
24983 +       unlock_new_inode(inode);
24984 +
24985 +       root = d_make_root(inode);
24986 +       if (unlikely(!root))
24987 +               goto out;
24988 +       err = PTR_ERR(root);
24989 +       if (IS_ERR(root))
24990 +               goto out;
24991 +
24992 +       err = au_di_init(root);
24993 +       if (!err) {
24994 +               sb->s_root = root;
24995 +               return 0; /* success */
24996 +       }
24997 +       dput(root);
24998 +
24999 +out:
25000 +       return err;
25001 +}
25002 +
25003 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
25004 +                          int silent __maybe_unused)
25005 +{
25006 +       int err;
25007 +       struct au_opts opts;
25008 +       struct dentry *root;
25009 +       struct inode *inode;
25010 +       char *arg = raw_data;
25011 +
25012 +       if (unlikely(!arg || !*arg)) {
25013 +               err = -EINVAL;
25014 +               pr_err("no arg\n");
25015 +               goto out;
25016 +       }
25017 +
25018 +       err = -ENOMEM;
25019 +       memset(&opts, 0, sizeof(opts));
25020 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
25021 +       if (unlikely(!opts.opt))
25022 +               goto out;
25023 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
25024 +       opts.sb_flags = sb->s_flags;
25025 +
25026 +       err = au_si_alloc(sb);
25027 +       if (unlikely(err))
25028 +               goto out_opts;
25029 +
25030 +       /* all timestamps always follow the ones on the branch */
25031 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
25032 +       sb->s_op = &aufs_sop;
25033 +       sb->s_d_op = &aufs_dop;
25034 +       sb->s_magic = AUFS_SUPER_MAGIC;
25035 +       sb->s_maxbytes = 0;
25036 +       au_export_init(sb);
25037 +
25038 +       err = alloc_root(sb);
25039 +       if (unlikely(err)) {
25040 +               si_write_unlock(sb);
25041 +               goto out_info;
25042 +       }
25043 +       root = sb->s_root;
25044 +       inode = root->d_inode;
25045 +
25046 +       /*
25047 +        * actually we can parse options regardless aufs lock here.
25048 +        * but at remount time, parsing must be done before aufs lock.
25049 +        * so we follow the same rule.
25050 +        */
25051 +       ii_write_lock_parent(inode);
25052 +       aufs_write_unlock(root);
25053 +       err = au_opts_parse(sb, arg, &opts);
25054 +       if (unlikely(err))
25055 +               goto out_root;
25056 +
25057 +       /* lock vfs_inode first, then aufs. */
25058 +       mutex_lock(&inode->i_mutex);
25059 +       aufs_write_lock(root);
25060 +       err = au_opts_mount(sb, &opts);
25061 +       au_opts_free(&opts);
25062 +       aufs_write_unlock(root);
25063 +       mutex_unlock(&inode->i_mutex);
25064 +       if (!err)
25065 +               goto out_opts; /* success */
25066 +
25067 +out_root:
25068 +       dput(root);
25069 +       sb->s_root = NULL;
25070 +out_info:
25071 +       dbgaufs_si_fin(au_sbi(sb));
25072 +       kobject_put(&au_sbi(sb)->si_kobj);
25073 +       sb->s_fs_info = NULL;
25074 +out_opts:
25075 +       free_page((unsigned long)opts.opt);
25076 +out:
25077 +       AuTraceErr(err);
25078 +       err = cvt_err(err);
25079 +       AuTraceErr(err);
25080 +       return err;
25081 +}
25082 +
25083 +/* ---------------------------------------------------------------------- */
25084 +
25085 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
25086 +                                const char *dev_name __maybe_unused,
25087 +                                void *raw_data)
25088 +{
25089 +       struct dentry *root;
25090 +       struct super_block *sb;
25091 +
25092 +       /* all timestamps always follow the ones on the branch */
25093 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
25094 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
25095 +       if (IS_ERR(root))
25096 +               goto out;
25097 +
25098 +       sb = root->d_sb;
25099 +       si_write_lock(sb, !AuLock_FLUSH);
25100 +       sysaufs_brs_add(sb, 0);
25101 +       si_write_unlock(sb);
25102 +       au_sbilist_add(sb);
25103 +
25104 +out:
25105 +       return root;
25106 +}
25107 +
25108 +static void aufs_kill_sb(struct super_block *sb)
25109 +{
25110 +       struct au_sbinfo *sbinfo;
25111 +
25112 +       sbinfo = au_sbi(sb);
25113 +       if (sbinfo) {
25114 +               au_sbilist_del(sb);
25115 +               aufs_write_lock(sb->s_root);
25116 +               if (sbinfo->si_wbr_create_ops->fin)
25117 +                       sbinfo->si_wbr_create_ops->fin(sb);
25118 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
25119 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
25120 +                       au_remount_refresh(sb);
25121 +               }
25122 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
25123 +                       au_plink_put(sb, /*verbose*/1);
25124 +               au_xino_clr(sb);
25125 +               sbinfo->si_sb = NULL;
25126 +               aufs_write_unlock(sb->s_root);
25127 +               au_nwt_flush(&sbinfo->si_nowait);
25128 +       }
25129 +       generic_shutdown_super(sb);
25130 +}
25131 +
25132 +struct file_system_type aufs_fs_type = {
25133 +       .name           = AUFS_FSTYPE,
25134 +       /* a race between rename and others */
25135 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
25136 +       .mount          = aufs_mount,
25137 +       .kill_sb        = aufs_kill_sb,
25138 +       /* no need to __module_get() and module_put(). */
25139 +       .owner          = THIS_MODULE,
25140 +};
25141 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
25142 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
25143 +++ linux/fs/aufs/super.h       2013-08-23 23:59:39.638250372 +0200
25144 @@ -0,0 +1,563 @@
25145 +/*
25146 + * Copyright (C) 2005-2013 Junjiro R. Okajima
25147 + *
25148 + * This program, aufs is free software; you can redistribute it and/or modify
25149 + * it under the terms of the GNU General Public License as published by
25150 + * the Free Software Foundation; either version 2 of the License, or
25151 + * (at your option) any later version.
25152 + *
25153 + * This program is distributed in the hope that it will be useful,
25154 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25155 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25156 + * GNU General Public License for more details.
25157 + *
25158 + * You should have received a copy of the GNU General Public License
25159 + * along with this program; if not, write to the Free Software
25160 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25161 + */
25162 +
25163 +/*
25164 + * super_block operations
25165 + */
25166 +
25167 +#ifndef __AUFS_SUPER_H__
25168 +#define __AUFS_SUPER_H__
25169 +
25170 +#ifdef __KERNEL__
25171 +
25172 +#include <linux/fs.h>
25173 +#include "rwsem.h"
25174 +#include "spl.h"
25175 +#include "wkq.h"
25176 +
25177 +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
25178 +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
25179 +                              loff_t *);
25180 +
25181 +/* policies to select one among multiple writable branches */
25182 +struct au_wbr_copyup_operations {
25183 +       int (*copyup)(struct dentry *dentry);
25184 +};
25185 +
25186 +struct au_wbr_create_operations {
25187 +       int (*create)(struct dentry *dentry, int isdir);
25188 +       int (*init)(struct super_block *sb);
25189 +       int (*fin)(struct super_block *sb);
25190 +};
25191 +
25192 +struct au_wbr_mfs {
25193 +       struct mutex    mfs_lock; /* protect this structure */
25194 +       unsigned long   mfs_jiffy;
25195 +       unsigned long   mfs_expire;
25196 +       aufs_bindex_t   mfs_bindex;
25197 +
25198 +       unsigned long long      mfsrr_bytes;
25199 +       unsigned long long      mfsrr_watermark;
25200 +};
25201 +
25202 +struct pseudo_link {
25203 +       union {
25204 +               struct hlist_node hlist;
25205 +               struct rcu_head rcu;
25206 +       };
25207 +       struct inode *inode;
25208 +};
25209 +
25210 +#define AuPlink_NHASH 100
25211 +static inline int au_plink_hash(ino_t ino)
25212 +{
25213 +       return ino % AuPlink_NHASH;
25214 +}
25215 +
25216 +struct au_branch;
25217 +struct au_sbinfo {
25218 +       /* nowait tasks in the system-wide workqueue */
25219 +       struct au_nowait_tasks  si_nowait;
25220 +
25221 +       /*
25222 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
25223 +        * rwsem for au_sbinfo is necessary.
25224 +        */
25225 +       struct au_rwsem         si_rwsem;
25226 +
25227 +       /* prevent recursive locking in deleting inode */
25228 +       struct {
25229 +               unsigned long           *bitmap;
25230 +               spinlock_t              tree_lock;
25231 +               struct radix_tree_root  tree;
25232 +       } au_si_pid;
25233 +
25234 +       /*
25235 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
25236 +        * remount.
25237 +        */
25238 +       atomic_long_t           si_ninodes, si_nfiles;
25239 +
25240 +       /* branch management */
25241 +       unsigned int            si_generation;
25242 +
25243 +       /* see above flags */
25244 +       unsigned char           au_si_status;
25245 +
25246 +       aufs_bindex_t           si_bend;
25247 +
25248 +       /* dirty trick to keep br_id plus */
25249 +       unsigned int            si_last_br_id :
25250 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
25251 +       struct au_branch        **si_branch;
25252 +
25253 +       /* policy to select a writable branch */
25254 +       unsigned char           si_wbr_copyup;
25255 +       unsigned char           si_wbr_create;
25256 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
25257 +       struct au_wbr_create_operations *si_wbr_create_ops;
25258 +
25259 +       /* round robin */
25260 +       atomic_t                si_wbr_rr_next;
25261 +
25262 +       /* most free space */
25263 +       struct au_wbr_mfs       si_wbr_mfs;
25264 +
25265 +       /* mount flags */
25266 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
25267 +       unsigned int            si_mntflags;
25268 +
25269 +       /* external inode number (bitmap and translation table) */
25270 +       au_readf_t              si_xread;
25271 +       au_writef_t             si_xwrite;
25272 +       struct file             *si_xib;
25273 +       struct mutex            si_xib_mtx; /* protect xib members */
25274 +       unsigned long           *si_xib_buf;
25275 +       unsigned long           si_xib_last_pindex;
25276 +       int                     si_xib_next_bit;
25277 +       aufs_bindex_t           si_xino_brid;
25278 +       /* reserved for future use */
25279 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
25280 +
25281 +#ifdef CONFIG_AUFS_EXPORT
25282 +       /* i_generation */
25283 +       struct file             *si_xigen;
25284 +       atomic_t                si_xigen_next;
25285 +#endif
25286 +
25287 +       /* vdir parameters */
25288 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
25289 +       unsigned int            si_rdblk;       /* deblk size */
25290 +       unsigned int            si_rdhash;      /* hash size */
25291 +
25292 +       /*
25293 +        * If the number of whiteouts are larger than si_dirwh, leave all of
25294 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
25295 +        * future fsck.aufs or kernel thread will remove them later.
25296 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
25297 +        */
25298 +       unsigned int            si_dirwh;
25299 +
25300 +       /*
25301 +        * rename(2) a directory with all children.
25302 +        */
25303 +       /* reserved for future use */
25304 +       /* int                  si_rendir; */
25305 +
25306 +       /* pseudo_link list */
25307 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
25308 +       wait_queue_head_t       si_plink_wq;
25309 +       spinlock_t              si_plink_maint_lock;
25310 +       pid_t                   si_plink_maint_pid;
25311 +
25312 +       /* file list */
25313 +       struct au_sphlhead      si_files;
25314 +
25315 +       /*
25316 +        * sysfs and lifetime management.
25317 +        * this is not a small structure and it may be a waste of memory in case
25318 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
25319 +        * but using sysfs is majority.
25320 +        */
25321 +       struct kobject          si_kobj;
25322 +#ifdef CONFIG_DEBUG_FS
25323 +       struct dentry            *si_dbgaufs;
25324 +       struct dentry            *si_dbgaufs_plink;
25325 +       struct dentry            *si_dbgaufs_xib;
25326 +#ifdef CONFIG_AUFS_EXPORT
25327 +       struct dentry            *si_dbgaufs_xigen;
25328 +#endif
25329 +#endif
25330 +
25331 +#ifdef CONFIG_AUFS_SBILIST
25332 +       struct list_head        si_list;
25333 +#endif
25334 +
25335 +       /* dirty, necessary for unmounting, sysfs and sysrq */
25336 +       struct super_block      *si_sb;
25337 +};
25338 +
25339 +/* sbinfo status flags */
25340 +/*
25341 + * set true when refresh_dirs() failed at remount time.
25342 + * then try refreshing dirs at access time again.
25343 + * if it is false, refreshing dirs at access time is unnecesary
25344 + */
25345 +#define AuSi_FAILED_REFRESH_DIR        1
25346 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
25347 +                                          unsigned int flag)
25348 +{
25349 +       AuRwMustAnyLock(&sbi->si_rwsem);
25350 +       return sbi->au_si_status & flag;
25351 +}
25352 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
25353 +#define au_fset_si(sbinfo, name) do { \
25354 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
25355 +       (sbinfo)->au_si_status |= AuSi_##name; \
25356 +} while (0)
25357 +#define au_fclr_si(sbinfo, name) do { \
25358 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
25359 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
25360 +} while (0)
25361 +
25362 +/* ---------------------------------------------------------------------- */
25363 +
25364 +/* policy to select one among writable branches */
25365 +#define AuWbrCopyup(sbinfo, ...) \
25366 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
25367 +#define AuWbrCreate(sbinfo, ...) \
25368 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
25369 +
25370 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
25371 +#define AuLock_DW              1               /* write-lock dentry */
25372 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
25373 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
25374 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
25375 +#define AuLock_DIR             (1 << 4)        /* target is a dir */
25376 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
25377 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
25378 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
25379 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
25380 +#define au_fset_lock(flags, name) \
25381 +       do { (flags) |= AuLock_##name; } while (0)
25382 +#define au_fclr_lock(flags, name) \
25383 +       do { (flags) &= ~AuLock_##name; } while (0)
25384 +
25385 +/* ---------------------------------------------------------------------- */
25386 +
25387 +/* super.c */
25388 +extern struct file_system_type aufs_fs_type;
25389 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
25390 +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
25391 +                                          void *arg);
25392 +void au_array_free(void *array);
25393 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
25394 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
25395 +void au_iarray_free(struct inode **a, unsigned long long max);
25396 +
25397 +/* sbinfo.c */
25398 +void au_si_free(struct kobject *kobj);
25399 +int au_si_alloc(struct super_block *sb);
25400 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
25401 +
25402 +unsigned int au_sigen_inc(struct super_block *sb);
25403 +aufs_bindex_t au_new_br_id(struct super_block *sb);
25404 +
25405 +int si_read_lock(struct super_block *sb, int flags);
25406 +int si_write_lock(struct super_block *sb, int flags);
25407 +int aufs_read_lock(struct dentry *dentry, int flags);
25408 +void aufs_read_unlock(struct dentry *dentry, int flags);
25409 +void aufs_write_lock(struct dentry *dentry);
25410 +void aufs_write_unlock(struct dentry *dentry);
25411 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
25412 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
25413 +
25414 +int si_pid_test_slow(struct super_block *sb);
25415 +void si_pid_set_slow(struct super_block *sb);
25416 +void si_pid_clr_slow(struct super_block *sb);
25417 +
25418 +/* wbr_policy.c */
25419 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
25420 +extern struct au_wbr_create_operations au_wbr_create_ops[];
25421 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
25422 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
25423 +
25424 +/* mvdown.c */
25425 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
25426 +
25427 +/* ---------------------------------------------------------------------- */
25428 +
25429 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
25430 +{
25431 +       return sb->s_fs_info;
25432 +}
25433 +
25434 +/* ---------------------------------------------------------------------- */
25435 +
25436 +#ifdef CONFIG_AUFS_EXPORT
25437 +int au_test_nfsd(void);
25438 +void au_export_init(struct super_block *sb);
25439 +void au_xigen_inc(struct inode *inode);
25440 +int au_xigen_new(struct inode *inode);
25441 +int au_xigen_set(struct super_block *sb, struct file *base);
25442 +void au_xigen_clr(struct super_block *sb);
25443 +
25444 +static inline int au_busy_or_stale(void)
25445 +{
25446 +       if (!au_test_nfsd())
25447 +               return -EBUSY;
25448 +       return -ESTALE;
25449 +}
25450 +#else
25451 +AuStubInt0(au_test_nfsd, void)
25452 +AuStubVoid(au_export_init, struct super_block *sb)
25453 +AuStubVoid(au_xigen_inc, struct inode *inode)
25454 +AuStubInt0(au_xigen_new, struct inode *inode)
25455 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
25456 +AuStubVoid(au_xigen_clr, struct super_block *sb)
25457 +static inline int au_busy_or_stale(void)
25458 +{
25459 +       return -EBUSY;
25460 +}
25461 +#endif /* CONFIG_AUFS_EXPORT */
25462 +
25463 +/* ---------------------------------------------------------------------- */
25464 +
25465 +#ifdef CONFIG_AUFS_SBILIST
25466 +/* module.c */
25467 +extern struct au_splhead au_sbilist;
25468 +
25469 +static inline void au_sbilist_init(void)
25470 +{
25471 +       au_spl_init(&au_sbilist);
25472 +}
25473 +
25474 +static inline void au_sbilist_add(struct super_block *sb)
25475 +{
25476 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
25477 +}
25478 +
25479 +static inline void au_sbilist_del(struct super_block *sb)
25480 +{
25481 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
25482 +}
25483 +
25484 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
25485 +static inline void au_sbilist_lock(void)
25486 +{
25487 +       spin_lock(&au_sbilist.spin);
25488 +}
25489 +
25490 +static inline void au_sbilist_unlock(void)
25491 +{
25492 +       spin_unlock(&au_sbilist.spin);
25493 +}
25494 +#define AuGFP_SBILIST  GFP_ATOMIC
25495 +#else
25496 +AuStubVoid(au_sbilist_lock, void)
25497 +AuStubVoid(au_sbilist_unlock, void)
25498 +#define AuGFP_SBILIST  GFP_NOFS
25499 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
25500 +#else
25501 +AuStubVoid(au_sbilist_init, void)
25502 +AuStubVoid(au_sbilist_add, struct super_block*)
25503 +AuStubVoid(au_sbilist_del, struct super_block*)
25504 +AuStubVoid(au_sbilist_lock, void)
25505 +AuStubVoid(au_sbilist_unlock, void)
25506 +#define AuGFP_SBILIST  GFP_NOFS
25507 +#endif
25508 +
25509 +/* ---------------------------------------------------------------------- */
25510 +
25511 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
25512 +{
25513 +       /*
25514 +        * This function is a dynamic '__init' fucntion actually,
25515 +        * so the tiny check for si_rwsem is unnecessary.
25516 +        */
25517 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
25518 +#ifdef CONFIG_DEBUG_FS
25519 +       sbinfo->si_dbgaufs = NULL;
25520 +       sbinfo->si_dbgaufs_plink = NULL;
25521 +       sbinfo->si_dbgaufs_xib = NULL;
25522 +#ifdef CONFIG_AUFS_EXPORT
25523 +       sbinfo->si_dbgaufs_xigen = NULL;
25524 +#endif
25525 +#endif
25526 +}
25527 +
25528 +/* ---------------------------------------------------------------------- */
25529 +
25530 +static inline pid_t si_pid_bit(void)
25531 +{
25532 +       /* the origin of pid is 1, but the bitmap's is 0 */
25533 +       return current->pid - 1;
25534 +}
25535 +
25536 +static inline int si_pid_test(struct super_block *sb)
25537 +{
25538 +       pid_t bit = si_pid_bit();
25539 +       if (bit < PID_MAX_DEFAULT)
25540 +               return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
25541 +       else
25542 +               return si_pid_test_slow(sb);
25543 +}
25544 +
25545 +static inline void si_pid_set(struct super_block *sb)
25546 +{
25547 +       pid_t bit = si_pid_bit();
25548 +       if (bit < PID_MAX_DEFAULT) {
25549 +               AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
25550 +               set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
25551 +               /* smp_mb(); */
25552 +       } else
25553 +               si_pid_set_slow(sb);
25554 +}
25555 +
25556 +static inline void si_pid_clr(struct super_block *sb)
25557 +{
25558 +       pid_t bit = si_pid_bit();
25559 +       if (bit < PID_MAX_DEFAULT) {
25560 +               AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
25561 +               clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
25562 +               /* smp_mb(); */
25563 +       } else
25564 +               si_pid_clr_slow(sb);
25565 +}
25566 +
25567 +/* ---------------------------------------------------------------------- */
25568 +
25569 +/* lock superblock. mainly for entry point functions */
25570 +/*
25571 + * __si_read_lock, __si_write_lock,
25572 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
25573 + */
25574 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
25575 +
25576 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
25577 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
25578 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
25579 +
25580 +static inline void si_noflush_read_lock(struct super_block *sb)
25581 +{
25582 +       __si_read_lock(sb);
25583 +       si_pid_set(sb);
25584 +}
25585 +
25586 +static inline int si_noflush_read_trylock(struct super_block *sb)
25587 +{
25588 +       int locked = __si_read_trylock(sb);
25589 +       if (locked)
25590 +               si_pid_set(sb);
25591 +       return locked;
25592 +}
25593 +
25594 +static inline void si_noflush_write_lock(struct super_block *sb)
25595 +{
25596 +       __si_write_lock(sb);
25597 +       si_pid_set(sb);
25598 +}
25599 +
25600 +static inline int si_noflush_write_trylock(struct super_block *sb)
25601 +{
25602 +       int locked = __si_write_trylock(sb);
25603 +       if (locked)
25604 +               si_pid_set(sb);
25605 +       return locked;
25606 +}
25607 +
25608 +#if 0 /* unused */
25609 +static inline int si_read_trylock(struct super_block *sb, int flags)
25610 +{
25611 +       if (au_ftest_lock(flags, FLUSH))
25612 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
25613 +       return si_noflush_read_trylock(sb);
25614 +}
25615 +#endif
25616 +
25617 +static inline void si_read_unlock(struct super_block *sb)
25618 +{
25619 +       si_pid_clr(sb);
25620 +       __si_read_unlock(sb);
25621 +}
25622 +
25623 +#if 0 /* unused */
25624 +static inline int si_write_trylock(struct super_block *sb, int flags)
25625 +{
25626 +       if (au_ftest_lock(flags, FLUSH))
25627 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
25628 +       return si_noflush_write_trylock(sb);
25629 +}
25630 +#endif
25631 +
25632 +static inline void si_write_unlock(struct super_block *sb)
25633 +{
25634 +       si_pid_clr(sb);
25635 +       __si_write_unlock(sb);
25636 +}
25637 +
25638 +#if 0 /* unused */
25639 +static inline void si_downgrade_lock(struct super_block *sb)
25640 +{
25641 +       __si_downgrade_lock(sb);
25642 +}
25643 +#endif
25644 +
25645 +/* ---------------------------------------------------------------------- */
25646 +
25647 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
25648 +{
25649 +       SiMustAnyLock(sb);
25650 +       return au_sbi(sb)->si_bend;
25651 +}
25652 +
25653 +static inline unsigned int au_mntflags(struct super_block *sb)
25654 +{
25655 +       SiMustAnyLock(sb);
25656 +       return au_sbi(sb)->si_mntflags;
25657 +}
25658 +
25659 +static inline unsigned int au_sigen(struct super_block *sb)
25660 +{
25661 +       SiMustAnyLock(sb);
25662 +       return au_sbi(sb)->si_generation;
25663 +}
25664 +
25665 +static inline void au_ninodes_inc(struct super_block *sb)
25666 +{
25667 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
25668 +}
25669 +
25670 +static inline void au_ninodes_dec(struct super_block *sb)
25671 +{
25672 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
25673 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
25674 +}
25675 +
25676 +static inline void au_nfiles_inc(struct super_block *sb)
25677 +{
25678 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
25679 +}
25680 +
25681 +static inline void au_nfiles_dec(struct super_block *sb)
25682 +{
25683 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
25684 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
25685 +}
25686 +
25687 +static inline struct au_branch *au_sbr(struct super_block *sb,
25688 +                                      aufs_bindex_t bindex)
25689 +{
25690 +       SiMustAnyLock(sb);
25691 +       return au_sbi(sb)->si_branch[0 + bindex];
25692 +}
25693 +
25694 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
25695 +{
25696 +       SiMustWriteLock(sb);
25697 +       au_sbi(sb)->si_xino_brid = brid;
25698 +}
25699 +
25700 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
25701 +{
25702 +       SiMustAnyLock(sb);
25703 +       return au_sbi(sb)->si_xino_brid;
25704 +}
25705 +
25706 +#endif /* __KERNEL__ */
25707 +#endif /* __AUFS_SUPER_H__ */
25708 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
25709 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
25710 +++ linux/fs/aufs/sysaufs.c     2013-07-06 13:20:47.753531903 +0200
25711 @@ -0,0 +1,105 @@
25712 +/*
25713 + * Copyright (C) 2005-2013 Junjiro R. Okajima
25714 + *
25715 + * This program, aufs is free software; you can redistribute it and/or modify
25716 + * it under the terms of the GNU General Public License as published by
25717 + * the Free Software Foundation; either version 2 of the License, or
25718 + * (at your option) any later version.
25719 + *
25720 + * This program is distributed in the hope that it will be useful,
25721 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25722 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25723 + * GNU General Public License for more details.
25724 + *
25725 + * You should have received a copy of the GNU General Public License
25726 + * along with this program; if not, write to the Free Software
25727 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25728 + */
25729 +
25730 +/*
25731 + * sysfs interface and lifetime management
25732 + * they are necessary regardless sysfs is disabled.
25733 + */
25734 +
25735 +#include <linux/random.h>
25736 +#include "aufs.h"
25737 +
25738 +unsigned long sysaufs_si_mask;
25739 +struct kset *sysaufs_kset;
25740 +
25741 +#define AuSiAttr(_name) { \
25742 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
25743 +       .show   = sysaufs_si_##_name,                           \
25744 +}
25745 +
25746 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
25747 +struct attribute *sysaufs_si_attrs[] = {
25748 +       &sysaufs_si_attr_xi_path.attr,
25749 +       NULL,
25750 +};
25751 +
25752 +static const struct sysfs_ops au_sbi_ops = {
25753 +       .show   = sysaufs_si_show
25754 +};
25755 +
25756 +static struct kobj_type au_sbi_ktype = {
25757 +       .release        = au_si_free,
25758 +       .sysfs_ops      = &au_sbi_ops,
25759 +       .default_attrs  = sysaufs_si_attrs
25760 +};
25761 +
25762 +/* ---------------------------------------------------------------------- */
25763 +
25764 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
25765 +{
25766 +       int err;
25767 +
25768 +       sbinfo->si_kobj.kset = sysaufs_kset;
25769 +       /* cf. sysaufs_name() */
25770 +       err = kobject_init_and_add
25771 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
25772 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
25773 +
25774 +       dbgaufs_si_null(sbinfo);
25775 +       if (!err) {
25776 +               err = dbgaufs_si_init(sbinfo);
25777 +               if (unlikely(err))
25778 +                       kobject_put(&sbinfo->si_kobj);
25779 +       }
25780 +       return err;
25781 +}
25782 +
25783 +void sysaufs_fin(void)
25784 +{
25785 +       dbgaufs_fin();
25786 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
25787 +       kset_unregister(sysaufs_kset);
25788 +}
25789 +
25790 +int __init sysaufs_init(void)
25791 +{
25792 +       int err;
25793 +
25794 +       do {
25795 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
25796 +       } while (!sysaufs_si_mask);
25797 +
25798 +       err = -EINVAL;
25799 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
25800 +       if (unlikely(!sysaufs_kset))
25801 +               goto out;
25802 +       err = PTR_ERR(sysaufs_kset);
25803 +       if (IS_ERR(sysaufs_kset))
25804 +               goto out;
25805 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
25806 +       if (unlikely(err)) {
25807 +               kset_unregister(sysaufs_kset);
25808 +               goto out;
25809 +       }
25810 +
25811 +       err = dbgaufs_init();
25812 +       if (unlikely(err))
25813 +               sysaufs_fin();
25814 +out:
25815 +       return err;
25816 +}
25817 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
25818 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
25819 +++ linux/fs/aufs/sysaufs.h     2013-07-06 13:20:47.753531903 +0200
25820 @@ -0,0 +1,104 @@
25821 +/*
25822 + * Copyright (C) 2005-2013 Junjiro R. Okajima
25823 + *
25824 + * This program, aufs is free software; you can redistribute it and/or modify
25825 + * it under the terms of the GNU General Public License as published by
25826 + * the Free Software Foundation; either version 2 of the License, or
25827 + * (at your option) any later version.
25828 + *
25829 + * This program is distributed in the hope that it will be useful,
25830 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25831 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25832 + * GNU General Public License for more details.
25833 + *
25834 + * You should have received a copy of the GNU General Public License
25835 + * along with this program; if not, write to the Free Software
25836 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25837 + */
25838 +
25839 +/*
25840 + * sysfs interface and mount lifetime management
25841 + */
25842 +
25843 +#ifndef __SYSAUFS_H__
25844 +#define __SYSAUFS_H__
25845 +
25846 +#ifdef __KERNEL__
25847 +
25848 +#include <linux/sysfs.h>
25849 +#include "module.h"
25850 +
25851 +struct super_block;
25852 +struct au_sbinfo;
25853 +
25854 +struct sysaufs_si_attr {
25855 +       struct attribute attr;
25856 +       int (*show)(struct seq_file *seq, struct super_block *sb);
25857 +};
25858 +
25859 +/* ---------------------------------------------------------------------- */
25860 +
25861 +/* sysaufs.c */
25862 +extern unsigned long sysaufs_si_mask;
25863 +extern struct kset *sysaufs_kset;
25864 +extern struct attribute *sysaufs_si_attrs[];
25865 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
25866 +int __init sysaufs_init(void);
25867 +void sysaufs_fin(void);
25868 +
25869 +/* ---------------------------------------------------------------------- */
25870 +
25871 +/* some people doesn't like to show a pointer in kernel */
25872 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
25873 +{
25874 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
25875 +}
25876 +
25877 +#define SysaufsSiNamePrefix    "si_"
25878 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
25879 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
25880 +{
25881 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
25882 +                sysaufs_si_id(sbinfo));
25883 +}
25884 +
25885 +struct au_branch;
25886 +#ifdef CONFIG_SYSFS
25887 +/* sysfs.c */
25888 +extern struct attribute_group *sysaufs_attr_group;
25889 +
25890 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
25891 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
25892 +                        char *buf);
25893 +
25894 +void sysaufs_br_init(struct au_branch *br);
25895 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
25896 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
25897 +
25898 +#define sysaufs_brs_init()     do {} while (0)
25899 +
25900 +#else
25901 +#define sysaufs_attr_group     NULL
25902 +
25903 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
25904 +
25905 +static inline
25906 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
25907 +                        char *buf)
25908 +{
25909 +       return 0;
25910 +}
25911 +
25912 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
25913 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
25914 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
25915 +
25916 +static inline void sysaufs_brs_init(void)
25917 +{
25918 +       sysaufs_brs = 0;
25919 +}
25920 +
25921 +#endif /* CONFIG_SYSFS */
25922 +
25923 +#endif /* __KERNEL__ */
25924 +#endif /* __SYSAUFS_H__ */
25925 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
25926 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
25927 +++ linux/fs/aufs/sysfs.c       2013-07-06 13:20:47.753531903 +0200
25928 @@ -0,0 +1,257 @@
25929 +/*
25930 + * Copyright (C) 2005-2013 Junjiro R. Okajima
25931 + *
25932 + * This program, aufs is free software; you can redistribute it and/or modify
25933 + * it under the terms of the GNU General Public License as published by
25934 + * the Free Software Foundation; either version 2 of the License, or
25935 + * (at your option) any later version.
25936 + *
25937 + * This program is distributed in the hope that it will be useful,
25938 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25939 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25940 + * GNU General Public License for more details.
25941 + *
25942 + * You should have received a copy of the GNU General Public License
25943 + * along with this program; if not, write to the Free Software
25944 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25945 + */
25946 +
25947 +/*
25948 + * sysfs interface
25949 + */
25950 +
25951 +#include <linux/seq_file.h>
25952 +#include "aufs.h"
25953 +
25954 +#ifdef CONFIG_AUFS_FS_MODULE
25955 +/* this entry violates the "one line per file" policy of sysfs */
25956 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
25957 +                          char *buf)
25958 +{
25959 +       ssize_t err;
25960 +       static char *conf =
25961 +/* this file is generated at compiling */
25962 +#include "conf.str"
25963 +               ;
25964 +
25965 +       err = snprintf(buf, PAGE_SIZE, conf);
25966 +       if (unlikely(err >= PAGE_SIZE))
25967 +               err = -EFBIG;
25968 +       return err;
25969 +}
25970 +
25971 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
25972 +#endif
25973 +
25974 +static struct attribute *au_attr[] = {
25975 +#ifdef CONFIG_AUFS_FS_MODULE
25976 +       &au_config_attr.attr,
25977 +#endif
25978 +       NULL,   /* need to NULL terminate the list of attributes */
25979 +};
25980 +
25981 +static struct attribute_group sysaufs_attr_group_body = {
25982 +       .attrs = au_attr
25983 +};
25984 +
25985 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
25986 +
25987 +/* ---------------------------------------------------------------------- */
25988 +
25989 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
25990 +{
25991 +       int err;
25992 +
25993 +       SiMustAnyLock(sb);
25994 +
25995 +       err = 0;
25996 +       if (au_opt_test(au_mntflags(sb), XINO)) {
25997 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
25998 +               seq_putc(seq, '\n');
25999 +       }
26000 +       return err;
26001 +}
26002 +
26003 +/*
26004 + * the lifetime of branch is independent from the entry under sysfs.
26005 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
26006 + * unlinked.
26007 + */
26008 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
26009 +                        aufs_bindex_t bindex)
26010 +{
26011 +       int err;
26012 +       struct path path;
26013 +       struct dentry *root;
26014 +       struct au_branch *br;
26015 +       char *perm;
26016 +
26017 +       AuDbg("b%d\n", bindex);
26018 +
26019 +       err = 0;
26020 +       root = sb->s_root;
26021 +       di_read_lock_parent(root, !AuLock_IR);
26022 +       br = au_sbr(sb, bindex);
26023 +       path.mnt = au_br_mnt(br);
26024 +       path.dentry = au_h_dptr(root, bindex);
26025 +       au_seq_path(seq, &path);
26026 +       di_read_unlock(root, !AuLock_IR);
26027 +       perm = au_optstr_br_perm(br->br_perm);
26028 +       if (perm) {
26029 +               err = seq_printf(seq, "=%s\n", perm);
26030 +               kfree(perm);
26031 +               if (err == -1)
26032 +                       err = -E2BIG;
26033 +       } else
26034 +               err = -ENOMEM;
26035 +       return err;
26036 +}
26037 +
26038 +/* ---------------------------------------------------------------------- */
26039 +
26040 +static struct seq_file *au_seq(char *p, ssize_t len)
26041 +{
26042 +       struct seq_file *seq;
26043 +
26044 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
26045 +       if (seq) {
26046 +               /* mutex_init(&seq.lock); */
26047 +               seq->buf = p;
26048 +               seq->size = len;
26049 +               return seq; /* success */
26050 +       }
26051 +
26052 +       seq = ERR_PTR(-ENOMEM);
26053 +       return seq;
26054 +}
26055 +
26056 +#define SysaufsBr_PREFIX "br"
26057 +
26058 +/* todo: file size may exceed PAGE_SIZE */
26059 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
26060 +                       char *buf)
26061 +{
26062 +       ssize_t err;
26063 +       long l;
26064 +       aufs_bindex_t bend;
26065 +       struct au_sbinfo *sbinfo;
26066 +       struct super_block *sb;
26067 +       struct seq_file *seq;
26068 +       char *name;
26069 +       struct attribute **cattr;
26070 +
26071 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
26072 +       sb = sbinfo->si_sb;
26073 +
26074 +       /*
26075 +        * prevent a race condition between sysfs and aufs.
26076 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
26077 +        * prohibits maintaining the sysfs entries.
26078 +        * hew we acquire read lock after sysfs_get_active_two().
26079 +        * on the other hand, the remount process may maintain the sysfs/aufs
26080 +        * entries after acquiring write lock.
26081 +        * it can cause a deadlock.
26082 +        * simply we gave up processing read here.
26083 +        */
26084 +       err = -EBUSY;
26085 +       if (unlikely(!si_noflush_read_trylock(sb)))
26086 +               goto out;
26087 +
26088 +       seq = au_seq(buf, PAGE_SIZE);
26089 +       err = PTR_ERR(seq);
26090 +       if (IS_ERR(seq))
26091 +               goto out_unlock;
26092 +
26093 +       name = (void *)attr->name;
26094 +       cattr = sysaufs_si_attrs;
26095 +       while (*cattr) {
26096 +               if (!strcmp(name, (*cattr)->name)) {
26097 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
26098 +                               ->show(seq, sb);
26099 +                       goto out_seq;
26100 +               }
26101 +               cattr++;
26102 +       }
26103 +
26104 +       bend = au_sbend(sb);
26105 +       if (!strncmp(name, SysaufsBr_PREFIX, sizeof(SysaufsBr_PREFIX) - 1)) {
26106 +               name += sizeof(SysaufsBr_PREFIX) - 1;
26107 +               err = kstrtol(name, 10, &l);
26108 +               if (!err) {
26109 +                       if (l <= bend)
26110 +                               err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l);
26111 +                       else
26112 +                               err = -ENOENT;
26113 +               }
26114 +               goto out_seq;
26115 +       }
26116 +       BUG();
26117 +
26118 +out_seq:
26119 +       if (!err) {
26120 +               err = seq->count;
26121 +               /* sysfs limit */
26122 +               if (unlikely(err == PAGE_SIZE))
26123 +                       err = -EFBIG;
26124 +       }
26125 +       kfree(seq);
26126 +out_unlock:
26127 +       si_read_unlock(sb);
26128 +out:
26129 +       return err;
26130 +}
26131 +
26132 +/* ---------------------------------------------------------------------- */
26133 +
26134 +void sysaufs_br_init(struct au_branch *br)
26135 +{
26136 +       struct attribute *attr = &br->br_attr;
26137 +
26138 +       sysfs_attr_init(attr);
26139 +       attr->name = br->br_name;
26140 +       attr->mode = S_IRUGO;
26141 +}
26142 +
26143 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
26144 +{
26145 +       struct au_branch *br;
26146 +       struct kobject *kobj;
26147 +       aufs_bindex_t bend;
26148 +
26149 +       dbgaufs_brs_del(sb, bindex);
26150 +
26151 +       if (!sysaufs_brs)
26152 +               return;
26153 +
26154 +       kobj = &au_sbi(sb)->si_kobj;
26155 +       bend = au_sbend(sb);
26156 +       for (; bindex <= bend; bindex++) {
26157 +               br = au_sbr(sb, bindex);
26158 +               sysfs_remove_file(kobj, &br->br_attr);
26159 +       }
26160 +}
26161 +
26162 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
26163 +{
26164 +       int err;
26165 +       aufs_bindex_t bend;
26166 +       struct kobject *kobj;
26167 +       struct au_branch *br;
26168 +
26169 +       dbgaufs_brs_add(sb, bindex);
26170 +
26171 +       if (!sysaufs_brs)
26172 +               return;
26173 +
26174 +       kobj = &au_sbi(sb)->si_kobj;
26175 +       bend = au_sbend(sb);
26176 +       for (; bindex <= bend; bindex++) {
26177 +               br = au_sbr(sb, bindex);
26178 +               snprintf(br->br_name, sizeof(br->br_name), SysaufsBr_PREFIX
26179 +                        "%d", bindex);
26180 +               err = sysfs_create_file(kobj, &br->br_attr);
26181 +               if (unlikely(err))
26182 +                       pr_warn("failed %s under sysfs(%d)\n",
26183 +                               br->br_name, err);
26184 +       }
26185 +}
26186 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
26187 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
26188 +++ linux/fs/aufs/sysrq.c       2013-07-06 13:20:47.753531903 +0200
26189 @@ -0,0 +1,155 @@
26190 +/*
26191 + * Copyright (C) 2005-2013 Junjiro R. Okajima
26192 + *
26193 + * This program, aufs is free software; you can redistribute it and/or modify
26194 + * it under the terms of the GNU General Public License as published by
26195 + * the Free Software Foundation; either version 2 of the License, or
26196 + * (at your option) any later version.
26197 + *
26198 + * This program is distributed in the hope that it will be useful,
26199 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26200 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26201 + * GNU General Public License for more details.
26202 + *
26203 + * You should have received a copy of the GNU General Public License
26204 + * along with this program; if not, write to the Free Software
26205 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26206 + */
26207 +
26208 +/*
26209 + * magic sysrq hanlder
26210 + */
26211 +
26212 +/* #include <linux/sysrq.h> */
26213 +#include <linux/writeback.h>
26214 +#include "aufs.h"
26215 +
26216 +/* ---------------------------------------------------------------------- */
26217 +
26218 +static void sysrq_sb(struct super_block *sb)
26219 +{
26220 +       char *plevel;
26221 +       struct au_sbinfo *sbinfo;
26222 +       struct file *file;
26223 +       struct au_sphlhead *files;
26224 +       struct au_finfo *finfo;
26225 +
26226 +       plevel = au_plevel;
26227 +       au_plevel = KERN_WARNING;
26228 +
26229 +       /* since we define pr_fmt, call printk directly */
26230 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
26231 +
26232 +       sbinfo = au_sbi(sb);
26233 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
26234 +       pr("superblock\n");
26235 +       au_dpri_sb(sb);
26236 +
26237 +#if 0
26238 +       pr("root dentry\n");
26239 +       au_dpri_dentry(sb->s_root);
26240 +       pr("root inode\n");
26241 +       au_dpri_inode(sb->s_root->d_inode);
26242 +#endif
26243 +
26244 +#if 0
26245 +       do {
26246 +               int err, i, j, ndentry;
26247 +               struct au_dcsub_pages dpages;
26248 +               struct au_dpage *dpage;
26249 +
26250 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
26251 +               if (unlikely(err))
26252 +                       break;
26253 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
26254 +               if (!err)
26255 +                       for (i = 0; i < dpages.ndpage; i++) {
26256 +                               dpage = dpages.dpages + i;
26257 +                               ndentry = dpage->ndentry;
26258 +                               for (j = 0; j < ndentry; j++)
26259 +                                       au_dpri_dentry(dpage->dentries[j]);
26260 +                       }
26261 +               au_dpages_free(&dpages);
26262 +       } while (0);
26263 +#endif
26264 +
26265 +#if 1
26266 +       {
26267 +               struct inode *i;
26268 +               pr("isolated inode\n");
26269 +               spin_lock(&inode_sb_list_lock);
26270 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
26271 +                       spin_lock(&i->i_lock);
26272 +                       if (1 || hlist_empty(&i->i_dentry))
26273 +                               au_dpri_inode(i);
26274 +                       spin_unlock(&i->i_lock);
26275 +               }
26276 +               spin_unlock(&inode_sb_list_lock);
26277 +       }
26278 +#endif
26279 +       pr("files\n");
26280 +       files = &au_sbi(sb)->si_files;
26281 +       spin_lock(&files->spin);
26282 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
26283 +               umode_t mode;
26284 +               file = finfo->fi_file;
26285 +               mode = file_inode(file)->i_mode;
26286 +               if (!special_file(mode) || au_special_file(mode))
26287 +                       au_dpri_file(file);
26288 +       }
26289 +       spin_unlock(&files->spin);
26290 +       pr("done\n");
26291 +
26292 +#undef pr
26293 +       au_plevel = plevel;
26294 +}
26295 +
26296 +/* ---------------------------------------------------------------------- */
26297 +
26298 +/* module parameter */
26299 +static char *aufs_sysrq_key = "a";
26300 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
26301 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
26302 +
26303 +static void au_sysrq(int key __maybe_unused)
26304 +{
26305 +       struct au_sbinfo *sbinfo;
26306 +
26307 +       lockdep_off();
26308 +       au_sbilist_lock();
26309 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
26310 +               sysrq_sb(sbinfo->si_sb);
26311 +       au_sbilist_unlock();
26312 +       lockdep_on();
26313 +}
26314 +
26315 +static struct sysrq_key_op au_sysrq_op = {
26316 +       .handler        = au_sysrq,
26317 +       .help_msg       = "Aufs",
26318 +       .action_msg     = "Aufs",
26319 +       .enable_mask    = SYSRQ_ENABLE_DUMP
26320 +};
26321 +
26322 +/* ---------------------------------------------------------------------- */
26323 +
26324 +int __init au_sysrq_init(void)
26325 +{
26326 +       int err;
26327 +       char key;
26328 +
26329 +       err = -1;
26330 +       key = *aufs_sysrq_key;
26331 +       if ('a' <= key && key <= 'z')
26332 +               err = register_sysrq_key(key, &au_sysrq_op);
26333 +       if (unlikely(err))
26334 +               pr_err("err %d, sysrq=%c\n", err, key);
26335 +       return err;
26336 +}
26337 +
26338 +void au_sysrq_fin(void)
26339 +{
26340 +       int err;
26341 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
26342 +       if (unlikely(err))
26343 +               pr_err("err %d (ignored)\n", err);
26344 +}
26345 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
26346 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
26347 +++ linux/fs/aufs/vdir.c        2013-07-30 22:42:55.842946719 +0200
26348 @@ -0,0 +1,878 @@
26349 +/*
26350 + * Copyright (C) 2005-2013 Junjiro R. Okajima
26351 + *
26352 + * This program, aufs is free software; you can redistribute it and/or modify
26353 + * it under the terms of the GNU General Public License as published by
26354 + * the Free Software Foundation; either version 2 of the License, or
26355 + * (at your option) any later version.
26356 + *
26357 + * This program is distributed in the hope that it will be useful,
26358 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26359 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26360 + * GNU General Public License for more details.
26361 + *
26362 + * You should have received a copy of the GNU General Public License
26363 + * along with this program; if not, write to the Free Software
26364 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26365 + */
26366 +
26367 +/*
26368 + * virtual or vertical directory
26369 + */
26370 +
26371 +#include "aufs.h"
26372 +
26373 +static unsigned int calc_size(int nlen)
26374 +{
26375 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
26376 +}
26377 +
26378 +static int set_deblk_end(union au_vdir_deblk_p *p,
26379 +                        union au_vdir_deblk_p *deblk_end)
26380 +{
26381 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
26382 +               p->de->de_str.len = 0;
26383 +               /* smp_mb(); */
26384 +               return 0;
26385 +       }
26386 +       return -1; /* error */
26387 +}
26388 +
26389 +/* returns true or false */
26390 +static int is_deblk_end(union au_vdir_deblk_p *p,
26391 +                       union au_vdir_deblk_p *deblk_end)
26392 +{
26393 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
26394 +               return !p->de->de_str.len;
26395 +       return 1;
26396 +}
26397 +
26398 +static unsigned char *last_deblk(struct au_vdir *vdir)
26399 +{
26400 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
26401 +}
26402 +
26403 +/* ---------------------------------------------------------------------- */
26404 +
26405 +/* estimate the apropriate size for name hash table */
26406 +unsigned int au_rdhash_est(loff_t sz)
26407 +{
26408 +       unsigned int n;
26409 +
26410 +       n = UINT_MAX;
26411 +       sz >>= 10;
26412 +       if (sz < n)
26413 +               n = sz;
26414 +       if (sz < AUFS_RDHASH_DEF)
26415 +               n = AUFS_RDHASH_DEF;
26416 +       /* pr_info("n %u\n", n); */
26417 +       return n;
26418 +}
26419 +
26420 +/*
26421 + * the allocated memory has to be freed by
26422 + * au_nhash_wh_free() or au_nhash_de_free().
26423 + */
26424 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
26425 +{
26426 +       struct hlist_head *head;
26427 +       unsigned int u;
26428 +
26429 +       head = kmalloc(sizeof(*nhash->nh_head) * num_hash, gfp);
26430 +       if (head) {
26431 +               nhash->nh_num = num_hash;
26432 +               nhash->nh_head = head;
26433 +               for (u = 0; u < num_hash; u++)
26434 +                       INIT_HLIST_HEAD(head++);
26435 +               return 0; /* success */
26436 +       }
26437 +
26438 +       return -ENOMEM;
26439 +}
26440 +
26441 +static void nhash_count(struct hlist_head *head)
26442 +{
26443 +#if 0
26444 +       unsigned long n;
26445 +       struct hlist_node *pos;
26446 +
26447 +       n = 0;
26448 +       hlist_for_each(pos, head)
26449 +               n++;
26450 +       pr_info("%lu\n", n);
26451 +#endif
26452 +}
26453 +
26454 +static void au_nhash_wh_do_free(struct hlist_head *head)
26455 +{
26456 +       struct au_vdir_wh *pos;
26457 +       struct hlist_node *node;
26458 +
26459 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
26460 +               kfree(pos);
26461 +}
26462 +
26463 +static void au_nhash_de_do_free(struct hlist_head *head)
26464 +{
26465 +       struct au_vdir_dehstr *pos;
26466 +       struct hlist_node *node;
26467 +
26468 +       hlist_for_each_entry_safe(pos, node, head, hash)
26469 +               au_cache_free_vdir_dehstr(pos);
26470 +}
26471 +
26472 +static void au_nhash_do_free(struct au_nhash *nhash,
26473 +                            void (*free)(struct hlist_head *head))
26474 +{
26475 +       unsigned int n;
26476 +       struct hlist_head *head;
26477 +
26478 +       n = nhash->nh_num;
26479 +       if (!n)
26480 +               return;
26481 +
26482 +       head = nhash->nh_head;
26483 +       while (n-- > 0) {
26484 +               nhash_count(head);
26485 +               free(head++);
26486 +       }
26487 +       kfree(nhash->nh_head);
26488 +}
26489 +
26490 +void au_nhash_wh_free(struct au_nhash *whlist)
26491 +{
26492 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
26493 +}
26494 +
26495 +static void au_nhash_de_free(struct au_nhash *delist)
26496 +{
26497 +       au_nhash_do_free(delist, au_nhash_de_do_free);
26498 +}
26499 +
26500 +/* ---------------------------------------------------------------------- */
26501 +
26502 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
26503 +                           int limit)
26504 +{
26505 +       int num;
26506 +       unsigned int u, n;
26507 +       struct hlist_head *head;
26508 +       struct au_vdir_wh *pos;
26509 +
26510 +       num = 0;
26511 +       n = whlist->nh_num;
26512 +       head = whlist->nh_head;
26513 +       for (u = 0; u < n; u++, head++)
26514 +               hlist_for_each_entry(pos, head, wh_hash)
26515 +                       if (pos->wh_bindex == btgt && ++num > limit)
26516 +                               return 1;
26517 +       return 0;
26518 +}
26519 +
26520 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
26521 +                                      unsigned char *name,
26522 +                                      unsigned int len)
26523 +{
26524 +       unsigned int v;
26525 +       /* const unsigned int magic_bit = 12; */
26526 +
26527 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
26528 +
26529 +       v = 0;
26530 +       while (len--)
26531 +               v += *name++;
26532 +       /* v = hash_long(v, magic_bit); */
26533 +       v %= nhash->nh_num;
26534 +       return nhash->nh_head + v;
26535 +}
26536 +
26537 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
26538 +                             int nlen)
26539 +{
26540 +       return str->len == nlen && !memcmp(str->name, name, nlen);
26541 +}
26542 +
26543 +/* returns found or not */
26544 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
26545 +{
26546 +       struct hlist_head *head;
26547 +       struct au_vdir_wh *pos;
26548 +       struct au_vdir_destr *str;
26549 +
26550 +       head = au_name_hash(whlist, name, nlen);
26551 +       hlist_for_each_entry(pos, head, wh_hash) {
26552 +               str = &pos->wh_str;
26553 +               AuDbg("%.*s\n", str->len, str->name);
26554 +               if (au_nhash_test_name(str, name, nlen))
26555 +                       return 1;
26556 +       }
26557 +       return 0;
26558 +}
26559 +
26560 +/* returns found(true) or not */
26561 +static int test_known(struct au_nhash *delist, char *name, int nlen)
26562 +{
26563 +       struct hlist_head *head;
26564 +       struct au_vdir_dehstr *pos;
26565 +       struct au_vdir_destr *str;
26566 +
26567 +       head = au_name_hash(delist, name, nlen);
26568 +       hlist_for_each_entry(pos, head, hash) {
26569 +               str = pos->str;
26570 +               AuDbg("%.*s\n", str->len, str->name);
26571 +               if (au_nhash_test_name(str, name, nlen))
26572 +                       return 1;
26573 +       }
26574 +       return 0;
26575 +}
26576 +
26577 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
26578 +                           unsigned char d_type)
26579 +{
26580 +#ifdef CONFIG_AUFS_SHWH
26581 +       wh->wh_ino = ino;
26582 +       wh->wh_type = d_type;
26583 +#endif
26584 +}
26585 +
26586 +/* ---------------------------------------------------------------------- */
26587 +
26588 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
26589 +                      unsigned int d_type, aufs_bindex_t bindex,
26590 +                      unsigned char shwh)
26591 +{
26592 +       int err;
26593 +       struct au_vdir_destr *str;
26594 +       struct au_vdir_wh *wh;
26595 +
26596 +       AuDbg("%.*s\n", nlen, name);
26597 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
26598 +
26599 +       err = -ENOMEM;
26600 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
26601 +       if (unlikely(!wh))
26602 +               goto out;
26603 +
26604 +       err = 0;
26605 +       wh->wh_bindex = bindex;
26606 +       if (shwh)
26607 +               au_shwh_init_wh(wh, ino, d_type);
26608 +       str = &wh->wh_str;
26609 +       str->len = nlen;
26610 +       memcpy(str->name, name, nlen);
26611 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
26612 +       /* smp_mb(); */
26613 +
26614 +out:
26615 +       return err;
26616 +}
26617 +
26618 +static int append_deblk(struct au_vdir *vdir)
26619 +{
26620 +       int err;
26621 +       unsigned long ul;
26622 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
26623 +       union au_vdir_deblk_p p, deblk_end;
26624 +       unsigned char **o;
26625 +
26626 +       err = -ENOMEM;
26627 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
26628 +                    GFP_NOFS);
26629 +       if (unlikely(!o))
26630 +               goto out;
26631 +
26632 +       vdir->vd_deblk = o;
26633 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
26634 +       if (p.deblk) {
26635 +               ul = vdir->vd_nblk++;
26636 +               vdir->vd_deblk[ul] = p.deblk;
26637 +               vdir->vd_last.ul = ul;
26638 +               vdir->vd_last.p.deblk = p.deblk;
26639 +               deblk_end.deblk = p.deblk + deblk_sz;
26640 +               err = set_deblk_end(&p, &deblk_end);
26641 +       }
26642 +
26643 +out:
26644 +       return err;
26645 +}
26646 +
26647 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
26648 +                    unsigned int d_type, struct au_nhash *delist)
26649 +{
26650 +       int err;
26651 +       unsigned int sz;
26652 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
26653 +       union au_vdir_deblk_p p, *room, deblk_end;
26654 +       struct au_vdir_dehstr *dehstr;
26655 +
26656 +       p.deblk = last_deblk(vdir);
26657 +       deblk_end.deblk = p.deblk + deblk_sz;
26658 +       room = &vdir->vd_last.p;
26659 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
26660 +                 || !is_deblk_end(room, &deblk_end));
26661 +
26662 +       sz = calc_size(nlen);
26663 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
26664 +               err = append_deblk(vdir);
26665 +               if (unlikely(err))
26666 +                       goto out;
26667 +
26668 +               p.deblk = last_deblk(vdir);
26669 +               deblk_end.deblk = p.deblk + deblk_sz;
26670 +               /* smp_mb(); */
26671 +               AuDebugOn(room->deblk != p.deblk);
26672 +       }
26673 +
26674 +       err = -ENOMEM;
26675 +       dehstr = au_cache_alloc_vdir_dehstr();
26676 +       if (unlikely(!dehstr))
26677 +               goto out;
26678 +
26679 +       dehstr->str = &room->de->de_str;
26680 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
26681 +       room->de->de_ino = ino;
26682 +       room->de->de_type = d_type;
26683 +       room->de->de_str.len = nlen;
26684 +       memcpy(room->de->de_str.name, name, nlen);
26685 +
26686 +       err = 0;
26687 +       room->deblk += sz;
26688 +       if (unlikely(set_deblk_end(room, &deblk_end)))
26689 +               err = append_deblk(vdir);
26690 +       /* smp_mb(); */
26691 +
26692 +out:
26693 +       return err;
26694 +}
26695 +
26696 +/* ---------------------------------------------------------------------- */
26697 +
26698 +void au_vdir_free(struct au_vdir *vdir)
26699 +{
26700 +       unsigned char **deblk;
26701 +
26702 +       deblk = vdir->vd_deblk;
26703 +       while (vdir->vd_nblk--)
26704 +               kfree(*deblk++);
26705 +       kfree(vdir->vd_deblk);
26706 +       au_cache_free_vdir(vdir);
26707 +}
26708 +
26709 +static struct au_vdir *alloc_vdir(struct file *file)
26710 +{
26711 +       struct au_vdir *vdir;
26712 +       struct super_block *sb;
26713 +       int err;
26714 +
26715 +       sb = file->f_dentry->d_sb;
26716 +       SiMustAnyLock(sb);
26717 +
26718 +       err = -ENOMEM;
26719 +       vdir = au_cache_alloc_vdir();
26720 +       if (unlikely(!vdir))
26721 +               goto out;
26722 +
26723 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
26724 +       if (unlikely(!vdir->vd_deblk))
26725 +               goto out_free;
26726 +
26727 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
26728 +       if (!vdir->vd_deblk_sz) {
26729 +               /* estimate the apropriate size for deblk */
26730 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
26731 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
26732 +       }
26733 +       vdir->vd_nblk = 0;
26734 +       vdir->vd_version = 0;
26735 +       vdir->vd_jiffy = 0;
26736 +       err = append_deblk(vdir);
26737 +       if (!err)
26738 +               return vdir; /* success */
26739 +
26740 +       kfree(vdir->vd_deblk);
26741 +
26742 +out_free:
26743 +       au_cache_free_vdir(vdir);
26744 +out:
26745 +       vdir = ERR_PTR(err);
26746 +       return vdir;
26747 +}
26748 +
26749 +static int reinit_vdir(struct au_vdir *vdir)
26750 +{
26751 +       int err;
26752 +       union au_vdir_deblk_p p, deblk_end;
26753 +
26754 +       while (vdir->vd_nblk > 1) {
26755 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
26756 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
26757 +               vdir->vd_nblk--;
26758 +       }
26759 +       p.deblk = vdir->vd_deblk[0];
26760 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
26761 +       err = set_deblk_end(&p, &deblk_end);
26762 +       /* keep vd_dblk_sz */
26763 +       vdir->vd_last.ul = 0;
26764 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
26765 +       vdir->vd_version = 0;
26766 +       vdir->vd_jiffy = 0;
26767 +       /* smp_mb(); */
26768 +       return err;
26769 +}
26770 +
26771 +/* ---------------------------------------------------------------------- */
26772 +
26773 +#define AuFillVdir_CALLED      1
26774 +#define AuFillVdir_WHABLE      (1 << 1)
26775 +#define AuFillVdir_SHWH                (1 << 2)
26776 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
26777 +#define au_fset_fillvdir(flags, name) \
26778 +       do { (flags) |= AuFillVdir_##name; } while (0)
26779 +#define au_fclr_fillvdir(flags, name) \
26780 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
26781 +
26782 +#ifndef CONFIG_AUFS_SHWH
26783 +#undef AuFillVdir_SHWH
26784 +#define AuFillVdir_SHWH                0
26785 +#endif
26786 +
26787 +struct fillvdir_arg {
26788 +       struct file             *file;
26789 +       struct au_vdir          *vdir;
26790 +       struct au_nhash         delist;
26791 +       struct au_nhash         whlist;
26792 +       aufs_bindex_t           bindex;
26793 +       unsigned int            flags;
26794 +       int                     err;
26795 +};
26796 +
26797 +static int fillvdir(void *__arg, const char *__name, int nlen,
26798 +                   loff_t offset __maybe_unused, u64 h_ino,
26799 +                   unsigned int d_type)
26800 +{
26801 +       struct fillvdir_arg *arg = __arg;
26802 +       char *name = (void *)__name;
26803 +       struct super_block *sb;
26804 +       ino_t ino;
26805 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
26806 +
26807 +       arg->err = 0;
26808 +       sb = arg->file->f_dentry->d_sb;
26809 +       au_fset_fillvdir(arg->flags, CALLED);
26810 +       /* smp_mb(); */
26811 +       if (nlen <= AUFS_WH_PFX_LEN
26812 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
26813 +               if (test_known(&arg->delist, name, nlen)
26814 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
26815 +                       goto out; /* already exists or whiteouted */
26816 +
26817 +               sb = arg->file->f_dentry->d_sb;
26818 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
26819 +               if (!arg->err) {
26820 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
26821 +                               d_type = DT_UNKNOWN;
26822 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
26823 +                                            d_type, &arg->delist);
26824 +               }
26825 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
26826 +               name += AUFS_WH_PFX_LEN;
26827 +               nlen -= AUFS_WH_PFX_LEN;
26828 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
26829 +                       goto out; /* already whiteouted */
26830 +
26831 +               if (shwh)
26832 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
26833 +                                            &ino);
26834 +               if (!arg->err) {
26835 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
26836 +                               d_type = DT_UNKNOWN;
26837 +                       arg->err = au_nhash_append_wh
26838 +                               (&arg->whlist, name, nlen, ino, d_type,
26839 +                                arg->bindex, shwh);
26840 +               }
26841 +       }
26842 +
26843 +out:
26844 +       if (!arg->err)
26845 +               arg->vdir->vd_jiffy = jiffies;
26846 +       /* smp_mb(); */
26847 +       AuTraceErr(arg->err);
26848 +       return arg->err;
26849 +}
26850 +
26851 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
26852 +                         struct au_nhash *whlist, struct au_nhash *delist)
26853 +{
26854 +#ifdef CONFIG_AUFS_SHWH
26855 +       int err;
26856 +       unsigned int nh, u;
26857 +       struct hlist_head *head;
26858 +       struct au_vdir_wh *pos;
26859 +       struct hlist_node *n;
26860 +       char *p, *o;
26861 +       struct au_vdir_destr *destr;
26862 +
26863 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
26864 +
26865 +       err = -ENOMEM;
26866 +       o = p = (void *)__get_free_page(GFP_NOFS);
26867 +       if (unlikely(!p))
26868 +               goto out;
26869 +
26870 +       err = 0;
26871 +       nh = whlist->nh_num;
26872 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
26873 +       p += AUFS_WH_PFX_LEN;
26874 +       for (u = 0; u < nh; u++) {
26875 +               head = whlist->nh_head + u;
26876 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
26877 +                       destr = &pos->wh_str;
26878 +                       memcpy(p, destr->name, destr->len);
26879 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
26880 +                                       pos->wh_ino, pos->wh_type, delist);
26881 +                       if (unlikely(err))
26882 +                               break;
26883 +               }
26884 +       }
26885 +
26886 +       free_page((unsigned long)o);
26887 +
26888 +out:
26889 +       AuTraceErr(err);
26890 +       return err;
26891 +#else
26892 +       return 0;
26893 +#endif
26894 +}
26895 +
26896 +static int au_do_read_vdir(struct fillvdir_arg *arg)
26897 +{
26898 +       int err;
26899 +       unsigned int rdhash;
26900 +       loff_t offset;
26901 +       aufs_bindex_t bend, bindex, bstart;
26902 +       unsigned char shwh;
26903 +       struct file *hf, *file;
26904 +       struct super_block *sb;
26905 +
26906 +       file = arg->file;
26907 +       sb = file->f_dentry->d_sb;
26908 +       SiMustAnyLock(sb);
26909 +
26910 +       rdhash = au_sbi(sb)->si_rdhash;
26911 +       if (!rdhash)
26912 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
26913 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
26914 +       if (unlikely(err))
26915 +               goto out;
26916 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
26917 +       if (unlikely(err))
26918 +               goto out_delist;
26919 +
26920 +       err = 0;
26921 +       arg->flags = 0;
26922 +       shwh = 0;
26923 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
26924 +               shwh = 1;
26925 +               au_fset_fillvdir(arg->flags, SHWH);
26926 +       }
26927 +       bstart = au_fbstart(file);
26928 +       bend = au_fbend_dir(file);
26929 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
26930 +               hf = au_hf_dir(file, bindex);
26931 +               if (!hf)
26932 +                       continue;
26933 +
26934 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
26935 +               err = offset;
26936 +               if (unlikely(offset))
26937 +                       break;
26938 +
26939 +               arg->bindex = bindex;
26940 +               au_fclr_fillvdir(arg->flags, WHABLE);
26941 +               if (shwh
26942 +                   || (bindex != bend
26943 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
26944 +                       au_fset_fillvdir(arg->flags, WHABLE);
26945 +               do {
26946 +                       arg->err = 0;
26947 +                       au_fclr_fillvdir(arg->flags, CALLED);
26948 +                       /* smp_mb(); */
26949 +                       err = vfsub_readdir(hf, fillvdir, arg);
26950 +                       if (err >= 0)
26951 +                               err = arg->err;
26952 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
26953 +       }
26954 +
26955 +       if (!err && shwh)
26956 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
26957 +
26958 +       au_nhash_wh_free(&arg->whlist);
26959 +
26960 +out_delist:
26961 +       au_nhash_de_free(&arg->delist);
26962 +out:
26963 +       return err;
26964 +}
26965 +
26966 +static int read_vdir(struct file *file, int may_read)
26967 +{
26968 +       int err;
26969 +       unsigned long expire;
26970 +       unsigned char do_read;
26971 +       struct fillvdir_arg arg;
26972 +       struct inode *inode;
26973 +       struct au_vdir *vdir, *allocated;
26974 +
26975 +       err = 0;
26976 +       inode = file_inode(file);
26977 +       IMustLock(inode);
26978 +       SiMustAnyLock(inode->i_sb);
26979 +
26980 +       allocated = NULL;
26981 +       do_read = 0;
26982 +       expire = au_sbi(inode->i_sb)->si_rdcache;
26983 +       vdir = au_ivdir(inode);
26984 +       if (!vdir) {
26985 +               do_read = 1;
26986 +               vdir = alloc_vdir(file);
26987 +               err = PTR_ERR(vdir);
26988 +               if (IS_ERR(vdir))
26989 +                       goto out;
26990 +               err = 0;
26991 +               allocated = vdir;
26992 +       } else if (may_read
26993 +                  && (inode->i_version != vdir->vd_version
26994 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
26995 +               do_read = 1;
26996 +               err = reinit_vdir(vdir);
26997 +               if (unlikely(err))
26998 +                       goto out;
26999 +       }
27000 +
27001 +       if (!do_read)
27002 +               return 0; /* success */
27003 +
27004 +       arg.file = file;
27005 +       arg.vdir = vdir;
27006 +       err = au_do_read_vdir(&arg);
27007 +       if (!err) {
27008 +               /* file->f_pos = 0; */
27009 +               vdir->vd_version = inode->i_version;
27010 +               vdir->vd_last.ul = 0;
27011 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
27012 +               if (allocated)
27013 +                       au_set_ivdir(inode, allocated);
27014 +       } else if (allocated)
27015 +               au_vdir_free(allocated);
27016 +
27017 +out:
27018 +       return err;
27019 +}
27020 +
27021 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
27022 +{
27023 +       int err, rerr;
27024 +       unsigned long ul, n;
27025 +       const unsigned int deblk_sz = src->vd_deblk_sz;
27026 +
27027 +       AuDebugOn(tgt->vd_nblk != 1);
27028 +
27029 +       err = -ENOMEM;
27030 +       if (tgt->vd_nblk < src->vd_nblk) {
27031 +               unsigned char **p;
27032 +
27033 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
27034 +                            GFP_NOFS);
27035 +               if (unlikely(!p))
27036 +                       goto out;
27037 +               tgt->vd_deblk = p;
27038 +       }
27039 +
27040 +       if (tgt->vd_deblk_sz != deblk_sz) {
27041 +               unsigned char *p;
27042 +
27043 +               tgt->vd_deblk_sz = deblk_sz;
27044 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
27045 +               if (unlikely(!p))
27046 +                       goto out;
27047 +               tgt->vd_deblk[0] = p;
27048 +       }
27049 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
27050 +       tgt->vd_version = src->vd_version;
27051 +       tgt->vd_jiffy = src->vd_jiffy;
27052 +
27053 +       n = src->vd_nblk;
27054 +       for (ul = 1; ul < n; ul++) {
27055 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
27056 +                                           GFP_NOFS);
27057 +               if (unlikely(!tgt->vd_deblk[ul]))
27058 +                       goto out;
27059 +               tgt->vd_nblk++;
27060 +       }
27061 +       tgt->vd_nblk = n;
27062 +       tgt->vd_last.ul = tgt->vd_last.ul;
27063 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
27064 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
27065 +               - src->vd_deblk[src->vd_last.ul];
27066 +       /* smp_mb(); */
27067 +       return 0; /* success */
27068 +
27069 +out:
27070 +       rerr = reinit_vdir(tgt);
27071 +       BUG_ON(rerr);
27072 +       return err;
27073 +}
27074 +
27075 +int au_vdir_init(struct file *file)
27076 +{
27077 +       int err;
27078 +       struct inode *inode;
27079 +       struct au_vdir *vdir_cache, *allocated;
27080 +
27081 +       err = read_vdir(file, !file->f_pos);
27082 +       if (unlikely(err))
27083 +               goto out;
27084 +
27085 +       allocated = NULL;
27086 +       vdir_cache = au_fvdir_cache(file);
27087 +       if (!vdir_cache) {
27088 +               vdir_cache = alloc_vdir(file);
27089 +               err = PTR_ERR(vdir_cache);
27090 +               if (IS_ERR(vdir_cache))
27091 +                       goto out;
27092 +               allocated = vdir_cache;
27093 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
27094 +               err = reinit_vdir(vdir_cache);
27095 +               if (unlikely(err))
27096 +                       goto out;
27097 +       } else
27098 +               return 0; /* success */
27099 +
27100 +       inode = file_inode(file);
27101 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
27102 +       if (!err) {
27103 +               file->f_version = inode->i_version;
27104 +               if (allocated)
27105 +                       au_set_fvdir_cache(file, allocated);
27106 +       } else if (allocated)
27107 +               au_vdir_free(allocated);
27108 +
27109 +out:
27110 +       return err;
27111 +}
27112 +
27113 +static loff_t calc_offset(struct au_vdir *vdir)
27114 +{
27115 +       loff_t offset;
27116 +       union au_vdir_deblk_p p;
27117 +
27118 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
27119 +       offset = vdir->vd_last.p.deblk - p.deblk;
27120 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
27121 +       return offset;
27122 +}
27123 +
27124 +/* returns true or false */
27125 +static int seek_vdir(struct file *file)
27126 +{
27127 +       int valid;
27128 +       unsigned int deblk_sz;
27129 +       unsigned long ul, n;
27130 +       loff_t offset;
27131 +       union au_vdir_deblk_p p, deblk_end;
27132 +       struct au_vdir *vdir_cache;
27133 +
27134 +       valid = 1;
27135 +       vdir_cache = au_fvdir_cache(file);
27136 +       offset = calc_offset(vdir_cache);
27137 +       AuDbg("offset %lld\n", offset);
27138 +       if (file->f_pos == offset)
27139 +               goto out;
27140 +
27141 +       vdir_cache->vd_last.ul = 0;
27142 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
27143 +       if (!file->f_pos)
27144 +               goto out;
27145 +
27146 +       valid = 0;
27147 +       deblk_sz = vdir_cache->vd_deblk_sz;
27148 +       ul = div64_u64(file->f_pos, deblk_sz);
27149 +       AuDbg("ul %lu\n", ul);
27150 +       if (ul >= vdir_cache->vd_nblk)
27151 +               goto out;
27152 +
27153 +       n = vdir_cache->vd_nblk;
27154 +       for (; ul < n; ul++) {
27155 +               p.deblk = vdir_cache->vd_deblk[ul];
27156 +               deblk_end.deblk = p.deblk + deblk_sz;
27157 +               offset = ul;
27158 +               offset *= deblk_sz;
27159 +               while (!is_deblk_end(&p, &deblk_end) && offset < file->f_pos) {
27160 +                       unsigned int l;
27161 +
27162 +                       l = calc_size(p.de->de_str.len);
27163 +                       offset += l;
27164 +                       p.deblk += l;
27165 +               }
27166 +               if (!is_deblk_end(&p, &deblk_end)) {
27167 +                       valid = 1;
27168 +                       vdir_cache->vd_last.ul = ul;
27169 +                       vdir_cache->vd_last.p = p;
27170 +                       break;
27171 +               }
27172 +       }
27173 +
27174 +out:
27175 +       /* smp_mb(); */
27176 +       AuTraceErr(!valid);
27177 +       return valid;
27178 +}
27179 +
27180 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir)
27181 +{
27182 +       int err;
27183 +       unsigned int l, deblk_sz;
27184 +       union au_vdir_deblk_p deblk_end;
27185 +       struct au_vdir *vdir_cache;
27186 +       struct au_vdir_de *de;
27187 +
27188 +       vdir_cache = au_fvdir_cache(file);
27189 +       if (!seek_vdir(file))
27190 +               return 0;
27191 +
27192 +       deblk_sz = vdir_cache->vd_deblk_sz;
27193 +       while (1) {
27194 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
27195 +               deblk_end.deblk += deblk_sz;
27196 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
27197 +                       de = vdir_cache->vd_last.p.de;
27198 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
27199 +                             de->de_str.len, de->de_str.name, file->f_pos,
27200 +                             (unsigned long)de->de_ino, de->de_type);
27201 +                       err = filldir(dirent, de->de_str.name, de->de_str.len,
27202 +                                     file->f_pos, de->de_ino, de->de_type);
27203 +                       if (unlikely(err)) {
27204 +                               AuTraceErr(err);
27205 +                               /* todo: ignore the error caused by udba? */
27206 +                               /* return err; */
27207 +                               return 0;
27208 +                       }
27209 +
27210 +                       l = calc_size(de->de_str.len);
27211 +                       vdir_cache->vd_last.p.deblk += l;
27212 +                       file->f_pos += l;
27213 +               }
27214 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
27215 +                       vdir_cache->vd_last.ul++;
27216 +                       vdir_cache->vd_last.p.deblk
27217 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
27218 +                       file->f_pos = deblk_sz * vdir_cache->vd_last.ul;
27219 +                       continue;
27220 +               }
27221 +               break;
27222 +       }
27223 +
27224 +       /* smp_mb(); */
27225 +       return 0;
27226 +}
27227 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
27228 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
27229 +++ linux/fs/aufs/vfsub.c       2013-07-30 22:42:55.842946719 +0200
27230 @@ -0,0 +1,769 @@
27231 +/*
27232 + * Copyright (C) 2005-2013 Junjiro R. Okajima
27233 + *
27234 + * This program, aufs is free software; you can redistribute it and/or modify
27235 + * it under the terms of the GNU General Public License as published by
27236 + * the Free Software Foundation; either version 2 of the License, or
27237 + * (at your option) any later version.
27238 + *
27239 + * This program is distributed in the hope that it will be useful,
27240 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27241 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27242 + * GNU General Public License for more details.
27243 + *
27244 + * You should have received a copy of the GNU General Public License
27245 + * along with this program; if not, write to the Free Software
27246 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
27247 + */
27248 +
27249 +/*
27250 + * sub-routines for VFS
27251 + */
27252 +
27253 +#include <linux/ima.h>
27254 +#include <linux/namei.h>
27255 +#include <linux/security.h>
27256 +#include <linux/splice.h>
27257 +#include "aufs.h"
27258 +
27259 +int vfsub_update_h_iattr(struct path *h_path, int *did)
27260 +{
27261 +       int err;
27262 +       struct kstat st;
27263 +       struct super_block *h_sb;
27264 +
27265 +       /* for remote fs, leave work for its getattr or d_revalidate */
27266 +       /* for bad i_attr fs, handle them in aufs_getattr() */
27267 +       /* still some fs may acquire i_mutex. we need to skip them */
27268 +       err = 0;
27269 +       if (!did)
27270 +               did = &err;
27271 +       h_sb = h_path->dentry->d_sb;
27272 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
27273 +       if (*did)
27274 +               err = vfs_getattr(h_path, &st);
27275 +
27276 +       return err;
27277 +}
27278 +
27279 +/* ---------------------------------------------------------------------- */
27280 +
27281 +struct file *vfsub_dentry_open(struct path *path, int flags)
27282 +{
27283 +       struct file *file;
27284 +
27285 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
27286 +                          current_cred());
27287 +       if (!IS_ERR_OR_NULL(file)
27288 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
27289 +               i_readcount_inc(path->dentry->d_inode);
27290 +
27291 +       return file;
27292 +}
27293 +
27294 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
27295 +{
27296 +       struct file *file;
27297 +
27298 +       lockdep_off();
27299 +       file = filp_open(path,
27300 +                        oflags /* | __FMODE_NONOTIFY */,
27301 +                        mode);
27302 +       lockdep_on();
27303 +       if (IS_ERR(file))
27304 +               goto out;
27305 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
27306 +
27307 +out:
27308 +       return file;
27309 +}
27310 +
27311 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
27312 +{
27313 +       int err;
27314 +
27315 +       err = kern_path(name, flags, path);
27316 +       if (!err && path->dentry->d_inode)
27317 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
27318 +       return err;
27319 +}
27320 +
27321 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
27322 +                                   int len)
27323 +{
27324 +       struct path path = {
27325 +               .mnt = NULL
27326 +       };
27327 +
27328 +       /* VFS checks it too, but by WARN_ON_ONCE() */
27329 +       IMustLock(parent->d_inode);
27330 +
27331 +       path.dentry = lookup_one_len(name, parent, len);
27332 +       if (IS_ERR(path.dentry))
27333 +               goto out;
27334 +       if (path.dentry->d_inode)
27335 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
27336 +
27337 +out:
27338 +       AuTraceErrPtr(path.dentry);
27339 +       return path.dentry;
27340 +}
27341 +
27342 +void vfsub_call_lkup_one(void *args)
27343 +{
27344 +       struct vfsub_lkup_one_args *a = args;
27345 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
27346 +}
27347 +
27348 +/* ---------------------------------------------------------------------- */
27349 +
27350 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
27351 +                                struct dentry *d2, struct au_hinode *hdir2)
27352 +{
27353 +       struct dentry *d;
27354 +
27355 +       lockdep_off();
27356 +       d = lock_rename(d1, d2);
27357 +       lockdep_on();
27358 +       au_hn_suspend(hdir1);
27359 +       if (hdir1 != hdir2)
27360 +               au_hn_suspend(hdir2);
27361 +
27362 +       return d;
27363 +}
27364 +
27365 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
27366 +                        struct dentry *d2, struct au_hinode *hdir2)
27367 +{
27368 +       au_hn_resume(hdir1);
27369 +       if (hdir1 != hdir2)
27370 +               au_hn_resume(hdir2);
27371 +       lockdep_off();
27372 +       unlock_rename(d1, d2);
27373 +       lockdep_on();
27374 +}
27375 +
27376 +/* ---------------------------------------------------------------------- */
27377 +
27378 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
27379 +{
27380 +       int err;
27381 +       struct dentry *d;
27382 +
27383 +       IMustLock(dir);
27384 +
27385 +       d = path->dentry;
27386 +       path->dentry = d->d_parent;
27387 +       err = security_path_mknod(path, d, mode, 0);
27388 +       path->dentry = d;
27389 +       if (unlikely(err))
27390 +               goto out;
27391 +
27392 +       err = vfs_create(dir, path->dentry, mode, want_excl);
27393 +       if (!err) {
27394 +               struct path tmp = *path;
27395 +               int did;
27396 +
27397 +               vfsub_update_h_iattr(&tmp, &did);
27398 +               if (did) {
27399 +                       tmp.dentry = path->dentry->d_parent;
27400 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27401 +               }
27402 +               /*ignore*/
27403 +       }
27404 +
27405 +out:
27406 +       return err;
27407 +}
27408 +
27409 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
27410 +{
27411 +       int err;
27412 +       struct dentry *d;
27413 +
27414 +       IMustLock(dir);
27415 +
27416 +       d = path->dentry;
27417 +       path->dentry = d->d_parent;
27418 +       err = security_path_symlink(path, d, symname);
27419 +       path->dentry = d;
27420 +       if (unlikely(err))
27421 +               goto out;
27422 +
27423 +       err = vfs_symlink(dir, path->dentry, symname);
27424 +       if (!err) {
27425 +               struct path tmp = *path;
27426 +               int did;
27427 +
27428 +               vfsub_update_h_iattr(&tmp, &did);
27429 +               if (did) {
27430 +                       tmp.dentry = path->dentry->d_parent;
27431 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27432 +               }
27433 +               /*ignore*/
27434 +       }
27435 +
27436 +out:
27437 +       return err;
27438 +}
27439 +
27440 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
27441 +{
27442 +       int err;
27443 +       struct dentry *d;
27444 +
27445 +       IMustLock(dir);
27446 +
27447 +       d = path->dentry;
27448 +       path->dentry = d->d_parent;
27449 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
27450 +       path->dentry = d;
27451 +       if (unlikely(err))
27452 +               goto out;
27453 +
27454 +       err = vfs_mknod(dir, path->dentry, mode, dev);
27455 +       if (!err) {
27456 +               struct path tmp = *path;
27457 +               int did;
27458 +
27459 +               vfsub_update_h_iattr(&tmp, &did);
27460 +               if (did) {
27461 +                       tmp.dentry = path->dentry->d_parent;
27462 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27463 +               }
27464 +               /*ignore*/
27465 +       }
27466 +
27467 +out:
27468 +       return err;
27469 +}
27470 +
27471 +static int au_test_nlink(struct inode *inode)
27472 +{
27473 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
27474 +
27475 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
27476 +           || inode->i_nlink < link_max)
27477 +               return 0;
27478 +       return -EMLINK;
27479 +}
27480 +
27481 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path)
27482 +{
27483 +       int err;
27484 +       struct dentry *d;
27485 +
27486 +       IMustLock(dir);
27487 +
27488 +       err = au_test_nlink(src_dentry->d_inode);
27489 +       if (unlikely(err))
27490 +               return err;
27491 +
27492 +       /* we don't call may_linkat() */
27493 +       d = path->dentry;
27494 +       path->dentry = d->d_parent;
27495 +       err = security_path_link(src_dentry, path, d);
27496 +       path->dentry = d;
27497 +       if (unlikely(err))
27498 +               goto out;
27499 +
27500 +       lockdep_off();
27501 +       err = vfs_link(src_dentry, dir, path->dentry);
27502 +       lockdep_on();
27503 +       if (!err) {
27504 +               struct path tmp = *path;
27505 +               int did;
27506 +
27507 +               /* fuse has different memory inode for the same inumber */
27508 +               vfsub_update_h_iattr(&tmp, &did);
27509 +               if (did) {
27510 +                       tmp.dentry = path->dentry->d_parent;
27511 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27512 +                       tmp.dentry = src_dentry;
27513 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27514 +               }
27515 +               /*ignore*/
27516 +       }
27517 +
27518 +out:
27519 +       return err;
27520 +}
27521 +
27522 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
27523 +                struct inode *dir, struct path *path)
27524 +{
27525 +       int err;
27526 +       struct path tmp = {
27527 +               .mnt    = path->mnt
27528 +       };
27529 +       struct dentry *d;
27530 +
27531 +       IMustLock(dir);
27532 +       IMustLock(src_dir);
27533 +
27534 +       d = path->dentry;
27535 +       path->dentry = d->d_parent;
27536 +       tmp.dentry = src_dentry->d_parent;
27537 +       err = security_path_rename(&tmp, src_dentry, path, d);
27538 +       path->dentry = d;
27539 +       if (unlikely(err))
27540 +               goto out;
27541 +
27542 +       lockdep_off();
27543 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry);
27544 +       lockdep_on();
27545 +       if (!err) {
27546 +               int did;
27547 +
27548 +               tmp.dentry = d->d_parent;
27549 +               vfsub_update_h_iattr(&tmp, &did);
27550 +               if (did) {
27551 +                       tmp.dentry = src_dentry;
27552 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27553 +                       tmp.dentry = src_dentry->d_parent;
27554 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27555 +               }
27556 +               /*ignore*/
27557 +       }
27558 +
27559 +out:
27560 +       return err;
27561 +}
27562 +
27563 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
27564 +{
27565 +       int err;
27566 +       struct dentry *d;
27567 +
27568 +       IMustLock(dir);
27569 +
27570 +       d = path->dentry;
27571 +       path->dentry = d->d_parent;
27572 +       err = security_path_mkdir(path, d, mode);
27573 +       path->dentry = d;
27574 +       if (unlikely(err))
27575 +               goto out;
27576 +
27577 +       err = vfs_mkdir(dir, path->dentry, mode);
27578 +       if (!err) {
27579 +               struct path tmp = *path;
27580 +               int did;
27581 +
27582 +               vfsub_update_h_iattr(&tmp, &did);
27583 +               if (did) {
27584 +                       tmp.dentry = path->dentry->d_parent;
27585 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27586 +               }
27587 +               /*ignore*/
27588 +       }
27589 +
27590 +out:
27591 +       return err;
27592 +}
27593 +
27594 +int vfsub_rmdir(struct inode *dir, struct path *path)
27595 +{
27596 +       int err;
27597 +       struct dentry *d;
27598 +
27599 +       IMustLock(dir);
27600 +
27601 +       d = path->dentry;
27602 +       path->dentry = d->d_parent;
27603 +       err = security_path_rmdir(path, d);
27604 +       path->dentry = d;
27605 +       if (unlikely(err))
27606 +               goto out;
27607 +
27608 +       lockdep_off();
27609 +       err = vfs_rmdir(dir, path->dentry);
27610 +       lockdep_on();
27611 +       if (!err) {
27612 +               struct path tmp = {
27613 +                       .dentry = path->dentry->d_parent,
27614 +                       .mnt    = path->mnt
27615 +               };
27616 +
27617 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
27618 +       }
27619 +
27620 +out:
27621 +       return err;
27622 +}
27623 +
27624 +/* ---------------------------------------------------------------------- */
27625 +
27626 +/* todo: support mmap_sem? */
27627 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
27628 +                    loff_t *ppos)
27629 +{
27630 +       ssize_t err;
27631 +
27632 +       lockdep_off();
27633 +       err = vfs_read(file, ubuf, count, ppos);
27634 +       lockdep_on();
27635 +       if (err >= 0)
27636 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
27637 +       return err;
27638 +}
27639 +
27640 +/* todo: kernel_read()? */
27641 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
27642 +                    loff_t *ppos)
27643 +{
27644 +       ssize_t err;
27645 +       mm_segment_t oldfs;
27646 +       union {
27647 +               void *k;
27648 +               char __user *u;
27649 +       } buf;
27650 +
27651 +       buf.k = kbuf;
27652 +       oldfs = get_fs();
27653 +       set_fs(KERNEL_DS);
27654 +       err = vfsub_read_u(file, buf.u, count, ppos);
27655 +       set_fs(oldfs);
27656 +       return err;
27657 +}
27658 +
27659 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
27660 +                     loff_t *ppos)
27661 +{
27662 +       ssize_t err;
27663 +
27664 +       lockdep_off();
27665 +       err = vfs_write(file, ubuf, count, ppos);
27666 +       lockdep_on();
27667 +       if (err >= 0)
27668 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
27669 +       return err;
27670 +}
27671 +
27672 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
27673 +{
27674 +       ssize_t err;
27675 +       mm_segment_t oldfs;
27676 +       union {
27677 +               void *k;
27678 +               const char __user *u;
27679 +       } buf;
27680 +
27681 +       buf.k = kbuf;
27682 +       oldfs = get_fs();
27683 +       set_fs(KERNEL_DS);
27684 +       err = vfsub_write_u(file, buf.u, count, ppos);
27685 +       set_fs(oldfs);
27686 +       return err;
27687 +}
27688 +
27689 +int vfsub_flush(struct file *file, fl_owner_t id)
27690 +{
27691 +       int err;
27692 +
27693 +       err = 0;
27694 +       if (file->f_op && file->f_op->flush) {
27695 +               if (!au_test_nfs(file->f_dentry->d_sb))
27696 +                       err = file->f_op->flush(file, id);
27697 +               else {
27698 +                       lockdep_off();
27699 +                       err = file->f_op->flush(file, id);
27700 +                       lockdep_on();
27701 +               }
27702 +               if (!err)
27703 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
27704 +               /*ignore*/
27705 +       }
27706 +       return err;
27707 +}
27708 +
27709 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg)
27710 +{
27711 +       int err;
27712 +
27713 +       lockdep_off();
27714 +       err = vfs_readdir(file, filldir, arg);
27715 +       lockdep_on();
27716 +       if (err >= 0)
27717 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
27718 +       return err;
27719 +}
27720 +
27721 +long vfsub_splice_to(struct file *in, loff_t *ppos,
27722 +                    struct pipe_inode_info *pipe, size_t len,
27723 +                    unsigned int flags)
27724 +{
27725 +       long err;
27726 +
27727 +       lockdep_off();
27728 +       err = do_splice_to(in, ppos, pipe, len, flags);
27729 +       lockdep_on();
27730 +       file_accessed(in);
27731 +       if (err >= 0)
27732 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
27733 +       return err;
27734 +}
27735 +
27736 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
27737 +                      loff_t *ppos, size_t len, unsigned int flags)
27738 +{
27739 +       long err;
27740 +
27741 +       lockdep_off();
27742 +       err = do_splice_from(pipe, out, ppos, len, flags);
27743 +       lockdep_on();
27744 +       if (err >= 0)
27745 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
27746 +       return err;
27747 +}
27748 +
27749 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
27750 +{
27751 +       int err;
27752 +
27753 +       /* file can be NULL */
27754 +       lockdep_off();
27755 +       err = vfs_fsync(file, datasync);
27756 +       lockdep_on();
27757 +       if (!err) {
27758 +               if (!path) {
27759 +                       AuDebugOn(!file);
27760 +                       path = &file->f_path;
27761 +               }
27762 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
27763 +       }
27764 +       return err;
27765 +}
27766 +
27767 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
27768 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
27769 +               struct file *h_file)
27770 +{
27771 +       int err;
27772 +       struct inode *h_inode;
27773 +       struct super_block *h_sb;
27774 +
27775 +       if (!h_file) {
27776 +               err = vfsub_truncate(h_path, length);
27777 +               goto out;
27778 +       }
27779 +
27780 +       h_inode = h_path->dentry->d_inode;
27781 +       h_sb = h_inode->i_sb;
27782 +       lockdep_off();
27783 +       sb_start_write(h_sb);
27784 +       lockdep_on();
27785 +       err = locks_verify_truncate(h_inode, h_file, length);
27786 +       if (!err)
27787 +               err = security_path_truncate(h_path);
27788 +       if (!err) {
27789 +               lockdep_off();
27790 +               err = do_truncate(h_path->dentry, length, attr, h_file);
27791 +               lockdep_on();
27792 +       }
27793 +       lockdep_off();
27794 +       sb_end_write(h_sb);
27795 +       lockdep_on();
27796 +
27797 +out:
27798 +       return err;
27799 +}
27800 +
27801 +/* ---------------------------------------------------------------------- */
27802 +
27803 +struct au_vfsub_mkdir_args {
27804 +       int *errp;
27805 +       struct inode *dir;
27806 +       struct path *path;
27807 +       int mode;
27808 +};
27809 +
27810 +static void au_call_vfsub_mkdir(void *args)
27811 +{
27812 +       struct au_vfsub_mkdir_args *a = args;
27813 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
27814 +}
27815 +
27816 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
27817 +{
27818 +       int err, do_sio, wkq_err;
27819 +
27820 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
27821 +       if (!do_sio)
27822 +               err = vfsub_mkdir(dir, path, mode);
27823 +       else {
27824 +               struct au_vfsub_mkdir_args args = {
27825 +                       .errp   = &err,
27826 +                       .dir    = dir,
27827 +                       .path   = path,
27828 +                       .mode   = mode
27829 +               };
27830 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
27831 +               if (unlikely(wkq_err))
27832 +                       err = wkq_err;
27833 +       }
27834 +
27835 +       return err;
27836 +}
27837 +
27838 +struct au_vfsub_rmdir_args {
27839 +       int *errp;
27840 +       struct inode *dir;
27841 +       struct path *path;
27842 +};
27843 +
27844 +static void au_call_vfsub_rmdir(void *args)
27845 +{
27846 +       struct au_vfsub_rmdir_args *a = args;
27847 +       *a->errp = vfsub_rmdir(a->dir, a->path);
27848 +}
27849 +
27850 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
27851 +{
27852 +       int err, do_sio, wkq_err;
27853 +
27854 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
27855 +       if (!do_sio)
27856 +               err = vfsub_rmdir(dir, path);
27857 +       else {
27858 +               struct au_vfsub_rmdir_args args = {
27859 +                       .errp   = &err,
27860 +                       .dir    = dir,
27861 +                       .path   = path
27862 +               };
27863 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
27864 +               if (unlikely(wkq_err))
27865 +                       err = wkq_err;
27866 +       }
27867 +
27868 +       return err;
27869 +}
27870 +
27871 +/* ---------------------------------------------------------------------- */
27872 +
27873 +struct notify_change_args {
27874 +       int *errp;
27875 +       struct path *path;
27876 +       struct iattr *ia;
27877 +};
27878 +
27879 +static void call_notify_change(void *args)
27880 +{
27881 +       struct notify_change_args *a = args;
27882 +       struct inode *h_inode;
27883 +
27884 +       h_inode = a->path->dentry->d_inode;
27885 +       IMustLock(h_inode);
27886 +
27887 +       *a->errp = -EPERM;
27888 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
27889 +               *a->errp = notify_change(a->path->dentry, a->ia);
27890 +               if (!*a->errp)
27891 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
27892 +       }
27893 +       AuTraceErr(*a->errp);
27894 +}
27895 +
27896 +int vfsub_notify_change(struct path *path, struct iattr *ia)
27897 +{
27898 +       int err;
27899 +       struct notify_change_args args = {
27900 +               .errp   = &err,
27901 +               .path   = path,
27902 +               .ia     = ia
27903 +       };
27904 +
27905 +       call_notify_change(&args);
27906 +
27907 +       return err;
27908 +}
27909 +
27910 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia)
27911 +{
27912 +       int err, wkq_err;
27913 +       struct notify_change_args args = {
27914 +               .errp   = &err,
27915 +               .path   = path,
27916 +               .ia     = ia
27917 +       };
27918 +
27919 +       wkq_err = au_wkq_wait(call_notify_change, &args);
27920 +       if (unlikely(wkq_err))
27921 +               err = wkq_err;
27922 +
27923 +       return err;
27924 +}
27925 +
27926 +/* ---------------------------------------------------------------------- */
27927 +
27928 +struct unlink_args {
27929 +       int *errp;
27930 +       struct inode *dir;
27931 +       struct path *path;
27932 +};
27933 +
27934 +static void call_unlink(void *args)
27935 +{
27936 +       struct unlink_args *a = args;
27937 +       struct dentry *d = a->path->dentry;
27938 +       struct inode *h_inode;
27939 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
27940 +                                     && d->d_count == 1);
27941 +
27942 +       IMustLock(a->dir);
27943 +
27944 +       a->path->dentry = d->d_parent;
27945 +       *a->errp = security_path_unlink(a->path, d);
27946 +       a->path->dentry = d;
27947 +       if (unlikely(*a->errp))
27948 +               return;
27949 +
27950 +       if (!stop_sillyrename)
27951 +               dget(d);
27952 +       h_inode = d->d_inode;
27953 +       if (h_inode)
27954 +               ihold(h_inode);
27955 +
27956 +       lockdep_off();
27957 +       *a->errp = vfs_unlink(a->dir, d);
27958 +       lockdep_on();
27959 +       if (!*a->errp) {
27960 +               struct path tmp = {
27961 +                       .dentry = d->d_parent,
27962 +                       .mnt    = a->path->mnt
27963 +               };
27964 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
27965 +       }
27966 +
27967 +       if (!stop_sillyrename)
27968 +               dput(d);
27969 +       if (h_inode)
27970 +               iput(h_inode);
27971 +
27972 +       AuTraceErr(*a->errp);
27973 +}
27974 +
27975 +/*
27976 + * @dir: must be locked.
27977 + * @dentry: target dentry.
27978 + */
27979 +int vfsub_unlink(struct inode *dir, struct path *path, int force)
27980 +{
27981 +       int err;
27982 +       struct unlink_args args = {
27983 +               .errp   = &err,
27984 +               .dir    = dir,
27985 +               .path   = path
27986 +       };
27987 +
27988 +       if (!force)
27989 +               call_unlink(&args);
27990 +       else {
27991 +               int wkq_err;
27992 +
27993 +               wkq_err = au_wkq_wait(call_unlink, &args);
27994 +               if (unlikely(wkq_err))
27995 +                       err = wkq_err;
27996 +       }
27997 +
27998 +       return err;
27999 +}
28000 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
28001 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
28002 +++ linux/fs/aufs/vfsub.h       2013-07-30 22:42:55.842946719 +0200
28003 @@ -0,0 +1,259 @@
28004 +/*
28005 + * Copyright (C) 2005-2013 Junjiro R. Okajima
28006 + *
28007 + * This program, aufs is free software; you can redistribute it and/or modify
28008 + * it under the terms of the GNU General Public License as published by
28009 + * the Free Software Foundation; either version 2 of the License, or
28010 + * (at your option) any later version.
28011 + *
28012 + * This program is distributed in the hope that it will be useful,
28013 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28014 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28015 + * GNU General Public License for more details.
28016 + *
28017 + * You should have received a copy of the GNU General Public License
28018 + * along with this program; if not, write to the Free Software
28019 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28020 + */
28021 +
28022 +/*
28023 + * sub-routines for VFS
28024 + */
28025 +
28026 +#ifndef __AUFS_VFSUB_H__
28027 +#define __AUFS_VFSUB_H__
28028 +
28029 +#ifdef __KERNEL__
28030 +
28031 +#include <linux/fs.h>
28032 +#include <linux/mount.h>
28033 +#include "debug.h"
28034 +
28035 +/* copied from linux/fs/internal.h */
28036 +/* todo: BAD approach!! */
28037 +extern void __mnt_drop_write(struct vfsmount *);
28038 +extern spinlock_t inode_sb_list_lock;
28039 +
28040 +/* lock subclass for lower inode */
28041 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
28042 +/* reduce? gave up. */
28043 +enum {
28044 +       AuLsc_I_Begin = I_MUTEX_QUOTA, /* 4 */
28045 +       AuLsc_I_PARENT,         /* lower inode, parent first */
28046 +       AuLsc_I_PARENT2,        /* copyup dirs */
28047 +       AuLsc_I_PARENT3,        /* copyup wh */
28048 +       AuLsc_I_CHILD,
28049 +       AuLsc_I_CHILD2,
28050 +       AuLsc_I_End
28051 +};
28052 +
28053 +/* to debug easier, do not make them inlined functions */
28054 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
28055 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
28056 +
28057 +/* ---------------------------------------------------------------------- */
28058 +
28059 +static inline void vfsub_drop_nlink(struct inode *inode)
28060 +{
28061 +       AuDebugOn(!inode->i_nlink);
28062 +       drop_nlink(inode);
28063 +}
28064 +
28065 +static inline void vfsub_dead_dir(struct inode *inode)
28066 +{
28067 +       AuDebugOn(!S_ISDIR(inode->i_mode));
28068 +       inode->i_flags |= S_DEAD;
28069 +       clear_nlink(inode);
28070 +}
28071 +
28072 +/* ---------------------------------------------------------------------- */
28073 +
28074 +int vfsub_update_h_iattr(struct path *h_path, int *did);
28075 +struct file *vfsub_dentry_open(struct path *path, int flags);
28076 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
28077 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
28078 +
28079 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
28080 +                                   int len);
28081 +
28082 +struct vfsub_lkup_one_args {
28083 +       struct dentry **errp;
28084 +       struct qstr *name;
28085 +       struct dentry *parent;
28086 +};
28087 +
28088 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
28089 +                                           struct dentry *parent)
28090 +{
28091 +       return vfsub_lookup_one_len(name->name, parent, name->len);
28092 +}
28093 +
28094 +void vfsub_call_lkup_one(void *args);
28095 +
28096 +/* ---------------------------------------------------------------------- */
28097 +
28098 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
28099 +{
28100 +       int err;
28101 +       lockdep_off();
28102 +       err = mnt_want_write(mnt);
28103 +       lockdep_on();
28104 +       return err;
28105 +}
28106 +
28107 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
28108 +{
28109 +       lockdep_off();
28110 +       mnt_drop_write(mnt);
28111 +       lockdep_on();
28112 +}
28113 +
28114 +static inline void vfsub_mnt_drop_write_file(struct file *file)
28115 +{
28116 +       lockdep_off();
28117 +       mnt_drop_write_file(file);
28118 +       lockdep_on();
28119 +}
28120 +
28121 +/* ---------------------------------------------------------------------- */
28122 +
28123 +struct au_hinode;
28124 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
28125 +                                struct dentry *d2, struct au_hinode *hdir2);
28126 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
28127 +                        struct dentry *d2, struct au_hinode *hdir2);
28128 +
28129 +int vfsub_create(struct inode *dir, struct path *path, int mode,
28130 +                bool want_excl);
28131 +int vfsub_symlink(struct inode *dir, struct path *path,
28132 +                 const char *symname);
28133 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
28134 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
28135 +              struct path *path);
28136 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
28137 +                struct inode *hdir, struct path *path);
28138 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
28139 +int vfsub_rmdir(struct inode *dir, struct path *path);
28140 +
28141 +/* ---------------------------------------------------------------------- */
28142 +
28143 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
28144 +                    loff_t *ppos);
28145 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
28146 +                       loff_t *ppos);
28147 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
28148 +                     loff_t *ppos);
28149 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
28150 +                     loff_t *ppos);
28151 +int vfsub_flush(struct file *file, fl_owner_t id);
28152 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg);
28153 +
28154 +static inline loff_t vfsub_f_size_read(struct file *file)
28155 +{
28156 +       return i_size_read(file_inode(file));
28157 +}
28158 +
28159 +static inline unsigned int vfsub_file_flags(struct file *file)
28160 +{
28161 +       unsigned int flags;
28162 +
28163 +       spin_lock(&file->f_lock);
28164 +       flags = file->f_flags;
28165 +       spin_unlock(&file->f_lock);
28166 +
28167 +       return flags;
28168 +}
28169 +
28170 +static inline void vfsub_file_accessed(struct file *h_file)
28171 +{
28172 +       file_accessed(h_file);
28173 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
28174 +}
28175 +
28176 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
28177 +                                    struct dentry *h_dentry)
28178 +{
28179 +       struct path h_path = {
28180 +               .dentry = h_dentry,
28181 +               .mnt    = h_mnt
28182 +       };
28183 +       touch_atime(&h_path);
28184 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
28185 +}
28186 +
28187 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
28188 +                                   int flags)
28189 +{
28190 +       return update_time(h_inode, ts, flags);
28191 +       /* no vfsub_update_h_iattr() since we don't have struct path */
28192 +}
28193 +
28194 +long vfsub_splice_to(struct file *in, loff_t *ppos,
28195 +                    struct pipe_inode_info *pipe, size_t len,
28196 +                    unsigned int flags);
28197 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
28198 +                      loff_t *ppos, size_t len, unsigned int flags);
28199 +
28200 +static inline long vfsub_truncate(struct path *path, loff_t length)
28201 +{
28202 +       long err;
28203 +       lockdep_off();
28204 +       err = vfs_truncate(path, length);
28205 +       lockdep_on();
28206 +       return err;
28207 +}
28208 +
28209 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
28210 +               struct file *h_file);
28211 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
28212 +
28213 +/* ---------------------------------------------------------------------- */
28214 +
28215 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
28216 +{
28217 +       loff_t err;
28218 +
28219 +       lockdep_off();
28220 +       err = vfs_llseek(file, offset, origin);
28221 +       lockdep_on();
28222 +       return err;
28223 +}
28224 +
28225 +/* ---------------------------------------------------------------------- */
28226 +
28227 +/* dirty workaround for strict type of fmode_t */
28228 +union vfsub_fmu {
28229 +       fmode_t fm;
28230 +       unsigned int ui;
28231 +};
28232 +
28233 +static inline unsigned int vfsub_fmode_to_uint(fmode_t fm)
28234 +{
28235 +       union vfsub_fmu u = {
28236 +               .fm = fm
28237 +       };
28238 +
28239 +       BUILD_BUG_ON(sizeof(u.fm) != sizeof(u.ui));
28240 +
28241 +       return u.ui;
28242 +}
28243 +
28244 +static inline fmode_t vfsub_uint_to_fmode(unsigned int ui)
28245 +{
28246 +       union vfsub_fmu u = {
28247 +               .ui = ui
28248 +       };
28249 +
28250 +       return u.fm;
28251 +}
28252 +
28253 +/* ---------------------------------------------------------------------- */
28254 +
28255 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
28256 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
28257 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia);
28258 +int vfsub_notify_change(struct path *path, struct iattr *ia);
28259 +int vfsub_unlink(struct inode *dir, struct path *path, int force);
28260 +
28261 +#endif /* __KERNEL__ */
28262 +#endif /* __AUFS_VFSUB_H__ */
28263 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
28264 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
28265 +++ linux/fs/aufs/wbr_policy.c  2013-08-23 23:59:39.638250372 +0200
28266 @@ -0,0 +1,693 @@
28267 +/*
28268 + * Copyright (C) 2005-2013 Junjiro R. Okajima
28269 + *
28270 + * This program, aufs is free software; you can redistribute it and/or modify
28271 + * it under the terms of the GNU General Public License as published by
28272 + * the Free Software Foundation; either version 2 of the License, or
28273 + * (at your option) any later version.
28274 + *
28275 + * This program is distributed in the hope that it will be useful,
28276 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28277 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28278 + * GNU General Public License for more details.
28279 + *
28280 + * You should have received a copy of the GNU General Public License
28281 + * along with this program; if not, write to the Free Software
28282 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28283 + */
28284 +
28285 +/*
28286 + * policies for selecting one among multiple writable branches
28287 + */
28288 +
28289 +#include <linux/statfs.h>
28290 +#include "aufs.h"
28291 +
28292 +/* subset of cpup_attr() */
28293 +static noinline_for_stack
28294 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
28295 +{
28296 +       int err, sbits;
28297 +       struct iattr ia;
28298 +       struct inode *h_isrc;
28299 +
28300 +       h_isrc = h_src->d_inode;
28301 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
28302 +       ia.ia_mode = h_isrc->i_mode;
28303 +       ia.ia_uid = h_isrc->i_uid;
28304 +       ia.ia_gid = h_isrc->i_gid;
28305 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
28306 +       au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc->i_flags);
28307 +       err = vfsub_sio_notify_change(h_path, &ia);
28308 +
28309 +       /* is this nfs only? */
28310 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
28311 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
28312 +               ia.ia_mode = h_isrc->i_mode;
28313 +               err = vfsub_sio_notify_change(h_path, &ia);
28314 +       }
28315 +
28316 +       return err;
28317 +}
28318 +
28319 +#define AuCpdown_PARENT_OPQ    1
28320 +#define AuCpdown_WHED          (1 << 1)
28321 +#define AuCpdown_MADE_DIR      (1 << 2)
28322 +#define AuCpdown_DIROPQ                (1 << 3)
28323 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
28324 +#define au_fset_cpdown(flags, name) \
28325 +       do { (flags) |= AuCpdown_##name; } while (0)
28326 +#define au_fclr_cpdown(flags, name) \
28327 +       do { (flags) &= ~AuCpdown_##name; } while (0)
28328 +
28329 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
28330 +                            unsigned int *flags)
28331 +{
28332 +       int err;
28333 +       struct dentry *opq_dentry;
28334 +
28335 +       opq_dentry = au_diropq_create(dentry, bdst);
28336 +       err = PTR_ERR(opq_dentry);
28337 +       if (IS_ERR(opq_dentry))
28338 +               goto out;
28339 +       dput(opq_dentry);
28340 +       au_fset_cpdown(*flags, DIROPQ);
28341 +
28342 +out:
28343 +       return err;
28344 +}
28345 +
28346 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
28347 +                           struct inode *dir, aufs_bindex_t bdst)
28348 +{
28349 +       int err;
28350 +       struct path h_path;
28351 +       struct au_branch *br;
28352 +
28353 +       br = au_sbr(dentry->d_sb, bdst);
28354 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
28355 +       err = PTR_ERR(h_path.dentry);
28356 +       if (IS_ERR(h_path.dentry))
28357 +               goto out;
28358 +
28359 +       err = 0;
28360 +       if (h_path.dentry->d_inode) {
28361 +               h_path.mnt = au_br_mnt(br);
28362 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
28363 +                                         dentry);
28364 +       }
28365 +       dput(h_path.dentry);
28366 +
28367 +out:
28368 +       return err;
28369 +}
28370 +
28371 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
28372 +                        struct au_pin *pin,
28373 +                        struct dentry *h_parent, void *arg)
28374 +{
28375 +       int err, rerr;
28376 +       aufs_bindex_t bopq, bstart;
28377 +       struct path h_path;
28378 +       struct dentry *parent;
28379 +       struct inode *h_dir, *h_inode, *inode, *dir;
28380 +       unsigned int *flags = arg;
28381 +
28382 +       bstart = au_dbstart(dentry);
28383 +       /* dentry is di-locked */
28384 +       parent = dget_parent(dentry);
28385 +       dir = parent->d_inode;
28386 +       h_dir = h_parent->d_inode;
28387 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
28388 +       IMustLock(h_dir);
28389 +
28390 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
28391 +       if (unlikely(err < 0))
28392 +               goto out;
28393 +       h_path.dentry = au_h_dptr(dentry, bdst);
28394 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
28395 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
28396 +                             S_IRWXU | S_IRUGO | S_IXUGO);
28397 +       if (unlikely(err))
28398 +               goto out_put;
28399 +       au_fset_cpdown(*flags, MADE_DIR);
28400 +
28401 +       bopq = au_dbdiropq(dentry);
28402 +       au_fclr_cpdown(*flags, WHED);
28403 +       au_fclr_cpdown(*flags, DIROPQ);
28404 +       if (au_dbwh(dentry) == bdst)
28405 +               au_fset_cpdown(*flags, WHED);
28406 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
28407 +               au_fset_cpdown(*flags, PARENT_OPQ);
28408 +       h_inode = h_path.dentry->d_inode;
28409 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
28410 +       if (au_ftest_cpdown(*flags, WHED)) {
28411 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
28412 +               if (unlikely(err)) {
28413 +                       mutex_unlock(&h_inode->i_mutex);
28414 +                       goto out_dir;
28415 +               }
28416 +       }
28417 +
28418 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
28419 +       mutex_unlock(&h_inode->i_mutex);
28420 +       if (unlikely(err))
28421 +               goto out_opq;
28422 +
28423 +       if (au_ftest_cpdown(*flags, WHED)) {
28424 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
28425 +               if (unlikely(err))
28426 +                       goto out_opq;
28427 +       }
28428 +
28429 +       inode = dentry->d_inode;
28430 +       if (au_ibend(inode) < bdst)
28431 +               au_set_ibend(inode, bdst);
28432 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
28433 +                     au_hi_flags(inode, /*isdir*/1));
28434 +       goto out; /* success */
28435 +
28436 +       /* revert */
28437 +out_opq:
28438 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
28439 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
28440 +               rerr = au_diropq_remove(dentry, bdst);
28441 +               mutex_unlock(&h_inode->i_mutex);
28442 +               if (unlikely(rerr)) {
28443 +                       AuIOErr("failed removing diropq for %.*s b%d (%d)\n",
28444 +                               AuDLNPair(dentry), bdst, rerr);
28445 +                       err = -EIO;
28446 +                       goto out;
28447 +               }
28448 +       }
28449 +out_dir:
28450 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
28451 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
28452 +               if (unlikely(rerr)) {
28453 +                       AuIOErr("failed removing %.*s b%d (%d)\n",
28454 +                               AuDLNPair(dentry), bdst, rerr);
28455 +                       err = -EIO;
28456 +               }
28457 +       }
28458 +out_put:
28459 +       au_set_h_dptr(dentry, bdst, NULL);
28460 +       if (au_dbend(dentry) == bdst)
28461 +               au_update_dbend(dentry);
28462 +out:
28463 +       dput(parent);
28464 +       return err;
28465 +}
28466 +
28467 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
28468 +{
28469 +       int err;
28470 +       unsigned int flags;
28471 +
28472 +       flags = 0;
28473 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
28474 +
28475 +       return err;
28476 +}
28477 +
28478 +/* ---------------------------------------------------------------------- */
28479 +
28480 +/* policies for create */
28481 +
28482 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
28483 +{
28484 +       int err, i, j, ndentry;
28485 +       aufs_bindex_t bopq;
28486 +       struct au_dcsub_pages dpages;
28487 +       struct au_dpage *dpage;
28488 +       struct dentry **dentries, *parent, *d;
28489 +
28490 +       err = au_dpages_init(&dpages, GFP_NOFS);
28491 +       if (unlikely(err))
28492 +               goto out;
28493 +       parent = dget_parent(dentry);
28494 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
28495 +       if (unlikely(err))
28496 +               goto out_free;
28497 +
28498 +       err = bindex;
28499 +       for (i = 0; i < dpages.ndpage; i++) {
28500 +               dpage = dpages.dpages + i;
28501 +               dentries = dpage->dentries;
28502 +               ndentry = dpage->ndentry;
28503 +               for (j = 0; j < ndentry; j++) {
28504 +                       d = dentries[j];
28505 +                       di_read_lock_parent2(d, !AuLock_IR);
28506 +                       bopq = au_dbdiropq(d);
28507 +                       di_read_unlock(d, !AuLock_IR);
28508 +                       if (bopq >= 0 && bopq < err)
28509 +                               err = bopq;
28510 +               }
28511 +       }
28512 +
28513 +out_free:
28514 +       dput(parent);
28515 +       au_dpages_free(&dpages);
28516 +out:
28517 +       return err;
28518 +}
28519 +
28520 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
28521 +{
28522 +       for (; bindex >= 0; bindex--)
28523 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
28524 +                       return bindex;
28525 +       return -EROFS;
28526 +}
28527 +
28528 +/* top down parent */
28529 +static int au_wbr_create_tdp(struct dentry *dentry, int isdir __maybe_unused)
28530 +{
28531 +       int err;
28532 +       aufs_bindex_t bstart, bindex;
28533 +       struct super_block *sb;
28534 +       struct dentry *parent, *h_parent;
28535 +
28536 +       sb = dentry->d_sb;
28537 +       bstart = au_dbstart(dentry);
28538 +       err = bstart;
28539 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
28540 +               goto out;
28541 +
28542 +       err = -EROFS;
28543 +       parent = dget_parent(dentry);
28544 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
28545 +               h_parent = au_h_dptr(parent, bindex);
28546 +               if (!h_parent || !h_parent->d_inode)
28547 +                       continue;
28548 +
28549 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
28550 +                       err = bindex;
28551 +                       break;
28552 +               }
28553 +       }
28554 +       dput(parent);
28555 +
28556 +       /* bottom up here */
28557 +       if (unlikely(err < 0)) {
28558 +               err = au_wbr_bu(sb, bstart - 1);
28559 +               if (err >= 0)
28560 +                       err = au_wbr_nonopq(dentry, err);
28561 +       }
28562 +
28563 +out:
28564 +       AuDbg("b%d\n", err);
28565 +       return err;
28566 +}
28567 +
28568 +/* ---------------------------------------------------------------------- */
28569 +
28570 +/* an exception for the policy other than tdp */
28571 +static int au_wbr_create_exp(struct dentry *dentry)
28572 +{
28573 +       int err;
28574 +       aufs_bindex_t bwh, bdiropq;
28575 +       struct dentry *parent;
28576 +
28577 +       err = -1;
28578 +       bwh = au_dbwh(dentry);
28579 +       parent = dget_parent(dentry);
28580 +       bdiropq = au_dbdiropq(parent);
28581 +       if (bwh >= 0) {
28582 +               if (bdiropq >= 0)
28583 +                       err = min(bdiropq, bwh);
28584 +               else
28585 +                       err = bwh;
28586 +               AuDbg("%d\n", err);
28587 +       } else if (bdiropq >= 0) {
28588 +               err = bdiropq;
28589 +               AuDbg("%d\n", err);
28590 +       }
28591 +       dput(parent);
28592 +
28593 +       if (err >= 0)
28594 +               err = au_wbr_nonopq(dentry, err);
28595 +
28596 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
28597 +               err = -1;
28598 +
28599 +       AuDbg("%d\n", err);
28600 +       return err;
28601 +}
28602 +
28603 +/* ---------------------------------------------------------------------- */
28604 +
28605 +/* round robin */
28606 +static int au_wbr_create_init_rr(struct super_block *sb)
28607 +{
28608 +       int err;
28609 +
28610 +       err = au_wbr_bu(sb, au_sbend(sb));
28611 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
28612 +       /* smp_mb(); */
28613 +
28614 +       AuDbg("b%d\n", err);
28615 +       return err;
28616 +}
28617 +
28618 +static int au_wbr_create_rr(struct dentry *dentry, int isdir)
28619 +{
28620 +       int err, nbr;
28621 +       unsigned int u;
28622 +       aufs_bindex_t bindex, bend;
28623 +       struct super_block *sb;
28624 +       atomic_t *next;
28625 +
28626 +       err = au_wbr_create_exp(dentry);
28627 +       if (err >= 0)
28628 +               goto out;
28629 +
28630 +       sb = dentry->d_sb;
28631 +       next = &au_sbi(sb)->si_wbr_rr_next;
28632 +       bend = au_sbend(sb);
28633 +       nbr = bend + 1;
28634 +       for (bindex = 0; bindex <= bend; bindex++) {
28635 +               if (!isdir) {
28636 +                       err = atomic_dec_return(next) + 1;
28637 +                       /* modulo for 0 is meaningless */
28638 +                       if (unlikely(!err))
28639 +                               err = atomic_dec_return(next) + 1;
28640 +               } else
28641 +                       err = atomic_read(next);
28642 +               AuDbg("%d\n", err);
28643 +               u = err;
28644 +               err = u % nbr;
28645 +               AuDbg("%d\n", err);
28646 +               if (!au_br_rdonly(au_sbr(sb, err)))
28647 +                       break;
28648 +               err = -EROFS;
28649 +       }
28650 +
28651 +       if (err >= 0)
28652 +               err = au_wbr_nonopq(dentry, err);
28653 +
28654 +out:
28655 +       AuDbg("%d\n", err);
28656 +       return err;
28657 +}
28658 +
28659 +/* ---------------------------------------------------------------------- */
28660 +
28661 +/* most free space */
28662 +static void au_mfs(struct dentry *dentry)
28663 +{
28664 +       struct super_block *sb;
28665 +       struct au_branch *br;
28666 +       struct au_wbr_mfs *mfs;
28667 +       aufs_bindex_t bindex, bend;
28668 +       int err;
28669 +       unsigned long long b, bavail;
28670 +       struct path h_path;
28671 +       /* reduce the stack usage */
28672 +       struct kstatfs *st;
28673 +
28674 +       st = kmalloc(sizeof(*st), GFP_NOFS);
28675 +       if (unlikely(!st)) {
28676 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
28677 +               return;
28678 +       }
28679 +
28680 +       bavail = 0;
28681 +       sb = dentry->d_sb;
28682 +       mfs = &au_sbi(sb)->si_wbr_mfs;
28683 +       MtxMustLock(&mfs->mfs_lock);
28684 +       mfs->mfs_bindex = -EROFS;
28685 +       mfs->mfsrr_bytes = 0;
28686 +       bend = au_sbend(sb);
28687 +       for (bindex = 0; bindex <= bend; bindex++) {
28688 +               br = au_sbr(sb, bindex);
28689 +               if (au_br_rdonly(br))
28690 +                       continue;
28691 +
28692 +               /* sb->s_root for NFS is unreliable */
28693 +               h_path.mnt = au_br_mnt(br);
28694 +               h_path.dentry = h_path.mnt->mnt_root;
28695 +               err = vfs_statfs(&h_path, st);
28696 +               if (unlikely(err)) {
28697 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
28698 +                       continue;
28699 +               }
28700 +
28701 +               /* when the available size is equal, select the lower one */
28702 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
28703 +                            || sizeof(b) < sizeof(st->f_bsize));
28704 +               b = st->f_bavail * st->f_bsize;
28705 +               br->br_wbr->wbr_bytes = b;
28706 +               if (b >= bavail) {
28707 +                       bavail = b;
28708 +                       mfs->mfs_bindex = bindex;
28709 +                       mfs->mfs_jiffy = jiffies;
28710 +               }
28711 +       }
28712 +
28713 +       mfs->mfsrr_bytes = bavail;
28714 +       AuDbg("b%d\n", mfs->mfs_bindex);
28715 +       kfree(st);
28716 +}
28717 +
28718 +static int au_wbr_create_mfs(struct dentry *dentry, int isdir __maybe_unused)
28719 +{
28720 +       int err;
28721 +       struct super_block *sb;
28722 +       struct au_wbr_mfs *mfs;
28723 +
28724 +       err = au_wbr_create_exp(dentry);
28725 +       if (err >= 0)
28726 +               goto out;
28727 +
28728 +       sb = dentry->d_sb;
28729 +       mfs = &au_sbi(sb)->si_wbr_mfs;
28730 +       mutex_lock(&mfs->mfs_lock);
28731 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
28732 +           || mfs->mfs_bindex < 0
28733 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
28734 +               au_mfs(dentry);
28735 +       mutex_unlock(&mfs->mfs_lock);
28736 +       err = mfs->mfs_bindex;
28737 +
28738 +       if (err >= 0)
28739 +               err = au_wbr_nonopq(dentry, err);
28740 +
28741 +out:
28742 +       AuDbg("b%d\n", err);
28743 +       return err;
28744 +}
28745 +
28746 +static int au_wbr_create_init_mfs(struct super_block *sb)
28747 +{
28748 +       struct au_wbr_mfs *mfs;
28749 +
28750 +       mfs = &au_sbi(sb)->si_wbr_mfs;
28751 +       mutex_init(&mfs->mfs_lock);
28752 +       mfs->mfs_jiffy = 0;
28753 +       mfs->mfs_bindex = -EROFS;
28754 +
28755 +       return 0;
28756 +}
28757 +
28758 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
28759 +{
28760 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
28761 +       return 0;
28762 +}
28763 +
28764 +/* ---------------------------------------------------------------------- */
28765 +
28766 +/* most free space and then round robin */
28767 +static int au_wbr_create_mfsrr(struct dentry *dentry, int isdir)
28768 +{
28769 +       int err;
28770 +       struct au_wbr_mfs *mfs;
28771 +
28772 +       err = au_wbr_create_mfs(dentry, isdir);
28773 +       if (err >= 0) {
28774 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
28775 +               mutex_lock(&mfs->mfs_lock);
28776 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
28777 +                       err = au_wbr_create_rr(dentry, isdir);
28778 +               mutex_unlock(&mfs->mfs_lock);
28779 +       }
28780 +
28781 +       AuDbg("b%d\n", err);
28782 +       return err;
28783 +}
28784 +
28785 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
28786 +{
28787 +       int err;
28788 +
28789 +       au_wbr_create_init_mfs(sb); /* ignore */
28790 +       err = au_wbr_create_init_rr(sb);
28791 +
28792 +       return err;
28793 +}
28794 +
28795 +/* ---------------------------------------------------------------------- */
28796 +
28797 +/* top down parent and most free space */
28798 +static int au_wbr_create_pmfs(struct dentry *dentry, int isdir)
28799 +{
28800 +       int err, e2;
28801 +       unsigned long long b;
28802 +       aufs_bindex_t bindex, bstart, bend;
28803 +       struct super_block *sb;
28804 +       struct dentry *parent, *h_parent;
28805 +       struct au_branch *br;
28806 +
28807 +       err = au_wbr_create_tdp(dentry, isdir);
28808 +       if (unlikely(err < 0))
28809 +               goto out;
28810 +       parent = dget_parent(dentry);
28811 +       bstart = au_dbstart(parent);
28812 +       bend = au_dbtaildir(parent);
28813 +       if (bstart == bend)
28814 +               goto out_parent; /* success */
28815 +
28816 +       e2 = au_wbr_create_mfs(dentry, isdir);
28817 +       if (e2 < 0)
28818 +               goto out_parent; /* success */
28819 +
28820 +       /* when the available size is equal, select upper one */
28821 +       sb = dentry->d_sb;
28822 +       br = au_sbr(sb, err);
28823 +       b = br->br_wbr->wbr_bytes;
28824 +       AuDbg("b%d, %llu\n", err, b);
28825 +
28826 +       for (bindex = bstart; bindex <= bend; bindex++) {
28827 +               h_parent = au_h_dptr(parent, bindex);
28828 +               if (!h_parent || !h_parent->d_inode)
28829 +                       continue;
28830 +
28831 +               br = au_sbr(sb, bindex);
28832 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
28833 +                       b = br->br_wbr->wbr_bytes;
28834 +                       err = bindex;
28835 +                       AuDbg("b%d, %llu\n", err, b);
28836 +               }
28837 +       }
28838 +
28839 +       if (err >= 0)
28840 +               err = au_wbr_nonopq(dentry, err);
28841 +
28842 +out_parent:
28843 +       dput(parent);
28844 +out:
28845 +       AuDbg("b%d\n", err);
28846 +       return err;
28847 +}
28848 +
28849 +/* ---------------------------------------------------------------------- */
28850 +
28851 +/* policies for copyup */
28852 +
28853 +/* top down parent */
28854 +static int au_wbr_copyup_tdp(struct dentry *dentry)
28855 +{
28856 +       return au_wbr_create_tdp(dentry, /*isdir, anything is ok*/0);
28857 +}
28858 +
28859 +/* bottom up parent */
28860 +static int au_wbr_copyup_bup(struct dentry *dentry)
28861 +{
28862 +       int err;
28863 +       aufs_bindex_t bindex, bstart;
28864 +       struct dentry *parent, *h_parent;
28865 +       struct super_block *sb;
28866 +
28867 +       err = -EROFS;
28868 +       sb = dentry->d_sb;
28869 +       parent = dget_parent(dentry);
28870 +       bstart = au_dbstart(parent);
28871 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
28872 +               h_parent = au_h_dptr(parent, bindex);
28873 +               if (!h_parent || !h_parent->d_inode)
28874 +                       continue;
28875 +
28876 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
28877 +                       err = bindex;
28878 +                       break;
28879 +               }
28880 +       }
28881 +       dput(parent);
28882 +
28883 +       /* bottom up here */
28884 +       if (unlikely(err < 0))
28885 +               err = au_wbr_bu(sb, bstart - 1);
28886 +
28887 +       AuDbg("b%d\n", err);
28888 +       return err;
28889 +}
28890 +
28891 +/* bottom up */
28892 +static int au_wbr_copyup_bu(struct dentry *dentry)
28893 +{
28894 +       int err;
28895 +       aufs_bindex_t bstart;
28896 +
28897 +       bstart = au_dbstart(dentry);
28898 +       err = au_wbr_bu(dentry->d_sb, bstart);
28899 +       AuDbg("b%d\n", err);
28900 +       if (err > bstart)
28901 +               err = au_wbr_nonopq(dentry, err);
28902 +
28903 +       AuDbg("b%d\n", err);
28904 +       return err;
28905 +}
28906 +
28907 +/* ---------------------------------------------------------------------- */
28908 +
28909 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
28910 +       [AuWbrCopyup_TDP] = {
28911 +               .copyup = au_wbr_copyup_tdp
28912 +       },
28913 +       [AuWbrCopyup_BUP] = {
28914 +               .copyup = au_wbr_copyup_bup
28915 +       },
28916 +       [AuWbrCopyup_BU] = {
28917 +               .copyup = au_wbr_copyup_bu
28918 +       }
28919 +};
28920 +
28921 +struct au_wbr_create_operations au_wbr_create_ops[] = {
28922 +       [AuWbrCreate_TDP] = {
28923 +               .create = au_wbr_create_tdp
28924 +       },
28925 +       [AuWbrCreate_RR] = {
28926 +               .create = au_wbr_create_rr,
28927 +               .init   = au_wbr_create_init_rr
28928 +       },
28929 +       [AuWbrCreate_MFS] = {
28930 +               .create = au_wbr_create_mfs,
28931 +               .init   = au_wbr_create_init_mfs,
28932 +               .fin    = au_wbr_create_fin_mfs
28933 +       },
28934 +       [AuWbrCreate_MFSV] = {
28935 +               .create = au_wbr_create_mfs,
28936 +               .init   = au_wbr_create_init_mfs,
28937 +               .fin    = au_wbr_create_fin_mfs
28938 +       },
28939 +       [AuWbrCreate_MFSRR] = {
28940 +               .create = au_wbr_create_mfsrr,
28941 +               .init   = au_wbr_create_init_mfsrr,
28942 +               .fin    = au_wbr_create_fin_mfs
28943 +       },
28944 +       [AuWbrCreate_MFSRRV] = {
28945 +               .create = au_wbr_create_mfsrr,
28946 +               .init   = au_wbr_create_init_mfsrr,
28947 +               .fin    = au_wbr_create_fin_mfs
28948 +       },
28949 +       [AuWbrCreate_PMFS] = {
28950 +               .create = au_wbr_create_pmfs,
28951 +               .init   = au_wbr_create_init_mfs,
28952 +               .fin    = au_wbr_create_fin_mfs
28953 +       },
28954 +       [AuWbrCreate_PMFSV] = {
28955 +               .create = au_wbr_create_pmfs,
28956 +               .init   = au_wbr_create_init_mfs,
28957 +               .fin    = au_wbr_create_fin_mfs
28958 +       }
28959 +};
28960 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
28961 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
28962 +++ linux/fs/aufs/whout.c       2013-07-06 13:20:47.760198800 +0200
28963 @@ -0,0 +1,1022 @@
28964 +/*
28965 + * Copyright (C) 2005-2013 Junjiro R. Okajima
28966 + *
28967 + * This program, aufs is free software; you can redistribute it and/or modify
28968 + * it under the terms of the GNU General Public License as published by
28969 + * the Free Software Foundation; either version 2 of the License, or
28970 + * (at your option) any later version.
28971 + *
28972 + * This program is distributed in the hope that it will be useful,
28973 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28974 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28975 + * GNU General Public License for more details.
28976 + *
28977 + * You should have received a copy of the GNU General Public License
28978 + * along with this program; if not, write to the Free Software
28979 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28980 + */
28981 +
28982 +/*
28983 + * whiteout for logical deletion and opaque directory
28984 + */
28985 +
28986 +#include "aufs.h"
28987 +
28988 +#define WH_MASK                        S_IRUGO
28989 +
28990 +/*
28991 + * If a directory contains this file, then it is opaque.  We start with the
28992 + * .wh. flag so that it is blocked by lookup.
28993 + */
28994 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
28995 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
28996 +
28997 +/*
28998 + * generate whiteout name, which is NOT terminated by NULL.
28999 + * @name: original d_name.name
29000 + * @len: original d_name.len
29001 + * @wh: whiteout qstr
29002 + * returns zero when succeeds, otherwise error.
29003 + * succeeded value as wh->name should be freed by kfree().
29004 + */
29005 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
29006 +{
29007 +       char *p;
29008 +
29009 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
29010 +               return -ENAMETOOLONG;
29011 +
29012 +       wh->len = name->len + AUFS_WH_PFX_LEN;
29013 +       p = kmalloc(wh->len, GFP_NOFS);
29014 +       wh->name = p;
29015 +       if (p) {
29016 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29017 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
29018 +               /* smp_mb(); */
29019 +               return 0;
29020 +       }
29021 +       return -ENOMEM;
29022 +}
29023 +
29024 +/* ---------------------------------------------------------------------- */
29025 +
29026 +/*
29027 + * test if the @wh_name exists under @h_parent.
29028 + * @try_sio specifies the necessary of super-io.
29029 + */
29030 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
29031 +              struct au_branch *br, int try_sio)
29032 +{
29033 +       int err;
29034 +       struct dentry *wh_dentry;
29035 +
29036 +       if (!try_sio)
29037 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
29038 +       else
29039 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent, br);
29040 +       err = PTR_ERR(wh_dentry);
29041 +       if (IS_ERR(wh_dentry))
29042 +               goto out;
29043 +
29044 +       err = 0;
29045 +       if (!wh_dentry->d_inode)
29046 +               goto out_wh; /* success */
29047 +
29048 +       err = 1;
29049 +       if (S_ISREG(wh_dentry->d_inode->i_mode))
29050 +               goto out_wh; /* success */
29051 +
29052 +       err = -EIO;
29053 +       AuIOErr("%.*s Invalid whiteout entry type 0%o.\n",
29054 +               AuDLNPair(wh_dentry), wh_dentry->d_inode->i_mode);
29055 +
29056 +out_wh:
29057 +       dput(wh_dentry);
29058 +out:
29059 +       return err;
29060 +}
29061 +
29062 +/*
29063 + * test if the @h_dentry sets opaque or not.
29064 + */
29065 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br)
29066 +{
29067 +       int err;
29068 +       struct inode *h_dir;
29069 +
29070 +       h_dir = h_dentry->d_inode;
29071 +       err = au_wh_test(h_dentry, &diropq_name, br,
29072 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
29073 +       return err;
29074 +}
29075 +
29076 +/*
29077 + * returns a negative dentry whose name is unique and temporary.
29078 + */
29079 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
29080 +                            struct qstr *prefix)
29081 +{
29082 +       struct dentry *dentry;
29083 +       int i;
29084 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
29085 +               *name, *p;
29086 +       /* strict atomic_t is unnecessary here */
29087 +       static unsigned short cnt;
29088 +       struct qstr qs;
29089 +
29090 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
29091 +
29092 +       name = defname;
29093 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
29094 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
29095 +               dentry = ERR_PTR(-ENAMETOOLONG);
29096 +               if (unlikely(qs.len > NAME_MAX))
29097 +                       goto out;
29098 +               dentry = ERR_PTR(-ENOMEM);
29099 +               name = kmalloc(qs.len + 1, GFP_NOFS);
29100 +               if (unlikely(!name))
29101 +                       goto out;
29102 +       }
29103 +
29104 +       /* doubly whiteout-ed */
29105 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
29106 +       p = name + AUFS_WH_PFX_LEN * 2;
29107 +       memcpy(p, prefix->name, prefix->len);
29108 +       p += prefix->len;
29109 +       *p++ = '.';
29110 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
29111 +
29112 +       qs.name = name;
29113 +       for (i = 0; i < 3; i++) {
29114 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
29115 +               dentry = au_sio_lkup_one(&qs, h_parent, br);
29116 +               if (IS_ERR(dentry) || !dentry->d_inode)
29117 +                       goto out_name;
29118 +               dput(dentry);
29119 +       }
29120 +       /* pr_warn("could not get random name\n"); */
29121 +       dentry = ERR_PTR(-EEXIST);
29122 +       AuDbg("%.*s\n", AuLNPair(&qs));
29123 +       BUG();
29124 +
29125 +out_name:
29126 +       if (name != defname)
29127 +               kfree(name);
29128 +out:
29129 +       AuTraceErrPtr(dentry);
29130 +       return dentry;
29131 +}
29132 +
29133 +/*
29134 + * rename the @h_dentry on @br to the whiteouted temporary name.
29135 + */
29136 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
29137 +{
29138 +       int err;
29139 +       struct path h_path = {
29140 +               .mnt = au_br_mnt(br)
29141 +       };
29142 +       struct inode *h_dir;
29143 +       struct dentry *h_parent;
29144 +
29145 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
29146 +       h_dir = h_parent->d_inode;
29147 +       IMustLock(h_dir);
29148 +
29149 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
29150 +       err = PTR_ERR(h_path.dentry);
29151 +       if (IS_ERR(h_path.dentry))
29152 +               goto out;
29153 +
29154 +       /* under the same dir, no need to lock_rename() */
29155 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path);
29156 +       AuTraceErr(err);
29157 +       dput(h_path.dentry);
29158 +
29159 +out:
29160 +       AuTraceErr(err);
29161 +       return err;
29162 +}
29163 +
29164 +/* ---------------------------------------------------------------------- */
29165 +/*
29166 + * functions for removing a whiteout
29167 + */
29168 +
29169 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
29170 +{
29171 +       int force;
29172 +
29173 +       /*
29174 +        * forces superio when the dir has a sticky bit.
29175 +        * this may be a violation of unix fs semantics.
29176 +        */
29177 +       force = (h_dir->i_mode & S_ISVTX)
29178 +               && !uid_eq(current_fsuid(), h_path->dentry->d_inode->i_uid);
29179 +       return vfsub_unlink(h_dir, h_path, force);
29180 +}
29181 +
29182 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
29183 +                       struct dentry *dentry)
29184 +{
29185 +       int err;
29186 +
29187 +       err = do_unlink_wh(h_dir, h_path);
29188 +       if (!err && dentry)
29189 +               au_set_dbwh(dentry, -1);
29190 +
29191 +       return err;
29192 +}
29193 +
29194 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
29195 +                         struct au_branch *br)
29196 +{
29197 +       int err;
29198 +       struct path h_path = {
29199 +               .mnt = au_br_mnt(br)
29200 +       };
29201 +
29202 +       err = 0;
29203 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
29204 +       if (IS_ERR(h_path.dentry))
29205 +               err = PTR_ERR(h_path.dentry);
29206 +       else {
29207 +               if (h_path.dentry->d_inode
29208 +                   && S_ISREG(h_path.dentry->d_inode->i_mode))
29209 +                       err = do_unlink_wh(h_parent->d_inode, &h_path);
29210 +               dput(h_path.dentry);
29211 +       }
29212 +
29213 +       return err;
29214 +}
29215 +
29216 +/* ---------------------------------------------------------------------- */
29217 +/*
29218 + * initialize/clean whiteout for a branch
29219 + */
29220 +
29221 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
29222 +                       const int isdir)
29223 +{
29224 +       int err;
29225 +
29226 +       if (!whpath->dentry->d_inode)
29227 +               return;
29228 +
29229 +       if (isdir)
29230 +               err = vfsub_rmdir(h_dir, whpath);
29231 +       else
29232 +               err = vfsub_unlink(h_dir, whpath, /*force*/0);
29233 +       if (unlikely(err))
29234 +               pr_warn("failed removing %.*s (%d), ignored.\n",
29235 +                       AuDLNPair(whpath->dentry), err);
29236 +}
29237 +
29238 +static int test_linkable(struct dentry *h_root)
29239 +{
29240 +       struct inode *h_dir = h_root->d_inode;
29241 +
29242 +       if (h_dir->i_op->link)
29243 +               return 0;
29244 +
29245 +       pr_err("%.*s (%s) doesn't support link(2), use noplink and rw+nolwh\n",
29246 +              AuDLNPair(h_root), au_sbtype(h_root->d_sb));
29247 +       return -ENOSYS;
29248 +}
29249 +
29250 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
29251 +static int au_whdir(struct inode *h_dir, struct path *path)
29252 +{
29253 +       int err;
29254 +
29255 +       err = -EEXIST;
29256 +       if (!path->dentry->d_inode) {
29257 +               int mode = S_IRWXU;
29258 +
29259 +               if (au_test_nfs(path->dentry->d_sb))
29260 +                       mode |= S_IXUGO;
29261 +               err = vfsub_mkdir(h_dir, path, mode);
29262 +       } else if (S_ISDIR(path->dentry->d_inode->i_mode))
29263 +               err = 0;
29264 +       else
29265 +               pr_err("unknown %.*s exists\n", AuDLNPair(path->dentry));
29266 +
29267 +       return err;
29268 +}
29269 +
29270 +struct au_wh_base {
29271 +       const struct qstr *name;
29272 +       struct dentry *dentry;
29273 +};
29274 +
29275 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
29276 +                         struct path *h_path)
29277 +{
29278 +       h_path->dentry = base[AuBrWh_BASE].dentry;
29279 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
29280 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
29281 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
29282 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
29283 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
29284 +}
29285 +
29286 +/*
29287 + * returns tri-state,
29288 + * minus: error, caller should print the mesage
29289 + * zero: succuess
29290 + * plus: error, caller should NOT print the mesage
29291 + */
29292 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
29293 +                               int do_plink, struct au_wh_base base[],
29294 +                               struct path *h_path)
29295 +{
29296 +       int err;
29297 +       struct inode *h_dir;
29298 +
29299 +       h_dir = h_root->d_inode;
29300 +       h_path->dentry = base[AuBrWh_BASE].dentry;
29301 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
29302 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
29303 +       if (do_plink) {
29304 +               err = test_linkable(h_root);
29305 +               if (unlikely(err)) {
29306 +                       err = 1;
29307 +                       goto out;
29308 +               }
29309 +
29310 +               err = au_whdir(h_dir, h_path);
29311 +               if (unlikely(err))
29312 +                       goto out;
29313 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
29314 +       } else
29315 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
29316 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
29317 +       err = au_whdir(h_dir, h_path);
29318 +       if (unlikely(err))
29319 +               goto out;
29320 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
29321 +
29322 +out:
29323 +       return err;
29324 +}
29325 +
29326 +/*
29327 + * for the moment, aufs supports the branch filesystem which does not support
29328 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
29329 + * copyup failed. finally, such filesystem will not be used as the writable
29330 + * branch.
29331 + *
29332 + * returns tri-state, see above.
29333 + */
29334 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
29335 +                        int do_plink, struct au_wh_base base[],
29336 +                        struct path *h_path)
29337 +{
29338 +       int err;
29339 +       struct inode *h_dir;
29340 +
29341 +       WbrWhMustWriteLock(wbr);
29342 +
29343 +       err = test_linkable(h_root);
29344 +       if (unlikely(err)) {
29345 +               err = 1;
29346 +               goto out;
29347 +       }
29348 +
29349 +       /*
29350 +        * todo: should this create be done in /sbin/mount.aufs helper?
29351 +        */
29352 +       err = -EEXIST;
29353 +       h_dir = h_root->d_inode;
29354 +       if (!base[AuBrWh_BASE].dentry->d_inode) {
29355 +               h_path->dentry = base[AuBrWh_BASE].dentry;
29356 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
29357 +       } else if (S_ISREG(base[AuBrWh_BASE].dentry->d_inode->i_mode))
29358 +               err = 0;
29359 +       else
29360 +               pr_err("unknown %.*s/%.*s exists\n",
29361 +                      AuDLNPair(h_root), AuDLNPair(base[AuBrWh_BASE].dentry));
29362 +       if (unlikely(err))
29363 +               goto out;
29364 +
29365 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
29366 +       if (do_plink) {
29367 +               err = au_whdir(h_dir, h_path);
29368 +               if (unlikely(err))
29369 +                       goto out;
29370 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
29371 +       } else
29372 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
29373 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
29374 +
29375 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
29376 +       err = au_whdir(h_dir, h_path);
29377 +       if (unlikely(err))
29378 +               goto out;
29379 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
29380 +
29381 +out:
29382 +       return err;
29383 +}
29384 +
29385 +/*
29386 + * initialize the whiteout base file/dir for @br.
29387 + */
29388 +int au_wh_init(struct au_branch *br, struct super_block *sb)
29389 +{
29390 +       int err, i;
29391 +       const unsigned char do_plink
29392 +               = !!au_opt_test(au_mntflags(sb), PLINK);
29393 +       struct inode *h_dir;
29394 +       struct path path = br->br_path;
29395 +       struct dentry *h_root = path.dentry;
29396 +       struct au_wbr *wbr = br->br_wbr;
29397 +       static const struct qstr base_name[] = {
29398 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
29399 +                                         sizeof(AUFS_BASE_NAME) - 1),
29400 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
29401 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
29402 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
29403 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
29404 +       };
29405 +       struct au_wh_base base[] = {
29406 +               [AuBrWh_BASE] = {
29407 +                       .name   = base_name + AuBrWh_BASE,
29408 +                       .dentry = NULL
29409 +               },
29410 +               [AuBrWh_PLINK] = {
29411 +                       .name   = base_name + AuBrWh_PLINK,
29412 +                       .dentry = NULL
29413 +               },
29414 +               [AuBrWh_ORPH] = {
29415 +                       .name   = base_name + AuBrWh_ORPH,
29416 +                       .dentry = NULL
29417 +               }
29418 +       };
29419 +
29420 +       if (wbr)
29421 +               WbrWhMustWriteLock(wbr);
29422 +
29423 +       for (i = 0; i < AuBrWh_Last; i++) {
29424 +               /* doubly whiteouted */
29425 +               struct dentry *d;
29426 +
29427 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
29428 +               err = PTR_ERR(d);
29429 +               if (IS_ERR(d))
29430 +                       goto out;
29431 +
29432 +               base[i].dentry = d;
29433 +               AuDebugOn(wbr
29434 +                         && wbr->wbr_wh[i]
29435 +                         && wbr->wbr_wh[i] != base[i].dentry);
29436 +       }
29437 +
29438 +       if (wbr)
29439 +               for (i = 0; i < AuBrWh_Last; i++) {
29440 +                       dput(wbr->wbr_wh[i]);
29441 +                       wbr->wbr_wh[i] = NULL;
29442 +               }
29443 +
29444 +       err = 0;
29445 +       if (!au_br_writable(br->br_perm)) {
29446 +               h_dir = h_root->d_inode;
29447 +               au_wh_init_ro(h_dir, base, &path);
29448 +       } else if (!au_br_wh_linkable(br->br_perm)) {
29449 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
29450 +               if (err > 0)
29451 +                       goto out;
29452 +               else if (err)
29453 +                       goto out_err;
29454 +       } else {
29455 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
29456 +               if (err > 0)
29457 +                       goto out;
29458 +               else if (err)
29459 +                       goto out_err;
29460 +       }
29461 +       goto out; /* success */
29462 +
29463 +out_err:
29464 +       pr_err("an error(%d) on the writable branch %.*s(%s)\n",
29465 +              err, AuDLNPair(h_root), au_sbtype(h_root->d_sb));
29466 +out:
29467 +       for (i = 0; i < AuBrWh_Last; i++)
29468 +               dput(base[i].dentry);
29469 +       return err;
29470 +}
29471 +
29472 +/* ---------------------------------------------------------------------- */
29473 +/*
29474 + * whiteouts are all hard-linked usually.
29475 + * when its link count reaches a ceiling, we create a new whiteout base
29476 + * asynchronously.
29477 + */
29478 +
29479 +struct reinit_br_wh {
29480 +       struct super_block *sb;
29481 +       struct au_branch *br;
29482 +};
29483 +
29484 +static void reinit_br_wh(void *arg)
29485 +{
29486 +       int err;
29487 +       aufs_bindex_t bindex;
29488 +       struct path h_path;
29489 +       struct reinit_br_wh *a = arg;
29490 +       struct au_wbr *wbr;
29491 +       struct inode *dir;
29492 +       struct dentry *h_root;
29493 +       struct au_hinode *hdir;
29494 +
29495 +       err = 0;
29496 +       wbr = a->br->br_wbr;
29497 +       /* big aufs lock */
29498 +       si_noflush_write_lock(a->sb);
29499 +       if (!au_br_writable(a->br->br_perm))
29500 +               goto out;
29501 +       bindex = au_br_index(a->sb, a->br->br_id);
29502 +       if (unlikely(bindex < 0))
29503 +               goto out;
29504 +
29505 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
29506 +       dir = a->sb->s_root->d_inode;
29507 +       hdir = au_hi(dir, bindex);
29508 +       h_root = au_h_dptr(a->sb->s_root, bindex);
29509 +       AuDebugOn(h_root != au_br_dentry(a->br));
29510 +
29511 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
29512 +       wbr_wh_write_lock(wbr);
29513 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
29514 +                         h_root, a->br);
29515 +       if (!err) {
29516 +               h_path.dentry = wbr->wbr_whbase;
29517 +               h_path.mnt = au_br_mnt(a->br);
29518 +               err = vfsub_unlink(hdir->hi_inode, &h_path, /*force*/0);
29519 +       } else {
29520 +               pr_warn("%.*s is moved, ignored\n",
29521 +                       AuDLNPair(wbr->wbr_whbase));
29522 +               err = 0;
29523 +       }
29524 +       dput(wbr->wbr_whbase);
29525 +       wbr->wbr_whbase = NULL;
29526 +       if (!err)
29527 +               err = au_wh_init(a->br, a->sb);
29528 +       wbr_wh_write_unlock(wbr);
29529 +       au_hn_imtx_unlock(hdir);
29530 +       di_read_unlock(a->sb->s_root, AuLock_IR);
29531 +
29532 +out:
29533 +       if (wbr)
29534 +               atomic_dec(&wbr->wbr_wh_running);
29535 +       atomic_dec(&a->br->br_count);
29536 +       si_write_unlock(a->sb);
29537 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
29538 +       kfree(arg);
29539 +       if (unlikely(err))
29540 +               AuIOErr("err %d\n", err);
29541 +}
29542 +
29543 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
29544 +{
29545 +       int do_dec, wkq_err;
29546 +       struct reinit_br_wh *arg;
29547 +
29548 +       do_dec = 1;
29549 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
29550 +               goto out;
29551 +
29552 +       /* ignore ENOMEM */
29553 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
29554 +       if (arg) {
29555 +               /*
29556 +                * dec(wh_running), kfree(arg) and dec(br_count)
29557 +                * in reinit function
29558 +                */
29559 +               arg->sb = sb;
29560 +               arg->br = br;
29561 +               atomic_inc(&br->br_count);
29562 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
29563 +               if (unlikely(wkq_err)) {
29564 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
29565 +                       atomic_dec(&br->br_count);
29566 +                       kfree(arg);
29567 +               }
29568 +               do_dec = 0;
29569 +       }
29570 +
29571 +out:
29572 +       if (do_dec)
29573 +               atomic_dec(&br->br_wbr->wbr_wh_running);
29574 +}
29575 +
29576 +/* ---------------------------------------------------------------------- */
29577 +
29578 +/*
29579 + * create the whiteout @wh.
29580 + */
29581 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
29582 +                            struct dentry *wh)
29583 +{
29584 +       int err;
29585 +       struct path h_path = {
29586 +               .dentry = wh
29587 +       };
29588 +       struct au_branch *br;
29589 +       struct au_wbr *wbr;
29590 +       struct dentry *h_parent;
29591 +       struct inode *h_dir;
29592 +
29593 +       h_parent = wh->d_parent; /* dir inode is locked */
29594 +       h_dir = h_parent->d_inode;
29595 +       IMustLock(h_dir);
29596 +
29597 +       br = au_sbr(sb, bindex);
29598 +       h_path.mnt = au_br_mnt(br);
29599 +       wbr = br->br_wbr;
29600 +       wbr_wh_read_lock(wbr);
29601 +       if (wbr->wbr_whbase) {
29602 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path);
29603 +               if (!err || err != -EMLINK)
29604 +                       goto out;
29605 +
29606 +               /* link count full. re-initialize br_whbase. */
29607 +               kick_reinit_br_wh(sb, br);
29608 +       }
29609 +
29610 +       /* return this error in this context */
29611 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
29612 +
29613 +out:
29614 +       wbr_wh_read_unlock(wbr);
29615 +       return err;
29616 +}
29617 +
29618 +/* ---------------------------------------------------------------------- */
29619 +
29620 +/*
29621 + * create or remove the diropq.
29622 + */
29623 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
29624 +                               unsigned int flags)
29625 +{
29626 +       struct dentry *opq_dentry, *h_dentry;
29627 +       struct super_block *sb;
29628 +       struct au_branch *br;
29629 +       int err;
29630 +
29631 +       sb = dentry->d_sb;
29632 +       br = au_sbr(sb, bindex);
29633 +       h_dentry = au_h_dptr(dentry, bindex);
29634 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
29635 +       if (IS_ERR(opq_dentry))
29636 +               goto out;
29637 +
29638 +       if (au_ftest_diropq(flags, CREATE)) {
29639 +               err = link_or_create_wh(sb, bindex, opq_dentry);
29640 +               if (!err) {
29641 +                       au_set_dbdiropq(dentry, bindex);
29642 +                       goto out; /* success */
29643 +               }
29644 +       } else {
29645 +               struct path tmp = {
29646 +                       .dentry = opq_dentry,
29647 +                       .mnt    = au_br_mnt(br)
29648 +               };
29649 +               err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
29650 +               if (!err)
29651 +                       au_set_dbdiropq(dentry, -1);
29652 +       }
29653 +       dput(opq_dentry);
29654 +       opq_dentry = ERR_PTR(err);
29655 +
29656 +out:
29657 +       return opq_dentry;
29658 +}
29659 +
29660 +struct do_diropq_args {
29661 +       struct dentry **errp;
29662 +       struct dentry *dentry;
29663 +       aufs_bindex_t bindex;
29664 +       unsigned int flags;
29665 +};
29666 +
29667 +static void call_do_diropq(void *args)
29668 +{
29669 +       struct do_diropq_args *a = args;
29670 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
29671 +}
29672 +
29673 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
29674 +                            unsigned int flags)
29675 +{
29676 +       struct dentry *diropq, *h_dentry;
29677 +
29678 +       h_dentry = au_h_dptr(dentry, bindex);
29679 +       if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
29680 +               diropq = do_diropq(dentry, bindex, flags);
29681 +       else {
29682 +               int wkq_err;
29683 +               struct do_diropq_args args = {
29684 +                       .errp           = &diropq,
29685 +                       .dentry         = dentry,
29686 +                       .bindex         = bindex,
29687 +                       .flags          = flags
29688 +               };
29689 +
29690 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
29691 +               if (unlikely(wkq_err))
29692 +                       diropq = ERR_PTR(wkq_err);
29693 +       }
29694 +
29695 +       return diropq;
29696 +}
29697 +
29698 +/* ---------------------------------------------------------------------- */
29699 +
29700 +/*
29701 + * lookup whiteout dentry.
29702 + * @h_parent: lower parent dentry which must exist and be locked
29703 + * @base_name: name of dentry which will be whiteouted
29704 + * returns dentry for whiteout.
29705 + */
29706 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
29707 +                         struct au_branch *br)
29708 +{
29709 +       int err;
29710 +       struct qstr wh_name;
29711 +       struct dentry *wh_dentry;
29712 +
29713 +       err = au_wh_name_alloc(&wh_name, base_name);
29714 +       wh_dentry = ERR_PTR(err);
29715 +       if (!err) {
29716 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
29717 +               kfree(wh_name.name);
29718 +       }
29719 +       return wh_dentry;
29720 +}
29721 +
29722 +/*
29723 + * link/create a whiteout for @dentry on @bindex.
29724 + */
29725 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
29726 +                           struct dentry *h_parent)
29727 +{
29728 +       struct dentry *wh_dentry;
29729 +       struct super_block *sb;
29730 +       int err;
29731 +
29732 +       sb = dentry->d_sb;
29733 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
29734 +       if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
29735 +               err = link_or_create_wh(sb, bindex, wh_dentry);
29736 +               if (!err)
29737 +                       au_set_dbwh(dentry, bindex);
29738 +               else {
29739 +                       dput(wh_dentry);
29740 +                       wh_dentry = ERR_PTR(err);
29741 +               }
29742 +       }
29743 +
29744 +       return wh_dentry;
29745 +}
29746 +
29747 +/* ---------------------------------------------------------------------- */
29748 +
29749 +/* Delete all whiteouts in this directory on branch bindex. */
29750 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
29751 +                          aufs_bindex_t bindex, struct au_branch *br)
29752 +{
29753 +       int err;
29754 +       unsigned long ul, n;
29755 +       struct qstr wh_name;
29756 +       char *p;
29757 +       struct hlist_head *head;
29758 +       struct au_vdir_wh *pos;
29759 +       struct au_vdir_destr *str;
29760 +
29761 +       err = -ENOMEM;
29762 +       p = (void *)__get_free_page(GFP_NOFS);
29763 +       wh_name.name = p;
29764 +       if (unlikely(!wh_name.name))
29765 +               goto out;
29766 +
29767 +       err = 0;
29768 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29769 +       p += AUFS_WH_PFX_LEN;
29770 +       n = whlist->nh_num;
29771 +       head = whlist->nh_head;
29772 +       for (ul = 0; !err && ul < n; ul++, head++) {
29773 +               hlist_for_each_entry(pos, head, wh_hash) {
29774 +                       if (pos->wh_bindex != bindex)
29775 +                               continue;
29776 +
29777 +                       str = &pos->wh_str;
29778 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
29779 +                               memcpy(p, str->name, str->len);
29780 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
29781 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
29782 +                               if (!err)
29783 +                                       continue;
29784 +                               break;
29785 +                       }
29786 +                       AuIOErr("whiteout name too long %.*s\n",
29787 +                               str->len, str->name);
29788 +                       err = -EIO;
29789 +                       break;
29790 +               }
29791 +       }
29792 +       free_page((unsigned long)wh_name.name);
29793 +
29794 +out:
29795 +       return err;
29796 +}
29797 +
29798 +struct del_wh_children_args {
29799 +       int *errp;
29800 +       struct dentry *h_dentry;
29801 +       struct au_nhash *whlist;
29802 +       aufs_bindex_t bindex;
29803 +       struct au_branch *br;
29804 +};
29805 +
29806 +static void call_del_wh_children(void *args)
29807 +{
29808 +       struct del_wh_children_args *a = args;
29809 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
29810 +}
29811 +
29812 +/* ---------------------------------------------------------------------- */
29813 +
29814 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
29815 +{
29816 +       struct au_whtmp_rmdir *whtmp;
29817 +       int err;
29818 +       unsigned int rdhash;
29819 +
29820 +       SiMustAnyLock(sb);
29821 +
29822 +       whtmp = kmalloc(sizeof(*whtmp), gfp);
29823 +       if (unlikely(!whtmp)) {
29824 +               whtmp = ERR_PTR(-ENOMEM);
29825 +               goto out;
29826 +       }
29827 +
29828 +       whtmp->dir = NULL;
29829 +       whtmp->br = NULL;
29830 +       whtmp->wh_dentry = NULL;
29831 +       /* no estimation for dir size */
29832 +       rdhash = au_sbi(sb)->si_rdhash;
29833 +       if (!rdhash)
29834 +               rdhash = AUFS_RDHASH_DEF;
29835 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
29836 +       if (unlikely(err)) {
29837 +               kfree(whtmp);
29838 +               whtmp = ERR_PTR(err);
29839 +       }
29840 +
29841 +out:
29842 +       return whtmp;
29843 +}
29844 +
29845 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
29846 +{
29847 +       if (whtmp->br)
29848 +               atomic_dec(&whtmp->br->br_count);
29849 +       dput(whtmp->wh_dentry);
29850 +       iput(whtmp->dir);
29851 +       au_nhash_wh_free(&whtmp->whlist);
29852 +       kfree(whtmp);
29853 +}
29854 +
29855 +/*
29856 + * rmdir the whiteouted temporary named dir @h_dentry.
29857 + * @whlist: whiteouted children.
29858 + */
29859 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
29860 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
29861 +{
29862 +       int err;
29863 +       struct path h_tmp;
29864 +       struct inode *wh_inode, *h_dir;
29865 +       struct au_branch *br;
29866 +
29867 +       h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
29868 +       IMustLock(h_dir);
29869 +
29870 +       br = au_sbr(dir->i_sb, bindex);
29871 +       wh_inode = wh_dentry->d_inode;
29872 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
29873 +
29874 +       /*
29875 +        * someone else might change some whiteouts while we were sleeping.
29876 +        * it means this whlist may have an obsoleted entry.
29877 +        */
29878 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
29879 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
29880 +       else {
29881 +               int wkq_err;
29882 +               struct del_wh_children_args args = {
29883 +                       .errp           = &err,
29884 +                       .h_dentry       = wh_dentry,
29885 +                       .whlist         = whlist,
29886 +                       .bindex         = bindex,
29887 +                       .br             = br
29888 +               };
29889 +
29890 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
29891 +               if (unlikely(wkq_err))
29892 +                       err = wkq_err;
29893 +       }
29894 +       mutex_unlock(&wh_inode->i_mutex);
29895 +
29896 +       if (!err) {
29897 +               h_tmp.dentry = wh_dentry;
29898 +               h_tmp.mnt = au_br_mnt(br);
29899 +               err = vfsub_rmdir(h_dir, &h_tmp);
29900 +       }
29901 +
29902 +       if (!err) {
29903 +               if (au_ibstart(dir) == bindex) {
29904 +                       /* todo: dir->i_mutex is necessary */
29905 +                       au_cpup_attr_timesizes(dir);
29906 +                       vfsub_drop_nlink(dir);
29907 +               }
29908 +               return 0; /* success */
29909 +       }
29910 +
29911 +       pr_warn("failed removing %.*s(%d), ignored\n",
29912 +               AuDLNPair(wh_dentry), err);
29913 +       return err;
29914 +}
29915 +
29916 +static void call_rmdir_whtmp(void *args)
29917 +{
29918 +       int err;
29919 +       aufs_bindex_t bindex;
29920 +       struct au_whtmp_rmdir *a = args;
29921 +       struct super_block *sb;
29922 +       struct dentry *h_parent;
29923 +       struct inode *h_dir;
29924 +       struct au_hinode *hdir;
29925 +
29926 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
29927 +       /* mutex_lock(&a->dir->i_mutex); */
29928 +       err = -EROFS;
29929 +       sb = a->dir->i_sb;
29930 +       si_read_lock(sb, !AuLock_FLUSH);
29931 +       if (!au_br_writable(a->br->br_perm))
29932 +               goto out;
29933 +       bindex = au_br_index(sb, a->br->br_id);
29934 +       if (unlikely(bindex < 0))
29935 +               goto out;
29936 +
29937 +       err = -EIO;
29938 +       ii_write_lock_parent(a->dir);
29939 +       h_parent = dget_parent(a->wh_dentry);
29940 +       h_dir = h_parent->d_inode;
29941 +       hdir = au_hi(a->dir, bindex);
29942 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
29943 +       if (unlikely(err))
29944 +               goto out_mnt;
29945 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
29946 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
29947 +                         a->br);
29948 +       if (!err)
29949 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
29950 +       au_hn_imtx_unlock(hdir);
29951 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
29952 +
29953 +out_mnt:
29954 +       dput(h_parent);
29955 +       ii_write_unlock(a->dir);
29956 +out:
29957 +       /* mutex_unlock(&a->dir->i_mutex); */
29958 +       au_whtmp_rmdir_free(a);
29959 +       si_read_unlock(sb);
29960 +       au_nwt_done(&au_sbi(sb)->si_nowait);
29961 +       if (unlikely(err))
29962 +               AuIOErr("err %d\n", err);
29963 +}
29964 +
29965 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
29966 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
29967 +{
29968 +       int wkq_err;
29969 +       struct super_block *sb;
29970 +
29971 +       IMustLock(dir);
29972 +
29973 +       /* all post-process will be done in do_rmdir_whtmp(). */
29974 +       sb = dir->i_sb;
29975 +       args->dir = au_igrab(dir);
29976 +       args->br = au_sbr(sb, bindex);
29977 +       atomic_inc(&args->br->br_count);
29978 +       args->wh_dentry = dget(wh_dentry);
29979 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
29980 +       if (unlikely(wkq_err)) {
29981 +               pr_warn("rmdir error %.*s (%d), ignored\n",
29982 +                       AuDLNPair(wh_dentry), wkq_err);
29983 +               au_whtmp_rmdir_free(args);
29984 +       }
29985 +}
29986 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
29987 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
29988 +++ linux/fs/aufs/whout.h       2013-07-06 13:20:47.760198800 +0200
29989 @@ -0,0 +1,87 @@
29990 +/*
29991 + * Copyright (C) 2005-2013 Junjiro R. Okajima
29992 + *
29993 + * This program, aufs is free software; you can redistribute it and/or modify
29994 + * it under the terms of the GNU General Public License as published by
29995 + * the Free Software Foundation; either version 2 of the License, or
29996 + * (at your option) any later version.
29997 + *
29998 + * This program is distributed in the hope that it will be useful,
29999 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30000 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30001 + * GNU General Public License for more details.
30002 + *
30003 + * You should have received a copy of the GNU General Public License
30004 + * along with this program; if not, write to the Free Software
30005 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30006 + */
30007 +
30008 +/*
30009 + * whiteout for logical deletion and opaque directory
30010 + */
30011 +
30012 +#ifndef __AUFS_WHOUT_H__
30013 +#define __AUFS_WHOUT_H__
30014 +
30015 +#ifdef __KERNEL__
30016 +
30017 +#include "dir.h"
30018 +
30019 +/* whout.c */
30020 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
30021 +struct au_branch;
30022 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
30023 +              struct au_branch *br, int try_sio);
30024 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br);
30025 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
30026 +                            struct qstr *prefix);
30027 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
30028 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
30029 +                       struct dentry *dentry);
30030 +int au_wh_init(struct au_branch *br, struct super_block *sb);
30031 +
30032 +/* diropq flags */
30033 +#define AuDiropq_CREATE        1
30034 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
30035 +#define au_fset_diropq(flags, name) \
30036 +       do { (flags) |= AuDiropq_##name; } while (0)
30037 +#define au_fclr_diropq(flags, name) \
30038 +       do { (flags) &= ~AuDiropq_##name; } while (0)
30039 +
30040 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
30041 +                            unsigned int flags);
30042 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
30043 +                         struct au_branch *br);
30044 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
30045 +                           struct dentry *h_parent);
30046 +
30047 +/* real rmdir for the whiteout-ed dir */
30048 +struct au_whtmp_rmdir {
30049 +       struct inode *dir;
30050 +       struct au_branch *br;
30051 +       struct dentry *wh_dentry;
30052 +       struct au_nhash whlist;
30053 +};
30054 +
30055 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
30056 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
30057 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
30058 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
30059 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
30060 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
30061 +
30062 +/* ---------------------------------------------------------------------- */
30063 +
30064 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
30065 +                                             aufs_bindex_t bindex)
30066 +{
30067 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
30068 +}
30069 +
30070 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
30071 +{
30072 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
30073 +}
30074 +
30075 +#endif /* __KERNEL__ */
30076 +#endif /* __AUFS_WHOUT_H__ */
30077 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
30078 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
30079 +++ linux/fs/aufs/wkq.c 2013-08-23 23:59:39.638250372 +0200
30080 @@ -0,0 +1,213 @@
30081 +/*
30082 + * Copyright (C) 2005-2013 Junjiro R. Okajima
30083 + *
30084 + * This program, aufs is free software; you can redistribute it and/or modify
30085 + * it under the terms of the GNU General Public License as published by
30086 + * the Free Software Foundation; either version 2 of the License, or
30087 + * (at your option) any later version.
30088 + *
30089 + * This program is distributed in the hope that it will be useful,
30090 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30091 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30092 + * GNU General Public License for more details.
30093 + *
30094 + * You should have received a copy of the GNU General Public License
30095 + * along with this program; if not, write to the Free Software
30096 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30097 + */
30098 +
30099 +/*
30100 + * workqueue for asynchronous/super-io operations
30101 + * todo: try new dredential scheme
30102 + */
30103 +
30104 +#include <linux/module.h>
30105 +#include "aufs.h"
30106 +
30107 +/* internal workqueue named AUFS_WKQ_NAME */
30108 +
30109 +static struct workqueue_struct *au_wkq;
30110 +
30111 +struct au_wkinfo {
30112 +       struct work_struct wk;
30113 +       struct kobject *kobj;
30114 +
30115 +       unsigned int flags; /* see wkq.h */
30116 +
30117 +       au_wkq_func_t func;
30118 +       void *args;
30119 +
30120 +       struct completion *comp;
30121 +};
30122 +
30123 +/* ---------------------------------------------------------------------- */
30124 +
30125 +static void wkq_func(struct work_struct *wk)
30126 +{
30127 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
30128 +
30129 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
30130 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
30131 +
30132 +       wkinfo->func(wkinfo->args);
30133 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
30134 +               complete(wkinfo->comp);
30135 +       else {
30136 +               kobject_put(wkinfo->kobj);
30137 +               module_put(THIS_MODULE); /* todo: ?? */
30138 +               kfree(wkinfo);
30139 +       }
30140 +}
30141 +
30142 +/*
30143 + * Since struct completion is large, try allocating it dynamically.
30144 + */
30145 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
30146 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
30147 +
30148 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
30149 +{
30150 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
30151 +       if (*comp) {
30152 +               init_completion(*comp);
30153 +               wkinfo->comp = *comp;
30154 +               return 0;
30155 +       }
30156 +       return -ENOMEM;
30157 +}
30158 +
30159 +static void au_wkq_comp_free(struct completion *comp)
30160 +{
30161 +       kfree(comp);
30162 +}
30163 +
30164 +#else
30165 +
30166 +/* no braces */
30167 +#define AuWkqCompDeclare(name) \
30168 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
30169 +       struct completion *comp = &_ ## name
30170 +
30171 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
30172 +{
30173 +       wkinfo->comp = *comp;
30174 +       return 0;
30175 +}
30176 +
30177 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
30178 +{
30179 +       /* empty */
30180 +}
30181 +#endif /* 4KSTACKS */
30182 +
30183 +static void au_wkq_run(struct au_wkinfo *wkinfo)
30184 +{
30185 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
30186 +               if (au_wkq_test()) {
30187 +                       AuWarn1("wkq from wkq, due to a dead dir by UDBA?\n");
30188 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
30189 +               }
30190 +       } else
30191 +               au_dbg_verify_kthread();
30192 +
30193 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
30194 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
30195 +               queue_work(au_wkq, &wkinfo->wk);
30196 +       } else {
30197 +               INIT_WORK(&wkinfo->wk, wkq_func);
30198 +               schedule_work(&wkinfo->wk);
30199 +       }
30200 +}
30201 +
30202 +/*
30203 + * Be careful. It is easy to make deadlock happen.
30204 + * processA: lock, wkq and wait
30205 + * processB: wkq and wait, lock in wkq
30206 + * --> deadlock
30207 + */
30208 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
30209 +{
30210 +       int err;
30211 +       AuWkqCompDeclare(comp);
30212 +       struct au_wkinfo wkinfo = {
30213 +               .flags  = flags,
30214 +               .func   = func,
30215 +               .args   = args
30216 +       };
30217 +
30218 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
30219 +       if (!err) {
30220 +               au_wkq_run(&wkinfo);
30221 +               /* no timeout, no interrupt */
30222 +               wait_for_completion(wkinfo.comp);
30223 +               au_wkq_comp_free(comp);
30224 +               destroy_work_on_stack(&wkinfo.wk);
30225 +       }
30226 +
30227 +       return err;
30228 +
30229 +}
30230 +
30231 +/*
30232 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
30233 + * problem in a concurrent umounting.
30234 + */
30235 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
30236 +                 unsigned int flags)
30237 +{
30238 +       int err;
30239 +       struct au_wkinfo *wkinfo;
30240 +
30241 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
30242 +
30243 +       /*
30244 +        * wkq_func() must free this wkinfo.
30245 +        * it highly depends upon the implementation of workqueue.
30246 +        */
30247 +       err = 0;
30248 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
30249 +       if (wkinfo) {
30250 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
30251 +               wkinfo->flags = flags & ~AuWkq_WAIT;
30252 +               wkinfo->func = func;
30253 +               wkinfo->args = args;
30254 +               wkinfo->comp = NULL;
30255 +               kobject_get(wkinfo->kobj);
30256 +               __module_get(THIS_MODULE); /* todo: ?? */
30257 +
30258 +               au_wkq_run(wkinfo);
30259 +       } else {
30260 +               err = -ENOMEM;
30261 +               au_nwt_done(&au_sbi(sb)->si_nowait);
30262 +       }
30263 +
30264 +       return err;
30265 +}
30266 +
30267 +/* ---------------------------------------------------------------------- */
30268 +
30269 +void au_nwt_init(struct au_nowait_tasks *nwt)
30270 +{
30271 +       atomic_set(&nwt->nw_len, 0);
30272 +       /* smp_mb(); */ /* atomic_set */
30273 +       init_waitqueue_head(&nwt->nw_wq);
30274 +}
30275 +
30276 +void au_wkq_fin(void)
30277 +{
30278 +       destroy_workqueue(au_wkq);
30279 +}
30280 +
30281 +int __init au_wkq_init(void)
30282 +{
30283 +       int err;
30284 +
30285 +       err = 0;
30286 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
30287 +       if (IS_ERR(au_wkq))
30288 +               err = PTR_ERR(au_wkq);
30289 +       else if (!au_wkq)
30290 +               err = -ENOMEM;
30291 +
30292 +       return err;
30293 +}
30294 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
30295 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
30296 +++ linux/fs/aufs/wkq.h 2013-07-06 13:20:47.760198800 +0200
30297 @@ -0,0 +1,92 @@
30298 +/*
30299 + * Copyright (C) 2005-2013 Junjiro R. Okajima
30300 + *
30301 + * This program, aufs is free software; you can redistribute it and/or modify
30302 + * it under the terms of the GNU General Public License as published by
30303 + * the Free Software Foundation; either version 2 of the License, or
30304 + * (at your option) any later version.
30305 + *
30306 + * This program is distributed in the hope that it will be useful,
30307 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30308 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30309 + * GNU General Public License for more details.
30310 + *
30311 + * You should have received a copy of the GNU General Public License
30312 + * along with this program; if not, write to the Free Software
30313 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30314 + */
30315 +
30316 +/*
30317 + * workqueue for asynchronous/super-io operations
30318 + * todo: try new credentials management scheme
30319 + */
30320 +
30321 +#ifndef __AUFS_WKQ_H__
30322 +#define __AUFS_WKQ_H__
30323 +
30324 +#ifdef __KERNEL__
30325 +
30326 +struct super_block;
30327 +
30328 +/* ---------------------------------------------------------------------- */
30329 +
30330 +/*
30331 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
30332 + */
30333 +struct au_nowait_tasks {
30334 +       atomic_t                nw_len;
30335 +       wait_queue_head_t       nw_wq;
30336 +};
30337 +
30338 +/* ---------------------------------------------------------------------- */
30339 +
30340 +typedef void (*au_wkq_func_t)(void *args);
30341 +
30342 +/* wkq flags */
30343 +#define AuWkq_WAIT     1
30344 +#define AuWkq_NEST     (1 << 1)
30345 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
30346 +#define au_fset_wkq(flags, name) \
30347 +       do { (flags) |= AuWkq_##name; } while (0)
30348 +#define au_fclr_wkq(flags, name) \
30349 +       do { (flags) &= ~AuWkq_##name; } while (0)
30350 +
30351 +#ifndef CONFIG_AUFS_HNOTIFY
30352 +#undef AuWkq_NEST
30353 +#define AuWkq_NEST     0
30354 +#endif
30355 +
30356 +/* wkq.c */
30357 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
30358 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
30359 +                 unsigned int flags);
30360 +void au_nwt_init(struct au_nowait_tasks *nwt);
30361 +int __init au_wkq_init(void);
30362 +void au_wkq_fin(void);
30363 +
30364 +/* ---------------------------------------------------------------------- */
30365 +
30366 +static inline int au_wkq_test(void)
30367 +{
30368 +       return current->flags & PF_WQ_WORKER;
30369 +}
30370 +
30371 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
30372 +{
30373 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
30374 +}
30375 +
30376 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
30377 +{
30378 +       if (atomic_dec_and_test(&nwt->nw_len))
30379 +               wake_up_all(&nwt->nw_wq);
30380 +}
30381 +
30382 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
30383 +{
30384 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
30385 +       return 0;
30386 +}
30387 +
30388 +#endif /* __KERNEL__ */
30389 +#endif /* __AUFS_WKQ_H__ */
30390 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
30391 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
30392 +++ linux/fs/aufs/xino.c        2013-07-06 13:20:47.760198800 +0200
30393 @@ -0,0 +1,1264 @@
30394 +/*
30395 + * Copyright (C) 2005-2013 Junjiro R. Okajima
30396 + *
30397 + * This program, aufs is free software; you can redistribute it and/or modify
30398 + * it under the terms of the GNU General Public License as published by
30399 + * the Free Software Foundation; either version 2 of the License, or
30400 + * (at your option) any later version.
30401 + *
30402 + * This program is distributed in the hope that it will be useful,
30403 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30404 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30405 + * GNU General Public License for more details.
30406 + *
30407 + * You should have received a copy of the GNU General Public License
30408 + * along with this program; if not, write to the Free Software
30409 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30410 + */
30411 +
30412 +/*
30413 + * external inode number translation table and bitmap
30414 + */
30415 +
30416 +#include <linux/seq_file.h>
30417 +#include "aufs.h"
30418 +
30419 +/* todo: unnecessary to support mmap_sem since kernel-space? */
30420 +ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
30421 +                  loff_t *pos)
30422 +{
30423 +       ssize_t err;
30424 +       mm_segment_t oldfs;
30425 +       union {
30426 +               void *k;
30427 +               char __user *u;
30428 +       } buf;
30429 +
30430 +       buf.k = kbuf;
30431 +       oldfs = get_fs();
30432 +       set_fs(KERNEL_DS);
30433 +       do {
30434 +               /* todo: signal_pending? */
30435 +               err = func(file, buf.u, size, pos);
30436 +       } while (err == -EAGAIN || err == -EINTR);
30437 +       set_fs(oldfs);
30438 +
30439 +#if 0 /* reserved for future use */
30440 +       if (err > 0)
30441 +               fsnotify_access(file->f_dentry);
30442 +#endif
30443 +
30444 +       return err;
30445 +}
30446 +
30447 +/* ---------------------------------------------------------------------- */
30448 +
30449 +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
30450 +                             size_t size, loff_t *pos)
30451 +{
30452 +       ssize_t err;
30453 +       mm_segment_t oldfs;
30454 +       union {
30455 +               void *k;
30456 +               const char __user *u;
30457 +       } buf;
30458 +
30459 +       buf.k = kbuf;
30460 +       oldfs = get_fs();
30461 +       set_fs(KERNEL_DS);
30462 +       do {
30463 +               /* todo: signal_pending? */
30464 +               err = func(file, buf.u, size, pos);
30465 +       } while (err == -EAGAIN || err == -EINTR);
30466 +       set_fs(oldfs);
30467 +
30468 +#if 0 /* reserved for future use */
30469 +       if (err > 0)
30470 +               fsnotify_modify(file->f_dentry);
30471 +#endif
30472 +
30473 +       return err;
30474 +}
30475 +
30476 +struct do_xino_fwrite_args {
30477 +       ssize_t *errp;
30478 +       au_writef_t func;
30479 +       struct file *file;
30480 +       void *buf;
30481 +       size_t size;
30482 +       loff_t *pos;
30483 +};
30484 +
30485 +static void call_do_xino_fwrite(void *args)
30486 +{
30487 +       struct do_xino_fwrite_args *a = args;
30488 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
30489 +}
30490 +
30491 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
30492 +                   loff_t *pos)
30493 +{
30494 +       ssize_t err;
30495 +
30496 +       /* todo: signal block and no wkq? */
30497 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
30498 +               lockdep_off();
30499 +               err = do_xino_fwrite(func, file, buf, size, pos);
30500 +               lockdep_on();
30501 +       } else {
30502 +               /*
30503 +                * it breaks RLIMIT_FSIZE and normal user's limit,
30504 +                * users should care about quota and real 'filesystem full.'
30505 +                */
30506 +               int wkq_err;
30507 +               struct do_xino_fwrite_args args = {
30508 +                       .errp   = &err,
30509 +                       .func   = func,
30510 +                       .file   = file,
30511 +                       .buf    = buf,
30512 +                       .size   = size,
30513 +                       .pos    = pos
30514 +               };
30515 +
30516 +               wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
30517 +               if (unlikely(wkq_err))
30518 +                       err = wkq_err;
30519 +       }
30520 +
30521 +       return err;
30522 +}
30523 +
30524 +/* ---------------------------------------------------------------------- */
30525 +
30526 +/*
30527 + * create a new xinofile at the same place/path as @base_file.
30528 + */
30529 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
30530 +{
30531 +       struct file *file;
30532 +       struct dentry *base, *parent;
30533 +       struct inode *dir;
30534 +       struct qstr *name;
30535 +       struct path path;
30536 +       int err;
30537 +
30538 +       base = base_file->f_dentry;
30539 +       parent = base->d_parent; /* dir inode is locked */
30540 +       dir = parent->d_inode;
30541 +       IMustLock(dir);
30542 +
30543 +       file = ERR_PTR(-EINVAL);
30544 +       name = &base->d_name;
30545 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
30546 +       if (IS_ERR(path.dentry)) {
30547 +               file = (void *)path.dentry;
30548 +               pr_err("%.*s lookup err %ld\n",
30549 +                      AuLNPair(name), PTR_ERR(path.dentry));
30550 +               goto out;
30551 +       }
30552 +
30553 +       /* no need to mnt_want_write() since we call dentry_open() later */
30554 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
30555 +       if (unlikely(err)) {
30556 +               file = ERR_PTR(err);
30557 +               pr_err("%.*s create err %d\n", AuLNPair(name), err);
30558 +               goto out_dput;
30559 +       }
30560 +
30561 +       path.mnt = base_file->f_path.mnt;
30562 +       file = vfsub_dentry_open(&path,
30563 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
30564 +                                /* | __FMODE_NONOTIFY */);
30565 +       if (IS_ERR(file)) {
30566 +               pr_err("%.*s open err %ld\n", AuLNPair(name), PTR_ERR(file));
30567 +               goto out_dput;
30568 +       }
30569 +
30570 +       err = vfsub_unlink(dir, &file->f_path, /*force*/0);
30571 +       if (unlikely(err)) {
30572 +               pr_err("%.*s unlink err %d\n", AuLNPair(name), err);
30573 +               goto out_fput;
30574 +       }
30575 +
30576 +       if (copy_src) {
30577 +               /* no one can touch copy_src xino */
30578 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
30579 +               if (unlikely(err)) {
30580 +                       pr_err("%.*s copy err %d\n", AuLNPair(name), err);
30581 +                       goto out_fput;
30582 +               }
30583 +       }
30584 +       goto out_dput; /* success */
30585 +
30586 +out_fput:
30587 +       fput(file);
30588 +       file = ERR_PTR(err);
30589 +out_dput:
30590 +       dput(path.dentry);
30591 +out:
30592 +       return file;
30593 +}
30594 +
30595 +struct au_xino_lock_dir {
30596 +       struct au_hinode *hdir;
30597 +       struct dentry *parent;
30598 +       struct mutex *mtx;
30599 +};
30600 +
30601 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
30602 +                            struct au_xino_lock_dir *ldir)
30603 +{
30604 +       aufs_bindex_t brid, bindex;
30605 +
30606 +       ldir->hdir = NULL;
30607 +       bindex = -1;
30608 +       brid = au_xino_brid(sb);
30609 +       if (brid >= 0)
30610 +               bindex = au_br_index(sb, brid);
30611 +       if (bindex >= 0) {
30612 +               ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
30613 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
30614 +       } else {
30615 +               ldir->parent = dget_parent(xino->f_dentry);
30616 +               ldir->mtx = &ldir->parent->d_inode->i_mutex;
30617 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
30618 +       }
30619 +}
30620 +
30621 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
30622 +{
30623 +       if (ldir->hdir)
30624 +               au_hn_imtx_unlock(ldir->hdir);
30625 +       else {
30626 +               mutex_unlock(ldir->mtx);
30627 +               dput(ldir->parent);
30628 +       }
30629 +}
30630 +
30631 +/* ---------------------------------------------------------------------- */
30632 +
30633 +/* trucate xino files asynchronously */
30634 +
30635 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
30636 +{
30637 +       int err;
30638 +       aufs_bindex_t bi, bend;
30639 +       struct au_branch *br;
30640 +       struct file *new_xino, *file;
30641 +       struct super_block *h_sb;
30642 +       struct au_xino_lock_dir ldir;
30643 +
30644 +       err = -EINVAL;
30645 +       bend = au_sbend(sb);
30646 +       if (unlikely(bindex < 0 || bend < bindex))
30647 +               goto out;
30648 +       br = au_sbr(sb, bindex);
30649 +       file = br->br_xino.xi_file;
30650 +       if (!file)
30651 +               goto out;
30652 +
30653 +       au_xino_lock_dir(sb, file, &ldir);
30654 +       /* mnt_want_write() is unnecessary here */
30655 +       new_xino = au_xino_create2(file, file);
30656 +       au_xino_unlock_dir(&ldir);
30657 +       err = PTR_ERR(new_xino);
30658 +       if (IS_ERR(new_xino))
30659 +               goto out;
30660 +       err = 0;
30661 +       fput(file);
30662 +       br->br_xino.xi_file = new_xino;
30663 +
30664 +       h_sb = au_br_sb(br);
30665 +       for (bi = 0; bi <= bend; bi++) {
30666 +               if (unlikely(bi == bindex))
30667 +                       continue;
30668 +               br = au_sbr(sb, bi);
30669 +               if (au_br_sb(br) != h_sb)
30670 +                       continue;
30671 +
30672 +               fput(br->br_xino.xi_file);
30673 +               br->br_xino.xi_file = new_xino;
30674 +               get_file(new_xino);
30675 +       }
30676 +
30677 +out:
30678 +       return err;
30679 +}
30680 +
30681 +struct xino_do_trunc_args {
30682 +       struct super_block *sb;
30683 +       struct au_branch *br;
30684 +};
30685 +
30686 +static void xino_do_trunc(void *_args)
30687 +{
30688 +       struct xino_do_trunc_args *args = _args;
30689 +       struct super_block *sb;
30690 +       struct au_branch *br;
30691 +       struct inode *dir;
30692 +       int err;
30693 +       aufs_bindex_t bindex;
30694 +
30695 +       err = 0;
30696 +       sb = args->sb;
30697 +       dir = sb->s_root->d_inode;
30698 +       br = args->br;
30699 +
30700 +       si_noflush_write_lock(sb);
30701 +       ii_read_lock_parent(dir);
30702 +       bindex = au_br_index(sb, br->br_id);
30703 +       err = au_xino_trunc(sb, bindex);
30704 +       if (!err
30705 +           && file_inode(br->br_xino.xi_file)->i_blocks
30706 +           >= br->br_xino_upper)
30707 +               br->br_xino_upper += AUFS_XINO_TRUNC_STEP;
30708 +
30709 +       ii_read_unlock(dir);
30710 +       if (unlikely(err))
30711 +               pr_warn("err b%d, upper %llu, (%d)\n",
30712 +                       bindex, (unsigned long long)br->br_xino_upper, err);
30713 +       atomic_dec(&br->br_xino_running);
30714 +       atomic_dec(&br->br_count);
30715 +       si_write_unlock(sb);
30716 +       au_nwt_done(&au_sbi(sb)->si_nowait);
30717 +       kfree(args);
30718 +}
30719 +
30720 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
30721 +{
30722 +       struct xino_do_trunc_args *args;
30723 +       int wkq_err;
30724 +
30725 +       if (file_inode(br->br_xino.xi_file)->i_blocks
30726 +           < br->br_xino_upper)
30727 +               return;
30728 +
30729 +       if (atomic_inc_return(&br->br_xino_running) > 1)
30730 +               goto out;
30731 +
30732 +       /* lock and kfree() will be called in trunc_xino() */
30733 +       args = kmalloc(sizeof(*args), GFP_NOFS);
30734 +       if (unlikely(!args)) {
30735 +               AuErr1("no memory\n");
30736 +               goto out_args;
30737 +       }
30738 +
30739 +       atomic_inc(&br->br_count);
30740 +       args->sb = sb;
30741 +       args->br = br;
30742 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
30743 +       if (!wkq_err)
30744 +               return; /* success */
30745 +
30746 +       pr_err("wkq %d\n", wkq_err);
30747 +       atomic_dec(&br->br_count);
30748 +
30749 +out_args:
30750 +       kfree(args);
30751 +out:
30752 +       atomic_dec(&br->br_xino_running);
30753 +}
30754 +
30755 +/* ---------------------------------------------------------------------- */
30756 +
30757 +static int au_xino_do_write(au_writef_t write, struct file *file,
30758 +                           ino_t h_ino, ino_t ino)
30759 +{
30760 +       loff_t pos;
30761 +       ssize_t sz;
30762 +
30763 +       pos = h_ino;
30764 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
30765 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
30766 +               return -EFBIG;
30767 +       }
30768 +       pos *= sizeof(ino);
30769 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
30770 +       if (sz == sizeof(ino))
30771 +               return 0; /* success */
30772 +
30773 +       AuIOErr("write failed (%zd)\n", sz);
30774 +       return -EIO;
30775 +}
30776 +
30777 +/*
30778 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
30779 + * at the position of @h_ino.
30780 + * even if @ino is zero, it is written to the xinofile and means no entry.
30781 + * if the size of the xino file on a specific filesystem exceeds the watermark,
30782 + * try truncating it.
30783 + */
30784 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
30785 +                 ino_t ino)
30786 +{
30787 +       int err;
30788 +       unsigned int mnt_flags;
30789 +       struct au_branch *br;
30790 +
30791 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
30792 +                    || ((loff_t)-1) > 0);
30793 +       SiMustAnyLock(sb);
30794 +
30795 +       mnt_flags = au_mntflags(sb);
30796 +       if (!au_opt_test(mnt_flags, XINO))
30797 +               return 0;
30798 +
30799 +       br = au_sbr(sb, bindex);
30800 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
30801 +                              h_ino, ino);
30802 +       if (!err) {
30803 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
30804 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
30805 +                       xino_try_trunc(sb, br);
30806 +               return 0; /* success */
30807 +       }
30808 +
30809 +       AuIOErr("write failed (%d)\n", err);
30810 +       return -EIO;
30811 +}
30812 +
30813 +/* ---------------------------------------------------------------------- */
30814 +
30815 +/* aufs inode number bitmap */
30816 +
30817 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
30818 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
30819 +{
30820 +       ino_t ino;
30821 +
30822 +       AuDebugOn(bit < 0 || page_bits <= bit);
30823 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
30824 +       return ino;
30825 +}
30826 +
30827 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
30828 +{
30829 +       AuDebugOn(ino < AUFS_FIRST_INO);
30830 +       ino -= AUFS_FIRST_INO;
30831 +       *pindex = ino / page_bits;
30832 +       *bit = ino % page_bits;
30833 +}
30834 +
30835 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
30836 +{
30837 +       int err;
30838 +       loff_t pos;
30839 +       ssize_t sz;
30840 +       struct au_sbinfo *sbinfo;
30841 +       struct file *xib;
30842 +       unsigned long *p;
30843 +
30844 +       sbinfo = au_sbi(sb);
30845 +       MtxMustLock(&sbinfo->si_xib_mtx);
30846 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
30847 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
30848 +
30849 +       if (pindex == sbinfo->si_xib_last_pindex)
30850 +               return 0;
30851 +
30852 +       xib = sbinfo->si_xib;
30853 +       p = sbinfo->si_xib_buf;
30854 +       pos = sbinfo->si_xib_last_pindex;
30855 +       pos *= PAGE_SIZE;
30856 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
30857 +       if (unlikely(sz != PAGE_SIZE))
30858 +               goto out;
30859 +
30860 +       pos = pindex;
30861 +       pos *= PAGE_SIZE;
30862 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
30863 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
30864 +       else {
30865 +               memset(p, 0, PAGE_SIZE);
30866 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
30867 +       }
30868 +       if (sz == PAGE_SIZE) {
30869 +               sbinfo->si_xib_last_pindex = pindex;
30870 +               return 0; /* success */
30871 +       }
30872 +
30873 +out:
30874 +       AuIOErr1("write failed (%zd)\n", sz);
30875 +       err = sz;
30876 +       if (sz >= 0)
30877 +               err = -EIO;
30878 +       return err;
30879 +}
30880 +
30881 +/* ---------------------------------------------------------------------- */
30882 +
30883 +static void au_xib_clear_bit(struct inode *inode)
30884 +{
30885 +       int err, bit;
30886 +       unsigned long pindex;
30887 +       struct super_block *sb;
30888 +       struct au_sbinfo *sbinfo;
30889 +
30890 +       AuDebugOn(inode->i_nlink);
30891 +
30892 +       sb = inode->i_sb;
30893 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
30894 +       AuDebugOn(page_bits <= bit);
30895 +       sbinfo = au_sbi(sb);
30896 +       mutex_lock(&sbinfo->si_xib_mtx);
30897 +       err = xib_pindex(sb, pindex);
30898 +       if (!err) {
30899 +               clear_bit(bit, sbinfo->si_xib_buf);
30900 +               sbinfo->si_xib_next_bit = bit;
30901 +       }
30902 +       mutex_unlock(&sbinfo->si_xib_mtx);
30903 +}
30904 +
30905 +/* for s_op->delete_inode() */
30906 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
30907 +{
30908 +       int err;
30909 +       unsigned int mnt_flags;
30910 +       aufs_bindex_t bindex, bend, bi;
30911 +       unsigned char try_trunc;
30912 +       struct au_iinfo *iinfo;
30913 +       struct super_block *sb;
30914 +       struct au_hinode *hi;
30915 +       struct inode *h_inode;
30916 +       struct au_branch *br;
30917 +       au_writef_t xwrite;
30918 +
30919 +       sb = inode->i_sb;
30920 +       mnt_flags = au_mntflags(sb);
30921 +       if (!au_opt_test(mnt_flags, XINO)
30922 +           || inode->i_ino == AUFS_ROOT_INO)
30923 +               return;
30924 +
30925 +       if (unlinked) {
30926 +               au_xigen_inc(inode);
30927 +               au_xib_clear_bit(inode);
30928 +       }
30929 +
30930 +       iinfo = au_ii(inode);
30931 +       if (!iinfo)
30932 +               return;
30933 +
30934 +       bindex = iinfo->ii_bstart;
30935 +       if (bindex < 0)
30936 +               return;
30937 +
30938 +       xwrite = au_sbi(sb)->si_xwrite;
30939 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
30940 +       hi = iinfo->ii_hinode + bindex;
30941 +       bend = iinfo->ii_bend;
30942 +       for (; bindex <= bend; bindex++, hi++) {
30943 +               h_inode = hi->hi_inode;
30944 +               if (!h_inode
30945 +                   || (!unlinked && h_inode->i_nlink))
30946 +                       continue;
30947 +
30948 +               /* inode may not be revalidated */
30949 +               bi = au_br_index(sb, hi->hi_id);
30950 +               if (bi < 0)
30951 +                       continue;
30952 +
30953 +               br = au_sbr(sb, bi);
30954 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
30955 +                                      h_inode->i_ino, /*ino*/0);
30956 +               if (!err && try_trunc
30957 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
30958 +                       xino_try_trunc(sb, br);
30959 +       }
30960 +}
30961 +
30962 +/* get an unused inode number from bitmap */
30963 +ino_t au_xino_new_ino(struct super_block *sb)
30964 +{
30965 +       ino_t ino;
30966 +       unsigned long *p, pindex, ul, pend;
30967 +       struct au_sbinfo *sbinfo;
30968 +       struct file *file;
30969 +       int free_bit, err;
30970 +
30971 +       if (!au_opt_test(au_mntflags(sb), XINO))
30972 +               return iunique(sb, AUFS_FIRST_INO);
30973 +
30974 +       sbinfo = au_sbi(sb);
30975 +       mutex_lock(&sbinfo->si_xib_mtx);
30976 +       p = sbinfo->si_xib_buf;
30977 +       free_bit = sbinfo->si_xib_next_bit;
30978 +       if (free_bit < page_bits && !test_bit(free_bit, p))
30979 +               goto out; /* success */
30980 +       free_bit = find_first_zero_bit(p, page_bits);
30981 +       if (free_bit < page_bits)
30982 +               goto out; /* success */
30983 +
30984 +       pindex = sbinfo->si_xib_last_pindex;
30985 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
30986 +               err = xib_pindex(sb, ul);
30987 +               if (unlikely(err))
30988 +                       goto out_err;
30989 +               free_bit = find_first_zero_bit(p, page_bits);
30990 +               if (free_bit < page_bits)
30991 +                       goto out; /* success */
30992 +       }
30993 +
30994 +       file = sbinfo->si_xib;
30995 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
30996 +       for (ul = pindex + 1; ul <= pend; ul++) {
30997 +               err = xib_pindex(sb, ul);
30998 +               if (unlikely(err))
30999 +                       goto out_err;
31000 +               free_bit = find_first_zero_bit(p, page_bits);
31001 +               if (free_bit < page_bits)
31002 +                       goto out; /* success */
31003 +       }
31004 +       BUG();
31005 +
31006 +out:
31007 +       set_bit(free_bit, p);
31008 +       sbinfo->si_xib_next_bit = free_bit + 1;
31009 +       pindex = sbinfo->si_xib_last_pindex;
31010 +       mutex_unlock(&sbinfo->si_xib_mtx);
31011 +       ino = xib_calc_ino(pindex, free_bit);
31012 +       AuDbg("i%lu\n", (unsigned long)ino);
31013 +       return ino;
31014 +out_err:
31015 +       mutex_unlock(&sbinfo->si_xib_mtx);
31016 +       AuDbg("i0\n");
31017 +       return 0;
31018 +}
31019 +
31020 +/*
31021 + * read @ino from xinofile for the specified branch{@sb, @bindex}
31022 + * at the position of @h_ino.
31023 + * if @ino does not exist and @do_new is true, get new one.
31024 + */
31025 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
31026 +                ino_t *ino)
31027 +{
31028 +       int err;
31029 +       ssize_t sz;
31030 +       loff_t pos;
31031 +       struct file *file;
31032 +       struct au_sbinfo *sbinfo;
31033 +
31034 +       *ino = 0;
31035 +       if (!au_opt_test(au_mntflags(sb), XINO))
31036 +               return 0; /* no xino */
31037 +
31038 +       err = 0;
31039 +       sbinfo = au_sbi(sb);
31040 +       pos = h_ino;
31041 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
31042 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
31043 +               return -EFBIG;
31044 +       }
31045 +       pos *= sizeof(*ino);
31046 +
31047 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
31048 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
31049 +               return 0; /* no ino */
31050 +
31051 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
31052 +       if (sz == sizeof(*ino))
31053 +               return 0; /* success */
31054 +
31055 +       err = sz;
31056 +       if (unlikely(sz >= 0)) {
31057 +               err = -EIO;
31058 +               AuIOErr("xino read error (%zd)\n", sz);
31059 +       }
31060 +
31061 +       return err;
31062 +}
31063 +
31064 +/* ---------------------------------------------------------------------- */
31065 +
31066 +/* create and set a new xino file */
31067 +
31068 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
31069 +{
31070 +       struct file *file;
31071 +       struct dentry *h_parent, *d;
31072 +       struct inode *h_dir;
31073 +       int err;
31074 +
31075 +       /*
31076 +        * at mount-time, and the xino file is the default path,
31077 +        * hnotify is disabled so we have no notify events to ignore.
31078 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
31079 +        */
31080 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
31081 +                              /* | __FMODE_NONOTIFY */,
31082 +                              S_IRUGO | S_IWUGO);
31083 +       if (IS_ERR(file)) {
31084 +               if (!silent)
31085 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
31086 +               return file;
31087 +       }
31088 +
31089 +       /* keep file count */
31090 +       h_parent = dget_parent(file->f_dentry);
31091 +       h_dir = h_parent->d_inode;
31092 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
31093 +       /* mnt_want_write() is unnecessary here */
31094 +       err = vfsub_unlink(h_dir, &file->f_path, /*force*/0);
31095 +       mutex_unlock(&h_dir->i_mutex);
31096 +       dput(h_parent);
31097 +       if (unlikely(err)) {
31098 +               if (!silent)
31099 +                       pr_err("unlink %s(%d)\n", fname, err);
31100 +               goto out;
31101 +       }
31102 +
31103 +       err = -EINVAL;
31104 +       d = file->f_dentry;
31105 +       if (unlikely(sb == d->d_sb)) {
31106 +               if (!silent)
31107 +                       pr_err("%s must be outside\n", fname);
31108 +               goto out;
31109 +       }
31110 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
31111 +               if (!silent)
31112 +                       pr_err("xino doesn't support %s(%s)\n",
31113 +                              fname, au_sbtype(d->d_sb));
31114 +               goto out;
31115 +       }
31116 +       return file; /* success */
31117 +
31118 +out:
31119 +       fput(file);
31120 +       file = ERR_PTR(err);
31121 +       return file;
31122 +}
31123 +
31124 +/*
31125 + * find another branch who is on the same filesystem of the specified
31126 + * branch{@btgt}. search until @bend.
31127 + */
31128 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
31129 +                       aufs_bindex_t bend)
31130 +{
31131 +       aufs_bindex_t bindex;
31132 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
31133 +
31134 +       for (bindex = 0; bindex < btgt; bindex++)
31135 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
31136 +                       return bindex;
31137 +       for (bindex++; bindex <= bend; bindex++)
31138 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
31139 +                       return bindex;
31140 +       return -1;
31141 +}
31142 +
31143 +/* ---------------------------------------------------------------------- */
31144 +
31145 +/*
31146 + * initialize the xinofile for the specified branch @br
31147 + * at the place/path where @base_file indicates.
31148 + * test whether another branch is on the same filesystem or not,
31149 + * if @do_test is true.
31150 + */
31151 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
31152 +              struct file *base_file, int do_test)
31153 +{
31154 +       int err;
31155 +       ino_t ino;
31156 +       aufs_bindex_t bend, bindex;
31157 +       struct au_branch *shared_br, *b;
31158 +       struct file *file;
31159 +       struct super_block *tgt_sb;
31160 +
31161 +       shared_br = NULL;
31162 +       bend = au_sbend(sb);
31163 +       if (do_test) {
31164 +               tgt_sb = au_br_sb(br);
31165 +               for (bindex = 0; bindex <= bend; bindex++) {
31166 +                       b = au_sbr(sb, bindex);
31167 +                       if (tgt_sb == au_br_sb(b)) {
31168 +                               shared_br = b;
31169 +                               break;
31170 +                       }
31171 +               }
31172 +       }
31173 +
31174 +       if (!shared_br || !shared_br->br_xino.xi_file) {
31175 +               struct au_xino_lock_dir ldir;
31176 +
31177 +               au_xino_lock_dir(sb, base_file, &ldir);
31178 +               /* mnt_want_write() is unnecessary here */
31179 +               file = au_xino_create2(base_file, NULL);
31180 +               au_xino_unlock_dir(&ldir);
31181 +               err = PTR_ERR(file);
31182 +               if (IS_ERR(file))
31183 +                       goto out;
31184 +               br->br_xino.xi_file = file;
31185 +       } else {
31186 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
31187 +               get_file(br->br_xino.xi_file);
31188 +       }
31189 +
31190 +       ino = AUFS_ROOT_INO;
31191 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
31192 +                              h_ino, ino);
31193 +       if (unlikely(err)) {
31194 +               fput(br->br_xino.xi_file);
31195 +               br->br_xino.xi_file = NULL;
31196 +       }
31197 +
31198 +out:
31199 +       return err;
31200 +}
31201 +
31202 +/* ---------------------------------------------------------------------- */
31203 +
31204 +/* trucate a xino bitmap file */
31205 +
31206 +/* todo: slow */
31207 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
31208 +{
31209 +       int err, bit;
31210 +       ssize_t sz;
31211 +       unsigned long pindex;
31212 +       loff_t pos, pend;
31213 +       struct au_sbinfo *sbinfo;
31214 +       au_readf_t func;
31215 +       ino_t *ino;
31216 +       unsigned long *p;
31217 +
31218 +       err = 0;
31219 +       sbinfo = au_sbi(sb);
31220 +       MtxMustLock(&sbinfo->si_xib_mtx);
31221 +       p = sbinfo->si_xib_buf;
31222 +       func = sbinfo->si_xread;
31223 +       pend = vfsub_f_size_read(file);
31224 +       pos = 0;
31225 +       while (pos < pend) {
31226 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
31227 +               err = sz;
31228 +               if (unlikely(sz <= 0))
31229 +                       goto out;
31230 +
31231 +               err = 0;
31232 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
31233 +                       if (unlikely(*ino < AUFS_FIRST_INO))
31234 +                               continue;
31235 +
31236 +                       xib_calc_bit(*ino, &pindex, &bit);
31237 +                       AuDebugOn(page_bits <= bit);
31238 +                       err = xib_pindex(sb, pindex);
31239 +                       if (!err)
31240 +                               set_bit(bit, p);
31241 +                       else
31242 +                               goto out;
31243 +               }
31244 +       }
31245 +
31246 +out:
31247 +       return err;
31248 +}
31249 +
31250 +static int xib_restore(struct super_block *sb)
31251 +{
31252 +       int err;
31253 +       aufs_bindex_t bindex, bend;
31254 +       void *page;
31255 +
31256 +       err = -ENOMEM;
31257 +       page = (void *)__get_free_page(GFP_NOFS);
31258 +       if (unlikely(!page))
31259 +               goto out;
31260 +
31261 +       err = 0;
31262 +       bend = au_sbend(sb);
31263 +       for (bindex = 0; !err && bindex <= bend; bindex++)
31264 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
31265 +                       err = do_xib_restore
31266 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
31267 +               else
31268 +                       AuDbg("b%d\n", bindex);
31269 +       free_page((unsigned long)page);
31270 +
31271 +out:
31272 +       return err;
31273 +}
31274 +
31275 +int au_xib_trunc(struct super_block *sb)
31276 +{
31277 +       int err;
31278 +       ssize_t sz;
31279 +       loff_t pos;
31280 +       struct au_xino_lock_dir ldir;
31281 +       struct au_sbinfo *sbinfo;
31282 +       unsigned long *p;
31283 +       struct file *file;
31284 +
31285 +       SiMustWriteLock(sb);
31286 +
31287 +       err = 0;
31288 +       sbinfo = au_sbi(sb);
31289 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
31290 +               goto out;
31291 +
31292 +       file = sbinfo->si_xib;
31293 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
31294 +               goto out;
31295 +
31296 +       au_xino_lock_dir(sb, file, &ldir);
31297 +       /* mnt_want_write() is unnecessary here */
31298 +       file = au_xino_create2(sbinfo->si_xib, NULL);
31299 +       au_xino_unlock_dir(&ldir);
31300 +       err = PTR_ERR(file);
31301 +       if (IS_ERR(file))
31302 +               goto out;
31303 +       fput(sbinfo->si_xib);
31304 +       sbinfo->si_xib = file;
31305 +
31306 +       p = sbinfo->si_xib_buf;
31307 +       memset(p, 0, PAGE_SIZE);
31308 +       pos = 0;
31309 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
31310 +       if (unlikely(sz != PAGE_SIZE)) {
31311 +               err = sz;
31312 +               AuIOErr("err %d\n", err);
31313 +               if (sz >= 0)
31314 +                       err = -EIO;
31315 +               goto out;
31316 +       }
31317 +
31318 +       mutex_lock(&sbinfo->si_xib_mtx);
31319 +       /* mnt_want_write() is unnecessary here */
31320 +       err = xib_restore(sb);
31321 +       mutex_unlock(&sbinfo->si_xib_mtx);
31322 +
31323 +out:
31324 +       return err;
31325 +}
31326 +
31327 +/* ---------------------------------------------------------------------- */
31328 +
31329 +/*
31330 + * xino mount option handlers
31331 + */
31332 +static au_readf_t find_readf(struct file *h_file)
31333 +{
31334 +       const struct file_operations *fop = h_file->f_op;
31335 +
31336 +       if (fop) {
31337 +               if (fop->read)
31338 +                       return fop->read;
31339 +               if (fop->aio_read)
31340 +                       return do_sync_read;
31341 +       }
31342 +       return ERR_PTR(-ENOSYS);
31343 +}
31344 +
31345 +static au_writef_t find_writef(struct file *h_file)
31346 +{
31347 +       const struct file_operations *fop = h_file->f_op;
31348 +
31349 +       if (fop) {
31350 +               if (fop->write)
31351 +                       return fop->write;
31352 +               if (fop->aio_write)
31353 +                       return do_sync_write;
31354 +       }
31355 +       return ERR_PTR(-ENOSYS);
31356 +}
31357 +
31358 +/* xino bitmap */
31359 +static void xino_clear_xib(struct super_block *sb)
31360 +{
31361 +       struct au_sbinfo *sbinfo;
31362 +
31363 +       SiMustWriteLock(sb);
31364 +
31365 +       sbinfo = au_sbi(sb);
31366 +       sbinfo->si_xread = NULL;
31367 +       sbinfo->si_xwrite = NULL;
31368 +       if (sbinfo->si_xib)
31369 +               fput(sbinfo->si_xib);
31370 +       sbinfo->si_xib = NULL;
31371 +       free_page((unsigned long)sbinfo->si_xib_buf);
31372 +       sbinfo->si_xib_buf = NULL;
31373 +}
31374 +
31375 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
31376 +{
31377 +       int err;
31378 +       loff_t pos;
31379 +       struct au_sbinfo *sbinfo;
31380 +       struct file *file;
31381 +
31382 +       SiMustWriteLock(sb);
31383 +
31384 +       sbinfo = au_sbi(sb);
31385 +       file = au_xino_create2(base, sbinfo->si_xib);
31386 +       err = PTR_ERR(file);
31387 +       if (IS_ERR(file))
31388 +               goto out;
31389 +       if (sbinfo->si_xib)
31390 +               fput(sbinfo->si_xib);
31391 +       sbinfo->si_xib = file;
31392 +       sbinfo->si_xread = find_readf(file);
31393 +       sbinfo->si_xwrite = find_writef(file);
31394 +
31395 +       err = -ENOMEM;
31396 +       if (!sbinfo->si_xib_buf)
31397 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
31398 +       if (unlikely(!sbinfo->si_xib_buf))
31399 +               goto out_unset;
31400 +
31401 +       sbinfo->si_xib_last_pindex = 0;
31402 +       sbinfo->si_xib_next_bit = 0;
31403 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
31404 +               pos = 0;
31405 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
31406 +                                 PAGE_SIZE, &pos);
31407 +               if (unlikely(err != PAGE_SIZE))
31408 +                       goto out_free;
31409 +       }
31410 +       err = 0;
31411 +       goto out; /* success */
31412 +
31413 +out_free:
31414 +       free_page((unsigned long)sbinfo->si_xib_buf);
31415 +       sbinfo->si_xib_buf = NULL;
31416 +       if (err >= 0)
31417 +               err = -EIO;
31418 +out_unset:
31419 +       fput(sbinfo->si_xib);
31420 +       sbinfo->si_xib = NULL;
31421 +       sbinfo->si_xread = NULL;
31422 +       sbinfo->si_xwrite = NULL;
31423 +out:
31424 +       return err;
31425 +}
31426 +
31427 +/* xino for each branch */
31428 +static void xino_clear_br(struct super_block *sb)
31429 +{
31430 +       aufs_bindex_t bindex, bend;
31431 +       struct au_branch *br;
31432 +
31433 +       bend = au_sbend(sb);
31434 +       for (bindex = 0; bindex <= bend; bindex++) {
31435 +               br = au_sbr(sb, bindex);
31436 +               if (!br || !br->br_xino.xi_file)
31437 +                       continue;
31438 +
31439 +               fput(br->br_xino.xi_file);
31440 +               br->br_xino.xi_file = NULL;
31441 +       }
31442 +}
31443 +
31444 +static int au_xino_set_br(struct super_block *sb, struct file *base)
31445 +{
31446 +       int err;
31447 +       ino_t ino;
31448 +       aufs_bindex_t bindex, bend, bshared;
31449 +       struct {
31450 +               struct file *old, *new;
31451 +       } *fpair, *p;
31452 +       struct au_branch *br;
31453 +       struct inode *inode;
31454 +       au_writef_t writef;
31455 +
31456 +       SiMustWriteLock(sb);
31457 +
31458 +       err = -ENOMEM;
31459 +       bend = au_sbend(sb);
31460 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
31461 +       if (unlikely(!fpair))
31462 +               goto out;
31463 +
31464 +       inode = sb->s_root->d_inode;
31465 +       ino = AUFS_ROOT_INO;
31466 +       writef = au_sbi(sb)->si_xwrite;
31467 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
31468 +               br = au_sbr(sb, bindex);
31469 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
31470 +               if (bshared >= 0) {
31471 +                       /* shared xino */
31472 +                       *p = fpair[bshared];
31473 +                       get_file(p->new);
31474 +               }
31475 +
31476 +               if (!p->new) {
31477 +                       /* new xino */
31478 +                       p->old = br->br_xino.xi_file;
31479 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
31480 +                       err = PTR_ERR(p->new);
31481 +                       if (IS_ERR(p->new)) {
31482 +                               p->new = NULL;
31483 +                               goto out_pair;
31484 +                       }
31485 +               }
31486 +
31487 +               err = au_xino_do_write(writef, p->new,
31488 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
31489 +               if (unlikely(err))
31490 +                       goto out_pair;
31491 +       }
31492 +
31493 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
31494 +               br = au_sbr(sb, bindex);
31495 +               if (br->br_xino.xi_file)
31496 +                       fput(br->br_xino.xi_file);
31497 +               get_file(p->new);
31498 +               br->br_xino.xi_file = p->new;
31499 +       }
31500 +
31501 +out_pair:
31502 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
31503 +               if (p->new)
31504 +                       fput(p->new);
31505 +               else
31506 +                       break;
31507 +       kfree(fpair);
31508 +out:
31509 +       return err;
31510 +}
31511 +
31512 +void au_xino_clr(struct super_block *sb)
31513 +{
31514 +       struct au_sbinfo *sbinfo;
31515 +
31516 +       au_xigen_clr(sb);
31517 +       xino_clear_xib(sb);
31518 +       xino_clear_br(sb);
31519 +       sbinfo = au_sbi(sb);
31520 +       /* lvalue, do not call au_mntflags() */
31521 +       au_opt_clr(sbinfo->si_mntflags, XINO);
31522 +}
31523 +
31524 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
31525 +{
31526 +       int err, skip;
31527 +       struct dentry *parent, *cur_parent;
31528 +       struct qstr *dname, *cur_name;
31529 +       struct file *cur_xino;
31530 +       struct inode *dir;
31531 +       struct au_sbinfo *sbinfo;
31532 +
31533 +       SiMustWriteLock(sb);
31534 +
31535 +       err = 0;
31536 +       sbinfo = au_sbi(sb);
31537 +       parent = dget_parent(xino->file->f_dentry);
31538 +       if (remount) {
31539 +               skip = 0;
31540 +               dname = &xino->file->f_dentry->d_name;
31541 +               cur_xino = sbinfo->si_xib;
31542 +               if (cur_xino) {
31543 +                       cur_parent = dget_parent(cur_xino->f_dentry);
31544 +                       cur_name = &cur_xino->f_dentry->d_name;
31545 +                       skip = (cur_parent == parent
31546 +                               && dname->len == cur_name->len
31547 +                               && !memcmp(dname->name, cur_name->name,
31548 +                                          dname->len));
31549 +                       dput(cur_parent);
31550 +               }
31551 +               if (skip)
31552 +                       goto out;
31553 +       }
31554 +
31555 +       au_opt_set(sbinfo->si_mntflags, XINO);
31556 +       dir = parent->d_inode;
31557 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
31558 +       /* mnt_want_write() is unnecessary here */
31559 +       err = au_xino_set_xib(sb, xino->file);
31560 +       if (!err)
31561 +               err = au_xigen_set(sb, xino->file);
31562 +       if (!err)
31563 +               err = au_xino_set_br(sb, xino->file);
31564 +       mutex_unlock(&dir->i_mutex);
31565 +       if (!err)
31566 +               goto out; /* success */
31567 +
31568 +       /* reset all */
31569 +       AuIOErr("failed creating xino(%d).\n", err);
31570 +
31571 +out:
31572 +       dput(parent);
31573 +       return err;
31574 +}
31575 +
31576 +/* ---------------------------------------------------------------------- */
31577 +
31578 +/*
31579 + * create a xinofile at the default place/path.
31580 + */
31581 +struct file *au_xino_def(struct super_block *sb)
31582 +{
31583 +       struct file *file;
31584 +       char *page, *p;
31585 +       struct au_branch *br;
31586 +       struct super_block *h_sb;
31587 +       struct path path;
31588 +       aufs_bindex_t bend, bindex, bwr;
31589 +
31590 +       br = NULL;
31591 +       bend = au_sbend(sb);
31592 +       bwr = -1;
31593 +       for (bindex = 0; bindex <= bend; bindex++) {
31594 +               br = au_sbr(sb, bindex);
31595 +               if (au_br_writable(br->br_perm)
31596 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
31597 +                       bwr = bindex;
31598 +                       break;
31599 +               }
31600 +       }
31601 +
31602 +       if (bwr >= 0) {
31603 +               file = ERR_PTR(-ENOMEM);
31604 +               page = (void *)__get_free_page(GFP_NOFS);
31605 +               if (unlikely(!page))
31606 +                       goto out;
31607 +               path.mnt = au_br_mnt(br);
31608 +               path.dentry = au_h_dptr(sb->s_root, bwr);
31609 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
31610 +               file = (void *)p;
31611 +               if (!IS_ERR(p)) {
31612 +                       strcat(p, "/" AUFS_XINO_FNAME);
31613 +                       AuDbg("%s\n", p);
31614 +                       file = au_xino_create(sb, p, /*silent*/0);
31615 +                       if (!IS_ERR(file))
31616 +                               au_xino_brid_set(sb, br->br_id);
31617 +               }
31618 +               free_page((unsigned long)page);
31619 +       } else {
31620 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
31621 +               if (IS_ERR(file))
31622 +                       goto out;
31623 +               h_sb = file->f_dentry->d_sb;
31624 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
31625 +                       pr_err("xino doesn't support %s(%s)\n",
31626 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
31627 +                       fput(file);
31628 +                       file = ERR_PTR(-EINVAL);
31629 +               }
31630 +               if (!IS_ERR(file))
31631 +                       au_xino_brid_set(sb, -1);
31632 +       }
31633 +
31634 +out:
31635 +       return file;
31636 +}
31637 +
31638 +/* ---------------------------------------------------------------------- */
31639 +
31640 +int au_xino_path(struct seq_file *seq, struct file *file)
31641 +{
31642 +       int err;
31643 +
31644 +       err = au_seq_path(seq, &file->f_path);
31645 +       if (unlikely(err < 0))
31646 +               goto out;
31647 +
31648 +       err = 0;
31649 +#define Deleted "\\040(deleted)"
31650 +       seq->count -= sizeof(Deleted) - 1;
31651 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
31652 +                        sizeof(Deleted) - 1));
31653 +#undef Deleted
31654 +
31655 +out:
31656 +       return err;
31657 +}
31658 diff -urN /usr/share/empty/include/linux/aufs_type.h linux/include/linux/aufs_type.h
31659 --- /usr/share/empty/include/linux/aufs_type.h  1970-01-01 01:00:00.000000000 +0100
31660 +++ linux/include/linux/aufs_type.h     2013-07-06 13:20:47.760198800 +0200
31661 @@ -0,0 +1,19 @@
31662 +/*
31663 + * Copyright (C) 2012-2013 Junjiro R. Okajima
31664 + *
31665 + * This program, aufs is free software; you can redistribute it and/or modify
31666 + * it under the terms of the GNU General Public License as published by
31667 + * the Free Software Foundation; either version 2 of the License, or
31668 + * (at your option) any later version.
31669 + *
31670 + * This program is distributed in the hope that it will be useful,
31671 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31672 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31673 + * GNU General Public License for more details.
31674 + *
31675 + * You should have received a copy of the GNU General Public License
31676 + * along with this program; if not, write to the Free Software
31677 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
31678 + */
31679 +
31680 +#include <uapi/linux/aufs_type.h>
31681 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
31682 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
31683 +++ linux/include/uapi/linux/aufs_type.h        2013-08-23 23:59:39.638250372 +0200
31684 @@ -0,0 +1,251 @@
31685 +/*
31686 + * Copyright (C) 2005-2013 Junjiro R. Okajima
31687 + *
31688 + * This program, aufs is free software; you can redistribute it and/or modify
31689 + * it under the terms of the GNU General Public License as published by
31690 + * the Free Software Foundation; either version 2 of the License, or
31691 + * (at your option) any later version.
31692 + *
31693 + * This program is distributed in the hope that it will be useful,
31694 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31695 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31696 + * GNU General Public License for more details.
31697 + *
31698 + * You should have received a copy of the GNU General Public License
31699 + * along with this program; if not, write to the Free Software
31700 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
31701 + */
31702 +
31703 +#ifndef __AUFS_TYPE_H__
31704 +#define __AUFS_TYPE_H__
31705 +
31706 +#define AUFS_NAME      "aufs"
31707 +
31708 +#ifdef __KERNEL__
31709 +/*
31710 + * define it before including all other headers.
31711 + * sched.h may use pr_* macros before defining "current", so define the
31712 + * no-current version first, and re-define later.
31713 + */
31714 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
31715 +#include <linux/sched.h>
31716 +#undef pr_fmt
31717 +#define pr_fmt(fmt) \
31718 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
31719 +               (int)sizeof(current->comm), current->comm, current->pid
31720 +#else
31721 +#include <stdint.h>
31722 +#include <sys/types.h>
31723 +#endif /* __KERNEL__ */
31724 +
31725 +#include <linux/limits.h>
31726 +
31727 +#define AUFS_VERSION   "3.10-20130819"
31728 +
31729 +/* todo? move this to linux-2.6.19/include/magic.h */
31730 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
31731 +
31732 +/* ---------------------------------------------------------------------- */
31733 +
31734 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
31735 +typedef int8_t aufs_bindex_t;
31736 +#define AUFS_BRANCH_MAX 127
31737 +#else
31738 +typedef int16_t aufs_bindex_t;
31739 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
31740 +#define AUFS_BRANCH_MAX 511
31741 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
31742 +#define AUFS_BRANCH_MAX 1023
31743 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
31744 +#define AUFS_BRANCH_MAX 32767
31745 +#endif
31746 +#endif
31747 +
31748 +#ifdef __KERNEL__
31749 +#ifndef AUFS_BRANCH_MAX
31750 +#error unknown CONFIG_AUFS_BRANCH_MAX value
31751 +#endif
31752 +#endif /* __KERNEL__ */
31753 +
31754 +/* ---------------------------------------------------------------------- */
31755 +
31756 +#define AUFS_FSTYPE            AUFS_NAME
31757 +
31758 +#define AUFS_ROOT_INO          2
31759 +#define AUFS_FIRST_INO         11
31760 +
31761 +#define AUFS_WH_PFX            ".wh."
31762 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
31763 +#define AUFS_WH_TMP_LEN                4
31764 +/* a limit for rmdir/rename a dir and copyup */
31765 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
31766 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
31767 +                               - 1                     /* dot */\
31768 +                               - AUFS_WH_TMP_LEN)      /* hex */
31769 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
31770 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
31771 +#define AUFS_XINO_TRUNC_INIT   64 /* blocks */
31772 +#define AUFS_XINO_TRUNC_STEP   4  /* blocks */
31773 +#define AUFS_DIRWH_DEF         3
31774 +#define AUFS_RDCACHE_DEF       10 /* seconds */
31775 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
31776 +#define AUFS_RDBLK_DEF         512 /* bytes */
31777 +#define AUFS_RDHASH_DEF                32
31778 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
31779 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
31780 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
31781 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
31782 +
31783 +/* pseudo-link maintenace under /proc */
31784 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
31785 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
31786 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
31787 +
31788 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
31789 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
31790 +
31791 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
31792 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
31793 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
31794 +
31795 +/* doubly whiteouted */
31796 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
31797 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
31798 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
31799 +
31800 +/* branch permissions and attributes */
31801 +#define AUFS_BRPERM_RW         "rw"
31802 +#define AUFS_BRPERM_RO         "ro"
31803 +#define AUFS_BRPERM_RR         "rr"
31804 +#define AUFS_BRRATTR_WH                "wh"
31805 +#define AUFS_BRWATTR_NLWH      "nolwh"
31806 +#define AUFS_BRATTR_UNPIN      "unpin"
31807 +
31808 +/* ---------------------------------------------------------------------- */
31809 +
31810 +/* ioctl */
31811 +enum {
31812 +       /* readdir in userspace */
31813 +       AuCtl_RDU,
31814 +       AuCtl_RDU_INO,
31815 +
31816 +       /* pathconf wrapper */
31817 +       AuCtl_WBR_FD,
31818 +
31819 +       /* busy inode */
31820 +       AuCtl_IBUSY,
31821 +
31822 +       /* move-down */
31823 +       AuCtl_MVDOWN
31824 +};
31825 +
31826 +/* borrowed from linux/include/linux/kernel.h */
31827 +#ifndef ALIGN
31828 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
31829 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
31830 +#endif
31831 +
31832 +/* borrowed from linux/include/linux/compiler-gcc3.h */
31833 +#ifndef __aligned
31834 +#define __aligned(x)                   __attribute__((aligned(x)))
31835 +#endif
31836 +
31837 +#ifdef __KERNEL__
31838 +#ifndef __packed
31839 +#define __packed                       __attribute__((packed))
31840 +#endif
31841 +#endif
31842 +
31843 +struct au_rdu_cookie {
31844 +       uint64_t        h_pos;
31845 +       int16_t         bindex;
31846 +       uint8_t         flags;
31847 +       uint8_t         pad;
31848 +       uint32_t        generation;
31849 +} __aligned(8);
31850 +
31851 +struct au_rdu_ent {
31852 +       uint64_t        ino;
31853 +       int16_t         bindex;
31854 +       uint8_t         type;
31855 +       uint8_t         nlen;
31856 +       uint8_t         wh;
31857 +       char            name[0];
31858 +} __aligned(8);
31859 +
31860 +static inline int au_rdu_len(int nlen)
31861 +{
31862 +       /* include the terminating NULL */
31863 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
31864 +                    sizeof(uint64_t));
31865 +}
31866 +
31867 +union au_rdu_ent_ul {
31868 +       struct au_rdu_ent __user        *e;
31869 +       uint64_t                        ul;
31870 +};
31871 +
31872 +enum {
31873 +       AufsCtlRduV_SZ,
31874 +       AufsCtlRduV_End
31875 +};
31876 +
31877 +struct aufs_rdu {
31878 +       /* input */
31879 +       union {
31880 +               uint64_t        sz;     /* AuCtl_RDU */
31881 +               uint64_t        nent;   /* AuCtl_RDU_INO */
31882 +       };
31883 +       union au_rdu_ent_ul     ent;
31884 +       uint16_t                verify[AufsCtlRduV_End];
31885 +
31886 +       /* input/output */
31887 +       uint32_t                blk;
31888 +
31889 +       /* output */
31890 +       union au_rdu_ent_ul     tail;
31891 +       /* number of entries which were added in a single call */
31892 +       uint64_t                rent;
31893 +       uint8_t                 full;
31894 +       uint8_t                 shwh;
31895 +
31896 +       struct au_rdu_cookie    cookie;
31897 +} __aligned(8);
31898 +
31899 +/* ---------------------------------------------------------------------- */
31900 +
31901 +struct aufs_wbr_fd {
31902 +       uint32_t        oflags;
31903 +       int16_t         brid;
31904 +} __aligned(8);
31905 +
31906 +/* ---------------------------------------------------------------------- */
31907 +
31908 +struct aufs_ibusy {
31909 +       uint64_t        ino, h_ino;
31910 +       int16_t         bindex;
31911 +} __aligned(8);
31912 +
31913 +/* ---------------------------------------------------------------------- */
31914 +
31915 +/* flags for move-down */
31916 +#define AUFS_MVDOWN_VERBOSE    1
31917 +/* will be added more */
31918 +
31919 +struct aufs_mvdown {
31920 +       uint8_t         flags;
31921 +       /* will be added more */
31922 +} __aligned(8);
31923 +
31924 +/* ---------------------------------------------------------------------- */
31925 +
31926 +#define AuCtlType              'A'
31927 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
31928 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
31929 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
31930 +                                    struct aufs_wbr_fd)
31931 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
31932 +#define AUFS_CTL_MVDOWN                _IOR(AuCtlType, AuCtl_MVDOWN, \
31933 +                                    struct aufs_mvdown)
31934 +
31935 +#endif /* __AUFS_TYPE_H__ */
This page took 3.980755 seconds and 3 git commands to generate.