]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs3.patch
- aufs updated but build still fails
[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/file_table.c b/fs/file_table.c
39 index 485dc0e..8db8096 100644
40 --- a/fs/file_table.c
41 +++ b/fs/file_table.c
42 @@ -36,7 +36,7 @@ struct files_stat_struct files_stat = {
43         .max_files = NR_FILE
44  };
45  
46 -DEFINE_STATIC_LGLOCK(files_lglock);
47 +DEFINE_LGLOCK(files_lglock);
48  
49  /* SLAB cache for file structures */
50  static struct kmem_cache *filp_cachep __read_mostly;
51 diff --git a/fs/inode.c b/fs/inode.c
52 index 00d5fc3..f324521 100644
53 --- a/fs/inode.c
54 +++ b/fs/inode.c
55 @@ -1498,7 +1498,7 @@ static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
56   * This does the actual work of updating an inodes time or version.  Must have
57   * had called mnt_want_write() before calling this.
58   */
59 -static int update_time(struct inode *inode, struct timespec *time, int flags)
60 +int update_time(struct inode *inode, struct timespec *time, int flags)
61  {
62         if (inode->i_op->update_time)
63                 return inode->i_op->update_time(inode, time, flags);
64 diff --git a/fs/splice.c b/fs/splice.c
65 index d37431d..987346f 100644
66 --- a/fs/splice.c
67 +++ b/fs/splice.c
68 @@ -1093,8 +1093,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
69  /*
70   * Attempt to initiate a splice from pipe to file.
71   */
72 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
73 -                          loff_t *ppos, size_t len, unsigned int flags)
74 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
75 +                   loff_t *ppos, size_t len, unsigned int flags)
76  {
77         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
78                                 loff_t *, size_t, unsigned int);
79 @@ -1124,9 +1124,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
80  /*
81   * Attempt to initiate a splice from a file to a pipe.
82   */
83 -static long do_splice_to(struct file *in, loff_t *ppos,
84 -                        struct pipe_inode_info *pipe, size_t len,
85 -                        unsigned int flags)
86 +long do_splice_to(struct file *in, loff_t *ppos,
87 +                 struct pipe_inode_info *pipe, size_t len,
88 +                 unsigned int flags)
89  {
90         ssize_t (*splice_read)(struct file *, loff_t *,
91                                struct pipe_inode_info *, size_t, unsigned int);
92 diff --git a/include/linux/fs.h b/include/linux/fs.h
93 index 65c2be2..0148214 100644
94 --- a/include/linux/fs.h
95 +++ b/include/linux/fs.h
96 @@ -2574,6 +2574,7 @@ extern int inode_change_ok(const struct inode *, struct iattr *);
97  extern int inode_newsize_ok(const struct inode *, loff_t offset);
98  extern void setattr_copy(struct inode *inode, const struct iattr *attr);
99  
100 +extern int update_time(struct inode *, struct timespec *, int);
101  extern int file_update_time(struct file *file);
102  
103  extern int generic_show_options(struct seq_file *m, struct dentry *root);
104 diff --git a/include/linux/splice.h b/include/linux/splice.h
105 index 74575cb..bfc6fb6 100644
106 --- a/include/linux/splice.h
107 +++ b/include/linux/splice.h
108 @@ -92,4 +92,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
109  extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
110  
111  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
112 +
113 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
114 +                          loff_t *ppos, size_t len, unsigned int flags);
115 +extern long do_splice_to(struct file *in, loff_t *ppos,
116 +                        struct pipe_inode_info *pipe, size_t len,
117 +                        unsigned int flags);
118  #endif
119 aufs3.10 standalone patch
120
121 diff --git a/fs/file_table.c b/fs/file_table.c
122 index 8db8096..e271e28 100644
123 --- a/fs/file_table.c
124 +++ b/fs/file_table.c
125 @@ -37,6 +37,7 @@ struct files_stat_struct files_stat = {
126  };
127  
128  DEFINE_LGLOCK(files_lglock);
129 +EXPORT_SYMBOL(files_lglock);
130  
131  /* SLAB cache for file structures */
132  static struct kmem_cache *filp_cachep __read_mostly;
133 @@ -405,6 +406,8 @@ void file_sb_list_del(struct file *file)
134         }
135  }
136  
137 +EXPORT_SYMBOL(file_sb_list_del);
138 +
139  #ifdef CONFIG_SMP
140  
141  /*
142 diff --git a/fs/inode.c b/fs/inode.c
143 index f324521..bff7670 100644
144 --- a/fs/inode.c
145 +++ b/fs/inode.c
146 @@ -56,6 +56,7 @@ static struct hlist_head *inode_hashtable __read_mostly;
147  static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
148  
149  __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
150 +EXPORT_SYMBOL(inode_sb_list_lock);
151  
152  /*
153   * Empty aops. Can be used for the cases where the user does not
154 @@ -1514,6 +1515,7 @@ int update_time(struct inode *inode, struct timespec *time, int flags)
155         mark_inode_dirty_sync(inode);
156         return 0;
157  }
158 +EXPORT_SYMBOL(update_time);
159  
160  /**
161   *     touch_atime     -       update the access time
162 diff --git a/fs/namespace.c b/fs/namespace.c
163 index 7b1ca9b..51db6ad 100644
164 --- a/fs/namespace.c
165 +++ b/fs/namespace.c
166 @@ -54,6 +54,7 @@ EXPORT_SYMBOL_GPL(fs_kobj);
167   * tree or hash is modified or when a vfsmount structure is modified.
168   */
169  DEFINE_BRLOCK(vfsmount_lock);
170 +EXPORT_SYMBOL(vfsmount_lock);
171  
172  static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
173  {
174 @@ -427,6 +428,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
175         mnt_dec_writers(real_mount(mnt));
176         preempt_enable();
177  }
178 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
179  
180  /**
181   * mnt_drop_write - give up write access to a mount
182 @@ -1456,6 +1458,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
183         }
184         return 0;
185  }
186 +EXPORT_SYMBOL(iterate_mounts);
187  
188  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
189  {
190 diff --git a/fs/notify/group.c b/fs/notify/group.c
191 index bd2625b..2ff2a0f 100644
192 --- a/fs/notify/group.c
193 +++ b/fs/notify/group.c
194 @@ -22,6 +22,7 @@
195  #include <linux/srcu.h>
196  #include <linux/rculist.h>
197  #include <linux/wait.h>
198 +#include <linux/module.h>
199  
200  #include <linux/fsnotify_backend.h>
201  #include "fsnotify.h"
202 @@ -65,6 +66,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
203  {
204         atomic_inc(&group->refcnt);
205  }
206 +EXPORT_SYMBOL(fsnotify_get_group);
207  
208  /*
209   * Drop a reference to a group.  Free it if it's through.
210 @@ -74,6 +76,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
211         if (atomic_dec_and_test(&group->refcnt))
212                 fsnotify_final_destroy_group(group);
213  }
214 +EXPORT_SYMBOL(fsnotify_put_group);
215  
216  /*
217   * Create a new fsnotify_group and hold a reference for the group returned.
218 @@ -102,6 +105,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
219  
220         return group;
221  }
222 +EXPORT_SYMBOL(fsnotify_alloc_group);
223  
224  int fsnotify_fasync(int fd, struct file *file, int on)
225  {
226 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
227 index fc6b49b..a6bb87d 100644
228 --- a/fs/notify/mark.c
229 +++ b/fs/notify/mark.c
230 @@ -115,6 +115,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
231                 mark->free_mark(mark);
232         }
233  }
234 +EXPORT_SYMBOL(fsnotify_put_mark);
235  
236  /*
237   * Any time a mark is getting freed we end up here.
238 @@ -197,6 +198,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
239         fsnotify_destroy_mark_locked(mark, group);
240         mutex_unlock(&group->mark_mutex);
241  }
242 +EXPORT_SYMBOL(fsnotify_destroy_mark);
243  
244  void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
245  {
246 @@ -281,6 +283,7 @@ err:
247  
248         return ret;
249  }
250 +EXPORT_SYMBOL(fsnotify_add_mark);
251  
252  int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
253                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
254 @@ -342,6 +345,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
255         atomic_set(&mark->refcnt, 1);
256         mark->free_mark = free_mark;
257  }
258 +EXPORT_SYMBOL(fsnotify_init_mark);
259  
260  static int fsnotify_mark_destroy(void *ignored)
261  {
262 diff --git a/fs/open.c b/fs/open.c
263 index 8c74100..be563cd 100644
264 --- a/fs/open.c
265 +++ b/fs/open.c
266 @@ -61,6 +61,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
267         mutex_unlock(&dentry->d_inode->i_mutex);
268         return ret;
269  }
270 +EXPORT_SYMBOL(do_truncate);
271  
272  long vfs_truncate(struct path *path, loff_t length)
273  {
274 diff --git a/fs/splice.c b/fs/splice.c
275 index 987346f..8d6a045 100644
276 --- a/fs/splice.c
277 +++ b/fs/splice.c
278 @@ -1120,6 +1120,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
279         file_end_write(out);
280         return ret;
281  }
282 +EXPORT_SYMBOL(do_splice_from);
283  
284  /*
285   * Attempt to initiate a splice from a file to a pipe.
286 @@ -1146,6 +1147,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
287  
288         return splice_read(in, ppos, pipe, len, flags);
289  }
290 +EXPORT_SYMBOL(do_splice_to);
291  
292  /**
293   * splice_direct_to_actor - splices data directly between two non-pipes
294 diff --git a/security/commoncap.c b/security/commoncap.c
295 index c44b6fe..d78b003 100644
296 --- a/security/commoncap.c
297 +++ b/security/commoncap.c
298 @@ -988,9 +988,11 @@ int cap_mmap_addr(unsigned long addr)
299         }
300         return ret;
301  }
302 +EXPORT_SYMBOL(cap_mmap_addr);
303  
304  int cap_mmap_file(struct file *file, unsigned long reqprot,
305                   unsigned long prot, unsigned long flags)
306  {
307         return 0;
308  }
309 +EXPORT_SYMBOL(cap_mmap_file);
310 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
311 index dd0dc57..9760ecb6 100644
312 --- a/security/device_cgroup.c
313 +++ b/security/device_cgroup.c
314 @@ -7,6 +7,7 @@
315  #include <linux/device_cgroup.h>
316  #include <linux/cgroup.h>
317  #include <linux/ctype.h>
318 +#include <linux/export.h>
319  #include <linux/list.h>
320  #include <linux/uaccess.h>
321  #include <linux/seq_file.h>
322 @@ -789,6 +790,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
323         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
324                         access);
325  }
326 +EXPORT_SYMBOL(__devcgroup_inode_permission);
327  
328  int devcgroup_inode_mknod(int mode, dev_t dev)
329  {
330 diff --git a/security/security.c b/security/security.c
331 index a3dce87..06a6ea6 100644
332 --- a/security/security.c
333 +++ b/security/security.c
334 @@ -396,6 +396,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
335                 return 0;
336         return security_ops->path_rmdir(dir, dentry);
337  }
338 +EXPORT_SYMBOL(security_path_rmdir);
339  
340  int security_path_unlink(struct path *dir, struct dentry *dentry)
341  {
342 @@ -412,6 +413,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
343                 return 0;
344         return security_ops->path_symlink(dir, dentry, old_name);
345  }
346 +EXPORT_SYMBOL(security_path_symlink);
347  
348  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
349                        struct dentry *new_dentry)
350 @@ -420,6 +422,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
351                 return 0;
352         return security_ops->path_link(old_dentry, new_dir, new_dentry);
353  }
354 +EXPORT_SYMBOL(security_path_link);
355  
356  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
357                          struct path *new_dir, struct dentry *new_dentry)
358 @@ -438,6 +441,7 @@ int security_path_truncate(struct path *path)
359                 return 0;
360         return security_ops->path_truncate(path);
361  }
362 +EXPORT_SYMBOL(security_path_truncate);
363  
364  int security_path_chmod(struct path *path, umode_t mode)
365  {
366 @@ -445,6 +449,7 @@ int security_path_chmod(struct path *path, umode_t mode)
367                 return 0;
368         return security_ops->path_chmod(path, mode);
369  }
370 +EXPORT_SYMBOL(security_path_chmod);
371  
372  int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
373  {
374 @@ -452,6 +457,7 @@ int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
375                 return 0;
376         return security_ops->path_chown(path, uid, gid);
377  }
378 +EXPORT_SYMBOL(security_path_chown);
379  
380  int security_path_chroot(struct path *path)
381  {
382 @@ -528,6 +534,7 @@ int security_inode_readlink(struct dentry *dentry)
383                 return 0;
384         return security_ops->inode_readlink(dentry);
385  }
386 +EXPORT_SYMBOL(security_inode_readlink);
387  
388  int security_inode_follow_link(struct dentry *dentry, struct nameidata *nd)
389  {
390 @@ -542,6 +549,7 @@ int security_inode_permission(struct inode *inode, int mask)
391                 return 0;
392         return security_ops->inode_permission(inode, mask);
393  }
394 +EXPORT_SYMBOL(security_inode_permission);
395  
396  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
397  {
398 @@ -663,6 +671,7 @@ int security_file_permission(struct file *file, int mask)
399  
400         return fsnotify_perm(file, mask);
401  }
402 +EXPORT_SYMBOL(security_file_permission);
403  
404  int security_file_alloc(struct file *file)
405  {
406 @@ -723,6 +732,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
407                 return ret;
408         return ima_file_mmap(file, prot);
409  }
410 +EXPORT_SYMBOL(security_mmap_file);
411  
412  int security_mmap_addr(unsigned long addr)
413  {
414 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
415 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
416 +++ linux/Documentation/ABI/testing/debugfs-aufs        2013-07-06 13:20:47.716863966 +0200
417 @@ -0,0 +1,50 @@
418 +What:          /debug/aufs/si_<id>/
419 +Date:          March 2009
420 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
421 +Description:
422 +               Under /debug/aufs, a directory named si_<id> is created
423 +               per aufs mount, where <id> is a unique id generated
424 +               internally.
425 +
426 +What:          /debug/aufs/si_<id>/plink
427 +Date:          Apr 2013
428 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
429 +Description:
430 +               It has three lines and shows the information about the
431 +               pseudo-link. The first line is a single number
432 +               representing a number of buckets. The second line is a
433 +               number of pseudo-links per buckets (separated by a
434 +               blank). The last line is a single number representing a
435 +               total number of psedo-links.
436 +               When the aufs mount option 'noplink' is specified, it
437 +               will show "1\n0\n0\n".
438 +
439 +What:          /debug/aufs/si_<id>/xib
440 +Date:          March 2009
441 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
442 +Description:
443 +               It shows the consumed blocks by xib (External Inode Number
444 +               Bitmap), its block size and file size.
445 +               When the aufs mount option 'noxino' is specified, it
446 +               will be empty. About XINO files, see the aufs manual.
447 +
448 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
449 +Date:          March 2009
450 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
451 +Description:
452 +               It shows the consumed blocks by xino (External Inode Number
453 +               Translation Table), its link count, block size and file
454 +               size.
455 +               When the aufs mount option 'noxino' is specified, it
456 +               will be empty. About XINO files, see the aufs manual.
457 +
458 +What:          /debug/aufs/si_<id>/xigen
459 +Date:          March 2009
460 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
461 +Description:
462 +               It shows the consumed blocks by xigen (External Inode
463 +               Generation Table), its block size and file size.
464 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
465 +               be created.
466 +               When the aufs mount option 'noxino' is specified, it
467 +               will be empty. About XINO files, see the aufs manual.
468 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
469 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
470 +++ linux/Documentation/ABI/testing/sysfs-aufs  2013-07-06 13:20:47.730197761 +0200
471 @@ -0,0 +1,24 @@
472 +What:          /sys/fs/aufs/si_<id>/
473 +Date:          March 2009
474 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
475 +Description:
476 +               Under /sys/fs/aufs, a directory named si_<id> is created
477 +               per aufs mount, where <id> is a unique id generated
478 +               internally.
479 +
480 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
481 +Date:          March 2009
482 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
483 +Description:
484 +               It shows the abolute path of a member directory (which
485 +               is called branch) in aufs, and its permission.
486 +
487 +What:          /sys/fs/aufs/si_<id>/xi_path
488 +Date:          March 2009
489 +Contact:       J. R. Okajima <hooanon05@yahoo.co.jp>
490 +Description:
491 +               It shows the abolute path of XINO (External Inode Number
492 +               Bitmap, Translation Table and Generation Table) file
493 +               even if it is the default path.
494 +               When the aufs mount option 'noxino' is specified, it
495 +               will be empty. About XINO files, see the aufs manual.
496 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
497 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
498 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2013-07-06 13:20:47.730197761 +0200
499 @@ -0,0 +1,162 @@
500 +
501 +# Copyright (C) 2005-2013 Junjiro R. Okajima
502 +# 
503 +# This program is free software; you can redistribute it and/or modify
504 +# it under the terms of the GNU General Public License as published by
505 +# the Free Software Foundation; either version 2 of the License, or
506 +# (at your option) any later version.
507 +# 
508 +# This program is distributed in the hope that it will be useful,
509 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
510 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
511 +# GNU General Public License for more details.
512 +# 
513 +# You should have received a copy of the GNU General Public License
514 +# along with this program; if not, write to the Free Software
515 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
516 +
517 +Introduction
518 +----------------------------------------
519 +
520 +aufs [ei ju: ef es] | [a u f s]
521 +1. abbrev. for "advanced multi-layered unification filesystem".
522 +2. abbrev. for "another unionfs".
523 +3. abbrev. for "auf das" in German which means "on the" in English.
524 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
525 +   But "Filesystem aufs Filesystem" is hard to understand.
526 +
527 +AUFS is a filesystem with features:
528 +- multi layered stackable unification filesystem, the member directory
529 +  is called as a branch.
530 +- branch permission and attribute, 'readonly', 'real-readonly',
531 +  'readwrite', 'whiteout-able', 'link-able whiteout' and their
532 +  combination.
533 +- internal "file copy-on-write".
534 +- logical deletion, whiteout.
535 +- dynamic branch manipulation, adding, deleting and changing permission.
536 +- allow bypassing aufs, user's direct branch access.
537 +- external inode number translation table and bitmap which maintains the
538 +  persistent aufs inode number.
539 +- seekable directory, including NFS readdir.
540 +- file mapping, mmap and sharing pages.
541 +- pseudo-link, hardlink over branches.
542 +- loopback mounted filesystem as a branch.
543 +- several policies to select one among multiple writable branches.
544 +- revert a single systemcall when an error occurs in aufs.
545 +- and more...
546 +
547 +
548 +Multi Layered Stackable Unification Filesystem
549 +----------------------------------------------------------------------
550 +Most people already knows what it is.
551 +It is a filesystem which unifies several directories and provides a
552 +merged single directory. When users access a file, the access will be
553 +passed/re-directed/converted (sorry, I am not sure which English word is
554 +correct) to the real file on the member filesystem. The member
555 +filesystem is called 'lower filesystem' or 'branch' and has a mode
556 +'readonly' and 'readwrite.' And the deletion for a file on the lower
557 +readonly branch is handled by creating 'whiteout' on the upper writable
558 +branch.
559 +
560 +On LKML, there have been discussions about UnionMount (Jan Blunck,
561 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
562 +different approaches to implement the merged-view.
563 +The former tries putting it into VFS, and the latter implements as a
564 +separate filesystem.
565 +(If I misunderstand about these implementations, please let me know and
566 +I shall correct it. Because it is a long time ago when I read their
567 +source files last time).
568 +
569 +UnionMount's approach will be able to small, but may be hard to share
570 +branches between several UnionMount since the whiteout in it is
571 +implemented in the inode on branch filesystem and always
572 +shared. According to Bharata's post, readdir does not seems to be
573 +finished yet.
574 +There are several missing features known in this implementations such as
575 +- for users, the inode number may change silently. eg. copy-up.
576 +- link(2) may break by copy-up.
577 +- read(2) may get an obsoleted filedata (fstat(2) too).
578 +- fcntl(F_SETLK) may be broken by copy-up.
579 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
580 +  open(O_RDWR).
581 +
582 +Unionfs has a longer history. When I started implementing a stacking filesystem
583 +(Aug 2005), it already existed. It has virtual super_block, inode,
584 +dentry and file objects and they have an array pointing lower same kind
585 +objects. After contributing many patches for Unionfs, I re-started my
586 +project AUFS (Jun 2006).
587 +
588 +In AUFS, the structure of filesystem resembles to Unionfs, but I
589 +implemented my own ideas, approaches and enhancements and it became
590 +totally different one.
591 +
592 +Comparing DM snapshot and fs based implementation
593 +- the number of bytes to be copied between devices is much smaller.
594 +- the type of filesystem must be one and only.
595 +- the fs must be writable, no readonly fs, even for the lower original
596 +  device. so the compression fs will not be usable. but if we use
597 +  loopback mount, we may address this issue.
598 +  for instance,
599 +       mount /cdrom/squashfs.img /sq
600 +       losetup /sq/ext2.img
601 +       losetup /somewhere/cow
602 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
603 +- it will be difficult (or needs more operations) to extract the
604 +  difference between the original device and COW.
605 +- DM snapshot-merge may help a lot when users try merging. in the
606 +  fs-layer union, users will use rsync(1).
607 +
608 +
609 +Several characters/aspects of aufs
610 +----------------------------------------------------------------------
611 +
612 +Aufs has several characters or aspects.
613 +1. a filesystem, callee of VFS helper
614 +2. sub-VFS, caller of VFS helper for branches
615 +3. a virtual filesystem which maintains persistent inode number
616 +4. reader/writer of files on branches such like an application
617 +
618 +1. Callee of VFS Helper
619 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
620 +unlink(2) from an application reaches sys_unlink() kernel function and
621 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
622 +calls filesystem specific unlink operation. Actually aufs implements the
623 +unlink operation but it behaves like a redirector.
624 +
625 +2. Caller of VFS Helper for Branches
626 +aufs_unlink() passes the unlink request to the branch filesystem as if
627 +it were called from VFS. So the called unlink operation of the branch
628 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
629 +every necessary pre/post operation for the branch filesystem.
630 +- acquire the lock for the parent dir on a branch
631 +- lookup in a branch
632 +- revalidate dentry on a branch
633 +- mnt_want_write() for a branch
634 +- vfs_unlink() for a branch
635 +- mnt_drop_write() for a branch
636 +- release the lock on a branch
637 +
638 +3. Persistent Inode Number
639 +One of the most important issue for a filesystem is to maintain inode
640 +numbers. This is particularly important to support exporting a
641 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
642 +backend block device for its own. But some storage is necessary to
643 +maintain inode number. It may be a large space and may not suit to keep
644 +in memory. Aufs rents some space from its first writable branch
645 +filesystem (by default) and creates file(s) on it. These files are
646 +created by aufs internally and removed soon (currently) keeping opened.
647 +Note: Because these files are removed, they are totally gone after
648 +      unmounting aufs. It means the inode numbers are not persistent
649 +      across unmount or reboot. I have a plan to make them really
650 +      persistent which will be important for aufs on NFS server.
651 +
652 +4. Read/Write Files Internally (copy-on-write)
653 +Because a branch can be readonly, when you write a file on it, aufs will
654 +"copy-up" it to the upper writable branch internally. And then write the
655 +originally requested thing to the file. Generally kernel doesn't
656 +open/read/write file actively. In aufs, even a single write may cause a
657 +internal "file copy". This behaviour is very similar to cp(1) command.
658 +
659 +Some people may think it is better to pass such work to user space
660 +helper, instead of doing in kernel space. Actually I am still thinking
661 +about it. But currently I have implemented it in kernel space.
662 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
663 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
664 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2013-08-23 23:59:39.631583456 +0200
665 @@ -0,0 +1,243 @@
666 +
667 +# Copyright (C) 2005-2013 Junjiro R. Okajima
668 +# 
669 +# This program is free software; you can redistribute it and/or modify
670 +# it under the terms of the GNU General Public License as published by
671 +# the Free Software Foundation; either version 2 of the License, or
672 +# (at your option) any later version.
673 +# 
674 +# This program is distributed in the hope that it will be useful,
675 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
676 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
677 +# GNU General Public License for more details.
678 +# 
679 +# You should have received a copy of the GNU General Public License
680 +# along with this program; if not, write to the Free Software
681 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
682 +
683 +Basic Aufs Internal Structure
684 +
685 +Superblock/Inode/Dentry/File Objects
686 +----------------------------------------------------------------------
687 +As like an ordinary filesystem, aufs has its own
688 +superblock/inode/dentry/file objects. All these objects have a
689 +dynamically allocated array and store the same kind of pointers to the
690 +lower filesystem, branch.
691 +For example, when you build a union with one readwrite branch and one
692 +readonly, mounted /au, /rw and /ro respectively.
693 +- /au = /rw + /ro
694 +- /ro/fileA exists but /rw/fileA
695 +
696 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
697 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
698 +- [0] = NULL
699 +- [1] = /ro/fileA
700 +
701 +This style of an array is essentially same to the aufs
702 +superblock/inode/dentry/file objects.
703 +
704 +Because aufs supports manipulating branches, ie. add/delete/change
705 +dynamically, these objects has its own generation. When branches are
706 +changed, the generation in aufs superblock is incremented. And a
707 +generation in other object are compared when it is accessed.
708 +When a generation in other objects are obsoleted, aufs refreshes the
709 +internal array.
710 +
711 +
712 +Superblock
713 +----------------------------------------------------------------------
714 +Additionally aufs superblock has some data for policies to select one
715 +among multiple writable branches, XIB files, pseudo-links and kobject.
716 +See below in detail.
717 +About the policies which supports copy-down a directory, see policy.txt
718 +too.
719 +
720 +
721 +Branch and XINO(External Inode Number Translation Table)
722 +----------------------------------------------------------------------
723 +Every branch has its own xino (external inode number translation table)
724 +file. The xino file is created and unlinked by aufs internally. When two
725 +members of a union exist on the same filesystem, they share the single
726 +xino file.
727 +The struct of a xino file is simple, just a sequence of aufs inode
728 +numbers which is indexed by the lower inode number.
729 +In the above sample, assume the inode number of /ro/fileA is i111 and
730 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
731 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
732 +
733 +When the inode numbers are not contiguous, the xino file will be sparse
734 +which has a hole in it and doesn't consume as much disk space as it
735 +might appear. If your branch filesystem consumes disk space for such
736 +holes, then you should specify 'xino=' option at mounting aufs.
737 +
738 +Also a writable branch has three kinds of "whiteout bases". All these
739 +are existed when the branch is joined to aufs and the names are
740 +whiteout-ed doubly, so that users will never see their names in aufs
741 +hierarchy.
742 +1. a regular file which will be linked to all whiteouts.
743 +2. a directory to store a pseudo-link.
744 +3. a directory to store an "orphan-ed" file temporary.
745 +
746 +1. Whiteout Base
747 +   When you remove a file on a readonly branch, aufs handles it as a
748 +   logical deletion and creates a whiteout on the upper writable branch
749 +   as a hardlink of this file in order not to consume inode on the
750 +   writable branch.
751 +2. Pseudo-link Dir
752 +   See below, Pseudo-link.
753 +3. Step-Parent Dir
754 +   When "fileC" exists on the lower readonly branch only and it is
755 +   opened and removed with its parent dir, and then user writes
756 +   something into it, then aufs copies-up fileC to this
757 +   directory. Because there is no other dir to store fileC. After
758 +   creating a file under this dir, the file is unlinked.
759 +
760 +Because aufs supports manipulating branches, ie. add/delete/change
761 +dynamically, a branch has its own id. When the branch order changes, aufs
762 +finds the new index by searching the branch id.
763 +
764 +
765 +Pseudo-link
766 +----------------------------------------------------------------------
767 +Assume "fileA" exists on the lower readonly branch only and it is
768 +hardlinked to "fileB" on the branch. When you write something to fileA,
769 +aufs copies-up it to the upper writable branch. Additionally aufs
770 +creates a hardlink under the Pseudo-link Directory of the writable
771 +branch. The inode of a pseudo-link is kept in aufs super_block as a
772 +simple list. If fileB is read after unlinking fileA, aufs returns
773 +filedata from the pseudo-link instead of the lower readonly
774 +branch. Because the pseudo-link is based upon the inode, to keep the
775 +inode number by xino (see above) is important.
776 +
777 +All the hardlinks under the Pseudo-link Directory of the writable branch
778 +should be restored in a proper location later. Aufs provides a utility
779 +to do this. The userspace helpers executed at remounting and unmounting
780 +aufs by default.
781 +During this utility is running, it puts aufs into the pseudo-link
782 +maintenance mode. In this mode, only the process which began the
783 +maintenance mode (and its child processes) is allowed to operate in
784 +aufs. Some other processes which are not related to the pseudo-link will
785 +be allowed to run too, but the rest have to return an error or wait
786 +until the maintenance mode ends. If a process already acquires an inode
787 +mutex (in VFS), it has to return an error.
788 +
789 +
790 +XIB(external inode number bitmap)
791 +----------------------------------------------------------------------
792 +Addition to the xino file per a branch, aufs has an external inode number
793 +bitmap in a superblock object. It is also a file such like a xino file.
794 +It is a simple bitmap to mark whether the aufs inode number is in-use or
795 +not.
796 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
797 +
798 +Aufs implements a feature to truncate/refresh both of xino and xib to
799 +reduce the number of consumed disk blocks for these files.
800 +
801 +
802 +Virtual or Vertical Dir, and Readdir in Userspace
803 +----------------------------------------------------------------------
804 +In order to support multiple layers (branches), aufs readdir operation
805 +constructs a virtual dir block on memory. For readdir, aufs calls
806 +vfs_readdir() internally for each dir on branches, merges their entries
807 +with eliminating the whiteout-ed ones, and sets it to file (dir)
808 +object. So the file object has its entry list until it is closed. The
809 +entry list will be updated when the file position is zero and becomes
810 +old. This decision is made in aufs automatically.
811 +
812 +The dynamically allocated memory block for the name of entries has a
813 +unit of 512 bytes (by default) and stores the names contiguously (no
814 +padding). Another block for each entry is handled by kmem_cache too.
815 +During building dir blocks, aufs creates hash list and judging whether
816 +the entry is whiteouted by its upper branch or already listed.
817 +The merged result is cached in the corresponding inode object and
818 +maintained by a customizable life-time option.
819 +
820 +Some people may call it can be a security hole or invite DoS attack
821 +since the opened and once readdir-ed dir (file object) holds its entry
822 +list and becomes a pressure for system memory. But I'd say it is similar
823 +to files under /proc or /sys. The virtual files in them also holds a
824 +memory page (generally) while they are opened. When an idea to reduce
825 +memory for them is introduced, it will be applied to aufs too.
826 +For those who really hate this situation, I've developed readdir(3)
827 +library which operates this merging in userspace. You just need to set
828 +LD_PRELOAD environment variable, and aufs will not consume no memory in
829 +kernel space for readdir(3).
830 +
831 +
832 +Workqueue
833 +----------------------------------------------------------------------
834 +Aufs sometimes requires privilege access to a branch. For instance,
835 +in copy-up/down operation. When a user process is going to make changes
836 +to a file which exists in the lower readonly branch only, and the mode
837 +of one of ancestor directories may not be writable by a user
838 +process. Here aufs copy-up the file with its ancestors and they may
839 +require privilege to set its owner/group/mode/etc.
840 +This is a typical case of a application character of aufs (see
841 +Introduction).
842 +
843 +Aufs uses workqueue synchronously for this case. It creates its own
844 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
845 +passes the request to call mkdir or write (for example), and wait for
846 +its completion. This approach solves a problem of a signal handler
847 +simply.
848 +If aufs didn't adopt the workqueue and changed the privilege of the
849 +process, and if the mkdir/write call arises SIGXFSZ or other signal,
850 +then the user process might gain a privilege or the generated core file
851 +was owned by a superuser.
852 +
853 +Also aufs uses the system global workqueue ("events" kernel thread) too
854 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
855 +whiteout base and etc. This is unrelated to a privilege.
856 +Most of aufs operation tries acquiring a rw_semaphore for aufs
857 +superblock at the beginning, at the same time waits for the completion
858 +of all queued asynchronous tasks.
859 +
860 +
861 +Whiteout
862 +----------------------------------------------------------------------
863 +The whiteout in aufs is very similar to Unionfs's. That is represented
864 +by its filename. UnionMount takes an approach of a file mode, but I am
865 +afraid several utilities (find(1) or something) will have to support it.
866 +
867 +Basically the whiteout represents "logical deletion" which stops aufs to
868 +lookup further, but also it represents "dir is opaque" which also stop
869 +lookup.
870 +
871 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
872 +In order to make several functions in a single systemcall to be
873 +revertible, aufs adopts an approach to rename a directory to a temporary
874 +unique whiteouted name.
875 +For example, in rename(2) dir where the target dir already existed, aufs
876 +renames the target dir to a temporary unique whiteouted name before the
877 +actual rename on a branch and then handles other actions (make it opaque,
878 +update the attributes, etc). If an error happens in these actions, aufs
879 +simply renames the whiteouted name back and returns an error. If all are
880 +succeeded, aufs registers a function to remove the whiteouted unique
881 +temporary name completely and asynchronously to the system global
882 +workqueue.
883 +
884 +
885 +Copy-up
886 +----------------------------------------------------------------------
887 +It is a well-known feature or concept.
888 +When user modifies a file on a readonly branch, aufs operate "copy-up"
889 +internally and makes change to the new file on the upper writable branch.
890 +When the trigger systemcall does not update the timestamps of the parent
891 +dir, aufs reverts it after copy-up.
892 +
893 +
894 +Move-down (aufs3.9 and later)
895 +----------------------------------------------------------------------
896 +"Copy-up" is one of the essential feature in aufs. It copies a file from
897 +the lower readonly branch to the upper writable branch when a user
898 +changes something about the file.
899 +"Move-down" is an opposite action of copy-up. Basically this action is
900 +ran manually instead of automatically and internally.
901 +
902 +Sometimes users want to move-down a file from the upper writable branch
903 +to the lower readonly or writable branch. For instance,
904 +- the free space of the upper writable branch is going to run out.
905 +- create a new intermediate branch between the upper and lower branch.
906 +- etc.
907 +
908 +For this purpose, use "aumvdown" command in aufs-util.git.
909 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
910 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
911 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2013-07-06 13:20:47.730197761 +0200
912 @@ -0,0 +1,106 @@
913 +
914 +# Copyright (C) 2005-2013 Junjiro R. Okajima
915 +# 
916 +# This program is free software; you can redistribute it and/or modify
917 +# it under the terms of the GNU General Public License as published by
918 +# the Free Software Foundation; either version 2 of the License, or
919 +# (at your option) any later version.
920 +# 
921 +# This program is distributed in the hope that it will be useful,
922 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
923 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
924 +# GNU General Public License for more details.
925 +# 
926 +# You should have received a copy of the GNU General Public License
927 +# along with this program; if not, write to the Free Software
928 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
929 +
930 +Lookup in a Branch
931 +----------------------------------------------------------------------
932 +Since aufs has a character of sub-VFS (see Introduction), it operates
933 +lookup for branches as VFS does. It may be a heavy work. Generally
934 +speaking struct nameidata is a bigger structure and includes many
935 +information. But almost all lookup operation in aufs is the simplest
936 +case, ie. lookup only an entry directly connected to its parent. Digging
937 +down the directory hierarchy is unnecessary.
938 +
939 +VFS has a function lookup_one_len() for that use, but it is not usable
940 +for a branch filesystem which requires struct nameidata. So aufs
941 +implements a simple lookup wrapper function. When a branch filesystem
942 +allows NULL as nameidata, it calls lookup_one_len(). Otherwise it builds
943 +a simplest nameidata and calls lookup_hash().
944 +Here aufs applies "a principle in NFSD", ie. if the filesystem supports
945 +NFS-export, then it has to support NULL as a nameidata parameter for
946 +->create(), ->lookup() and ->d_revalidate(). So the lookup wrapper in
947 +aufs tests if ->s_export_op in the branch is NULL or not.
948 +
949 +When a branch is a remote filesystem, aufs basically trusts its
950 +->d_revalidate(), also aufs forces the hardest revalidate tests for
951 +them.
952 +For d_revalidate, aufs implements three levels of revalidate tests. See
953 +"Revalidate Dentry and UDBA" in detail.
954 +
955 +
956 +Loopback Mount
957 +----------------------------------------------------------------------
958 +Basically aufs supports any type of filesystem and block device for a
959 +branch (actually there are some exceptions). But it is prohibited to add
960 +a loopback mounted one whose backend file exists in a filesystem which is
961 +already added to aufs. The reason is to protect aufs from a recursive
962 +lookup. If it was allowed, the aufs lookup operation might re-enter a
963 +lookup for the loopback mounted branch in the same context, and will
964 +cause a deadlock.
965 +
966 +
967 +Revalidate Dentry and UDBA (User's Direct Branch Access)
968 +----------------------------------------------------------------------
969 +Generally VFS helpers re-validate a dentry as a part of lookup.
970 +0. digging down the directory hierarchy.
971 +1. lock the parent dir by its i_mutex.
972 +2. lookup the final (child) entry.
973 +3. revalidate it.
974 +4. call the actual operation (create, unlink, etc.)
975 +5. unlock the parent dir
976 +
977 +If the filesystem implements its ->d_revalidate() (step 3), then it is
978 +called. Actually aufs implements it and checks the dentry on a branch is
979 +still valid.
980 +But it is not enough. Because aufs has to release the lock for the
981 +parent dir on a branch at the end of ->lookup() (step 2) and
982 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
983 +held by VFS.
984 +If the file on a branch is changed directly, eg. bypassing aufs, after
985 +aufs released the lock, then the subsequent operation may cause
986 +something unpleasant result.
987 +
988 +This situation is a result of VFS architecture, ->lookup() and
989 +->d_revalidate() is separated. But I never say it is wrong. It is a good
990 +design from VFS's point of view. It is just not suitable for sub-VFS
991 +character in aufs.
992 +
993 +Aufs supports such case by three level of revalidation which is
994 +selectable by user.
995 +1. Simple Revalidate
996 +   Addition to the native flow in VFS's, confirm the child-parent
997 +   relationship on the branch just after locking the parent dir on the
998 +   branch in the "actual operation" (step 4). When this validation
999 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1000 +   checks the validation of the dentry on branches.
1001 +2. Monitor Changes Internally by Inotify/Fsnotify
1002 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1003 +   the dentry on the branch, and returns EBUSY if it finds different
1004 +   dentry.
1005 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1006 +   during it is in cache. When the event is notified, aufs registers a
1007 +   function to kernel 'events' thread by schedule_work(). And the
1008 +   function sets some special status to the cached aufs dentry and inode
1009 +   private data. If they are not cached, then aufs has nothing to
1010 +   do. When the same file is accessed through aufs (step 0-3) later,
1011 +   aufs will detect the status and refresh all necessary data.
1012 +   In this mode, aufs has to ignore the event which is fired by aufs
1013 +   itself.
1014 +3. No Extra Validation
1015 +   This is the simplest test and doesn't add any additional revalidation
1016 +   test, and skip therevalidatin in step 4. It is useful and improves
1017 +   aufs performance when system surely hide the aufs branches from user,
1018 +   by over-mounting something (or another method).
1019 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1020 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1021 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2013-07-06 13:20:47.730197761 +0200
1022 @@ -0,0 +1,76 @@
1023 +
1024 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1025 +# 
1026 +# This program is free software; you can redistribute it and/or modify
1027 +# it under the terms of the GNU General Public License as published by
1028 +# the Free Software Foundation; either version 2 of the License, or
1029 +# (at your option) any later version.
1030 +# 
1031 +# This program is distributed in the hope that it will be useful,
1032 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1033 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1034 +# GNU General Public License for more details.
1035 +# 
1036 +# You should have received a copy of the GNU General Public License
1037 +# along with this program; if not, write to the Free Software
1038 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1039 +
1040 +Branch Manipulation
1041 +
1042 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1043 +and changing its permission/attribute, there are a lot of works to do.
1044 +
1045 +
1046 +Add a Branch
1047 +----------------------------------------------------------------------
1048 +o Confirm the adding dir exists outside of aufs, including loopback
1049 +  mount.
1050 +- and other various attributes...
1051 +o Initialize the xino file and whiteout bases if necessary.
1052 +  See struct.txt.
1053 +
1054 +o Check the owner/group/mode of the directory
1055 +  When the owner/group/mode of the adding directory differs from the
1056 +  existing branch, aufs issues a warning because it may impose a
1057 +  security risk.
1058 +  For example, when a upper writable branch has a world writable empty
1059 +  top directory, a malicious user can create any files on the writable
1060 +  branch directly, like copy-up and modify manually. If something like
1061 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1062 +  writable branch, and the writable branch is world-writable, then a
1063 +  malicious guy may create /etc/passwd on the writable branch directly
1064 +  and the infected file will be valid in aufs.
1065 +  I am afraid it can be a security issue, but nothing to do except
1066 +  producing a warning.
1067 +
1068 +
1069 +Delete a Branch
1070 +----------------------------------------------------------------------
1071 +o Confirm the deleting branch is not busy
1072 +  To be general, there is one merit to adopt "remount" interface to
1073 +  manipulate branches. It is to discard caches. At deleting a branch,
1074 +  aufs checks the still cached (and connected) dentries and inodes. If
1075 +  there are any, then they are all in-use. An inode without its
1076 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1077 +
1078 +  For the cached one, aufs checks whether the same named entry exists on
1079 +  other branches.
1080 +  If the cached one is a directory, because aufs provides a merged view
1081 +  to users, as long as one dir is left on any branch aufs can show the
1082 +  dir to users. In this case, the branch can be removed from aufs.
1083 +  Otherwise aufs rejects deleting the branch.
1084 +
1085 +  If any file on the deleting branch is opened by aufs, then aufs
1086 +  rejects deleting.
1087 +
1088 +
1089 +Modify the Permission of a Branch
1090 +----------------------------------------------------------------------
1091 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1092 +  See struct.txt.
1093 +
1094 +o rw --> ro: Confirm the modifying branch is not busy
1095 +  Aufs rejects the request if any of these conditions are true.
1096 +  - a file on the branch is mmap-ed.
1097 +  - a regular file on the branch is opened for write and there is no
1098 +    same named entry on the upper branch.
1099 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1100 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1101 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2013-07-06 13:20:47.730197761 +0200
1102 @@ -0,0 +1,65 @@
1103 +
1104 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1105 +# 
1106 +# This program is free software; you can redistribute it and/or modify
1107 +# it under the terms of the GNU General Public License as published by
1108 +# the Free Software Foundation; either version 2 of the License, or
1109 +# (at your option) any later version.
1110 +# 
1111 +# This program is distributed in the hope that it will be useful,
1112 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1113 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1114 +# GNU General Public License for more details.
1115 +# 
1116 +# You should have received a copy of the GNU General Public License
1117 +# along with this program; if not, write to the Free Software
1118 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1119 +
1120 +Policies to Select One among Multiple Writable Branches
1121 +----------------------------------------------------------------------
1122 +When the number of writable branch is more than one, aufs has to decide
1123 +the target branch for file creation or copy-up. By default, the highest
1124 +writable branch which has the parent (or ancestor) dir of the target
1125 +file is chosen (top-down-parent policy).
1126 +By user's request, aufs implements some other policies to select the
1127 +writable branch, for file creation two policies, round-robin and
1128 +most-free-space policies. For copy-up three policies, top-down-parent,
1129 +bottom-up-parent and bottom-up policies.
1130 +
1131 +As expected, the round-robin policy selects the branch in circular. When
1132 +you have two writable branches and creates 10 new files, 5 files will be
1133 +created for each branch. mkdir(2) systemcall is an exception. When you
1134 +create 10 new directories, all will be created on the same branch.
1135 +And the most-free-space policy selects the one which has most free
1136 +space among the writable branches. The amount of free space will be
1137 +checked by aufs internally, and users can specify its time interval.
1138 +
1139 +The policies for copy-up is more simple,
1140 +top-down-parent is equivalent to the same named on in create policy,
1141 +bottom-up-parent selects the writable branch where the parent dir
1142 +exists and the nearest upper one from the copyup-source,
1143 +bottom-up selects the nearest upper writable branch from the
1144 +copyup-source, regardless the existence of the parent dir.
1145 +
1146 +There are some rules or exceptions to apply these policies.
1147 +- If there is a readonly branch above the policy-selected branch and
1148 +  the parent dir is marked as opaque (a variation of whiteout), or the
1149 +  target (creating) file is whiteout-ed on the upper readonly branch,
1150 +  then the result of the policy is ignored and the target file will be
1151 +  created on the nearest upper writable branch than the readonly branch.
1152 +- If there is a writable branch above the policy-selected branch and
1153 +  the parent dir is marked as opaque or the target file is whiteouted
1154 +  on the branch, then the result of the policy is ignored and the target
1155 +  file will be created on the highest one among the upper writable
1156 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1157 +  it as usual.
1158 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1159 +  They try selecting the branch where the source exists as possible
1160 +  since copyup a large file will take long time. If it can't be,
1161 +  ie. the branch where the source exists is readonly, then they will
1162 +  follow the copyup policy.
1163 +- There is an exception for rename(2) when the target exists.
1164 +  If the rename target exists, aufs compares the index of the branches
1165 +  where the source and the target exists and selects the higher
1166 +  one. If the selected branch is readonly, then aufs follows the
1167 +  copyup policy.
1168 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
1169 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
1170 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2013-07-06 13:20:47.730197761 +0200
1171 @@ -0,0 +1,47 @@
1172 +
1173 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1174 +# 
1175 +# This program is free software; you can redistribute it and/or modify
1176 +# it under the terms of the GNU General Public License as published by
1177 +# the Free Software Foundation; either version 2 of the License, or
1178 +# (at your option) any later version.
1179 +# 
1180 +# This program is distributed in the hope that it will be useful,
1181 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1182 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1183 +# GNU General Public License for more details.
1184 +# 
1185 +# You should have received a copy of the GNU General Public License
1186 +# along with this program; if not, write to the Free Software
1187 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1188 +
1189 +mmap(2) -- File Memory Mapping
1190 +----------------------------------------------------------------------
1191 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1192 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1193 +->mmap().
1194 +This approach is simple and good, but there is one problem.
1195 +Under /proc, several entries show the mmap-ped files by its path (with
1196 +device and inode number), and the printed path will be the path on the
1197 +branch fs's instead of virtual aufs's.
1198 +This is not a problem in most cases, but some utilities lsof(1) (and its
1199 +user) may expect the path on aufs.
1200 +
1201 +To address this issue, aufs adds a new member called vm_prfile in struct
1202 +vm_area_struct (and struct vm_region). The original vm_file points to
1203 +the file on the branch fs in order to handle everything correctly as
1204 +usual. The new vm_prfile points to a virtual file in aufs, and the
1205 +show-functions in procfs refers to vm_prfile if it is set.
1206 +Also we need to maintain several other places where touching vm_file
1207 +such like
1208 +- fork()/clone() copies vma and the reference count of vm_file is
1209 +  incremented.
1210 +- merging vma maintains the ref count too.
1211 +
1212 +This is not a good approach. It just faking the printed path. But it
1213 +leaves all behaviour around f_mapping unchanged. This is surely an
1214 +advantage.
1215 +Actually aufs had adopted another complicated approach which calls
1216 +generic_file_mmap() and handles struct vm_operations_struct. In this
1217 +approach, aufs met a hard problem and I could not solve it without
1218 +switching the approach.
1219 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
1220 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
1221 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2013-07-06 13:20:47.736864659 +0200
1222 @@ -0,0 +1,59 @@
1223 +
1224 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1225 +# 
1226 +# This program is free software; you can redistribute it and/or modify
1227 +# it under the terms of the GNU General Public License as published by
1228 +# the Free Software Foundation; either version 2 of the License, or
1229 +# (at your option) any later version.
1230 +# 
1231 +# This program is distributed in the hope that it will be useful,
1232 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1233 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1234 +# GNU General Public License for more details.
1235 +# 
1236 +# You should have received a copy of the GNU General Public License
1237 +# along with this program; if not, write to the Free Software
1238 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1239 +
1240 +Export Aufs via NFS
1241 +----------------------------------------------------------------------
1242 +Here is an approach.
1243 +- like xino/xib, add a new file 'xigen' which stores aufs inode
1244 +  generation.
1245 +- iget_locked(): initialize aufs inode generation for a new inode, and
1246 +  store it in xigen file.
1247 +- destroy_inode(): increment aufs inode generation and store it in xigen
1248 +  file. it is necessary even if it is not unlinked, because any data of
1249 +  inode may be changed by UDBA.
1250 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
1251 +  build file handle by
1252 +  + branch id (4 bytes)
1253 +  + superblock generation (4 bytes)
1254 +  + inode number (4 or 8 bytes)
1255 +  + parent dir inode number (4 or 8 bytes)
1256 +  + inode generation (4 bytes))
1257 +  + return value of exportfs_encode_fh() for the parent on a branch (4
1258 +    bytes)
1259 +  + file handle for a branch (by exportfs_encode_fh())
1260 +- fh_to_dentry():
1261 +  + find the index of a branch from its id in handle, and check it is
1262 +    still exist in aufs.
1263 +  + 1st level: get the inode number from handle and search it in cache.
1264 +  + 2nd level: if not found, get the parent inode number from handle and
1265 +    search it in cache. and then open the parent dir, find the matching
1266 +    inode number by vfs_readdir() and get its name, and call
1267 +    lookup_one_len() for the target dentry.
1268 +  + 3rd level: if the parent dir is not cached, call
1269 +    exportfs_decode_fh() for a branch and get the parent on a branch,
1270 +    build a pathname of it, convert it a pathname in aufs, call
1271 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
1272 +    the 2nd level.
1273 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
1274 +    for every branch, but not itself. to get this, (currently) aufs
1275 +    searches in current->nsproxy->mnt_ns list. it may not be a good
1276 +    idea, but I didn't get other approach.
1277 +  + test the generation of the gotten inode.
1278 +- every inode operation: they may get EBUSY due to UDBA. in this case,
1279 +  convert it into ESTALE for NFSD.
1280 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
1281 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
1282 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
1283 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
1284 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2013-07-06 13:20:47.736864659 +0200
1285 @@ -0,0 +1,53 @@
1286 +
1287 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1288 +# 
1289 +# This program is free software; you can redistribute it and/or modify
1290 +# it under the terms of the GNU General Public License as published by
1291 +# the Free Software Foundation; either version 2 of the License, or
1292 +# (at your option) any later version.
1293 +# 
1294 +# This program is distributed in the hope that it will be useful,
1295 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1296 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1297 +# GNU General Public License for more details.
1298 +# 
1299 +# You should have received a copy of the GNU General Public License
1300 +# along with this program; if not, write to the Free Software
1301 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1302 +
1303 +Show Whiteout Mode (shwh)
1304 +----------------------------------------------------------------------
1305 +Generally aufs hides the name of whiteouts. But in some cases, to show
1306 +them is very useful for users. For instance, creating a new middle layer
1307 +(branch) by merging existing layers.
1308 +
1309 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
1310 +When you have three branches,
1311 +- Bottom: 'system', squashfs (underlying base system), read-only
1312 +- Middle: 'mods', squashfs, read-only
1313 +- Top: 'overlay', ram (tmpfs), read-write
1314 +
1315 +The top layer is loaded at boot time and saved at shutdown, to preserve
1316 +the changes made to the system during the session.
1317 +When larger changes have been made, or smaller changes have accumulated,
1318 +the size of the saved top layer data grows. At this point, it would be
1319 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
1320 +and rewrite the 'mods' squashfs, clearing the top layer and thus
1321 +restoring save and load speed.
1322 +
1323 +This merging is simplified by the use of another aufs mount, of just the
1324 +two overlay branches using the 'shwh' option.
1325 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
1326 +       aufs /livesys/merge_union
1327 +
1328 +A merged view of these two branches is then available at
1329 +/livesys/merge_union, and the new feature is that the whiteouts are
1330 +visible!
1331 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
1332 +writing to all branches. Also the default mode for all branches is 'ro'.
1333 +It is now possible to save the combined contents of the two overlay
1334 +branches to a new squashfs, e.g.:
1335 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
1336 +
1337 +This new squashfs archive can be stored on the boot device and the
1338 +initramfs will use it to replace the old one at the next boot.
1339 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
1340 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
1341 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2013-07-06 13:20:47.736864659 +0200
1342 @@ -0,0 +1,47 @@
1343 +
1344 +# Copyright (C) 2010-2013 Junjiro R. Okajima
1345 +#
1346 +# This program is free software; you can redistribute it and/or modify
1347 +# it under the terms of the GNU General Public License as published by
1348 +# the Free Software Foundation; either version 2 of the License, or
1349 +# (at your option) any later version.
1350 +#
1351 +# This program is distributed in the hope that it will be useful,
1352 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1353 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1354 +# GNU General Public License for more details.
1355 +#
1356 +# You should have received a copy of the GNU General Public License
1357 +# along with this program; if not, write to the Free Software
1358 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1359 +
1360 +Dynamically customizable FS operations
1361 +----------------------------------------------------------------------
1362 +Generally FS operations (struct inode_operations, struct
1363 +address_space_operations, struct file_operations, etc.) are defined as
1364 +"static const", but it never means that FS have only one set of
1365 +operation. Some FS have multiple sets of them. For instance, ext2 has
1366 +three sets, one for XIP, for NOBH, and for normal.
1367 +Since aufs overrides and redirects these operations, sometimes aufs has
1368 +to change its behaviour according to the branch FS type. More imporantly
1369 +VFS acts differently if a function (member in the struct) is set or
1370 +not. It means aufs should have several sets of operations and select one
1371 +among them according to the branch FS definition.
1372 +
1373 +In order to solve this problem and not to affect the behavour of VFS,
1374 +aufs defines these operations dynamically. For instance, aufs defines
1375 +aio_read function for struct file_operations, but it may not be set to
1376 +the file_operations. When the branch FS doesn't have it, aufs doesn't
1377 +set it to its file_operations while the function definition itself is
1378 +still alive. So the behaviour of io_submit(2) will not change, and it
1379 +will return an error when aio_read is not defined.
1380 +
1381 +The lifetime of these dynamically generated operation object is
1382 +maintained by aufs branch object. When the branch is removed from aufs,
1383 +the reference counter of the object is decremented. When it reaches
1384 +zero, the dynamically generated operation object will be freed.
1385 +
1386 +This approach is designed to support AIO (io_submit), Direcit I/O and
1387 +XIP mainly.
1388 +Currently this approach is applied to file_operations and
1389 +vm_operations_struct for regular files only.
1390 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt linux/Documentation/filesystems/aufs/design/99plan.txt
1391 --- /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt   1970-01-01 01:00:00.000000000 +0100
1392 +++ linux/Documentation/filesystems/aufs/design/99plan.txt      2013-07-06 13:20:47.736864659 +0200
1393 @@ -0,0 +1,96 @@
1394 +
1395 +# Copyright (C) 2005-2013 Junjiro R. Okajima
1396 +# 
1397 +# This program is free software; you can redistribute it and/or modify
1398 +# it under the terms of the GNU General Public License as published by
1399 +# the Free Software Foundation; either version 2 of the License, or
1400 +# (at your option) any later version.
1401 +# 
1402 +# This program is distributed in the hope that it will be useful,
1403 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1404 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1405 +# GNU General Public License for more details.
1406 +# 
1407 +# You should have received a copy of the GNU General Public License
1408 +# along with this program; if not, write to the Free Software
1409 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1410 +
1411 +Plan
1412 +
1413 +Restoring some features which was implemented in aufs1.
1414 +They were dropped in aufs2 in order to make source files simpler and
1415 +easier to be reviewed.
1416 +
1417 +
1418 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1419 +----------------------------------------------------------------------
1420 +Let's try case study.
1421 +- aufs has two branches, upper readwrite and lower readonly.
1422 +  /au = /rw + /ro
1423 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1424 +- user invoked "chmod a+rx /au/dirA"
1425 +- then "dirA" becomes world readable?
1426 +
1427 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1428 +or it may be a natively readonly filesystem. If aufs respects the lower
1429 +branch, it should not respond readdir request from other users. But user
1430 +allowed it by chmod. Should really aufs rejects showing the entries
1431 +under /ro/dirA?
1432 +
1433 +To be honest, I don't have a best solution for this case. So I
1434 +implemented 'dirperm1' and 'nodirperm1' option in aufs1, and leave it to
1435 +users.
1436 +When dirperm1 is specified, aufs checks only the highest one for the
1437 +directory permission, and shows the entries. Otherwise, as usual, checks
1438 +every dir existing on all branches and rejects the request.
1439 +
1440 +As a side effect, dirperm1 option improves the performance of aufs
1441 +because the number of permission check is reduced.
1442 +
1443 +
1444 +Being Another Aufs's Readonly Branch (robr)
1445 +----------------------------------------------------------------------
1446 +Aufs1 allows aufs to be another aufs's readonly branch.
1447 +This feature was developed by a user's request. But it may not be used
1448 +currecnly.
1449 +
1450 +
1451 +Copy-up on Open (coo=)
1452 +----------------------------------------------------------------------
1453 +By default the internal copy-up is executed when it is really necessary.
1454 +It is not done when a file is opened for writing, but when write(2) is
1455 +done. Users who have many (over 100) branches want to know and analyse
1456 +when and what file is copied-up. To insert a new upper branch which
1457 +contains such files only may improve the performance of aufs.
1458 +
1459 +Aufs1 implemented "coo=none | leaf | all" option.
1460 +
1461 +
1462 +Refresh the Opened File (refrof)
1463 +----------------------------------------------------------------------
1464 +This option is implemented in aufs1 but incomplete.
1465 +
1466 +When user reads from a file, he expects to get its latest filedata
1467 +generally. If the file is removed and a new same named file is created,
1468 +the content he gets is unchanged, ie. the unlinked filedata.
1469 +
1470 +Let's try case study again.
1471 +- aufs has two branches.
1472 +  /au = /rw + /ro
1473 +- "fileA" exists under /ro, but /rw.
1474 +- user opened "/au/fileA".
1475 +- he or someone else inserts a branch (/new) between /rw and /ro.
1476 +  /au = /rw + /new + /ro
1477 +- the new branch has "fileA".
1478 +- user reads from the opened "fileA"
1479 +- which filedata should aufs return, from /ro or /new?
1480 +
1481 +Some people says it has to be "from /ro" and it is a semantics of Unix.
1482 +The others say it should be "from /new" because the file is not removed
1483 +and it is equivalent to the case of someone else modifies the file.
1484 +
1485 +Here again I don't have a best and final answer. I got an idea to
1486 +implement 'refrof' and 'norefrof' option. When 'refrof' (REFResh the
1487 +Opened File) is specified (by default), aufs returns the filedata from
1488 +/new.
1489 +Otherwise from /new.
1490 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
1491 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
1492 +++ linux/Documentation/filesystems/aufs/README 2013-07-30 22:42:46.229279157 +0200
1493 @@ -0,0 +1,346 @@
1494 +
1495 +Aufs3 -- advanced multi layered unification filesystem version 3.x
1496 +http://aufs.sf.net
1497 +Junjiro R. Okajima
1498 +
1499 +
1500 +0. Introduction
1501 +----------------------------------------
1502 +In the early days, aufs was entirely re-designed and re-implemented
1503 +Unionfs Version 1.x series. After many original ideas, approaches,
1504 +improvements and implementations, it becomes totally different from
1505 +Unionfs while keeping the basic features.
1506 +Recently, Unionfs Version 2.x series begin taking some of the same
1507 +approaches to aufs1's.
1508 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
1509 +University and his team.
1510 +
1511 +Aufs3 supports linux-3.0 and later.
1512 +If you want older kernel version support, try aufs2-2.6.git or
1513 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
1514 +
1515 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
1516 +According to Christoph Hellwig, linux rejects all union-type filesystems
1517 +but UnionMount.
1518 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
1519 +
1520 +
1521 +1. Features
1522 +----------------------------------------
1523 +- unite several directories into a single virtual filesystem. The member
1524 +  directory is called as a branch.
1525 +- you can specify the permission flags to the branch, which are 'readonly',
1526 +  'readwrite' and 'whiteout-able.'
1527 +- by upper writable branch, internal copyup and whiteout, files/dirs on
1528 +  readonly branch are modifiable logically.
1529 +- dynamic branch manipulation, add, del.
1530 +- etc...
1531 +
1532 +Also there are many enhancements in aufs1, such as:
1533 +- readdir(3) in userspace.
1534 +- keep inode number by external inode number table
1535 +- keep the timestamps of file/dir in internal copyup operation
1536 +- seekable directory, supporting NFS readdir.
1537 +- whiteout is hardlinked in order to reduce the consumption of inodes
1538 +  on branch
1539 +- do not copyup, nor create a whiteout when it is unnecessary
1540 +- revert a single systemcall when an error occurs in aufs
1541 +- remount interface instead of ioctl
1542 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
1543 +- loopback mounted filesystem as a branch
1544 +- kernel thread for removing the dir who has a plenty of whiteouts
1545 +- support copyup sparse file (a file which has a 'hole' in it)
1546 +- default permission flags for branches
1547 +- selectable permission flags for ro branch, whether whiteout can
1548 +  exist or not
1549 +- export via NFS.
1550 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
1551 +- support multiple writable branches, some policies to select one
1552 +  among multiple writable branches.
1553 +- a new semantics for link(2) and rename(2) to support multiple
1554 +  writable branches.
1555 +- no glibc changes are required.
1556 +- pseudo hardlink (hardlink over branches)
1557 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
1558 +  including NFS or remote filesystem branch.
1559 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
1560 +- and more...
1561 +
1562 +Currently these features are dropped temporary from aufs3.
1563 +See design/08plan.txt in detail.
1564 +- test only the highest one for the directory permission (dirperm1)
1565 +- copyup on open (coo=)
1566 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
1567 +  (robr)
1568 +- statistics of aufs thread (/sys/fs/aufs/stat)
1569 +- delegation mode (dlgt)
1570 +  a delegation of the internal branch access to support task I/O
1571 +  accounting, which also supports Linux Security Modules (LSM) mainly
1572 +  for Suse AppArmor.
1573 +- intent.open/create (file open in a single lookup)
1574 +
1575 +Features or just an idea in the future (see also design/*.txt),
1576 +- reorder the branch index without del/re-add.
1577 +- permanent xino files for NFSD
1578 +- an option for refreshing the opened files after add/del branches
1579 +- 'move' policy for copy-up between two writable branches, after
1580 +  checking free space.
1581 +- light version, without branch manipulation. (unnecessary?)
1582 +- copyup in userspace
1583 +- inotify in userspace
1584 +- readv/writev
1585 +- xattr, acl
1586 +
1587 +
1588 +2. Download
1589 +----------------------------------------
1590 +There were three GIT trees for aufs3, aufs3-linux.git,
1591 +aufs3-standalone.git, and aufs-util.git. Note that there is no "3" in
1592 +"aufs-util.git."
1593 +While the aufs-util is always necessary, you need either of aufs3-linux
1594 +or aufs3-standalone.
1595 +
1596 +The aufs3-linux tree includes the whole linux mainline GIT tree,
1597 +git://git.kernel.org/.../torvalds/linux.git.
1598 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
1599 +build aufs3 as an external kernel module.
1600 +
1601 +On the other hand, the aufs3-standalone tree has only aufs source files
1602 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
1603 +
1604 +You will find GIT branches whose name is in form of "aufs3.x" where "x"
1605 +represents the linux kernel version, "linux-3.x". For instance,
1606 +"aufs3.0" is for linux-3.0. For latest "linux-3.x-rcN", use
1607 +"aufs3.x-rcN" branch.
1608 +
1609 +o aufs3-linux tree
1610 +$ git clone --reference /your/linux/git/tree \
1611 +       git://git.code.sf.net/p/aufs/aufs3-linux aufs-aufs3-linux \
1612 +       aufs3-linux.git
1613 +- if you don't have linux GIT tree, then remove "--reference ..."
1614 +$ cd aufs3-linux.git
1615 +$ git checkout origin/aufs3.0
1616 +
1617 +o aufs3-standalone tree
1618 +$ git clone git://git.code.sf.net/p/aufs/aufs3-standalone \
1619 +       aufs3-standalone.git
1620 +$ cd aufs3-standalone.git
1621 +$ git checkout origin/aufs3.0
1622 +
1623 +o aufs-util tree
1624 +$ git clone git://git.code.sf.net/p/aufs/aufs-util \
1625 +       aufs-util.git
1626 +$ cd aufs-util.git
1627 +$ git checkout origin/aufs3.0
1628 +
1629 +Note: The 3.x-rcN branch is to be used with `rc' kernel versions ONLY.
1630 +The minor version number, 'x' in '3.x', of aufs may not always
1631 +follow the minor version number of the kernel.
1632 +Because changes in the kernel that cause the use of a new
1633 +minor version number do not always require changes to aufs-util.
1634 +
1635 +Since aufs-util has its own minor version number, you may not be
1636 +able to find a GIT branch in aufs-util for your kernel's
1637 +exact minor version number.
1638 +In this case, you should git-checkout the branch for the
1639 +nearest lower number.
1640 +
1641 +For (an unreleased) example:
1642 +If you are using "linux-3.10" and the "aufs3.10" branch
1643 +does not exist in aufs-util repository, then "aufs3.9", "aufs3.8"
1644 +or something numerically smaller is the branch for your kernel.
1645 +
1646 +Also you can view all branches by
1647 +       $ git branch -a
1648 +
1649 +
1650 +3. Configuration and Compilation
1651 +----------------------------------------
1652 +Make sure you have git-checkout'ed the correct branch.
1653 +
1654 +For aufs3-linux tree,
1655 +- enable CONFIG_AUFS_FS.
1656 +- set other aufs configurations if necessary.
1657 +
1658 +For aufs3-standalone tree,
1659 +There are several ways to build.
1660 +
1661 +1.
1662 +- apply ./aufs3-kbuild.patch to your kernel source files.
1663 +- apply ./aufs3-base.patch too.
1664 +- apply ./aufs3-proc_map.patch too, if you want to make /proc/PID/maps (and
1665 +  others including lsof(1)) show the file path on aufs instead of the
1666 +  path on the branch fs.
1667 +- apply ./aufs3-standalone.patch too, if you have a plan to set
1668 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs3-standalone.patch.
1669 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
1670 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
1671 +- enable CONFIG_AUFS_FS, you can select either
1672 +  =m or =y.
1673 +- and build your kernel as usual.
1674 +- install the built kernel.
1675 +  Note: Since linux-3.9, every filesystem module requires an alias
1676 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
1677 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
1678 +- install the header files too by "make headers_install" to the
1679 +  directory where you specify. By default, it is $PWD/usr.
1680 +  "make help" shows a brief note for headers_install.
1681 +- and reboot your system.
1682 +
1683 +2.
1684 +- module only (CONFIG_AUFS_FS=m).
1685 +- apply ./aufs3-base.patch to your kernel source files.
1686 +- apply ./aufs3-proc_map.patch too to your kernel source files,
1687 +  if you want to make /proc/PID/maps (and others including lsof(1)) show
1688 +  the file path on aufs instead of the path on the branch fs.
1689 +- apply ./aufs3-standalone.patch too.
1690 +- build your kernel, don't forget "make headers_install", and reboot.
1691 +- edit ./config.mk and set other aufs configurations if necessary.
1692 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
1693 +  every aufs configurations.
1694 +- build the module by simple "make".
1695 +  Note: Since linux-3.9, every filesystem module requires an alias
1696 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
1697 +  modules.aliases file.
1698 +- you can specify ${KDIR} make variable which points to your kernel
1699 +  source tree.
1700 +- install the files
1701 +  + run "make install" to install the aufs module, or copy the built
1702 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
1703 +  + run "make install_headers" (instead of headers_install) to install
1704 +    the modified aufs header file (you can specify DESTDIR which is
1705 +    available in aufs standalone version's Makefile only), or copy
1706 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
1707 +    you like manually. By default, the target directory is $PWD/usr.
1708 +- no need to apply aufs3-kbuild.patch, nor copying source files to your
1709 +  kernel source tree.
1710 +
1711 +Note: The header file aufs_type.h is necessary to build aufs-util
1712 +      as well as "make headers_install" in the kernel source tree.
1713 +      headers_install is subject to be forgotten, but it is essentially
1714 +      necessary, not only for building aufs-util.
1715 +      You may not meet problems without headers_install in some older
1716 +      version though.
1717 +
1718 +And then,
1719 +- read README in aufs-util, build and install it
1720 +- note that your distribution may contain an obsoleted version of
1721 +  aufs_type.h in /usr/include/linux or something. When you build aufs
1722 +  utilities, make sure that your compiler refers the correct aufs header
1723 +  file which is built by "make headers_install."
1724 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
1725 +  then run "make install_ulib" too. And refer to the aufs manual in
1726 +  detail.
1727 +
1728 +
1729 +4. Usage
1730 +----------------------------------------
1731 +At first, make sure aufs-util are installed, and please read the aufs
1732 +manual, aufs.5 in aufs-util.git tree.
1733 +$ man -l aufs.5
1734 +
1735 +And then,
1736 +$ mkdir /tmp/rw /tmp/aufs
1737 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
1738 +
1739 +Here is another example. The result is equivalent.
1740 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
1741 +  Or
1742 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
1743 +# mount -o remount,append:${HOME} /tmp/aufs
1744 +
1745 +Then, you can see whole tree of your home dir through /tmp/aufs. If
1746 +you modify a file under /tmp/aufs, the one on your home directory is
1747 +not affected, instead the same named file will be newly created under
1748 +/tmp/rw. And all of your modification to a file will be applied to
1749 +the one under /tmp/rw. This is called the file based Copy on Write
1750 +(COW) method.
1751 +Aufs mount options are described in aufs.5.
1752 +If you run chroot or something and make your aufs as a root directory,
1753 +then you need to customize the shutdown script. See the aufs manual in
1754 +detail.
1755 +
1756 +Additionally, there are some sample usages of aufs which are a
1757 +diskless system with network booting, and LiveCD over NFS.
1758 +See sample dir in CVS tree on SourceForge.
1759 +
1760 +
1761 +5. Contact
1762 +----------------------------------------
1763 +When you have any problems or strange behaviour in aufs, please let me
1764 +know with:
1765 +- /proc/mounts (instead of the output of mount(8))
1766 +- /sys/module/aufs/*
1767 +- /sys/fs/aufs/* (if you have them)
1768 +- /debug/aufs/* (if you have them)
1769 +- linux kernel version
1770 +  if your kernel is not plain, for example modified by distributor,
1771 +  the url where i can download its source is necessary too.
1772 +- aufs version which was printed at loading the module or booting the
1773 +  system, instead of the date you downloaded.
1774 +- configuration (define/undefine CONFIG_AUFS_xxx)
1775 +- kernel configuration or /proc/config.gz (if you have it)
1776 +- behaviour which you think to be incorrect
1777 +- actual operation, reproducible one is better
1778 +- mailto: aufs-users at lists.sourceforge.net
1779 +
1780 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
1781 +and Feature Requests) on SourceForge. Please join and write to
1782 +aufs-users ML.
1783 +
1784 +
1785 +6. Acknowledgements
1786 +----------------------------------------
1787 +Thanks to everyone who have tried and are using aufs, whoever
1788 +have reported a bug or any feedback.
1789 +
1790 +Especially donators:
1791 +Tomas Matejicek(slax.org) made a donation (much more than once).
1792 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
1793 +       scripts) is making "doubling" donations.
1794 +       Unfortunately I cannot list all of the donators, but I really
1795 +       appreciate.
1796 +       It ends Aug 2010, but the ordinary donation URL is still available.
1797 +       <http://sourceforge.net/donate/index.php?group_id=167503>
1798 +Dai Itasaka made a donation (2007/8).
1799 +Chuck Smith made a donation (2008/4, 10 and 12).
1800 +Henk Schoneveld made a donation (2008/9).
1801 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
1802 +Francois Dupoux made a donation (2008/11).
1803 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
1804 +       aufs2 GIT tree (2009/2).
1805 +William Grant made a donation (2009/3).
1806 +Patrick Lane made a donation (2009/4).
1807 +The Mail Archive (mail-archive.com) made donations (2009/5).
1808 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
1809 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
1810 +Pavel Pronskiy made a donation (2011/2).
1811 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
1812 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
1813 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
1814 +11).
1815 +Sam Liddicott made a donation (2011/9).
1816 +Era Scarecrow made a donation (2013/4).
1817 +Bor Ratajc made a donation (2013/4).
1818 +Alessandro Gorreta made a donation (2013/4).
1819 +POIRETTE Marc made a donation (2013/4).
1820 +Alessandro Gorreta made a donation (2013/4).
1821 +lauri kasvandik made a donation (2013/5).
1822 +pemasu from Finland made a donation (2013/7).
1823 +
1824 +Thank you very much.
1825 +Donations are always, including future donations, very important and
1826 +helpful for me to keep on developing aufs.
1827 +
1828 +
1829 +7.
1830 +----------------------------------------
1831 +If you are an experienced user, no explanation is needed. Aufs is
1832 +just a linux filesystem.
1833 +
1834 +
1835 +Enjoy!
1836 +
1837 +# Local variables: ;
1838 +# mode: text;
1839 +# End: ;
1840 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
1841 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
1842 +++ linux/fs/aufs/aufs.h        2013-07-06 13:20:47.736864659 +0200
1843 @@ -0,0 +1,60 @@
1844 +/*
1845 + * Copyright (C) 2005-2013 Junjiro R. Okajima
1846 + *
1847 + * This program, aufs is free software; you can redistribute it and/or modify
1848 + * it under the terms of the GNU General Public License as published by
1849 + * the Free Software Foundation; either version 2 of the License, or
1850 + * (at your option) any later version.
1851 + *
1852 + * This program is distributed in the hope that it will be useful,
1853 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1854 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1855 + * GNU General Public License for more details.
1856 + *
1857 + * You should have received a copy of the GNU General Public License
1858 + * along with this program; if not, write to the Free Software
1859 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1860 + */
1861 +
1862 +/*
1863 + * all header files
1864 + */
1865 +
1866 +#ifndef __AUFS_H__
1867 +#define __AUFS_H__
1868 +
1869 +#ifdef __KERNEL__
1870 +
1871 +#define AuStub(type, name, body, ...) \
1872 +       static inline type name(__VA_ARGS__) { body; }
1873 +
1874 +#define AuStubVoid(name, ...) \
1875 +       AuStub(void, name, , __VA_ARGS__)
1876 +#define AuStubInt0(name, ...) \
1877 +       AuStub(int, name, return 0, __VA_ARGS__)
1878 +
1879 +#include "debug.h"
1880 +
1881 +#include "branch.h"
1882 +#include "cpup.h"
1883 +#include "dcsub.h"
1884 +#include "dbgaufs.h"
1885 +#include "dentry.h"
1886 +#include "dir.h"
1887 +#include "dynop.h"
1888 +#include "file.h"
1889 +#include "fstype.h"
1890 +#include "inode.h"
1891 +#include "loop.h"
1892 +#include "module.h"
1893 +#include "opts.h"
1894 +#include "rwsem.h"
1895 +#include "spl.h"
1896 +#include "super.h"
1897 +#include "sysaufs.h"
1898 +#include "vfsub.h"
1899 +#include "whout.h"
1900 +#include "wkq.h"
1901 +
1902 +#endif /* __KERNEL__ */
1903 +#endif /* __AUFS_H__ */
1904 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
1905 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
1906 +++ linux/fs/aufs/branch.c      2013-07-30 22:42:55.839613269 +0200
1907 @@ -0,0 +1,1213 @@
1908 +/*
1909 + * Copyright (C) 2005-2013 Junjiro R. Okajima
1910 + *
1911 + * This program, aufs is free software; you can redistribute it and/or modify
1912 + * it under the terms of the GNU General Public License as published by
1913 + * the Free Software Foundation; either version 2 of the License, or
1914 + * (at your option) any later version.
1915 + *
1916 + * This program is distributed in the hope that it will be useful,
1917 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1918 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1919 + * GNU General Public License for more details.
1920 + *
1921 + * You should have received a copy of the GNU General Public License
1922 + * along with this program; if not, write to the Free Software
1923 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1924 + */
1925 +
1926 +/*
1927 + * branch management
1928 + */
1929 +
1930 +#include <linux/compat.h>
1931 +#include <linux/statfs.h>
1932 +#include "aufs.h"
1933 +
1934 +/*
1935 + * free a single branch
1936 + */
1937 +
1938 +/* prohibit rmdir to the root of the branch */
1939 +/* todo: another new flag? */
1940 +static void au_br_dflags_force(struct au_branch *br)
1941 +{
1942 +       struct dentry *h_dentry;
1943 +
1944 +       h_dentry = au_br_dentry(br);
1945 +       spin_lock(&h_dentry->d_lock);
1946 +       br->br_dflags = h_dentry->d_flags & DCACHE_MOUNTED;
1947 +       h_dentry->d_flags |= DCACHE_MOUNTED;
1948 +       spin_unlock(&h_dentry->d_lock);
1949 +}
1950 +
1951 +/* restore its d_flags */
1952 +static void au_br_dflags_restore(struct au_branch *br)
1953 +{
1954 +       struct dentry *h_dentry;
1955 +
1956 +       if (br->br_dflags)
1957 +               return;
1958 +
1959 +       h_dentry = au_br_dentry(br);
1960 +       spin_lock(&h_dentry->d_lock);
1961 +       h_dentry->d_flags &= ~DCACHE_MOUNTED;
1962 +       spin_unlock(&h_dentry->d_lock);
1963 +}
1964 +
1965 +static void au_br_do_free(struct au_branch *br)
1966 +{
1967 +       int i;
1968 +       struct au_wbr *wbr;
1969 +       struct au_dykey **key;
1970 +
1971 +       au_hnotify_fin_br(br);
1972 +
1973 +       if (br->br_xino.xi_file)
1974 +               fput(br->br_xino.xi_file);
1975 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
1976 +
1977 +       AuDebugOn(atomic_read(&br->br_count));
1978 +
1979 +       wbr = br->br_wbr;
1980 +       if (wbr) {
1981 +               for (i = 0; i < AuBrWh_Last; i++)
1982 +                       dput(wbr->wbr_wh[i]);
1983 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
1984 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
1985 +       }
1986 +
1987 +       key = br->br_dykey;
1988 +       for (i = 0; i < AuBrDynOp; i++, key++)
1989 +               if (*key)
1990 +                       au_dy_put(*key);
1991 +               else
1992 +                       break;
1993 +
1994 +       au_br_dflags_restore(br);
1995 +
1996 +       /* recursive lock, s_umount of branch's */
1997 +       lockdep_off();
1998 +       path_put(&br->br_path);
1999 +       lockdep_on();
2000 +       kfree(wbr);
2001 +       kfree(br);
2002 +}
2003 +
2004 +/*
2005 + * frees all branches
2006 + */
2007 +void au_br_free(struct au_sbinfo *sbinfo)
2008 +{
2009 +       aufs_bindex_t bmax;
2010 +       struct au_branch **br;
2011 +
2012 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2013 +
2014 +       bmax = sbinfo->si_bend + 1;
2015 +       br = sbinfo->si_branch;
2016 +       while (bmax--)
2017 +               au_br_do_free(*br++);
2018 +}
2019 +
2020 +/*
2021 + * find the index of a branch which is specified by @br_id.
2022 + */
2023 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2024 +{
2025 +       aufs_bindex_t bindex, bend;
2026 +
2027 +       bend = au_sbend(sb);
2028 +       for (bindex = 0; bindex <= bend; bindex++)
2029 +               if (au_sbr_id(sb, bindex) == br_id)
2030 +                       return bindex;
2031 +       return -1;
2032 +}
2033 +
2034 +/* ---------------------------------------------------------------------- */
2035 +
2036 +/*
2037 + * add a branch
2038 + */
2039 +
2040 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2041 +                       struct dentry *h_root)
2042 +{
2043 +       if (unlikely(h_adding == h_root
2044 +                    || au_test_loopback_overlap(sb, h_adding)))
2045 +               return 1;
2046 +       if (h_adding->d_sb != h_root->d_sb)
2047 +               return 0;
2048 +       return au_test_subdir(h_adding, h_root)
2049 +               || au_test_subdir(h_root, h_adding);
2050 +}
2051 +
2052 +/*
2053 + * returns a newly allocated branch. @new_nbranch is a number of branches
2054 + * after adding a branch.
2055 + */
2056 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2057 +                                    int perm)
2058 +{
2059 +       struct au_branch *add_branch;
2060 +       struct dentry *root;
2061 +       int err;
2062 +
2063 +       err = -ENOMEM;
2064 +       root = sb->s_root;
2065 +       add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
2066 +       if (unlikely(!add_branch))
2067 +               goto out;
2068 +
2069 +       err = au_hnotify_init_br(add_branch, perm);
2070 +       if (unlikely(err))
2071 +               goto out_br;
2072 +
2073 +       add_branch->br_wbr = NULL;
2074 +       if (au_br_writable(perm)) {
2075 +               /* may be freed separately at changing the branch permission */
2076 +               add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
2077 +                                            GFP_NOFS);
2078 +               if (unlikely(!add_branch->br_wbr))
2079 +                       goto out_hnotify;
2080 +       }
2081 +
2082 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
2083 +       if (!err)
2084 +               err = au_di_realloc(au_di(root), new_nbranch);
2085 +       if (!err)
2086 +               err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
2087 +       if (!err)
2088 +               return add_branch; /* success */
2089 +
2090 +       kfree(add_branch->br_wbr);
2091 +
2092 +out_hnotify:
2093 +       au_hnotify_fin_br(add_branch);
2094 +out_br:
2095 +       kfree(add_branch);
2096 +out:
2097 +       return ERR_PTR(err);
2098 +}
2099 +
2100 +/*
2101 + * test if the branch permission is legal or not.
2102 + */
2103 +static int test_br(struct inode *inode, int brperm, char *path)
2104 +{
2105 +       int err;
2106 +
2107 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
2108 +       if (!err)
2109 +               goto out;
2110 +
2111 +       err = -EINVAL;
2112 +       pr_err("write permission for readonly mount or inode, %s\n", path);
2113 +
2114 +out:
2115 +       return err;
2116 +}
2117 +
2118 +/*
2119 + * returns:
2120 + * 0: success, the caller will add it
2121 + * plus: success, it is already unified, the caller should ignore it
2122 + * minus: error
2123 + */
2124 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
2125 +{
2126 +       int err;
2127 +       aufs_bindex_t bend, bindex;
2128 +       struct dentry *root;
2129 +       struct inode *inode, *h_inode;
2130 +
2131 +       root = sb->s_root;
2132 +       bend = au_sbend(sb);
2133 +       if (unlikely(bend >= 0
2134 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
2135 +               err = 1;
2136 +               if (!remount) {
2137 +                       err = -EINVAL;
2138 +                       pr_err("%s duplicated\n", add->pathname);
2139 +               }
2140 +               goto out;
2141 +       }
2142 +
2143 +       err = -ENOSPC; /* -E2BIG; */
2144 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2145 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
2146 +               pr_err("number of branches exceeded %s\n", add->pathname);
2147 +               goto out;
2148 +       }
2149 +
2150 +       err = -EDOM;
2151 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
2152 +               pr_err("bad index %d\n", add->bindex);
2153 +               goto out;
2154 +       }
2155 +
2156 +       inode = add->path.dentry->d_inode;
2157 +       err = -ENOENT;
2158 +       if (unlikely(!inode->i_nlink)) {
2159 +               pr_err("no existence %s\n", add->pathname);
2160 +               goto out;
2161 +       }
2162 +
2163 +       err = -EINVAL;
2164 +       if (unlikely(inode->i_sb == sb)) {
2165 +               pr_err("%s must be outside\n", add->pathname);
2166 +               goto out;
2167 +       }
2168 +
2169 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
2170 +               pr_err("unsupported filesystem, %s (%s)\n",
2171 +                      add->pathname, au_sbtype(inode->i_sb));
2172 +               goto out;
2173 +       }
2174 +
2175 +       err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
2176 +       if (unlikely(err))
2177 +               goto out;
2178 +
2179 +       if (bend < 0)
2180 +               return 0; /* success */
2181 +
2182 +       err = -EINVAL;
2183 +       for (bindex = 0; bindex <= bend; bindex++)
2184 +               if (unlikely(test_overlap(sb, add->path.dentry,
2185 +                                         au_h_dptr(root, bindex)))) {
2186 +                       pr_err("%s is overlapped\n", add->pathname);
2187 +                       goto out;
2188 +               }
2189 +
2190 +       err = 0;
2191 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
2192 +               h_inode = au_h_dptr(root, 0)->d_inode;
2193 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
2194 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
2195 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
2196 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
2197 +                               add->pathname,
2198 +                               i_uid_read(inode), i_gid_read(inode),
2199 +                               (inode->i_mode & S_IALLUGO),
2200 +                               i_uid_read(h_inode), i_gid_read(h_inode),
2201 +                               (h_inode->i_mode & S_IALLUGO));
2202 +       }
2203 +
2204 +out:
2205 +       return err;
2206 +}
2207 +
2208 +/*
2209 + * initialize or clean the whiteouts for an adding branch
2210 + */
2211 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
2212 +                        int new_perm)
2213 +{
2214 +       int err, old_perm;
2215 +       aufs_bindex_t bindex;
2216 +       struct mutex *h_mtx;
2217 +       struct au_wbr *wbr;
2218 +       struct au_hinode *hdir;
2219 +
2220 +       err = vfsub_mnt_want_write(au_br_mnt(br));
2221 +       if (unlikely(err))
2222 +               goto out;
2223 +
2224 +       wbr = br->br_wbr;
2225 +       old_perm = br->br_perm;
2226 +       br->br_perm = new_perm;
2227 +       hdir = NULL;
2228 +       h_mtx = NULL;
2229 +       bindex = au_br_index(sb, br->br_id);
2230 +       if (0 <= bindex) {
2231 +               hdir = au_hi(sb->s_root->d_inode, bindex);
2232 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
2233 +       } else {
2234 +               h_mtx = &au_br_dentry(br)->d_inode->i_mutex;
2235 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
2236 +       }
2237 +       if (!wbr)
2238 +               err = au_wh_init(br, sb);
2239 +       else {
2240 +               wbr_wh_write_lock(wbr);
2241 +               err = au_wh_init(br, sb);
2242 +               wbr_wh_write_unlock(wbr);
2243 +       }
2244 +       if (hdir)
2245 +               au_hn_imtx_unlock(hdir);
2246 +       else
2247 +               mutex_unlock(h_mtx);
2248 +       vfsub_mnt_drop_write(au_br_mnt(br));
2249 +       br->br_perm = old_perm;
2250 +
2251 +       if (!err && wbr && !au_br_writable(new_perm)) {
2252 +               kfree(wbr);
2253 +               br->br_wbr = NULL;
2254 +       }
2255 +
2256 +out:
2257 +       return err;
2258 +}
2259 +
2260 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
2261 +                      int perm)
2262 +{
2263 +       int err;
2264 +       struct kstatfs kst;
2265 +       struct au_wbr *wbr;
2266 +
2267 +       wbr = br->br_wbr;
2268 +       au_rw_init(&wbr->wbr_wh_rwsem);
2269 +       memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
2270 +       atomic_set(&wbr->wbr_wh_running, 0);
2271 +       wbr->wbr_bytes = 0;
2272 +
2273 +       /*
2274 +        * a limit for rmdir/rename a dir
2275 +        * cf. AUFS_MAX_NAMELEN in include/linux/aufs_type.h
2276 +        */
2277 +       err = vfs_statfs(&br->br_path, &kst);
2278 +       if (unlikely(err))
2279 +               goto out;
2280 +       err = -EINVAL;
2281 +       if (kst.f_namelen >= NAME_MAX)
2282 +               err = au_br_init_wh(sb, br, perm);
2283 +       else
2284 +               pr_err("%.*s(%s), unsupported namelen %ld\n",
2285 +                      AuDLNPair(au_br_dentry(br)),
2286 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
2287 +
2288 +out:
2289 +       return err;
2290 +}
2291 +
2292 +/* intialize a new branch */
2293 +static int au_br_init(struct au_branch *br, struct super_block *sb,
2294 +                     struct au_opt_add *add)
2295 +{
2296 +       int err;
2297 +
2298 +       err = 0;
2299 +       memset(&br->br_xino, 0, sizeof(br->br_xino));
2300 +       mutex_init(&br->br_xino.xi_nondir_mtx);
2301 +       br->br_perm = add->perm;
2302 +       BUILD_BUG_ON(sizeof(br->br_dflags)
2303 +                    != sizeof(br->br_path.dentry->d_flags));
2304 +       br->br_dflags = DCACHE_MOUNTED;
2305 +       br->br_path = add->path; /* set first, path_get() later */
2306 +       spin_lock_init(&br->br_dykey_lock);
2307 +       memset(br->br_dykey, 0, sizeof(br->br_dykey));
2308 +       atomic_set(&br->br_count, 0);
2309 +       br->br_xino_upper = AUFS_XINO_TRUNC_INIT;
2310 +       atomic_set(&br->br_xino_running, 0);
2311 +       br->br_id = au_new_br_id(sb);
2312 +       AuDebugOn(br->br_id < 0);
2313 +
2314 +       if (au_br_writable(add->perm)) {
2315 +               err = au_wbr_init(br, sb, add->perm);
2316 +               if (unlikely(err))
2317 +                       goto out_err;
2318 +       }
2319 +
2320 +       if (au_opt_test(au_mntflags(sb), XINO)) {
2321 +               err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
2322 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
2323 +               if (unlikely(err)) {
2324 +                       AuDebugOn(br->br_xino.xi_file);
2325 +                       goto out_err;
2326 +               }
2327 +       }
2328 +
2329 +       sysaufs_br_init(br);
2330 +       path_get(&br->br_path);
2331 +       goto out; /* success */
2332 +
2333 +out_err:
2334 +       memset(&br->br_path, 0, sizeof(br->br_path));
2335 +out:
2336 +       return err;
2337 +}
2338 +
2339 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
2340 +                            struct au_branch *br, aufs_bindex_t bend,
2341 +                            aufs_bindex_t amount)
2342 +{
2343 +       struct au_branch **brp;
2344 +
2345 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2346 +
2347 +       brp = sbinfo->si_branch + bindex;
2348 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
2349 +       *brp = br;
2350 +       sbinfo->si_bend++;
2351 +       if (unlikely(bend < 0))
2352 +               sbinfo->si_bend = 0;
2353 +}
2354 +
2355 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
2356 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2357 +{
2358 +       struct au_hdentry *hdp;
2359 +
2360 +       AuRwMustWriteLock(&dinfo->di_rwsem);
2361 +
2362 +       hdp = dinfo->di_hdentry + bindex;
2363 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
2364 +       au_h_dentry_init(hdp);
2365 +       dinfo->di_bend++;
2366 +       if (unlikely(bend < 0))
2367 +               dinfo->di_bstart = 0;
2368 +}
2369 +
2370 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
2371 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2372 +{
2373 +       struct au_hinode *hip;
2374 +
2375 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
2376 +
2377 +       hip = iinfo->ii_hinode + bindex;
2378 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
2379 +       hip->hi_inode = NULL;
2380 +       au_hn_init(hip);
2381 +       iinfo->ii_bend++;
2382 +       if (unlikely(bend < 0))
2383 +               iinfo->ii_bstart = 0;
2384 +}
2385 +
2386 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
2387 +                        aufs_bindex_t bindex)
2388 +{
2389 +       struct dentry *root, *h_dentry;
2390 +       struct inode *root_inode;
2391 +       aufs_bindex_t bend, amount;
2392 +
2393 +       au_br_dflags_force(br);
2394 +
2395 +       root = sb->s_root;
2396 +       root_inode = root->d_inode;
2397 +       bend = au_sbend(sb);
2398 +       amount = bend + 1 - bindex;
2399 +       h_dentry = au_br_dentry(br);
2400 +       au_sbilist_lock();
2401 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
2402 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
2403 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
2404 +       au_set_h_dptr(root, bindex, dget(h_dentry));
2405 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
2406 +                     /*flags*/0);
2407 +       au_sbilist_unlock();
2408 +}
2409 +
2410 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
2411 +{
2412 +       int err;
2413 +       aufs_bindex_t bend, add_bindex;
2414 +       struct dentry *root, *h_dentry;
2415 +       struct inode *root_inode;
2416 +       struct au_branch *add_branch;
2417 +
2418 +       root = sb->s_root;
2419 +       root_inode = root->d_inode;
2420 +       IMustLock(root_inode);
2421 +       err = test_add(sb, add, remount);
2422 +       if (unlikely(err < 0))
2423 +               goto out;
2424 +       if (err) {
2425 +               err = 0;
2426 +               goto out; /* success */
2427 +       }
2428 +
2429 +       bend = au_sbend(sb);
2430 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
2431 +       err = PTR_ERR(add_branch);
2432 +       if (IS_ERR(add_branch))
2433 +               goto out;
2434 +
2435 +       err = au_br_init(add_branch, sb, add);
2436 +       if (unlikely(err)) {
2437 +               au_br_do_free(add_branch);
2438 +               goto out;
2439 +       }
2440 +
2441 +       add_bindex = add->bindex;
2442 +       if (!remount)
2443 +               au_br_do_add(sb, add_branch, add_bindex);
2444 +       else {
2445 +               sysaufs_brs_del(sb, add_bindex);
2446 +               au_br_do_add(sb, add_branch, add_bindex);
2447 +               sysaufs_brs_add(sb, add_bindex);
2448 +       }
2449 +
2450 +       h_dentry = add->path.dentry;
2451 +       if (!add_bindex) {
2452 +               au_cpup_attr_all(root_inode, /*force*/1);
2453 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
2454 +       } else
2455 +               au_add_nlink(root_inode, h_dentry->d_inode);
2456 +
2457 +       /*
2458 +        * this test/set prevents aufs from handling unnecesary notify events
2459 +        * of xino files, in case of re-adding a writable branch which was
2460 +        * once detached from aufs.
2461 +        */
2462 +       if (au_xino_brid(sb) < 0
2463 +           && au_br_writable(add_branch->br_perm)
2464 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
2465 +           && add_branch->br_xino.xi_file
2466 +           && add_branch->br_xino.xi_file->f_dentry->d_parent == h_dentry)
2467 +               au_xino_brid_set(sb, add_branch->br_id);
2468 +
2469 +out:
2470 +       return err;
2471 +}
2472 +
2473 +/* ---------------------------------------------------------------------- */
2474 +
2475 +/*
2476 + * delete a branch
2477 + */
2478 +
2479 +/* to show the line number, do not make it inlined function */
2480 +#define AuVerbose(do_info, fmt, ...) do { \
2481 +       if (do_info) \
2482 +               pr_info(fmt, ##__VA_ARGS__); \
2483 +} while (0)
2484 +
2485 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
2486 +                        aufs_bindex_t bend)
2487 +{
2488 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
2489 +}
2490 +
2491 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
2492 +                        aufs_bindex_t bend)
2493 +{
2494 +       return au_test_ibusy(dentry->d_inode, bstart, bend);
2495 +}
2496 +
2497 +/*
2498 + * test if the branch is deletable or not.
2499 + */
2500 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
2501 +                           unsigned int sigen, const unsigned int verbose)
2502 +{
2503 +       int err, i, j, ndentry;
2504 +       aufs_bindex_t bstart, bend;
2505 +       struct au_dcsub_pages dpages;
2506 +       struct au_dpage *dpage;
2507 +       struct dentry *d;
2508 +
2509 +       err = au_dpages_init(&dpages, GFP_NOFS);
2510 +       if (unlikely(err))
2511 +               goto out;
2512 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
2513 +       if (unlikely(err))
2514 +               goto out_dpages;
2515 +
2516 +       for (i = 0; !err && i < dpages.ndpage; i++) {
2517 +               dpage = dpages.dpages + i;
2518 +               ndentry = dpage->ndentry;
2519 +               for (j = 0; !err && j < ndentry; j++) {
2520 +                       d = dpage->dentries[j];
2521 +                       AuDebugOn(!d->d_count);
2522 +                       if (!au_digen_test(d, sigen)) {
2523 +                               di_read_lock_child(d, AuLock_IR);
2524 +                               if (unlikely(au_dbrange_test(d))) {
2525 +                                       di_read_unlock(d, AuLock_IR);
2526 +                                       continue;
2527 +                               }
2528 +                       } else {
2529 +                               di_write_lock_child(d);
2530 +                               if (unlikely(au_dbrange_test(d))) {
2531 +                                       di_write_unlock(d);
2532 +                                       continue;
2533 +                               }
2534 +                               err = au_reval_dpath(d, sigen);
2535 +                               if (!err)
2536 +                                       di_downgrade_lock(d, AuLock_IR);
2537 +                               else {
2538 +                                       di_write_unlock(d);
2539 +                                       break;
2540 +                               }
2541 +                       }
2542 +
2543 +                       /* AuDbgDentry(d); */
2544 +                       bstart = au_dbstart(d);
2545 +                       bend = au_dbend(d);
2546 +                       if (bstart <= bindex
2547 +                           && bindex <= bend
2548 +                           && au_h_dptr(d, bindex)
2549 +                           && au_test_dbusy(d, bstart, bend)) {
2550 +                               err = -EBUSY;
2551 +                               AuVerbose(verbose, "busy %.*s\n", AuDLNPair(d));
2552 +                               AuDbgDentry(d);
2553 +                       }
2554 +                       di_read_unlock(d, AuLock_IR);
2555 +               }
2556 +       }
2557 +
2558 +out_dpages:
2559 +       au_dpages_free(&dpages);
2560 +out:
2561 +       return err;
2562 +}
2563 +
2564 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
2565 +                          unsigned int sigen, const unsigned int verbose)
2566 +{
2567 +       int err;
2568 +       unsigned long long max, ull;
2569 +       struct inode *i, **array;
2570 +       aufs_bindex_t bstart, bend;
2571 +
2572 +       array = au_iarray_alloc(sb, &max);
2573 +       err = PTR_ERR(array);
2574 +       if (IS_ERR(array))
2575 +               goto out;
2576 +
2577 +       err = 0;
2578 +       AuDbg("b%d\n", bindex);
2579 +       for (ull = 0; !err && ull < max; ull++) {
2580 +               i = array[ull];
2581 +               if (i->i_ino == AUFS_ROOT_INO)
2582 +                       continue;
2583 +
2584 +               /* AuDbgInode(i); */
2585 +               if (au_iigen(i, NULL) == sigen)
2586 +                       ii_read_lock_child(i);
2587 +               else {
2588 +                       ii_write_lock_child(i);
2589 +                       err = au_refresh_hinode_self(i);
2590 +                       au_iigen_dec(i);
2591 +                       if (!err)
2592 +                               ii_downgrade_lock(i);
2593 +                       else {
2594 +                               ii_write_unlock(i);
2595 +                               break;
2596 +                       }
2597 +               }
2598 +
2599 +               bstart = au_ibstart(i);
2600 +               bend = au_ibend(i);
2601 +               if (bstart <= bindex
2602 +                   && bindex <= bend
2603 +                   && au_h_iptr(i, bindex)
2604 +                   && au_test_ibusy(i, bstart, bend)) {
2605 +                       err = -EBUSY;
2606 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
2607 +                       AuDbgInode(i);
2608 +               }
2609 +               ii_read_unlock(i);
2610 +       }
2611 +       au_iarray_free(array, max);
2612 +
2613 +out:
2614 +       return err;
2615 +}
2616 +
2617 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
2618 +                             const unsigned int verbose)
2619 +{
2620 +       int err;
2621 +       unsigned int sigen;
2622 +
2623 +       sigen = au_sigen(root->d_sb);
2624 +       DiMustNoWaiters(root);
2625 +       IiMustNoWaiters(root->d_inode);
2626 +       di_write_unlock(root);
2627 +       err = test_dentry_busy(root, bindex, sigen, verbose);
2628 +       if (!err)
2629 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
2630 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
2631 +
2632 +       return err;
2633 +}
2634 +
2635 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
2636 +                            const aufs_bindex_t bindex,
2637 +                            const aufs_bindex_t bend)
2638 +{
2639 +       struct au_branch **brp, **p;
2640 +
2641 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2642 +
2643 +       brp = sbinfo->si_branch + bindex;
2644 +       if (bindex < bend)
2645 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
2646 +       sbinfo->si_branch[0 + bend] = NULL;
2647 +       sbinfo->si_bend--;
2648 +
2649 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
2650 +       if (p)
2651 +               sbinfo->si_branch = p;
2652 +       /* harmless error */
2653 +}
2654 +
2655 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
2656 +                            const aufs_bindex_t bend)
2657 +{
2658 +       struct au_hdentry *hdp, *p;
2659 +
2660 +       AuRwMustWriteLock(&dinfo->di_rwsem);
2661 +
2662 +       hdp = dinfo->di_hdentry;
2663 +       if (bindex < bend)
2664 +               memmove(hdp + bindex, hdp + bindex + 1,
2665 +                       sizeof(*hdp) * (bend - bindex));
2666 +       hdp[0 + bend].hd_dentry = NULL;
2667 +       dinfo->di_bend--;
2668 +
2669 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
2670 +       if (p)
2671 +               dinfo->di_hdentry = p;
2672 +       /* harmless error */
2673 +}
2674 +
2675 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
2676 +                            const aufs_bindex_t bend)
2677 +{
2678 +       struct au_hinode *hip, *p;
2679 +
2680 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
2681 +
2682 +       hip = iinfo->ii_hinode + bindex;
2683 +       if (bindex < bend)
2684 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
2685 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
2686 +       au_hn_init(iinfo->ii_hinode + bend);
2687 +       iinfo->ii_bend--;
2688 +
2689 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
2690 +       if (p)
2691 +               iinfo->ii_hinode = p;
2692 +       /* harmless error */
2693 +}
2694 +
2695 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
2696 +                        struct au_branch *br)
2697 +{
2698 +       aufs_bindex_t bend;
2699 +       struct au_sbinfo *sbinfo;
2700 +       struct dentry *root, *h_root;
2701 +       struct inode *inode, *h_inode;
2702 +       struct au_hinode *hinode;
2703 +
2704 +       SiMustWriteLock(sb);
2705 +
2706 +       root = sb->s_root;
2707 +       inode = root->d_inode;
2708 +       sbinfo = au_sbi(sb);
2709 +       bend = sbinfo->si_bend;
2710 +
2711 +       h_root = au_h_dptr(root, bindex);
2712 +       hinode = au_hi(inode, bindex);
2713 +       h_inode = au_igrab(hinode->hi_inode);
2714 +       au_hiput(hinode);
2715 +
2716 +       au_sbilist_lock();
2717 +       au_br_do_del_brp(sbinfo, bindex, bend);
2718 +       au_br_do_del_hdp(au_di(root), bindex, bend);
2719 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
2720 +       au_sbilist_unlock();
2721 +
2722 +       dput(h_root);
2723 +       iput(h_inode);
2724 +       au_br_do_free(br);
2725 +}
2726 +
2727 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
2728 +{
2729 +       int err, rerr, i;
2730 +       unsigned int mnt_flags;
2731 +       aufs_bindex_t bindex, bend, br_id;
2732 +       unsigned char do_wh, verbose;
2733 +       struct au_branch *br;
2734 +       struct au_wbr *wbr;
2735 +
2736 +       err = 0;
2737 +       bindex = au_find_dbindex(sb->s_root, del->h_path.dentry);
2738 +       if (bindex < 0) {
2739 +               if (remount)
2740 +                       goto out; /* success */
2741 +               err = -ENOENT;
2742 +               pr_err("%s no such branch\n", del->pathname);
2743 +               goto out;
2744 +       }
2745 +       AuDbg("bindex b%d\n", bindex);
2746 +
2747 +       err = -EBUSY;
2748 +       mnt_flags = au_mntflags(sb);
2749 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
2750 +       bend = au_sbend(sb);
2751 +       if (unlikely(!bend)) {
2752 +               AuVerbose(verbose, "no more branches left\n");
2753 +               goto out;
2754 +       }
2755 +       br = au_sbr(sb, bindex);
2756 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
2757 +       i = atomic_read(&br->br_count);
2758 +       if (unlikely(i)) {
2759 +               AuVerbose(verbose, "%d file(s) opened\n", i);
2760 +               goto out;
2761 +       }
2762 +
2763 +       wbr = br->br_wbr;
2764 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
2765 +       if (do_wh) {
2766 +               /* instead of WbrWhMustWriteLock(wbr) */
2767 +               SiMustWriteLock(sb);
2768 +               for (i = 0; i < AuBrWh_Last; i++) {
2769 +                       dput(wbr->wbr_wh[i]);
2770 +                       wbr->wbr_wh[i] = NULL;
2771 +               }
2772 +       }
2773 +
2774 +       err = test_children_busy(sb->s_root, bindex, verbose);
2775 +       if (unlikely(err)) {
2776 +               if (do_wh)
2777 +                       goto out_wh;
2778 +               goto out;
2779 +       }
2780 +
2781 +       err = 0;
2782 +       br_id = br->br_id;
2783 +       if (!remount)
2784 +               au_br_do_del(sb, bindex, br);
2785 +       else {
2786 +               sysaufs_brs_del(sb, bindex);
2787 +               au_br_do_del(sb, bindex, br);
2788 +               sysaufs_brs_add(sb, bindex);
2789 +       }
2790 +
2791 +       if (!bindex) {
2792 +               au_cpup_attr_all(sb->s_root->d_inode, /*force*/1);
2793 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
2794 +       } else
2795 +               au_sub_nlink(sb->s_root->d_inode, del->h_path.dentry->d_inode);
2796 +       if (au_opt_test(mnt_flags, PLINK))
2797 +               au_plink_half_refresh(sb, br_id);
2798 +
2799 +       if (au_xino_brid(sb) == br_id)
2800 +               au_xino_brid_set(sb, -1);
2801 +       goto out; /* success */
2802 +
2803 +out_wh:
2804 +       /* revert */
2805 +       rerr = au_br_init_wh(sb, br, br->br_perm);
2806 +       if (rerr)
2807 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
2808 +                       del->pathname, rerr);
2809 +out:
2810 +       return err;
2811 +}
2812 +
2813 +/* ---------------------------------------------------------------------- */
2814 +
2815 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
2816 +{
2817 +       int err;
2818 +       aufs_bindex_t bstart, bend;
2819 +       struct aufs_ibusy ibusy;
2820 +       struct inode *inode, *h_inode;
2821 +
2822 +       err = -EPERM;
2823 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
2824 +               goto out;
2825 +
2826 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
2827 +       if (!err)
2828 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
2829 +       if (unlikely(err)) {
2830 +               err = -EFAULT;
2831 +               AuTraceErr(err);
2832 +               goto out;
2833 +       }
2834 +
2835 +       err = -EINVAL;
2836 +       si_read_lock(sb, AuLock_FLUSH);
2837 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
2838 +               goto out_unlock;
2839 +
2840 +       err = 0;
2841 +       ibusy.h_ino = 0; /* invalid */
2842 +       inode = ilookup(sb, ibusy.ino);
2843 +       if (!inode
2844 +           || inode->i_ino == AUFS_ROOT_INO
2845 +           || is_bad_inode(inode))
2846 +               goto out_unlock;
2847 +
2848 +       ii_read_lock_child(inode);
2849 +       bstart = au_ibstart(inode);
2850 +       bend = au_ibend(inode);
2851 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
2852 +               h_inode = au_h_iptr(inode, ibusy.bindex);
2853 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
2854 +                       ibusy.h_ino = h_inode->i_ino;
2855 +       }
2856 +       ii_read_unlock(inode);
2857 +       iput(inode);
2858 +
2859 +out_unlock:
2860 +       si_read_unlock(sb);
2861 +       if (!err) {
2862 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
2863 +               if (unlikely(err)) {
2864 +                       err = -EFAULT;
2865 +                       AuTraceErr(err);
2866 +               }
2867 +       }
2868 +out:
2869 +       return err;
2870 +}
2871 +
2872 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
2873 +{
2874 +       return au_ibusy(file->f_dentry->d_sb, (void __user *)arg);
2875 +}
2876 +
2877 +#ifdef CONFIG_COMPAT
2878 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
2879 +{
2880 +       return au_ibusy(file->f_dentry->d_sb, compat_ptr(arg));
2881 +}
2882 +#endif
2883 +
2884 +/* ---------------------------------------------------------------------- */
2885 +
2886 +/*
2887 + * change a branch permission
2888 + */
2889 +
2890 +static void au_warn_ima(void)
2891 +{
2892 +#ifdef CONFIG_IMA
2893 +       /* since it doesn't support mark_files_ro() */
2894 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
2895 +#endif
2896 +}
2897 +
2898 +static int do_need_sigen_inc(int a, int b)
2899 +{
2900 +       return au_br_whable(a) && !au_br_whable(b);
2901 +}
2902 +
2903 +static int need_sigen_inc(int old, int new)
2904 +{
2905 +       return do_need_sigen_inc(old, new)
2906 +               || do_need_sigen_inc(new, old);
2907 +}
2908 +
2909 +static unsigned long long au_farray_cb(void *a,
2910 +                                      unsigned long long max __maybe_unused,
2911 +                                      void *arg)
2912 +{
2913 +       unsigned long long n;
2914 +       struct file **p, *f;
2915 +       struct super_block *sb = arg;
2916 +
2917 +       n = 0;
2918 +       p = a;
2919 +       lg_global_lock(&files_lglock);
2920 +       do_file_list_for_each_entry(sb, f) {
2921 +               if (au_fi(f)
2922 +                   && file_count(f)
2923 +                   && !special_file(file_inode(f)->i_mode)) {
2924 +                       get_file(f);
2925 +                       *p++ = f;
2926 +                       n++;
2927 +                       AuDebugOn(n > max);
2928 +               }
2929 +       } while_file_list_for_each_entry;
2930 +       lg_global_unlock(&files_lglock);
2931 +
2932 +       return n;
2933 +}
2934 +
2935 +static struct file **au_farray_alloc(struct super_block *sb,
2936 +                                    unsigned long long *max)
2937 +{
2938 +       *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
2939 +       return au_array_alloc(max, au_farray_cb, sb);
2940 +}
2941 +
2942 +static void au_farray_free(struct file **a, unsigned long long max)
2943 +{
2944 +       unsigned long long ull;
2945 +
2946 +       for (ull = 0; ull < max; ull++)
2947 +               if (a[ull])
2948 +                       fput(a[ull]);
2949 +       au_array_free(a);
2950 +}
2951 +
2952 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
2953 +{
2954 +       int err, do_warn;
2955 +       unsigned int mnt_flags;
2956 +       unsigned long long ull, max;
2957 +       aufs_bindex_t br_id;
2958 +       unsigned char verbose;
2959 +       struct file *file, *hf, **array;
2960 +       struct inode *inode;
2961 +       struct au_hfile *hfile;
2962 +
2963 +       mnt_flags = au_mntflags(sb);
2964 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
2965 +
2966 +       array = au_farray_alloc(sb, &max);
2967 +       err = PTR_ERR(array);
2968 +       if (IS_ERR(array))
2969 +               goto out;
2970 +
2971 +       do_warn = 0;
2972 +       br_id = au_sbr_id(sb, bindex);
2973 +       for (ull = 0; ull < max; ull++) {
2974 +               file = array[ull];
2975 +
2976 +               /* AuDbg("%.*s\n", AuDLNPair(file->f_dentry)); */
2977 +               fi_read_lock(file);
2978 +               if (unlikely(au_test_mmapped(file))) {
2979 +                       err = -EBUSY;
2980 +                       AuVerbose(verbose, "mmapped %.*s\n",
2981 +                                 AuDLNPair(file->f_dentry));
2982 +                       AuDbgFile(file);
2983 +                       FiMustNoWaiters(file);
2984 +                       fi_read_unlock(file);
2985 +                       goto out_array;
2986 +               }
2987 +
2988 +               inode = file_inode(file);
2989 +               hfile = &au_fi(file)->fi_htop;
2990 +               hf = hfile->hf_file;
2991 +               if (!S_ISREG(inode->i_mode)
2992 +                   || !(file->f_mode & FMODE_WRITE)
2993 +                   || hfile->hf_br->br_id != br_id
2994 +                   || !(hf->f_mode & FMODE_WRITE))
2995 +                       array[ull] = NULL;
2996 +               else {
2997 +                       do_warn = 1;
2998 +                       get_file(file);
2999 +               }
3000 +
3001 +               FiMustNoWaiters(file);
3002 +               fi_read_unlock(file);
3003 +               fput(file);
3004 +       }
3005 +
3006 +       err = 0;
3007 +       if (do_warn)
3008 +               au_warn_ima();
3009 +
3010 +       for (ull = 0; ull < max; ull++) {
3011 +               file = array[ull];
3012 +               if (!file)
3013 +                       continue;
3014 +
3015 +               /* todo: already flushed? */
3016 +               /* cf. fs/super.c:mark_files_ro() */
3017 +               /* fi_read_lock(file); */
3018 +               hfile = &au_fi(file)->fi_htop;
3019 +               hf = hfile->hf_file;
3020 +               /* fi_read_unlock(file); */
3021 +               spin_lock(&hf->f_lock);
3022 +               hf->f_mode &= ~FMODE_WRITE;
3023 +               spin_unlock(&hf->f_lock);
3024 +               if (!file_check_writeable(hf)) {
3025 +                       __mnt_drop_write(hf->f_path.mnt);
3026 +                       file_release_write(hf);
3027 +               }
3028 +       }
3029 +
3030 +out_array:
3031 +       au_farray_free(array, max);
3032 +out:
3033 +       AuTraceErr(err);
3034 +       return err;
3035 +}
3036 +
3037 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3038 +             int *do_refresh)
3039 +{
3040 +       int err, rerr;
3041 +       aufs_bindex_t bindex;
3042 +       struct dentry *root;
3043 +       struct au_branch *br;
3044 +
3045 +       root = sb->s_root;
3046 +       bindex = au_find_dbindex(root, mod->h_root);
3047 +       if (bindex < 0) {
3048 +               if (remount)
3049 +                       return 0; /* success */
3050 +               err = -ENOENT;
3051 +               pr_err("%s no such branch\n", mod->path);
3052 +               goto out;
3053 +       }
3054 +       AuDbg("bindex b%d\n", bindex);
3055 +
3056 +       err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
3057 +       if (unlikely(err))
3058 +               goto out;
3059 +
3060 +       br = au_sbr(sb, bindex);
3061 +       AuDebugOn(mod->h_root != au_br_dentry(br));
3062 +       if (br->br_perm == mod->perm)
3063 +               return 0; /* success */
3064 +
3065 +       if (au_br_writable(br->br_perm)) {
3066 +               /* remove whiteout base */
3067 +               err = au_br_init_wh(sb, br, mod->perm);
3068 +               if (unlikely(err))
3069 +                       goto out;
3070 +
3071 +               if (!au_br_writable(mod->perm)) {
3072 +                       /* rw --> ro, file might be mmapped */
3073 +                       DiMustNoWaiters(root);
3074 +                       IiMustNoWaiters(root->d_inode);
3075 +                       di_write_unlock(root);
3076 +                       err = au_br_mod_files_ro(sb, bindex);
3077 +                       /* aufs_write_lock() calls ..._child() */
3078 +                       di_write_lock_child(root);
3079 +
3080 +                       if (unlikely(err)) {
3081 +                               rerr = -ENOMEM;
3082 +                               br->br_wbr = kmalloc(sizeof(*br->br_wbr),
3083 +                                                    GFP_NOFS);
3084 +                               if (br->br_wbr)
3085 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
3086 +                               if (unlikely(rerr)) {
3087 +                                       AuIOErr("nested error %d (%d)\n",
3088 +                                               rerr, err);
3089 +                                       br->br_perm = mod->perm;
3090 +                               }
3091 +                       }
3092 +               }
3093 +       } else if (au_br_writable(mod->perm)) {
3094 +               /* ro --> rw */
3095 +               err = -ENOMEM;
3096 +               br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
3097 +               if (br->br_wbr) {
3098 +                       err = au_wbr_init(br, sb, mod->perm);
3099 +                       if (unlikely(err)) {
3100 +                               kfree(br->br_wbr);
3101 +                               br->br_wbr = NULL;
3102 +                       }
3103 +               }
3104 +       }
3105 +
3106 +       if (!err) {
3107 +               if ((br->br_perm & AuBrAttr_UNPIN)
3108 +                   && !(mod->perm & AuBrAttr_UNPIN))
3109 +                       au_br_dflags_force(br);
3110 +               else if (!(br->br_perm & AuBrAttr_UNPIN)
3111 +                        && (mod->perm & AuBrAttr_UNPIN))
3112 +                       au_br_dflags_restore(br);
3113 +               *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
3114 +               br->br_perm = mod->perm;
3115 +       }
3116 +
3117 +out:
3118 +       AuTraceErr(err);
3119 +       return err;
3120 +}
3121 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
3122 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
3123 +++ linux/fs/aufs/branch.h      2013-07-06 13:20:47.740198107 +0200
3124 @@ -0,0 +1,255 @@
3125 +/*
3126 + * Copyright (C) 2005-2013 Junjiro R. Okajima
3127 + *
3128 + * This program, aufs is free software; you can redistribute it and/or modify
3129 + * it under the terms of the GNU General Public License as published by
3130 + * the Free Software Foundation; either version 2 of the License, or
3131 + * (at your option) any later version.
3132 + *
3133 + * This program is distributed in the hope that it will be useful,
3134 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3135 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3136 + * GNU General Public License for more details.
3137 + *
3138 + * You should have received a copy of the GNU General Public License
3139 + * along with this program; if not, write to the Free Software
3140 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
3141 + */
3142 +
3143 +/*
3144 + * branch filesystems and xino for them
3145 + */
3146 +
3147 +#ifndef __AUFS_BRANCH_H__
3148 +#define __AUFS_BRANCH_H__
3149 +
3150 +#ifdef __KERNEL__
3151 +
3152 +#include <linux/mount.h>
3153 +#include "dynop.h"
3154 +#include "rwsem.h"
3155 +#include "super.h"
3156 +
3157 +/* ---------------------------------------------------------------------- */
3158 +
3159 +/* a xino file */
3160 +struct au_xino_file {
3161 +       struct file             *xi_file;
3162 +       struct mutex            xi_nondir_mtx;
3163 +
3164 +       /* todo: make xino files an array to support huge inode number */
3165 +
3166 +#ifdef CONFIG_DEBUG_FS
3167 +       struct dentry            *xi_dbgaufs;
3168 +#endif
3169 +};
3170 +
3171 +/* members for writable branch only */
3172 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
3173 +struct au_wbr {
3174 +       struct au_rwsem         wbr_wh_rwsem;
3175 +       struct dentry           *wbr_wh[AuBrWh_Last];
3176 +       atomic_t                wbr_wh_running;
3177 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
3178 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
3179 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
3180 +
3181 +       /* mfs mode */
3182 +       unsigned long long      wbr_bytes;
3183 +};
3184 +
3185 +/* ext2 has 3 types of operations at least, ext3 has 4 */
3186 +#define AuBrDynOp (AuDyLast * 4)
3187 +
3188 +#ifdef CONFIG_AUFS_HFSNOTIFY
3189 +/* support for asynchronous destruction */
3190 +struct au_br_hfsnotify {
3191 +       struct fsnotify_group   *hfsn_group;
3192 +};
3193 +#endif
3194 +
3195 +/* protected by superblock rwsem */
3196 +struct au_branch {
3197 +       struct au_xino_file     br_xino;
3198 +
3199 +       aufs_bindex_t           br_id;
3200 +
3201 +       int                     br_perm;
3202 +       unsigned int            br_dflags;
3203 +       struct path             br_path;
3204 +       spinlock_t              br_dykey_lock;
3205 +       struct au_dykey         *br_dykey[AuBrDynOp];
3206 +       atomic_t                br_count;
3207 +
3208 +       struct au_wbr           *br_wbr;
3209 +
3210 +       /* xino truncation */
3211 +       blkcnt_t                br_xino_upper;  /* watermark in blocks */
3212 +       atomic_t                br_xino_running;
3213 +
3214 +#ifdef CONFIG_AUFS_HFSNOTIFY
3215 +       struct au_br_hfsnotify  *br_hfsn;
3216 +#endif
3217 +
3218 +#ifdef CONFIG_SYSFS
3219 +       /* an entry under sysfs per mount-point */
3220 +       char                    br_name[8];
3221 +       struct attribute        br_attr;
3222 +#endif
3223 +};
3224 +
3225 +/* ---------------------------------------------------------------------- */
3226 +
3227 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
3228 +{
3229 +       return br->br_path.mnt;
3230 +}
3231 +
3232 +static inline struct dentry *au_br_dentry(struct au_branch *br)
3233 +{
3234 +       return br->br_path.dentry;
3235 +}
3236 +
3237 +static inline struct super_block *au_br_sb(struct au_branch *br)
3238 +{
3239 +       return au_br_mnt(br)->mnt_sb;
3240 +}
3241 +
3242 +/* branch permissions and attributes */
3243 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
3244 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
3245 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
3246 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
3247 +
3248 +#define AuBrRAttr_WH           (1 << 3)        /* whiteout-able */
3249 +
3250 +#define AuBrWAttr_NoLinkWH     (1 << 4)        /* un-hardlinkable whiteouts */
3251 +
3252 +#define AuBrAttr_UNPIN         (1 << 5)        /* rename-able top dir of
3253 +                                                  branch */
3254 +
3255 +static inline int au_br_writable(int brperm)
3256 +{
3257 +       return brperm & AuBrPerm_RW;
3258 +}
3259 +
3260 +static inline int au_br_whable(int brperm)
3261 +{
3262 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
3263 +}
3264 +
3265 +static inline int au_br_wh_linkable(int brperm)
3266 +{
3267 +       return !(brperm & AuBrWAttr_NoLinkWH);
3268 +}
3269 +
3270 +static inline int au_br_rdonly(struct au_branch *br)
3271 +{
3272 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
3273 +               || !au_br_writable(br->br_perm))
3274 +               ? -EROFS : 0;
3275 +}
3276 +
3277 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
3278 +{
3279 +#ifdef CONFIG_AUFS_HNOTIFY
3280 +       return !(brperm & AuBrPerm_RR);
3281 +#else
3282 +       return 0;
3283 +#endif
3284 +}
3285 +
3286 +/* ---------------------------------------------------------------------- */
3287 +
3288 +/* branch.c */
3289 +struct au_sbinfo;
3290 +void au_br_free(struct au_sbinfo *sinfo);
3291 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
3292 +struct au_opt_add;
3293 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
3294 +struct au_opt_del;
3295 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
3296 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
3297 +#ifdef CONFIG_COMPAT
3298 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
3299 +#endif
3300 +struct au_opt_mod;
3301 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3302 +             int *do_refresh);
3303 +
3304 +/* xino.c */
3305 +static const loff_t au_loff_max = LLONG_MAX;
3306 +
3307 +int au_xib_trunc(struct super_block *sb);
3308 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
3309 +                  loff_t *pos);
3310 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
3311 +                   loff_t *pos);
3312 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
3313 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
3314 +ino_t au_xino_new_ino(struct super_block *sb);
3315 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
3316 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3317 +                 ino_t ino);
3318 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3319 +                ino_t *ino);
3320 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
3321 +              struct file *base_file, int do_test);
3322 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
3323 +
3324 +struct au_opt_xino;
3325 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
3326 +void au_xino_clr(struct super_block *sb);
3327 +struct file *au_xino_def(struct super_block *sb);
3328 +int au_xino_path(struct seq_file *seq, struct file *file);
3329 +
3330 +/* ---------------------------------------------------------------------- */
3331 +
3332 +/* Superblock to branch */
3333 +static inline
3334 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
3335 +{
3336 +       return au_sbr(sb, bindex)->br_id;
3337 +}
3338 +
3339 +static inline
3340 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
3341 +{
3342 +       return au_br_mnt(au_sbr(sb, bindex));
3343 +}
3344 +
3345 +static inline
3346 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
3347 +{
3348 +       return au_br_sb(au_sbr(sb, bindex));
3349 +}
3350 +
3351 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
3352 +{
3353 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
3354 +}
3355 +
3356 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
3357 +{
3358 +       return au_sbr(sb, bindex)->br_perm;
3359 +}
3360 +
3361 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
3362 +{
3363 +       return au_br_whable(au_sbr_perm(sb, bindex));
3364 +}
3365 +
3366 +/* ---------------------------------------------------------------------- */
3367 +
3368 +/*
3369 + * wbr_wh_read_lock, wbr_wh_write_lock
3370 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
3371 + */
3372 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
3373 +
3374 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
3375 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
3376 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
3377 +
3378 +#endif /* __KERNEL__ */
3379 +#endif /* __AUFS_BRANCH_H__ */
3380 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
3381 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
3382 +++ linux/fs/aufs/conf.mk       2013-07-06 13:20:47.740198107 +0200
3383 @@ -0,0 +1,38 @@
3384 +
3385 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
3386 +
3387 +define AuConf
3388 +ifdef ${1}
3389 +AuConfStr += ${1}=${${1}}
3390 +endif
3391 +endef
3392 +
3393 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
3394 +       SBILIST \
3395 +       HNOTIFY HFSNOTIFY \
3396 +       EXPORT INO_T_64 \
3397 +       RDU \
3398 +       PROC_MAP \
3399 +       SP_IATTR \
3400 +       SHWH \
3401 +       BR_RAMFS \
3402 +       BR_FUSE POLL \
3403 +       BR_HFSPLUS \
3404 +       BDEV_LOOP \
3405 +       DEBUG MAGIC_SYSRQ
3406 +$(foreach i, ${AuConfAll}, \
3407 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
3408 +
3409 +AuConfName = ${obj}/conf.str
3410 +${AuConfName}.tmp: FORCE
3411 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
3412 +${AuConfName}: ${AuConfName}.tmp
3413 +       @diff -q $< $@ > /dev/null 2>&1 || { \
3414 +       echo '  GEN    ' $@; \
3415 +       cp -p $< $@; \
3416 +       }
3417 +FORCE:
3418 +clean-files += ${AuConfName} ${AuConfName}.tmp
3419 +${obj}/sysfs.o: ${AuConfName}
3420 +
3421 +-include ${srctree}/${src}/conf_priv.mk
3422 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
3423 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
3424 +++ linux/fs/aufs/cpup.c        2013-08-23 23:59:39.631583456 +0200
3425 @@ -0,0 +1,1255 @@
3426 +/*
3427 + * Copyright (C) 2005-2013 Junjiro R. Okajima
3428 + *
3429 + * This program, aufs is free software; you can redistribute it and/or modify
3430 + * it under the terms of the GNU General Public License as published by
3431 + * the Free Software Foundation; either version 2 of the License, or
3432 + * (at your option) any later version.
3433 + *
3434 + * This program is distributed in the hope that it will be useful,
3435 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3436 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3437 + * GNU General Public License for more details.
3438 + *
3439 + * You should have received a copy of the GNU General Public License
3440 + * along with this program; if not, write to the Free Software
3441 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
3442 + */
3443 +
3444 +/*
3445 + * copy-up functions, see wbr_policy.c for copy-down
3446 + */
3447 +
3448 +#include <linux/fs_stack.h>
3449 +#include <linux/mm.h>
3450 +#include "aufs.h"
3451 +
3452 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
3453 +{
3454 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
3455 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
3456 +
3457 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
3458 +
3459 +       dst->i_flags |= iflags & ~mask;
3460 +       if (au_test_fs_notime(dst->i_sb))
3461 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
3462 +}
3463 +
3464 +void au_cpup_attr_timesizes(struct inode *inode)
3465 +{
3466 +       struct inode *h_inode;
3467 +
3468 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3469 +       fsstack_copy_attr_times(inode, h_inode);
3470 +       fsstack_copy_inode_size(inode, h_inode);
3471 +}
3472 +
3473 +void au_cpup_attr_nlink(struct inode *inode, int force)
3474 +{
3475 +       struct inode *h_inode;
3476 +       struct super_block *sb;
3477 +       aufs_bindex_t bindex, bend;
3478 +
3479 +       sb = inode->i_sb;
3480 +       bindex = au_ibstart(inode);
3481 +       h_inode = au_h_iptr(inode, bindex);
3482 +       if (!force
3483 +           && !S_ISDIR(h_inode->i_mode)
3484 +           && au_opt_test(au_mntflags(sb), PLINK)
3485 +           && au_plink_test(inode))
3486 +               return;
3487 +
3488 +       /*
3489 +        * 0 can happen in revalidating.
3490 +        * h_inode->i_mutex is not held, but it is harmless since once i_nlink
3491 +        * reaches 0, it will never become positive.
3492 +        */
3493 +       set_nlink(inode, h_inode->i_nlink);
3494 +
3495 +       /*
3496 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
3497 +        * it may includes whplink directory.
3498 +        */
3499 +       if (S_ISDIR(h_inode->i_mode)) {
3500 +               bend = au_ibend(inode);
3501 +               for (bindex++; bindex <= bend; bindex++) {
3502 +                       h_inode = au_h_iptr(inode, bindex);
3503 +                       if (h_inode)
3504 +                               au_add_nlink(inode, h_inode);
3505 +               }
3506 +       }
3507 +}
3508 +
3509 +void au_cpup_attr_changeable(struct inode *inode)
3510 +{
3511 +       struct inode *h_inode;
3512 +
3513 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3514 +       inode->i_mode = h_inode->i_mode;
3515 +       inode->i_uid = h_inode->i_uid;
3516 +       inode->i_gid = h_inode->i_gid;
3517 +       au_cpup_attr_timesizes(inode);
3518 +       au_cpup_attr_flags(inode, h_inode->i_flags);
3519 +}
3520 +
3521 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
3522 +{
3523 +       struct au_iinfo *iinfo = au_ii(inode);
3524 +
3525 +       IiMustWriteLock(inode);
3526 +
3527 +       iinfo->ii_higen = h_inode->i_generation;
3528 +       iinfo->ii_hsb1 = h_inode->i_sb;
3529 +}
3530 +
3531 +void au_cpup_attr_all(struct inode *inode, int force)
3532 +{
3533 +       struct inode *h_inode;
3534 +
3535 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3536 +       au_cpup_attr_changeable(inode);
3537 +       if (inode->i_nlink > 0)
3538 +               au_cpup_attr_nlink(inode, force);
3539 +       inode->i_rdev = h_inode->i_rdev;
3540 +       inode->i_blkbits = h_inode->i_blkbits;
3541 +       au_cpup_igen(inode, h_inode);
3542 +}
3543 +
3544 +/* ---------------------------------------------------------------------- */
3545 +
3546 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
3547 +
3548 +/* keep the timestamps of the parent dir when cpup */
3549 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
3550 +                   struct path *h_path)
3551 +{
3552 +       struct inode *h_inode;
3553 +
3554 +       dt->dt_dentry = dentry;
3555 +       dt->dt_h_path = *h_path;
3556 +       h_inode = h_path->dentry->d_inode;
3557 +       dt->dt_atime = h_inode->i_atime;
3558 +       dt->dt_mtime = h_inode->i_mtime;
3559 +       /* smp_mb(); */
3560 +}
3561 +
3562 +void au_dtime_revert(struct au_dtime *dt)
3563 +{
3564 +       struct iattr attr;
3565 +       int err;
3566 +
3567 +       attr.ia_atime = dt->dt_atime;
3568 +       attr.ia_mtime = dt->dt_mtime;
3569 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
3570 +               | ATTR_ATIME | ATTR_ATIME_SET;
3571 +
3572 +       err = vfsub_notify_change(&dt->dt_h_path, &attr);
3573 +       if (unlikely(err))
3574 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
3575 +}
3576 +
3577 +/* ---------------------------------------------------------------------- */
3578 +
3579 +/* internal use only */
3580 +struct au_cpup_reg_attr {
3581 +       int             valid;
3582 +       struct kstat    st;
3583 +       unsigned int    iflags; /* inode->i_flags */
3584 +};
3585 +
3586 +static noinline_for_stack
3587 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
3588 +              struct au_cpup_reg_attr *h_src_attr)
3589 +{
3590 +       int err, sbits;
3591 +       struct iattr ia;
3592 +       struct path h_path;
3593 +       struct inode *h_isrc, *h_idst;
3594 +       struct kstat *h_st;
3595 +
3596 +       h_path.dentry = au_h_dptr(dst, bindex);
3597 +       h_idst = h_path.dentry->d_inode;
3598 +       h_path.mnt = au_sbr_mnt(dst->d_sb, bindex);
3599 +       h_isrc = h_src->d_inode;
3600 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
3601 +               | ATTR_ATIME | ATTR_MTIME
3602 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
3603 +       if (h_src_attr && h_src_attr->valid) {
3604 +               h_st = &h_src_attr->st;
3605 +               ia.ia_uid = h_st->uid;
3606 +               ia.ia_gid = h_st->gid;
3607 +               ia.ia_atime = h_st->atime;
3608 +               ia.ia_mtime = h_st->mtime;
3609 +               if (h_idst->i_mode != h_st->mode
3610 +                   && !S_ISLNK(h_idst->i_mode)) {
3611 +                       ia.ia_valid |= ATTR_MODE;
3612 +                       ia.ia_mode = h_st->mode;
3613 +               }
3614 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
3615 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
3616 +       } else {
3617 +               ia.ia_uid = h_isrc->i_uid;
3618 +               ia.ia_gid = h_isrc->i_gid;
3619 +               ia.ia_atime = h_isrc->i_atime;
3620 +               ia.ia_mtime = h_isrc->i_mtime;
3621 +               if (h_idst->i_mode != h_isrc->i_mode
3622 +                   && !S_ISLNK(h_idst->i_mode)) {
3623 +                       ia.ia_valid |= ATTR_MODE;
3624 +                       ia.ia_mode = h_isrc->i_mode;
3625 +               }
3626 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
3627 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
3628 +       }
3629 +       err = vfsub_notify_change(&h_path, &ia);
3630 +
3631 +       /* is this nfs only? */
3632 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
3633 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
3634 +               ia.ia_mode = h_isrc->i_mode;
3635 +               err = vfsub_notify_change(&h_path, &ia);
3636 +       }
3637 +
3638 +       return err;
3639 +}
3640 +
3641 +/* ---------------------------------------------------------------------- */
3642 +
3643 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
3644 +                          char *buf, unsigned long blksize)
3645 +{
3646 +       int err;
3647 +       size_t sz, rbytes, wbytes;
3648 +       unsigned char all_zero;
3649 +       char *p, *zp;
3650 +       struct mutex *h_mtx;
3651 +       /* reduce stack usage */
3652 +       struct iattr *ia;
3653 +
3654 +       zp = page_address(ZERO_PAGE(0));
3655 +       if (unlikely(!zp))
3656 +               return -ENOMEM; /* possible? */
3657 +
3658 +       err = 0;
3659 +       all_zero = 0;
3660 +       while (len) {
3661 +               AuDbg("len %lld\n", len);
3662 +               sz = blksize;
3663 +               if (len < blksize)
3664 +                       sz = len;
3665 +
3666 +               rbytes = 0;
3667 +               /* todo: signal_pending? */
3668 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
3669 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
3670 +                       err = rbytes;
3671 +               }
3672 +               if (unlikely(err < 0))
3673 +                       break;
3674 +
3675 +               all_zero = 0;
3676 +               if (len >= rbytes && rbytes == blksize)
3677 +                       all_zero = !memcmp(buf, zp, rbytes);
3678 +               if (!all_zero) {
3679 +                       wbytes = rbytes;
3680 +                       p = buf;
3681 +                       while (wbytes) {
3682 +                               size_t b;
3683 +
3684 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
3685 +                               err = b;
3686 +                               /* todo: signal_pending? */
3687 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
3688 +                                       continue;
3689 +                               if (unlikely(err < 0))
3690 +                                       break;
3691 +                               wbytes -= b;
3692 +                               p += b;
3693 +                       }
3694 +               } else {
3695 +                       loff_t res;
3696 +
3697 +                       AuLabel(hole);
3698 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
3699 +                       err = res;
3700 +                       if (unlikely(res < 0))
3701 +                               break;
3702 +               }
3703 +               len -= rbytes;
3704 +               err = 0;
3705 +       }
3706 +
3707 +       /* the last block may be a hole */
3708 +       if (!err && all_zero) {
3709 +               AuLabel(last hole);
3710 +
3711 +               err = 1;
3712 +               if (au_test_nfs(dst->f_dentry->d_sb)) {
3713 +                       /* nfs requires this step to make last hole */
3714 +                       /* is this only nfs? */
3715 +                       do {
3716 +                               /* todo: signal_pending? */
3717 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
3718 +                       } while (err == -EAGAIN || err == -EINTR);
3719 +                       if (err == 1)
3720 +                               dst->f_pos--;
3721 +               }
3722 +
3723 +               if (err == 1) {
3724 +                       ia = (void *)buf;
3725 +                       ia->ia_size = dst->f_pos;
3726 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
3727 +                       ia->ia_file = dst;
3728 +                       h_mtx = &file_inode(dst)->i_mutex;
3729 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
3730 +                       err = vfsub_notify_change(&dst->f_path, ia);
3731 +                       mutex_unlock(h_mtx);
3732 +               }
3733 +       }
3734 +
3735 +       return err;
3736 +}
3737 +
3738 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
3739 +{
3740 +       int err;
3741 +       unsigned long blksize;
3742 +       unsigned char do_kfree;
3743 +       char *buf;
3744 +
3745 +       err = -ENOMEM;
3746 +       blksize = dst->f_dentry->d_sb->s_blocksize;
3747 +       if (!blksize || PAGE_SIZE < blksize)
3748 +               blksize = PAGE_SIZE;
3749 +       AuDbg("blksize %lu\n", blksize);
3750 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
3751 +       if (do_kfree)
3752 +               buf = kmalloc(blksize, GFP_NOFS);
3753 +       else
3754 +               buf = (void *)__get_free_page(GFP_NOFS);
3755 +       if (unlikely(!buf))
3756 +               goto out;
3757 +
3758 +       if (len > (1 << 22))
3759 +               AuDbg("copying a large file %lld\n", (long long)len);
3760 +
3761 +       src->f_pos = 0;
3762 +       dst->f_pos = 0;
3763 +       err = au_do_copy_file(dst, src, len, buf, blksize);
3764 +       if (do_kfree)
3765 +               kfree(buf);
3766 +       else
3767 +               free_page((unsigned long)buf);
3768 +
3769 +out:
3770 +       return err;
3771 +}
3772 +
3773 +/*
3774 + * to support a sparse file which is opened with O_APPEND,
3775 + * we need to close the file.
3776 + */
3777 +static int au_cp_regular(struct au_cp_generic *cpg)
3778 +{
3779 +       int err, i;
3780 +       enum { SRC, DST };
3781 +       struct {
3782 +               aufs_bindex_t bindex;
3783 +               unsigned int flags;
3784 +               struct dentry *dentry;
3785 +               struct file *file;
3786 +               void *label, *label_file;
3787 +       } *f, file[] = {
3788 +               {
3789 +                       .bindex = cpg->bsrc,
3790 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
3791 +                       .file = NULL,
3792 +                       .label = &&out,
3793 +                       .label_file = &&out_src
3794 +               },
3795 +               {
3796 +                       .bindex = cpg->bdst,
3797 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
3798 +                       .file = NULL,
3799 +                       .label = &&out_src,
3800 +                       .label_file = &&out_dst
3801 +               }
3802 +       };
3803 +       struct super_block *sb;
3804 +
3805 +       /* bsrc branch can be ro/rw. */
3806 +       sb = cpg->dentry->d_sb;
3807 +       f = file;
3808 +       for (i = 0; i < 2; i++, f++) {
3809 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
3810 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
3811 +                                   /*file*/NULL);
3812 +               err = PTR_ERR(f->file);
3813 +               if (IS_ERR(f->file))
3814 +                       goto *f->label;
3815 +               err = -EINVAL;
3816 +               if (unlikely(!f->file->f_op))
3817 +                       goto *f->label_file;
3818 +       }
3819 +
3820 +       /* try stopping to update while we copyup */
3821 +       IMustLock(file[SRC].dentry->d_inode);
3822 +       err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
3823 +
3824 +out_dst:
3825 +       fput(file[DST].file);
3826 +       au_sbr_put(sb, file[DST].bindex);
3827 +out_src:
3828 +       fput(file[SRC].file);
3829 +       au_sbr_put(sb, file[SRC].bindex);
3830 +out:
3831 +       return err;
3832 +}
3833 +
3834 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
3835 +                             struct au_cpup_reg_attr *h_src_attr)
3836 +{
3837 +       int err, rerr;
3838 +       loff_t l;
3839 +       struct path h_path;
3840 +       struct inode *h_src_inode;
3841 +
3842 +       err = 0;
3843 +       h_src_inode = au_h_iptr(cpg->dentry->d_inode, cpg->bsrc);
3844 +       l = i_size_read(h_src_inode);
3845 +       if (cpg->len == -1 || l < cpg->len)
3846 +               cpg->len = l;
3847 +       if (cpg->len) {
3848 +               /* try stopping to update while we are referencing */
3849 +               mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
3850 +               au_pin_hdir_unlock(cpg->pin);
3851 +
3852 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
3853 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
3854 +               h_src_attr->iflags = h_src_inode->i_flags;
3855 +               err = vfs_getattr(&h_path, &h_src_attr->st);
3856 +               if (unlikely(err)) {
3857 +                       mutex_unlock(&h_src_inode->i_mutex);
3858 +                       goto out;
3859 +               }
3860 +               h_src_attr->valid = 1;
3861 +               err = au_cp_regular(cpg);
3862 +               mutex_unlock(&h_src_inode->i_mutex);
3863 +               rerr = au_pin_hdir_relock(cpg->pin);
3864 +               if (!err && rerr)
3865 +                       err = rerr;
3866 +       }
3867 +
3868 +out:
3869 +       return err;
3870 +}
3871 +
3872 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
3873 +                             struct inode *h_dir)
3874 +{
3875 +       int err, symlen;
3876 +       mm_segment_t old_fs;
3877 +       union {
3878 +               char *k;
3879 +               char __user *u;
3880 +       } sym;
3881 +
3882 +       err = -ENOSYS;
3883 +       if (unlikely(!h_src->d_inode->i_op->readlink))
3884 +               goto out;
3885 +
3886 +       err = -ENOMEM;
3887 +       sym.k = (void *)__get_free_page(GFP_NOFS);
3888 +       if (unlikely(!sym.k))
3889 +               goto out;
3890 +
3891 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
3892 +       old_fs = get_fs();
3893 +       set_fs(KERNEL_DS);
3894 +       symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
3895 +       err = symlen;
3896 +       set_fs(old_fs);
3897 +
3898 +       if (symlen > 0) {
3899 +               sym.k[symlen] = 0;
3900 +               err = vfsub_symlink(h_dir, h_path, sym.k);
3901 +       }
3902 +       free_page((unsigned long)sym.k);
3903 +
3904 +out:
3905 +       return err;
3906 +}
3907 +
3908 +static noinline_for_stack
3909 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
3910 +              struct au_cpup_reg_attr *h_src_attr)
3911 +{
3912 +       int err;
3913 +       umode_t mode;
3914 +       unsigned int mnt_flags;
3915 +       unsigned char isdir;
3916 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
3917 +       struct au_dtime dt;
3918 +       struct path h_path;
3919 +       struct dentry *h_src, *h_dst, *h_parent;
3920 +       struct inode *h_inode, *h_dir;
3921 +       struct super_block *sb;
3922 +
3923 +       /* bsrc branch can be ro/rw. */
3924 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
3925 +       h_inode = h_src->d_inode;
3926 +       AuDebugOn(h_inode != au_h_iptr(cpg->dentry->d_inode, cpg->bsrc));
3927 +
3928 +       /* try stopping to be referenced while we are creating */
3929 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
3930 +       if (au_ftest_cpup(cpg->flags, RENAME))
3931 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
3932 +                                 AUFS_WH_PFX_LEN));
3933 +       h_parent = h_dst->d_parent; /* dir inode is locked */
3934 +       h_dir = h_parent->d_inode;
3935 +       IMustLock(h_dir);
3936 +       AuDebugOn(h_parent != h_dst->d_parent);
3937 +
3938 +       sb = cpg->dentry->d_sb;
3939 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
3940 +       if (do_dt) {
3941 +               h_path.dentry = h_parent;
3942 +               au_dtime_store(&dt, dst_parent, &h_path);
3943 +       }
3944 +       h_path.dentry = h_dst;
3945 +
3946 +       isdir = 0;
3947 +       mode = h_inode->i_mode;
3948 +       switch (mode & S_IFMT) {
3949 +       case S_IFREG:
3950 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
3951 +                                  /*want_excl*/true);
3952 +               if (!err)
3953 +                       err = au_do_cpup_regular(cpg, h_src_attr);
3954 +               break;
3955 +       case S_IFDIR:
3956 +               isdir = 1;
3957 +               err = vfsub_mkdir(h_dir, &h_path, mode);
3958 +               if (!err) {
3959 +                       /*
3960 +                        * strange behaviour from the users view,
3961 +                        * particularry setattr case
3962 +                        */
3963 +                       if (au_ibstart(dst_parent->d_inode) == cpg->bdst)
3964 +                               au_cpup_attr_nlink(dst_parent->d_inode,
3965 +                                                  /*force*/1);
3966 +                       au_cpup_attr_nlink(cpg->dentry->d_inode, /*force*/1);
3967 +               }
3968 +               break;
3969 +       case S_IFLNK:
3970 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
3971 +               break;
3972 +       case S_IFCHR:
3973 +       case S_IFBLK:
3974 +               AuDebugOn(!capable(CAP_MKNOD));
3975 +               /*FALLTHROUGH*/
3976 +       case S_IFIFO:
3977 +       case S_IFSOCK:
3978 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
3979 +               break;
3980 +       default:
3981 +               AuIOErr("Unknown inode type 0%o\n", mode);
3982 +               err = -EIO;
3983 +       }
3984 +
3985 +       mnt_flags = au_mntflags(sb);
3986 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
3987 +           && !isdir
3988 +           && au_opt_test(mnt_flags, XINO)
3989 +           && h_inode->i_nlink == 1
3990 +           /* todo: unnecessary? */
3991 +           /* && cpg->dentry->d_inode->i_nlink == 1 */
3992 +           && cpg->bdst < cpg->bsrc
3993 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
3994 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
3995 +               /* ignore this error */
3996 +
3997 +       if (do_dt)
3998 +               au_dtime_revert(&dt);
3999 +       return err;
4000 +}
4001 +
4002 +static int au_do_ren_after_cpup(struct dentry *dentry, aufs_bindex_t bdst,
4003 +                               struct path *h_path)
4004 +{
4005 +       int err;
4006 +       struct dentry *h_dentry, *h_parent;
4007 +       struct inode *h_dir;
4008 +
4009 +       h_dentry = dget(au_h_dptr(dentry, bdst));
4010 +       au_set_h_dptr(dentry, bdst, NULL);
4011 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
4012 +       if (unlikely(err)) {
4013 +               au_set_h_dptr(dentry, bdst, h_dentry);
4014 +               goto out;
4015 +       }
4016 +
4017 +       h_path->dentry = dget(au_h_dptr(dentry, bdst));
4018 +       au_set_h_dptr(dentry, bdst, h_dentry);
4019 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
4020 +       h_dir = h_parent->d_inode;
4021 +       IMustLock(h_dir);
4022 +       AuDbg("%.*s %.*s\n", AuDLNPair(h_dentry), AuDLNPair(h_path->dentry));
4023 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path);
4024 +       dput(h_path->dentry);
4025 +
4026 +out:
4027 +       return err;
4028 +}
4029 +
4030 +/*
4031 + * copyup the @dentry from @bsrc to @bdst.
4032 + * the caller must set the both of lower dentries.
4033 + * @len is for truncating when it is -1 copyup the entire file.
4034 + * in link/rename cases, @dst_parent may be different from the real one.
4035 + * basic->bsrc can be larger than basic->bdst.
4036 + */
4037 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
4038 +{
4039 +       int err, rerr;
4040 +       aufs_bindex_t old_ibstart;
4041 +       unsigned char isdir, plink;
4042 +       struct dentry *h_src, *h_dst, *h_parent;
4043 +       struct inode *dst_inode, *h_dir, *inode;
4044 +       struct super_block *sb;
4045 +       struct au_branch *br;
4046 +       /* to reuduce stack size */
4047 +       struct {
4048 +               struct au_dtime dt;
4049 +               struct path h_path;
4050 +               struct au_cpup_reg_attr h_src_attr;
4051 +       } *a;
4052 +
4053 +       err = -ENOMEM;
4054 +       a = kmalloc(sizeof(*a), GFP_NOFS);
4055 +       if (unlikely(!a))
4056 +               goto out;
4057 +       a->h_src_attr.valid = 0;
4058 +
4059 +       sb = cpg->dentry->d_sb;
4060 +       br = au_sbr(sb, cpg->bdst);
4061 +       a->h_path.mnt = au_br_mnt(br);
4062 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
4063 +       h_parent = h_dst->d_parent; /* dir inode is locked */
4064 +       h_dir = h_parent->d_inode;
4065 +       IMustLock(h_dir);
4066 +
4067 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
4068 +       inode = cpg->dentry->d_inode;
4069 +
4070 +       if (!dst_parent)
4071 +               dst_parent = dget_parent(cpg->dentry);
4072 +       else
4073 +               dget(dst_parent);
4074 +
4075 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
4076 +       dst_inode = au_h_iptr(inode, cpg->bdst);
4077 +       if (dst_inode) {
4078 +               if (unlikely(!plink)) {
4079 +                       err = -EIO;
4080 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
4081 +                               "but plink is disabled\n",
4082 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
4083 +                       goto out_parent;
4084 +               }
4085 +
4086 +               if (dst_inode->i_nlink) {
4087 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
4088 +
4089 +                       h_src = au_plink_lkup(inode, cpg->bdst);
4090 +                       err = PTR_ERR(h_src);
4091 +                       if (IS_ERR(h_src))
4092 +                               goto out_parent;
4093 +                       if (unlikely(!h_src->d_inode)) {
4094 +                               err = -EIO;
4095 +                               AuIOErr("i%lu exists on a upper branch "
4096 +                                       "but not pseudo-linked\n",
4097 +                                       inode->i_ino);
4098 +                               dput(h_src);
4099 +                               goto out_parent;
4100 +                       }
4101 +
4102 +                       if (do_dt) {
4103 +                               a->h_path.dentry = h_parent;
4104 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
4105 +                       }
4106 +
4107 +                       a->h_path.dentry = h_dst;
4108 +                       err = vfsub_link(h_src, h_dir, &a->h_path);
4109 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
4110 +                               err = au_do_ren_after_cpup
4111 +                                       (cpg->dentry, cpg->bdst, &a->h_path);
4112 +                       if (do_dt)
4113 +                               au_dtime_revert(&a->dt);
4114 +                       dput(h_src);
4115 +                       goto out_parent;
4116 +               } else
4117 +                       /* todo: cpup_wh_file? */
4118 +                       /* udba work */
4119 +                       au_update_ibrange(inode, /*do_put_zero*/1);
4120 +       }
4121 +
4122 +       isdir = S_ISDIR(inode->i_mode);
4123 +       old_ibstart = au_ibstart(inode);
4124 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
4125 +       if (unlikely(err))
4126 +               goto out_rev;
4127 +       dst_inode = h_dst->d_inode;
4128 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
4129 +       /* todo: necessary? */
4130 +       /* au_pin_hdir_unlock(cpg->pin); */
4131 +
4132 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
4133 +       if (unlikely(err)) {
4134 +               /* todo: necessary? */
4135 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
4136 +               mutex_unlock(&dst_inode->i_mutex);
4137 +               goto out_rev;
4138 +       }
4139 +
4140 +       if (cpg->bdst < old_ibstart) {
4141 +               if (S_ISREG(inode->i_mode)) {
4142 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
4143 +                       if (unlikely(err)) {
4144 +                               /* ignore an error */
4145 +                               /* au_pin_hdir_relock(cpg->pin); */
4146 +                               mutex_unlock(&dst_inode->i_mutex);
4147 +                               goto out_rev;
4148 +                       }
4149 +               }
4150 +               au_set_ibstart(inode, cpg->bdst);
4151 +       } else
4152 +               au_set_ibend(inode, cpg->bdst);
4153 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
4154 +                     au_hi_flags(inode, isdir));
4155 +
4156 +       /* todo: necessary? */
4157 +       /* err = au_pin_hdir_relock(cpg->pin); */
4158 +       mutex_unlock(&dst_inode->i_mutex);
4159 +       if (unlikely(err))
4160 +               goto out_rev;
4161 +
4162 +       if (!isdir
4163 +           && h_src->d_inode->i_nlink > 1
4164 +           && plink)
4165 +               au_plink_append(inode, cpg->bdst, h_dst);
4166 +
4167 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
4168 +               a->h_path.dentry = h_dst;
4169 +               err = au_do_ren_after_cpup(cpg->dentry, cpg->bdst, &a->h_path);
4170 +       }
4171 +       if (!err)
4172 +               goto out_parent; /* success */
4173 +
4174 +       /* revert */
4175 +out_rev:
4176 +       a->h_path.dentry = h_parent;
4177 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
4178 +       a->h_path.dentry = h_dst;
4179 +       rerr = 0;
4180 +       if (h_dst->d_inode) {
4181 +               if (!isdir)
4182 +                       rerr = vfsub_unlink(h_dir, &a->h_path, /*force*/0);
4183 +               else
4184 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
4185 +       }
4186 +       au_dtime_revert(&a->dt);
4187 +       if (rerr) {
4188 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
4189 +               err = -EIO;
4190 +       }
4191 +out_parent:
4192 +       dput(dst_parent);
4193 +       kfree(a);
4194 +out:
4195 +       return err;
4196 +}
4197 +
4198 +#if 0 /* unused */
4199 +struct au_cpup_single_args {
4200 +       int *errp;
4201 +       struct au_cp_generic *cpg;
4202 +       struct dentry *dst_parent;
4203 +};
4204 +
4205 +static void au_call_cpup_single(void *args)
4206 +{
4207 +       struct au_cpup_single_args *a = args;
4208 +
4209 +       au_pin_hdir_acquire_nest(a->cpg->pin);
4210 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
4211 +       au_pin_hdir_release(a->cpg->pin);
4212 +}
4213 +#endif
4214 +
4215 +/*
4216 + * prevent SIGXFSZ in copy-up.
4217 + * testing CAP_MKNOD is for generic fs,
4218 + * but CAP_FSETID is for xfs only, currently.
4219 + */
4220 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
4221 +{
4222 +       int do_sio;
4223 +       struct super_block *sb;
4224 +       struct inode *h_dir;
4225 +
4226 +       do_sio = 0;
4227 +       sb = au_pinned_parent(pin)->d_sb;
4228 +       if (!au_wkq_test()
4229 +           && (!au_sbi(sb)->si_plink_maint_pid
4230 +               || au_plink_maint(sb, AuLock_NOPLM))) {
4231 +               switch (mode & S_IFMT) {
4232 +               case S_IFREG:
4233 +                       /* no condition about RLIMIT_FSIZE and the file size */
4234 +                       do_sio = 1;
4235 +                       break;
4236 +               case S_IFCHR:
4237 +               case S_IFBLK:
4238 +                       do_sio = !capable(CAP_MKNOD);
4239 +                       break;
4240 +               }
4241 +               if (!do_sio)
4242 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
4243 +                                 && !capable(CAP_FSETID));
4244 +               /* this workaround may be removed in the future */
4245 +               if (!do_sio) {
4246 +                       h_dir = au_pinned_h_dir(pin);
4247 +                       do_sio = h_dir->i_mode & S_ISVTX;
4248 +               }
4249 +       }
4250 +
4251 +       return do_sio;
4252 +}
4253 +
4254 +#if 0 /* unused */
4255 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
4256 +{
4257 +       int err, wkq_err;
4258 +       struct dentry *h_dentry;
4259 +
4260 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
4261 +       if (!au_cpup_sio_test(pin, h_dentry->d_inode->i_mode))
4262 +               err = au_cpup_single(cpg, dst_parent);
4263 +       else {
4264 +               struct au_cpup_single_args args = {
4265 +                       .errp           = &err,
4266 +                       .cpg            = cpg,
4267 +                       .dst_parent     = dst_parent
4268 +               };
4269 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
4270 +               if (unlikely(wkq_err))
4271 +                       err = wkq_err;
4272 +       }
4273 +
4274 +       return err;
4275 +}
4276 +#endif
4277 +
4278 +/*
4279 + * copyup the @dentry from the first active lower branch to @bdst,
4280 + * using au_cpup_single().
4281 + */
4282 +static int au_cpup_simple(struct au_cp_generic *cpg)
4283 +{
4284 +       int err;
4285 +       unsigned int flags_orig;
4286 +       struct dentry *dentry;
4287 +
4288 +       AuDebugOn(cpg->bsrc < 0);
4289 +
4290 +       dentry = cpg->dentry;
4291 +       DiMustWriteLock(dentry);
4292 +
4293 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
4294 +       if (!err) {
4295 +               flags_orig = cpg->flags;
4296 +               au_fset_cpup(cpg->flags, RENAME);
4297 +               err = au_cpup_single(cpg, NULL);
4298 +               cpg->flags = flags_orig;
4299 +               if (!err)
4300 +                       return 0; /* success */
4301 +
4302 +               /* revert */
4303 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
4304 +               au_set_dbstart(dentry, cpg->bsrc);
4305 +       }
4306 +
4307 +       return err;
4308 +}
4309 +
4310 +struct au_cpup_simple_args {
4311 +       int *errp;
4312 +       struct au_cp_generic *cpg;
4313 +};
4314 +
4315 +static void au_call_cpup_simple(void *args)
4316 +{
4317 +       struct au_cpup_simple_args *a = args;
4318 +
4319 +       au_pin_hdir_acquire_nest(a->cpg->pin);
4320 +       *a->errp = au_cpup_simple(a->cpg);
4321 +       au_pin_hdir_release(a->cpg->pin);
4322 +}
4323 +
4324 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
4325 +{
4326 +       int err, wkq_err;
4327 +       struct dentry *dentry, *parent;
4328 +       struct file *h_file;
4329 +       struct inode *h_dir;
4330 +
4331 +       dentry = cpg->dentry;
4332 +       h_file = NULL;
4333 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
4334 +               AuDebugOn(cpg->bsrc < 0);
4335 +               h_file = au_h_open_pre(dentry, cpg->bsrc);
4336 +               err = PTR_ERR(h_file);
4337 +               if (IS_ERR(h_file))
4338 +                       goto out;
4339 +       }
4340 +
4341 +       parent = dget_parent(dentry);
4342 +       h_dir = au_h_iptr(parent->d_inode, cpg->bdst);
4343 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
4344 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
4345 +               err = au_cpup_simple(cpg);
4346 +       else {
4347 +               struct au_cpup_simple_args args = {
4348 +                       .errp           = &err,
4349 +                       .cpg            = cpg
4350 +               };
4351 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
4352 +               if (unlikely(wkq_err))
4353 +                       err = wkq_err;
4354 +       }
4355 +
4356 +       dput(parent);
4357 +       if (h_file)
4358 +               au_h_open_post(dentry, cpg->bsrc, h_file);
4359 +
4360 +out:
4361 +       return err;
4362 +}
4363 +
4364 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
4365 +{
4366 +       aufs_bindex_t bsrc, bend;
4367 +       struct dentry *dentry, *h_dentry;
4368 +
4369 +       if (cpg->bsrc < 0) {
4370 +               dentry = cpg->dentry;
4371 +               bend = au_dbend(dentry);
4372 +               for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
4373 +                       h_dentry = au_h_dptr(dentry, bsrc);
4374 +                       if (h_dentry) {
4375 +                               AuDebugOn(!h_dentry->d_inode);
4376 +                               break;
4377 +                       }
4378 +               }
4379 +               AuDebugOn(bsrc > bend);
4380 +               cpg->bsrc = bsrc;
4381 +       }
4382 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
4383 +       return au_do_sio_cpup_simple(cpg);
4384 +}
4385 +
4386 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
4387 +{
4388 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
4389 +       return au_do_sio_cpup_simple(cpg);
4390 +}
4391 +
4392 +/* ---------------------------------------------------------------------- */
4393 +
4394 +/*
4395 + * copyup the deleted file for writing.
4396 + */
4397 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
4398 +                        struct file *file)
4399 +{
4400 +       int err;
4401 +       unsigned int flags_orig;
4402 +       aufs_bindex_t bsrc_orig;
4403 +       struct dentry *h_d_dst, *h_d_start;
4404 +       struct au_dinfo *dinfo;
4405 +       struct au_hdentry *hdp;
4406 +
4407 +       dinfo = au_di(cpg->dentry);
4408 +       AuRwMustWriteLock(&dinfo->di_rwsem);
4409 +
4410 +       bsrc_orig = cpg->bsrc;
4411 +       cpg->bsrc = dinfo->di_bstart;
4412 +       hdp = dinfo->di_hdentry;
4413 +       h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
4414 +       dinfo->di_bstart = cpg->bdst;
4415 +       hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
4416 +       h_d_start = NULL;
4417 +       if (file) {
4418 +               h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
4419 +               hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_dentry;
4420 +       }
4421 +       flags_orig = cpg->flags;
4422 +       cpg->flags = !AuCpup_DTIME;
4423 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
4424 +       cpg->flags = flags_orig;
4425 +       if (file) {
4426 +               if (!err)
4427 +                       err = au_reopen_nondir(file);
4428 +               hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
4429 +       }
4430 +       hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
4431 +       dinfo->di_bstart = cpg->bsrc;
4432 +       cpg->bsrc = bsrc_orig;
4433 +
4434 +       return err;
4435 +}
4436 +
4437 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
4438 +{
4439 +       int err;
4440 +       aufs_bindex_t bdst;
4441 +       struct au_dtime dt;
4442 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
4443 +       struct au_branch *br;
4444 +       struct path h_path;
4445 +
4446 +       dentry = cpg->dentry;
4447 +       bdst = cpg->bdst;
4448 +       br = au_sbr(dentry->d_sb, bdst);
4449 +       parent = dget_parent(dentry);
4450 +       h_parent = au_h_dptr(parent, bdst);
4451 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
4452 +       err = PTR_ERR(wh_dentry);
4453 +       if (IS_ERR(wh_dentry))
4454 +               goto out;
4455 +
4456 +       h_path.dentry = h_parent;
4457 +       h_path.mnt = au_br_mnt(br);
4458 +       au_dtime_store(&dt, parent, &h_path);
4459 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
4460 +       if (unlikely(err))
4461 +               goto out_wh;
4462 +
4463 +       dget(wh_dentry);
4464 +       h_path.dentry = wh_dentry;
4465 +       if (!S_ISDIR(wh_dentry->d_inode->i_mode))
4466 +               err = vfsub_unlink(h_parent->d_inode, &h_path, /*force*/0);
4467 +       else
4468 +               err = vfsub_rmdir(h_parent->d_inode, &h_path);
4469 +       if (unlikely(err)) {
4470 +               AuIOErr("failed remove copied-up tmp file %.*s(%d)\n",
4471 +                       AuDLNPair(wh_dentry), err);
4472 +               err = -EIO;
4473 +       }
4474 +       au_dtime_revert(&dt);
4475 +       au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
4476 +
4477 +out_wh:
4478 +       dput(wh_dentry);
4479 +out:
4480 +       dput(parent);
4481 +       return err;
4482 +}
4483 +
4484 +struct au_cpup_wh_args {
4485 +       int *errp;
4486 +       struct au_cp_generic *cpg;
4487 +       struct file *file;
4488 +};
4489 +
4490 +static void au_call_cpup_wh(void *args)
4491 +{
4492 +       struct au_cpup_wh_args *a = args;
4493 +
4494 +       au_pin_hdir_acquire_nest(a->cpg->pin);
4495 +       *a->errp = au_cpup_wh(a->cpg, a->file);
4496 +       au_pin_hdir_release(a->cpg->pin);
4497 +}
4498 +
4499 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
4500 +{
4501 +       int err, wkq_err;
4502 +       aufs_bindex_t bdst;
4503 +       struct dentry *dentry, *parent, *h_orph, *h_parent, *h_dentry;
4504 +       struct inode *dir, *h_dir, *h_tmpdir;
4505 +       struct au_wbr *wbr;
4506 +       struct au_pin wh_pin, *pin_orig;
4507 +
4508 +       dentry = cpg->dentry;
4509 +       bdst = cpg->bdst;
4510 +       parent = dget_parent(dentry);
4511 +       dir = parent->d_inode;
4512 +       h_orph = NULL;
4513 +       h_parent = NULL;
4514 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
4515 +       h_tmpdir = h_dir;
4516 +       pin_orig = NULL;
4517 +       if (!h_dir->i_nlink) {
4518 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
4519 +               h_orph = wbr->wbr_orph;
4520 +
4521 +               h_parent = dget(au_h_dptr(parent, bdst));
4522 +               au_set_h_dptr(parent, bdst, dget(h_orph));
4523 +               h_tmpdir = h_orph->d_inode;
4524 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
4525 +
4526 +               if (file)
4527 +                       h_dentry = au_hf_top(file)->f_dentry;
4528 +               else
4529 +                       h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
4530 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
4531 +               /* todo: au_h_open_pre()? */
4532 +
4533 +               pin_orig = cpg->pin;
4534 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
4535 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
4536 +               cpg->pin = &wh_pin;
4537 +       }
4538 +
4539 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
4540 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
4541 +               err = au_cpup_wh(cpg, file);
4542 +       else {
4543 +               struct au_cpup_wh_args args = {
4544 +                       .errp   = &err,
4545 +                       .cpg    = cpg,
4546 +                       .file   = file
4547 +               };
4548 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
4549 +               if (unlikely(wkq_err))
4550 +                       err = wkq_err;
4551 +       }
4552 +
4553 +       if (h_orph) {
4554 +               mutex_unlock(&h_tmpdir->i_mutex);
4555 +               /* todo: au_h_open_post()? */
4556 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
4557 +               au_set_h_dptr(parent, bdst, h_parent);
4558 +               AuDebugOn(!pin_orig);
4559 +               cpg->pin = pin_orig;
4560 +       }
4561 +       iput(h_dir);
4562 +       dput(parent);
4563 +
4564 +       return err;
4565 +}
4566 +
4567 +/* ---------------------------------------------------------------------- */
4568 +
4569 +/*
4570 + * generic routine for both of copy-up and copy-down.
4571 + */
4572 +/* cf. revalidate function in file.c */
4573 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
4574 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
4575 +                        struct au_pin *pin,
4576 +                        struct dentry *h_parent, void *arg),
4577 +              void *arg)
4578 +{
4579 +       int err;
4580 +       struct au_pin pin;
4581 +       struct dentry *d, *parent, *h_parent, *real_parent;
4582 +
4583 +       err = 0;
4584 +       parent = dget_parent(dentry);
4585 +       if (IS_ROOT(parent))
4586 +               goto out;
4587 +
4588 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
4589 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
4590 +
4591 +       /* do not use au_dpage */
4592 +       real_parent = parent;
4593 +       while (1) {
4594 +               dput(parent);
4595 +               parent = dget_parent(dentry);
4596 +               h_parent = au_h_dptr(parent, bdst);
4597 +               if (h_parent)
4598 +                       goto out; /* success */
4599 +
4600 +               /* find top dir which is necessary to cpup */
4601 +               do {
4602 +                       d = parent;
4603 +                       dput(parent);
4604 +                       parent = dget_parent(d);
4605 +                       di_read_lock_parent3(parent, !AuLock_IR);
4606 +                       h_parent = au_h_dptr(parent, bdst);
4607 +                       di_read_unlock(parent, !AuLock_IR);
4608 +               } while (!h_parent);
4609 +
4610 +               if (d != real_parent)
4611 +                       di_write_lock_child3(d);
4612 +
4613 +               /* somebody else might create while we were sleeping */
4614 +               if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
4615 +                       if (au_h_dptr(d, bdst))
4616 +                               au_update_dbstart(d);
4617 +
4618 +                       au_pin_set_dentry(&pin, d);
4619 +                       err = au_do_pin(&pin);
4620 +                       if (!err) {
4621 +                               err = cp(d, bdst, &pin, h_parent, arg);
4622 +                               au_unpin(&pin);
4623 +                       }
4624 +               }
4625 +
4626 +               if (d != real_parent)
4627 +                       di_write_unlock(d);
4628 +               if (unlikely(err))
4629 +                       break;
4630 +       }
4631 +
4632 +out:
4633 +       dput(parent);
4634 +       return err;
4635 +}
4636 +
4637 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
4638 +                      struct au_pin *pin,
4639 +                      struct dentry *h_parent __maybe_unused ,
4640 +                      void *arg __maybe_unused)
4641 +{
4642 +       struct au_cp_generic cpg = {
4643 +               .dentry = dentry,
4644 +               .bdst   = bdst,
4645 +               .bsrc   = -1,
4646 +               .len    = 0,
4647 +               .pin    = pin,
4648 +               .flags  = AuCpup_DTIME
4649 +       };
4650 +       return au_sio_cpup_simple(&cpg);
4651 +}
4652 +
4653 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
4654 +{
4655 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
4656 +}
4657 +
4658 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
4659 +{
4660 +       int err;
4661 +       struct dentry *parent;
4662 +       struct inode *dir;
4663 +
4664 +       parent = dget_parent(dentry);
4665 +       dir = parent->d_inode;
4666 +       err = 0;
4667 +       if (au_h_iptr(dir, bdst))
4668 +               goto out;
4669 +
4670 +       di_read_unlock(parent, AuLock_IR);
4671 +       di_write_lock_parent(parent);
4672 +       /* someone else might change our inode while we were sleeping */
4673 +       if (!au_h_iptr(dir, bdst))
4674 +               err = au_cpup_dirs(dentry, bdst);
4675 +       di_downgrade_lock(parent, AuLock_IR);
4676 +
4677 +out:
4678 +       dput(parent);
4679 +       return err;
4680 +}
4681 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
4682 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
4683 +++ linux/fs/aufs/cpup.h        2013-08-23 23:59:39.634916914 +0200
4684 @@ -0,0 +1,90 @@
4685 +/*
4686 + * Copyright (C) 2005-2013 Junjiro R. Okajima
4687 + *
4688 + * This program, aufs is free software; you can redistribute it and/or modify
4689 + * it under the terms of the GNU General Public License as published by
4690 + * the Free Software Foundation; either version 2 of the License, or
4691 + * (at your option) any later version.
4692 + *
4693 + * This program is distributed in the hope that it will be useful,
4694 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4695 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4696 + * GNU General Public License for more details.
4697 + *
4698 + * You should have received a copy of the GNU General Public License
4699 + * along with this program; if not, write to the Free Software
4700 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4701 + */
4702 +
4703 +/*
4704 + * copy-up/down functions
4705 + */
4706 +
4707 +#ifndef __AUFS_CPUP_H__
4708 +#define __AUFS_CPUP_H__
4709 +
4710 +#ifdef __KERNEL__
4711 +
4712 +#include <linux/path.h>
4713 +
4714 +struct inode;
4715 +struct file;
4716 +struct au_pin;
4717 +
4718 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
4719 +void au_cpup_attr_timesizes(struct inode *inode);
4720 +void au_cpup_attr_nlink(struct inode *inode, int force);
4721 +void au_cpup_attr_changeable(struct inode *inode);
4722 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
4723 +void au_cpup_attr_all(struct inode *inode, int force);
4724 +
4725 +/* ---------------------------------------------------------------------- */
4726 +
4727 +struct au_cp_generic {
4728 +       struct dentry   *dentry;
4729 +       aufs_bindex_t   bdst, bsrc;
4730 +       loff_t          len;
4731 +       struct au_pin   *pin;
4732 +       unsigned int    flags;
4733 +};
4734 +
4735 +/* cpup flags */
4736 +#define AuCpup_DTIME   1               /* do dtime_store/revert */
4737 +#define AuCpup_KEEPLINO        (1 << 1)        /* do not clear the lower xino,
4738 +                                          for link(2) */
4739 +#define AuCpup_RENAME  (1 << 2)        /* rename after cpup */
4740 +#define AuCpup_HOPEN   (1 << 3)        /* call h_open_pre/post() in cpup */
4741 +
4742 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
4743 +#define au_fset_cpup(flags, name) \
4744 +       do { (flags) |= AuCpup_##name; } while (0)
4745 +#define au_fclr_cpup(flags, name) \
4746 +       do { (flags) &= ~AuCpup_##name; } while (0)
4747 +
4748 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
4749 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
4750 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
4751 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
4752 +
4753 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
4754 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
4755 +                        struct au_pin *pin,
4756 +                        struct dentry *h_parent, void *arg),
4757 +              void *arg);
4758 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
4759 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
4760 +
4761 +/* ---------------------------------------------------------------------- */
4762 +
4763 +/* keep timestamps when copyup */
4764 +struct au_dtime {
4765 +       struct dentry *dt_dentry;
4766 +       struct path dt_h_path;
4767 +       struct timespec dt_atime, dt_mtime;
4768 +};
4769 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4770 +                   struct path *h_path);
4771 +void au_dtime_revert(struct au_dtime *dt);
4772 +
4773 +#endif /* __KERNEL__ */
4774 +#endif /* __AUFS_CPUP_H__ */
4775 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
4776 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
4777 +++ linux/fs/aufs/dbgaufs.c     2013-07-06 13:20:47.740198107 +0200
4778 @@ -0,0 +1,433 @@
4779 +/*
4780 + * Copyright (C) 2005-2013 Junjiro R. Okajima
4781 + *
4782 + * This program, aufs is free software; you can redistribute it and/or modify
4783 + * it under the terms of the GNU General Public License as published by
4784 + * the Free Software Foundation; either version 2 of the License, or
4785 + * (at your option) any later version.
4786 + *
4787 + * This program is distributed in the hope that it will be useful,
4788 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4789 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4790 + * GNU General Public License for more details.
4791 + *
4792 + * You should have received a copy of the GNU General Public License
4793 + * along with this program; if not, write to the Free Software
4794 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4795 + */
4796 +
4797 +/*
4798 + * debugfs interface
4799 + */
4800 +
4801 +#include <linux/debugfs.h>
4802 +#include "aufs.h"
4803 +
4804 +#ifndef CONFIG_SYSFS
4805 +#error DEBUG_FS depends upon SYSFS
4806 +#endif
4807 +
4808 +static struct dentry *dbgaufs;
4809 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
4810 +
4811 +/* 20 is max digits length of ulong 64 */
4812 +struct dbgaufs_arg {
4813 +       int n;
4814 +       char a[20 * 4];
4815 +};
4816 +
4817 +/*
4818 + * common function for all XINO files
4819 + */
4820 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
4821 +                             struct file *file)
4822 +{
4823 +       kfree(file->private_data);
4824 +       return 0;
4825 +}
4826 +
4827 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
4828 +{
4829 +       int err;
4830 +       struct kstat st;
4831 +       struct dbgaufs_arg *p;
4832 +
4833 +       err = -ENOMEM;
4834 +       p = kmalloc(sizeof(*p), GFP_NOFS);
4835 +       if (unlikely(!p))
4836 +               goto out;
4837 +
4838 +       err = 0;
4839 +       p->n = 0;
4840 +       file->private_data = p;
4841 +       if (!xf)
4842 +               goto out;
4843 +
4844 +       err = vfs_getattr(&xf->f_path, &st);
4845 +       if (!err) {
4846 +               if (do_fcnt)
4847 +                       p->n = snprintf
4848 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
4849 +                                (long)file_count(xf), st.blocks, st.blksize,
4850 +                                (long long)st.size);
4851 +               else
4852 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
4853 +                                       st.blocks, st.blksize,
4854 +                                       (long long)st.size);
4855 +               AuDebugOn(p->n >= sizeof(p->a));
4856 +       } else {
4857 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
4858 +               err = 0;
4859 +       }
4860 +
4861 +out:
4862 +       return err;
4863 +
4864 +}
4865 +
4866 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
4867 +                              size_t count, loff_t *ppos)
4868 +{
4869 +       struct dbgaufs_arg *p;
4870 +
4871 +       p = file->private_data;
4872 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
4873 +}
4874 +
4875 +/* ---------------------------------------------------------------------- */
4876 +
4877 +struct dbgaufs_plink_arg {
4878 +       int n;
4879 +       char a[];
4880 +};
4881 +
4882 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
4883 +                                struct file *file)
4884 +{
4885 +       free_page((unsigned long)file->private_data);
4886 +       return 0;
4887 +}
4888 +
4889 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
4890 +{
4891 +       int err, i, limit;
4892 +       unsigned long n, sum;
4893 +       struct dbgaufs_plink_arg *p;
4894 +       struct au_sbinfo *sbinfo;
4895 +       struct super_block *sb;
4896 +       struct au_sphlhead *sphl;
4897 +
4898 +       err = -ENOMEM;
4899 +       p = (void *)get_zeroed_page(GFP_NOFS);
4900 +       if (unlikely(!p))
4901 +               goto out;
4902 +
4903 +       err = -EFBIG;
4904 +       sbinfo = inode->i_private;
4905 +       sb = sbinfo->si_sb;
4906 +       si_noflush_read_lock(sb);
4907 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
4908 +               limit = PAGE_SIZE - sizeof(p->n);
4909 +
4910 +               /* the number of buckets */
4911 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
4912 +               p->n += n;
4913 +               limit -= n;
4914 +
4915 +               sum = 0;
4916 +               for (i = 0, sphl = sbinfo->si_plink;
4917 +                    i < AuPlink_NHASH;
4918 +                    i++, sphl++) {
4919 +                       n = au_sphl_count(sphl);
4920 +                       sum += n;
4921 +
4922 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
4923 +                       p->n += n;
4924 +                       limit -= n;
4925 +                       if (unlikely(limit <= 0))
4926 +                               goto out_free;
4927 +               }
4928 +               p->a[p->n - 1] = '\n';
4929 +
4930 +               /* the sum of plinks */
4931 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
4932 +               p->n += n;
4933 +               limit -= n;
4934 +               if (unlikely(limit <= 0))
4935 +                       goto out_free;
4936 +       } else {
4937 +#define str "1\n0\n0\n"
4938 +               p->n = sizeof(str) - 1;
4939 +               strcpy(p->a, str);
4940 +#undef str
4941 +       }
4942 +       si_read_unlock(sb);
4943 +
4944 +       err = 0;
4945 +       file->private_data = p;
4946 +       goto out; /* success */
4947 +
4948 +out_free:
4949 +       free_page((unsigned long)p);
4950 +out:
4951 +       return err;
4952 +}
4953 +
4954 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
4955 +                                 size_t count, loff_t *ppos)
4956 +{
4957 +       struct dbgaufs_plink_arg *p;
4958 +
4959 +       p = file->private_data;
4960 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
4961 +}
4962 +
4963 +static const struct file_operations dbgaufs_plink_fop = {
4964 +       .owner          = THIS_MODULE,
4965 +       .open           = dbgaufs_plink_open,
4966 +       .release        = dbgaufs_plink_release,
4967 +       .read           = dbgaufs_plink_read
4968 +};
4969 +
4970 +/* ---------------------------------------------------------------------- */
4971 +
4972 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
4973 +{
4974 +       int err;
4975 +       struct au_sbinfo *sbinfo;
4976 +       struct super_block *sb;
4977 +
4978 +       sbinfo = inode->i_private;
4979 +       sb = sbinfo->si_sb;
4980 +       si_noflush_read_lock(sb);
4981 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
4982 +       si_read_unlock(sb);
4983 +       return err;
4984 +}
4985 +
4986 +static const struct file_operations dbgaufs_xib_fop = {
4987 +       .owner          = THIS_MODULE,
4988 +       .open           = dbgaufs_xib_open,
4989 +       .release        = dbgaufs_xi_release,
4990 +       .read           = dbgaufs_xi_read
4991 +};
4992 +
4993 +/* ---------------------------------------------------------------------- */
4994 +
4995 +#define DbgaufsXi_PREFIX "xi"
4996 +
4997 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
4998 +{
4999 +       int err;
5000 +       long l;
5001 +       struct au_sbinfo *sbinfo;
5002 +       struct super_block *sb;
5003 +       struct file *xf;
5004 +       struct qstr *name;
5005 +
5006 +       err = -ENOENT;
5007 +       xf = NULL;
5008 +       name = &file->f_dentry->d_name;
5009 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
5010 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
5011 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
5012 +               goto out;
5013 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
5014 +       if (unlikely(err))
5015 +               goto out;
5016 +
5017 +       sbinfo = inode->i_private;
5018 +       sb = sbinfo->si_sb;
5019 +       si_noflush_read_lock(sb);
5020 +       if (l <= au_sbend(sb)) {
5021 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
5022 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
5023 +       } else
5024 +               err = -ENOENT;
5025 +       si_read_unlock(sb);
5026 +
5027 +out:
5028 +       return err;
5029 +}
5030 +
5031 +static const struct file_operations dbgaufs_xino_fop = {
5032 +       .owner          = THIS_MODULE,
5033 +       .open           = dbgaufs_xino_open,
5034 +       .release        = dbgaufs_xi_release,
5035 +       .read           = dbgaufs_xi_read
5036 +};
5037 +
5038 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
5039 +{
5040 +       aufs_bindex_t bend;
5041 +       struct au_branch *br;
5042 +       struct au_xino_file *xi;
5043 +
5044 +       if (!au_sbi(sb)->si_dbgaufs)
5045 +               return;
5046 +
5047 +       bend = au_sbend(sb);
5048 +       for (; bindex <= bend; bindex++) {
5049 +               br = au_sbr(sb, bindex);
5050 +               xi = &br->br_xino;
5051 +               debugfs_remove(xi->xi_dbgaufs);
5052 +               xi->xi_dbgaufs = NULL;
5053 +       }
5054 +}
5055 +
5056 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
5057 +{
5058 +       struct au_sbinfo *sbinfo;
5059 +       struct dentry *parent;
5060 +       struct au_branch *br;
5061 +       struct au_xino_file *xi;
5062 +       aufs_bindex_t bend;
5063 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
5064 +
5065 +       sbinfo = au_sbi(sb);
5066 +       parent = sbinfo->si_dbgaufs;
5067 +       if (!parent)
5068 +               return;
5069 +
5070 +       bend = au_sbend(sb);
5071 +       for (; bindex <= bend; bindex++) {
5072 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
5073 +               br = au_sbr(sb, bindex);
5074 +               xi = &br->br_xino;
5075 +               AuDebugOn(xi->xi_dbgaufs);
5076 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
5077 +                                                    sbinfo, &dbgaufs_xino_fop);
5078 +               /* ignore an error */
5079 +               if (unlikely(!xi->xi_dbgaufs))
5080 +                       AuWarn1("failed %s under debugfs\n", name);
5081 +       }
5082 +}
5083 +
5084 +/* ---------------------------------------------------------------------- */
5085 +
5086 +#ifdef CONFIG_AUFS_EXPORT
5087 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
5088 +{
5089 +       int err;
5090 +       struct au_sbinfo *sbinfo;
5091 +       struct super_block *sb;
5092 +
5093 +       sbinfo = inode->i_private;
5094 +       sb = sbinfo->si_sb;
5095 +       si_noflush_read_lock(sb);
5096 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
5097 +       si_read_unlock(sb);
5098 +       return err;
5099 +}
5100 +
5101 +static const struct file_operations dbgaufs_xigen_fop = {
5102 +       .owner          = THIS_MODULE,
5103 +       .open           = dbgaufs_xigen_open,
5104 +       .release        = dbgaufs_xi_release,
5105 +       .read           = dbgaufs_xi_read
5106 +};
5107 +
5108 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
5109 +{
5110 +       int err;
5111 +
5112 +       /*
5113 +        * This function is a dynamic '__init' fucntion actually,
5114 +        * so the tiny check for si_rwsem is unnecessary.
5115 +        */
5116 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
5117 +
5118 +       err = -EIO;
5119 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
5120 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5121 +                &dbgaufs_xigen_fop);
5122 +       if (sbinfo->si_dbgaufs_xigen)
5123 +               err = 0;
5124 +
5125 +       return err;
5126 +}
5127 +#else
5128 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
5129 +{
5130 +       return 0;
5131 +}
5132 +#endif /* CONFIG_AUFS_EXPORT */
5133 +
5134 +/* ---------------------------------------------------------------------- */
5135 +
5136 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
5137 +{
5138 +       /*
5139 +        * This function is a dynamic '__init' fucntion actually,
5140 +        * so the tiny check for si_rwsem is unnecessary.
5141 +        */
5142 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
5143 +
5144 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
5145 +       sbinfo->si_dbgaufs = NULL;
5146 +       kobject_put(&sbinfo->si_kobj);
5147 +}
5148 +
5149 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
5150 +{
5151 +       int err;
5152 +       char name[SysaufsSiNameLen];
5153 +
5154 +       /*
5155 +        * This function is a dynamic '__init' fucntion actually,
5156 +        * so the tiny check for si_rwsem is unnecessary.
5157 +        */
5158 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
5159 +
5160 +       err = -ENOENT;
5161 +       if (!dbgaufs) {
5162 +               AuErr1("/debug/aufs is uninitialized\n");
5163 +               goto out;
5164 +       }
5165 +
5166 +       err = -EIO;
5167 +       sysaufs_name(sbinfo, name);
5168 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
5169 +       if (unlikely(!sbinfo->si_dbgaufs))
5170 +               goto out;
5171 +       kobject_get(&sbinfo->si_kobj);
5172 +
5173 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
5174 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5175 +                &dbgaufs_xib_fop);
5176 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
5177 +               goto out_dir;
5178 +
5179 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
5180 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5181 +                &dbgaufs_plink_fop);
5182 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
5183 +               goto out_dir;
5184 +
5185 +       err = dbgaufs_xigen_init(sbinfo);
5186 +       if (!err)
5187 +               goto out; /* success */
5188 +
5189 +out_dir:
5190 +       dbgaufs_si_fin(sbinfo);
5191 +out:
5192 +       return err;
5193 +}
5194 +
5195 +/* ---------------------------------------------------------------------- */
5196 +
5197 +void dbgaufs_fin(void)
5198 +{
5199 +       debugfs_remove(dbgaufs);
5200 +}
5201 +
5202 +int __init dbgaufs_init(void)
5203 +{
5204 +       int err;
5205 +
5206 +       err = -EIO;
5207 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
5208 +       if (dbgaufs)
5209 +               err = 0;
5210 +       return err;
5211 +}
5212 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
5213 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
5214 +++ linux/fs/aufs/dbgaufs.h     2013-07-06 13:20:47.740198107 +0200
5215 @@ -0,0 +1,49 @@
5216 +/*
5217 + * Copyright (C) 2005-2013 Junjiro R. Okajima
5218 + *
5219 + * This program, aufs is free software; you can redistribute it and/or modify
5220 + * it under the terms of the GNU General Public License as published by
5221 + * the Free Software Foundation; either version 2 of the License, or
5222 + * (at your option) any later version.
5223 + *
5224 + * This program is distributed in the hope that it will be useful,
5225 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5226 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5227 + * GNU General Public License for more details.
5228 + *
5229 + * You should have received a copy of the GNU General Public License
5230 + * along with this program; if not, write to the Free Software
5231 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5232 + */
5233 +
5234 +/*
5235 + * debugfs interface
5236 + */
5237 +
5238 +#ifndef __DBGAUFS_H__
5239 +#define __DBGAUFS_H__
5240 +
5241 +#ifdef __KERNEL__
5242 +
5243 +struct super_block;
5244 +struct au_sbinfo;
5245 +
5246 +#ifdef CONFIG_DEBUG_FS
5247 +/* dbgaufs.c */
5248 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
5249 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
5250 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
5251 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
5252 +void dbgaufs_fin(void);
5253 +int __init dbgaufs_init(void);
5254 +#else
5255 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
5256 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
5257 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
5258 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
5259 +AuStubVoid(dbgaufs_fin, void)
5260 +AuStubInt0(__init dbgaufs_init, void)
5261 +#endif /* CONFIG_DEBUG_FS */
5262 +
5263 +#endif /* __KERNEL__ */
5264 +#endif /* __DBGAUFS_H__ */
5265 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
5266 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
5267 +++ linux/fs/aufs/dcsub.c       2013-07-30 22:42:55.839613269 +0200
5268 @@ -0,0 +1,243 @@
5269 +/*
5270 + * Copyright (C) 2005-2013 Junjiro R. Okajima
5271 + *
5272 + * This program, aufs is free software; you can redistribute it and/or modify
5273 + * it under the terms of the GNU General Public License as published by
5274 + * the Free Software Foundation; either version 2 of the License, or
5275 + * (at your option) any later version.
5276 + *
5277 + * This program is distributed in the hope that it will be useful,
5278 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5279 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5280 + * GNU General Public License for more details.
5281 + *
5282 + * You should have received a copy of the GNU General Public License
5283 + * along with this program; if not, write to the Free Software
5284 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5285 + */
5286 +
5287 +/*
5288 + * sub-routines for dentry cache
5289 + */
5290 +
5291 +#include "aufs.h"
5292 +
5293 +static void au_dpage_free(struct au_dpage *dpage)
5294 +{
5295 +       int i;
5296 +       struct dentry **p;
5297 +
5298 +       p = dpage->dentries;
5299 +       for (i = 0; i < dpage->ndentry; i++)
5300 +               dput(*p++);
5301 +       free_page((unsigned long)dpage->dentries);
5302 +}
5303 +
5304 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
5305 +{
5306 +       int err;
5307 +       void *p;
5308 +
5309 +       err = -ENOMEM;
5310 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
5311 +       if (unlikely(!dpages->dpages))
5312 +               goto out;
5313 +
5314 +       p = (void *)__get_free_page(gfp);
5315 +       if (unlikely(!p))
5316 +               goto out_dpages;
5317 +
5318 +       dpages->dpages[0].ndentry = 0;
5319 +       dpages->dpages[0].dentries = p;
5320 +       dpages->ndpage = 1;
5321 +       return 0; /* success */
5322 +
5323 +out_dpages:
5324 +       kfree(dpages->dpages);
5325 +out:
5326 +       return err;
5327 +}
5328 +
5329 +void au_dpages_free(struct au_dcsub_pages *dpages)
5330 +{
5331 +       int i;
5332 +       struct au_dpage *p;
5333 +
5334 +       p = dpages->dpages;
5335 +       for (i = 0; i < dpages->ndpage; i++)
5336 +               au_dpage_free(p++);
5337 +       kfree(dpages->dpages);
5338 +}
5339 +
5340 +static int au_dpages_append(struct au_dcsub_pages *dpages,
5341 +                           struct dentry *dentry, gfp_t gfp)
5342 +{
5343 +       int err, sz;
5344 +       struct au_dpage *dpage;
5345 +       void *p;
5346 +
5347 +       dpage = dpages->dpages + dpages->ndpage - 1;
5348 +       sz = PAGE_SIZE / sizeof(dentry);
5349 +       if (unlikely(dpage->ndentry >= sz)) {
5350 +               AuLabel(new dpage);
5351 +               err = -ENOMEM;
5352 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
5353 +               p = au_kzrealloc(dpages->dpages, sz,
5354 +                                sz + sizeof(*dpages->dpages), gfp);
5355 +               if (unlikely(!p))
5356 +                       goto out;
5357 +
5358 +               dpages->dpages = p;
5359 +               dpage = dpages->dpages + dpages->ndpage;
5360 +               p = (void *)__get_free_page(gfp);
5361 +               if (unlikely(!p))
5362 +                       goto out;
5363 +
5364 +               dpage->ndentry = 0;
5365 +               dpage->dentries = p;
5366 +               dpages->ndpage++;
5367 +       }
5368 +
5369 +       AuDebugOn(!dentry->d_count);
5370 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
5371 +       return 0; /* success */
5372 +
5373 +out:
5374 +       return err;
5375 +}
5376 +
5377 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
5378 +                  au_dpages_test test, void *arg)
5379 +{
5380 +       int err;
5381 +       struct dentry *this_parent;
5382 +       struct list_head *next;
5383 +       struct super_block *sb = root->d_sb;
5384 +
5385 +       err = 0;
5386 +       write_seqlock(&rename_lock);
5387 +       this_parent = root;
5388 +       spin_lock(&this_parent->d_lock);
5389 +repeat:
5390 +       next = this_parent->d_subdirs.next;
5391 +resume:
5392 +       if (this_parent->d_sb == sb
5393 +           && !IS_ROOT(this_parent)
5394 +           && au_di(this_parent)
5395 +           && this_parent->d_count
5396 +           && (!test || test(this_parent, arg))) {
5397 +               err = au_dpages_append(dpages, this_parent, GFP_ATOMIC);
5398 +               if (unlikely(err))
5399 +                       goto out;
5400 +       }
5401 +
5402 +       while (next != &this_parent->d_subdirs) {
5403 +               struct list_head *tmp = next;
5404 +               struct dentry *dentry = list_entry(tmp, struct dentry,
5405 +                                                  d_u.d_child);
5406 +
5407 +               next = tmp->next;
5408 +               spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
5409 +               if (dentry->d_count) {
5410 +                       if (!list_empty(&dentry->d_subdirs)) {
5411 +                               spin_unlock(&this_parent->d_lock);
5412 +                               spin_release(&dentry->d_lock.dep_map, 1,
5413 +                                            _RET_IP_);
5414 +                               this_parent = dentry;
5415 +                               spin_acquire(&this_parent->d_lock.dep_map, 0, 1,
5416 +                                            _RET_IP_);
5417 +                               goto repeat;
5418 +                       }
5419 +                       if (dentry->d_sb == sb
5420 +                           && au_di(dentry)
5421 +                           && (!test || test(dentry, arg)))
5422 +                               err = au_dpages_append(dpages, dentry,
5423 +                                                      GFP_ATOMIC);
5424 +               }
5425 +               spin_unlock(&dentry->d_lock);
5426 +               if (unlikely(err))
5427 +                       goto out;
5428 +       }
5429 +
5430 +       if (this_parent != root) {
5431 +               struct dentry *tmp;
5432 +               struct dentry *child;
5433 +
5434 +               tmp = this_parent->d_parent;
5435 +               rcu_read_lock();
5436 +               spin_unlock(&this_parent->d_lock);
5437 +               child = this_parent;
5438 +               this_parent = tmp;
5439 +               spin_lock(&this_parent->d_lock);
5440 +               rcu_read_unlock();
5441 +               next = child->d_u.d_child.next;
5442 +               goto resume;
5443 +       }
5444 +
5445 +out:
5446 +       spin_unlock(&this_parent->d_lock);
5447 +       write_sequnlock(&rename_lock);
5448 +       return err;
5449 +}
5450 +
5451 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5452 +                      int do_include, au_dpages_test test, void *arg)
5453 +{
5454 +       int err;
5455 +
5456 +       err = 0;
5457 +       write_seqlock(&rename_lock);
5458 +       spin_lock(&dentry->d_lock);
5459 +       if (do_include
5460 +           && dentry->d_count
5461 +           && (!test || test(dentry, arg)))
5462 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5463 +       spin_unlock(&dentry->d_lock);
5464 +       if (unlikely(err))
5465 +               goto out;
5466 +
5467 +       /*
5468 +        * vfsmount_lock is unnecessary since this is a traverse in a single
5469 +        * mount
5470 +        */
5471 +       while (!IS_ROOT(dentry)) {
5472 +               dentry = dentry->d_parent; /* rename_lock is locked */
5473 +               spin_lock(&dentry->d_lock);
5474 +               if (dentry->d_count
5475 +                   && (!test || test(dentry, arg)))
5476 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5477 +               spin_unlock(&dentry->d_lock);
5478 +               if (unlikely(err))
5479 +                       break;
5480 +       }
5481 +
5482 +out:
5483 +       write_sequnlock(&rename_lock);
5484 +       return err;
5485 +}
5486 +
5487 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
5488 +{
5489 +       return au_di(dentry) && dentry->d_sb == arg;
5490 +}
5491 +
5492 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
5493 +                           struct dentry *dentry, int do_include)
5494 +{
5495 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
5496 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
5497 +}
5498 +
5499 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
5500 +{
5501 +       struct path path[2] = {
5502 +               {
5503 +                       .dentry = d1
5504 +               },
5505 +               {
5506 +                       .dentry = d2
5507 +               }
5508 +       };
5509 +
5510 +       return path_is_under(path + 0, path + 1);
5511 +}
5512 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
5513 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
5514 +++ linux/fs/aufs/dcsub.h       2013-07-06 13:20:47.740198107 +0200
5515 @@ -0,0 +1,94 @@
5516 +/*
5517 + * Copyright (C) 2005-2013 Junjiro R. Okajima
5518 + *
5519 + * This program, aufs is free software; you can redistribute it and/or modify
5520 + * it under the terms of the GNU General Public License as published by
5521 + * the Free Software Foundation; either version 2 of the License, or
5522 + * (at your option) any later version.
5523 + *
5524 + * This program is distributed in the hope that it will be useful,
5525 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5526 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5527 + * GNU General Public License for more details.
5528 + *
5529 + * You should have received a copy of the GNU General Public License
5530 + * along with this program; if not, write to the Free Software
5531 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5532 + */
5533 +
5534 +/*
5535 + * sub-routines for dentry cache
5536 + */
5537 +
5538 +#ifndef __AUFS_DCSUB_H__
5539 +#define __AUFS_DCSUB_H__
5540 +
5541 +#ifdef __KERNEL__
5542 +
5543 +#include <linux/dcache.h>
5544 +#include <linux/fs.h>
5545 +
5546 +struct dentry;
5547 +
5548 +struct au_dpage {
5549 +       int ndentry;
5550 +       struct dentry **dentries;
5551 +};
5552 +
5553 +struct au_dcsub_pages {
5554 +       int ndpage;
5555 +       struct au_dpage *dpages;
5556 +};
5557 +
5558 +/* ---------------------------------------------------------------------- */
5559 +
5560 +/* dcsub.c */
5561 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
5562 +void au_dpages_free(struct au_dcsub_pages *dpages);
5563 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
5564 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
5565 +                  au_dpages_test test, void *arg);
5566 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5567 +                      int do_include, au_dpages_test test, void *arg);
5568 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
5569 +                           struct dentry *dentry, int do_include);
5570 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
5571 +
5572 +/* ---------------------------------------------------------------------- */
5573 +
5574 +static inline int au_d_hashed_positive(struct dentry *d)
5575 +{
5576 +       int err;
5577 +       struct inode *inode = d->d_inode;
5578 +       err = 0;
5579 +       if (unlikely(d_unhashed(d) || !inode || !inode->i_nlink))
5580 +               err = -ENOENT;
5581 +       return err;
5582 +}
5583 +
5584 +static inline int au_d_alive(struct dentry *d)
5585 +{
5586 +       int err;
5587 +       struct inode *inode;
5588 +       err = 0;
5589 +       if (!IS_ROOT(d))
5590 +               err = au_d_hashed_positive(d);
5591 +       else {
5592 +               inode = d->d_inode;
5593 +               if (unlikely(d_unlinked(d) || !inode || !inode->i_nlink))
5594 +                       err = -ENOENT;
5595 +       }
5596 +       return err;
5597 +}
5598 +
5599 +static inline int au_alive_dir(struct dentry *d)
5600 +{
5601 +       int err;
5602 +       err = au_d_alive(d);
5603 +       if (unlikely(err || IS_DEADDIR(d->d_inode)))
5604 +               err = -ENOENT;
5605 +       return err;
5606 +}
5607 +
5608 +#endif /* __KERNEL__ */
5609 +#endif /* __AUFS_DCSUB_H__ */
5610 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
5611 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
5612 +++ linux/fs/aufs/debug.c       2013-08-23 23:59:39.634916914 +0200
5613 @@ -0,0 +1,491 @@
5614 +/*
5615 + * Copyright (C) 2005-2013 Junjiro R. Okajima
5616 + *
5617 + * This program, aufs is free software; you can redistribute it and/or modify
5618 + * it under the terms of the GNU General Public License as published by
5619 + * the Free Software Foundation; either version 2 of the License, or
5620 + * (at your option) any later version.
5621 + *
5622 + * This program is distributed in the hope that it will be useful,
5623 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5624 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5625 + * GNU General Public License for more details.
5626 + *
5627 + * You should have received a copy of the GNU General Public License
5628 + * along with this program; if not, write to the Free Software
5629 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
5630 + */
5631 +
5632 +/*
5633 + * debug print functions
5634 + */
5635 +
5636 +#include <linux/vt_kern.h>
5637 +#include "aufs.h"
5638 +
5639 +int aufs_debug;
5640 +MODULE_PARM_DESC(debug, "debug print");
5641 +module_param_named(debug, aufs_debug, int, S_IRUGO | S_IWUSR | S_IWGRP);
5642 +
5643 +char *au_plevel = KERN_DEBUG;
5644 +#define dpri(fmt, ...) do {                                    \
5645 +       if ((au_plevel                                          \
5646 +            && strcmp(au_plevel, KERN_DEBUG))                  \
5647 +           || au_debug_test())                                 \
5648 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
5649 +} while (0)
5650 +
5651 +/* ---------------------------------------------------------------------- */
5652 +
5653 +void au_dpri_whlist(struct au_nhash *whlist)
5654 +{
5655 +       unsigned long ul, n;
5656 +       struct hlist_head *head;
5657 +       struct au_vdir_wh *pos;
5658 +
5659 +       n = whlist->nh_num;
5660 +       head = whlist->nh_head;
5661 +       for (ul = 0; ul < n; ul++) {
5662 +               hlist_for_each_entry(pos, head, wh_hash)
5663 +                       dpri("b%d, %.*s, %d\n",
5664 +                            pos->wh_bindex,
5665 +                            pos->wh_str.len, pos->wh_str.name,
5666 +                            pos->wh_str.len);
5667 +               head++;
5668 +       }
5669 +}
5670 +
5671 +void au_dpri_vdir(struct au_vdir *vdir)
5672 +{
5673 +       unsigned long ul;
5674 +       union au_vdir_deblk_p p;
5675 +       unsigned char *o;
5676 +
5677 +       if (!vdir || IS_ERR(vdir)) {
5678 +               dpri("err %ld\n", PTR_ERR(vdir));
5679 +               return;
5680 +       }
5681 +
5682 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
5683 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
5684 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
5685 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
5686 +               p.deblk = vdir->vd_deblk[ul];
5687 +               o = p.deblk;
5688 +               dpri("[%lu]: %p\n", ul, o);
5689 +       }
5690 +}
5691 +
5692 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
5693 +                       struct dentry *wh)
5694 +{
5695 +       char *n = NULL;
5696 +       int l = 0;
5697 +
5698 +       if (!inode || IS_ERR(inode)) {
5699 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
5700 +               return -1;
5701 +       }
5702 +
5703 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
5704 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
5705 +                    && sizeof(inode->i_blocks) != sizeof(u64));
5706 +       if (wh) {
5707 +               n = (void *)wh->d_name.name;
5708 +               l = wh->d_name.len;
5709 +       }
5710 +
5711 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
5712 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
5713 +            bindex, inode,
5714 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
5715 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
5716 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
5717 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
5718 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
5719 +            inode->i_state, inode->i_flags, inode->i_version,
5720 +            inode->i_generation,
5721 +            l ? ", wh " : "", l, n);
5722 +       return 0;
5723 +}
5724 +
5725 +void au_dpri_inode(struct inode *inode)
5726 +{
5727 +       struct au_iinfo *iinfo;
5728 +       aufs_bindex_t bindex;
5729 +       int err, hn;
5730 +
5731 +       err = do_pri_inode(-1, inode, -1, NULL);
5732 +       if (err || !au_test_aufs(inode->i_sb))
5733 +               return;
5734 +
5735 +       iinfo = au_ii(inode);
5736 +       if (!iinfo)
5737 +               return;
5738 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
5739 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
5740 +       if (iinfo->ii_bstart < 0)
5741 +               return;
5742 +       hn = 0;
5743 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
5744 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
5745 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
5746 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
5747 +       }
5748 +}
5749 +
5750 +void au_dpri_dalias(struct inode *inode)
5751 +{
5752 +       struct dentry *d;
5753 +
5754 +       spin_lock(&inode->i_lock);
5755 +       hlist_for_each_entry(d, &inode->i_dentry, d_alias)
5756 +               au_dpri_dentry(d);
5757 +       spin_unlock(&inode->i_lock);
5758 +}
5759 +
5760 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
5761 +{
5762 +       struct dentry *wh = NULL;
5763 +       int hn;
5764 +
5765 +       if (!dentry || IS_ERR(dentry)) {
5766 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
5767 +               return -1;
5768 +       }
5769 +       /* do not call dget_parent() here */
5770 +       /* note: access d_xxx without d_lock */
5771 +       dpri("d%d: %.*s?/%.*s, %s, cnt %d, flags 0x%x\n",
5772 +            bindex,
5773 +            AuDLNPair(dentry->d_parent), AuDLNPair(dentry),
5774 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
5775 +            dentry->d_count, dentry->d_flags);
5776 +       hn = -1;
5777 +       if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
5778 +               struct au_iinfo *iinfo = au_ii(dentry->d_inode);
5779 +               if (iinfo) {
5780 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
5781 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
5782 +               }
5783 +       }
5784 +       do_pri_inode(bindex, dentry->d_inode, hn, wh);
5785 +       return 0;
5786 +}
5787 +
5788 +void au_dpri_dentry(struct dentry *dentry)
5789 +{
5790 +       struct au_dinfo *dinfo;
5791 +       aufs_bindex_t bindex;
5792 +       int err;
5793 +       struct au_hdentry *hdp;
5794 +
5795 +       err = do_pri_dentry(-1, dentry);
5796 +       if (err || !au_test_aufs(dentry->d_sb))
5797 +               return;
5798 +
5799 +       dinfo = au_di(dentry);
5800 +       if (!dinfo)
5801 +               return;
5802 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d\n",
5803 +            dinfo->di_bstart, dinfo->di_bend,
5804 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry));
5805 +       if (dinfo->di_bstart < 0)
5806 +               return;
5807 +       hdp = dinfo->di_hdentry;
5808 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
5809 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
5810 +}
5811 +
5812 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
5813 +{
5814 +       char a[32];
5815 +
5816 +       if (!file || IS_ERR(file)) {
5817 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
5818 +               return -1;
5819 +       }
5820 +       a[0] = 0;
5821 +       if (bindex < 0
5822 +           && file->f_dentry
5823 +           && au_test_aufs(file->f_dentry->d_sb)
5824 +           && au_fi(file))
5825 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
5826 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
5827 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
5828 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
5829 +            file->f_version, file->f_pos, a);
5830 +       if (file->f_dentry)
5831 +               do_pri_dentry(bindex, file->f_dentry);
5832 +       return 0;
5833 +}
5834 +
5835 +void au_dpri_file(struct file *file)
5836 +{
5837 +       struct au_finfo *finfo;
5838 +       struct au_fidir *fidir;
5839 +       struct au_hfile *hfile;
5840 +       aufs_bindex_t bindex;
5841 +       int err;
5842 +
5843 +       err = do_pri_file(-1, file);
5844 +       if (err || !file->f_dentry || !au_test_aufs(file->f_dentry->d_sb))
5845 +               return;
5846 +
5847 +       finfo = au_fi(file);
5848 +       if (!finfo)
5849 +               return;
5850 +       if (finfo->fi_btop < 0)
5851 +               return;
5852 +       fidir = finfo->fi_hdir;
5853 +       if (!fidir)
5854 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
5855 +       else
5856 +               for (bindex = finfo->fi_btop;
5857 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
5858 +                    bindex++) {
5859 +                       hfile = fidir->fd_hfile + bindex;
5860 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
5861 +               }
5862 +}
5863 +
5864 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
5865 +{
5866 +       struct vfsmount *mnt;
5867 +       struct super_block *sb;
5868 +
5869 +       if (!br || IS_ERR(br))
5870 +               goto out;
5871 +       mnt = au_br_mnt(br);
5872 +       if (!mnt || IS_ERR(mnt))
5873 +               goto out;
5874 +       sb = mnt->mnt_sb;
5875 +       if (!sb || IS_ERR(sb))
5876 +               goto out;
5877 +
5878 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
5879 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
5880 +            "xino %d\n",
5881 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
5882 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
5883 +            sb->s_flags, sb->s_count,
5884 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
5885 +       return 0;
5886 +
5887 +out:
5888 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
5889 +       return -1;
5890 +}
5891 +
5892 +void au_dpri_sb(struct super_block *sb)
5893 +{
5894 +       struct au_sbinfo *sbinfo;
5895 +       aufs_bindex_t bindex;
5896 +       int err;
5897 +       /* to reuduce stack size */
5898 +       struct {
5899 +               struct vfsmount mnt;
5900 +               struct au_branch fake;
5901 +       } *a;
5902 +
5903 +       /* this function can be called from magic sysrq */
5904 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
5905 +       if (unlikely(!a)) {
5906 +               dpri("no memory\n");
5907 +               return;
5908 +       }
5909 +
5910 +       a->mnt.mnt_sb = sb;
5911 +       a->fake.br_perm = 0;
5912 +       a->fake.br_path.mnt = &a->mnt;
5913 +       a->fake.br_xino.xi_file = NULL;
5914 +       atomic_set(&a->fake.br_count, 0);
5915 +       smp_mb(); /* atomic_set */
5916 +       err = do_pri_br(-1, &a->fake);
5917 +       kfree(a);
5918 +       dpri("dev 0x%x\n", sb->s_dev);
5919 +       if (err || !au_test_aufs(sb))
5920 +               return;
5921 +
5922 +       sbinfo = au_sbi(sb);
5923 +       if (!sbinfo)
5924 +               return;
5925 +       dpri("nw %d, gen %u, kobj %d\n",
5926 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
5927 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
5928 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
5929 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
5930 +}
5931 +
5932 +/* ---------------------------------------------------------------------- */
5933 +
5934 +void au_dbg_sleep_jiffy(int jiffy)
5935 +{
5936 +       while (jiffy)
5937 +               jiffy = schedule_timeout_uninterruptible(jiffy);
5938 +}
5939 +
5940 +void au_dbg_iattr(struct iattr *ia)
5941 +{
5942 +#define AuBit(name)                                    \
5943 +       do {                                            \
5944 +               if (ia->ia_valid & ATTR_ ## name)       \
5945 +                       dpri(#name "\n");               \
5946 +       } while (0)
5947 +       AuBit(MODE);
5948 +       AuBit(UID);
5949 +       AuBit(GID);
5950 +       AuBit(SIZE);
5951 +       AuBit(ATIME);
5952 +       AuBit(MTIME);
5953 +       AuBit(CTIME);
5954 +       AuBit(ATIME_SET);
5955 +       AuBit(MTIME_SET);
5956 +       AuBit(FORCE);
5957 +       AuBit(ATTR_FLAG);
5958 +       AuBit(KILL_SUID);
5959 +       AuBit(KILL_SGID);
5960 +       AuBit(FILE);
5961 +       AuBit(KILL_PRIV);
5962 +       AuBit(OPEN);
5963 +       AuBit(TIMES_SET);
5964 +#undef AuBit
5965 +       dpri("ia_file %p\n", ia->ia_file);
5966 +}
5967 +
5968 +/* ---------------------------------------------------------------------- */
5969 +
5970 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
5971 +{
5972 +       struct inode *h_inode, *inode = dentry->d_inode;
5973 +       struct dentry *h_dentry;
5974 +       aufs_bindex_t bindex, bend, bi;
5975 +
5976 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
5977 +               return;
5978 +
5979 +       bend = au_dbend(dentry);
5980 +       bi = au_ibend(inode);
5981 +       if (bi < bend)
5982 +               bend = bi;
5983 +       bindex = au_dbstart(dentry);
5984 +       bi = au_ibstart(inode);
5985 +       if (bi > bindex)
5986 +               bindex = bi;
5987 +
5988 +       for (; bindex <= bend; bindex++) {
5989 +               h_dentry = au_h_dptr(dentry, bindex);
5990 +               if (!h_dentry)
5991 +                       continue;
5992 +               h_inode = au_h_iptr(inode, bindex);
5993 +               if (unlikely(h_inode != h_dentry->d_inode)) {
5994 +                       int old = au_debug_test();
5995 +                       if (!old)
5996 +                               au_debug(1);
5997 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
5998 +                       AuDbgDentry(dentry);
5999 +                       AuDbgInode(inode);
6000 +                       if (!old)
6001 +                               au_debug(0);
6002 +                       BUG();
6003 +               }
6004 +       }
6005 +}
6006 +
6007 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen)
6008 +{
6009 +       struct dentry *parent;
6010 +
6011 +       parent = dget_parent(dentry);
6012 +       AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
6013 +       AuDebugOn(IS_ROOT(dentry));
6014 +       AuDebugOn(au_digen_test(parent, sigen));
6015 +       dput(parent);
6016 +}
6017 +
6018 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen)
6019 +{
6020 +       struct dentry *parent;
6021 +       struct inode *inode;
6022 +
6023 +       parent = dget_parent(dentry);
6024 +       inode = dentry->d_inode;
6025 +       AuDebugOn(inode && S_ISDIR(dentry->d_inode->i_mode));
6026 +       AuDebugOn(au_digen_test(parent, sigen));
6027 +       dput(parent);
6028 +}
6029 +
6030 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
6031 +{
6032 +       int err, i, j;
6033 +       struct au_dcsub_pages dpages;
6034 +       struct au_dpage *dpage;
6035 +       struct dentry **dentries;
6036 +
6037 +       err = au_dpages_init(&dpages, GFP_NOFS);
6038 +       AuDebugOn(err);
6039 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
6040 +       AuDebugOn(err);
6041 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
6042 +               dpage = dpages.dpages + i;
6043 +               dentries = dpage->dentries;
6044 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
6045 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
6046 +       }
6047 +       au_dpages_free(&dpages);
6048 +}
6049 +
6050 +void au_dbg_verify_kthread(void)
6051 +{
6052 +       if (au_wkq_test()) {
6053 +               au_dbg_blocked();
6054 +               /*
6055 +                * It may be recursive, but udba=notify between two aufs mounts,
6056 +                * where a single ro branch is shared, is not a problem.
6057 +                */
6058 +               /* WARN_ON(1); */
6059 +       }
6060 +}
6061 +
6062 +/* ---------------------------------------------------------------------- */
6063 +
6064 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo __maybe_unused)
6065 +{
6066 +#ifdef AuForceNoPlink
6067 +       au_opt_clr(sbinfo->si_mntflags, PLINK);
6068 +#endif
6069 +#ifdef AuForceNoXino
6070 +       au_opt_clr(sbinfo->si_mntflags, XINO);
6071 +#endif
6072 +#ifdef AuForceNoRefrof
6073 +       au_opt_clr(sbinfo->si_mntflags, REFROF);
6074 +#endif
6075 +#ifdef AuForceHnotify
6076 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_HNOTIFY);
6077 +#endif
6078 +#ifdef AuForceRd0
6079 +       sbinfo->si_rdblk = 0;
6080 +       sbinfo->si_rdhash = 0;
6081 +#endif
6082 +}
6083 +
6084 +int __init au_debug_init(void)
6085 +{
6086 +       aufs_bindex_t bindex;
6087 +       struct au_vdir_destr destr;
6088 +
6089 +       bindex = -1;
6090 +       AuDebugOn(bindex >= 0);
6091 +
6092 +       destr.len = -1;
6093 +       AuDebugOn(destr.len < NAME_MAX);
6094 +
6095 +#ifdef CONFIG_4KSTACKS
6096 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
6097 +#endif
6098 +
6099 +#ifdef AuForceNoBrs
6100 +       sysaufs_brs = 0;
6101 +#endif
6102 +
6103 +       return 0;
6104 +}
6105 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
6106 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
6107 +++ linux/fs/aufs/debug.h       2013-07-06 13:20:47.740198107 +0200
6108 @@ -0,0 +1,242 @@
6109 +/*
6110 + * Copyright (C) 2005-2013 Junjiro R. Okajima
6111 + *
6112 + * This program, aufs is free software; you can redistribute it and/or modify
6113 + * it under the terms of the GNU General Public License as published by
6114 + * the Free Software Foundation; either version 2 of the License, or
6115 + * (at your option) any later version.
6116 + *
6117 + * This program is distributed in the hope that it will be useful,
6118 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6119 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6120 + * GNU General Public License for more details.
6121 + *
6122 + * You should have received a copy of the GNU General Public License
6123 + * along with this program; if not, write to the Free Software
6124 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
6125 + */
6126 +
6127 +/*
6128 + * debug print functions
6129 + */
6130 +
6131 +#ifndef __AUFS_DEBUG_H__
6132 +#define __AUFS_DEBUG_H__
6133 +
6134 +#ifdef __KERNEL__
6135 +
6136 +#include <linux/module.h>
6137 +#include <linux/kallsyms.h>
6138 +#include <linux/sysrq.h>
6139 +
6140 +#ifdef CONFIG_AUFS_DEBUG
6141 +#define AuDebugOn(a)           BUG_ON(a)
6142 +
6143 +/* module parameter */
6144 +extern int aufs_debug;
6145 +static inline void au_debug(int n)
6146 +{
6147 +       aufs_debug = n;
6148 +       smp_mb();
6149 +}
6150 +
6151 +static inline int au_debug_test(void)
6152 +{
6153 +       return aufs_debug;
6154 +}
6155 +#else
6156 +#define AuDebugOn(a)           do {} while (0)
6157 +AuStubVoid(au_debug, int n)
6158 +AuStubInt0(au_debug_test, void)
6159 +#endif /* CONFIG_AUFS_DEBUG */
6160 +
6161 +/* ---------------------------------------------------------------------- */
6162 +
6163 +/* debug print */
6164 +
6165 +#define AuDbg(fmt, ...) do { \
6166 +       if (au_debug_test()) \
6167 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
6168 +} while (0)
6169 +#define AuLabel(l)             AuDbg(#l "\n")
6170 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
6171 +#define AuWarn1(fmt, ...) do { \
6172 +       static unsigned char _c; \
6173 +       if (!_c++) \
6174 +               pr_warn(fmt, ##__VA_ARGS__); \
6175 +} while (0)
6176 +
6177 +#define AuErr1(fmt, ...) do { \
6178 +       static unsigned char _c; \
6179 +       if (!_c++) \
6180 +               pr_err(fmt, ##__VA_ARGS__); \
6181 +} while (0)
6182 +
6183 +#define AuIOErr1(fmt, ...) do { \
6184 +       static unsigned char _c; \
6185 +       if (!_c++) \
6186 +               AuIOErr(fmt, ##__VA_ARGS__); \
6187 +} while (0)
6188 +
6189 +#define AuUnsupportMsg "This operation is not supported." \
6190 +                       " Please report this application to aufs-users ML."
6191 +#define AuUnsupport(fmt, ...) do { \
6192 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
6193 +       dump_stack(); \
6194 +} while (0)
6195 +
6196 +#define AuTraceErr(e) do { \
6197 +       if (unlikely((e) < 0)) \
6198 +               AuDbg("err %d\n", (int)(e)); \
6199 +} while (0)
6200 +
6201 +#define AuTraceErrPtr(p) do { \
6202 +       if (IS_ERR(p)) \
6203 +               AuDbg("err %ld\n", PTR_ERR(p)); \
6204 +} while (0)
6205 +
6206 +/* dirty macros for debug print, use with "%.*s" and caution */
6207 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
6208 +#define AuDLNPair(d)           AuLNPair(&(d)->d_name)
6209 +
6210 +/* ---------------------------------------------------------------------- */
6211 +
6212 +struct au_sbinfo;
6213 +struct au_finfo;
6214 +struct dentry;
6215 +#ifdef CONFIG_AUFS_DEBUG
6216 +extern char *au_plevel;
6217 +struct au_nhash;
6218 +void au_dpri_whlist(struct au_nhash *whlist);
6219 +struct au_vdir;
6220 +void au_dpri_vdir(struct au_vdir *vdir);
6221 +struct inode;
6222 +void au_dpri_inode(struct inode *inode);
6223 +void au_dpri_dalias(struct inode *inode);
6224 +void au_dpri_dentry(struct dentry *dentry);
6225 +struct file;
6226 +void au_dpri_file(struct file *filp);
6227 +struct super_block;
6228 +void au_dpri_sb(struct super_block *sb);
6229 +
6230 +void au_dbg_sleep_jiffy(int jiffy);
6231 +struct iattr;
6232 +void au_dbg_iattr(struct iattr *ia);
6233 +
6234 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
6235 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
6236 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen);
6237 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen);
6238 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
6239 +void au_dbg_verify_kthread(void);
6240 +
6241 +int __init au_debug_init(void);
6242 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo);
6243 +#define AuDbgWhlist(w) do { \
6244 +       AuDbg(#w "\n"); \
6245 +       au_dpri_whlist(w); \
6246 +} while (0)
6247 +
6248 +#define AuDbgVdir(v) do { \
6249 +       AuDbg(#v "\n"); \
6250 +       au_dpri_vdir(v); \
6251 +} while (0)
6252 +
6253 +#define AuDbgInode(i) do { \
6254 +       AuDbg(#i "\n"); \
6255 +       au_dpri_inode(i); \
6256 +} while (0)
6257 +
6258 +#define AuDbgDAlias(i) do { \
6259 +       AuDbg(#i "\n"); \
6260 +       au_dpri_dalias(i); \
6261 +} while (0)
6262 +
6263 +#define AuDbgDentry(d) do { \
6264 +       AuDbg(#d "\n"); \
6265 +       au_dpri_dentry(d); \
6266 +} while (0)
6267 +
6268 +#define AuDbgFile(f) do { \
6269 +       AuDbg(#f "\n"); \
6270 +       au_dpri_file(f); \
6271 +} while (0)
6272 +
6273 +#define AuDbgSb(sb) do { \
6274 +       AuDbg(#sb "\n"); \
6275 +       au_dpri_sb(sb); \
6276 +} while (0)
6277 +
6278 +#define AuDbgSleep(sec) do { \
6279 +       AuDbg("sleep %d sec\n", sec); \
6280 +       ssleep(sec); \
6281 +} while (0)
6282 +
6283 +#define AuDbgSleepJiffy(jiffy) do { \
6284 +       AuDbg("sleep %d jiffies\n", jiffy); \
6285 +       au_dbg_sleep_jiffy(jiffy); \
6286 +} while (0)
6287 +
6288 +#define AuDbgIAttr(ia) do { \
6289 +       AuDbg("ia_valid 0x%x\n", (ia)->ia_valid); \
6290 +       au_dbg_iattr(ia); \
6291 +} while (0)
6292 +
6293 +#define AuDbgSym(addr) do {                            \
6294 +       char sym[KSYM_SYMBOL_LEN];                      \
6295 +       sprint_symbol(sym, (unsigned long)addr);        \
6296 +       AuDbg("%s\n", sym);                             \
6297 +} while (0)
6298 +
6299 +#define AuInfoSym(addr) do {                           \
6300 +       char sym[KSYM_SYMBOL_LEN];                      \
6301 +       sprint_symbol(sym, (unsigned long)addr);        \
6302 +       AuInfo("%s\n", sym);                            \
6303 +} while (0)
6304 +#else
6305 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
6306 +AuStubVoid(au_dbg_verify_dir_parent, struct dentry *dentry, unsigned int sigen)
6307 +AuStubVoid(au_dbg_verify_nondir_parent, struct dentry *dentry,
6308 +          unsigned int sigen)
6309 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
6310 +AuStubVoid(au_dbg_verify_kthread, void)
6311 +AuStubInt0(__init au_debug_init, void)
6312 +AuStubVoid(au_debug_sbinfo_init, struct au_sbinfo *sbinfo)
6313 +
6314 +#define AuDbgWhlist(w)         do {} while (0)
6315 +#define AuDbgVdir(v)           do {} while (0)
6316 +#define AuDbgInode(i)          do {} while (0)
6317 +#define AuDbgDAlias(i)         do {} while (0)
6318 +#define AuDbgDentry(d)         do {} while (0)
6319 +#define AuDbgFile(f)           do {} while (0)
6320 +#define AuDbgSb(sb)            do {} while (0)
6321 +#define AuDbgSleep(sec)                do {} while (0)
6322 +#define AuDbgSleepJiffy(jiffy) do {} while (0)
6323 +#define AuDbgIAttr(ia)         do {} while (0)
6324 +#define AuDbgSym(addr)         do {} while (0)
6325 +#define AuInfoSym(addr)                do {} while (0)
6326 +#endif /* CONFIG_AUFS_DEBUG */
6327 +
6328 +/* ---------------------------------------------------------------------- */
6329 +
6330 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
6331 +int __init au_sysrq_init(void);
6332 +void au_sysrq_fin(void);
6333 +
6334 +#ifdef CONFIG_HW_CONSOLE
6335 +#define au_dbg_blocked() do { \
6336 +       WARN_ON(1); \
6337 +       handle_sysrq('w'); \
6338 +} while (0)
6339 +#else
6340 +AuStubVoid(au_dbg_blocked, void)
6341 +#endif
6342 +
6343 +#else
6344 +AuStubInt0(__init au_sysrq_init, void)
6345 +AuStubVoid(au_sysrq_fin, void)
6346 +AuStubVoid(au_dbg_blocked, void)
6347 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
6348 +
6349 +#endif /* __KERNEL__ */
6350 +#endif /* __AUFS_DEBUG_H__ */
6351 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
6352 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
6353 +++ linux/fs/aufs/dentry.c      2013-07-06 13:20:47.740198107 +0200
6354 @@ -0,0 +1,1065 @@
6355 +/*
6356 + * Copyright (C) 2005-2013 Junjiro R. Okajima
6357 + *
6358 + * This program, aufs is free software; you can redistribute it and/or modify
6359 + * it under the terms of the GNU General Public License as published by
6360 + * the Free Software Foundation; either version 2 of the License, or
6361 + * (at your option) any later version.
6362 + *
6363 + * This program is distributed in the hope that it will be useful,
6364 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6365 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6366 + * GNU General Public License for more details.
6367 + *
6368 + * You should have received a copy of the GNU General Public License
6369 + * along with this program; if not, write to the Free Software
6370 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
6371 + */
6372 +
6373 +/*
6374 + * lookup and dentry operations
6375 + */
6376 +
6377 +#include <linux/namei.h>
6378 +#include "aufs.h"
6379 +
6380 +#define AuLkup_ALLOW_NEG       1
6381 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
6382 +#define au_fset_lkup(flags, name) \
6383 +       do { (flags) |= AuLkup_##name; } while (0)
6384 +#define au_fclr_lkup(flags, name) \
6385 +       do { (flags) &= ~AuLkup_##name; } while (0)
6386 +
6387 +struct au_do_lookup_args {
6388 +       unsigned int            flags;
6389 +       mode_t                  type;
6390 +};
6391 +
6392 +/*
6393 + * returns positive/negative dentry, NULL or an error.
6394 + * NULL means whiteout-ed or not-found.
6395 + */
6396 +static struct dentry*
6397 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
6398 +            aufs_bindex_t bindex, struct qstr *wh_name,
6399 +            struct au_do_lookup_args *args)
6400 +{
6401 +       struct dentry *h_dentry;
6402 +       struct inode *h_inode, *inode;
6403 +       struct au_branch *br;
6404 +       int wh_found, opq;
6405 +       unsigned char wh_able;
6406 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
6407 +
6408 +       wh_found = 0;
6409 +       br = au_sbr(dentry->d_sb, bindex);
6410 +       wh_able = !!au_br_whable(br->br_perm);
6411 +       if (wh_able)
6412 +               wh_found = au_wh_test(h_parent, wh_name, br, /*try_sio*/0);
6413 +       h_dentry = ERR_PTR(wh_found);
6414 +       if (!wh_found)
6415 +               goto real_lookup;
6416 +       if (unlikely(wh_found < 0))
6417 +               goto out;
6418 +
6419 +       /* We found a whiteout */
6420 +       /* au_set_dbend(dentry, bindex); */
6421 +       au_set_dbwh(dentry, bindex);
6422 +       if (!allow_neg)
6423 +               return NULL; /* success */
6424 +
6425 +real_lookup:
6426 +       h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
6427 +       if (IS_ERR(h_dentry))
6428 +               goto out;
6429 +
6430 +       h_inode = h_dentry->d_inode;
6431 +       if (!h_inode) {
6432 +               if (!allow_neg)
6433 +                       goto out_neg;
6434 +       } else if (wh_found
6435 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
6436 +               goto out_neg;
6437 +
6438 +       if (au_dbend(dentry) <= bindex)
6439 +               au_set_dbend(dentry, bindex);
6440 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
6441 +               au_set_dbstart(dentry, bindex);
6442 +       au_set_h_dptr(dentry, bindex, h_dentry);
6443 +
6444 +       inode = dentry->d_inode;
6445 +       if (!h_inode || !S_ISDIR(h_inode->i_mode) || !wh_able
6446 +           || (inode && !S_ISDIR(inode->i_mode)))
6447 +               goto out; /* success */
6448 +
6449 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
6450 +       opq = au_diropq_test(h_dentry, br);
6451 +       mutex_unlock(&h_inode->i_mutex);
6452 +       if (opq > 0)
6453 +               au_set_dbdiropq(dentry, bindex);
6454 +       else if (unlikely(opq < 0)) {
6455 +               au_set_h_dptr(dentry, bindex, NULL);
6456 +               h_dentry = ERR_PTR(opq);
6457 +       }
6458 +       goto out;
6459 +
6460 +out_neg:
6461 +       dput(h_dentry);
6462 +       h_dentry = NULL;
6463 +out:
6464 +       return h_dentry;
6465 +}
6466 +
6467 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
6468 +{
6469 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
6470 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
6471 +               return -EPERM;
6472 +       return 0;
6473 +}
6474 +
6475 +/*
6476 + * returns the number of lower positive dentries,
6477 + * otherwise an error.
6478 + * can be called at unlinking with @type is zero.
6479 + */
6480 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
6481 +{
6482 +       int npositive, err;
6483 +       aufs_bindex_t bindex, btail, bdiropq;
6484 +       unsigned char isdir;
6485 +       struct qstr whname;
6486 +       struct au_do_lookup_args args = {
6487 +               .flags          = 0,
6488 +               .type           = type
6489 +       };
6490 +       const struct qstr *name = &dentry->d_name;
6491 +       struct dentry *parent;
6492 +       struct inode *inode;
6493 +
6494 +       err = au_test_shwh(dentry->d_sb, name);
6495 +       if (unlikely(err))
6496 +               goto out;
6497 +
6498 +       err = au_wh_name_alloc(&whname, name);
6499 +       if (unlikely(err))
6500 +               goto out;
6501 +
6502 +       inode = dentry->d_inode;
6503 +       isdir = !!(inode && S_ISDIR(inode->i_mode));
6504 +       if (!type)
6505 +               au_fset_lkup(args.flags, ALLOW_NEG);
6506 +
6507 +       npositive = 0;
6508 +       parent = dget_parent(dentry);
6509 +       btail = au_dbtaildir(parent);
6510 +       for (bindex = bstart; bindex <= btail; bindex++) {
6511 +               struct dentry *h_parent, *h_dentry;
6512 +               struct inode *h_inode, *h_dir;
6513 +
6514 +               h_dentry = au_h_dptr(dentry, bindex);
6515 +               if (h_dentry) {
6516 +                       if (h_dentry->d_inode)
6517 +                               npositive++;
6518 +                       if (type != S_IFDIR)
6519 +                               break;
6520 +                       continue;
6521 +               }
6522 +               h_parent = au_h_dptr(parent, bindex);
6523 +               if (!h_parent)
6524 +                       continue;
6525 +               h_dir = h_parent->d_inode;
6526 +               if (!h_dir || !S_ISDIR(h_dir->i_mode))
6527 +                       continue;
6528 +
6529 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
6530 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
6531 +                                       &args);
6532 +               mutex_unlock(&h_dir->i_mutex);
6533 +               err = PTR_ERR(h_dentry);
6534 +               if (IS_ERR(h_dentry))
6535 +                       goto out_parent;
6536 +               au_fclr_lkup(args.flags, ALLOW_NEG);
6537 +
6538 +               if (au_dbwh(dentry) >= 0)
6539 +                       break;
6540 +               if (!h_dentry)
6541 +                       continue;
6542 +               h_inode = h_dentry->d_inode;
6543 +               if (!h_inode)
6544 +                       continue;
6545 +               npositive++;
6546 +               if (!args.type)
6547 +                       args.type = h_inode->i_mode & S_IFMT;
6548 +               if (args.type != S_IFDIR)
6549 +                       break;
6550 +               else if (isdir) {
6551 +                       /* the type of lower may be different */
6552 +                       bdiropq = au_dbdiropq(dentry);
6553 +                       if (bdiropq >= 0 && bdiropq <= bindex)
6554 +                               break;
6555 +               }
6556 +       }
6557 +
6558 +       if (npositive) {
6559 +               AuLabel(positive);
6560 +               au_update_dbstart(dentry);
6561 +       }
6562 +       err = npositive;
6563 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
6564 +                    && au_dbstart(dentry) < 0)) {
6565 +               err = -EIO;
6566 +               AuIOErr("both of real entry and whiteout found, %.*s, err %d\n",
6567 +                       AuDLNPair(dentry), err);
6568 +       }
6569 +
6570 +out_parent:
6571 +       dput(parent);
6572 +       kfree(whname.name);
6573 +out:
6574 +       return err;
6575 +}
6576 +
6577 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
6578 +                              struct au_branch *br)
6579 +{
6580 +       struct dentry *dentry;
6581 +       int wkq_err;
6582 +
6583 +       if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
6584 +               dentry = vfsub_lkup_one(name, parent);
6585 +       else {
6586 +               struct vfsub_lkup_one_args args = {
6587 +                       .errp   = &dentry,
6588 +                       .name   = name,
6589 +                       .parent = parent
6590 +               };
6591 +
6592 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
6593 +               if (unlikely(wkq_err))
6594 +                       dentry = ERR_PTR(wkq_err);
6595 +       }
6596 +
6597 +       return dentry;
6598 +}
6599 +
6600 +/*
6601 + * lookup @dentry on @bindex which should be negative.
6602 + */
6603 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
6604 +{
6605 +       int err;
6606 +       struct dentry *parent, *h_parent, *h_dentry;
6607 +       struct au_branch *br;
6608 +
6609 +       parent = dget_parent(dentry);
6610 +       h_parent = au_h_dptr(parent, bindex);
6611 +       br = au_sbr(dentry->d_sb, bindex);
6612 +       if (wh)
6613 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6614 +       else
6615 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent, br);
6616 +       err = PTR_ERR(h_dentry);
6617 +       if (IS_ERR(h_dentry))
6618 +               goto out;
6619 +       if (unlikely(h_dentry->d_inode)) {
6620 +               err = -EIO;
6621 +               AuIOErr("%.*s should be negative on b%d.\n",
6622 +                       AuDLNPair(h_dentry), bindex);
6623 +               dput(h_dentry);
6624 +               goto out;
6625 +       }
6626 +
6627 +       err = 0;
6628 +       if (bindex < au_dbstart(dentry))
6629 +               au_set_dbstart(dentry, bindex);
6630 +       if (au_dbend(dentry) < bindex)
6631 +               au_set_dbend(dentry, bindex);
6632 +       au_set_h_dptr(dentry, bindex, h_dentry);
6633 +
6634 +out:
6635 +       dput(parent);
6636 +       return err;
6637 +}
6638 +
6639 +/* ---------------------------------------------------------------------- */
6640 +
6641 +/* subset of struct inode */
6642 +struct au_iattr {
6643 +       unsigned long           i_ino;
6644 +       /* unsigned int         i_nlink; */
6645 +       kuid_t                  i_uid;
6646 +       kgid_t                  i_gid;
6647 +       u64                     i_version;
6648 +/*
6649 +       loff_t                  i_size;
6650 +       blkcnt_t                i_blocks;
6651 +*/
6652 +       umode_t                 i_mode;
6653 +};
6654 +
6655 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
6656 +{
6657 +       ia->i_ino = h_inode->i_ino;
6658 +       /* ia->i_nlink = h_inode->i_nlink; */
6659 +       ia->i_uid = h_inode->i_uid;
6660 +       ia->i_gid = h_inode->i_gid;
6661 +       ia->i_version = h_inode->i_version;
6662 +/*
6663 +       ia->i_size = h_inode->i_size;
6664 +       ia->i_blocks = h_inode->i_blocks;
6665 +*/
6666 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
6667 +}
6668 +
6669 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
6670 +{
6671 +       return ia->i_ino != h_inode->i_ino
6672 +               /* || ia->i_nlink != h_inode->i_nlink */
6673 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
6674 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
6675 +               || ia->i_version != h_inode->i_version
6676 +/*
6677 +               || ia->i_size != h_inode->i_size
6678 +               || ia->i_blocks != h_inode->i_blocks
6679 +*/
6680 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
6681 +}
6682 +
6683 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
6684 +                             struct au_branch *br)
6685 +{
6686 +       int err;
6687 +       struct au_iattr ia;
6688 +       struct inode *h_inode;
6689 +       struct dentry *h_d;
6690 +       struct super_block *h_sb;
6691 +
6692 +       err = 0;
6693 +       memset(&ia, -1, sizeof(ia));
6694 +       h_sb = h_dentry->d_sb;
6695 +       h_inode = h_dentry->d_inode;
6696 +       if (h_inode)
6697 +               au_iattr_save(&ia, h_inode);
6698 +       else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
6699 +               /* nfs d_revalidate may return 0 for negative dentry */
6700 +               /* fuse d_revalidate always return 0 for negative dentry */
6701 +               goto out;
6702 +
6703 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
6704 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
6705 +       err = PTR_ERR(h_d);
6706 +       if (IS_ERR(h_d))
6707 +               goto out;
6708 +
6709 +       err = 0;
6710 +       if (unlikely(h_d != h_dentry
6711 +                    || h_d->d_inode != h_inode
6712 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
6713 +               err = au_busy_or_stale();
6714 +       dput(h_d);
6715 +
6716 +out:
6717 +       AuTraceErr(err);
6718 +       return err;
6719 +}
6720 +
6721 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
6722 +               struct dentry *h_parent, struct au_branch *br)
6723 +{
6724 +       int err;
6725 +
6726 +       err = 0;
6727 +       if (udba == AuOpt_UDBA_REVAL
6728 +           && !au_test_fs_remote(h_dentry->d_sb)) {
6729 +               IMustLock(h_dir);
6730 +               err = (h_dentry->d_parent->d_inode != h_dir);
6731 +       } else if (udba != AuOpt_UDBA_NONE)
6732 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
6733 +
6734 +       return err;
6735 +}
6736 +
6737 +/* ---------------------------------------------------------------------- */
6738 +
6739 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
6740 +{
6741 +       int err;
6742 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
6743 +       struct au_hdentry tmp, *p, *q;
6744 +       struct au_dinfo *dinfo;
6745 +       struct super_block *sb;
6746 +
6747 +       DiMustWriteLock(dentry);
6748 +
6749 +       sb = dentry->d_sb;
6750 +       dinfo = au_di(dentry);
6751 +       bend = dinfo->di_bend;
6752 +       bwh = dinfo->di_bwh;
6753 +       bdiropq = dinfo->di_bdiropq;
6754 +       p = dinfo->di_hdentry + dinfo->di_bstart;
6755 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
6756 +               if (!p->hd_dentry)
6757 +                       continue;
6758 +
6759 +               new_bindex = au_br_index(sb, p->hd_id);
6760 +               if (new_bindex == bindex)
6761 +                       continue;
6762 +
6763 +               if (dinfo->di_bwh == bindex)
6764 +                       bwh = new_bindex;
6765 +               if (dinfo->di_bdiropq == bindex)
6766 +                       bdiropq = new_bindex;
6767 +               if (new_bindex < 0) {
6768 +                       au_hdput(p);
6769 +                       p->hd_dentry = NULL;
6770 +                       continue;
6771 +               }
6772 +
6773 +               /* swap two lower dentries, and loop again */
6774 +               q = dinfo->di_hdentry + new_bindex;
6775 +               tmp = *q;
6776 +               *q = *p;
6777 +               *p = tmp;
6778 +               if (tmp.hd_dentry) {
6779 +                       bindex--;
6780 +                       p--;
6781 +               }
6782 +       }
6783 +
6784 +       dinfo->di_bwh = -1;
6785 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
6786 +               dinfo->di_bwh = bwh;
6787 +
6788 +       dinfo->di_bdiropq = -1;
6789 +       if (bdiropq >= 0
6790 +           && bdiropq <= au_sbend(sb)
6791 +           && au_sbr_whable(sb, bdiropq))
6792 +               dinfo->di_bdiropq = bdiropq;
6793 +
6794 +       err = -EIO;
6795 +       dinfo->di_bstart = -1;
6796 +       dinfo->di_bend = -1;
6797 +       bend = au_dbend(parent);
6798 +       p = dinfo->di_hdentry;
6799 +       for (bindex = 0; bindex <= bend; bindex++, p++)
6800 +               if (p->hd_dentry) {
6801 +                       dinfo->di_bstart = bindex;
6802 +                       break;
6803 +               }
6804 +
6805 +       if (dinfo->di_bstart >= 0) {
6806 +               p = dinfo->di_hdentry + bend;
6807 +               for (bindex = bend; bindex >= 0; bindex--, p--)
6808 +                       if (p->hd_dentry) {
6809 +                               dinfo->di_bend = bindex;
6810 +                               err = 0;
6811 +                               break;
6812 +                       }
6813 +       }
6814 +
6815 +       return err;
6816 +}
6817 +
6818 +static void au_do_hide(struct dentry *dentry)
6819 +{
6820 +       struct inode *inode;
6821 +
6822 +       inode = dentry->d_inode;
6823 +       if (inode) {
6824 +               if (!S_ISDIR(inode->i_mode)) {
6825 +                       if (inode->i_nlink && !d_unhashed(dentry))
6826 +                               drop_nlink(inode);
6827 +               } else {
6828 +                       clear_nlink(inode);
6829 +                       /* stop next lookup */
6830 +                       inode->i_flags |= S_DEAD;
6831 +               }
6832 +               smp_mb(); /* necessary? */
6833 +       }
6834 +       d_drop(dentry);
6835 +}
6836 +
6837 +static int au_hide_children(struct dentry *parent)
6838 +{
6839 +       int err, i, j, ndentry;
6840 +       struct au_dcsub_pages dpages;
6841 +       struct au_dpage *dpage;
6842 +       struct dentry *dentry;
6843 +
6844 +       err = au_dpages_init(&dpages, GFP_NOFS);
6845 +       if (unlikely(err))
6846 +               goto out;
6847 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
6848 +       if (unlikely(err))
6849 +               goto out_dpages;
6850 +
6851 +       /* in reverse order */
6852 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
6853 +               dpage = dpages.dpages + i;
6854 +               ndentry = dpage->ndentry;
6855 +               for (j = ndentry - 1; j >= 0; j--) {
6856 +                       dentry = dpage->dentries[j];
6857 +                       if (dentry != parent)
6858 +                               au_do_hide(dentry);
6859 +               }
6860 +       }
6861 +
6862 +out_dpages:
6863 +       au_dpages_free(&dpages);
6864 +out:
6865 +       return err;
6866 +}
6867 +
6868 +static void au_hide(struct dentry *dentry)
6869 +{
6870 +       int err;
6871 +       struct inode *inode;
6872 +
6873 +       AuDbgDentry(dentry);
6874 +       inode = dentry->d_inode;
6875 +       if (inode && S_ISDIR(inode->i_mode)) {
6876 +               /* shrink_dcache_parent(dentry); */
6877 +               err = au_hide_children(dentry);
6878 +               if (unlikely(err))
6879 +                       AuIOErr("%.*s, failed hiding children, ignored %d\n",
6880 +                               AuDLNPair(dentry), err);
6881 +       }
6882 +       au_do_hide(dentry);
6883 +}
6884 +
6885 +/*
6886 + * By adding a dirty branch, a cached dentry may be affected in various ways.
6887 + *
6888 + * a dirty branch is added
6889 + * - on the top of layers
6890 + * - in the middle of layers
6891 + * - to the bottom of layers
6892 + *
6893 + * on the added branch there exists
6894 + * - a whiteout
6895 + * - a diropq
6896 + * - a same named entry
6897 + *   + exist
6898 + *     * negative --> positive
6899 + *     * positive --> positive
6900 + *      - type is unchanged
6901 + *      - type is changed
6902 + *   + doesn't exist
6903 + *     * negative --> negative
6904 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
6905 + * - none
6906 + */
6907 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
6908 +                              struct au_dinfo *tmp)
6909 +{
6910 +       int err;
6911 +       aufs_bindex_t bindex, bend;
6912 +       struct {
6913 +               struct dentry *dentry;
6914 +               struct inode *inode;
6915 +               mode_t mode;
6916 +       } orig_h, tmp_h;
6917 +       struct au_hdentry *hd;
6918 +       struct inode *inode, *h_inode;
6919 +       struct dentry *h_dentry;
6920 +
6921 +       err = 0;
6922 +       AuDebugOn(dinfo->di_bstart < 0);
6923 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
6924 +       orig_h.inode = orig_h.dentry->d_inode;
6925 +       orig_h.mode = 0;
6926 +       if (orig_h.inode)
6927 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
6928 +       memset(&tmp_h, 0, sizeof(tmp_h));
6929 +       if (tmp->di_bstart >= 0) {
6930 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
6931 +               tmp_h.inode = tmp_h.dentry->d_inode;
6932 +               if (tmp_h.inode)
6933 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
6934 +       }
6935 +
6936 +       inode = dentry->d_inode;
6937 +       if (!orig_h.inode) {
6938 +               AuDbg("nagative originally\n");
6939 +               if (inode) {
6940 +                       au_hide(dentry);
6941 +                       goto out;
6942 +               }
6943 +               AuDebugOn(inode);
6944 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
6945 +               AuDebugOn(dinfo->di_bdiropq != -1);
6946 +
6947 +               if (!tmp_h.inode) {
6948 +                       AuDbg("negative --> negative\n");
6949 +                       /* should have only one negative lower */
6950 +                       if (tmp->di_bstart >= 0
6951 +                           && tmp->di_bstart < dinfo->di_bstart) {
6952 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
6953 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
6954 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
6955 +                               au_di_cp(dinfo, tmp);
6956 +                               hd = tmp->di_hdentry + tmp->di_bstart;
6957 +                               au_set_h_dptr(dentry, tmp->di_bstart,
6958 +                                             dget(hd->hd_dentry));
6959 +                       }
6960 +                       au_dbg_verify_dinode(dentry);
6961 +               } else {
6962 +                       AuDbg("negative --> positive\n");
6963 +                       /*
6964 +                        * similar to the behaviour of creating with bypassing
6965 +                        * aufs.
6966 +                        * unhash it in order to force an error in the
6967 +                        * succeeding create operation.
6968 +                        * we should not set S_DEAD here.
6969 +                        */
6970 +                       d_drop(dentry);
6971 +                       /* au_di_swap(tmp, dinfo); */
6972 +                       au_dbg_verify_dinode(dentry);
6973 +               }
6974 +       } else {
6975 +               AuDbg("positive originally\n");
6976 +               /* inode may be NULL */
6977 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
6978 +               if (!tmp_h.inode) {
6979 +                       AuDbg("positive --> negative\n");
6980 +                       /* or bypassing aufs */
6981 +                       au_hide(dentry);
6982 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
6983 +                               dinfo->di_bwh = tmp->di_bwh;
6984 +                       if (inode)
6985 +                               err = au_refresh_hinode_self(inode);
6986 +                       au_dbg_verify_dinode(dentry);
6987 +               } else if (orig_h.mode == tmp_h.mode) {
6988 +                       AuDbg("positive --> positive, same type\n");
6989 +                       if (!S_ISDIR(orig_h.mode)
6990 +                           && dinfo->di_bstart > tmp->di_bstart) {
6991 +                               /*
6992 +                                * similar to the behaviour of removing and
6993 +                                * creating.
6994 +                                */
6995 +                               au_hide(dentry);
6996 +                               if (inode)
6997 +                                       err = au_refresh_hinode_self(inode);
6998 +                               au_dbg_verify_dinode(dentry);
6999 +                       } else {
7000 +                               /* fill empty slots */
7001 +                               if (dinfo->di_bstart > tmp->di_bstart)
7002 +                                       dinfo->di_bstart = tmp->di_bstart;
7003 +                               if (dinfo->di_bend < tmp->di_bend)
7004 +                                       dinfo->di_bend = tmp->di_bend;
7005 +                               dinfo->di_bwh = tmp->di_bwh;
7006 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
7007 +                               hd = tmp->di_hdentry;
7008 +                               bend = dinfo->di_bend;
7009 +                               for (bindex = tmp->di_bstart; bindex <= bend;
7010 +                                    bindex++) {
7011 +                                       if (au_h_dptr(dentry, bindex))
7012 +                                               continue;
7013 +                                       h_dentry = hd[bindex].hd_dentry;
7014 +                                       if (!h_dentry)
7015 +                                               continue;
7016 +                                       h_inode = h_dentry->d_inode;
7017 +                                       AuDebugOn(!h_inode);
7018 +                                       AuDebugOn(orig_h.mode
7019 +                                                 != (h_inode->i_mode
7020 +                                                     & S_IFMT));
7021 +                                       au_set_h_dptr(dentry, bindex,
7022 +                                                     dget(h_dentry));
7023 +                               }
7024 +                               err = au_refresh_hinode(inode, dentry);
7025 +                               au_dbg_verify_dinode(dentry);
7026 +                       }
7027 +               } else {
7028 +                       AuDbg("positive --> positive, different type\n");
7029 +                       /* similar to the behaviour of removing and creating */
7030 +                       au_hide(dentry);
7031 +                       if (inode)
7032 +                               err = au_refresh_hinode_self(inode);
7033 +                       au_dbg_verify_dinode(dentry);
7034 +               }
7035 +       }
7036 +
7037 +out:
7038 +       return err;
7039 +}
7040 +
7041 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
7042 +{
7043 +       int err, ebrange;
7044 +       unsigned int sigen;
7045 +       struct au_dinfo *dinfo, *tmp;
7046 +       struct super_block *sb;
7047 +       struct inode *inode;
7048 +
7049 +       DiMustWriteLock(dentry);
7050 +       AuDebugOn(IS_ROOT(dentry));
7051 +       AuDebugOn(!parent->d_inode);
7052 +
7053 +       sb = dentry->d_sb;
7054 +       inode = dentry->d_inode;
7055 +       sigen = au_sigen(sb);
7056 +       err = au_digen_test(parent, sigen);
7057 +       if (unlikely(err))
7058 +               goto out;
7059 +
7060 +       dinfo = au_di(dentry);
7061 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
7062 +       if (unlikely(err))
7063 +               goto out;
7064 +       ebrange = au_dbrange_test(dentry);
7065 +       if (!ebrange)
7066 +               ebrange = au_do_refresh_hdentry(dentry, parent);
7067 +
7068 +       if (d_unhashed(dentry) || ebrange) {
7069 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
7070 +               if (inode)
7071 +                       err = au_refresh_hinode_self(inode);
7072 +               au_dbg_verify_dinode(dentry);
7073 +               if (!err)
7074 +                       goto out_dgen; /* success */
7075 +               goto out;
7076 +       }
7077 +
7078 +       /* temporary dinfo */
7079 +       AuDbgDentry(dentry);
7080 +       err = -ENOMEM;
7081 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
7082 +       if (unlikely(!tmp))
7083 +               goto out;
7084 +       au_di_swap(tmp, dinfo);
7085 +       /* returns the number of positive dentries */
7086 +       /*
7087 +        * if current working dir is removed, it returns an error.
7088 +        * but the dentry is legal.
7089 +        */
7090 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
7091 +       AuDbgDentry(dentry);
7092 +       au_di_swap(tmp, dinfo);
7093 +       if (err == -ENOENT)
7094 +               err = 0;
7095 +       if (err >= 0) {
7096 +               /* compare/refresh by dinfo */
7097 +               AuDbgDentry(dentry);
7098 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
7099 +               au_dbg_verify_dinode(dentry);
7100 +               AuTraceErr(err);
7101 +       }
7102 +       au_rw_write_unlock(&tmp->di_rwsem);
7103 +       au_di_free(tmp);
7104 +       if (unlikely(err))
7105 +               goto out;
7106 +
7107 +out_dgen:
7108 +       au_update_digen(dentry);
7109 +out:
7110 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
7111 +               AuIOErr("failed refreshing %.*s, %d\n",
7112 +                       AuDLNPair(dentry), err);
7113 +               AuDbgDentry(dentry);
7114 +       }
7115 +       AuTraceErr(err);
7116 +       return err;
7117 +}
7118 +
7119 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
7120 +                          struct dentry *dentry, aufs_bindex_t bindex)
7121 +{
7122 +       int err, valid;
7123 +
7124 +       err = 0;
7125 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
7126 +               goto out;
7127 +
7128 +       AuDbg("b%d\n", bindex);
7129 +       /*
7130 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
7131 +        * due to whiteout and branch permission.
7132 +        */
7133 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
7134 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
7135 +       /* it may return tri-state */
7136 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
7137 +
7138 +       if (unlikely(valid < 0))
7139 +               err = valid;
7140 +       else if (!valid)
7141 +               err = -EINVAL;
7142 +
7143 +out:
7144 +       AuTraceErr(err);
7145 +       return err;
7146 +}
7147 +
7148 +/* todo: remove this */
7149 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
7150 +                         unsigned int flags, int do_udba)
7151 +{
7152 +       int err;
7153 +       umode_t mode, h_mode;
7154 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
7155 +       unsigned char plus, unhashed, is_root, h_plus;
7156 +       struct inode *h_inode, *h_cached_inode;
7157 +       struct dentry *h_dentry;
7158 +       struct qstr *name, *h_name;
7159 +
7160 +       err = 0;
7161 +       plus = 0;
7162 +       mode = 0;
7163 +       ibs = -1;
7164 +       ibe = -1;
7165 +       unhashed = !!d_unhashed(dentry);
7166 +       is_root = !!IS_ROOT(dentry);
7167 +       name = &dentry->d_name;
7168 +
7169 +       /*
7170 +        * Theoretically, REVAL test should be unnecessary in case of
7171 +        * {FS,I}NOTIFY.
7172 +        * But {fs,i}notify doesn't fire some necessary events,
7173 +        *      IN_ATTRIB for atime/nlink/pageio
7174 +        *      IN_DELETE for NFS dentry
7175 +        * Let's do REVAL test too.
7176 +        */
7177 +       if (do_udba && inode) {
7178 +               mode = (inode->i_mode & S_IFMT);
7179 +               plus = (inode->i_nlink > 0);
7180 +               ibs = au_ibstart(inode);
7181 +               ibe = au_ibend(inode);
7182 +       }
7183 +
7184 +       bstart = au_dbstart(dentry);
7185 +       btail = bstart;
7186 +       if (inode && S_ISDIR(inode->i_mode))
7187 +               btail = au_dbtaildir(dentry);
7188 +       for (bindex = bstart; bindex <= btail; bindex++) {
7189 +               h_dentry = au_h_dptr(dentry, bindex);
7190 +               if (!h_dentry)
7191 +                       continue;
7192 +
7193 +               AuDbg("b%d, %.*s\n", bindex, AuDLNPair(h_dentry));
7194 +               spin_lock(&h_dentry->d_lock);
7195 +               h_name = &h_dentry->d_name;
7196 +               if (unlikely(do_udba
7197 +                            && !is_root
7198 +                            && (unhashed != !!d_unhashed(h_dentry)
7199 +                                || name->len != h_name->len
7200 +                                || memcmp(name->name, h_name->name, name->len))
7201 +                           )) {
7202 +                       AuDbg("unhash 0x%x 0x%x, %.*s %.*s\n",
7203 +                                 unhashed, d_unhashed(h_dentry),
7204 +                                 AuDLNPair(dentry), AuDLNPair(h_dentry));
7205 +                       spin_unlock(&h_dentry->d_lock);
7206 +                       goto err;
7207 +               }
7208 +               spin_unlock(&h_dentry->d_lock);
7209 +
7210 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
7211 +               if (unlikely(err))
7212 +                       /* do not goto err, to keep the errno */
7213 +                       break;
7214 +
7215 +               /* todo: plink too? */
7216 +               if (!do_udba)
7217 +                       continue;
7218 +
7219 +               /* UDBA tests */
7220 +               h_inode = h_dentry->d_inode;
7221 +               if (unlikely(!!inode != !!h_inode))
7222 +                       goto err;
7223 +
7224 +               h_plus = plus;
7225 +               h_mode = mode;
7226 +               h_cached_inode = h_inode;
7227 +               if (h_inode) {
7228 +                       h_mode = (h_inode->i_mode & S_IFMT);
7229 +                       h_plus = (h_inode->i_nlink > 0);
7230 +               }
7231 +               if (inode && ibs <= bindex && bindex <= ibe)
7232 +                       h_cached_inode = au_h_iptr(inode, bindex);
7233 +
7234 +               if (unlikely(plus != h_plus
7235 +                            || mode != h_mode
7236 +                            || h_cached_inode != h_inode))
7237 +                       goto err;
7238 +               continue;
7239 +
7240 +       err:
7241 +               err = -EINVAL;
7242 +               break;
7243 +       }
7244 +
7245 +       return err;
7246 +}
7247 +
7248 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
7249 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
7250 +{
7251 +       int err;
7252 +       struct dentry *parent;
7253 +
7254 +       if (!au_digen_test(dentry, sigen))
7255 +               return 0;
7256 +
7257 +       parent = dget_parent(dentry);
7258 +       di_read_lock_parent(parent, AuLock_IR);
7259 +       AuDebugOn(au_digen_test(parent, sigen));
7260 +       au_dbg_verify_gen(parent, sigen);
7261 +       err = au_refresh_dentry(dentry, parent);
7262 +       di_read_unlock(parent, AuLock_IR);
7263 +       dput(parent);
7264 +       AuTraceErr(err);
7265 +       return err;
7266 +}
7267 +
7268 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
7269 +{
7270 +       int err;
7271 +       struct dentry *d, *parent;
7272 +       struct inode *inode;
7273 +
7274 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
7275 +               return simple_reval_dpath(dentry, sigen);
7276 +
7277 +       /* slow loop, keep it simple and stupid */
7278 +       /* cf: au_cpup_dirs() */
7279 +       err = 0;
7280 +       parent = NULL;
7281 +       while (au_digen_test(dentry, sigen)) {
7282 +               d = dentry;
7283 +               while (1) {
7284 +                       dput(parent);
7285 +                       parent = dget_parent(d);
7286 +                       if (!au_digen_test(parent, sigen))
7287 +                               break;
7288 +                       d = parent;
7289 +               }
7290 +
7291 +               inode = d->d_inode;
7292 +               if (d != dentry)
7293 +                       di_write_lock_child2(d);
7294 +
7295 +               /* someone might update our dentry while we were sleeping */
7296 +               if (au_digen_test(d, sigen)) {
7297 +                       /*
7298 +                        * todo: consolidate with simple_reval_dpath(),
7299 +                        * do_refresh() and au_reval_for_attr().
7300 +                        */
7301 +                       di_read_lock_parent(parent, AuLock_IR);
7302 +                       err = au_refresh_dentry(d, parent);
7303 +                       di_read_unlock(parent, AuLock_IR);
7304 +               }
7305 +
7306 +               if (d != dentry)
7307 +                       di_write_unlock(d);
7308 +               dput(parent);
7309 +               if (unlikely(err))
7310 +                       break;
7311 +       }
7312 +
7313 +       return err;
7314 +}
7315 +
7316 +/*
7317 + * if valid returns 1, otherwise 0.
7318 + */
7319 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
7320 +{
7321 +       int valid, err;
7322 +       unsigned int sigen;
7323 +       unsigned char do_udba;
7324 +       struct super_block *sb;
7325 +       struct inode *inode;
7326 +
7327 +       /* todo: support rcu-walk? */
7328 +       if (flags & LOOKUP_RCU)
7329 +               return -ECHILD;
7330 +
7331 +       valid = 0;
7332 +       if (unlikely(!au_di(dentry)))
7333 +               goto out;
7334 +
7335 +       inode = dentry->d_inode;
7336 +       if (inode && is_bad_inode(inode))
7337 +               goto out;
7338 +
7339 +       valid = 1;
7340 +       sb = dentry->d_sb;
7341 +       /*
7342 +        * todo: very ugly
7343 +        * i_mutex of parent dir may be held,
7344 +        * but we should not return 'invalid' due to busy.
7345 +        */
7346 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
7347 +       if (unlikely(err)) {
7348 +               valid = err;
7349 +               AuTraceErr(err);
7350 +               goto out;
7351 +       }
7352 +       if (unlikely(au_dbrange_test(dentry))) {
7353 +               err = -EINVAL;
7354 +               AuTraceErr(err);
7355 +               goto out_dgrade;
7356 +       }
7357 +
7358 +       sigen = au_sigen(sb);
7359 +       if (au_digen_test(dentry, sigen)) {
7360 +               AuDebugOn(IS_ROOT(dentry));
7361 +               err = au_reval_dpath(dentry, sigen);
7362 +               if (unlikely(err)) {
7363 +                       AuTraceErr(err);
7364 +                       goto out_dgrade;
7365 +               }
7366 +       }
7367 +       di_downgrade_lock(dentry, AuLock_IR);
7368 +
7369 +       err = -EINVAL;
7370 +       if (inode && (IS_DEADDIR(inode) || !inode->i_nlink))
7371 +               goto out_inval;
7372 +
7373 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
7374 +       if (do_udba && inode) {
7375 +               aufs_bindex_t bstart = au_ibstart(inode);
7376 +               struct inode *h_inode;
7377 +
7378 +               if (bstart >= 0) {
7379 +                       h_inode = au_h_iptr(inode, bstart);
7380 +                       if (h_inode && au_test_higen(inode, h_inode))
7381 +                               goto out_inval;
7382 +               }
7383 +       }
7384 +
7385 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
7386 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
7387 +               err = -EIO;
7388 +               AuDbg("both of real entry and whiteout found, %.*s, err %d\n",
7389 +                     AuDLNPair(dentry), err);
7390 +       }
7391 +       goto out_inval;
7392 +
7393 +out_dgrade:
7394 +       di_downgrade_lock(dentry, AuLock_IR);
7395 +out_inval:
7396 +       aufs_read_unlock(dentry, AuLock_IR);
7397 +       AuTraceErr(err);
7398 +       valid = !err;
7399 +out:
7400 +       if (!valid) {
7401 +               AuDbg("%.*s invalid, %d\n", AuDLNPair(dentry), valid);
7402 +               d_drop(dentry);
7403 +       }
7404 +       return valid;
7405 +}
7406 +
7407 +static void aufs_d_release(struct dentry *dentry)
7408 +{
7409 +       if (au_di(dentry)) {
7410 +               au_di_fin(dentry);
7411 +               au_hn_di_reinit(dentry);
7412 +       }
7413 +}
7414 +
7415 +const struct dentry_operations aufs_dop = {
7416 +       .d_revalidate           = aufs_d_revalidate,
7417 +       .d_weak_revalidate      = aufs_d_revalidate,
7418 +       .d_release              = aufs_d_release
7419 +};
7420 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
7421 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
7422 +++ linux/fs/aufs/dentry.h      2013-07-06 13:20:47.740198107 +0200
7423 @@ -0,0 +1,234 @@
7424 +/*
7425 + * Copyright (C) 2005-2013 Junjiro R. Okajima
7426 + *
7427 + * This program, aufs is free software; you can redistribute it and/or modify
7428 + * it under the terms of the GNU General Public License as published by
7429 + * the Free Software Foundation; either version 2 of the License, or
7430 + * (at your option) any later version.
7431 + *
7432 + * This program is distributed in the hope that it will be useful,
7433 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7434 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7435 + * GNU General Public License for more details.
7436 + *
7437 + * You should have received a copy of the GNU General Public License
7438 + * along with this program; if not, write to the Free Software
7439 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7440 + */
7441 +
7442 +/*
7443 + * lookup and dentry operations
7444 + */
7445 +
7446 +#ifndef __AUFS_DENTRY_H__
7447 +#define __AUFS_DENTRY_H__
7448 +
7449 +#ifdef __KERNEL__
7450 +
7451 +#include <linux/dcache.h>
7452 +#include "rwsem.h"
7453 +
7454 +struct au_hdentry {
7455 +       struct dentry           *hd_dentry;
7456 +       aufs_bindex_t           hd_id;
7457 +};
7458 +
7459 +struct au_dinfo {
7460 +       atomic_t                di_generation;
7461 +
7462 +       struct au_rwsem         di_rwsem;
7463 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
7464 +       struct au_hdentry       *di_hdentry;
7465 +} ____cacheline_aligned_in_smp;
7466 +
7467 +/* ---------------------------------------------------------------------- */
7468 +
7469 +/* dentry.c */
7470 +extern const struct dentry_operations aufs_dop;
7471 +struct au_branch;
7472 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
7473 +                              struct au_branch *br);
7474 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7475 +               struct dentry *h_parent, struct au_branch *br);
7476 +
7477 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
7478 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
7479 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
7480 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
7481 +
7482 +/* dinfo.c */
7483 +void au_di_init_once(void *_di);
7484 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
7485 +void au_di_free(struct au_dinfo *dinfo);
7486 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
7487 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
7488 +int au_di_init(struct dentry *dentry);
7489 +void au_di_fin(struct dentry *dentry);
7490 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
7491 +
7492 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
7493 +void di_read_unlock(struct dentry *d, int flags);
7494 +void di_downgrade_lock(struct dentry *d, int flags);
7495 +void di_write_lock(struct dentry *d, unsigned int lsc);
7496 +void di_write_unlock(struct dentry *d);
7497 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
7498 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
7499 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
7500 +
7501 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
7502 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
7503 +aufs_bindex_t au_dbtail(struct dentry *dentry);
7504 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
7505 +
7506 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
7507 +                  struct dentry *h_dentry);
7508 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
7509 +int au_dbrange_test(struct dentry *dentry);
7510 +void au_update_digen(struct dentry *dentry);
7511 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
7512 +void au_update_dbstart(struct dentry *dentry);
7513 +void au_update_dbend(struct dentry *dentry);
7514 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
7515 +
7516 +/* ---------------------------------------------------------------------- */
7517 +
7518 +static inline struct au_dinfo *au_di(struct dentry *dentry)
7519 +{
7520 +       return dentry->d_fsdata;
7521 +}
7522 +
7523 +/* ---------------------------------------------------------------------- */
7524 +
7525 +/* lock subclass for dinfo */
7526 +enum {
7527 +       AuLsc_DI_CHILD,         /* child first */
7528 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
7529 +       AuLsc_DI_CHILD3,        /* copyup dirs */
7530 +       AuLsc_DI_PARENT,
7531 +       AuLsc_DI_PARENT2,
7532 +       AuLsc_DI_PARENT3,
7533 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
7534 +};
7535 +
7536 +/*
7537 + * di_read_lock_child, di_write_lock_child,
7538 + * di_read_lock_child2, di_write_lock_child2,
7539 + * di_read_lock_child3, di_write_lock_child3,
7540 + * di_read_lock_parent, di_write_lock_parent,
7541 + * di_read_lock_parent2, di_write_lock_parent2,
7542 + * di_read_lock_parent3, di_write_lock_parent3,
7543 + */
7544 +#define AuReadLockFunc(name, lsc) \
7545 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
7546 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
7547 +
7548 +#define AuWriteLockFunc(name, lsc) \
7549 +static inline void di_write_lock_##name(struct dentry *d) \
7550 +{ di_write_lock(d, AuLsc_DI_##lsc); }
7551 +
7552 +#define AuRWLockFuncs(name, lsc) \
7553 +       AuReadLockFunc(name, lsc) \
7554 +       AuWriteLockFunc(name, lsc)
7555 +
7556 +AuRWLockFuncs(child, CHILD);
7557 +AuRWLockFuncs(child2, CHILD2);
7558 +AuRWLockFuncs(child3, CHILD3);
7559 +AuRWLockFuncs(parent, PARENT);
7560 +AuRWLockFuncs(parent2, PARENT2);
7561 +AuRWLockFuncs(parent3, PARENT3);
7562 +
7563 +#undef AuReadLockFunc
7564 +#undef AuWriteLockFunc
7565 +#undef AuRWLockFuncs
7566 +
7567 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
7568 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
7569 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
7570 +
7571 +/* ---------------------------------------------------------------------- */
7572 +
7573 +/* todo: memory barrier? */
7574 +static inline unsigned int au_digen(struct dentry *d)
7575 +{
7576 +       return atomic_read(&au_di(d)->di_generation);
7577 +}
7578 +
7579 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
7580 +{
7581 +       hdentry->hd_dentry = NULL;
7582 +}
7583 +
7584 +static inline void au_hdput(struct au_hdentry *hd)
7585 +{
7586 +       if (hd)
7587 +               dput(hd->hd_dentry);
7588 +}
7589 +
7590 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
7591 +{
7592 +       DiMustAnyLock(dentry);
7593 +       return au_di(dentry)->di_bstart;
7594 +}
7595 +
7596 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
7597 +{
7598 +       DiMustAnyLock(dentry);
7599 +       return au_di(dentry)->di_bend;
7600 +}
7601 +
7602 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
7603 +{
7604 +       DiMustAnyLock(dentry);
7605 +       return au_di(dentry)->di_bwh;
7606 +}
7607 +
7608 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
7609 +{
7610 +       DiMustAnyLock(dentry);
7611 +       return au_di(dentry)->di_bdiropq;
7612 +}
7613 +
7614 +/* todo: hard/soft set? */
7615 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
7616 +{
7617 +       DiMustWriteLock(dentry);
7618 +       au_di(dentry)->di_bstart = bindex;
7619 +}
7620 +
7621 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
7622 +{
7623 +       DiMustWriteLock(dentry);
7624 +       au_di(dentry)->di_bend = bindex;
7625 +}
7626 +
7627 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
7628 +{
7629 +       DiMustWriteLock(dentry);
7630 +       /* dbwh can be outside of bstart - bend range */
7631 +       au_di(dentry)->di_bwh = bindex;
7632 +}
7633 +
7634 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
7635 +{
7636 +       DiMustWriteLock(dentry);
7637 +       au_di(dentry)->di_bdiropq = bindex;
7638 +}
7639 +
7640 +/* ---------------------------------------------------------------------- */
7641 +
7642 +#ifdef CONFIG_AUFS_HNOTIFY
7643 +static inline void au_digen_dec(struct dentry *d)
7644 +{
7645 +       atomic_dec(&au_di(d)->di_generation);
7646 +}
7647 +
7648 +static inline void au_hn_di_reinit(struct dentry *dentry)
7649 +{
7650 +       dentry->d_fsdata = NULL;
7651 +}
7652 +#else
7653 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
7654 +#endif /* CONFIG_AUFS_HNOTIFY */
7655 +
7656 +#endif /* __KERNEL__ */
7657 +#endif /* __AUFS_DENTRY_H__ */
7658 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
7659 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
7660 +++ linux/fs/aufs/dinfo.c       2013-07-30 22:42:55.839613269 +0200
7661 @@ -0,0 +1,543 @@
7662 +/*
7663 + * Copyright (C) 2005-2013 Junjiro R. Okajima
7664 + *
7665 + * This program, aufs is free software; you can redistribute it and/or modify
7666 + * it under the terms of the GNU General Public License as published by
7667 + * the Free Software Foundation; either version 2 of the License, or
7668 + * (at your option) any later version.
7669 + *
7670 + * This program is distributed in the hope that it will be useful,
7671 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7672 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7673 + * GNU General Public License for more details.
7674 + *
7675 + * You should have received a copy of the GNU General Public License
7676 + * along with this program; if not, write to the Free Software
7677 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7678 + */
7679 +
7680 +/*
7681 + * dentry private data
7682 + */
7683 +
7684 +#include "aufs.h"
7685 +
7686 +void au_di_init_once(void *_dinfo)
7687 +{
7688 +       struct au_dinfo *dinfo = _dinfo;
7689 +       static struct lock_class_key aufs_di;
7690 +
7691 +       au_rw_init(&dinfo->di_rwsem);
7692 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
7693 +}
7694 +
7695 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
7696 +{
7697 +       struct au_dinfo *dinfo;
7698 +       int nbr, i;
7699 +
7700 +       dinfo = au_cache_alloc_dinfo();
7701 +       if (unlikely(!dinfo))
7702 +               goto out;
7703 +
7704 +       nbr = au_sbend(sb) + 1;
7705 +       if (nbr <= 0)
7706 +               nbr = 1;
7707 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
7708 +       if (dinfo->di_hdentry) {
7709 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
7710 +               dinfo->di_bstart = -1;
7711 +               dinfo->di_bend = -1;
7712 +               dinfo->di_bwh = -1;
7713 +               dinfo->di_bdiropq = -1;
7714 +               for (i = 0; i < nbr; i++)
7715 +                       dinfo->di_hdentry[i].hd_id = -1;
7716 +               goto out;
7717 +       }
7718 +
7719 +       au_cache_free_dinfo(dinfo);
7720 +       dinfo = NULL;
7721 +
7722 +out:
7723 +       return dinfo;
7724 +}
7725 +
7726 +void au_di_free(struct au_dinfo *dinfo)
7727 +{
7728 +       struct au_hdentry *p;
7729 +       aufs_bindex_t bend, bindex;
7730 +
7731 +       /* dentry may not be revalidated */
7732 +       bindex = dinfo->di_bstart;
7733 +       if (bindex >= 0) {
7734 +               bend = dinfo->di_bend;
7735 +               p = dinfo->di_hdentry + bindex;
7736 +               while (bindex++ <= bend)
7737 +                       au_hdput(p++);
7738 +       }
7739 +       kfree(dinfo->di_hdentry);
7740 +       au_cache_free_dinfo(dinfo);
7741 +}
7742 +
7743 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
7744 +{
7745 +       struct au_hdentry *p;
7746 +       aufs_bindex_t bi;
7747 +
7748 +       AuRwMustWriteLock(&a->di_rwsem);
7749 +       AuRwMustWriteLock(&b->di_rwsem);
7750 +
7751 +#define DiSwap(v, name)                                \
7752 +       do {                                    \
7753 +               v = a->di_##name;               \
7754 +               a->di_##name = b->di_##name;    \
7755 +               b->di_##name = v;               \
7756 +       } while (0)
7757 +
7758 +       DiSwap(p, hdentry);
7759 +       DiSwap(bi, bstart);
7760 +       DiSwap(bi, bend);
7761 +       DiSwap(bi, bwh);
7762 +       DiSwap(bi, bdiropq);
7763 +       /* smp_mb(); */
7764 +
7765 +#undef DiSwap
7766 +}
7767 +
7768 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
7769 +{
7770 +       AuRwMustWriteLock(&dst->di_rwsem);
7771 +       AuRwMustWriteLock(&src->di_rwsem);
7772 +
7773 +       dst->di_bstart = src->di_bstart;
7774 +       dst->di_bend = src->di_bend;
7775 +       dst->di_bwh = src->di_bwh;
7776 +       dst->di_bdiropq = src->di_bdiropq;
7777 +       /* smp_mb(); */
7778 +}
7779 +
7780 +int au_di_init(struct dentry *dentry)
7781 +{
7782 +       int err;
7783 +       struct super_block *sb;
7784 +       struct au_dinfo *dinfo;
7785 +
7786 +       err = 0;
7787 +       sb = dentry->d_sb;
7788 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
7789 +       if (dinfo) {
7790 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
7791 +               /* smp_mb(); */ /* atomic_set */
7792 +               dentry->d_fsdata = dinfo;
7793 +       } else
7794 +               err = -ENOMEM;
7795 +
7796 +       return err;
7797 +}
7798 +
7799 +void au_di_fin(struct dentry *dentry)
7800 +{
7801 +       struct au_dinfo *dinfo;
7802 +
7803 +       dinfo = au_di(dentry);
7804 +       AuRwDestroy(&dinfo->di_rwsem);
7805 +       au_di_free(dinfo);
7806 +}
7807 +
7808 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
7809 +{
7810 +       int err, sz;
7811 +       struct au_hdentry *hdp;
7812 +
7813 +       AuRwMustWriteLock(&dinfo->di_rwsem);
7814 +
7815 +       err = -ENOMEM;
7816 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
7817 +       if (!sz)
7818 +               sz = sizeof(*hdp);
7819 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
7820 +       if (hdp) {
7821 +               dinfo->di_hdentry = hdp;
7822 +               err = 0;
7823 +       }
7824 +
7825 +       return err;
7826 +}
7827 +
7828 +/* ---------------------------------------------------------------------- */
7829 +
7830 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
7831 +{
7832 +       switch (lsc) {
7833 +       case AuLsc_DI_CHILD:
7834 +               ii_write_lock_child(inode);
7835 +               break;
7836 +       case AuLsc_DI_CHILD2:
7837 +               ii_write_lock_child2(inode);
7838 +               break;
7839 +       case AuLsc_DI_CHILD3:
7840 +               ii_write_lock_child3(inode);
7841 +               break;
7842 +       case AuLsc_DI_PARENT:
7843 +               ii_write_lock_parent(inode);
7844 +               break;
7845 +       case AuLsc_DI_PARENT2:
7846 +               ii_write_lock_parent2(inode);
7847 +               break;
7848 +       case AuLsc_DI_PARENT3:
7849 +               ii_write_lock_parent3(inode);
7850 +               break;
7851 +       default:
7852 +               BUG();
7853 +       }
7854 +}
7855 +
7856 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
7857 +{
7858 +       switch (lsc) {
7859 +       case AuLsc_DI_CHILD:
7860 +               ii_read_lock_child(inode);
7861 +               break;
7862 +       case AuLsc_DI_CHILD2:
7863 +               ii_read_lock_child2(inode);
7864 +               break;
7865 +       case AuLsc_DI_CHILD3:
7866 +               ii_read_lock_child3(inode);
7867 +               break;
7868 +       case AuLsc_DI_PARENT:
7869 +               ii_read_lock_parent(inode);
7870 +               break;
7871 +       case AuLsc_DI_PARENT2:
7872 +               ii_read_lock_parent2(inode);
7873 +               break;
7874 +       case AuLsc_DI_PARENT3:
7875 +               ii_read_lock_parent3(inode);
7876 +               break;
7877 +       default:
7878 +               BUG();
7879 +       }
7880 +}
7881 +
7882 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
7883 +{
7884 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
7885 +       if (d->d_inode) {
7886 +               if (au_ftest_lock(flags, IW))
7887 +                       do_ii_write_lock(d->d_inode, lsc);
7888 +               else if (au_ftest_lock(flags, IR))
7889 +                       do_ii_read_lock(d->d_inode, lsc);
7890 +       }
7891 +}
7892 +
7893 +void di_read_unlock(struct dentry *d, int flags)
7894 +{
7895 +       if (d->d_inode) {
7896 +               if (au_ftest_lock(flags, IW)) {
7897 +                       au_dbg_verify_dinode(d);
7898 +                       ii_write_unlock(d->d_inode);
7899 +               } else if (au_ftest_lock(flags, IR)) {
7900 +                       au_dbg_verify_dinode(d);
7901 +                       ii_read_unlock(d->d_inode);
7902 +               }
7903 +       }
7904 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
7905 +}
7906 +
7907 +void di_downgrade_lock(struct dentry *d, int flags)
7908 +{
7909 +       if (d->d_inode && au_ftest_lock(flags, IR))
7910 +               ii_downgrade_lock(d->d_inode);
7911 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
7912 +}
7913 +
7914 +void di_write_lock(struct dentry *d, unsigned int lsc)
7915 +{
7916 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
7917 +       if (d->d_inode)
7918 +               do_ii_write_lock(d->d_inode, lsc);
7919 +}
7920 +
7921 +void di_write_unlock(struct dentry *d)
7922 +{
7923 +       au_dbg_verify_dinode(d);
7924 +       if (d->d_inode)
7925 +               ii_write_unlock(d->d_inode);
7926 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
7927 +}
7928 +
7929 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
7930 +{
7931 +       AuDebugOn(d1 == d2
7932 +                 || d1->d_inode == d2->d_inode
7933 +                 || d1->d_sb != d2->d_sb);
7934 +
7935 +       if (isdir && au_test_subdir(d1, d2)) {
7936 +               di_write_lock_child(d1);
7937 +               di_write_lock_child2(d2);
7938 +       } else {
7939 +               /* there should be no races */
7940 +               di_write_lock_child(d2);
7941 +               di_write_lock_child2(d1);
7942 +       }
7943 +}
7944 +
7945 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
7946 +{
7947 +       AuDebugOn(d1 == d2
7948 +                 || d1->d_inode == d2->d_inode
7949 +                 || d1->d_sb != d2->d_sb);
7950 +
7951 +       if (isdir && au_test_subdir(d1, d2)) {
7952 +               di_write_lock_parent(d1);
7953 +               di_write_lock_parent2(d2);
7954 +       } else {
7955 +               /* there should be no races */
7956 +               di_write_lock_parent(d2);
7957 +               di_write_lock_parent2(d1);
7958 +       }
7959 +}
7960 +
7961 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
7962 +{
7963 +       di_write_unlock(d1);
7964 +       if (d1->d_inode == d2->d_inode)
7965 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
7966 +       else
7967 +               di_write_unlock(d2);
7968 +}
7969 +
7970 +/* ---------------------------------------------------------------------- */
7971 +
7972 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
7973 +{
7974 +       struct dentry *d;
7975 +
7976 +       DiMustAnyLock(dentry);
7977 +
7978 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7979 +               return NULL;
7980 +       AuDebugOn(bindex < 0);
7981 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
7982 +       AuDebugOn(d && d->d_count <= 0);
7983 +       return d;
7984 +}
7985 +
7986 +/*
7987 + * extended version of au_h_dptr().
7988 + * returns a hashed and positive h_dentry in bindex, NULL, or error.
7989 + */
7990 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
7991 +{
7992 +       struct dentry *h_dentry;
7993 +       struct inode *inode, *h_inode;
7994 +
7995 +       inode = dentry->d_inode;
7996 +       AuDebugOn(!inode);
7997 +
7998 +       h_dentry = NULL;
7999 +       if (au_dbstart(dentry) <= bindex
8000 +           && bindex <= au_dbend(dentry))
8001 +               h_dentry = au_h_dptr(dentry, bindex);
8002 +       if (h_dentry && !au_d_hashed_positive(h_dentry)) {
8003 +               dget(h_dentry);
8004 +               goto out; /* success */
8005 +       }
8006 +
8007 +       AuDebugOn(bindex < au_ibstart(inode));
8008 +       AuDebugOn(au_ibend(inode) < bindex);
8009 +       h_inode = au_h_iptr(inode, bindex);
8010 +       h_dentry = d_find_alias(h_inode);
8011 +       if (h_dentry) {
8012 +               if (!IS_ERR(h_dentry)) {
8013 +                       if (!au_d_hashed_positive(h_dentry))
8014 +                               goto out; /* success */
8015 +                       dput(h_dentry);
8016 +               } else
8017 +                       goto out;
8018 +       }
8019 +
8020 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
8021 +               h_dentry = au_plink_lkup(inode, bindex);
8022 +               AuDebugOn(!h_dentry);
8023 +               if (!IS_ERR(h_dentry)) {
8024 +                       if (!au_d_hashed_positive(h_dentry))
8025 +                               goto out; /* success */
8026 +                       dput(h_dentry);
8027 +                       h_dentry = NULL;
8028 +               }
8029 +       }
8030 +
8031 +out:
8032 +       AuDbgDentry(h_dentry);
8033 +       return h_dentry;
8034 +}
8035 +
8036 +aufs_bindex_t au_dbtail(struct dentry *dentry)
8037 +{
8038 +       aufs_bindex_t bend, bwh;
8039 +
8040 +       bend = au_dbend(dentry);
8041 +       if (0 <= bend) {
8042 +               bwh = au_dbwh(dentry);
8043 +               if (!bwh)
8044 +                       return bwh;
8045 +               if (0 < bwh && bwh < bend)
8046 +                       return bwh - 1;
8047 +       }
8048 +       return bend;
8049 +}
8050 +
8051 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
8052 +{
8053 +       aufs_bindex_t bend, bopq;
8054 +
8055 +       bend = au_dbtail(dentry);
8056 +       if (0 <= bend) {
8057 +               bopq = au_dbdiropq(dentry);
8058 +               if (0 <= bopq && bopq < bend)
8059 +                       bend = bopq;
8060 +       }
8061 +       return bend;
8062 +}
8063 +
8064 +/* ---------------------------------------------------------------------- */
8065 +
8066 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8067 +                  struct dentry *h_dentry)
8068 +{
8069 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
8070 +       struct au_branch *br;
8071 +
8072 +       DiMustWriteLock(dentry);
8073 +
8074 +       au_hdput(hd);
8075 +       hd->hd_dentry = h_dentry;
8076 +       if (h_dentry) {
8077 +               br = au_sbr(dentry->d_sb, bindex);
8078 +               hd->hd_id = br->br_id;
8079 +       }
8080 +}
8081 +
8082 +int au_dbrange_test(struct dentry *dentry)
8083 +{
8084 +       int err;
8085 +       aufs_bindex_t bstart, bend;
8086 +
8087 +       err = 0;
8088 +       bstart = au_dbstart(dentry);
8089 +       bend = au_dbend(dentry);
8090 +       if (bstart >= 0)
8091 +               AuDebugOn(bend < 0 && bstart > bend);
8092 +       else {
8093 +               err = -EIO;
8094 +               AuDebugOn(bend >= 0);
8095 +       }
8096 +
8097 +       return err;
8098 +}
8099 +
8100 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
8101 +{
8102 +       int err;
8103 +
8104 +       err = 0;
8105 +       if (unlikely(au_digen(dentry) != sigen
8106 +                    || au_iigen_test(dentry->d_inode, sigen)))
8107 +               err = -EIO;
8108 +
8109 +       return err;
8110 +}
8111 +
8112 +void au_update_digen(struct dentry *dentry)
8113 +{
8114 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
8115 +       /* smp_mb(); */ /* atomic_set */
8116 +}
8117 +
8118 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
8119 +{
8120 +       struct au_dinfo *dinfo;
8121 +       struct dentry *h_d;
8122 +       struct au_hdentry *hdp;
8123 +
8124 +       DiMustWriteLock(dentry);
8125 +
8126 +       dinfo = au_di(dentry);
8127 +       if (!dinfo || dinfo->di_bstart < 0)
8128 +               return;
8129 +
8130 +       hdp = dinfo->di_hdentry;
8131 +       if (do_put_zero) {
8132 +               aufs_bindex_t bindex, bend;
8133 +
8134 +               bend = dinfo->di_bend;
8135 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
8136 +                       h_d = hdp[0 + bindex].hd_dentry;
8137 +                       if (h_d && !h_d->d_inode)
8138 +                               au_set_h_dptr(dentry, bindex, NULL);
8139 +               }
8140 +       }
8141 +
8142 +       dinfo->di_bstart = -1;
8143 +       while (++dinfo->di_bstart <= dinfo->di_bend)
8144 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
8145 +                       break;
8146 +       if (dinfo->di_bstart > dinfo->di_bend) {
8147 +               dinfo->di_bstart = -1;
8148 +               dinfo->di_bend = -1;
8149 +               return;
8150 +       }
8151 +
8152 +       dinfo->di_bend++;
8153 +       while (0 <= --dinfo->di_bend)
8154 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
8155 +                       break;
8156 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
8157 +}
8158 +
8159 +void au_update_dbstart(struct dentry *dentry)
8160 +{
8161 +       aufs_bindex_t bindex, bend;
8162 +       struct dentry *h_dentry;
8163 +
8164 +       bend = au_dbend(dentry);
8165 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
8166 +               h_dentry = au_h_dptr(dentry, bindex);
8167 +               if (!h_dentry)
8168 +                       continue;
8169 +               if (h_dentry->d_inode) {
8170 +                       au_set_dbstart(dentry, bindex);
8171 +                       return;
8172 +               }
8173 +               au_set_h_dptr(dentry, bindex, NULL);
8174 +       }
8175 +}
8176 +
8177 +void au_update_dbend(struct dentry *dentry)
8178 +{
8179 +       aufs_bindex_t bindex, bstart;
8180 +       struct dentry *h_dentry;
8181 +
8182 +       bstart = au_dbstart(dentry);
8183 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
8184 +               h_dentry = au_h_dptr(dentry, bindex);
8185 +               if (!h_dentry)
8186 +                       continue;
8187 +               if (h_dentry->d_inode) {
8188 +                       au_set_dbend(dentry, bindex);
8189 +                       return;
8190 +               }
8191 +               au_set_h_dptr(dentry, bindex, NULL);
8192 +       }
8193 +}
8194 +
8195 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
8196 +{
8197 +       aufs_bindex_t bindex, bend;
8198 +
8199 +       bend = au_dbend(dentry);
8200 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
8201 +               if (au_h_dptr(dentry, bindex) == h_dentry)
8202 +                       return bindex;
8203 +       return -1;
8204 +}
8205 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
8206 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
8207 +++ linux/fs/aufs/dir.c 2013-07-30 22:42:55.839613269 +0200
8208 @@ -0,0 +1,632 @@
8209 +/*
8210 + * Copyright (C) 2005-2013 Junjiro R. Okajima
8211 + *
8212 + * This program, aufs is free software; you can redistribute it and/or modify
8213 + * it under the terms of the GNU General Public License as published by
8214 + * the Free Software Foundation; either version 2 of the License, or
8215 + * (at your option) any later version.
8216 + *
8217 + * This program is distributed in the hope that it will be useful,
8218 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8219 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8220 + * GNU General Public License for more details.
8221 + *
8222 + * You should have received a copy of the GNU General Public License
8223 + * along with this program; if not, write to the Free Software
8224 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
8225 + */
8226 +
8227 +/*
8228 + * directory operations
8229 + */
8230 +
8231 +#include <linux/fs_stack.h>
8232 +#include "aufs.h"
8233 +
8234 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
8235 +{
8236 +       unsigned int nlink;
8237 +
8238 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8239 +
8240 +       nlink = dir->i_nlink;
8241 +       nlink += h_dir->i_nlink - 2;
8242 +       if (h_dir->i_nlink < 2)
8243 +               nlink += 2;
8244 +       smp_mb();
8245 +       /* 0 can happen in revaliding */
8246 +       set_nlink(dir, nlink);
8247 +}
8248 +
8249 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
8250 +{
8251 +       unsigned int nlink;
8252 +
8253 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8254 +
8255 +       nlink = dir->i_nlink;
8256 +       nlink -= h_dir->i_nlink - 2;
8257 +       if (h_dir->i_nlink < 2)
8258 +               nlink -= 2;
8259 +       smp_mb();
8260 +       /* nlink == 0 means the branch-fs is broken */
8261 +       set_nlink(dir, nlink);
8262 +}
8263 +
8264 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
8265 +{
8266 +       loff_t sz;
8267 +       aufs_bindex_t bindex, bend;
8268 +       struct file *h_file;
8269 +       struct dentry *h_dentry;
8270 +
8271 +       sz = 0;
8272 +       if (file) {
8273 +               AuDebugOn(!file_inode(file));
8274 +               AuDebugOn(!S_ISDIR(file_inode(file)->i_mode));
8275 +
8276 +               bend = au_fbend_dir(file);
8277 +               for (bindex = au_fbstart(file);
8278 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
8279 +                    bindex++) {
8280 +                       h_file = au_hf_dir(file, bindex);
8281 +                       if (h_file && file_inode(h_file))
8282 +                               sz += vfsub_f_size_read(h_file);
8283 +               }
8284 +       } else {
8285 +               AuDebugOn(!dentry);
8286 +               AuDebugOn(!dentry->d_inode);
8287 +               AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
8288 +
8289 +               bend = au_dbtaildir(dentry);
8290 +               for (bindex = au_dbstart(dentry);
8291 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
8292 +                    bindex++) {
8293 +                       h_dentry = au_h_dptr(dentry, bindex);
8294 +                       if (h_dentry && h_dentry->d_inode)
8295 +                               sz += i_size_read(h_dentry->d_inode);
8296 +               }
8297 +       }
8298 +       if (sz < KMALLOC_MAX_SIZE)
8299 +               sz = roundup_pow_of_two(sz);
8300 +       if (sz > KMALLOC_MAX_SIZE)
8301 +               sz = KMALLOC_MAX_SIZE;
8302 +       else if (sz < NAME_MAX) {
8303 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
8304 +               sz = AUFS_RDBLK_DEF;
8305 +       }
8306 +       return sz;
8307 +}
8308 +
8309 +/* ---------------------------------------------------------------------- */
8310 +
8311 +static int reopen_dir(struct file *file)
8312 +{
8313 +       int err;
8314 +       unsigned int flags;
8315 +       aufs_bindex_t bindex, btail, bstart;
8316 +       struct dentry *dentry, *h_dentry;
8317 +       struct file *h_file;
8318 +
8319 +       /* open all lower dirs */
8320 +       dentry = file->f_dentry;
8321 +       bstart = au_dbstart(dentry);
8322 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
8323 +               au_set_h_fptr(file, bindex, NULL);
8324 +       au_set_fbstart(file, bstart);
8325 +
8326 +       btail = au_dbtaildir(dentry);
8327 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
8328 +               au_set_h_fptr(file, bindex, NULL);
8329 +       au_set_fbend_dir(file, btail);
8330 +
8331 +       flags = vfsub_file_flags(file);
8332 +       for (bindex = bstart; bindex <= btail; bindex++) {
8333 +               h_dentry = au_h_dptr(dentry, bindex);
8334 +               if (!h_dentry)
8335 +                       continue;
8336 +               h_file = au_hf_dir(file, bindex);
8337 +               if (h_file)
8338 +                       continue;
8339 +
8340 +               h_file = au_h_open(dentry, bindex, flags, file);
8341 +               err = PTR_ERR(h_file);
8342 +               if (IS_ERR(h_file))
8343 +                       goto out; /* close all? */
8344 +               au_set_h_fptr(file, bindex, h_file);
8345 +       }
8346 +       au_update_figen(file);
8347 +       /* todo: necessary? */
8348 +       /* file->f_ra = h_file->f_ra; */
8349 +       err = 0;
8350 +
8351 +out:
8352 +       return err;
8353 +}
8354 +
8355 +static int do_open_dir(struct file *file, int flags)
8356 +{
8357 +       int err;
8358 +       aufs_bindex_t bindex, btail;
8359 +       struct dentry *dentry, *h_dentry;
8360 +       struct file *h_file;
8361 +
8362 +       FiMustWriteLock(file);
8363 +
8364 +       dentry = file->f_dentry;
8365 +       err = au_alive_dir(dentry);
8366 +       if (unlikely(err))
8367 +               goto out;
8368 +
8369 +       file->f_version = dentry->d_inode->i_version;
8370 +       bindex = au_dbstart(dentry);
8371 +       au_set_fbstart(file, bindex);
8372 +       btail = au_dbtaildir(dentry);
8373 +       au_set_fbend_dir(file, btail);
8374 +       for (; !err && bindex <= btail; bindex++) {
8375 +               h_dentry = au_h_dptr(dentry, bindex);
8376 +               if (!h_dentry)
8377 +                       continue;
8378 +
8379 +               h_file = au_h_open(dentry, bindex, flags, file);
8380 +               if (IS_ERR(h_file)) {
8381 +                       err = PTR_ERR(h_file);
8382 +                       break;
8383 +               }
8384 +               au_set_h_fptr(file, bindex, h_file);
8385 +       }
8386 +       au_update_figen(file);
8387 +       /* todo: necessary? */
8388 +       /* file->f_ra = h_file->f_ra; */
8389 +       if (!err)
8390 +               return 0; /* success */
8391 +
8392 +       /* close all */
8393 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
8394 +               au_set_h_fptr(file, bindex, NULL);
8395 +       au_set_fbstart(file, -1);
8396 +       au_set_fbend_dir(file, -1);
8397 +
8398 +out:
8399 +       return err;
8400 +}
8401 +
8402 +static int aufs_open_dir(struct inode *inode __maybe_unused,
8403 +                        struct file *file)
8404 +{
8405 +       int err;
8406 +       struct super_block *sb;
8407 +       struct au_fidir *fidir;
8408 +
8409 +       err = -ENOMEM;
8410 +       sb = file->f_dentry->d_sb;
8411 +       si_read_lock(sb, AuLock_FLUSH);
8412 +       fidir = au_fidir_alloc(sb);
8413 +       if (fidir) {
8414 +               err = au_do_open(file, do_open_dir, fidir);
8415 +               if (unlikely(err))
8416 +                       kfree(fidir);
8417 +       }
8418 +       si_read_unlock(sb);
8419 +       return err;
8420 +}
8421 +
8422 +static int aufs_release_dir(struct inode *inode __maybe_unused,
8423 +                           struct file *file)
8424 +{
8425 +       struct au_vdir *vdir_cache;
8426 +       struct au_finfo *finfo;
8427 +       struct au_fidir *fidir;
8428 +       aufs_bindex_t bindex, bend;
8429 +
8430 +       finfo = au_fi(file);
8431 +       fidir = finfo->fi_hdir;
8432 +       if (fidir) {
8433 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
8434 +               if (vdir_cache)
8435 +                       au_vdir_free(vdir_cache);
8436 +
8437 +               bindex = finfo->fi_btop;
8438 +               if (bindex >= 0) {
8439 +                       /*
8440 +                        * calls fput() instead of filp_close(),
8441 +                        * since no dnotify or lock for the lower file.
8442 +                        */
8443 +                       bend = fidir->fd_bbot;
8444 +                       for (; bindex <= bend; bindex++)
8445 +                               au_set_h_fptr(file, bindex, NULL);
8446 +               }
8447 +               kfree(fidir);
8448 +               finfo->fi_hdir = NULL;
8449 +       }
8450 +       au_finfo_fin(file);
8451 +       return 0;
8452 +}
8453 +
8454 +/* ---------------------------------------------------------------------- */
8455 +
8456 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
8457 +{
8458 +       int err;
8459 +       aufs_bindex_t bindex, bend;
8460 +       struct file *h_file;
8461 +
8462 +       err = 0;
8463 +       bend = au_fbend_dir(file);
8464 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8465 +               h_file = au_hf_dir(file, bindex);
8466 +               if (h_file)
8467 +                       err = vfsub_flush(h_file, id);
8468 +       }
8469 +       return err;
8470 +}
8471 +
8472 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
8473 +{
8474 +       return au_do_flush(file, id, au_do_flush_dir);
8475 +}
8476 +
8477 +/* ---------------------------------------------------------------------- */
8478 +
8479 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
8480 +{
8481 +       int err;
8482 +       aufs_bindex_t bend, bindex;
8483 +       struct inode *inode;
8484 +       struct super_block *sb;
8485 +
8486 +       err = 0;
8487 +       sb = dentry->d_sb;
8488 +       inode = dentry->d_inode;
8489 +       IMustLock(inode);
8490 +       bend = au_dbend(dentry);
8491 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
8492 +               struct path h_path;
8493 +
8494 +               if (au_test_ro(sb, bindex, inode))
8495 +                       continue;
8496 +               h_path.dentry = au_h_dptr(dentry, bindex);
8497 +               if (!h_path.dentry)
8498 +                       continue;
8499 +
8500 +               h_path.mnt = au_sbr_mnt(sb, bindex);
8501 +               err = vfsub_fsync(NULL, &h_path, datasync);
8502 +       }
8503 +
8504 +       return err;
8505 +}
8506 +
8507 +static int au_do_fsync_dir(struct file *file, int datasync)
8508 +{
8509 +       int err;
8510 +       aufs_bindex_t bend, bindex;
8511 +       struct file *h_file;
8512 +       struct super_block *sb;
8513 +       struct inode *inode;
8514 +
8515 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8516 +       if (unlikely(err))
8517 +               goto out;
8518 +
8519 +       sb = file->f_dentry->d_sb;
8520 +       inode = file_inode(file);
8521 +       bend = au_fbend_dir(file);
8522 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8523 +               h_file = au_hf_dir(file, bindex);
8524 +               if (!h_file || au_test_ro(sb, bindex, inode))
8525 +                       continue;
8526 +
8527 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
8528 +       }
8529 +
8530 +out:
8531 +       return err;
8532 +}
8533 +
8534 +/*
8535 + * @file may be NULL
8536 + */
8537 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
8538 +                         int datasync)
8539 +{
8540 +       int err;
8541 +       struct dentry *dentry;
8542 +       struct super_block *sb;
8543 +       struct mutex *mtx;
8544 +
8545 +       err = 0;
8546 +       dentry = file->f_dentry;
8547 +       mtx = &dentry->d_inode->i_mutex;
8548 +       mutex_lock(mtx);
8549 +       sb = dentry->d_sb;
8550 +       si_noflush_read_lock(sb);
8551 +       if (file)
8552 +               err = au_do_fsync_dir(file, datasync);
8553 +       else {
8554 +               di_write_lock_child(dentry);
8555 +               err = au_do_fsync_dir_no_file(dentry, datasync);
8556 +       }
8557 +       au_cpup_attr_timesizes(dentry->d_inode);
8558 +       di_write_unlock(dentry);
8559 +       if (file)
8560 +               fi_write_unlock(file);
8561 +
8562 +       si_read_unlock(sb);
8563 +       mutex_unlock(mtx);
8564 +       return err;
8565 +}
8566 +
8567 +/* ---------------------------------------------------------------------- */
8568 +
8569 +static int aufs_readdir(struct file *file, void *dirent, filldir_t filldir)
8570 +{
8571 +       int err;
8572 +       struct dentry *dentry;
8573 +       struct inode *inode, *h_inode;
8574 +       struct super_block *sb;
8575 +
8576 +       dentry = file->f_dentry;
8577 +       inode = dentry->d_inode;
8578 +       IMustLock(inode);
8579 +
8580 +       sb = dentry->d_sb;
8581 +       si_read_lock(sb, AuLock_FLUSH);
8582 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
8583 +       if (unlikely(err))
8584 +               goto out;
8585 +       err = au_alive_dir(dentry);
8586 +       if (!err)
8587 +               err = au_vdir_init(file);
8588 +       di_downgrade_lock(dentry, AuLock_IR);
8589 +       if (unlikely(err))
8590 +               goto out_unlock;
8591 +
8592 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
8593 +       if (!au_test_nfsd()) {
8594 +               err = au_vdir_fill_de(file, dirent, filldir);
8595 +               fsstack_copy_attr_atime(inode, h_inode);
8596 +       } else {
8597 +               /*
8598 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
8599 +                * encode_fh() and others.
8600 +                */
8601 +               atomic_inc(&h_inode->i_count);
8602 +               di_read_unlock(dentry, AuLock_IR);
8603 +               si_read_unlock(sb);
8604 +               err = au_vdir_fill_de(file, dirent, filldir);
8605 +               fsstack_copy_attr_atime(inode, h_inode);
8606 +               fi_write_unlock(file);
8607 +               iput(h_inode);
8608 +
8609 +               AuTraceErr(err);
8610 +               return err;
8611 +       }
8612 +
8613 +out_unlock:
8614 +       di_read_unlock(dentry, AuLock_IR);
8615 +       fi_write_unlock(file);
8616 +out:
8617 +       si_read_unlock(sb);
8618 +       return err;
8619 +}
8620 +
8621 +/* ---------------------------------------------------------------------- */
8622 +
8623 +#define AuTestEmpty_WHONLY     1
8624 +#define AuTestEmpty_CALLED     (1 << 1)
8625 +#define AuTestEmpty_SHWH       (1 << 2)
8626 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
8627 +#define au_fset_testempty(flags, name) \
8628 +       do { (flags) |= AuTestEmpty_##name; } while (0)
8629 +#define au_fclr_testempty(flags, name) \
8630 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
8631 +
8632 +#ifndef CONFIG_AUFS_SHWH
8633 +#undef AuTestEmpty_SHWH
8634 +#define AuTestEmpty_SHWH       0
8635 +#endif
8636 +
8637 +struct test_empty_arg {
8638 +       struct au_nhash *whlist;
8639 +       unsigned int flags;
8640 +       int err;
8641 +       aufs_bindex_t bindex;
8642 +};
8643 +
8644 +static int test_empty_cb(void *__arg, const char *__name, int namelen,
8645 +                        loff_t offset __maybe_unused, u64 ino,
8646 +                        unsigned int d_type)
8647 +{
8648 +       struct test_empty_arg *arg = __arg;
8649 +       char *name = (void *)__name;
8650 +
8651 +       arg->err = 0;
8652 +       au_fset_testempty(arg->flags, CALLED);
8653 +       /* smp_mb(); */
8654 +       if (name[0] == '.'
8655 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
8656 +               goto out; /* success */
8657 +
8658 +       if (namelen <= AUFS_WH_PFX_LEN
8659 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
8660 +               if (au_ftest_testempty(arg->flags, WHONLY)
8661 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
8662 +                       arg->err = -ENOTEMPTY;
8663 +               goto out;
8664 +       }
8665 +
8666 +       name += AUFS_WH_PFX_LEN;
8667 +       namelen -= AUFS_WH_PFX_LEN;
8668 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
8669 +               arg->err = au_nhash_append_wh
8670 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
8671 +                        au_ftest_testempty(arg->flags, SHWH));
8672 +
8673 +out:
8674 +       /* smp_mb(); */
8675 +       AuTraceErr(arg->err);
8676 +       return arg->err;
8677 +}
8678 +
8679 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8680 +{
8681 +       int err;
8682 +       struct file *h_file;
8683 +
8684 +       h_file = au_h_open(dentry, arg->bindex,
8685 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
8686 +                          /*file*/NULL);
8687 +       err = PTR_ERR(h_file);
8688 +       if (IS_ERR(h_file))
8689 +               goto out;
8690 +
8691 +       err = 0;
8692 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
8693 +           && !file_inode(h_file)->i_nlink)
8694 +               goto out_put;
8695 +
8696 +       do {
8697 +               arg->err = 0;
8698 +               au_fclr_testempty(arg->flags, CALLED);
8699 +               /* smp_mb(); */
8700 +               err = vfsub_readdir(h_file, test_empty_cb, arg);
8701 +               if (err >= 0)
8702 +                       err = arg->err;
8703 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
8704 +
8705 +out_put:
8706 +       fput(h_file);
8707 +       au_sbr_put(dentry->d_sb, arg->bindex);
8708 +out:
8709 +       return err;
8710 +}
8711 +
8712 +struct do_test_empty_args {
8713 +       int *errp;
8714 +       struct dentry *dentry;
8715 +       struct test_empty_arg *arg;
8716 +};
8717 +
8718 +static void call_do_test_empty(void *args)
8719 +{
8720 +       struct do_test_empty_args *a = args;
8721 +       *a->errp = do_test_empty(a->dentry, a->arg);
8722 +}
8723 +
8724 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
8725 +{
8726 +       int err, wkq_err;
8727 +       struct dentry *h_dentry;
8728 +       struct inode *h_inode;
8729 +
8730 +       h_dentry = au_h_dptr(dentry, arg->bindex);
8731 +       h_inode = h_dentry->d_inode;
8732 +       /* todo: i_mode changes anytime? */
8733 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
8734 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
8735 +       mutex_unlock(&h_inode->i_mutex);
8736 +       if (!err)
8737 +               err = do_test_empty(dentry, arg);
8738 +       else {
8739 +               struct do_test_empty_args args = {
8740 +                       .errp   = &err,
8741 +                       .dentry = dentry,
8742 +                       .arg    = arg
8743 +               };
8744 +               unsigned int flags = arg->flags;
8745 +
8746 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
8747 +               if (unlikely(wkq_err))
8748 +                       err = wkq_err;
8749 +               arg->flags = flags;
8750 +       }
8751 +
8752 +       return err;
8753 +}
8754 +
8755 +int au_test_empty_lower(struct dentry *dentry)
8756 +{
8757 +       int err;
8758 +       unsigned int rdhash;
8759 +       aufs_bindex_t bindex, bstart, btail;
8760 +       struct au_nhash whlist;
8761 +       struct test_empty_arg arg;
8762 +
8763 +       SiMustAnyLock(dentry->d_sb);
8764 +
8765 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
8766 +       if (!rdhash)
8767 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
8768 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
8769 +       if (unlikely(err))
8770 +               goto out;
8771 +
8772 +       arg.flags = 0;
8773 +       arg.whlist = &whlist;
8774 +       bstart = au_dbstart(dentry);
8775 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
8776 +               au_fset_testempty(arg.flags, SHWH);
8777 +       arg.bindex = bstart;
8778 +       err = do_test_empty(dentry, &arg);
8779 +       if (unlikely(err))
8780 +               goto out_whlist;
8781 +
8782 +       au_fset_testempty(arg.flags, WHONLY);
8783 +       btail = au_dbtaildir(dentry);
8784 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
8785 +               struct dentry *h_dentry;
8786 +
8787 +               h_dentry = au_h_dptr(dentry, bindex);
8788 +               if (h_dentry && h_dentry->d_inode) {
8789 +                       arg.bindex = bindex;
8790 +                       err = do_test_empty(dentry, &arg);
8791 +               }
8792 +       }
8793 +
8794 +out_whlist:
8795 +       au_nhash_wh_free(&whlist);
8796 +out:
8797 +       return err;
8798 +}
8799 +
8800 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
8801 +{
8802 +       int err;
8803 +       struct test_empty_arg arg;
8804 +       aufs_bindex_t bindex, btail;
8805 +
8806 +       err = 0;
8807 +       arg.whlist = whlist;
8808 +       arg.flags = AuTestEmpty_WHONLY;
8809 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
8810 +               au_fset_testempty(arg.flags, SHWH);
8811 +       btail = au_dbtaildir(dentry);
8812 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
8813 +               struct dentry *h_dentry;
8814 +
8815 +               h_dentry = au_h_dptr(dentry, bindex);
8816 +               if (h_dentry && h_dentry->d_inode) {
8817 +                       arg.bindex = bindex;
8818 +                       err = sio_test_empty(dentry, &arg);
8819 +               }
8820 +       }
8821 +
8822 +       return err;
8823 +}
8824 +
8825 +/* ---------------------------------------------------------------------- */
8826 +
8827 +const struct file_operations aufs_dir_fop = {
8828 +       .owner          = THIS_MODULE,
8829 +       .llseek         = default_llseek,
8830 +       .read           = generic_read_dir,
8831 +       .readdir        = aufs_readdir,
8832 +       .unlocked_ioctl = aufs_ioctl_dir,
8833 +#ifdef CONFIG_COMPAT
8834 +       .compat_ioctl   = aufs_compat_ioctl_dir,
8835 +#endif
8836 +       .open           = aufs_open_dir,
8837 +       .release        = aufs_release_dir,
8838 +       .flush          = aufs_flush_dir,
8839 +       .fsync          = aufs_fsync_dir
8840 +};
8841 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
8842 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
8843 +++ linux/fs/aufs/dir.h 2013-07-30 22:42:55.839613269 +0200
8844 @@ -0,0 +1,137 @@
8845 +/*
8846 + * Copyright (C) 2005-2013 Junjiro R. Okajima
8847 + *
8848 + * This program, aufs is free software; you can redistribute it and/or modify
8849 + * it under the terms of the GNU General Public License as published by
8850 + * the Free Software Foundation; either version 2 of the License, or
8851 + * (at your option) any later version.
8852 + *
8853 + * This program is distributed in the hope that it will be useful,
8854 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8855 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8856 + * GNU General Public License for more details.
8857 + *
8858 + * You should have received a copy of the GNU General Public License
8859 + * along with this program; if not, write to the Free Software
8860 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
8861 + */
8862 +
8863 +/*
8864 + * directory operations
8865 + */
8866 +
8867 +#ifndef __AUFS_DIR_H__
8868 +#define __AUFS_DIR_H__
8869 +
8870 +#ifdef __KERNEL__
8871 +
8872 +#include <linux/fs.h>
8873 +
8874 +/* ---------------------------------------------------------------------- */
8875 +
8876 +/* need to be faster and smaller */
8877 +
8878 +struct au_nhash {
8879 +       unsigned int            nh_num;
8880 +       struct hlist_head       *nh_head;
8881 +};
8882 +
8883 +struct au_vdir_destr {
8884 +       unsigned char   len;
8885 +       unsigned char   name[0];
8886 +} __packed;
8887 +
8888 +struct au_vdir_dehstr {
8889 +       struct hlist_node       hash;
8890 +       struct au_vdir_destr    *str;
8891 +} ____cacheline_aligned_in_smp;
8892 +
8893 +struct au_vdir_de {
8894 +       ino_t                   de_ino;
8895 +       unsigned char           de_type;
8896 +       /* caution: packed */
8897 +       struct au_vdir_destr    de_str;
8898 +} __packed;
8899 +
8900 +struct au_vdir_wh {
8901 +       struct hlist_node       wh_hash;
8902 +#ifdef CONFIG_AUFS_SHWH
8903 +       ino_t                   wh_ino;
8904 +       aufs_bindex_t           wh_bindex;
8905 +       unsigned char           wh_type;
8906 +#else
8907 +       aufs_bindex_t           wh_bindex;
8908 +#endif
8909 +       /* caution: packed */
8910 +       struct au_vdir_destr    wh_str;
8911 +} __packed;
8912 +
8913 +union au_vdir_deblk_p {
8914 +       unsigned char           *deblk;
8915 +       struct au_vdir_de       *de;
8916 +};
8917 +
8918 +struct au_vdir {
8919 +       unsigned char   **vd_deblk;
8920 +       unsigned long   vd_nblk;
8921 +       struct {
8922 +               unsigned long           ul;
8923 +               union au_vdir_deblk_p   p;
8924 +       } vd_last;
8925 +
8926 +       unsigned long   vd_version;
8927 +       unsigned int    vd_deblk_sz;
8928 +       unsigned long   vd_jiffy;
8929 +} ____cacheline_aligned_in_smp;
8930 +
8931 +/* ---------------------------------------------------------------------- */
8932 +
8933 +/* dir.c */
8934 +extern const struct file_operations aufs_dir_fop;
8935 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
8936 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
8937 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
8938 +int au_test_empty_lower(struct dentry *dentry);
8939 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
8940 +
8941 +/* vdir.c */
8942 +unsigned int au_rdhash_est(loff_t sz);
8943 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
8944 +void au_nhash_wh_free(struct au_nhash *whlist);
8945 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
8946 +                           int limit);
8947 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
8948 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
8949 +                      unsigned int d_type, aufs_bindex_t bindex,
8950 +                      unsigned char shwh);
8951 +void au_vdir_free(struct au_vdir *vdir);
8952 +int au_vdir_init(struct file *file);
8953 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir);
8954 +
8955 +/* ioctl.c */
8956 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
8957 +
8958 +#ifdef CONFIG_AUFS_RDU
8959 +/* rdu.c */
8960 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
8961 +#ifdef CONFIG_COMPAT
8962 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
8963 +                        unsigned long arg);
8964 +#endif
8965 +#else
8966 +static inline long au_rdu_ioctl(struct file *file, unsigned int cmd,
8967 +                               unsigned long arg)
8968 +{
8969 +       return -EINVAL;
8970 +}
8971 +#ifdef CONFIG_COMPAT
8972 +static inline long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
8973 +                                      unsigned long arg)
8974 +{
8975 +       return -EINVAL;
8976 +}
8977 +#endif
8978 +#endif
8979 +
8980 +#endif /* __KERNEL__ */
8981 +#endif /* __AUFS_DIR_H__ */
8982 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
8983 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
8984 +++ linux/fs/aufs/dynop.c       2013-07-30 22:42:55.839613269 +0200
8985 @@ -0,0 +1,379 @@
8986 +/*
8987 + * Copyright (C) 2010-2013 Junjiro R. Okajima
8988 + *
8989 + * This program, aufs is free software; you can redistribute it and/or modify
8990 + * it under the terms of the GNU General Public License as published by
8991 + * the Free Software Foundation; either version 2 of the License, or
8992 + * (at your option) any later version.
8993 + *
8994 + * This program is distributed in the hope that it will be useful,
8995 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8996 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8997 + * GNU General Public License for more details.
8998 + *
8999 + * You should have received a copy of the GNU General Public License
9000 + * along with this program; if not, write to the Free Software
9001 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9002 + */
9003 +
9004 +/*
9005 + * dynamically customizable operations for regular files
9006 + */
9007 +
9008 +#include "aufs.h"
9009 +
9010 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
9011 +
9012 +/*
9013 + * How large will these lists be?
9014 + * Usually just a few elements, 20-30 at most for each, I guess.
9015 + */
9016 +static struct au_splhead dynop[AuDyLast];
9017 +
9018 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
9019 +{
9020 +       struct au_dykey *key, *tmp;
9021 +       struct list_head *head;
9022 +
9023 +       key = NULL;
9024 +       head = &spl->head;
9025 +       rcu_read_lock();
9026 +       list_for_each_entry_rcu(tmp, head, dk_list)
9027 +               if (tmp->dk_op.dy_hop == h_op) {
9028 +                       key = tmp;
9029 +                       kref_get(&key->dk_kref);
9030 +                       break;
9031 +               }
9032 +       rcu_read_unlock();
9033 +
9034 +       return key;
9035 +}
9036 +
9037 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
9038 +{
9039 +       struct au_dykey **k, *found;
9040 +       const void *h_op = key->dk_op.dy_hop;
9041 +       int i;
9042 +
9043 +       found = NULL;
9044 +       k = br->br_dykey;
9045 +       for (i = 0; i < AuBrDynOp; i++)
9046 +               if (k[i]) {
9047 +                       if (k[i]->dk_op.dy_hop == h_op) {
9048 +                               found = k[i];
9049 +                               break;
9050 +                       }
9051 +               } else
9052 +                       break;
9053 +       if (!found) {
9054 +               spin_lock(&br->br_dykey_lock);
9055 +               for (; i < AuBrDynOp; i++)
9056 +                       if (k[i]) {
9057 +                               if (k[i]->dk_op.dy_hop == h_op) {
9058 +                                       found = k[i];
9059 +                                       break;
9060 +                               }
9061 +                       } else {
9062 +                               k[i] = key;
9063 +                               break;
9064 +                       }
9065 +               spin_unlock(&br->br_dykey_lock);
9066 +               BUG_ON(i == AuBrDynOp); /* expand the array */
9067 +       }
9068 +
9069 +       return found;
9070 +}
9071 +
9072 +/* kref_get() if @key is already added */
9073 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
9074 +{
9075 +       struct au_dykey *tmp, *found;
9076 +       struct list_head *head;
9077 +       const void *h_op = key->dk_op.dy_hop;
9078 +
9079 +       found = NULL;
9080 +       head = &spl->head;
9081 +       spin_lock(&spl->spin);
9082 +       list_for_each_entry(tmp, head, dk_list)
9083 +               if (tmp->dk_op.dy_hop == h_op) {
9084 +                       kref_get(&tmp->dk_kref);
9085 +                       found = tmp;
9086 +                       break;
9087 +               }
9088 +       if (!found)
9089 +               list_add_rcu(&key->dk_list, head);
9090 +       spin_unlock(&spl->spin);
9091 +
9092 +       if (!found)
9093 +               DyPrSym(key);
9094 +       return found;
9095 +}
9096 +
9097 +static void dy_free_rcu(struct rcu_head *rcu)
9098 +{
9099 +       struct au_dykey *key;
9100 +
9101 +       key = container_of(rcu, struct au_dykey, dk_rcu);
9102 +       DyPrSym(key);
9103 +       kfree(key);
9104 +}
9105 +
9106 +static void dy_free(struct kref *kref)
9107 +{
9108 +       struct au_dykey *key;
9109 +       struct au_splhead *spl;
9110 +
9111 +       key = container_of(kref, struct au_dykey, dk_kref);
9112 +       spl = dynop + key->dk_op.dy_type;
9113 +       au_spl_del_rcu(&key->dk_list, spl);
9114 +       call_rcu(&key->dk_rcu, dy_free_rcu);
9115 +}
9116 +
9117 +void au_dy_put(struct au_dykey *key)
9118 +{
9119 +       kref_put(&key->dk_kref, dy_free);
9120 +}
9121 +
9122 +/* ---------------------------------------------------------------------- */
9123 +
9124 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
9125 +
9126 +#ifdef CONFIG_AUFS_DEBUG
9127 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
9128 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
9129 +#else
9130 +#define DyDbgDeclare(cnt)      do {} while (0)
9131 +#define DyDbgInc(cnt)          do {} while (0)
9132 +#endif
9133 +
9134 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
9135 +       DyDbgInc(cnt);                                                  \
9136 +       if (h_op->func) {                                               \
9137 +               if (src.func)                                           \
9138 +                       dst.func = src.func;                            \
9139 +               else                                                    \
9140 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
9141 +       }                                                               \
9142 +} while (0)
9143 +
9144 +#define DySetForce(func, dst, src) do {                \
9145 +       AuDebugOn(!src.func);                   \
9146 +       DyDbgInc(cnt);                          \
9147 +       dst.func = src.func;                    \
9148 +} while (0)
9149 +
9150 +#define DySetAop(func) \
9151 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
9152 +#define DySetAopForce(func) \
9153 +       DySetForce(func, dyaop->da_op, aufs_aop)
9154 +
9155 +static void dy_aop(struct au_dykey *key, const void *h_op,
9156 +                  struct super_block *h_sb __maybe_unused)
9157 +{
9158 +       struct au_dyaop *dyaop = (void *)key;
9159 +       const struct address_space_operations *h_aop = h_op;
9160 +       DyDbgDeclare(cnt);
9161 +
9162 +       AuDbg("%s\n", au_sbtype(h_sb));
9163 +
9164 +       DySetAop(writepage);
9165 +       DySetAopForce(readpage);        /* force */
9166 +       DySetAop(writepages);
9167 +       DySetAop(set_page_dirty);
9168 +       DySetAop(readpages);
9169 +       DySetAop(write_begin);
9170 +       DySetAop(write_end);
9171 +       DySetAop(bmap);
9172 +       DySetAop(invalidatepage);
9173 +       DySetAop(releasepage);
9174 +       DySetAop(freepage);
9175 +       /* these two will be changed according to an aufs mount option */
9176 +       DySetAop(direct_IO);
9177 +       DySetAop(get_xip_mem);
9178 +       DySetAop(migratepage);
9179 +       DySetAop(launder_page);
9180 +       DySetAop(is_partially_uptodate);
9181 +       DySetAop(error_remove_page);
9182 +       DySetAop(swap_activate);
9183 +       DySetAop(swap_deactivate);
9184 +
9185 +       DyDbgSize(cnt, *h_aop);
9186 +       dyaop->da_get_xip_mem = h_aop->get_xip_mem;
9187 +}
9188 +
9189 +/* ---------------------------------------------------------------------- */
9190 +
9191 +static void dy_bug(struct kref *kref)
9192 +{
9193 +       BUG();
9194 +}
9195 +
9196 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
9197 +{
9198 +       struct au_dykey *key, *old;
9199 +       struct au_splhead *spl;
9200 +       struct op {
9201 +               unsigned int sz;
9202 +               void (*set)(struct au_dykey *key, const void *h_op,
9203 +                           struct super_block *h_sb __maybe_unused);
9204 +       };
9205 +       static const struct op a[] = {
9206 +               [AuDy_AOP] = {
9207 +                       .sz     = sizeof(struct au_dyaop),
9208 +                       .set    = dy_aop
9209 +               }
9210 +       };
9211 +       const struct op *p;
9212 +
9213 +       spl = dynop + op->dy_type;
9214 +       key = dy_gfind_get(spl, op->dy_hop);
9215 +       if (key)
9216 +               goto out_add; /* success */
9217 +
9218 +       p = a + op->dy_type;
9219 +       key = kzalloc(p->sz, GFP_NOFS);
9220 +       if (unlikely(!key)) {
9221 +               key = ERR_PTR(-ENOMEM);
9222 +               goto out;
9223 +       }
9224 +
9225 +       key->dk_op.dy_hop = op->dy_hop;
9226 +       kref_init(&key->dk_kref);
9227 +       p->set(key, op->dy_hop, au_br_sb(br));
9228 +       old = dy_gadd(spl, key);
9229 +       if (old) {
9230 +               kfree(key);
9231 +               key = old;
9232 +       }
9233 +
9234 +out_add:
9235 +       old = dy_bradd(br, key);
9236 +       if (old)
9237 +               /* its ref-count should never be zero here */
9238 +               kref_put(&key->dk_kref, dy_bug);
9239 +out:
9240 +       return key;
9241 +}
9242 +
9243 +/* ---------------------------------------------------------------------- */
9244 +/*
9245 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
9246 + * This behaviour is neccessary to return an error from open(O_DIRECT) instead
9247 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
9248 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
9249 + * See the aufs manual in detail.
9250 + *
9251 + * To keep this behaviour, aufs has to set NULL to ->get_xip_mem too, and the
9252 + * performance of fadvise() and madvise() may be affected.
9253 + */
9254 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
9255 +{
9256 +       if (!do_dx) {
9257 +               dyaop->da_op.direct_IO = NULL;
9258 +               dyaop->da_op.get_xip_mem = NULL;
9259 +       } else {
9260 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
9261 +               dyaop->da_op.get_xip_mem = aufs_aop.get_xip_mem;
9262 +               if (!dyaop->da_get_xip_mem)
9263 +                       dyaop->da_op.get_xip_mem = NULL;
9264 +       }
9265 +}
9266 +
9267 +static struct au_dyaop *dy_aget(struct au_branch *br,
9268 +                               const struct address_space_operations *h_aop,
9269 +                               int do_dx)
9270 +{
9271 +       struct au_dyaop *dyaop;
9272 +       struct au_dynop op;
9273 +
9274 +       op.dy_type = AuDy_AOP;
9275 +       op.dy_haop = h_aop;
9276 +       dyaop = (void *)dy_get(&op, br);
9277 +       if (IS_ERR(dyaop))
9278 +               goto out;
9279 +       dy_adx(dyaop, do_dx);
9280 +
9281 +out:
9282 +       return dyaop;
9283 +}
9284 +
9285 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
9286 +               struct inode *h_inode)
9287 +{
9288 +       int err, do_dx;
9289 +       struct super_block *sb;
9290 +       struct au_branch *br;
9291 +       struct au_dyaop *dyaop;
9292 +
9293 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
9294 +       IiMustWriteLock(inode);
9295 +
9296 +       sb = inode->i_sb;
9297 +       br = au_sbr(sb, bindex);
9298 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
9299 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
9300 +       err = PTR_ERR(dyaop);
9301 +       if (IS_ERR(dyaop))
9302 +               /* unnecessary to call dy_fput() */
9303 +               goto out;
9304 +
9305 +       err = 0;
9306 +       inode->i_mapping->a_ops = &dyaop->da_op;
9307 +
9308 +out:
9309 +       return err;
9310 +}
9311 +
9312 +/*
9313 + * Is it safe to replace a_ops during the inode/file is in operation?
9314 + * Yes, I hope so.
9315 + */
9316 +int au_dy_irefresh(struct inode *inode)
9317 +{
9318 +       int err;
9319 +       aufs_bindex_t bstart;
9320 +       struct inode *h_inode;
9321 +
9322 +       err = 0;
9323 +       if (S_ISREG(inode->i_mode)) {
9324 +               bstart = au_ibstart(inode);
9325 +               h_inode = au_h_iptr(inode, bstart);
9326 +               err = au_dy_iaop(inode, bstart, h_inode);
9327 +       }
9328 +       return err;
9329 +}
9330 +
9331 +void au_dy_arefresh(int do_dx)
9332 +{
9333 +       struct au_splhead *spl;
9334 +       struct list_head *head;
9335 +       struct au_dykey *key;
9336 +
9337 +       spl = dynop + AuDy_AOP;
9338 +       head = &spl->head;
9339 +       spin_lock(&spl->spin);
9340 +       list_for_each_entry(key, head, dk_list)
9341 +               dy_adx((void *)key, do_dx);
9342 +       spin_unlock(&spl->spin);
9343 +}
9344 +
9345 +/* ---------------------------------------------------------------------- */
9346 +
9347 +void __init au_dy_init(void)
9348 +{
9349 +       int i;
9350 +
9351 +       /* make sure that 'struct au_dykey *' can be any type */
9352 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
9353 +
9354 +       for (i = 0; i < AuDyLast; i++)
9355 +               au_spl_init(dynop + i);
9356 +}
9357 +
9358 +void au_dy_fin(void)
9359 +{
9360 +       int i;
9361 +
9362 +       for (i = 0; i < AuDyLast; i++)
9363 +               WARN_ON(!list_empty(&dynop[i].head));
9364 +}
9365 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
9366 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
9367 +++ linux/fs/aufs/dynop.h       2013-07-06 13:20:47.740198107 +0200
9368 @@ -0,0 +1,76 @@
9369 +/*
9370 + * Copyright (C) 2010-2013 Junjiro R. Okajima
9371 + *
9372 + * This program, aufs is free software; you can redistribute it and/or modify
9373 + * it under the terms of the GNU General Public License as published by
9374 + * the Free Software Foundation; either version 2 of the License, or
9375 + * (at your option) any later version.
9376 + *
9377 + * This program is distributed in the hope that it will be useful,
9378 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9379 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9380 + * GNU General Public License for more details.
9381 + *
9382 + * You should have received a copy of the GNU General Public License
9383 + * along with this program; if not, write to the Free Software
9384 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9385 + */
9386 +
9387 +/*
9388 + * dynamically customizable operations (for regular files only)
9389 + */
9390 +
9391 +#ifndef __AUFS_DYNOP_H__
9392 +#define __AUFS_DYNOP_H__
9393 +
9394 +#ifdef __KERNEL__
9395 +
9396 +#include "inode.h"
9397 +
9398 +enum {AuDy_AOP, AuDyLast};
9399 +
9400 +struct au_dynop {
9401 +       int                                             dy_type;
9402 +       union {
9403 +               const void                              *dy_hop;
9404 +               const struct address_space_operations   *dy_haop;
9405 +       };
9406 +};
9407 +
9408 +struct au_dykey {
9409 +       union {
9410 +               struct list_head        dk_list;
9411 +               struct rcu_head         dk_rcu;
9412 +       };
9413 +       struct au_dynop         dk_op;
9414 +
9415 +       /*
9416 +        * during I am in the branch local array, kref is gotten. when the
9417 +        * branch is removed, kref is put.
9418 +        */
9419 +       struct kref             dk_kref;
9420 +};
9421 +
9422 +/* stop unioning since their sizes are very different from each other */
9423 +struct au_dyaop {
9424 +       struct au_dykey                 da_key;
9425 +       struct address_space_operations da_op; /* not const */
9426 +       int (*da_get_xip_mem)(struct address_space *, pgoff_t, int,
9427 +                             void **, unsigned long *);
9428 +};
9429 +
9430 +/* ---------------------------------------------------------------------- */
9431 +
9432 +/* dynop.c */
9433 +struct au_branch;
9434 +void au_dy_put(struct au_dykey *key);
9435 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
9436 +               struct inode *h_inode);
9437 +int au_dy_irefresh(struct inode *inode);
9438 +void au_dy_arefresh(int do_dio);
9439 +
9440 +void __init au_dy_init(void);
9441 +void au_dy_fin(void);
9442 +
9443 +#endif /* __KERNEL__ */
9444 +#endif /* __AUFS_DYNOP_H__ */
9445 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
9446 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
9447 +++ linux/fs/aufs/export.c      2013-07-30 22:42:55.839613269 +0200
9448 @@ -0,0 +1,826 @@
9449 +/*
9450 + * Copyright (C) 2005-2013 Junjiro R. Okajima
9451 + *
9452 + * This program, aufs is free software; you can redistribute it and/or modify
9453 + * it under the terms of the GNU General Public License as published by
9454 + * the Free Software Foundation; either version 2 of the License, or
9455 + * (at your option) any later version.
9456 + *
9457 + * This program is distributed in the hope that it will be useful,
9458 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9459 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9460 + * GNU General Public License for more details.
9461 + *
9462 + * You should have received a copy of the GNU General Public License
9463 + * along with this program; if not, write to the Free Software
9464 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
9465 + */
9466 +
9467 +/*
9468 + * export via nfs
9469 + */
9470 +
9471 +#include <linux/exportfs.h>
9472 +#include <linux/fs_struct.h>
9473 +#include <linux/namei.h>
9474 +#include <linux/nsproxy.h>
9475 +#include <linux/random.h>
9476 +#include <linux/writeback.h>
9477 +#include "../fs/mount.h"
9478 +#include "aufs.h"
9479 +
9480 +union conv {
9481 +#ifdef CONFIG_AUFS_INO_T_64
9482 +       __u32 a[2];
9483 +#else
9484 +       __u32 a[1];
9485 +#endif
9486 +       ino_t ino;
9487 +};
9488 +
9489 +static ino_t decode_ino(__u32 *a)
9490 +{
9491 +       union conv u;
9492 +
9493 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
9494 +       u.a[0] = a[0];
9495 +#ifdef CONFIG_AUFS_INO_T_64
9496 +       u.a[1] = a[1];
9497 +#endif
9498 +       return u.ino;
9499 +}
9500 +
9501 +static void encode_ino(__u32 *a, ino_t ino)
9502 +{
9503 +       union conv u;
9504 +
9505 +       u.ino = ino;
9506 +       a[0] = u.a[0];
9507 +#ifdef CONFIG_AUFS_INO_T_64
9508 +       a[1] = u.a[1];
9509 +#endif
9510 +}
9511 +
9512 +/* NFS file handle */
9513 +enum {
9514 +       Fh_br_id,
9515 +       Fh_sigen,
9516 +#ifdef CONFIG_AUFS_INO_T_64
9517 +       /* support 64bit inode number */
9518 +       Fh_ino1,
9519 +       Fh_ino2,
9520 +       Fh_dir_ino1,
9521 +       Fh_dir_ino2,
9522 +#else
9523 +       Fh_ino1,
9524 +       Fh_dir_ino1,
9525 +#endif
9526 +       Fh_igen,
9527 +       Fh_h_type,
9528 +       Fh_tail,
9529 +
9530 +       Fh_ino = Fh_ino1,
9531 +       Fh_dir_ino = Fh_dir_ino1
9532 +};
9533 +
9534 +static int au_test_anon(struct dentry *dentry)
9535 +{
9536 +       /* note: read d_flags without d_lock */
9537 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
9538 +}
9539 +
9540 +int au_test_nfsd(void)
9541 +{
9542 +       int ret;
9543 +       struct task_struct *tsk = current;
9544 +       char comm[sizeof(tsk->comm)];
9545 +
9546 +       ret = 0;
9547 +       if (tsk->flags & PF_KTHREAD) {
9548 +               get_task_comm(comm, tsk);
9549 +               ret = !strcmp(comm, "nfsd");
9550 +       }
9551 +
9552 +       return ret;
9553 +}
9554 +
9555 +/* ---------------------------------------------------------------------- */
9556 +/* inode generation external table */
9557 +
9558 +void au_xigen_inc(struct inode *inode)
9559 +{
9560 +       loff_t pos;
9561 +       ssize_t sz;
9562 +       __u32 igen;
9563 +       struct super_block *sb;
9564 +       struct au_sbinfo *sbinfo;
9565 +
9566 +       sb = inode->i_sb;
9567 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
9568 +
9569 +       sbinfo = au_sbi(sb);
9570 +       pos = inode->i_ino;
9571 +       pos *= sizeof(igen);
9572 +       igen = inode->i_generation + 1;
9573 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
9574 +                        sizeof(igen), &pos);
9575 +       if (sz == sizeof(igen))
9576 +               return; /* success */
9577 +
9578 +       if (unlikely(sz >= 0))
9579 +               AuIOErr("xigen error (%zd)\n", sz);
9580 +}
9581 +
9582 +int au_xigen_new(struct inode *inode)
9583 +{
9584 +       int err;
9585 +       loff_t pos;
9586 +       ssize_t sz;
9587 +       struct super_block *sb;
9588 +       struct au_sbinfo *sbinfo;
9589 +       struct file *file;
9590 +
9591 +       err = 0;
9592 +       /* todo: dirty, at mount time */
9593 +       if (inode->i_ino == AUFS_ROOT_INO)
9594 +               goto out;
9595 +       sb = inode->i_sb;
9596 +       SiMustAnyLock(sb);
9597 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
9598 +               goto out;
9599 +
9600 +       err = -EFBIG;
9601 +       pos = inode->i_ino;
9602 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
9603 +               AuIOErr1("too large i%lld\n", pos);
9604 +               goto out;
9605 +       }
9606 +       pos *= sizeof(inode->i_generation);
9607 +
9608 +       err = 0;
9609 +       sbinfo = au_sbi(sb);
9610 +       file = sbinfo->si_xigen;
9611 +       BUG_ON(!file);
9612 +
9613 +       if (vfsub_f_size_read(file)
9614 +           < pos + sizeof(inode->i_generation)) {
9615 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
9616 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
9617 +                                sizeof(inode->i_generation), &pos);
9618 +       } else
9619 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
9620 +                               sizeof(inode->i_generation), &pos);
9621 +       if (sz == sizeof(inode->i_generation))
9622 +               goto out; /* success */
9623 +
9624 +       err = sz;
9625 +       if (unlikely(sz >= 0)) {
9626 +               err = -EIO;
9627 +               AuIOErr("xigen error (%zd)\n", sz);
9628 +       }
9629 +
9630 +out:
9631 +       return err;
9632 +}
9633 +
9634 +int au_xigen_set(struct super_block *sb, struct file *base)
9635 +{
9636 +       int err;
9637 +       struct au_sbinfo *sbinfo;
9638 +       struct file *file;
9639 +
9640 +       SiMustWriteLock(sb);
9641 +
9642 +       sbinfo = au_sbi(sb);
9643 +       file = au_xino_create2(base, sbinfo->si_xigen);
9644 +       err = PTR_ERR(file);
9645 +       if (IS_ERR(file))
9646 +               goto out;
9647 +       err = 0;
9648 +       if (sbinfo->si_xigen)
9649 +               fput(sbinfo->si_xigen);
9650 +       sbinfo->si_xigen = file;
9651 +
9652 +out:
9653 +       return err;
9654 +}
9655 +
9656 +void au_xigen_clr(struct super_block *sb)
9657 +{
9658 +       struct au_sbinfo *sbinfo;
9659 +
9660 +       SiMustWriteLock(sb);
9661 +
9662 +       sbinfo = au_sbi(sb);
9663 +       if (sbinfo->si_xigen) {
9664 +               fput(sbinfo->si_xigen);
9665 +               sbinfo->si_xigen = NULL;
9666 +       }
9667 +}
9668 +
9669 +/* ---------------------------------------------------------------------- */
9670 +
9671 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
9672 +                                   ino_t dir_ino)
9673 +{
9674 +       struct dentry *dentry, *d;
9675 +       struct inode *inode;
9676 +       unsigned int sigen;
9677 +
9678 +       dentry = NULL;
9679 +       inode = ilookup(sb, ino);
9680 +       if (!inode)
9681 +               goto out;
9682 +
9683 +       dentry = ERR_PTR(-ESTALE);
9684 +       sigen = au_sigen(sb);
9685 +       if (unlikely(is_bad_inode(inode)
9686 +                    || IS_DEADDIR(inode)
9687 +                    || sigen != au_iigen(inode, NULL)))
9688 +               goto out_iput;
9689 +
9690 +       dentry = NULL;
9691 +       if (!dir_ino || S_ISDIR(inode->i_mode))
9692 +               dentry = d_find_alias(inode);
9693 +       else {
9694 +               spin_lock(&inode->i_lock);
9695 +               hlist_for_each_entry(d, &inode->i_dentry, d_alias) {
9696 +                       spin_lock(&d->d_lock);
9697 +                       if (!au_test_anon(d)
9698 +                           && d->d_parent->d_inode->i_ino == dir_ino) {
9699 +                               dentry = dget_dlock(d);
9700 +                               spin_unlock(&d->d_lock);
9701 +                               break;
9702 +                       }
9703 +                       spin_unlock(&d->d_lock);
9704 +               }
9705 +               spin_unlock(&inode->i_lock);
9706 +       }
9707 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
9708 +               /* need to refresh */
9709 +               dput(dentry);
9710 +               dentry = NULL;
9711 +       }
9712 +
9713 +out_iput:
9714 +       iput(inode);
9715 +out:
9716 +       AuTraceErrPtr(dentry);
9717 +       return dentry;
9718 +}
9719 +
9720 +/* ---------------------------------------------------------------------- */
9721 +
9722 +/* todo: dirty? */
9723 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
9724 +
9725 +struct au_compare_mnt_args {
9726 +       /* input */
9727 +       struct super_block *sb;
9728 +
9729 +       /* output */
9730 +       struct vfsmount *mnt;
9731 +};
9732 +
9733 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
9734 +{
9735 +       struct au_compare_mnt_args *a = arg;
9736 +
9737 +       if (mnt->mnt_sb != a->sb)
9738 +               return 0;
9739 +       a->mnt = mntget(mnt);
9740 +       return 1;
9741 +}
9742 +
9743 +static struct vfsmount *au_mnt_get(struct super_block *sb)
9744 +{
9745 +       int err;
9746 +       struct path root;
9747 +       struct au_compare_mnt_args args = {
9748 +               .sb = sb
9749 +       };
9750 +
9751 +       get_fs_root(current->fs, &root);
9752 +       br_read_lock(&vfsmount_lock);
9753 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
9754 +       br_read_unlock(&vfsmount_lock);
9755 +       path_put(&root);
9756 +       AuDebugOn(!err);
9757 +       AuDebugOn(!args.mnt);
9758 +       return args.mnt;
9759 +}
9760 +
9761 +struct au_nfsd_si_lock {
9762 +       unsigned int sigen;
9763 +       aufs_bindex_t bindex, br_id;
9764 +       unsigned char force_lock;
9765 +};
9766 +
9767 +static int si_nfsd_read_lock(struct super_block *sb,
9768 +                            struct au_nfsd_si_lock *nsi_lock)
9769 +{
9770 +       int err;
9771 +       aufs_bindex_t bindex;
9772 +
9773 +       si_read_lock(sb, AuLock_FLUSH);
9774 +
9775 +       /* branch id may be wrapped around */
9776 +       err = 0;
9777 +       bindex = au_br_index(sb, nsi_lock->br_id);
9778 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
9779 +               goto out; /* success */
9780 +
9781 +       err = -ESTALE;
9782 +       bindex = -1;
9783 +       if (!nsi_lock->force_lock)
9784 +               si_read_unlock(sb);
9785 +
9786 +out:
9787 +       nsi_lock->bindex = bindex;
9788 +       return err;
9789 +}
9790 +
9791 +struct find_name_by_ino {
9792 +       int called, found;
9793 +       ino_t ino;
9794 +       char *name;
9795 +       int namelen;
9796 +};
9797 +
9798 +static int
9799 +find_name_by_ino(void *arg, const char *name, int namelen, loff_t offset,
9800 +                u64 ino, unsigned int d_type)
9801 +{
9802 +       struct find_name_by_ino *a = arg;
9803 +
9804 +       a->called++;
9805 +       if (a->ino != ino)
9806 +               return 0;
9807 +
9808 +       memcpy(a->name, name, namelen);
9809 +       a->namelen = namelen;
9810 +       a->found = 1;
9811 +       return 1;
9812 +}
9813 +
9814 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
9815 +                                    struct au_nfsd_si_lock *nsi_lock)
9816 +{
9817 +       struct dentry *dentry, *parent;
9818 +       struct file *file;
9819 +       struct inode *dir;
9820 +       struct find_name_by_ino arg;
9821 +       int err;
9822 +
9823 +       parent = path->dentry;
9824 +       if (nsi_lock)
9825 +               si_read_unlock(parent->d_sb);
9826 +       file = vfsub_dentry_open(path, au_dir_roflags);
9827 +       dentry = (void *)file;
9828 +       if (IS_ERR(file))
9829 +               goto out;
9830 +
9831 +       dentry = ERR_PTR(-ENOMEM);
9832 +       arg.name = (void *)__get_free_page(GFP_NOFS);
9833 +       if (unlikely(!arg.name))
9834 +               goto out_file;
9835 +       arg.ino = ino;
9836 +       arg.found = 0;
9837 +       do {
9838 +               arg.called = 0;
9839 +               /* smp_mb(); */
9840 +               err = vfsub_readdir(file, find_name_by_ino, &arg);
9841 +       } while (!err && !arg.found && arg.called);
9842 +       dentry = ERR_PTR(err);
9843 +       if (unlikely(err))
9844 +               goto out_name;
9845 +       /* instead of ENOENT */
9846 +       dentry = ERR_PTR(-ESTALE);
9847 +       if (!arg.found)
9848 +               goto out_name;
9849 +
9850 +       /* do not call vfsub_lkup_one() */
9851 +       dir = parent->d_inode;
9852 +       mutex_lock(&dir->i_mutex);
9853 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
9854 +       mutex_unlock(&dir->i_mutex);
9855 +       AuTraceErrPtr(dentry);
9856 +       if (IS_ERR(dentry))
9857 +               goto out_name;
9858 +       AuDebugOn(au_test_anon(dentry));
9859 +       if (unlikely(!dentry->d_inode)) {
9860 +               dput(dentry);
9861 +               dentry = ERR_PTR(-ENOENT);
9862 +       }
9863 +
9864 +out_name:
9865 +       free_page((unsigned long)arg.name);
9866 +out_file:
9867 +       fput(file);
9868 +out:
9869 +       if (unlikely(nsi_lock
9870 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
9871 +               if (!IS_ERR(dentry)) {
9872 +                       dput(dentry);
9873 +                       dentry = ERR_PTR(-ESTALE);
9874 +               }
9875 +       AuTraceErrPtr(dentry);
9876 +       return dentry;
9877 +}
9878 +
9879 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
9880 +                                       ino_t dir_ino,
9881 +                                       struct au_nfsd_si_lock *nsi_lock)
9882 +{
9883 +       struct dentry *dentry;
9884 +       struct path path;
9885 +
9886 +       if (dir_ino != AUFS_ROOT_INO) {
9887 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
9888 +               dentry = path.dentry;
9889 +               if (!path.dentry || IS_ERR(path.dentry))
9890 +                       goto out;
9891 +               AuDebugOn(au_test_anon(path.dentry));
9892 +       } else
9893 +               path.dentry = dget(sb->s_root);
9894 +
9895 +       path.mnt = au_mnt_get(sb);
9896 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
9897 +       path_put(&path);
9898 +
9899 +out:
9900 +       AuTraceErrPtr(dentry);
9901 +       return dentry;
9902 +}
9903 +
9904 +/* ---------------------------------------------------------------------- */
9905 +
9906 +static int h_acceptable(void *expv, struct dentry *dentry)
9907 +{
9908 +       return 1;
9909 +}
9910 +
9911 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
9912 +                          char *buf, int len, struct super_block *sb)
9913 +{
9914 +       char *p;
9915 +       int n;
9916 +       struct path path;
9917 +
9918 +       p = d_path(h_rootpath, buf, len);
9919 +       if (IS_ERR(p))
9920 +               goto out;
9921 +       n = strlen(p);
9922 +
9923 +       path.mnt = h_rootpath->mnt;
9924 +       path.dentry = h_parent;
9925 +       p = d_path(&path, buf, len);
9926 +       if (IS_ERR(p))
9927 +               goto out;
9928 +       if (n != 1)
9929 +               p += n;
9930 +
9931 +       path.mnt = au_mnt_get(sb);
9932 +       path.dentry = sb->s_root;
9933 +       p = d_path(&path, buf, len - strlen(p));
9934 +       mntput(path.mnt);
9935 +       if (IS_ERR(p))
9936 +               goto out;
9937 +       if (n != 1)
9938 +               p[strlen(p)] = '/';
9939 +
9940 +out:
9941 +       AuTraceErrPtr(p);
9942 +       return p;
9943 +}
9944 +
9945 +static
9946 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
9947 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
9948 +{
9949 +       struct dentry *dentry, *h_parent, *root;
9950 +       struct super_block *h_sb;
9951 +       char *pathname, *p;
9952 +       struct vfsmount *h_mnt;
9953 +       struct au_branch *br;
9954 +       int err;
9955 +       struct path path;
9956 +
9957 +       br = au_sbr(sb, nsi_lock->bindex);
9958 +       h_mnt = au_br_mnt(br);
9959 +       h_sb = h_mnt->mnt_sb;
9960 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
9961 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
9962 +                                     fh_len - Fh_tail, fh[Fh_h_type],
9963 +                                     h_acceptable, /*context*/NULL);
9964 +       dentry = h_parent;
9965 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
9966 +               AuWarn1("%s decode_fh failed, %ld\n",
9967 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
9968 +               goto out;
9969 +       }
9970 +       dentry = NULL;
9971 +       if (unlikely(au_test_anon(h_parent))) {
9972 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
9973 +                       au_sbtype(h_sb));
9974 +               goto out_h_parent;
9975 +       }
9976 +
9977 +       dentry = ERR_PTR(-ENOMEM);
9978 +       pathname = (void *)__get_free_page(GFP_NOFS);
9979 +       if (unlikely(!pathname))
9980 +               goto out_h_parent;
9981 +
9982 +       root = sb->s_root;
9983 +       path.mnt = h_mnt;
9984 +       di_read_lock_parent(root, !AuLock_IR);
9985 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
9986 +       di_read_unlock(root, !AuLock_IR);
9987 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
9988 +       dentry = (void *)p;
9989 +       if (IS_ERR(p))
9990 +               goto out_pathname;
9991 +
9992 +       si_read_unlock(sb);
9993 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
9994 +       dentry = ERR_PTR(err);
9995 +       if (unlikely(err))
9996 +               goto out_relock;
9997 +
9998 +       dentry = ERR_PTR(-ENOENT);
9999 +       AuDebugOn(au_test_anon(path.dentry));
10000 +       if (unlikely(!path.dentry->d_inode))
10001 +               goto out_path;
10002 +
10003 +       if (ino != path.dentry->d_inode->i_ino)
10004 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
10005 +       else
10006 +               dentry = dget(path.dentry);
10007 +
10008 +out_path:
10009 +       path_put(&path);
10010 +out_relock:
10011 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
10012 +               if (!IS_ERR(dentry)) {
10013 +                       dput(dentry);
10014 +                       dentry = ERR_PTR(-ESTALE);
10015 +               }
10016 +out_pathname:
10017 +       free_page((unsigned long)pathname);
10018 +out_h_parent:
10019 +       dput(h_parent);
10020 +out:
10021 +       AuTraceErrPtr(dentry);
10022 +       return dentry;
10023 +}
10024 +
10025 +/* ---------------------------------------------------------------------- */
10026 +
10027 +static struct dentry *
10028 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
10029 +                 int fh_type)
10030 +{
10031 +       struct dentry *dentry;
10032 +       __u32 *fh = fid->raw;
10033 +       struct au_branch *br;
10034 +       ino_t ino, dir_ino;
10035 +       struct au_nfsd_si_lock nsi_lock = {
10036 +               .force_lock     = 0
10037 +       };
10038 +
10039 +       dentry = ERR_PTR(-ESTALE);
10040 +       /* it should never happen, but the file handle is unreliable */
10041 +       if (unlikely(fh_len < Fh_tail))
10042 +               goto out;
10043 +       nsi_lock.sigen = fh[Fh_sigen];
10044 +       nsi_lock.br_id = fh[Fh_br_id];
10045 +
10046 +       /* branch id may be wrapped around */
10047 +       br = NULL;
10048 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
10049 +               goto out;
10050 +       nsi_lock.force_lock = 1;
10051 +
10052 +       /* is this inode still cached? */
10053 +       ino = decode_ino(fh + Fh_ino);
10054 +       /* it should never happen */
10055 +       if (unlikely(ino == AUFS_ROOT_INO))
10056 +               goto out;
10057 +
10058 +       dir_ino = decode_ino(fh + Fh_dir_ino);
10059 +       dentry = decode_by_ino(sb, ino, dir_ino);
10060 +       if (IS_ERR(dentry))
10061 +               goto out_unlock;
10062 +       if (dentry)
10063 +               goto accept;
10064 +
10065 +       /* is the parent dir cached? */
10066 +       br = au_sbr(sb, nsi_lock.bindex);
10067 +       atomic_inc(&br->br_count);
10068 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
10069 +       if (IS_ERR(dentry))
10070 +               goto out_unlock;
10071 +       if (dentry)
10072 +               goto accept;
10073 +
10074 +       /* lookup path */
10075 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
10076 +       if (IS_ERR(dentry))
10077 +               goto out_unlock;
10078 +       if (unlikely(!dentry))
10079 +               /* todo?: make it ESTALE */
10080 +               goto out_unlock;
10081 +
10082 +accept:
10083 +       if (!au_digen_test(dentry, au_sigen(sb))
10084 +           && dentry->d_inode->i_generation == fh[Fh_igen])
10085 +               goto out_unlock; /* success */
10086 +
10087 +       dput(dentry);
10088 +       dentry = ERR_PTR(-ESTALE);
10089 +out_unlock:
10090 +       if (br)
10091 +               atomic_dec(&br->br_count);
10092 +       si_read_unlock(sb);
10093 +out:
10094 +       AuTraceErrPtr(dentry);
10095 +       return dentry;
10096 +}
10097 +
10098 +#if 0 /* reserved for future use */
10099 +/* support subtreecheck option */
10100 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
10101 +                                       int fh_len, int fh_type)
10102 +{
10103 +       struct dentry *parent;
10104 +       __u32 *fh = fid->raw;
10105 +       ino_t dir_ino;
10106 +
10107 +       dir_ino = decode_ino(fh + Fh_dir_ino);
10108 +       parent = decode_by_ino(sb, dir_ino, 0);
10109 +       if (IS_ERR(parent))
10110 +               goto out;
10111 +       if (!parent)
10112 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
10113 +                                       dir_ino, fh, fh_len);
10114 +
10115 +out:
10116 +       AuTraceErrPtr(parent);
10117 +       return parent;
10118 +}
10119 +#endif
10120 +
10121 +/* ---------------------------------------------------------------------- */
10122 +
10123 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
10124 +                         struct inode *dir)
10125 +{
10126 +       int err;
10127 +       aufs_bindex_t bindex;
10128 +       struct super_block *sb, *h_sb;
10129 +       struct dentry *dentry, *parent, *h_parent;
10130 +       struct inode *h_dir;
10131 +       struct au_branch *br;
10132 +
10133 +       err = -ENOSPC;
10134 +       if (unlikely(*max_len <= Fh_tail)) {
10135 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
10136 +               goto out;
10137 +       }
10138 +
10139 +       err = FILEID_ROOT;
10140 +       if (inode->i_ino == AUFS_ROOT_INO) {
10141 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
10142 +               goto out;
10143 +       }
10144 +
10145 +       h_parent = NULL;
10146 +       sb = inode->i_sb;
10147 +       err = si_read_lock(sb, AuLock_FLUSH);
10148 +       if (unlikely(err))
10149 +               goto out;
10150 +
10151 +#ifdef CONFIG_AUFS_DEBUG
10152 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
10153 +               AuWarn1("NFS-exporting requires xino\n");
10154 +#endif
10155 +       err = -EIO;
10156 +       parent = NULL;
10157 +       ii_read_lock_child(inode);
10158 +       bindex = au_ibstart(inode);
10159 +       if (!dir) {
10160 +               dentry = d_find_alias(inode);
10161 +               if (unlikely(!dentry))
10162 +                       goto out_unlock;
10163 +               AuDebugOn(au_test_anon(dentry));
10164 +               parent = dget_parent(dentry);
10165 +               dput(dentry);
10166 +               if (unlikely(!parent))
10167 +                       goto out_unlock;
10168 +               dir = parent->d_inode;
10169 +       }
10170 +
10171 +       ii_read_lock_parent(dir);
10172 +       h_dir = au_h_iptr(dir, bindex);
10173 +       ii_read_unlock(dir);
10174 +       if (unlikely(!h_dir))
10175 +               goto out_parent;
10176 +       h_parent = d_find_alias(h_dir);
10177 +       if (unlikely(!h_parent))
10178 +               goto out_hparent;
10179 +
10180 +       err = -EPERM;
10181 +       br = au_sbr(sb, bindex);
10182 +       h_sb = au_br_sb(br);
10183 +       if (unlikely(!h_sb->s_export_op)) {
10184 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
10185 +               goto out_hparent;
10186 +       }
10187 +
10188 +       fh[Fh_br_id] = br->br_id;
10189 +       fh[Fh_sigen] = au_sigen(sb);
10190 +       encode_ino(fh + Fh_ino, inode->i_ino);
10191 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
10192 +       fh[Fh_igen] = inode->i_generation;
10193 +
10194 +       *max_len -= Fh_tail;
10195 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
10196 +                                          max_len,
10197 +                                          /*connectable or subtreecheck*/0);
10198 +       err = fh[Fh_h_type];
10199 +       *max_len += Fh_tail;
10200 +       /* todo: macros? */
10201 +       if (err != FILEID_INVALID)
10202 +               err = 99;
10203 +       else
10204 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
10205 +
10206 +out_hparent:
10207 +       dput(h_parent);
10208 +out_parent:
10209 +       dput(parent);
10210 +out_unlock:
10211 +       ii_read_unlock(inode);
10212 +       si_read_unlock(sb);
10213 +out:
10214 +       if (unlikely(err < 0))
10215 +               err = FILEID_INVALID;
10216 +       return err;
10217 +}
10218 +
10219 +/* ---------------------------------------------------------------------- */
10220 +
10221 +static int aufs_commit_metadata(struct inode *inode)
10222 +{
10223 +       int err;
10224 +       aufs_bindex_t bindex;
10225 +       struct super_block *sb;
10226 +       struct inode *h_inode;
10227 +       int (*f)(struct inode *inode);
10228 +
10229 +       sb = inode->i_sb;
10230 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
10231 +       ii_write_lock_child(inode);
10232 +       bindex = au_ibstart(inode);
10233 +       AuDebugOn(bindex < 0);
10234 +       h_inode = au_h_iptr(inode, bindex);
10235 +
10236 +       f = h_inode->i_sb->s_export_op->commit_metadata;
10237 +       if (f)
10238 +               err = f(h_inode);
10239 +       else {
10240 +               struct writeback_control wbc = {
10241 +                       .sync_mode      = WB_SYNC_ALL,
10242 +                       .nr_to_write    = 0 /* metadata only */
10243 +               };
10244 +
10245 +               err = sync_inode(h_inode, &wbc);
10246 +       }
10247 +
10248 +       au_cpup_attr_timesizes(inode);
10249 +       ii_write_unlock(inode);
10250 +       si_read_unlock(sb);
10251 +       return err;
10252 +}
10253 +
10254 +/* ---------------------------------------------------------------------- */
10255 +
10256 +static struct export_operations aufs_export_op = {
10257 +       .fh_to_dentry           = aufs_fh_to_dentry,
10258 +       /* .fh_to_parent        = aufs_fh_to_parent, */
10259 +       .encode_fh              = aufs_encode_fh,
10260 +       .commit_metadata        = aufs_commit_metadata
10261 +};
10262 +
10263 +void au_export_init(struct super_block *sb)
10264 +{
10265 +       struct au_sbinfo *sbinfo;
10266 +       __u32 u;
10267 +
10268 +       sb->s_export_op = &aufs_export_op;
10269 +       sbinfo = au_sbi(sb);
10270 +       sbinfo->si_xigen = NULL;
10271 +       get_random_bytes(&u, sizeof(u));
10272 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
10273 +       atomic_set(&sbinfo->si_xigen_next, u);
10274 +}
10275 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
10276 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
10277 +++ linux/fs/aufs/file.c        2013-08-23 23:59:39.634916914 +0200
10278 @@ -0,0 +1,708 @@
10279 +/*
10280 + * Copyright (C) 2005-2013 Junjiro R. Okajima
10281 + *
10282 + * This program, aufs is free software; you can redistribute it and/or modify
10283 + * it under the terms of the GNU General Public License as published by
10284 + * the Free Software Foundation; either version 2 of the License, or
10285 + * (at your option) any later version.
10286 + *
10287 + * This program is distributed in the hope that it will be useful,
10288 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10289 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10290 + * GNU General Public License for more details.
10291 + *
10292 + * You should have received a copy of the GNU General Public License
10293 + * along with this program; if not, write to the Free Software
10294 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
10295 + */
10296 +
10297 +/*
10298 + * handling file/dir, and address_space operation
10299 + */
10300 +
10301 +#ifdef CONFIG_AUFS_DEBUG
10302 +#include <linux/migrate.h>
10303 +#endif
10304 +#include <linux/pagemap.h>
10305 +#include "aufs.h"
10306 +
10307 +/* drop flags for writing */
10308 +unsigned int au_file_roflags(unsigned int flags)
10309 +{
10310 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
10311 +       flags |= O_RDONLY | O_NOATIME;
10312 +       return flags;
10313 +}
10314 +
10315 +/* common functions to regular file and dir */
10316 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
10317 +                      struct file *file)
10318 +{
10319 +       struct file *h_file;
10320 +       struct dentry *h_dentry;
10321 +       struct inode *h_inode;
10322 +       struct super_block *sb;
10323 +       struct au_branch *br;
10324 +       struct path h_path;
10325 +       int err, exec_flag;
10326 +
10327 +       /* a race condition can happen between open and unlink/rmdir */
10328 +       h_file = ERR_PTR(-ENOENT);
10329 +       h_dentry = au_h_dptr(dentry, bindex);
10330 +       if (au_test_nfsd() && !h_dentry)
10331 +               goto out;
10332 +       h_inode = h_dentry->d_inode;
10333 +       if (au_test_nfsd() && !h_inode)
10334 +               goto out;
10335 +       spin_lock(&h_dentry->d_lock);
10336 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
10337 +               || !h_inode
10338 +               /* || !dentry->d_inode->i_nlink */
10339 +               ;
10340 +       spin_unlock(&h_dentry->d_lock);
10341 +       if (unlikely(err))
10342 +               goto out;
10343 +
10344 +       sb = dentry->d_sb;
10345 +       br = au_sbr(sb, bindex);
10346 +       h_file = ERR_PTR(-EACCES);
10347 +       exec_flag = flags & __FMODE_EXEC;
10348 +       if (exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC))
10349 +               goto out;
10350 +
10351 +       /* drop flags for writing */
10352 +       if (au_test_ro(sb, bindex, dentry->d_inode))
10353 +               flags = au_file_roflags(flags);
10354 +       flags &= ~O_CREAT;
10355 +       atomic_inc(&br->br_count);
10356 +       h_path.dentry = h_dentry;
10357 +       h_path.mnt = au_br_mnt(br);
10358 +       if (!au_special_file(h_inode->i_mode))
10359 +               h_file = vfsub_dentry_open(&h_path, flags);
10360 +       else {
10361 +               /* this block depends upon the configuration */
10362 +               di_read_unlock(dentry, AuLock_IR);
10363 +               fi_write_unlock(file);
10364 +               si_read_unlock(sb);
10365 +               h_file = vfsub_dentry_open(&h_path, flags);
10366 +               si_noflush_read_lock(sb);
10367 +               fi_write_lock(file);
10368 +               di_read_lock_child(dentry, AuLock_IR);
10369 +       }
10370 +       if (IS_ERR(h_file))
10371 +               goto out_br;
10372 +
10373 +       if (exec_flag) {
10374 +               err = deny_write_access(h_file);
10375 +               if (unlikely(err)) {
10376 +                       fput(h_file);
10377 +                       h_file = ERR_PTR(err);
10378 +                       goto out_br;
10379 +               }
10380 +       }
10381 +       fsnotify_open(h_file);
10382 +       goto out; /* success */
10383 +
10384 +out_br:
10385 +       atomic_dec(&br->br_count);
10386 +out:
10387 +       return h_file;
10388 +}
10389 +
10390 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
10391 +              struct au_fidir *fidir)
10392 +{
10393 +       int err;
10394 +       struct dentry *dentry;
10395 +
10396 +       err = au_finfo_init(file, fidir);
10397 +       if (unlikely(err))
10398 +               goto out;
10399 +
10400 +       dentry = file->f_dentry;
10401 +       di_read_lock_child(dentry, AuLock_IR);
10402 +       err = open(file, vfsub_file_flags(file));
10403 +       di_read_unlock(dentry, AuLock_IR);
10404 +
10405 +       fi_write_unlock(file);
10406 +       if (unlikely(err)) {
10407 +               au_fi(file)->fi_hdir = NULL;
10408 +               au_finfo_fin(file);
10409 +       }
10410 +
10411 +out:
10412 +       return err;
10413 +}
10414 +
10415 +int au_reopen_nondir(struct file *file)
10416 +{
10417 +       int err;
10418 +       aufs_bindex_t bstart;
10419 +       struct dentry *dentry;
10420 +       struct file *h_file, *h_file_tmp;
10421 +
10422 +       dentry = file->f_dentry;
10423 +       AuDebugOn(au_special_file(dentry->d_inode->i_mode));
10424 +       bstart = au_dbstart(dentry);
10425 +       h_file_tmp = NULL;
10426 +       if (au_fbstart(file) == bstart) {
10427 +               h_file = au_hf_top(file);
10428 +               if (file->f_mode == h_file->f_mode)
10429 +                       return 0; /* success */
10430 +               h_file_tmp = h_file;
10431 +               get_file(h_file_tmp);
10432 +               au_set_h_fptr(file, bstart, NULL);
10433 +       }
10434 +       AuDebugOn(au_fi(file)->fi_hdir);
10435 +       /*
10436 +        * it can happen
10437 +        * file exists on both of rw and ro
10438 +        * open --> dbstart and fbstart are both 0
10439 +        * prepend a branch as rw, "rw" become ro
10440 +        * remove rw/file
10441 +        * delete the top branch, "rw" becomes rw again
10442 +        *      --> dbstart is 1, fbstart is still 0
10443 +        * write --> fbstart is 0 but dbstart is 1
10444 +        */
10445 +       /* AuDebugOn(au_fbstart(file) < bstart); */
10446 +
10447 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
10448 +                          file);
10449 +       err = PTR_ERR(h_file);
10450 +       if (IS_ERR(h_file)) {
10451 +               if (h_file_tmp) {
10452 +                       atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
10453 +                       au_set_h_fptr(file, bstart, h_file_tmp);
10454 +                       h_file_tmp = NULL;
10455 +               }
10456 +               goto out; /* todo: close all? */
10457 +       }
10458 +
10459 +       err = 0;
10460 +       au_set_fbstart(file, bstart);
10461 +       au_set_h_fptr(file, bstart, h_file);
10462 +       au_update_figen(file);
10463 +       /* todo: necessary? */
10464 +       /* file->f_ra = h_file->f_ra; */
10465 +
10466 +out:
10467 +       if (h_file_tmp)
10468 +               fput(h_file_tmp);
10469 +       return err;
10470 +}
10471 +
10472 +/* ---------------------------------------------------------------------- */
10473 +
10474 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
10475 +                       struct dentry *hi_wh)
10476 +{
10477 +       int err;
10478 +       aufs_bindex_t bstart;
10479 +       struct au_dinfo *dinfo;
10480 +       struct dentry *h_dentry;
10481 +       struct au_hdentry *hdp;
10482 +
10483 +       dinfo = au_di(file->f_dentry);
10484 +       AuRwMustWriteLock(&dinfo->di_rwsem);
10485 +
10486 +       bstart = dinfo->di_bstart;
10487 +       dinfo->di_bstart = btgt;
10488 +       hdp = dinfo->di_hdentry;
10489 +       h_dentry = hdp[0 + btgt].hd_dentry;
10490 +       hdp[0 + btgt].hd_dentry = hi_wh;
10491 +       err = au_reopen_nondir(file);
10492 +       hdp[0 + btgt].hd_dentry = h_dentry;
10493 +       dinfo->di_bstart = bstart;
10494 +
10495 +       return err;
10496 +}
10497 +
10498 +static int au_ready_to_write_wh(struct file *file, loff_t len,
10499 +                               aufs_bindex_t bcpup, struct au_pin *pin)
10500 +{
10501 +       int err;
10502 +       struct inode *inode, *h_inode;
10503 +       struct dentry *h_dentry, *hi_wh;
10504 +       struct au_cp_generic cpg = {
10505 +               .dentry = file->f_dentry,
10506 +               .bdst   = bcpup,
10507 +               .bsrc   = -1,
10508 +               .len    = len,
10509 +               .pin    = pin
10510 +       };
10511 +
10512 +       au_update_dbstart(cpg.dentry);
10513 +       inode = cpg.dentry->d_inode;
10514 +       h_inode = NULL;
10515 +       if (au_dbstart(cpg.dentry) <= bcpup
10516 +           && au_dbend(cpg.dentry) >= bcpup) {
10517 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
10518 +               if (h_dentry)
10519 +                       h_inode = h_dentry->d_inode;
10520 +       }
10521 +       hi_wh = au_hi_wh(inode, bcpup);
10522 +       if (!hi_wh && !h_inode)
10523 +               err = au_sio_cpup_wh(&cpg, file);
10524 +       else
10525 +               /* already copied-up after unlink */
10526 +               err = au_reopen_wh(file, bcpup, hi_wh);
10527 +
10528 +       if (!err
10529 +           && inode->i_nlink > 1
10530 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
10531 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
10532 +
10533 +       return err;
10534 +}
10535 +
10536 +/*
10537 + * prepare the @file for writing.
10538 + */
10539 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
10540 +{
10541 +       int err;
10542 +       aufs_bindex_t dbstart;
10543 +       struct dentry *parent, *h_dentry;
10544 +       struct inode *inode;
10545 +       struct super_block *sb;
10546 +       struct file *h_file;
10547 +       struct au_cp_generic cpg = {
10548 +               .dentry = file->f_dentry,
10549 +               .bdst   = -1,
10550 +               .bsrc   = -1,
10551 +               .len    = len,
10552 +               .pin    = pin,
10553 +               .flags  = AuCpup_DTIME
10554 +       };
10555 +
10556 +       sb = cpg.dentry->d_sb;
10557 +       inode = cpg.dentry->d_inode;
10558 +       AuDebugOn(au_special_file(inode->i_mode));
10559 +       cpg.bsrc = au_fbstart(file);
10560 +       err = au_test_ro(sb, cpg.bsrc, inode);
10561 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
10562 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
10563 +                            /*flags*/0);
10564 +               goto out;
10565 +       }
10566 +
10567 +       /* need to cpup or reopen */
10568 +       parent = dget_parent(cpg.dentry);
10569 +       di_write_lock_parent(parent);
10570 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
10571 +       cpg.bdst = err;
10572 +       if (unlikely(err < 0))
10573 +               goto out_dgrade;
10574 +       err = 0;
10575 +
10576 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
10577 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
10578 +               if (unlikely(err))
10579 +                       goto out_dgrade;
10580 +       }
10581 +
10582 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
10583 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10584 +       if (unlikely(err))
10585 +               goto out_dgrade;
10586 +
10587 +       h_dentry = au_hf_top(file)->f_dentry;
10588 +       dbstart = au_dbstart(cpg.dentry);
10589 +       if (dbstart <= cpg.bdst) {
10590 +               h_dentry = au_h_dptr(cpg.dentry, cpg.bdst);
10591 +               AuDebugOn(!h_dentry);
10592 +               cpg.bsrc = cpg.bdst;
10593 +       }
10594 +
10595 +       if (dbstart <= cpg.bdst         /* just reopen */
10596 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
10597 +               ) {
10598 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc);
10599 +               if (IS_ERR(h_file))
10600 +                       err = PTR_ERR(h_file);
10601 +               else {
10602 +                       di_downgrade_lock(parent, AuLock_IR);
10603 +                       if (dbstart > cpg.bdst)
10604 +                               err = au_sio_cpup_simple(&cpg);
10605 +                       if (!err)
10606 +                               err = au_reopen_nondir(file);
10607 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
10608 +               }
10609 +       } else {                        /* copyup as wh and reopen */
10610 +               /*
10611 +                * since writable hfsplus branch is not supported,
10612 +                * h_open_pre/post() are unnecessary.
10613 +                */
10614 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
10615 +               di_downgrade_lock(parent, AuLock_IR);
10616 +       }
10617 +
10618 +       if (!err) {
10619 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
10620 +               goto out_dput; /* success */
10621 +       }
10622 +       au_unpin(pin);
10623 +       goto out_unlock;
10624 +
10625 +out_dgrade:
10626 +       di_downgrade_lock(parent, AuLock_IR);
10627 +out_unlock:
10628 +       di_read_unlock(parent, AuLock_IR);
10629 +out_dput:
10630 +       dput(parent);
10631 +out:
10632 +       return err;
10633 +}
10634 +
10635 +/* ---------------------------------------------------------------------- */
10636 +
10637 +int au_do_flush(struct file *file, fl_owner_t id,
10638 +               int (*flush)(struct file *file, fl_owner_t id))
10639 +{
10640 +       int err;
10641 +       struct super_block *sb;
10642 +       struct inode *inode;
10643 +
10644 +       inode = file_inode(file);
10645 +       sb = inode->i_sb;
10646 +       si_noflush_read_lock(sb);
10647 +       fi_read_lock(file);
10648 +       ii_read_lock_child(inode);
10649 +
10650 +       err = flush(file, id);
10651 +       au_cpup_attr_timesizes(inode);
10652 +
10653 +       ii_read_unlock(inode);
10654 +       fi_read_unlock(file);
10655 +       si_read_unlock(sb);
10656 +       return err;
10657 +}
10658 +
10659 +/* ---------------------------------------------------------------------- */
10660 +
10661 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
10662 +{
10663 +       int err;
10664 +       struct au_pin pin;
10665 +       struct au_finfo *finfo;
10666 +       struct dentry *parent, *hi_wh;
10667 +       struct inode *inode;
10668 +       struct super_block *sb;
10669 +       struct au_cp_generic cpg = {
10670 +               .dentry = file->f_dentry,
10671 +               .bdst   = -1,
10672 +               .bsrc   = -1,
10673 +               .len    = -1,
10674 +               .pin    = &pin,
10675 +               .flags  = AuCpup_DTIME
10676 +       };
10677 +
10678 +       FiMustWriteLock(file);
10679 +
10680 +       err = 0;
10681 +       finfo = au_fi(file);
10682 +       sb = cpg.dentry->d_sb;
10683 +       inode = cpg.dentry->d_inode;
10684 +       cpg.bdst = au_ibstart(inode);
10685 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
10686 +               goto out;
10687 +
10688 +       parent = dget_parent(cpg.dentry);
10689 +       if (au_test_ro(sb, cpg.bdst, inode)) {
10690 +               di_read_lock_parent(parent, !AuLock_IR);
10691 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
10692 +               cpg.bdst = err;
10693 +               di_read_unlock(parent, !AuLock_IR);
10694 +               if (unlikely(err < 0))
10695 +                       goto out_parent;
10696 +               err = 0;
10697 +       }
10698 +
10699 +       di_read_lock_parent(parent, AuLock_IR);
10700 +       hi_wh = au_hi_wh(inode, cpg.bdst);
10701 +       if (!S_ISDIR(inode->i_mode)
10702 +           && au_opt_test(au_mntflags(sb), PLINK)
10703 +           && au_plink_test(inode)
10704 +           && !d_unhashed(cpg.dentry)
10705 +           && cpg.bdst < au_dbstart(cpg.dentry)) {
10706 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
10707 +               if (unlikely(err))
10708 +                       goto out_unlock;
10709 +
10710 +               /* always superio. */
10711 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
10712 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
10713 +               if (!err) {
10714 +                       err = au_sio_cpup_simple(&cpg);
10715 +                       au_unpin(&pin);
10716 +               }
10717 +       } else if (hi_wh) {
10718 +               /* already copied-up after unlink */
10719 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
10720 +               *need_reopen = 0;
10721 +       }
10722 +
10723 +out_unlock:
10724 +       di_read_unlock(parent, AuLock_IR);
10725 +out_parent:
10726 +       dput(parent);
10727 +out:
10728 +       return err;
10729 +}
10730 +
10731 +static void au_do_refresh_dir(struct file *file)
10732 +{
10733 +       aufs_bindex_t bindex, bend, new_bindex, brid;
10734 +       struct au_hfile *p, tmp, *q;
10735 +       struct au_finfo *finfo;
10736 +       struct super_block *sb;
10737 +       struct au_fidir *fidir;
10738 +
10739 +       FiMustWriteLock(file);
10740 +
10741 +       sb = file->f_dentry->d_sb;
10742 +       finfo = au_fi(file);
10743 +       fidir = finfo->fi_hdir;
10744 +       AuDebugOn(!fidir);
10745 +       p = fidir->fd_hfile + finfo->fi_btop;
10746 +       brid = p->hf_br->br_id;
10747 +       bend = fidir->fd_bbot;
10748 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
10749 +               if (!p->hf_file)
10750 +                       continue;
10751 +
10752 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
10753 +               if (new_bindex == bindex)
10754 +                       continue;
10755 +               if (new_bindex < 0) {
10756 +                       au_set_h_fptr(file, bindex, NULL);
10757 +                       continue;
10758 +               }
10759 +
10760 +               /* swap two lower inode, and loop again */
10761 +               q = fidir->fd_hfile + new_bindex;
10762 +               tmp = *q;
10763 +               *q = *p;
10764 +               *p = tmp;
10765 +               if (tmp.hf_file) {
10766 +                       bindex--;
10767 +                       p--;
10768 +               }
10769 +       }
10770 +
10771 +       p = fidir->fd_hfile;
10772 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_dentry)) {
10773 +               bend = au_sbend(sb);
10774 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
10775 +                    finfo->fi_btop++, p++)
10776 +                       if (p->hf_file) {
10777 +                               if (file_inode(p->hf_file))
10778 +                                       break;
10779 +                               else
10780 +                                       au_hfput(p, file);
10781 +                       }
10782 +       } else {
10783 +               bend = au_br_index(sb, brid);
10784 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
10785 +                    finfo->fi_btop++, p++)
10786 +                       if (p->hf_file)
10787 +                               au_hfput(p, file);
10788 +               bend = au_sbend(sb);
10789 +       }
10790 +
10791 +       p = fidir->fd_hfile + bend;
10792 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
10793 +            fidir->fd_bbot--, p--)
10794 +               if (p->hf_file) {
10795 +                       if (file_inode(p->hf_file))
10796 +                               break;
10797 +                       else
10798 +                               au_hfput(p, file);
10799 +               }
10800 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
10801 +}
10802 +
10803 +/*
10804 + * after branch manipulating, refresh the file.
10805 + */
10806 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
10807 +{
10808 +       int err, need_reopen;
10809 +       aufs_bindex_t bend, bindex;
10810 +       struct dentry *dentry;
10811 +       struct au_finfo *finfo;
10812 +       struct au_hfile *hfile;
10813 +
10814 +       dentry = file->f_dentry;
10815 +       finfo = au_fi(file);
10816 +       if (!finfo->fi_hdir) {
10817 +               hfile = &finfo->fi_htop;
10818 +               AuDebugOn(!hfile->hf_file);
10819 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
10820 +               AuDebugOn(bindex < 0);
10821 +               if (bindex != finfo->fi_btop)
10822 +                       au_set_fbstart(file, bindex);
10823 +       } else {
10824 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
10825 +               if (unlikely(err))
10826 +                       goto out;
10827 +               au_do_refresh_dir(file);
10828 +       }
10829 +
10830 +       err = 0;
10831 +       need_reopen = 1;
10832 +       if (!au_test_mmapped(file))
10833 +               err = au_file_refresh_by_inode(file, &need_reopen);
10834 +       if (!err && need_reopen && !d_unlinked(dentry))
10835 +               err = reopen(file);
10836 +       if (!err) {
10837 +               au_update_figen(file);
10838 +               goto out; /* success */
10839 +       }
10840 +
10841 +       /* error, close all lower files */
10842 +       if (finfo->fi_hdir) {
10843 +               bend = au_fbend_dir(file);
10844 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
10845 +                       au_set_h_fptr(file, bindex, NULL);
10846 +       }
10847 +
10848 +out:
10849 +       return err;
10850 +}
10851 +
10852 +/* common function to regular file and dir */
10853 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
10854 +                         int wlock)
10855 +{
10856 +       int err;
10857 +       unsigned int sigen, figen;
10858 +       aufs_bindex_t bstart;
10859 +       unsigned char pseudo_link;
10860 +       struct dentry *dentry;
10861 +       struct inode *inode;
10862 +
10863 +       err = 0;
10864 +       dentry = file->f_dentry;
10865 +       inode = dentry->d_inode;
10866 +       AuDebugOn(au_special_file(inode->i_mode));
10867 +       sigen = au_sigen(dentry->d_sb);
10868 +       fi_write_lock(file);
10869 +       figen = au_figen(file);
10870 +       di_write_lock_child(dentry);
10871 +       bstart = au_dbstart(dentry);
10872 +       pseudo_link = (bstart != au_ibstart(inode));
10873 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
10874 +               if (!wlock) {
10875 +                       di_downgrade_lock(dentry, AuLock_IR);
10876 +                       fi_downgrade_lock(file);
10877 +               }
10878 +               goto out; /* success */
10879 +       }
10880 +
10881 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
10882 +       if (au_digen_test(dentry, sigen)) {
10883 +               err = au_reval_dpath(dentry, sigen);
10884 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
10885 +       }
10886 +
10887 +       if (!err)
10888 +               err = refresh_file(file, reopen);
10889 +       if (!err) {
10890 +               if (!wlock) {
10891 +                       di_downgrade_lock(dentry, AuLock_IR);
10892 +                       fi_downgrade_lock(file);
10893 +               }
10894 +       } else {
10895 +               di_write_unlock(dentry);
10896 +               fi_write_unlock(file);
10897 +       }
10898 +
10899 +out:
10900 +       return err;
10901 +}
10902 +
10903 +/* ---------------------------------------------------------------------- */
10904 +
10905 +/* cf. aufs_nopage() */
10906 +/* for madvise(2) */
10907 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
10908 +{
10909 +       unlock_page(page);
10910 +       return 0;
10911 +}
10912 +
10913 +/* it will never be called, but necessary to support O_DIRECT */
10914 +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
10915 +                             const struct iovec *iov, loff_t offset,
10916 +                             unsigned long nr_segs)
10917 +{ BUG(); return 0; }
10918 +
10919 +/*
10920 + * it will never be called, but madvise and fadvise behaves differently
10921 + * when get_xip_mem is defined
10922 + */
10923 +static int aufs_get_xip_mem(struct address_space *mapping, pgoff_t pgoff,
10924 +                           int create, void **kmem, unsigned long *pfn)
10925 +{ BUG(); return 0; }
10926 +
10927 +/* they will never be called. */
10928 +#ifdef CONFIG_AUFS_DEBUG
10929 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
10930 +                           loff_t pos, unsigned len, unsigned flags,
10931 +                           struct page **pagep, void **fsdata)
10932 +{ AuUnsupport(); return 0; }
10933 +static int aufs_write_end(struct file *file, struct address_space *mapping,
10934 +                         loff_t pos, unsigned len, unsigned copied,
10935 +                         struct page *page, void *fsdata)
10936 +{ AuUnsupport(); return 0; }
10937 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
10938 +{ AuUnsupport(); return 0; }
10939 +
10940 +static int aufs_set_page_dirty(struct page *page)
10941 +{ AuUnsupport(); return 0; }
10942 +static void aufs_invalidatepage(struct page *page, unsigned long offset)
10943 +{ AuUnsupport(); }
10944 +static int aufs_releasepage(struct page *page, gfp_t gfp)
10945 +{ AuUnsupport(); return 0; }
10946 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
10947 +                           struct page *page, enum migrate_mode mode)
10948 +{ AuUnsupport(); return 0; }
10949 +static int aufs_launder_page(struct page *page)
10950 +{ AuUnsupport(); return 0; }
10951 +static int aufs_is_partially_uptodate(struct page *page,
10952 +                                     read_descriptor_t *desc,
10953 +                                     unsigned long from)
10954 +{ AuUnsupport(); return 0; }
10955 +static int aufs_error_remove_page(struct address_space *mapping,
10956 +                                 struct page *page)
10957 +{ AuUnsupport(); return 0; }
10958 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
10959 +                             sector_t *span)
10960 +{ AuUnsupport(); return 0; }
10961 +static void aufs_swap_deactivate(struct file *file)
10962 +{ AuUnsupport(); }
10963 +#endif /* CONFIG_AUFS_DEBUG */
10964 +
10965 +const struct address_space_operations aufs_aop = {
10966 +       .readpage               = aufs_readpage,
10967 +       .direct_IO              = aufs_direct_IO,
10968 +       .get_xip_mem            = aufs_get_xip_mem,
10969 +#ifdef CONFIG_AUFS_DEBUG
10970 +       .writepage              = aufs_writepage,
10971 +       /* no writepages, because of writepage */
10972 +       .set_page_dirty         = aufs_set_page_dirty,
10973 +       /* no readpages, because of readpage */
10974 +       .write_begin            = aufs_write_begin,
10975 +       .write_end              = aufs_write_end,
10976 +       /* no bmap, no block device */
10977 +       .invalidatepage         = aufs_invalidatepage,
10978 +       .releasepage            = aufs_releasepage,
10979 +       .migratepage            = aufs_migratepage,
10980 +       .launder_page           = aufs_launder_page,
10981 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
10982 +       .error_remove_page      = aufs_error_remove_page,
10983 +       .swap_activate          = aufs_swap_activate,
10984 +       .swap_deactivate        = aufs_swap_deactivate
10985 +#endif /* CONFIG_AUFS_DEBUG */
10986 +};
10987 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
10988 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
10989 +++ linux/fs/aufs/file.h        2013-08-23 23:59:39.634916914 +0200
10990 @@ -0,0 +1,310 @@
10991 +/*
10992 + * Copyright (C) 2005-2013 Junjiro R. Okajima
10993 + *
10994 + * This program, aufs is free software; you can redistribute it and/or modify
10995 + * it under the terms of the GNU General Public License as published by
10996 + * the Free Software Foundation; either version 2 of the License, or
10997 + * (at your option) any later version.
10998 + *
10999 + * This program is distributed in the hope that it will be useful,
11000 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11001 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11002 + * GNU General Public License for more details.
11003 + *
11004 + * You should have received a copy of the GNU General Public License
11005 + * along with this program; if not, write to the Free Software
11006 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11007 + */
11008 +
11009 +/*
11010 + * file operations
11011 + */
11012 +
11013 +#ifndef __AUFS_FILE_H__
11014 +#define __AUFS_FILE_H__
11015 +
11016 +#ifdef __KERNEL__
11017 +
11018 +#include <linux/file.h>
11019 +#include <linux/fs.h>
11020 +#include <linux/poll.h>
11021 +#include "rwsem.h"
11022 +
11023 +struct au_branch;
11024 +struct au_hfile {
11025 +       struct file             *hf_file;
11026 +       struct au_branch        *hf_br;
11027 +};
11028 +
11029 +struct au_vdir;
11030 +struct au_fidir {
11031 +       aufs_bindex_t           fd_bbot;
11032 +       aufs_bindex_t           fd_nent;
11033 +       struct au_vdir          *fd_vdir_cache;
11034 +       struct au_hfile         fd_hfile[];
11035 +};
11036 +
11037 +static inline int au_fidir_sz(int nent)
11038 +{
11039 +       AuDebugOn(nent < 0);
11040 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
11041 +}
11042 +
11043 +struct au_finfo {
11044 +       atomic_t                fi_generation;
11045 +
11046 +       struct au_rwsem         fi_rwsem;
11047 +       aufs_bindex_t           fi_btop;
11048 +
11049 +       /* do not union them */
11050 +       struct {                                /* for non-dir */
11051 +               struct au_hfile                 fi_htop;
11052 +               atomic_t                        fi_mmapped;
11053 +       };
11054 +       struct au_fidir         *fi_hdir;       /* for dir only */
11055 +} ____cacheline_aligned_in_smp;
11056 +
11057 +/* ---------------------------------------------------------------------- */
11058 +
11059 +/* file.c */
11060 +extern const struct address_space_operations aufs_aop;
11061 +unsigned int au_file_roflags(unsigned int flags);
11062 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
11063 +                      struct file *file);
11064 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
11065 +              struct au_fidir *fidir);
11066 +int au_reopen_nondir(struct file *file);
11067 +struct au_pin;
11068 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
11069 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
11070 +                         int wlock);
11071 +int au_do_flush(struct file *file, fl_owner_t id,
11072 +               int (*flush)(struct file *file, fl_owner_t id));
11073 +
11074 +/* poll.c */
11075 +#ifdef CONFIG_AUFS_POLL
11076 +unsigned int aufs_poll(struct file *file, poll_table *wait);
11077 +#endif
11078 +
11079 +#ifdef CONFIG_AUFS_BR_HFSPLUS
11080 +/* hfsplus.c */
11081 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex);
11082 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
11083 +                   struct file *h_file);
11084 +#else
11085 +static inline
11086 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex)
11087 +{
11088 +       return NULL;
11089 +}
11090 +
11091 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
11092 +          struct file *h_file);
11093 +#endif
11094 +
11095 +/* f_op.c */
11096 +extern const struct file_operations aufs_file_fop;
11097 +int au_do_open_nondir(struct file *file, int flags);
11098 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
11099 +
11100 +#ifdef CONFIG_AUFS_SP_IATTR
11101 +/* f_op_sp.c */
11102 +struct au_finfo *au_fi_sp(struct file *file);
11103 +int au_special_file(umode_t mode);
11104 +void au_init_special_fop(struct inode *inode, umode_t mode, dev_t rdev);
11105 +#else
11106 +static inline struct au_finfo *au_fi_sp(struct file *file)
11107 +{
11108 +       return NULL;
11109 +}
11110 +AuStubInt0(au_special_file, umode_t mode)
11111 +static inline void au_init_special_fop(struct inode *inode, umode_t mode,
11112 +                                      dev_t rdev)
11113 +{
11114 +       init_special_inode(inode, mode, rdev);
11115 +}
11116 +#endif
11117 +
11118 +/* finfo.c */
11119 +void au_hfput(struct au_hfile *hf, struct file *file);
11120 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
11121 +                  struct file *h_file);
11122 +
11123 +void au_update_figen(struct file *file);
11124 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
11125 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
11126 +
11127 +void au_fi_init_once(void *_fi);
11128 +void au_finfo_fin(struct file *file);
11129 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
11130 +
11131 +/* ioctl.c */
11132 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
11133 +#ifdef CONFIG_COMPAT
11134 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
11135 +                          unsigned long arg);
11136 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
11137 +                             unsigned long arg);
11138 +#endif
11139 +
11140 +/* ---------------------------------------------------------------------- */
11141 +
11142 +static inline struct au_finfo *au_fi(struct file *file)
11143 +{
11144 +       struct au_finfo *finfo;
11145 +
11146 +       finfo = au_fi_sp(file);
11147 +       if (!finfo)
11148 +               finfo = file->private_data;
11149 +       return finfo;
11150 +}
11151 +
11152 +/* ---------------------------------------------------------------------- */
11153 +
11154 +/*
11155 + * fi_read_lock, fi_write_lock,
11156 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
11157 + */
11158 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
11159 +
11160 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
11161 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
11162 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
11163 +
11164 +/* ---------------------------------------------------------------------- */
11165 +
11166 +/* todo: hard/soft set? */
11167 +static inline aufs_bindex_t au_fbstart(struct file *file)
11168 +{
11169 +       FiMustAnyLock(file);
11170 +       return au_fi(file)->fi_btop;
11171 +}
11172 +
11173 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
11174 +{
11175 +       FiMustAnyLock(file);
11176 +       AuDebugOn(!au_fi(file)->fi_hdir);
11177 +       return au_fi(file)->fi_hdir->fd_bbot;
11178 +}
11179 +
11180 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
11181 +{
11182 +       FiMustAnyLock(file);
11183 +       AuDebugOn(!au_fi(file)->fi_hdir);
11184 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
11185 +}
11186 +
11187 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
11188 +{
11189 +       FiMustWriteLock(file);
11190 +       au_fi(file)->fi_btop = bindex;
11191 +}
11192 +
11193 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
11194 +{
11195 +       FiMustWriteLock(file);
11196 +       AuDebugOn(!au_fi(file)->fi_hdir);
11197 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
11198 +}
11199 +
11200 +static inline void au_set_fvdir_cache(struct file *file,
11201 +                                     struct au_vdir *vdir_cache)
11202 +{
11203 +       FiMustWriteLock(file);
11204 +       AuDebugOn(!au_fi(file)->fi_hdir);
11205 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
11206 +}
11207 +
11208 +static inline struct file *au_hf_top(struct file *file)
11209 +{
11210 +       FiMustAnyLock(file);
11211 +       AuDebugOn(au_fi(file)->fi_hdir);
11212 +       return au_fi(file)->fi_htop.hf_file;
11213 +}
11214 +
11215 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
11216 +{
11217 +       FiMustAnyLock(file);
11218 +       AuDebugOn(!au_fi(file)->fi_hdir);
11219 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
11220 +}
11221 +
11222 +/* todo: memory barrier? */
11223 +static inline unsigned int au_figen(struct file *f)
11224 +{
11225 +       return atomic_read(&au_fi(f)->fi_generation);
11226 +}
11227 +
11228 +static inline void au_set_mmapped(struct file *f)
11229 +{
11230 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
11231 +               return;
11232 +       pr_warn("fi_mmapped wrapped around\n");
11233 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
11234 +               ;
11235 +}
11236 +
11237 +static inline void au_unset_mmapped(struct file *f)
11238 +{
11239 +       atomic_dec(&au_fi(f)->fi_mmapped);
11240 +}
11241 +
11242 +static inline int au_test_mmapped(struct file *f)
11243 +{
11244 +       return atomic_read(&au_fi(f)->fi_mmapped);
11245 +}
11246 +
11247 +/* customize vma->vm_file */
11248 +
11249 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
11250 +                                      struct file *file)
11251 +{
11252 +       struct file *f;
11253 +
11254 +       f = vma->vm_file;
11255 +       get_file(file);
11256 +       vma->vm_file = file;
11257 +       fput(f);
11258 +}
11259 +
11260 +#ifdef CONFIG_MMU
11261 +#define AuDbgVmRegion(file, vma) do {} while (0)
11262 +
11263 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
11264 +                                   struct file *file)
11265 +{
11266 +       au_do_vm_file_reset(vma, file);
11267 +}
11268 +#else
11269 +#define AuDbgVmRegion(file, vma) \
11270 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
11271 +
11272 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
11273 +                                   struct file *file)
11274 +{
11275 +       struct file *f;
11276 +
11277 +       au_do_vm_file_reset(vma, file);
11278 +       f = vma->vm_region->vm_file;
11279 +       get_file(file);
11280 +       vma->vm_region->vm_file = file;
11281 +       fput(f);
11282 +}
11283 +#endif /* CONFIG_MMU */
11284 +
11285 +/* handle vma->vm_prfile */
11286 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
11287 +                                   struct file *file)
11288 +{
11289 +#ifdef CONFIG_AUFS_PROC_MAP
11290 +       get_file(file);
11291 +       vma->vm_prfile = file;
11292 +#ifndef CONFIG_MMU
11293 +       get_file(file);
11294 +       vma->vm_region->vm_prfile = file;
11295 +#endif
11296 +#endif
11297 +}
11298 +
11299 +#endif /* __KERNEL__ */
11300 +#endif /* __AUFS_FILE_H__ */
11301 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
11302 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
11303 +++ linux/fs/aufs/finfo.c       2013-07-06 13:20:47.750198454 +0200
11304 @@ -0,0 +1,157 @@
11305 +/*
11306 + * Copyright (C) 2005-2013 Junjiro R. Okajima
11307 + *
11308 + * This program, aufs is free software; you can redistribute it and/or modify
11309 + * it under the terms of the GNU General Public License as published by
11310 + * the Free Software Foundation; either version 2 of the License, or
11311 + * (at your option) any later version.
11312 + *
11313 + * This program is distributed in the hope that it will be useful,
11314 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11315 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11316 + * GNU General Public License for more details.
11317 + *
11318 + * You should have received a copy of the GNU General Public License
11319 + * along with this program; if not, write to the Free Software
11320 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11321 + */
11322 +
11323 +/*
11324 + * file private data
11325 + */
11326 +
11327 +#include "aufs.h"
11328 +
11329 +void au_hfput(struct au_hfile *hf, struct file *file)
11330 +{
11331 +       /* todo: direct access f_flags */
11332 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
11333 +               allow_write_access(hf->hf_file);
11334 +       fput(hf->hf_file);
11335 +       hf->hf_file = NULL;
11336 +       atomic_dec(&hf->hf_br->br_count);
11337 +       hf->hf_br = NULL;
11338 +}
11339 +
11340 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
11341 +{
11342 +       struct au_finfo *finfo = au_fi(file);
11343 +       struct au_hfile *hf;
11344 +       struct au_fidir *fidir;
11345 +
11346 +       fidir = finfo->fi_hdir;
11347 +       if (!fidir) {
11348 +               AuDebugOn(finfo->fi_btop != bindex);
11349 +               hf = &finfo->fi_htop;
11350 +       } else
11351 +               hf = fidir->fd_hfile + bindex;
11352 +
11353 +       if (hf && hf->hf_file)
11354 +               au_hfput(hf, file);
11355 +       if (val) {
11356 +               FiMustWriteLock(file);
11357 +               hf->hf_file = val;
11358 +               hf->hf_br = au_sbr(file->f_dentry->d_sb, bindex);
11359 +       }
11360 +}
11361 +
11362 +void au_update_figen(struct file *file)
11363 +{
11364 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_dentry));
11365 +       /* smp_mb(); */ /* atomic_set */
11366 +}
11367 +
11368 +/* ---------------------------------------------------------------------- */
11369 +
11370 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
11371 +{
11372 +       struct au_fidir *fidir;
11373 +       int nbr;
11374 +
11375 +       nbr = au_sbend(sb) + 1;
11376 +       if (nbr < 2)
11377 +               nbr = 2; /* initial allocate for 2 branches */
11378 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
11379 +       if (fidir) {
11380 +               fidir->fd_bbot = -1;
11381 +               fidir->fd_nent = nbr;
11382 +               fidir->fd_vdir_cache = NULL;
11383 +       }
11384 +
11385 +       return fidir;
11386 +}
11387 +
11388 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
11389 +{
11390 +       int err;
11391 +       struct au_fidir *fidir, *p;
11392 +
11393 +       AuRwMustWriteLock(&finfo->fi_rwsem);
11394 +       fidir = finfo->fi_hdir;
11395 +       AuDebugOn(!fidir);
11396 +
11397 +       err = -ENOMEM;
11398 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
11399 +                        GFP_NOFS);
11400 +       if (p) {
11401 +               p->fd_nent = nbr;
11402 +               finfo->fi_hdir = p;
11403 +               err = 0;
11404 +       }
11405 +
11406 +       return err;
11407 +}
11408 +
11409 +/* ---------------------------------------------------------------------- */
11410 +
11411 +void au_finfo_fin(struct file *file)
11412 +{
11413 +       struct au_finfo *finfo;
11414 +
11415 +       au_nfiles_dec(file->f_dentry->d_sb);
11416 +
11417 +       finfo = au_fi(file);
11418 +       AuDebugOn(finfo->fi_hdir);
11419 +       AuRwDestroy(&finfo->fi_rwsem);
11420 +       au_cache_free_finfo(finfo);
11421 +}
11422 +
11423 +void au_fi_init_once(void *_finfo)
11424 +{
11425 +       struct au_finfo *finfo = _finfo;
11426 +       static struct lock_class_key aufs_fi;
11427 +
11428 +       au_rw_init(&finfo->fi_rwsem);
11429 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
11430 +}
11431 +
11432 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
11433 +{
11434 +       int err;
11435 +       struct au_finfo *finfo;
11436 +       struct dentry *dentry;
11437 +
11438 +       err = -ENOMEM;
11439 +       dentry = file->f_dentry;
11440 +       finfo = au_cache_alloc_finfo();
11441 +       if (unlikely(!finfo))
11442 +               goto out;
11443 +
11444 +       err = 0;
11445 +       au_nfiles_inc(dentry->d_sb);
11446 +       /* verbose coding for lock class name */
11447 +       if (!fidir)
11448 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
11449 +       else
11450 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
11451 +       au_rw_write_lock(&finfo->fi_rwsem);
11452 +       finfo->fi_btop = -1;
11453 +       finfo->fi_hdir = fidir;
11454 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
11455 +       /* smp_mb(); */ /* atomic_set */
11456 +
11457 +       file->private_data = finfo;
11458 +
11459 +out:
11460 +       return err;
11461 +}
11462 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
11463 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
11464 +++ linux/fs/aufs/f_op.c        2013-08-23 23:59:39.634916914 +0200
11465 @@ -0,0 +1,721 @@
11466 +/*
11467 + * Copyright (C) 2005-2013 Junjiro R. Okajima
11468 + *
11469 + * This program, aufs is free software; you can redistribute it and/or modify
11470 + * it under the terms of the GNU General Public License as published by
11471 + * the Free Software Foundation; either version 2 of the License, or
11472 + * (at your option) any later version.
11473 + *
11474 + * This program is distributed in the hope that it will be useful,
11475 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11476 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11477 + * GNU General Public License for more details.
11478 + *
11479 + * You should have received a copy of the GNU General Public License
11480 + * along with this program; if not, write to the Free Software
11481 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11482 + */
11483 +
11484 +/*
11485 + * file and vm operations
11486 + */
11487 +
11488 +#include <linux/aio.h>
11489 +#include <linux/fs_stack.h>
11490 +#include <linux/mman.h>
11491 +#include <linux/security.h>
11492 +#include "aufs.h"
11493 +
11494 +int au_do_open_nondir(struct file *file, int flags)
11495 +{
11496 +       int err;
11497 +       aufs_bindex_t bindex;
11498 +       struct file *h_file;
11499 +       struct dentry *dentry;
11500 +       struct au_finfo *finfo;
11501 +
11502 +       FiMustWriteLock(file);
11503 +
11504 +       dentry = file->f_dentry;
11505 +       err = au_d_alive(dentry);
11506 +       if (unlikely(err))
11507 +               goto out;
11508 +
11509 +       finfo = au_fi(file);
11510 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
11511 +       atomic_set(&finfo->fi_mmapped, 0);
11512 +       bindex = au_dbstart(dentry);
11513 +       h_file = au_h_open(dentry, bindex, flags, file);
11514 +       if (IS_ERR(h_file))
11515 +               err = PTR_ERR(h_file);
11516 +       else {
11517 +               au_set_fbstart(file, bindex);
11518 +               au_set_h_fptr(file, bindex, h_file);
11519 +               au_update_figen(file);
11520 +               /* todo: necessary? */
11521 +               /* file->f_ra = h_file->f_ra; */
11522 +       }
11523 +
11524 +out:
11525 +       return err;
11526 +}
11527 +
11528 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
11529 +                           struct file *file)
11530 +{
11531 +       int err;
11532 +       struct super_block *sb;
11533 +
11534 +       AuDbg("%.*s, f_flags 0x%x, f_mode 0x%x\n",
11535 +             AuDLNPair(file->f_dentry), vfsub_file_flags(file),
11536 +             file->f_mode);
11537 +
11538 +       sb = file->f_dentry->d_sb;
11539 +       si_read_lock(sb, AuLock_FLUSH);
11540 +       err = au_do_open(file, au_do_open_nondir, /*fidir*/NULL);
11541 +       si_read_unlock(sb);
11542 +       return err;
11543 +}
11544 +
11545 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
11546 +{
11547 +       struct au_finfo *finfo;
11548 +       aufs_bindex_t bindex;
11549 +
11550 +       finfo = au_fi(file);
11551 +       bindex = finfo->fi_btop;
11552 +       if (bindex >= 0)
11553 +               au_set_h_fptr(file, bindex, NULL);
11554 +
11555 +       au_finfo_fin(file);
11556 +       return 0;
11557 +}
11558 +
11559 +/* ---------------------------------------------------------------------- */
11560 +
11561 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
11562 +{
11563 +       int err;
11564 +       struct file *h_file;
11565 +
11566 +       err = 0;
11567 +       h_file = au_hf_top(file);
11568 +       if (h_file)
11569 +               err = vfsub_flush(h_file, id);
11570 +       return err;
11571 +}
11572 +
11573 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
11574 +{
11575 +       return au_do_flush(file, id, au_do_flush_nondir);
11576 +}
11577 +
11578 +/* ---------------------------------------------------------------------- */
11579 +/*
11580 + * read and write functions acquire [fdi]_rwsem once, but release before
11581 + * mmap_sem. This is because to stop a race condition between mmap(2).
11582 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
11583 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
11584 + * read functions after [fdi]_rwsem are released, but it should be harmless.
11585 + */
11586 +
11587 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
11588 +                        loff_t *ppos)
11589 +{
11590 +       ssize_t err;
11591 +       struct dentry *dentry;
11592 +       struct file *h_file;
11593 +       struct super_block *sb;
11594 +
11595 +       dentry = file->f_dentry;
11596 +       sb = dentry->d_sb;
11597 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11598 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11599 +       if (unlikely(err))
11600 +               goto out;
11601 +
11602 +       h_file = au_hf_top(file);
11603 +       get_file(h_file);
11604 +       di_read_unlock(dentry, AuLock_IR);
11605 +       fi_read_unlock(file);
11606 +
11607 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
11608 +       err = vfsub_read_u(h_file, buf, count, ppos);
11609 +       /* todo: necessary? */
11610 +       /* file->f_ra = h_file->f_ra; */
11611 +       /* update without lock, I don't think it a problem */
11612 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
11613 +       fput(h_file);
11614 +
11615 +out:
11616 +       si_read_unlock(sb);
11617 +       return err;
11618 +}
11619 +
11620 +/*
11621 + * todo: very ugly
11622 + * it locks both of i_mutex and si_rwsem for read in safe.
11623 + * if the plink maintenance mode continues forever (that is the problem),
11624 + * may loop forever.
11625 + */
11626 +static void au_mtx_and_read_lock(struct inode *inode)
11627 +{
11628 +       int err;
11629 +       struct super_block *sb = inode->i_sb;
11630 +
11631 +       while (1) {
11632 +               mutex_lock(&inode->i_mutex);
11633 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
11634 +               if (!err)
11635 +                       break;
11636 +               mutex_unlock(&inode->i_mutex);
11637 +               si_read_lock(sb, AuLock_NOPLMW);
11638 +               si_read_unlock(sb);
11639 +       }
11640 +}
11641 +
11642 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
11643 +                         size_t count, loff_t *ppos)
11644 +{
11645 +       ssize_t err;
11646 +       struct au_pin pin;
11647 +       struct dentry *dentry;
11648 +       struct super_block *sb;
11649 +       struct inode *inode;
11650 +       struct file *h_file;
11651 +       char __user *buf = (char __user *)ubuf;
11652 +
11653 +       dentry = file->f_dentry;
11654 +       sb = dentry->d_sb;
11655 +       inode = dentry->d_inode;
11656 +       au_mtx_and_read_lock(inode);
11657 +
11658 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11659 +       if (unlikely(err))
11660 +               goto out;
11661 +
11662 +       err = au_ready_to_write(file, -1, &pin);
11663 +       di_downgrade_lock(dentry, AuLock_IR);
11664 +       if (unlikely(err)) {
11665 +               di_read_unlock(dentry, AuLock_IR);
11666 +               fi_write_unlock(file);
11667 +               goto out;
11668 +       }
11669 +
11670 +       h_file = au_hf_top(file);
11671 +       get_file(h_file);
11672 +       au_unpin(&pin);
11673 +       di_read_unlock(dentry, AuLock_IR);
11674 +       fi_write_unlock(file);
11675 +
11676 +       err = vfsub_write_u(h_file, buf, count, ppos);
11677 +       ii_write_lock_child(inode);
11678 +       au_cpup_attr_timesizes(inode);
11679 +       inode->i_mode = file_inode(h_file)->i_mode;
11680 +       ii_write_unlock(inode);
11681 +       fput(h_file);
11682 +
11683 +out:
11684 +       si_read_unlock(sb);
11685 +       mutex_unlock(&inode->i_mutex);
11686 +       return err;
11687 +}
11688 +
11689 +static ssize_t au_do_aio(struct file *h_file, int rw, struct kiocb *kio,
11690 +                        const struct iovec *iov, unsigned long nv, loff_t pos)
11691 +{
11692 +       ssize_t err;
11693 +       struct file *file;
11694 +       ssize_t (*func)(struct kiocb *, const struct iovec *, unsigned long,
11695 +                       loff_t);
11696 +
11697 +       err = security_file_permission(h_file, rw);
11698 +       if (unlikely(err))
11699 +               goto out;
11700 +
11701 +       err = -ENOSYS;
11702 +       func = NULL;
11703 +       if (rw == MAY_READ)
11704 +               func = h_file->f_op->aio_read;
11705 +       else if (rw == MAY_WRITE)
11706 +               func = h_file->f_op->aio_write;
11707 +       if (func) {
11708 +               file = kio->ki_filp;
11709 +               kio->ki_filp = h_file;
11710 +               lockdep_off();
11711 +               err = func(kio, iov, nv, pos);
11712 +               lockdep_on();
11713 +               kio->ki_filp = file;
11714 +       } else
11715 +               /* currently there is no such fs */
11716 +               WARN_ON_ONCE(1);
11717 +
11718 +out:
11719 +       return err;
11720 +}
11721 +
11722 +static ssize_t aufs_aio_read(struct kiocb *kio, const struct iovec *iov,
11723 +                            unsigned long nv, loff_t pos)
11724 +{
11725 +       ssize_t err;
11726 +       struct file *file, *h_file;
11727 +       struct dentry *dentry;
11728 +       struct super_block *sb;
11729 +
11730 +       file = kio->ki_filp;
11731 +       dentry = file->f_dentry;
11732 +       sb = dentry->d_sb;
11733 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11734 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11735 +       if (unlikely(err))
11736 +               goto out;
11737 +
11738 +       h_file = au_hf_top(file);
11739 +       get_file(h_file);
11740 +       di_read_unlock(dentry, AuLock_IR);
11741 +       fi_read_unlock(file);
11742 +
11743 +       err = au_do_aio(h_file, MAY_READ, kio, iov, nv, pos);
11744 +       /* todo: necessary? */
11745 +       /* file->f_ra = h_file->f_ra; */
11746 +       /* update without lock, I don't think it a problem */
11747 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
11748 +       fput(h_file);
11749 +
11750 +out:
11751 +       si_read_unlock(sb);
11752 +       return err;
11753 +}
11754 +
11755 +static ssize_t aufs_aio_write(struct kiocb *kio, const struct iovec *iov,
11756 +                             unsigned long nv, loff_t pos)
11757 +{
11758 +       ssize_t err;
11759 +       struct au_pin pin;
11760 +       struct dentry *dentry;
11761 +       struct inode *inode;
11762 +       struct file *file, *h_file;
11763 +       struct super_block *sb;
11764 +
11765 +       file = kio->ki_filp;
11766 +       dentry = file->f_dentry;
11767 +       sb = dentry->d_sb;
11768 +       inode = dentry->d_inode;
11769 +       au_mtx_and_read_lock(inode);
11770 +
11771 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11772 +       if (unlikely(err))
11773 +               goto out;
11774 +
11775 +       err = au_ready_to_write(file, -1, &pin);
11776 +       di_downgrade_lock(dentry, AuLock_IR);
11777 +       if (unlikely(err)) {
11778 +               di_read_unlock(dentry, AuLock_IR);
11779 +               fi_write_unlock(file);
11780 +               goto out;
11781 +       }
11782 +
11783 +       h_file = au_hf_top(file);
11784 +       get_file(h_file);
11785 +       au_unpin(&pin);
11786 +       di_read_unlock(dentry, AuLock_IR);
11787 +       fi_write_unlock(file);
11788 +
11789 +       err = au_do_aio(h_file, MAY_WRITE, kio, iov, nv, pos);
11790 +       ii_write_lock_child(inode);
11791 +       au_cpup_attr_timesizes(inode);
11792 +       inode->i_mode = file_inode(h_file)->i_mode;
11793 +       ii_write_unlock(inode);
11794 +       fput(h_file);
11795 +
11796 +out:
11797 +       si_read_unlock(sb);
11798 +       mutex_unlock(&inode->i_mutex);
11799 +       return err;
11800 +}
11801 +
11802 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
11803 +                               struct pipe_inode_info *pipe, size_t len,
11804 +                               unsigned int flags)
11805 +{
11806 +       ssize_t err;
11807 +       struct file *h_file;
11808 +       struct dentry *dentry;
11809 +       struct super_block *sb;
11810 +
11811 +       dentry = file->f_dentry;
11812 +       sb = dentry->d_sb;
11813 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11814 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
11815 +       if (unlikely(err))
11816 +               goto out;
11817 +
11818 +       err = -EINVAL;
11819 +       h_file = au_hf_top(file);
11820 +       get_file(h_file);
11821 +       if (au_test_loopback_kthread()) {
11822 +               au_warn_loopback(h_file->f_dentry->d_sb);
11823 +               if (file->f_mapping != h_file->f_mapping) {
11824 +                       file->f_mapping = h_file->f_mapping;
11825 +                       smp_mb(); /* unnecessary? */
11826 +               }
11827 +       }
11828 +       di_read_unlock(dentry, AuLock_IR);
11829 +       fi_read_unlock(file);
11830 +
11831 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
11832 +       /* todo: necessasry? */
11833 +       /* file->f_ra = h_file->f_ra; */
11834 +       /* update without lock, I don't think it a problem */
11835 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
11836 +       fput(h_file);
11837 +
11838 +out:
11839 +       si_read_unlock(sb);
11840 +       return err;
11841 +}
11842 +
11843 +static ssize_t
11844 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
11845 +                 size_t len, unsigned int flags)
11846 +{
11847 +       ssize_t err;
11848 +       struct au_pin pin;
11849 +       struct dentry *dentry;
11850 +       struct inode *inode;
11851 +       struct file *h_file;
11852 +       struct super_block *sb;
11853 +
11854 +       dentry = file->f_dentry;
11855 +       sb = dentry->d_sb;
11856 +       inode = dentry->d_inode;
11857 +       au_mtx_and_read_lock(inode);
11858 +
11859 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11860 +       if (unlikely(err))
11861 +               goto out;
11862 +
11863 +       err = au_ready_to_write(file, -1, &pin);
11864 +       di_downgrade_lock(dentry, AuLock_IR);
11865 +       if (unlikely(err)) {
11866 +               di_read_unlock(dentry, AuLock_IR);
11867 +               fi_write_unlock(file);
11868 +               goto out;
11869 +       }
11870 +
11871 +       h_file = au_hf_top(file);
11872 +       get_file(h_file);
11873 +       au_unpin(&pin);
11874 +       di_read_unlock(dentry, AuLock_IR);
11875 +       fi_write_unlock(file);
11876 +
11877 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
11878 +       ii_write_lock_child(inode);
11879 +       au_cpup_attr_timesizes(inode);
11880 +       inode->i_mode = file_inode(h_file)->i_mode;
11881 +       ii_write_unlock(inode);
11882 +       fput(h_file);
11883 +
11884 +out:
11885 +       si_read_unlock(sb);
11886 +       mutex_unlock(&inode->i_mutex);
11887 +       return err;
11888 +}
11889 +
11890 +/* ---------------------------------------------------------------------- */
11891 +
11892 +/*
11893 + * The locking order around current->mmap_sem.
11894 + * - in most and regular cases
11895 + *   file I/O syscall -- aufs_read() or something
11896 + *     -- si_rwsem for read -- mmap_sem
11897 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
11898 + * - in mmap case
11899 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
11900 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
11901 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
11902 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
11903 + * It means that when aufs acquires si_rwsem for write, the process should never
11904 + * acquire mmap_sem.
11905 + *
11906 + * Actually aufs_readdir() holds [fdi]i_rwsem before mmap_sem, but this is not a
11907 + * problem either since any directory is not able to be mmap-ed.
11908 + * The similar scenario is applied to aufs_readlink() too.
11909 + */
11910 +
11911 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
11912 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
11913 +
11914 +static unsigned long au_arch_prot_conv(unsigned long flags)
11915 +{
11916 +       /* currently ppc64 only */
11917 +#ifdef CONFIG_PPC64
11918 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
11919 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
11920 +       return AuConv_VM_PROT(flags, SAO);
11921 +#else
11922 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
11923 +       return 0;
11924 +#endif
11925 +}
11926 +
11927 +static unsigned long au_prot_conv(unsigned long flags)
11928 +{
11929 +       return AuConv_VM_PROT(flags, READ)
11930 +               | AuConv_VM_PROT(flags, WRITE)
11931 +               | AuConv_VM_PROT(flags, EXEC)
11932 +               | au_arch_prot_conv(flags);
11933 +}
11934 +
11935 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
11936 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
11937 +
11938 +static unsigned long au_flag_conv(unsigned long flags)
11939 +{
11940 +       return AuConv_VM_MAP(flags, GROWSDOWN)
11941 +               | AuConv_VM_MAP(flags, DENYWRITE)
11942 +               | AuConv_VM_MAP(flags, LOCKED);
11943 +}
11944 +
11945 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
11946 +{
11947 +       int err;
11948 +       aufs_bindex_t bstart;
11949 +       const unsigned char wlock
11950 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
11951 +       struct dentry *dentry;
11952 +       struct super_block *sb;
11953 +       struct file *h_file;
11954 +       struct au_branch *br;
11955 +       struct au_pin pin;
11956 +
11957 +       AuDbgVmRegion(file, vma);
11958 +
11959 +       dentry = file->f_dentry;
11960 +       sb = dentry->d_sb;
11961 +       lockdep_off();
11962 +       si_read_lock(sb, AuLock_NOPLMW);
11963 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
11964 +       if (unlikely(err))
11965 +               goto out;
11966 +
11967 +       if (wlock) {
11968 +               err = au_ready_to_write(file, -1, &pin);
11969 +               di_write_unlock(dentry);
11970 +               if (unlikely(err)) {
11971 +                       fi_write_unlock(file);
11972 +                       goto out;
11973 +               }
11974 +               au_unpin(&pin);
11975 +       } else
11976 +               di_write_unlock(dentry);
11977 +
11978 +       bstart = au_fbstart(file);
11979 +       br = au_sbr(sb, bstart);
11980 +       h_file = au_hf_top(file);
11981 +       get_file(h_file);
11982 +       au_set_mmapped(file);
11983 +       fi_write_unlock(file);
11984 +       lockdep_on();
11985 +
11986 +       au_vm_file_reset(vma, h_file);
11987 +       err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
11988 +                                au_flag_conv(vma->vm_flags));
11989 +       if (!err)
11990 +               err = h_file->f_op->mmap(h_file, vma);
11991 +       if (unlikely(err))
11992 +               goto out_reset;
11993 +
11994 +       au_vm_prfile_set(vma, file);
11995 +       /* update without lock, I don't think it a problem */
11996 +       fsstack_copy_attr_atime(file_inode(file), file_inode(h_file));
11997 +       goto out_fput; /* success */
11998 +
11999 +out_reset:
12000 +       au_unset_mmapped(file);
12001 +       au_vm_file_reset(vma, file);
12002 +out_fput:
12003 +       fput(h_file);
12004 +       lockdep_off();
12005 +out:
12006 +       si_read_unlock(sb);
12007 +       lockdep_on();
12008 +       AuTraceErr(err);
12009 +       return err;
12010 +}
12011 +
12012 +/* ---------------------------------------------------------------------- */
12013 +
12014 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
12015 +                            int datasync)
12016 +{
12017 +       int err;
12018 +       struct au_pin pin;
12019 +       struct dentry *dentry;
12020 +       struct inode *inode;
12021 +       struct file *h_file;
12022 +       struct super_block *sb;
12023 +
12024 +       dentry = file->f_dentry;
12025 +       inode = dentry->d_inode;
12026 +       sb = dentry->d_sb;
12027 +       mutex_lock(&inode->i_mutex);
12028 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
12029 +       if (unlikely(err))
12030 +               goto out;
12031 +
12032 +       err = 0; /* -EBADF; */ /* posix? */
12033 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
12034 +               goto out_si;
12035 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12036 +       if (unlikely(err))
12037 +               goto out_si;
12038 +
12039 +       err = au_ready_to_write(file, -1, &pin);
12040 +       di_downgrade_lock(dentry, AuLock_IR);
12041 +       if (unlikely(err))
12042 +               goto out_unlock;
12043 +       au_unpin(&pin);
12044 +
12045 +       err = -EINVAL;
12046 +       h_file = au_hf_top(file);
12047 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
12048 +       au_cpup_attr_timesizes(inode);
12049 +
12050 +out_unlock:
12051 +       di_read_unlock(dentry, AuLock_IR);
12052 +       fi_write_unlock(file);
12053 +out_si:
12054 +       si_read_unlock(sb);
12055 +out:
12056 +       mutex_unlock(&inode->i_mutex);
12057 +       return err;
12058 +}
12059 +
12060 +/* no one supports this operation, currently */
12061 +#if 0
12062 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
12063 +{
12064 +       int err;
12065 +       struct au_pin pin;
12066 +       struct dentry *dentry;
12067 +       struct inode *inode;
12068 +       struct file *file, *h_file;
12069 +
12070 +       file = kio->ki_filp;
12071 +       dentry = file->f_dentry;
12072 +       inode = dentry->d_inode;
12073 +       au_mtx_and_read_lock(inode);
12074 +
12075 +       err = 0; /* -EBADF; */ /* posix? */
12076 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
12077 +               goto out;
12078 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12079 +       if (unlikely(err))
12080 +               goto out;
12081 +
12082 +       err = au_ready_to_write(file, -1, &pin);
12083 +       di_downgrade_lock(dentry, AuLock_IR);
12084 +       if (unlikely(err))
12085 +               goto out_unlock;
12086 +       au_unpin(&pin);
12087 +
12088 +       err = -ENOSYS;
12089 +       h_file = au_hf_top(file);
12090 +       if (h_file->f_op && h_file->f_op->aio_fsync) {
12091 +               struct mutex *h_mtx;
12092 +
12093 +               h_mtx = &file_inode(h_file)->i_mutex;
12094 +               if (!is_sync_kiocb(kio)) {
12095 +                       get_file(h_file);
12096 +                       fput(file);
12097 +               }
12098 +               kio->ki_filp = h_file;
12099 +               err = h_file->f_op->aio_fsync(kio, datasync);
12100 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
12101 +               if (!err)
12102 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
12103 +               /*ignore*/
12104 +               au_cpup_attr_timesizes(inode);
12105 +               mutex_unlock(h_mtx);
12106 +       }
12107 +
12108 +out_unlock:
12109 +       di_read_unlock(dentry, AuLock_IR);
12110 +       fi_write_unlock(file);
12111 +out:
12112 +       si_read_unlock(inode->sb);
12113 +       mutex_unlock(&inode->i_mutex);
12114 +       return err;
12115 +}
12116 +#endif
12117 +
12118 +static int aufs_fasync(int fd, struct file *file, int flag)
12119 +{
12120 +       int err;
12121 +       struct file *h_file;
12122 +       struct dentry *dentry;
12123 +       struct super_block *sb;
12124 +
12125 +       dentry = file->f_dentry;
12126 +       sb = dentry->d_sb;
12127 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
12128 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
12129 +       if (unlikely(err))
12130 +               goto out;
12131 +
12132 +       h_file = au_hf_top(file);
12133 +       if (h_file->f_op && h_file->f_op->fasync)
12134 +               err = h_file->f_op->fasync(fd, h_file, flag);
12135 +
12136 +       di_read_unlock(dentry, AuLock_IR);
12137 +       fi_read_unlock(file);
12138 +
12139 +out:
12140 +       si_read_unlock(sb);
12141 +       return err;
12142 +}
12143 +
12144 +/* ---------------------------------------------------------------------- */
12145 +
12146 +/* no one supports this operation, currently */
12147 +#if 0
12148 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
12149 +                            size_t len, loff_t *pos , int more)
12150 +{
12151 +}
12152 +#endif
12153 +
12154 +/* ---------------------------------------------------------------------- */
12155 +
12156 +const struct file_operations aufs_file_fop = {
12157 +       .owner          = THIS_MODULE,
12158 +
12159 +       .llseek         = default_llseek,
12160 +
12161 +       .read           = aufs_read,
12162 +       .write          = aufs_write,
12163 +       .aio_read       = aufs_aio_read,
12164 +       .aio_write      = aufs_aio_write,
12165 +#ifdef CONFIG_AUFS_POLL
12166 +       .poll           = aufs_poll,
12167 +#endif
12168 +       .unlocked_ioctl = aufs_ioctl_nondir,
12169 +#ifdef CONFIG_COMPAT
12170 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
12171 +#endif
12172 +       .mmap           = aufs_mmap,
12173 +       .open           = aufs_open_nondir,
12174 +       .flush          = aufs_flush_nondir,
12175 +       .release        = aufs_release_nondir,
12176 +       .fsync          = aufs_fsync_nondir,
12177 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
12178 +       .fasync         = aufs_fasync,
12179 +       /* .sendpage    = aufs_sendpage, */
12180 +       .splice_write   = aufs_splice_write,
12181 +       .splice_read    = aufs_splice_read,
12182 +#if 0
12183 +       .aio_splice_write = aufs_aio_splice_write,
12184 +       .aio_splice_read  = aufs_aio_splice_read
12185 +#endif
12186 +};
12187 diff -urN /usr/share/empty/fs/aufs/f_op_sp.c linux/fs/aufs/f_op_sp.c
12188 --- /usr/share/empty/fs/aufs/f_op_sp.c  1970-01-01 01:00:00.000000000 +0100
12189 +++ linux/fs/aufs/f_op_sp.c     2013-08-23 23:59:39.634916914 +0200
12190 @@ -0,0 +1,383 @@
12191 +/*
12192 + * Copyright (C) 2005-2013 Junjiro R. Okajima
12193 + *
12194 + * This program, aufs is free software; you can redistribute it and/or modify
12195 + * it under the terms of the GNU General Public License as published by
12196 + * the Free Software Foundation; either version 2 of the License, or
12197 + * (at your option) any later version.
12198 + *
12199 + * This program is distributed in the hope that it will be useful,
12200 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12201 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12202 + * GNU General Public License for more details.
12203 + *
12204 + * You should have received a copy of the GNU General Public License
12205 + * along with this program; if not, write to the Free Software
12206 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12207 + */
12208 +
12209 +/*
12210 + * file operations for special files.
12211 + * while they exist in aufs virtually,
12212 + * their file I/O is handled out of aufs.
12213 + */
12214 +
12215 +#include <linux/aio.h>
12216 +#include "aufs.h"
12217 +
12218 +/*
12219 + * I don't think the size of this list grows much.
12220 + * so here is a very simple list implemented in order to find finfo matching a
12221 + * given file.
12222 + */
12223 +static struct au_sphlhead au_finfo_sp = {
12224 +       .spin   = __SPIN_LOCK_INITIALIZER(au_finfo_sp.spin),
12225 +       .head   = HLIST_HEAD_INIT
12226 +};
12227 +
12228 +struct au_finfo_sp {
12229 +       struct hlist_node       hlist;
12230 +       struct file             *file;
12231 +       struct au_finfo         *finfo;
12232 +};
12233 +
12234 +struct au_finfo *au_fi_sp(struct file *file)
12235 +{
12236 +       struct au_finfo *finfo;
12237 +       struct au_finfo_sp *sp;
12238 +
12239 +       finfo = NULL;
12240 +       spin_lock(&au_finfo_sp.spin);
12241 +       hlist_for_each_entry(sp, &au_finfo_sp.head, hlist) {
12242 +               if (sp->file != file)
12243 +                       continue;
12244 +               finfo = sp->finfo;
12245 +               break;
12246 +       }
12247 +       spin_unlock(&au_finfo_sp.spin);
12248 +
12249 +       return finfo;
12250 +}
12251 +
12252 +static int au_fi_sp_add(struct file *file)
12253 +{
12254 +       int err;
12255 +       struct au_finfo_sp *sp;
12256 +
12257 +       err = -ENOMEM;
12258 +       sp = kmalloc(sizeof(*sp), GFP_NOFS);
12259 +       if (sp) {
12260 +               err = 0;
12261 +               sp->file = file;
12262 +               sp->finfo = file->private_data;
12263 +               spin_lock(&au_finfo_sp.spin);
12264 +               hlist_add_head(&sp->hlist, &au_finfo_sp.head);
12265 +               spin_unlock(&au_finfo_sp.spin);
12266 +       }
12267 +       return err;
12268 +}
12269 +
12270 +static void au_fi_sp_del(struct file *file)
12271 +{
12272 +       struct au_finfo_sp *sp, *do_free;
12273 +
12274 +       do_free = NULL;
12275 +       spin_lock(&au_finfo_sp.spin);
12276 +       hlist_for_each_entry(sp, &au_finfo_sp.head, hlist) {
12277 +               if (sp->file != file)
12278 +                       continue;
12279 +               hlist_del(&sp->hlist);
12280 +               do_free = sp;
12281 +               break;
12282 +       }
12283 +       spin_unlock(&au_finfo_sp.spin);
12284 +       kfree(do_free);
12285 +}
12286 +
12287 +/* ---------------------------------------------------------------------- */
12288 +
12289 +static ssize_t aufs_aio_read_sp(struct kiocb *kio, const struct iovec *iov,
12290 +                               unsigned long nv, loff_t pos)
12291 +{
12292 +       ssize_t err;
12293 +       aufs_bindex_t bstart;
12294 +       unsigned char wbr;
12295 +       struct file *file, *h_file;
12296 +       struct super_block *sb;
12297 +
12298 +       file = kio->ki_filp;
12299 +       sb = file->f_dentry->d_sb;
12300 +       si_read_lock(sb, AuLock_FLUSH);
12301 +       fi_read_lock(file);
12302 +       bstart = au_fbstart(file);
12303 +       h_file = au_hf_top(file);
12304 +       fi_read_unlock(file);
12305 +       wbr = !!au_br_writable(au_sbr(sb, bstart)->br_perm);
12306 +       si_read_unlock(sb);
12307 +
12308 +       /* do not change the file in kio */
12309 +       AuDebugOn(!h_file->f_op || !h_file->f_op->aio_read);
12310 +       err = h_file->f_op->aio_read(kio, iov, nv, pos);
12311 +       if (err > 0 && wbr)
12312 +               file_accessed(h_file);
12313 +
12314 +       return err;
12315 +}
12316 +
12317 +static ssize_t aufs_aio_write_sp(struct kiocb *kio, const struct iovec *iov,
12318 +                                unsigned long nv, loff_t pos)
12319 +{
12320 +       ssize_t err;
12321 +       aufs_bindex_t bstart;
12322 +       unsigned char wbr;
12323 +       struct super_block *sb;
12324 +       struct file *file, *h_file;
12325 +
12326 +       file = kio->ki_filp;
12327 +       sb = file->f_dentry->d_sb;
12328 +       si_read_lock(sb, AuLock_FLUSH);
12329 +       fi_read_lock(file);
12330 +       bstart = au_fbstart(file);
12331 +       h_file = au_hf_top(file);
12332 +       fi_read_unlock(file);
12333 +       wbr = !!au_br_writable(au_sbr(sb, bstart)->br_perm);
12334 +       si_read_unlock(sb);
12335 +
12336 +       /* do not change the file in kio */
12337 +       AuDebugOn(!h_file->f_op || !h_file->f_op->aio_write);
12338 +       err = h_file->f_op->aio_write(kio, iov, nv, pos);
12339 +       return err;
12340 +}
12341 +
12342 +/* ---------------------------------------------------------------------- */
12343 +
12344 +static int aufs_release_sp(struct inode *inode, struct file *file)
12345 +{
12346 +       int err;
12347 +       struct file *h_file;
12348 +
12349 +       fi_read_lock(file);
12350 +       h_file = au_hf_top(file);
12351 +       fi_read_unlock(file);
12352 +       /* close this fifo in aufs */
12353 +       err = h_file->f_op->release(inode, file); /* ignore */
12354 +       aufs_release_nondir(inode, file); /* ignore */
12355 +       au_fi_sp_del(file);
12356 +       return err;
12357 +}
12358 +
12359 +/* ---------------------------------------------------------------------- */
12360 +
12361 +/* currently, support only FIFO */
12362 +enum {
12363 +       AuSp_FIFO, AuSp_FIFO_R, AuSp_FIFO_W, AuSp_FIFO_RW,
12364 +       /* AuSp_SOCK, AuSp_CHR, AuSp_BLK, */
12365 +       AuSp_Last
12366 +};
12367 +static int aufs_open_sp(struct inode *inode, struct file *file);
12368 +static struct au_sp_fop {
12369 +       int                     done;
12370 +       struct file_operations  fop;    /* not 'const' */
12371 +       spinlock_t              spin;
12372 +} au_sp_fop[AuSp_Last] = {
12373 +       [AuSp_FIFO] = {
12374 +               .fop    = {
12375 +                       .owner  = THIS_MODULE,
12376 +                       .open   = aufs_open_sp
12377 +               }
12378 +       }
12379 +};
12380 +
12381 +static void au_init_fop_sp(struct file *file)
12382 +{
12383 +       struct au_sp_fop *p;
12384 +       int i;
12385 +       struct file *h_file;
12386 +
12387 +       p = au_sp_fop;
12388 +       if (unlikely(!p->done)) {
12389 +               /* initialize first time only */
12390 +               static DEFINE_SPINLOCK(spin);
12391 +
12392 +               spin_lock(&spin);
12393 +               if (!p->done) {
12394 +                       BUILD_BUG_ON(sizeof(au_sp_fop)/sizeof(*au_sp_fop)
12395 +                                    != AuSp_Last);
12396 +                       for (i = 0; i < AuSp_Last; i++)
12397 +                               spin_lock_init(&p[i].spin);
12398 +                       p->done = 1;
12399 +               }
12400 +               spin_unlock(&spin);
12401 +       }
12402 +
12403 +       switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
12404 +       case FMODE_READ:
12405 +               i = AuSp_FIFO_R;
12406 +               break;
12407 +       case FMODE_WRITE:
12408 +               i = AuSp_FIFO_W;
12409 +               break;
12410 +       case FMODE_READ | FMODE_WRITE:
12411 +               i = AuSp_FIFO_RW;
12412 +               break;
12413 +       default:
12414 +               BUG();
12415 +       }
12416 +
12417 +       p += i;
12418 +       if (unlikely(!p->done)) {
12419 +               /* initialize first time only */
12420 +               h_file = au_hf_top(file);
12421 +               spin_lock(&p->spin);
12422 +               if (!p->done) {
12423 +                       p->fop = *h_file->f_op;
12424 +                       p->fop.owner = THIS_MODULE;
12425 +                       if (p->fop.aio_read)
12426 +                               p->fop.aio_read = aufs_aio_read_sp;
12427 +                       if (p->fop.aio_write)
12428 +                               p->fop.aio_write = aufs_aio_write_sp;
12429 +                       p->fop.release = aufs_release_sp;
12430 +                       p->done = 1;
12431 +               }
12432 +               spin_unlock(&p->spin);
12433 +       }
12434 +       file->f_op = &p->fop;
12435 +}
12436 +
12437 +static int au_cpup_sp(struct dentry *dentry)
12438 +{
12439 +       int err;
12440 +       struct au_pin pin;
12441 +       struct au_wr_dir_args wr_dir_args = {
12442 +               .force_btgt     = -1,
12443 +               .flags          = 0
12444 +       };
12445 +       struct au_cp_generic cpg = {
12446 +               .dentry = dentry,
12447 +               .bdst   = -1,
12448 +               .bsrc   = -1,
12449 +               .len    = -1,
12450 +               .pin    = &pin,
12451 +               .flags  = AuCpup_DTIME
12452 +       };
12453 +
12454 +       AuDbg("%.*s\n", AuDLNPair(dentry));
12455 +
12456 +       di_read_unlock(dentry, AuLock_IR);
12457 +       di_write_lock_child(dentry);
12458 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
12459 +       if (unlikely(err < 0))
12460 +               goto out;
12461 +       cpg.bdst = err;
12462 +       err = 0;
12463 +       if (cpg.bdst == au_dbstart(dentry))
12464 +               goto out; /* success */
12465 +
12466 +       err = au_pin(&pin, dentry, cpg.bdst, au_opt_udba(dentry->d_sb),
12467 +                    AuPin_MNT_WRITE);
12468 +       if (!err) {
12469 +               err = au_sio_cpup_simple(&cpg);
12470 +               au_unpin(&pin);
12471 +       }
12472 +
12473 +out:
12474 +       di_downgrade_lock(dentry, AuLock_IR);
12475 +       return err;
12476 +}
12477 +
12478 +static int au_do_open_sp(struct file *file, int flags)
12479 +{
12480 +       int err;
12481 +       struct dentry *dentry;
12482 +       struct super_block *sb;
12483 +       struct file *h_file;
12484 +       struct inode *h_inode;
12485 +
12486 +       err = au_fi_sp_add(file);
12487 +       if (unlikely(err))
12488 +               goto out;
12489 +
12490 +       dentry = file->f_dentry;
12491 +       AuDbg("%.*s\n", AuDLNPair(dentry));
12492 +
12493 +       /*
12494 +        * try copying-up.
12495 +        * operate on the ro branch is not an error.
12496 +        */
12497 +       au_cpup_sp(dentry); /* ignore */
12498 +
12499 +       /* prepare h_file */
12500 +       err = au_do_open_nondir(file, vfsub_file_flags(file));
12501 +       if (unlikely(err))
12502 +               goto out_del;
12503 +
12504 +       sb = dentry->d_sb;
12505 +       h_file = au_hf_top(file);
12506 +       h_inode = file_inode(h_file);
12507 +       di_read_unlock(dentry, AuLock_IR);
12508 +       fi_write_unlock(file);
12509 +       si_read_unlock(sb);
12510 +       /* open this fifo in aufs */
12511 +       err = h_inode->i_fop->open(file_inode(file), file);
12512 +       si_noflush_read_lock(sb);
12513 +       fi_write_lock(file);
12514 +       di_read_lock_child(dentry, AuLock_IR);
12515 +       if (!err) {
12516 +               au_init_fop_sp(file);
12517 +               goto out; /* success */
12518 +       }
12519 +
12520 +out_del:
12521 +       au_fi_sp_del(file);
12522 +out:
12523 +       return err;
12524 +}
12525 +
12526 +static int aufs_open_sp(struct inode *inode, struct file *file)
12527 +{
12528 +       int err;
12529 +       struct super_block *sb;
12530 +
12531 +       sb = file->f_dentry->d_sb;
12532 +       si_read_lock(sb, AuLock_FLUSH);
12533 +       err = au_do_open(file, au_do_open_sp, /*fidir*/NULL);
12534 +       si_read_unlock(sb);
12535 +       return err;
12536 +}
12537 +
12538 +/* ---------------------------------------------------------------------- */
12539 +
12540 +void au_init_special_fop(struct inode *inode, umode_t mode, dev_t rdev)
12541 +{
12542 +       init_special_inode(inode, mode, rdev);
12543 +
12544 +       switch (mode & S_IFMT) {
12545 +       case S_IFIFO:
12546 +               inode->i_fop = &au_sp_fop[AuSp_FIFO].fop;
12547 +               /*FALLTHROUGH*/
12548 +       case S_IFCHR:
12549 +       case S_IFBLK:
12550 +       case S_IFSOCK:
12551 +               break;
12552 +       default:
12553 +               AuDebugOn(1);
12554 +       }
12555 +}
12556 +
12557 +int au_special_file(umode_t mode)
12558 +{
12559 +       int ret;
12560 +
12561 +       ret = 0;
12562 +       switch (mode & S_IFMT) {
12563 +       case S_IFIFO:
12564 +#if 0
12565 +       case S_IFCHR:
12566 +       case S_IFBLK:
12567 +       case S_IFSOCK:
12568 +#endif
12569 +               ret = 1;
12570 +       }
12571 +
12572 +       return ret;
12573 +}
12574 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
12575 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
12576 +++ linux/fs/aufs/fstype.h      2013-08-23 23:59:39.634916914 +0200
12577 @@ -0,0 +1,470 @@
12578 +/*
12579 + * Copyright (C) 2005-2013 Junjiro R. Okajima
12580 + *
12581 + * This program, aufs is free software; you can redistribute it and/or modify
12582 + * it under the terms of the GNU General Public License as published by
12583 + * the Free Software Foundation; either version 2 of the License, or
12584 + * (at your option) any later version.
12585 + *
12586 + * This program is distributed in the hope that it will be useful,
12587 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12588 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12589 + * GNU General Public License for more details.
12590 + *
12591 + * You should have received a copy of the GNU General Public License
12592 + * along with this program; if not, write to the Free Software
12593 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
12594 + */
12595 +
12596 +/*
12597 + * judging filesystem type
12598 + */
12599 +
12600 +#ifndef __AUFS_FSTYPE_H__
12601 +#define __AUFS_FSTYPE_H__
12602 +
12603 +#ifdef __KERNEL__
12604 +
12605 +#include <linux/fs.h>
12606 +#include <linux/magic.h>
12607 +#include <linux/romfs_fs.h>
12608 +
12609 +static inline int au_test_aufs(struct super_block *sb)
12610 +{
12611 +       return sb->s_magic == AUFS_SUPER_MAGIC;
12612 +}
12613 +
12614 +static inline const char *au_sbtype(struct super_block *sb)
12615 +{
12616 +       return sb->s_type->name;
12617 +}
12618 +
12619 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
12620 +{
12621 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
12622 +       return sb->s_magic == ROMFS_MAGIC;
12623 +#else
12624 +       return 0;
12625 +#endif
12626 +}
12627 +
12628 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
12629 +{
12630 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
12631 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
12632 +#else
12633 +       return 0;
12634 +#endif
12635 +}
12636 +
12637 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
12638 +{
12639 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
12640 +       return sb->s_magic == CRAMFS_MAGIC;
12641 +#endif
12642 +       return 0;
12643 +}
12644 +
12645 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
12646 +{
12647 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
12648 +       return sb->s_magic == NFS_SUPER_MAGIC;
12649 +#else
12650 +       return 0;
12651 +#endif
12652 +}
12653 +
12654 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
12655 +{
12656 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
12657 +       return sb->s_magic == FUSE_SUPER_MAGIC;
12658 +#else
12659 +       return 0;
12660 +#endif
12661 +}
12662 +
12663 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
12664 +{
12665 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
12666 +       return sb->s_magic == XFS_SB_MAGIC;
12667 +#else
12668 +       return 0;
12669 +#endif
12670 +}
12671 +
12672 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
12673 +{
12674 +#ifdef CONFIG_TMPFS
12675 +       return sb->s_magic == TMPFS_MAGIC;
12676 +#else
12677 +       return 0;
12678 +#endif
12679 +}
12680 +
12681 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
12682 +{
12683 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
12684 +       return !strcmp(au_sbtype(sb), "ecryptfs");
12685 +#else
12686 +       return 0;
12687 +#endif
12688 +}
12689 +
12690 +static inline int au_test_ocfs2(struct super_block *sb __maybe_unused)
12691 +{
12692 +#if defined(CONFIG_OCFS2_FS) || defined(CONFIG_OCFS2_FS_MODULE)
12693 +       return sb->s_magic == OCFS2_SUPER_MAGIC;
12694 +#else
12695 +       return 0;
12696 +#endif
12697 +}
12698 +
12699 +static inline int au_test_ocfs2_dlmfs(struct super_block *sb __maybe_unused)
12700 +{
12701 +#if defined(CONFIG_OCFS2_FS_O2CB) || defined(CONFIG_OCFS2_FS_O2CB_MODULE)
12702 +       return sb->s_magic == DLMFS_MAGIC;
12703 +#else
12704 +       return 0;
12705 +#endif
12706 +}
12707 +
12708 +static inline int au_test_coda(struct super_block *sb __maybe_unused)
12709 +{
12710 +#if defined(CONFIG_CODA_FS) || defined(CONFIG_CODA_FS_MODULE)
12711 +       return sb->s_magic == CODA_SUPER_MAGIC;
12712 +#else
12713 +       return 0;
12714 +#endif
12715 +}
12716 +
12717 +static inline int au_test_v9fs(struct super_block *sb __maybe_unused)
12718 +{
12719 +#if defined(CONFIG_9P_FS) || defined(CONFIG_9P_FS_MODULE)
12720 +       return sb->s_magic == V9FS_MAGIC;
12721 +#else
12722 +       return 0;
12723 +#endif
12724 +}
12725 +
12726 +static inline int au_test_ext4(struct super_block *sb __maybe_unused)
12727 +{
12728 +#if defined(CONFIG_EXT4_FS) || defined(CONFIG_EXT4_FS_MODULE)
12729 +       return sb->s_magic == EXT4_SUPER_MAGIC;
12730 +#else
12731 +       return 0;
12732 +#endif
12733 +}
12734 +
12735 +static inline int au_test_sysv(struct super_block *sb __maybe_unused)
12736 +{
12737 +#if defined(CONFIG_SYSV_FS) || defined(CONFIG_SYSV_FS_MODULE)
12738 +       return !strcmp(au_sbtype(sb), "sysv");
12739 +#else
12740 +       return 0;
12741 +#endif
12742 +}
12743 +
12744 +static inline int au_test_ramfs(struct super_block *sb)
12745 +{
12746 +       return sb->s_magic == RAMFS_MAGIC;
12747 +}
12748 +
12749 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
12750 +{
12751 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
12752 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
12753 +#else
12754 +       return 0;
12755 +#endif
12756 +}
12757 +
12758 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
12759 +{
12760 +#ifdef CONFIG_PROC_FS
12761 +       return sb->s_magic == PROC_SUPER_MAGIC;
12762 +#else
12763 +       return 0;
12764 +#endif
12765 +}
12766 +
12767 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
12768 +{
12769 +#ifdef CONFIG_SYSFS
12770 +       return sb->s_magic == SYSFS_MAGIC;
12771 +#else
12772 +       return 0;
12773 +#endif
12774 +}
12775 +
12776 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
12777 +{
12778 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
12779 +       return sb->s_magic == CONFIGFS_MAGIC;
12780 +#else
12781 +       return 0;
12782 +#endif
12783 +}
12784 +
12785 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
12786 +{
12787 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
12788 +       return sb->s_magic == MINIX3_SUPER_MAGIC
12789 +               || sb->s_magic == MINIX2_SUPER_MAGIC
12790 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
12791 +               || sb->s_magic == MINIX_SUPER_MAGIC
12792 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
12793 +#else
12794 +       return 0;
12795 +#endif
12796 +}
12797 +
12798 +static inline int au_test_cifs(struct super_block *sb __maybe_unused)
12799 +{
12800 +#if defined(CONFIG_CIFS_FS) || defined(CONFIGCIFS_FS_MODULE)
12801 +       return sb->s_magic == CIFS_MAGIC_NUMBER;
12802 +#else
12803 +       return 0;
12804 +#endif
12805 +}
12806 +
12807 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
12808 +{
12809 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
12810 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
12811 +#else
12812 +       return 0;
12813 +#endif
12814 +}
12815 +
12816 +static inline int au_test_msdos(struct super_block *sb)
12817 +{
12818 +       return au_test_fat(sb);
12819 +}
12820 +
12821 +static inline int au_test_vfat(struct super_block *sb)
12822 +{
12823 +       return au_test_fat(sb);
12824 +}
12825 +
12826 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
12827 +{
12828 +#ifdef CONFIG_SECURITYFS
12829 +       return sb->s_magic == SECURITYFS_MAGIC;
12830 +#else
12831 +       return 0;
12832 +#endif
12833 +}
12834 +
12835 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
12836 +{
12837 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
12838 +       return sb->s_magic == SQUASHFS_MAGIC;
12839 +#else
12840 +       return 0;
12841 +#endif
12842 +}
12843 +
12844 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
12845 +{
12846 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
12847 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
12848 +#else
12849 +       return 0;
12850 +#endif
12851 +}
12852 +
12853 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
12854 +{
12855 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
12856 +       return sb->s_magic == XENFS_SUPER_MAGIC;
12857 +#else
12858 +       return 0;
12859 +#endif
12860 +}
12861 +
12862 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
12863 +{
12864 +#ifdef CONFIG_DEBUG_FS
12865 +       return sb->s_magic == DEBUGFS_MAGIC;
12866 +#else
12867 +       return 0;
12868 +#endif
12869 +}
12870 +
12871 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
12872 +{
12873 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
12874 +       return sb->s_magic == NILFS_SUPER_MAGIC;
12875 +#else
12876 +       return 0;
12877 +#endif
12878 +}
12879 +
12880 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
12881 +{
12882 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
12883 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
12884 +#else
12885 +       return 0;
12886 +#endif
12887 +}
12888 +
12889 +/* ---------------------------------------------------------------------- */
12890 +/*
12891 + * they can't be an aufs branch.
12892 + */
12893 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
12894 +{
12895 +       return
12896 +#ifndef CONFIG_AUFS_BR_RAMFS
12897 +               au_test_ramfs(sb) ||
12898 +#endif
12899 +               au_test_procfs(sb)
12900 +               || au_test_sysfs(sb)
12901 +               || au_test_configfs(sb)
12902 +               || au_test_debugfs(sb)
12903 +               || au_test_securityfs(sb)
12904 +               || au_test_xenfs(sb)
12905 +               || au_test_ecryptfs(sb)
12906 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
12907 +               || au_test_aufs(sb); /* will be supported in next version */
12908 +}
12909 +
12910 +static inline int au_test_fs_remote(struct super_block *sb)
12911 +{
12912 +       return !au_test_tmpfs(sb)
12913 +#ifdef CONFIG_AUFS_BR_RAMFS
12914 +               && !au_test_ramfs(sb)
12915 +#endif
12916 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
12917 +}
12918 +
12919 +/* ---------------------------------------------------------------------- */
12920 +
12921 +/*
12922 + * Note: these functions (below) are created after reading ->getattr() in all
12923 + * filesystems under linux/fs. it means we have to do so in every update...
12924 + */
12925 +
12926 +/*
12927 + * some filesystems require getattr to refresh the inode attributes before
12928 + * referencing.
12929 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
12930 + * and leave the work for d_revalidate()
12931 + */
12932 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
12933 +{
12934 +       return au_test_nfs(sb)
12935 +               || au_test_fuse(sb)
12936 +               /* || au_test_ocfs2(sb) */      /* untested */
12937 +               /* || au_test_btrfs(sb) */      /* untested */
12938 +               /* || au_test_coda(sb) */       /* untested */
12939 +               /* || au_test_v9fs(sb) */       /* untested */
12940 +               ;
12941 +}
12942 +
12943 +/*
12944 + * filesystems which don't maintain i_size or i_blocks.
12945 + */
12946 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
12947 +{
12948 +       return au_test_xfs(sb)
12949 +               || au_test_btrfs(sb)
12950 +               || au_test_ubifs(sb)
12951 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
12952 +               /* || au_test_ext4(sb) */       /* untested */
12953 +               /* || au_test_ocfs2(sb) */      /* untested */
12954 +               /* || au_test_ocfs2_dlmfs(sb) */ /* untested */
12955 +               /* || au_test_sysv(sb) */       /* untested */
12956 +               /* || au_test_minix(sb) */      /* untested */
12957 +               ;
12958 +}
12959 +
12960 +/*
12961 + * filesystems which don't store the correct value in some of their inode
12962 + * attributes.
12963 + */
12964 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
12965 +{
12966 +       return au_test_fs_bad_iattr_size(sb)
12967 +               /* || au_test_cifs(sb) */       /* untested */
12968 +               || au_test_fat(sb)
12969 +               || au_test_msdos(sb)
12970 +               || au_test_vfat(sb);
12971 +}
12972 +
12973 +/* they don't check i_nlink in link(2) */
12974 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
12975 +{
12976 +       return au_test_tmpfs(sb)
12977 +#ifdef CONFIG_AUFS_BR_RAMFS
12978 +               || au_test_ramfs(sb)
12979 +#endif
12980 +               || au_test_ubifs(sb)
12981 +               || au_test_hfsplus(sb);
12982 +}
12983 +
12984 +/*
12985 + * filesystems which sets S_NOATIME and S_NOCMTIME.
12986 + */
12987 +static inline int au_test_fs_notime(struct super_block *sb)
12988 +{
12989 +       return au_test_nfs(sb)
12990 +               || au_test_fuse(sb)
12991 +               || au_test_ubifs(sb)
12992 +               /* || au_test_cifs(sb) */       /* untested */
12993 +               ;
12994 +}
12995 +
12996 +/*
12997 + * filesystems which requires replacing i_mapping.
12998 + */
12999 +static inline int au_test_fs_bad_mapping(struct super_block *sb)
13000 +{
13001 +       return au_test_fuse(sb)
13002 +               || au_test_ubifs(sb);
13003 +}
13004 +
13005 +/* temporary support for i#1 in cramfs */
13006 +static inline int au_test_fs_unique_ino(struct inode *inode)
13007 +{
13008 +       if (au_test_cramfs(inode->i_sb))
13009 +               return inode->i_ino != 1;
13010 +       return 1;
13011 +}
13012 +
13013 +/* ---------------------------------------------------------------------- */
13014 +
13015 +/*
13016 + * the filesystem where the xino files placed must support i/o after unlink and
13017 + * maintain i_size and i_blocks.
13018 + */
13019 +static inline int au_test_fs_bad_xino(struct super_block *sb)
13020 +{
13021 +       return au_test_fs_remote(sb)
13022 +               || au_test_fs_bad_iattr_size(sb)
13023 +               /* don't want unnecessary work for xino */
13024 +               || au_test_aufs(sb)
13025 +               || au_test_ecryptfs(sb)
13026 +               || au_test_nilfs(sb);
13027 +}
13028 +
13029 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
13030 +{
13031 +       return au_test_tmpfs(sb)
13032 +               || au_test_ramfs(sb);
13033 +}
13034 +
13035 +/*
13036 + * test if the @sb is real-readonly.
13037 + */
13038 +static inline int au_test_fs_rr(struct super_block *sb)
13039 +{
13040 +       return au_test_squashfs(sb)
13041 +               || au_test_iso9660(sb)
13042 +               || au_test_cramfs(sb)
13043 +               || au_test_romfs(sb);
13044 +}
13045 +
13046 +#endif /* __KERNEL__ */
13047 +#endif /* __AUFS_FSTYPE_H__ */
13048 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
13049 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
13050 +++ linux/fs/aufs/hfsnotify.c   2013-07-06 13:20:47.750198454 +0200
13051 @@ -0,0 +1,296 @@
13052 +/*
13053 + * Copyright (C) 2005-2013 Junjiro R. Okajima
13054 + *
13055 + * This program, aufs is free software; you can redistribute it and/or modify
13056 + * it under the terms of the GNU General Public License as published by
13057 + * the Free Software Foundation; either version 2 of the License, or
13058 + * (at your option) any later version.
13059 + *
13060 + * This program is distributed in the hope that it will be useful,
13061 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13062 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13063 + * GNU General Public License for more details.
13064 + *
13065 + * You should have received a copy of the GNU General Public License
13066 + * along with this program; if not, write to the Free Software
13067 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13068 + */
13069 +
13070 +/*
13071 + * fsnotify for the lower directories
13072 + */
13073 +
13074 +#include "aufs.h"
13075 +
13076 +/* FS_IN_IGNORED is unnecessary */
13077 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
13078 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
13079 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
13080 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
13081 +
13082 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
13083 +{
13084 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
13085 +                                            hn_mark);
13086 +       AuDbg("here\n");
13087 +       au_cache_free_hnotify(hn);
13088 +       smp_mb__before_atomic_dec();
13089 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
13090 +               wake_up(&au_hfsn_wq);
13091 +}
13092 +
13093 +static int au_hfsn_alloc(struct au_hinode *hinode)
13094 +{
13095 +       int err;
13096 +       struct au_hnotify *hn;
13097 +       struct super_block *sb;
13098 +       struct au_branch *br;
13099 +       struct fsnotify_mark *mark;
13100 +       aufs_bindex_t bindex;
13101 +
13102 +       hn = hinode->hi_notify;
13103 +       sb = hn->hn_aufs_inode->i_sb;
13104 +       bindex = au_br_index(sb, hinode->hi_id);
13105 +       br = au_sbr(sb, bindex);
13106 +       AuDebugOn(!br->br_hfsn);
13107 +
13108 +       mark = &hn->hn_mark;
13109 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
13110 +       mark->mask = AuHfsnMask;
13111 +       /*
13112 +        * by udba rename or rmdir, aufs assign a new inode to the known
13113 +        * h_inode, so specify 1 to allow dups.
13114 +        */
13115 +       err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
13116 +                                /*mnt*/NULL, /*allow_dups*/1);
13117 +       /* even if err */
13118 +       fsnotify_put_mark(mark);
13119 +
13120 +       return err;
13121 +}
13122 +
13123 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
13124 +{
13125 +       struct fsnotify_mark *mark;
13126 +       unsigned long long ull;
13127 +       struct fsnotify_group *group;
13128 +
13129 +       ull = atomic64_inc_return(&au_hfsn_ifree);
13130 +       BUG_ON(!ull);
13131 +
13132 +       mark = &hn->hn_mark;
13133 +       spin_lock(&mark->lock);
13134 +       group = mark->group;
13135 +       fsnotify_get_group(group);
13136 +       spin_unlock(&mark->lock);
13137 +       fsnotify_destroy_mark(mark, group);
13138 +       fsnotify_put_group(group);
13139 +
13140 +       /* free hn by myself */
13141 +       return 0;
13142 +}
13143 +
13144 +/* ---------------------------------------------------------------------- */
13145 +
13146 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
13147 +{
13148 +       struct fsnotify_mark *mark;
13149 +
13150 +       mark = &hinode->hi_notify->hn_mark;
13151 +       spin_lock(&mark->lock);
13152 +       if (do_set) {
13153 +               AuDebugOn(mark->mask & AuHfsnMask);
13154 +               mark->mask |= AuHfsnMask;
13155 +       } else {
13156 +               AuDebugOn(!(mark->mask & AuHfsnMask));
13157 +               mark->mask &= ~AuHfsnMask;
13158 +       }
13159 +       spin_unlock(&mark->lock);
13160 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
13161 +}
13162 +
13163 +/* ---------------------------------------------------------------------- */
13164 +
13165 +/* #define AuDbgHnotify */
13166 +#ifdef AuDbgHnotify
13167 +static char *au_hfsn_name(u32 mask)
13168 +{
13169 +#ifdef CONFIG_AUFS_DEBUG
13170 +#define test_ret(flag)                         \
13171 +       do {                                    \
13172 +               if (mask & flag)                \
13173 +                       return #flag;           \
13174 +       } while (0)
13175 +       test_ret(FS_ACCESS);
13176 +       test_ret(FS_MODIFY);
13177 +       test_ret(FS_ATTRIB);
13178 +       test_ret(FS_CLOSE_WRITE);
13179 +       test_ret(FS_CLOSE_NOWRITE);
13180 +       test_ret(FS_OPEN);
13181 +       test_ret(FS_MOVED_FROM);
13182 +       test_ret(FS_MOVED_TO);
13183 +       test_ret(FS_CREATE);
13184 +       test_ret(FS_DELETE);
13185 +       test_ret(FS_DELETE_SELF);
13186 +       test_ret(FS_MOVE_SELF);
13187 +       test_ret(FS_UNMOUNT);
13188 +       test_ret(FS_Q_OVERFLOW);
13189 +       test_ret(FS_IN_IGNORED);
13190 +       test_ret(FS_IN_ISDIR);
13191 +       test_ret(FS_IN_ONESHOT);
13192 +       test_ret(FS_EVENT_ON_CHILD);
13193 +       return "";
13194 +#undef test_ret
13195 +#else
13196 +       return "??";
13197 +#endif
13198 +}
13199 +#endif
13200 +
13201 +/* ---------------------------------------------------------------------- */
13202 +
13203 +static void au_hfsn_free_group(struct fsnotify_group *group)
13204 +{
13205 +       struct au_br_hfsnotify *hfsn = group->private;
13206 +
13207 +       AuDbg("here\n");
13208 +       kfree(hfsn);
13209 +}
13210 +
13211 +static int au_hfsn_handle_event(struct fsnotify_group *group,
13212 +                               struct fsnotify_mark *inode_mark,
13213 +                               struct fsnotify_mark *vfsmount_mark,
13214 +                               struct fsnotify_event *event)
13215 +{
13216 +       int err;
13217 +       struct au_hnotify *hnotify;
13218 +       struct inode *h_dir, *h_inode;
13219 +       __u32 mask;
13220 +       struct qstr h_child_qstr = QSTR_INIT(event->file_name, event->name_len);
13221 +
13222 +       AuDebugOn(event->data_type != FSNOTIFY_EVENT_INODE);
13223 +
13224 +       err = 0;
13225 +       /* if FS_UNMOUNT happens, there must be another bug */
13226 +       mask = event->mask;
13227 +       AuDebugOn(mask & FS_UNMOUNT);
13228 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
13229 +               goto out;
13230 +
13231 +       h_dir = event->to_tell;
13232 +       h_inode = event->inode;
13233 +#ifdef AuDbgHnotify
13234 +       au_debug(1);
13235 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
13236 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
13237 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
13238 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
13239 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
13240 +               /* WARN_ON(1); */
13241 +       }
13242 +       au_debug(0);
13243 +#endif
13244 +
13245 +       AuDebugOn(!inode_mark);
13246 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
13247 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
13248 +
13249 +out:
13250 +       return err;
13251 +}
13252 +
13253 +/* isn't it waste to ask every registered 'group'? */
13254 +/* copied from linux/fs/notify/inotify/inotify_fsnotiry.c */
13255 +/* it should be exported to modules */
13256 +static bool au_hfsn_should_send_event(struct fsnotify_group *group,
13257 +                                     struct inode *h_inode,
13258 +                                     struct fsnotify_mark *inode_mark,
13259 +                                     struct fsnotify_mark *vfsmount_mark,
13260 +                                     __u32 mask, void *data, int data_type)
13261 +{
13262 +       mask = (mask & ~FS_EVENT_ON_CHILD);
13263 +       return inode_mark->mask & mask;
13264 +}
13265 +
13266 +static struct fsnotify_ops au_hfsn_ops = {
13267 +       .should_send_event      = au_hfsn_should_send_event,
13268 +       .handle_event           = au_hfsn_handle_event,
13269 +       .free_group_priv        = au_hfsn_free_group
13270 +};
13271 +
13272 +/* ---------------------------------------------------------------------- */
13273 +
13274 +static void au_hfsn_fin_br(struct au_branch *br)
13275 +{
13276 +       struct au_br_hfsnotify *hfsn;
13277 +
13278 +       hfsn = br->br_hfsn;
13279 +       if (hfsn)
13280 +               fsnotify_put_group(hfsn->hfsn_group);
13281 +}
13282 +
13283 +static int au_hfsn_init_br(struct au_branch *br, int perm)
13284 +{
13285 +       int err;
13286 +       struct fsnotify_group *group;
13287 +       struct au_br_hfsnotify *hfsn;
13288 +
13289 +       err = 0;
13290 +       br->br_hfsn = NULL;
13291 +       if (!au_br_hnotifyable(perm))
13292 +               goto out;
13293 +
13294 +       err = -ENOMEM;
13295 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
13296 +       if (unlikely(!hfsn))
13297 +               goto out;
13298 +
13299 +       err = 0;
13300 +       group = fsnotify_alloc_group(&au_hfsn_ops);
13301 +       if (IS_ERR(group)) {
13302 +               err = PTR_ERR(group);
13303 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
13304 +               goto out_hfsn;
13305 +       }
13306 +
13307 +       group->private = hfsn;
13308 +       hfsn->hfsn_group = group;
13309 +       br->br_hfsn = hfsn;
13310 +       goto out; /* success */
13311 +
13312 +out_hfsn:
13313 +       kfree(hfsn);
13314 +out:
13315 +       return err;
13316 +}
13317 +
13318 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
13319 +{
13320 +       int err;
13321 +
13322 +       err = 0;
13323 +       if (!br->br_hfsn)
13324 +               err = au_hfsn_init_br(br, perm);
13325 +
13326 +       return err;
13327 +}
13328 +
13329 +/* ---------------------------------------------------------------------- */
13330 +
13331 +static void au_hfsn_fin(void)
13332 +{
13333 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
13334 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
13335 +}
13336 +
13337 +const struct au_hnotify_op au_hnotify_op = {
13338 +       .ctl            = au_hfsn_ctl,
13339 +       .alloc          = au_hfsn_alloc,
13340 +       .free           = au_hfsn_free,
13341 +
13342 +       .fin            = au_hfsn_fin,
13343 +
13344 +       .reset_br       = au_hfsn_reset_br,
13345 +       .fin_br         = au_hfsn_fin_br,
13346 +       .init_br        = au_hfsn_init_br
13347 +};
13348 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
13349 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
13350 +++ linux/fs/aufs/hfsplus.c     2013-07-06 13:20:47.750198454 +0200
13351 @@ -0,0 +1,56 @@
13352 +/*
13353 + * Copyright (C) 2010-2013 Junjiro R. Okajima
13354 + *
13355 + * This program, aufs is free software; you can redistribute it and/or modify
13356 + * it under the terms of the GNU General Public License as published by
13357 + * the Free Software Foundation; either version 2 of the License, or
13358 + * (at your option) any later version.
13359 + *
13360 + * This program is distributed in the hope that it will be useful,
13361 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13362 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13363 + * GNU General Public License for more details.
13364 + *
13365 + * You should have received a copy of the GNU General Public License
13366 + * along with this program; if not, write to the Free Software
13367 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13368 + */
13369 +
13370 +/*
13371 + * special support for filesystems which aqucires an inode mutex
13372 + * at final closing a file, eg, hfsplus.
13373 + *
13374 + * This trick is very simple and stupid, just to open the file before really
13375 + * neceeary open to tell hfsplus that this is not the final closing.
13376 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
13377 + * and au_h_open_post() after releasing it.
13378 + */
13379 +
13380 +#include "aufs.h"
13381 +
13382 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex)
13383 +{
13384 +       struct file *h_file;
13385 +       struct dentry *h_dentry;
13386 +
13387 +       h_dentry = au_h_dptr(dentry, bindex);
13388 +       AuDebugOn(!h_dentry);
13389 +       AuDebugOn(!h_dentry->d_inode);
13390 +
13391 +       h_file = NULL;
13392 +       if (au_test_hfsplus(h_dentry->d_sb)
13393 +           && S_ISREG(h_dentry->d_inode->i_mode))
13394 +               h_file = au_h_open(dentry, bindex,
13395 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
13396 +                                  /*file*/NULL);
13397 +       return h_file;
13398 +}
13399 +
13400 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
13401 +                   struct file *h_file)
13402 +{
13403 +       if (h_file) {
13404 +               fput(h_file);
13405 +               au_sbr_put(dentry->d_sb, bindex);
13406 +       }
13407 +}
13408 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
13409 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
13410 +++ linux/fs/aufs/hnotify.c     2013-07-30 22:42:55.842946719 +0200
13411 @@ -0,0 +1,712 @@
13412 +/*
13413 + * Copyright (C) 2005-2013 Junjiro R. Okajima
13414 + *
13415 + * This program, aufs is free software; you can redistribute it and/or modify
13416 + * it under the terms of the GNU General Public License as published by
13417 + * the Free Software Foundation; either version 2 of the License, or
13418 + * (at your option) any later version.
13419 + *
13420 + * This program is distributed in the hope that it will be useful,
13421 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13422 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13423 + * GNU General Public License for more details.
13424 + *
13425 + * You should have received a copy of the GNU General Public License
13426 + * along with this program; if not, write to the Free Software
13427 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13428 + */
13429 +
13430 +/*
13431 + * abstraction to notify the direct changes on lower directories
13432 + */
13433 +
13434 +#include "aufs.h"
13435 +
13436 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
13437 +{
13438 +       int err;
13439 +       struct au_hnotify *hn;
13440 +
13441 +       err = -ENOMEM;
13442 +       hn = au_cache_alloc_hnotify();
13443 +       if (hn) {
13444 +               hn->hn_aufs_inode = inode;
13445 +               hinode->hi_notify = hn;
13446 +               err = au_hnotify_op.alloc(hinode);
13447 +               AuTraceErr(err);
13448 +               if (unlikely(err)) {
13449 +                       hinode->hi_notify = NULL;
13450 +                       au_cache_free_hnotify(hn);
13451 +                       /*
13452 +                        * The upper dir was removed by udba, but the same named
13453 +                        * dir left. In this case, aufs assignes a new inode
13454 +                        * number and set the monitor again.
13455 +                        * For the lower dir, the old monitnor is still left.
13456 +                        */
13457 +                       if (err == -EEXIST)
13458 +                               err = 0;
13459 +               }
13460 +       }
13461 +
13462 +       AuTraceErr(err);
13463 +       return err;
13464 +}
13465 +
13466 +void au_hn_free(struct au_hinode *hinode)
13467 +{
13468 +       struct au_hnotify *hn;
13469 +
13470 +       hn = hinode->hi_notify;
13471 +       if (hn) {
13472 +               hinode->hi_notify = NULL;
13473 +               if (au_hnotify_op.free(hinode, hn))
13474 +                       au_cache_free_hnotify(hn);
13475 +       }
13476 +}
13477 +
13478 +/* ---------------------------------------------------------------------- */
13479 +
13480 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
13481 +{
13482 +       if (hinode->hi_notify)
13483 +               au_hnotify_op.ctl(hinode, do_set);
13484 +}
13485 +
13486 +void au_hn_reset(struct inode *inode, unsigned int flags)
13487 +{
13488 +       aufs_bindex_t bindex, bend;
13489 +       struct inode *hi;
13490 +       struct dentry *iwhdentry;
13491 +
13492 +       bend = au_ibend(inode);
13493 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
13494 +               hi = au_h_iptr(inode, bindex);
13495 +               if (!hi)
13496 +                       continue;
13497 +
13498 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
13499 +               iwhdentry = au_hi_wh(inode, bindex);
13500 +               if (iwhdentry)
13501 +                       dget(iwhdentry);
13502 +               au_igrab(hi);
13503 +               au_set_h_iptr(inode, bindex, NULL, 0);
13504 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
13505 +                             flags & ~AuHi_XINO);
13506 +               iput(hi);
13507 +               dput(iwhdentry);
13508 +               /* mutex_unlock(&hi->i_mutex); */
13509 +       }
13510 +}
13511 +
13512 +/* ---------------------------------------------------------------------- */
13513 +
13514 +static int hn_xino(struct inode *inode, struct inode *h_inode)
13515 +{
13516 +       int err;
13517 +       aufs_bindex_t bindex, bend, bfound, bstart;
13518 +       struct inode *h_i;
13519 +
13520 +       err = 0;
13521 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13522 +               pr_warn("branch root dir was changed\n");
13523 +               goto out;
13524 +       }
13525 +
13526 +       bfound = -1;
13527 +       bend = au_ibend(inode);
13528 +       bstart = au_ibstart(inode);
13529 +#if 0 /* reserved for future use */
13530 +       if (bindex == bend) {
13531 +               /* keep this ino in rename case */
13532 +               goto out;
13533 +       }
13534 +#endif
13535 +       for (bindex = bstart; bindex <= bend; bindex++)
13536 +               if (au_h_iptr(inode, bindex) == h_inode) {
13537 +                       bfound = bindex;
13538 +                       break;
13539 +               }
13540 +       if (bfound < 0)
13541 +               goto out;
13542 +
13543 +       for (bindex = bstart; bindex <= bend; bindex++) {
13544 +               h_i = au_h_iptr(inode, bindex);
13545 +               if (!h_i)
13546 +                       continue;
13547 +
13548 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
13549 +               /* ignore this error */
13550 +               /* bad action? */
13551 +       }
13552 +
13553 +       /* children inode number will be broken */
13554 +
13555 +out:
13556 +       AuTraceErr(err);
13557 +       return err;
13558 +}
13559 +
13560 +static int hn_gen_tree(struct dentry *dentry)
13561 +{
13562 +       int err, i, j, ndentry;
13563 +       struct au_dcsub_pages dpages;
13564 +       struct au_dpage *dpage;
13565 +       struct dentry **dentries;
13566 +
13567 +       err = au_dpages_init(&dpages, GFP_NOFS);
13568 +       if (unlikely(err))
13569 +               goto out;
13570 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
13571 +       if (unlikely(err))
13572 +               goto out_dpages;
13573 +
13574 +       for (i = 0; i < dpages.ndpage; i++) {
13575 +               dpage = dpages.dpages + i;
13576 +               dentries = dpage->dentries;
13577 +               ndentry = dpage->ndentry;
13578 +               for (j = 0; j < ndentry; j++) {
13579 +                       struct dentry *d;
13580 +
13581 +                       d = dentries[j];
13582 +                       if (IS_ROOT(d))
13583 +                               continue;
13584 +
13585 +                       au_digen_dec(d);
13586 +                       if (d->d_inode)
13587 +                               /* todo: reset children xino?
13588 +                                  cached children only? */
13589 +                               au_iigen_dec(d->d_inode);
13590 +               }
13591 +       }
13592 +
13593 +out_dpages:
13594 +       au_dpages_free(&dpages);
13595 +
13596 +#if 0
13597 +       /* discard children */
13598 +       dentry_unhash(dentry);
13599 +       dput(dentry);
13600 +#endif
13601 +out:
13602 +       return err;
13603 +}
13604 +
13605 +/*
13606 + * return 0 if processed.
13607 + */
13608 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
13609 +                          const unsigned int isdir)
13610 +{
13611 +       int err;
13612 +       struct dentry *d;
13613 +       struct qstr *dname;
13614 +
13615 +       err = 1;
13616 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13617 +               pr_warn("branch root dir was changed\n");
13618 +               err = 0;
13619 +               goto out;
13620 +       }
13621 +
13622 +       if (!isdir) {
13623 +               AuDebugOn(!name);
13624 +               au_iigen_dec(inode);
13625 +               spin_lock(&inode->i_lock);
13626 +               hlist_for_each_entry(d, &inode->i_dentry, d_alias) {
13627 +                       spin_lock(&d->d_lock);
13628 +                       dname = &d->d_name;
13629 +                       if (dname->len != nlen
13630 +                           && memcmp(dname->name, name, nlen)) {
13631 +                               spin_unlock(&d->d_lock);
13632 +                               continue;
13633 +                       }
13634 +                       err = 0;
13635 +                       au_digen_dec(d);
13636 +                       spin_unlock(&d->d_lock);
13637 +                       break;
13638 +               }
13639 +               spin_unlock(&inode->i_lock);
13640 +       } else {
13641 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
13642 +               d = d_find_alias(inode);
13643 +               if (!d) {
13644 +                       au_iigen_dec(inode);
13645 +                       goto out;
13646 +               }
13647 +
13648 +               spin_lock(&d->d_lock);
13649 +               dname = &d->d_name;
13650 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
13651 +                       spin_unlock(&d->d_lock);
13652 +                       err = hn_gen_tree(d);
13653 +                       spin_lock(&d->d_lock);
13654 +               }
13655 +               spin_unlock(&d->d_lock);
13656 +               dput(d);
13657 +       }
13658 +
13659 +out:
13660 +       AuTraceErr(err);
13661 +       return err;
13662 +}
13663 +
13664 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
13665 +{
13666 +       int err;
13667 +       struct inode *inode;
13668 +
13669 +       inode = dentry->d_inode;
13670 +       if (IS_ROOT(dentry)
13671 +           /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
13672 +               ) {
13673 +               pr_warn("branch root dir was changed\n");
13674 +               return 0;
13675 +       }
13676 +
13677 +       err = 0;
13678 +       if (!isdir) {
13679 +               au_digen_dec(dentry);
13680 +               if (inode)
13681 +                       au_iigen_dec(inode);
13682 +       } else {
13683 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
13684 +               if (inode)
13685 +                       err = hn_gen_tree(dentry);
13686 +       }
13687 +
13688 +       AuTraceErr(err);
13689 +       return err;
13690 +}
13691 +
13692 +/* ---------------------------------------------------------------------- */
13693 +
13694 +/* hnotify job flags */
13695 +#define AuHnJob_XINO0          1
13696 +#define AuHnJob_GEN            (1 << 1)
13697 +#define AuHnJob_DIRENT         (1 << 2)
13698 +#define AuHnJob_ISDIR          (1 << 3)
13699 +#define AuHnJob_TRYXINO0       (1 << 4)
13700 +#define AuHnJob_MNTPNT         (1 << 5)
13701 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
13702 +#define au_fset_hnjob(flags, name) \
13703 +       do { (flags) |= AuHnJob_##name; } while (0)
13704 +#define au_fclr_hnjob(flags, name) \
13705 +       do { (flags) &= ~AuHnJob_##name; } while (0)
13706 +
13707 +enum {
13708 +       AuHn_CHILD,
13709 +       AuHn_PARENT,
13710 +       AuHnLast
13711 +};
13712 +
13713 +struct au_hnotify_args {
13714 +       struct inode *h_dir, *dir, *h_child_inode;
13715 +       u32 mask;
13716 +       unsigned int flags[AuHnLast];
13717 +       unsigned int h_child_nlen;
13718 +       char h_child_name[];
13719 +};
13720 +
13721 +struct hn_job_args {
13722 +       unsigned int flags;
13723 +       struct inode *inode, *h_inode, *dir, *h_dir;
13724 +       struct dentry *dentry;
13725 +       char *h_name;
13726 +       int h_nlen;
13727 +};
13728 +
13729 +static int hn_job(struct hn_job_args *a)
13730 +{
13731 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
13732 +
13733 +       /* reset xino */
13734 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
13735 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
13736 +
13737 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
13738 +           && a->inode
13739 +           && a->h_inode) {
13740 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
13741 +               if (!a->h_inode->i_nlink)
13742 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
13743 +               mutex_unlock(&a->h_inode->i_mutex);
13744 +       }
13745 +
13746 +       /* make the generation obsolete */
13747 +       if (au_ftest_hnjob(a->flags, GEN)) {
13748 +               int err = -1;
13749 +               if (a->inode)
13750 +                       err = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
13751 +                                             isdir);
13752 +               if (err && a->dentry)
13753 +                       hn_gen_by_name(a->dentry, isdir);
13754 +               /* ignore this error */
13755 +       }
13756 +
13757 +       /* make dir entries obsolete */
13758 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
13759 +               struct au_vdir *vdir;
13760 +
13761 +               vdir = au_ivdir(a->inode);
13762 +               if (vdir)
13763 +                       vdir->vd_jiffy = 0;
13764 +               /* IMustLock(a->inode); */
13765 +               /* a->inode->i_version++; */
13766 +       }
13767 +
13768 +       /* can do nothing but warn */
13769 +       if (au_ftest_hnjob(a->flags, MNTPNT)
13770 +           && a->dentry
13771 +           && d_mountpoint(a->dentry))
13772 +               pr_warn("mount-point %.*s is removed or renamed\n",
13773 +                       AuDLNPair(a->dentry));
13774 +
13775 +       return 0;
13776 +}
13777 +
13778 +/* ---------------------------------------------------------------------- */
13779 +
13780 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
13781 +                                          struct inode *dir)
13782 +{
13783 +       struct dentry *dentry, *d, *parent;
13784 +       struct qstr *dname;
13785 +
13786 +       parent = d_find_alias(dir);
13787 +       if (!parent)
13788 +               return NULL;
13789 +
13790 +       dentry = NULL;
13791 +       spin_lock(&parent->d_lock);
13792 +       list_for_each_entry(d, &parent->d_subdirs, d_u.d_child) {
13793 +               /* AuDbg("%.*s\n", AuDLNPair(d)); */
13794 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
13795 +               dname = &d->d_name;
13796 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
13797 +                       goto cont_unlock;
13798 +               if (au_di(d))
13799 +                       au_digen_dec(d);
13800 +               else
13801 +                       goto cont_unlock;
13802 +               if (d->d_count) {
13803 +                       dentry = dget_dlock(d);
13804 +                       spin_unlock(&d->d_lock);
13805 +                       break;
13806 +               }
13807 +
13808 +       cont_unlock:
13809 +               spin_unlock(&d->d_lock);
13810 +       }
13811 +       spin_unlock(&parent->d_lock);
13812 +       dput(parent);
13813 +
13814 +       if (dentry)
13815 +               di_write_lock_child(dentry);
13816 +
13817 +       return dentry;
13818 +}
13819 +
13820 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
13821 +                                        aufs_bindex_t bindex, ino_t h_ino)
13822 +{
13823 +       struct inode *inode;
13824 +       ino_t ino;
13825 +       int err;
13826 +
13827 +       inode = NULL;
13828 +       err = au_xino_read(sb, bindex, h_ino, &ino);
13829 +       if (!err && ino)
13830 +               inode = ilookup(sb, ino);
13831 +       if (!inode)
13832 +               goto out;
13833 +
13834 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13835 +               pr_warn("wrong root branch\n");
13836 +               iput(inode);
13837 +               inode = NULL;
13838 +               goto out;
13839 +       }
13840 +
13841 +       ii_write_lock_child(inode);
13842 +
13843 +out:
13844 +       return inode;
13845 +}
13846 +
13847 +static void au_hn_bh(void *_args)
13848 +{
13849 +       struct au_hnotify_args *a = _args;
13850 +       struct super_block *sb;
13851 +       aufs_bindex_t bindex, bend, bfound;
13852 +       unsigned char xino, try_iput;
13853 +       int err;
13854 +       struct inode *inode;
13855 +       ino_t h_ino;
13856 +       struct hn_job_args args;
13857 +       struct dentry *dentry;
13858 +       struct au_sbinfo *sbinfo;
13859 +
13860 +       AuDebugOn(!_args);
13861 +       AuDebugOn(!a->h_dir);
13862 +       AuDebugOn(!a->dir);
13863 +       AuDebugOn(!a->mask);
13864 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
13865 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
13866 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
13867 +
13868 +       inode = NULL;
13869 +       dentry = NULL;
13870 +       /*
13871 +        * do not lock a->dir->i_mutex here
13872 +        * because of d_revalidate() may cause a deadlock.
13873 +        */
13874 +       sb = a->dir->i_sb;
13875 +       AuDebugOn(!sb);
13876 +       sbinfo = au_sbi(sb);
13877 +       AuDebugOn(!sbinfo);
13878 +       si_write_lock(sb, AuLock_NOPLMW);
13879 +
13880 +       ii_read_lock_parent(a->dir);
13881 +       bfound = -1;
13882 +       bend = au_ibend(a->dir);
13883 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
13884 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
13885 +                       bfound = bindex;
13886 +                       break;
13887 +               }
13888 +       ii_read_unlock(a->dir);
13889 +       if (unlikely(bfound < 0))
13890 +               goto out;
13891 +
13892 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
13893 +       h_ino = 0;
13894 +       if (a->h_child_inode)
13895 +               h_ino = a->h_child_inode->i_ino;
13896 +
13897 +       if (a->h_child_nlen
13898 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
13899 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
13900 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
13901 +                                             a->dir);
13902 +       try_iput = 0;
13903 +       if (dentry)
13904 +               inode = dentry->d_inode;
13905 +       if (xino && !inode && h_ino
13906 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
13907 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
13908 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
13909 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
13910 +               try_iput = 1;
13911 +           }
13912 +
13913 +       args.flags = a->flags[AuHn_CHILD];
13914 +       args.dentry = dentry;
13915 +       args.inode = inode;
13916 +       args.h_inode = a->h_child_inode;
13917 +       args.dir = a->dir;
13918 +       args.h_dir = a->h_dir;
13919 +       args.h_name = a->h_child_name;
13920 +       args.h_nlen = a->h_child_nlen;
13921 +       err = hn_job(&args);
13922 +       if (dentry) {
13923 +               if (au_di(dentry))
13924 +                       di_write_unlock(dentry);
13925 +               dput(dentry);
13926 +       }
13927 +       if (inode && try_iput) {
13928 +               ii_write_unlock(inode);
13929 +               iput(inode);
13930 +       }
13931 +
13932 +       ii_write_lock_parent(a->dir);
13933 +       args.flags = a->flags[AuHn_PARENT];
13934 +       args.dentry = NULL;
13935 +       args.inode = a->dir;
13936 +       args.h_inode = a->h_dir;
13937 +       args.dir = NULL;
13938 +       args.h_dir = NULL;
13939 +       args.h_name = NULL;
13940 +       args.h_nlen = 0;
13941 +       err = hn_job(&args);
13942 +       ii_write_unlock(a->dir);
13943 +
13944 +out:
13945 +       iput(a->h_child_inode);
13946 +       iput(a->h_dir);
13947 +       iput(a->dir);
13948 +       si_write_unlock(sb);
13949 +       au_nwt_done(&sbinfo->si_nowait);
13950 +       kfree(a);
13951 +}
13952 +
13953 +/* ---------------------------------------------------------------------- */
13954 +
13955 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
13956 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
13957 +{
13958 +       int err, len;
13959 +       unsigned int flags[AuHnLast], f;
13960 +       unsigned char isdir, isroot, wh;
13961 +       struct inode *dir;
13962 +       struct au_hnotify_args *args;
13963 +       char *p, *h_child_name;
13964 +
13965 +       err = 0;
13966 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
13967 +       dir = igrab(hnotify->hn_aufs_inode);
13968 +       if (!dir)
13969 +               goto out;
13970 +
13971 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
13972 +       wh = 0;
13973 +       h_child_name = (void *)h_child_qstr->name;
13974 +       len = h_child_qstr->len;
13975 +       if (h_child_name) {
13976 +               if (len > AUFS_WH_PFX_LEN
13977 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
13978 +                       h_child_name += AUFS_WH_PFX_LEN;
13979 +                       len -= AUFS_WH_PFX_LEN;
13980 +                       wh = 1;
13981 +               }
13982 +       }
13983 +
13984 +       isdir = 0;
13985 +       if (h_child_inode)
13986 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
13987 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
13988 +       flags[AuHn_CHILD] = 0;
13989 +       if (isdir)
13990 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
13991 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
13992 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
13993 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
13994 +       case FS_MOVED_FROM:
13995 +       case FS_MOVED_TO:
13996 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
13997 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
13998 +               /*FALLTHROUGH*/
13999 +       case FS_CREATE:
14000 +               AuDebugOn(!h_child_name || !h_child_inode);
14001 +               break;
14002 +
14003 +       case FS_DELETE:
14004 +               /*
14005 +                * aufs never be able to get this child inode.
14006 +                * revalidation should be in d_revalidate()
14007 +                * by checking i_nlink, i_generation or d_unhashed().
14008 +                */
14009 +               AuDebugOn(!h_child_name);
14010 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
14011 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
14012 +               break;
14013 +
14014 +       default:
14015 +               AuDebugOn(1);
14016 +       }
14017 +
14018 +       if (wh)
14019 +               h_child_inode = NULL;
14020 +
14021 +       err = -ENOMEM;
14022 +       /* iput() and kfree() will be called in au_hnotify() */
14023 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
14024 +       if (unlikely(!args)) {
14025 +               AuErr1("no memory\n");
14026 +               iput(dir);
14027 +               goto out;
14028 +       }
14029 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
14030 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
14031 +       args->mask = mask;
14032 +       args->dir = dir;
14033 +       args->h_dir = igrab(h_dir);
14034 +       if (h_child_inode)
14035 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
14036 +       args->h_child_inode = h_child_inode;
14037 +       args->h_child_nlen = len;
14038 +       if (len) {
14039 +               p = (void *)args;
14040 +               p += sizeof(*args);
14041 +               memcpy(p, h_child_name, len);
14042 +               p[len] = 0;
14043 +       }
14044 +
14045 +       f = 0;
14046 +       if (!dir->i_nlink)
14047 +               f = AuWkq_NEST;
14048 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
14049 +       if (unlikely(err)) {
14050 +               pr_err("wkq %d\n", err);
14051 +               iput(args->h_child_inode);
14052 +               iput(args->h_dir);
14053 +               iput(args->dir);
14054 +               kfree(args);
14055 +       }
14056 +
14057 +out:
14058 +       return err;
14059 +}
14060 +
14061 +/* ---------------------------------------------------------------------- */
14062 +
14063 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
14064 +{
14065 +       int err;
14066 +
14067 +       AuDebugOn(!(udba & AuOptMask_UDBA));
14068 +
14069 +       err = 0;
14070 +       if (au_hnotify_op.reset_br)
14071 +               err = au_hnotify_op.reset_br(udba, br, perm);
14072 +
14073 +       return err;
14074 +}
14075 +
14076 +int au_hnotify_init_br(struct au_branch *br, int perm)
14077 +{
14078 +       int err;
14079 +
14080 +       err = 0;
14081 +       if (au_hnotify_op.init_br)
14082 +               err = au_hnotify_op.init_br(br, perm);
14083 +
14084 +       return err;
14085 +}
14086 +
14087 +void au_hnotify_fin_br(struct au_branch *br)
14088 +{
14089 +       if (au_hnotify_op.fin_br)
14090 +               au_hnotify_op.fin_br(br);
14091 +}
14092 +
14093 +static void au_hn_destroy_cache(void)
14094 +{
14095 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
14096 +       au_cachep[AuCache_HNOTIFY] = NULL;
14097 +}
14098 +
14099 +int __init au_hnotify_init(void)
14100 +{
14101 +       int err;
14102 +
14103 +       err = -ENOMEM;
14104 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
14105 +       if (au_cachep[AuCache_HNOTIFY]) {
14106 +               err = 0;
14107 +               if (au_hnotify_op.init)
14108 +                       err = au_hnotify_op.init();
14109 +               if (unlikely(err))
14110 +                       au_hn_destroy_cache();
14111 +       }
14112 +       AuTraceErr(err);
14113 +       return err;
14114 +}
14115 +
14116 +void au_hnotify_fin(void)
14117 +{
14118 +       if (au_hnotify_op.fin)
14119 +               au_hnotify_op.fin();
14120 +       /* cf. au_cache_fin() */
14121 +       if (au_cachep[AuCache_HNOTIFY])
14122 +               au_hn_destroy_cache();
14123 +}
14124 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
14125 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
14126 +++ linux/fs/aufs/iinfo.c       2013-07-06 13:20:47.750198454 +0200
14127 @@ -0,0 +1,276 @@
14128 +/*
14129 + * Copyright (C) 2005-2013 Junjiro R. Okajima
14130 + *
14131 + * This program, aufs is free software; you can redistribute it and/or modify
14132 + * it under the terms of the GNU General Public License as published by
14133 + * the Free Software Foundation; either version 2 of the License, or
14134 + * (at your option) any later version.
14135 + *
14136 + * This program is distributed in the hope that it will be useful,
14137 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14138 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14139 + * GNU General Public License for more details.
14140 + *
14141 + * You should have received a copy of the GNU General Public License
14142 + * along with this program; if not, write to the Free Software
14143 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14144 + */
14145 +
14146 +/*
14147 + * inode private data
14148 + */
14149 +
14150 +#include "aufs.h"
14151 +
14152 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
14153 +{
14154 +       struct inode *h_inode;
14155 +
14156 +       IiMustAnyLock(inode);
14157 +
14158 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
14159 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
14160 +       return h_inode;
14161 +}
14162 +
14163 +/* todo: hard/soft set? */
14164 +void au_hiput(struct au_hinode *hinode)
14165 +{
14166 +       au_hn_free(hinode);
14167 +       dput(hinode->hi_whdentry);
14168 +       iput(hinode->hi_inode);
14169 +}
14170 +
14171 +unsigned int au_hi_flags(struct inode *inode, int isdir)
14172 +{
14173 +       unsigned int flags;
14174 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
14175 +
14176 +       flags = 0;
14177 +       if (au_opt_test(mnt_flags, XINO))
14178 +               au_fset_hi(flags, XINO);
14179 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
14180 +               au_fset_hi(flags, HNOTIFY);
14181 +       return flags;
14182 +}
14183 +
14184 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
14185 +                  struct inode *h_inode, unsigned int flags)
14186 +{
14187 +       struct au_hinode *hinode;
14188 +       struct inode *hi;
14189 +       struct au_iinfo *iinfo = au_ii(inode);
14190 +
14191 +       IiMustWriteLock(inode);
14192 +
14193 +       hinode = iinfo->ii_hinode + bindex;
14194 +       hi = hinode->hi_inode;
14195 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
14196 +
14197 +       if (hi)
14198 +               au_hiput(hinode);
14199 +       hinode->hi_inode = h_inode;
14200 +       if (h_inode) {
14201 +               int err;
14202 +               struct super_block *sb = inode->i_sb;
14203 +               struct au_branch *br;
14204 +
14205 +               AuDebugOn(inode->i_mode
14206 +                         && (h_inode->i_mode & S_IFMT)
14207 +                         != (inode->i_mode & S_IFMT));
14208 +               if (bindex == iinfo->ii_bstart)
14209 +                       au_cpup_igen(inode, h_inode);
14210 +               br = au_sbr(sb, bindex);
14211 +               hinode->hi_id = br->br_id;
14212 +               if (au_ftest_hi(flags, XINO)) {
14213 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
14214 +                                           inode->i_ino);
14215 +                       if (unlikely(err))
14216 +                               AuIOErr1("failed au_xino_write() %d\n", err);
14217 +               }
14218 +
14219 +               if (au_ftest_hi(flags, HNOTIFY)
14220 +                   && au_br_hnotifyable(br->br_perm)) {
14221 +                       err = au_hn_alloc(hinode, inode);
14222 +                       if (unlikely(err))
14223 +                               AuIOErr1("au_hn_alloc() %d\n", err);
14224 +               }
14225 +       }
14226 +}
14227 +
14228 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
14229 +                 struct dentry *h_wh)
14230 +{
14231 +       struct au_hinode *hinode;
14232 +
14233 +       IiMustWriteLock(inode);
14234 +
14235 +       hinode = au_ii(inode)->ii_hinode + bindex;
14236 +       AuDebugOn(hinode->hi_whdentry);
14237 +       hinode->hi_whdentry = h_wh;
14238 +}
14239 +
14240 +void au_update_iigen(struct inode *inode, int half)
14241 +{
14242 +       struct au_iinfo *iinfo;
14243 +       struct au_iigen *iigen;
14244 +       unsigned int sigen;
14245 +
14246 +       sigen = au_sigen(inode->i_sb);
14247 +       iinfo = au_ii(inode);
14248 +       iigen = &iinfo->ii_generation;
14249 +       spin_lock(&iinfo->ii_genspin);
14250 +       iigen->ig_generation = sigen;
14251 +       if (half)
14252 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
14253 +       else
14254 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
14255 +       spin_unlock(&iinfo->ii_genspin);
14256 +}
14257 +
14258 +/* it may be called at remount time, too */
14259 +void au_update_ibrange(struct inode *inode, int do_put_zero)
14260 +{
14261 +       struct au_iinfo *iinfo;
14262 +       aufs_bindex_t bindex, bend;
14263 +
14264 +       iinfo = au_ii(inode);
14265 +       if (!iinfo)
14266 +               return;
14267 +
14268 +       IiMustWriteLock(inode);
14269 +
14270 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
14271 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
14272 +                    bindex++) {
14273 +                       struct inode *h_i;
14274 +
14275 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
14276 +                       if (h_i && !h_i->i_nlink)
14277 +                               au_set_h_iptr(inode, bindex, NULL, 0);
14278 +               }
14279 +       }
14280 +
14281 +       iinfo->ii_bstart = -1;
14282 +       iinfo->ii_bend = -1;
14283 +       bend = au_sbend(inode->i_sb);
14284 +       for (bindex = 0; bindex <= bend; bindex++)
14285 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
14286 +                       iinfo->ii_bstart = bindex;
14287 +                       break;
14288 +               }
14289 +       if (iinfo->ii_bstart >= 0)
14290 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
14291 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
14292 +                               iinfo->ii_bend = bindex;
14293 +                               break;
14294 +                       }
14295 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
14296 +}
14297 +
14298 +/* ---------------------------------------------------------------------- */
14299 +
14300 +void au_icntnr_init_once(void *_c)
14301 +{
14302 +       struct au_icntnr *c = _c;
14303 +       struct au_iinfo *iinfo = &c->iinfo;
14304 +       static struct lock_class_key aufs_ii;
14305 +
14306 +       spin_lock_init(&iinfo->ii_genspin);
14307 +       au_rw_init(&iinfo->ii_rwsem);
14308 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
14309 +       inode_init_once(&c->vfs_inode);
14310 +}
14311 +
14312 +int au_iinfo_init(struct inode *inode)
14313 +{
14314 +       struct au_iinfo *iinfo;
14315 +       struct super_block *sb;
14316 +       int nbr, i;
14317 +
14318 +       sb = inode->i_sb;
14319 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
14320 +       nbr = au_sbend(sb) + 1;
14321 +       if (unlikely(nbr <= 0))
14322 +               nbr = 1;
14323 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
14324 +       if (iinfo->ii_hinode) {
14325 +               au_ninodes_inc(sb);
14326 +               for (i = 0; i < nbr; i++)
14327 +                       iinfo->ii_hinode[i].hi_id = -1;
14328 +
14329 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
14330 +               iinfo->ii_bstart = -1;
14331 +               iinfo->ii_bend = -1;
14332 +               iinfo->ii_vdir = NULL;
14333 +               return 0;
14334 +       }
14335 +       return -ENOMEM;
14336 +}
14337 +
14338 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
14339 +{
14340 +       int err, sz;
14341 +       struct au_hinode *hip;
14342 +
14343 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
14344 +
14345 +       err = -ENOMEM;
14346 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
14347 +       if (!sz)
14348 +               sz = sizeof(*hip);
14349 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
14350 +       if (hip) {
14351 +               iinfo->ii_hinode = hip;
14352 +               err = 0;
14353 +       }
14354 +
14355 +       return err;
14356 +}
14357 +
14358 +void au_iinfo_fin(struct inode *inode)
14359 +{
14360 +       struct au_iinfo *iinfo;
14361 +       struct au_hinode *hi;
14362 +       struct super_block *sb;
14363 +       aufs_bindex_t bindex, bend;
14364 +       const unsigned char unlinked = !inode->i_nlink;
14365 +
14366 +       iinfo = au_ii(inode);
14367 +       /* bad_inode case */
14368 +       if (!iinfo)
14369 +               return;
14370 +
14371 +       sb = inode->i_sb;
14372 +       au_ninodes_dec(sb);
14373 +       if (si_pid_test(sb))
14374 +               au_xino_delete_inode(inode, unlinked);
14375 +       else {
14376 +               /*
14377 +                * it is safe to hide the dependency between sbinfo and
14378 +                * sb->s_umount.
14379 +                */
14380 +               lockdep_off();
14381 +               si_noflush_read_lock(sb);
14382 +               au_xino_delete_inode(inode, unlinked);
14383 +               si_read_unlock(sb);
14384 +               lockdep_on();
14385 +       }
14386 +
14387 +       if (iinfo->ii_vdir)
14388 +               au_vdir_free(iinfo->ii_vdir);
14389 +
14390 +       bindex = iinfo->ii_bstart;
14391 +       if (bindex >= 0) {
14392 +               hi = iinfo->ii_hinode + bindex;
14393 +               bend = iinfo->ii_bend;
14394 +               while (bindex++ <= bend) {
14395 +                       if (hi->hi_inode)
14396 +                               au_hiput(hi);
14397 +                       hi++;
14398 +               }
14399 +       }
14400 +       kfree(iinfo->ii_hinode);
14401 +       iinfo->ii_hinode = NULL;
14402 +       AuRwDestroy(&iinfo->ii_rwsem);
14403 +}
14404 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
14405 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
14406 +++ linux/fs/aufs/inode.c       2013-07-06 13:20:47.750198454 +0200
14407 @@ -0,0 +1,492 @@
14408 +/*
14409 + * Copyright (C) 2005-2013 Junjiro R. Okajima
14410 + *
14411 + * This program, aufs is free software; you can redistribute it and/or modify
14412 + * it under the terms of the GNU General Public License as published by
14413 + * the Free Software Foundation; either version 2 of the License, or
14414 + * (at your option) any later version.
14415 + *
14416 + * This program is distributed in the hope that it will be useful,
14417 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14418 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14419 + * GNU General Public License for more details.
14420 + *
14421 + * You should have received a copy of the GNU General Public License
14422 + * along with this program; if not, write to the Free Software
14423 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14424 + */
14425 +
14426 +/*
14427 + * inode functions
14428 + */
14429 +
14430 +#include "aufs.h"
14431 +
14432 +struct inode *au_igrab(struct inode *inode)
14433 +{
14434 +       if (inode) {
14435 +               AuDebugOn(!atomic_read(&inode->i_count));
14436 +               ihold(inode);
14437 +       }
14438 +       return inode;
14439 +}
14440 +
14441 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
14442 +{
14443 +       au_cpup_attr_all(inode, /*force*/0);
14444 +       au_update_iigen(inode, /*half*/1);
14445 +       if (do_version)
14446 +               inode->i_version++;
14447 +}
14448 +
14449 +static int au_ii_refresh(struct inode *inode, int *update)
14450 +{
14451 +       int err, e;
14452 +       umode_t type;
14453 +       aufs_bindex_t bindex, new_bindex;
14454 +       struct super_block *sb;
14455 +       struct au_iinfo *iinfo;
14456 +       struct au_hinode *p, *q, tmp;
14457 +
14458 +       IiMustWriteLock(inode);
14459 +
14460 +       *update = 0;
14461 +       sb = inode->i_sb;
14462 +       type = inode->i_mode & S_IFMT;
14463 +       iinfo = au_ii(inode);
14464 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
14465 +       if (unlikely(err))
14466 +               goto out;
14467 +
14468 +       AuDebugOn(iinfo->ii_bstart < 0);
14469 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
14470 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
14471 +            bindex++, p++) {
14472 +               if (!p->hi_inode)
14473 +                       continue;
14474 +
14475 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
14476 +               new_bindex = au_br_index(sb, p->hi_id);
14477 +               if (new_bindex == bindex)
14478 +                       continue;
14479 +
14480 +               if (new_bindex < 0) {
14481 +                       *update = 1;
14482 +                       au_hiput(p);
14483 +                       p->hi_inode = NULL;
14484 +                       continue;
14485 +               }
14486 +
14487 +               if (new_bindex < iinfo->ii_bstart)
14488 +                       iinfo->ii_bstart = new_bindex;
14489 +               if (iinfo->ii_bend < new_bindex)
14490 +                       iinfo->ii_bend = new_bindex;
14491 +               /* swap two lower inode, and loop again */
14492 +               q = iinfo->ii_hinode + new_bindex;
14493 +               tmp = *q;
14494 +               *q = *p;
14495 +               *p = tmp;
14496 +               if (tmp.hi_inode) {
14497 +                       bindex--;
14498 +                       p--;
14499 +               }
14500 +       }
14501 +       au_update_ibrange(inode, /*do_put_zero*/0);
14502 +       e = au_dy_irefresh(inode);
14503 +       if (unlikely(e && !err))
14504 +               err = e;
14505 +
14506 +out:
14507 +       AuTraceErr(err);
14508 +       return err;
14509 +}
14510 +
14511 +int au_refresh_hinode_self(struct inode *inode)
14512 +{
14513 +       int err, update;
14514 +
14515 +       err = au_ii_refresh(inode, &update);
14516 +       if (!err)
14517 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
14518 +
14519 +       AuTraceErr(err);
14520 +       return err;
14521 +}
14522 +
14523 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
14524 +{
14525 +       int err, e, update;
14526 +       unsigned int flags;
14527 +       umode_t mode;
14528 +       aufs_bindex_t bindex, bend;
14529 +       unsigned char isdir;
14530 +       struct au_hinode *p;
14531 +       struct au_iinfo *iinfo;
14532 +
14533 +       err = au_ii_refresh(inode, &update);
14534 +       if (unlikely(err))
14535 +               goto out;
14536 +
14537 +       update = 0;
14538 +       iinfo = au_ii(inode);
14539 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
14540 +       mode = (inode->i_mode & S_IFMT);
14541 +       isdir = S_ISDIR(mode);
14542 +       flags = au_hi_flags(inode, isdir);
14543 +       bend = au_dbend(dentry);
14544 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
14545 +               struct inode *h_i;
14546 +               struct dentry *h_d;
14547 +
14548 +               h_d = au_h_dptr(dentry, bindex);
14549 +               if (!h_d || !h_d->d_inode)
14550 +                       continue;
14551 +
14552 +               AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
14553 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
14554 +                       h_i = au_h_iptr(inode, bindex);
14555 +                       if (h_i) {
14556 +                               if (h_i == h_d->d_inode)
14557 +                                       continue;
14558 +                               err = -EIO;
14559 +                               break;
14560 +                       }
14561 +               }
14562 +               if (bindex < iinfo->ii_bstart)
14563 +                       iinfo->ii_bstart = bindex;
14564 +               if (iinfo->ii_bend < bindex)
14565 +                       iinfo->ii_bend = bindex;
14566 +               au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
14567 +               update = 1;
14568 +       }
14569 +       au_update_ibrange(inode, /*do_put_zero*/0);
14570 +       e = au_dy_irefresh(inode);
14571 +       if (unlikely(e && !err))
14572 +               err = e;
14573 +       if (!err)
14574 +               au_refresh_hinode_attr(inode, update && isdir);
14575 +
14576 +out:
14577 +       AuTraceErr(err);
14578 +       return err;
14579 +}
14580 +
14581 +static int set_inode(struct inode *inode, struct dentry *dentry)
14582 +{
14583 +       int err;
14584 +       unsigned int flags;
14585 +       umode_t mode;
14586 +       aufs_bindex_t bindex, bstart, btail;
14587 +       unsigned char isdir;
14588 +       struct dentry *h_dentry;
14589 +       struct inode *h_inode;
14590 +       struct au_iinfo *iinfo;
14591 +
14592 +       IiMustWriteLock(inode);
14593 +
14594 +       err = 0;
14595 +       isdir = 0;
14596 +       bstart = au_dbstart(dentry);
14597 +       h_inode = au_h_dptr(dentry, bstart)->d_inode;
14598 +       mode = h_inode->i_mode;
14599 +       switch (mode & S_IFMT) {
14600 +       case S_IFREG:
14601 +               btail = au_dbtail(dentry);
14602 +               inode->i_op = &aufs_iop;
14603 +               inode->i_fop = &aufs_file_fop;
14604 +               err = au_dy_iaop(inode, bstart, h_inode);
14605 +               if (unlikely(err))
14606 +                       goto out;
14607 +               break;
14608 +       case S_IFDIR:
14609 +               isdir = 1;
14610 +               btail = au_dbtaildir(dentry);
14611 +               inode->i_op = &aufs_dir_iop;
14612 +               inode->i_fop = &aufs_dir_fop;
14613 +               break;
14614 +       case S_IFLNK:
14615 +               btail = au_dbtail(dentry);
14616 +               inode->i_op = &aufs_symlink_iop;
14617 +               break;
14618 +       case S_IFBLK:
14619 +       case S_IFCHR:
14620 +       case S_IFIFO:
14621 +       case S_IFSOCK:
14622 +               btail = au_dbtail(dentry);
14623 +               inode->i_op = &aufs_iop;
14624 +               au_init_special_fop(inode, mode, h_inode->i_rdev);
14625 +               break;
14626 +       default:
14627 +               AuIOErr("Unknown file type 0%o\n", mode);
14628 +               err = -EIO;
14629 +               goto out;
14630 +       }
14631 +
14632 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
14633 +       flags = au_hi_flags(inode, isdir);
14634 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
14635 +           && au_ftest_hi(flags, HNOTIFY)
14636 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
14637 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
14638 +               au_fclr_hi(flags, HNOTIFY);
14639 +       iinfo = au_ii(inode);
14640 +       iinfo->ii_bstart = bstart;
14641 +       iinfo->ii_bend = btail;
14642 +       for (bindex = bstart; bindex <= btail; bindex++) {
14643 +               h_dentry = au_h_dptr(dentry, bindex);
14644 +               if (h_dentry)
14645 +                       au_set_h_iptr(inode, bindex,
14646 +                                     au_igrab(h_dentry->d_inode), flags);
14647 +       }
14648 +       au_cpup_attr_all(inode, /*force*/1);
14649 +
14650 +out:
14651 +       return err;
14652 +}
14653 +
14654 +/*
14655 + * successful returns with iinfo write_locked
14656 + * minus: errno
14657 + * zero: success, matched
14658 + * plus: no error, but unmatched
14659 + */
14660 +static int reval_inode(struct inode *inode, struct dentry *dentry)
14661 +{
14662 +       int err;
14663 +       unsigned int gen;
14664 +       struct au_iigen iigen;
14665 +       aufs_bindex_t bindex, bend;
14666 +       struct inode *h_inode, *h_dinode;
14667 +
14668 +       /*
14669 +        * before this function, if aufs got any iinfo lock, it must be only
14670 +        * one, the parent dir.
14671 +        * it can happen by UDBA and the obsoleted inode number.
14672 +        */
14673 +       err = -EIO;
14674 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
14675 +               goto out;
14676 +
14677 +       err = 1;
14678 +       ii_write_lock_new_child(inode);
14679 +       h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
14680 +       bend = au_ibend(inode);
14681 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
14682 +               h_inode = au_h_iptr(inode, bindex);
14683 +               if (!h_inode || h_inode != h_dinode)
14684 +                       continue;
14685 +
14686 +               err = 0;
14687 +               gen = au_iigen(inode, &iigen);
14688 +               if (gen == au_digen(dentry)
14689 +                   && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
14690 +                       break;
14691 +
14692 +               /* fully refresh inode using dentry */
14693 +               err = au_refresh_hinode(inode, dentry);
14694 +               if (!err)
14695 +                       au_update_iigen(inode, /*half*/0);
14696 +               break;
14697 +       }
14698 +
14699 +       if (unlikely(err))
14700 +               ii_write_unlock(inode);
14701 +out:
14702 +       return err;
14703 +}
14704 +
14705 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
14706 +          unsigned int d_type, ino_t *ino)
14707 +{
14708 +       int err;
14709 +       struct mutex *mtx;
14710 +
14711 +       /* prevent hardlinked inode number from race condition */
14712 +       mtx = NULL;
14713 +       if (d_type != DT_DIR) {
14714 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
14715 +               mutex_lock(mtx);
14716 +       }
14717 +       err = au_xino_read(sb, bindex, h_ino, ino);
14718 +       if (unlikely(err))
14719 +               goto out;
14720 +
14721 +       if (!*ino) {
14722 +               err = -EIO;
14723 +               *ino = au_xino_new_ino(sb);
14724 +               if (unlikely(!*ino))
14725 +                       goto out;
14726 +               err = au_xino_write(sb, bindex, h_ino, *ino);
14727 +               if (unlikely(err))
14728 +                       goto out;
14729 +       }
14730 +
14731 +out:
14732 +       if (mtx)
14733 +               mutex_unlock(mtx);
14734 +       return err;
14735 +}
14736 +
14737 +/* successful returns with iinfo write_locked */
14738 +/* todo: return with unlocked? */
14739 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
14740 +{
14741 +       struct inode *inode, *h_inode;
14742 +       struct dentry *h_dentry;
14743 +       struct super_block *sb;
14744 +       struct mutex *mtx;
14745 +       ino_t h_ino, ino;
14746 +       int err;
14747 +       aufs_bindex_t bstart;
14748 +
14749 +       sb = dentry->d_sb;
14750 +       bstart = au_dbstart(dentry);
14751 +       h_dentry = au_h_dptr(dentry, bstart);
14752 +       h_inode = h_dentry->d_inode;
14753 +       h_ino = h_inode->i_ino;
14754 +
14755 +       /*
14756 +        * stop 'race'-ing between hardlinks under different
14757 +        * parents.
14758 +        */
14759 +       mtx = NULL;
14760 +       if (!S_ISDIR(h_inode->i_mode))
14761 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
14762 +
14763 +new_ino:
14764 +       if (mtx)
14765 +               mutex_lock(mtx);
14766 +       err = au_xino_read(sb, bstart, h_ino, &ino);
14767 +       inode = ERR_PTR(err);
14768 +       if (unlikely(err))
14769 +               goto out;
14770 +
14771 +       if (!ino) {
14772 +               ino = au_xino_new_ino(sb);
14773 +               if (unlikely(!ino)) {
14774 +                       inode = ERR_PTR(-EIO);
14775 +                       goto out;
14776 +               }
14777 +       }
14778 +
14779 +       AuDbg("i%lu\n", (unsigned long)ino);
14780 +       inode = au_iget_locked(sb, ino);
14781 +       err = PTR_ERR(inode);
14782 +       if (IS_ERR(inode))
14783 +               goto out;
14784 +
14785 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
14786 +       if (inode->i_state & I_NEW) {
14787 +               /* verbose coding for lock class name */
14788 +               if (unlikely(S_ISLNK(h_inode->i_mode)))
14789 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
14790 +                                   au_lc_key + AuLcSymlink_IIINFO);
14791 +               else if (unlikely(S_ISDIR(h_inode->i_mode)))
14792 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
14793 +                                   au_lc_key + AuLcDir_IIINFO);
14794 +               else /* likely */
14795 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
14796 +                                   au_lc_key + AuLcNonDir_IIINFO);
14797 +
14798 +               ii_write_lock_new_child(inode);
14799 +               err = set_inode(inode, dentry);
14800 +               if (!err) {
14801 +                       unlock_new_inode(inode);
14802 +                       goto out; /* success */
14803 +               }
14804 +
14805 +               /*
14806 +                * iget_failed() calls iput(), but we need to call
14807 +                * ii_write_unlock() after iget_failed(). so dirty hack for
14808 +                * i_count.
14809 +                */
14810 +               atomic_inc(&inode->i_count);
14811 +               iget_failed(inode);
14812 +               ii_write_unlock(inode);
14813 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
14814 +               /* ignore this error */
14815 +               goto out_iput;
14816 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
14817 +               /*
14818 +                * horrible race condition between lookup, readdir and copyup
14819 +                * (or something).
14820 +                */
14821 +               if (mtx)
14822 +                       mutex_unlock(mtx);
14823 +               err = reval_inode(inode, dentry);
14824 +               if (unlikely(err < 0)) {
14825 +                       mtx = NULL;
14826 +                       goto out_iput;
14827 +               }
14828 +
14829 +               if (!err) {
14830 +                       mtx = NULL;
14831 +                       goto out; /* success */
14832 +               } else if (mtx)
14833 +                       mutex_lock(mtx);
14834 +       }
14835 +
14836 +       if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
14837 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
14838 +                       " b%d, %s, %.*s, hi%lu, i%lu.\n",
14839 +                       bstart, au_sbtype(h_dentry->d_sb), AuDLNPair(dentry),
14840 +                       (unsigned long)h_ino, (unsigned long)ino);
14841 +       ino = 0;
14842 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
14843 +       if (!err) {
14844 +               iput(inode);
14845 +               if (mtx)
14846 +                       mutex_unlock(mtx);
14847 +               goto new_ino;
14848 +       }
14849 +
14850 +out_iput:
14851 +       iput(inode);
14852 +       inode = ERR_PTR(err);
14853 +out:
14854 +       if (mtx)
14855 +               mutex_unlock(mtx);
14856 +       return inode;
14857 +}
14858 +
14859 +/* ---------------------------------------------------------------------- */
14860 +
14861 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
14862 +              struct inode *inode)
14863 +{
14864 +       int err;
14865 +
14866 +       err = au_br_rdonly(au_sbr(sb, bindex));
14867 +
14868 +       /* pseudo-link after flushed may happen out of bounds */
14869 +       if (!err
14870 +           && inode
14871 +           && au_ibstart(inode) <= bindex
14872 +           && bindex <= au_ibend(inode)) {
14873 +               /*
14874 +                * permission check is unnecessary since vfsub routine
14875 +                * will be called later
14876 +                */
14877 +               struct inode *hi = au_h_iptr(inode, bindex);
14878 +               if (hi)
14879 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
14880 +       }
14881 +
14882 +       return err;
14883 +}
14884 +
14885 +int au_test_h_perm(struct inode *h_inode, int mask)
14886 +{
14887 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
14888 +               return 0;
14889 +       return inode_permission(h_inode, mask);
14890 +}
14891 +
14892 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
14893 +{
14894 +       if (au_test_nfs(h_inode->i_sb)
14895 +           && (mask & MAY_WRITE)
14896 +           && S_ISDIR(h_inode->i_mode))
14897 +               mask |= MAY_READ; /* force permission check */
14898 +       return au_test_h_perm(h_inode, mask);
14899 +}
14900 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
14901 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
14902 +++ linux/fs/aufs/inode.h       2013-07-06 13:20:47.750198454 +0200
14903 @@ -0,0 +1,600 @@
14904 +/*
14905 + * Copyright (C) 2005-2013 Junjiro R. Okajima
14906 + *
14907 + * This program, aufs is free software; you can redistribute it and/or modify
14908 + * it under the terms of the GNU General Public License as published by
14909 + * the Free Software Foundation; either version 2 of the License, or
14910 + * (at your option) any later version.
14911 + *
14912 + * This program is distributed in the hope that it will be useful,
14913 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14914 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14915 + * GNU General Public License for more details.
14916 + *
14917 + * You should have received a copy of the GNU General Public License
14918 + * along with this program; if not, write to the Free Software
14919 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
14920 + */
14921 +
14922 +/*
14923 + * inode operations
14924 + */
14925 +
14926 +#ifndef __AUFS_INODE_H__
14927 +#define __AUFS_INODE_H__
14928 +
14929 +#ifdef __KERNEL__
14930 +
14931 +#include <linux/fsnotify.h>
14932 +#include "rwsem.h"
14933 +
14934 +struct vfsmount;
14935 +
14936 +struct au_hnotify {
14937 +#ifdef CONFIG_AUFS_HNOTIFY
14938 +#ifdef CONFIG_AUFS_HFSNOTIFY
14939 +       /* never use fsnotify_add_vfsmount_mark() */
14940 +       struct fsnotify_mark            hn_mark;
14941 +#endif
14942 +       struct inode                    *hn_aufs_inode; /* no get/put */
14943 +#endif
14944 +} ____cacheline_aligned_in_smp;
14945 +
14946 +struct au_hinode {
14947 +       struct inode            *hi_inode;
14948 +       aufs_bindex_t           hi_id;
14949 +#ifdef CONFIG_AUFS_HNOTIFY
14950 +       struct au_hnotify       *hi_notify;
14951 +#endif
14952 +
14953 +       /* reference to the copied-up whiteout with get/put */
14954 +       struct dentry           *hi_whdentry;
14955 +};
14956 +
14957 +/* ig_flags */
14958 +#define AuIG_HALF_REFRESHED            1
14959 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
14960 +#define au_ig_fset(flags, name) \
14961 +       do { (flags) |= AuIG_##name; } while (0)
14962 +#define au_ig_fclr(flags, name) \
14963 +       do { (flags) &= ~AuIG_##name; } while (0)
14964 +
14965 +struct au_iigen {
14966 +       __u32           ig_generation, ig_flags;
14967 +};
14968 +
14969 +struct au_vdir;
14970 +struct au_iinfo {
14971 +       spinlock_t              ii_genspin;
14972 +       struct au_iigen         ii_generation;
14973 +       struct super_block      *ii_hsb1;       /* no get/put */
14974 +
14975 +       struct au_rwsem         ii_rwsem;
14976 +       aufs_bindex_t           ii_bstart, ii_bend;
14977 +       __u32                   ii_higen;
14978 +       struct au_hinode        *ii_hinode;
14979 +       struct au_vdir          *ii_vdir;
14980 +};
14981 +
14982 +struct au_icntnr {
14983 +       struct au_iinfo iinfo;
14984 +       struct inode vfs_inode;
14985 +} ____cacheline_aligned_in_smp;
14986 +
14987 +/* au_pin flags */
14988 +#define AuPin_DI_LOCKED                1
14989 +#define AuPin_MNT_WRITE                (1 << 1)
14990 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
14991 +#define au_fset_pin(flags, name) \
14992 +       do { (flags) |= AuPin_##name; } while (0)
14993 +#define au_fclr_pin(flags, name) \
14994 +       do { (flags) &= ~AuPin_##name; } while (0)
14995 +
14996 +struct au_pin {
14997 +       /* input */
14998 +       struct dentry *dentry;
14999 +       unsigned int udba;
15000 +       unsigned char lsc_di, lsc_hi, flags;
15001 +       aufs_bindex_t bindex;
15002 +
15003 +       /* output */
15004 +       struct dentry *parent;
15005 +       struct au_hinode *hdir;
15006 +       struct vfsmount *h_mnt;
15007 +
15008 +       /* temporary unlock/relock for copyup */
15009 +       struct dentry *h_dentry, *h_parent;
15010 +       struct au_branch *br;
15011 +       struct task_struct *task;
15012 +};
15013 +
15014 +void au_pin_hdir_unlock(struct au_pin *p);
15015 +int au_pin_hdir_relock(struct au_pin *p);
15016 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
15017 +void au_pin_hdir_acquire_nest(struct au_pin *p);
15018 +void au_pin_hdir_release(struct au_pin *p);
15019 +
15020 +/* ---------------------------------------------------------------------- */
15021 +
15022 +static inline struct au_iinfo *au_ii(struct inode *inode)
15023 +{
15024 +       struct au_iinfo *iinfo;
15025 +
15026 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15027 +       if (iinfo->ii_hinode)
15028 +               return iinfo;
15029 +       return NULL; /* debugging bad_inode case */
15030 +}
15031 +
15032 +/* ---------------------------------------------------------------------- */
15033 +
15034 +/* inode.c */
15035 +struct inode *au_igrab(struct inode *inode);
15036 +int au_refresh_hinode_self(struct inode *inode);
15037 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
15038 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
15039 +          unsigned int d_type, ino_t *ino);
15040 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
15041 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
15042 +              struct inode *inode);
15043 +int au_test_h_perm(struct inode *h_inode, int mask);
15044 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
15045 +
15046 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
15047 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
15048 +{
15049 +#ifdef CONFIG_AUFS_SHWH
15050 +       return au_ino(sb, bindex, h_ino, d_type, ino);
15051 +#else
15052 +       return 0;
15053 +#endif
15054 +}
15055 +
15056 +/* i_op.c */
15057 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
15058 +
15059 +/* au_wr_dir flags */
15060 +#define AuWrDir_ADD_ENTRY      1
15061 +#define AuWrDir_TMP_WHENTRY    (1 << 1)
15062 +#define AuWrDir_ISDIR          (1 << 2)
15063 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
15064 +#define au_fset_wrdir(flags, name) \
15065 +       do { (flags) |= AuWrDir_##name; } while (0)
15066 +#define au_fclr_wrdir(flags, name) \
15067 +       do { (flags) &= ~AuWrDir_##name; } while (0)
15068 +
15069 +struct au_wr_dir_args {
15070 +       aufs_bindex_t force_btgt;
15071 +       unsigned char flags;
15072 +};
15073 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
15074 +             struct au_wr_dir_args *args);
15075 +
15076 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
15077 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
15078 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
15079 +                unsigned int udba, unsigned char flags);
15080 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
15081 +          unsigned int udba, unsigned char flags) __must_check;
15082 +int au_do_pin(struct au_pin *pin) __must_check;
15083 +void au_unpin(struct au_pin *pin);
15084 +
15085 +/* i_op_add.c */
15086 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
15087 +              struct dentry *h_parent, int isdir);
15088 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
15089 +              dev_t dev);
15090 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
15091 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
15092 +               bool want_excl);
15093 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
15094 +             struct dentry *dentry);
15095 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
15096 +
15097 +/* i_op_del.c */
15098 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
15099 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
15100 +              struct dentry *h_parent, int isdir);
15101 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
15102 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
15103 +
15104 +/* i_op_ren.c */
15105 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
15106 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
15107 +               struct inode *dir, struct dentry *dentry);
15108 +
15109 +/* iinfo.c */
15110 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
15111 +void au_hiput(struct au_hinode *hinode);
15112 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15113 +                 struct dentry *h_wh);
15114 +unsigned int au_hi_flags(struct inode *inode, int isdir);
15115 +
15116 +/* hinode flags */
15117 +#define AuHi_XINO      1
15118 +#define AuHi_HNOTIFY   (1 << 1)
15119 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
15120 +#define au_fset_hi(flags, name) \
15121 +       do { (flags) |= AuHi_##name; } while (0)
15122 +#define au_fclr_hi(flags, name) \
15123 +       do { (flags) &= ~AuHi_##name; } while (0)
15124 +
15125 +#ifndef CONFIG_AUFS_HNOTIFY
15126 +#undef AuHi_HNOTIFY
15127 +#define AuHi_HNOTIFY   0
15128 +#endif
15129 +
15130 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15131 +                  struct inode *h_inode, unsigned int flags);
15132 +
15133 +void au_update_iigen(struct inode *inode, int half);
15134 +void au_update_ibrange(struct inode *inode, int do_put_zero);
15135 +
15136 +void au_icntnr_init_once(void *_c);
15137 +int au_iinfo_init(struct inode *inode);
15138 +void au_iinfo_fin(struct inode *inode);
15139 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
15140 +
15141 +#ifdef CONFIG_PROC_FS
15142 +/* plink.c */
15143 +int au_plink_maint(struct super_block *sb, int flags);
15144 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
15145 +int au_plink_maint_enter(struct super_block *sb);
15146 +#ifdef CONFIG_AUFS_DEBUG
15147 +void au_plink_list(struct super_block *sb);
15148 +#else
15149 +AuStubVoid(au_plink_list, struct super_block *sb)
15150 +#endif
15151 +int au_plink_test(struct inode *inode);
15152 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
15153 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
15154 +                    struct dentry *h_dentry);
15155 +void au_plink_put(struct super_block *sb, int verbose);
15156 +void au_plink_clean(struct super_block *sb, int verbose);
15157 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
15158 +#else
15159 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
15160 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
15161 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
15162 +AuStubVoid(au_plink_list, struct super_block *sb);
15163 +AuStubInt0(au_plink_test, struct inode *inode);
15164 +AuStub(struct dentry *, au_plink_lkup, return NULL,
15165 +       struct inode *inode, aufs_bindex_t bindex);
15166 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
15167 +          struct dentry *h_dentry);
15168 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
15169 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
15170 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
15171 +#endif /* CONFIG_PROC_FS */
15172 +
15173 +/* ---------------------------------------------------------------------- */
15174 +
15175 +/* lock subclass for iinfo */
15176 +enum {
15177 +       AuLsc_II_CHILD,         /* child first */
15178 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
15179 +       AuLsc_II_CHILD3,        /* copyup dirs */
15180 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
15181 +       AuLsc_II_PARENT2,
15182 +       AuLsc_II_PARENT3,       /* copyup dirs */
15183 +       AuLsc_II_NEW_CHILD
15184 +};
15185 +
15186 +/*
15187 + * ii_read_lock_child, ii_write_lock_child,
15188 + * ii_read_lock_child2, ii_write_lock_child2,
15189 + * ii_read_lock_child3, ii_write_lock_child3,
15190 + * ii_read_lock_parent, ii_write_lock_parent,
15191 + * ii_read_lock_parent2, ii_write_lock_parent2,
15192 + * ii_read_lock_parent3, ii_write_lock_parent3,
15193 + * ii_read_lock_new_child, ii_write_lock_new_child,
15194 + */
15195 +#define AuReadLockFunc(name, lsc) \
15196 +static inline void ii_read_lock_##name(struct inode *i) \
15197 +{ \
15198 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15199 +}
15200 +
15201 +#define AuWriteLockFunc(name, lsc) \
15202 +static inline void ii_write_lock_##name(struct inode *i) \
15203 +{ \
15204 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15205 +}
15206 +
15207 +#define AuRWLockFuncs(name, lsc) \
15208 +       AuReadLockFunc(name, lsc) \
15209 +       AuWriteLockFunc(name, lsc)
15210 +
15211 +AuRWLockFuncs(child, CHILD);
15212 +AuRWLockFuncs(child2, CHILD2);
15213 +AuRWLockFuncs(child3, CHILD3);
15214 +AuRWLockFuncs(parent, PARENT);
15215 +AuRWLockFuncs(parent2, PARENT2);
15216 +AuRWLockFuncs(parent3, PARENT3);
15217 +AuRWLockFuncs(new_child, NEW_CHILD);
15218 +
15219 +#undef AuReadLockFunc
15220 +#undef AuWriteLockFunc
15221 +#undef AuRWLockFuncs
15222 +
15223 +/*
15224 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
15225 + */
15226 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
15227 +
15228 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
15229 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
15230 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
15231 +
15232 +/* ---------------------------------------------------------------------- */
15233 +
15234 +static inline void au_icntnr_init(struct au_icntnr *c)
15235 +{
15236 +#ifdef CONFIG_AUFS_DEBUG
15237 +       c->vfs_inode.i_mode = 0;
15238 +#endif
15239 +}
15240 +
15241 +static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
15242 +{
15243 +       unsigned int gen;
15244 +       struct au_iinfo *iinfo;
15245 +
15246 +       iinfo = au_ii(inode);
15247 +       spin_lock(&iinfo->ii_genspin);
15248 +       if (iigen)
15249 +               *iigen = iinfo->ii_generation;
15250 +       gen = iinfo->ii_generation.ig_generation;
15251 +       spin_unlock(&iinfo->ii_genspin);
15252 +
15253 +       return gen;
15254 +}
15255 +
15256 +/* tiny test for inode number */
15257 +/* tmpfs generation is too rough */
15258 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
15259 +{
15260 +       struct au_iinfo *iinfo;
15261 +
15262 +       iinfo = au_ii(inode);
15263 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
15264 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
15265 +                && iinfo->ii_higen == h_inode->i_generation);
15266 +}
15267 +
15268 +static inline void au_iigen_dec(struct inode *inode)
15269 +{
15270 +       struct au_iinfo *iinfo;
15271 +
15272 +       iinfo = au_ii(inode);
15273 +       spin_lock(&iinfo->ii_genspin);
15274 +       iinfo->ii_generation.ig_generation--;
15275 +       spin_unlock(&iinfo->ii_genspin);
15276 +}
15277 +
15278 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
15279 +{
15280 +       int err;
15281 +
15282 +       err = 0;
15283 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
15284 +               err = -EIO;
15285 +
15286 +       return err;
15287 +}
15288 +
15289 +/* ---------------------------------------------------------------------- */
15290 +
15291 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
15292 +                                       aufs_bindex_t bindex)
15293 +{
15294 +       IiMustAnyLock(inode);
15295 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
15296 +}
15297 +
15298 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
15299 +{
15300 +       IiMustAnyLock(inode);
15301 +       return au_ii(inode)->ii_bstart;
15302 +}
15303 +
15304 +static inline aufs_bindex_t au_ibend(struct inode *inode)
15305 +{
15306 +       IiMustAnyLock(inode);
15307 +       return au_ii(inode)->ii_bend;
15308 +}
15309 +
15310 +static inline struct au_vdir *au_ivdir(struct inode *inode)
15311 +{
15312 +       IiMustAnyLock(inode);
15313 +       return au_ii(inode)->ii_vdir;
15314 +}
15315 +
15316 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
15317 +{
15318 +       IiMustAnyLock(inode);
15319 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
15320 +}
15321 +
15322 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
15323 +{
15324 +       IiMustWriteLock(inode);
15325 +       au_ii(inode)->ii_bstart = bindex;
15326 +}
15327 +
15328 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
15329 +{
15330 +       IiMustWriteLock(inode);
15331 +       au_ii(inode)->ii_bend = bindex;
15332 +}
15333 +
15334 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
15335 +{
15336 +       IiMustWriteLock(inode);
15337 +       au_ii(inode)->ii_vdir = vdir;
15338 +}
15339 +
15340 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
15341 +{
15342 +       IiMustAnyLock(inode);
15343 +       return au_ii(inode)->ii_hinode + bindex;
15344 +}
15345 +
15346 +/* ---------------------------------------------------------------------- */
15347 +
15348 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
15349 +{
15350 +       if (pin)
15351 +               return pin->parent;
15352 +       return NULL;
15353 +}
15354 +
15355 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
15356 +{
15357 +       if (pin && pin->hdir)
15358 +               return pin->hdir->hi_inode;
15359 +       return NULL;
15360 +}
15361 +
15362 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
15363 +{
15364 +       if (pin)
15365 +               return pin->hdir;
15366 +       return NULL;
15367 +}
15368 +
15369 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
15370 +{
15371 +       if (pin)
15372 +               pin->dentry = dentry;
15373 +}
15374 +
15375 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
15376 +                                          unsigned char lflag)
15377 +{
15378 +       if (pin) {
15379 +               if (lflag)
15380 +                       au_fset_pin(pin->flags, DI_LOCKED);
15381 +               else
15382 +                       au_fclr_pin(pin->flags, DI_LOCKED);
15383 +       }
15384 +}
15385 +
15386 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
15387 +{
15388 +       if (pin) {
15389 +               dput(pin->parent);
15390 +               pin->parent = dget(parent);
15391 +       }
15392 +}
15393 +
15394 +/* ---------------------------------------------------------------------- */
15395 +
15396 +struct au_branch;
15397 +#ifdef CONFIG_AUFS_HNOTIFY
15398 +struct au_hnotify_op {
15399 +       void (*ctl)(struct au_hinode *hinode, int do_set);
15400 +       int (*alloc)(struct au_hinode *hinode);
15401 +
15402 +       /*
15403 +        * if it returns true, the the caller should free hinode->hi_notify,
15404 +        * otherwise ->free() frees it.
15405 +        */
15406 +       int (*free)(struct au_hinode *hinode,
15407 +                   struct au_hnotify *hn) __must_check;
15408 +
15409 +       void (*fin)(void);
15410 +       int (*init)(void);
15411 +
15412 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
15413 +       void (*fin_br)(struct au_branch *br);
15414 +       int (*init_br)(struct au_branch *br, int perm);
15415 +};
15416 +
15417 +/* hnotify.c */
15418 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
15419 +void au_hn_free(struct au_hinode *hinode);
15420 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
15421 +void au_hn_reset(struct inode *inode, unsigned int flags);
15422 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15423 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
15424 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
15425 +int au_hnotify_init_br(struct au_branch *br, int perm);
15426 +void au_hnotify_fin_br(struct au_branch *br);
15427 +int __init au_hnotify_init(void);
15428 +void au_hnotify_fin(void);
15429 +
15430 +/* hfsnotify.c */
15431 +extern const struct au_hnotify_op au_hnotify_op;
15432 +
15433 +static inline
15434 +void au_hn_init(struct au_hinode *hinode)
15435 +{
15436 +       hinode->hi_notify = NULL;
15437 +}
15438 +
15439 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
15440 +{
15441 +       return hinode->hi_notify;
15442 +}
15443 +
15444 +#else
15445 +static inline
15446 +int au_hn_alloc(struct au_hinode *hinode __maybe_unused,
15447 +               struct inode *inode __maybe_unused)
15448 +{
15449 +       return -EOPNOTSUPP;
15450 +}
15451 +
15452 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
15453 +{
15454 +       return NULL;
15455 +}
15456 +
15457 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
15458 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
15459 +          int do_set __maybe_unused)
15460 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
15461 +          unsigned int flags __maybe_unused)
15462 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
15463 +          struct au_branch *br __maybe_unused,
15464 +          int perm __maybe_unused)
15465 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
15466 +          int perm __maybe_unused)
15467 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
15468 +AuStubInt0(__init au_hnotify_init, void)
15469 +AuStubVoid(au_hnotify_fin, void)
15470 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
15471 +#endif /* CONFIG_AUFS_HNOTIFY */
15472 +
15473 +static inline void au_hn_suspend(struct au_hinode *hdir)
15474 +{
15475 +       au_hn_ctl(hdir, /*do_set*/0);
15476 +}
15477 +
15478 +static inline void au_hn_resume(struct au_hinode *hdir)
15479 +{
15480 +       au_hn_ctl(hdir, /*do_set*/1);
15481 +}
15482 +
15483 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
15484 +{
15485 +       mutex_lock(&hdir->hi_inode->i_mutex);
15486 +       au_hn_suspend(hdir);
15487 +}
15488 +
15489 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
15490 +                                         unsigned int sc __maybe_unused)
15491 +{
15492 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
15493 +       au_hn_suspend(hdir);
15494 +}
15495 +
15496 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
15497 +{
15498 +       au_hn_resume(hdir);
15499 +       mutex_unlock(&hdir->hi_inode->i_mutex);
15500 +}
15501 +
15502 +#endif /* __KERNEL__ */
15503 +#endif /* __AUFS_INODE_H__ */
15504 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
15505 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
15506 +++ linux/fs/aufs/ioctl.c       2013-08-23 23:59:39.634916914 +0200
15507 @@ -0,0 +1,202 @@
15508 +/*
15509 + * Copyright (C) 2005-2013 Junjiro R. Okajima
15510 + *
15511 + * This program, aufs is free software; you can redistribute it and/or modify
15512 + * it under the terms of the GNU General Public License as published by
15513 + * the Free Software Foundation; either version 2 of the License, or
15514 + * (at your option) any later version.
15515 + *
15516 + * This program is distributed in the hope that it will be useful,
15517 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15518 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15519 + * GNU General Public License for more details.
15520 + *
15521 + * You should have received a copy of the GNU General Public License
15522 + * along with this program; if not, write to the Free Software
15523 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15524 + */
15525 +
15526 +/*
15527 + * ioctl
15528 + * plink-management and readdir in userspace.
15529 + * assist the pathconf(3) wrapper library.
15530 + * move-down
15531 + */
15532 +
15533 +#include <linux/compat.h>
15534 +#include <linux/file.h>
15535 +#include "aufs.h"
15536 +
15537 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
15538 +{
15539 +       int err, fd;
15540 +       aufs_bindex_t wbi, bindex, bend;
15541 +       struct file *h_file;
15542 +       struct super_block *sb;
15543 +       struct dentry *root;
15544 +       struct au_branch *br;
15545 +       struct aufs_wbr_fd wbrfd = {
15546 +               .oflags = au_dir_roflags,
15547 +               .brid   = -1
15548 +       };
15549 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
15550 +               | O_NOATIME | O_CLOEXEC;
15551 +
15552 +       AuDebugOn(wbrfd.oflags & ~valid);
15553 +
15554 +       if (arg) {
15555 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
15556 +               if (unlikely(err)) {
15557 +                       err = -EFAULT;
15558 +                       goto out;
15559 +               }
15560 +
15561 +               err = -EINVAL;
15562 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
15563 +               wbrfd.oflags |= au_dir_roflags;
15564 +               AuDbg("0%o\n", wbrfd.oflags);
15565 +               if (unlikely(wbrfd.oflags & ~valid))
15566 +                       goto out;
15567 +       }
15568 +
15569 +       fd = get_unused_fd();
15570 +       err = fd;
15571 +       if (unlikely(fd < 0))
15572 +               goto out;
15573 +
15574 +       h_file = ERR_PTR(-EINVAL);
15575 +       wbi = 0;
15576 +       br = NULL;
15577 +       sb = path->dentry->d_sb;
15578 +       root = sb->s_root;
15579 +       aufs_read_lock(root, AuLock_IR);
15580 +       bend = au_sbend(sb);
15581 +       if (wbrfd.brid >= 0) {
15582 +               wbi = au_br_index(sb, wbrfd.brid);
15583 +               if (unlikely(wbi < 0 || wbi > bend))
15584 +                       goto out_unlock;
15585 +       }
15586 +
15587 +       h_file = ERR_PTR(-ENOENT);
15588 +       br = au_sbr(sb, wbi);
15589 +       if (!au_br_writable(br->br_perm)) {
15590 +               if (arg)
15591 +                       goto out_unlock;
15592 +
15593 +               bindex = wbi + 1;
15594 +               wbi = -1;
15595 +               for (; bindex <= bend; bindex++) {
15596 +                       br = au_sbr(sb, bindex);
15597 +                       if (au_br_writable(br->br_perm)) {
15598 +                               wbi = bindex;
15599 +                               br = au_sbr(sb, wbi);
15600 +                               break;
15601 +                       }
15602 +               }
15603 +       }
15604 +       AuDbg("wbi %d\n", wbi);
15605 +       if (wbi >= 0)
15606 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL);
15607 +
15608 +out_unlock:
15609 +       aufs_read_unlock(root, AuLock_IR);
15610 +       err = PTR_ERR(h_file);
15611 +       if (IS_ERR(h_file))
15612 +               goto out_fd;
15613 +
15614 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
15615 +       fd_install(fd, h_file);
15616 +       err = fd;
15617 +       goto out; /* success */
15618 +
15619 +out_fd:
15620 +       put_unused_fd(fd);
15621 +out:
15622 +       AuTraceErr(err);
15623 +       return err;
15624 +}
15625 +
15626 +/* ---------------------------------------------------------------------- */
15627 +
15628 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
15629 +{
15630 +       long err;
15631 +
15632 +       switch (cmd) {
15633 +       case AUFS_CTL_RDU:
15634 +       case AUFS_CTL_RDU_INO:
15635 +               err = au_rdu_ioctl(file, cmd, arg);
15636 +               break;
15637 +
15638 +       case AUFS_CTL_WBR_FD:
15639 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15640 +               break;
15641 +
15642 +       case AUFS_CTL_IBUSY:
15643 +               err = au_ibusy_ioctl(file, arg);
15644 +               break;
15645 +
15646 +       default:
15647 +               /* do not call the lower */
15648 +               AuDbg("0x%x\n", cmd);
15649 +               err = -ENOTTY;
15650 +       }
15651 +
15652 +       AuTraceErr(err);
15653 +       return err;
15654 +}
15655 +
15656 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
15657 +{
15658 +       long err;
15659 +
15660 +       switch (cmd) {
15661 +       case AUFS_CTL_MVDOWN:
15662 +               WARN_ONCE(1, "move-down is still testing...\n");
15663 +               err = au_mvdown(file->f_dentry, (void __user *)arg);
15664 +               break;
15665 +
15666 +       case AUFS_CTL_WBR_FD:
15667 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15668 +               break;
15669 +
15670 +       default:
15671 +               /* do not call the lower */
15672 +               AuDbg("0x%x\n", cmd);
15673 +               err = -ENOTTY;
15674 +       }
15675 +
15676 +       AuTraceErr(err);
15677 +       return err;
15678 +}
15679 +
15680 +#ifdef CONFIG_COMPAT
15681 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15682 +                          unsigned long arg)
15683 +{
15684 +       long err;
15685 +
15686 +       switch (cmd) {
15687 +       case AUFS_CTL_RDU:
15688 +       case AUFS_CTL_RDU_INO:
15689 +               err = au_rdu_compat_ioctl(file, cmd, arg);
15690 +               break;
15691 +
15692 +       case AUFS_CTL_IBUSY:
15693 +               err = au_ibusy_compat_ioctl(file, arg);
15694 +               break;
15695 +
15696 +       default:
15697 +               err = aufs_ioctl_dir(file, cmd, arg);
15698 +       }
15699 +
15700 +       AuTraceErr(err);
15701 +       return err;
15702 +}
15703 +
15704 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15705 +                             unsigned long arg)
15706 +{
15707 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
15708 +}
15709 +#endif
15710 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
15711 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
15712 +++ linux/fs/aufs/i_op_add.c    2013-08-23 23:59:39.634916914 +0200
15713 @@ -0,0 +1,739 @@
15714 +/*
15715 + * Copyright (C) 2005-2013 Junjiro R. Okajima
15716 + *
15717 + * This program, aufs is free software; you can redistribute it and/or modify
15718 + * it under the terms of the GNU General Public License as published by
15719 + * the Free Software Foundation; either version 2 of the License, or
15720 + * (at your option) any later version.
15721 + *
15722 + * This program is distributed in the hope that it will be useful,
15723 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15724 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15725 + * GNU General Public License for more details.
15726 + *
15727 + * You should have received a copy of the GNU General Public License
15728 + * along with this program; if not, write to the Free Software
15729 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15730 + */
15731 +
15732 +/*
15733 + * inode operations (add entry)
15734 + */
15735 +
15736 +#include "aufs.h"
15737 +
15738 +/*
15739 + * final procedure of adding a new entry, except link(2).
15740 + * remove whiteout, instantiate, copyup the parent dir's times and size
15741 + * and update version.
15742 + * if it failed, re-create the removed whiteout.
15743 + */
15744 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
15745 +                 struct dentry *wh_dentry, struct dentry *dentry)
15746 +{
15747 +       int err, rerr;
15748 +       aufs_bindex_t bwh;
15749 +       struct path h_path;
15750 +       struct inode *inode, *h_dir;
15751 +       struct dentry *wh;
15752 +
15753 +       bwh = -1;
15754 +       if (wh_dentry) {
15755 +               h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
15756 +               IMustLock(h_dir);
15757 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
15758 +               bwh = au_dbwh(dentry);
15759 +               h_path.dentry = wh_dentry;
15760 +               h_path.mnt = au_sbr_mnt(dir->i_sb, bindex);
15761 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
15762 +                                         dentry);
15763 +               if (unlikely(err))
15764 +                       goto out;
15765 +       }
15766 +
15767 +       inode = au_new_inode(dentry, /*must_new*/1);
15768 +       if (!IS_ERR(inode)) {
15769 +               d_instantiate(dentry, inode);
15770 +               dir = dentry->d_parent->d_inode; /* dir inode is locked */
15771 +               IMustLock(dir);
15772 +               if (au_ibstart(dir) == au_dbstart(dentry))
15773 +                       au_cpup_attr_timesizes(dir);
15774 +               dir->i_version++;
15775 +               return 0; /* success */
15776 +       }
15777 +
15778 +       err = PTR_ERR(inode);
15779 +       if (!wh_dentry)
15780 +               goto out;
15781 +
15782 +       /* revert */
15783 +       /* dir inode is locked */
15784 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
15785 +       rerr = PTR_ERR(wh);
15786 +       if (IS_ERR(wh)) {
15787 +               AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
15788 +                       AuDLNPair(dentry), err, rerr);
15789 +               err = -EIO;
15790 +       } else
15791 +               dput(wh);
15792 +
15793 +out:
15794 +       return err;
15795 +}
15796 +
15797 +static int au_d_may_add(struct dentry *dentry)
15798 +{
15799 +       int err;
15800 +
15801 +       err = 0;
15802 +       if (unlikely(d_unhashed(dentry)))
15803 +               err = -ENOENT;
15804 +       if (unlikely(dentry->d_inode))
15805 +               err = -EEXIST;
15806 +       return err;
15807 +}
15808 +
15809 +/*
15810 + * simple tests for the adding inode operations.
15811 + * following the checks in vfs, plus the parent-child relationship.
15812 + */
15813 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
15814 +              struct dentry *h_parent, int isdir)
15815 +{
15816 +       int err;
15817 +       umode_t h_mode;
15818 +       struct dentry *h_dentry;
15819 +       struct inode *h_inode;
15820 +
15821 +       err = -ENAMETOOLONG;
15822 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
15823 +               goto out;
15824 +
15825 +       h_dentry = au_h_dptr(dentry, bindex);
15826 +       h_inode = h_dentry->d_inode;
15827 +       if (!dentry->d_inode) {
15828 +               err = -EEXIST;
15829 +               if (unlikely(h_inode))
15830 +                       goto out;
15831 +       } else {
15832 +               /* rename(2) case */
15833 +               err = -EIO;
15834 +               if (unlikely(!h_inode || !h_inode->i_nlink))
15835 +                       goto out;
15836 +
15837 +               h_mode = h_inode->i_mode;
15838 +               if (!isdir) {
15839 +                       err = -EISDIR;
15840 +                       if (unlikely(S_ISDIR(h_mode)))
15841 +                               goto out;
15842 +               } else if (unlikely(!S_ISDIR(h_mode))) {
15843 +                       err = -ENOTDIR;
15844 +                       goto out;
15845 +               }
15846 +       }
15847 +
15848 +       err = 0;
15849 +       /* expected parent dir is locked */
15850 +       if (unlikely(h_parent != h_dentry->d_parent))
15851 +               err = -EIO;
15852 +
15853 +out:
15854 +       AuTraceErr(err);
15855 +       return err;
15856 +}
15857 +
15858 +/*
15859 + * initial procedure of adding a new entry.
15860 + * prepare writable branch and the parent dir, lock it,
15861 + * and lookup whiteout for the new entry.
15862 + */
15863 +static struct dentry*
15864 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
15865 +                 struct dentry *src_dentry, struct au_pin *pin,
15866 +                 struct au_wr_dir_args *wr_dir_args)
15867 +{
15868 +       struct dentry *wh_dentry, *h_parent;
15869 +       struct super_block *sb;
15870 +       struct au_branch *br;
15871 +       int err;
15872 +       unsigned int udba;
15873 +       aufs_bindex_t bcpup;
15874 +
15875 +       AuDbg("%.*s\n", AuDLNPair(dentry));
15876 +
15877 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
15878 +       bcpup = err;
15879 +       wh_dentry = ERR_PTR(err);
15880 +       if (unlikely(err < 0))
15881 +               goto out;
15882 +
15883 +       sb = dentry->d_sb;
15884 +       udba = au_opt_udba(sb);
15885 +       err = au_pin(pin, dentry, bcpup, udba,
15886 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
15887 +       wh_dentry = ERR_PTR(err);
15888 +       if (unlikely(err))
15889 +               goto out;
15890 +
15891 +       h_parent = au_pinned_h_parent(pin);
15892 +       if (udba != AuOpt_UDBA_NONE
15893 +           && au_dbstart(dentry) == bcpup)
15894 +               err = au_may_add(dentry, bcpup, h_parent,
15895 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
15896 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
15897 +               err = -ENAMETOOLONG;
15898 +       wh_dentry = ERR_PTR(err);
15899 +       if (unlikely(err))
15900 +               goto out_unpin;
15901 +
15902 +       br = au_sbr(sb, bcpup);
15903 +       if (dt) {
15904 +               struct path tmp = {
15905 +                       .dentry = h_parent,
15906 +                       .mnt    = au_br_mnt(br)
15907 +               };
15908 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
15909 +       }
15910 +
15911 +       wh_dentry = NULL;
15912 +       if (bcpup != au_dbwh(dentry))
15913 +               goto out; /* success */
15914 +
15915 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
15916 +
15917 +out_unpin:
15918 +       if (IS_ERR(wh_dentry))
15919 +               au_unpin(pin);
15920 +out:
15921 +       return wh_dentry;
15922 +}
15923 +
15924 +/* ---------------------------------------------------------------------- */
15925 +
15926 +enum { Mknod, Symlink, Creat };
15927 +struct simple_arg {
15928 +       int type;
15929 +       union {
15930 +               struct {
15931 +                       umode_t mode;
15932 +                       bool want_excl;
15933 +               } c;
15934 +               struct {
15935 +                       const char *symname;
15936 +               } s;
15937 +               struct {
15938 +                       umode_t mode;
15939 +                       dev_t dev;
15940 +               } m;
15941 +       } u;
15942 +};
15943 +
15944 +static int add_simple(struct inode *dir, struct dentry *dentry,
15945 +                     struct simple_arg *arg)
15946 +{
15947 +       int err;
15948 +       aufs_bindex_t bstart;
15949 +       unsigned char created;
15950 +       struct dentry *wh_dentry, *parent;
15951 +       struct inode *h_dir;
15952 +       /* to reuduce stack size */
15953 +       struct {
15954 +               struct au_dtime dt;
15955 +               struct au_pin pin;
15956 +               struct path h_path;
15957 +               struct au_wr_dir_args wr_dir_args;
15958 +       } *a;
15959 +
15960 +       AuDbg("%.*s\n", AuDLNPair(dentry));
15961 +       IMustLock(dir);
15962 +
15963 +       err = -ENOMEM;
15964 +       a = kmalloc(sizeof(*a), GFP_NOFS);
15965 +       if (unlikely(!a))
15966 +               goto out;
15967 +       a->wr_dir_args.force_btgt = -1;
15968 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
15969 +
15970 +       parent = dentry->d_parent; /* dir inode is locked */
15971 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
15972 +       if (unlikely(err))
15973 +               goto out_free;
15974 +       err = au_d_may_add(dentry);
15975 +       if (unlikely(err))
15976 +               goto out_unlock;
15977 +       di_write_lock_parent(parent);
15978 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
15979 +                                     &a->pin, &a->wr_dir_args);
15980 +       err = PTR_ERR(wh_dentry);
15981 +       if (IS_ERR(wh_dentry))
15982 +               goto out_parent;
15983 +
15984 +       bstart = au_dbstart(dentry);
15985 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
15986 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
15987 +       h_dir = au_pinned_h_dir(&a->pin);
15988 +       switch (arg->type) {
15989 +       case Creat:
15990 +               err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
15991 +                                  arg->u.c.want_excl);
15992 +               break;
15993 +       case Symlink:
15994 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
15995 +               break;
15996 +       case Mknod:
15997 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
15998 +                                 arg->u.m.dev);
15999 +               break;
16000 +       default:
16001 +               BUG();
16002 +       }
16003 +       created = !err;
16004 +       if (!err)
16005 +               err = epilog(dir, bstart, wh_dentry, dentry);
16006 +
16007 +       /* revert */
16008 +       if (unlikely(created && err && a->h_path.dentry->d_inode)) {
16009 +               int rerr;
16010 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*force*/0);
16011 +               if (rerr) {
16012 +                       AuIOErr("%.*s revert failure(%d, %d)\n",
16013 +                               AuDLNPair(dentry), err, rerr);
16014 +                       err = -EIO;
16015 +               }
16016 +               au_dtime_revert(&a->dt);
16017 +       }
16018 +
16019 +       au_unpin(&a->pin);
16020 +       dput(wh_dentry);
16021 +
16022 +out_parent:
16023 +       di_write_unlock(parent);
16024 +out_unlock:
16025 +       if (unlikely(err)) {
16026 +               au_update_dbstart(dentry);
16027 +               d_drop(dentry);
16028 +       }
16029 +       aufs_read_unlock(dentry, AuLock_DW);
16030 +out_free:
16031 +       kfree(a);
16032 +out:
16033 +       return err;
16034 +}
16035 +
16036 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16037 +              dev_t dev)
16038 +{
16039 +       struct simple_arg arg = {
16040 +               .type = Mknod,
16041 +               .u.m = {
16042 +                       .mode   = mode,
16043 +                       .dev    = dev
16044 +               }
16045 +       };
16046 +       return add_simple(dir, dentry, &arg);
16047 +}
16048 +
16049 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
16050 +{
16051 +       struct simple_arg arg = {
16052 +               .type = Symlink,
16053 +               .u.s.symname = symname
16054 +       };
16055 +       return add_simple(dir, dentry, &arg);
16056 +}
16057 +
16058 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
16059 +               bool want_excl)
16060 +{
16061 +       struct simple_arg arg = {
16062 +               .type = Creat,
16063 +               .u.c = {
16064 +                       .mode           = mode,
16065 +                       .want_excl      = want_excl
16066 +               }
16067 +       };
16068 +       return add_simple(dir, dentry, &arg);
16069 +}
16070 +
16071 +/* ---------------------------------------------------------------------- */
16072 +
16073 +struct au_link_args {
16074 +       aufs_bindex_t bdst, bsrc;
16075 +       struct au_pin pin;
16076 +       struct path h_path;
16077 +       struct dentry *src_parent, *parent;
16078 +};
16079 +
16080 +static int au_cpup_before_link(struct dentry *src_dentry,
16081 +                              struct au_link_args *a)
16082 +{
16083 +       int err;
16084 +       struct dentry *h_src_dentry;
16085 +       struct au_cp_generic cpg = {
16086 +               .dentry = src_dentry,
16087 +               .bdst   = a->bdst,
16088 +               .bsrc   = a->bsrc,
16089 +               .len    = -1,
16090 +               .pin    = &a->pin,
16091 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
16092 +       };
16093 +
16094 +       di_read_lock_parent(a->src_parent, AuLock_IR);
16095 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
16096 +       if (unlikely(err))
16097 +               goto out;
16098 +
16099 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
16100 +       err = au_pin(&a->pin, src_dentry, a->bdst,
16101 +                    au_opt_udba(src_dentry->d_sb),
16102 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
16103 +       if (unlikely(err))
16104 +               goto out;
16105 +
16106 +       err = au_sio_cpup_simple(&cpg);
16107 +       au_unpin(&a->pin);
16108 +
16109 +out:
16110 +       di_read_unlock(a->src_parent, AuLock_IR);
16111 +       return err;
16112 +}
16113 +
16114 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
16115 +                          struct au_link_args *a)
16116 +{
16117 +       int err;
16118 +       unsigned char plink;
16119 +       aufs_bindex_t bend;
16120 +       struct dentry *h_src_dentry;
16121 +       struct inode *h_inode, *inode;
16122 +       struct super_block *sb;
16123 +       struct file *h_file;
16124 +
16125 +       plink = 0;
16126 +       h_inode = NULL;
16127 +       sb = src_dentry->d_sb;
16128 +       inode = src_dentry->d_inode;
16129 +       if (au_ibstart(inode) <= a->bdst)
16130 +               h_inode = au_h_iptr(inode, a->bdst);
16131 +       if (!h_inode || !h_inode->i_nlink) {
16132 +               /* copyup src_dentry as the name of dentry. */
16133 +               bend = au_dbend(dentry);
16134 +               if (bend < a->bsrc)
16135 +                       au_set_dbend(dentry, a->bsrc);
16136 +               au_set_h_dptr(dentry, a->bsrc,
16137 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
16138 +               dget(a->h_path.dentry);
16139 +               au_set_h_dptr(dentry, a->bdst, NULL);
16140 +               dentry->d_inode = src_dentry->d_inode; /* tmp */
16141 +               h_file = au_h_open_pre(dentry, a->bsrc);
16142 +               if (IS_ERR(h_file))
16143 +                       err = PTR_ERR(h_file);
16144 +               else {
16145 +                       struct au_cp_generic cpg = {
16146 +                               .dentry = dentry,
16147 +                               .bdst   = a->bdst,
16148 +                               .bsrc   = -1,
16149 +                               .len    = -1,
16150 +                               .pin    = &a->pin,
16151 +                               .flags  = AuCpup_KEEPLINO
16152 +                       };
16153 +                       err = au_sio_cpup_simple(&cpg);
16154 +                       au_h_open_post(dentry, a->bsrc, h_file);
16155 +                       if (!err) {
16156 +                               dput(a->h_path.dentry);
16157 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
16158 +                       } else
16159 +                               au_set_h_dptr(dentry, a->bdst,
16160 +                                             a->h_path.dentry);
16161 +               }
16162 +               dentry->d_inode = NULL; /* restore */
16163 +               au_set_h_dptr(dentry, a->bsrc, NULL);
16164 +               au_set_dbend(dentry, bend);
16165 +       } else {
16166 +               /* the inode of src_dentry already exists on a.bdst branch */
16167 +               h_src_dentry = d_find_alias(h_inode);
16168 +               if (!h_src_dentry && au_plink_test(inode)) {
16169 +                       plink = 1;
16170 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
16171 +                       err = PTR_ERR(h_src_dentry);
16172 +                       if (IS_ERR(h_src_dentry))
16173 +                               goto out;
16174 +
16175 +                       if (unlikely(!h_src_dentry->d_inode)) {
16176 +                               dput(h_src_dentry);
16177 +                               h_src_dentry = NULL;
16178 +                       }
16179 +
16180 +               }
16181 +               if (h_src_dentry) {
16182 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
16183 +                                        &a->h_path);
16184 +                       dput(h_src_dentry);
16185 +               } else {
16186 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
16187 +                               h_inode->i_ino, a->bdst);
16188 +                       err = -EIO;
16189 +               }
16190 +       }
16191 +
16192 +       if (!err && !plink)
16193 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
16194 +
16195 +out:
16196 +       AuTraceErr(err);
16197 +       return err;
16198 +}
16199 +
16200 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
16201 +             struct dentry *dentry)
16202 +{
16203 +       int err, rerr;
16204 +       struct au_dtime dt;
16205 +       struct au_link_args *a;
16206 +       struct dentry *wh_dentry, *h_src_dentry;
16207 +       struct inode *inode;
16208 +       struct super_block *sb;
16209 +       struct au_wr_dir_args wr_dir_args = {
16210 +               /* .force_btgt  = -1, */
16211 +               .flags          = AuWrDir_ADD_ENTRY
16212 +       };
16213 +
16214 +       IMustLock(dir);
16215 +       inode = src_dentry->d_inode;
16216 +       IMustLock(inode);
16217 +
16218 +       err = -ENOMEM;
16219 +       a = kzalloc(sizeof(*a), GFP_NOFS);
16220 +       if (unlikely(!a))
16221 +               goto out;
16222 +
16223 +       a->parent = dentry->d_parent; /* dir inode is locked */
16224 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
16225 +                                       AuLock_NOPLM | AuLock_GEN);
16226 +       if (unlikely(err))
16227 +               goto out_kfree;
16228 +       err = au_d_hashed_positive(src_dentry);
16229 +       if (unlikely(err))
16230 +               goto out_unlock;
16231 +       err = au_d_may_add(dentry);
16232 +       if (unlikely(err))
16233 +               goto out_unlock;
16234 +
16235 +       a->src_parent = dget_parent(src_dentry);
16236 +       wr_dir_args.force_btgt = au_ibstart(inode);
16237 +
16238 +       di_write_lock_parent(a->parent);
16239 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
16240 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
16241 +                                     &wr_dir_args);
16242 +       err = PTR_ERR(wh_dentry);
16243 +       if (IS_ERR(wh_dentry))
16244 +               goto out_parent;
16245 +
16246 +       err = 0;
16247 +       sb = dentry->d_sb;
16248 +       a->bdst = au_dbstart(dentry);
16249 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
16250 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
16251 +       a->bsrc = au_ibstart(inode);
16252 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
16253 +       if (!h_src_dentry) {
16254 +               a->bsrc = au_dbstart(src_dentry);
16255 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
16256 +               AuDebugOn(!h_src_dentry);
16257 +       } else if (IS_ERR(h_src_dentry))
16258 +               goto out_parent;
16259 +
16260 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
16261 +               if (a->bdst < a->bsrc
16262 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
16263 +                       err = au_cpup_or_link(src_dentry, dentry, a);
16264 +               else
16265 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
16266 +                                        &a->h_path);
16267 +               dput(h_src_dentry);
16268 +       } else {
16269 +               /*
16270 +                * copyup src_dentry to the branch we process,
16271 +                * and then link(2) to it.
16272 +                */
16273 +               dput(h_src_dentry);
16274 +               if (a->bdst < a->bsrc
16275 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
16276 +                       au_unpin(&a->pin);
16277 +                       di_write_unlock(a->parent);
16278 +                       err = au_cpup_before_link(src_dentry, a);
16279 +                       di_write_lock_parent(a->parent);
16280 +                       if (!err)
16281 +                               err = au_pin(&a->pin, dentry, a->bdst,
16282 +                                            au_opt_udba(sb),
16283 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
16284 +                       if (unlikely(err))
16285 +                               goto out_wh;
16286 +               }
16287 +               if (!err) {
16288 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
16289 +                       err = -ENOENT;
16290 +                       if (h_src_dentry && h_src_dentry->d_inode)
16291 +                               err = vfsub_link(h_src_dentry,
16292 +                                                au_pinned_h_dir(&a->pin),
16293 +                                                &a->h_path);
16294 +               }
16295 +       }
16296 +       if (unlikely(err))
16297 +               goto out_unpin;
16298 +
16299 +       if (wh_dentry) {
16300 +               a->h_path.dentry = wh_dentry;
16301 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
16302 +                                         dentry);
16303 +               if (unlikely(err))
16304 +                       goto out_revert;
16305 +       }
16306 +
16307 +       dir->i_version++;
16308 +       if (au_ibstart(dir) == au_dbstart(dentry))
16309 +               au_cpup_attr_timesizes(dir);
16310 +       inc_nlink(inode);
16311 +       inode->i_ctime = dir->i_ctime;
16312 +       d_instantiate(dentry, au_igrab(inode));
16313 +       if (d_unhashed(a->h_path.dentry))
16314 +               /* some filesystem calls d_drop() */
16315 +               d_drop(dentry);
16316 +       goto out_unpin; /* success */
16317 +
16318 +out_revert:
16319 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, /*force*/0);
16320 +       if (unlikely(rerr)) {
16321 +               AuIOErr("%.*s reverting failed(%d, %d)\n",
16322 +                       AuDLNPair(dentry), err, rerr);
16323 +               err = -EIO;
16324 +       }
16325 +       au_dtime_revert(&dt);
16326 +out_unpin:
16327 +       au_unpin(&a->pin);
16328 +out_wh:
16329 +       dput(wh_dentry);
16330 +out_parent:
16331 +       di_write_unlock(a->parent);
16332 +       dput(a->src_parent);
16333 +out_unlock:
16334 +       if (unlikely(err)) {
16335 +               au_update_dbstart(dentry);
16336 +               d_drop(dentry);
16337 +       }
16338 +       aufs_read_and_write_unlock2(dentry, src_dentry);
16339 +out_kfree:
16340 +       kfree(a);
16341 +out:
16342 +       AuTraceErr(err);
16343 +       return err;
16344 +}
16345 +
16346 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16347 +{
16348 +       int err, rerr;
16349 +       aufs_bindex_t bindex;
16350 +       unsigned char diropq;
16351 +       struct path h_path;
16352 +       struct dentry *wh_dentry, *parent, *opq_dentry;
16353 +       struct mutex *h_mtx;
16354 +       struct super_block *sb;
16355 +       struct {
16356 +               struct au_pin pin;
16357 +               struct au_dtime dt;
16358 +       } *a; /* reduce the stack usage */
16359 +       struct au_wr_dir_args wr_dir_args = {
16360 +               .force_btgt     = -1,
16361 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
16362 +       };
16363 +
16364 +       IMustLock(dir);
16365 +
16366 +       err = -ENOMEM;
16367 +       a = kmalloc(sizeof(*a), GFP_NOFS);
16368 +       if (unlikely(!a))
16369 +               goto out;
16370 +
16371 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
16372 +       if (unlikely(err))
16373 +               goto out_free;
16374 +       err = au_d_may_add(dentry);
16375 +       if (unlikely(err))
16376 +               goto out_unlock;
16377 +
16378 +       parent = dentry->d_parent; /* dir inode is locked */
16379 +       di_write_lock_parent(parent);
16380 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
16381 +                                     &a->pin, &wr_dir_args);
16382 +       err = PTR_ERR(wh_dentry);
16383 +       if (IS_ERR(wh_dentry))
16384 +               goto out_parent;
16385 +
16386 +       sb = dentry->d_sb;
16387 +       bindex = au_dbstart(dentry);
16388 +       h_path.dentry = au_h_dptr(dentry, bindex);
16389 +       h_path.mnt = au_sbr_mnt(sb, bindex);
16390 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
16391 +       if (unlikely(err))
16392 +               goto out_unpin;
16393 +
16394 +       /* make the dir opaque */
16395 +       diropq = 0;
16396 +       h_mtx = &h_path.dentry->d_inode->i_mutex;
16397 +       if (wh_dentry
16398 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
16399 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
16400 +               opq_dentry = au_diropq_create(dentry, bindex);
16401 +               mutex_unlock(h_mtx);
16402 +               err = PTR_ERR(opq_dentry);
16403 +               if (IS_ERR(opq_dentry))
16404 +                       goto out_dir;
16405 +               dput(opq_dentry);
16406 +               diropq = 1;
16407 +       }
16408 +
16409 +       err = epilog(dir, bindex, wh_dentry, dentry);
16410 +       if (!err) {
16411 +               inc_nlink(dir);
16412 +               goto out_unpin; /* success */
16413 +       }
16414 +
16415 +       /* revert */
16416 +       if (diropq) {
16417 +               AuLabel(revert opq);
16418 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
16419 +               rerr = au_diropq_remove(dentry, bindex);
16420 +               mutex_unlock(h_mtx);
16421 +               if (rerr) {
16422 +                       AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
16423 +                               AuDLNPair(dentry), err, rerr);
16424 +                       err = -EIO;
16425 +               }
16426 +       }
16427 +
16428 +out_dir:
16429 +       AuLabel(revert dir);
16430 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
16431 +       if (rerr) {
16432 +               AuIOErr("%.*s reverting dir failed(%d, %d)\n",
16433 +                       AuDLNPair(dentry), err, rerr);
16434 +               err = -EIO;
16435 +       }
16436 +       au_dtime_revert(&a->dt);
16437 +out_unpin:
16438 +       au_unpin(&a->pin);
16439 +       dput(wh_dentry);
16440 +out_parent:
16441 +       di_write_unlock(parent);
16442 +out_unlock:
16443 +       if (unlikely(err)) {
16444 +               au_update_dbstart(dentry);
16445 +               d_drop(dentry);
16446 +       }
16447 +       aufs_read_unlock(dentry, AuLock_DW);
16448 +out_free:
16449 +       kfree(a);
16450 +out:
16451 +       return err;
16452 +}
16453 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
16454 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
16455 +++ linux/fs/aufs/i_op.c        2013-08-23 23:59:39.634916914 +0200
16456 @@ -0,0 +1,1115 @@
16457 +/*
16458 + * Copyright (C) 2005-2013 Junjiro R. Okajima
16459 + *
16460 + * This program, aufs is free software; you can redistribute it and/or modify
16461 + * it under the terms of the GNU General Public License as published by
16462 + * the Free Software Foundation; either version 2 of the License, or
16463 + * (at your option) any later version.
16464 + *
16465 + * This program is distributed in the hope that it will be useful,
16466 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16467 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16468 + * GNU General Public License for more details.
16469 + *
16470 + * You should have received a copy of the GNU General Public License
16471 + * along with this program; if not, write to the Free Software
16472 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16473 + */
16474 +
16475 +/*
16476 + * inode operations (except add/del/rename)
16477 + */
16478 +
16479 +#include <linux/device_cgroup.h>
16480 +#include <linux/fs_stack.h>
16481 +#include <linux/mm.h>
16482 +#include <linux/namei.h>
16483 +#include <linux/security.h>
16484 +#include "aufs.h"
16485 +
16486 +static int h_permission(struct inode *h_inode, int mask,
16487 +                       struct vfsmount *h_mnt, int brperm)
16488 +{
16489 +       int err;
16490 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
16491 +
16492 +       err = -EACCES;
16493 +       if ((write_mask && IS_IMMUTABLE(h_inode))
16494 +           || ((mask & MAY_EXEC)
16495 +               && S_ISREG(h_inode->i_mode)
16496 +               && ((h_mnt->mnt_flags & MNT_NOEXEC)
16497 +                   || !(h_inode->i_mode & S_IXUGO))))
16498 +               goto out;
16499 +
16500 +       /*
16501 +        * - skip the lower fs test in the case of write to ro branch.
16502 +        * - nfs dir permission write check is optimized, but a policy for
16503 +        *   link/rename requires a real check.
16504 +        */
16505 +       if ((write_mask && !au_br_writable(brperm))
16506 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
16507 +               && write_mask && !(mask & MAY_READ))
16508 +           || !h_inode->i_op->permission) {
16509 +               /* AuLabel(generic_permission); */
16510 +               err = generic_permission(h_inode, mask);
16511 +       } else {
16512 +               /* AuLabel(h_inode->permission); */
16513 +               err = h_inode->i_op->permission(h_inode, mask);
16514 +               AuTraceErr(err);
16515 +       }
16516 +
16517 +       if (!err)
16518 +               err = devcgroup_inode_permission(h_inode, mask);
16519 +       if (!err)
16520 +               err = security_inode_permission(h_inode, mask);
16521 +
16522 +#if 0
16523 +       if (!err) {
16524 +               /* todo: do we need to call ima_path_check()? */
16525 +               struct path h_path = {
16526 +                       .dentry =
16527 +                       .mnt    = h_mnt
16528 +               };
16529 +               err = ima_path_check(&h_path,
16530 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
16531 +                                    IMA_COUNT_LEAVE);
16532 +       }
16533 +#endif
16534 +
16535 +out:
16536 +       return err;
16537 +}
16538 +
16539 +static int aufs_permission(struct inode *inode, int mask)
16540 +{
16541 +       int err;
16542 +       aufs_bindex_t bindex, bend;
16543 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
16544 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
16545 +       struct inode *h_inode;
16546 +       struct super_block *sb;
16547 +       struct au_branch *br;
16548 +
16549 +       /* todo: support rcu-walk? */
16550 +       if (mask & MAY_NOT_BLOCK)
16551 +               return -ECHILD;
16552 +
16553 +       sb = inode->i_sb;
16554 +       si_read_lock(sb, AuLock_FLUSH);
16555 +       ii_read_lock_child(inode);
16556 +#if 0
16557 +       err = au_iigen_test(inode, au_sigen(sb));
16558 +       if (unlikely(err))
16559 +               goto out;
16560 +#endif
16561 +
16562 +       if (!isdir || write_mask) {
16563 +               err = au_busy_or_stale();
16564 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
16565 +               if (unlikely(!h_inode
16566 +                            || (h_inode->i_mode & S_IFMT)
16567 +                            != (inode->i_mode & S_IFMT)))
16568 +                       goto out;
16569 +
16570 +               err = 0;
16571 +               bindex = au_ibstart(inode);
16572 +               br = au_sbr(sb, bindex);
16573 +               err = h_permission(h_inode, mask, au_br_mnt(br), br->br_perm);
16574 +               if (write_mask
16575 +                   && !err
16576 +                   && !special_file(h_inode->i_mode)) {
16577 +                       /* test whether the upper writable branch exists */
16578 +                       err = -EROFS;
16579 +                       for (; bindex >= 0; bindex--)
16580 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
16581 +                                       err = 0;
16582 +                                       break;
16583 +                               }
16584 +               }
16585 +               goto out;
16586 +       }
16587 +
16588 +       /* non-write to dir */
16589 +       err = 0;
16590 +       bend = au_ibend(inode);
16591 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
16592 +               h_inode = au_h_iptr(inode, bindex);
16593 +               if (h_inode) {
16594 +                       err = au_busy_or_stale();
16595 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
16596 +                               break;
16597 +
16598 +                       br = au_sbr(sb, bindex);
16599 +                       err = h_permission(h_inode, mask, au_br_mnt(br),
16600 +                                          br->br_perm);
16601 +               }
16602 +       }
16603 +
16604 +out:
16605 +       ii_read_unlock(inode);
16606 +       si_read_unlock(sb);
16607 +       return err;
16608 +}
16609 +
16610 +/* ---------------------------------------------------------------------- */
16611 +
16612 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
16613 +                                 unsigned int flags)
16614 +{
16615 +       struct dentry *ret, *parent;
16616 +       struct inode *inode;
16617 +       struct super_block *sb;
16618 +       int err, npositive;
16619 +
16620 +       IMustLock(dir);
16621 +
16622 +       /* todo: support rcu-walk? */
16623 +       ret = ERR_PTR(-ECHILD);
16624 +       if (flags & LOOKUP_RCU)
16625 +               goto out;
16626 +
16627 +       ret = ERR_PTR(-ENAMETOOLONG);
16628 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
16629 +               goto out;
16630 +
16631 +       sb = dir->i_sb;
16632 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16633 +       ret = ERR_PTR(err);
16634 +       if (unlikely(err))
16635 +               goto out;
16636 +
16637 +       err = au_di_init(dentry);
16638 +       ret = ERR_PTR(err);
16639 +       if (unlikely(err))
16640 +               goto out_si;
16641 +
16642 +       inode = NULL;
16643 +       npositive = 0; /* suppress a warning */
16644 +       parent = dentry->d_parent; /* dir inode is locked */
16645 +       di_read_lock_parent(parent, AuLock_IR);
16646 +       err = au_alive_dir(parent);
16647 +       if (!err)
16648 +               err = au_digen_test(parent, au_sigen(sb));
16649 +       if (!err) {
16650 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
16651 +                                          /*type*/0);
16652 +               err = npositive;
16653 +       }
16654 +       di_read_unlock(parent, AuLock_IR);
16655 +       ret = ERR_PTR(err);
16656 +       if (unlikely(err < 0))
16657 +               goto out_unlock;
16658 +
16659 +       if (npositive) {
16660 +               inode = au_new_inode(dentry, /*must_new*/0);
16661 +               ret = (void *)inode;
16662 +       }
16663 +       if (IS_ERR(inode)) {
16664 +               inode = NULL;
16665 +               goto out_unlock;
16666 +       }
16667 +
16668 +       ret = d_splice_alias(inode, dentry);
16669 +#if 0
16670 +       if (unlikely(d_need_lookup(dentry))) {
16671 +               spin_lock(&dentry->d_lock);
16672 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
16673 +               spin_unlock(&dentry->d_lock);
16674 +       } else
16675 +#endif
16676 +       if (unlikely(IS_ERR(ret) && inode)) {
16677 +               ii_write_unlock(inode);
16678 +               iput(inode);
16679 +               inode = NULL;
16680 +       }
16681 +
16682 +out_unlock:
16683 +       di_write_unlock(dentry);
16684 +       if (inode) {
16685 +               /* verbose coding for lock class name */
16686 +               if (unlikely(S_ISLNK(inode->i_mode)))
16687 +                       au_rw_class(&au_di(dentry)->di_rwsem,
16688 +                                   au_lc_key + AuLcSymlink_DIINFO);
16689 +               else if (unlikely(S_ISDIR(inode->i_mode)))
16690 +                       au_rw_class(&au_di(dentry)->di_rwsem,
16691 +                                   au_lc_key + AuLcDir_DIINFO);
16692 +               else /* likely */
16693 +                       au_rw_class(&au_di(dentry)->di_rwsem,
16694 +                                   au_lc_key + AuLcNonDir_DIINFO);
16695 +       }
16696 +out_si:
16697 +       si_read_unlock(sb);
16698 +out:
16699 +       return ret;
16700 +}
16701 +
16702 +/* ---------------------------------------------------------------------- */
16703 +
16704 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
16705 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
16706 +                         aufs_bindex_t bstart)
16707 +{
16708 +       int err;
16709 +       struct dentry *h_parent;
16710 +       struct inode *h_dir;
16711 +
16712 +       if (add_entry)
16713 +               IMustLock(parent->d_inode);
16714 +       else
16715 +               di_write_lock_parent(parent);
16716 +
16717 +       err = 0;
16718 +       if (!au_h_dptr(parent, bcpup)) {
16719 +               if (bstart > bcpup)
16720 +                       err = au_cpup_dirs(dentry, bcpup);
16721 +               else if (bstart < bcpup)
16722 +                       err = au_cpdown_dirs(dentry, bcpup);
16723 +               else
16724 +                       BUG();
16725 +       }
16726 +       if (!err && add_entry) {
16727 +               h_parent = au_h_dptr(parent, bcpup);
16728 +               h_dir = h_parent->d_inode;
16729 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
16730 +               err = au_lkup_neg(dentry, bcpup,
16731 +                                 au_ftest_wrdir(add_entry, TMP_WHENTRY));
16732 +               /* todo: no unlock here */
16733 +               mutex_unlock(&h_dir->i_mutex);
16734 +
16735 +               AuDbg("bcpup %d\n", bcpup);
16736 +               if (!err) {
16737 +                       if (!dentry->d_inode)
16738 +                               au_set_h_dptr(dentry, bstart, NULL);
16739 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
16740 +               }
16741 +       }
16742 +
16743 +       if (!add_entry)
16744 +               di_write_unlock(parent);
16745 +       if (!err)
16746 +               err = bcpup; /* success */
16747 +
16748 +       AuTraceErr(err);
16749 +       return err;
16750 +}
16751 +
16752 +/*
16753 + * decide the branch and the parent dir where we will create a new entry.
16754 + * returns new bindex or an error.
16755 + * copyup the parent dir if needed.
16756 + */
16757 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16758 +             struct au_wr_dir_args *args)
16759 +{
16760 +       int err;
16761 +       aufs_bindex_t bcpup, bstart, src_bstart;
16762 +       const unsigned char add_entry
16763 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
16764 +               | au_ftest_wrdir(args->flags, TMP_WHENTRY);
16765 +       struct super_block *sb;
16766 +       struct dentry *parent;
16767 +       struct au_sbinfo *sbinfo;
16768 +
16769 +       sb = dentry->d_sb;
16770 +       sbinfo = au_sbi(sb);
16771 +       parent = dget_parent(dentry);
16772 +       bstart = au_dbstart(dentry);
16773 +       bcpup = bstart;
16774 +       if (args->force_btgt < 0) {
16775 +               if (src_dentry) {
16776 +                       src_bstart = au_dbstart(src_dentry);
16777 +                       if (src_bstart < bstart)
16778 +                               bcpup = src_bstart;
16779 +               } else if (add_entry) {
16780 +                       err = AuWbrCreate(sbinfo, dentry,
16781 +                                         au_ftest_wrdir(args->flags, ISDIR));
16782 +                       bcpup = err;
16783 +               }
16784 +
16785 +               if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
16786 +                       if (add_entry)
16787 +                               err = AuWbrCopyup(sbinfo, dentry);
16788 +                       else {
16789 +                               if (!IS_ROOT(dentry)) {
16790 +                                       di_read_lock_parent(parent, !AuLock_IR);
16791 +                                       err = AuWbrCopyup(sbinfo, dentry);
16792 +                                       di_read_unlock(parent, !AuLock_IR);
16793 +                               } else
16794 +                                       err = AuWbrCopyup(sbinfo, dentry);
16795 +                       }
16796 +                       bcpup = err;
16797 +                       if (unlikely(err < 0))
16798 +                               goto out;
16799 +               }
16800 +       } else {
16801 +               bcpup = args->force_btgt;
16802 +               AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
16803 +       }
16804 +
16805 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
16806 +       err = bcpup;
16807 +       if (bcpup == bstart)
16808 +               goto out; /* success */
16809 +
16810 +       /* copyup the new parent into the branch we process */
16811 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
16812 +       if (err >= 0) {
16813 +               if (!dentry->d_inode) {
16814 +                       au_set_h_dptr(dentry, bstart, NULL);
16815 +                       au_set_dbstart(dentry, bcpup);
16816 +                       au_set_dbend(dentry, bcpup);
16817 +               }
16818 +               AuDebugOn(add_entry && !au_h_dptr(dentry, bcpup));
16819 +       }
16820 +
16821 +out:
16822 +       dput(parent);
16823 +       return err;
16824 +}
16825 +
16826 +/* ---------------------------------------------------------------------- */
16827 +
16828 +void au_pin_hdir_unlock(struct au_pin *p)
16829 +{
16830 +       if (p->hdir)
16831 +               au_hn_imtx_unlock(p->hdir);
16832 +}
16833 +
16834 +static int au_pin_hdir_lock(struct au_pin *p)
16835 +{
16836 +       int err;
16837 +
16838 +       err = 0;
16839 +       if (!p->hdir)
16840 +               goto out;
16841 +
16842 +       /* even if an error happens later, keep this lock */
16843 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
16844 +
16845 +       err = -EBUSY;
16846 +       if (unlikely(p->hdir->hi_inode != p->h_parent->d_inode))
16847 +               goto out;
16848 +
16849 +       err = 0;
16850 +       if (p->h_dentry)
16851 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
16852 +                                 p->h_parent, p->br);
16853 +
16854 +out:
16855 +       return err;
16856 +}
16857 +
16858 +int au_pin_hdir_relock(struct au_pin *p)
16859 +{
16860 +       int err, i;
16861 +       struct inode *h_i;
16862 +       struct dentry *h_d[] = {
16863 +               p->h_dentry,
16864 +               p->h_parent
16865 +       };
16866 +
16867 +       err = au_pin_hdir_lock(p);
16868 +       if (unlikely(err))
16869 +               goto out;
16870 +
16871 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
16872 +               if (!h_d[i])
16873 +                       continue;
16874 +               h_i = h_d[i]->d_inode;
16875 +               if (h_i)
16876 +                       err = !h_i->i_nlink;
16877 +       }
16878 +
16879 +out:
16880 +       return err;
16881 +}
16882 +
16883 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
16884 +{
16885 +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
16886 +       p->hdir->hi_inode->i_mutex.owner = task;
16887 +#endif
16888 +}
16889 +
16890 +void au_pin_hdir_acquire_nest(struct au_pin *p)
16891 +{
16892 +       if (p->hdir) {
16893 +               mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
16894 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
16895 +               au_pin_hdir_set_owner(p, current);
16896 +       }
16897 +}
16898 +
16899 +void au_pin_hdir_release(struct au_pin *p)
16900 +{
16901 +       if (p->hdir) {
16902 +               au_pin_hdir_set_owner(p, p->task);
16903 +               mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
16904 +       }
16905 +}
16906 +
16907 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
16908 +{
16909 +       if (pin && pin->parent)
16910 +               return au_h_dptr(pin->parent, pin->bindex);
16911 +       return NULL;
16912 +}
16913 +
16914 +void au_unpin(struct au_pin *p)
16915 +{
16916 +       if (p->hdir)
16917 +               au_pin_hdir_unlock(p);
16918 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
16919 +               vfsub_mnt_drop_write(p->h_mnt);
16920 +       if (!p->hdir)
16921 +               return;
16922 +
16923 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
16924 +               di_read_unlock(p->parent, AuLock_IR);
16925 +       iput(p->hdir->hi_inode);
16926 +       dput(p->parent);
16927 +       p->parent = NULL;
16928 +       p->hdir = NULL;
16929 +       p->h_mnt = NULL;
16930 +       /* do not clear p->task */
16931 +}
16932 +
16933 +int au_do_pin(struct au_pin *p)
16934 +{
16935 +       int err;
16936 +       struct super_block *sb;
16937 +       struct inode *h_dir;
16938 +
16939 +       err = 0;
16940 +       sb = p->dentry->d_sb;
16941 +       p->br = au_sbr(sb, p->bindex);
16942 +       if (IS_ROOT(p->dentry)) {
16943 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
16944 +                       p->h_mnt = au_br_mnt(p->br);
16945 +                       err = vfsub_mnt_want_write(p->h_mnt);
16946 +                       if (unlikely(err)) {
16947 +                               au_fclr_pin(p->flags, MNT_WRITE);
16948 +                               goto out_err;
16949 +                       }
16950 +               }
16951 +               goto out;
16952 +       }
16953 +
16954 +       p->h_dentry = NULL;
16955 +       if (p->bindex <= au_dbend(p->dentry))
16956 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
16957 +
16958 +       p->parent = dget_parent(p->dentry);
16959 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
16960 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
16961 +
16962 +       h_dir = NULL;
16963 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
16964 +       p->hdir = au_hi(p->parent->d_inode, p->bindex);
16965 +       if (p->hdir)
16966 +               h_dir = p->hdir->hi_inode;
16967 +
16968 +       /*
16969 +        * udba case, or
16970 +        * if DI_LOCKED is not set, then p->parent may be different
16971 +        * and h_parent can be NULL.
16972 +        */
16973 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
16974 +               err = -EBUSY;
16975 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
16976 +                       di_read_unlock(p->parent, AuLock_IR);
16977 +               dput(p->parent);
16978 +               p->parent = NULL;
16979 +               goto out_err;
16980 +       }
16981 +
16982 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
16983 +               p->h_mnt = au_br_mnt(p->br);
16984 +               err = vfsub_mnt_want_write(p->h_mnt);
16985 +               if (unlikely(err)) {
16986 +                       au_fclr_pin(p->flags, MNT_WRITE);
16987 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
16988 +                               di_read_unlock(p->parent, AuLock_IR);
16989 +                       dput(p->parent);
16990 +                       p->parent = NULL;
16991 +                       goto out_err;
16992 +               }
16993 +       }
16994 +
16995 +       au_igrab(h_dir);
16996 +       err = au_pin_hdir_lock(p);
16997 +       if (!err)
16998 +               goto out; /* success */
16999 +
17000 +out_err:
17001 +       pr_err("err %d\n", err);
17002 +       err = au_busy_or_stale();
17003 +out:
17004 +       return err;
17005 +}
17006 +
17007 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
17008 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
17009 +                unsigned int udba, unsigned char flags)
17010 +{
17011 +       p->dentry = dentry;
17012 +       p->udba = udba;
17013 +       p->lsc_di = lsc_di;
17014 +       p->lsc_hi = lsc_hi;
17015 +       p->flags = flags;
17016 +       p->bindex = bindex;
17017 +
17018 +       p->parent = NULL;
17019 +       p->hdir = NULL;
17020 +       p->h_mnt = NULL;
17021 +
17022 +       p->h_dentry = NULL;
17023 +       p->h_parent = NULL;
17024 +       p->br = NULL;
17025 +       p->task = current;
17026 +}
17027 +
17028 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
17029 +          unsigned int udba, unsigned char flags)
17030 +{
17031 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
17032 +                   udba, flags);
17033 +       return au_do_pin(pin);
17034 +}
17035 +
17036 +/* ---------------------------------------------------------------------- */
17037 +
17038 +/*
17039 + * ->setattr() and ->getattr() are called in various cases.
17040 + * chmod, stat: dentry is revalidated.
17041 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
17042 + *               unhashed.
17043 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
17044 + */
17045 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
17046 +static int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
17047 +{
17048 +       int err;
17049 +       struct inode *inode;
17050 +       struct dentry *parent;
17051 +
17052 +       err = 0;
17053 +       inode = dentry->d_inode;
17054 +       if (au_digen_test(dentry, sigen)) {
17055 +               parent = dget_parent(dentry);
17056 +               di_read_lock_parent(parent, AuLock_IR);
17057 +               err = au_refresh_dentry(dentry, parent);
17058 +               di_read_unlock(parent, AuLock_IR);
17059 +               dput(parent);
17060 +       }
17061 +
17062 +       AuTraceErr(err);
17063 +       return err;
17064 +}
17065 +
17066 +#define AuIcpup_DID_CPUP       1
17067 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
17068 +#define au_fset_icpup(flags, name) \
17069 +       do { (flags) |= AuIcpup_##name; } while (0)
17070 +#define au_fclr_icpup(flags, name) \
17071 +       do { (flags) &= ~AuIcpup_##name; } while (0)
17072 +
17073 +struct au_icpup_args {
17074 +       unsigned char flags;
17075 +       unsigned char pin_flags;
17076 +       aufs_bindex_t btgt;
17077 +       unsigned int udba;
17078 +       struct au_pin pin;
17079 +       struct path h_path;
17080 +       struct inode *h_inode;
17081 +};
17082 +
17083 +static int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
17084 +                           struct au_icpup_args *a)
17085 +{
17086 +       int err;
17087 +       loff_t sz;
17088 +       aufs_bindex_t bstart, ibstart;
17089 +       struct dentry *hi_wh, *parent;
17090 +       struct inode *inode;
17091 +       struct au_wr_dir_args wr_dir_args = {
17092 +               .force_btgt     = -1,
17093 +               .flags          = 0
17094 +       };
17095 +
17096 +       bstart = au_dbstart(dentry);
17097 +       inode = dentry->d_inode;
17098 +       if (S_ISDIR(inode->i_mode))
17099 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
17100 +       /* plink or hi_wh() case */
17101 +       ibstart = au_ibstart(inode);
17102 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
17103 +               wr_dir_args.force_btgt = ibstart;
17104 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17105 +       if (unlikely(err < 0))
17106 +               goto out;
17107 +       a->btgt = err;
17108 +       if (err != bstart)
17109 +               au_fset_icpup(a->flags, DID_CPUP);
17110 +
17111 +       err = 0;
17112 +       a->pin_flags = AuPin_MNT_WRITE;
17113 +       parent = NULL;
17114 +       if (!IS_ROOT(dentry)) {
17115 +               au_fset_pin(a->pin_flags, DI_LOCKED);
17116 +               parent = dget_parent(dentry);
17117 +               di_write_lock_parent(parent);
17118 +       }
17119 +
17120 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
17121 +       if (unlikely(err))
17122 +               goto out_parent;
17123 +
17124 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17125 +       a->h_inode = a->h_path.dentry->d_inode;
17126 +       mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
17127 +       sz = -1;
17128 +       if ((ia->ia_valid & ATTR_SIZE) && ia->ia_size < i_size_read(a->h_inode))
17129 +               sz = ia->ia_size;
17130 +       mutex_unlock(&a->h_inode->i_mutex);
17131 +
17132 +       hi_wh = NULL;
17133 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
17134 +               hi_wh = au_hi_wh(inode, a->btgt);
17135 +               if (!hi_wh) {
17136 +                       struct au_cp_generic cpg = {
17137 +                               .dentry = dentry,
17138 +                               .bdst   = a->btgt,
17139 +                               .bsrc   = -1,
17140 +                               .len    = sz,
17141 +                               .pin    = &a->pin
17142 +                       };
17143 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
17144 +                       if (unlikely(err))
17145 +                               goto out_unlock;
17146 +                       hi_wh = au_hi_wh(inode, a->btgt);
17147 +                       /* todo: revalidate hi_wh? */
17148 +               }
17149 +       }
17150 +
17151 +       if (parent) {
17152 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
17153 +               di_downgrade_lock(parent, AuLock_IR);
17154 +               dput(parent);
17155 +               parent = NULL;
17156 +       }
17157 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
17158 +               goto out; /* success */
17159 +
17160 +       if (!d_unhashed(dentry)) {
17161 +               struct au_cp_generic cpg = {
17162 +                       .dentry = dentry,
17163 +                       .bdst   = a->btgt,
17164 +                       .bsrc   = bstart,
17165 +                       .len    = sz,
17166 +                       .pin    = &a->pin,
17167 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
17168 +               };
17169 +               err = au_sio_cpup_simple(&cpg);
17170 +               if (!err)
17171 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
17172 +       } else if (!hi_wh)
17173 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
17174 +       else
17175 +               a->h_path.dentry = hi_wh; /* do not dget here */
17176 +
17177 +out_unlock:
17178 +       a->h_inode = a->h_path.dentry->d_inode;
17179 +       if (!err)
17180 +               goto out; /* success */
17181 +       au_unpin(&a->pin);
17182 +out_parent:
17183 +       if (parent) {
17184 +               di_write_unlock(parent);
17185 +               dput(parent);
17186 +       }
17187 +out:
17188 +       if (!err)
17189 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
17190 +       return err;
17191 +}
17192 +
17193 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
17194 +{
17195 +       int err;
17196 +       struct inode *inode;
17197 +       struct super_block *sb;
17198 +       struct file *file;
17199 +       struct au_icpup_args *a;
17200 +
17201 +       inode = dentry->d_inode;
17202 +       IMustLock(inode);
17203 +
17204 +       err = -ENOMEM;
17205 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17206 +       if (unlikely(!a))
17207 +               goto out;
17208 +
17209 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
17210 +               ia->ia_valid &= ~ATTR_MODE;
17211 +
17212 +       file = NULL;
17213 +       sb = dentry->d_sb;
17214 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17215 +       if (unlikely(err))
17216 +               goto out_kfree;
17217 +
17218 +       if (ia->ia_valid & ATTR_FILE) {
17219 +               /* currently ftruncate(2) only */
17220 +               AuDebugOn(!S_ISREG(inode->i_mode));
17221 +               file = ia->ia_file;
17222 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
17223 +               if (unlikely(err))
17224 +                       goto out_si;
17225 +               ia->ia_file = au_hf_top(file);
17226 +               a->udba = AuOpt_UDBA_NONE;
17227 +       } else {
17228 +               /* fchmod() doesn't pass ia_file */
17229 +               a->udba = au_opt_udba(sb);
17230 +               di_write_lock_child(dentry);
17231 +               /* no d_unlinked(), to set UDBA_NONE for root */
17232 +               if (d_unhashed(dentry))
17233 +                       a->udba = AuOpt_UDBA_NONE;
17234 +               if (a->udba != AuOpt_UDBA_NONE) {
17235 +                       AuDebugOn(IS_ROOT(dentry));
17236 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
17237 +                       if (unlikely(err))
17238 +                               goto out_dentry;
17239 +               }
17240 +       }
17241 +
17242 +       err = au_pin_and_icpup(dentry, ia, a);
17243 +       if (unlikely(err < 0))
17244 +               goto out_dentry;
17245 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
17246 +               ia->ia_file = NULL;
17247 +               ia->ia_valid &= ~ATTR_FILE;
17248 +       }
17249 +
17250 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
17251 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
17252 +           == (ATTR_MODE | ATTR_CTIME)) {
17253 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
17254 +               if (unlikely(err))
17255 +                       goto out_unlock;
17256 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
17257 +                  && (ia->ia_valid & ATTR_CTIME)) {
17258 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
17259 +               if (unlikely(err))
17260 +                       goto out_unlock;
17261 +       }
17262 +
17263 +       if (ia->ia_valid & ATTR_SIZE) {
17264 +               struct file *f;
17265 +
17266 +               if (ia->ia_size < i_size_read(inode))
17267 +                       /* unmap only */
17268 +                       truncate_setsize(inode, ia->ia_size);
17269 +
17270 +               f = NULL;
17271 +               if (ia->ia_valid & ATTR_FILE)
17272 +                       f = ia->ia_file;
17273 +               mutex_unlock(&a->h_inode->i_mutex);
17274 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
17275 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
17276 +       } else
17277 +               err = vfsub_notify_change(&a->h_path, ia);
17278 +       if (!err)
17279 +               au_cpup_attr_changeable(inode);
17280 +
17281 +out_unlock:
17282 +       mutex_unlock(&a->h_inode->i_mutex);
17283 +       au_unpin(&a->pin);
17284 +       if (unlikely(err))
17285 +               au_update_dbstart(dentry);
17286 +out_dentry:
17287 +       di_write_unlock(dentry);
17288 +       if (file) {
17289 +               fi_write_unlock(file);
17290 +               ia->ia_file = file;
17291 +               ia->ia_valid |= ATTR_FILE;
17292 +       }
17293 +out_si:
17294 +       si_read_unlock(sb);
17295 +out_kfree:
17296 +       kfree(a);
17297 +out:
17298 +       AuTraceErr(err);
17299 +       return err;
17300 +}
17301 +
17302 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
17303 +                            unsigned int nlink)
17304 +{
17305 +       unsigned int n;
17306 +
17307 +       inode->i_mode = st->mode;
17308 +       /* don't i_[ug]id_write() here */
17309 +       inode->i_uid = st->uid;
17310 +       inode->i_gid = st->gid;
17311 +       inode->i_atime = st->atime;
17312 +       inode->i_mtime = st->mtime;
17313 +       inode->i_ctime = st->ctime;
17314 +
17315 +       au_cpup_attr_nlink(inode, /*force*/0);
17316 +       if (S_ISDIR(inode->i_mode)) {
17317 +               n = inode->i_nlink;
17318 +               n -= nlink;
17319 +               n += st->nlink;
17320 +               smp_mb();
17321 +               /* 0 can happen */
17322 +               set_nlink(inode, n);
17323 +       }
17324 +
17325 +       spin_lock(&inode->i_lock);
17326 +       inode->i_blocks = st->blocks;
17327 +       i_size_write(inode, st->size);
17328 +       spin_unlock(&inode->i_lock);
17329 +}
17330 +
17331 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
17332 +                       struct dentry *dentry, struct kstat *st)
17333 +{
17334 +       int err;
17335 +       unsigned int mnt_flags;
17336 +       aufs_bindex_t bindex;
17337 +       unsigned char udba_none, positive;
17338 +       struct super_block *sb, *h_sb;
17339 +       struct inode *inode;
17340 +       struct path h_path;
17341 +
17342 +       sb = dentry->d_sb;
17343 +       inode = dentry->d_inode;
17344 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17345 +       if (unlikely(err))
17346 +               goto out;
17347 +       mnt_flags = au_mntflags(sb);
17348 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
17349 +
17350 +       /* support fstat(2) */
17351 +       if (!d_unlinked(dentry) && !udba_none) {
17352 +               unsigned int sigen = au_sigen(sb);
17353 +               err = au_digen_test(dentry, sigen);
17354 +               if (!err) {
17355 +                       di_read_lock_child(dentry, AuLock_IR);
17356 +                       err = au_dbrange_test(dentry);
17357 +                       if (unlikely(err))
17358 +                               goto out_unlock;
17359 +               } else {
17360 +                       AuDebugOn(IS_ROOT(dentry));
17361 +                       di_write_lock_child(dentry);
17362 +                       err = au_dbrange_test(dentry);
17363 +                       if (!err)
17364 +                               err = au_reval_for_attr(dentry, sigen);
17365 +                       di_downgrade_lock(dentry, AuLock_IR);
17366 +                       if (unlikely(err))
17367 +                               goto out_unlock;
17368 +               }
17369 +       } else
17370 +               di_read_lock_child(dentry, AuLock_IR);
17371 +
17372 +       bindex = au_ibstart(inode);
17373 +       h_path.mnt = au_sbr_mnt(sb, bindex);
17374 +       h_sb = h_path.mnt->mnt_sb;
17375 +       if (!au_test_fs_bad_iattr(h_sb) && udba_none)
17376 +               goto out_fill; /* success */
17377 +
17378 +       h_path.dentry = NULL;
17379 +       if (au_dbstart(dentry) == bindex)
17380 +               h_path.dentry = dget(au_h_dptr(dentry, bindex));
17381 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
17382 +               h_path.dentry = au_plink_lkup(inode, bindex);
17383 +               if (IS_ERR(h_path.dentry))
17384 +                       goto out_fill; /* pretending success */
17385 +       }
17386 +       /* illegally overlapped or something */
17387 +       if (unlikely(!h_path.dentry))
17388 +               goto out_fill; /* pretending success */
17389 +
17390 +       positive = !!h_path.dentry->d_inode;
17391 +       if (positive)
17392 +               err = vfs_getattr(&h_path, st);
17393 +       dput(h_path.dentry);
17394 +       if (!err) {
17395 +               if (positive)
17396 +                       au_refresh_iattr(inode, st,
17397 +                                        h_path.dentry->d_inode->i_nlink);
17398 +               goto out_fill; /* success */
17399 +       }
17400 +       AuTraceErr(err);
17401 +       goto out_unlock;
17402 +
17403 +out_fill:
17404 +       generic_fillattr(inode, st);
17405 +out_unlock:
17406 +       di_read_unlock(dentry, AuLock_IR);
17407 +       si_read_unlock(sb);
17408 +out:
17409 +       AuTraceErr(err);
17410 +       return err;
17411 +}
17412 +
17413 +/* ---------------------------------------------------------------------- */
17414 +
17415 +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
17416 +                     int bufsiz)
17417 +{
17418 +       int err;
17419 +       struct super_block *sb;
17420 +       struct dentry *h_dentry;
17421 +
17422 +       err = -EINVAL;
17423 +       h_dentry = au_h_dptr(dentry, bindex);
17424 +       if (unlikely(!h_dentry->d_inode->i_op->readlink))
17425 +               goto out;
17426 +
17427 +       err = security_inode_readlink(h_dentry);
17428 +       if (unlikely(err))
17429 +               goto out;
17430 +
17431 +       sb = dentry->d_sb;
17432 +       if (!au_test_ro(sb, bindex, dentry->d_inode)) {
17433 +               vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
17434 +               fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
17435 +       }
17436 +       err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
17437 +
17438 +out:
17439 +       return err;
17440 +}
17441 +
17442 +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
17443 +{
17444 +       int err;
17445 +
17446 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
17447 +       if (unlikely(err))
17448 +               goto out;
17449 +       err = au_d_hashed_positive(dentry);
17450 +       if (!err)
17451 +               err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
17452 +       aufs_read_unlock(dentry, AuLock_IR);
17453 +
17454 +out:
17455 +       return err;
17456 +}
17457 +
17458 +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
17459 +{
17460 +       int err;
17461 +       mm_segment_t old_fs;
17462 +       union {
17463 +               char *k;
17464 +               char __user *u;
17465 +       } buf;
17466 +
17467 +       err = -ENOMEM;
17468 +       buf.k = (void *)__get_free_page(GFP_NOFS);
17469 +       if (unlikely(!buf.k))
17470 +               goto out;
17471 +
17472 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
17473 +       if (unlikely(err))
17474 +               goto out_name;
17475 +
17476 +       err = au_d_hashed_positive(dentry);
17477 +       if (!err) {
17478 +               old_fs = get_fs();
17479 +               set_fs(KERNEL_DS);
17480 +               err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
17481 +               set_fs(old_fs);
17482 +       }
17483 +       aufs_read_unlock(dentry, AuLock_IR);
17484 +
17485 +       if (err >= 0) {
17486 +               buf.k[err] = 0;
17487 +               /* will be freed by put_link */
17488 +               nd_set_link(nd, buf.k);
17489 +               return NULL; /* success */
17490 +       }
17491 +
17492 +out_name:
17493 +       free_page((unsigned long)buf.k);
17494 +out:
17495 +       AuTraceErr(err);
17496 +       return ERR_PTR(err);
17497 +}
17498 +
17499 +static void aufs_put_link(struct dentry *dentry __maybe_unused,
17500 +                         struct nameidata *nd, void *cookie __maybe_unused)
17501 +{
17502 +       char *p;
17503 +
17504 +       p = nd_get_link(nd);
17505 +       if (!IS_ERR_OR_NULL(p))
17506 +               free_page((unsigned long)p);
17507 +}
17508 +
17509 +/* ---------------------------------------------------------------------- */
17510 +
17511 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
17512 +{
17513 +       int err;
17514 +       struct super_block *sb;
17515 +       struct inode *h_inode;
17516 +
17517 +       sb = inode->i_sb;
17518 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
17519 +       lockdep_off();
17520 +       si_read_lock(sb, AuLock_FLUSH);
17521 +       ii_write_lock_child(inode);
17522 +       lockdep_on();
17523 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
17524 +       err = vfsub_update_time(h_inode, ts, flags);
17525 +       lockdep_off();
17526 +       ii_write_unlock(inode);
17527 +       si_read_unlock(sb);
17528 +       lockdep_on();
17529 +       return err;
17530 +}
17531 +
17532 +/* ---------------------------------------------------------------------- */
17533 +
17534 +struct inode_operations aufs_symlink_iop = {
17535 +       .permission     = aufs_permission,
17536 +       .setattr        = aufs_setattr,
17537 +       .getattr        = aufs_getattr,
17538 +
17539 +       .readlink       = aufs_readlink,
17540 +       .follow_link    = aufs_follow_link,
17541 +       .put_link       = aufs_put_link,
17542 +
17543 +       /* .update_time = aufs_update_time */
17544 +};
17545 +
17546 +struct inode_operations aufs_dir_iop = {
17547 +       .create         = aufs_create,
17548 +       .lookup         = aufs_lookup,
17549 +       .link           = aufs_link,
17550 +       .unlink         = aufs_unlink,
17551 +       .symlink        = aufs_symlink,
17552 +       .mkdir          = aufs_mkdir,
17553 +       .rmdir          = aufs_rmdir,
17554 +       .mknod          = aufs_mknod,
17555 +       .rename         = aufs_rename,
17556 +
17557 +       .permission     = aufs_permission,
17558 +       .setattr        = aufs_setattr,
17559 +       .getattr        = aufs_getattr,
17560 +
17561 +       .update_time    = aufs_update_time
17562 +       /* no support for atomic_open() */
17563 +};
17564 +
17565 +struct inode_operations aufs_iop = {
17566 +       .permission     = aufs_permission,
17567 +       .setattr        = aufs_setattr,
17568 +       .getattr        = aufs_getattr,
17569 +
17570 +       .update_time    = aufs_update_time
17571 +};
17572 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
17573 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
17574 +++ linux/fs/aufs/i_op_del.c    2013-08-23 23:59:39.634916914 +0200
17575 @@ -0,0 +1,502 @@
17576 +/*
17577 + * Copyright (C) 2005-2013 Junjiro R. Okajima
17578 + *
17579 + * This program, aufs is free software; you can redistribute it and/or modify
17580 + * it under the terms of the GNU General Public License as published by
17581 + * the Free Software Foundation; either version 2 of the License, or
17582 + * (at your option) any later version.
17583 + *
17584 + * This program is distributed in the hope that it will be useful,
17585 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17586 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17587 + * GNU General Public License for more details.
17588 + *
17589 + * You should have received a copy of the GNU General Public License
17590 + * along with this program; if not, write to the Free Software
17591 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17592 + */
17593 +
17594 +/*
17595 + * inode operations (del entry)
17596 + */
17597 +
17598 +#include "aufs.h"
17599 +
17600 +/*
17601 + * decide if a new whiteout for @dentry is necessary or not.
17602 + * when it is necessary, prepare the parent dir for the upper branch whose
17603 + * branch index is @bcpup for creation. the actual creation of the whiteout will
17604 + * be done by caller.
17605 + * return value:
17606 + * 0: wh is unnecessary
17607 + * plus: wh is necessary
17608 + * minus: error
17609 + */
17610 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
17611 +{
17612 +       int need_wh, err;
17613 +       aufs_bindex_t bstart;
17614 +       struct super_block *sb;
17615 +
17616 +       sb = dentry->d_sb;
17617 +       bstart = au_dbstart(dentry);
17618 +       if (*bcpup < 0) {
17619 +               *bcpup = bstart;
17620 +               if (au_test_ro(sb, bstart, dentry->d_inode)) {
17621 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
17622 +                       *bcpup = err;
17623 +                       if (unlikely(err < 0))
17624 +                               goto out;
17625 +               }
17626 +       } else
17627 +               AuDebugOn(bstart < *bcpup
17628 +                         || au_test_ro(sb, *bcpup, dentry->d_inode));
17629 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
17630 +
17631 +       if (*bcpup != bstart) {
17632 +               err = au_cpup_dirs(dentry, *bcpup);
17633 +               if (unlikely(err))
17634 +                       goto out;
17635 +               need_wh = 1;
17636 +       } else {
17637 +               struct au_dinfo *dinfo, *tmp;
17638 +
17639 +               need_wh = -ENOMEM;
17640 +               dinfo = au_di(dentry);
17641 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
17642 +               if (tmp) {
17643 +                       au_di_cp(tmp, dinfo);
17644 +                       au_di_swap(tmp, dinfo);
17645 +                       /* returns the number of positive dentries */
17646 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
17647 +                       au_di_swap(tmp, dinfo);
17648 +                       au_rw_write_unlock(&tmp->di_rwsem);
17649 +                       au_di_free(tmp);
17650 +               }
17651 +       }
17652 +       AuDbg("need_wh %d\n", need_wh);
17653 +       err = need_wh;
17654 +
17655 +out:
17656 +       return err;
17657 +}
17658 +
17659 +/*
17660 + * simple tests for the del-entry operations.
17661 + * following the checks in vfs, plus the parent-child relationship.
17662 + */
17663 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
17664 +              struct dentry *h_parent, int isdir)
17665 +{
17666 +       int err;
17667 +       umode_t h_mode;
17668 +       struct dentry *h_dentry, *h_latest;
17669 +       struct inode *h_inode;
17670 +
17671 +       h_dentry = au_h_dptr(dentry, bindex);
17672 +       h_inode = h_dentry->d_inode;
17673 +       if (dentry->d_inode) {
17674 +               err = -ENOENT;
17675 +               if (unlikely(!h_inode || !h_inode->i_nlink))
17676 +                       goto out;
17677 +
17678 +               h_mode = h_inode->i_mode;
17679 +               if (!isdir) {
17680 +                       err = -EISDIR;
17681 +                       if (unlikely(S_ISDIR(h_mode)))
17682 +                               goto out;
17683 +               } else if (unlikely(!S_ISDIR(h_mode))) {
17684 +                       err = -ENOTDIR;
17685 +                       goto out;
17686 +               }
17687 +       } else {
17688 +               /* rename(2) case */
17689 +               err = -EIO;
17690 +               if (unlikely(h_inode))
17691 +                       goto out;
17692 +       }
17693 +
17694 +       err = -ENOENT;
17695 +       /* expected parent dir is locked */
17696 +       if (unlikely(h_parent != h_dentry->d_parent))
17697 +               goto out;
17698 +       err = 0;
17699 +
17700 +       /*
17701 +        * rmdir a dir may break the consistency on some filesystem.
17702 +        * let's try heavy test.
17703 +        */
17704 +       err = -EACCES;
17705 +       if (unlikely(au_test_h_perm(h_parent->d_inode, MAY_EXEC | MAY_WRITE)))
17706 +               goto out;
17707 +
17708 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent,
17709 +                                  au_sbr(dentry->d_sb, bindex));
17710 +       err = -EIO;
17711 +       if (IS_ERR(h_latest))
17712 +               goto out;
17713 +       if (h_latest == h_dentry)
17714 +               err = 0;
17715 +       dput(h_latest);
17716 +
17717 +out:
17718 +       return err;
17719 +}
17720 +
17721 +/*
17722 + * decide the branch where we operate for @dentry. the branch index will be set
17723 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
17724 + * dir for reverting.
17725 + * when a new whiteout is necessary, create it.
17726 + */
17727 +static struct dentry*
17728 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
17729 +                   struct au_dtime *dt, struct au_pin *pin)
17730 +{
17731 +       struct dentry *wh_dentry;
17732 +       struct super_block *sb;
17733 +       struct path h_path;
17734 +       int err, need_wh;
17735 +       unsigned int udba;
17736 +       aufs_bindex_t bcpup;
17737 +
17738 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
17739 +       wh_dentry = ERR_PTR(need_wh);
17740 +       if (unlikely(need_wh < 0))
17741 +               goto out;
17742 +
17743 +       sb = dentry->d_sb;
17744 +       udba = au_opt_udba(sb);
17745 +       bcpup = *rbcpup;
17746 +       err = au_pin(pin, dentry, bcpup, udba,
17747 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17748 +       wh_dentry = ERR_PTR(err);
17749 +       if (unlikely(err))
17750 +               goto out;
17751 +
17752 +       h_path.dentry = au_pinned_h_parent(pin);
17753 +       if (udba != AuOpt_UDBA_NONE
17754 +           && au_dbstart(dentry) == bcpup) {
17755 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
17756 +               wh_dentry = ERR_PTR(err);
17757 +               if (unlikely(err))
17758 +                       goto out_unpin;
17759 +       }
17760 +
17761 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
17762 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
17763 +       wh_dentry = NULL;
17764 +       if (!need_wh)
17765 +               goto out; /* success, no need to create whiteout */
17766 +
17767 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
17768 +       if (IS_ERR(wh_dentry))
17769 +               goto out_unpin;
17770 +
17771 +       /* returns with the parent is locked and wh_dentry is dget-ed */
17772 +       goto out; /* success */
17773 +
17774 +out_unpin:
17775 +       au_unpin(pin);
17776 +out:
17777 +       return wh_dentry;
17778 +}
17779 +
17780 +/*
17781 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
17782 + * in order to be revertible and save time for removing many child whiteouts
17783 + * under the dir.
17784 + * returns 1 when there are too many child whiteout and caller should remove
17785 + * them asynchronously. returns 0 when the number of children is enough small to
17786 + * remove now or the branch fs is a remote fs.
17787 + * otherwise return an error.
17788 + */
17789 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
17790 +                          struct au_nhash *whlist, struct inode *dir)
17791 +{
17792 +       int rmdir_later, err, dirwh;
17793 +       struct dentry *h_dentry;
17794 +       struct super_block *sb;
17795 +
17796 +       sb = dentry->d_sb;
17797 +       SiMustAnyLock(sb);
17798 +       h_dentry = au_h_dptr(dentry, bindex);
17799 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
17800 +       if (unlikely(err))
17801 +               goto out;
17802 +
17803 +       /* stop monitoring */
17804 +       au_hn_free(au_hi(dentry->d_inode, bindex));
17805 +
17806 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
17807 +               dirwh = au_sbi(sb)->si_dirwh;
17808 +               rmdir_later = (dirwh <= 1);
17809 +               if (!rmdir_later)
17810 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
17811 +                                                             dirwh);
17812 +               if (rmdir_later)
17813 +                       return rmdir_later;
17814 +       }
17815 +
17816 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
17817 +       if (unlikely(err)) {
17818 +               AuIOErr("rmdir %.*s, b%d failed, %d. ignored\n",
17819 +                       AuDLNPair(h_dentry), bindex, err);
17820 +               err = 0;
17821 +       }
17822 +
17823 +out:
17824 +       AuTraceErr(err);
17825 +       return err;
17826 +}
17827 +
17828 +/*
17829 + * final procedure for deleting a entry.
17830 + * maintain dentry and iattr.
17831 + */
17832 +static void epilog(struct inode *dir, struct dentry *dentry,
17833 +                  aufs_bindex_t bindex)
17834 +{
17835 +       struct inode *inode;
17836 +
17837 +       inode = dentry->d_inode;
17838 +       d_drop(dentry);
17839 +       inode->i_ctime = dir->i_ctime;
17840 +
17841 +       if (au_ibstart(dir) == bindex)
17842 +               au_cpup_attr_timesizes(dir);
17843 +       dir->i_version++;
17844 +}
17845 +
17846 +/*
17847 + * when an error happened, remove the created whiteout and revert everything.
17848 + */
17849 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
17850 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
17851 +                    struct dentry *dentry, struct au_dtime *dt)
17852 +{
17853 +       int rerr;
17854 +       struct path h_path = {
17855 +               .dentry = wh_dentry,
17856 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
17857 +       };
17858 +
17859 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
17860 +       if (!rerr) {
17861 +               au_set_dbwh(dentry, bwh);
17862 +               au_dtime_revert(dt);
17863 +               return 0;
17864 +       }
17865 +
17866 +       AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
17867 +               AuDLNPair(dentry), err, rerr);
17868 +       return -EIO;
17869 +}
17870 +
17871 +/* ---------------------------------------------------------------------- */
17872 +
17873 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
17874 +{
17875 +       int err;
17876 +       aufs_bindex_t bwh, bindex, bstart;
17877 +       struct inode *inode, *h_dir;
17878 +       struct dentry *parent, *wh_dentry;
17879 +       /* to reuduce stack size */
17880 +       struct {
17881 +               struct au_dtime dt;
17882 +               struct au_pin pin;
17883 +               struct path h_path;
17884 +       } *a;
17885 +
17886 +       IMustLock(dir);
17887 +
17888 +       err = -ENOMEM;
17889 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17890 +       if (unlikely(!a))
17891 +               goto out;
17892 +
17893 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17894 +       if (unlikely(err))
17895 +               goto out_free;
17896 +       err = au_d_hashed_positive(dentry);
17897 +       if (unlikely(err))
17898 +               goto out_unlock;
17899 +       inode = dentry->d_inode;
17900 +       IMustLock(inode);
17901 +       err = -EISDIR;
17902 +       if (unlikely(S_ISDIR(inode->i_mode)))
17903 +               goto out_unlock; /* possible? */
17904 +
17905 +       bstart = au_dbstart(dentry);
17906 +       bwh = au_dbwh(dentry);
17907 +       bindex = -1;
17908 +       parent = dentry->d_parent; /* dir inode is locked */
17909 +       di_write_lock_parent(parent);
17910 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
17911 +                                       &a->pin);
17912 +       err = PTR_ERR(wh_dentry);
17913 +       if (IS_ERR(wh_dentry))
17914 +               goto out_parent;
17915 +
17916 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
17917 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17918 +       dget(a->h_path.dentry);
17919 +       if (bindex == bstart) {
17920 +               h_dir = au_pinned_h_dir(&a->pin);
17921 +               err = vfsub_unlink(h_dir, &a->h_path, /*force*/0);
17922 +       } else {
17923 +               /* dir inode is locked */
17924 +               h_dir = wh_dentry->d_parent->d_inode;
17925 +               IMustLock(h_dir);
17926 +               err = 0;
17927 +       }
17928 +
17929 +       if (!err) {
17930 +               vfsub_drop_nlink(inode);
17931 +               epilog(dir, dentry, bindex);
17932 +
17933 +               /* update target timestamps */
17934 +               if (bindex == bstart) {
17935 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
17936 +                       /*ignore*/
17937 +                       inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
17938 +               } else
17939 +                       /* todo: this timestamp may be reverted later */
17940 +                       inode->i_ctime = h_dir->i_ctime;
17941 +               goto out_unpin; /* success */
17942 +       }
17943 +
17944 +       /* revert */
17945 +       if (wh_dentry) {
17946 +               int rerr;
17947 +
17948 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
17949 +                                &a->dt);
17950 +               if (rerr)
17951 +                       err = rerr;
17952 +       }
17953 +
17954 +out_unpin:
17955 +       au_unpin(&a->pin);
17956 +       dput(wh_dentry);
17957 +       dput(a->h_path.dentry);
17958 +out_parent:
17959 +       di_write_unlock(parent);
17960 +out_unlock:
17961 +       aufs_read_unlock(dentry, AuLock_DW);
17962 +out_free:
17963 +       kfree(a);
17964 +out:
17965 +       return err;
17966 +}
17967 +
17968 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
17969 +{
17970 +       int err, rmdir_later;
17971 +       aufs_bindex_t bwh, bindex, bstart;
17972 +       struct inode *inode;
17973 +       struct dentry *parent, *wh_dentry, *h_dentry;
17974 +       struct au_whtmp_rmdir *args;
17975 +       /* to reuduce stack size */
17976 +       struct {
17977 +               struct au_dtime dt;
17978 +               struct au_pin pin;
17979 +       } *a;
17980 +
17981 +       IMustLock(dir);
17982 +
17983 +       err = -ENOMEM;
17984 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17985 +       if (unlikely(!a))
17986 +               goto out;
17987 +
17988 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
17989 +       if (unlikely(err))
17990 +               goto out_free;
17991 +       err = au_alive_dir(dentry);
17992 +       if (unlikely(err))
17993 +               goto out_unlock;
17994 +       inode = dentry->d_inode;
17995 +       IMustLock(inode);
17996 +       err = -ENOTDIR;
17997 +       if (unlikely(!S_ISDIR(inode->i_mode)))
17998 +               goto out_unlock; /* possible? */
17999 +
18000 +       err = -ENOMEM;
18001 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
18002 +       if (unlikely(!args))
18003 +               goto out_unlock;
18004 +
18005 +       parent = dentry->d_parent; /* dir inode is locked */
18006 +       di_write_lock_parent(parent);
18007 +       err = au_test_empty(dentry, &args->whlist);
18008 +       if (unlikely(err))
18009 +               goto out_parent;
18010 +
18011 +       bstart = au_dbstart(dentry);
18012 +       bwh = au_dbwh(dentry);
18013 +       bindex = -1;
18014 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
18015 +                                       &a->pin);
18016 +       err = PTR_ERR(wh_dentry);
18017 +       if (IS_ERR(wh_dentry))
18018 +               goto out_parent;
18019 +
18020 +       h_dentry = au_h_dptr(dentry, bstart);
18021 +       dget(h_dentry);
18022 +       rmdir_later = 0;
18023 +       if (bindex == bstart) {
18024 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
18025 +               if (err > 0) {
18026 +                       rmdir_later = err;
18027 +                       err = 0;
18028 +               }
18029 +       } else {
18030 +               /* stop monitoring */
18031 +               au_hn_free(au_hi(inode, bstart));
18032 +
18033 +               /* dir inode is locked */
18034 +               IMustLock(wh_dentry->d_parent->d_inode);
18035 +               err = 0;
18036 +       }
18037 +
18038 +       if (!err) {
18039 +               vfsub_dead_dir(inode);
18040 +               au_set_dbdiropq(dentry, -1);
18041 +               epilog(dir, dentry, bindex);
18042 +
18043 +               if (rmdir_later) {
18044 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
18045 +                       args = NULL;
18046 +               }
18047 +
18048 +               goto out_unpin; /* success */
18049 +       }
18050 +
18051 +       /* revert */
18052 +       AuLabel(revert);
18053 +       if (wh_dentry) {
18054 +               int rerr;
18055 +
18056 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
18057 +                                &a->dt);
18058 +               if (rerr)
18059 +                       err = rerr;
18060 +       }
18061 +
18062 +out_unpin:
18063 +       au_unpin(&a->pin);
18064 +       dput(wh_dentry);
18065 +       dput(h_dentry);
18066 +out_parent:
18067 +       di_write_unlock(parent);
18068 +       if (args)
18069 +               au_whtmp_rmdir_free(args);
18070 +out_unlock:
18071 +       aufs_read_unlock(dentry, AuLock_DW);
18072 +out_free:
18073 +       kfree(a);
18074 +out:
18075 +       AuTraceErr(err);
18076 +       return err;
18077 +}
18078 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
18079 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
18080 +++ linux/fs/aufs/i_op_ren.c    2013-08-23 23:59:39.634916914 +0200
18081 @@ -0,0 +1,1009 @@
18082 +/*
18083 + * Copyright (C) 2005-2013 Junjiro R. Okajima
18084 + *
18085 + * This program, aufs is free software; you can redistribute it and/or modify
18086 + * it under the terms of the GNU General Public License as published by
18087 + * the Free Software Foundation; either version 2 of the License, or
18088 + * (at your option) any later version.
18089 + *
18090 + * This program is distributed in the hope that it will be useful,
18091 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18092 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18093 + * GNU General Public License for more details.
18094 + *
18095 + * You should have received a copy of the GNU General Public License
18096 + * along with this program; if not, write to the Free Software
18097 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18098 + */
18099 +
18100 +/*
18101 + * inode operation (rename entry)
18102 + * todo: this is crazy monster
18103 + */
18104 +
18105 +#include "aufs.h"
18106 +
18107 +enum { AuSRC, AuDST, AuSrcDst };
18108 +enum { AuPARENT, AuCHILD, AuParentChild };
18109 +
18110 +#define AuRen_ISDIR    1
18111 +#define AuRen_ISSAMEDIR        (1 << 1)
18112 +#define AuRen_WHSRC    (1 << 2)
18113 +#define AuRen_WHDST    (1 << 3)
18114 +#define AuRen_MNT_WRITE        (1 << 4)
18115 +#define AuRen_DT_DSTDIR        (1 << 5)
18116 +#define AuRen_DIROPQ   (1 << 6)
18117 +#define AuRen_CPUP     (1 << 7)
18118 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
18119 +#define au_fset_ren(flags, name) \
18120 +       do { (flags) |= AuRen_##name; } while (0)
18121 +#define au_fclr_ren(flags, name) \
18122 +       do { (flags) &= ~AuRen_##name; } while (0)
18123 +
18124 +struct au_ren_args {
18125 +       struct {
18126 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
18127 +                       *wh_dentry;
18128 +               struct inode *dir, *inode;
18129 +               struct au_hinode *hdir;
18130 +               struct au_dtime dt[AuParentChild];
18131 +               aufs_bindex_t bstart;
18132 +       } sd[AuSrcDst];
18133 +
18134 +#define src_dentry     sd[AuSRC].dentry
18135 +#define src_dir                sd[AuSRC].dir
18136 +#define src_inode      sd[AuSRC].inode
18137 +#define src_h_dentry   sd[AuSRC].h_dentry
18138 +#define src_parent     sd[AuSRC].parent
18139 +#define src_h_parent   sd[AuSRC].h_parent
18140 +#define src_wh_dentry  sd[AuSRC].wh_dentry
18141 +#define src_hdir       sd[AuSRC].hdir
18142 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
18143 +#define src_dt         sd[AuSRC].dt
18144 +#define src_bstart     sd[AuSRC].bstart
18145 +
18146 +#define dst_dentry     sd[AuDST].dentry
18147 +#define dst_dir                sd[AuDST].dir
18148 +#define dst_inode      sd[AuDST].inode
18149 +#define dst_h_dentry   sd[AuDST].h_dentry
18150 +#define dst_parent     sd[AuDST].parent
18151 +#define dst_h_parent   sd[AuDST].h_parent
18152 +#define dst_wh_dentry  sd[AuDST].wh_dentry
18153 +#define dst_hdir       sd[AuDST].hdir
18154 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
18155 +#define dst_dt         sd[AuDST].dt
18156 +#define dst_bstart     sd[AuDST].bstart
18157 +
18158 +       struct dentry *h_trap;
18159 +       struct au_branch *br;
18160 +       struct au_hinode *src_hinode;
18161 +       struct path h_path;
18162 +       struct au_nhash whlist;
18163 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
18164 +
18165 +       unsigned int flags;
18166 +
18167 +       struct au_whtmp_rmdir *thargs;
18168 +       struct dentry *h_dst;
18169 +};
18170 +
18171 +/* ---------------------------------------------------------------------- */
18172 +
18173 +/*
18174 + * functions for reverting.
18175 + * when an error happened in a single rename systemcall, we should revert
18176 + * everything as if nothing happend.
18177 + * we don't need to revert the copied-up/down the parent dir since they are
18178 + * harmless.
18179 + */
18180 +
18181 +#define RevertFailure(fmt, ...) do { \
18182 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
18183 +               ##__VA_ARGS__, err, rerr); \
18184 +       err = -EIO; \
18185 +} while (0)
18186 +
18187 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
18188 +{
18189 +       int rerr;
18190 +
18191 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
18192 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
18193 +       au_hn_imtx_unlock(a->src_hinode);
18194 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
18195 +       if (rerr)
18196 +               RevertFailure("remove diropq %.*s", AuDLNPair(a->src_dentry));
18197 +}
18198 +
18199 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
18200 +{
18201 +       int rerr;
18202 +
18203 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
18204 +                                         a->src_h_parent);
18205 +       rerr = PTR_ERR(a->h_path.dentry);
18206 +       if (IS_ERR(a->h_path.dentry)) {
18207 +               RevertFailure("lkup one %.*s", AuDLNPair(a->src_dentry));
18208 +               return;
18209 +       }
18210 +
18211 +       rerr = vfsub_rename(a->dst_h_dir,
18212 +                           au_h_dptr(a->src_dentry, a->btgt),
18213 +                           a->src_h_dir, &a->h_path);
18214 +       d_drop(a->h_path.dentry);
18215 +       dput(a->h_path.dentry);
18216 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
18217 +       if (rerr)
18218 +               RevertFailure("rename %.*s", AuDLNPair(a->src_dentry));
18219 +}
18220 +
18221 +static void au_ren_rev_cpup(int err, struct au_ren_args *a)
18222 +{
18223 +       int rerr;
18224 +
18225 +       a->h_path.dentry = a->dst_h_dentry;
18226 +       rerr = vfsub_unlink(a->dst_h_dir, &a->h_path, /*force*/0);
18227 +       au_set_h_dptr(a->src_dentry, a->btgt, NULL);
18228 +       au_set_dbstart(a->src_dentry, a->src_bstart);
18229 +       if (rerr)
18230 +               RevertFailure("unlink %.*s", AuDLNPair(a->dst_h_dentry));
18231 +}
18232 +
18233 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
18234 +{
18235 +       int rerr;
18236 +
18237 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
18238 +                                         a->dst_h_parent);
18239 +       rerr = PTR_ERR(a->h_path.dentry);
18240 +       if (IS_ERR(a->h_path.dentry)) {
18241 +               RevertFailure("lkup one %.*s", AuDLNPair(a->dst_dentry));
18242 +               return;
18243 +       }
18244 +       if (a->h_path.dentry->d_inode) {
18245 +               d_drop(a->h_path.dentry);
18246 +               dput(a->h_path.dentry);
18247 +               return;
18248 +       }
18249 +
18250 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path);
18251 +       d_drop(a->h_path.dentry);
18252 +       dput(a->h_path.dentry);
18253 +       if (!rerr)
18254 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
18255 +       else
18256 +               RevertFailure("rename %.*s", AuDLNPair(a->h_dst));
18257 +}
18258 +
18259 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
18260 +{
18261 +       int rerr;
18262 +
18263 +       a->h_path.dentry = a->src_wh_dentry;
18264 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
18265 +       au_set_dbwh(a->src_dentry, a->src_bwh);
18266 +       if (rerr)
18267 +               RevertFailure("unlink %.*s", AuDLNPair(a->src_wh_dentry));
18268 +}
18269 +#undef RevertFailure
18270 +
18271 +/* ---------------------------------------------------------------------- */
18272 +
18273 +/*
18274 + * when we have to copyup the renaming entry, do it with the rename-target name
18275 + * in order to minimize the cost (the later actual rename is unnecessary).
18276 + * otherwise rename it on the target branch.
18277 + */
18278 +static int au_ren_or_cpup(struct au_ren_args *a)
18279 +{
18280 +       int err;
18281 +       struct dentry *d;
18282 +
18283 +       d = a->src_dentry;
18284 +       if (au_dbstart(d) == a->btgt) {
18285 +               a->h_path.dentry = a->dst_h_dentry;
18286 +               if (au_ftest_ren(a->flags, DIROPQ)
18287 +                   && au_dbdiropq(d) == a->btgt)
18288 +                       au_fclr_ren(a->flags, DIROPQ);
18289 +               AuDebugOn(au_dbstart(d) != a->btgt);
18290 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
18291 +                                  a->dst_h_dir, &a->h_path);
18292 +       } else
18293 +               BUG();
18294 +
18295 +       if (!err && a->h_dst)
18296 +               /* it will be set to dinfo later */
18297 +               dget(a->h_dst);
18298 +
18299 +       return err;
18300 +}
18301 +
18302 +/* cf. aufs_rmdir() */
18303 +static int au_ren_del_whtmp(struct au_ren_args *a)
18304 +{
18305 +       int err;
18306 +       struct inode *dir;
18307 +
18308 +       dir = a->dst_dir;
18309 +       SiMustAnyLock(dir->i_sb);
18310 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
18311 +                                    au_sbi(dir->i_sb)->si_dirwh)
18312 +           || au_test_fs_remote(a->h_dst->d_sb)) {
18313 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
18314 +               if (unlikely(err))
18315 +                       pr_warn("failed removing whtmp dir %.*s (%d), "
18316 +                               "ignored.\n", AuDLNPair(a->h_dst), err);
18317 +       } else {
18318 +               au_nhash_wh_free(&a->thargs->whlist);
18319 +               a->thargs->whlist = a->whlist;
18320 +               a->whlist.nh_num = 0;
18321 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
18322 +               dput(a->h_dst);
18323 +               a->thargs = NULL;
18324 +       }
18325 +
18326 +       return 0;
18327 +}
18328 +
18329 +/* make it 'opaque' dir. */
18330 +static int au_ren_diropq(struct au_ren_args *a)
18331 +{
18332 +       int err;
18333 +       struct dentry *diropq;
18334 +
18335 +       err = 0;
18336 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
18337 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
18338 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
18339 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
18340 +       au_hn_imtx_unlock(a->src_hinode);
18341 +       if (IS_ERR(diropq))
18342 +               err = PTR_ERR(diropq);
18343 +       dput(diropq);
18344 +
18345 +       return err;
18346 +}
18347 +
18348 +static int do_rename(struct au_ren_args *a)
18349 +{
18350 +       int err;
18351 +       struct dentry *d, *h_d;
18352 +
18353 +       /* prepare workqueue args for asynchronous rmdir */
18354 +       h_d = a->dst_h_dentry;
18355 +       if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
18356 +               err = -ENOMEM;
18357 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
18358 +               if (unlikely(!a->thargs))
18359 +                       goto out;
18360 +               a->h_dst = dget(h_d);
18361 +       }
18362 +
18363 +       /* create whiteout for src_dentry */
18364 +       if (au_ftest_ren(a->flags, WHSRC)) {
18365 +               a->src_bwh = au_dbwh(a->src_dentry);
18366 +               AuDebugOn(a->src_bwh >= 0);
18367 +               a->src_wh_dentry
18368 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
18369 +               err = PTR_ERR(a->src_wh_dentry);
18370 +               if (IS_ERR(a->src_wh_dentry))
18371 +                       goto out_thargs;
18372 +       }
18373 +
18374 +       /* lookup whiteout for dentry */
18375 +       if (au_ftest_ren(a->flags, WHDST)) {
18376 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
18377 +                                a->br);
18378 +               err = PTR_ERR(h_d);
18379 +               if (IS_ERR(h_d))
18380 +                       goto out_whsrc;
18381 +               if (!h_d->d_inode)
18382 +                       dput(h_d);
18383 +               else
18384 +                       a->dst_wh_dentry = h_d;
18385 +       }
18386 +
18387 +       /* rename dentry to tmpwh */
18388 +       if (a->thargs) {
18389 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
18390 +               if (unlikely(err))
18391 +                       goto out_whdst;
18392 +
18393 +               d = a->dst_dentry;
18394 +               au_set_h_dptr(d, a->btgt, NULL);
18395 +               err = au_lkup_neg(d, a->btgt, /*wh*/0);
18396 +               if (unlikely(err))
18397 +                       goto out_whtmp;
18398 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
18399 +       }
18400 +
18401 +       BUG_ON(a->dst_h_dentry->d_inode && a->src_bstart != a->btgt);
18402 +
18403 +       /* rename by vfs_rename or cpup */
18404 +       d = a->dst_dentry;
18405 +       if (au_ftest_ren(a->flags, ISDIR)
18406 +           && (a->dst_wh_dentry
18407 +               || au_dbdiropq(d) == a->btgt
18408 +               /* hide the lower to keep xino */
18409 +               || a->btgt < au_dbend(d)
18410 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
18411 +               au_fset_ren(a->flags, DIROPQ);
18412 +       err = au_ren_or_cpup(a);
18413 +       if (unlikely(err))
18414 +               /* leave the copied-up one */
18415 +               goto out_whtmp;
18416 +
18417 +       /* make dir opaque */
18418 +       if (au_ftest_ren(a->flags, DIROPQ)) {
18419 +               err = au_ren_diropq(a);
18420 +               if (unlikely(err))
18421 +                       goto out_rename;
18422 +       }
18423 +
18424 +       /* update target timestamps */
18425 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
18426 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
18427 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
18428 +       a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
18429 +
18430 +       /* remove whiteout for dentry */
18431 +       if (a->dst_wh_dentry) {
18432 +               a->h_path.dentry = a->dst_wh_dentry;
18433 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
18434 +                                         a->dst_dentry);
18435 +               if (unlikely(err))
18436 +                       goto out_diropq;
18437 +       }
18438 +
18439 +       /* remove whtmp */
18440 +       if (a->thargs)
18441 +               au_ren_del_whtmp(a); /* ignore this error */
18442 +
18443 +       err = 0;
18444 +       goto out_success;
18445 +
18446 +out_diropq:
18447 +       if (au_ftest_ren(a->flags, DIROPQ))
18448 +               au_ren_rev_diropq(err, a);
18449 +out_rename:
18450 +       if (!au_ftest_ren(a->flags, CPUP))
18451 +               au_ren_rev_rename(err, a);
18452 +       else
18453 +               au_ren_rev_cpup(err, a);
18454 +       dput(a->h_dst);
18455 +out_whtmp:
18456 +       if (a->thargs)
18457 +               au_ren_rev_whtmp(err, a);
18458 +out_whdst:
18459 +       dput(a->dst_wh_dentry);
18460 +       a->dst_wh_dentry = NULL;
18461 +out_whsrc:
18462 +       if (a->src_wh_dentry)
18463 +               au_ren_rev_whsrc(err, a);
18464 +out_success:
18465 +       dput(a->src_wh_dentry);
18466 +       dput(a->dst_wh_dentry);
18467 +out_thargs:
18468 +       if (a->thargs) {
18469 +               dput(a->h_dst);
18470 +               au_whtmp_rmdir_free(a->thargs);
18471 +               a->thargs = NULL;
18472 +       }
18473 +out:
18474 +       return err;
18475 +}
18476 +
18477 +/* ---------------------------------------------------------------------- */
18478 +
18479 +/*
18480 + * test if @dentry dir can be rename destination or not.
18481 + * success means, it is a logically empty dir.
18482 + */
18483 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
18484 +{
18485 +       return au_test_empty(dentry, whlist);
18486 +}
18487 +
18488 +/*
18489 + * test if @dentry dir can be rename source or not.
18490 + * if it can, return 0 and @children is filled.
18491 + * success means,
18492 + * - it is a logically empty dir.
18493 + * - or, it exists on writable branch and has no children including whiteouts
18494 + *       on the lower branch.
18495 + */
18496 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
18497 +{
18498 +       int err;
18499 +       unsigned int rdhash;
18500 +       aufs_bindex_t bstart;
18501 +
18502 +       bstart = au_dbstart(dentry);
18503 +       if (bstart != btgt) {
18504 +               struct au_nhash whlist;
18505 +
18506 +               SiMustAnyLock(dentry->d_sb);
18507 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
18508 +               if (!rdhash)
18509 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
18510 +                                                          dentry));
18511 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
18512 +               if (unlikely(err))
18513 +                       goto out;
18514 +               err = au_test_empty(dentry, &whlist);
18515 +               au_nhash_wh_free(&whlist);
18516 +               goto out;
18517 +       }
18518 +
18519 +       if (bstart == au_dbtaildir(dentry))
18520 +               return 0; /* success */
18521 +
18522 +       err = au_test_empty_lower(dentry);
18523 +
18524 +out:
18525 +       if (err == -ENOTEMPTY) {
18526 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
18527 +                       " is not supported\n");
18528 +               err = -EXDEV;
18529 +       }
18530 +       return err;
18531 +}
18532 +
18533 +/* side effect: sets whlist and h_dentry */
18534 +static int au_ren_may_dir(struct au_ren_args *a)
18535 +{
18536 +       int err;
18537 +       unsigned int rdhash;
18538 +       struct dentry *d;
18539 +
18540 +       d = a->dst_dentry;
18541 +       SiMustAnyLock(d->d_sb);
18542 +
18543 +       err = 0;
18544 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
18545 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
18546 +               if (!rdhash)
18547 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
18548 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
18549 +               if (unlikely(err))
18550 +                       goto out;
18551 +
18552 +               au_set_dbstart(d, a->dst_bstart);
18553 +               err = may_rename_dstdir(d, &a->whlist);
18554 +               au_set_dbstart(d, a->btgt);
18555 +       }
18556 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
18557 +       if (unlikely(err))
18558 +               goto out;
18559 +
18560 +       d = a->src_dentry;
18561 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
18562 +       if (au_ftest_ren(a->flags, ISDIR)) {
18563 +               err = may_rename_srcdir(d, a->btgt);
18564 +               if (unlikely(err)) {
18565 +                       au_nhash_wh_free(&a->whlist);
18566 +                       a->whlist.nh_num = 0;
18567 +               }
18568 +       }
18569 +out:
18570 +       return err;
18571 +}
18572 +
18573 +/* ---------------------------------------------------------------------- */
18574 +
18575 +/*
18576 + * simple tests for rename.
18577 + * following the checks in vfs, plus the parent-child relationship.
18578 + */
18579 +static int au_may_ren(struct au_ren_args *a)
18580 +{
18581 +       int err, isdir;
18582 +       struct inode *h_inode;
18583 +
18584 +       if (a->src_bstart == a->btgt) {
18585 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
18586 +                                au_ftest_ren(a->flags, ISDIR));
18587 +               if (unlikely(err))
18588 +                       goto out;
18589 +               err = -EINVAL;
18590 +               if (unlikely(a->src_h_dentry == a->h_trap))
18591 +                       goto out;
18592 +       }
18593 +
18594 +       err = 0;
18595 +       if (a->dst_bstart != a->btgt)
18596 +               goto out;
18597 +
18598 +       err = -ENOTEMPTY;
18599 +       if (unlikely(a->dst_h_dentry == a->h_trap))
18600 +               goto out;
18601 +
18602 +       err = -EIO;
18603 +       h_inode = a->dst_h_dentry->d_inode;
18604 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
18605 +       if (!a->dst_dentry->d_inode) {
18606 +               if (unlikely(h_inode))
18607 +                       goto out;
18608 +               err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
18609 +                                isdir);
18610 +       } else {
18611 +               if (unlikely(!h_inode || !h_inode->i_nlink))
18612 +                       goto out;
18613 +               err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
18614 +                                isdir);
18615 +               if (unlikely(err))
18616 +                       goto out;
18617 +       }
18618 +
18619 +out:
18620 +       if (unlikely(err == -ENOENT || err == -EEXIST))
18621 +               err = -EIO;
18622 +       AuTraceErr(err);
18623 +       return err;
18624 +}
18625 +
18626 +/* ---------------------------------------------------------------------- */
18627 +
18628 +/*
18629 + * locking order
18630 + * (VFS)
18631 + * - src_dir and dir by lock_rename()
18632 + * - inode if exitsts
18633 + * (aufs)
18634 + * - lock all
18635 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
18636 + *     + si_read_lock
18637 + *     + di_write_lock2_child()
18638 + *       + di_write_lock_child()
18639 + *        + ii_write_lock_child()
18640 + *       + di_write_lock_child2()
18641 + *        + ii_write_lock_child2()
18642 + *     + src_parent and parent
18643 + *       + di_write_lock_parent()
18644 + *        + ii_write_lock_parent()
18645 + *       + di_write_lock_parent2()
18646 + *        + ii_write_lock_parent2()
18647 + *   + lower src_dir and dir by vfsub_lock_rename()
18648 + *   + verify the every relationships between child and parent. if any
18649 + *     of them failed, unlock all and return -EBUSY.
18650 + */
18651 +static void au_ren_unlock(struct au_ren_args *a)
18652 +{
18653 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
18654 +                           a->dst_h_parent, a->dst_hdir);
18655 +       if (au_ftest_ren(a->flags, MNT_WRITE))
18656 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
18657 +}
18658 +
18659 +static int au_ren_lock(struct au_ren_args *a)
18660 +{
18661 +       int err;
18662 +       unsigned int udba;
18663 +
18664 +       err = 0;
18665 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
18666 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
18667 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
18668 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
18669 +
18670 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
18671 +       if (unlikely(err))
18672 +               goto out;
18673 +       au_fset_ren(a->flags, MNT_WRITE);
18674 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
18675 +                                     a->dst_h_parent, a->dst_hdir);
18676 +       udba = au_opt_udba(a->src_dentry->d_sb);
18677 +       if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
18678 +                    || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
18679 +               err = au_busy_or_stale();
18680 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
18681 +               err = au_h_verify(a->src_h_dentry, udba,
18682 +                                 a->src_h_parent->d_inode, a->src_h_parent,
18683 +                                 a->br);
18684 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
18685 +               err = au_h_verify(a->dst_h_dentry, udba,
18686 +                                 a->dst_h_parent->d_inode, a->dst_h_parent,
18687 +                                 a->br);
18688 +       if (!err)
18689 +               goto out; /* success */
18690 +
18691 +       err = au_busy_or_stale();
18692 +       au_ren_unlock(a);
18693 +
18694 +out:
18695 +       return err;
18696 +}
18697 +
18698 +/* ---------------------------------------------------------------------- */
18699 +
18700 +static void au_ren_refresh_dir(struct au_ren_args *a)
18701 +{
18702 +       struct inode *dir;
18703 +
18704 +       dir = a->dst_dir;
18705 +       dir->i_version++;
18706 +       if (au_ftest_ren(a->flags, ISDIR)) {
18707 +               /* is this updating defined in POSIX? */
18708 +               au_cpup_attr_timesizes(a->src_inode);
18709 +               au_cpup_attr_nlink(dir, /*force*/1);
18710 +       }
18711 +
18712 +       if (au_ibstart(dir) == a->btgt)
18713 +               au_cpup_attr_timesizes(dir);
18714 +
18715 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
18716 +               return;
18717 +
18718 +       dir = a->src_dir;
18719 +       dir->i_version++;
18720 +       if (au_ftest_ren(a->flags, ISDIR))
18721 +               au_cpup_attr_nlink(dir, /*force*/1);
18722 +       if (au_ibstart(dir) == a->btgt)
18723 +               au_cpup_attr_timesizes(dir);
18724 +}
18725 +
18726 +static void au_ren_refresh(struct au_ren_args *a)
18727 +{
18728 +       aufs_bindex_t bend, bindex;
18729 +       struct dentry *d, *h_d;
18730 +       struct inode *i, *h_i;
18731 +       struct super_block *sb;
18732 +
18733 +       d = a->dst_dentry;
18734 +       d_drop(d);
18735 +       if (a->h_dst)
18736 +               /* already dget-ed by au_ren_or_cpup() */
18737 +               au_set_h_dptr(d, a->btgt, a->h_dst);
18738 +
18739 +       i = a->dst_inode;
18740 +       if (i) {
18741 +               if (!au_ftest_ren(a->flags, ISDIR))
18742 +                       vfsub_drop_nlink(i);
18743 +               else {
18744 +                       vfsub_dead_dir(i);
18745 +                       au_cpup_attr_timesizes(i);
18746 +               }
18747 +               au_update_dbrange(d, /*do_put_zero*/1);
18748 +       } else {
18749 +               bend = a->btgt;
18750 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
18751 +                       au_set_h_dptr(d, bindex, NULL);
18752 +               bend = au_dbend(d);
18753 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
18754 +                       au_set_h_dptr(d, bindex, NULL);
18755 +               au_update_dbrange(d, /*do_put_zero*/0);
18756 +       }
18757 +
18758 +       d = a->src_dentry;
18759 +       au_set_dbwh(d, -1);
18760 +       bend = au_dbend(d);
18761 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
18762 +               h_d = au_h_dptr(d, bindex);
18763 +               if (h_d)
18764 +                       au_set_h_dptr(d, bindex, NULL);
18765 +       }
18766 +       au_set_dbend(d, a->btgt);
18767 +
18768 +       sb = d->d_sb;
18769 +       i = a->src_inode;
18770 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
18771 +               return; /* success */
18772 +
18773 +       bend = au_ibend(i);
18774 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
18775 +               h_i = au_h_iptr(i, bindex);
18776 +               if (h_i) {
18777 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
18778 +                       /* ignore this error */
18779 +                       au_set_h_iptr(i, bindex, NULL, 0);
18780 +               }
18781 +       }
18782 +       au_set_ibend(i, a->btgt);
18783 +}
18784 +
18785 +/* ---------------------------------------------------------------------- */
18786 +
18787 +/* mainly for link(2) and rename(2) */
18788 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
18789 +{
18790 +       aufs_bindex_t bdiropq, bwh;
18791 +       struct dentry *parent;
18792 +       struct au_branch *br;
18793 +
18794 +       parent = dentry->d_parent;
18795 +       IMustLock(parent->d_inode); /* dir is locked */
18796 +
18797 +       bdiropq = au_dbdiropq(parent);
18798 +       bwh = au_dbwh(dentry);
18799 +       br = au_sbr(dentry->d_sb, btgt);
18800 +       if (au_br_rdonly(br)
18801 +           || (0 <= bdiropq && bdiropq < btgt)
18802 +           || (0 <= bwh && bwh < btgt))
18803 +               btgt = -1;
18804 +
18805 +       AuDbg("btgt %d\n", btgt);
18806 +       return btgt;
18807 +}
18808 +
18809 +/* sets src_bstart, dst_bstart and btgt */
18810 +static int au_ren_wbr(struct au_ren_args *a)
18811 +{
18812 +       int err;
18813 +       struct au_wr_dir_args wr_dir_args = {
18814 +               /* .force_btgt  = -1, */
18815 +               .flags          = AuWrDir_ADD_ENTRY
18816 +       };
18817 +
18818 +       a->src_bstart = au_dbstart(a->src_dentry);
18819 +       a->dst_bstart = au_dbstart(a->dst_dentry);
18820 +       if (au_ftest_ren(a->flags, ISDIR))
18821 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
18822 +       wr_dir_args.force_btgt = a->src_bstart;
18823 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
18824 +               wr_dir_args.force_btgt = a->dst_bstart;
18825 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
18826 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
18827 +       a->btgt = err;
18828 +
18829 +       return err;
18830 +}
18831 +
18832 +static void au_ren_dt(struct au_ren_args *a)
18833 +{
18834 +       a->h_path.dentry = a->src_h_parent;
18835 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
18836 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
18837 +               a->h_path.dentry = a->dst_h_parent;
18838 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
18839 +       }
18840 +
18841 +       au_fclr_ren(a->flags, DT_DSTDIR);
18842 +       if (!au_ftest_ren(a->flags, ISDIR))
18843 +               return;
18844 +
18845 +       a->h_path.dentry = a->src_h_dentry;
18846 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
18847 +       if (a->dst_h_dentry->d_inode) {
18848 +               au_fset_ren(a->flags, DT_DSTDIR);
18849 +               a->h_path.dentry = a->dst_h_dentry;
18850 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
18851 +       }
18852 +}
18853 +
18854 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
18855 +{
18856 +       struct dentry *h_d;
18857 +       struct mutex *h_mtx;
18858 +
18859 +       au_dtime_revert(a->src_dt + AuPARENT);
18860 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
18861 +               au_dtime_revert(a->dst_dt + AuPARENT);
18862 +
18863 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
18864 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
18865 +               h_mtx = &h_d->d_inode->i_mutex;
18866 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18867 +               au_dtime_revert(a->src_dt + AuCHILD);
18868 +               mutex_unlock(h_mtx);
18869 +
18870 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
18871 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
18872 +                       h_mtx = &h_d->d_inode->i_mutex;
18873 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18874 +                       au_dtime_revert(a->dst_dt + AuCHILD);
18875 +                       mutex_unlock(h_mtx);
18876 +               }
18877 +       }
18878 +}
18879 +
18880 +/* ---------------------------------------------------------------------- */
18881 +
18882 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
18883 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
18884 +{
18885 +       int err, flags;
18886 +       /* reduce stack space */
18887 +       struct au_ren_args *a;
18888 +
18889 +       AuDbg("%.*s, %.*s\n", AuDLNPair(_src_dentry), AuDLNPair(_dst_dentry));
18890 +       IMustLock(_src_dir);
18891 +       IMustLock(_dst_dir);
18892 +
18893 +       err = -ENOMEM;
18894 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
18895 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18896 +       if (unlikely(!a))
18897 +               goto out;
18898 +
18899 +       a->src_dir = _src_dir;
18900 +       a->src_dentry = _src_dentry;
18901 +       a->src_inode = a->src_dentry->d_inode;
18902 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
18903 +       a->dst_dir = _dst_dir;
18904 +       a->dst_dentry = _dst_dentry;
18905 +       a->dst_inode = a->dst_dentry->d_inode;
18906 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
18907 +       if (a->dst_inode) {
18908 +               IMustLock(a->dst_inode);
18909 +               au_igrab(a->dst_inode);
18910 +       }
18911 +
18912 +       err = -ENOTDIR;
18913 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
18914 +       if (S_ISDIR(a->src_inode->i_mode)) {
18915 +               au_fset_ren(a->flags, ISDIR);
18916 +               if (unlikely(a->dst_inode && !S_ISDIR(a->dst_inode->i_mode)))
18917 +                       goto out_free;
18918 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
18919 +                                               AuLock_DIR | flags);
18920 +       } else
18921 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
18922 +                                               flags);
18923 +       if (unlikely(err))
18924 +               goto out_free;
18925 +
18926 +       err = au_d_hashed_positive(a->src_dentry);
18927 +       if (unlikely(err))
18928 +               goto out_unlock;
18929 +       err = -ENOENT;
18930 +       if (a->dst_inode) {
18931 +               /*
18932 +                * If it is a dir, VFS unhash dst_dentry before this
18933 +                * function. It means we cannot rely upon d_unhashed().
18934 +                */
18935 +               if (unlikely(!a->dst_inode->i_nlink))
18936 +                       goto out_unlock;
18937 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
18938 +                       err = au_d_hashed_positive(a->dst_dentry);
18939 +                       if (unlikely(err))
18940 +                               goto out_unlock;
18941 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
18942 +                       goto out_unlock;
18943 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
18944 +               goto out_unlock;
18945 +
18946 +       /*
18947 +        * is it possible?
18948 +        * yes, it happend (in linux-3.3-rcN) but I don't know why.
18949 +        * there may exist a problem somewhere else.
18950 +        */
18951 +       err = -EINVAL;
18952 +       if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
18953 +               goto out_unlock;
18954 +
18955 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
18956 +       di_write_lock_parent(a->dst_parent);
18957 +
18958 +       /* which branch we process */
18959 +       err = au_ren_wbr(a);
18960 +       if (unlikely(err < 0))
18961 +               goto out_parent;
18962 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
18963 +       a->h_path.mnt = au_br_mnt(a->br);
18964 +
18965 +       /* are they available to be renamed */
18966 +       err = au_ren_may_dir(a);
18967 +       if (unlikely(err))
18968 +               goto out_children;
18969 +
18970 +       /* prepare the writable parent dir on the same branch */
18971 +       if (a->dst_bstart == a->btgt) {
18972 +               au_fset_ren(a->flags, WHDST);
18973 +       } else {
18974 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
18975 +               if (unlikely(err))
18976 +                       goto out_children;
18977 +       }
18978 +
18979 +       if (a->src_dir != a->dst_dir) {
18980 +               /*
18981 +                * this temporary unlock is safe,
18982 +                * because both dir->i_mutex are locked.
18983 +                */
18984 +               di_write_unlock(a->dst_parent);
18985 +               di_write_lock_parent(a->src_parent);
18986 +               err = au_wr_dir_need_wh(a->src_dentry,
18987 +                                       au_ftest_ren(a->flags, ISDIR),
18988 +                                       &a->btgt);
18989 +               di_write_unlock(a->src_parent);
18990 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
18991 +               au_fclr_ren(a->flags, ISSAMEDIR);
18992 +       } else
18993 +               err = au_wr_dir_need_wh(a->src_dentry,
18994 +                                       au_ftest_ren(a->flags, ISDIR),
18995 +                                       &a->btgt);
18996 +       if (unlikely(err < 0))
18997 +               goto out_children;
18998 +       if (err)
18999 +               au_fset_ren(a->flags, WHSRC);
19000 +
19001 +       /* cpup src */
19002 +       if (a->src_bstart != a->btgt) {
19003 +               struct au_pin pin;
19004 +
19005 +               err = au_pin(&pin, a->src_dentry, a->btgt,
19006 +                            au_opt_udba(a->src_dentry->d_sb),
19007 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19008 +               if (!err) {
19009 +                       struct au_cp_generic cpg = {
19010 +                               .dentry = a->src_dentry,
19011 +                               .bdst   = a->btgt,
19012 +                               .bsrc   = a->src_bstart,
19013 +                               .len    = -1,
19014 +                               .pin    = &pin,
19015 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
19016 +                       };
19017 +                       AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
19018 +                       err = au_sio_cpup_simple(&cpg);
19019 +                       au_unpin(&pin);
19020 +               }
19021 +               if (unlikely(err))
19022 +                       goto out_children;
19023 +               a->src_bstart = a->btgt;
19024 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
19025 +               au_fset_ren(a->flags, WHSRC);
19026 +       }
19027 +
19028 +       /* lock them all */
19029 +       err = au_ren_lock(a);
19030 +       if (unlikely(err))
19031 +               /* leave the copied-up one */
19032 +               goto out_children;
19033 +
19034 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
19035 +               err = au_may_ren(a);
19036 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
19037 +               err = -ENAMETOOLONG;
19038 +       if (unlikely(err))
19039 +               goto out_hdir;
19040 +
19041 +       /* store timestamps to be revertible */
19042 +       au_ren_dt(a);
19043 +
19044 +       /* here we go */
19045 +       err = do_rename(a);
19046 +       if (unlikely(err))
19047 +               goto out_dt;
19048 +
19049 +       /* update dir attributes */
19050 +       au_ren_refresh_dir(a);
19051 +
19052 +       /* dput/iput all lower dentries */
19053 +       au_ren_refresh(a);
19054 +
19055 +       goto out_hdir; /* success */
19056 +
19057 +out_dt:
19058 +       au_ren_rev_dt(err, a);
19059 +out_hdir:
19060 +       au_ren_unlock(a);
19061 +out_children:
19062 +       au_nhash_wh_free(&a->whlist);
19063 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
19064 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
19065 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
19066 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
19067 +       }
19068 +out_parent:
19069 +       if (!err)
19070 +               d_move(a->src_dentry, a->dst_dentry);
19071 +       else {
19072 +               au_update_dbstart(a->dst_dentry);
19073 +               if (!a->dst_inode)
19074 +                       d_drop(a->dst_dentry);
19075 +       }
19076 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
19077 +               di_write_unlock(a->dst_parent);
19078 +       else
19079 +               di_write_unlock2(a->src_parent, a->dst_parent);
19080 +out_unlock:
19081 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
19082 +out_free:
19083 +       iput(a->dst_inode);
19084 +       if (a->thargs)
19085 +               au_whtmp_rmdir_free(a->thargs);
19086 +       kfree(a);
19087 +out:
19088 +       AuTraceErr(err);
19089 +       return err;
19090 +}
19091 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
19092 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
19093 +++ linux/fs/aufs/Kconfig       2013-07-06 13:20:47.736864659 +0200
19094 @@ -0,0 +1,202 @@
19095 +config AUFS_FS
19096 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
19097 +       help
19098 +       Aufs is a stackable unification filesystem such as Unionfs,
19099 +       which unifies several directories and provides a merged single
19100 +       directory.
19101 +       In the early days, aufs was entirely re-designed and
19102 +       re-implemented Unionfs Version 1.x series. Introducing many
19103 +       original ideas, approaches and improvements, it becomes totally
19104 +       different from Unionfs while keeping the basic features.
19105 +
19106 +if AUFS_FS
19107 +choice
19108 +       prompt "Maximum number of branches"
19109 +       default AUFS_BRANCH_MAX_127
19110 +       help
19111 +       Specifies the maximum number of branches (or member directories)
19112 +       in a single aufs. The larger value consumes more system
19113 +       resources and has a minor impact to performance.
19114 +config AUFS_BRANCH_MAX_127
19115 +       bool "127"
19116 +       help
19117 +       Specifies the maximum number of branches (or member directories)
19118 +       in a single aufs. The larger value consumes more system
19119 +       resources and has a minor impact to performance.
19120 +config AUFS_BRANCH_MAX_511
19121 +       bool "511"
19122 +       help
19123 +       Specifies the maximum number of branches (or member directories)
19124 +       in a single aufs. The larger value consumes more system
19125 +       resources and has a minor impact to performance.
19126 +config AUFS_BRANCH_MAX_1023
19127 +       bool "1023"
19128 +       help
19129 +       Specifies the maximum number of branches (or member directories)
19130 +       in a single aufs. The larger value consumes more system
19131 +       resources and has a minor impact to performance.
19132 +config AUFS_BRANCH_MAX_32767
19133 +       bool "32767"
19134 +       help
19135 +       Specifies the maximum number of branches (or member directories)
19136 +       in a single aufs. The larger value consumes more system
19137 +       resources and has a minor impact to performance.
19138 +endchoice
19139 +
19140 +config AUFS_SBILIST
19141 +       bool
19142 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
19143 +       default y
19144 +       help
19145 +       Automatic configuration for internal use.
19146 +       When aufs supports Magic SysRq or /proc, enabled automatically.
19147 +
19148 +config AUFS_HNOTIFY
19149 +       bool "Detect direct branch access (bypassing aufs)"
19150 +       help
19151 +       If you want to modify files on branches directly, eg. bypassing aufs,
19152 +       and want aufs to detect the changes of them fully, then enable this
19153 +       option and use 'udba=notify' mount option.
19154 +       Currently there is only one available configuration, "fsnotify".
19155 +       It will have a negative impact to the performance.
19156 +       See detail in aufs.5.
19157 +
19158 +choice
19159 +       prompt "method" if AUFS_HNOTIFY
19160 +       default AUFS_HFSNOTIFY
19161 +config AUFS_HFSNOTIFY
19162 +       bool "fsnotify"
19163 +       select FSNOTIFY
19164 +endchoice
19165 +
19166 +config AUFS_EXPORT
19167 +       bool "NFS-exportable aufs"
19168 +       depends on EXPORTFS
19169 +       help
19170 +       If you want to export your mounted aufs via NFS, then enable this
19171 +       option. There are several requirements for this configuration.
19172 +       See detail in aufs.5.
19173 +
19174 +config AUFS_INO_T_64
19175 +       bool
19176 +       depends on AUFS_EXPORT
19177 +       depends on 64BIT && !(ALPHA || S390)
19178 +       default y
19179 +       help
19180 +       Automatic configuration for internal use.
19181 +       /* typedef unsigned long/int __kernel_ino_t */
19182 +       /* alpha and s390x are int */
19183 +
19184 +config AUFS_RDU
19185 +       bool "Readdir in userspace"
19186 +       help
19187 +       Aufs has two methods to provide a merged view for a directory,
19188 +       by a user-space library and by kernel-space natively. The latter
19189 +       is always enabled but sometimes large and slow.
19190 +       If you enable this option, install the library in aufs2-util
19191 +       package, and set some environment variables for your readdir(3),
19192 +       then the work will be handled in user-space which generally
19193 +       shows better performance in most cases.
19194 +       See detail in aufs.5.
19195 +
19196 +config AUFS_PROC_MAP
19197 +       bool "support for /proc/maps and lsof(1)"
19198 +       depends on PROC_FS
19199 +       help
19200 +       When you issue mmap(2) in aufs, it is actually a direct mmap(2)
19201 +       call to the file on the branch fs since the file in aufs is
19202 +       purely virtual. And the file path printed in /proc/maps (and
19203 +       others) will be the path on the branch fs. In most cases, it
19204 +       does no harm. But some utilities like lsof(1) may confuse since
19205 +       the utility or user may expect the file path in aufs to be
19206 +       printed.
19207 +       To address this issue, aufs provides a patch which introduces a
19208 +       new member called vm_prfile into struct vm_are_struct. The patch
19209 +       is meaningless without enabling this configuration since nobody
19210 +       sets the new vm_prfile member.
19211 +       If you don't apply the patch, then enabling this configuration
19212 +       will cause a compile error.
19213 +       This approach is fragile since if someone else make some changes
19214 +       around vm_file, then vm_prfile may not work anymore. As a
19215 +       workaround such case, aufs provides this configuration. If you
19216 +       disable it, then lsof(1) may produce incorrect result but the
19217 +       problem will be gone even if the aufs patch is applied (I hope).
19218 +
19219 +config AUFS_SP_IATTR
19220 +       bool "Respect the attributes (mtime/ctime mainly) of special files"
19221 +       help
19222 +       When you write something to a special file, some attributes of it
19223 +       (mtime/ctime mainly) may be updated. Generally such updates are
19224 +       less important (actually some device drivers and NFS ignore
19225 +       it). But some applications (such like test program) requires
19226 +       such updates. If you need these updates, then enable this
19227 +       configuration which introduces some overhead.
19228 +       Currently this configuration handles FIFO only.
19229 +
19230 +config AUFS_SHWH
19231 +       bool "Show whiteouts"
19232 +       help
19233 +       If you want to make the whiteouts in aufs visible, then enable
19234 +       this option and specify 'shwh' mount option. Although it may
19235 +       sounds like philosophy or something, but in technically it
19236 +       simply shows the name of whiteout with keeping its behaviour.
19237 +
19238 +config AUFS_BR_RAMFS
19239 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
19240 +       help
19241 +       If you want to use ramfs as an aufs branch fs, then enable this
19242 +       option. Generally tmpfs is recommended.
19243 +       Aufs prohibited them to be a branch fs by default, because
19244 +       initramfs becomes unusable after switch_root or something
19245 +       generally. If you sets initramfs as an aufs branch and boot your
19246 +       system by switch_root, you will meet a problem easily since the
19247 +       files in initramfs may be inaccessible.
19248 +       Unless you are going to use ramfs as an aufs branch fs without
19249 +       switch_root or something, leave it N.
19250 +
19251 +config AUFS_BR_FUSE
19252 +       bool "Fuse fs as an aufs branch"
19253 +       depends on FUSE_FS
19254 +       select AUFS_POLL
19255 +       help
19256 +       If you want to use fuse-based userspace filesystem as an aufs
19257 +       branch fs, then enable this option.
19258 +       It implements the internal poll(2) operation which is
19259 +       implemented by fuse only (curretnly).
19260 +
19261 +config AUFS_POLL
19262 +       bool
19263 +       help
19264 +       Automatic configuration for internal use.
19265 +
19266 +config AUFS_BR_HFSPLUS
19267 +       bool "Hfsplus as an aufs branch"
19268 +       depends on HFSPLUS_FS
19269 +       default y
19270 +       help
19271 +       If you want to use hfsplus fs as an aufs branch fs, then enable
19272 +       this option. This option introduces a small overhead at
19273 +       copying-up a file on hfsplus.
19274 +
19275 +config AUFS_BDEV_LOOP
19276 +       bool
19277 +       depends on BLK_DEV_LOOP
19278 +       default y
19279 +       help
19280 +       Automatic configuration for internal use.
19281 +       Convert =[ym] into =y.
19282 +
19283 +config AUFS_DEBUG
19284 +       bool "Debug aufs"
19285 +       help
19286 +       Enable this to compile aufs internal debug code.
19287 +       It will have a negative impact to the performance.
19288 +
19289 +config AUFS_MAGIC_SYSRQ
19290 +       bool
19291 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
19292 +       default y
19293 +       help
19294 +       Automatic configuration for internal use.
19295 +       When aufs supports Magic SysRq, enabled automatically.
19296 +endif
19297 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
19298 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
19299 +++ linux/fs/aufs/loop.c        2013-07-30 22:42:55.842946719 +0200
19300 @@ -0,0 +1,135 @@
19301 +/*
19302 + * Copyright (C) 2005-2013 Junjiro R. Okajima
19303 + *
19304 + * This program, aufs is free software; you can redistribute it and/or modify
19305 + * it under the terms of the GNU General Public License as published by
19306 + * the Free Software Foundation; either version 2 of the License, or
19307 + * (at your option) any later version.
19308 + *
19309 + * This program is distributed in the hope that it will be useful,
19310 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19311 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19312 + * GNU General Public License for more details.
19313 + *
19314 + * You should have received a copy of the GNU General Public License
19315 + * along with this program; if not, write to the Free Software
19316 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19317 + */
19318 +
19319 +/*
19320 + * support for loopback block device as a branch
19321 + */
19322 +
19323 +#include <linux/loop.h>
19324 +#include "aufs.h"
19325 +
19326 +/*
19327 + * test if two lower dentries have overlapping branches.
19328 + */
19329 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
19330 +{
19331 +       struct super_block *h_sb;
19332 +       struct loop_device *l;
19333 +
19334 +       h_sb = h_adding->d_sb;
19335 +       if (MAJOR(h_sb->s_dev) != LOOP_MAJOR)
19336 +               return 0;
19337 +
19338 +       l = h_sb->s_bdev->bd_disk->private_data;
19339 +       h_adding = l->lo_backing_file->f_dentry;
19340 +       /*
19341 +        * h_adding can be local NFS.
19342 +        * in this case aufs cannot detect the loop.
19343 +        */
19344 +       if (unlikely(h_adding->d_sb == sb))
19345 +               return 1;
19346 +       return !!au_test_subdir(h_adding, sb->s_root);
19347 +}
19348 +
19349 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
19350 +int au_test_loopback_kthread(void)
19351 +{
19352 +       int ret;
19353 +       struct task_struct *tsk = current;
19354 +       char c, comm[sizeof(tsk->comm)];
19355 +
19356 +       ret = 0;
19357 +       if (tsk->flags & PF_KTHREAD) {
19358 +               get_task_comm(comm, tsk);
19359 +               c = comm[4];
19360 +               ret = ('0' <= c && c <= '9'
19361 +                      && !strncmp(comm, "loop", 4));
19362 +       }
19363 +
19364 +       return ret;
19365 +}
19366 +
19367 +/* ---------------------------------------------------------------------- */
19368 +
19369 +#define au_warn_loopback_step  16
19370 +static int au_warn_loopback_nelem = au_warn_loopback_step;
19371 +static unsigned long *au_warn_loopback_array;
19372 +
19373 +void au_warn_loopback(struct super_block *h_sb)
19374 +{
19375 +       int i, new_nelem;
19376 +       unsigned long *a, magic;
19377 +       static DEFINE_SPINLOCK(spin);
19378 +
19379 +       magic = h_sb->s_magic;
19380 +       spin_lock(&spin);
19381 +       a = au_warn_loopback_array;
19382 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
19383 +               if (a[i] == magic) {
19384 +                       spin_unlock(&spin);
19385 +                       return;
19386 +               }
19387 +
19388 +       /* h_sb is new to us, print it */
19389 +       if (i < au_warn_loopback_nelem) {
19390 +               a[i] = magic;
19391 +               goto pr;
19392 +       }
19393 +
19394 +       /* expand the array */
19395 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
19396 +       a = au_kzrealloc(au_warn_loopback_array,
19397 +                        au_warn_loopback_nelem * sizeof(unsigned long),
19398 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
19399 +       if (a) {
19400 +               au_warn_loopback_nelem = new_nelem;
19401 +               au_warn_loopback_array = a;
19402 +               a[i] = magic;
19403 +               goto pr;
19404 +       }
19405 +
19406 +       spin_unlock(&spin);
19407 +       AuWarn1("realloc failed, ignored\n");
19408 +       return;
19409 +
19410 +pr:
19411 +       spin_unlock(&spin);
19412 +       pr_warn("you may want to try another patch for loopback file "
19413 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
19414 +}
19415 +
19416 +int au_loopback_init(void)
19417 +{
19418 +       int err;
19419 +       struct super_block *sb __maybe_unused;
19420 +
19421 +       AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
19422 +
19423 +       err = 0;
19424 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
19425 +                                        sizeof(unsigned long), GFP_NOFS);
19426 +       if (unlikely(!au_warn_loopback_array))
19427 +               err = -ENOMEM;
19428 +
19429 +       return err;
19430 +}
19431 +
19432 +void au_loopback_fin(void)
19433 +{
19434 +       kfree(au_warn_loopback_array);
19435 +}
19436 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
19437 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
19438 +++ linux/fs/aufs/loop.h        2013-07-30 22:42:55.842946719 +0200
19439 @@ -0,0 +1,50 @@
19440 +/*
19441 + * Copyright (C) 2005-2013 Junjiro R. Okajima
19442 + *
19443 + * This program, aufs is free software; you can redistribute it and/or modify
19444 + * it under the terms of the GNU General Public License as published by
19445 + * the Free Software Foundation; either version 2 of the License, or
19446 + * (at your option) any later version.
19447 + *
19448 + * This program is distributed in the hope that it will be useful,
19449 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19450 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19451 + * GNU General Public License for more details.
19452 + *
19453 + * You should have received a copy of the GNU General Public License
19454 + * along with this program; if not, write to the Free Software
19455 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19456 + */
19457 +
19458 +/*
19459 + * support for loopback mount as a branch
19460 + */
19461 +
19462 +#ifndef __AUFS_LOOP_H__
19463 +#define __AUFS_LOOP_H__
19464 +
19465 +#ifdef __KERNEL__
19466 +
19467 +struct dentry;
19468 +struct super_block;
19469 +
19470 +#ifdef CONFIG_AUFS_BDEV_LOOP
19471 +/* loop.c */
19472 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
19473 +int au_test_loopback_kthread(void);
19474 +void au_warn_loopback(struct super_block *h_sb);
19475 +
19476 +int au_loopback_init(void);
19477 +void au_loopback_fin(void);
19478 +#else
19479 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
19480 +          struct dentry *h_adding)
19481 +AuStubInt0(au_test_loopback_kthread, void)
19482 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
19483 +
19484 +AuStubInt0(au_loopback_init, void)
19485 +AuStubVoid(au_loopback_fin, void)
19486 +#endif /* BLK_DEV_LOOP */
19487 +
19488 +#endif /* __KERNEL__ */
19489 +#endif /* __AUFS_LOOP_H__ */
19490 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
19491 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
19492 +++ linux/fs/aufs/magic.mk      2013-07-06 13:20:47.750198454 +0200
19493 @@ -0,0 +1,54 @@
19494 +
19495 +# defined in ${srctree}/fs/fuse/inode.c
19496 +# tristate
19497 +ifdef CONFIG_FUSE_FS
19498 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
19499 +endif
19500 +
19501 +# defined in ${srctree}/fs/ocfs2/ocfs2_fs.h
19502 +# tristate
19503 +ifdef CONFIG_OCFS2_FS
19504 +ccflags-y += -DOCFS2_SUPER_MAGIC=0x7461636f
19505 +endif
19506 +
19507 +# defined in ${srctree}/fs/ocfs2/dlm/userdlm.h
19508 +# tristate
19509 +ifdef CONFIG_OCFS2_FS_O2CB
19510 +ccflags-y += -DDLMFS_MAGIC=0x76a9f425
19511 +endif
19512 +
19513 +# defined in ${srctree}/fs/cifs/cifsfs.c
19514 +# tristate
19515 +ifdef CONFIG_CIFS_FS
19516 +ccflags-y += -DCIFS_MAGIC_NUMBER=0xFF534D42
19517 +endif
19518 +
19519 +# defined in ${srctree}/fs/xfs/xfs_sb.h
19520 +# tristate
19521 +ifdef CONFIG_XFS_FS
19522 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
19523 +endif
19524 +
19525 +# defined in ${srctree}/fs/configfs/mount.c
19526 +# tristate
19527 +ifdef CONFIG_CONFIGFS_FS
19528 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
19529 +endif
19530 +
19531 +# defined in ${srctree}/fs/9p/v9fs.h
19532 +# tristate
19533 +ifdef CONFIG_9P_FS
19534 +ccflags-y += -DV9FS_MAGIC=0x01021997
19535 +endif
19536 +
19537 +# defined in ${srctree}/fs/ubifs/ubifs.h
19538 +# tristate
19539 +ifdef CONFIG_UBIFS_FS
19540 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
19541 +endif
19542 +
19543 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
19544 +# tristate
19545 +ifdef CONFIG_HFSPLUS_FS
19546 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
19547 +endif
19548 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
19549 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
19550 +++ linux/fs/aufs/Makefile      2013-08-23 23:59:39.631583456 +0200
19551 @@ -0,0 +1,42 @@
19552 +
19553 +include ${src}/magic.mk
19554 +ifeq (${CONFIG_AUFS_FS},m)
19555 +include ${src}/conf.mk
19556 +endif
19557 +-include ${src}/priv_def.mk
19558 +
19559 +# cf. include/linux/kernel.h
19560 +# enable pr_debug
19561 +ccflags-y += -DDEBUG
19562 +# sparse requires the full pathname
19563 +ifdef M
19564 +ccflags-y += -include ${M}/../../include/linux/aufs_type.h
19565 +else
19566 +ccflags-y += -include ${srctree}/include/linux/aufs_type.h
19567 +endif
19568 +
19569 +obj-$(CONFIG_AUFS_FS) += aufs.o
19570 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
19571 +       wkq.o vfsub.o dcsub.o \
19572 +       cpup.o whout.o wbr_policy.o \
19573 +       dinfo.o dentry.o \
19574 +       dynop.o \
19575 +       finfo.o file.o f_op.o \
19576 +       dir.o vdir.o \
19577 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
19578 +       mvdown.o ioctl.o
19579 +
19580 +# all are boolean
19581 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
19582 +aufs-$(CONFIG_SYSFS) += sysfs.o
19583 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
19584 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
19585 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
19586 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
19587 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
19588 +aufs-$(CONFIG_AUFS_POLL) += poll.o
19589 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
19590 +aufs-$(CONFIG_AUFS_SP_IATTR) += f_op_sp.o
19591 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
19592 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
19593 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
19594 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
19595 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
19596 +++ linux/fs/aufs/module.c      2013-07-06 13:20:47.750198454 +0200
19597 @@ -0,0 +1,203 @@
19598 +/*
19599 + * Copyright (C) 2005-2013 Junjiro R. Okajima
19600 + *
19601 + * This program, aufs is free software; you can redistribute it and/or modify
19602 + * it under the terms of the GNU General Public License as published by
19603 + * the Free Software Foundation; either version 2 of the License, or
19604 + * (at your option) any later version.
19605 + *
19606 + * This program is distributed in the hope that it will be useful,
19607 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19608 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19609 + * GNU General Public License for more details.
19610 + *
19611 + * You should have received a copy of the GNU General Public License
19612 + * along with this program; if not, write to the Free Software
19613 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19614 + */
19615 +
19616 +/*
19617 + * module global variables and operations
19618 + */
19619 +
19620 +#include <linux/module.h>
19621 +#include <linux/seq_file.h>
19622 +#include "aufs.h"
19623 +
19624 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
19625 +{
19626 +       if (new_sz <= nused)
19627 +               return p;
19628 +
19629 +       p = krealloc(p, new_sz, gfp);
19630 +       if (p)
19631 +               memset(p + nused, 0, new_sz - nused);
19632 +       return p;
19633 +}
19634 +
19635 +/* ---------------------------------------------------------------------- */
19636 +
19637 +/*
19638 + * aufs caches
19639 + */
19640 +struct kmem_cache *au_cachep[AuCache_Last];
19641 +static int __init au_cache_init(void)
19642 +{
19643 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
19644 +       if (au_cachep[AuCache_DINFO])
19645 +               /* SLAB_DESTROY_BY_RCU */
19646 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
19647 +                                                       au_icntnr_init_once);
19648 +       if (au_cachep[AuCache_ICNTNR])
19649 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
19650 +                                                      au_fi_init_once);
19651 +       if (au_cachep[AuCache_FINFO])
19652 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
19653 +       if (au_cachep[AuCache_VDIR])
19654 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
19655 +       if (au_cachep[AuCache_DEHSTR])
19656 +               return 0;
19657 +
19658 +       return -ENOMEM;
19659 +}
19660 +
19661 +static void au_cache_fin(void)
19662 +{
19663 +       int i;
19664 +
19665 +       /*
19666 +        * Make sure all delayed rcu free inodes are flushed before we
19667 +        * destroy cache.
19668 +        */
19669 +       rcu_barrier();
19670 +
19671 +       /* excluding AuCache_HNOTIFY */
19672 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
19673 +       for (i = 0; i < AuCache_HNOTIFY; i++)
19674 +               if (au_cachep[i]) {
19675 +                       kmem_cache_destroy(au_cachep[i]);
19676 +                       au_cachep[i] = NULL;
19677 +               }
19678 +}
19679 +
19680 +/* ---------------------------------------------------------------------- */
19681 +
19682 +int au_dir_roflags;
19683 +
19684 +#ifdef CONFIG_AUFS_SBILIST
19685 +/*
19686 + * iterate_supers_type() doesn't protect us from
19687 + * remounting (branch management)
19688 + */
19689 +struct au_splhead au_sbilist;
19690 +#endif
19691 +
19692 +struct lock_class_key au_lc_key[AuLcKey_Last];
19693 +
19694 +/*
19695 + * functions for module interface.
19696 + */
19697 +MODULE_LICENSE("GPL");
19698 +/* MODULE_LICENSE("GPL v2"); */
19699 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
19700 +MODULE_DESCRIPTION(AUFS_NAME
19701 +       " -- Advanced multi layered unification filesystem");
19702 +MODULE_VERSION(AUFS_VERSION);
19703 +MODULE_ALIAS_FS(AUFS_NAME);
19704 +
19705 +/* this module parameter has no meaning when SYSFS is disabled */
19706 +int sysaufs_brs = 1;
19707 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
19708 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
19709 +
19710 +/* ---------------------------------------------------------------------- */
19711 +
19712 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
19713 +
19714 +int au_seq_path(struct seq_file *seq, struct path *path)
19715 +{
19716 +       return seq_path(seq, path, au_esc_chars);
19717 +}
19718 +
19719 +/* ---------------------------------------------------------------------- */
19720 +
19721 +static int __init aufs_init(void)
19722 +{
19723 +       int err, i;
19724 +       char *p;
19725 +
19726 +       p = au_esc_chars;
19727 +       for (i = 1; i <= ' '; i++)
19728 +               *p++ = i;
19729 +       *p++ = '\\';
19730 +       *p++ = '\x7f';
19731 +       *p = 0;
19732 +
19733 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
19734 +
19735 +       au_sbilist_init();
19736 +       sysaufs_brs_init();
19737 +       au_debug_init();
19738 +       au_dy_init();
19739 +       err = sysaufs_init();
19740 +       if (unlikely(err))
19741 +               goto out;
19742 +       err = au_procfs_init();
19743 +       if (unlikely(err))
19744 +               goto out_sysaufs;
19745 +       err = au_wkq_init();
19746 +       if (unlikely(err))
19747 +               goto out_procfs;
19748 +       err = au_loopback_init();
19749 +       if (unlikely(err))
19750 +               goto out_wkq;
19751 +       err = au_hnotify_init();
19752 +       if (unlikely(err))
19753 +               goto out_loopback;
19754 +       err = au_sysrq_init();
19755 +       if (unlikely(err))
19756 +               goto out_hin;
19757 +       err = au_cache_init();
19758 +       if (unlikely(err))
19759 +               goto out_sysrq;
19760 +       err = register_filesystem(&aufs_fs_type);
19761 +       if (unlikely(err))
19762 +               goto out_cache;
19763 +       /* since we define pr_fmt, call printk directly */
19764 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
19765 +       goto out; /* success */
19766 +
19767 +out_cache:
19768 +       au_cache_fin();
19769 +out_sysrq:
19770 +       au_sysrq_fin();
19771 +out_hin:
19772 +       au_hnotify_fin();
19773 +out_loopback:
19774 +       au_loopback_fin();
19775 +out_wkq:
19776 +       au_wkq_fin();
19777 +out_procfs:
19778 +       au_procfs_fin();
19779 +out_sysaufs:
19780 +       sysaufs_fin();
19781 +       au_dy_fin();
19782 +out:
19783 +       return err;
19784 +}
19785 +
19786 +static void __exit aufs_exit(void)
19787 +{
19788 +       unregister_filesystem(&aufs_fs_type);
19789 +       au_cache_fin();
19790 +       au_sysrq_fin();
19791 +       au_hnotify_fin();
19792 +       au_loopback_fin();
19793 +       au_wkq_fin();
19794 +       au_procfs_fin();
19795 +       sysaufs_fin();
19796 +       au_dy_fin();
19797 +}
19798 +
19799 +module_init(aufs_init);
19800 +module_exit(aufs_exit);
19801 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
19802 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
19803 +++ linux/fs/aufs/module.h      2013-07-06 13:20:47.750198454 +0200
19804 @@ -0,0 +1,105 @@
19805 +/*
19806 + * Copyright (C) 2005-2013 Junjiro R. Okajima
19807 + *
19808 + * This program, aufs is free software; you can redistribute it and/or modify
19809 + * it under the terms of the GNU General Public License as published by
19810 + * the Free Software Foundation; either version 2 of the License, or
19811 + * (at your option) any later version.
19812 + *
19813 + * This program is distributed in the hope that it will be useful,
19814 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19815 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19816 + * GNU General Public License for more details.
19817 + *
19818 + * You should have received a copy of the GNU General Public License
19819 + * along with this program; if not, write to the Free Software
19820 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19821 + */
19822 +
19823 +/*
19824 + * module initialization and module-global
19825 + */
19826 +
19827 +#ifndef __AUFS_MODULE_H__
19828 +#define __AUFS_MODULE_H__
19829 +
19830 +#ifdef __KERNEL__
19831 +
19832 +#include <linux/slab.h>
19833 +
19834 +struct path;
19835 +struct seq_file;
19836 +
19837 +/* module parameters */
19838 +extern int sysaufs_brs;
19839 +
19840 +/* ---------------------------------------------------------------------- */
19841 +
19842 +extern int au_dir_roflags;
19843 +
19844 +enum {
19845 +       AuLcNonDir_FIINFO,
19846 +       AuLcNonDir_DIINFO,
19847 +       AuLcNonDir_IIINFO,
19848 +
19849 +       AuLcDir_FIINFO,
19850 +       AuLcDir_DIINFO,
19851 +       AuLcDir_IIINFO,
19852 +
19853 +       AuLcSymlink_DIINFO,
19854 +       AuLcSymlink_IIINFO,
19855 +
19856 +       AuLcKey_Last
19857 +};
19858 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
19859 +
19860 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
19861 +int au_seq_path(struct seq_file *seq, struct path *path);
19862 +
19863 +#ifdef CONFIG_PROC_FS
19864 +/* procfs.c */
19865 +int __init au_procfs_init(void);
19866 +void au_procfs_fin(void);
19867 +#else
19868 +AuStubInt0(au_procfs_init, void);
19869 +AuStubVoid(au_procfs_fin, void);
19870 +#endif
19871 +
19872 +/* ---------------------------------------------------------------------- */
19873 +
19874 +/* kmem cache */
19875 +enum {
19876 +       AuCache_DINFO,
19877 +       AuCache_ICNTNR,
19878 +       AuCache_FINFO,
19879 +       AuCache_VDIR,
19880 +       AuCache_DEHSTR,
19881 +       AuCache_HNOTIFY, /* must be last */
19882 +       AuCache_Last
19883 +};
19884 +
19885 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
19886 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
19887 +#define AuCacheCtor(type, ctor)        \
19888 +       kmem_cache_create(#type, sizeof(struct type), \
19889 +                         __alignof__(struct type), AuCacheFlags, ctor)
19890 +
19891 +extern struct kmem_cache *au_cachep[];
19892 +
19893 +#define AuCacheFuncs(name, index) \
19894 +static inline struct au_##name *au_cache_alloc_##name(void) \
19895 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
19896 +static inline void au_cache_free_##name(struct au_##name *p) \
19897 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
19898 +
19899 +AuCacheFuncs(dinfo, DINFO);
19900 +AuCacheFuncs(icntnr, ICNTNR);
19901 +AuCacheFuncs(finfo, FINFO);
19902 +AuCacheFuncs(vdir, VDIR);
19903 +AuCacheFuncs(vdir_dehstr, DEHSTR);
19904 +#ifdef CONFIG_AUFS_HNOTIFY
19905 +AuCacheFuncs(hnotify, HNOTIFY);
19906 +#endif
19907 +
19908 +#endif /* __KERNEL__ */
19909 +#endif /* __AUFS_MODULE_H__ */
19910 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
19911 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
19912 +++ linux/fs/aufs/mvdown.c      2013-08-23 23:59:39.634916914 +0200
19913 @@ -0,0 +1,533 @@
19914 +/*
19915 + * Copyright (C) 2011-2013 Junjiro R. Okajima
19916 + *
19917 + * This program, aufs is free software; you can redistribute it and/or modify
19918 + * it under the terms of the GNU General Public License as published by
19919 + * the Free Software Foundation; either version 2 of the License, or
19920 + * (at your option) any later version.
19921 + *
19922 + * This program is distributed in the hope that it will be useful,
19923 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19924 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19925 + * GNU General Public License for more details.
19926 + *
19927 + * You should have received a copy of the GNU General Public License
19928 + * along with this program; if not, write to the Free Software
19929 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19930 + */
19931 +
19932 +#include "aufs.h"
19933 +
19934 +enum {
19935 +       AUFS_MVDOWN_SRC,
19936 +       AUFS_MVDOWN_DST,
19937 +       AUFS_MVDOWN_NARRAY
19938 +};
19939 +
19940 +struct au_mvd_args {
19941 +       struct {
19942 +               aufs_bindex_t bindex;
19943 +               struct super_block *h_sb;
19944 +               struct dentry *h_parent;
19945 +               struct au_hinode *hdir;
19946 +               struct inode *h_dir;
19947 +       } info[AUFS_MVDOWN_NARRAY];
19948 +
19949 +       struct aufs_mvdown mvdown;
19950 +       struct dentry *dentry, *parent;
19951 +       struct inode *inode, *dir;
19952 +       struct super_block *sb;
19953 +       aufs_bindex_t bopq, bwh, bfound;
19954 +       unsigned char rename_lock;
19955 +       struct au_pin pin;
19956 +};
19957 +
19958 +#define mvd_bsrc               info[AUFS_MVDOWN_SRC].bindex
19959 +#define mvd_h_src_sb           info[AUFS_MVDOWN_SRC].h_sb
19960 +#define mvd_h_src_parent       info[AUFS_MVDOWN_SRC].h_parent
19961 +#define mvd_hdir_src           info[AUFS_MVDOWN_SRC].hdir
19962 +#define mvd_h_src_dir          info[AUFS_MVDOWN_SRC].h_dir
19963 +
19964 +#define mvd_bdst               info[AUFS_MVDOWN_DST].bindex
19965 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_DST].h_sb
19966 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_DST].h_parent
19967 +#define mvd_hdir_dst           info[AUFS_MVDOWN_DST].hdir
19968 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_DST].h_dir
19969 +
19970 +#define AU_MVD_PR(flag, ...) do {                      \
19971 +               if (flag)                               \
19972 +                       pr_err(__VA_ARGS__);            \
19973 +       } while (0)
19974 +
19975 +/* make the parent dir on bdst */
19976 +static int au_do_mkdir(const unsigned char verbose, struct au_mvd_args *a)
19977 +{
19978 +       int err;
19979 +
19980 +       err = 0;
19981 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
19982 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
19983 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
19984 +       a->mvd_h_dst_parent = NULL;
19985 +       if (au_dbend(a->parent) >= a->mvd_bdst)
19986 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
19987 +       if (!a->mvd_h_dst_parent) {
19988 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
19989 +               if (unlikely(err)) {
19990 +                       AU_MVD_PR(verbose, "cpdown_dirs failed\n");
19991 +                       goto out;
19992 +               }
19993 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
19994 +       }
19995 +
19996 +out:
19997 +       AuTraceErr(err);
19998 +       return err;
19999 +}
20000 +
20001 +/* lock them all */
20002 +static int au_do_lock(const unsigned char verbose, struct au_mvd_args *a)
20003 +{
20004 +       int err;
20005 +       struct dentry *h_trap;
20006 +
20007 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
20008 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
20009 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
20010 +               a->rename_lock = 0;
20011 +               err = au_pin(&a->pin, a->dentry, a->mvd_bdst, au_opt_udba(a->sb),
20012 +                            AuPin_MNT_WRITE | AuPin_DI_LOCKED);
20013 +               if (!err) {
20014 +                       a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
20015 +                       mutex_lock_nested(&a->mvd_h_src_dir->i_mutex,
20016 +                                         AuLsc_I_PARENT3);
20017 +               } else
20018 +                       AU_MVD_PR(verbose, "pin failed\n");
20019 +               goto out;
20020 +       }
20021 +
20022 +       err = 0;
20023 +       a->rename_lock = 1;
20024 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
20025 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
20026 +       if (h_trap) {
20027 +               err = (h_trap != a->mvd_h_src_parent);
20028 +               if (err)
20029 +                       err = (h_trap != a->mvd_h_dst_parent);
20030 +       }
20031 +       BUG_ON(err); /* it should never happen */
20032 +
20033 +out:
20034 +       AuTraceErr(err);
20035 +       return err;
20036 +}
20037 +
20038 +static void au_do_unlock(const unsigned char verbose, struct au_mvd_args *a)
20039 +{
20040 +       if (!a->rename_lock) {
20041 +               mutex_unlock(&a->mvd_h_src_dir->i_mutex);
20042 +               au_unpin(&a->pin);
20043 +       } else
20044 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
20045 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
20046 +}
20047 +
20048 +/* copy-down the file */
20049 +static int au_do_cpdown(const unsigned char verbose, struct au_mvd_args *a)
20050 +{
20051 +       int err;
20052 +       struct au_cp_generic cpg = {
20053 +               .dentry = a->dentry,
20054 +               .bdst   = a->mvd_bdst,
20055 +               .bsrc   = a->mvd_bsrc,
20056 +               .len    = -1,
20057 +               .pin    = &a->pin,
20058 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
20059 +       };
20060 +
20061 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
20062 +       err = au_sio_cpdown_simple(&cpg);
20063 +       if (unlikely(err))
20064 +               AU_MVD_PR(verbose, "cpdown failed\n");
20065 +
20066 +       AuTraceErr(err);
20067 +       return err;
20068 +}
20069 +
20070 +/*
20071 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
20072 + * were sleeping
20073 + */
20074 +static int au_do_unlink_wh(const unsigned char verbose, struct au_mvd_args *a)
20075 +{
20076 +       int err;
20077 +       struct path h_path;
20078 +       struct au_branch *br;
20079 +
20080 +       br = au_sbr(a->sb, a->mvd_bdst);
20081 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
20082 +       err = PTR_ERR(h_path.dentry);
20083 +       if (IS_ERR(h_path.dentry)) {
20084 +               AU_MVD_PR(verbose, "wh_lkup failed\n");
20085 +               goto out;
20086 +       }
20087 +
20088 +       err = 0;
20089 +       if (h_path.dentry->d_inode) {
20090 +               h_path.mnt = au_br_mnt(br);
20091 +               err = vfsub_unlink(a->mvd_h_dst_parent->d_inode, &h_path,
20092 +                                  /*force*/0);
20093 +               if (unlikely(err))
20094 +                       AU_MVD_PR(verbose, "wh_unlink failed\n");
20095 +       }
20096 +       dput(h_path.dentry);
20097 +
20098 +out:
20099 +       AuTraceErr(err);
20100 +       return err;
20101 +}
20102 +
20103 +/*
20104 + * unlink the topmost h_dentry
20105 + * Note: the target file MAY be modified by UDBA between this mutex_unlock() and
20106 + *     mutex_lock() in vfs_unlink(). in this case, such changes may be lost.
20107 + */
20108 +static int au_do_unlink(const unsigned char verbose, struct au_mvd_args *a)
20109 +{
20110 +       int err;
20111 +       struct path h_path;
20112 +
20113 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
20114 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
20115 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, /*force*/0);
20116 +       if (unlikely(err))
20117 +               AU_MVD_PR(verbose, "unlink failed\n");
20118 +
20119 +       AuTraceErr(err);
20120 +       return err;
20121 +}
20122 +
20123 +/*
20124 + * copy-down the file and unlink the bsrc file.
20125 + * - unlink the bdst whout if exist
20126 + * - copy-down the file (with whtmp name and rename)
20127 + * - unlink the bsrc file
20128 + */
20129 +static int au_do_mvdown(const unsigned char verbose, struct au_mvd_args *a)
20130 +{
20131 +       int err;
20132 +
20133 +       err = au_do_mkdir(verbose, a);
20134 +       if (!err)
20135 +               err = au_do_lock(verbose, a);
20136 +       if (unlikely(err))
20137 +               goto out;
20138 +
20139 +       /*
20140 +        * do not revert the activities we made on bdst since they should be
20141 +        * harmless in aufs.
20142 +        */
20143 +
20144 +       err = au_do_cpdown(verbose, a);
20145 +       if (!err)
20146 +               err = au_do_unlink_wh(verbose, a);
20147 +       if (!err)
20148 +               err = au_do_unlink(verbose, a);
20149 +       if (unlikely(err))
20150 +               goto out_unlock;
20151 +
20152 +       /* maintain internal array */
20153 +       au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
20154 +       au_set_dbstart(a->dentry, a->mvd_bdst);
20155 +       if (au_dbend(a->dentry) < a->mvd_bdst)
20156 +               au_set_dbend(a->dentry, a->mvd_bdst);
20157 +       au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
20158 +       au_set_ibstart(a->inode, a->mvd_bdst);
20159 +       if (au_ibend(a->inode) < a->mvd_bdst)
20160 +               au_set_ibend(a->inode, a->mvd_bdst);
20161 +
20162 +out_unlock:
20163 +       au_do_unlock(verbose, a);
20164 +out:
20165 +       AuTraceErr(err);
20166 +       return err;
20167 +}
20168 +
20169 +/* ---------------------------------------------------------------------- */
20170 +
20171 +static int find_lower_writable(struct super_block *sb, aufs_bindex_t bindex)
20172 +{
20173 +       aufs_bindex_t bend;
20174 +       struct au_branch *br;
20175 +
20176 +       bend = au_sbend(sb);
20177 +       for (bindex++; bindex <= bend; bindex++) {
20178 +               br = au_sbr(sb, bindex);
20179 +               if (!au_br_rdonly(br))
20180 +                       return bindex;
20181 +       }
20182 +
20183 +       return -1;
20184 +}
20185 +
20186 +/* make sure the file is idle */
20187 +static int au_mvd_args_busy(const unsigned char verbose, struct au_mvd_args *a)
20188 +{
20189 +       int err, plinked;
20190 +       struct inode *h_src_inode;
20191 +
20192 +       err = 0;
20193 +       h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
20194 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
20195 +       if (au_dbstart(a->dentry) == a->mvd_bsrc
20196 +           && a->dentry->d_count == 1
20197 +           && atomic_read(&a->inode->i_count) == 1
20198 +           /* && h_src_inode->i_nlink == 1 */
20199 +           && (!plinked || !au_plink_test(a->inode))
20200 +           && a->inode->i_nlink == 1)
20201 +               goto out;
20202 +
20203 +       err = -EBUSY;
20204 +       AU_MVD_PR(verbose,
20205 +                 "b%d, d{b%d, c%u?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
20206 +                 a->mvd_bsrc, au_dbstart(a->dentry), a->dentry->d_count,
20207 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
20208 +                 h_src_inode->i_nlink,
20209 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
20210 +
20211 +out:
20212 +       AuTraceErr(err);
20213 +       return err;
20214 +}
20215 +
20216 +/* make sure the parent dir is fine */
20217 +static int au_mvd_args_parent(const unsigned char verbose,
20218 +                             struct au_mvd_args *a)
20219 +{
20220 +       int err;
20221 +       aufs_bindex_t bindex;
20222 +
20223 +       err = 0;
20224 +       if (unlikely(au_alive_dir(a->parent))) {
20225 +               err = -ENOENT;
20226 +               AU_MVD_PR(verbose, "parent dir is dead\n");
20227 +               goto out;
20228 +       }
20229 +
20230 +       a->bopq = au_dbdiropq(a->parent);
20231 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
20232 +       AuDbg("b%d\n", bindex);
20233 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
20234 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
20235 +               err = -EINVAL;
20236 +               AU_MVD_PR(verbose, "parent dir is opaque b%d, b%d\n",
20237 +                         a->bopq, a->mvd_bdst);
20238 +       }
20239 +
20240 +out:
20241 +       AuTraceErr(err);
20242 +       return err;
20243 +}
20244 +
20245 +static int au_mvd_args_intermediate(const unsigned char verbose,
20246 +                                   struct au_mvd_args *a)
20247 +{
20248 +       int err;
20249 +       struct au_dinfo *dinfo, *tmp;
20250 +
20251 +       /* lookup the next lower positive entry */
20252 +       err = -ENOMEM;
20253 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
20254 +       if (unlikely(!tmp))
20255 +               goto out;
20256 +
20257 +       a->bfound = -1;
20258 +       a->bwh = -1;
20259 +       dinfo = au_di(a->dentry);
20260 +       au_di_cp(tmp, dinfo);
20261 +       au_di_swap(tmp, dinfo);
20262 +
20263 +       /* returns the number of positive dentries */
20264 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
20265 +       if (!err)
20266 +               a->bwh = au_dbwh(a->dentry);
20267 +       else if (err > 0)
20268 +               a->bfound = au_dbstart(a->dentry);
20269 +
20270 +       au_di_swap(tmp, dinfo);
20271 +       au_rw_write_unlock(&tmp->di_rwsem);
20272 +       au_di_free(tmp);
20273 +       if (unlikely(err < 0))
20274 +               AU_MVD_PR(verbose, "failed look-up lower\n");
20275 +
20276 +       /*
20277 +        * here, we have these cases.
20278 +        * bfound == -1
20279 +        *      no positive dentry under bsrc. there are more sub-cases.
20280 +        *      bwh < 0
20281 +        *              there no whiteout, we can safely move-down.
20282 +        *      bwh <= bsrc
20283 +        *              impossible
20284 +        *      bsrc < bwh && bwh < bdst
20285 +        *              there is a whiteout on RO branch. cannot proceed.
20286 +        *      bwh == bdst
20287 +        *              there is a whiteout on the RW target branch. it should
20288 +        *              be removed.
20289 +        *      bdst < bwh
20290 +        *              there is a whiteout somewhere unrelated branch.
20291 +        * -1 < bfound && bfound <= bsrc
20292 +        *      impossible.
20293 +        * bfound < bdst
20294 +        *      found, but it is on RO branch between bsrc and bdst. cannot
20295 +        *      proceed.
20296 +        * bfound == bdst
20297 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
20298 +        *      error.
20299 +        * bdst < bfound
20300 +        *      found, after we create the file on bdst, it will be hidden.
20301 +        */
20302 +
20303 +       AuDebugOn(a->bfound == -1
20304 +                 && a->bwh != -1
20305 +                 && a->bwh <= a->mvd_bsrc);
20306 +       AuDebugOn(-1 < a->bfound
20307 +                 && a->bfound <= a->mvd_bsrc);
20308 +
20309 +       err = -EINVAL;
20310 +       if (a->bfound == -1
20311 +           && a->mvd_bsrc < a->bwh
20312 +           && a->bwh != -1
20313 +           && a->bwh < a->mvd_bdst) {
20314 +               AU_MVD_PR(verbose, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
20315 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
20316 +               goto out;
20317 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
20318 +               AU_MVD_PR(verbose, "bdst %d, bfound %d\n",
20319 +                         a->mvd_bdst, a->bfound);
20320 +               goto out;
20321 +       }
20322 +
20323 +       err = 0; /* success */
20324 +
20325 +out:
20326 +       AuTraceErr(err);
20327 +       return err;
20328 +}
20329 +
20330 +static int au_mvd_args_exist(const unsigned char verbose, struct au_mvd_args *a)
20331 +{
20332 +       int err;
20333 +
20334 +       err = (a->bfound != a->mvd_bdst) ? 0 : -EEXIST;
20335 +       AuTraceErr(err);
20336 +       return err;
20337 +}
20338 +
20339 +static int au_mvd_args(const unsigned char verbose, struct au_mvd_args *a)
20340 +{
20341 +       int err;
20342 +       struct au_branch *br;
20343 +
20344 +       err = -EISDIR;
20345 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
20346 +               goto out;
20347 +
20348 +       err = -EINVAL;
20349 +       a->mvd_bsrc = au_ibstart(a->inode);
20350 +       if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
20351 +               AU_MVD_PR(verbose, "on the bottom\n");
20352 +               goto out;
20353 +       }
20354 +       br = au_sbr(a->sb, a->mvd_bsrc);
20355 +       err = au_br_rdonly(br);
20356 +       if (unlikely(err))
20357 +               goto out;
20358 +
20359 +       err = -EINVAL;
20360 +       a->mvd_bdst = find_lower_writable(a->sb, a->mvd_bsrc);
20361 +       if (unlikely(a->mvd_bdst < 0)) {
20362 +               AU_MVD_PR(verbose, "no writable lower branch\n");
20363 +               goto out;
20364 +       }
20365 +
20366 +       err = au_mvd_args_busy(verbose, a);
20367 +       if (!err)
20368 +               err = au_mvd_args_parent(verbose, a);
20369 +       if (!err)
20370 +               err = au_mvd_args_intermediate(verbose, a);
20371 +       if (!err)
20372 +               err = au_mvd_args_exist(verbose, a);
20373 +       if (!err)
20374 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
20375 +
20376 +out:
20377 +       AuTraceErr(err);
20378 +       return err;
20379 +}
20380 +
20381 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
20382 +{
20383 +       int err;
20384 +       unsigned char verbose;
20385 +       struct au_mvd_args args = {
20386 +               .dentry = dentry,
20387 +               .inode  = dentry->d_inode,
20388 +               .sb     = dentry->d_sb
20389 +       };
20390 +
20391 +       err = -EPERM;
20392 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
20393 +               goto out;
20394 +
20395 +       err = copy_from_user(&args.mvdown, uarg, sizeof(args.mvdown));
20396 +       if (unlikely(err)) {
20397 +               err = -EFAULT;
20398 +               goto out;
20399 +       }
20400 +       AuDbg("flags 0x%x\n", args.mvdown.flags);
20401 +
20402 +       err = -EBUSY;
20403 +       verbose = !!(args.mvdown.flags & AUFS_MVDOWN_VERBOSE);
20404 +       args.parent = dget_parent(dentry);
20405 +       args.dir = args.parent->d_inode;
20406 +       mutex_lock_nested(&args.dir->i_mutex, I_MUTEX_PARENT);
20407 +       dput(args.parent);
20408 +       if (unlikely(args.parent != dentry->d_parent)) {
20409 +               AU_MVD_PR(verbose, "parent dir is moved\n");
20410 +               goto out_dir;
20411 +       }
20412 +
20413 +       mutex_lock_nested(&args.inode->i_mutex, I_MUTEX_CHILD);
20414 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
20415 +       if (unlikely(err))
20416 +               goto out_inode;
20417 +
20418 +       di_write_lock_parent(args.parent);
20419 +       err = au_mvd_args(verbose, &args);
20420 +       if (unlikely(err))
20421 +               goto out_parent;
20422 +
20423 +       AuDbgDentry(dentry);
20424 +       AuDbgInode(args.inode);
20425 +       err = au_do_mvdown(verbose, &args);
20426 +       if (unlikely(err))
20427 +               goto out_parent;
20428 +       AuDbgDentry(dentry);
20429 +       AuDbgInode(args.inode);
20430 +
20431 +       au_cpup_attr_timesizes(args.dir);
20432 +       au_cpup_attr_timesizes(args.inode);
20433 +       au_cpup_igen(args.inode, au_h_iptr(args.inode, args.mvd_bdst));
20434 +       /* au_digen_dec(dentry); */
20435 +
20436 +out_parent:
20437 +       di_write_unlock(args.parent);
20438 +       aufs_read_unlock(dentry, AuLock_DW);
20439 +out_inode:
20440 +       mutex_unlock(&args.inode->i_mutex);
20441 +out_dir:
20442 +       mutex_unlock(&args.dir->i_mutex);
20443 +out:
20444 +       AuTraceErr(err);
20445 +       return err;
20446 +}
20447 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
20448 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
20449 +++ linux/fs/aufs/opts.c        2013-07-06 13:20:47.753531903 +0200
20450 @@ -0,0 +1,1697 @@
20451 +/*
20452 + * Copyright (C) 2005-2013 Junjiro R. Okajima
20453 + *
20454 + * This program, aufs is free software; you can redistribute it and/or modify
20455 + * it under the terms of the GNU General Public License as published by
20456 + * the Free Software Foundation; either version 2 of the License, or
20457 + * (at your option) any later version.
20458 + *
20459 + * This program is distributed in the hope that it will be useful,
20460 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20461 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20462 + * GNU General Public License for more details.
20463 + *
20464 + * You should have received a copy of the GNU General Public License
20465 + * along with this program; if not, write to the Free Software
20466 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20467 + */
20468 +
20469 +/*
20470 + * mount options/flags
20471 + */
20472 +
20473 +#include <linux/namei.h>
20474 +#include <linux/types.h> /* a distribution requires */
20475 +#include <linux/parser.h>
20476 +#include "aufs.h"
20477 +
20478 +/* ---------------------------------------------------------------------- */
20479 +
20480 +enum {
20481 +       Opt_br,
20482 +       Opt_add, Opt_del, Opt_mod, Opt_reorder, Opt_append, Opt_prepend,
20483 +       Opt_idel, Opt_imod, Opt_ireorder,
20484 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash, Opt_rendir,
20485 +       Opt_rdblk_def, Opt_rdhash_def,
20486 +       Opt_xino, Opt_zxino, Opt_noxino,
20487 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
20488 +       Opt_trunc_xino_path, Opt_itrunc_xino,
20489 +       Opt_trunc_xib, Opt_notrunc_xib,
20490 +       Opt_shwh, Opt_noshwh,
20491 +       Opt_plink, Opt_noplink, Opt_list_plink,
20492 +       Opt_udba,
20493 +       Opt_dio, Opt_nodio,
20494 +       /* Opt_lock, Opt_unlock, */
20495 +       Opt_cmd, Opt_cmd_args,
20496 +       Opt_diropq_a, Opt_diropq_w,
20497 +       Opt_warn_perm, Opt_nowarn_perm,
20498 +       Opt_wbr_copyup, Opt_wbr_create,
20499 +       Opt_refrof, Opt_norefrof,
20500 +       Opt_verbose, Opt_noverbose,
20501 +       Opt_sum, Opt_nosum, Opt_wsum,
20502 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
20503 +};
20504 +
20505 +static match_table_t options = {
20506 +       {Opt_br, "br=%s"},
20507 +       {Opt_br, "br:%s"},
20508 +
20509 +       {Opt_add, "add=%d:%s"},
20510 +       {Opt_add, "add:%d:%s"},
20511 +       {Opt_add, "ins=%d:%s"},
20512 +       {Opt_add, "ins:%d:%s"},
20513 +       {Opt_append, "append=%s"},
20514 +       {Opt_append, "append:%s"},
20515 +       {Opt_prepend, "prepend=%s"},
20516 +       {Opt_prepend, "prepend:%s"},
20517 +
20518 +       {Opt_del, "del=%s"},
20519 +       {Opt_del, "del:%s"},
20520 +       /* {Opt_idel, "idel:%d"}, */
20521 +       {Opt_mod, "mod=%s"},
20522 +       {Opt_mod, "mod:%s"},
20523 +       /* {Opt_imod, "imod:%d:%s"}, */
20524 +
20525 +       {Opt_dirwh, "dirwh=%d"},
20526 +
20527 +       {Opt_xino, "xino=%s"},
20528 +       {Opt_noxino, "noxino"},
20529 +       {Opt_trunc_xino, "trunc_xino"},
20530 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
20531 +       {Opt_notrunc_xino, "notrunc_xino"},
20532 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
20533 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
20534 +       /* {Opt_zxino, "zxino=%s"}, */
20535 +       {Opt_trunc_xib, "trunc_xib"},
20536 +       {Opt_notrunc_xib, "notrunc_xib"},
20537 +
20538 +#ifdef CONFIG_PROC_FS
20539 +       {Opt_plink, "plink"},
20540 +#else
20541 +       {Opt_ignore_silent, "plink"},
20542 +#endif
20543 +
20544 +       {Opt_noplink, "noplink"},
20545 +
20546 +#ifdef CONFIG_AUFS_DEBUG
20547 +       {Opt_list_plink, "list_plink"},
20548 +#endif
20549 +
20550 +       {Opt_udba, "udba=%s"},
20551 +
20552 +       {Opt_dio, "dio"},
20553 +       {Opt_nodio, "nodio"},
20554 +
20555 +       {Opt_diropq_a, "diropq=always"},
20556 +       {Opt_diropq_a, "diropq=a"},
20557 +       {Opt_diropq_w, "diropq=whiteouted"},
20558 +       {Opt_diropq_w, "diropq=w"},
20559 +
20560 +       {Opt_warn_perm, "warn_perm"},
20561 +       {Opt_nowarn_perm, "nowarn_perm"},
20562 +
20563 +       /* keep them temporary */
20564 +       {Opt_ignore_silent, "coo=%s"},
20565 +       {Opt_ignore_silent, "nodlgt"},
20566 +       {Opt_ignore_silent, "nodirperm1"},
20567 +       {Opt_ignore_silent, "clean_plink"},
20568 +
20569 +#ifdef CONFIG_AUFS_SHWH
20570 +       {Opt_shwh, "shwh"},
20571 +#endif
20572 +       {Opt_noshwh, "noshwh"},
20573 +
20574 +       {Opt_rendir, "rendir=%d"},
20575 +
20576 +       {Opt_refrof, "refrof"},
20577 +       {Opt_norefrof, "norefrof"},
20578 +
20579 +       {Opt_verbose, "verbose"},
20580 +       {Opt_verbose, "v"},
20581 +       {Opt_noverbose, "noverbose"},
20582 +       {Opt_noverbose, "quiet"},
20583 +       {Opt_noverbose, "q"},
20584 +       {Opt_noverbose, "silent"},
20585 +
20586 +       {Opt_sum, "sum"},
20587 +       {Opt_nosum, "nosum"},
20588 +       {Opt_wsum, "wsum"},
20589 +
20590 +       {Opt_rdcache, "rdcache=%d"},
20591 +       {Opt_rdblk, "rdblk=%d"},
20592 +       {Opt_rdblk_def, "rdblk=def"},
20593 +       {Opt_rdhash, "rdhash=%d"},
20594 +       {Opt_rdhash_def, "rdhash=def"},
20595 +
20596 +       {Opt_wbr_create, "create=%s"},
20597 +       {Opt_wbr_create, "create_policy=%s"},
20598 +       {Opt_wbr_copyup, "cpup=%s"},
20599 +       {Opt_wbr_copyup, "copyup=%s"},
20600 +       {Opt_wbr_copyup, "copyup_policy=%s"},
20601 +
20602 +       /* internal use for the scripts */
20603 +       {Opt_ignore_silent, "si=%s"},
20604 +
20605 +       {Opt_br, "dirs=%s"},
20606 +       {Opt_ignore, "debug=%d"},
20607 +       {Opt_ignore, "delete=whiteout"},
20608 +       {Opt_ignore, "delete=all"},
20609 +       {Opt_ignore, "imap=%s"},
20610 +
20611 +       /* temporary workaround, due to old mount(8)? */
20612 +       {Opt_ignore_silent, "relatime"},
20613 +
20614 +       {Opt_err, NULL}
20615 +};
20616 +
20617 +/* ---------------------------------------------------------------------- */
20618 +
20619 +static const char *au_parser_pattern(int val, struct match_token *token)
20620 +{
20621 +       while (token->pattern) {
20622 +               if (token->token == val)
20623 +                       return token->pattern;
20624 +               token++;
20625 +       }
20626 +       BUG();
20627 +       return "??";
20628 +}
20629 +
20630 +/* ---------------------------------------------------------------------- */
20631 +
20632 +static match_table_t brperm = {
20633 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
20634 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
20635 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
20636 +       {0, NULL}
20637 +};
20638 +
20639 +static match_table_t brattr = {
20640 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
20641 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
20642 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
20643 +       {0, NULL}
20644 +};
20645 +
20646 +#define AuBrStr_LONGEST        AUFS_BRPERM_RW \
20647 +       "+" AUFS_BRATTR_UNPIN \
20648 +       "+" AUFS_BRWATTR_NLWH
20649 +
20650 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
20651 +{
20652 +       int attr, v;
20653 +       char *p;
20654 +
20655 +       attr = 0;
20656 +       do {
20657 +               p = strchr(str, '+');
20658 +               if (p)
20659 +                       *p = 0;
20660 +               v = match_token(str, table, args);
20661 +               if (v)
20662 +                       attr |= v;
20663 +               else {
20664 +                       if (p)
20665 +                               *p = '+';
20666 +                       pr_warn("ignored branch attribute %s\n", str);
20667 +                       break;
20668 +               }
20669 +               if (p)
20670 +                       str = p + 1;
20671 +       } while (p);
20672 +
20673 +       return attr;
20674 +}
20675 +
20676 +static int noinline_for_stack br_perm_val(char *perm)
20677 +{
20678 +       int val;
20679 +       char *p, *q;
20680 +       substring_t args[MAX_OPT_ARGS];
20681 +
20682 +       p = strchr(perm, '+');
20683 +       if (p)
20684 +               *p = 0;
20685 +       val = match_token(perm, brperm, args);
20686 +       if (!val) {
20687 +               if (p)
20688 +                       *p = '+';
20689 +               pr_warn("ignored branch permission %s\n", perm);
20690 +               val = AuBrPerm_RO;
20691 +               goto out;
20692 +       }
20693 +       if (!p)
20694 +               goto out;
20695 +
20696 +       p++;
20697 +       while (1) {
20698 +               q = strchr(p, '+');
20699 +               if (q)
20700 +                       *q = 0;
20701 +               val |= br_attr_val(p, brattr, args);
20702 +               if (q) {
20703 +                       *q = '+';
20704 +                       p = q + 1;
20705 +               } else
20706 +                       break;
20707 +       }
20708 +       switch (val & AuBrPerm_Mask) {
20709 +       case AuBrPerm_RO:
20710 +       case AuBrPerm_RR:
20711 +               if (unlikely(val & AuBrWAttr_NoLinkWH)) {
20712 +                       pr_warn("ignored branch attribute %s\n",
20713 +                               AUFS_BRWATTR_NLWH);
20714 +                       val &= ~AuBrWAttr_NoLinkWH;
20715 +               }
20716 +               break;
20717 +       case AuBrPerm_RW:
20718 +               if (unlikely(val & AuBrRAttr_WH)) {
20719 +                       pr_warn("ignored branch attribute %s\n",
20720 +                               AUFS_BRRATTR_WH);
20721 +                       val &= ~AuBrRAttr_WH;
20722 +               }
20723 +               break;
20724 +       }
20725 +
20726 +out:
20727 +       return val;
20728 +}
20729 +
20730 +/* Caller should free the return value */
20731 +char *au_optstr_br_perm(int brperm)
20732 +{
20733 +       char *p, a[sizeof(AuBrStr_LONGEST)];
20734 +       int sz;
20735 +
20736 +#define SetPerm(str) do {                      \
20737 +               sz = sizeof(str);               \
20738 +               memcpy(a, str, sz);             \
20739 +               p = a + sz - 1;                 \
20740 +       } while (0)
20741 +
20742 +#define AppendAttr(flag, str) do {                     \
20743 +               if (brperm & flag) {            \
20744 +                       sz = sizeof(str);       \
20745 +                       *p++ = '+';             \
20746 +                       memcpy(p, str, sz);     \
20747 +                       p += sz - 1;            \
20748 +               }                               \
20749 +       } while (0)
20750 +
20751 +       switch (brperm & AuBrPerm_Mask) {
20752 +       case AuBrPerm_RO:
20753 +               SetPerm(AUFS_BRPERM_RO);
20754 +               break;
20755 +       case AuBrPerm_RR:
20756 +               SetPerm(AUFS_BRPERM_RR);
20757 +               break;
20758 +       case AuBrPerm_RW:
20759 +               SetPerm(AUFS_BRPERM_RW);
20760 +               break;
20761 +       default:
20762 +               AuDebugOn(1);
20763 +       }
20764 +
20765 +       AppendAttr(AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN);
20766 +       AppendAttr(AuBrRAttr_WH, AUFS_BRRATTR_WH);
20767 +       AppendAttr(AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH);
20768 +
20769 +       AuDebugOn(strlen(a) >= sizeof(a));
20770 +       return kstrdup(a, GFP_NOFS);
20771 +#undef SetPerm
20772 +#undef AppendAttr
20773 +}
20774 +
20775 +/* ---------------------------------------------------------------------- */
20776 +
20777 +static match_table_t udbalevel = {
20778 +       {AuOpt_UDBA_REVAL, "reval"},
20779 +       {AuOpt_UDBA_NONE, "none"},
20780 +#ifdef CONFIG_AUFS_HNOTIFY
20781 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
20782 +#ifdef CONFIG_AUFS_HFSNOTIFY
20783 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
20784 +#endif
20785 +#endif
20786 +       {-1, NULL}
20787 +};
20788 +
20789 +static int noinline_for_stack udba_val(char *str)
20790 +{
20791 +       substring_t args[MAX_OPT_ARGS];
20792 +
20793 +       return match_token(str, udbalevel, args);
20794 +}
20795 +
20796 +const char *au_optstr_udba(int udba)
20797 +{
20798 +       return au_parser_pattern(udba, (void *)udbalevel);
20799 +}
20800 +
20801 +/* ---------------------------------------------------------------------- */
20802 +
20803 +static match_table_t au_wbr_create_policy = {
20804 +       {AuWbrCreate_TDP, "tdp"},
20805 +       {AuWbrCreate_TDP, "top-down-parent"},
20806 +       {AuWbrCreate_RR, "rr"},
20807 +       {AuWbrCreate_RR, "round-robin"},
20808 +       {AuWbrCreate_MFS, "mfs"},
20809 +       {AuWbrCreate_MFS, "most-free-space"},
20810 +       {AuWbrCreate_MFSV, "mfs:%d"},
20811 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
20812 +
20813 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
20814 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
20815 +       {AuWbrCreate_PMFS, "pmfs"},
20816 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
20817 +
20818 +       {-1, NULL}
20819 +};
20820 +
20821 +/*
20822 + * cf. linux/lib/parser.c and cmdline.c
20823 + * gave up calling memparse() since it uses simple_strtoull() instead of
20824 + * kstrto...().
20825 + */
20826 +static int noinline_for_stack
20827 +au_match_ull(substring_t *s, unsigned long long *result)
20828 +{
20829 +       int err;
20830 +       unsigned int len;
20831 +       char a[32];
20832 +
20833 +       err = -ERANGE;
20834 +       len = s->to - s->from;
20835 +       if (len + 1 <= sizeof(a)) {
20836 +               memcpy(a, s->from, len);
20837 +               a[len] = '\0';
20838 +               err = kstrtoull(a, 0, result);
20839 +       }
20840 +       return err;
20841 +}
20842 +
20843 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
20844 +                           struct au_opt_wbr_create *create)
20845 +{
20846 +       int err;
20847 +       unsigned long long ull;
20848 +
20849 +       err = 0;
20850 +       if (!au_match_ull(arg, &ull))
20851 +               create->mfsrr_watermark = ull;
20852 +       else {
20853 +               pr_err("bad integer in %s\n", str);
20854 +               err = -EINVAL;
20855 +       }
20856 +
20857 +       return err;
20858 +}
20859 +
20860 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
20861 +                         struct au_opt_wbr_create *create)
20862 +{
20863 +       int n, err;
20864 +
20865 +       err = 0;
20866 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
20867 +               create->mfs_second = n;
20868 +       else {
20869 +               pr_err("bad integer in %s\n", str);
20870 +               err = -EINVAL;
20871 +       }
20872 +
20873 +       return err;
20874 +}
20875 +
20876 +static int noinline_for_stack
20877 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
20878 +{
20879 +       int err, e;
20880 +       substring_t args[MAX_OPT_ARGS];
20881 +
20882 +       err = match_token(str, au_wbr_create_policy, args);
20883 +       create->wbr_create = err;
20884 +       switch (err) {
20885 +       case AuWbrCreate_MFSRRV:
20886 +               e = au_wbr_mfs_wmark(&args[0], str, create);
20887 +               if (!e)
20888 +                       e = au_wbr_mfs_sec(&args[1], str, create);
20889 +               if (unlikely(e))
20890 +                       err = e;
20891 +               break;
20892 +       case AuWbrCreate_MFSRR:
20893 +               e = au_wbr_mfs_wmark(&args[0], str, create);
20894 +               if (unlikely(e)) {
20895 +                       err = e;
20896 +                       break;
20897 +               }
20898 +               /*FALLTHROUGH*/
20899 +       case AuWbrCreate_MFS:
20900 +       case AuWbrCreate_PMFS:
20901 +               create->mfs_second = AUFS_MFS_DEF_SEC;
20902 +               break;
20903 +       case AuWbrCreate_MFSV:
20904 +       case AuWbrCreate_PMFSV:
20905 +               e = au_wbr_mfs_sec(&args[0], str, create);
20906 +               if (unlikely(e))
20907 +                       err = e;
20908 +               break;
20909 +       }
20910 +
20911 +       return err;
20912 +}
20913 +
20914 +const char *au_optstr_wbr_create(int wbr_create)
20915 +{
20916 +       return au_parser_pattern(wbr_create, (void *)au_wbr_create_policy);
20917 +}
20918 +
20919 +static match_table_t au_wbr_copyup_policy = {
20920 +       {AuWbrCopyup_TDP, "tdp"},
20921 +       {AuWbrCopyup_TDP, "top-down-parent"},
20922 +       {AuWbrCopyup_BUP, "bup"},
20923 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
20924 +       {AuWbrCopyup_BU, "bu"},
20925 +       {AuWbrCopyup_BU, "bottom-up"},
20926 +       {-1, NULL}
20927 +};
20928 +
20929 +static int noinline_for_stack au_wbr_copyup_val(char *str)
20930 +{
20931 +       substring_t args[MAX_OPT_ARGS];
20932 +
20933 +       return match_token(str, au_wbr_copyup_policy, args);
20934 +}
20935 +
20936 +const char *au_optstr_wbr_copyup(int wbr_copyup)
20937 +{
20938 +       return au_parser_pattern(wbr_copyup, (void *)au_wbr_copyup_policy);
20939 +}
20940 +
20941 +/* ---------------------------------------------------------------------- */
20942 +
20943 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
20944 +
20945 +static void dump_opts(struct au_opts *opts)
20946 +{
20947 +#ifdef CONFIG_AUFS_DEBUG
20948 +       /* reduce stack space */
20949 +       union {
20950 +               struct au_opt_add *add;
20951 +               struct au_opt_del *del;
20952 +               struct au_opt_mod *mod;
20953 +               struct au_opt_xino *xino;
20954 +               struct au_opt_xino_itrunc *xino_itrunc;
20955 +               struct au_opt_wbr_create *create;
20956 +       } u;
20957 +       struct au_opt *opt;
20958 +
20959 +       opt = opts->opt;
20960 +       while (opt->type != Opt_tail) {
20961 +               switch (opt->type) {
20962 +               case Opt_add:
20963 +                       u.add = &opt->add;
20964 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
20965 +                                 u.add->bindex, u.add->pathname, u.add->perm,
20966 +                                 u.add->path.dentry);
20967 +                       break;
20968 +               case Opt_del:
20969 +               case Opt_idel:
20970 +                       u.del = &opt->del;
20971 +                       AuDbg("del {%s, %p}\n",
20972 +                             u.del->pathname, u.del->h_path.dentry);
20973 +                       break;
20974 +               case Opt_mod:
20975 +               case Opt_imod:
20976 +                       u.mod = &opt->mod;
20977 +                       AuDbg("mod {%s, 0x%x, %p}\n",
20978 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
20979 +                       break;
20980 +               case Opt_append:
20981 +                       u.add = &opt->add;
20982 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
20983 +                                 u.add->bindex, u.add->pathname, u.add->perm,
20984 +                                 u.add->path.dentry);
20985 +                       break;
20986 +               case Opt_prepend:
20987 +                       u.add = &opt->add;
20988 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
20989 +                                 u.add->bindex, u.add->pathname, u.add->perm,
20990 +                                 u.add->path.dentry);
20991 +                       break;
20992 +               case Opt_dirwh:
20993 +                       AuDbg("dirwh %d\n", opt->dirwh);
20994 +                       break;
20995 +               case Opt_rdcache:
20996 +                       AuDbg("rdcache %d\n", opt->rdcache);
20997 +                       break;
20998 +               case Opt_rdblk:
20999 +                       AuDbg("rdblk %u\n", opt->rdblk);
21000 +                       break;
21001 +               case Opt_rdblk_def:
21002 +                       AuDbg("rdblk_def\n");
21003 +                       break;
21004 +               case Opt_rdhash:
21005 +                       AuDbg("rdhash %u\n", opt->rdhash);
21006 +                       break;
21007 +               case Opt_rdhash_def:
21008 +                       AuDbg("rdhash_def\n");
21009 +                       break;
21010 +               case Opt_xino:
21011 +                       u.xino = &opt->xino;
21012 +                       AuDbg("xino {%s %.*s}\n",
21013 +                                 u.xino->path,
21014 +                                 AuDLNPair(u.xino->file->f_dentry));
21015 +                       break;
21016 +               case Opt_trunc_xino:
21017 +                       AuLabel(trunc_xino);
21018 +                       break;
21019 +               case Opt_notrunc_xino:
21020 +                       AuLabel(notrunc_xino);
21021 +                       break;
21022 +               case Opt_trunc_xino_path:
21023 +               case Opt_itrunc_xino:
21024 +                       u.xino_itrunc = &opt->xino_itrunc;
21025 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
21026 +                       break;
21027 +
21028 +               case Opt_noxino:
21029 +                       AuLabel(noxino);
21030 +                       break;
21031 +               case Opt_trunc_xib:
21032 +                       AuLabel(trunc_xib);
21033 +                       break;
21034 +               case Opt_notrunc_xib:
21035 +                       AuLabel(notrunc_xib);
21036 +                       break;
21037 +               case Opt_shwh:
21038 +                       AuLabel(shwh);
21039 +                       break;
21040 +               case Opt_noshwh:
21041 +                       AuLabel(noshwh);
21042 +                       break;
21043 +               case Opt_plink:
21044 +                       AuLabel(plink);
21045 +                       break;
21046 +               case Opt_noplink:
21047 +                       AuLabel(noplink);
21048 +                       break;
21049 +               case Opt_list_plink:
21050 +                       AuLabel(list_plink);
21051 +                       break;
21052 +               case Opt_udba:
21053 +                       AuDbg("udba %d, %s\n",
21054 +                                 opt->udba, au_optstr_udba(opt->udba));
21055 +                       break;
21056 +               case Opt_dio:
21057 +                       AuLabel(dio);
21058 +                       break;
21059 +               case Opt_nodio:
21060 +                       AuLabel(nodio);
21061 +                       break;
21062 +               case Opt_diropq_a:
21063 +                       AuLabel(diropq_a);
21064 +                       break;
21065 +               case Opt_diropq_w:
21066 +                       AuLabel(diropq_w);
21067 +                       break;
21068 +               case Opt_warn_perm:
21069 +                       AuLabel(warn_perm);
21070 +                       break;
21071 +               case Opt_nowarn_perm:
21072 +                       AuLabel(nowarn_perm);
21073 +                       break;
21074 +               case Opt_refrof:
21075 +                       AuLabel(refrof);
21076 +                       break;
21077 +               case Opt_norefrof:
21078 +                       AuLabel(norefrof);
21079 +                       break;
21080 +               case Opt_verbose:
21081 +                       AuLabel(verbose);
21082 +                       break;
21083 +               case Opt_noverbose:
21084 +                       AuLabel(noverbose);
21085 +                       break;
21086 +               case Opt_sum:
21087 +                       AuLabel(sum);
21088 +                       break;
21089 +               case Opt_nosum:
21090 +                       AuLabel(nosum);
21091 +                       break;
21092 +               case Opt_wsum:
21093 +                       AuLabel(wsum);
21094 +                       break;
21095 +               case Opt_wbr_create:
21096 +                       u.create = &opt->wbr_create;
21097 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
21098 +                                 au_optstr_wbr_create(u.create->wbr_create));
21099 +                       switch (u.create->wbr_create) {
21100 +                       case AuWbrCreate_MFSV:
21101 +                       case AuWbrCreate_PMFSV:
21102 +                               AuDbg("%d sec\n", u.create->mfs_second);
21103 +                               break;
21104 +                       case AuWbrCreate_MFSRR:
21105 +                               AuDbg("%llu watermark\n",
21106 +                                         u.create->mfsrr_watermark);
21107 +                               break;
21108 +                       case AuWbrCreate_MFSRRV:
21109 +                               AuDbg("%llu watermark, %d sec\n",
21110 +                                         u.create->mfsrr_watermark,
21111 +                                         u.create->mfs_second);
21112 +                               break;
21113 +                       }
21114 +                       break;
21115 +               case Opt_wbr_copyup:
21116 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
21117 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
21118 +                       break;
21119 +               default:
21120 +                       BUG();
21121 +               }
21122 +               opt++;
21123 +       }
21124 +#endif
21125 +}
21126 +
21127 +void au_opts_free(struct au_opts *opts)
21128 +{
21129 +       struct au_opt *opt;
21130 +
21131 +       opt = opts->opt;
21132 +       while (opt->type != Opt_tail) {
21133 +               switch (opt->type) {
21134 +               case Opt_add:
21135 +               case Opt_append:
21136 +               case Opt_prepend:
21137 +                       path_put(&opt->add.path);
21138 +                       break;
21139 +               case Opt_del:
21140 +               case Opt_idel:
21141 +                       path_put(&opt->del.h_path);
21142 +                       break;
21143 +               case Opt_mod:
21144 +               case Opt_imod:
21145 +                       dput(opt->mod.h_root);
21146 +                       break;
21147 +               case Opt_xino:
21148 +                       fput(opt->xino.file);
21149 +                       break;
21150 +               }
21151 +               opt++;
21152 +       }
21153 +}
21154 +
21155 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
21156 +                  aufs_bindex_t bindex)
21157 +{
21158 +       int err;
21159 +       struct au_opt_add *add = &opt->add;
21160 +       char *p;
21161 +
21162 +       add->bindex = bindex;
21163 +       add->perm = AuBrPerm_RO;
21164 +       add->pathname = opt_str;
21165 +       p = strchr(opt_str, '=');
21166 +       if (p) {
21167 +               *p++ = 0;
21168 +               if (*p)
21169 +                       add->perm = br_perm_val(p);
21170 +       }
21171 +
21172 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
21173 +       if (!err) {
21174 +               if (!p) {
21175 +                       add->perm = AuBrPerm_RO;
21176 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
21177 +                               add->perm = AuBrPerm_RR;
21178 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
21179 +                               add->perm = AuBrPerm_RW;
21180 +               }
21181 +               opt->type = Opt_add;
21182 +               goto out;
21183 +       }
21184 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
21185 +       err = -EINVAL;
21186 +
21187 +out:
21188 +       return err;
21189 +}
21190 +
21191 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
21192 +{
21193 +       int err;
21194 +
21195 +       del->pathname = args[0].from;
21196 +       AuDbg("del path %s\n", del->pathname);
21197 +
21198 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
21199 +       if (unlikely(err))
21200 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
21201 +
21202 +       return err;
21203 +}
21204 +
21205 +#if 0 /* reserved for future use */
21206 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
21207 +                             struct au_opt_del *del, substring_t args[])
21208 +{
21209 +       int err;
21210 +       struct dentry *root;
21211 +
21212 +       err = -EINVAL;
21213 +       root = sb->s_root;
21214 +       aufs_read_lock(root, AuLock_FLUSH);
21215 +       if (bindex < 0 || au_sbend(sb) < bindex) {
21216 +               pr_err("out of bounds, %d\n", bindex);
21217 +               goto out;
21218 +       }
21219 +
21220 +       err = 0;
21221 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
21222 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
21223 +
21224 +out:
21225 +       aufs_read_unlock(root, !AuLock_IR);
21226 +       return err;
21227 +}
21228 +#endif
21229 +
21230 +static int noinline_for_stack
21231 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
21232 +{
21233 +       int err;
21234 +       struct path path;
21235 +       char *p;
21236 +
21237 +       err = -EINVAL;
21238 +       mod->path = args[0].from;
21239 +       p = strchr(mod->path, '=');
21240 +       if (unlikely(!p)) {
21241 +               pr_err("no permssion %s\n", args[0].from);
21242 +               goto out;
21243 +       }
21244 +
21245 +       *p++ = 0;
21246 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
21247 +       if (unlikely(err)) {
21248 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
21249 +               goto out;
21250 +       }
21251 +
21252 +       mod->perm = br_perm_val(p);
21253 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
21254 +       mod->h_root = dget(path.dentry);
21255 +       path_put(&path);
21256 +
21257 +out:
21258 +       return err;
21259 +}
21260 +
21261 +#if 0 /* reserved for future use */
21262 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
21263 +                             struct au_opt_mod *mod, substring_t args[])
21264 +{
21265 +       int err;
21266 +       struct dentry *root;
21267 +
21268 +       err = -EINVAL;
21269 +       root = sb->s_root;
21270 +       aufs_read_lock(root, AuLock_FLUSH);
21271 +       if (bindex < 0 || au_sbend(sb) < bindex) {
21272 +               pr_err("out of bounds, %d\n", bindex);
21273 +               goto out;
21274 +       }
21275 +
21276 +       err = 0;
21277 +       mod->perm = br_perm_val(args[1].from);
21278 +       AuDbg("mod path %s, perm 0x%x, %s\n",
21279 +             mod->path, mod->perm, args[1].from);
21280 +       mod->h_root = dget(au_h_dptr(root, bindex));
21281 +
21282 +out:
21283 +       aufs_read_unlock(root, !AuLock_IR);
21284 +       return err;
21285 +}
21286 +#endif
21287 +
21288 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
21289 +                             substring_t args[])
21290 +{
21291 +       int err;
21292 +       struct file *file;
21293 +
21294 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
21295 +       err = PTR_ERR(file);
21296 +       if (IS_ERR(file))
21297 +               goto out;
21298 +
21299 +       err = -EINVAL;
21300 +       if (unlikely(file->f_dentry->d_sb == sb)) {
21301 +               fput(file);
21302 +               pr_err("%s must be outside\n", args[0].from);
21303 +               goto out;
21304 +       }
21305 +
21306 +       err = 0;
21307 +       xino->file = file;
21308 +       xino->path = args[0].from;
21309 +
21310 +out:
21311 +       return err;
21312 +}
21313 +
21314 +static int noinline_for_stack
21315 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
21316 +                              struct au_opt_xino_itrunc *xino_itrunc,
21317 +                              substring_t args[])
21318 +{
21319 +       int err;
21320 +       aufs_bindex_t bend, bindex;
21321 +       struct path path;
21322 +       struct dentry *root;
21323 +
21324 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
21325 +       if (unlikely(err)) {
21326 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
21327 +               goto out;
21328 +       }
21329 +
21330 +       xino_itrunc->bindex = -1;
21331 +       root = sb->s_root;
21332 +       aufs_read_lock(root, AuLock_FLUSH);
21333 +       bend = au_sbend(sb);
21334 +       for (bindex = 0; bindex <= bend; bindex++) {
21335 +               if (au_h_dptr(root, bindex) == path.dentry) {
21336 +                       xino_itrunc->bindex = bindex;
21337 +                       break;
21338 +               }
21339 +       }
21340 +       aufs_read_unlock(root, !AuLock_IR);
21341 +       path_put(&path);
21342 +
21343 +       if (unlikely(xino_itrunc->bindex < 0)) {
21344 +               pr_err("no such branch %s\n", args[0].from);
21345 +               err = -EINVAL;
21346 +       }
21347 +
21348 +out:
21349 +       return err;
21350 +}
21351 +
21352 +/* called without aufs lock */
21353 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
21354 +{
21355 +       int err, n, token;
21356 +       aufs_bindex_t bindex;
21357 +       unsigned char skipped;
21358 +       struct dentry *root;
21359 +       struct au_opt *opt, *opt_tail;
21360 +       char *opt_str;
21361 +       /* reduce the stack space */
21362 +       union {
21363 +               struct au_opt_xino_itrunc *xino_itrunc;
21364 +               struct au_opt_wbr_create *create;
21365 +       } u;
21366 +       struct {
21367 +               substring_t args[MAX_OPT_ARGS];
21368 +       } *a;
21369 +
21370 +       err = -ENOMEM;
21371 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21372 +       if (unlikely(!a))
21373 +               goto out;
21374 +
21375 +       root = sb->s_root;
21376 +       err = 0;
21377 +       bindex = 0;
21378 +       opt = opts->opt;
21379 +       opt_tail = opt + opts->max_opt - 1;
21380 +       opt->type = Opt_tail;
21381 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
21382 +               err = -EINVAL;
21383 +               skipped = 0;
21384 +               token = match_token(opt_str, options, a->args);
21385 +               switch (token) {
21386 +               case Opt_br:
21387 +                       err = 0;
21388 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
21389 +                              && *opt_str) {
21390 +                               err = opt_add(opt, opt_str, opts->sb_flags,
21391 +                                             bindex++);
21392 +                               if (unlikely(!err && ++opt > opt_tail)) {
21393 +                                       err = -E2BIG;
21394 +                                       break;
21395 +                               }
21396 +                               opt->type = Opt_tail;
21397 +                               skipped = 1;
21398 +                       }
21399 +                       break;
21400 +               case Opt_add:
21401 +                       if (unlikely(match_int(&a->args[0], &n))) {
21402 +                               pr_err("bad integer in %s\n", opt_str);
21403 +                               break;
21404 +                       }
21405 +                       bindex = n;
21406 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
21407 +                                     bindex);
21408 +                       if (!err)
21409 +                               opt->type = token;
21410 +                       break;
21411 +               case Opt_append:
21412 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
21413 +                                     /*dummy bindex*/1);
21414 +                       if (!err)
21415 +                               opt->type = token;
21416 +                       break;
21417 +               case Opt_prepend:
21418 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
21419 +                                     /*bindex*/0);
21420 +                       if (!err)
21421 +                               opt->type = token;
21422 +                       break;
21423 +               case Opt_del:
21424 +                       err = au_opts_parse_del(&opt->del, a->args);
21425 +                       if (!err)
21426 +                               opt->type = token;
21427 +                       break;
21428 +#if 0 /* reserved for future use */
21429 +               case Opt_idel:
21430 +                       del->pathname = "(indexed)";
21431 +                       if (unlikely(match_int(&args[0], &n))) {
21432 +                               pr_err("bad integer in %s\n", opt_str);
21433 +                               break;
21434 +                       }
21435 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
21436 +                       if (!err)
21437 +                               opt->type = token;
21438 +                       break;
21439 +#endif
21440 +               case Opt_mod:
21441 +                       err = au_opts_parse_mod(&opt->mod, a->args);
21442 +                       if (!err)
21443 +                               opt->type = token;
21444 +                       break;
21445 +#ifdef IMOD /* reserved for future use */
21446 +               case Opt_imod:
21447 +                       u.mod->path = "(indexed)";
21448 +                       if (unlikely(match_int(&a->args[0], &n))) {
21449 +                               pr_err("bad integer in %s\n", opt_str);
21450 +                               break;
21451 +                       }
21452 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
21453 +                       if (!err)
21454 +                               opt->type = token;
21455 +                       break;
21456 +#endif
21457 +               case Opt_xino:
21458 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
21459 +                       if (!err)
21460 +                               opt->type = token;
21461 +                       break;
21462 +
21463 +               case Opt_trunc_xino_path:
21464 +                       err = au_opts_parse_xino_itrunc_path
21465 +                               (sb, &opt->xino_itrunc, a->args);
21466 +                       if (!err)
21467 +                               opt->type = token;
21468 +                       break;
21469 +
21470 +               case Opt_itrunc_xino:
21471 +                       u.xino_itrunc = &opt->xino_itrunc;
21472 +                       if (unlikely(match_int(&a->args[0], &n))) {
21473 +                               pr_err("bad integer in %s\n", opt_str);
21474 +                               break;
21475 +                       }
21476 +                       u.xino_itrunc->bindex = n;
21477 +                       aufs_read_lock(root, AuLock_FLUSH);
21478 +                       if (n < 0 || au_sbend(sb) < n) {
21479 +                               pr_err("out of bounds, %d\n", n);
21480 +                               aufs_read_unlock(root, !AuLock_IR);
21481 +                               break;
21482 +                       }
21483 +                       aufs_read_unlock(root, !AuLock_IR);
21484 +                       err = 0;
21485 +                       opt->type = token;
21486 +                       break;
21487 +
21488 +               case Opt_dirwh:
21489 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
21490 +                               break;
21491 +                       err = 0;
21492 +                       opt->type = token;
21493 +                       break;
21494 +
21495 +               case Opt_rdcache:
21496 +                       if (unlikely(match_int(&a->args[0], &n))) {
21497 +                               pr_err("bad integer in %s\n", opt_str);
21498 +                               break;
21499 +                       }
21500 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
21501 +                               pr_err("rdcache must be smaller than %d\n",
21502 +                                      AUFS_RDCACHE_MAX);
21503 +                               break;
21504 +                       }
21505 +                       opt->rdcache = n;
21506 +                       err = 0;
21507 +                       opt->type = token;
21508 +                       break;
21509 +               case Opt_rdblk:
21510 +                       if (unlikely(match_int(&a->args[0], &n)
21511 +                                    || n < 0
21512 +                                    || n > KMALLOC_MAX_SIZE)) {
21513 +                               pr_err("bad integer in %s\n", opt_str);
21514 +                               break;
21515 +                       }
21516 +                       if (unlikely(n && n < NAME_MAX)) {
21517 +                               pr_err("rdblk must be larger than %d\n",
21518 +                                      NAME_MAX);
21519 +                               break;
21520 +                       }
21521 +                       opt->rdblk = n;
21522 +                       err = 0;
21523 +                       opt->type = token;
21524 +                       break;
21525 +               case Opt_rdhash:
21526 +                       if (unlikely(match_int(&a->args[0], &n)
21527 +                                    || n < 0
21528 +                                    || n * sizeof(struct hlist_head)
21529 +                                    > KMALLOC_MAX_SIZE)) {
21530 +                               pr_err("bad integer in %s\n", opt_str);
21531 +                               break;
21532 +                       }
21533 +                       opt->rdhash = n;
21534 +                       err = 0;
21535 +                       opt->type = token;
21536 +                       break;
21537 +
21538 +               case Opt_trunc_xino:
21539 +               case Opt_notrunc_xino:
21540 +               case Opt_noxino:
21541 +               case Opt_trunc_xib:
21542 +               case Opt_notrunc_xib:
21543 +               case Opt_shwh:
21544 +               case Opt_noshwh:
21545 +               case Opt_plink:
21546 +               case Opt_noplink:
21547 +               case Opt_list_plink:
21548 +               case Opt_dio:
21549 +               case Opt_nodio:
21550 +               case Opt_diropq_a:
21551 +               case Opt_diropq_w:
21552 +               case Opt_warn_perm:
21553 +               case Opt_nowarn_perm:
21554 +               case Opt_refrof:
21555 +               case Opt_norefrof:
21556 +               case Opt_verbose:
21557 +               case Opt_noverbose:
21558 +               case Opt_sum:
21559 +               case Opt_nosum:
21560 +               case Opt_wsum:
21561 +               case Opt_rdblk_def:
21562 +               case Opt_rdhash_def:
21563 +                       err = 0;
21564 +                       opt->type = token;
21565 +                       break;
21566 +
21567 +               case Opt_udba:
21568 +                       opt->udba = udba_val(a->args[0].from);
21569 +                       if (opt->udba >= 0) {
21570 +                               err = 0;
21571 +                               opt->type = token;
21572 +                       } else
21573 +                               pr_err("wrong value, %s\n", opt_str);
21574 +                       break;
21575 +
21576 +               case Opt_wbr_create:
21577 +                       u.create = &opt->wbr_create;
21578 +                       u.create->wbr_create
21579 +                               = au_wbr_create_val(a->args[0].from, u.create);
21580 +                       if (u.create->wbr_create >= 0) {
21581 +                               err = 0;
21582 +                               opt->type = token;
21583 +                       } else
21584 +                               pr_err("wrong value, %s\n", opt_str);
21585 +                       break;
21586 +               case Opt_wbr_copyup:
21587 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
21588 +                       if (opt->wbr_copyup >= 0) {
21589 +                               err = 0;
21590 +                               opt->type = token;
21591 +                       } else
21592 +                               pr_err("wrong value, %s\n", opt_str);
21593 +                       break;
21594 +
21595 +               case Opt_ignore:
21596 +                       pr_warn("ignored %s\n", opt_str);
21597 +                       /*FALLTHROUGH*/
21598 +               case Opt_ignore_silent:
21599 +                       skipped = 1;
21600 +                       err = 0;
21601 +                       break;
21602 +               case Opt_err:
21603 +                       pr_err("unknown option %s\n", opt_str);
21604 +                       break;
21605 +               }
21606 +
21607 +               if (!err && !skipped) {
21608 +                       if (unlikely(++opt > opt_tail)) {
21609 +                               err = -E2BIG;
21610 +                               opt--;
21611 +                               opt->type = Opt_tail;
21612 +                               break;
21613 +                       }
21614 +                       opt->type = Opt_tail;
21615 +               }
21616 +       }
21617 +
21618 +       kfree(a);
21619 +       dump_opts(opts);
21620 +       if (unlikely(err))
21621 +               au_opts_free(opts);
21622 +
21623 +out:
21624 +       return err;
21625 +}
21626 +
21627 +static int au_opt_wbr_create(struct super_block *sb,
21628 +                            struct au_opt_wbr_create *create)
21629 +{
21630 +       int err;
21631 +       struct au_sbinfo *sbinfo;
21632 +
21633 +       SiMustWriteLock(sb);
21634 +
21635 +       err = 1; /* handled */
21636 +       sbinfo = au_sbi(sb);
21637 +       if (sbinfo->si_wbr_create_ops->fin) {
21638 +               err = sbinfo->si_wbr_create_ops->fin(sb);
21639 +               if (!err)
21640 +                       err = 1;
21641 +       }
21642 +
21643 +       sbinfo->si_wbr_create = create->wbr_create;
21644 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
21645 +       switch (create->wbr_create) {
21646 +       case AuWbrCreate_MFSRRV:
21647 +       case AuWbrCreate_MFSRR:
21648 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
21649 +               /*FALLTHROUGH*/
21650 +       case AuWbrCreate_MFS:
21651 +       case AuWbrCreate_MFSV:
21652 +       case AuWbrCreate_PMFS:
21653 +       case AuWbrCreate_PMFSV:
21654 +               sbinfo->si_wbr_mfs.mfs_expire
21655 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
21656 +               break;
21657 +       }
21658 +
21659 +       if (sbinfo->si_wbr_create_ops->init)
21660 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
21661 +
21662 +       return err;
21663 +}
21664 +
21665 +/*
21666 + * returns,
21667 + * plus: processed without an error
21668 + * zero: unprocessed
21669 + */
21670 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
21671 +                        struct au_opts *opts)
21672 +{
21673 +       int err;
21674 +       struct au_sbinfo *sbinfo;
21675 +
21676 +       SiMustWriteLock(sb);
21677 +
21678 +       err = 1; /* handled */
21679 +       sbinfo = au_sbi(sb);
21680 +       switch (opt->type) {
21681 +       case Opt_udba:
21682 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
21683 +               sbinfo->si_mntflags |= opt->udba;
21684 +               opts->given_udba |= opt->udba;
21685 +               break;
21686 +
21687 +       case Opt_plink:
21688 +               au_opt_set(sbinfo->si_mntflags, PLINK);
21689 +               break;
21690 +       case Opt_noplink:
21691 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
21692 +                       au_plink_put(sb, /*verbose*/1);
21693 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
21694 +               break;
21695 +       case Opt_list_plink:
21696 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
21697 +                       au_plink_list(sb);
21698 +               break;
21699 +
21700 +       case Opt_dio:
21701 +               au_opt_set(sbinfo->si_mntflags, DIO);
21702 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
21703 +               break;
21704 +       case Opt_nodio:
21705 +               au_opt_clr(sbinfo->si_mntflags, DIO);
21706 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
21707 +               break;
21708 +
21709 +       case Opt_diropq_a:
21710 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
21711 +               break;
21712 +       case Opt_diropq_w:
21713 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
21714 +               break;
21715 +
21716 +       case Opt_warn_perm:
21717 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
21718 +               break;
21719 +       case Opt_nowarn_perm:
21720 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
21721 +               break;
21722 +
21723 +       case Opt_refrof:
21724 +               au_opt_set(sbinfo->si_mntflags, REFROF);
21725 +               break;
21726 +       case Opt_norefrof:
21727 +               au_opt_clr(sbinfo->si_mntflags, REFROF);
21728 +               break;
21729 +
21730 +       case Opt_verbose:
21731 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
21732 +               break;
21733 +       case Opt_noverbose:
21734 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
21735 +               break;
21736 +
21737 +       case Opt_sum:
21738 +               au_opt_set(sbinfo->si_mntflags, SUM);
21739 +               break;
21740 +       case Opt_wsum:
21741 +               au_opt_clr(sbinfo->si_mntflags, SUM);
21742 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
21743 +       case Opt_nosum:
21744 +               au_opt_clr(sbinfo->si_mntflags, SUM);
21745 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
21746 +               break;
21747 +
21748 +       case Opt_wbr_create:
21749 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
21750 +               break;
21751 +       case Opt_wbr_copyup:
21752 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
21753 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
21754 +               break;
21755 +
21756 +       case Opt_dirwh:
21757 +               sbinfo->si_dirwh = opt->dirwh;
21758 +               break;
21759 +
21760 +       case Opt_rdcache:
21761 +               sbinfo->si_rdcache
21762 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
21763 +               break;
21764 +       case Opt_rdblk:
21765 +               sbinfo->si_rdblk = opt->rdblk;
21766 +               break;
21767 +       case Opt_rdblk_def:
21768 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
21769 +               break;
21770 +       case Opt_rdhash:
21771 +               sbinfo->si_rdhash = opt->rdhash;
21772 +               break;
21773 +       case Opt_rdhash_def:
21774 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
21775 +               break;
21776 +
21777 +       case Opt_shwh:
21778 +               au_opt_set(sbinfo->si_mntflags, SHWH);
21779 +               break;
21780 +       case Opt_noshwh:
21781 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
21782 +               break;
21783 +
21784 +       case Opt_trunc_xino:
21785 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
21786 +               break;
21787 +       case Opt_notrunc_xino:
21788 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
21789 +               break;
21790 +
21791 +       case Opt_trunc_xino_path:
21792 +       case Opt_itrunc_xino:
21793 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
21794 +               if (!err)
21795 +                       err = 1;
21796 +               break;
21797 +
21798 +       case Opt_trunc_xib:
21799 +               au_fset_opts(opts->flags, TRUNC_XIB);
21800 +               break;
21801 +       case Opt_notrunc_xib:
21802 +               au_fclr_opts(opts->flags, TRUNC_XIB);
21803 +               break;
21804 +
21805 +       default:
21806 +               err = 0;
21807 +               break;
21808 +       }
21809 +
21810 +       return err;
21811 +}
21812 +
21813 +/*
21814 + * returns tri-state.
21815 + * plus: processed without an error
21816 + * zero: unprocessed
21817 + * minus: error
21818 + */
21819 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
21820 +                    struct au_opts *opts)
21821 +{
21822 +       int err, do_refresh;
21823 +
21824 +       err = 0;
21825 +       switch (opt->type) {
21826 +       case Opt_append:
21827 +               opt->add.bindex = au_sbend(sb) + 1;
21828 +               if (opt->add.bindex < 0)
21829 +                       opt->add.bindex = 0;
21830 +               goto add;
21831 +       case Opt_prepend:
21832 +               opt->add.bindex = 0;
21833 +       add:
21834 +       case Opt_add:
21835 +               err = au_br_add(sb, &opt->add,
21836 +                               au_ftest_opts(opts->flags, REMOUNT));
21837 +               if (!err) {
21838 +                       err = 1;
21839 +                       au_fset_opts(opts->flags, REFRESH);
21840 +               }
21841 +               break;
21842 +
21843 +       case Opt_del:
21844 +       case Opt_idel:
21845 +               err = au_br_del(sb, &opt->del,
21846 +                               au_ftest_opts(opts->flags, REMOUNT));
21847 +               if (!err) {
21848 +                       err = 1;
21849 +                       au_fset_opts(opts->flags, TRUNC_XIB);
21850 +                       au_fset_opts(opts->flags, REFRESH);
21851 +               }
21852 +               break;
21853 +
21854 +       case Opt_mod:
21855 +       case Opt_imod:
21856 +               err = au_br_mod(sb, &opt->mod,
21857 +                               au_ftest_opts(opts->flags, REMOUNT),
21858 +                               &do_refresh);
21859 +               if (!err) {
21860 +                       err = 1;
21861 +                       if (do_refresh)
21862 +                               au_fset_opts(opts->flags, REFRESH);
21863 +               }
21864 +               break;
21865 +       }
21866 +
21867 +       return err;
21868 +}
21869 +
21870 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
21871 +                      struct au_opt_xino **opt_xino,
21872 +                      struct au_opts *opts)
21873 +{
21874 +       int err;
21875 +       aufs_bindex_t bend, bindex;
21876 +       struct dentry *root, *parent, *h_root;
21877 +
21878 +       err = 0;
21879 +       switch (opt->type) {
21880 +       case Opt_xino:
21881 +               err = au_xino_set(sb, &opt->xino,
21882 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
21883 +               if (unlikely(err))
21884 +                       break;
21885 +
21886 +               *opt_xino = &opt->xino;
21887 +               au_xino_brid_set(sb, -1);
21888 +
21889 +               /* safe d_parent access */
21890 +               parent = opt->xino.file->f_dentry->d_parent;
21891 +               root = sb->s_root;
21892 +               bend = au_sbend(sb);
21893 +               for (bindex = 0; bindex <= bend; bindex++) {
21894 +                       h_root = au_h_dptr(root, bindex);
21895 +                       if (h_root == parent) {
21896 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
21897 +                               break;
21898 +                       }
21899 +               }
21900 +               break;
21901 +
21902 +       case Opt_noxino:
21903 +               au_xino_clr(sb);
21904 +               au_xino_brid_set(sb, -1);
21905 +               *opt_xino = (void *)-1;
21906 +               break;
21907 +       }
21908 +
21909 +       return err;
21910 +}
21911 +
21912 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
21913 +                  unsigned int pending)
21914 +{
21915 +       int err;
21916 +       aufs_bindex_t bindex, bend;
21917 +       unsigned char do_plink, skip, do_free;
21918 +       struct au_branch *br;
21919 +       struct au_wbr *wbr;
21920 +       struct dentry *root;
21921 +       struct inode *dir, *h_dir;
21922 +       struct au_sbinfo *sbinfo;
21923 +       struct au_hinode *hdir;
21924 +
21925 +       SiMustAnyLock(sb);
21926 +
21927 +       sbinfo = au_sbi(sb);
21928 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
21929 +
21930 +       if (!(sb_flags & MS_RDONLY)) {
21931 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
21932 +                       pr_warn("first branch should be rw\n");
21933 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
21934 +                       pr_warn("shwh should be used with ro\n");
21935 +       }
21936 +
21937 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
21938 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
21939 +               pr_warn("udba=*notify requires xino\n");
21940 +
21941 +       err = 0;
21942 +       root = sb->s_root;
21943 +       dir = root->d_inode;
21944 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
21945 +       bend = au_sbend(sb);
21946 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
21947 +               skip = 0;
21948 +               h_dir = au_h_iptr(dir, bindex);
21949 +               br = au_sbr(sb, bindex);
21950 +               do_free = 0;
21951 +
21952 +               wbr = br->br_wbr;
21953 +               if (wbr)
21954 +                       wbr_wh_read_lock(wbr);
21955 +
21956 +               if (!au_br_writable(br->br_perm)) {
21957 +                       do_free = !!wbr;
21958 +                       skip = (!wbr
21959 +                               || (!wbr->wbr_whbase
21960 +                                   && !wbr->wbr_plink
21961 +                                   && !wbr->wbr_orph));
21962 +               } else if (!au_br_wh_linkable(br->br_perm)) {
21963 +                       /* skip = (!br->br_whbase && !br->br_orph); */
21964 +                       skip = (!wbr || !wbr->wbr_whbase);
21965 +                       if (skip && wbr) {
21966 +                               if (do_plink)
21967 +                                       skip = !!wbr->wbr_plink;
21968 +                               else
21969 +                                       skip = !wbr->wbr_plink;
21970 +                       }
21971 +               } else {
21972 +                       /* skip = (br->br_whbase && br->br_ohph); */
21973 +                       skip = (wbr && wbr->wbr_whbase);
21974 +                       if (skip) {
21975 +                               if (do_plink)
21976 +                                       skip = !!wbr->wbr_plink;
21977 +                               else
21978 +                                       skip = !wbr->wbr_plink;
21979 +                       }
21980 +               }
21981 +               if (wbr)
21982 +                       wbr_wh_read_unlock(wbr);
21983 +
21984 +               if (skip)
21985 +                       continue;
21986 +
21987 +               hdir = au_hi(dir, bindex);
21988 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
21989 +               if (wbr)
21990 +                       wbr_wh_write_lock(wbr);
21991 +               err = au_wh_init(br, sb);
21992 +               if (wbr)
21993 +                       wbr_wh_write_unlock(wbr);
21994 +               au_hn_imtx_unlock(hdir);
21995 +
21996 +               if (!err && do_free) {
21997 +                       kfree(wbr);
21998 +                       br->br_wbr = NULL;
21999 +               }
22000 +       }
22001 +
22002 +       return err;
22003 +}
22004 +
22005 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
22006 +{
22007 +       int err;
22008 +       unsigned int tmp;
22009 +       aufs_bindex_t bindex, bend;
22010 +       struct au_opt *opt;
22011 +       struct au_opt_xino *opt_xino, xino;
22012 +       struct au_sbinfo *sbinfo;
22013 +       struct au_branch *br;
22014 +
22015 +       SiMustWriteLock(sb);
22016 +
22017 +       err = 0;
22018 +       opt_xino = NULL;
22019 +       opt = opts->opt;
22020 +       while (err >= 0 && opt->type != Opt_tail)
22021 +               err = au_opt_simple(sb, opt++, opts);
22022 +       if (err > 0)
22023 +               err = 0;
22024 +       else if (unlikely(err < 0))
22025 +               goto out;
22026 +
22027 +       /* disable xino and udba temporary */
22028 +       sbinfo = au_sbi(sb);
22029 +       tmp = sbinfo->si_mntflags;
22030 +       au_opt_clr(sbinfo->si_mntflags, XINO);
22031 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
22032 +
22033 +       opt = opts->opt;
22034 +       while (err >= 0 && opt->type != Opt_tail)
22035 +               err = au_opt_br(sb, opt++, opts);
22036 +       if (err > 0)
22037 +               err = 0;
22038 +       else if (unlikely(err < 0))
22039 +               goto out;
22040 +
22041 +       bend = au_sbend(sb);
22042 +       if (unlikely(bend < 0)) {
22043 +               err = -EINVAL;
22044 +               pr_err("no branches\n");
22045 +               goto out;
22046 +       }
22047 +
22048 +       if (au_opt_test(tmp, XINO))
22049 +               au_opt_set(sbinfo->si_mntflags, XINO);
22050 +       opt = opts->opt;
22051 +       while (!err && opt->type != Opt_tail)
22052 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
22053 +       if (unlikely(err))
22054 +               goto out;
22055 +
22056 +       err = au_opts_verify(sb, sb->s_flags, tmp);
22057 +       if (unlikely(err))
22058 +               goto out;
22059 +
22060 +       /* restore xino */
22061 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
22062 +               xino.file = au_xino_def(sb);
22063 +               err = PTR_ERR(xino.file);
22064 +               if (IS_ERR(xino.file))
22065 +                       goto out;
22066 +
22067 +               err = au_xino_set(sb, &xino, /*remount*/0);
22068 +               fput(xino.file);
22069 +               if (unlikely(err))
22070 +                       goto out;
22071 +       }
22072 +
22073 +       /* restore udba */
22074 +       tmp &= AuOptMask_UDBA;
22075 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
22076 +       sbinfo->si_mntflags |= tmp;
22077 +       bend = au_sbend(sb);
22078 +       for (bindex = 0; bindex <= bend; bindex++) {
22079 +               br = au_sbr(sb, bindex);
22080 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
22081 +               if (unlikely(err))
22082 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
22083 +                               bindex, err);
22084 +               /* go on even if err */
22085 +       }
22086 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
22087 +               struct inode *dir = sb->s_root->d_inode;
22088 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
22089 +       }
22090 +
22091 +out:
22092 +       return err;
22093 +}
22094 +
22095 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
22096 +{
22097 +       int err, rerr;
22098 +       struct inode *dir;
22099 +       struct au_opt_xino *opt_xino;
22100 +       struct au_opt *opt;
22101 +       struct au_sbinfo *sbinfo;
22102 +
22103 +       SiMustWriteLock(sb);
22104 +
22105 +       dir = sb->s_root->d_inode;
22106 +       sbinfo = au_sbi(sb);
22107 +       err = 0;
22108 +       opt_xino = NULL;
22109 +       opt = opts->opt;
22110 +       while (err >= 0 && opt->type != Opt_tail) {
22111 +               err = au_opt_simple(sb, opt, opts);
22112 +               if (!err)
22113 +                       err = au_opt_br(sb, opt, opts);
22114 +               if (!err)
22115 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
22116 +               opt++;
22117 +       }
22118 +       if (err > 0)
22119 +               err = 0;
22120 +       AuTraceErr(err);
22121 +       /* go on even err */
22122 +
22123 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
22124 +       if (unlikely(rerr && !err))
22125 +               err = rerr;
22126 +
22127 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
22128 +               rerr = au_xib_trunc(sb);
22129 +               if (unlikely(rerr && !err))
22130 +                       err = rerr;
22131 +       }
22132 +
22133 +       /* will be handled by the caller */
22134 +       if (!au_ftest_opts(opts->flags, REFRESH)
22135 +           && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
22136 +               au_fset_opts(opts->flags, REFRESH);
22137 +
22138 +       AuDbg("status 0x%x\n", opts->flags);
22139 +       return err;
22140 +}
22141 +
22142 +/* ---------------------------------------------------------------------- */
22143 +
22144 +unsigned int au_opt_udba(struct super_block *sb)
22145 +{
22146 +       return au_mntflags(sb) & AuOptMask_UDBA;
22147 +}
22148 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
22149 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
22150 +++ linux/fs/aufs/opts.h        2013-07-06 13:20:47.753531903 +0200
22151 @@ -0,0 +1,209 @@
22152 +/*
22153 + * Copyright (C) 2005-2013 Junjiro R. Okajima
22154 + *
22155 + * This program, aufs is free software; you can redistribute it and/or modify
22156 + * it under the terms of the GNU General Public License as published by
22157 + * the Free Software Foundation; either version 2 of the License, or
22158 + * (at your option) any later version.
22159 + *
22160 + * This program is distributed in the hope that it will be useful,
22161 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22162 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22163 + * GNU General Public License for more details.
22164 + *
22165 + * You should have received a copy of the GNU General Public License
22166 + * along with this program; if not, write to the Free Software
22167 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22168 + */
22169 +
22170 +/*
22171 + * mount options/flags
22172 + */
22173 +
22174 +#ifndef __AUFS_OPTS_H__
22175 +#define __AUFS_OPTS_H__
22176 +
22177 +#ifdef __KERNEL__
22178 +
22179 +#include <linux/path.h>
22180 +
22181 +struct file;
22182 +struct super_block;
22183 +
22184 +/* ---------------------------------------------------------------------- */
22185 +
22186 +/* mount flags */
22187 +#define AuOpt_XINO             1               /* external inode number bitmap
22188 +                                                  and translation table */
22189 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
22190 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
22191 +#define AuOpt_UDBA_REVAL       (1 << 3)
22192 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
22193 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
22194 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
22195 +#define AuOpt_DIRPERM1         (1 << 7)        /* unimplemented */
22196 +#define AuOpt_REFROF           (1 << 8)        /* unimplemented */
22197 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
22198 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
22199 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
22200 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
22201 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
22202 +#define AuOpt_DIO              (1 << 14)       /* direct io */
22203 +
22204 +#ifndef CONFIG_AUFS_HNOTIFY
22205 +#undef AuOpt_UDBA_HNOTIFY
22206 +#define AuOpt_UDBA_HNOTIFY     0
22207 +#endif
22208 +#ifndef CONFIG_AUFS_SHWH
22209 +#undef AuOpt_SHWH
22210 +#define AuOpt_SHWH             0
22211 +#endif
22212 +
22213 +#define AuOpt_Def      (AuOpt_XINO \
22214 +                        | AuOpt_UDBA_REVAL \
22215 +                        | AuOpt_PLINK \
22216 +                        /* | AuOpt_DIRPERM1 */ \
22217 +                        | AuOpt_WARN_PERM)
22218 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
22219 +                        | AuOpt_UDBA_REVAL \
22220 +                        | AuOpt_UDBA_HNOTIFY)
22221 +
22222 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
22223 +#define au_opt_set(flags, name) do { \
22224 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
22225 +       ((flags) |= AuOpt_##name); \
22226 +} while (0)
22227 +#define au_opt_set_udba(flags, name) do { \
22228 +       (flags) &= ~AuOptMask_UDBA; \
22229 +       ((flags) |= AuOpt_##name); \
22230 +} while (0)
22231 +#define au_opt_clr(flags, name) do { \
22232 +       ((flags) &= ~AuOpt_##name); \
22233 +} while (0)
22234 +
22235 +static inline unsigned int au_opts_plink(unsigned int mntflags)
22236 +{
22237 +#ifdef CONFIG_PROC_FS
22238 +       return mntflags;
22239 +#else
22240 +       return mntflags & ~AuOpt_PLINK;
22241 +#endif
22242 +}
22243 +
22244 +/* ---------------------------------------------------------------------- */
22245 +
22246 +/* policies to select one among multiple writable branches */
22247 +enum {
22248 +       AuWbrCreate_TDP,        /* top down parent */
22249 +       AuWbrCreate_RR,         /* round robin */
22250 +       AuWbrCreate_MFS,        /* most free space */
22251 +       AuWbrCreate_MFSV,       /* mfs with seconds */
22252 +       AuWbrCreate_MFSRR,      /* mfs then rr */
22253 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
22254 +       AuWbrCreate_PMFS,       /* parent and mfs */
22255 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
22256 +
22257 +       AuWbrCreate_Def = AuWbrCreate_TDP
22258 +};
22259 +
22260 +enum {
22261 +       AuWbrCopyup_TDP,        /* top down parent */
22262 +       AuWbrCopyup_BUP,        /* bottom up parent */
22263 +       AuWbrCopyup_BU,         /* bottom up */
22264 +
22265 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
22266 +};
22267 +
22268 +/* ---------------------------------------------------------------------- */
22269 +
22270 +struct au_opt_add {
22271 +       aufs_bindex_t   bindex;
22272 +       char            *pathname;
22273 +       int             perm;
22274 +       struct path     path;
22275 +};
22276 +
22277 +struct au_opt_del {
22278 +       char            *pathname;
22279 +       struct path     h_path;
22280 +};
22281 +
22282 +struct au_opt_mod {
22283 +       char            *path;
22284 +       int             perm;
22285 +       struct dentry   *h_root;
22286 +};
22287 +
22288 +struct au_opt_xino {
22289 +       char            *path;
22290 +       struct file     *file;
22291 +};
22292 +
22293 +struct au_opt_xino_itrunc {
22294 +       aufs_bindex_t   bindex;
22295 +};
22296 +
22297 +struct au_opt_wbr_create {
22298 +       int                     wbr_create;
22299 +       int                     mfs_second;
22300 +       unsigned long long      mfsrr_watermark;
22301 +};
22302 +
22303 +struct au_opt {
22304 +       int type;
22305 +       union {
22306 +               struct au_opt_xino      xino;
22307 +               struct au_opt_xino_itrunc xino_itrunc;
22308 +               struct au_opt_add       add;
22309 +               struct au_opt_del       del;
22310 +               struct au_opt_mod       mod;
22311 +               int                     dirwh;
22312 +               int                     rdcache;
22313 +               unsigned int            rdblk;
22314 +               unsigned int            rdhash;
22315 +               int                     udba;
22316 +               struct au_opt_wbr_create wbr_create;
22317 +               int                     wbr_copyup;
22318 +       };
22319 +};
22320 +
22321 +/* opts flags */
22322 +#define AuOpts_REMOUNT         1
22323 +#define AuOpts_REFRESH         (1 << 1)
22324 +#define AuOpts_TRUNC_XIB       (1 << 2)
22325 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
22326 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
22327 +#define au_fset_opts(flags, name) \
22328 +       do { (flags) |= AuOpts_##name; } while (0)
22329 +#define au_fclr_opts(flags, name) \
22330 +       do { (flags) &= ~AuOpts_##name; } while (0)
22331 +
22332 +struct au_opts {
22333 +       struct au_opt   *opt;
22334 +       int             max_opt;
22335 +
22336 +       unsigned int    given_udba;
22337 +       unsigned int    flags;
22338 +       unsigned long   sb_flags;
22339 +};
22340 +
22341 +/* ---------------------------------------------------------------------- */
22342 +
22343 +char *au_optstr_br_perm(int brperm);
22344 +const char *au_optstr_udba(int udba);
22345 +const char *au_optstr_wbr_copyup(int wbr_copyup);
22346 +const char *au_optstr_wbr_create(int wbr_create);
22347 +
22348 +void au_opts_free(struct au_opts *opts);
22349 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
22350 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
22351 +                  unsigned int pending);
22352 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
22353 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
22354 +
22355 +unsigned int au_opt_udba(struct super_block *sb);
22356 +
22357 +/* ---------------------------------------------------------------------- */
22358 +
22359 +#endif /* __KERNEL__ */
22360 +#endif /* __AUFS_OPTS_H__ */
22361 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
22362 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
22363 +++ linux/fs/aufs/plink.c       2013-07-06 13:20:47.753531903 +0200
22364 @@ -0,0 +1,520 @@
22365 +/*
22366 + * Copyright (C) 2005-2013 Junjiro R. Okajima
22367 + *
22368 + * This program, aufs is free software; you can redistribute it and/or modify
22369 + * it under the terms of the GNU General Public License as published by
22370 + * the Free Software Foundation; either version 2 of the License, or
22371 + * (at your option) any later version.
22372 + *
22373 + * This program is distributed in the hope that it will be useful,
22374 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22375 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22376 + * GNU General Public License for more details.
22377 + *
22378 + * You should have received a copy of the GNU General Public License
22379 + * along with this program; if not, write to the Free Software
22380 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22381 + */
22382 +
22383 +/*
22384 + * pseudo-link
22385 + */
22386 +
22387 +#include "aufs.h"
22388 +
22389 +/*
22390 + * the pseudo-link maintenance mode.
22391 + * during a user process maintains the pseudo-links,
22392 + * prohibit adding a new plink and branch manipulation.
22393 + *
22394 + * Flags
22395 + * NOPLM:
22396 + *     For entry functions which will handle plink, and i_mutex is already held
22397 + *     in VFS.
22398 + *     They cannot wait and should return an error at once.
22399 + *     Callers has to check the error.
22400 + * NOPLMW:
22401 + *     For entry functions which will handle plink, but i_mutex is not held
22402 + *     in VFS.
22403 + *     They can wait the plink maintenance mode to finish.
22404 + *
22405 + * They behave like F_SETLK and F_SETLKW.
22406 + * If the caller never handle plink, then both flags are unnecessary.
22407 + */
22408 +
22409 +int au_plink_maint(struct super_block *sb, int flags)
22410 +{
22411 +       int err;
22412 +       pid_t pid, ppid;
22413 +       struct au_sbinfo *sbi;
22414 +
22415 +       SiMustAnyLock(sb);
22416 +
22417 +       err = 0;
22418 +       if (!au_opt_test(au_mntflags(sb), PLINK))
22419 +               goto out;
22420 +
22421 +       sbi = au_sbi(sb);
22422 +       pid = sbi->si_plink_maint_pid;
22423 +       if (!pid || pid == current->pid)
22424 +               goto out;
22425 +
22426 +       /* todo: it highly depends upon /sbin/mount.aufs */
22427 +       rcu_read_lock();
22428 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
22429 +       rcu_read_unlock();
22430 +       if (pid == ppid)
22431 +               goto out;
22432 +
22433 +       if (au_ftest_lock(flags, NOPLMW)) {
22434 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
22435 +               /* AuDebugOn(!lockdep_depth(current)); */
22436 +               while (sbi->si_plink_maint_pid) {
22437 +                       si_read_unlock(sb);
22438 +                       /* gave up wake_up_bit() */
22439 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
22440 +
22441 +                       if (au_ftest_lock(flags, FLUSH))
22442 +                               au_nwt_flush(&sbi->si_nowait);
22443 +                       si_noflush_read_lock(sb);
22444 +               }
22445 +       } else if (au_ftest_lock(flags, NOPLM)) {
22446 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
22447 +               err = -EAGAIN;
22448 +       }
22449 +
22450 +out:
22451 +       return err;
22452 +}
22453 +
22454 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
22455 +{
22456 +       spin_lock(&sbinfo->si_plink_maint_lock);
22457 +       sbinfo->si_plink_maint_pid = 0;
22458 +       spin_unlock(&sbinfo->si_plink_maint_lock);
22459 +       wake_up_all(&sbinfo->si_plink_wq);
22460 +}
22461 +
22462 +int au_plink_maint_enter(struct super_block *sb)
22463 +{
22464 +       int err;
22465 +       struct au_sbinfo *sbinfo;
22466 +
22467 +       err = 0;
22468 +       sbinfo = au_sbi(sb);
22469 +       /* make sure i am the only one in this fs */
22470 +       si_write_lock(sb, AuLock_FLUSH);
22471 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
22472 +               spin_lock(&sbinfo->si_plink_maint_lock);
22473 +               if (!sbinfo->si_plink_maint_pid)
22474 +                       sbinfo->si_plink_maint_pid = current->pid;
22475 +               else
22476 +                       err = -EBUSY;
22477 +               spin_unlock(&sbinfo->si_plink_maint_lock);
22478 +       }
22479 +       si_write_unlock(sb);
22480 +
22481 +       return err;
22482 +}
22483 +
22484 +/* ---------------------------------------------------------------------- */
22485 +
22486 +#ifdef CONFIG_AUFS_DEBUG
22487 +void au_plink_list(struct super_block *sb)
22488 +{
22489 +       int i;
22490 +       struct au_sbinfo *sbinfo;
22491 +       struct hlist_head *plink_hlist;
22492 +       struct pseudo_link *plink;
22493 +
22494 +       SiMustAnyLock(sb);
22495 +
22496 +       sbinfo = au_sbi(sb);
22497 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
22498 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
22499 +
22500 +       for (i = 0; i < AuPlink_NHASH; i++) {
22501 +               plink_hlist = &sbinfo->si_plink[i].head;
22502 +               rcu_read_lock();
22503 +               hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
22504 +                       AuDbg("%lu\n", plink->inode->i_ino);
22505 +               rcu_read_unlock();
22506 +       }
22507 +}
22508 +#endif
22509 +
22510 +/* is the inode pseudo-linked? */
22511 +int au_plink_test(struct inode *inode)
22512 +{
22513 +       int found, i;
22514 +       struct au_sbinfo *sbinfo;
22515 +       struct hlist_head *plink_hlist;
22516 +       struct pseudo_link *plink;
22517 +
22518 +       sbinfo = au_sbi(inode->i_sb);
22519 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
22520 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
22521 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
22522 +
22523 +       found = 0;
22524 +       i = au_plink_hash(inode->i_ino);
22525 +       plink_hlist = &sbinfo->si_plink[i].head;
22526 +       rcu_read_lock();
22527 +       hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
22528 +               if (plink->inode == inode) {
22529 +                       found = 1;
22530 +                       break;
22531 +               }
22532 +       rcu_read_unlock();
22533 +       return found;
22534 +}
22535 +
22536 +/* ---------------------------------------------------------------------- */
22537 +
22538 +/*
22539 + * generate a name for plink.
22540 + * the file will be stored under AUFS_WH_PLINKDIR.
22541 + */
22542 +/* 20 is max digits length of ulong 64 */
22543 +#define PLINK_NAME_LEN ((20 + 1) * 2)
22544 +
22545 +static int plink_name(char *name, int len, struct inode *inode,
22546 +                     aufs_bindex_t bindex)
22547 +{
22548 +       int rlen;
22549 +       struct inode *h_inode;
22550 +
22551 +       h_inode = au_h_iptr(inode, bindex);
22552 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
22553 +       return rlen;
22554 +}
22555 +
22556 +struct au_do_plink_lkup_args {
22557 +       struct dentry **errp;
22558 +       struct qstr *tgtname;
22559 +       struct dentry *h_parent;
22560 +       struct au_branch *br;
22561 +};
22562 +
22563 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
22564 +                                      struct dentry *h_parent,
22565 +                                      struct au_branch *br)
22566 +{
22567 +       struct dentry *h_dentry;
22568 +       struct mutex *h_mtx;
22569 +
22570 +       h_mtx = &h_parent->d_inode->i_mutex;
22571 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
22572 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
22573 +       mutex_unlock(h_mtx);
22574 +       return h_dentry;
22575 +}
22576 +
22577 +static void au_call_do_plink_lkup(void *args)
22578 +{
22579 +       struct au_do_plink_lkup_args *a = args;
22580 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
22581 +}
22582 +
22583 +/* lookup the plink-ed @inode under the branch at @bindex */
22584 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
22585 +{
22586 +       struct dentry *h_dentry, *h_parent;
22587 +       struct au_branch *br;
22588 +       struct inode *h_dir;
22589 +       int wkq_err;
22590 +       char a[PLINK_NAME_LEN];
22591 +       struct qstr tgtname = QSTR_INIT(a, 0);
22592 +
22593 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
22594 +
22595 +       br = au_sbr(inode->i_sb, bindex);
22596 +       h_parent = br->br_wbr->wbr_plink;
22597 +       h_dir = h_parent->d_inode;
22598 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
22599 +
22600 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
22601 +               struct au_do_plink_lkup_args args = {
22602 +                       .errp           = &h_dentry,
22603 +                       .tgtname        = &tgtname,
22604 +                       .h_parent       = h_parent,
22605 +                       .br             = br
22606 +               };
22607 +
22608 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
22609 +               if (unlikely(wkq_err))
22610 +                       h_dentry = ERR_PTR(wkq_err);
22611 +       } else
22612 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
22613 +
22614 +       return h_dentry;
22615 +}
22616 +
22617 +/* create a pseudo-link */
22618 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
22619 +                     struct dentry *h_dentry, struct au_branch *br)
22620 +{
22621 +       int err;
22622 +       struct path h_path = {
22623 +               .mnt = au_br_mnt(br)
22624 +       };
22625 +       struct inode *h_dir;
22626 +
22627 +       h_dir = h_parent->d_inode;
22628 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
22629 +again:
22630 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
22631 +       err = PTR_ERR(h_path.dentry);
22632 +       if (IS_ERR(h_path.dentry))
22633 +               goto out;
22634 +
22635 +       err = 0;
22636 +       /* wh.plink dir is not monitored */
22637 +       /* todo: is it really safe? */
22638 +       if (h_path.dentry->d_inode
22639 +           && h_path.dentry->d_inode != h_dentry->d_inode) {
22640 +               err = vfsub_unlink(h_dir, &h_path, /*force*/0);
22641 +               dput(h_path.dentry);
22642 +               h_path.dentry = NULL;
22643 +               if (!err)
22644 +                       goto again;
22645 +       }
22646 +       if (!err && !h_path.dentry->d_inode)
22647 +               err = vfsub_link(h_dentry, h_dir, &h_path);
22648 +       dput(h_path.dentry);
22649 +
22650 +out:
22651 +       mutex_unlock(&h_dir->i_mutex);
22652 +       return err;
22653 +}
22654 +
22655 +struct do_whplink_args {
22656 +       int *errp;
22657 +       struct qstr *tgt;
22658 +       struct dentry *h_parent;
22659 +       struct dentry *h_dentry;
22660 +       struct au_branch *br;
22661 +};
22662 +
22663 +static void call_do_whplink(void *args)
22664 +{
22665 +       struct do_whplink_args *a = args;
22666 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
22667 +}
22668 +
22669 +static int whplink(struct dentry *h_dentry, struct inode *inode,
22670 +                  aufs_bindex_t bindex, struct au_branch *br)
22671 +{
22672 +       int err, wkq_err;
22673 +       struct au_wbr *wbr;
22674 +       struct dentry *h_parent;
22675 +       struct inode *h_dir;
22676 +       char a[PLINK_NAME_LEN];
22677 +       struct qstr tgtname = QSTR_INIT(a, 0);
22678 +
22679 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
22680 +       h_parent = wbr->wbr_plink;
22681 +       h_dir = h_parent->d_inode;
22682 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
22683 +
22684 +       /* always superio. */
22685 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
22686 +               struct do_whplink_args args = {
22687 +                       .errp           = &err,
22688 +                       .tgt            = &tgtname,
22689 +                       .h_parent       = h_parent,
22690 +                       .h_dentry       = h_dentry,
22691 +                       .br             = br
22692 +               };
22693 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
22694 +               if (unlikely(wkq_err))
22695 +                       err = wkq_err;
22696 +       } else
22697 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
22698 +
22699 +       return err;
22700 +}
22701 +
22702 +/* free a single plink */
22703 +static void do_put_plink(struct pseudo_link *plink, int do_del)
22704 +{
22705 +       if (do_del)
22706 +               hlist_del(&plink->hlist);
22707 +       iput(plink->inode);
22708 +       kfree(plink);
22709 +}
22710 +
22711 +static void do_put_plink_rcu(struct rcu_head *rcu)
22712 +{
22713 +       struct pseudo_link *plink;
22714 +
22715 +       plink = container_of(rcu, struct pseudo_link, rcu);
22716 +       iput(plink->inode);
22717 +       kfree(plink);
22718 +}
22719 +
22720 +/*
22721 + * create a new pseudo-link for @h_dentry on @bindex.
22722 + * the linked inode is held in aufs @inode.
22723 + */
22724 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
22725 +                    struct dentry *h_dentry)
22726 +{
22727 +       struct super_block *sb;
22728 +       struct au_sbinfo *sbinfo;
22729 +       struct hlist_head *plink_hlist;
22730 +       struct pseudo_link *plink, *tmp;
22731 +       struct au_sphlhead *sphl;
22732 +       int found, err, cnt, i;
22733 +
22734 +       sb = inode->i_sb;
22735 +       sbinfo = au_sbi(sb);
22736 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
22737 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
22738 +
22739 +       found = au_plink_test(inode);
22740 +       if (found)
22741 +               return;
22742 +
22743 +       i = au_plink_hash(inode->i_ino);
22744 +       sphl = sbinfo->si_plink + i;
22745 +       plink_hlist = &sphl->head;
22746 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
22747 +       if (tmp)
22748 +               tmp->inode = au_igrab(inode);
22749 +       else {
22750 +               err = -ENOMEM;
22751 +               goto out;
22752 +       }
22753 +
22754 +       spin_lock(&sphl->spin);
22755 +       hlist_for_each_entry(plink, plink_hlist, hlist) {
22756 +               if (plink->inode == inode) {
22757 +                       found = 1;
22758 +                       break;
22759 +               }
22760 +       }
22761 +       if (!found)
22762 +               hlist_add_head_rcu(&tmp->hlist, plink_hlist);
22763 +       spin_unlock(&sphl->spin);
22764 +       if (!found) {
22765 +               cnt = au_sphl_count(sphl);
22766 +#define msg "unexpectedly unblanced or too many pseudo-links"
22767 +               if (cnt > AUFS_PLINK_WARN)
22768 +                       AuWarn1(msg ", %d\n", cnt);
22769 +#undef msg
22770 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
22771 +       } else {
22772 +               do_put_plink(tmp, 0);
22773 +               return;
22774 +       }
22775 +
22776 +out:
22777 +       if (unlikely(err)) {
22778 +               pr_warn("err %d, damaged pseudo link.\n", err);
22779 +               if (tmp) {
22780 +                       au_sphl_del_rcu(&tmp->hlist, sphl);
22781 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
22782 +               }
22783 +       }
22784 +}
22785 +
22786 +/* free all plinks */
22787 +void au_plink_put(struct super_block *sb, int verbose)
22788 +{
22789 +       int i, warned;
22790 +       struct au_sbinfo *sbinfo;
22791 +       struct hlist_head *plink_hlist;
22792 +       struct hlist_node *tmp;
22793 +       struct pseudo_link *plink;
22794 +
22795 +       SiMustWriteLock(sb);
22796 +
22797 +       sbinfo = au_sbi(sb);
22798 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
22799 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
22800 +
22801 +       /* no spin_lock since sbinfo is write-locked */
22802 +       warned = 0;
22803 +       for (i = 0; i < AuPlink_NHASH; i++) {
22804 +               plink_hlist = &sbinfo->si_plink[i].head;
22805 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
22806 +                       pr_warn("pseudo-link is not flushed");
22807 +                       warned = 1;
22808 +               }
22809 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
22810 +                       do_put_plink(plink, 0);
22811 +               INIT_HLIST_HEAD(plink_hlist);
22812 +       }
22813 +}
22814 +
22815 +void au_plink_clean(struct super_block *sb, int verbose)
22816 +{
22817 +       struct dentry *root;
22818 +
22819 +       root = sb->s_root;
22820 +       aufs_write_lock(root);
22821 +       if (au_opt_test(au_mntflags(sb), PLINK))
22822 +               au_plink_put(sb, verbose);
22823 +       aufs_write_unlock(root);
22824 +}
22825 +
22826 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
22827 +{
22828 +       int do_put;
22829 +       aufs_bindex_t bstart, bend, bindex;
22830 +
22831 +       do_put = 0;
22832 +       bstart = au_ibstart(inode);
22833 +       bend = au_ibend(inode);
22834 +       if (bstart >= 0) {
22835 +               for (bindex = bstart; bindex <= bend; bindex++) {
22836 +                       if (!au_h_iptr(inode, bindex)
22837 +                           || au_ii_br_id(inode, bindex) != br_id)
22838 +                               continue;
22839 +                       au_set_h_iptr(inode, bindex, NULL, 0);
22840 +                       do_put = 1;
22841 +                       break;
22842 +               }
22843 +               if (do_put)
22844 +                       for (bindex = bstart; bindex <= bend; bindex++)
22845 +                               if (au_h_iptr(inode, bindex)) {
22846 +                                       do_put = 0;
22847 +                                       break;
22848 +                               }
22849 +       } else
22850 +               do_put = 1;
22851 +
22852 +       return do_put;
22853 +}
22854 +
22855 +/* free the plinks on a branch specified by @br_id */
22856 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
22857 +{
22858 +       struct au_sbinfo *sbinfo;
22859 +       struct hlist_head *plink_hlist;
22860 +       struct hlist_node *tmp;
22861 +       struct pseudo_link *plink;
22862 +       struct inode *inode;
22863 +       int i, do_put;
22864 +
22865 +       SiMustWriteLock(sb);
22866 +
22867 +       sbinfo = au_sbi(sb);
22868 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
22869 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
22870 +
22871 +       /* no spin_lock since sbinfo is write-locked */
22872 +       for (i = 0; i < AuPlink_NHASH; i++) {
22873 +               plink_hlist = &sbinfo->si_plink[i].head;
22874 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
22875 +                       inode = au_igrab(plink->inode);
22876 +                       ii_write_lock_child(inode);
22877 +                       do_put = au_plink_do_half_refresh(inode, br_id);
22878 +                       if (do_put)
22879 +                               do_put_plink(plink, 1);
22880 +                       ii_write_unlock(inode);
22881 +                       iput(inode);
22882 +               }
22883 +       }
22884 +}
22885 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
22886 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
22887 +++ linux/fs/aufs/poll.c        2013-07-06 13:20:47.753531903 +0200
22888 @@ -0,0 +1,56 @@
22889 +/*
22890 + * Copyright (C) 2005-2013 Junjiro R. Okajima
22891 + *
22892 + * This program, aufs is free software; you can redistribute it and/or modify
22893 + * it under the terms of the GNU General Public License as published by
22894 + * the Free Software Foundation; either version 2 of the License, or
22895 + * (at your option) any later version.
22896 + *
22897 + * This program is distributed in the hope that it will be useful,
22898 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22899 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22900 + * GNU General Public License for more details.
22901 + *
22902 + * You should have received a copy of the GNU General Public License
22903 + * along with this program; if not, write to the Free Software
22904 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22905 + */
22906 +
22907 +/*
22908 + * poll operation
22909 + * There is only one filesystem which implements ->poll operation, currently.
22910 + */
22911 +
22912 +#include "aufs.h"
22913 +
22914 +unsigned int aufs_poll(struct file *file, poll_table *wait)
22915 +{
22916 +       unsigned int mask;
22917 +       int err;
22918 +       struct file *h_file;
22919 +       struct dentry *dentry;
22920 +       struct super_block *sb;
22921 +
22922 +       /* We should pretend an error happened. */
22923 +       mask = POLLERR /* | POLLIN | POLLOUT */;
22924 +       dentry = file->f_dentry;
22925 +       sb = dentry->d_sb;
22926 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
22927 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
22928 +       if (unlikely(err))
22929 +               goto out;
22930 +
22931 +       /* it is not an error if h_file has no operation */
22932 +       mask = DEFAULT_POLLMASK;
22933 +       h_file = au_hf_top(file);
22934 +       if (h_file->f_op && h_file->f_op->poll)
22935 +               mask = h_file->f_op->poll(h_file, wait);
22936 +
22937 +       di_read_unlock(dentry, AuLock_IR);
22938 +       fi_read_unlock(file);
22939 +
22940 +out:
22941 +       si_read_unlock(sb);
22942 +       AuTraceErr((int)mask);
22943 +       return mask;
22944 +}
22945 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
22946 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
22947 +++ linux/fs/aufs/procfs.c      2013-07-06 13:20:47.753531903 +0200
22948 @@ -0,0 +1,170 @@
22949 +/*
22950 + * Copyright (C) 2010-2013 Junjiro R. Okajima
22951 + *
22952 + * This program, aufs is free software; you can redistribute it and/or modify
22953 + * it under the terms of the GNU General Public License as published by
22954 + * the Free Software Foundation; either version 2 of the License, or
22955 + * (at your option) any later version.
22956 + *
22957 + * This program is distributed in the hope that it will be useful,
22958 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22959 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22960 + * GNU General Public License for more details.
22961 + *
22962 + * You should have received a copy of the GNU General Public License
22963 + * along with this program; if not, write to the Free Software
22964 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22965 + */
22966 +
22967 +/*
22968 + * procfs interfaces
22969 + */
22970 +
22971 +#include <linux/proc_fs.h>
22972 +#include "aufs.h"
22973 +
22974 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
22975 +{
22976 +       struct au_sbinfo *sbinfo;
22977 +
22978 +       sbinfo = file->private_data;
22979 +       if (sbinfo) {
22980 +               au_plink_maint_leave(sbinfo);
22981 +               kobject_put(&sbinfo->si_kobj);
22982 +       }
22983 +
22984 +       return 0;
22985 +}
22986 +
22987 +static void au_procfs_plm_write_clean(struct file *file)
22988 +{
22989 +       struct au_sbinfo *sbinfo;
22990 +
22991 +       sbinfo = file->private_data;
22992 +       if (sbinfo)
22993 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
22994 +}
22995 +
22996 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
22997 +{
22998 +       int err;
22999 +       struct super_block *sb;
23000 +       struct au_sbinfo *sbinfo;
23001 +
23002 +       err = -EBUSY;
23003 +       if (unlikely(file->private_data))
23004 +               goto out;
23005 +
23006 +       sb = NULL;
23007 +       /* don't use au_sbilist_lock() here */
23008 +       spin_lock(&au_sbilist.spin);
23009 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
23010 +               if (id == sysaufs_si_id(sbinfo)) {
23011 +                       kobject_get(&sbinfo->si_kobj);
23012 +                       sb = sbinfo->si_sb;
23013 +                       break;
23014 +               }
23015 +       spin_unlock(&au_sbilist.spin);
23016 +
23017 +       err = -EINVAL;
23018 +       if (unlikely(!sb))
23019 +               goto out;
23020 +
23021 +       err = au_plink_maint_enter(sb);
23022 +       if (!err)
23023 +               /* keep kobject_get() */
23024 +               file->private_data = sbinfo;
23025 +       else
23026 +               kobject_put(&sbinfo->si_kobj);
23027 +out:
23028 +       return err;
23029 +}
23030 +
23031 +/*
23032 + * Accept a valid "si=xxxx" only.
23033 + * Once it is accepted successfully, accept "clean" too.
23034 + */
23035 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
23036 +                                  size_t count, loff_t *ppos)
23037 +{
23038 +       ssize_t err;
23039 +       unsigned long id;
23040 +       /* last newline is allowed */
23041 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
23042 +
23043 +       err = -EACCES;
23044 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
23045 +               goto out;
23046 +
23047 +       err = -EINVAL;
23048 +       if (unlikely(count > sizeof(buf)))
23049 +               goto out;
23050 +
23051 +       err = copy_from_user(buf, ubuf, count);
23052 +       if (unlikely(err)) {
23053 +               err = -EFAULT;
23054 +               goto out;
23055 +       }
23056 +       buf[count] = 0;
23057 +
23058 +       err = -EINVAL;
23059 +       if (!strcmp("clean", buf)) {
23060 +               au_procfs_plm_write_clean(file);
23061 +               goto out_success;
23062 +       } else if (unlikely(strncmp("si=", buf, 3)))
23063 +               goto out;
23064 +
23065 +       err = kstrtoul(buf + 3, 16, &id);
23066 +       if (unlikely(err))
23067 +               goto out;
23068 +
23069 +       err = au_procfs_plm_write_si(file, id);
23070 +       if (unlikely(err))
23071 +               goto out;
23072 +
23073 +out_success:
23074 +       err = count; /* success */
23075 +out:
23076 +       return err;
23077 +}
23078 +
23079 +static const struct file_operations au_procfs_plm_fop = {
23080 +       .write          = au_procfs_plm_write,
23081 +       .release        = au_procfs_plm_release,
23082 +       .owner          = THIS_MODULE
23083 +};
23084 +
23085 +/* ---------------------------------------------------------------------- */
23086 +
23087 +static struct proc_dir_entry *au_procfs_dir;
23088 +
23089 +void au_procfs_fin(void)
23090 +{
23091 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
23092 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
23093 +}
23094 +
23095 +int __init au_procfs_init(void)
23096 +{
23097 +       int err;
23098 +       struct proc_dir_entry *entry;
23099 +
23100 +       err = -ENOMEM;
23101 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
23102 +       if (unlikely(!au_procfs_dir))
23103 +               goto out;
23104 +
23105 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
23106 +                           au_procfs_dir, &au_procfs_plm_fop);
23107 +       if (unlikely(!entry))
23108 +               goto out_dir;
23109 +
23110 +       err = 0;
23111 +       goto out; /* success */
23112 +
23113 +
23114 +out_dir:
23115 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
23116 +out:
23117 +       return err;
23118 +}
23119 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
23120 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
23121 +++ linux/fs/aufs/rdu.c 2013-07-30 22:42:55.842946719 +0200
23122 @@ -0,0 +1,384 @@
23123 +/*
23124 + * Copyright (C) 2005-2013 Junjiro R. Okajima
23125 + *
23126 + * This program, aufs is free software; you can redistribute it and/or modify
23127 + * it under the terms of the GNU General Public License as published by
23128 + * the Free Software Foundation; either version 2 of the License, or
23129 + * (at your option) any later version.
23130 + *
23131 + * This program is distributed in the hope that it will be useful,
23132 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23133 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23134 + * GNU General Public License for more details.
23135 + *
23136 + * You should have received a copy of the GNU General Public License
23137 + * along with this program; if not, write to the Free Software
23138 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23139 + */
23140 +
23141 +/*
23142 + * readdir in userspace.
23143 + */
23144 +
23145 +#include <linux/compat.h>
23146 +#include <linux/fs_stack.h>
23147 +#include <linux/security.h>
23148 +#include "aufs.h"
23149 +
23150 +/* bits for struct aufs_rdu.flags */
23151 +#define        AuRdu_CALLED    1
23152 +#define        AuRdu_CONT      (1 << 1)
23153 +#define        AuRdu_FULL      (1 << 2)
23154 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
23155 +#define au_fset_rdu(flags, name) \
23156 +       do { (flags) |= AuRdu_##name; } while (0)
23157 +#define au_fclr_rdu(flags, name) \
23158 +       do { (flags) &= ~AuRdu_##name; } while (0)
23159 +
23160 +struct au_rdu_arg {
23161 +       struct aufs_rdu                 *rdu;
23162 +       union au_rdu_ent_ul             ent;
23163 +       unsigned long                   end;
23164 +
23165 +       struct super_block              *sb;
23166 +       int                             err;
23167 +};
23168 +
23169 +static int au_rdu_fill(void *__arg, const char *name, int nlen,
23170 +                      loff_t offset, u64 h_ino, unsigned int d_type)
23171 +{
23172 +       int err, len;
23173 +       struct au_rdu_arg *arg = __arg;
23174 +       struct aufs_rdu *rdu = arg->rdu;
23175 +       struct au_rdu_ent ent;
23176 +
23177 +       err = 0;
23178 +       arg->err = 0;
23179 +       au_fset_rdu(rdu->cookie.flags, CALLED);
23180 +       len = au_rdu_len(nlen);
23181 +       if (arg->ent.ul + len  < arg->end) {
23182 +               ent.ino = h_ino;
23183 +               ent.bindex = rdu->cookie.bindex;
23184 +               ent.type = d_type;
23185 +               ent.nlen = nlen;
23186 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
23187 +                       ent.type = DT_UNKNOWN;
23188 +
23189 +               /* unnecessary to support mmap_sem since this is a dir */
23190 +               err = -EFAULT;
23191 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
23192 +                       goto out;
23193 +               if (copy_to_user(arg->ent.e->name, name, nlen))
23194 +                       goto out;
23195 +               /* the terminating NULL */
23196 +               if (__put_user(0, arg->ent.e->name + nlen))
23197 +                       goto out;
23198 +               err = 0;
23199 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
23200 +               arg->ent.ul += len;
23201 +               rdu->rent++;
23202 +       } else {
23203 +               err = -EFAULT;
23204 +               au_fset_rdu(rdu->cookie.flags, FULL);
23205 +               rdu->full = 1;
23206 +               rdu->tail = arg->ent;
23207 +       }
23208 +
23209 +out:
23210 +       /* AuTraceErr(err); */
23211 +       return err;
23212 +}
23213 +
23214 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
23215 +{
23216 +       int err;
23217 +       loff_t offset;
23218 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
23219 +
23220 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
23221 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
23222 +       err = offset;
23223 +       if (unlikely(offset != cookie->h_pos))
23224 +               goto out;
23225 +
23226 +       err = 0;
23227 +       do {
23228 +               arg->err = 0;
23229 +               au_fclr_rdu(cookie->flags, CALLED);
23230 +               /* smp_mb(); */
23231 +               err = vfsub_readdir(h_file, au_rdu_fill, arg);
23232 +               if (err >= 0)
23233 +                       err = arg->err;
23234 +       } while (!err
23235 +                && au_ftest_rdu(cookie->flags, CALLED)
23236 +                && !au_ftest_rdu(cookie->flags, FULL));
23237 +       cookie->h_pos = h_file->f_pos;
23238 +
23239 +out:
23240 +       AuTraceErr(err);
23241 +       return err;
23242 +}
23243 +
23244 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
23245 +{
23246 +       int err;
23247 +       aufs_bindex_t bend;
23248 +       struct au_rdu_arg arg;
23249 +       struct dentry *dentry;
23250 +       struct inode *inode;
23251 +       struct file *h_file;
23252 +       struct au_rdu_cookie *cookie = &rdu->cookie;
23253 +
23254 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
23255 +       if (unlikely(err)) {
23256 +               err = -EFAULT;
23257 +               AuTraceErr(err);
23258 +               goto out;
23259 +       }
23260 +       rdu->rent = 0;
23261 +       rdu->tail = rdu->ent;
23262 +       rdu->full = 0;
23263 +       arg.rdu = rdu;
23264 +       arg.ent = rdu->ent;
23265 +       arg.end = arg.ent.ul;
23266 +       arg.end += rdu->sz;
23267 +
23268 +       err = -ENOTDIR;
23269 +       if (unlikely(!file->f_op || !file->f_op->readdir))
23270 +               goto out;
23271 +
23272 +       err = security_file_permission(file, MAY_READ);
23273 +       AuTraceErr(err);
23274 +       if (unlikely(err))
23275 +               goto out;
23276 +
23277 +       dentry = file->f_dentry;
23278 +       inode = dentry->d_inode;
23279 +#if 1
23280 +       mutex_lock(&inode->i_mutex);
23281 +#else
23282 +       err = mutex_lock_killable(&inode->i_mutex);
23283 +       AuTraceErr(err);
23284 +       if (unlikely(err))
23285 +               goto out;
23286 +#endif
23287 +
23288 +       arg.sb = inode->i_sb;
23289 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
23290 +       if (unlikely(err))
23291 +               goto out_mtx;
23292 +       err = au_alive_dir(dentry);
23293 +       if (unlikely(err))
23294 +               goto out_si;
23295 +       /* todo: reval? */
23296 +       fi_read_lock(file);
23297 +
23298 +       err = -EAGAIN;
23299 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
23300 +                    && cookie->generation != au_figen(file)))
23301 +               goto out_unlock;
23302 +
23303 +       err = 0;
23304 +       if (!rdu->blk) {
23305 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
23306 +               if (!rdu->blk)
23307 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
23308 +       }
23309 +       bend = au_fbstart(file);
23310 +       if (cookie->bindex < bend)
23311 +               cookie->bindex = bend;
23312 +       bend = au_fbend_dir(file);
23313 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
23314 +       for (; !err && cookie->bindex <= bend;
23315 +            cookie->bindex++, cookie->h_pos = 0) {
23316 +               h_file = au_hf_dir(file, cookie->bindex);
23317 +               if (!h_file)
23318 +                       continue;
23319 +
23320 +               au_fclr_rdu(cookie->flags, FULL);
23321 +               err = au_rdu_do(h_file, &arg);
23322 +               AuTraceErr(err);
23323 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
23324 +                       break;
23325 +       }
23326 +       AuDbg("rent %llu\n", rdu->rent);
23327 +
23328 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
23329 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
23330 +               au_fset_rdu(cookie->flags, CONT);
23331 +               cookie->generation = au_figen(file);
23332 +       }
23333 +
23334 +       ii_read_lock_child(inode);
23335 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
23336 +       ii_read_unlock(inode);
23337 +
23338 +out_unlock:
23339 +       fi_read_unlock(file);
23340 +out_si:
23341 +       si_read_unlock(arg.sb);
23342 +out_mtx:
23343 +       mutex_unlock(&inode->i_mutex);
23344 +out:
23345 +       AuTraceErr(err);
23346 +       return err;
23347 +}
23348 +
23349 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
23350 +{
23351 +       int err;
23352 +       ino_t ino;
23353 +       unsigned long long nent;
23354 +       union au_rdu_ent_ul *u;
23355 +       struct au_rdu_ent ent;
23356 +       struct super_block *sb;
23357 +
23358 +       err = 0;
23359 +       nent = rdu->nent;
23360 +       u = &rdu->ent;
23361 +       sb = file->f_dentry->d_sb;
23362 +       si_read_lock(sb, AuLock_FLUSH);
23363 +       while (nent-- > 0) {
23364 +               /* unnecessary to support mmap_sem since this is a dir */
23365 +               err = copy_from_user(&ent, u->e, sizeof(ent));
23366 +               if (!err)
23367 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
23368 +               if (unlikely(err)) {
23369 +                       err = -EFAULT;
23370 +                       AuTraceErr(err);
23371 +                       break;
23372 +               }
23373 +
23374 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
23375 +               if (!ent.wh)
23376 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
23377 +               else
23378 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
23379 +                                       &ino);
23380 +               if (unlikely(err)) {
23381 +                       AuTraceErr(err);
23382 +                       break;
23383 +               }
23384 +
23385 +               err = __put_user(ino, &u->e->ino);
23386 +               if (unlikely(err)) {
23387 +                       err = -EFAULT;
23388 +                       AuTraceErr(err);
23389 +                       break;
23390 +               }
23391 +               u->ul += au_rdu_len(ent.nlen);
23392 +       }
23393 +       si_read_unlock(sb);
23394 +
23395 +       return err;
23396 +}
23397 +
23398 +/* ---------------------------------------------------------------------- */
23399 +
23400 +static int au_rdu_verify(struct aufs_rdu *rdu)
23401 +{
23402 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
23403 +             "%llu, b%d, 0x%x, g%u}\n",
23404 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
23405 +             rdu->blk,
23406 +             rdu->rent, rdu->shwh, rdu->full,
23407 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
23408 +             rdu->cookie.generation);
23409 +
23410 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
23411 +               return 0;
23412 +
23413 +       AuDbg("%u:%u\n",
23414 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
23415 +       return -EINVAL;
23416 +}
23417 +
23418 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
23419 +{
23420 +       long err, e;
23421 +       struct aufs_rdu rdu;
23422 +       void __user *p = (void __user *)arg;
23423 +
23424 +       err = copy_from_user(&rdu, p, sizeof(rdu));
23425 +       if (unlikely(err)) {
23426 +               err = -EFAULT;
23427 +               AuTraceErr(err);
23428 +               goto out;
23429 +       }
23430 +       err = au_rdu_verify(&rdu);
23431 +       if (unlikely(err))
23432 +               goto out;
23433 +
23434 +       switch (cmd) {
23435 +       case AUFS_CTL_RDU:
23436 +               err = au_rdu(file, &rdu);
23437 +               if (unlikely(err))
23438 +                       break;
23439 +
23440 +               e = copy_to_user(p, &rdu, sizeof(rdu));
23441 +               if (unlikely(e)) {
23442 +                       err = -EFAULT;
23443 +                       AuTraceErr(err);
23444 +               }
23445 +               break;
23446 +       case AUFS_CTL_RDU_INO:
23447 +               err = au_rdu_ino(file, &rdu);
23448 +               break;
23449 +
23450 +       default:
23451 +               /* err = -ENOTTY; */
23452 +               err = -EINVAL;
23453 +       }
23454 +
23455 +out:
23456 +       AuTraceErr(err);
23457 +       return err;
23458 +}
23459 +
23460 +#ifdef CONFIG_COMPAT
23461 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
23462 +{
23463 +       long err, e;
23464 +       struct aufs_rdu rdu;
23465 +       void __user *p = compat_ptr(arg);
23466 +
23467 +       /* todo: get_user()? */
23468 +       err = copy_from_user(&rdu, p, sizeof(rdu));
23469 +       if (unlikely(err)) {
23470 +               err = -EFAULT;
23471 +               AuTraceErr(err);
23472 +               goto out;
23473 +       }
23474 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
23475 +       err = au_rdu_verify(&rdu);
23476 +       if (unlikely(err))
23477 +               goto out;
23478 +
23479 +       switch (cmd) {
23480 +       case AUFS_CTL_RDU:
23481 +               err = au_rdu(file, &rdu);
23482 +               if (unlikely(err))
23483 +                       break;
23484 +
23485 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
23486 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
23487 +               e = copy_to_user(p, &rdu, sizeof(rdu));
23488 +               if (unlikely(e)) {
23489 +                       err = -EFAULT;
23490 +                       AuTraceErr(err);
23491 +               }
23492 +               break;
23493 +       case AUFS_CTL_RDU_INO:
23494 +               err = au_rdu_ino(file, &rdu);
23495 +               break;
23496 +
23497 +       default:
23498 +               /* err = -ENOTTY; */
23499 +               err = -EINVAL;
23500 +       }
23501 +
23502 +out:
23503 +       AuTraceErr(err);
23504 +       return err;
23505 +}
23506 +#endif
23507 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
23508 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
23509 +++ linux/fs/aufs/rwsem.h       2013-07-06 13:20:47.753531903 +0200
23510 @@ -0,0 +1,188 @@
23511 +/*
23512 + * Copyright (C) 2005-2013 Junjiro R. Okajima
23513 + *
23514 + * This program, aufs is free software; you can redistribute it and/or modify
23515 + * it under the terms of the GNU General Public License as published by
23516 + * the Free Software Foundation; either version 2 of the License, or
23517 + * (at your option) any later version.
23518 + *
23519 + * This program is distributed in the hope that it will be useful,
23520 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23521 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23522 + * GNU General Public License for more details.
23523 + *
23524 + * You should have received a copy of the GNU General Public License
23525 + * along with this program; if not, write to the Free Software
23526 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23527 + */
23528 +
23529 +/*
23530 + * simple read-write semaphore wrappers
23531 + */
23532 +
23533 +#ifndef __AUFS_RWSEM_H__
23534 +#define __AUFS_RWSEM_H__
23535 +
23536 +#ifdef __KERNEL__
23537 +
23538 +#include "debug.h"
23539 +
23540 +struct au_rwsem {
23541 +       struct rw_semaphore     rwsem;
23542 +#ifdef CONFIG_AUFS_DEBUG
23543 +       /* just for debugging, not almighty counter */
23544 +       atomic_t                rcnt, wcnt;
23545 +#endif
23546 +};
23547 +
23548 +#ifdef CONFIG_AUFS_DEBUG
23549 +#define AuDbgCntInit(rw) do { \
23550 +       atomic_set(&(rw)->rcnt, 0); \
23551 +       atomic_set(&(rw)->wcnt, 0); \
23552 +       smp_mb(); /* atomic set */ \
23553 +} while (0)
23554 +
23555 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
23556 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
23557 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
23558 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
23559 +#else
23560 +#define AuDbgCntInit(rw)       do {} while (0)
23561 +#define AuDbgRcntInc(rw)       do {} while (0)
23562 +#define AuDbgRcntDec(rw)       do {} while (0)
23563 +#define AuDbgWcntInc(rw)       do {} while (0)
23564 +#define AuDbgWcntDec(rw)       do {} while (0)
23565 +#endif /* CONFIG_AUFS_DEBUG */
23566 +
23567 +/* to debug easier, do not make them inlined functions */
23568 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
23569 +/* rwsem_is_locked() is unusable */
23570 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
23571 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
23572 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
23573 +                                       && atomic_read(&(rw)->wcnt) <= 0)
23574 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
23575 +                                       || atomic_read(&(rw)->wcnt))
23576 +
23577 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
23578 +
23579 +static inline void au_rw_init(struct au_rwsem *rw)
23580 +{
23581 +       AuDbgCntInit(rw);
23582 +       init_rwsem(&rw->rwsem);
23583 +}
23584 +
23585 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
23586 +{
23587 +       au_rw_init(rw);
23588 +       down_write(&rw->rwsem);
23589 +       AuDbgWcntInc(rw);
23590 +}
23591 +
23592 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
23593 +                                          unsigned int lsc)
23594 +{
23595 +       au_rw_init(rw);
23596 +       down_write_nested(&rw->rwsem, lsc);
23597 +       AuDbgWcntInc(rw);
23598 +}
23599 +
23600 +static inline void au_rw_read_lock(struct au_rwsem *rw)
23601 +{
23602 +       down_read(&rw->rwsem);
23603 +       AuDbgRcntInc(rw);
23604 +}
23605 +
23606 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
23607 +{
23608 +       down_read_nested(&rw->rwsem, lsc);
23609 +       AuDbgRcntInc(rw);
23610 +}
23611 +
23612 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
23613 +{
23614 +       AuRwMustReadLock(rw);
23615 +       AuDbgRcntDec(rw);
23616 +       up_read(&rw->rwsem);
23617 +}
23618 +
23619 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
23620 +{
23621 +       AuRwMustWriteLock(rw);
23622 +       AuDbgRcntInc(rw);
23623 +       AuDbgWcntDec(rw);
23624 +       downgrade_write(&rw->rwsem);
23625 +}
23626 +
23627 +static inline void au_rw_write_lock(struct au_rwsem *rw)
23628 +{
23629 +       down_write(&rw->rwsem);
23630 +       AuDbgWcntInc(rw);
23631 +}
23632 +
23633 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
23634 +                                          unsigned int lsc)
23635 +{
23636 +       down_write_nested(&rw->rwsem, lsc);
23637 +       AuDbgWcntInc(rw);
23638 +}
23639 +
23640 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
23641 +{
23642 +       AuRwMustWriteLock(rw);
23643 +       AuDbgWcntDec(rw);
23644 +       up_write(&rw->rwsem);
23645 +}
23646 +
23647 +/* why is not _nested version defined */
23648 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
23649 +{
23650 +       int ret = down_read_trylock(&rw->rwsem);
23651 +       if (ret)
23652 +               AuDbgRcntInc(rw);
23653 +       return ret;
23654 +}
23655 +
23656 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
23657 +{
23658 +       int ret = down_write_trylock(&rw->rwsem);
23659 +       if (ret)
23660 +               AuDbgWcntInc(rw);
23661 +       return ret;
23662 +}
23663 +
23664 +#undef AuDbgCntInit
23665 +#undef AuDbgRcntInc
23666 +#undef AuDbgRcntDec
23667 +#undef AuDbgWcntInc
23668 +#undef AuDbgWcntDec
23669 +
23670 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
23671 +static inline void prefix##_read_lock(param) \
23672 +{ au_rw_read_lock(rwsem); } \
23673 +static inline void prefix##_write_lock(param) \
23674 +{ au_rw_write_lock(rwsem); } \
23675 +static inline int prefix##_read_trylock(param) \
23676 +{ return au_rw_read_trylock(rwsem); } \
23677 +static inline int prefix##_write_trylock(param) \
23678 +{ return au_rw_write_trylock(rwsem); }
23679 +/* why is not _nested version defined */
23680 +/* static inline void prefix##_read_trylock_nested(param, lsc)
23681 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
23682 +static inline void prefix##_write_trylock_nestd(param, lsc)
23683 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
23684 +
23685 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
23686 +static inline void prefix##_read_unlock(param) \
23687 +{ au_rw_read_unlock(rwsem); } \
23688 +static inline void prefix##_write_unlock(param) \
23689 +{ au_rw_write_unlock(rwsem); } \
23690 +static inline void prefix##_downgrade_lock(param) \
23691 +{ au_rw_dgrade_lock(rwsem); }
23692 +
23693 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
23694 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
23695 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
23696 +
23697 +#endif /* __KERNEL__ */
23698 +#endif /* __AUFS_RWSEM_H__ */
23699 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
23700 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
23701 +++ linux/fs/aufs/sbinfo.c      2013-07-06 13:20:47.753531903 +0200
23702 @@ -0,0 +1,346 @@
23703 +/*
23704 + * Copyright (C) 2005-2013 Junjiro R. Okajima
23705 + *
23706 + * This program, aufs is free software; you can redistribute it and/or modify
23707 + * it under the terms of the GNU General Public License as published by
23708 + * the Free Software Foundation; either version 2 of the License, or
23709 + * (at your option) any later version.
23710 + *
23711 + * This program is distributed in the hope that it will be useful,
23712 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23713 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23714 + * GNU General Public License for more details.
23715 + *
23716 + * You should have received a copy of the GNU General Public License
23717 + * along with this program; if not, write to the Free Software
23718 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23719 + */
23720 +
23721 +/*
23722 + * superblock private data
23723 + */
23724 +
23725 +#include "aufs.h"
23726 +
23727 +/*
23728 + * they are necessary regardless sysfs is disabled.
23729 + */
23730 +void au_si_free(struct kobject *kobj)
23731 +{
23732 +       int i;
23733 +       struct au_sbinfo *sbinfo;
23734 +       char *locked __maybe_unused; /* debug only */
23735 +
23736 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
23737 +       for (i = 0; i < AuPlink_NHASH; i++)
23738 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
23739 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
23740 +
23741 +       au_rw_write_lock(&sbinfo->si_rwsem);
23742 +       au_br_free(sbinfo);
23743 +       au_rw_write_unlock(&sbinfo->si_rwsem);
23744 +
23745 +       AuDebugOn(radix_tree_gang_lookup
23746 +                 (&sbinfo->au_si_pid.tree, (void **)&locked,
23747 +                  /*first_index*/PID_MAX_DEFAULT - 1,
23748 +                  /*max_items*/sizeof(locked)/sizeof(*locked)));
23749 +
23750 +       kfree(sbinfo->si_branch);
23751 +       kfree(sbinfo->au_si_pid.bitmap);
23752 +       mutex_destroy(&sbinfo->si_xib_mtx);
23753 +       AuRwDestroy(&sbinfo->si_rwsem);
23754 +
23755 +       kfree(sbinfo);
23756 +}
23757 +
23758 +int au_si_alloc(struct super_block *sb)
23759 +{
23760 +       int err, i;
23761 +       struct au_sbinfo *sbinfo;
23762 +       static struct lock_class_key aufs_si;
23763 +
23764 +       err = -ENOMEM;
23765 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
23766 +       if (unlikely(!sbinfo))
23767 +               goto out;
23768 +
23769 +       BUILD_BUG_ON(sizeof(unsigned long) !=
23770 +                    sizeof(*sbinfo->au_si_pid.bitmap));
23771 +       sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
23772 +                                       sizeof(*sbinfo->au_si_pid.bitmap),
23773 +                                       GFP_NOFS);
23774 +       if (unlikely(!sbinfo->au_si_pid.bitmap))
23775 +               goto out_sbinfo;
23776 +
23777 +       /* will be reallocated separately */
23778 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
23779 +       if (unlikely(!sbinfo->si_branch))
23780 +               goto out_pidmap;
23781 +
23782 +       err = sysaufs_si_init(sbinfo);
23783 +       if (unlikely(err))
23784 +               goto out_br;
23785 +
23786 +       au_nwt_init(&sbinfo->si_nowait);
23787 +       au_rw_init_wlock(&sbinfo->si_rwsem);
23788 +       au_rw_class(&sbinfo->si_rwsem, &aufs_si);
23789 +       spin_lock_init(&sbinfo->au_si_pid.tree_lock);
23790 +       INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
23791 +
23792 +       atomic_long_set(&sbinfo->si_ninodes, 0);
23793 +       atomic_long_set(&sbinfo->si_nfiles, 0);
23794 +
23795 +       sbinfo->si_bend = -1;
23796 +
23797 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
23798 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
23799 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
23800 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
23801 +
23802 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
23803 +
23804 +       mutex_init(&sbinfo->si_xib_mtx);
23805 +       sbinfo->si_xino_brid = -1;
23806 +       /* leave si_xib_last_pindex and si_xib_next_bit */
23807 +
23808 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
23809 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
23810 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
23811 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
23812 +
23813 +       for (i = 0; i < AuPlink_NHASH; i++)
23814 +               au_sphl_init(sbinfo->si_plink + i);
23815 +       init_waitqueue_head(&sbinfo->si_plink_wq);
23816 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
23817 +
23818 +       /* leave other members for sysaufs and si_mnt. */
23819 +       sbinfo->si_sb = sb;
23820 +       sb->s_fs_info = sbinfo;
23821 +       si_pid_set(sb);
23822 +       au_debug_sbinfo_init(sbinfo);
23823 +       return 0; /* success */
23824 +
23825 +out_br:
23826 +       kfree(sbinfo->si_branch);
23827 +out_pidmap:
23828 +       kfree(sbinfo->au_si_pid.bitmap);
23829 +out_sbinfo:
23830 +       kfree(sbinfo);
23831 +out:
23832 +       return err;
23833 +}
23834 +
23835 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
23836 +{
23837 +       int err, sz;
23838 +       struct au_branch **brp;
23839 +
23840 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
23841 +
23842 +       err = -ENOMEM;
23843 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
23844 +       if (unlikely(!sz))
23845 +               sz = sizeof(*brp);
23846 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
23847 +       if (brp) {
23848 +               sbinfo->si_branch = brp;
23849 +               err = 0;
23850 +       }
23851 +
23852 +       return err;
23853 +}
23854 +
23855 +/* ---------------------------------------------------------------------- */
23856 +
23857 +unsigned int au_sigen_inc(struct super_block *sb)
23858 +{
23859 +       unsigned int gen;
23860 +
23861 +       SiMustWriteLock(sb);
23862 +
23863 +       gen = ++au_sbi(sb)->si_generation;
23864 +       au_update_digen(sb->s_root);
23865 +       au_update_iigen(sb->s_root->d_inode, /*half*/0);
23866 +       sb->s_root->d_inode->i_version++;
23867 +       return gen;
23868 +}
23869 +
23870 +aufs_bindex_t au_new_br_id(struct super_block *sb)
23871 +{
23872 +       aufs_bindex_t br_id;
23873 +       int i;
23874 +       struct au_sbinfo *sbinfo;
23875 +
23876 +       SiMustWriteLock(sb);
23877 +
23878 +       sbinfo = au_sbi(sb);
23879 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
23880 +               br_id = ++sbinfo->si_last_br_id;
23881 +               AuDebugOn(br_id < 0);
23882 +               if (br_id && au_br_index(sb, br_id) < 0)
23883 +                       return br_id;
23884 +       }
23885 +
23886 +       return -1;
23887 +}
23888 +
23889 +/* ---------------------------------------------------------------------- */
23890 +
23891 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
23892 +int si_read_lock(struct super_block *sb, int flags)
23893 +{
23894 +       int err;
23895 +
23896 +       err = 0;
23897 +       if (au_ftest_lock(flags, FLUSH))
23898 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
23899 +
23900 +       si_noflush_read_lock(sb);
23901 +       err = au_plink_maint(sb, flags);
23902 +       if (unlikely(err))
23903 +               si_read_unlock(sb);
23904 +
23905 +       return err;
23906 +}
23907 +
23908 +int si_write_lock(struct super_block *sb, int flags)
23909 +{
23910 +       int err;
23911 +
23912 +       if (au_ftest_lock(flags, FLUSH))
23913 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
23914 +
23915 +       si_noflush_write_lock(sb);
23916 +       err = au_plink_maint(sb, flags);
23917 +       if (unlikely(err))
23918 +               si_write_unlock(sb);
23919 +
23920 +       return err;
23921 +}
23922 +
23923 +/* dentry and super_block lock. call at entry point */
23924 +int aufs_read_lock(struct dentry *dentry, int flags)
23925 +{
23926 +       int err;
23927 +       struct super_block *sb;
23928 +
23929 +       sb = dentry->d_sb;
23930 +       err = si_read_lock(sb, flags);
23931 +       if (unlikely(err))
23932 +               goto out;
23933 +
23934 +       if (au_ftest_lock(flags, DW))
23935 +               di_write_lock_child(dentry);
23936 +       else
23937 +               di_read_lock_child(dentry, flags);
23938 +
23939 +       if (au_ftest_lock(flags, GEN)) {
23940 +               err = au_digen_test(dentry, au_sigen(sb));
23941 +               AuDebugOn(!err && au_dbrange_test(dentry));
23942 +               if (unlikely(err))
23943 +                       aufs_read_unlock(dentry, flags);
23944 +       }
23945 +
23946 +out:
23947 +       return err;
23948 +}
23949 +
23950 +void aufs_read_unlock(struct dentry *dentry, int flags)
23951 +{
23952 +       if (au_ftest_lock(flags, DW))
23953 +               di_write_unlock(dentry);
23954 +       else
23955 +               di_read_unlock(dentry, flags);
23956 +       si_read_unlock(dentry->d_sb);
23957 +}
23958 +
23959 +void aufs_write_lock(struct dentry *dentry)
23960 +{
23961 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
23962 +       di_write_lock_child(dentry);
23963 +}
23964 +
23965 +void aufs_write_unlock(struct dentry *dentry)
23966 +{
23967 +       di_write_unlock(dentry);
23968 +       si_write_unlock(dentry->d_sb);
23969 +}
23970 +
23971 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
23972 +{
23973 +       int err;
23974 +       unsigned int sigen;
23975 +       struct super_block *sb;
23976 +
23977 +       sb = d1->d_sb;
23978 +       err = si_read_lock(sb, flags);
23979 +       if (unlikely(err))
23980 +               goto out;
23981 +
23982 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
23983 +
23984 +       if (au_ftest_lock(flags, GEN)) {
23985 +               sigen = au_sigen(sb);
23986 +               err = au_digen_test(d1, sigen);
23987 +               AuDebugOn(!err && au_dbrange_test(d1));
23988 +               if (!err) {
23989 +                       err = au_digen_test(d2, sigen);
23990 +                       AuDebugOn(!err && au_dbrange_test(d2));
23991 +               }
23992 +               if (unlikely(err))
23993 +                       aufs_read_and_write_unlock2(d1, d2);
23994 +       }
23995 +
23996 +out:
23997 +       return err;
23998 +}
23999 +
24000 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
24001 +{
24002 +       di_write_unlock2(d1, d2);
24003 +       si_read_unlock(d1->d_sb);
24004 +}
24005 +
24006 +/* ---------------------------------------------------------------------- */
24007 +
24008 +int si_pid_test_slow(struct super_block *sb)
24009 +{
24010 +       void *p;
24011 +
24012 +       rcu_read_lock();
24013 +       p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
24014 +       rcu_read_unlock();
24015 +
24016 +       return (long)!!p;
24017 +}
24018 +
24019 +void si_pid_set_slow(struct super_block *sb)
24020 +{
24021 +       int err;
24022 +       struct au_sbinfo *sbinfo;
24023 +
24024 +       AuDebugOn(si_pid_test_slow(sb));
24025 +
24026 +       sbinfo = au_sbi(sb);
24027 +       err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
24028 +       AuDebugOn(err);
24029 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
24030 +       err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
24031 +                               /*any valid ptr*/sb);
24032 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
24033 +       AuDebugOn(err);
24034 +       radix_tree_preload_end();
24035 +}
24036 +
24037 +void si_pid_clr_slow(struct super_block *sb)
24038 +{
24039 +       void *p;
24040 +       struct au_sbinfo *sbinfo;
24041 +
24042 +       AuDebugOn(!si_pid_test_slow(sb));
24043 +
24044 +       sbinfo = au_sbi(sb);
24045 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
24046 +       p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
24047 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
24048 +}
24049 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
24050 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
24051 +++ linux/fs/aufs/spl.h 2013-07-06 13:20:47.753531903 +0200
24052 @@ -0,0 +1,112 @@
24053 +/*
24054 + * Copyright (C) 2005-2013 Junjiro R. Okajima
24055 + *
24056 + * This program, aufs is free software; you can redistribute it and/or modify
24057 + * it under the terms of the GNU General Public License as published by
24058 + * the Free Software Foundation; either version 2 of the License, or
24059 + * (at your option) any later version.
24060 + *
24061 + * This program is distributed in the hope that it will be useful,
24062 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24063 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24064 + * GNU General Public License for more details.
24065 + *
24066 + * You should have received a copy of the GNU General Public License
24067 + * along with this program; if not, write to the Free Software
24068 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24069 + */
24070 +
24071 +/*
24072 + * simple list protected by a spinlock
24073 + */
24074 +
24075 +#ifndef __AUFS_SPL_H__
24076 +#define __AUFS_SPL_H__
24077 +
24078 +#ifdef __KERNEL__
24079 +
24080 +struct au_splhead {
24081 +       spinlock_t              spin;
24082 +       struct list_head        head;
24083 +};
24084 +
24085 +static inline void au_spl_init(struct au_splhead *spl)
24086 +{
24087 +       spin_lock_init(&spl->spin);
24088 +       INIT_LIST_HEAD(&spl->head);
24089 +}
24090 +
24091 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
24092 +{
24093 +       spin_lock(&spl->spin);
24094 +       list_add(list, &spl->head);
24095 +       spin_unlock(&spl->spin);
24096 +}
24097 +
24098 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
24099 +{
24100 +       spin_lock(&spl->spin);
24101 +       list_del(list);
24102 +       spin_unlock(&spl->spin);
24103 +}
24104 +
24105 +static inline void au_spl_del_rcu(struct list_head *list,
24106 +                                 struct au_splhead *spl)
24107 +{
24108 +       spin_lock(&spl->spin);
24109 +       list_del_rcu(list);
24110 +       spin_unlock(&spl->spin);
24111 +}
24112 +
24113 +/* ---------------------------------------------------------------------- */
24114 +
24115 +struct au_sphlhead {
24116 +       spinlock_t              spin;
24117 +       struct hlist_head       head;
24118 +};
24119 +
24120 +static inline void au_sphl_init(struct au_sphlhead *sphl)
24121 +{
24122 +       spin_lock_init(&sphl->spin);
24123 +       INIT_HLIST_HEAD(&sphl->head);
24124 +}
24125 +
24126 +static inline void au_sphl_add(struct hlist_node *hlist,
24127 +                              struct au_sphlhead *sphl)
24128 +{
24129 +       spin_lock(&sphl->spin);
24130 +       hlist_add_head(hlist, &sphl->head);
24131 +       spin_unlock(&sphl->spin);
24132 +}
24133 +
24134 +static inline void au_sphl_del(struct hlist_node *hlist,
24135 +                              struct au_sphlhead *sphl)
24136 +{
24137 +       spin_lock(&sphl->spin);
24138 +       hlist_del(hlist);
24139 +       spin_unlock(&sphl->spin);
24140 +}
24141 +
24142 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
24143 +                                  struct au_sphlhead *sphl)
24144 +{
24145 +       spin_lock(&sphl->spin);
24146 +       hlist_del_rcu(hlist);
24147 +       spin_unlock(&sphl->spin);
24148 +}
24149 +
24150 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
24151 +{
24152 +       unsigned long cnt;
24153 +       struct hlist_node *pos;
24154 +
24155 +       cnt = 0;
24156 +       spin_lock(&sphl->spin);
24157 +       hlist_for_each(pos, &sphl->head)
24158 +               cnt++;
24159 +       spin_unlock(&sphl->spin);
24160 +       return cnt;
24161 +}
24162 +
24163 +#endif /* __KERNEL__ */
24164 +#endif /* __AUFS_SPL_H__ */
24165 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
24166 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
24167 +++ linux/fs/aufs/super.c       2013-08-23 23:59:39.634916914 +0200
24168 @@ -0,0 +1,992 @@
24169 +/*
24170 + * Copyright (C) 2005-2013 Junjiro R. Okajima
24171 + *
24172 + * This program, aufs is free software; you can redistribute it and/or modify
24173 + * it under the terms of the GNU General Public License as published by
24174 + * the Free Software Foundation; either version 2 of the License, or
24175 + * (at your option) any later version.
24176 + *
24177 + * This program is distributed in the hope that it will be useful,
24178 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24179 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24180 + * GNU General Public License for more details.
24181 + *
24182 + * You should have received a copy of the GNU General Public License
24183 + * along with this program; if not, write to the Free Software
24184 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24185 + */
24186 +
24187 +/*
24188 + * mount and super_block operations
24189 + */
24190 +
24191 +#include <linux/mm.h>
24192 +#include <linux/module.h>
24193 +#include <linux/seq_file.h>
24194 +#include <linux/statfs.h>
24195 +#include <linux/vmalloc.h>
24196 +#include <linux/writeback.h>
24197 +#include "aufs.h"
24198 +
24199 +/*
24200 + * super_operations
24201 + */
24202 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
24203 +{
24204 +       struct au_icntnr *c;
24205 +
24206 +       c = au_cache_alloc_icntnr();
24207 +       if (c) {
24208 +               au_icntnr_init(c);
24209 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
24210 +               c->iinfo.ii_hinode = NULL;
24211 +               return &c->vfs_inode;
24212 +       }
24213 +       return NULL;
24214 +}
24215 +
24216 +static void aufs_destroy_inode_cb(struct rcu_head *head)
24217 +{
24218 +       struct inode *inode = container_of(head, struct inode, i_rcu);
24219 +
24220 +       INIT_HLIST_HEAD(&inode->i_dentry);
24221 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
24222 +}
24223 +
24224 +static void aufs_destroy_inode(struct inode *inode)
24225 +{
24226 +       au_iinfo_fin(inode);
24227 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
24228 +}
24229 +
24230 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
24231 +{
24232 +       struct inode *inode;
24233 +       int err;
24234 +
24235 +       inode = iget_locked(sb, ino);
24236 +       if (unlikely(!inode)) {
24237 +               inode = ERR_PTR(-ENOMEM);
24238 +               goto out;
24239 +       }
24240 +       if (!(inode->i_state & I_NEW))
24241 +               goto out;
24242 +
24243 +       err = au_xigen_new(inode);
24244 +       if (!err)
24245 +               err = au_iinfo_init(inode);
24246 +       if (!err)
24247 +               inode->i_version++;
24248 +       else {
24249 +               iget_failed(inode);
24250 +               inode = ERR_PTR(err);
24251 +       }
24252 +
24253 +out:
24254 +       /* never return NULL */
24255 +       AuDebugOn(!inode);
24256 +       AuTraceErrPtr(inode);
24257 +       return inode;
24258 +}
24259 +
24260 +/* lock free root dinfo */
24261 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
24262 +{
24263 +       int err;
24264 +       aufs_bindex_t bindex, bend;
24265 +       struct path path;
24266 +       struct au_hdentry *hdp;
24267 +       struct au_branch *br;
24268 +       char *perm;
24269 +
24270 +       err = 0;
24271 +       bend = au_sbend(sb);
24272 +       hdp = au_di(sb->s_root)->di_hdentry;
24273 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
24274 +               br = au_sbr(sb, bindex);
24275 +               path.mnt = au_br_mnt(br);
24276 +               path.dentry = hdp[bindex].hd_dentry;
24277 +               err = au_seq_path(seq, &path);
24278 +               if (err > 0) {
24279 +                       perm = au_optstr_br_perm(br->br_perm);
24280 +                       if (perm) {
24281 +                               err = seq_printf(seq, "=%s", perm);
24282 +                               kfree(perm);
24283 +                               if (err == -1)
24284 +                                       err = -E2BIG;
24285 +                       } else
24286 +                               err = -ENOMEM;
24287 +               }
24288 +               if (!err && bindex != bend)
24289 +                       err = seq_putc(seq, ':');
24290 +       }
24291 +
24292 +       return err;
24293 +}
24294 +
24295 +static void au_show_wbr_create(struct seq_file *m, int v,
24296 +                              struct au_sbinfo *sbinfo)
24297 +{
24298 +       const char *pat;
24299 +
24300 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24301 +
24302 +       seq_puts(m, ",create=");
24303 +       pat = au_optstr_wbr_create(v);
24304 +       switch (v) {
24305 +       case AuWbrCreate_TDP:
24306 +       case AuWbrCreate_RR:
24307 +       case AuWbrCreate_MFS:
24308 +       case AuWbrCreate_PMFS:
24309 +               seq_puts(m, pat);
24310 +               break;
24311 +       case AuWbrCreate_MFSV:
24312 +               seq_printf(m, /*pat*/"mfs:%lu",
24313 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24314 +                          / MSEC_PER_SEC);
24315 +               break;
24316 +       case AuWbrCreate_PMFSV:
24317 +               seq_printf(m, /*pat*/"pmfs:%lu",
24318 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24319 +                          / MSEC_PER_SEC);
24320 +               break;
24321 +       case AuWbrCreate_MFSRR:
24322 +               seq_printf(m, /*pat*/"mfsrr:%llu",
24323 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
24324 +               break;
24325 +       case AuWbrCreate_MFSRRV:
24326 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
24327 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
24328 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24329 +                          / MSEC_PER_SEC);
24330 +               break;
24331 +       }
24332 +}
24333 +
24334 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
24335 +{
24336 +#ifdef CONFIG_SYSFS
24337 +       return 0;
24338 +#else
24339 +       int err;
24340 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
24341 +       aufs_bindex_t bindex, brid;
24342 +       struct qstr *name;
24343 +       struct file *f;
24344 +       struct dentry *d, *h_root;
24345 +       struct au_hdentry *hdp;
24346 +
24347 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24348 +
24349 +       err = 0;
24350 +       f = au_sbi(sb)->si_xib;
24351 +       if (!f)
24352 +               goto out;
24353 +
24354 +       /* stop printing the default xino path on the first writable branch */
24355 +       h_root = NULL;
24356 +       brid = au_xino_brid(sb);
24357 +       if (brid >= 0) {
24358 +               bindex = au_br_index(sb, brid);
24359 +               hdp = au_di(sb->s_root)->di_hdentry;
24360 +               h_root = hdp[0 + bindex].hd_dentry;
24361 +       }
24362 +       d = f->f_dentry;
24363 +       name = &d->d_name;
24364 +       /* safe ->d_parent because the file is unlinked */
24365 +       if (d->d_parent == h_root
24366 +           && name->len == len
24367 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
24368 +               goto out;
24369 +
24370 +       seq_puts(seq, ",xino=");
24371 +       err = au_xino_path(seq, f);
24372 +
24373 +out:
24374 +       return err;
24375 +#endif
24376 +}
24377 +
24378 +/* seq_file will re-call me in case of too long string */
24379 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
24380 +{
24381 +       int err;
24382 +       unsigned int mnt_flags, v;
24383 +       struct super_block *sb;
24384 +       struct au_sbinfo *sbinfo;
24385 +
24386 +#define AuBool(name, str) do { \
24387 +       v = au_opt_test(mnt_flags, name); \
24388 +       if (v != au_opt_test(AuOpt_Def, name)) \
24389 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
24390 +} while (0)
24391 +
24392 +#define AuStr(name, str) do { \
24393 +       v = mnt_flags & AuOptMask_##name; \
24394 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
24395 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
24396 +} while (0)
24397 +
24398 +#define AuUInt(name, str, val) do { \
24399 +       if (val != AUFS_##name##_DEF) \
24400 +               seq_printf(m, "," #str "=%u", val); \
24401 +} while (0)
24402 +
24403 +       /* lock free root dinfo */
24404 +       sb = dentry->d_sb;
24405 +       si_noflush_read_lock(sb);
24406 +       sbinfo = au_sbi(sb);
24407 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
24408 +
24409 +       mnt_flags = au_mntflags(sb);
24410 +       if (au_opt_test(mnt_flags, XINO)) {
24411 +               err = au_show_xino(m, sb);
24412 +               if (unlikely(err))
24413 +                       goto out;
24414 +       } else
24415 +               seq_puts(m, ",noxino");
24416 +
24417 +       AuBool(TRUNC_XINO, trunc_xino);
24418 +       AuStr(UDBA, udba);
24419 +       AuBool(SHWH, shwh);
24420 +       AuBool(PLINK, plink);
24421 +       AuBool(DIO, dio);
24422 +       /* AuBool(DIRPERM1, dirperm1); */
24423 +       /* AuBool(REFROF, refrof); */
24424 +
24425 +       v = sbinfo->si_wbr_create;
24426 +       if (v != AuWbrCreate_Def)
24427 +               au_show_wbr_create(m, v, sbinfo);
24428 +
24429 +       v = sbinfo->si_wbr_copyup;
24430 +       if (v != AuWbrCopyup_Def)
24431 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
24432 +
24433 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
24434 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
24435 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
24436 +
24437 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
24438 +
24439 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
24440 +       AuUInt(RDCACHE, rdcache, v);
24441 +
24442 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
24443 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
24444 +
24445 +       AuBool(SUM, sum);
24446 +       /* AuBool(SUM_W, wsum); */
24447 +       AuBool(WARN_PERM, warn_perm);
24448 +       AuBool(VERBOSE, verbose);
24449 +
24450 +out:
24451 +       /* be sure to print "br:" last */
24452 +       if (!sysaufs_brs) {
24453 +               seq_puts(m, ",br:");
24454 +               au_show_brs(m, sb);
24455 +       }
24456 +       si_read_unlock(sb);
24457 +       return 0;
24458 +
24459 +#undef AuBool
24460 +#undef AuStr
24461 +#undef AuUInt
24462 +}
24463 +
24464 +/* ---------------------------------------------------------------------- */
24465 +
24466 +/* sum mode which returns the summation for statfs(2) */
24467 +
24468 +static u64 au_add_till_max(u64 a, u64 b)
24469 +{
24470 +       u64 old;
24471 +
24472 +       old = a;
24473 +       a += b;
24474 +       if (old <= a)
24475 +               return a;
24476 +       return ULLONG_MAX;
24477 +}
24478 +
24479 +static u64 au_mul_till_max(u64 a, long mul)
24480 +{
24481 +       u64 old;
24482 +
24483 +       old = a;
24484 +       a *= mul;
24485 +       if (old <= a)
24486 +               return a;
24487 +       return ULLONG_MAX;
24488 +}
24489 +
24490 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
24491 +{
24492 +       int err;
24493 +       long bsize, factor;
24494 +       u64 blocks, bfree, bavail, files, ffree;
24495 +       aufs_bindex_t bend, bindex, i;
24496 +       unsigned char shared;
24497 +       struct path h_path;
24498 +       struct super_block *h_sb;
24499 +
24500 +       err = 0;
24501 +       bsize = LONG_MAX;
24502 +       files = 0;
24503 +       ffree = 0;
24504 +       blocks = 0;
24505 +       bfree = 0;
24506 +       bavail = 0;
24507 +       bend = au_sbend(sb);
24508 +       for (bindex = 0; bindex <= bend; bindex++) {
24509 +               h_path.mnt = au_sbr_mnt(sb, bindex);
24510 +               h_sb = h_path.mnt->mnt_sb;
24511 +               shared = 0;
24512 +               for (i = 0; !shared && i < bindex; i++)
24513 +                       shared = (au_sbr_sb(sb, i) == h_sb);
24514 +               if (shared)
24515 +                       continue;
24516 +
24517 +               /* sb->s_root for NFS is unreliable */
24518 +               h_path.dentry = h_path.mnt->mnt_root;
24519 +               err = vfs_statfs(&h_path, buf);
24520 +               if (unlikely(err))
24521 +                       goto out;
24522 +
24523 +               if (bsize > buf->f_bsize) {
24524 +                       /*
24525 +                        * we will reduce bsize, so we have to expand blocks
24526 +                        * etc. to match them again
24527 +                        */
24528 +                       factor = (bsize / buf->f_bsize);
24529 +                       blocks = au_mul_till_max(blocks, factor);
24530 +                       bfree = au_mul_till_max(bfree, factor);
24531 +                       bavail = au_mul_till_max(bavail, factor);
24532 +                       bsize = buf->f_bsize;
24533 +               }
24534 +
24535 +               factor = (buf->f_bsize / bsize);
24536 +               blocks = au_add_till_max(blocks,
24537 +                               au_mul_till_max(buf->f_blocks, factor));
24538 +               bfree = au_add_till_max(bfree,
24539 +                               au_mul_till_max(buf->f_bfree, factor));
24540 +               bavail = au_add_till_max(bavail,
24541 +                               au_mul_till_max(buf->f_bavail, factor));
24542 +               files = au_add_till_max(files, buf->f_files);
24543 +               ffree = au_add_till_max(ffree, buf->f_ffree);
24544 +       }
24545 +
24546 +       buf->f_bsize = bsize;
24547 +       buf->f_blocks = blocks;
24548 +       buf->f_bfree = bfree;
24549 +       buf->f_bavail = bavail;
24550 +       buf->f_files = files;
24551 +       buf->f_ffree = ffree;
24552 +       buf->f_frsize = 0;
24553 +
24554 +out:
24555 +       return err;
24556 +}
24557 +
24558 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
24559 +{
24560 +       int err;
24561 +       struct path h_path;
24562 +       struct super_block *sb;
24563 +
24564 +       /* lock free root dinfo */
24565 +       sb = dentry->d_sb;
24566 +       si_noflush_read_lock(sb);
24567 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
24568 +               /* sb->s_root for NFS is unreliable */
24569 +               h_path.mnt = au_sbr_mnt(sb, 0);
24570 +               h_path.dentry = h_path.mnt->mnt_root;
24571 +               err = vfs_statfs(&h_path, buf);
24572 +       } else
24573 +               err = au_statfs_sum(sb, buf);
24574 +       si_read_unlock(sb);
24575 +
24576 +       if (!err) {
24577 +               buf->f_type = AUFS_SUPER_MAGIC;
24578 +               buf->f_namelen = AUFS_MAX_NAMELEN;
24579 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
24580 +       }
24581 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
24582 +
24583 +       return err;
24584 +}
24585 +
24586 +/* ---------------------------------------------------------------------- */
24587 +
24588 +static int aufs_sync_fs(struct super_block *sb, int wait)
24589 +{
24590 +       int err, e;
24591 +       aufs_bindex_t bend, bindex;
24592 +       struct au_branch *br;
24593 +       struct super_block *h_sb;
24594 +
24595 +       err = 0;
24596 +       si_noflush_read_lock(sb);
24597 +       bend = au_sbend(sb);
24598 +       for (bindex = 0; bindex <= bend; bindex++) {
24599 +               br = au_sbr(sb, bindex);
24600 +               if (!au_br_writable(br->br_perm))
24601 +                       continue;
24602 +
24603 +               h_sb = au_sbr_sb(sb, bindex);
24604 +               if (h_sb->s_op->sync_fs) {
24605 +                       e = h_sb->s_op->sync_fs(h_sb, wait);
24606 +                       if (unlikely(e && !err))
24607 +                               err = e;
24608 +                       /* go on even if an error happens */
24609 +               }
24610 +       }
24611 +       si_read_unlock(sb);
24612 +
24613 +       return err;
24614 +}
24615 +
24616 +/* ---------------------------------------------------------------------- */
24617 +
24618 +/* final actions when unmounting a file system */
24619 +static void aufs_put_super(struct super_block *sb)
24620 +{
24621 +       struct au_sbinfo *sbinfo;
24622 +
24623 +       sbinfo = au_sbi(sb);
24624 +       if (!sbinfo)
24625 +               return;
24626 +
24627 +       dbgaufs_si_fin(sbinfo);
24628 +       kobject_put(&sbinfo->si_kobj);
24629 +}
24630 +
24631 +/* ---------------------------------------------------------------------- */
24632 +
24633 +void au_array_free(void *array)
24634 +{
24635 +       if (array) {
24636 +               if (!is_vmalloc_addr(array))
24637 +                       kfree(array);
24638 +               else
24639 +                       vfree(array);
24640 +       }
24641 +}
24642 +
24643 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
24644 +{
24645 +       void *array;
24646 +       unsigned long long n;
24647 +
24648 +       array = NULL;
24649 +       n = 0;
24650 +       if (!*hint)
24651 +               goto out;
24652 +
24653 +       if (*hint > ULLONG_MAX / sizeof(array)) {
24654 +               array = ERR_PTR(-EMFILE);
24655 +               pr_err("hint %llu\n", *hint);
24656 +               goto out;
24657 +       }
24658 +
24659 +       array = kmalloc(sizeof(array) * *hint, GFP_NOFS);
24660 +       if (unlikely(!array))
24661 +               array = vmalloc(sizeof(array) * *hint);
24662 +       if (unlikely(!array)) {
24663 +               array = ERR_PTR(-ENOMEM);
24664 +               goto out;
24665 +       }
24666 +
24667 +       n = cb(array, *hint, arg);
24668 +       AuDebugOn(n > *hint);
24669 +
24670 +out:
24671 +       *hint = n;
24672 +       return array;
24673 +}
24674 +
24675 +static unsigned long long au_iarray_cb(void *a,
24676 +                                      unsigned long long max __maybe_unused,
24677 +                                      void *arg)
24678 +{
24679 +       unsigned long long n;
24680 +       struct inode **p, *inode;
24681 +       struct list_head *head;
24682 +
24683 +       n = 0;
24684 +       p = a;
24685 +       head = arg;
24686 +       spin_lock(&inode_sb_list_lock);
24687 +       list_for_each_entry(inode, head, i_sb_list) {
24688 +               if (!is_bad_inode(inode)
24689 +                   && au_ii(inode)->ii_bstart >= 0) {
24690 +                       spin_lock(&inode->i_lock);
24691 +                       if (atomic_read(&inode->i_count)) {
24692 +                               au_igrab(inode);
24693 +                               *p++ = inode;
24694 +                               n++;
24695 +                               AuDebugOn(n > max);
24696 +                       }
24697 +                       spin_unlock(&inode->i_lock);
24698 +               }
24699 +       }
24700 +       spin_unlock(&inode_sb_list_lock);
24701 +
24702 +       return n;
24703 +}
24704 +
24705 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
24706 +{
24707 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
24708 +       return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
24709 +}
24710 +
24711 +void au_iarray_free(struct inode **a, unsigned long long max)
24712 +{
24713 +       unsigned long long ull;
24714 +
24715 +       for (ull = 0; ull < max; ull++)
24716 +               iput(a[ull]);
24717 +       au_array_free(a);
24718 +}
24719 +
24720 +/* ---------------------------------------------------------------------- */
24721 +
24722 +/*
24723 + * refresh dentry and inode at remount time.
24724 + */
24725 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
24726 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
24727 +                     struct dentry *parent)
24728 +{
24729 +       int err;
24730 +
24731 +       di_write_lock_child(dentry);
24732 +       di_read_lock_parent(parent, AuLock_IR);
24733 +       err = au_refresh_dentry(dentry, parent);
24734 +       if (!err && dir_flags)
24735 +               au_hn_reset(dentry->d_inode, dir_flags);
24736 +       di_read_unlock(parent, AuLock_IR);
24737 +       di_write_unlock(dentry);
24738 +
24739 +       return err;
24740 +}
24741 +
24742 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
24743 +                          struct au_sbinfo *sbinfo,
24744 +                          const unsigned int dir_flags)
24745 +{
24746 +       int err;
24747 +       struct dentry *parent;
24748 +       struct inode *inode;
24749 +
24750 +       err = 0;
24751 +       parent = dget_parent(dentry);
24752 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
24753 +               inode = dentry->d_inode;
24754 +               if (inode) {
24755 +                       if (!S_ISDIR(inode->i_mode))
24756 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
24757 +                                                parent);
24758 +                       else {
24759 +                               err = au_do_refresh(dentry, dir_flags, parent);
24760 +                               if (unlikely(err))
24761 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
24762 +                       }
24763 +               } else
24764 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
24765 +               AuDbgDentry(dentry);
24766 +       }
24767 +       dput(parent);
24768 +
24769 +       AuTraceErr(err);
24770 +       return err;
24771 +}
24772 +
24773 +static int au_refresh_d(struct super_block *sb)
24774 +{
24775 +       int err, i, j, ndentry, e;
24776 +       unsigned int sigen;
24777 +       struct au_dcsub_pages dpages;
24778 +       struct au_dpage *dpage;
24779 +       struct dentry **dentries, *d;
24780 +       struct au_sbinfo *sbinfo;
24781 +       struct dentry *root = sb->s_root;
24782 +       const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
24783 +
24784 +       err = au_dpages_init(&dpages, GFP_NOFS);
24785 +       if (unlikely(err))
24786 +               goto out;
24787 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
24788 +       if (unlikely(err))
24789 +               goto out_dpages;
24790 +
24791 +       sigen = au_sigen(sb);
24792 +       sbinfo = au_sbi(sb);
24793 +       for (i = 0; i < dpages.ndpage; i++) {
24794 +               dpage = dpages.dpages + i;
24795 +               dentries = dpage->dentries;
24796 +               ndentry = dpage->ndentry;
24797 +               for (j = 0; j < ndentry; j++) {
24798 +                       d = dentries[j];
24799 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
24800 +                       if (unlikely(e && !err))
24801 +                               err = e;
24802 +                       /* go on even err */
24803 +               }
24804 +       }
24805 +
24806 +out_dpages:
24807 +       au_dpages_free(&dpages);
24808 +out:
24809 +       return err;
24810 +}
24811 +
24812 +static int au_refresh_i(struct super_block *sb)
24813 +{
24814 +       int err, e;
24815 +       unsigned int sigen;
24816 +       unsigned long long max, ull;
24817 +       struct inode *inode, **array;
24818 +
24819 +       array = au_iarray_alloc(sb, &max);
24820 +       err = PTR_ERR(array);
24821 +       if (IS_ERR(array))
24822 +               goto out;
24823 +
24824 +       err = 0;
24825 +       sigen = au_sigen(sb);
24826 +       for (ull = 0; ull < max; ull++) {
24827 +               inode = array[ull];
24828 +               if (au_iigen(inode, NULL) != sigen) {
24829 +                       ii_write_lock_child(inode);
24830 +                       e = au_refresh_hinode_self(inode);
24831 +                       ii_write_unlock(inode);
24832 +                       if (unlikely(e)) {
24833 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
24834 +                               if (!err)
24835 +                                       err = e;
24836 +                               /* go on even if err */
24837 +                       }
24838 +               }
24839 +       }
24840 +
24841 +       au_iarray_free(array, max);
24842 +
24843 +out:
24844 +       return err;
24845 +}
24846 +
24847 +static void au_remount_refresh(struct super_block *sb)
24848 +{
24849 +       int err, e;
24850 +       unsigned int udba;
24851 +       aufs_bindex_t bindex, bend;
24852 +       struct dentry *root;
24853 +       struct inode *inode;
24854 +       struct au_branch *br;
24855 +
24856 +       au_sigen_inc(sb);
24857 +       au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
24858 +
24859 +       root = sb->s_root;
24860 +       DiMustNoWaiters(root);
24861 +       inode = root->d_inode;
24862 +       IiMustNoWaiters(inode);
24863 +
24864 +       udba = au_opt_udba(sb);
24865 +       bend = au_sbend(sb);
24866 +       for (bindex = 0; bindex <= bend; bindex++) {
24867 +               br = au_sbr(sb, bindex);
24868 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
24869 +               if (unlikely(err))
24870 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
24871 +                               bindex, err);
24872 +               /* go on even if err */
24873 +       }
24874 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
24875 +
24876 +       di_write_unlock(root);
24877 +       err = au_refresh_d(sb);
24878 +       e = au_refresh_i(sb);
24879 +       if (unlikely(e && !err))
24880 +               err = e;
24881 +       /* aufs_write_lock() calls ..._child() */
24882 +       di_write_lock_child(root);
24883 +
24884 +       au_cpup_attr_all(inode, /*force*/1);
24885 +
24886 +       if (unlikely(err))
24887 +               AuIOErr("refresh failed, ignored, %d\n", err);
24888 +}
24889 +
24890 +/* stop extra interpretation of errno in mount(8), and strange error messages */
24891 +static int cvt_err(int err)
24892 +{
24893 +       AuTraceErr(err);
24894 +
24895 +       switch (err) {
24896 +       case -ENOENT:
24897 +       case -ENOTDIR:
24898 +       case -EEXIST:
24899 +       case -EIO:
24900 +               err = -EINVAL;
24901 +       }
24902 +       return err;
24903 +}
24904 +
24905 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
24906 +{
24907 +       int err, do_dx;
24908 +       unsigned int mntflags;
24909 +       struct au_opts opts;
24910 +       struct dentry *root;
24911 +       struct inode *inode;
24912 +       struct au_sbinfo *sbinfo;
24913 +
24914 +       err = 0;
24915 +       root = sb->s_root;
24916 +       if (!data || !*data) {
24917 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
24918 +               if (!err) {
24919 +                       di_write_lock_child(root);
24920 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
24921 +                       aufs_write_unlock(root);
24922 +               }
24923 +               goto out;
24924 +       }
24925 +
24926 +       err = -ENOMEM;
24927 +       memset(&opts, 0, sizeof(opts));
24928 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
24929 +       if (unlikely(!opts.opt))
24930 +               goto out;
24931 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
24932 +       opts.flags = AuOpts_REMOUNT;
24933 +       opts.sb_flags = *flags;
24934 +
24935 +       /* parse it before aufs lock */
24936 +       err = au_opts_parse(sb, data, &opts);
24937 +       if (unlikely(err))
24938 +               goto out_opts;
24939 +
24940 +       sbinfo = au_sbi(sb);
24941 +       inode = root->d_inode;
24942 +       mutex_lock(&inode->i_mutex);
24943 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
24944 +       if (unlikely(err))
24945 +               goto out_mtx;
24946 +       di_write_lock_child(root);
24947 +
24948 +       /* au_opts_remount() may return an error */
24949 +       err = au_opts_remount(sb, &opts);
24950 +       au_opts_free(&opts);
24951 +
24952 +       if (au_ftest_opts(opts.flags, REFRESH))
24953 +               au_remount_refresh(sb);
24954 +
24955 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
24956 +               mntflags = au_mntflags(sb);
24957 +               do_dx = !!au_opt_test(mntflags, DIO);
24958 +               au_dy_arefresh(do_dx);
24959 +       }
24960 +
24961 +       aufs_write_unlock(root);
24962 +
24963 +out_mtx:
24964 +       mutex_unlock(&inode->i_mutex);
24965 +out_opts:
24966 +       free_page((unsigned long)opts.opt);
24967 +out:
24968 +       err = cvt_err(err);
24969 +       AuTraceErr(err);
24970 +       return err;
24971 +}
24972 +
24973 +static const struct super_operations aufs_sop = {
24974 +       .alloc_inode    = aufs_alloc_inode,
24975 +       .destroy_inode  = aufs_destroy_inode,
24976 +       /* always deleting, no clearing */
24977 +       .drop_inode     = generic_delete_inode,
24978 +       .show_options   = aufs_show_options,
24979 +       .statfs         = aufs_statfs,
24980 +       .put_super      = aufs_put_super,
24981 +       .sync_fs        = aufs_sync_fs,
24982 +       .remount_fs     = aufs_remount_fs
24983 +};
24984 +
24985 +/* ---------------------------------------------------------------------- */
24986 +
24987 +static int alloc_root(struct super_block *sb)
24988 +{
24989 +       int err;
24990 +       struct inode *inode;
24991 +       struct dentry *root;
24992 +
24993 +       err = -ENOMEM;
24994 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
24995 +       err = PTR_ERR(inode);
24996 +       if (IS_ERR(inode))
24997 +               goto out;
24998 +
24999 +       inode->i_op = &aufs_dir_iop;
25000 +       inode->i_fop = &aufs_dir_fop;
25001 +       inode->i_mode = S_IFDIR;
25002 +       set_nlink(inode, 2);
25003 +       unlock_new_inode(inode);
25004 +
25005 +       root = d_make_root(inode);
25006 +       if (unlikely(!root))
25007 +               goto out;
25008 +       err = PTR_ERR(root);
25009 +       if (IS_ERR(root))
25010 +               goto out;
25011 +
25012 +       err = au_di_init(root);
25013 +       if (!err) {
25014 +               sb->s_root = root;
25015 +               return 0; /* success */
25016 +       }
25017 +       dput(root);
25018 +
25019 +out:
25020 +       return err;
25021 +}
25022 +
25023 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
25024 +                          int silent __maybe_unused)
25025 +{
25026 +       int err;
25027 +       struct au_opts opts;
25028 +       struct dentry *root;
25029 +       struct inode *inode;
25030 +       char *arg = raw_data;
25031 +
25032 +       if (unlikely(!arg || !*arg)) {
25033 +               err = -EINVAL;
25034 +               pr_err("no arg\n");
25035 +               goto out;
25036 +       }
25037 +
25038 +       err = -ENOMEM;
25039 +       memset(&opts, 0, sizeof(opts));
25040 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
25041 +       if (unlikely(!opts.opt))
25042 +               goto out;
25043 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
25044 +       opts.sb_flags = sb->s_flags;
25045 +
25046 +       err = au_si_alloc(sb);
25047 +       if (unlikely(err))
25048 +               goto out_opts;
25049 +
25050 +       /* all timestamps always follow the ones on the branch */
25051 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
25052 +       sb->s_op = &aufs_sop;
25053 +       sb->s_d_op = &aufs_dop;
25054 +       sb->s_magic = AUFS_SUPER_MAGIC;
25055 +       sb->s_maxbytes = 0;
25056 +       au_export_init(sb);
25057 +
25058 +       err = alloc_root(sb);
25059 +       if (unlikely(err)) {
25060 +               si_write_unlock(sb);
25061 +               goto out_info;
25062 +       }
25063 +       root = sb->s_root;
25064 +       inode = root->d_inode;
25065 +
25066 +       /*
25067 +        * actually we can parse options regardless aufs lock here.
25068 +        * but at remount time, parsing must be done before aufs lock.
25069 +        * so we follow the same rule.
25070 +        */
25071 +       ii_write_lock_parent(inode);
25072 +       aufs_write_unlock(root);
25073 +       err = au_opts_parse(sb, arg, &opts);
25074 +       if (unlikely(err))
25075 +               goto out_root;
25076 +
25077 +       /* lock vfs_inode first, then aufs. */
25078 +       mutex_lock(&inode->i_mutex);
25079 +       aufs_write_lock(root);
25080 +       err = au_opts_mount(sb, &opts);
25081 +       au_opts_free(&opts);
25082 +       aufs_write_unlock(root);
25083 +       mutex_unlock(&inode->i_mutex);
25084 +       if (!err)
25085 +               goto out_opts; /* success */
25086 +
25087 +out_root:
25088 +       dput(root);
25089 +       sb->s_root = NULL;
25090 +out_info:
25091 +       dbgaufs_si_fin(au_sbi(sb));
25092 +       kobject_put(&au_sbi(sb)->si_kobj);
25093 +       sb->s_fs_info = NULL;
25094 +out_opts:
25095 +       free_page((unsigned long)opts.opt);
25096 +out:
25097 +       AuTraceErr(err);
25098 +       err = cvt_err(err);
25099 +       AuTraceErr(err);
25100 +       return err;
25101 +}
25102 +
25103 +/* ---------------------------------------------------------------------- */
25104 +
25105 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
25106 +                                const char *dev_name __maybe_unused,
25107 +                                void *raw_data)
25108 +{
25109 +       struct dentry *root;
25110 +       struct super_block *sb;
25111 +
25112 +       /* all timestamps always follow the ones on the branch */
25113 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
25114 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
25115 +       if (IS_ERR(root))
25116 +               goto out;
25117 +
25118 +       sb = root->d_sb;
25119 +       si_write_lock(sb, !AuLock_FLUSH);
25120 +       sysaufs_brs_add(sb, 0);
25121 +       si_write_unlock(sb);
25122 +       au_sbilist_add(sb);
25123 +
25124 +out:
25125 +       return root;
25126 +}
25127 +
25128 +static void aufs_kill_sb(struct super_block *sb)
25129 +{
25130 +       struct au_sbinfo *sbinfo;
25131 +
25132 +       sbinfo = au_sbi(sb);
25133 +       if (sbinfo) {
25134 +               au_sbilist_del(sb);
25135 +               aufs_write_lock(sb->s_root);
25136 +               if (sbinfo->si_wbr_create_ops->fin)
25137 +                       sbinfo->si_wbr_create_ops->fin(sb);
25138 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
25139 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
25140 +                       au_remount_refresh(sb);
25141 +               }
25142 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
25143 +                       au_plink_put(sb, /*verbose*/1);
25144 +               au_xino_clr(sb);
25145 +               sbinfo->si_sb = NULL;
25146 +               aufs_write_unlock(sb->s_root);
25147 +               au_nwt_flush(&sbinfo->si_nowait);
25148 +       }
25149 +       generic_shutdown_super(sb);
25150 +}
25151 +
25152 +struct file_system_type aufs_fs_type = {
25153 +       .name           = AUFS_FSTYPE,
25154 +       /* a race between rename and others */
25155 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
25156 +       .mount          = aufs_mount,
25157 +       .kill_sb        = aufs_kill_sb,
25158 +       /* no need to __module_get() and module_put(). */
25159 +       .owner          = THIS_MODULE,
25160 +};
25161 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
25162 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
25163 +++ linux/fs/aufs/super.h       2013-08-23 23:59:39.638250372 +0200
25164 @@ -0,0 +1,559 @@
25165 +/*
25166 + * Copyright (C) 2005-2013 Junjiro R. Okajima
25167 + *
25168 + * This program, aufs is free software; you can redistribute it and/or modify
25169 + * it under the terms of the GNU General Public License as published by
25170 + * the Free Software Foundation; either version 2 of the License, or
25171 + * (at your option) any later version.
25172 + *
25173 + * This program is distributed in the hope that it will be useful,
25174 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25175 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25176 + * GNU General Public License for more details.
25177 + *
25178 + * You should have received a copy of the GNU General Public License
25179 + * along with this program; if not, write to the Free Software
25180 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25181 + */
25182 +
25183 +/*
25184 + * super_block operations
25185 + */
25186 +
25187 +#ifndef __AUFS_SUPER_H__
25188 +#define __AUFS_SUPER_H__
25189 +
25190 +#ifdef __KERNEL__
25191 +
25192 +#include <linux/fs.h>
25193 +#include "rwsem.h"
25194 +#include "spl.h"
25195 +#include "wkq.h"
25196 +
25197 +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
25198 +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
25199 +                              loff_t *);
25200 +
25201 +/* policies to select one among multiple writable branches */
25202 +struct au_wbr_copyup_operations {
25203 +       int (*copyup)(struct dentry *dentry);
25204 +};
25205 +
25206 +struct au_wbr_create_operations {
25207 +       int (*create)(struct dentry *dentry, int isdir);
25208 +       int (*init)(struct super_block *sb);
25209 +       int (*fin)(struct super_block *sb);
25210 +};
25211 +
25212 +struct au_wbr_mfs {
25213 +       struct mutex    mfs_lock; /* protect this structure */
25214 +       unsigned long   mfs_jiffy;
25215 +       unsigned long   mfs_expire;
25216 +       aufs_bindex_t   mfs_bindex;
25217 +
25218 +       unsigned long long      mfsrr_bytes;
25219 +       unsigned long long      mfsrr_watermark;
25220 +};
25221 +
25222 +struct pseudo_link {
25223 +       union {
25224 +               struct hlist_node hlist;
25225 +               struct rcu_head rcu;
25226 +       };
25227 +       struct inode *inode;
25228 +};
25229 +
25230 +#define AuPlink_NHASH 100
25231 +static inline int au_plink_hash(ino_t ino)
25232 +{
25233 +       return ino % AuPlink_NHASH;
25234 +}
25235 +
25236 +struct au_branch;
25237 +struct au_sbinfo {
25238 +       /* nowait tasks in the system-wide workqueue */
25239 +       struct au_nowait_tasks  si_nowait;
25240 +
25241 +       /*
25242 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
25243 +        * rwsem for au_sbinfo is necessary.
25244 +        */
25245 +       struct au_rwsem         si_rwsem;
25246 +
25247 +       /* prevent recursive locking in deleting inode */
25248 +       struct {
25249 +               unsigned long           *bitmap;
25250 +               spinlock_t              tree_lock;
25251 +               struct radix_tree_root  tree;
25252 +       } au_si_pid;
25253 +
25254 +       /*
25255 +        * dirty approach to protect sb->sb_inodes and ->s_files from remount.
25256 +        */
25257 +       atomic_long_t           si_ninodes, si_nfiles;
25258 +
25259 +       /* branch management */
25260 +       unsigned int            si_generation;
25261 +
25262 +       /* see above flags */
25263 +       unsigned char           au_si_status;
25264 +
25265 +       aufs_bindex_t           si_bend;
25266 +
25267 +       /* dirty trick to keep br_id plus */
25268 +       unsigned int            si_last_br_id :
25269 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
25270 +       struct au_branch        **si_branch;
25271 +
25272 +       /* policy to select a writable branch */
25273 +       unsigned char           si_wbr_copyup;
25274 +       unsigned char           si_wbr_create;
25275 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
25276 +       struct au_wbr_create_operations *si_wbr_create_ops;
25277 +
25278 +       /* round robin */
25279 +       atomic_t                si_wbr_rr_next;
25280 +
25281 +       /* most free space */
25282 +       struct au_wbr_mfs       si_wbr_mfs;
25283 +
25284 +       /* mount flags */
25285 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
25286 +       unsigned int            si_mntflags;
25287 +
25288 +       /* external inode number (bitmap and translation table) */
25289 +       au_readf_t              si_xread;
25290 +       au_writef_t             si_xwrite;
25291 +       struct file             *si_xib;
25292 +       struct mutex            si_xib_mtx; /* protect xib members */
25293 +       unsigned long           *si_xib_buf;
25294 +       unsigned long           si_xib_last_pindex;
25295 +       int                     si_xib_next_bit;
25296 +       aufs_bindex_t           si_xino_brid;
25297 +       /* reserved for future use */
25298 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
25299 +
25300 +#ifdef CONFIG_AUFS_EXPORT
25301 +       /* i_generation */
25302 +       struct file             *si_xigen;
25303 +       atomic_t                si_xigen_next;
25304 +#endif
25305 +
25306 +       /* vdir parameters */
25307 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
25308 +       unsigned int            si_rdblk;       /* deblk size */
25309 +       unsigned int            si_rdhash;      /* hash size */
25310 +
25311 +       /*
25312 +        * If the number of whiteouts are larger than si_dirwh, leave all of
25313 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
25314 +        * future fsck.aufs or kernel thread will remove them later.
25315 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
25316 +        */
25317 +       unsigned int            si_dirwh;
25318 +
25319 +       /*
25320 +        * rename(2) a directory with all children.
25321 +        */
25322 +       /* reserved for future use */
25323 +       /* int                  si_rendir; */
25324 +
25325 +       /* pseudo_link list */
25326 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
25327 +       wait_queue_head_t       si_plink_wq;
25328 +       spinlock_t              si_plink_maint_lock;
25329 +       pid_t                   si_plink_maint_pid;
25330 +
25331 +       /*
25332 +        * sysfs and lifetime management.
25333 +        * this is not a small structure and it may be a waste of memory in case
25334 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
25335 +        * but using sysfs is majority.
25336 +        */
25337 +       struct kobject          si_kobj;
25338 +#ifdef CONFIG_DEBUG_FS
25339 +       struct dentry            *si_dbgaufs;
25340 +       struct dentry            *si_dbgaufs_plink;
25341 +       struct dentry            *si_dbgaufs_xib;
25342 +#ifdef CONFIG_AUFS_EXPORT
25343 +       struct dentry            *si_dbgaufs_xigen;
25344 +#endif
25345 +#endif
25346 +
25347 +#ifdef CONFIG_AUFS_SBILIST
25348 +       struct list_head        si_list;
25349 +#endif
25350 +
25351 +       /* dirty, necessary for unmounting, sysfs and sysrq */
25352 +       struct super_block      *si_sb;
25353 +};
25354 +
25355 +/* sbinfo status flags */
25356 +/*
25357 + * set true when refresh_dirs() failed at remount time.
25358 + * then try refreshing dirs at access time again.
25359 + * if it is false, refreshing dirs at access time is unnecesary
25360 + */
25361 +#define AuSi_FAILED_REFRESH_DIR        1
25362 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
25363 +                                          unsigned int flag)
25364 +{
25365 +       AuRwMustAnyLock(&sbi->si_rwsem);
25366 +       return sbi->au_si_status & flag;
25367 +}
25368 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
25369 +#define au_fset_si(sbinfo, name) do { \
25370 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
25371 +       (sbinfo)->au_si_status |= AuSi_##name; \
25372 +} while (0)
25373 +#define au_fclr_si(sbinfo, name) do { \
25374 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
25375 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
25376 +} while (0)
25377 +
25378 +/* ---------------------------------------------------------------------- */
25379 +
25380 +/* policy to select one among writable branches */
25381 +#define AuWbrCopyup(sbinfo, ...) \
25382 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
25383 +#define AuWbrCreate(sbinfo, ...) \
25384 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
25385 +
25386 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
25387 +#define AuLock_DW              1               /* write-lock dentry */
25388 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
25389 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
25390 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
25391 +#define AuLock_DIR             (1 << 4)        /* target is a dir */
25392 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
25393 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
25394 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
25395 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
25396 +#define au_fset_lock(flags, name) \
25397 +       do { (flags) |= AuLock_##name; } while (0)
25398 +#define au_fclr_lock(flags, name) \
25399 +       do { (flags) &= ~AuLock_##name; } while (0)
25400 +
25401 +/* ---------------------------------------------------------------------- */
25402 +
25403 +/* super.c */
25404 +extern struct file_system_type aufs_fs_type;
25405 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
25406 +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
25407 +                                          void *arg);
25408 +void au_array_free(void *array);
25409 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
25410 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
25411 +void au_iarray_free(struct inode **a, unsigned long long max);
25412 +
25413 +/* sbinfo.c */
25414 +void au_si_free(struct kobject *kobj);
25415 +int au_si_alloc(struct super_block *sb);
25416 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
25417 +
25418 +unsigned int au_sigen_inc(struct super_block *sb);
25419 +aufs_bindex_t au_new_br_id(struct super_block *sb);
25420 +
25421 +int si_read_lock(struct super_block *sb, int flags);
25422 +int si_write_lock(struct super_block *sb, int flags);
25423 +int aufs_read_lock(struct dentry *dentry, int flags);
25424 +void aufs_read_unlock(struct dentry *dentry, int flags);
25425 +void aufs_write_lock(struct dentry *dentry);
25426 +void aufs_write_unlock(struct dentry *dentry);
25427 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
25428 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
25429 +
25430 +int si_pid_test_slow(struct super_block *sb);
25431 +void si_pid_set_slow(struct super_block *sb);
25432 +void si_pid_clr_slow(struct super_block *sb);
25433 +
25434 +/* wbr_policy.c */
25435 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
25436 +extern struct au_wbr_create_operations au_wbr_create_ops[];
25437 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
25438 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
25439 +
25440 +/* mvdown.c */
25441 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
25442 +
25443 +/* ---------------------------------------------------------------------- */
25444 +
25445 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
25446 +{
25447 +       return sb->s_fs_info;
25448 +}
25449 +
25450 +/* ---------------------------------------------------------------------- */
25451 +
25452 +#ifdef CONFIG_AUFS_EXPORT
25453 +int au_test_nfsd(void);
25454 +void au_export_init(struct super_block *sb);
25455 +void au_xigen_inc(struct inode *inode);
25456 +int au_xigen_new(struct inode *inode);
25457 +int au_xigen_set(struct super_block *sb, struct file *base);
25458 +void au_xigen_clr(struct super_block *sb);
25459 +
25460 +static inline int au_busy_or_stale(void)
25461 +{
25462 +       if (!au_test_nfsd())
25463 +               return -EBUSY;
25464 +       return -ESTALE;
25465 +}
25466 +#else
25467 +AuStubInt0(au_test_nfsd, void)
25468 +AuStubVoid(au_export_init, struct super_block *sb)
25469 +AuStubVoid(au_xigen_inc, struct inode *inode)
25470 +AuStubInt0(au_xigen_new, struct inode *inode)
25471 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
25472 +AuStubVoid(au_xigen_clr, struct super_block *sb)
25473 +static inline int au_busy_or_stale(void)
25474 +{
25475 +       return -EBUSY;
25476 +}
25477 +#endif /* CONFIG_AUFS_EXPORT */
25478 +
25479 +/* ---------------------------------------------------------------------- */
25480 +
25481 +#ifdef CONFIG_AUFS_SBILIST
25482 +/* module.c */
25483 +extern struct au_splhead au_sbilist;
25484 +
25485 +static inline void au_sbilist_init(void)
25486 +{
25487 +       au_spl_init(&au_sbilist);
25488 +}
25489 +
25490 +static inline void au_sbilist_add(struct super_block *sb)
25491 +{
25492 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
25493 +}
25494 +
25495 +static inline void au_sbilist_del(struct super_block *sb)
25496 +{
25497 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
25498 +}
25499 +
25500 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
25501 +static inline void au_sbilist_lock(void)
25502 +{
25503 +       spin_lock(&au_sbilist.spin);
25504 +}
25505 +
25506 +static inline void au_sbilist_unlock(void)
25507 +{
25508 +       spin_unlock(&au_sbilist.spin);
25509 +}
25510 +#define AuGFP_SBILIST  GFP_ATOMIC
25511 +#else
25512 +AuStubVoid(au_sbilist_lock, void)
25513 +AuStubVoid(au_sbilist_unlock, void)
25514 +#define AuGFP_SBILIST  GFP_NOFS
25515 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
25516 +#else
25517 +AuStubVoid(au_sbilist_init, void)
25518 +AuStubVoid(au_sbilist_add, struct super_block*)
25519 +AuStubVoid(au_sbilist_del, struct super_block*)
25520 +AuStubVoid(au_sbilist_lock, void)
25521 +AuStubVoid(au_sbilist_unlock, void)
25522 +#define AuGFP_SBILIST  GFP_NOFS
25523 +#endif
25524 +
25525 +/* ---------------------------------------------------------------------- */
25526 +
25527 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
25528 +{
25529 +       /*
25530 +        * This function is a dynamic '__init' fucntion actually,
25531 +        * so the tiny check for si_rwsem is unnecessary.
25532 +        */
25533 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
25534 +#ifdef CONFIG_DEBUG_FS
25535 +       sbinfo->si_dbgaufs = NULL;
25536 +       sbinfo->si_dbgaufs_plink = NULL;
25537 +       sbinfo->si_dbgaufs_xib = NULL;
25538 +#ifdef CONFIG_AUFS_EXPORT
25539 +       sbinfo->si_dbgaufs_xigen = NULL;
25540 +#endif
25541 +#endif
25542 +}
25543 +
25544 +/* ---------------------------------------------------------------------- */
25545 +
25546 +static inline pid_t si_pid_bit(void)
25547 +{
25548 +       /* the origin of pid is 1, but the bitmap's is 0 */
25549 +       return current->pid - 1;
25550 +}
25551 +
25552 +static inline int si_pid_test(struct super_block *sb)
25553 +{
25554 +       pid_t bit = si_pid_bit();
25555 +       if (bit < PID_MAX_DEFAULT)
25556 +               return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
25557 +       else
25558 +               return si_pid_test_slow(sb);
25559 +}
25560 +
25561 +static inline void si_pid_set(struct super_block *sb)
25562 +{
25563 +       pid_t bit = si_pid_bit();
25564 +       if (bit < PID_MAX_DEFAULT) {
25565 +               AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
25566 +               set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
25567 +               /* smp_mb(); */
25568 +       } else
25569 +               si_pid_set_slow(sb);
25570 +}
25571 +
25572 +static inline void si_pid_clr(struct super_block *sb)
25573 +{
25574 +       pid_t bit = si_pid_bit();
25575 +       if (bit < PID_MAX_DEFAULT) {
25576 +               AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
25577 +               clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
25578 +               /* smp_mb(); */
25579 +       } else
25580 +               si_pid_clr_slow(sb);
25581 +}
25582 +
25583 +/* ---------------------------------------------------------------------- */
25584 +
25585 +/* lock superblock. mainly for entry point functions */
25586 +/*
25587 + * __si_read_lock, __si_write_lock,
25588 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
25589 + */
25590 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
25591 +
25592 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
25593 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
25594 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
25595 +
25596 +static inline void si_noflush_read_lock(struct super_block *sb)
25597 +{
25598 +       __si_read_lock(sb);
25599 +       si_pid_set(sb);
25600 +}
25601 +
25602 +static inline int si_noflush_read_trylock(struct super_block *sb)
25603 +{
25604 +       int locked = __si_read_trylock(sb);
25605 +       if (locked)
25606 +               si_pid_set(sb);
25607 +       return locked;
25608 +}
25609 +
25610 +static inline void si_noflush_write_lock(struct super_block *sb)
25611 +{
25612 +       __si_write_lock(sb);
25613 +       si_pid_set(sb);
25614 +}
25615 +
25616 +static inline int si_noflush_write_trylock(struct super_block *sb)
25617 +{
25618 +       int locked = __si_write_trylock(sb);
25619 +       if (locked)
25620 +               si_pid_set(sb);
25621 +       return locked;
25622 +}
25623 +
25624 +#if 0 /* unused */
25625 +static inline int si_read_trylock(struct super_block *sb, int flags)
25626 +{
25627 +       if (au_ftest_lock(flags, FLUSH))
25628 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
25629 +       return si_noflush_read_trylock(sb);
25630 +}
25631 +#endif
25632 +
25633 +static inline void si_read_unlock(struct super_block *sb)
25634 +{
25635 +       si_pid_clr(sb);
25636 +       __si_read_unlock(sb);
25637 +}
25638 +
25639 +#if 0 /* unused */
25640 +static inline int si_write_trylock(struct super_block *sb, int flags)
25641 +{
25642 +       if (au_ftest_lock(flags, FLUSH))
25643 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
25644 +       return si_noflush_write_trylock(sb);
25645 +}
25646 +#endif
25647 +
25648 +static inline void si_write_unlock(struct super_block *sb)
25649 +{
25650 +       si_pid_clr(sb);
25651 +       __si_write_unlock(sb);
25652 +}
25653 +
25654 +#if 0 /* unused */
25655 +static inline void si_downgrade_lock(struct super_block *sb)
25656 +{
25657 +       __si_downgrade_lock(sb);
25658 +}
25659 +#endif
25660 +
25661 +/* ---------------------------------------------------------------------- */
25662 +
25663 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
25664 +{
25665 +       SiMustAnyLock(sb);
25666 +       return au_sbi(sb)->si_bend;
25667 +}
25668 +
25669 +static inline unsigned int au_mntflags(struct super_block *sb)
25670 +{
25671 +       SiMustAnyLock(sb);
25672 +       return au_sbi(sb)->si_mntflags;
25673 +}
25674 +
25675 +static inline unsigned int au_sigen(struct super_block *sb)
25676 +{
25677 +       SiMustAnyLock(sb);
25678 +       return au_sbi(sb)->si_generation;
25679 +}
25680 +
25681 +static inline void au_ninodes_inc(struct super_block *sb)
25682 +{
25683 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
25684 +}
25685 +
25686 +static inline void au_ninodes_dec(struct super_block *sb)
25687 +{
25688 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
25689 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
25690 +}
25691 +
25692 +static inline void au_nfiles_inc(struct super_block *sb)
25693 +{
25694 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
25695 +}
25696 +
25697 +static inline void au_nfiles_dec(struct super_block *sb)
25698 +{
25699 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
25700 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
25701 +}
25702 +
25703 +static inline struct au_branch *au_sbr(struct super_block *sb,
25704 +                                      aufs_bindex_t bindex)
25705 +{
25706 +       SiMustAnyLock(sb);
25707 +       return au_sbi(sb)->si_branch[0 + bindex];
25708 +}
25709 +
25710 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
25711 +{
25712 +       SiMustWriteLock(sb);
25713 +       au_sbi(sb)->si_xino_brid = brid;
25714 +}
25715 +
25716 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
25717 +{
25718 +       SiMustAnyLock(sb);
25719 +       return au_sbi(sb)->si_xino_brid;
25720 +}
25721 +
25722 +#endif /* __KERNEL__ */
25723 +#endif /* __AUFS_SUPER_H__ */
25724 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
25725 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
25726 +++ linux/fs/aufs/sysaufs.c     2013-07-06 13:20:47.753531903 +0200
25727 @@ -0,0 +1,105 @@
25728 +/*
25729 + * Copyright (C) 2005-2013 Junjiro R. Okajima
25730 + *
25731 + * This program, aufs is free software; you can redistribute it and/or modify
25732 + * it under the terms of the GNU General Public License as published by
25733 + * the Free Software Foundation; either version 2 of the License, or
25734 + * (at your option) any later version.
25735 + *
25736 + * This program is distributed in the hope that it will be useful,
25737 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25738 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25739 + * GNU General Public License for more details.
25740 + *
25741 + * You should have received a copy of the GNU General Public License
25742 + * along with this program; if not, write to the Free Software
25743 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25744 + */
25745 +
25746 +/*
25747 + * sysfs interface and lifetime management
25748 + * they are necessary regardless sysfs is disabled.
25749 + */
25750 +
25751 +#include <linux/random.h>
25752 +#include "aufs.h"
25753 +
25754 +unsigned long sysaufs_si_mask;
25755 +struct kset *sysaufs_kset;
25756 +
25757 +#define AuSiAttr(_name) { \
25758 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
25759 +       .show   = sysaufs_si_##_name,                           \
25760 +}
25761 +
25762 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
25763 +struct attribute *sysaufs_si_attrs[] = {
25764 +       &sysaufs_si_attr_xi_path.attr,
25765 +       NULL,
25766 +};
25767 +
25768 +static const struct sysfs_ops au_sbi_ops = {
25769 +       .show   = sysaufs_si_show
25770 +};
25771 +
25772 +static struct kobj_type au_sbi_ktype = {
25773 +       .release        = au_si_free,
25774 +       .sysfs_ops      = &au_sbi_ops,
25775 +       .default_attrs  = sysaufs_si_attrs
25776 +};
25777 +
25778 +/* ---------------------------------------------------------------------- */
25779 +
25780 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
25781 +{
25782 +       int err;
25783 +
25784 +       sbinfo->si_kobj.kset = sysaufs_kset;
25785 +       /* cf. sysaufs_name() */
25786 +       err = kobject_init_and_add
25787 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
25788 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
25789 +
25790 +       dbgaufs_si_null(sbinfo);
25791 +       if (!err) {
25792 +               err = dbgaufs_si_init(sbinfo);
25793 +               if (unlikely(err))
25794 +                       kobject_put(&sbinfo->si_kobj);
25795 +       }
25796 +       return err;
25797 +}
25798 +
25799 +void sysaufs_fin(void)
25800 +{
25801 +       dbgaufs_fin();
25802 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
25803 +       kset_unregister(sysaufs_kset);
25804 +}
25805 +
25806 +int __init sysaufs_init(void)
25807 +{
25808 +       int err;
25809 +
25810 +       do {
25811 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
25812 +       } while (!sysaufs_si_mask);
25813 +
25814 +       err = -EINVAL;
25815 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
25816 +       if (unlikely(!sysaufs_kset))
25817 +               goto out;
25818 +       err = PTR_ERR(sysaufs_kset);
25819 +       if (IS_ERR(sysaufs_kset))
25820 +               goto out;
25821 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
25822 +       if (unlikely(err)) {
25823 +               kset_unregister(sysaufs_kset);
25824 +               goto out;
25825 +       }
25826 +
25827 +       err = dbgaufs_init();
25828 +       if (unlikely(err))
25829 +               sysaufs_fin();
25830 +out:
25831 +       return err;
25832 +}
25833 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
25834 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
25835 +++ linux/fs/aufs/sysaufs.h     2013-07-06 13:20:47.753531903 +0200
25836 @@ -0,0 +1,104 @@
25837 +/*
25838 + * Copyright (C) 2005-2013 Junjiro R. Okajima
25839 + *
25840 + * This program, aufs is free software; you can redistribute it and/or modify
25841 + * it under the terms of the GNU General Public License as published by
25842 + * the Free Software Foundation; either version 2 of the License, or
25843 + * (at your option) any later version.
25844 + *
25845 + * This program is distributed in the hope that it will be useful,
25846 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25847 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25848 + * GNU General Public License for more details.
25849 + *
25850 + * You should have received a copy of the GNU General Public License
25851 + * along with this program; if not, write to the Free Software
25852 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25853 + */
25854 +
25855 +/*
25856 + * sysfs interface and mount lifetime management
25857 + */
25858 +
25859 +#ifndef __SYSAUFS_H__
25860 +#define __SYSAUFS_H__
25861 +
25862 +#ifdef __KERNEL__
25863 +
25864 +#include <linux/sysfs.h>
25865 +#include "module.h"
25866 +
25867 +struct super_block;
25868 +struct au_sbinfo;
25869 +
25870 +struct sysaufs_si_attr {
25871 +       struct attribute attr;
25872 +       int (*show)(struct seq_file *seq, struct super_block *sb);
25873 +};
25874 +
25875 +/* ---------------------------------------------------------------------- */
25876 +
25877 +/* sysaufs.c */
25878 +extern unsigned long sysaufs_si_mask;
25879 +extern struct kset *sysaufs_kset;
25880 +extern struct attribute *sysaufs_si_attrs[];
25881 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
25882 +int __init sysaufs_init(void);
25883 +void sysaufs_fin(void);
25884 +
25885 +/* ---------------------------------------------------------------------- */
25886 +
25887 +/* some people doesn't like to show a pointer in kernel */
25888 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
25889 +{
25890 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
25891 +}
25892 +
25893 +#define SysaufsSiNamePrefix    "si_"
25894 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
25895 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
25896 +{
25897 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
25898 +                sysaufs_si_id(sbinfo));
25899 +}
25900 +
25901 +struct au_branch;
25902 +#ifdef CONFIG_SYSFS
25903 +/* sysfs.c */
25904 +extern struct attribute_group *sysaufs_attr_group;
25905 +
25906 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
25907 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
25908 +                        char *buf);
25909 +
25910 +void sysaufs_br_init(struct au_branch *br);
25911 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
25912 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
25913 +
25914 +#define sysaufs_brs_init()     do {} while (0)
25915 +
25916 +#else
25917 +#define sysaufs_attr_group     NULL
25918 +
25919 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
25920 +
25921 +static inline
25922 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
25923 +                        char *buf)
25924 +{
25925 +       return 0;
25926 +}
25927 +
25928 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
25929 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
25930 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
25931 +
25932 +static inline void sysaufs_brs_init(void)
25933 +{
25934 +       sysaufs_brs = 0;
25935 +}
25936 +
25937 +#endif /* CONFIG_SYSFS */
25938 +
25939 +#endif /* __KERNEL__ */
25940 +#endif /* __SYSAUFS_H__ */
25941 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
25942 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
25943 +++ linux/fs/aufs/sysfs.c       2013-07-06 13:20:47.753531903 +0200
25944 @@ -0,0 +1,257 @@
25945 +/*
25946 + * Copyright (C) 2005-2013 Junjiro R. Okajima
25947 + *
25948 + * This program, aufs is free software; you can redistribute it and/or modify
25949 + * it under the terms of the GNU General Public License as published by
25950 + * the Free Software Foundation; either version 2 of the License, or
25951 + * (at your option) any later version.
25952 + *
25953 + * This program is distributed in the hope that it will be useful,
25954 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25955 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25956 + * GNU General Public License for more details.
25957 + *
25958 + * You should have received a copy of the GNU General Public License
25959 + * along with this program; if not, write to the Free Software
25960 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25961 + */
25962 +
25963 +/*
25964 + * sysfs interface
25965 + */
25966 +
25967 +#include <linux/seq_file.h>
25968 +#include "aufs.h"
25969 +
25970 +#ifdef CONFIG_AUFS_FS_MODULE
25971 +/* this entry violates the "one line per file" policy of sysfs */
25972 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
25973 +                          char *buf)
25974 +{
25975 +       ssize_t err;
25976 +       static char *conf =
25977 +/* this file is generated at compiling */
25978 +#include "conf.str"
25979 +               ;
25980 +
25981 +       err = snprintf(buf, PAGE_SIZE, conf);
25982 +       if (unlikely(err >= PAGE_SIZE))
25983 +               err = -EFBIG;
25984 +       return err;
25985 +}
25986 +
25987 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
25988 +#endif
25989 +
25990 +static struct attribute *au_attr[] = {
25991 +#ifdef CONFIG_AUFS_FS_MODULE
25992 +       &au_config_attr.attr,
25993 +#endif
25994 +       NULL,   /* need to NULL terminate the list of attributes */
25995 +};
25996 +
25997 +static struct attribute_group sysaufs_attr_group_body = {
25998 +       .attrs = au_attr
25999 +};
26000 +
26001 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
26002 +
26003 +/* ---------------------------------------------------------------------- */
26004 +
26005 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
26006 +{
26007 +       int err;
26008 +
26009 +       SiMustAnyLock(sb);
26010 +
26011 +       err = 0;
26012 +       if (au_opt_test(au_mntflags(sb), XINO)) {
26013 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
26014 +               seq_putc(seq, '\n');
26015 +       }
26016 +       return err;
26017 +}
26018 +
26019 +/*
26020 + * the lifetime of branch is independent from the entry under sysfs.
26021 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
26022 + * unlinked.
26023 + */
26024 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
26025 +                        aufs_bindex_t bindex)
26026 +{
26027 +       int err;
26028 +       struct path path;
26029 +       struct dentry *root;
26030 +       struct au_branch *br;
26031 +       char *perm;
26032 +
26033 +       AuDbg("b%d\n", bindex);
26034 +
26035 +       err = 0;
26036 +       root = sb->s_root;
26037 +       di_read_lock_parent(root, !AuLock_IR);
26038 +       br = au_sbr(sb, bindex);
26039 +       path.mnt = au_br_mnt(br);
26040 +       path.dentry = au_h_dptr(root, bindex);
26041 +       au_seq_path(seq, &path);
26042 +       di_read_unlock(root, !AuLock_IR);
26043 +       perm = au_optstr_br_perm(br->br_perm);
26044 +       if (perm) {
26045 +               err = seq_printf(seq, "=%s\n", perm);
26046 +               kfree(perm);
26047 +               if (err == -1)
26048 +                       err = -E2BIG;
26049 +       } else
26050 +               err = -ENOMEM;
26051 +       return err;
26052 +}
26053 +
26054 +/* ---------------------------------------------------------------------- */
26055 +
26056 +static struct seq_file *au_seq(char *p, ssize_t len)
26057 +{
26058 +       struct seq_file *seq;
26059 +
26060 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
26061 +       if (seq) {
26062 +               /* mutex_init(&seq.lock); */
26063 +               seq->buf = p;
26064 +               seq->size = len;
26065 +               return seq; /* success */
26066 +       }
26067 +
26068 +       seq = ERR_PTR(-ENOMEM);
26069 +       return seq;
26070 +}
26071 +
26072 +#define SysaufsBr_PREFIX "br"
26073 +
26074 +/* todo: file size may exceed PAGE_SIZE */
26075 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
26076 +                       char *buf)
26077 +{
26078 +       ssize_t err;
26079 +       long l;
26080 +       aufs_bindex_t bend;
26081 +       struct au_sbinfo *sbinfo;
26082 +       struct super_block *sb;
26083 +       struct seq_file *seq;
26084 +       char *name;
26085 +       struct attribute **cattr;
26086 +
26087 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
26088 +       sb = sbinfo->si_sb;
26089 +
26090 +       /*
26091 +        * prevent a race condition between sysfs and aufs.
26092 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
26093 +        * prohibits maintaining the sysfs entries.
26094 +        * hew we acquire read lock after sysfs_get_active_two().
26095 +        * on the other hand, the remount process may maintain the sysfs/aufs
26096 +        * entries after acquiring write lock.
26097 +        * it can cause a deadlock.
26098 +        * simply we gave up processing read here.
26099 +        */
26100 +       err = -EBUSY;
26101 +       if (unlikely(!si_noflush_read_trylock(sb)))
26102 +               goto out;
26103 +
26104 +       seq = au_seq(buf, PAGE_SIZE);
26105 +       err = PTR_ERR(seq);
26106 +       if (IS_ERR(seq))
26107 +               goto out_unlock;
26108 +
26109 +       name = (void *)attr->name;
26110 +       cattr = sysaufs_si_attrs;
26111 +       while (*cattr) {
26112 +               if (!strcmp(name, (*cattr)->name)) {
26113 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
26114 +                               ->show(seq, sb);
26115 +                       goto out_seq;
26116 +               }
26117 +               cattr++;
26118 +       }
26119 +
26120 +       bend = au_sbend(sb);
26121 +       if (!strncmp(name, SysaufsBr_PREFIX, sizeof(SysaufsBr_PREFIX) - 1)) {
26122 +               name += sizeof(SysaufsBr_PREFIX) - 1;
26123 +               err = kstrtol(name, 10, &l);
26124 +               if (!err) {
26125 +                       if (l <= bend)
26126 +                               err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l);
26127 +                       else
26128 +                               err = -ENOENT;
26129 +               }
26130 +               goto out_seq;
26131 +       }
26132 +       BUG();
26133 +
26134 +out_seq:
26135 +       if (!err) {
26136 +               err = seq->count;
26137 +               /* sysfs limit */
26138 +               if (unlikely(err == PAGE_SIZE))
26139 +                       err = -EFBIG;
26140 +       }
26141 +       kfree(seq);
26142 +out_unlock:
26143 +       si_read_unlock(sb);
26144 +out:
26145 +       return err;
26146 +}
26147 +
26148 +/* ---------------------------------------------------------------------- */
26149 +
26150 +void sysaufs_br_init(struct au_branch *br)
26151 +{
26152 +       struct attribute *attr = &br->br_attr;
26153 +
26154 +       sysfs_attr_init(attr);
26155 +       attr->name = br->br_name;
26156 +       attr->mode = S_IRUGO;
26157 +}
26158 +
26159 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
26160 +{
26161 +       struct au_branch *br;
26162 +       struct kobject *kobj;
26163 +       aufs_bindex_t bend;
26164 +
26165 +       dbgaufs_brs_del(sb, bindex);
26166 +
26167 +       if (!sysaufs_brs)
26168 +               return;
26169 +
26170 +       kobj = &au_sbi(sb)->si_kobj;
26171 +       bend = au_sbend(sb);
26172 +       for (; bindex <= bend; bindex++) {
26173 +               br = au_sbr(sb, bindex);
26174 +               sysfs_remove_file(kobj, &br->br_attr);
26175 +       }
26176 +}
26177 +
26178 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
26179 +{
26180 +       int err;
26181 +       aufs_bindex_t bend;
26182 +       struct kobject *kobj;
26183 +       struct au_branch *br;
26184 +
26185 +       dbgaufs_brs_add(sb, bindex);
26186 +
26187 +       if (!sysaufs_brs)
26188 +               return;
26189 +
26190 +       kobj = &au_sbi(sb)->si_kobj;
26191 +       bend = au_sbend(sb);
26192 +       for (; bindex <= bend; bindex++) {
26193 +               br = au_sbr(sb, bindex);
26194 +               snprintf(br->br_name, sizeof(br->br_name), SysaufsBr_PREFIX
26195 +                        "%d", bindex);
26196 +               err = sysfs_create_file(kobj, &br->br_attr);
26197 +               if (unlikely(err))
26198 +                       pr_warn("failed %s under sysfs(%d)\n",
26199 +                               br->br_name, err);
26200 +       }
26201 +}
26202 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
26203 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
26204 +++ linux/fs/aufs/sysrq.c       2013-07-06 13:20:47.753531903 +0200
26205 @@ -0,0 +1,151 @@
26206 +/*
26207 + * Copyright (C) 2005-2013 Junjiro R. Okajima
26208 + *
26209 + * This program, aufs is free software; you can redistribute it and/or modify
26210 + * it under the terms of the GNU General Public License as published by
26211 + * the Free Software Foundation; either version 2 of the License, or
26212 + * (at your option) any later version.
26213 + *
26214 + * This program is distributed in the hope that it will be useful,
26215 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26216 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26217 + * GNU General Public License for more details.
26218 + *
26219 + * You should have received a copy of the GNU General Public License
26220 + * along with this program; if not, write to the Free Software
26221 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26222 + */
26223 +
26224 +/*
26225 + * magic sysrq hanlder
26226 + */
26227 +
26228 +/* #include <linux/sysrq.h> */
26229 +#include <linux/writeback.h>
26230 +#include "aufs.h"
26231 +
26232 +/* ---------------------------------------------------------------------- */
26233 +
26234 +static void sysrq_sb(struct super_block *sb)
26235 +{
26236 +       char *plevel;
26237 +       struct au_sbinfo *sbinfo;
26238 +       struct file *file;
26239 +
26240 +       plevel = au_plevel;
26241 +       au_plevel = KERN_WARNING;
26242 +
26243 +       /* since we define pr_fmt, call printk directly */
26244 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
26245 +
26246 +       sbinfo = au_sbi(sb);
26247 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
26248 +       pr("superblock\n");
26249 +       au_dpri_sb(sb);
26250 +
26251 +#if 0
26252 +       pr("root dentry\n");
26253 +       au_dpri_dentry(sb->s_root);
26254 +       pr("root inode\n");
26255 +       au_dpri_inode(sb->s_root->d_inode);
26256 +#endif
26257 +
26258 +#if 0
26259 +       do {
26260 +               int err, i, j, ndentry;
26261 +               struct au_dcsub_pages dpages;
26262 +               struct au_dpage *dpage;
26263 +
26264 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
26265 +               if (unlikely(err))
26266 +                       break;
26267 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
26268 +               if (!err)
26269 +                       for (i = 0; i < dpages.ndpage; i++) {
26270 +                               dpage = dpages.dpages + i;
26271 +                               ndentry = dpage->ndentry;
26272 +                               for (j = 0; j < ndentry; j++)
26273 +                                       au_dpri_dentry(dpage->dentries[j]);
26274 +                       }
26275 +               au_dpages_free(&dpages);
26276 +       } while (0);
26277 +#endif
26278 +
26279 +#if 1
26280 +       {
26281 +               struct inode *i;
26282 +               pr("isolated inode\n");
26283 +               spin_lock(&inode_sb_list_lock);
26284 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
26285 +                       spin_lock(&i->i_lock);
26286 +                       if (1 || hlist_empty(&i->i_dentry))
26287 +                               au_dpri_inode(i);
26288 +                       spin_unlock(&i->i_lock);
26289 +               }
26290 +               spin_unlock(&inode_sb_list_lock);
26291 +       }
26292 +#endif
26293 +       pr("files\n");
26294 +       lg_global_lock(&files_lglock);
26295 +       do_file_list_for_each_entry(sb, file) {
26296 +               umode_t mode;
26297 +               mode = file_inode(file)->i_mode;
26298 +               if (!special_file(mode) || au_special_file(mode))
26299 +                       au_dpri_file(file);
26300 +       } while_file_list_for_each_entry;
26301 +       lg_global_unlock(&files_lglock);
26302 +       pr("done\n");
26303 +
26304 +#undef pr
26305 +       au_plevel = plevel;
26306 +}
26307 +
26308 +/* ---------------------------------------------------------------------- */
26309 +
26310 +/* module parameter */
26311 +static char *aufs_sysrq_key = "a";
26312 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
26313 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
26314 +
26315 +static void au_sysrq(int key __maybe_unused)
26316 +{
26317 +       struct au_sbinfo *sbinfo;
26318 +
26319 +       lockdep_off();
26320 +       au_sbilist_lock();
26321 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
26322 +               sysrq_sb(sbinfo->si_sb);
26323 +       au_sbilist_unlock();
26324 +       lockdep_on();
26325 +}
26326 +
26327 +static struct sysrq_key_op au_sysrq_op = {
26328 +       .handler        = au_sysrq,
26329 +       .help_msg       = "Aufs",
26330 +       .action_msg     = "Aufs",
26331 +       .enable_mask    = SYSRQ_ENABLE_DUMP
26332 +};
26333 +
26334 +/* ---------------------------------------------------------------------- */
26335 +
26336 +int __init au_sysrq_init(void)
26337 +{
26338 +       int err;
26339 +       char key;
26340 +
26341 +       err = -1;
26342 +       key = *aufs_sysrq_key;
26343 +       if ('a' <= key && key <= 'z')
26344 +               err = register_sysrq_key(key, &au_sysrq_op);
26345 +       if (unlikely(err))
26346 +               pr_err("err %d, sysrq=%c\n", err, key);
26347 +       return err;
26348 +}
26349 +
26350 +void au_sysrq_fin(void)
26351 +{
26352 +       int err;
26353 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
26354 +       if (unlikely(err))
26355 +               pr_err("err %d (ignored)\n", err);
26356 +}
26357 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
26358 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
26359 +++ linux/fs/aufs/vdir.c        2013-07-30 22:42:55.842946719 +0200
26360 @@ -0,0 +1,878 @@
26361 +/*
26362 + * Copyright (C) 2005-2013 Junjiro R. Okajima
26363 + *
26364 + * This program, aufs is free software; you can redistribute it and/or modify
26365 + * it under the terms of the GNU General Public License as published by
26366 + * the Free Software Foundation; either version 2 of the License, or
26367 + * (at your option) any later version.
26368 + *
26369 + * This program is distributed in the hope that it will be useful,
26370 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26371 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26372 + * GNU General Public License for more details.
26373 + *
26374 + * You should have received a copy of the GNU General Public License
26375 + * along with this program; if not, write to the Free Software
26376 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26377 + */
26378 +
26379 +/*
26380 + * virtual or vertical directory
26381 + */
26382 +
26383 +#include "aufs.h"
26384 +
26385 +static unsigned int calc_size(int nlen)
26386 +{
26387 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
26388 +}
26389 +
26390 +static int set_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 +               p->de->de_str.len = 0;
26395 +               /* smp_mb(); */
26396 +               return 0;
26397 +       }
26398 +       return -1; /* error */
26399 +}
26400 +
26401 +/* returns true or false */
26402 +static int is_deblk_end(union au_vdir_deblk_p *p,
26403 +                       union au_vdir_deblk_p *deblk_end)
26404 +{
26405 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
26406 +               return !p->de->de_str.len;
26407 +       return 1;
26408 +}
26409 +
26410 +static unsigned char *last_deblk(struct au_vdir *vdir)
26411 +{
26412 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
26413 +}
26414 +
26415 +/* ---------------------------------------------------------------------- */
26416 +
26417 +/* estimate the apropriate size for name hash table */
26418 +unsigned int au_rdhash_est(loff_t sz)
26419 +{
26420 +       unsigned int n;
26421 +
26422 +       n = UINT_MAX;
26423 +       sz >>= 10;
26424 +       if (sz < n)
26425 +               n = sz;
26426 +       if (sz < AUFS_RDHASH_DEF)
26427 +               n = AUFS_RDHASH_DEF;
26428 +       /* pr_info("n %u\n", n); */
26429 +       return n;
26430 +}
26431 +
26432 +/*
26433 + * the allocated memory has to be freed by
26434 + * au_nhash_wh_free() or au_nhash_de_free().
26435 + */
26436 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
26437 +{
26438 +       struct hlist_head *head;
26439 +       unsigned int u;
26440 +
26441 +       head = kmalloc(sizeof(*nhash->nh_head) * num_hash, gfp);
26442 +       if (head) {
26443 +               nhash->nh_num = num_hash;
26444 +               nhash->nh_head = head;
26445 +               for (u = 0; u < num_hash; u++)
26446 +                       INIT_HLIST_HEAD(head++);
26447 +               return 0; /* success */
26448 +       }
26449 +
26450 +       return -ENOMEM;
26451 +}
26452 +
26453 +static void nhash_count(struct hlist_head *head)
26454 +{
26455 +#if 0
26456 +       unsigned long n;
26457 +       struct hlist_node *pos;
26458 +
26459 +       n = 0;
26460 +       hlist_for_each(pos, head)
26461 +               n++;
26462 +       pr_info("%lu\n", n);
26463 +#endif
26464 +}
26465 +
26466 +static void au_nhash_wh_do_free(struct hlist_head *head)
26467 +{
26468 +       struct au_vdir_wh *pos;
26469 +       struct hlist_node *node;
26470 +
26471 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
26472 +               kfree(pos);
26473 +}
26474 +
26475 +static void au_nhash_de_do_free(struct hlist_head *head)
26476 +{
26477 +       struct au_vdir_dehstr *pos;
26478 +       struct hlist_node *node;
26479 +
26480 +       hlist_for_each_entry_safe(pos, node, head, hash)
26481 +               au_cache_free_vdir_dehstr(pos);
26482 +}
26483 +
26484 +static void au_nhash_do_free(struct au_nhash *nhash,
26485 +                            void (*free)(struct hlist_head *head))
26486 +{
26487 +       unsigned int n;
26488 +       struct hlist_head *head;
26489 +
26490 +       n = nhash->nh_num;
26491 +       if (!n)
26492 +               return;
26493 +
26494 +       head = nhash->nh_head;
26495 +       while (n-- > 0) {
26496 +               nhash_count(head);
26497 +               free(head++);
26498 +       }
26499 +       kfree(nhash->nh_head);
26500 +}
26501 +
26502 +void au_nhash_wh_free(struct au_nhash *whlist)
26503 +{
26504 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
26505 +}
26506 +
26507 +static void au_nhash_de_free(struct au_nhash *delist)
26508 +{
26509 +       au_nhash_do_free(delist, au_nhash_de_do_free);
26510 +}
26511 +
26512 +/* ---------------------------------------------------------------------- */
26513 +
26514 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
26515 +                           int limit)
26516 +{
26517 +       int num;
26518 +       unsigned int u, n;
26519 +       struct hlist_head *head;
26520 +       struct au_vdir_wh *pos;
26521 +
26522 +       num = 0;
26523 +       n = whlist->nh_num;
26524 +       head = whlist->nh_head;
26525 +       for (u = 0; u < n; u++, head++)
26526 +               hlist_for_each_entry(pos, head, wh_hash)
26527 +                       if (pos->wh_bindex == btgt && ++num > limit)
26528 +                               return 1;
26529 +       return 0;
26530 +}
26531 +
26532 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
26533 +                                      unsigned char *name,
26534 +                                      unsigned int len)
26535 +{
26536 +       unsigned int v;
26537 +       /* const unsigned int magic_bit = 12; */
26538 +
26539 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
26540 +
26541 +       v = 0;
26542 +       while (len--)
26543 +               v += *name++;
26544 +       /* v = hash_long(v, magic_bit); */
26545 +       v %= nhash->nh_num;
26546 +       return nhash->nh_head + v;
26547 +}
26548 +
26549 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
26550 +                             int nlen)
26551 +{
26552 +       return str->len == nlen && !memcmp(str->name, name, nlen);
26553 +}
26554 +
26555 +/* returns found or not */
26556 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
26557 +{
26558 +       struct hlist_head *head;
26559 +       struct au_vdir_wh *pos;
26560 +       struct au_vdir_destr *str;
26561 +
26562 +       head = au_name_hash(whlist, name, nlen);
26563 +       hlist_for_each_entry(pos, head, wh_hash) {
26564 +               str = &pos->wh_str;
26565 +               AuDbg("%.*s\n", str->len, str->name);
26566 +               if (au_nhash_test_name(str, name, nlen))
26567 +                       return 1;
26568 +       }
26569 +       return 0;
26570 +}
26571 +
26572 +/* returns found(true) or not */
26573 +static int test_known(struct au_nhash *delist, char *name, int nlen)
26574 +{
26575 +       struct hlist_head *head;
26576 +       struct au_vdir_dehstr *pos;
26577 +       struct au_vdir_destr *str;
26578 +
26579 +       head = au_name_hash(delist, name, nlen);
26580 +       hlist_for_each_entry(pos, head, hash) {
26581 +               str = pos->str;
26582 +               AuDbg("%.*s\n", str->len, str->name);
26583 +               if (au_nhash_test_name(str, name, nlen))
26584 +                       return 1;
26585 +       }
26586 +       return 0;
26587 +}
26588 +
26589 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
26590 +                           unsigned char d_type)
26591 +{
26592 +#ifdef CONFIG_AUFS_SHWH
26593 +       wh->wh_ino = ino;
26594 +       wh->wh_type = d_type;
26595 +#endif
26596 +}
26597 +
26598 +/* ---------------------------------------------------------------------- */
26599 +
26600 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
26601 +                      unsigned int d_type, aufs_bindex_t bindex,
26602 +                      unsigned char shwh)
26603 +{
26604 +       int err;
26605 +       struct au_vdir_destr *str;
26606 +       struct au_vdir_wh *wh;
26607 +
26608 +       AuDbg("%.*s\n", nlen, name);
26609 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
26610 +
26611 +       err = -ENOMEM;
26612 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
26613 +       if (unlikely(!wh))
26614 +               goto out;
26615 +
26616 +       err = 0;
26617 +       wh->wh_bindex = bindex;
26618 +       if (shwh)
26619 +               au_shwh_init_wh(wh, ino, d_type);
26620 +       str = &wh->wh_str;
26621 +       str->len = nlen;
26622 +       memcpy(str->name, name, nlen);
26623 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
26624 +       /* smp_mb(); */
26625 +
26626 +out:
26627 +       return err;
26628 +}
26629 +
26630 +static int append_deblk(struct au_vdir *vdir)
26631 +{
26632 +       int err;
26633 +       unsigned long ul;
26634 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
26635 +       union au_vdir_deblk_p p, deblk_end;
26636 +       unsigned char **o;
26637 +
26638 +       err = -ENOMEM;
26639 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
26640 +                    GFP_NOFS);
26641 +       if (unlikely(!o))
26642 +               goto out;
26643 +
26644 +       vdir->vd_deblk = o;
26645 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
26646 +       if (p.deblk) {
26647 +               ul = vdir->vd_nblk++;
26648 +               vdir->vd_deblk[ul] = p.deblk;
26649 +               vdir->vd_last.ul = ul;
26650 +               vdir->vd_last.p.deblk = p.deblk;
26651 +               deblk_end.deblk = p.deblk + deblk_sz;
26652 +               err = set_deblk_end(&p, &deblk_end);
26653 +       }
26654 +
26655 +out:
26656 +       return err;
26657 +}
26658 +
26659 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
26660 +                    unsigned int d_type, struct au_nhash *delist)
26661 +{
26662 +       int err;
26663 +       unsigned int sz;
26664 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
26665 +       union au_vdir_deblk_p p, *room, deblk_end;
26666 +       struct au_vdir_dehstr *dehstr;
26667 +
26668 +       p.deblk = last_deblk(vdir);
26669 +       deblk_end.deblk = p.deblk + deblk_sz;
26670 +       room = &vdir->vd_last.p;
26671 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
26672 +                 || !is_deblk_end(room, &deblk_end));
26673 +
26674 +       sz = calc_size(nlen);
26675 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
26676 +               err = append_deblk(vdir);
26677 +               if (unlikely(err))
26678 +                       goto out;
26679 +
26680 +               p.deblk = last_deblk(vdir);
26681 +               deblk_end.deblk = p.deblk + deblk_sz;
26682 +               /* smp_mb(); */
26683 +               AuDebugOn(room->deblk != p.deblk);
26684 +       }
26685 +
26686 +       err = -ENOMEM;
26687 +       dehstr = au_cache_alloc_vdir_dehstr();
26688 +       if (unlikely(!dehstr))
26689 +               goto out;
26690 +
26691 +       dehstr->str = &room->de->de_str;
26692 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
26693 +       room->de->de_ino = ino;
26694 +       room->de->de_type = d_type;
26695 +       room->de->de_str.len = nlen;
26696 +       memcpy(room->de->de_str.name, name, nlen);
26697 +
26698 +       err = 0;
26699 +       room->deblk += sz;
26700 +       if (unlikely(set_deblk_end(room, &deblk_end)))
26701 +               err = append_deblk(vdir);
26702 +       /* smp_mb(); */
26703 +
26704 +out:
26705 +       return err;
26706 +}
26707 +
26708 +/* ---------------------------------------------------------------------- */
26709 +
26710 +void au_vdir_free(struct au_vdir *vdir)
26711 +{
26712 +       unsigned char **deblk;
26713 +
26714 +       deblk = vdir->vd_deblk;
26715 +       while (vdir->vd_nblk--)
26716 +               kfree(*deblk++);
26717 +       kfree(vdir->vd_deblk);
26718 +       au_cache_free_vdir(vdir);
26719 +}
26720 +
26721 +static struct au_vdir *alloc_vdir(struct file *file)
26722 +{
26723 +       struct au_vdir *vdir;
26724 +       struct super_block *sb;
26725 +       int err;
26726 +
26727 +       sb = file->f_dentry->d_sb;
26728 +       SiMustAnyLock(sb);
26729 +
26730 +       err = -ENOMEM;
26731 +       vdir = au_cache_alloc_vdir();
26732 +       if (unlikely(!vdir))
26733 +               goto out;
26734 +
26735 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
26736 +       if (unlikely(!vdir->vd_deblk))
26737 +               goto out_free;
26738 +
26739 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
26740 +       if (!vdir->vd_deblk_sz) {
26741 +               /* estimate the apropriate size for deblk */
26742 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
26743 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
26744 +       }
26745 +       vdir->vd_nblk = 0;
26746 +       vdir->vd_version = 0;
26747 +       vdir->vd_jiffy = 0;
26748 +       err = append_deblk(vdir);
26749 +       if (!err)
26750 +               return vdir; /* success */
26751 +
26752 +       kfree(vdir->vd_deblk);
26753 +
26754 +out_free:
26755 +       au_cache_free_vdir(vdir);
26756 +out:
26757 +       vdir = ERR_PTR(err);
26758 +       return vdir;
26759 +}
26760 +
26761 +static int reinit_vdir(struct au_vdir *vdir)
26762 +{
26763 +       int err;
26764 +       union au_vdir_deblk_p p, deblk_end;
26765 +
26766 +       while (vdir->vd_nblk > 1) {
26767 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
26768 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
26769 +               vdir->vd_nblk--;
26770 +       }
26771 +       p.deblk = vdir->vd_deblk[0];
26772 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
26773 +       err = set_deblk_end(&p, &deblk_end);
26774 +       /* keep vd_dblk_sz */
26775 +       vdir->vd_last.ul = 0;
26776 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
26777 +       vdir->vd_version = 0;
26778 +       vdir->vd_jiffy = 0;
26779 +       /* smp_mb(); */
26780 +       return err;
26781 +}
26782 +
26783 +/* ---------------------------------------------------------------------- */
26784 +
26785 +#define AuFillVdir_CALLED      1
26786 +#define AuFillVdir_WHABLE      (1 << 1)
26787 +#define AuFillVdir_SHWH                (1 << 2)
26788 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
26789 +#define au_fset_fillvdir(flags, name) \
26790 +       do { (flags) |= AuFillVdir_##name; } while (0)
26791 +#define au_fclr_fillvdir(flags, name) \
26792 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
26793 +
26794 +#ifndef CONFIG_AUFS_SHWH
26795 +#undef AuFillVdir_SHWH
26796 +#define AuFillVdir_SHWH                0
26797 +#endif
26798 +
26799 +struct fillvdir_arg {
26800 +       struct file             *file;
26801 +       struct au_vdir          *vdir;
26802 +       struct au_nhash         delist;
26803 +       struct au_nhash         whlist;
26804 +       aufs_bindex_t           bindex;
26805 +       unsigned int            flags;
26806 +       int                     err;
26807 +};
26808 +
26809 +static int fillvdir(void *__arg, const char *__name, int nlen,
26810 +                   loff_t offset __maybe_unused, u64 h_ino,
26811 +                   unsigned int d_type)
26812 +{
26813 +       struct fillvdir_arg *arg = __arg;
26814 +       char *name = (void *)__name;
26815 +       struct super_block *sb;
26816 +       ino_t ino;
26817 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
26818 +
26819 +       arg->err = 0;
26820 +       sb = arg->file->f_dentry->d_sb;
26821 +       au_fset_fillvdir(arg->flags, CALLED);
26822 +       /* smp_mb(); */
26823 +       if (nlen <= AUFS_WH_PFX_LEN
26824 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
26825 +               if (test_known(&arg->delist, name, nlen)
26826 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
26827 +                       goto out; /* already exists or whiteouted */
26828 +
26829 +               sb = arg->file->f_dentry->d_sb;
26830 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
26831 +               if (!arg->err) {
26832 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
26833 +                               d_type = DT_UNKNOWN;
26834 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
26835 +                                            d_type, &arg->delist);
26836 +               }
26837 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
26838 +               name += AUFS_WH_PFX_LEN;
26839 +               nlen -= AUFS_WH_PFX_LEN;
26840 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
26841 +                       goto out; /* already whiteouted */
26842 +
26843 +               if (shwh)
26844 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
26845 +                                            &ino);
26846 +               if (!arg->err) {
26847 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
26848 +                               d_type = DT_UNKNOWN;
26849 +                       arg->err = au_nhash_append_wh
26850 +                               (&arg->whlist, name, nlen, ino, d_type,
26851 +                                arg->bindex, shwh);
26852 +               }
26853 +       }
26854 +
26855 +out:
26856 +       if (!arg->err)
26857 +               arg->vdir->vd_jiffy = jiffies;
26858 +       /* smp_mb(); */
26859 +       AuTraceErr(arg->err);
26860 +       return arg->err;
26861 +}
26862 +
26863 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
26864 +                         struct au_nhash *whlist, struct au_nhash *delist)
26865 +{
26866 +#ifdef CONFIG_AUFS_SHWH
26867 +       int err;
26868 +       unsigned int nh, u;
26869 +       struct hlist_head *head;
26870 +       struct au_vdir_wh *pos;
26871 +       struct hlist_node *n;
26872 +       char *p, *o;
26873 +       struct au_vdir_destr *destr;
26874 +
26875 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
26876 +
26877 +       err = -ENOMEM;
26878 +       o = p = (void *)__get_free_page(GFP_NOFS);
26879 +       if (unlikely(!p))
26880 +               goto out;
26881 +
26882 +       err = 0;
26883 +       nh = whlist->nh_num;
26884 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
26885 +       p += AUFS_WH_PFX_LEN;
26886 +       for (u = 0; u < nh; u++) {
26887 +               head = whlist->nh_head + u;
26888 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
26889 +                       destr = &pos->wh_str;
26890 +                       memcpy(p, destr->name, destr->len);
26891 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
26892 +                                       pos->wh_ino, pos->wh_type, delist);
26893 +                       if (unlikely(err))
26894 +                               break;
26895 +               }
26896 +       }
26897 +
26898 +       free_page((unsigned long)o);
26899 +
26900 +out:
26901 +       AuTraceErr(err);
26902 +       return err;
26903 +#else
26904 +       return 0;
26905 +#endif
26906 +}
26907 +
26908 +static int au_do_read_vdir(struct fillvdir_arg *arg)
26909 +{
26910 +       int err;
26911 +       unsigned int rdhash;
26912 +       loff_t offset;
26913 +       aufs_bindex_t bend, bindex, bstart;
26914 +       unsigned char shwh;
26915 +       struct file *hf, *file;
26916 +       struct super_block *sb;
26917 +
26918 +       file = arg->file;
26919 +       sb = file->f_dentry->d_sb;
26920 +       SiMustAnyLock(sb);
26921 +
26922 +       rdhash = au_sbi(sb)->si_rdhash;
26923 +       if (!rdhash)
26924 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
26925 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
26926 +       if (unlikely(err))
26927 +               goto out;
26928 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
26929 +       if (unlikely(err))
26930 +               goto out_delist;
26931 +
26932 +       err = 0;
26933 +       arg->flags = 0;
26934 +       shwh = 0;
26935 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
26936 +               shwh = 1;
26937 +               au_fset_fillvdir(arg->flags, SHWH);
26938 +       }
26939 +       bstart = au_fbstart(file);
26940 +       bend = au_fbend_dir(file);
26941 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
26942 +               hf = au_hf_dir(file, bindex);
26943 +               if (!hf)
26944 +                       continue;
26945 +
26946 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
26947 +               err = offset;
26948 +               if (unlikely(offset))
26949 +                       break;
26950 +
26951 +               arg->bindex = bindex;
26952 +               au_fclr_fillvdir(arg->flags, WHABLE);
26953 +               if (shwh
26954 +                   || (bindex != bend
26955 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
26956 +                       au_fset_fillvdir(arg->flags, WHABLE);
26957 +               do {
26958 +                       arg->err = 0;
26959 +                       au_fclr_fillvdir(arg->flags, CALLED);
26960 +                       /* smp_mb(); */
26961 +                       err = vfsub_readdir(hf, fillvdir, arg);
26962 +                       if (err >= 0)
26963 +                               err = arg->err;
26964 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
26965 +       }
26966 +
26967 +       if (!err && shwh)
26968 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
26969 +
26970 +       au_nhash_wh_free(&arg->whlist);
26971 +
26972 +out_delist:
26973 +       au_nhash_de_free(&arg->delist);
26974 +out:
26975 +       return err;
26976 +}
26977 +
26978 +static int read_vdir(struct file *file, int may_read)
26979 +{
26980 +       int err;
26981 +       unsigned long expire;
26982 +       unsigned char do_read;
26983 +       struct fillvdir_arg arg;
26984 +       struct inode *inode;
26985 +       struct au_vdir *vdir, *allocated;
26986 +
26987 +       err = 0;
26988 +       inode = file_inode(file);
26989 +       IMustLock(inode);
26990 +       SiMustAnyLock(inode->i_sb);
26991 +
26992 +       allocated = NULL;
26993 +       do_read = 0;
26994 +       expire = au_sbi(inode->i_sb)->si_rdcache;
26995 +       vdir = au_ivdir(inode);
26996 +       if (!vdir) {
26997 +               do_read = 1;
26998 +               vdir = alloc_vdir(file);
26999 +               err = PTR_ERR(vdir);
27000 +               if (IS_ERR(vdir))
27001 +                       goto out;
27002 +               err = 0;
27003 +               allocated = vdir;
27004 +       } else if (may_read
27005 +                  && (inode->i_version != vdir->vd_version
27006 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
27007 +               do_read = 1;
27008 +               err = reinit_vdir(vdir);
27009 +               if (unlikely(err))
27010 +                       goto out;
27011 +       }
27012 +
27013 +       if (!do_read)
27014 +               return 0; /* success */
27015 +
27016 +       arg.file = file;
27017 +       arg.vdir = vdir;
27018 +       err = au_do_read_vdir(&arg);
27019 +       if (!err) {
27020 +               /* file->f_pos = 0; */
27021 +               vdir->vd_version = inode->i_version;
27022 +               vdir->vd_last.ul = 0;
27023 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
27024 +               if (allocated)
27025 +                       au_set_ivdir(inode, allocated);
27026 +       } else if (allocated)
27027 +               au_vdir_free(allocated);
27028 +
27029 +out:
27030 +       return err;
27031 +}
27032 +
27033 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
27034 +{
27035 +       int err, rerr;
27036 +       unsigned long ul, n;
27037 +       const unsigned int deblk_sz = src->vd_deblk_sz;
27038 +
27039 +       AuDebugOn(tgt->vd_nblk != 1);
27040 +
27041 +       err = -ENOMEM;
27042 +       if (tgt->vd_nblk < src->vd_nblk) {
27043 +               unsigned char **p;
27044 +
27045 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
27046 +                            GFP_NOFS);
27047 +               if (unlikely(!p))
27048 +                       goto out;
27049 +               tgt->vd_deblk = p;
27050 +       }
27051 +
27052 +       if (tgt->vd_deblk_sz != deblk_sz) {
27053 +               unsigned char *p;
27054 +
27055 +               tgt->vd_deblk_sz = deblk_sz;
27056 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
27057 +               if (unlikely(!p))
27058 +                       goto out;
27059 +               tgt->vd_deblk[0] = p;
27060 +       }
27061 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
27062 +       tgt->vd_version = src->vd_version;
27063 +       tgt->vd_jiffy = src->vd_jiffy;
27064 +
27065 +       n = src->vd_nblk;
27066 +       for (ul = 1; ul < n; ul++) {
27067 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
27068 +                                           GFP_NOFS);
27069 +               if (unlikely(!tgt->vd_deblk[ul]))
27070 +                       goto out;
27071 +               tgt->vd_nblk++;
27072 +       }
27073 +       tgt->vd_nblk = n;
27074 +       tgt->vd_last.ul = tgt->vd_last.ul;
27075 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
27076 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
27077 +               - src->vd_deblk[src->vd_last.ul];
27078 +       /* smp_mb(); */
27079 +       return 0; /* success */
27080 +
27081 +out:
27082 +       rerr = reinit_vdir(tgt);
27083 +       BUG_ON(rerr);
27084 +       return err;
27085 +}
27086 +
27087 +int au_vdir_init(struct file *file)
27088 +{
27089 +       int err;
27090 +       struct inode *inode;
27091 +       struct au_vdir *vdir_cache, *allocated;
27092 +
27093 +       err = read_vdir(file, !file->f_pos);
27094 +       if (unlikely(err))
27095 +               goto out;
27096 +
27097 +       allocated = NULL;
27098 +       vdir_cache = au_fvdir_cache(file);
27099 +       if (!vdir_cache) {
27100 +               vdir_cache = alloc_vdir(file);
27101 +               err = PTR_ERR(vdir_cache);
27102 +               if (IS_ERR(vdir_cache))
27103 +                       goto out;
27104 +               allocated = vdir_cache;
27105 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
27106 +               err = reinit_vdir(vdir_cache);
27107 +               if (unlikely(err))
27108 +                       goto out;
27109 +       } else
27110 +               return 0; /* success */
27111 +
27112 +       inode = file_inode(file);
27113 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
27114 +       if (!err) {
27115 +               file->f_version = inode->i_version;
27116 +               if (allocated)
27117 +                       au_set_fvdir_cache(file, allocated);
27118 +       } else if (allocated)
27119 +               au_vdir_free(allocated);
27120 +
27121 +out:
27122 +       return err;
27123 +}
27124 +
27125 +static loff_t calc_offset(struct au_vdir *vdir)
27126 +{
27127 +       loff_t offset;
27128 +       union au_vdir_deblk_p p;
27129 +
27130 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
27131 +       offset = vdir->vd_last.p.deblk - p.deblk;
27132 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
27133 +       return offset;
27134 +}
27135 +
27136 +/* returns true or false */
27137 +static int seek_vdir(struct file *file)
27138 +{
27139 +       int valid;
27140 +       unsigned int deblk_sz;
27141 +       unsigned long ul, n;
27142 +       loff_t offset;
27143 +       union au_vdir_deblk_p p, deblk_end;
27144 +       struct au_vdir *vdir_cache;
27145 +
27146 +       valid = 1;
27147 +       vdir_cache = au_fvdir_cache(file);
27148 +       offset = calc_offset(vdir_cache);
27149 +       AuDbg("offset %lld\n", offset);
27150 +       if (file->f_pos == offset)
27151 +               goto out;
27152 +
27153 +       vdir_cache->vd_last.ul = 0;
27154 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
27155 +       if (!file->f_pos)
27156 +               goto out;
27157 +
27158 +       valid = 0;
27159 +       deblk_sz = vdir_cache->vd_deblk_sz;
27160 +       ul = div64_u64(file->f_pos, deblk_sz);
27161 +       AuDbg("ul %lu\n", ul);
27162 +       if (ul >= vdir_cache->vd_nblk)
27163 +               goto out;
27164 +
27165 +       n = vdir_cache->vd_nblk;
27166 +       for (; ul < n; ul++) {
27167 +               p.deblk = vdir_cache->vd_deblk[ul];
27168 +               deblk_end.deblk = p.deblk + deblk_sz;
27169 +               offset = ul;
27170 +               offset *= deblk_sz;
27171 +               while (!is_deblk_end(&p, &deblk_end) && offset < file->f_pos) {
27172 +                       unsigned int l;
27173 +
27174 +                       l = calc_size(p.de->de_str.len);
27175 +                       offset += l;
27176 +                       p.deblk += l;
27177 +               }
27178 +               if (!is_deblk_end(&p, &deblk_end)) {
27179 +                       valid = 1;
27180 +                       vdir_cache->vd_last.ul = ul;
27181 +                       vdir_cache->vd_last.p = p;
27182 +                       break;
27183 +               }
27184 +       }
27185 +
27186 +out:
27187 +       /* smp_mb(); */
27188 +       AuTraceErr(!valid);
27189 +       return valid;
27190 +}
27191 +
27192 +int au_vdir_fill_de(struct file *file, void *dirent, filldir_t filldir)
27193 +{
27194 +       int err;
27195 +       unsigned int l, deblk_sz;
27196 +       union au_vdir_deblk_p deblk_end;
27197 +       struct au_vdir *vdir_cache;
27198 +       struct au_vdir_de *de;
27199 +
27200 +       vdir_cache = au_fvdir_cache(file);
27201 +       if (!seek_vdir(file))
27202 +               return 0;
27203 +
27204 +       deblk_sz = vdir_cache->vd_deblk_sz;
27205 +       while (1) {
27206 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
27207 +               deblk_end.deblk += deblk_sz;
27208 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
27209 +                       de = vdir_cache->vd_last.p.de;
27210 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
27211 +                             de->de_str.len, de->de_str.name, file->f_pos,
27212 +                             (unsigned long)de->de_ino, de->de_type);
27213 +                       err = filldir(dirent, de->de_str.name, de->de_str.len,
27214 +                                     file->f_pos, de->de_ino, de->de_type);
27215 +                       if (unlikely(err)) {
27216 +                               AuTraceErr(err);
27217 +                               /* todo: ignore the error caused by udba? */
27218 +                               /* return err; */
27219 +                               return 0;
27220 +                       }
27221 +
27222 +                       l = calc_size(de->de_str.len);
27223 +                       vdir_cache->vd_last.p.deblk += l;
27224 +                       file->f_pos += l;
27225 +               }
27226 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
27227 +                       vdir_cache->vd_last.ul++;
27228 +                       vdir_cache->vd_last.p.deblk
27229 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
27230 +                       file->f_pos = deblk_sz * vdir_cache->vd_last.ul;
27231 +                       continue;
27232 +               }
27233 +               break;
27234 +       }
27235 +
27236 +       /* smp_mb(); */
27237 +       return 0;
27238 +}
27239 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
27240 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
27241 +++ linux/fs/aufs/vfsub.c       2013-07-30 22:42:55.842946719 +0200
27242 @@ -0,0 +1,769 @@
27243 +/*
27244 + * Copyright (C) 2005-2013 Junjiro R. Okajima
27245 + *
27246 + * This program, aufs is free software; you can redistribute it and/or modify
27247 + * it under the terms of the GNU General Public License as published by
27248 + * the Free Software Foundation; either version 2 of the License, or
27249 + * (at your option) any later version.
27250 + *
27251 + * This program is distributed in the hope that it will be useful,
27252 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27253 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27254 + * GNU General Public License for more details.
27255 + *
27256 + * You should have received a copy of the GNU General Public License
27257 + * along with this program; if not, write to the Free Software
27258 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
27259 + */
27260 +
27261 +/*
27262 + * sub-routines for VFS
27263 + */
27264 +
27265 +#include <linux/ima.h>
27266 +#include <linux/namei.h>
27267 +#include <linux/security.h>
27268 +#include <linux/splice.h>
27269 +#include "aufs.h"
27270 +
27271 +int vfsub_update_h_iattr(struct path *h_path, int *did)
27272 +{
27273 +       int err;
27274 +       struct kstat st;
27275 +       struct super_block *h_sb;
27276 +
27277 +       /* for remote fs, leave work for its getattr or d_revalidate */
27278 +       /* for bad i_attr fs, handle them in aufs_getattr() */
27279 +       /* still some fs may acquire i_mutex. we need to skip them */
27280 +       err = 0;
27281 +       if (!did)
27282 +               did = &err;
27283 +       h_sb = h_path->dentry->d_sb;
27284 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
27285 +       if (*did)
27286 +               err = vfs_getattr(h_path, &st);
27287 +
27288 +       return err;
27289 +}
27290 +
27291 +/* ---------------------------------------------------------------------- */
27292 +
27293 +struct file *vfsub_dentry_open(struct path *path, int flags)
27294 +{
27295 +       struct file *file;
27296 +
27297 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
27298 +                          current_cred());
27299 +       if (!IS_ERR_OR_NULL(file)
27300 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
27301 +               i_readcount_inc(path->dentry->d_inode);
27302 +
27303 +       return file;
27304 +}
27305 +
27306 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
27307 +{
27308 +       struct file *file;
27309 +
27310 +       lockdep_off();
27311 +       file = filp_open(path,
27312 +                        oflags /* | __FMODE_NONOTIFY */,
27313 +                        mode);
27314 +       lockdep_on();
27315 +       if (IS_ERR(file))
27316 +               goto out;
27317 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
27318 +
27319 +out:
27320 +       return file;
27321 +}
27322 +
27323 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
27324 +{
27325 +       int err;
27326 +
27327 +       err = kern_path(name, flags, path);
27328 +       if (!err && path->dentry->d_inode)
27329 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
27330 +       return err;
27331 +}
27332 +
27333 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
27334 +                                   int len)
27335 +{
27336 +       struct path path = {
27337 +               .mnt = NULL
27338 +       };
27339 +
27340 +       /* VFS checks it too, but by WARN_ON_ONCE() */
27341 +       IMustLock(parent->d_inode);
27342 +
27343 +       path.dentry = lookup_one_len(name, parent, len);
27344 +       if (IS_ERR(path.dentry))
27345 +               goto out;
27346 +       if (path.dentry->d_inode)
27347 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
27348 +
27349 +out:
27350 +       AuTraceErrPtr(path.dentry);
27351 +       return path.dentry;
27352 +}
27353 +
27354 +void vfsub_call_lkup_one(void *args)
27355 +{
27356 +       struct vfsub_lkup_one_args *a = args;
27357 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
27358 +}
27359 +
27360 +/* ---------------------------------------------------------------------- */
27361 +
27362 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
27363 +                                struct dentry *d2, struct au_hinode *hdir2)
27364 +{
27365 +       struct dentry *d;
27366 +
27367 +       lockdep_off();
27368 +       d = lock_rename(d1, d2);
27369 +       lockdep_on();
27370 +       au_hn_suspend(hdir1);
27371 +       if (hdir1 != hdir2)
27372 +               au_hn_suspend(hdir2);
27373 +
27374 +       return d;
27375 +}
27376 +
27377 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
27378 +                        struct dentry *d2, struct au_hinode *hdir2)
27379 +{
27380 +       au_hn_resume(hdir1);
27381 +       if (hdir1 != hdir2)
27382 +               au_hn_resume(hdir2);
27383 +       lockdep_off();
27384 +       unlock_rename(d1, d2);
27385 +       lockdep_on();
27386 +}
27387 +
27388 +/* ---------------------------------------------------------------------- */
27389 +
27390 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
27391 +{
27392 +       int err;
27393 +       struct dentry *d;
27394 +
27395 +       IMustLock(dir);
27396 +
27397 +       d = path->dentry;
27398 +       path->dentry = d->d_parent;
27399 +       err = security_path_mknod(path, d, mode, 0);
27400 +       path->dentry = d;
27401 +       if (unlikely(err))
27402 +               goto out;
27403 +
27404 +       err = vfs_create(dir, path->dentry, mode, want_excl);
27405 +       if (!err) {
27406 +               struct path tmp = *path;
27407 +               int did;
27408 +
27409 +               vfsub_update_h_iattr(&tmp, &did);
27410 +               if (did) {
27411 +                       tmp.dentry = path->dentry->d_parent;
27412 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27413 +               }
27414 +               /*ignore*/
27415 +       }
27416 +
27417 +out:
27418 +       return err;
27419 +}
27420 +
27421 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
27422 +{
27423 +       int err;
27424 +       struct dentry *d;
27425 +
27426 +       IMustLock(dir);
27427 +
27428 +       d = path->dentry;
27429 +       path->dentry = d->d_parent;
27430 +       err = security_path_symlink(path, d, symname);
27431 +       path->dentry = d;
27432 +       if (unlikely(err))
27433 +               goto out;
27434 +
27435 +       err = vfs_symlink(dir, path->dentry, symname);
27436 +       if (!err) {
27437 +               struct path tmp = *path;
27438 +               int did;
27439 +
27440 +               vfsub_update_h_iattr(&tmp, &did);
27441 +               if (did) {
27442 +                       tmp.dentry = path->dentry->d_parent;
27443 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27444 +               }
27445 +               /*ignore*/
27446 +       }
27447 +
27448 +out:
27449 +       return err;
27450 +}
27451 +
27452 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
27453 +{
27454 +       int err;
27455 +       struct dentry *d;
27456 +
27457 +       IMustLock(dir);
27458 +
27459 +       d = path->dentry;
27460 +       path->dentry = d->d_parent;
27461 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
27462 +       path->dentry = d;
27463 +       if (unlikely(err))
27464 +               goto out;
27465 +
27466 +       err = vfs_mknod(dir, path->dentry, mode, dev);
27467 +       if (!err) {
27468 +               struct path tmp = *path;
27469 +               int did;
27470 +
27471 +               vfsub_update_h_iattr(&tmp, &did);
27472 +               if (did) {
27473 +                       tmp.dentry = path->dentry->d_parent;
27474 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27475 +               }
27476 +               /*ignore*/
27477 +       }
27478 +
27479 +out:
27480 +       return err;
27481 +}
27482 +
27483 +static int au_test_nlink(struct inode *inode)
27484 +{
27485 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
27486 +
27487 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
27488 +           || inode->i_nlink < link_max)
27489 +               return 0;
27490 +       return -EMLINK;
27491 +}
27492 +
27493 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path)
27494 +{
27495 +       int err;
27496 +       struct dentry *d;
27497 +
27498 +       IMustLock(dir);
27499 +
27500 +       err = au_test_nlink(src_dentry->d_inode);
27501 +       if (unlikely(err))
27502 +               return err;
27503 +
27504 +       /* we don't call may_linkat() */
27505 +       d = path->dentry;
27506 +       path->dentry = d->d_parent;
27507 +       err = security_path_link(src_dentry, path, d);
27508 +       path->dentry = d;
27509 +       if (unlikely(err))
27510 +               goto out;
27511 +
27512 +       lockdep_off();
27513 +       err = vfs_link(src_dentry, dir, path->dentry);
27514 +       lockdep_on();
27515 +       if (!err) {
27516 +               struct path tmp = *path;
27517 +               int did;
27518 +
27519 +               /* fuse has different memory inode for the same inumber */
27520 +               vfsub_update_h_iattr(&tmp, &did);
27521 +               if (did) {
27522 +                       tmp.dentry = path->dentry->d_parent;
27523 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27524 +                       tmp.dentry = src_dentry;
27525 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27526 +               }
27527 +               /*ignore*/
27528 +       }
27529 +
27530 +out:
27531 +       return err;
27532 +}
27533 +
27534 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
27535 +                struct inode *dir, struct path *path)
27536 +{
27537 +       int err;
27538 +       struct path tmp = {
27539 +               .mnt    = path->mnt
27540 +       };
27541 +       struct dentry *d;
27542 +
27543 +       IMustLock(dir);
27544 +       IMustLock(src_dir);
27545 +
27546 +       d = path->dentry;
27547 +       path->dentry = d->d_parent;
27548 +       tmp.dentry = src_dentry->d_parent;
27549 +       err = security_path_rename(&tmp, src_dentry, path, d);
27550 +       path->dentry = d;
27551 +       if (unlikely(err))
27552 +               goto out;
27553 +
27554 +       lockdep_off();
27555 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry);
27556 +       lockdep_on();
27557 +       if (!err) {
27558 +               int did;
27559 +
27560 +               tmp.dentry = d->d_parent;
27561 +               vfsub_update_h_iattr(&tmp, &did);
27562 +               if (did) {
27563 +                       tmp.dentry = src_dentry;
27564 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27565 +                       tmp.dentry = src_dentry->d_parent;
27566 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27567 +               }
27568 +               /*ignore*/
27569 +       }
27570 +
27571 +out:
27572 +       return err;
27573 +}
27574 +
27575 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
27576 +{
27577 +       int err;
27578 +       struct dentry *d;
27579 +
27580 +       IMustLock(dir);
27581 +
27582 +       d = path->dentry;
27583 +       path->dentry = d->d_parent;
27584 +       err = security_path_mkdir(path, d, mode);
27585 +       path->dentry = d;
27586 +       if (unlikely(err))
27587 +               goto out;
27588 +
27589 +       err = vfs_mkdir(dir, path->dentry, mode);
27590 +       if (!err) {
27591 +               struct path tmp = *path;
27592 +               int did;
27593 +
27594 +               vfsub_update_h_iattr(&tmp, &did);
27595 +               if (did) {
27596 +                       tmp.dentry = path->dentry->d_parent;
27597 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27598 +               }
27599 +               /*ignore*/
27600 +       }
27601 +
27602 +out:
27603 +       return err;
27604 +}
27605 +
27606 +int vfsub_rmdir(struct inode *dir, struct path *path)
27607 +{
27608 +       int err;
27609 +       struct dentry *d;
27610 +
27611 +       IMustLock(dir);
27612 +
27613 +       d = path->dentry;
27614 +       path->dentry = d->d_parent;
27615 +       err = security_path_rmdir(path, d);
27616 +       path->dentry = d;
27617 +       if (unlikely(err))
27618 +               goto out;
27619 +
27620 +       lockdep_off();
27621 +       err = vfs_rmdir(dir, path->dentry);
27622 +       lockdep_on();
27623 +       if (!err) {
27624 +               struct path tmp = {
27625 +                       .dentry = path->dentry->d_parent,
27626 +                       .mnt    = path->mnt
27627 +               };
27628 +
27629 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
27630 +       }
27631 +
27632 +out:
27633 +       return err;
27634 +}
27635 +
27636 +/* ---------------------------------------------------------------------- */
27637 +
27638 +/* todo: support mmap_sem? */
27639 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
27640 +                    loff_t *ppos)
27641 +{
27642 +       ssize_t err;
27643 +
27644 +       lockdep_off();
27645 +       err = vfs_read(file, ubuf, count, ppos);
27646 +       lockdep_on();
27647 +       if (err >= 0)
27648 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
27649 +       return err;
27650 +}
27651 +
27652 +/* todo: kernel_read()? */
27653 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
27654 +                    loff_t *ppos)
27655 +{
27656 +       ssize_t err;
27657 +       mm_segment_t oldfs;
27658 +       union {
27659 +               void *k;
27660 +               char __user *u;
27661 +       } buf;
27662 +
27663 +       buf.k = kbuf;
27664 +       oldfs = get_fs();
27665 +       set_fs(KERNEL_DS);
27666 +       err = vfsub_read_u(file, buf.u, count, ppos);
27667 +       set_fs(oldfs);
27668 +       return err;
27669 +}
27670 +
27671 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
27672 +                     loff_t *ppos)
27673 +{
27674 +       ssize_t err;
27675 +
27676 +       lockdep_off();
27677 +       err = vfs_write(file, ubuf, count, ppos);
27678 +       lockdep_on();
27679 +       if (err >= 0)
27680 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
27681 +       return err;
27682 +}
27683 +
27684 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
27685 +{
27686 +       ssize_t err;
27687 +       mm_segment_t oldfs;
27688 +       union {
27689 +               void *k;
27690 +               const char __user *u;
27691 +       } buf;
27692 +
27693 +       buf.k = kbuf;
27694 +       oldfs = get_fs();
27695 +       set_fs(KERNEL_DS);
27696 +       err = vfsub_write_u(file, buf.u, count, ppos);
27697 +       set_fs(oldfs);
27698 +       return err;
27699 +}
27700 +
27701 +int vfsub_flush(struct file *file, fl_owner_t id)
27702 +{
27703 +       int err;
27704 +
27705 +       err = 0;
27706 +       if (file->f_op && file->f_op->flush) {
27707 +               if (!au_test_nfs(file->f_dentry->d_sb))
27708 +                       err = file->f_op->flush(file, id);
27709 +               else {
27710 +                       lockdep_off();
27711 +                       err = file->f_op->flush(file, id);
27712 +                       lockdep_on();
27713 +               }
27714 +               if (!err)
27715 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
27716 +               /*ignore*/
27717 +       }
27718 +       return err;
27719 +}
27720 +
27721 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg)
27722 +{
27723 +       int err;
27724 +
27725 +       lockdep_off();
27726 +       err = vfs_readdir(file, filldir, arg);
27727 +       lockdep_on();
27728 +       if (err >= 0)
27729 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
27730 +       return err;
27731 +}
27732 +
27733 +long vfsub_splice_to(struct file *in, loff_t *ppos,
27734 +                    struct pipe_inode_info *pipe, size_t len,
27735 +                    unsigned int flags)
27736 +{
27737 +       long err;
27738 +
27739 +       lockdep_off();
27740 +       err = do_splice_to(in, ppos, pipe, len, flags);
27741 +       lockdep_on();
27742 +       file_accessed(in);
27743 +       if (err >= 0)
27744 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
27745 +       return err;
27746 +}
27747 +
27748 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
27749 +                      loff_t *ppos, size_t len, unsigned int flags)
27750 +{
27751 +       long err;
27752 +
27753 +       lockdep_off();
27754 +       err = do_splice_from(pipe, out, ppos, len, flags);
27755 +       lockdep_on();
27756 +       if (err >= 0)
27757 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
27758 +       return err;
27759 +}
27760 +
27761 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
27762 +{
27763 +       int err;
27764 +
27765 +       /* file can be NULL */
27766 +       lockdep_off();
27767 +       err = vfs_fsync(file, datasync);
27768 +       lockdep_on();
27769 +       if (!err) {
27770 +               if (!path) {
27771 +                       AuDebugOn(!file);
27772 +                       path = &file->f_path;
27773 +               }
27774 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
27775 +       }
27776 +       return err;
27777 +}
27778 +
27779 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
27780 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
27781 +               struct file *h_file)
27782 +{
27783 +       int err;
27784 +       struct inode *h_inode;
27785 +       struct super_block *h_sb;
27786 +
27787 +       if (!h_file) {
27788 +               err = vfsub_truncate(h_path, length);
27789 +               goto out;
27790 +       }
27791 +
27792 +       h_inode = h_path->dentry->d_inode;
27793 +       h_sb = h_inode->i_sb;
27794 +       lockdep_off();
27795 +       sb_start_write(h_sb);
27796 +       lockdep_on();
27797 +       err = locks_verify_truncate(h_inode, h_file, length);
27798 +       if (!err)
27799 +               err = security_path_truncate(h_path);
27800 +       if (!err) {
27801 +               lockdep_off();
27802 +               err = do_truncate(h_path->dentry, length, attr, h_file);
27803 +               lockdep_on();
27804 +       }
27805 +       lockdep_off();
27806 +       sb_end_write(h_sb);
27807 +       lockdep_on();
27808 +
27809 +out:
27810 +       return err;
27811 +}
27812 +
27813 +/* ---------------------------------------------------------------------- */
27814 +
27815 +struct au_vfsub_mkdir_args {
27816 +       int *errp;
27817 +       struct inode *dir;
27818 +       struct path *path;
27819 +       int mode;
27820 +};
27821 +
27822 +static void au_call_vfsub_mkdir(void *args)
27823 +{
27824 +       struct au_vfsub_mkdir_args *a = args;
27825 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
27826 +}
27827 +
27828 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
27829 +{
27830 +       int err, do_sio, wkq_err;
27831 +
27832 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
27833 +       if (!do_sio)
27834 +               err = vfsub_mkdir(dir, path, mode);
27835 +       else {
27836 +               struct au_vfsub_mkdir_args args = {
27837 +                       .errp   = &err,
27838 +                       .dir    = dir,
27839 +                       .path   = path,
27840 +                       .mode   = mode
27841 +               };
27842 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
27843 +               if (unlikely(wkq_err))
27844 +                       err = wkq_err;
27845 +       }
27846 +
27847 +       return err;
27848 +}
27849 +
27850 +struct au_vfsub_rmdir_args {
27851 +       int *errp;
27852 +       struct inode *dir;
27853 +       struct path *path;
27854 +};
27855 +
27856 +static void au_call_vfsub_rmdir(void *args)
27857 +{
27858 +       struct au_vfsub_rmdir_args *a = args;
27859 +       *a->errp = vfsub_rmdir(a->dir, a->path);
27860 +}
27861 +
27862 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
27863 +{
27864 +       int err, do_sio, wkq_err;
27865 +
27866 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
27867 +       if (!do_sio)
27868 +               err = vfsub_rmdir(dir, path);
27869 +       else {
27870 +               struct au_vfsub_rmdir_args args = {
27871 +                       .errp   = &err,
27872 +                       .dir    = dir,
27873 +                       .path   = path
27874 +               };
27875 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
27876 +               if (unlikely(wkq_err))
27877 +                       err = wkq_err;
27878 +       }
27879 +
27880 +       return err;
27881 +}
27882 +
27883 +/* ---------------------------------------------------------------------- */
27884 +
27885 +struct notify_change_args {
27886 +       int *errp;
27887 +       struct path *path;
27888 +       struct iattr *ia;
27889 +};
27890 +
27891 +static void call_notify_change(void *args)
27892 +{
27893 +       struct notify_change_args *a = args;
27894 +       struct inode *h_inode;
27895 +
27896 +       h_inode = a->path->dentry->d_inode;
27897 +       IMustLock(h_inode);
27898 +
27899 +       *a->errp = -EPERM;
27900 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
27901 +               *a->errp = notify_change(a->path->dentry, a->ia);
27902 +               if (!*a->errp)
27903 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
27904 +       }
27905 +       AuTraceErr(*a->errp);
27906 +}
27907 +
27908 +int vfsub_notify_change(struct path *path, struct iattr *ia)
27909 +{
27910 +       int err;
27911 +       struct notify_change_args args = {
27912 +               .errp   = &err,
27913 +               .path   = path,
27914 +               .ia     = ia
27915 +       };
27916 +
27917 +       call_notify_change(&args);
27918 +
27919 +       return err;
27920 +}
27921 +
27922 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia)
27923 +{
27924 +       int err, wkq_err;
27925 +       struct notify_change_args args = {
27926 +               .errp   = &err,
27927 +               .path   = path,
27928 +               .ia     = ia
27929 +       };
27930 +
27931 +       wkq_err = au_wkq_wait(call_notify_change, &args);
27932 +       if (unlikely(wkq_err))
27933 +               err = wkq_err;
27934 +
27935 +       return err;
27936 +}
27937 +
27938 +/* ---------------------------------------------------------------------- */
27939 +
27940 +struct unlink_args {
27941 +       int *errp;
27942 +       struct inode *dir;
27943 +       struct path *path;
27944 +};
27945 +
27946 +static void call_unlink(void *args)
27947 +{
27948 +       struct unlink_args *a = args;
27949 +       struct dentry *d = a->path->dentry;
27950 +       struct inode *h_inode;
27951 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
27952 +                                     && d->d_count == 1);
27953 +
27954 +       IMustLock(a->dir);
27955 +
27956 +       a->path->dentry = d->d_parent;
27957 +       *a->errp = security_path_unlink(a->path, d);
27958 +       a->path->dentry = d;
27959 +       if (unlikely(*a->errp))
27960 +               return;
27961 +
27962 +       if (!stop_sillyrename)
27963 +               dget(d);
27964 +       h_inode = d->d_inode;
27965 +       if (h_inode)
27966 +               ihold(h_inode);
27967 +
27968 +       lockdep_off();
27969 +       *a->errp = vfs_unlink(a->dir, d);
27970 +       lockdep_on();
27971 +       if (!*a->errp) {
27972 +               struct path tmp = {
27973 +                       .dentry = d->d_parent,
27974 +                       .mnt    = a->path->mnt
27975 +               };
27976 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
27977 +       }
27978 +
27979 +       if (!stop_sillyrename)
27980 +               dput(d);
27981 +       if (h_inode)
27982 +               iput(h_inode);
27983 +
27984 +       AuTraceErr(*a->errp);
27985 +}
27986 +
27987 +/*
27988 + * @dir: must be locked.
27989 + * @dentry: target dentry.
27990 + */
27991 +int vfsub_unlink(struct inode *dir, struct path *path, int force)
27992 +{
27993 +       int err;
27994 +       struct unlink_args args = {
27995 +               .errp   = &err,
27996 +               .dir    = dir,
27997 +               .path   = path
27998 +       };
27999 +
28000 +       if (!force)
28001 +               call_unlink(&args);
28002 +       else {
28003 +               int wkq_err;
28004 +
28005 +               wkq_err = au_wkq_wait(call_unlink, &args);
28006 +               if (unlikely(wkq_err))
28007 +                       err = wkq_err;
28008 +       }
28009 +
28010 +       return err;
28011 +}
28012 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
28013 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
28014 +++ linux/fs/aufs/vfsub.h       2013-07-30 22:42:55.842946719 +0200
28015 @@ -0,0 +1,294 @@
28016 +/*
28017 + * Copyright (C) 2005-2013 Junjiro R. Okajima
28018 + *
28019 + * This program, aufs is free software; you can redistribute it and/or modify
28020 + * it under the terms of the GNU General Public License as published by
28021 + * the Free Software Foundation; either version 2 of the License, or
28022 + * (at your option) any later version.
28023 + *
28024 + * This program is distributed in the hope that it will be useful,
28025 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28026 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28027 + * GNU General Public License for more details.
28028 + *
28029 + * You should have received a copy of the GNU General Public License
28030 + * along with this program; if not, write to the Free Software
28031 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28032 + */
28033 +
28034 +/*
28035 + * sub-routines for VFS
28036 + */
28037 +
28038 +#ifndef __AUFS_VFSUB_H__
28039 +#define __AUFS_VFSUB_H__
28040 +
28041 +#ifdef __KERNEL__
28042 +
28043 +#include <linux/fs.h>
28044 +#include <linux/lglock.h>
28045 +#include <linux/mount.h>
28046 +#include "debug.h"
28047 +
28048 +/* copied from linux/fs/internal.h */
28049 +/* todo: BAD approach!! */
28050 +extern struct lglock vfsmount_lock;
28051 +extern void __mnt_drop_write(struct vfsmount *);
28052 +extern spinlock_t inode_sb_list_lock;
28053 +
28054 +/* copied from linux/fs/file_table.c */
28055 +extern struct lglock files_lglock;
28056 +#ifdef CONFIG_SMP
28057 +/*
28058 + * These macros iterate all files on all CPUs for a given superblock.
28059 + * files_lglock must be held globally.
28060 + */
28061 +#define do_file_list_for_each_entry(__sb, __file)              \
28062 +{                                                              \
28063 +       int i;                                                  \
28064 +       for_each_possible_cpu(i) {                              \
28065 +               struct list_head *list;                         \
28066 +               list = per_cpu_ptr((__sb)->s_files, i);         \
28067 +               list_for_each_entry((__file), list, f_u.fu_list)
28068 +
28069 +#define while_file_list_for_each_entry                         \
28070 +       }                                                       \
28071 +}
28072 +
28073 +#else
28074 +
28075 +#define do_file_list_for_each_entry(__sb, __file)              \
28076 +{                                                              \
28077 +       struct list_head *list;                                 \
28078 +       list = &(sb)->s_files;                                  \
28079 +       list_for_each_entry((__file), list, f_u.fu_list)
28080 +
28081 +#define while_file_list_for_each_entry                         \
28082 +}
28083 +#endif
28084 +
28085 +/* ---------------------------------------------------------------------- */
28086 +
28087 +/* lock subclass for lower inode */
28088 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
28089 +/* reduce? gave up. */
28090 +enum {
28091 +       AuLsc_I_Begin = I_MUTEX_QUOTA, /* 4 */
28092 +       AuLsc_I_PARENT,         /* lower inode, parent first */
28093 +       AuLsc_I_PARENT2,        /* copyup dirs */
28094 +       AuLsc_I_PARENT3,        /* copyup wh */
28095 +       AuLsc_I_CHILD,
28096 +       AuLsc_I_CHILD2,
28097 +       AuLsc_I_End
28098 +};
28099 +
28100 +/* to debug easier, do not make them inlined functions */
28101 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
28102 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
28103 +
28104 +/* ---------------------------------------------------------------------- */
28105 +
28106 +static inline void vfsub_drop_nlink(struct inode *inode)
28107 +{
28108 +       AuDebugOn(!inode->i_nlink);
28109 +       drop_nlink(inode);
28110 +}
28111 +
28112 +static inline void vfsub_dead_dir(struct inode *inode)
28113 +{
28114 +       AuDebugOn(!S_ISDIR(inode->i_mode));
28115 +       inode->i_flags |= S_DEAD;
28116 +       clear_nlink(inode);
28117 +}
28118 +
28119 +/* ---------------------------------------------------------------------- */
28120 +
28121 +int vfsub_update_h_iattr(struct path *h_path, int *did);
28122 +struct file *vfsub_dentry_open(struct path *path, int flags);
28123 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
28124 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
28125 +
28126 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
28127 +                                   int len);
28128 +
28129 +struct vfsub_lkup_one_args {
28130 +       struct dentry **errp;
28131 +       struct qstr *name;
28132 +       struct dentry *parent;
28133 +};
28134 +
28135 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
28136 +                                           struct dentry *parent)
28137 +{
28138 +       return vfsub_lookup_one_len(name->name, parent, name->len);
28139 +}
28140 +
28141 +void vfsub_call_lkup_one(void *args);
28142 +
28143 +/* ---------------------------------------------------------------------- */
28144 +
28145 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
28146 +{
28147 +       int err;
28148 +       lockdep_off();
28149 +       err = mnt_want_write(mnt);
28150 +       lockdep_on();
28151 +       return err;
28152 +}
28153 +
28154 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
28155 +{
28156 +       lockdep_off();
28157 +       mnt_drop_write(mnt);
28158 +       lockdep_on();
28159 +}
28160 +
28161 +static inline void vfsub_mnt_drop_write_file(struct file *file)
28162 +{
28163 +       lockdep_off();
28164 +       mnt_drop_write_file(file);
28165 +       lockdep_on();
28166 +}
28167 +
28168 +/* ---------------------------------------------------------------------- */
28169 +
28170 +struct au_hinode;
28171 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
28172 +                                struct dentry *d2, struct au_hinode *hdir2);
28173 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
28174 +                        struct dentry *d2, struct au_hinode *hdir2);
28175 +
28176 +int vfsub_create(struct inode *dir, struct path *path, int mode,
28177 +                bool want_excl);
28178 +int vfsub_symlink(struct inode *dir, struct path *path,
28179 +                 const char *symname);
28180 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
28181 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
28182 +              struct path *path);
28183 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
28184 +                struct inode *hdir, struct path *path);
28185 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
28186 +int vfsub_rmdir(struct inode *dir, struct path *path);
28187 +
28188 +/* ---------------------------------------------------------------------- */
28189 +
28190 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
28191 +                    loff_t *ppos);
28192 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
28193 +                       loff_t *ppos);
28194 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
28195 +                     loff_t *ppos);
28196 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
28197 +                     loff_t *ppos);
28198 +int vfsub_flush(struct file *file, fl_owner_t id);
28199 +int vfsub_readdir(struct file *file, filldir_t filldir, void *arg);
28200 +
28201 +static inline loff_t vfsub_f_size_read(struct file *file)
28202 +{
28203 +       return i_size_read(file_inode(file));
28204 +}
28205 +
28206 +static inline unsigned int vfsub_file_flags(struct file *file)
28207 +{
28208 +       unsigned int flags;
28209 +
28210 +       spin_lock(&file->f_lock);
28211 +       flags = file->f_flags;
28212 +       spin_unlock(&file->f_lock);
28213 +
28214 +       return flags;
28215 +}
28216 +
28217 +static inline void vfsub_file_accessed(struct file *h_file)
28218 +{
28219 +       file_accessed(h_file);
28220 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
28221 +}
28222 +
28223 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
28224 +                                    struct dentry *h_dentry)
28225 +{
28226 +       struct path h_path = {
28227 +               .dentry = h_dentry,
28228 +               .mnt    = h_mnt
28229 +       };
28230 +       touch_atime(&h_path);
28231 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
28232 +}
28233 +
28234 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
28235 +                                   int flags)
28236 +{
28237 +       return update_time(h_inode, ts, flags);
28238 +       /* no vfsub_update_h_iattr() since we don't have struct path */
28239 +}
28240 +
28241 +long vfsub_splice_to(struct file *in, loff_t *ppos,
28242 +                    struct pipe_inode_info *pipe, size_t len,
28243 +                    unsigned int flags);
28244 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
28245 +                      loff_t *ppos, size_t len, unsigned int flags);
28246 +
28247 +static inline long vfsub_truncate(struct path *path, loff_t length)
28248 +{
28249 +       long err;
28250 +       lockdep_off();
28251 +       err = vfs_truncate(path, length);
28252 +       lockdep_on();
28253 +       return err;
28254 +}
28255 +
28256 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
28257 +               struct file *h_file);
28258 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
28259 +
28260 +/* ---------------------------------------------------------------------- */
28261 +
28262 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
28263 +{
28264 +       loff_t err;
28265 +
28266 +       lockdep_off();
28267 +       err = vfs_llseek(file, offset, origin);
28268 +       lockdep_on();
28269 +       return err;
28270 +}
28271 +
28272 +/* ---------------------------------------------------------------------- */
28273 +
28274 +/* dirty workaround for strict type of fmode_t */
28275 +union vfsub_fmu {
28276 +       fmode_t fm;
28277 +       unsigned int ui;
28278 +};
28279 +
28280 +static inline unsigned int vfsub_fmode_to_uint(fmode_t fm)
28281 +{
28282 +       union vfsub_fmu u = {
28283 +               .fm = fm
28284 +       };
28285 +
28286 +       BUILD_BUG_ON(sizeof(u.fm) != sizeof(u.ui));
28287 +
28288 +       return u.ui;
28289 +}
28290 +
28291 +static inline fmode_t vfsub_uint_to_fmode(unsigned int ui)
28292 +{
28293 +       union vfsub_fmu u = {
28294 +               .ui = ui
28295 +       };
28296 +
28297 +       return u.fm;
28298 +}
28299 +
28300 +/* ---------------------------------------------------------------------- */
28301 +
28302 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
28303 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
28304 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia);
28305 +int vfsub_notify_change(struct path *path, struct iattr *ia);
28306 +int vfsub_unlink(struct inode *dir, struct path *path, int force);
28307 +
28308 +#endif /* __KERNEL__ */
28309 +#endif /* __AUFS_VFSUB_H__ */
28310 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
28311 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
28312 +++ linux/fs/aufs/wbr_policy.c  2013-08-23 23:59:39.638250372 +0200
28313 @@ -0,0 +1,693 @@
28314 +/*
28315 + * Copyright (C) 2005-2013 Junjiro R. Okajima
28316 + *
28317 + * This program, aufs is free software; you can redistribute it and/or modify
28318 + * it under the terms of the GNU General Public License as published by
28319 + * the Free Software Foundation; either version 2 of the License, or
28320 + * (at your option) any later version.
28321 + *
28322 + * This program is distributed in the hope that it will be useful,
28323 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28324 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28325 + * GNU General Public License for more details.
28326 + *
28327 + * You should have received a copy of the GNU General Public License
28328 + * along with this program; if not, write to the Free Software
28329 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28330 + */
28331 +
28332 +/*
28333 + * policies for selecting one among multiple writable branches
28334 + */
28335 +
28336 +#include <linux/statfs.h>
28337 +#include "aufs.h"
28338 +
28339 +/* subset of cpup_attr() */
28340 +static noinline_for_stack
28341 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
28342 +{
28343 +       int err, sbits;
28344 +       struct iattr ia;
28345 +       struct inode *h_isrc;
28346 +
28347 +       h_isrc = h_src->d_inode;
28348 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
28349 +       ia.ia_mode = h_isrc->i_mode;
28350 +       ia.ia_uid = h_isrc->i_uid;
28351 +       ia.ia_gid = h_isrc->i_gid;
28352 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
28353 +       au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc->i_flags);
28354 +       err = vfsub_sio_notify_change(h_path, &ia);
28355 +
28356 +       /* is this nfs only? */
28357 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
28358 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
28359 +               ia.ia_mode = h_isrc->i_mode;
28360 +               err = vfsub_sio_notify_change(h_path, &ia);
28361 +       }
28362 +
28363 +       return err;
28364 +}
28365 +
28366 +#define AuCpdown_PARENT_OPQ    1
28367 +#define AuCpdown_WHED          (1 << 1)
28368 +#define AuCpdown_MADE_DIR      (1 << 2)
28369 +#define AuCpdown_DIROPQ                (1 << 3)
28370 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
28371 +#define au_fset_cpdown(flags, name) \
28372 +       do { (flags) |= AuCpdown_##name; } while (0)
28373 +#define au_fclr_cpdown(flags, name) \
28374 +       do { (flags) &= ~AuCpdown_##name; } while (0)
28375 +
28376 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
28377 +                            unsigned int *flags)
28378 +{
28379 +       int err;
28380 +       struct dentry *opq_dentry;
28381 +
28382 +       opq_dentry = au_diropq_create(dentry, bdst);
28383 +       err = PTR_ERR(opq_dentry);
28384 +       if (IS_ERR(opq_dentry))
28385 +               goto out;
28386 +       dput(opq_dentry);
28387 +       au_fset_cpdown(*flags, DIROPQ);
28388 +
28389 +out:
28390 +       return err;
28391 +}
28392 +
28393 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
28394 +                           struct inode *dir, aufs_bindex_t bdst)
28395 +{
28396 +       int err;
28397 +       struct path h_path;
28398 +       struct au_branch *br;
28399 +
28400 +       br = au_sbr(dentry->d_sb, bdst);
28401 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
28402 +       err = PTR_ERR(h_path.dentry);
28403 +       if (IS_ERR(h_path.dentry))
28404 +               goto out;
28405 +
28406 +       err = 0;
28407 +       if (h_path.dentry->d_inode) {
28408 +               h_path.mnt = au_br_mnt(br);
28409 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
28410 +                                         dentry);
28411 +       }
28412 +       dput(h_path.dentry);
28413 +
28414 +out:
28415 +       return err;
28416 +}
28417 +
28418 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
28419 +                        struct au_pin *pin,
28420 +                        struct dentry *h_parent, void *arg)
28421 +{
28422 +       int err, rerr;
28423 +       aufs_bindex_t bopq, bstart;
28424 +       struct path h_path;
28425 +       struct dentry *parent;
28426 +       struct inode *h_dir, *h_inode, *inode, *dir;
28427 +       unsigned int *flags = arg;
28428 +
28429 +       bstart = au_dbstart(dentry);
28430 +       /* dentry is di-locked */
28431 +       parent = dget_parent(dentry);
28432 +       dir = parent->d_inode;
28433 +       h_dir = h_parent->d_inode;
28434 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
28435 +       IMustLock(h_dir);
28436 +
28437 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
28438 +       if (unlikely(err < 0))
28439 +               goto out;
28440 +       h_path.dentry = au_h_dptr(dentry, bdst);
28441 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
28442 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
28443 +                             S_IRWXU | S_IRUGO | S_IXUGO);
28444 +       if (unlikely(err))
28445 +               goto out_put;
28446 +       au_fset_cpdown(*flags, MADE_DIR);
28447 +
28448 +       bopq = au_dbdiropq(dentry);
28449 +       au_fclr_cpdown(*flags, WHED);
28450 +       au_fclr_cpdown(*flags, DIROPQ);
28451 +       if (au_dbwh(dentry) == bdst)
28452 +               au_fset_cpdown(*flags, WHED);
28453 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
28454 +               au_fset_cpdown(*flags, PARENT_OPQ);
28455 +       h_inode = h_path.dentry->d_inode;
28456 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
28457 +       if (au_ftest_cpdown(*flags, WHED)) {
28458 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
28459 +               if (unlikely(err)) {
28460 +                       mutex_unlock(&h_inode->i_mutex);
28461 +                       goto out_dir;
28462 +               }
28463 +       }
28464 +
28465 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
28466 +       mutex_unlock(&h_inode->i_mutex);
28467 +       if (unlikely(err))
28468 +               goto out_opq;
28469 +
28470 +       if (au_ftest_cpdown(*flags, WHED)) {
28471 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
28472 +               if (unlikely(err))
28473 +                       goto out_opq;
28474 +       }
28475 +
28476 +       inode = dentry->d_inode;
28477 +       if (au_ibend(inode) < bdst)
28478 +               au_set_ibend(inode, bdst);
28479 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
28480 +                     au_hi_flags(inode, /*isdir*/1));
28481 +       goto out; /* success */
28482 +
28483 +       /* revert */
28484 +out_opq:
28485 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
28486 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
28487 +               rerr = au_diropq_remove(dentry, bdst);
28488 +               mutex_unlock(&h_inode->i_mutex);
28489 +               if (unlikely(rerr)) {
28490 +                       AuIOErr("failed removing diropq for %.*s b%d (%d)\n",
28491 +                               AuDLNPair(dentry), bdst, rerr);
28492 +                       err = -EIO;
28493 +                       goto out;
28494 +               }
28495 +       }
28496 +out_dir:
28497 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
28498 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
28499 +               if (unlikely(rerr)) {
28500 +                       AuIOErr("failed removing %.*s b%d (%d)\n",
28501 +                               AuDLNPair(dentry), bdst, rerr);
28502 +                       err = -EIO;
28503 +               }
28504 +       }
28505 +out_put:
28506 +       au_set_h_dptr(dentry, bdst, NULL);
28507 +       if (au_dbend(dentry) == bdst)
28508 +               au_update_dbend(dentry);
28509 +out:
28510 +       dput(parent);
28511 +       return err;
28512 +}
28513 +
28514 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
28515 +{
28516 +       int err;
28517 +       unsigned int flags;
28518 +
28519 +       flags = 0;
28520 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
28521 +
28522 +       return err;
28523 +}
28524 +
28525 +/* ---------------------------------------------------------------------- */
28526 +
28527 +/* policies for create */
28528 +
28529 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
28530 +{
28531 +       int err, i, j, ndentry;
28532 +       aufs_bindex_t bopq;
28533 +       struct au_dcsub_pages dpages;
28534 +       struct au_dpage *dpage;
28535 +       struct dentry **dentries, *parent, *d;
28536 +
28537 +       err = au_dpages_init(&dpages, GFP_NOFS);
28538 +       if (unlikely(err))
28539 +               goto out;
28540 +       parent = dget_parent(dentry);
28541 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
28542 +       if (unlikely(err))
28543 +               goto out_free;
28544 +
28545 +       err = bindex;
28546 +       for (i = 0; i < dpages.ndpage; i++) {
28547 +               dpage = dpages.dpages + i;
28548 +               dentries = dpage->dentries;
28549 +               ndentry = dpage->ndentry;
28550 +               for (j = 0; j < ndentry; j++) {
28551 +                       d = dentries[j];
28552 +                       di_read_lock_parent2(d, !AuLock_IR);
28553 +                       bopq = au_dbdiropq(d);
28554 +                       di_read_unlock(d, !AuLock_IR);
28555 +                       if (bopq >= 0 && bopq < err)
28556 +                               err = bopq;
28557 +               }
28558 +       }
28559 +
28560 +out_free:
28561 +       dput(parent);
28562 +       au_dpages_free(&dpages);
28563 +out:
28564 +       return err;
28565 +}
28566 +
28567 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
28568 +{
28569 +       for (; bindex >= 0; bindex--)
28570 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
28571 +                       return bindex;
28572 +       return -EROFS;
28573 +}
28574 +
28575 +/* top down parent */
28576 +static int au_wbr_create_tdp(struct dentry *dentry, int isdir __maybe_unused)
28577 +{
28578 +       int err;
28579 +       aufs_bindex_t bstart, bindex;
28580 +       struct super_block *sb;
28581 +       struct dentry *parent, *h_parent;
28582 +
28583 +       sb = dentry->d_sb;
28584 +       bstart = au_dbstart(dentry);
28585 +       err = bstart;
28586 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
28587 +               goto out;
28588 +
28589 +       err = -EROFS;
28590 +       parent = dget_parent(dentry);
28591 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
28592 +               h_parent = au_h_dptr(parent, bindex);
28593 +               if (!h_parent || !h_parent->d_inode)
28594 +                       continue;
28595 +
28596 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
28597 +                       err = bindex;
28598 +                       break;
28599 +               }
28600 +       }
28601 +       dput(parent);
28602 +
28603 +       /* bottom up here */
28604 +       if (unlikely(err < 0)) {
28605 +               err = au_wbr_bu(sb, bstart - 1);
28606 +               if (err >= 0)
28607 +                       err = au_wbr_nonopq(dentry, err);
28608 +       }
28609 +
28610 +out:
28611 +       AuDbg("b%d\n", err);
28612 +       return err;
28613 +}
28614 +
28615 +/* ---------------------------------------------------------------------- */
28616 +
28617 +/* an exception for the policy other than tdp */
28618 +static int au_wbr_create_exp(struct dentry *dentry)
28619 +{
28620 +       int err;
28621 +       aufs_bindex_t bwh, bdiropq;
28622 +       struct dentry *parent;
28623 +
28624 +       err = -1;
28625 +       bwh = au_dbwh(dentry);
28626 +       parent = dget_parent(dentry);
28627 +       bdiropq = au_dbdiropq(parent);
28628 +       if (bwh >= 0) {
28629 +               if (bdiropq >= 0)
28630 +                       err = min(bdiropq, bwh);
28631 +               else
28632 +                       err = bwh;
28633 +               AuDbg("%d\n", err);
28634 +       } else if (bdiropq >= 0) {
28635 +               err = bdiropq;
28636 +               AuDbg("%d\n", err);
28637 +       }
28638 +       dput(parent);
28639 +
28640 +       if (err >= 0)
28641 +               err = au_wbr_nonopq(dentry, err);
28642 +
28643 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
28644 +               err = -1;
28645 +
28646 +       AuDbg("%d\n", err);
28647 +       return err;
28648 +}
28649 +
28650 +/* ---------------------------------------------------------------------- */
28651 +
28652 +/* round robin */
28653 +static int au_wbr_create_init_rr(struct super_block *sb)
28654 +{
28655 +       int err;
28656 +
28657 +       err = au_wbr_bu(sb, au_sbend(sb));
28658 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
28659 +       /* smp_mb(); */
28660 +
28661 +       AuDbg("b%d\n", err);
28662 +       return err;
28663 +}
28664 +
28665 +static int au_wbr_create_rr(struct dentry *dentry, int isdir)
28666 +{
28667 +       int err, nbr;
28668 +       unsigned int u;
28669 +       aufs_bindex_t bindex, bend;
28670 +       struct super_block *sb;
28671 +       atomic_t *next;
28672 +
28673 +       err = au_wbr_create_exp(dentry);
28674 +       if (err >= 0)
28675 +               goto out;
28676 +
28677 +       sb = dentry->d_sb;
28678 +       next = &au_sbi(sb)->si_wbr_rr_next;
28679 +       bend = au_sbend(sb);
28680 +       nbr = bend + 1;
28681 +       for (bindex = 0; bindex <= bend; bindex++) {
28682 +               if (!isdir) {
28683 +                       err = atomic_dec_return(next) + 1;
28684 +                       /* modulo for 0 is meaningless */
28685 +                       if (unlikely(!err))
28686 +                               err = atomic_dec_return(next) + 1;
28687 +               } else
28688 +                       err = atomic_read(next);
28689 +               AuDbg("%d\n", err);
28690 +               u = err;
28691 +               err = u % nbr;
28692 +               AuDbg("%d\n", err);
28693 +               if (!au_br_rdonly(au_sbr(sb, err)))
28694 +                       break;
28695 +               err = -EROFS;
28696 +       }
28697 +
28698 +       if (err >= 0)
28699 +               err = au_wbr_nonopq(dentry, err);
28700 +
28701 +out:
28702 +       AuDbg("%d\n", err);
28703 +       return err;
28704 +}
28705 +
28706 +/* ---------------------------------------------------------------------- */
28707 +
28708 +/* most free space */
28709 +static void au_mfs(struct dentry *dentry)
28710 +{
28711 +       struct super_block *sb;
28712 +       struct au_branch *br;
28713 +       struct au_wbr_mfs *mfs;
28714 +       aufs_bindex_t bindex, bend;
28715 +       int err;
28716 +       unsigned long long b, bavail;
28717 +       struct path h_path;
28718 +       /* reduce the stack usage */
28719 +       struct kstatfs *st;
28720 +
28721 +       st = kmalloc(sizeof(*st), GFP_NOFS);
28722 +       if (unlikely(!st)) {
28723 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
28724 +               return;
28725 +       }
28726 +
28727 +       bavail = 0;
28728 +       sb = dentry->d_sb;
28729 +       mfs = &au_sbi(sb)->si_wbr_mfs;
28730 +       MtxMustLock(&mfs->mfs_lock);
28731 +       mfs->mfs_bindex = -EROFS;
28732 +       mfs->mfsrr_bytes = 0;
28733 +       bend = au_sbend(sb);
28734 +       for (bindex = 0; bindex <= bend; bindex++) {
28735 +               br = au_sbr(sb, bindex);
28736 +               if (au_br_rdonly(br))
28737 +                       continue;
28738 +
28739 +               /* sb->s_root for NFS is unreliable */
28740 +               h_path.mnt = au_br_mnt(br);
28741 +               h_path.dentry = h_path.mnt->mnt_root;
28742 +               err = vfs_statfs(&h_path, st);
28743 +               if (unlikely(err)) {
28744 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
28745 +                       continue;
28746 +               }
28747 +
28748 +               /* when the available size is equal, select the lower one */
28749 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
28750 +                            || sizeof(b) < sizeof(st->f_bsize));
28751 +               b = st->f_bavail * st->f_bsize;
28752 +               br->br_wbr->wbr_bytes = b;
28753 +               if (b >= bavail) {
28754 +                       bavail = b;
28755 +                       mfs->mfs_bindex = bindex;
28756 +                       mfs->mfs_jiffy = jiffies;
28757 +               }
28758 +       }
28759 +
28760 +       mfs->mfsrr_bytes = bavail;
28761 +       AuDbg("b%d\n", mfs->mfs_bindex);
28762 +       kfree(st);
28763 +}
28764 +
28765 +static int au_wbr_create_mfs(struct dentry *dentry, int isdir __maybe_unused)
28766 +{
28767 +       int err;
28768 +       struct super_block *sb;
28769 +       struct au_wbr_mfs *mfs;
28770 +
28771 +       err = au_wbr_create_exp(dentry);
28772 +       if (err >= 0)
28773 +               goto out;
28774 +
28775 +       sb = dentry->d_sb;
28776 +       mfs = &au_sbi(sb)->si_wbr_mfs;
28777 +       mutex_lock(&mfs->mfs_lock);
28778 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
28779 +           || mfs->mfs_bindex < 0
28780 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
28781 +               au_mfs(dentry);
28782 +       mutex_unlock(&mfs->mfs_lock);
28783 +       err = mfs->mfs_bindex;
28784 +
28785 +       if (err >= 0)
28786 +               err = au_wbr_nonopq(dentry, err);
28787 +
28788 +out:
28789 +       AuDbg("b%d\n", err);
28790 +       return err;
28791 +}
28792 +
28793 +static int au_wbr_create_init_mfs(struct super_block *sb)
28794 +{
28795 +       struct au_wbr_mfs *mfs;
28796 +
28797 +       mfs = &au_sbi(sb)->si_wbr_mfs;
28798 +       mutex_init(&mfs->mfs_lock);
28799 +       mfs->mfs_jiffy = 0;
28800 +       mfs->mfs_bindex = -EROFS;
28801 +
28802 +       return 0;
28803 +}
28804 +
28805 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
28806 +{
28807 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
28808 +       return 0;
28809 +}
28810 +
28811 +/* ---------------------------------------------------------------------- */
28812 +
28813 +/* most free space and then round robin */
28814 +static int au_wbr_create_mfsrr(struct dentry *dentry, int isdir)
28815 +{
28816 +       int err;
28817 +       struct au_wbr_mfs *mfs;
28818 +
28819 +       err = au_wbr_create_mfs(dentry, isdir);
28820 +       if (err >= 0) {
28821 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
28822 +               mutex_lock(&mfs->mfs_lock);
28823 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
28824 +                       err = au_wbr_create_rr(dentry, isdir);
28825 +               mutex_unlock(&mfs->mfs_lock);
28826 +       }
28827 +
28828 +       AuDbg("b%d\n", err);
28829 +       return err;
28830 +}
28831 +
28832 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
28833 +{
28834 +       int err;
28835 +
28836 +       au_wbr_create_init_mfs(sb); /* ignore */
28837 +       err = au_wbr_create_init_rr(sb);
28838 +
28839 +       return err;
28840 +}
28841 +
28842 +/* ---------------------------------------------------------------------- */
28843 +
28844 +/* top down parent and most free space */
28845 +static int au_wbr_create_pmfs(struct dentry *dentry, int isdir)
28846 +{
28847 +       int err, e2;
28848 +       unsigned long long b;
28849 +       aufs_bindex_t bindex, bstart, bend;
28850 +       struct super_block *sb;
28851 +       struct dentry *parent, *h_parent;
28852 +       struct au_branch *br;
28853 +
28854 +       err = au_wbr_create_tdp(dentry, isdir);
28855 +       if (unlikely(err < 0))
28856 +               goto out;
28857 +       parent = dget_parent(dentry);
28858 +       bstart = au_dbstart(parent);
28859 +       bend = au_dbtaildir(parent);
28860 +       if (bstart == bend)
28861 +               goto out_parent; /* success */
28862 +
28863 +       e2 = au_wbr_create_mfs(dentry, isdir);
28864 +       if (e2 < 0)
28865 +               goto out_parent; /* success */
28866 +
28867 +       /* when the available size is equal, select upper one */
28868 +       sb = dentry->d_sb;
28869 +       br = au_sbr(sb, err);
28870 +       b = br->br_wbr->wbr_bytes;
28871 +       AuDbg("b%d, %llu\n", err, b);
28872 +
28873 +       for (bindex = bstart; bindex <= bend; bindex++) {
28874 +               h_parent = au_h_dptr(parent, bindex);
28875 +               if (!h_parent || !h_parent->d_inode)
28876 +                       continue;
28877 +
28878 +               br = au_sbr(sb, bindex);
28879 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
28880 +                       b = br->br_wbr->wbr_bytes;
28881 +                       err = bindex;
28882 +                       AuDbg("b%d, %llu\n", err, b);
28883 +               }
28884 +       }
28885 +
28886 +       if (err >= 0)
28887 +               err = au_wbr_nonopq(dentry, err);
28888 +
28889 +out_parent:
28890 +       dput(parent);
28891 +out:
28892 +       AuDbg("b%d\n", err);
28893 +       return err;
28894 +}
28895 +
28896 +/* ---------------------------------------------------------------------- */
28897 +
28898 +/* policies for copyup */
28899 +
28900 +/* top down parent */
28901 +static int au_wbr_copyup_tdp(struct dentry *dentry)
28902 +{
28903 +       return au_wbr_create_tdp(dentry, /*isdir, anything is ok*/0);
28904 +}
28905 +
28906 +/* bottom up parent */
28907 +static int au_wbr_copyup_bup(struct dentry *dentry)
28908 +{
28909 +       int err;
28910 +       aufs_bindex_t bindex, bstart;
28911 +       struct dentry *parent, *h_parent;
28912 +       struct super_block *sb;
28913 +
28914 +       err = -EROFS;
28915 +       sb = dentry->d_sb;
28916 +       parent = dget_parent(dentry);
28917 +       bstart = au_dbstart(parent);
28918 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
28919 +               h_parent = au_h_dptr(parent, bindex);
28920 +               if (!h_parent || !h_parent->d_inode)
28921 +                       continue;
28922 +
28923 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
28924 +                       err = bindex;
28925 +                       break;
28926 +               }
28927 +       }
28928 +       dput(parent);
28929 +
28930 +       /* bottom up here */
28931 +       if (unlikely(err < 0))
28932 +               err = au_wbr_bu(sb, bstart - 1);
28933 +
28934 +       AuDbg("b%d\n", err);
28935 +       return err;
28936 +}
28937 +
28938 +/* bottom up */
28939 +static int au_wbr_copyup_bu(struct dentry *dentry)
28940 +{
28941 +       int err;
28942 +       aufs_bindex_t bstart;
28943 +
28944 +       bstart = au_dbstart(dentry);
28945 +       err = au_wbr_bu(dentry->d_sb, bstart);
28946 +       AuDbg("b%d\n", err);
28947 +       if (err > bstart)
28948 +               err = au_wbr_nonopq(dentry, err);
28949 +
28950 +       AuDbg("b%d\n", err);
28951 +       return err;
28952 +}
28953 +
28954 +/* ---------------------------------------------------------------------- */
28955 +
28956 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
28957 +       [AuWbrCopyup_TDP] = {
28958 +               .copyup = au_wbr_copyup_tdp
28959 +       },
28960 +       [AuWbrCopyup_BUP] = {
28961 +               .copyup = au_wbr_copyup_bup
28962 +       },
28963 +       [AuWbrCopyup_BU] = {
28964 +               .copyup = au_wbr_copyup_bu
28965 +       }
28966 +};
28967 +
28968 +struct au_wbr_create_operations au_wbr_create_ops[] = {
28969 +       [AuWbrCreate_TDP] = {
28970 +               .create = au_wbr_create_tdp
28971 +       },
28972 +       [AuWbrCreate_RR] = {
28973 +               .create = au_wbr_create_rr,
28974 +               .init   = au_wbr_create_init_rr
28975 +       },
28976 +       [AuWbrCreate_MFS] = {
28977 +               .create = au_wbr_create_mfs,
28978 +               .init   = au_wbr_create_init_mfs,
28979 +               .fin    = au_wbr_create_fin_mfs
28980 +       },
28981 +       [AuWbrCreate_MFSV] = {
28982 +               .create = au_wbr_create_mfs,
28983 +               .init   = au_wbr_create_init_mfs,
28984 +               .fin    = au_wbr_create_fin_mfs
28985 +       },
28986 +       [AuWbrCreate_MFSRR] = {
28987 +               .create = au_wbr_create_mfsrr,
28988 +               .init   = au_wbr_create_init_mfsrr,
28989 +               .fin    = au_wbr_create_fin_mfs
28990 +       },
28991 +       [AuWbrCreate_MFSRRV] = {
28992 +               .create = au_wbr_create_mfsrr,
28993 +               .init   = au_wbr_create_init_mfsrr,
28994 +               .fin    = au_wbr_create_fin_mfs
28995 +       },
28996 +       [AuWbrCreate_PMFS] = {
28997 +               .create = au_wbr_create_pmfs,
28998 +               .init   = au_wbr_create_init_mfs,
28999 +               .fin    = au_wbr_create_fin_mfs
29000 +       },
29001 +       [AuWbrCreate_PMFSV] = {
29002 +               .create = au_wbr_create_pmfs,
29003 +               .init   = au_wbr_create_init_mfs,
29004 +               .fin    = au_wbr_create_fin_mfs
29005 +       }
29006 +};
29007 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
29008 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
29009 +++ linux/fs/aufs/whout.c       2013-07-06 13:20:47.760198800 +0200
29010 @@ -0,0 +1,1022 @@
29011 +/*
29012 + * Copyright (C) 2005-2013 Junjiro R. Okajima
29013 + *
29014 + * This program, aufs is free software; you can redistribute it and/or modify
29015 + * it under the terms of the GNU General Public License as published by
29016 + * the Free Software Foundation; either version 2 of the License, or
29017 + * (at your option) any later version.
29018 + *
29019 + * This program is distributed in the hope that it will be useful,
29020 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29021 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29022 + * GNU General Public License for more details.
29023 + *
29024 + * You should have received a copy of the GNU General Public License
29025 + * along with this program; if not, write to the Free Software
29026 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29027 + */
29028 +
29029 +/*
29030 + * whiteout for logical deletion and opaque directory
29031 + */
29032 +
29033 +#include "aufs.h"
29034 +
29035 +#define WH_MASK                        S_IRUGO
29036 +
29037 +/*
29038 + * If a directory contains this file, then it is opaque.  We start with the
29039 + * .wh. flag so that it is blocked by lookup.
29040 + */
29041 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
29042 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
29043 +
29044 +/*
29045 + * generate whiteout name, which is NOT terminated by NULL.
29046 + * @name: original d_name.name
29047 + * @len: original d_name.len
29048 + * @wh: whiteout qstr
29049 + * returns zero when succeeds, otherwise error.
29050 + * succeeded value as wh->name should be freed by kfree().
29051 + */
29052 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
29053 +{
29054 +       char *p;
29055 +
29056 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
29057 +               return -ENAMETOOLONG;
29058 +
29059 +       wh->len = name->len + AUFS_WH_PFX_LEN;
29060 +       p = kmalloc(wh->len, GFP_NOFS);
29061 +       wh->name = p;
29062 +       if (p) {
29063 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29064 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
29065 +               /* smp_mb(); */
29066 +               return 0;
29067 +       }
29068 +       return -ENOMEM;
29069 +}
29070 +
29071 +/* ---------------------------------------------------------------------- */
29072 +
29073 +/*
29074 + * test if the @wh_name exists under @h_parent.
29075 + * @try_sio specifies the necessary of super-io.
29076 + */
29077 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
29078 +              struct au_branch *br, int try_sio)
29079 +{
29080 +       int err;
29081 +       struct dentry *wh_dentry;
29082 +
29083 +       if (!try_sio)
29084 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
29085 +       else
29086 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent, br);
29087 +       err = PTR_ERR(wh_dentry);
29088 +       if (IS_ERR(wh_dentry))
29089 +               goto out;
29090 +
29091 +       err = 0;
29092 +       if (!wh_dentry->d_inode)
29093 +               goto out_wh; /* success */
29094 +
29095 +       err = 1;
29096 +       if (S_ISREG(wh_dentry->d_inode->i_mode))
29097 +               goto out_wh; /* success */
29098 +
29099 +       err = -EIO;
29100 +       AuIOErr("%.*s Invalid whiteout entry type 0%o.\n",
29101 +               AuDLNPair(wh_dentry), wh_dentry->d_inode->i_mode);
29102 +
29103 +out_wh:
29104 +       dput(wh_dentry);
29105 +out:
29106 +       return err;
29107 +}
29108 +
29109 +/*
29110 + * test if the @h_dentry sets opaque or not.
29111 + */
29112 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br)
29113 +{
29114 +       int err;
29115 +       struct inode *h_dir;
29116 +
29117 +       h_dir = h_dentry->d_inode;
29118 +       err = au_wh_test(h_dentry, &diropq_name, br,
29119 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
29120 +       return err;
29121 +}
29122 +
29123 +/*
29124 + * returns a negative dentry whose name is unique and temporary.
29125 + */
29126 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
29127 +                            struct qstr *prefix)
29128 +{
29129 +       struct dentry *dentry;
29130 +       int i;
29131 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
29132 +               *name, *p;
29133 +       /* strict atomic_t is unnecessary here */
29134 +       static unsigned short cnt;
29135 +       struct qstr qs;
29136 +
29137 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
29138 +
29139 +       name = defname;
29140 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
29141 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
29142 +               dentry = ERR_PTR(-ENAMETOOLONG);
29143 +               if (unlikely(qs.len > NAME_MAX))
29144 +                       goto out;
29145 +               dentry = ERR_PTR(-ENOMEM);
29146 +               name = kmalloc(qs.len + 1, GFP_NOFS);
29147 +               if (unlikely(!name))
29148 +                       goto out;
29149 +       }
29150 +
29151 +       /* doubly whiteout-ed */
29152 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
29153 +       p = name + AUFS_WH_PFX_LEN * 2;
29154 +       memcpy(p, prefix->name, prefix->len);
29155 +       p += prefix->len;
29156 +       *p++ = '.';
29157 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
29158 +
29159 +       qs.name = name;
29160 +       for (i = 0; i < 3; i++) {
29161 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
29162 +               dentry = au_sio_lkup_one(&qs, h_parent, br);
29163 +               if (IS_ERR(dentry) || !dentry->d_inode)
29164 +                       goto out_name;
29165 +               dput(dentry);
29166 +       }
29167 +       /* pr_warn("could not get random name\n"); */
29168 +       dentry = ERR_PTR(-EEXIST);
29169 +       AuDbg("%.*s\n", AuLNPair(&qs));
29170 +       BUG();
29171 +
29172 +out_name:
29173 +       if (name != defname)
29174 +               kfree(name);
29175 +out:
29176 +       AuTraceErrPtr(dentry);
29177 +       return dentry;
29178 +}
29179 +
29180 +/*
29181 + * rename the @h_dentry on @br to the whiteouted temporary name.
29182 + */
29183 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
29184 +{
29185 +       int err;
29186 +       struct path h_path = {
29187 +               .mnt = au_br_mnt(br)
29188 +       };
29189 +       struct inode *h_dir;
29190 +       struct dentry *h_parent;
29191 +
29192 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
29193 +       h_dir = h_parent->d_inode;
29194 +       IMustLock(h_dir);
29195 +
29196 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
29197 +       err = PTR_ERR(h_path.dentry);
29198 +       if (IS_ERR(h_path.dentry))
29199 +               goto out;
29200 +
29201 +       /* under the same dir, no need to lock_rename() */
29202 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path);
29203 +       AuTraceErr(err);
29204 +       dput(h_path.dentry);
29205 +
29206 +out:
29207 +       AuTraceErr(err);
29208 +       return err;
29209 +}
29210 +
29211 +/* ---------------------------------------------------------------------- */
29212 +/*
29213 + * functions for removing a whiteout
29214 + */
29215 +
29216 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
29217 +{
29218 +       int force;
29219 +
29220 +       /*
29221 +        * forces superio when the dir has a sticky bit.
29222 +        * this may be a violation of unix fs semantics.
29223 +        */
29224 +       force = (h_dir->i_mode & S_ISVTX)
29225 +               && !uid_eq(current_fsuid(), h_path->dentry->d_inode->i_uid);
29226 +       return vfsub_unlink(h_dir, h_path, force);
29227 +}
29228 +
29229 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
29230 +                       struct dentry *dentry)
29231 +{
29232 +       int err;
29233 +
29234 +       err = do_unlink_wh(h_dir, h_path);
29235 +       if (!err && dentry)
29236 +               au_set_dbwh(dentry, -1);
29237 +
29238 +       return err;
29239 +}
29240 +
29241 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
29242 +                         struct au_branch *br)
29243 +{
29244 +       int err;
29245 +       struct path h_path = {
29246 +               .mnt = au_br_mnt(br)
29247 +       };
29248 +
29249 +       err = 0;
29250 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
29251 +       if (IS_ERR(h_path.dentry))
29252 +               err = PTR_ERR(h_path.dentry);
29253 +       else {
29254 +               if (h_path.dentry->d_inode
29255 +                   && S_ISREG(h_path.dentry->d_inode->i_mode))
29256 +                       err = do_unlink_wh(h_parent->d_inode, &h_path);
29257 +               dput(h_path.dentry);
29258 +       }
29259 +
29260 +       return err;
29261 +}
29262 +
29263 +/* ---------------------------------------------------------------------- */
29264 +/*
29265 + * initialize/clean whiteout for a branch
29266 + */
29267 +
29268 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
29269 +                       const int isdir)
29270 +{
29271 +       int err;
29272 +
29273 +       if (!whpath->dentry->d_inode)
29274 +               return;
29275 +
29276 +       if (isdir)
29277 +               err = vfsub_rmdir(h_dir, whpath);
29278 +       else
29279 +               err = vfsub_unlink(h_dir, whpath, /*force*/0);
29280 +       if (unlikely(err))
29281 +               pr_warn("failed removing %.*s (%d), ignored.\n",
29282 +                       AuDLNPair(whpath->dentry), err);
29283 +}
29284 +
29285 +static int test_linkable(struct dentry *h_root)
29286 +{
29287 +       struct inode *h_dir = h_root->d_inode;
29288 +
29289 +       if (h_dir->i_op->link)
29290 +               return 0;
29291 +
29292 +       pr_err("%.*s (%s) doesn't support link(2), use noplink and rw+nolwh\n",
29293 +              AuDLNPair(h_root), au_sbtype(h_root->d_sb));
29294 +       return -ENOSYS;
29295 +}
29296 +
29297 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
29298 +static int au_whdir(struct inode *h_dir, struct path *path)
29299 +{
29300 +       int err;
29301 +
29302 +       err = -EEXIST;
29303 +       if (!path->dentry->d_inode) {
29304 +               int mode = S_IRWXU;
29305 +
29306 +               if (au_test_nfs(path->dentry->d_sb))
29307 +                       mode |= S_IXUGO;
29308 +               err = vfsub_mkdir(h_dir, path, mode);
29309 +       } else if (S_ISDIR(path->dentry->d_inode->i_mode))
29310 +               err = 0;
29311 +       else
29312 +               pr_err("unknown %.*s exists\n", AuDLNPair(path->dentry));
29313 +
29314 +       return err;
29315 +}
29316 +
29317 +struct au_wh_base {
29318 +       const struct qstr *name;
29319 +       struct dentry *dentry;
29320 +};
29321 +
29322 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
29323 +                         struct path *h_path)
29324 +{
29325 +       h_path->dentry = base[AuBrWh_BASE].dentry;
29326 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
29327 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
29328 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
29329 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
29330 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
29331 +}
29332 +
29333 +/*
29334 + * returns tri-state,
29335 + * minus: error, caller should print the mesage
29336 + * zero: succuess
29337 + * plus: error, caller should NOT print the mesage
29338 + */
29339 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
29340 +                               int do_plink, struct au_wh_base base[],
29341 +                               struct path *h_path)
29342 +{
29343 +       int err;
29344 +       struct inode *h_dir;
29345 +
29346 +       h_dir = h_root->d_inode;
29347 +       h_path->dentry = base[AuBrWh_BASE].dentry;
29348 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
29349 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
29350 +       if (do_plink) {
29351 +               err = test_linkable(h_root);
29352 +               if (unlikely(err)) {
29353 +                       err = 1;
29354 +                       goto out;
29355 +               }
29356 +
29357 +               err = au_whdir(h_dir, h_path);
29358 +               if (unlikely(err))
29359 +                       goto out;
29360 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
29361 +       } else
29362 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
29363 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
29364 +       err = au_whdir(h_dir, h_path);
29365 +       if (unlikely(err))
29366 +               goto out;
29367 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
29368 +
29369 +out:
29370 +       return err;
29371 +}
29372 +
29373 +/*
29374 + * for the moment, aufs supports the branch filesystem which does not support
29375 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
29376 + * copyup failed. finally, such filesystem will not be used as the writable
29377 + * branch.
29378 + *
29379 + * returns tri-state, see above.
29380 + */
29381 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
29382 +                        int do_plink, struct au_wh_base base[],
29383 +                        struct path *h_path)
29384 +{
29385 +       int err;
29386 +       struct inode *h_dir;
29387 +
29388 +       WbrWhMustWriteLock(wbr);
29389 +
29390 +       err = test_linkable(h_root);
29391 +       if (unlikely(err)) {
29392 +               err = 1;
29393 +               goto out;
29394 +       }
29395 +
29396 +       /*
29397 +        * todo: should this create be done in /sbin/mount.aufs helper?
29398 +        */
29399 +       err = -EEXIST;
29400 +       h_dir = h_root->d_inode;
29401 +       if (!base[AuBrWh_BASE].dentry->d_inode) {
29402 +               h_path->dentry = base[AuBrWh_BASE].dentry;
29403 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
29404 +       } else if (S_ISREG(base[AuBrWh_BASE].dentry->d_inode->i_mode))
29405 +               err = 0;
29406 +       else
29407 +               pr_err("unknown %.*s/%.*s exists\n",
29408 +                      AuDLNPair(h_root), AuDLNPair(base[AuBrWh_BASE].dentry));
29409 +       if (unlikely(err))
29410 +               goto out;
29411 +
29412 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
29413 +       if (do_plink) {
29414 +               err = au_whdir(h_dir, h_path);
29415 +               if (unlikely(err))
29416 +                       goto out;
29417 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
29418 +       } else
29419 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
29420 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
29421 +
29422 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
29423 +       err = au_whdir(h_dir, h_path);
29424 +       if (unlikely(err))
29425 +               goto out;
29426 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
29427 +
29428 +out:
29429 +       return err;
29430 +}
29431 +
29432 +/*
29433 + * initialize the whiteout base file/dir for @br.
29434 + */
29435 +int au_wh_init(struct au_branch *br, struct super_block *sb)
29436 +{
29437 +       int err, i;
29438 +       const unsigned char do_plink
29439 +               = !!au_opt_test(au_mntflags(sb), PLINK);
29440 +       struct inode *h_dir;
29441 +       struct path path = br->br_path;
29442 +       struct dentry *h_root = path.dentry;
29443 +       struct au_wbr *wbr = br->br_wbr;
29444 +       static const struct qstr base_name[] = {
29445 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
29446 +                                         sizeof(AUFS_BASE_NAME) - 1),
29447 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
29448 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
29449 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
29450 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
29451 +       };
29452 +       struct au_wh_base base[] = {
29453 +               [AuBrWh_BASE] = {
29454 +                       .name   = base_name + AuBrWh_BASE,
29455 +                       .dentry = NULL
29456 +               },
29457 +               [AuBrWh_PLINK] = {
29458 +                       .name   = base_name + AuBrWh_PLINK,
29459 +                       .dentry = NULL
29460 +               },
29461 +               [AuBrWh_ORPH] = {
29462 +                       .name   = base_name + AuBrWh_ORPH,
29463 +                       .dentry = NULL
29464 +               }
29465 +       };
29466 +
29467 +       if (wbr)
29468 +               WbrWhMustWriteLock(wbr);
29469 +
29470 +       for (i = 0; i < AuBrWh_Last; i++) {
29471 +               /* doubly whiteouted */
29472 +               struct dentry *d;
29473 +
29474 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
29475 +               err = PTR_ERR(d);
29476 +               if (IS_ERR(d))
29477 +                       goto out;
29478 +
29479 +               base[i].dentry = d;
29480 +               AuDebugOn(wbr
29481 +                         && wbr->wbr_wh[i]
29482 +                         && wbr->wbr_wh[i] != base[i].dentry);
29483 +       }
29484 +
29485 +       if (wbr)
29486 +               for (i = 0; i < AuBrWh_Last; i++) {
29487 +                       dput(wbr->wbr_wh[i]);
29488 +                       wbr->wbr_wh[i] = NULL;
29489 +               }
29490 +
29491 +       err = 0;
29492 +       if (!au_br_writable(br->br_perm)) {
29493 +               h_dir = h_root->d_inode;
29494 +               au_wh_init_ro(h_dir, base, &path);
29495 +       } else if (!au_br_wh_linkable(br->br_perm)) {
29496 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
29497 +               if (err > 0)
29498 +                       goto out;
29499 +               else if (err)
29500 +                       goto out_err;
29501 +       } else {
29502 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
29503 +               if (err > 0)
29504 +                       goto out;
29505 +               else if (err)
29506 +                       goto out_err;
29507 +       }
29508 +       goto out; /* success */
29509 +
29510 +out_err:
29511 +       pr_err("an error(%d) on the writable branch %.*s(%s)\n",
29512 +              err, AuDLNPair(h_root), au_sbtype(h_root->d_sb));
29513 +out:
29514 +       for (i = 0; i < AuBrWh_Last; i++)
29515 +               dput(base[i].dentry);
29516 +       return err;
29517 +}
29518 +
29519 +/* ---------------------------------------------------------------------- */
29520 +/*
29521 + * whiteouts are all hard-linked usually.
29522 + * when its link count reaches a ceiling, we create a new whiteout base
29523 + * asynchronously.
29524 + */
29525 +
29526 +struct reinit_br_wh {
29527 +       struct super_block *sb;
29528 +       struct au_branch *br;
29529 +};
29530 +
29531 +static void reinit_br_wh(void *arg)
29532 +{
29533 +       int err;
29534 +       aufs_bindex_t bindex;
29535 +       struct path h_path;
29536 +       struct reinit_br_wh *a = arg;
29537 +       struct au_wbr *wbr;
29538 +       struct inode *dir;
29539 +       struct dentry *h_root;
29540 +       struct au_hinode *hdir;
29541 +
29542 +       err = 0;
29543 +       wbr = a->br->br_wbr;
29544 +       /* big aufs lock */
29545 +       si_noflush_write_lock(a->sb);
29546 +       if (!au_br_writable(a->br->br_perm))
29547 +               goto out;
29548 +       bindex = au_br_index(a->sb, a->br->br_id);
29549 +       if (unlikely(bindex < 0))
29550 +               goto out;
29551 +
29552 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
29553 +       dir = a->sb->s_root->d_inode;
29554 +       hdir = au_hi(dir, bindex);
29555 +       h_root = au_h_dptr(a->sb->s_root, bindex);
29556 +       AuDebugOn(h_root != au_br_dentry(a->br));
29557 +
29558 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
29559 +       wbr_wh_write_lock(wbr);
29560 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
29561 +                         h_root, a->br);
29562 +       if (!err) {
29563 +               h_path.dentry = wbr->wbr_whbase;
29564 +               h_path.mnt = au_br_mnt(a->br);
29565 +               err = vfsub_unlink(hdir->hi_inode, &h_path, /*force*/0);
29566 +       } else {
29567 +               pr_warn("%.*s is moved, ignored\n",
29568 +                       AuDLNPair(wbr->wbr_whbase));
29569 +               err = 0;
29570 +       }
29571 +       dput(wbr->wbr_whbase);
29572 +       wbr->wbr_whbase = NULL;
29573 +       if (!err)
29574 +               err = au_wh_init(a->br, a->sb);
29575 +       wbr_wh_write_unlock(wbr);
29576 +       au_hn_imtx_unlock(hdir);
29577 +       di_read_unlock(a->sb->s_root, AuLock_IR);
29578 +
29579 +out:
29580 +       if (wbr)
29581 +               atomic_dec(&wbr->wbr_wh_running);
29582 +       atomic_dec(&a->br->br_count);
29583 +       si_write_unlock(a->sb);
29584 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
29585 +       kfree(arg);
29586 +       if (unlikely(err))
29587 +               AuIOErr("err %d\n", err);
29588 +}
29589 +
29590 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
29591 +{
29592 +       int do_dec, wkq_err;
29593 +       struct reinit_br_wh *arg;
29594 +
29595 +       do_dec = 1;
29596 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
29597 +               goto out;
29598 +
29599 +       /* ignore ENOMEM */
29600 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
29601 +       if (arg) {
29602 +               /*
29603 +                * dec(wh_running), kfree(arg) and dec(br_count)
29604 +                * in reinit function
29605 +                */
29606 +               arg->sb = sb;
29607 +               arg->br = br;
29608 +               atomic_inc(&br->br_count);
29609 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
29610 +               if (unlikely(wkq_err)) {
29611 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
29612 +                       atomic_dec(&br->br_count);
29613 +                       kfree(arg);
29614 +               }
29615 +               do_dec = 0;
29616 +       }
29617 +
29618 +out:
29619 +       if (do_dec)
29620 +               atomic_dec(&br->br_wbr->wbr_wh_running);
29621 +}
29622 +
29623 +/* ---------------------------------------------------------------------- */
29624 +
29625 +/*
29626 + * create the whiteout @wh.
29627 + */
29628 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
29629 +                            struct dentry *wh)
29630 +{
29631 +       int err;
29632 +       struct path h_path = {
29633 +               .dentry = wh
29634 +       };
29635 +       struct au_branch *br;
29636 +       struct au_wbr *wbr;
29637 +       struct dentry *h_parent;
29638 +       struct inode *h_dir;
29639 +
29640 +       h_parent = wh->d_parent; /* dir inode is locked */
29641 +       h_dir = h_parent->d_inode;
29642 +       IMustLock(h_dir);
29643 +
29644 +       br = au_sbr(sb, bindex);
29645 +       h_path.mnt = au_br_mnt(br);
29646 +       wbr = br->br_wbr;
29647 +       wbr_wh_read_lock(wbr);
29648 +       if (wbr->wbr_whbase) {
29649 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path);
29650 +               if (!err || err != -EMLINK)
29651 +                       goto out;
29652 +
29653 +               /* link count full. re-initialize br_whbase. */
29654 +               kick_reinit_br_wh(sb, br);
29655 +       }
29656 +
29657 +       /* return this error in this context */
29658 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
29659 +
29660 +out:
29661 +       wbr_wh_read_unlock(wbr);
29662 +       return err;
29663 +}
29664 +
29665 +/* ---------------------------------------------------------------------- */
29666 +
29667 +/*
29668 + * create or remove the diropq.
29669 + */
29670 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
29671 +                               unsigned int flags)
29672 +{
29673 +       struct dentry *opq_dentry, *h_dentry;
29674 +       struct super_block *sb;
29675 +       struct au_branch *br;
29676 +       int err;
29677 +
29678 +       sb = dentry->d_sb;
29679 +       br = au_sbr(sb, bindex);
29680 +       h_dentry = au_h_dptr(dentry, bindex);
29681 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
29682 +       if (IS_ERR(opq_dentry))
29683 +               goto out;
29684 +
29685 +       if (au_ftest_diropq(flags, CREATE)) {
29686 +               err = link_or_create_wh(sb, bindex, opq_dentry);
29687 +               if (!err) {
29688 +                       au_set_dbdiropq(dentry, bindex);
29689 +                       goto out; /* success */
29690 +               }
29691 +       } else {
29692 +               struct path tmp = {
29693 +                       .dentry = opq_dentry,
29694 +                       .mnt    = au_br_mnt(br)
29695 +               };
29696 +               err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
29697 +               if (!err)
29698 +                       au_set_dbdiropq(dentry, -1);
29699 +       }
29700 +       dput(opq_dentry);
29701 +       opq_dentry = ERR_PTR(err);
29702 +
29703 +out:
29704 +       return opq_dentry;
29705 +}
29706 +
29707 +struct do_diropq_args {
29708 +       struct dentry **errp;
29709 +       struct dentry *dentry;
29710 +       aufs_bindex_t bindex;
29711 +       unsigned int flags;
29712 +};
29713 +
29714 +static void call_do_diropq(void *args)
29715 +{
29716 +       struct do_diropq_args *a = args;
29717 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
29718 +}
29719 +
29720 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
29721 +                            unsigned int flags)
29722 +{
29723 +       struct dentry *diropq, *h_dentry;
29724 +
29725 +       h_dentry = au_h_dptr(dentry, bindex);
29726 +       if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
29727 +               diropq = do_diropq(dentry, bindex, flags);
29728 +       else {
29729 +               int wkq_err;
29730 +               struct do_diropq_args args = {
29731 +                       .errp           = &diropq,
29732 +                       .dentry         = dentry,
29733 +                       .bindex         = bindex,
29734 +                       .flags          = flags
29735 +               };
29736 +
29737 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
29738 +               if (unlikely(wkq_err))
29739 +                       diropq = ERR_PTR(wkq_err);
29740 +       }
29741 +
29742 +       return diropq;
29743 +}
29744 +
29745 +/* ---------------------------------------------------------------------- */
29746 +
29747 +/*
29748 + * lookup whiteout dentry.
29749 + * @h_parent: lower parent dentry which must exist and be locked
29750 + * @base_name: name of dentry which will be whiteouted
29751 + * returns dentry for whiteout.
29752 + */
29753 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
29754 +                         struct au_branch *br)
29755 +{
29756 +       int err;
29757 +       struct qstr wh_name;
29758 +       struct dentry *wh_dentry;
29759 +
29760 +       err = au_wh_name_alloc(&wh_name, base_name);
29761 +       wh_dentry = ERR_PTR(err);
29762 +       if (!err) {
29763 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
29764 +               kfree(wh_name.name);
29765 +       }
29766 +       return wh_dentry;
29767 +}
29768 +
29769 +/*
29770 + * link/create a whiteout for @dentry on @bindex.
29771 + */
29772 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
29773 +                           struct dentry *h_parent)
29774 +{
29775 +       struct dentry *wh_dentry;
29776 +       struct super_block *sb;
29777 +       int err;
29778 +
29779 +       sb = dentry->d_sb;
29780 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
29781 +       if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
29782 +               err = link_or_create_wh(sb, bindex, wh_dentry);
29783 +               if (!err)
29784 +                       au_set_dbwh(dentry, bindex);
29785 +               else {
29786 +                       dput(wh_dentry);
29787 +                       wh_dentry = ERR_PTR(err);
29788 +               }
29789 +       }
29790 +
29791 +       return wh_dentry;
29792 +}
29793 +
29794 +/* ---------------------------------------------------------------------- */
29795 +
29796 +/* Delete all whiteouts in this directory on branch bindex. */
29797 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
29798 +                          aufs_bindex_t bindex, struct au_branch *br)
29799 +{
29800 +       int err;
29801 +       unsigned long ul, n;
29802 +       struct qstr wh_name;
29803 +       char *p;
29804 +       struct hlist_head *head;
29805 +       struct au_vdir_wh *pos;
29806 +       struct au_vdir_destr *str;
29807 +
29808 +       err = -ENOMEM;
29809 +       p = (void *)__get_free_page(GFP_NOFS);
29810 +       wh_name.name = p;
29811 +       if (unlikely(!wh_name.name))
29812 +               goto out;
29813 +
29814 +       err = 0;
29815 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29816 +       p += AUFS_WH_PFX_LEN;
29817 +       n = whlist->nh_num;
29818 +       head = whlist->nh_head;
29819 +       for (ul = 0; !err && ul < n; ul++, head++) {
29820 +               hlist_for_each_entry(pos, head, wh_hash) {
29821 +                       if (pos->wh_bindex != bindex)
29822 +                               continue;
29823 +
29824 +                       str = &pos->wh_str;
29825 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
29826 +                               memcpy(p, str->name, str->len);
29827 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
29828 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
29829 +                               if (!err)
29830 +                                       continue;
29831 +                               break;
29832 +                       }
29833 +                       AuIOErr("whiteout name too long %.*s\n",
29834 +                               str->len, str->name);
29835 +                       err = -EIO;
29836 +                       break;
29837 +               }
29838 +       }
29839 +       free_page((unsigned long)wh_name.name);
29840 +
29841 +out:
29842 +       return err;
29843 +}
29844 +
29845 +struct del_wh_children_args {
29846 +       int *errp;
29847 +       struct dentry *h_dentry;
29848 +       struct au_nhash *whlist;
29849 +       aufs_bindex_t bindex;
29850 +       struct au_branch *br;
29851 +};
29852 +
29853 +static void call_del_wh_children(void *args)
29854 +{
29855 +       struct del_wh_children_args *a = args;
29856 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
29857 +}
29858 +
29859 +/* ---------------------------------------------------------------------- */
29860 +
29861 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
29862 +{
29863 +       struct au_whtmp_rmdir *whtmp;
29864 +       int err;
29865 +       unsigned int rdhash;
29866 +
29867 +       SiMustAnyLock(sb);
29868 +
29869 +       whtmp = kmalloc(sizeof(*whtmp), gfp);
29870 +       if (unlikely(!whtmp)) {
29871 +               whtmp = ERR_PTR(-ENOMEM);
29872 +               goto out;
29873 +       }
29874 +
29875 +       whtmp->dir = NULL;
29876 +       whtmp->br = NULL;
29877 +       whtmp->wh_dentry = NULL;
29878 +       /* no estimation for dir size */
29879 +       rdhash = au_sbi(sb)->si_rdhash;
29880 +       if (!rdhash)
29881 +               rdhash = AUFS_RDHASH_DEF;
29882 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
29883 +       if (unlikely(err)) {
29884 +               kfree(whtmp);
29885 +               whtmp = ERR_PTR(err);
29886 +       }
29887 +
29888 +out:
29889 +       return whtmp;
29890 +}
29891 +
29892 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
29893 +{
29894 +       if (whtmp->br)
29895 +               atomic_dec(&whtmp->br->br_count);
29896 +       dput(whtmp->wh_dentry);
29897 +       iput(whtmp->dir);
29898 +       au_nhash_wh_free(&whtmp->whlist);
29899 +       kfree(whtmp);
29900 +}
29901 +
29902 +/*
29903 + * rmdir the whiteouted temporary named dir @h_dentry.
29904 + * @whlist: whiteouted children.
29905 + */
29906 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
29907 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
29908 +{
29909 +       int err;
29910 +       struct path h_tmp;
29911 +       struct inode *wh_inode, *h_dir;
29912 +       struct au_branch *br;
29913 +
29914 +       h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
29915 +       IMustLock(h_dir);
29916 +
29917 +       br = au_sbr(dir->i_sb, bindex);
29918 +       wh_inode = wh_dentry->d_inode;
29919 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
29920 +
29921 +       /*
29922 +        * someone else might change some whiteouts while we were sleeping.
29923 +        * it means this whlist may have an obsoleted entry.
29924 +        */
29925 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
29926 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
29927 +       else {
29928 +               int wkq_err;
29929 +               struct del_wh_children_args args = {
29930 +                       .errp           = &err,
29931 +                       .h_dentry       = wh_dentry,
29932 +                       .whlist         = whlist,
29933 +                       .bindex         = bindex,
29934 +                       .br             = br
29935 +               };
29936 +
29937 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
29938 +               if (unlikely(wkq_err))
29939 +                       err = wkq_err;
29940 +       }
29941 +       mutex_unlock(&wh_inode->i_mutex);
29942 +
29943 +       if (!err) {
29944 +               h_tmp.dentry = wh_dentry;
29945 +               h_tmp.mnt = au_br_mnt(br);
29946 +               err = vfsub_rmdir(h_dir, &h_tmp);
29947 +       }
29948 +
29949 +       if (!err) {
29950 +               if (au_ibstart(dir) == bindex) {
29951 +                       /* todo: dir->i_mutex is necessary */
29952 +                       au_cpup_attr_timesizes(dir);
29953 +                       vfsub_drop_nlink(dir);
29954 +               }
29955 +               return 0; /* success */
29956 +       }
29957 +
29958 +       pr_warn("failed removing %.*s(%d), ignored\n",
29959 +               AuDLNPair(wh_dentry), err);
29960 +       return err;
29961 +}
29962 +
29963 +static void call_rmdir_whtmp(void *args)
29964 +{
29965 +       int err;
29966 +       aufs_bindex_t bindex;
29967 +       struct au_whtmp_rmdir *a = args;
29968 +       struct super_block *sb;
29969 +       struct dentry *h_parent;
29970 +       struct inode *h_dir;
29971 +       struct au_hinode *hdir;
29972 +
29973 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
29974 +       /* mutex_lock(&a->dir->i_mutex); */
29975 +       err = -EROFS;
29976 +       sb = a->dir->i_sb;
29977 +       si_read_lock(sb, !AuLock_FLUSH);
29978 +       if (!au_br_writable(a->br->br_perm))
29979 +               goto out;
29980 +       bindex = au_br_index(sb, a->br->br_id);
29981 +       if (unlikely(bindex < 0))
29982 +               goto out;
29983 +
29984 +       err = -EIO;
29985 +       ii_write_lock_parent(a->dir);
29986 +       h_parent = dget_parent(a->wh_dentry);
29987 +       h_dir = h_parent->d_inode;
29988 +       hdir = au_hi(a->dir, bindex);
29989 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
29990 +       if (unlikely(err))
29991 +               goto out_mnt;
29992 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
29993 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
29994 +                         a->br);
29995 +       if (!err)
29996 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
29997 +       au_hn_imtx_unlock(hdir);
29998 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
29999 +
30000 +out_mnt:
30001 +       dput(h_parent);
30002 +       ii_write_unlock(a->dir);
30003 +out:
30004 +       /* mutex_unlock(&a->dir->i_mutex); */
30005 +       au_whtmp_rmdir_free(a);
30006 +       si_read_unlock(sb);
30007 +       au_nwt_done(&au_sbi(sb)->si_nowait);
30008 +       if (unlikely(err))
30009 +               AuIOErr("err %d\n", err);
30010 +}
30011 +
30012 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
30013 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
30014 +{
30015 +       int wkq_err;
30016 +       struct super_block *sb;
30017 +
30018 +       IMustLock(dir);
30019 +
30020 +       /* all post-process will be done in do_rmdir_whtmp(). */
30021 +       sb = dir->i_sb;
30022 +       args->dir = au_igrab(dir);
30023 +       args->br = au_sbr(sb, bindex);
30024 +       atomic_inc(&args->br->br_count);
30025 +       args->wh_dentry = dget(wh_dentry);
30026 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
30027 +       if (unlikely(wkq_err)) {
30028 +               pr_warn("rmdir error %.*s (%d), ignored\n",
30029 +                       AuDLNPair(wh_dentry), wkq_err);
30030 +               au_whtmp_rmdir_free(args);
30031 +       }
30032 +}
30033 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
30034 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
30035 +++ linux/fs/aufs/whout.h       2013-07-06 13:20:47.760198800 +0200
30036 @@ -0,0 +1,87 @@
30037 +/*
30038 + * Copyright (C) 2005-2013 Junjiro R. Okajima
30039 + *
30040 + * This program, aufs is free software; you can redistribute it and/or modify
30041 + * it under the terms of the GNU General Public License as published by
30042 + * the Free Software Foundation; either version 2 of the License, or
30043 + * (at your option) any later version.
30044 + *
30045 + * This program is distributed in the hope that it will be useful,
30046 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30047 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30048 + * GNU General Public License for more details.
30049 + *
30050 + * You should have received a copy of the GNU General Public License
30051 + * along with this program; if not, write to the Free Software
30052 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30053 + */
30054 +
30055 +/*
30056 + * whiteout for logical deletion and opaque directory
30057 + */
30058 +
30059 +#ifndef __AUFS_WHOUT_H__
30060 +#define __AUFS_WHOUT_H__
30061 +
30062 +#ifdef __KERNEL__
30063 +
30064 +#include "dir.h"
30065 +
30066 +/* whout.c */
30067 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
30068 +struct au_branch;
30069 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
30070 +              struct au_branch *br, int try_sio);
30071 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br);
30072 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
30073 +                            struct qstr *prefix);
30074 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
30075 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
30076 +                       struct dentry *dentry);
30077 +int au_wh_init(struct au_branch *br, struct super_block *sb);
30078 +
30079 +/* diropq flags */
30080 +#define AuDiropq_CREATE        1
30081 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
30082 +#define au_fset_diropq(flags, name) \
30083 +       do { (flags) |= AuDiropq_##name; } while (0)
30084 +#define au_fclr_diropq(flags, name) \
30085 +       do { (flags) &= ~AuDiropq_##name; } while (0)
30086 +
30087 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
30088 +                            unsigned int flags);
30089 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
30090 +                         struct au_branch *br);
30091 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
30092 +                           struct dentry *h_parent);
30093 +
30094 +/* real rmdir for the whiteout-ed dir */
30095 +struct au_whtmp_rmdir {
30096 +       struct inode *dir;
30097 +       struct au_branch *br;
30098 +       struct dentry *wh_dentry;
30099 +       struct au_nhash whlist;
30100 +};
30101 +
30102 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
30103 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
30104 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
30105 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
30106 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
30107 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
30108 +
30109 +/* ---------------------------------------------------------------------- */
30110 +
30111 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
30112 +                                             aufs_bindex_t bindex)
30113 +{
30114 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
30115 +}
30116 +
30117 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
30118 +{
30119 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
30120 +}
30121 +
30122 +#endif /* __KERNEL__ */
30123 +#endif /* __AUFS_WHOUT_H__ */
30124 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
30125 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
30126 +++ linux/fs/aufs/wkq.c 2013-08-23 23:59:39.638250372 +0200
30127 @@ -0,0 +1,213 @@
30128 +/*
30129 + * Copyright (C) 2005-2013 Junjiro R. Okajima
30130 + *
30131 + * This program, aufs is free software; you can redistribute it and/or modify
30132 + * it under the terms of the GNU General Public License as published by
30133 + * the Free Software Foundation; either version 2 of the License, or
30134 + * (at your option) any later version.
30135 + *
30136 + * This program is distributed in the hope that it will be useful,
30137 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30138 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30139 + * GNU General Public License for more details.
30140 + *
30141 + * You should have received a copy of the GNU General Public License
30142 + * along with this program; if not, write to the Free Software
30143 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30144 + */
30145 +
30146 +/*
30147 + * workqueue for asynchronous/super-io operations
30148 + * todo: try new dredential scheme
30149 + */
30150 +
30151 +#include <linux/module.h>
30152 +#include "aufs.h"
30153 +
30154 +/* internal workqueue named AUFS_WKQ_NAME */
30155 +
30156 +static struct workqueue_struct *au_wkq;
30157 +
30158 +struct au_wkinfo {
30159 +       struct work_struct wk;
30160 +       struct kobject *kobj;
30161 +
30162 +       unsigned int flags; /* see wkq.h */
30163 +
30164 +       au_wkq_func_t func;
30165 +       void *args;
30166 +
30167 +       struct completion *comp;
30168 +};
30169 +
30170 +/* ---------------------------------------------------------------------- */
30171 +
30172 +static void wkq_func(struct work_struct *wk)
30173 +{
30174 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
30175 +
30176 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
30177 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
30178 +
30179 +       wkinfo->func(wkinfo->args);
30180 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
30181 +               complete(wkinfo->comp);
30182 +       else {
30183 +               kobject_put(wkinfo->kobj);
30184 +               module_put(THIS_MODULE); /* todo: ?? */
30185 +               kfree(wkinfo);
30186 +       }
30187 +}
30188 +
30189 +/*
30190 + * Since struct completion is large, try allocating it dynamically.
30191 + */
30192 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
30193 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
30194 +
30195 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
30196 +{
30197 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
30198 +       if (*comp) {
30199 +               init_completion(*comp);
30200 +               wkinfo->comp = *comp;
30201 +               return 0;
30202 +       }
30203 +       return -ENOMEM;
30204 +}
30205 +
30206 +static void au_wkq_comp_free(struct completion *comp)
30207 +{
30208 +       kfree(comp);
30209 +}
30210 +
30211 +#else
30212 +
30213 +/* no braces */
30214 +#define AuWkqCompDeclare(name) \
30215 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
30216 +       struct completion *comp = &_ ## name
30217 +
30218 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
30219 +{
30220 +       wkinfo->comp = *comp;
30221 +       return 0;
30222 +}
30223 +
30224 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
30225 +{
30226 +       /* empty */
30227 +}
30228 +#endif /* 4KSTACKS */
30229 +
30230 +static void au_wkq_run(struct au_wkinfo *wkinfo)
30231 +{
30232 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
30233 +               if (au_wkq_test()) {
30234 +                       AuWarn1("wkq from wkq, due to a dead dir by UDBA?\n");
30235 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
30236 +               }
30237 +       } else
30238 +               au_dbg_verify_kthread();
30239 +
30240 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
30241 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
30242 +               queue_work(au_wkq, &wkinfo->wk);
30243 +       } else {
30244 +               INIT_WORK(&wkinfo->wk, wkq_func);
30245 +               schedule_work(&wkinfo->wk);
30246 +       }
30247 +}
30248 +
30249 +/*
30250 + * Be careful. It is easy to make deadlock happen.
30251 + * processA: lock, wkq and wait
30252 + * processB: wkq and wait, lock in wkq
30253 + * --> deadlock
30254 + */
30255 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
30256 +{
30257 +       int err;
30258 +       AuWkqCompDeclare(comp);
30259 +       struct au_wkinfo wkinfo = {
30260 +               .flags  = flags,
30261 +               .func   = func,
30262 +               .args   = args
30263 +       };
30264 +
30265 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
30266 +       if (!err) {
30267 +               au_wkq_run(&wkinfo);
30268 +               /* no timeout, no interrupt */
30269 +               wait_for_completion(wkinfo.comp);
30270 +               au_wkq_comp_free(comp);
30271 +               destroy_work_on_stack(&wkinfo.wk);
30272 +       }
30273 +
30274 +       return err;
30275 +
30276 +}
30277 +
30278 +/*
30279 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
30280 + * problem in a concurrent umounting.
30281 + */
30282 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
30283 +                 unsigned int flags)
30284 +{
30285 +       int err;
30286 +       struct au_wkinfo *wkinfo;
30287 +
30288 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
30289 +
30290 +       /*
30291 +        * wkq_func() must free this wkinfo.
30292 +        * it highly depends upon the implementation of workqueue.
30293 +        */
30294 +       err = 0;
30295 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
30296 +       if (wkinfo) {
30297 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
30298 +               wkinfo->flags = flags & ~AuWkq_WAIT;
30299 +               wkinfo->func = func;
30300 +               wkinfo->args = args;
30301 +               wkinfo->comp = NULL;
30302 +               kobject_get(wkinfo->kobj);
30303 +               __module_get(THIS_MODULE); /* todo: ?? */
30304 +
30305 +               au_wkq_run(wkinfo);
30306 +       } else {
30307 +               err = -ENOMEM;
30308 +               au_nwt_done(&au_sbi(sb)->si_nowait);
30309 +       }
30310 +
30311 +       return err;
30312 +}
30313 +
30314 +/* ---------------------------------------------------------------------- */
30315 +
30316 +void au_nwt_init(struct au_nowait_tasks *nwt)
30317 +{
30318 +       atomic_set(&nwt->nw_len, 0);
30319 +       /* smp_mb(); */ /* atomic_set */
30320 +       init_waitqueue_head(&nwt->nw_wq);
30321 +}
30322 +
30323 +void au_wkq_fin(void)
30324 +{
30325 +       destroy_workqueue(au_wkq);
30326 +}
30327 +
30328 +int __init au_wkq_init(void)
30329 +{
30330 +       int err;
30331 +
30332 +       err = 0;
30333 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
30334 +       if (IS_ERR(au_wkq))
30335 +               err = PTR_ERR(au_wkq);
30336 +       else if (!au_wkq)
30337 +               err = -ENOMEM;
30338 +
30339 +       return err;
30340 +}
30341 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
30342 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
30343 +++ linux/fs/aufs/wkq.h 2013-07-06 13:20:47.760198800 +0200
30344 @@ -0,0 +1,92 @@
30345 +/*
30346 + * Copyright (C) 2005-2013 Junjiro R. Okajima
30347 + *
30348 + * This program, aufs is free software; you can redistribute it and/or modify
30349 + * it under the terms of the GNU General Public License as published by
30350 + * the Free Software Foundation; either version 2 of the License, or
30351 + * (at your option) any later version.
30352 + *
30353 + * This program is distributed in the hope that it will be useful,
30354 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30355 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30356 + * GNU General Public License for more details.
30357 + *
30358 + * You should have received a copy of the GNU General Public License
30359 + * along with this program; if not, write to the Free Software
30360 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30361 + */
30362 +
30363 +/*
30364 + * workqueue for asynchronous/super-io operations
30365 + * todo: try new credentials management scheme
30366 + */
30367 +
30368 +#ifndef __AUFS_WKQ_H__
30369 +#define __AUFS_WKQ_H__
30370 +
30371 +#ifdef __KERNEL__
30372 +
30373 +struct super_block;
30374 +
30375 +/* ---------------------------------------------------------------------- */
30376 +
30377 +/*
30378 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
30379 + */
30380 +struct au_nowait_tasks {
30381 +       atomic_t                nw_len;
30382 +       wait_queue_head_t       nw_wq;
30383 +};
30384 +
30385 +/* ---------------------------------------------------------------------- */
30386 +
30387 +typedef void (*au_wkq_func_t)(void *args);
30388 +
30389 +/* wkq flags */
30390 +#define AuWkq_WAIT     1
30391 +#define AuWkq_NEST     (1 << 1)
30392 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
30393 +#define au_fset_wkq(flags, name) \
30394 +       do { (flags) |= AuWkq_##name; } while (0)
30395 +#define au_fclr_wkq(flags, name) \
30396 +       do { (flags) &= ~AuWkq_##name; } while (0)
30397 +
30398 +#ifndef CONFIG_AUFS_HNOTIFY
30399 +#undef AuWkq_NEST
30400 +#define AuWkq_NEST     0
30401 +#endif
30402 +
30403 +/* wkq.c */
30404 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
30405 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
30406 +                 unsigned int flags);
30407 +void au_nwt_init(struct au_nowait_tasks *nwt);
30408 +int __init au_wkq_init(void);
30409 +void au_wkq_fin(void);
30410 +
30411 +/* ---------------------------------------------------------------------- */
30412 +
30413 +static inline int au_wkq_test(void)
30414 +{
30415 +       return current->flags & PF_WQ_WORKER;
30416 +}
30417 +
30418 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
30419 +{
30420 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
30421 +}
30422 +
30423 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
30424 +{
30425 +       if (atomic_dec_and_test(&nwt->nw_len))
30426 +               wake_up_all(&nwt->nw_wq);
30427 +}
30428 +
30429 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
30430 +{
30431 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
30432 +       return 0;
30433 +}
30434 +
30435 +#endif /* __KERNEL__ */
30436 +#endif /* __AUFS_WKQ_H__ */
30437 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
30438 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
30439 +++ linux/fs/aufs/xino.c        2013-07-06 13:20:47.760198800 +0200
30440 @@ -0,0 +1,1264 @@
30441 +/*
30442 + * Copyright (C) 2005-2013 Junjiro R. Okajima
30443 + *
30444 + * This program, aufs is free software; you can redistribute it and/or modify
30445 + * it under the terms of the GNU General Public License as published by
30446 + * the Free Software Foundation; either version 2 of the License, or
30447 + * (at your option) any later version.
30448 + *
30449 + * This program is distributed in the hope that it will be useful,
30450 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30451 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30452 + * GNU General Public License for more details.
30453 + *
30454 + * You should have received a copy of the GNU General Public License
30455 + * along with this program; if not, write to the Free Software
30456 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30457 + */
30458 +
30459 +/*
30460 + * external inode number translation table and bitmap
30461 + */
30462 +
30463 +#include <linux/seq_file.h>
30464 +#include "aufs.h"
30465 +
30466 +/* todo: unnecessary to support mmap_sem since kernel-space? */
30467 +ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
30468 +                  loff_t *pos)
30469 +{
30470 +       ssize_t err;
30471 +       mm_segment_t oldfs;
30472 +       union {
30473 +               void *k;
30474 +               char __user *u;
30475 +       } buf;
30476 +
30477 +       buf.k = kbuf;
30478 +       oldfs = get_fs();
30479 +       set_fs(KERNEL_DS);
30480 +       do {
30481 +               /* todo: signal_pending? */
30482 +               err = func(file, buf.u, size, pos);
30483 +       } while (err == -EAGAIN || err == -EINTR);
30484 +       set_fs(oldfs);
30485 +
30486 +#if 0 /* reserved for future use */
30487 +       if (err > 0)
30488 +               fsnotify_access(file->f_dentry);
30489 +#endif
30490 +
30491 +       return err;
30492 +}
30493 +
30494 +/* ---------------------------------------------------------------------- */
30495 +
30496 +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
30497 +                             size_t size, loff_t *pos)
30498 +{
30499 +       ssize_t err;
30500 +       mm_segment_t oldfs;
30501 +       union {
30502 +               void *k;
30503 +               const char __user *u;
30504 +       } buf;
30505 +
30506 +       buf.k = kbuf;
30507 +       oldfs = get_fs();
30508 +       set_fs(KERNEL_DS);
30509 +       do {
30510 +               /* todo: signal_pending? */
30511 +               err = func(file, buf.u, size, pos);
30512 +       } while (err == -EAGAIN || err == -EINTR);
30513 +       set_fs(oldfs);
30514 +
30515 +#if 0 /* reserved for future use */
30516 +       if (err > 0)
30517 +               fsnotify_modify(file->f_dentry);
30518 +#endif
30519 +
30520 +       return err;
30521 +}
30522 +
30523 +struct do_xino_fwrite_args {
30524 +       ssize_t *errp;
30525 +       au_writef_t func;
30526 +       struct file *file;
30527 +       void *buf;
30528 +       size_t size;
30529 +       loff_t *pos;
30530 +};
30531 +
30532 +static void call_do_xino_fwrite(void *args)
30533 +{
30534 +       struct do_xino_fwrite_args *a = args;
30535 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
30536 +}
30537 +
30538 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
30539 +                   loff_t *pos)
30540 +{
30541 +       ssize_t err;
30542 +
30543 +       /* todo: signal block and no wkq? */
30544 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
30545 +               lockdep_off();
30546 +               err = do_xino_fwrite(func, file, buf, size, pos);
30547 +               lockdep_on();
30548 +       } else {
30549 +               /*
30550 +                * it breaks RLIMIT_FSIZE and normal user's limit,
30551 +                * users should care about quota and real 'filesystem full.'
30552 +                */
30553 +               int wkq_err;
30554 +               struct do_xino_fwrite_args args = {
30555 +                       .errp   = &err,
30556 +                       .func   = func,
30557 +                       .file   = file,
30558 +                       .buf    = buf,
30559 +                       .size   = size,
30560 +                       .pos    = pos
30561 +               };
30562 +
30563 +               wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
30564 +               if (unlikely(wkq_err))
30565 +                       err = wkq_err;
30566 +       }
30567 +
30568 +       return err;
30569 +}
30570 +
30571 +/* ---------------------------------------------------------------------- */
30572 +
30573 +/*
30574 + * create a new xinofile at the same place/path as @base_file.
30575 + */
30576 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
30577 +{
30578 +       struct file *file;
30579 +       struct dentry *base, *parent;
30580 +       struct inode *dir;
30581 +       struct qstr *name;
30582 +       struct path path;
30583 +       int err;
30584 +
30585 +       base = base_file->f_dentry;
30586 +       parent = base->d_parent; /* dir inode is locked */
30587 +       dir = parent->d_inode;
30588 +       IMustLock(dir);
30589 +
30590 +       file = ERR_PTR(-EINVAL);
30591 +       name = &base->d_name;
30592 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
30593 +       if (IS_ERR(path.dentry)) {
30594 +               file = (void *)path.dentry;
30595 +               pr_err("%.*s lookup err %ld\n",
30596 +                      AuLNPair(name), PTR_ERR(path.dentry));
30597 +               goto out;
30598 +       }
30599 +
30600 +       /* no need to mnt_want_write() since we call dentry_open() later */
30601 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
30602 +       if (unlikely(err)) {
30603 +               file = ERR_PTR(err);
30604 +               pr_err("%.*s create err %d\n", AuLNPair(name), err);
30605 +               goto out_dput;
30606 +       }
30607 +
30608 +       path.mnt = base_file->f_path.mnt;
30609 +       file = vfsub_dentry_open(&path,
30610 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
30611 +                                /* | __FMODE_NONOTIFY */);
30612 +       if (IS_ERR(file)) {
30613 +               pr_err("%.*s open err %ld\n", AuLNPair(name), PTR_ERR(file));
30614 +               goto out_dput;
30615 +       }
30616 +
30617 +       err = vfsub_unlink(dir, &file->f_path, /*force*/0);
30618 +       if (unlikely(err)) {
30619 +               pr_err("%.*s unlink err %d\n", AuLNPair(name), err);
30620 +               goto out_fput;
30621 +       }
30622 +
30623 +       if (copy_src) {
30624 +               /* no one can touch copy_src xino */
30625 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
30626 +               if (unlikely(err)) {
30627 +                       pr_err("%.*s copy err %d\n", AuLNPair(name), err);
30628 +                       goto out_fput;
30629 +               }
30630 +       }
30631 +       goto out_dput; /* success */
30632 +
30633 +out_fput:
30634 +       fput(file);
30635 +       file = ERR_PTR(err);
30636 +out_dput:
30637 +       dput(path.dentry);
30638 +out:
30639 +       return file;
30640 +}
30641 +
30642 +struct au_xino_lock_dir {
30643 +       struct au_hinode *hdir;
30644 +       struct dentry *parent;
30645 +       struct mutex *mtx;
30646 +};
30647 +
30648 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
30649 +                            struct au_xino_lock_dir *ldir)
30650 +{
30651 +       aufs_bindex_t brid, bindex;
30652 +
30653 +       ldir->hdir = NULL;
30654 +       bindex = -1;
30655 +       brid = au_xino_brid(sb);
30656 +       if (brid >= 0)
30657 +               bindex = au_br_index(sb, brid);
30658 +       if (bindex >= 0) {
30659 +               ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
30660 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
30661 +       } else {
30662 +               ldir->parent = dget_parent(xino->f_dentry);
30663 +               ldir->mtx = &ldir->parent->d_inode->i_mutex;
30664 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
30665 +       }
30666 +}
30667 +
30668 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
30669 +{
30670 +       if (ldir->hdir)
30671 +               au_hn_imtx_unlock(ldir->hdir);
30672 +       else {
30673 +               mutex_unlock(ldir->mtx);
30674 +               dput(ldir->parent);
30675 +       }
30676 +}
30677 +
30678 +/* ---------------------------------------------------------------------- */
30679 +
30680 +/* trucate xino files asynchronously */
30681 +
30682 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
30683 +{
30684 +       int err;
30685 +       aufs_bindex_t bi, bend;
30686 +       struct au_branch *br;
30687 +       struct file *new_xino, *file;
30688 +       struct super_block *h_sb;
30689 +       struct au_xino_lock_dir ldir;
30690 +
30691 +       err = -EINVAL;
30692 +       bend = au_sbend(sb);
30693 +       if (unlikely(bindex < 0 || bend < bindex))
30694 +               goto out;
30695 +       br = au_sbr(sb, bindex);
30696 +       file = br->br_xino.xi_file;
30697 +       if (!file)
30698 +               goto out;
30699 +
30700 +       au_xino_lock_dir(sb, file, &ldir);
30701 +       /* mnt_want_write() is unnecessary here */
30702 +       new_xino = au_xino_create2(file, file);
30703 +       au_xino_unlock_dir(&ldir);
30704 +       err = PTR_ERR(new_xino);
30705 +       if (IS_ERR(new_xino))
30706 +               goto out;
30707 +       err = 0;
30708 +       fput(file);
30709 +       br->br_xino.xi_file = new_xino;
30710 +
30711 +       h_sb = au_br_sb(br);
30712 +       for (bi = 0; bi <= bend; bi++) {
30713 +               if (unlikely(bi == bindex))
30714 +                       continue;
30715 +               br = au_sbr(sb, bi);
30716 +               if (au_br_sb(br) != h_sb)
30717 +                       continue;
30718 +
30719 +               fput(br->br_xino.xi_file);
30720 +               br->br_xino.xi_file = new_xino;
30721 +               get_file(new_xino);
30722 +       }
30723 +
30724 +out:
30725 +       return err;
30726 +}
30727 +
30728 +struct xino_do_trunc_args {
30729 +       struct super_block *sb;
30730 +       struct au_branch *br;
30731 +};
30732 +
30733 +static void xino_do_trunc(void *_args)
30734 +{
30735 +       struct xino_do_trunc_args *args = _args;
30736 +       struct super_block *sb;
30737 +       struct au_branch *br;
30738 +       struct inode *dir;
30739 +       int err;
30740 +       aufs_bindex_t bindex;
30741 +
30742 +       err = 0;
30743 +       sb = args->sb;
30744 +       dir = sb->s_root->d_inode;
30745 +       br = args->br;
30746 +
30747 +       si_noflush_write_lock(sb);
30748 +       ii_read_lock_parent(dir);
30749 +       bindex = au_br_index(sb, br->br_id);
30750 +       err = au_xino_trunc(sb, bindex);
30751 +       if (!err
30752 +           && file_inode(br->br_xino.xi_file)->i_blocks
30753 +           >= br->br_xino_upper)
30754 +               br->br_xino_upper += AUFS_XINO_TRUNC_STEP;
30755 +
30756 +       ii_read_unlock(dir);
30757 +       if (unlikely(err))
30758 +               pr_warn("err b%d, upper %llu, (%d)\n",
30759 +                       bindex, (unsigned long long)br->br_xino_upper, err);
30760 +       atomic_dec(&br->br_xino_running);
30761 +       atomic_dec(&br->br_count);
30762 +       si_write_unlock(sb);
30763 +       au_nwt_done(&au_sbi(sb)->si_nowait);
30764 +       kfree(args);
30765 +}
30766 +
30767 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
30768 +{
30769 +       struct xino_do_trunc_args *args;
30770 +       int wkq_err;
30771 +
30772 +       if (file_inode(br->br_xino.xi_file)->i_blocks
30773 +           < br->br_xino_upper)
30774 +               return;
30775 +
30776 +       if (atomic_inc_return(&br->br_xino_running) > 1)
30777 +               goto out;
30778 +
30779 +       /* lock and kfree() will be called in trunc_xino() */
30780 +       args = kmalloc(sizeof(*args), GFP_NOFS);
30781 +       if (unlikely(!args)) {
30782 +               AuErr1("no memory\n");
30783 +               goto out_args;
30784 +       }
30785 +
30786 +       atomic_inc(&br->br_count);
30787 +       args->sb = sb;
30788 +       args->br = br;
30789 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
30790 +       if (!wkq_err)
30791 +               return; /* success */
30792 +
30793 +       pr_err("wkq %d\n", wkq_err);
30794 +       atomic_dec(&br->br_count);
30795 +
30796 +out_args:
30797 +       kfree(args);
30798 +out:
30799 +       atomic_dec(&br->br_xino_running);
30800 +}
30801 +
30802 +/* ---------------------------------------------------------------------- */
30803 +
30804 +static int au_xino_do_write(au_writef_t write, struct file *file,
30805 +                           ino_t h_ino, ino_t ino)
30806 +{
30807 +       loff_t pos;
30808 +       ssize_t sz;
30809 +
30810 +       pos = h_ino;
30811 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
30812 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
30813 +               return -EFBIG;
30814 +       }
30815 +       pos *= sizeof(ino);
30816 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
30817 +       if (sz == sizeof(ino))
30818 +               return 0; /* success */
30819 +
30820 +       AuIOErr("write failed (%zd)\n", sz);
30821 +       return -EIO;
30822 +}
30823 +
30824 +/*
30825 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
30826 + * at the position of @h_ino.
30827 + * even if @ino is zero, it is written to the xinofile and means no entry.
30828 + * if the size of the xino file on a specific filesystem exceeds the watermark,
30829 + * try truncating it.
30830 + */
30831 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
30832 +                 ino_t ino)
30833 +{
30834 +       int err;
30835 +       unsigned int mnt_flags;
30836 +       struct au_branch *br;
30837 +
30838 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
30839 +                    || ((loff_t)-1) > 0);
30840 +       SiMustAnyLock(sb);
30841 +
30842 +       mnt_flags = au_mntflags(sb);
30843 +       if (!au_opt_test(mnt_flags, XINO))
30844 +               return 0;
30845 +
30846 +       br = au_sbr(sb, bindex);
30847 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
30848 +                              h_ino, ino);
30849 +       if (!err) {
30850 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
30851 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
30852 +                       xino_try_trunc(sb, br);
30853 +               return 0; /* success */
30854 +       }
30855 +
30856 +       AuIOErr("write failed (%d)\n", err);
30857 +       return -EIO;
30858 +}
30859 +
30860 +/* ---------------------------------------------------------------------- */
30861 +
30862 +/* aufs inode number bitmap */
30863 +
30864 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
30865 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
30866 +{
30867 +       ino_t ino;
30868 +
30869 +       AuDebugOn(bit < 0 || page_bits <= bit);
30870 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
30871 +       return ino;
30872 +}
30873 +
30874 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
30875 +{
30876 +       AuDebugOn(ino < AUFS_FIRST_INO);
30877 +       ino -= AUFS_FIRST_INO;
30878 +       *pindex = ino / page_bits;
30879 +       *bit = ino % page_bits;
30880 +}
30881 +
30882 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
30883 +{
30884 +       int err;
30885 +       loff_t pos;
30886 +       ssize_t sz;
30887 +       struct au_sbinfo *sbinfo;
30888 +       struct file *xib;
30889 +       unsigned long *p;
30890 +
30891 +       sbinfo = au_sbi(sb);
30892 +       MtxMustLock(&sbinfo->si_xib_mtx);
30893 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
30894 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
30895 +
30896 +       if (pindex == sbinfo->si_xib_last_pindex)
30897 +               return 0;
30898 +
30899 +       xib = sbinfo->si_xib;
30900 +       p = sbinfo->si_xib_buf;
30901 +       pos = sbinfo->si_xib_last_pindex;
30902 +       pos *= PAGE_SIZE;
30903 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
30904 +       if (unlikely(sz != PAGE_SIZE))
30905 +               goto out;
30906 +
30907 +       pos = pindex;
30908 +       pos *= PAGE_SIZE;
30909 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
30910 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
30911 +       else {
30912 +               memset(p, 0, PAGE_SIZE);
30913 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
30914 +       }
30915 +       if (sz == PAGE_SIZE) {
30916 +               sbinfo->si_xib_last_pindex = pindex;
30917 +               return 0; /* success */
30918 +       }
30919 +
30920 +out:
30921 +       AuIOErr1("write failed (%zd)\n", sz);
30922 +       err = sz;
30923 +       if (sz >= 0)
30924 +               err = -EIO;
30925 +       return err;
30926 +}
30927 +
30928 +/* ---------------------------------------------------------------------- */
30929 +
30930 +static void au_xib_clear_bit(struct inode *inode)
30931 +{
30932 +       int err, bit;
30933 +       unsigned long pindex;
30934 +       struct super_block *sb;
30935 +       struct au_sbinfo *sbinfo;
30936 +
30937 +       AuDebugOn(inode->i_nlink);
30938 +
30939 +       sb = inode->i_sb;
30940 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
30941 +       AuDebugOn(page_bits <= bit);
30942 +       sbinfo = au_sbi(sb);
30943 +       mutex_lock(&sbinfo->si_xib_mtx);
30944 +       err = xib_pindex(sb, pindex);
30945 +       if (!err) {
30946 +               clear_bit(bit, sbinfo->si_xib_buf);
30947 +               sbinfo->si_xib_next_bit = bit;
30948 +       }
30949 +       mutex_unlock(&sbinfo->si_xib_mtx);
30950 +}
30951 +
30952 +/* for s_op->delete_inode() */
30953 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
30954 +{
30955 +       int err;
30956 +       unsigned int mnt_flags;
30957 +       aufs_bindex_t bindex, bend, bi;
30958 +       unsigned char try_trunc;
30959 +       struct au_iinfo *iinfo;
30960 +       struct super_block *sb;
30961 +       struct au_hinode *hi;
30962 +       struct inode *h_inode;
30963 +       struct au_branch *br;
30964 +       au_writef_t xwrite;
30965 +
30966 +       sb = inode->i_sb;
30967 +       mnt_flags = au_mntflags(sb);
30968 +       if (!au_opt_test(mnt_flags, XINO)
30969 +           || inode->i_ino == AUFS_ROOT_INO)
30970 +               return;
30971 +
30972 +       if (unlinked) {
30973 +               au_xigen_inc(inode);
30974 +               au_xib_clear_bit(inode);
30975 +       }
30976 +
30977 +       iinfo = au_ii(inode);
30978 +       if (!iinfo)
30979 +               return;
30980 +
30981 +       bindex = iinfo->ii_bstart;
30982 +       if (bindex < 0)
30983 +               return;
30984 +
30985 +       xwrite = au_sbi(sb)->si_xwrite;
30986 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
30987 +       hi = iinfo->ii_hinode + bindex;
30988 +       bend = iinfo->ii_bend;
30989 +       for (; bindex <= bend; bindex++, hi++) {
30990 +               h_inode = hi->hi_inode;
30991 +               if (!h_inode
30992 +                   || (!unlinked && h_inode->i_nlink))
30993 +                       continue;
30994 +
30995 +               /* inode may not be revalidated */
30996 +               bi = au_br_index(sb, hi->hi_id);
30997 +               if (bi < 0)
30998 +                       continue;
30999 +
31000 +               br = au_sbr(sb, bi);
31001 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
31002 +                                      h_inode->i_ino, /*ino*/0);
31003 +               if (!err && try_trunc
31004 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
31005 +                       xino_try_trunc(sb, br);
31006 +       }
31007 +}
31008 +
31009 +/* get an unused inode number from bitmap */
31010 +ino_t au_xino_new_ino(struct super_block *sb)
31011 +{
31012 +       ino_t ino;
31013 +       unsigned long *p, pindex, ul, pend;
31014 +       struct au_sbinfo *sbinfo;
31015 +       struct file *file;
31016 +       int free_bit, err;
31017 +
31018 +       if (!au_opt_test(au_mntflags(sb), XINO))
31019 +               return iunique(sb, AUFS_FIRST_INO);
31020 +
31021 +       sbinfo = au_sbi(sb);
31022 +       mutex_lock(&sbinfo->si_xib_mtx);
31023 +       p = sbinfo->si_xib_buf;
31024 +       free_bit = sbinfo->si_xib_next_bit;
31025 +       if (free_bit < page_bits && !test_bit(free_bit, p))
31026 +               goto out; /* success */
31027 +       free_bit = find_first_zero_bit(p, page_bits);
31028 +       if (free_bit < page_bits)
31029 +               goto out; /* success */
31030 +
31031 +       pindex = sbinfo->si_xib_last_pindex;
31032 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
31033 +               err = xib_pindex(sb, ul);
31034 +               if (unlikely(err))
31035 +                       goto out_err;
31036 +               free_bit = find_first_zero_bit(p, page_bits);
31037 +               if (free_bit < page_bits)
31038 +                       goto out; /* success */
31039 +       }
31040 +
31041 +       file = sbinfo->si_xib;
31042 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
31043 +       for (ul = pindex + 1; ul <= pend; ul++) {
31044 +               err = xib_pindex(sb, ul);
31045 +               if (unlikely(err))
31046 +                       goto out_err;
31047 +               free_bit = find_first_zero_bit(p, page_bits);
31048 +               if (free_bit < page_bits)
31049 +                       goto out; /* success */
31050 +       }
31051 +       BUG();
31052 +
31053 +out:
31054 +       set_bit(free_bit, p);
31055 +       sbinfo->si_xib_next_bit = free_bit + 1;
31056 +       pindex = sbinfo->si_xib_last_pindex;
31057 +       mutex_unlock(&sbinfo->si_xib_mtx);
31058 +       ino = xib_calc_ino(pindex, free_bit);
31059 +       AuDbg("i%lu\n", (unsigned long)ino);
31060 +       return ino;
31061 +out_err:
31062 +       mutex_unlock(&sbinfo->si_xib_mtx);
31063 +       AuDbg("i0\n");
31064 +       return 0;
31065 +}
31066 +
31067 +/*
31068 + * read @ino from xinofile for the specified branch{@sb, @bindex}
31069 + * at the position of @h_ino.
31070 + * if @ino does not exist and @do_new is true, get new one.
31071 + */
31072 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
31073 +                ino_t *ino)
31074 +{
31075 +       int err;
31076 +       ssize_t sz;
31077 +       loff_t pos;
31078 +       struct file *file;
31079 +       struct au_sbinfo *sbinfo;
31080 +
31081 +       *ino = 0;
31082 +       if (!au_opt_test(au_mntflags(sb), XINO))
31083 +               return 0; /* no xino */
31084 +
31085 +       err = 0;
31086 +       sbinfo = au_sbi(sb);
31087 +       pos = h_ino;
31088 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
31089 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
31090 +               return -EFBIG;
31091 +       }
31092 +       pos *= sizeof(*ino);
31093 +
31094 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
31095 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
31096 +               return 0; /* no ino */
31097 +
31098 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
31099 +       if (sz == sizeof(*ino))
31100 +               return 0; /* success */
31101 +
31102 +       err = sz;
31103 +       if (unlikely(sz >= 0)) {
31104 +               err = -EIO;
31105 +               AuIOErr("xino read error (%zd)\n", sz);
31106 +       }
31107 +
31108 +       return err;
31109 +}
31110 +
31111 +/* ---------------------------------------------------------------------- */
31112 +
31113 +/* create and set a new xino file */
31114 +
31115 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
31116 +{
31117 +       struct file *file;
31118 +       struct dentry *h_parent, *d;
31119 +       struct inode *h_dir;
31120 +       int err;
31121 +
31122 +       /*
31123 +        * at mount-time, and the xino file is the default path,
31124 +        * hnotify is disabled so we have no notify events to ignore.
31125 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
31126 +        */
31127 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
31128 +                              /* | __FMODE_NONOTIFY */,
31129 +                              S_IRUGO | S_IWUGO);
31130 +       if (IS_ERR(file)) {
31131 +               if (!silent)
31132 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
31133 +               return file;
31134 +       }
31135 +
31136 +       /* keep file count */
31137 +       h_parent = dget_parent(file->f_dentry);
31138 +       h_dir = h_parent->d_inode;
31139 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
31140 +       /* mnt_want_write() is unnecessary here */
31141 +       err = vfsub_unlink(h_dir, &file->f_path, /*force*/0);
31142 +       mutex_unlock(&h_dir->i_mutex);
31143 +       dput(h_parent);
31144 +       if (unlikely(err)) {
31145 +               if (!silent)
31146 +                       pr_err("unlink %s(%d)\n", fname, err);
31147 +               goto out;
31148 +       }
31149 +
31150 +       err = -EINVAL;
31151 +       d = file->f_dentry;
31152 +       if (unlikely(sb == d->d_sb)) {
31153 +               if (!silent)
31154 +                       pr_err("%s must be outside\n", fname);
31155 +               goto out;
31156 +       }
31157 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
31158 +               if (!silent)
31159 +                       pr_err("xino doesn't support %s(%s)\n",
31160 +                              fname, au_sbtype(d->d_sb));
31161 +               goto out;
31162 +       }
31163 +       return file; /* success */
31164 +
31165 +out:
31166 +       fput(file);
31167 +       file = ERR_PTR(err);
31168 +       return file;
31169 +}
31170 +
31171 +/*
31172 + * find another branch who is on the same filesystem of the specified
31173 + * branch{@btgt}. search until @bend.
31174 + */
31175 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
31176 +                       aufs_bindex_t bend)
31177 +{
31178 +       aufs_bindex_t bindex;
31179 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
31180 +
31181 +       for (bindex = 0; bindex < btgt; bindex++)
31182 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
31183 +                       return bindex;
31184 +       for (bindex++; bindex <= bend; bindex++)
31185 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
31186 +                       return bindex;
31187 +       return -1;
31188 +}
31189 +
31190 +/* ---------------------------------------------------------------------- */
31191 +
31192 +/*
31193 + * initialize the xinofile for the specified branch @br
31194 + * at the place/path where @base_file indicates.
31195 + * test whether another branch is on the same filesystem or not,
31196 + * if @do_test is true.
31197 + */
31198 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
31199 +              struct file *base_file, int do_test)
31200 +{
31201 +       int err;
31202 +       ino_t ino;
31203 +       aufs_bindex_t bend, bindex;
31204 +       struct au_branch *shared_br, *b;
31205 +       struct file *file;
31206 +       struct super_block *tgt_sb;
31207 +
31208 +       shared_br = NULL;
31209 +       bend = au_sbend(sb);
31210 +       if (do_test) {
31211 +               tgt_sb = au_br_sb(br);
31212 +               for (bindex = 0; bindex <= bend; bindex++) {
31213 +                       b = au_sbr(sb, bindex);
31214 +                       if (tgt_sb == au_br_sb(b)) {
31215 +                               shared_br = b;
31216 +                               break;
31217 +                       }
31218 +               }
31219 +       }
31220 +
31221 +       if (!shared_br || !shared_br->br_xino.xi_file) {
31222 +               struct au_xino_lock_dir ldir;
31223 +
31224 +               au_xino_lock_dir(sb, base_file, &ldir);
31225 +               /* mnt_want_write() is unnecessary here */
31226 +               file = au_xino_create2(base_file, NULL);
31227 +               au_xino_unlock_dir(&ldir);
31228 +               err = PTR_ERR(file);
31229 +               if (IS_ERR(file))
31230 +                       goto out;
31231 +               br->br_xino.xi_file = file;
31232 +       } else {
31233 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
31234 +               get_file(br->br_xino.xi_file);
31235 +       }
31236 +
31237 +       ino = AUFS_ROOT_INO;
31238 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
31239 +                              h_ino, ino);
31240 +       if (unlikely(err)) {
31241 +               fput(br->br_xino.xi_file);
31242 +               br->br_xino.xi_file = NULL;
31243 +       }
31244 +
31245 +out:
31246 +       return err;
31247 +}
31248 +
31249 +/* ---------------------------------------------------------------------- */
31250 +
31251 +/* trucate a xino bitmap file */
31252 +
31253 +/* todo: slow */
31254 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
31255 +{
31256 +       int err, bit;
31257 +       ssize_t sz;
31258 +       unsigned long pindex;
31259 +       loff_t pos, pend;
31260 +       struct au_sbinfo *sbinfo;
31261 +       au_readf_t func;
31262 +       ino_t *ino;
31263 +       unsigned long *p;
31264 +
31265 +       err = 0;
31266 +       sbinfo = au_sbi(sb);
31267 +       MtxMustLock(&sbinfo->si_xib_mtx);
31268 +       p = sbinfo->si_xib_buf;
31269 +       func = sbinfo->si_xread;
31270 +       pend = vfsub_f_size_read(file);
31271 +       pos = 0;
31272 +       while (pos < pend) {
31273 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
31274 +               err = sz;
31275 +               if (unlikely(sz <= 0))
31276 +                       goto out;
31277 +
31278 +               err = 0;
31279 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
31280 +                       if (unlikely(*ino < AUFS_FIRST_INO))
31281 +                               continue;
31282 +
31283 +                       xib_calc_bit(*ino, &pindex, &bit);
31284 +                       AuDebugOn(page_bits <= bit);
31285 +                       err = xib_pindex(sb, pindex);
31286 +                       if (!err)
31287 +                               set_bit(bit, p);
31288 +                       else
31289 +                               goto out;
31290 +               }
31291 +       }
31292 +
31293 +out:
31294 +       return err;
31295 +}
31296 +
31297 +static int xib_restore(struct super_block *sb)
31298 +{
31299 +       int err;
31300 +       aufs_bindex_t bindex, bend;
31301 +       void *page;
31302 +
31303 +       err = -ENOMEM;
31304 +       page = (void *)__get_free_page(GFP_NOFS);
31305 +       if (unlikely(!page))
31306 +               goto out;
31307 +
31308 +       err = 0;
31309 +       bend = au_sbend(sb);
31310 +       for (bindex = 0; !err && bindex <= bend; bindex++)
31311 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
31312 +                       err = do_xib_restore
31313 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
31314 +               else
31315 +                       AuDbg("b%d\n", bindex);
31316 +       free_page((unsigned long)page);
31317 +
31318 +out:
31319 +       return err;
31320 +}
31321 +
31322 +int au_xib_trunc(struct super_block *sb)
31323 +{
31324 +       int err;
31325 +       ssize_t sz;
31326 +       loff_t pos;
31327 +       struct au_xino_lock_dir ldir;
31328 +       struct au_sbinfo *sbinfo;
31329 +       unsigned long *p;
31330 +       struct file *file;
31331 +
31332 +       SiMustWriteLock(sb);
31333 +
31334 +       err = 0;
31335 +       sbinfo = au_sbi(sb);
31336 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
31337 +               goto out;
31338 +
31339 +       file = sbinfo->si_xib;
31340 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
31341 +               goto out;
31342 +
31343 +       au_xino_lock_dir(sb, file, &ldir);
31344 +       /* mnt_want_write() is unnecessary here */
31345 +       file = au_xino_create2(sbinfo->si_xib, NULL);
31346 +       au_xino_unlock_dir(&ldir);
31347 +       err = PTR_ERR(file);
31348 +       if (IS_ERR(file))
31349 +               goto out;
31350 +       fput(sbinfo->si_xib);
31351 +       sbinfo->si_xib = file;
31352 +
31353 +       p = sbinfo->si_xib_buf;
31354 +       memset(p, 0, PAGE_SIZE);
31355 +       pos = 0;
31356 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
31357 +       if (unlikely(sz != PAGE_SIZE)) {
31358 +               err = sz;
31359 +               AuIOErr("err %d\n", err);
31360 +               if (sz >= 0)
31361 +                       err = -EIO;
31362 +               goto out;
31363 +       }
31364 +
31365 +       mutex_lock(&sbinfo->si_xib_mtx);
31366 +       /* mnt_want_write() is unnecessary here */
31367 +       err = xib_restore(sb);
31368 +       mutex_unlock(&sbinfo->si_xib_mtx);
31369 +
31370 +out:
31371 +       return err;
31372 +}
31373 +
31374 +/* ---------------------------------------------------------------------- */
31375 +
31376 +/*
31377 + * xino mount option handlers
31378 + */
31379 +static au_readf_t find_readf(struct file *h_file)
31380 +{
31381 +       const struct file_operations *fop = h_file->f_op;
31382 +
31383 +       if (fop) {
31384 +               if (fop->read)
31385 +                       return fop->read;
31386 +               if (fop->aio_read)
31387 +                       return do_sync_read;
31388 +       }
31389 +       return ERR_PTR(-ENOSYS);
31390 +}
31391 +
31392 +static au_writef_t find_writef(struct file *h_file)
31393 +{
31394 +       const struct file_operations *fop = h_file->f_op;
31395 +
31396 +       if (fop) {
31397 +               if (fop->write)
31398 +                       return fop->write;
31399 +               if (fop->aio_write)
31400 +                       return do_sync_write;
31401 +       }
31402 +       return ERR_PTR(-ENOSYS);
31403 +}
31404 +
31405 +/* xino bitmap */
31406 +static void xino_clear_xib(struct super_block *sb)
31407 +{
31408 +       struct au_sbinfo *sbinfo;
31409 +
31410 +       SiMustWriteLock(sb);
31411 +
31412 +       sbinfo = au_sbi(sb);
31413 +       sbinfo->si_xread = NULL;
31414 +       sbinfo->si_xwrite = NULL;
31415 +       if (sbinfo->si_xib)
31416 +               fput(sbinfo->si_xib);
31417 +       sbinfo->si_xib = NULL;
31418 +       free_page((unsigned long)sbinfo->si_xib_buf);
31419 +       sbinfo->si_xib_buf = NULL;
31420 +}
31421 +
31422 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
31423 +{
31424 +       int err;
31425 +       loff_t pos;
31426 +       struct au_sbinfo *sbinfo;
31427 +       struct file *file;
31428 +
31429 +       SiMustWriteLock(sb);
31430 +
31431 +       sbinfo = au_sbi(sb);
31432 +       file = au_xino_create2(base, sbinfo->si_xib);
31433 +       err = PTR_ERR(file);
31434 +       if (IS_ERR(file))
31435 +               goto out;
31436 +       if (sbinfo->si_xib)
31437 +               fput(sbinfo->si_xib);
31438 +       sbinfo->si_xib = file;
31439 +       sbinfo->si_xread = find_readf(file);
31440 +       sbinfo->si_xwrite = find_writef(file);
31441 +
31442 +       err = -ENOMEM;
31443 +       if (!sbinfo->si_xib_buf)
31444 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
31445 +       if (unlikely(!sbinfo->si_xib_buf))
31446 +               goto out_unset;
31447 +
31448 +       sbinfo->si_xib_last_pindex = 0;
31449 +       sbinfo->si_xib_next_bit = 0;
31450 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
31451 +               pos = 0;
31452 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
31453 +                                 PAGE_SIZE, &pos);
31454 +               if (unlikely(err != PAGE_SIZE))
31455 +                       goto out_free;
31456 +       }
31457 +       err = 0;
31458 +       goto out; /* success */
31459 +
31460 +out_free:
31461 +       free_page((unsigned long)sbinfo->si_xib_buf);
31462 +       sbinfo->si_xib_buf = NULL;
31463 +       if (err >= 0)
31464 +               err = -EIO;
31465 +out_unset:
31466 +       fput(sbinfo->si_xib);
31467 +       sbinfo->si_xib = NULL;
31468 +       sbinfo->si_xread = NULL;
31469 +       sbinfo->si_xwrite = NULL;
31470 +out:
31471 +       return err;
31472 +}
31473 +
31474 +/* xino for each branch */
31475 +static void xino_clear_br(struct super_block *sb)
31476 +{
31477 +       aufs_bindex_t bindex, bend;
31478 +       struct au_branch *br;
31479 +
31480 +       bend = au_sbend(sb);
31481 +       for (bindex = 0; bindex <= bend; bindex++) {
31482 +               br = au_sbr(sb, bindex);
31483 +               if (!br || !br->br_xino.xi_file)
31484 +                       continue;
31485 +
31486 +               fput(br->br_xino.xi_file);
31487 +               br->br_xino.xi_file = NULL;
31488 +       }
31489 +}
31490 +
31491 +static int au_xino_set_br(struct super_block *sb, struct file *base)
31492 +{
31493 +       int err;
31494 +       ino_t ino;
31495 +       aufs_bindex_t bindex, bend, bshared;
31496 +       struct {
31497 +               struct file *old, *new;
31498 +       } *fpair, *p;
31499 +       struct au_branch *br;
31500 +       struct inode *inode;
31501 +       au_writef_t writef;
31502 +
31503 +       SiMustWriteLock(sb);
31504 +
31505 +       err = -ENOMEM;
31506 +       bend = au_sbend(sb);
31507 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
31508 +       if (unlikely(!fpair))
31509 +               goto out;
31510 +
31511 +       inode = sb->s_root->d_inode;
31512 +       ino = AUFS_ROOT_INO;
31513 +       writef = au_sbi(sb)->si_xwrite;
31514 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
31515 +               br = au_sbr(sb, bindex);
31516 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
31517 +               if (bshared >= 0) {
31518 +                       /* shared xino */
31519 +                       *p = fpair[bshared];
31520 +                       get_file(p->new);
31521 +               }
31522 +
31523 +               if (!p->new) {
31524 +                       /* new xino */
31525 +                       p->old = br->br_xino.xi_file;
31526 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
31527 +                       err = PTR_ERR(p->new);
31528 +                       if (IS_ERR(p->new)) {
31529 +                               p->new = NULL;
31530 +                               goto out_pair;
31531 +                       }
31532 +               }
31533 +
31534 +               err = au_xino_do_write(writef, p->new,
31535 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
31536 +               if (unlikely(err))
31537 +                       goto out_pair;
31538 +       }
31539 +
31540 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
31541 +               br = au_sbr(sb, bindex);
31542 +               if (br->br_xino.xi_file)
31543 +                       fput(br->br_xino.xi_file);
31544 +               get_file(p->new);
31545 +               br->br_xino.xi_file = p->new;
31546 +       }
31547 +
31548 +out_pair:
31549 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
31550 +               if (p->new)
31551 +                       fput(p->new);
31552 +               else
31553 +                       break;
31554 +       kfree(fpair);
31555 +out:
31556 +       return err;
31557 +}
31558 +
31559 +void au_xino_clr(struct super_block *sb)
31560 +{
31561 +       struct au_sbinfo *sbinfo;
31562 +
31563 +       au_xigen_clr(sb);
31564 +       xino_clear_xib(sb);
31565 +       xino_clear_br(sb);
31566 +       sbinfo = au_sbi(sb);
31567 +       /* lvalue, do not call au_mntflags() */
31568 +       au_opt_clr(sbinfo->si_mntflags, XINO);
31569 +}
31570 +
31571 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
31572 +{
31573 +       int err, skip;
31574 +       struct dentry *parent, *cur_parent;
31575 +       struct qstr *dname, *cur_name;
31576 +       struct file *cur_xino;
31577 +       struct inode *dir;
31578 +       struct au_sbinfo *sbinfo;
31579 +
31580 +       SiMustWriteLock(sb);
31581 +
31582 +       err = 0;
31583 +       sbinfo = au_sbi(sb);
31584 +       parent = dget_parent(xino->file->f_dentry);
31585 +       if (remount) {
31586 +               skip = 0;
31587 +               dname = &xino->file->f_dentry->d_name;
31588 +               cur_xino = sbinfo->si_xib;
31589 +               if (cur_xino) {
31590 +                       cur_parent = dget_parent(cur_xino->f_dentry);
31591 +                       cur_name = &cur_xino->f_dentry->d_name;
31592 +                       skip = (cur_parent == parent
31593 +                               && dname->len == cur_name->len
31594 +                               && !memcmp(dname->name, cur_name->name,
31595 +                                          dname->len));
31596 +                       dput(cur_parent);
31597 +               }
31598 +               if (skip)
31599 +                       goto out;
31600 +       }
31601 +
31602 +       au_opt_set(sbinfo->si_mntflags, XINO);
31603 +       dir = parent->d_inode;
31604 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
31605 +       /* mnt_want_write() is unnecessary here */
31606 +       err = au_xino_set_xib(sb, xino->file);
31607 +       if (!err)
31608 +               err = au_xigen_set(sb, xino->file);
31609 +       if (!err)
31610 +               err = au_xino_set_br(sb, xino->file);
31611 +       mutex_unlock(&dir->i_mutex);
31612 +       if (!err)
31613 +               goto out; /* success */
31614 +
31615 +       /* reset all */
31616 +       AuIOErr("failed creating xino(%d).\n", err);
31617 +
31618 +out:
31619 +       dput(parent);
31620 +       return err;
31621 +}
31622 +
31623 +/* ---------------------------------------------------------------------- */
31624 +
31625 +/*
31626 + * create a xinofile at the default place/path.
31627 + */
31628 +struct file *au_xino_def(struct super_block *sb)
31629 +{
31630 +       struct file *file;
31631 +       char *page, *p;
31632 +       struct au_branch *br;
31633 +       struct super_block *h_sb;
31634 +       struct path path;
31635 +       aufs_bindex_t bend, bindex, bwr;
31636 +
31637 +       br = NULL;
31638 +       bend = au_sbend(sb);
31639 +       bwr = -1;
31640 +       for (bindex = 0; bindex <= bend; bindex++) {
31641 +               br = au_sbr(sb, bindex);
31642 +               if (au_br_writable(br->br_perm)
31643 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
31644 +                       bwr = bindex;
31645 +                       break;
31646 +               }
31647 +       }
31648 +
31649 +       if (bwr >= 0) {
31650 +               file = ERR_PTR(-ENOMEM);
31651 +               page = (void *)__get_free_page(GFP_NOFS);
31652 +               if (unlikely(!page))
31653 +                       goto out;
31654 +               path.mnt = au_br_mnt(br);
31655 +               path.dentry = au_h_dptr(sb->s_root, bwr);
31656 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
31657 +               file = (void *)p;
31658 +               if (!IS_ERR(p)) {
31659 +                       strcat(p, "/" AUFS_XINO_FNAME);
31660 +                       AuDbg("%s\n", p);
31661 +                       file = au_xino_create(sb, p, /*silent*/0);
31662 +                       if (!IS_ERR(file))
31663 +                               au_xino_brid_set(sb, br->br_id);
31664 +               }
31665 +               free_page((unsigned long)page);
31666 +       } else {
31667 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
31668 +               if (IS_ERR(file))
31669 +                       goto out;
31670 +               h_sb = file->f_dentry->d_sb;
31671 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
31672 +                       pr_err("xino doesn't support %s(%s)\n",
31673 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
31674 +                       fput(file);
31675 +                       file = ERR_PTR(-EINVAL);
31676 +               }
31677 +               if (!IS_ERR(file))
31678 +                       au_xino_brid_set(sb, -1);
31679 +       }
31680 +
31681 +out:
31682 +       return file;
31683 +}
31684 +
31685 +/* ---------------------------------------------------------------------- */
31686 +
31687 +int au_xino_path(struct seq_file *seq, struct file *file)
31688 +{
31689 +       int err;
31690 +
31691 +       err = au_seq_path(seq, &file->f_path);
31692 +       if (unlikely(err < 0))
31693 +               goto out;
31694 +
31695 +       err = 0;
31696 +#define Deleted "\\040(deleted)"
31697 +       seq->count -= sizeof(Deleted) - 1;
31698 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
31699 +                        sizeof(Deleted) - 1));
31700 +#undef Deleted
31701 +
31702 +out:
31703 +       return err;
31704 +}
31705 diff -urN /usr/share/empty/include/linux/aufs_type.h linux/include/linux/aufs_type.h
31706 --- /usr/share/empty/include/linux/aufs_type.h  1970-01-01 01:00:00.000000000 +0100
31707 +++ linux/include/linux/aufs_type.h     2013-07-06 13:20:47.760198800 +0200
31708 @@ -0,0 +1,19 @@
31709 +/*
31710 + * Copyright (C) 2012-2013 Junjiro R. Okajima
31711 + *
31712 + * This program, aufs is free software; you can redistribute it and/or modify
31713 + * it under the terms of the GNU General Public License as published by
31714 + * the Free Software Foundation; either version 2 of the License, or
31715 + * (at your option) any later version.
31716 + *
31717 + * This program is distributed in the hope that it will be useful,
31718 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31719 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31720 + * GNU General Public License for more details.
31721 + *
31722 + * You should have received a copy of the GNU General Public License
31723 + * along with this program; if not, write to the Free Software
31724 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
31725 + */
31726 +
31727 +#include <uapi/linux/aufs_type.h>
31728 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
31729 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
31730 +++ linux/include/uapi/linux/aufs_type.h        2013-08-23 23:59:39.638250372 +0200
31731 @@ -0,0 +1,251 @@
31732 +/*
31733 + * Copyright (C) 2005-2013 Junjiro R. Okajima
31734 + *
31735 + * This program, aufs is free software; you can redistribute it and/or modify
31736 + * it under the terms of the GNU General Public License as published by
31737 + * the Free Software Foundation; either version 2 of the License, or
31738 + * (at your option) any later version.
31739 + *
31740 + * This program is distributed in the hope that it will be useful,
31741 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31742 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31743 + * GNU General Public License for more details.
31744 + *
31745 + * You should have received a copy of the GNU General Public License
31746 + * along with this program; if not, write to the Free Software
31747 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
31748 + */
31749 +
31750 +#ifndef __AUFS_TYPE_H__
31751 +#define __AUFS_TYPE_H__
31752 +
31753 +#define AUFS_NAME      "aufs"
31754 +
31755 +#ifdef __KERNEL__
31756 +/*
31757 + * define it before including all other headers.
31758 + * sched.h may use pr_* macros before defining "current", so define the
31759 + * no-current version first, and re-define later.
31760 + */
31761 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
31762 +#include <linux/sched.h>
31763 +#undef pr_fmt
31764 +#define pr_fmt(fmt) \
31765 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
31766 +               (int)sizeof(current->comm), current->comm, current->pid
31767 +#else
31768 +#include <stdint.h>
31769 +#include <sys/types.h>
31770 +#endif /* __KERNEL__ */
31771 +
31772 +#include <linux/limits.h>
31773 +
31774 +#define AUFS_VERSION   "3.10-20130819"
31775 +
31776 +/* todo? move this to linux-2.6.19/include/magic.h */
31777 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
31778 +
31779 +/* ---------------------------------------------------------------------- */
31780 +
31781 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
31782 +typedef int8_t aufs_bindex_t;
31783 +#define AUFS_BRANCH_MAX 127
31784 +#else
31785 +typedef int16_t aufs_bindex_t;
31786 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
31787 +#define AUFS_BRANCH_MAX 511
31788 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
31789 +#define AUFS_BRANCH_MAX 1023
31790 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
31791 +#define AUFS_BRANCH_MAX 32767
31792 +#endif
31793 +#endif
31794 +
31795 +#ifdef __KERNEL__
31796 +#ifndef AUFS_BRANCH_MAX
31797 +#error unknown CONFIG_AUFS_BRANCH_MAX value
31798 +#endif
31799 +#endif /* __KERNEL__ */
31800 +
31801 +/* ---------------------------------------------------------------------- */
31802 +
31803 +#define AUFS_FSTYPE            AUFS_NAME
31804 +
31805 +#define AUFS_ROOT_INO          2
31806 +#define AUFS_FIRST_INO         11
31807 +
31808 +#define AUFS_WH_PFX            ".wh."
31809 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
31810 +#define AUFS_WH_TMP_LEN                4
31811 +/* a limit for rmdir/rename a dir and copyup */
31812 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
31813 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
31814 +                               - 1                     /* dot */\
31815 +                               - AUFS_WH_TMP_LEN)      /* hex */
31816 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
31817 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
31818 +#define AUFS_XINO_TRUNC_INIT   64 /* blocks */
31819 +#define AUFS_XINO_TRUNC_STEP   4  /* blocks */
31820 +#define AUFS_DIRWH_DEF         3
31821 +#define AUFS_RDCACHE_DEF       10 /* seconds */
31822 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
31823 +#define AUFS_RDBLK_DEF         512 /* bytes */
31824 +#define AUFS_RDHASH_DEF                32
31825 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
31826 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
31827 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
31828 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
31829 +
31830 +/* pseudo-link maintenace under /proc */
31831 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
31832 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
31833 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
31834 +
31835 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
31836 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
31837 +
31838 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
31839 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
31840 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
31841 +
31842 +/* doubly whiteouted */
31843 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
31844 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
31845 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
31846 +
31847 +/* branch permissions and attributes */
31848 +#define AUFS_BRPERM_RW         "rw"
31849 +#define AUFS_BRPERM_RO         "ro"
31850 +#define AUFS_BRPERM_RR         "rr"
31851 +#define AUFS_BRRATTR_WH                "wh"
31852 +#define AUFS_BRWATTR_NLWH      "nolwh"
31853 +#define AUFS_BRATTR_UNPIN      "unpin"
31854 +
31855 +/* ---------------------------------------------------------------------- */
31856 +
31857 +/* ioctl */
31858 +enum {
31859 +       /* readdir in userspace */
31860 +       AuCtl_RDU,
31861 +       AuCtl_RDU_INO,
31862 +
31863 +       /* pathconf wrapper */
31864 +       AuCtl_WBR_FD,
31865 +
31866 +       /* busy inode */
31867 +       AuCtl_IBUSY,
31868 +
31869 +       /* move-down */
31870 +       AuCtl_MVDOWN
31871 +};
31872 +
31873 +/* borrowed from linux/include/linux/kernel.h */
31874 +#ifndef ALIGN
31875 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
31876 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
31877 +#endif
31878 +
31879 +/* borrowed from linux/include/linux/compiler-gcc3.h */
31880 +#ifndef __aligned
31881 +#define __aligned(x)                   __attribute__((aligned(x)))
31882 +#endif
31883 +
31884 +#ifdef __KERNEL__
31885 +#ifndef __packed
31886 +#define __packed                       __attribute__((packed))
31887 +#endif
31888 +#endif
31889 +
31890 +struct au_rdu_cookie {
31891 +       uint64_t        h_pos;
31892 +       int16_t         bindex;
31893 +       uint8_t         flags;
31894 +       uint8_t         pad;
31895 +       uint32_t        generation;
31896 +} __aligned(8);
31897 +
31898 +struct au_rdu_ent {
31899 +       uint64_t        ino;
31900 +       int16_t         bindex;
31901 +       uint8_t         type;
31902 +       uint8_t         nlen;
31903 +       uint8_t         wh;
31904 +       char            name[0];
31905 +} __aligned(8);
31906 +
31907 +static inline int au_rdu_len(int nlen)
31908 +{
31909 +       /* include the terminating NULL */
31910 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
31911 +                    sizeof(uint64_t));
31912 +}
31913 +
31914 +union au_rdu_ent_ul {
31915 +       struct au_rdu_ent __user        *e;
31916 +       uint64_t                        ul;
31917 +};
31918 +
31919 +enum {
31920 +       AufsCtlRduV_SZ,
31921 +       AufsCtlRduV_End
31922 +};
31923 +
31924 +struct aufs_rdu {
31925 +       /* input */
31926 +       union {
31927 +               uint64_t        sz;     /* AuCtl_RDU */
31928 +               uint64_t        nent;   /* AuCtl_RDU_INO */
31929 +       };
31930 +       union au_rdu_ent_ul     ent;
31931 +       uint16_t                verify[AufsCtlRduV_End];
31932 +
31933 +       /* input/output */
31934 +       uint32_t                blk;
31935 +
31936 +       /* output */
31937 +       union au_rdu_ent_ul     tail;
31938 +       /* number of entries which were added in a single call */
31939 +       uint64_t                rent;
31940 +       uint8_t                 full;
31941 +       uint8_t                 shwh;
31942 +
31943 +       struct au_rdu_cookie    cookie;
31944 +} __aligned(8);
31945 +
31946 +/* ---------------------------------------------------------------------- */
31947 +
31948 +struct aufs_wbr_fd {
31949 +       uint32_t        oflags;
31950 +       int16_t         brid;
31951 +} __aligned(8);
31952 +
31953 +/* ---------------------------------------------------------------------- */
31954 +
31955 +struct aufs_ibusy {
31956 +       uint64_t        ino, h_ino;
31957 +       int16_t         bindex;
31958 +} __aligned(8);
31959 +
31960 +/* ---------------------------------------------------------------------- */
31961 +
31962 +/* flags for move-down */
31963 +#define AUFS_MVDOWN_VERBOSE    1
31964 +/* will be added more */
31965 +
31966 +struct aufs_mvdown {
31967 +       uint8_t         flags;
31968 +       /* will be added more */
31969 +} __aligned(8);
31970 +
31971 +/* ---------------------------------------------------------------------- */
31972 +
31973 +#define AuCtlType              'A'
31974 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
31975 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
31976 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
31977 +                                    struct aufs_wbr_fd)
31978 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
31979 +#define AUFS_CTL_MVDOWN                _IOR(AuCtlType, AuCtl_MVDOWN, \
31980 +                                    struct aufs_mvdown)
31981 +
31982 +#endif /* __AUFS_TYPE_H__ */
This page took 2.991031 seconds and 4 git commands to generate.