]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs3.patch
- sorted kernel configs
[packages/kernel.git] / kernel-aufs3.patch
1 aufs3.15 kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index 312393f..78632ed 100644
5 --- a/fs/Kconfig
6 +++ b/fs/Kconfig
7 @@ -209,6 +209,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 f9cb987..a0c580f 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 6929571..351ca48 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.15 base patch
37
38 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39 index f70a230..138104b 100644
40 --- a/drivers/block/loop.c
41 +++ b/drivers/block/loop.c
42 @@ -692,6 +692,24 @@ static inline int is_loop_device(struct file *file)
43         return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
44  }
45  
46 +/*
47 + * for AUFS
48 + * no get/put for file.
49 + */
50 +struct file *loop_backing_file(struct super_block *sb)
51 +{
52 +       struct file *ret;
53 +       struct loop_device *l;
54 +
55 +       ret = NULL;
56 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
57 +               l = sb->s_bdev->bd_disk->private_data;
58 +               ret = l->lo_backing_file;
59 +       }
60 +       return ret;
61 +}
62 +EXPORT_SYMBOL(loop_backing_file);
63 +
64  /* loop sysfs attributes */
65  
66  static ssize_t loop_attr_show(struct device *dev, char *page,
67 diff --git a/fs/inode.c b/fs/inode.c
68 index f96d2a6..2d72083 100644
69 --- a/fs/inode.c
70 +++ b/fs/inode.c
71 @@ -1496,7 +1496,7 @@ static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
72   * This does the actual work of updating an inodes time or version.  Must have
73   * had called mnt_want_write() before calling this.
74   */
75 -static int update_time(struct inode *inode, struct timespec *time, int flags)
76 +int update_time(struct inode *inode, struct timespec *time, int flags)
77  {
78         if (inode->i_op->update_time)
79                 return inode->i_op->update_time(inode, time, flags);
80 diff --git a/fs/splice.c b/fs/splice.c
81 index e246954..4797013 100644
82 --- a/fs/splice.c
83 +++ b/fs/splice.c
84 @@ -1103,8 +1103,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
85  /*
86   * Attempt to initiate a splice from pipe to file.
87   */
88 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
89 -                          loff_t *ppos, size_t len, unsigned int flags)
90 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
91 +                   loff_t *ppos, size_t len, unsigned int flags)
92  {
93         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
94                                 loff_t *, size_t, unsigned int);
95 @@ -1120,9 +1120,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
96  /*
97   * Attempt to initiate a splice from a file to a pipe.
98   */
99 -static long do_splice_to(struct file *in, loff_t *ppos,
100 -                        struct pipe_inode_info *pipe, size_t len,
101 -                        unsigned int flags)
102 +long do_splice_to(struct file *in, loff_t *ppos,
103 +                 struct pipe_inode_info *pipe, size_t len,
104 +                 unsigned int flags)
105  {
106         ssize_t (*splice_read)(struct file *, loff_t *,
107                                struct pipe_inode_info *, size_t, unsigned int);
108 diff --git a/include/linux/fs.h b/include/linux/fs.h
109 index 8780312..3ff3e89 100644
110 --- a/include/linux/fs.h
111 +++ b/include/linux/fs.h
112 @@ -2606,6 +2606,7 @@ extern int inode_change_ok(const struct inode *, struct iattr *);
113  extern int inode_newsize_ok(const struct inode *, loff_t offset);
114  extern void setattr_copy(struct inode *inode, const struct iattr *attr);
115  
116 +extern int update_time(struct inode *, struct timespec *, int);
117  extern int file_update_time(struct file *file);
118  
119  extern int generic_show_options(struct seq_file *m, struct dentry *root);
120 diff --git a/include/linux/splice.h b/include/linux/splice.h
121 index 0e43906..304169e 100644
122 --- a/include/linux/splice.h
123 +++ b/include/linux/splice.h
124 @@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
125  extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
126  
127  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
128 +
129 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
130 +                          loff_t *ppos, size_t len, unsigned int flags);
131 +extern long do_splice_to(struct file *in, loff_t *ppos,
132 +                        struct pipe_inode_info *pipe, size_t len,
133 +                        unsigned int flags);
134  #endif
135 aufs3.15 mmap patch
136
137 diff --git a/fs/buffer.c b/fs/buffer.c
138 index 9ddb9fc..1059a0b 100644
139 --- a/fs/buffer.c
140 +++ b/fs/buffer.c
141 @@ -2448,7 +2448,7 @@ int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
142          * Update file times before taking page lock. We may end up failing the
143          * fault so this update may be superfluous but who really cares...
144          */
145 -       file_update_time(vma->vm_file);
146 +       vma_file_update_time(vma);
147  
148         ret = __block_page_mkwrite(vma, vmf, get_block);
149         sb_end_pagefault(sb);
150 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
151 index d4a3574..e44a744 100644
152 --- a/fs/proc/nommu.c
153 +++ b/fs/proc/nommu.c
154 @@ -45,7 +45,9 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
155         file = region->vm_file;
156  
157         if (file) {
158 -               struct inode *inode = file_inode(region->vm_file);
159 +               struct inode *inode;
160 +               file = vmr_pr_or_file(region);
161 +               inode = file_inode(file);
162                 dev = inode->i_sb->s_dev;
163                 ino = inode->i_ino;
164         }
165 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
166 index c4b2646..dc3c0dc 100644
167 --- a/fs/proc/task_mmu.c
168 +++ b/fs/proc/task_mmu.c
169 @@ -265,7 +265,9 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
170         const char *name = NULL;
171  
172         if (file) {
173 -               struct inode *inode = file_inode(vma->vm_file);
174 +               struct inode *inode;
175 +               file = vma_pr_or_file(vma);
176 +               inode = file_inode(file);
177                 dev = inode->i_sb->s_dev;
178                 ino = inode->i_ino;
179                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
180 @@ -1408,6 +1410,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
181         seq_printf(m, "%08lx %s", vma->vm_start, buffer);
182  
183         if (file) {
184 +               file = vma_pr_or_file(vma);
185                 seq_printf(m, " file=");
186                 seq_path(m, &file->f_path, "\n\t= ");
187         } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
188 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
189 index 678455d..ad0ce45 100644
190 --- a/fs/proc/task_nommu.c
191 +++ b/fs/proc/task_nommu.c
192 @@ -141,7 +141,9 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
193         file = vma->vm_file;
194  
195         if (file) {
196 -               struct inode *inode = file_inode(vma->vm_file);
197 +               struct inode *inode;
198 +               file = vma_pr_or_file(file);
199 +               inode = file_inode(file);
200                 dev = inode->i_sb->s_dev;
201                 ino = inode->i_ino;
202                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
203 diff --git a/include/linux/mm.h b/include/linux/mm.h
204 index d677706..192e7ac4 100644
205 --- a/include/linux/mm.h
206 +++ b/include/linux/mm.h
207 @@ -18,6 +18,9 @@
208  #include <linux/pfn.h>
209  #include <linux/bit_spinlock.h>
210  #include <linux/shrinker.h>
211 +#include <linux/dcache.h>
212 +#include <linux/file.h>
213 +#include <linux/fs.h>
214  
215  struct mempolicy;
216  struct anon_vma;
217 @@ -1173,6 +1176,87 @@ static inline int fixup_user_fault(struct task_struct *tsk,
218  }
219  #endif
220  
221 +/*
222 + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
223 + * in /proc/PID/maps.
224 + */
225 +/* #define AUFS_DEBUG_MMAP */
226 +static inline void aufs_trace(struct file *f, struct file *pr,
227 +                             const char func[], int line, const char func2[])
228 +{
229 +#ifdef AUFS_DEBUG_MMAP
230 +       if (pr)
231 +               pr_info("%s:%d: %s, %p\n", func, line, func2,
232 +                       f ? (char *)f->f_dentry->d_name.name : "(null)");
233 +#endif
234 +}
235 +
236 +static inline struct file *vmr_do_pr_or_file(struct vm_region *region,
237 +                                            const char func[], int line)
238 +{
239 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
240 +       aufs_trace(f, pr, func, line, __func__);
241 +       return (f && pr) ? pr : f;
242 +}
243 +
244 +static inline void vmr_do_fput(struct vm_region *region,
245 +                              const char func[], int line)
246 +{
247 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
248 +       aufs_trace(f, pr, func, line, __func__);
249 +       fput(f);
250 +       if (f && pr)
251 +               fput(pr);
252 +}
253 +
254 +static inline void vma_do_file_update_time(struct vm_area_struct *vma,
255 +                                          const char func[], int line)
256 +{
257 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
258 +       aufs_trace(f, pr, func, line, __func__);
259 +       file_update_time(f);
260 +       if (f && pr)
261 +               file_update_time(pr);
262 +}
263 +
264 +static inline struct file *vma_do_pr_or_file(struct vm_area_struct *vma,
265 +                                            const char func[], int line)
266 +{
267 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
268 +       aufs_trace(f, pr, func, line, __func__);
269 +       return (f && pr) ? pr : f;
270 +}
271 +
272 +static inline void vma_do_get_file(struct vm_area_struct *vma,
273 +                                  const char func[], int line)
274 +{
275 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
276 +       aufs_trace(f, pr, func, line, __func__);
277 +       get_file(f);
278 +       if (f && pr)
279 +               get_file(pr);
280 +}
281 +
282 +static inline void vma_do_fput(struct vm_area_struct *vma,
283 +                              const char func[], int line)
284 +{
285 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
286 +       aufs_trace(f, pr, func, line, __func__);
287 +       fput(f);
288 +       if (f && pr)
289 +               fput(pr);
290 +}
291 +
292 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
293 +                                                         __LINE__)
294 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
295 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
296 +                                                               __LINE__)
297 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
298 +                                                         __LINE__)
299 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
300 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
301 +
302  extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
303  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
304                 void *buf, int len, int write);
305 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
306 index 8967e20..a57b589 100644
307 --- a/include/linux/mm_types.h
308 +++ b/include/linux/mm_types.h
309 @@ -232,6 +232,7 @@ struct vm_region {
310         unsigned long   vm_top;         /* region allocated to here */
311         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
312         struct file     *vm_file;       /* the backing file or NULL */
313 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
314  
315         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
316         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
317 @@ -300,6 +301,7 @@ struct vm_area_struct {
318         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
319                                            units, *not* PAGE_CACHE_SIZE */
320         struct file * vm_file;          /* File we map to (can be NULL). */
321 +       struct file *vm_prfile;         /* shadow of vm_file */
322         void * vm_private_data;         /* was vm_pte (shared mem) */
323  
324  #ifndef CONFIG_MMU
325 diff --git a/kernel/fork.c b/kernel/fork.c
326 index 54a8d26..dcf08b6 100644
327 --- a/kernel/fork.c
328 +++ b/kernel/fork.c
329 @@ -416,7 +416,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
330                         struct inode *inode = file_inode(file);
331                         struct address_space *mapping = file->f_mapping;
332  
333 -                       get_file(file);
334 +                       vma_get_file(tmp);
335                         if (tmp->vm_flags & VM_DENYWRITE)
336                                 atomic_dec(&inode->i_writecount);
337                         mutex_lock(&mapping->i_mmap_mutex);
338 diff --git a/mm/filemap.c b/mm/filemap.c
339 index 088358c..138a88c 100644
340 --- a/mm/filemap.c
341 +++ b/mm/filemap.c
342 @@ -2091,7 +2091,7 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
343         int ret = VM_FAULT_LOCKED;
344  
345         sb_start_pagefault(inode->i_sb);
346 -       file_update_time(vma->vm_file);
347 +       vma_file_update_time(vma);
348         lock_page(page);
349         if (page->mapping != inode->i_mapping) {
350                 unlock_page(page);
351 diff --git a/mm/fremap.c b/mm/fremap.c
352 index 34feba6..8d6c451 100644
353 --- a/mm/fremap.c
354 +++ b/mm/fremap.c
355 @@ -223,16 +223,27 @@ get_write_lock:
356                  */
357                 if (mapping_cap_account_dirty(mapping)) {
358                         unsigned long addr;
359 -                       struct file *file = get_file(vma->vm_file);
360 +                       struct file *file = vma->vm_file,
361 +                               *prfile = vma->vm_prfile;
362 +
363                         /* mmap_region may free vma; grab the info now */
364                         vm_flags = vma->vm_flags;
365  
366 +                       vma_get_file(vma);
367                         addr = mmap_region(file, start, size, vm_flags, pgoff);
368 -                       fput(file);
369 +                       vma_fput(vma);
370                         if (IS_ERR_VALUE(addr)) {
371                                 err = addr;
372                         } else {
373                                 BUG_ON(addr != start);
374 +                               if (prfile) {
375 +                                       struct vm_area_struct *new_vma;
376 +                                       new_vma = find_vma(mm, addr);
377 +                                       if (!new_vma->vm_prfile)
378 +                                               new_vma->vm_prfile = prfile;
379 +                                       if (new_vma != vma)
380 +                                               get_file(prfile);
381 +                               }
382                                 err = 0;
383                         }
384                         goto out_freed;
385 diff --git a/mm/madvise.c b/mm/madvise.c
386 index a402f8f..134e15d 100644
387 --- a/mm/madvise.c
388 +++ b/mm/madvise.c
389 @@ -327,12 +327,12 @@ static long madvise_remove(struct vm_area_struct *vma,
390          * vma's reference to the file) can go away as soon as we drop
391          * mmap_sem.
392          */
393 -       get_file(f);
394 +       vma_get_file(vma);
395         up_read(&current->mm->mmap_sem);
396         error = do_fallocate(f,
397                                 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
398                                 offset, end - start);
399 -       fput(f);
400 +       vma_fput(vma);
401         down_read(&current->mm->mmap_sem);
402         return error;
403  }
404 diff --git a/mm/memory.c b/mm/memory.c
405 index 037b812..6e7d241 100644
406 --- a/mm/memory.c
407 +++ b/mm/memory.c
408 @@ -2805,7 +2805,7 @@ reuse:
409                         set_page_dirty_balance(dirty_page);
410                         /* file_update_time outside page_lock */
411                         if (vma->vm_file)
412 -                               file_update_time(vma->vm_file);
413 +                               vma_file_update_time(vma);
414                 }
415                 put_page(dirty_page);
416                 if (page_mkwrite) {
417 diff --git a/mm/mmap.c b/mm/mmap.c
418 index b1202cf..40dd067 100644
419 --- a/mm/mmap.c
420 +++ b/mm/mmap.c
421 @@ -250,7 +250,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
422         if (vma->vm_ops && vma->vm_ops->close)
423                 vma->vm_ops->close(vma);
424         if (vma->vm_file)
425 -               fput(vma->vm_file);
426 +               vma_fput(vma);
427         mpol_put(vma_policy(vma));
428         kmem_cache_free(vm_area_cachep, vma);
429         return next;
430 @@ -861,7 +861,7 @@ again:                      remove_next = 1 + (end > next->vm_end);
431         if (remove_next) {
432                 if (file) {
433                         uprobe_munmap(next, next->vm_start, next->vm_end);
434 -                       fput(file);
435 +                       vma_fput(vma);
436                 }
437                 if (next->anon_vma)
438                         anon_vma_merge(vma, next);
439 @@ -1641,8 +1641,8 @@ out:
440  unmap_and_free_vma:
441         if (vm_flags & VM_DENYWRITE)
442                 allow_write_access(file);
443 +       vma_fput(vma);
444         vma->vm_file = NULL;
445 -       fput(file);
446  
447         /* Undo any partial mapping done by a device driver. */
448         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
449 @@ -2432,7 +2432,7 @@ static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
450                 goto out_free_mpol;
451  
452         if (new->vm_file)
453 -               get_file(new->vm_file);
454 +               vma_get_file(new);
455  
456         if (new->vm_ops && new->vm_ops->open)
457                 new->vm_ops->open(new);
458 @@ -2451,7 +2451,7 @@ static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
459         if (new->vm_ops && new->vm_ops->close)
460                 new->vm_ops->close(new);
461         if (new->vm_file)
462 -               fput(new->vm_file);
463 +               vma_fput(new);
464         unlink_anon_vmas(new);
465   out_free_mpol:
466         mpol_put(vma_policy(new));
467 @@ -2840,7 +2840,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
468                         if (anon_vma_clone(new_vma, vma))
469                                 goto out_free_mempol;
470                         if (new_vma->vm_file)
471 -                               get_file(new_vma->vm_file);
472 +                               vma_get_file(new_vma);
473                         if (new_vma->vm_ops && new_vma->vm_ops->open)
474                                 new_vma->vm_ops->open(new_vma);
475                         vma_link(mm, new_vma, prev, rb_link, rb_parent);
476 diff --git a/mm/msync.c b/mm/msync.c
477 index 632df45..02d770e 100644
478 --- a/mm/msync.c
479 +++ b/mm/msync.c
480 @@ -80,10 +80,10 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
481                 start = vma->vm_end;
482                 if ((flags & MS_SYNC) && file &&
483                                 (vma->vm_flags & VM_SHARED)) {
484 -                       get_file(file);
485 +                       vma_get_file(vma);
486                         up_read(&mm->mmap_sem);
487                         error = vfs_fsync(file, 0);
488 -                       fput(file);
489 +                       vma_fput(vma);
490                         if (error || start >= end)
491                                 goto out;
492                         down_read(&mm->mmap_sem);
493 diff --git a/mm/nommu.c b/mm/nommu.c
494 index 85f8d66..9f471fa 100644
495 --- a/mm/nommu.c
496 +++ b/mm/nommu.c
497 @@ -655,7 +655,7 @@ static void __put_nommu_region(struct vm_region *region)
498                 up_write(&nommu_region_sem);
499  
500                 if (region->vm_file)
501 -                       fput(region->vm_file);
502 +                       vmr_fput(region);
503  
504                 /* IO memory and memory shared directly out of the pagecache
505                  * from ramfs/tmpfs mustn't be released here */
506 @@ -820,7 +820,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
507         if (vma->vm_ops && vma->vm_ops->close)
508                 vma->vm_ops->close(vma);
509         if (vma->vm_file)
510 -               fput(vma->vm_file);
511 +               vma_fput(vma);
512         put_nommu_region(vma->vm_region);
513         kmem_cache_free(vm_area_cachep, vma);
514  }
515 @@ -1382,7 +1382,7 @@ unsigned long do_mmap_pgoff(struct file *file,
516                                         goto error_just_free;
517                                 }
518                         }
519 -                       fput(region->vm_file);
520 +                       vmr_fput(region);
521                         kmem_cache_free(vm_region_jar, region);
522                         region = pregion;
523                         result = start;
524 @@ -1458,10 +1458,10 @@ error_just_free:
525         up_write(&nommu_region_sem);
526  error:
527         if (region->vm_file)
528 -               fput(region->vm_file);
529 +               vmr_fput(region);
530         kmem_cache_free(vm_region_jar, region);
531         if (vma->vm_file)
532 -               fput(vma->vm_file);
533 +               vma_fput(vma);
534         kmem_cache_free(vm_area_cachep, vma);
535         kleave(" = %d", ret);
536         return ret;
537 aufs3.15 standalone patch
538
539 diff --git a/fs/inode.c b/fs/inode.c
540 index 2d72083..30b69da 100644
541 --- a/fs/inode.c
542 +++ b/fs/inode.c
543 @@ -57,6 +57,7 @@ static struct hlist_head *inode_hashtable __read_mostly;
544  static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
545  
546  __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
547 +EXPORT_SYMBOL(inode_sb_list_lock);
548  
549  /*
550   * Empty aops. Can be used for the cases where the user does not
551 @@ -1512,6 +1513,7 @@ int update_time(struct inode *inode, struct timespec *time, int flags)
552         mark_inode_dirty_sync(inode);
553         return 0;
554  }
555 +EXPORT_SYMBOL(update_time);
556  
557  /**
558   *     touch_atime     -       update the access time
559 diff --git a/fs/namespace.c b/fs/namespace.c
560 index 182bc41..c88e101 100644
561 --- a/fs/namespace.c
562 +++ b/fs/namespace.c
563 @@ -453,6 +453,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
564         mnt_dec_writers(real_mount(mnt));
565         preempt_enable();
566  }
567 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
568  
569  /**
570   * mnt_drop_write - give up write access to a mount
571 @@ -1564,6 +1565,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
572         }
573         return 0;
574  }
575 +EXPORT_SYMBOL(iterate_mounts);
576  
577  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
578  {
579 diff --git a/fs/notify/group.c b/fs/notify/group.c
580 index ad19959..adf290d 100644
581 --- a/fs/notify/group.c
582 +++ b/fs/notify/group.c
583 @@ -22,6 +22,7 @@
584  #include <linux/srcu.h>
585  #include <linux/rculist.h>
586  #include <linux/wait.h>
587 +#include <linux/module.h>
588  
589  #include <linux/fsnotify_backend.h>
590  #include "fsnotify.h"
591 @@ -72,6 +73,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
592  {
593         atomic_inc(&group->refcnt);
594  }
595 +EXPORT_SYMBOL(fsnotify_get_group);
596  
597  /*
598   * Drop a reference to a group.  Free it if it's through.
599 @@ -81,6 +83,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
600         if (atomic_dec_and_test(&group->refcnt))
601                 fsnotify_final_destroy_group(group);
602  }
603 +EXPORT_SYMBOL(fsnotify_put_group);
604  
605  /*
606   * Create a new fsnotify_group and hold a reference for the group returned.
607 @@ -109,6 +112,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
608  
609         return group;
610  }
611 +EXPORT_SYMBOL(fsnotify_alloc_group);
612  
613  int fsnotify_fasync(int fd, struct file *file, int on)
614  {
615 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
616 index 923fe4a..176b435 100644
617 --- a/fs/notify/mark.c
618 +++ b/fs/notify/mark.c
619 @@ -109,6 +109,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
620                 mark->free_mark(mark);
621         }
622  }
623 +EXPORT_SYMBOL(fsnotify_put_mark);
624  
625  /*
626   * Any time a mark is getting freed we end up here.
627 @@ -191,6 +192,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
628         fsnotify_destroy_mark_locked(mark, group);
629         mutex_unlock(&group->mark_mutex);
630  }
631 +EXPORT_SYMBOL(fsnotify_destroy_mark);
632  
633  void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
634  {
635 @@ -275,6 +277,7 @@ err:
636  
637         return ret;
638  }
639 +EXPORT_SYMBOL(fsnotify_add_mark);
640  
641  int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
642                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
643 @@ -336,6 +339,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
644         atomic_set(&mark->refcnt, 1);
645         mark->free_mark = free_mark;
646  }
647 +EXPORT_SYMBOL(fsnotify_init_mark);
648  
649  static int fsnotify_mark_destroy(void *ignored)
650  {
651 diff --git a/fs/open.c b/fs/open.c
652 index 9d64679..1aa876a 100644
653 --- a/fs/open.c
654 +++ b/fs/open.c
655 @@ -62,6 +62,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
656         mutex_unlock(&dentry->d_inode->i_mutex);
657         return ret;
658  }
659 +EXPORT_SYMBOL(do_truncate);
660  
661  long vfs_truncate(struct path *path, loff_t length)
662  {
663 @@ -299,6 +300,7 @@ int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
664         sb_end_write(inode->i_sb);
665         return ret;
666  }
667 +EXPORT_SYMBOL(do_fallocate);
668  
669  SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
670  {
671 diff --git a/fs/splice.c b/fs/splice.c
672 index 4797013..44b603a 100644
673 --- a/fs/splice.c
674 +++ b/fs/splice.c
675 @@ -1116,6 +1116,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
676  
677         return splice_write(pipe, out, ppos, len, flags);
678  }
679 +EXPORT_SYMBOL(do_splice_from);
680  
681  /*
682   * Attempt to initiate a splice from a file to a pipe.
683 @@ -1142,6 +1143,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
684  
685         return splice_read(in, ppos, pipe, len, flags);
686  }
687 +EXPORT_SYMBOL(do_splice_to);
688  
689  /**
690   * splice_direct_to_actor - splices data directly between two non-pipes
691 diff --git a/security/commoncap.c b/security/commoncap.c
692 index b9d613e..ba3b618 100644
693 --- a/security/commoncap.c
694 +++ b/security/commoncap.c
695 @@ -988,9 +988,11 @@ int cap_mmap_addr(unsigned long addr)
696         }
697         return ret;
698  }
699 +EXPORT_SYMBOL(cap_mmap_addr);
700  
701  int cap_mmap_file(struct file *file, unsigned long reqprot,
702                   unsigned long prot, unsigned long flags)
703  {
704         return 0;
705  }
706 +EXPORT_SYMBOL(cap_mmap_file);
707 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
708 index 9134dbf..4d4217a 100644
709 --- a/security/device_cgroup.c
710 +++ b/security/device_cgroup.c
711 @@ -7,6 +7,7 @@
712  #include <linux/device_cgroup.h>
713  #include <linux/cgroup.h>
714  #include <linux/ctype.h>
715 +#include <linux/export.h>
716  #include <linux/list.h>
717  #include <linux/uaccess.h>
718  #include <linux/seq_file.h>
719 @@ -856,6 +857,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
720         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
721                         access);
722  }
723 +EXPORT_SYMBOL(__devcgroup_inode_permission);
724  
725  int devcgroup_inode_mknod(int mode, dev_t dev)
726  {
727 diff --git a/security/security.c b/security/security.c
728 index 8b774f3..e3190b9 100644
729 --- a/security/security.c
730 +++ b/security/security.c
731 @@ -407,6 +407,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
732                 return 0;
733         return security_ops->path_rmdir(dir, dentry);
734  }
735 +EXPORT_SYMBOL(security_path_rmdir);
736  
737  int security_path_unlink(struct path *dir, struct dentry *dentry)
738  {
739 @@ -423,6 +424,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
740                 return 0;
741         return security_ops->path_symlink(dir, dentry, old_name);
742  }
743 +EXPORT_SYMBOL(security_path_symlink);
744  
745  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
746                        struct dentry *new_dentry)
747 @@ -431,6 +433,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
748                 return 0;
749         return security_ops->path_link(old_dentry, new_dir, new_dentry);
750  }
751 +EXPORT_SYMBOL(security_path_link);
752  
753  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
754                          struct path *new_dir, struct dentry *new_dentry,
755 @@ -458,6 +461,7 @@ int security_path_truncate(struct path *path)
756                 return 0;
757         return security_ops->path_truncate(path);
758  }
759 +EXPORT_SYMBOL(security_path_truncate);
760  
761  int security_path_chmod(struct path *path, umode_t mode)
762  {
763 @@ -465,6 +469,7 @@ int security_path_chmod(struct path *path, umode_t mode)
764                 return 0;
765         return security_ops->path_chmod(path, mode);
766  }
767 +EXPORT_SYMBOL(security_path_chmod);
768  
769  int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
770  {
771 @@ -472,6 +477,7 @@ int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
772                 return 0;
773         return security_ops->path_chown(path, uid, gid);
774  }
775 +EXPORT_SYMBOL(security_path_chown);
776  
777  int security_path_chroot(struct path *path)
778  {
779 @@ -557,6 +563,7 @@ int security_inode_readlink(struct dentry *dentry)
780                 return 0;
781         return security_ops->inode_readlink(dentry);
782  }
783 +EXPORT_SYMBOL(security_inode_readlink);
784  
785  int security_inode_follow_link(struct dentry *dentry, struct nameidata *nd)
786  {
787 @@ -571,6 +578,7 @@ int security_inode_permission(struct inode *inode, int mask)
788                 return 0;
789         return security_ops->inode_permission(inode, mask);
790  }
791 +EXPORT_SYMBOL(security_inode_permission);
792  
793  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
794  {
795 @@ -693,6 +701,7 @@ int security_file_permission(struct file *file, int mask)
796  
797         return fsnotify_perm(file, mask);
798  }
799 +EXPORT_SYMBOL(security_file_permission);
800  
801  int security_file_alloc(struct file *file)
802  {
803 @@ -753,6 +762,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
804                 return ret;
805         return ima_file_mmap(file, prot);
806  }
807 +EXPORT_SYMBOL(security_mmap_file);
808  
809  int security_mmap_addr(unsigned long addr)
810  {
811 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
812 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
813 +++ linux/Documentation/ABI/testing/debugfs-aufs        2014-06-24 00:14:06.769385243 +0200
814 @@ -0,0 +1,50 @@
815 +What:          /debug/aufs/si_<id>/
816 +Date:          March 2009
817 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
818 +Description:
819 +               Under /debug/aufs, a directory named si_<id> is created
820 +               per aufs mount, where <id> is a unique id generated
821 +               internally.
822 +
823 +What:          /debug/aufs/si_<id>/plink
824 +Date:          Apr 2013
825 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
826 +Description:
827 +               It has three lines and shows the information about the
828 +               pseudo-link. The first line is a single number
829 +               representing a number of buckets. The second line is a
830 +               number of pseudo-links per buckets (separated by a
831 +               blank). The last line is a single number representing a
832 +               total number of psedo-links.
833 +               When the aufs mount option 'noplink' is specified, it
834 +               will show "1\n0\n0\n".
835 +
836 +What:          /debug/aufs/si_<id>/xib
837 +Date:          March 2009
838 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
839 +Description:
840 +               It shows the consumed blocks by xib (External Inode Number
841 +               Bitmap), its block size and file size.
842 +               When the aufs mount option 'noxino' is specified, it
843 +               will be empty. About XINO files, see the aufs manual.
844 +
845 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
846 +Date:          March 2009
847 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
848 +Description:
849 +               It shows the consumed blocks by xino (External Inode Number
850 +               Translation Table), its link count, block size and file
851 +               size.
852 +               When the aufs mount option 'noxino' is specified, it
853 +               will be empty. About XINO files, see the aufs manual.
854 +
855 +What:          /debug/aufs/si_<id>/xigen
856 +Date:          March 2009
857 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
858 +Description:
859 +               It shows the consumed blocks by xigen (External Inode
860 +               Generation Table), its block size and file size.
861 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
862 +               be created.
863 +               When the aufs mount option 'noxino' is specified, it
864 +               will be empty. About XINO files, see the aufs manual.
865 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
866 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
867 +++ linux/Documentation/ABI/testing/sysfs-aufs  2014-06-24 00:14:06.769385243 +0200
868 @@ -0,0 +1,31 @@
869 +What:          /sys/fs/aufs/si_<id>/
870 +Date:          March 2009
871 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
872 +Description:
873 +               Under /sys/fs/aufs, a directory named si_<id> is created
874 +               per aufs mount, where <id> is a unique id generated
875 +               internally.
876 +
877 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
878 +Date:          March 2009
879 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
880 +Description:
881 +               It shows the abolute path of a member directory (which
882 +               is called branch) in aufs, and its permission.
883 +
884 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
885 +Date:          July 2013
886 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
887 +Description:
888 +               It shows the id of a member directory (which is called
889 +               branch) in aufs.
890 +
891 +What:          /sys/fs/aufs/si_<id>/xi_path
892 +Date:          March 2009
893 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
894 +Description:
895 +               It shows the abolute path of XINO (External Inode Number
896 +               Bitmap, Translation Table and Generation Table) file
897 +               even if it is the default path.
898 +               When the aufs mount option 'noxino' is specified, it
899 +               will be empty. About XINO files, see the aufs manual.
900 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
901 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
902 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2014-06-24 00:14:06.772718759 +0200
903 @@ -0,0 +1,161 @@
904 +
905 +# Copyright (C) 2005-2014 Junjiro R. Okajima
906 +# 
907 +# This program is free software; you can redistribute it and/or modify
908 +# it under the terms of the GNU General Public License as published by
909 +# the Free Software Foundation; either version 2 of the License, or
910 +# (at your option) any later version.
911 +# 
912 +# This program is distributed in the hope that it will be useful,
913 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
914 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
915 +# GNU General Public License for more details.
916 +# 
917 +# You should have received a copy of the GNU General Public License
918 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
919 +
920 +Introduction
921 +----------------------------------------
922 +
923 +aufs [ei ju: ef es] | [a u f s]
924 +1. abbrev. for "advanced multi-layered unification filesystem".
925 +2. abbrev. for "another unionfs".
926 +3. abbrev. for "auf das" in German which means "on the" in English.
927 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
928 +   But "Filesystem aufs Filesystem" is hard to understand.
929 +
930 +AUFS is a filesystem with features:
931 +- multi layered stackable unification filesystem, the member directory
932 +  is called as a branch.
933 +- branch permission and attribute, 'readonly', 'real-readonly',
934 +  'readwrite', 'whiteout-able', 'link-able whiteout' and their
935 +  combination.
936 +- internal "file copy-on-write".
937 +- logical deletion, whiteout.
938 +- dynamic branch manipulation, adding, deleting and changing permission.
939 +- allow bypassing aufs, user's direct branch access.
940 +- external inode number translation table and bitmap which maintains the
941 +  persistent aufs inode number.
942 +- seekable directory, including NFS readdir.
943 +- file mapping, mmap and sharing pages.
944 +- pseudo-link, hardlink over branches.
945 +- loopback mounted filesystem as a branch.
946 +- several policies to select one among multiple writable branches.
947 +- revert a single systemcall when an error occurs in aufs.
948 +- and more...
949 +
950 +
951 +Multi Layered Stackable Unification Filesystem
952 +----------------------------------------------------------------------
953 +Most people already knows what it is.
954 +It is a filesystem which unifies several directories and provides a
955 +merged single directory. When users access a file, the access will be
956 +passed/re-directed/converted (sorry, I am not sure which English word is
957 +correct) to the real file on the member filesystem. The member
958 +filesystem is called 'lower filesystem' or 'branch' and has a mode
959 +'readonly' and 'readwrite.' And the deletion for a file on the lower
960 +readonly branch is handled by creating 'whiteout' on the upper writable
961 +branch.
962 +
963 +On LKML, there have been discussions about UnionMount (Jan Blunck,
964 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
965 +different approaches to implement the merged-view.
966 +The former tries putting it into VFS, and the latter implements as a
967 +separate filesystem.
968 +(If I misunderstand about these implementations, please let me know and
969 +I shall correct it. Because it is a long time ago when I read their
970 +source files last time).
971 +
972 +UnionMount's approach will be able to small, but may be hard to share
973 +branches between several UnionMount since the whiteout in it is
974 +implemented in the inode on branch filesystem and always
975 +shared. According to Bharata's post, readdir does not seems to be
976 +finished yet.
977 +There are several missing features known in this implementations such as
978 +- for users, the inode number may change silently. eg. copy-up.
979 +- link(2) may break by copy-up.
980 +- read(2) may get an obsoleted filedata (fstat(2) too).
981 +- fcntl(F_SETLK) may be broken by copy-up.
982 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
983 +  open(O_RDWR).
984 +
985 +Unionfs has a longer history. When I started implementing a stacking filesystem
986 +(Aug 2005), it already existed. It has virtual super_block, inode,
987 +dentry and file objects and they have an array pointing lower same kind
988 +objects. After contributing many patches for Unionfs, I re-started my
989 +project AUFS (Jun 2006).
990 +
991 +In AUFS, the structure of filesystem resembles to Unionfs, but I
992 +implemented my own ideas, approaches and enhancements and it became
993 +totally different one.
994 +
995 +Comparing DM snapshot and fs based implementation
996 +- the number of bytes to be copied between devices is much smaller.
997 +- the type of filesystem must be one and only.
998 +- the fs must be writable, no readonly fs, even for the lower original
999 +  device. so the compression fs will not be usable. but if we use
1000 +  loopback mount, we may address this issue.
1001 +  for instance,
1002 +       mount /cdrom/squashfs.img /sq
1003 +       losetup /sq/ext2.img
1004 +       losetup /somewhere/cow
1005 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1006 +- it will be difficult (or needs more operations) to extract the
1007 +  difference between the original device and COW.
1008 +- DM snapshot-merge may help a lot when users try merging. in the
1009 +  fs-layer union, users will use rsync(1).
1010 +
1011 +
1012 +Several characters/aspects of aufs
1013 +----------------------------------------------------------------------
1014 +
1015 +Aufs has several characters or aspects.
1016 +1. a filesystem, callee of VFS helper
1017 +2. sub-VFS, caller of VFS helper for branches
1018 +3. a virtual filesystem which maintains persistent inode number
1019 +4. reader/writer of files on branches such like an application
1020 +
1021 +1. Callee of VFS Helper
1022 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1023 +unlink(2) from an application reaches sys_unlink() kernel function and
1024 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1025 +calls filesystem specific unlink operation. Actually aufs implements the
1026 +unlink operation but it behaves like a redirector.
1027 +
1028 +2. Caller of VFS Helper for Branches
1029 +aufs_unlink() passes the unlink request to the branch filesystem as if
1030 +it were called from VFS. So the called unlink operation of the branch
1031 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1032 +every necessary pre/post operation for the branch filesystem.
1033 +- acquire the lock for the parent dir on a branch
1034 +- lookup in a branch
1035 +- revalidate dentry on a branch
1036 +- mnt_want_write() for a branch
1037 +- vfs_unlink() for a branch
1038 +- mnt_drop_write() for a branch
1039 +- release the lock on a branch
1040 +
1041 +3. Persistent Inode Number
1042 +One of the most important issue for a filesystem is to maintain inode
1043 +numbers. This is particularly important to support exporting a
1044 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1045 +backend block device for its own. But some storage is necessary to
1046 +maintain inode number. It may be a large space and may not suit to keep
1047 +in memory. Aufs rents some space from its first writable branch
1048 +filesystem (by default) and creates file(s) on it. These files are
1049 +created by aufs internally and removed soon (currently) keeping opened.
1050 +Note: Because these files are removed, they are totally gone after
1051 +      unmounting aufs. It means the inode numbers are not persistent
1052 +      across unmount or reboot. I have a plan to make them really
1053 +      persistent which will be important for aufs on NFS server.
1054 +
1055 +4. Read/Write Files Internally (copy-on-write)
1056 +Because a branch can be readonly, when you write a file on it, aufs will
1057 +"copy-up" it to the upper writable branch internally. And then write the
1058 +originally requested thing to the file. Generally kernel doesn't
1059 +open/read/write file actively. In aufs, even a single write may cause a
1060 +internal "file copy". This behaviour is very similar to cp(1) command.
1061 +
1062 +Some people may think it is better to pass such work to user space
1063 +helper, instead of doing in kernel space. Actually I am still thinking
1064 +about it. But currently I have implemented it in kernel space.
1065 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1066 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1067 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2014-06-24 00:14:06.772718759 +0200
1068 @@ -0,0 +1,242 @@
1069 +
1070 +# Copyright (C) 2005-2014 Junjiro R. Okajima
1071 +# 
1072 +# This program is free software; you can redistribute it and/or modify
1073 +# it under the terms of the GNU General Public License as published by
1074 +# the Free Software Foundation; either version 2 of the License, or
1075 +# (at your option) any later version.
1076 +# 
1077 +# This program is distributed in the hope that it will be useful,
1078 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1079 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1080 +# GNU General Public License for more details.
1081 +# 
1082 +# You should have received a copy of the GNU General Public License
1083 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1084 +
1085 +Basic Aufs Internal Structure
1086 +
1087 +Superblock/Inode/Dentry/File Objects
1088 +----------------------------------------------------------------------
1089 +As like an ordinary filesystem, aufs has its own
1090 +superblock/inode/dentry/file objects. All these objects have a
1091 +dynamically allocated array and store the same kind of pointers to the
1092 +lower filesystem, branch.
1093 +For example, when you build a union with one readwrite branch and one
1094 +readonly, mounted /au, /rw and /ro respectively.
1095 +- /au = /rw + /ro
1096 +- /ro/fileA exists but /rw/fileA
1097 +
1098 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1099 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1100 +- [0] = NULL
1101 +- [1] = /ro/fileA
1102 +
1103 +This style of an array is essentially same to the aufs
1104 +superblock/inode/dentry/file objects.
1105 +
1106 +Because aufs supports manipulating branches, ie. add/delete/change
1107 +dynamically, these objects has its own generation. When branches are
1108 +changed, the generation in aufs superblock is incremented. And a
1109 +generation in other object are compared when it is accessed.
1110 +When a generation in other objects are obsoleted, aufs refreshes the
1111 +internal array.
1112 +
1113 +
1114 +Superblock
1115 +----------------------------------------------------------------------
1116 +Additionally aufs superblock has some data for policies to select one
1117 +among multiple writable branches, XIB files, pseudo-links and kobject.
1118 +See below in detail.
1119 +About the policies which supports copy-down a directory, see policy.txt
1120 +too.
1121 +
1122 +
1123 +Branch and XINO(External Inode Number Translation Table)
1124 +----------------------------------------------------------------------
1125 +Every branch has its own xino (external inode number translation table)
1126 +file. The xino file is created and unlinked by aufs internally. When two
1127 +members of a union exist on the same filesystem, they share the single
1128 +xino file.
1129 +The struct of a xino file is simple, just a sequence of aufs inode
1130 +numbers which is indexed by the lower inode number.
1131 +In the above sample, assume the inode number of /ro/fileA is i111 and
1132 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1133 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1134 +
1135 +When the inode numbers are not contiguous, the xino file will be sparse
1136 +which has a hole in it and doesn't consume as much disk space as it
1137 +might appear. If your branch filesystem consumes disk space for such
1138 +holes, then you should specify 'xino=' option at mounting aufs.
1139 +
1140 +Also a writable branch has three kinds of "whiteout bases". All these
1141 +are existed when the branch is joined to aufs and the names are
1142 +whiteout-ed doubly, so that users will never see their names in aufs
1143 +hierarchy.
1144 +1. a regular file which will be linked to all whiteouts.
1145 +2. a directory to store a pseudo-link.
1146 +3. a directory to store an "orphan-ed" file temporary.
1147 +
1148 +1. Whiteout Base
1149 +   When you remove a file on a readonly branch, aufs handles it as a
1150 +   logical deletion and creates a whiteout on the upper writable branch
1151 +   as a hardlink of this file in order not to consume inode on the
1152 +   writable branch.
1153 +2. Pseudo-link Dir
1154 +   See below, Pseudo-link.
1155 +3. Step-Parent Dir
1156 +   When "fileC" exists on the lower readonly branch only and it is
1157 +   opened and removed with its parent dir, and then user writes
1158 +   something into it, then aufs copies-up fileC to this
1159 +   directory. Because there is no other dir to store fileC. After
1160 +   creating a file under this dir, the file is unlinked.
1161 +
1162 +Because aufs supports manipulating branches, ie. add/delete/change
1163 +dynamically, a branch has its own id. When the branch order changes, aufs
1164 +finds the new index by searching the branch id.
1165 +
1166 +
1167 +Pseudo-link
1168 +----------------------------------------------------------------------
1169 +Assume "fileA" exists on the lower readonly branch only and it is
1170 +hardlinked to "fileB" on the branch. When you write something to fileA,
1171 +aufs copies-up it to the upper writable branch. Additionally aufs
1172 +creates a hardlink under the Pseudo-link Directory of the writable
1173 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1174 +simple list. If fileB is read after unlinking fileA, aufs returns
1175 +filedata from the pseudo-link instead of the lower readonly
1176 +branch. Because the pseudo-link is based upon the inode, to keep the
1177 +inode number by xino (see above) is important.
1178 +
1179 +All the hardlinks under the Pseudo-link Directory of the writable branch
1180 +should be restored in a proper location later. Aufs provides a utility
1181 +to do this. The userspace helpers executed at remounting and unmounting
1182 +aufs by default.
1183 +During this utility is running, it puts aufs into the pseudo-link
1184 +maintenance mode. In this mode, only the process which began the
1185 +maintenance mode (and its child processes) is allowed to operate in
1186 +aufs. Some other processes which are not related to the pseudo-link will
1187 +be allowed to run too, but the rest have to return an error or wait
1188 +until the maintenance mode ends. If a process already acquires an inode
1189 +mutex (in VFS), it has to return an error.
1190 +
1191 +
1192 +XIB(external inode number bitmap)
1193 +----------------------------------------------------------------------
1194 +Addition to the xino file per a branch, aufs has an external inode number
1195 +bitmap in a superblock object. It is also a file such like a xino file.
1196 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1197 +not.
1198 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1199 +
1200 +Aufs implements a feature to truncate/refresh both of xino and xib to
1201 +reduce the number of consumed disk blocks for these files.
1202 +
1203 +
1204 +Virtual or Vertical Dir, and Readdir in Userspace
1205 +----------------------------------------------------------------------
1206 +In order to support multiple layers (branches), aufs readdir operation
1207 +constructs a virtual dir block on memory. For readdir, aufs calls
1208 +vfs_readdir() internally for each dir on branches, merges their entries
1209 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1210 +object. So the file object has its entry list until it is closed. The
1211 +entry list will be updated when the file position is zero and becomes
1212 +old. This decision is made in aufs automatically.
1213 +
1214 +The dynamically allocated memory block for the name of entries has a
1215 +unit of 512 bytes (by default) and stores the names contiguously (no
1216 +padding). Another block for each entry is handled by kmem_cache too.
1217 +During building dir blocks, aufs creates hash list and judging whether
1218 +the entry is whiteouted by its upper branch or already listed.
1219 +The merged result is cached in the corresponding inode object and
1220 +maintained by a customizable life-time option.
1221 +
1222 +Some people may call it can be a security hole or invite DoS attack
1223 +since the opened and once readdir-ed dir (file object) holds its entry
1224 +list and becomes a pressure for system memory. But I'd say it is similar
1225 +to files under /proc or /sys. The virtual files in them also holds a
1226 +memory page (generally) while they are opened. When an idea to reduce
1227 +memory for them is introduced, it will be applied to aufs too.
1228 +For those who really hate this situation, I've developed readdir(3)
1229 +library which operates this merging in userspace. You just need to set
1230 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1231 +kernel space for readdir(3).
1232 +
1233 +
1234 +Workqueue
1235 +----------------------------------------------------------------------
1236 +Aufs sometimes requires privilege access to a branch. For instance,
1237 +in copy-up/down operation. When a user process is going to make changes
1238 +to a file which exists in the lower readonly branch only, and the mode
1239 +of one of ancestor directories may not be writable by a user
1240 +process. Here aufs copy-up the file with its ancestors and they may
1241 +require privilege to set its owner/group/mode/etc.
1242 +This is a typical case of a application character of aufs (see
1243 +Introduction).
1244 +
1245 +Aufs uses workqueue synchronously for this case. It creates its own
1246 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1247 +passes the request to call mkdir or write (for example), and wait for
1248 +its completion. This approach solves a problem of a signal handler
1249 +simply.
1250 +If aufs didn't adopt the workqueue and changed the privilege of the
1251 +process, and if the mkdir/write call arises SIGXFSZ or other signal,
1252 +then the user process might gain a privilege or the generated core file
1253 +was owned by a superuser.
1254 +
1255 +Also aufs uses the system global workqueue ("events" kernel thread) too
1256 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1257 +whiteout base and etc. This is unrelated to a privilege.
1258 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1259 +superblock at the beginning, at the same time waits for the completion
1260 +of all queued asynchronous tasks.
1261 +
1262 +
1263 +Whiteout
1264 +----------------------------------------------------------------------
1265 +The whiteout in aufs is very similar to Unionfs's. That is represented
1266 +by its filename. UnionMount takes an approach of a file mode, but I am
1267 +afraid several utilities (find(1) or something) will have to support it.
1268 +
1269 +Basically the whiteout represents "logical deletion" which stops aufs to
1270 +lookup further, but also it represents "dir is opaque" which also stop
1271 +lookup.
1272 +
1273 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1274 +In order to make several functions in a single systemcall to be
1275 +revertible, aufs adopts an approach to rename a directory to a temporary
1276 +unique whiteouted name.
1277 +For example, in rename(2) dir where the target dir already existed, aufs
1278 +renames the target dir to a temporary unique whiteouted name before the
1279 +actual rename on a branch and then handles other actions (make it opaque,
1280 +update the attributes, etc). If an error happens in these actions, aufs
1281 +simply renames the whiteouted name back and returns an error. If all are
1282 +succeeded, aufs registers a function to remove the whiteouted unique
1283 +temporary name completely and asynchronously to the system global
1284 +workqueue.
1285 +
1286 +
1287 +Copy-up
1288 +----------------------------------------------------------------------
1289 +It is a well-known feature or concept.
1290 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1291 +internally and makes change to the new file on the upper writable branch.
1292 +When the trigger systemcall does not update the timestamps of the parent
1293 +dir, aufs reverts it after copy-up.
1294 +
1295 +
1296 +Move-down (aufs3.9 and later)
1297 +----------------------------------------------------------------------
1298 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1299 +the lower readonly branch to the upper writable branch when a user
1300 +changes something about the file.
1301 +"Move-down" is an opposite action of copy-up. Basically this action is
1302 +ran manually instead of automatically and internally.
1303 +
1304 +Sometimes users want to move-down a file from the upper writable branch
1305 +to the lower readonly or writable branch. For instance,
1306 +- the free space of the upper writable branch is going to run out.
1307 +- create a new intermediate branch between the upper and lower branch.
1308 +- etc.
1309 +
1310 +For this purpose, use "aumvdown" command in aufs-util.git.
1311 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1312 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1313 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2014-06-24 00:14:06.772718759 +0200
1314 @@ -0,0 +1,105 @@
1315 +
1316 +# Copyright (C) 2005-2014 Junjiro R. Okajima
1317 +# 
1318 +# This program is free software; you can redistribute it and/or modify
1319 +# it under the terms of the GNU General Public License as published by
1320 +# the Free Software Foundation; either version 2 of the License, or
1321 +# (at your option) any later version.
1322 +# 
1323 +# This program is distributed in the hope that it will be useful,
1324 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1325 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1326 +# GNU General Public License for more details.
1327 +# 
1328 +# You should have received a copy of the GNU General Public License
1329 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1330 +
1331 +Lookup in a Branch
1332 +----------------------------------------------------------------------
1333 +Since aufs has a character of sub-VFS (see Introduction), it operates
1334 +lookup for branches as VFS does. It may be a heavy work. Generally
1335 +speaking struct nameidata is a bigger structure and includes many
1336 +information. But almost all lookup operation in aufs is the simplest
1337 +case, ie. lookup only an entry directly connected to its parent. Digging
1338 +down the directory hierarchy is unnecessary.
1339 +
1340 +VFS has a function lookup_one_len() for that use, but it is not usable
1341 +for a branch filesystem which requires struct nameidata. So aufs
1342 +implements a simple lookup wrapper function. When a branch filesystem
1343 +allows NULL as nameidata, it calls lookup_one_len(). Otherwise it builds
1344 +a simplest nameidata and calls lookup_hash().
1345 +Here aufs applies "a principle in NFSD", ie. if the filesystem supports
1346 +NFS-export, then it has to support NULL as a nameidata parameter for
1347 +->create(), ->lookup() and ->d_revalidate(). So the lookup wrapper in
1348 +aufs tests if ->s_export_op in the branch is NULL or not.
1349 +
1350 +When a branch is a remote filesystem, aufs basically trusts its
1351 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1352 +them.
1353 +For d_revalidate, aufs implements three levels of revalidate tests. See
1354 +"Revalidate Dentry and UDBA" in detail.
1355 +
1356 +
1357 +Loopback Mount
1358 +----------------------------------------------------------------------
1359 +Basically aufs supports any type of filesystem and block device for a
1360 +branch (actually there are some exceptions). But it is prohibited to add
1361 +a loopback mounted one whose backend file exists in a filesystem which is
1362 +already added to aufs. The reason is to protect aufs from a recursive
1363 +lookup. If it was allowed, the aufs lookup operation might re-enter a
1364 +lookup for the loopback mounted branch in the same context, and will
1365 +cause a deadlock.
1366 +
1367 +
1368 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1369 +----------------------------------------------------------------------
1370 +Generally VFS helpers re-validate a dentry as a part of lookup.
1371 +0. digging down the directory hierarchy.
1372 +1. lock the parent dir by its i_mutex.
1373 +2. lookup the final (child) entry.
1374 +3. revalidate it.
1375 +4. call the actual operation (create, unlink, etc.)
1376 +5. unlock the parent dir
1377 +
1378 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1379 +called. Actually aufs implements it and checks the dentry on a branch is
1380 +still valid.
1381 +But it is not enough. Because aufs has to release the lock for the
1382 +parent dir on a branch at the end of ->lookup() (step 2) and
1383 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1384 +held by VFS.
1385 +If the file on a branch is changed directly, eg. bypassing aufs, after
1386 +aufs released the lock, then the subsequent operation may cause
1387 +something unpleasant result.
1388 +
1389 +This situation is a result of VFS architecture, ->lookup() and
1390 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1391 +design from VFS's point of view. It is just not suitable for sub-VFS
1392 +character in aufs.
1393 +
1394 +Aufs supports such case by three level of revalidation which is
1395 +selectable by user.
1396 +1. Simple Revalidate
1397 +   Addition to the native flow in VFS's, confirm the child-parent
1398 +   relationship on the branch just after locking the parent dir on the
1399 +   branch in the "actual operation" (step 4). When this validation
1400 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1401 +   checks the validation of the dentry on branches.
1402 +2. Monitor Changes Internally by Inotify/Fsnotify
1403 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1404 +   the dentry on the branch, and returns EBUSY if it finds different
1405 +   dentry.
1406 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1407 +   during it is in cache. When the event is notified, aufs registers a
1408 +   function to kernel 'events' thread by schedule_work(). And the
1409 +   function sets some special status to the cached aufs dentry and inode
1410 +   private data. If they are not cached, then aufs has nothing to
1411 +   do. When the same file is accessed through aufs (step 0-3) later,
1412 +   aufs will detect the status and refresh all necessary data.
1413 +   In this mode, aufs has to ignore the event which is fired by aufs
1414 +   itself.
1415 +3. No Extra Validation
1416 +   This is the simplest test and doesn't add any additional revalidation
1417 +   test, and skip therevalidatin in step 4. It is useful and improves
1418 +   aufs performance when system surely hide the aufs branches from user,
1419 +   by over-mounting something (or another method).
1420 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1421 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1422 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2014-06-24 00:14:06.772718759 +0200
1423 @@ -0,0 +1,75 @@
1424 +
1425 +# Copyright (C) 2005-2014 Junjiro R. Okajima
1426 +# 
1427 +# This program is free software; you can redistribute it and/or modify
1428 +# it under the terms of the GNU General Public License as published by
1429 +# the Free Software Foundation; either version 2 of the License, or
1430 +# (at your option) any later version.
1431 +# 
1432 +# This program is distributed in the hope that it will be useful,
1433 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1434 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1435 +# GNU General Public License for more details.
1436 +# 
1437 +# You should have received a copy of the GNU General Public License
1438 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1439 +
1440 +Branch Manipulation
1441 +
1442 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1443 +and changing its permission/attribute, there are a lot of works to do.
1444 +
1445 +
1446 +Add a Branch
1447 +----------------------------------------------------------------------
1448 +o Confirm the adding dir exists outside of aufs, including loopback
1449 +  mount.
1450 +- and other various attributes...
1451 +o Initialize the xino file and whiteout bases if necessary.
1452 +  See struct.txt.
1453 +
1454 +o Check the owner/group/mode of the directory
1455 +  When the owner/group/mode of the adding directory differs from the
1456 +  existing branch, aufs issues a warning because it may impose a
1457 +  security risk.
1458 +  For example, when a upper writable branch has a world writable empty
1459 +  top directory, a malicious user can create any files on the writable
1460 +  branch directly, like copy-up and modify manually. If something like
1461 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1462 +  writable branch, and the writable branch is world-writable, then a
1463 +  malicious guy may create /etc/passwd on the writable branch directly
1464 +  and the infected file will be valid in aufs.
1465 +  I am afraid it can be a security issue, but nothing to do except
1466 +  producing a warning.
1467 +
1468 +
1469 +Delete a Branch
1470 +----------------------------------------------------------------------
1471 +o Confirm the deleting branch is not busy
1472 +  To be general, there is one merit to adopt "remount" interface to
1473 +  manipulate branches. It is to discard caches. At deleting a branch,
1474 +  aufs checks the still cached (and connected) dentries and inodes. If
1475 +  there are any, then they are all in-use. An inode without its
1476 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1477 +
1478 +  For the cached one, aufs checks whether the same named entry exists on
1479 +  other branches.
1480 +  If the cached one is a directory, because aufs provides a merged view
1481 +  to users, as long as one dir is left on any branch aufs can show the
1482 +  dir to users. In this case, the branch can be removed from aufs.
1483 +  Otherwise aufs rejects deleting the branch.
1484 +
1485 +  If any file on the deleting branch is opened by aufs, then aufs
1486 +  rejects deleting.
1487 +
1488 +
1489 +Modify the Permission of a Branch
1490 +----------------------------------------------------------------------
1491 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1492 +  See struct.txt.
1493 +
1494 +o rw --> ro: Confirm the modifying branch is not busy
1495 +  Aufs rejects the request if any of these conditions are true.
1496 +  - a file on the branch is mmap-ed.
1497 +  - a regular file on the branch is opened for write and there is no
1498 +    same named entry on the upper branch.
1499 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1500 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1501 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2014-06-24 00:14:06.772718759 +0200
1502 @@ -0,0 +1,64 @@
1503 +
1504 +# Copyright (C) 2005-2014 Junjiro R. Okajima
1505 +# 
1506 +# This program is free software; you can redistribute it and/or modify
1507 +# it under the terms of the GNU General Public License as published by
1508 +# the Free Software Foundation; either version 2 of the License, or
1509 +# (at your option) any later version.
1510 +# 
1511 +# This program is distributed in the hope that it will be useful,
1512 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1513 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1514 +# GNU General Public License for more details.
1515 +# 
1516 +# You should have received a copy of the GNU General Public License
1517 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1518 +
1519 +Policies to Select One among Multiple Writable Branches
1520 +----------------------------------------------------------------------
1521 +When the number of writable branch is more than one, aufs has to decide
1522 +the target branch for file creation or copy-up. By default, the highest
1523 +writable branch which has the parent (or ancestor) dir of the target
1524 +file is chosen (top-down-parent policy).
1525 +By user's request, aufs implements some other policies to select the
1526 +writable branch, for file creation two policies, round-robin and
1527 +most-free-space policies. For copy-up three policies, top-down-parent,
1528 +bottom-up-parent and bottom-up policies.
1529 +
1530 +As expected, the round-robin policy selects the branch in circular. When
1531 +you have two writable branches and creates 10 new files, 5 files will be
1532 +created for each branch. mkdir(2) systemcall is an exception. When you
1533 +create 10 new directories, all will be created on the same branch.
1534 +And the most-free-space policy selects the one which has most free
1535 +space among the writable branches. The amount of free space will be
1536 +checked by aufs internally, and users can specify its time interval.
1537 +
1538 +The policies for copy-up is more simple,
1539 +top-down-parent is equivalent to the same named on in create policy,
1540 +bottom-up-parent selects the writable branch where the parent dir
1541 +exists and the nearest upper one from the copyup-source,
1542 +bottom-up selects the nearest upper writable branch from the
1543 +copyup-source, regardless the existence of the parent dir.
1544 +
1545 +There are some rules or exceptions to apply these policies.
1546 +- If there is a readonly branch above the policy-selected branch and
1547 +  the parent dir is marked as opaque (a variation of whiteout), or the
1548 +  target (creating) file is whiteout-ed on the upper readonly branch,
1549 +  then the result of the policy is ignored and the target file will be
1550 +  created on the nearest upper writable branch than the readonly branch.
1551 +- If there is a writable branch above the policy-selected branch and
1552 +  the parent dir is marked as opaque or the target file is whiteouted
1553 +  on the branch, then the result of the policy is ignored and the target
1554 +  file will be created on the highest one among the upper writable
1555 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1556 +  it as usual.
1557 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1558 +  They try selecting the branch where the source exists as possible
1559 +  since copyup a large file will take long time. If it can't be,
1560 +  ie. the branch where the source exists is readonly, then they will
1561 +  follow the copyup policy.
1562 +- There is an exception for rename(2) when the target exists.
1563 +  If the rename target exists, aufs compares the index of the branches
1564 +  where the source and the target exists and selects the higher
1565 +  one. If the selected branch is readonly, then aufs follows the
1566 +  copyup policy.
1567 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
1568 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
1569 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2014-06-24 00:14:06.772718759 +0200
1570 @@ -0,0 +1,46 @@
1571 +
1572 +# Copyright (C) 2005-2014 Junjiro R. Okajima
1573 +# 
1574 +# This program is free software; you can redistribute it and/or modify
1575 +# it under the terms of the GNU General Public License as published by
1576 +# the Free Software Foundation; either version 2 of the License, or
1577 +# (at your option) any later version.
1578 +# 
1579 +# This program is distributed in the hope that it will be useful,
1580 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1581 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1582 +# GNU General Public License for more details.
1583 +# 
1584 +# You should have received a copy of the GNU General Public License
1585 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1586 +
1587 +mmap(2) -- File Memory Mapping
1588 +----------------------------------------------------------------------
1589 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1590 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1591 +->mmap().
1592 +This approach is simple and good, but there is one problem.
1593 +Under /proc, several entries show the mmap-ped files by its path (with
1594 +device and inode number), and the printed path will be the path on the
1595 +branch fs's instead of virtual aufs's.
1596 +This is not a problem in most cases, but some utilities lsof(1) (and its
1597 +user) may expect the path on aufs.
1598 +
1599 +To address this issue, aufs adds a new member called vm_prfile in struct
1600 +vm_area_struct (and struct vm_region). The original vm_file points to
1601 +the file on the branch fs in order to handle everything correctly as
1602 +usual. The new vm_prfile points to a virtual file in aufs, and the
1603 +show-functions in procfs refers to vm_prfile if it is set.
1604 +Also we need to maintain several other places where touching vm_file
1605 +such like
1606 +- fork()/clone() copies vma and the reference count of vm_file is
1607 +  incremented.
1608 +- merging vma maintains the ref count too.
1609 +
1610 +This is not a good approach. It just faking the printed path. But it
1611 +leaves all behaviour around f_mapping unchanged. This is surely an
1612 +advantage.
1613 +Actually aufs had adopted another complicated approach which calls
1614 +generic_file_mmap() and handles struct vm_operations_struct. In this
1615 +approach, aufs met a hard problem and I could not solve it without
1616 +switching the approach.
1617 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
1618 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
1619 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2014-06-24 00:14:06.772718759 +0200
1620 @@ -0,0 +1,58 @@
1621 +
1622 +# Copyright (C) 2005-2014 Junjiro R. Okajima
1623 +# 
1624 +# This program is free software; you can redistribute it and/or modify
1625 +# it under the terms of the GNU General Public License as published by
1626 +# the Free Software Foundation; either version 2 of the License, or
1627 +# (at your option) any later version.
1628 +# 
1629 +# This program is distributed in the hope that it will be useful,
1630 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1631 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1632 +# GNU General Public License for more details.
1633 +# 
1634 +# You should have received a copy of the GNU General Public License
1635 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1636 +
1637 +Export Aufs via NFS
1638 +----------------------------------------------------------------------
1639 +Here is an approach.
1640 +- like xino/xib, add a new file 'xigen' which stores aufs inode
1641 +  generation.
1642 +- iget_locked(): initialize aufs inode generation for a new inode, and
1643 +  store it in xigen file.
1644 +- destroy_inode(): increment aufs inode generation and store it in xigen
1645 +  file. it is necessary even if it is not unlinked, because any data of
1646 +  inode may be changed by UDBA.
1647 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
1648 +  build file handle by
1649 +  + branch id (4 bytes)
1650 +  + superblock generation (4 bytes)
1651 +  + inode number (4 or 8 bytes)
1652 +  + parent dir inode number (4 or 8 bytes)
1653 +  + inode generation (4 bytes))
1654 +  + return value of exportfs_encode_fh() for the parent on a branch (4
1655 +    bytes)
1656 +  + file handle for a branch (by exportfs_encode_fh())
1657 +- fh_to_dentry():
1658 +  + find the index of a branch from its id in handle, and check it is
1659 +    still exist in aufs.
1660 +  + 1st level: get the inode number from handle and search it in cache.
1661 +  + 2nd level: if not found, get the parent inode number from handle and
1662 +    search it in cache. and then open the parent dir, find the matching
1663 +    inode number by vfs_readdir() and get its name, and call
1664 +    lookup_one_len() for the target dentry.
1665 +  + 3rd level: if the parent dir is not cached, call
1666 +    exportfs_decode_fh() for a branch and get the parent on a branch,
1667 +    build a pathname of it, convert it a pathname in aufs, call
1668 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
1669 +    the 2nd level.
1670 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
1671 +    for every branch, but not itself. to get this, (currently) aufs
1672 +    searches in current->nsproxy->mnt_ns list. it may not be a good
1673 +    idea, but I didn't get other approach.
1674 +  + test the generation of the gotten inode.
1675 +- every inode operation: they may get EBUSY due to UDBA. in this case,
1676 +  convert it into ESTALE for NFSD.
1677 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
1678 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
1679 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
1680 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
1681 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2014-06-24 00:14:06.772718759 +0200
1682 @@ -0,0 +1,52 @@
1683 +
1684 +# Copyright (C) 2005-2014 Junjiro R. Okajima
1685 +# 
1686 +# This program is free software; you can redistribute it and/or modify
1687 +# it under the terms of the GNU General Public License as published by
1688 +# the Free Software Foundation; either version 2 of the License, or
1689 +# (at your option) any later version.
1690 +# 
1691 +# This program is distributed in the hope that it will be useful,
1692 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1693 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1694 +# GNU General Public License for more details.
1695 +# 
1696 +# You should have received a copy of the GNU General Public License
1697 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1698 +
1699 +Show Whiteout Mode (shwh)
1700 +----------------------------------------------------------------------
1701 +Generally aufs hides the name of whiteouts. But in some cases, to show
1702 +them is very useful for users. For instance, creating a new middle layer
1703 +(branch) by merging existing layers.
1704 +
1705 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
1706 +When you have three branches,
1707 +- Bottom: 'system', squashfs (underlying base system), read-only
1708 +- Middle: 'mods', squashfs, read-only
1709 +- Top: 'overlay', ram (tmpfs), read-write
1710 +
1711 +The top layer is loaded at boot time and saved at shutdown, to preserve
1712 +the changes made to the system during the session.
1713 +When larger changes have been made, or smaller changes have accumulated,
1714 +the size of the saved top layer data grows. At this point, it would be
1715 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
1716 +and rewrite the 'mods' squashfs, clearing the top layer and thus
1717 +restoring save and load speed.
1718 +
1719 +This merging is simplified by the use of another aufs mount, of just the
1720 +two overlay branches using the 'shwh' option.
1721 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
1722 +       aufs /livesys/merge_union
1723 +
1724 +A merged view of these two branches is then available at
1725 +/livesys/merge_union, and the new feature is that the whiteouts are
1726 +visible!
1727 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
1728 +writing to all branches. Also the default mode for all branches is 'ro'.
1729 +It is now possible to save the combined contents of the two overlay
1730 +branches to a new squashfs, e.g.:
1731 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
1732 +
1733 +This new squashfs archive can be stored on the boot device and the
1734 +initramfs will use it to replace the old one at the next boot.
1735 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
1736 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
1737 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2014-06-24 00:14:06.772718759 +0200
1738 @@ -0,0 +1,46 @@
1739 +
1740 +# Copyright (C) 2010-2014 Junjiro R. Okajima
1741 +#
1742 +# This program is free software; you can redistribute it and/or modify
1743 +# it under the terms of the GNU General Public License as published by
1744 +# the Free Software Foundation; either version 2 of the License, or
1745 +# (at your option) any later version.
1746 +#
1747 +# This program is distributed in the hope that it will be useful,
1748 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1749 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1750 +# GNU General Public License for more details.
1751 +#
1752 +# You should have received a copy of the GNU General Public License
1753 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1754 +
1755 +Dynamically customizable FS operations
1756 +----------------------------------------------------------------------
1757 +Generally FS operations (struct inode_operations, struct
1758 +address_space_operations, struct file_operations, etc.) are defined as
1759 +"static const", but it never means that FS have only one set of
1760 +operation. Some FS have multiple sets of them. For instance, ext2 has
1761 +three sets, one for XIP, for NOBH, and for normal.
1762 +Since aufs overrides and redirects these operations, sometimes aufs has
1763 +to change its behaviour according to the branch FS type. More imporantly
1764 +VFS acts differently if a function (member in the struct) is set or
1765 +not. It means aufs should have several sets of operations and select one
1766 +among them according to the branch FS definition.
1767 +
1768 +In order to solve this problem and not to affect the behavour of VFS,
1769 +aufs defines these operations dynamically. For instance, aufs defines
1770 +aio_read function for struct file_operations, but it may not be set to
1771 +the file_operations. When the branch FS doesn't have it, aufs doesn't
1772 +set it to its file_operations while the function definition itself is
1773 +still alive. So the behaviour of io_submit(2) will not change, and it
1774 +will return an error when aio_read is not defined.
1775 +
1776 +The lifetime of these dynamically generated operation object is
1777 +maintained by aufs branch object. When the branch is removed from aufs,
1778 +the reference counter of the object is decremented. When it reaches
1779 +zero, the dynamically generated operation object will be freed.
1780 +
1781 +This approach is designed to support AIO (io_submit), Direcit I/O and
1782 +XIP mainly.
1783 +Currently this approach is applied to file_operations and
1784 +vm_operations_struct for regular files only.
1785 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt linux/Documentation/filesystems/aufs/design/99plan.txt
1786 --- /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt   1970-01-01 01:00:00.000000000 +0100
1787 +++ linux/Documentation/filesystems/aufs/design/99plan.txt      2014-06-24 00:14:06.772718759 +0200
1788 @@ -0,0 +1,95 @@
1789 +
1790 +# Copyright (C) 2005-2014 Junjiro R. Okajima
1791 +# 
1792 +# This program is free software; you can redistribute it and/or modify
1793 +# it under the terms of the GNU General Public License as published by
1794 +# the Free Software Foundation; either version 2 of the License, or
1795 +# (at your option) any later version.
1796 +# 
1797 +# This program is distributed in the hope that it will be useful,
1798 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1799 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1800 +# GNU General Public License for more details.
1801 +# 
1802 +# You should have received a copy of the GNU General Public License
1803 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1804 +
1805 +Plan
1806 +
1807 +Restoring some features which was implemented in aufs1.
1808 +They were dropped in aufs2 in order to make source files simpler and
1809 +easier to be reviewed.
1810 +
1811 +
1812 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1813 +----------------------------------------------------------------------
1814 +Let's try case study.
1815 +- aufs has two branches, upper readwrite and lower readonly.
1816 +  /au = /rw + /ro
1817 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1818 +- user invoked "chmod a+rx /au/dirA"
1819 +- then "dirA" becomes world readable?
1820 +
1821 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1822 +or it may be a natively readonly filesystem. If aufs respects the lower
1823 +branch, it should not respond readdir request from other users. But user
1824 +allowed it by chmod. Should really aufs rejects showing the entries
1825 +under /ro/dirA?
1826 +
1827 +To be honest, I don't have a best solution for this case. So I
1828 +implemented 'dirperm1' and 'nodirperm1' option in aufs1, and leave it to
1829 +users.
1830 +When dirperm1 is specified, aufs checks only the highest one for the
1831 +directory permission, and shows the entries. Otherwise, as usual, checks
1832 +every dir existing on all branches and rejects the request.
1833 +
1834 +As a side effect, dirperm1 option improves the performance of aufs
1835 +because the number of permission check is reduced.
1836 +
1837 +
1838 +Being Another Aufs's Readonly Branch (robr)
1839 +----------------------------------------------------------------------
1840 +Aufs1 allows aufs to be another aufs's readonly branch.
1841 +This feature was developed by a user's request. But it may not be used
1842 +currecnly.
1843 +
1844 +
1845 +Copy-up on Open (coo=)
1846 +----------------------------------------------------------------------
1847 +By default the internal copy-up is executed when it is really necessary.
1848 +It is not done when a file is opened for writing, but when write(2) is
1849 +done. Users who have many (over 100) branches want to know and analyse
1850 +when and what file is copied-up. To insert a new upper branch which
1851 +contains such files only may improve the performance of aufs.
1852 +
1853 +Aufs1 implemented "coo=none | leaf | all" option.
1854 +
1855 +
1856 +Refresh the Opened File (refrof)
1857 +----------------------------------------------------------------------
1858 +This option is implemented in aufs1 but incomplete.
1859 +
1860 +When user reads from a file, he expects to get its latest filedata
1861 +generally. If the file is removed and a new same named file is created,
1862 +the content he gets is unchanged, ie. the unlinked filedata.
1863 +
1864 +Let's try case study again.
1865 +- aufs has two branches.
1866 +  /au = /rw + /ro
1867 +- "fileA" exists under /ro, but /rw.
1868 +- user opened "/au/fileA".
1869 +- he or someone else inserts a branch (/new) between /rw and /ro.
1870 +  /au = /rw + /new + /ro
1871 +- the new branch has "fileA".
1872 +- user reads from the opened "fileA"
1873 +- which filedata should aufs return, from /ro or /new?
1874 +
1875 +Some people says it has to be "from /ro" and it is a semantics of Unix.
1876 +The others say it should be "from /new" because the file is not removed
1877 +and it is equivalent to the case of someone else modifies the file.
1878 +
1879 +Here again I don't have a best and final answer. I got an idea to
1880 +implement 'refrof' and 'norefrof' option. When 'refrof' (REFResh the
1881 +Opened File) is specified (by default), aufs returns the filedata from
1882 +/new.
1883 +Otherwise from /new.
1884 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
1885 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
1886 +++ linux/Documentation/filesystems/aufs/README 2014-06-24 00:14:06.769385243 +0200
1887 @@ -0,0 +1,368 @@
1888 +
1889 +Aufs3 -- advanced multi layered unification filesystem version 3.x
1890 +http://aufs.sf.net
1891 +Junjiro R. Okajima
1892 +
1893 +
1894 +0. Introduction
1895 +----------------------------------------
1896 +In the early days, aufs was entirely re-designed and re-implemented
1897 +Unionfs Version 1.x series. After many original ideas, approaches,
1898 +improvements and implementations, it becomes totally different from
1899 +Unionfs while keeping the basic features.
1900 +Recently, Unionfs Version 2.x series begin taking some of the same
1901 +approaches to aufs1's.
1902 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
1903 +University and his team.
1904 +
1905 +Aufs3 supports linux-3.0 and later.
1906 +If you want older kernel version support, try aufs2-2.6.git or
1907 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
1908 +
1909 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
1910 +      According to Christoph Hellwig, linux rejects all union-type
1911 +      filesystems but UnionMount.
1912 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
1913 +
1914 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
1915 +    UnionMount, and he pointed out an issue around a directory mutex
1916 +    lock and aufs addressed it. But it is still unsure whether aufs will
1917 +    be merged (or any other union solution).
1918 +<http://lkml.org/lkml/2013/3/12/637>
1919 +
1920 +
1921 +1. Features
1922 +----------------------------------------
1923 +- unite several directories into a single virtual filesystem. The member
1924 +  directory is called as a branch.
1925 +- you can specify the permission flags to the branch, which are 'readonly',
1926 +  'readwrite' and 'whiteout-able.'
1927 +- by upper writable branch, internal copyup and whiteout, files/dirs on
1928 +  readonly branch are modifiable logically.
1929 +- dynamic branch manipulation, add, del.
1930 +- etc...
1931 +
1932 +Also there are many enhancements in aufs1, such as:
1933 +- readdir(3) in userspace.
1934 +- keep inode number by external inode number table
1935 +- keep the timestamps of file/dir in internal copyup operation
1936 +- seekable directory, supporting NFS readdir.
1937 +- whiteout is hardlinked in order to reduce the consumption of inodes
1938 +  on branch
1939 +- do not copyup, nor create a whiteout when it is unnecessary
1940 +- revert a single systemcall when an error occurs in aufs
1941 +- remount interface instead of ioctl
1942 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
1943 +- loopback mounted filesystem as a branch
1944 +- kernel thread for removing the dir who has a plenty of whiteouts
1945 +- support copyup sparse file (a file which has a 'hole' in it)
1946 +- default permission flags for branches
1947 +- selectable permission flags for ro branch, whether whiteout can
1948 +  exist or not
1949 +- export via NFS.
1950 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
1951 +- support multiple writable branches, some policies to select one
1952 +  among multiple writable branches.
1953 +- a new semantics for link(2) and rename(2) to support multiple
1954 +  writable branches.
1955 +- no glibc changes are required.
1956 +- pseudo hardlink (hardlink over branches)
1957 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
1958 +  including NFS or remote filesystem branch.
1959 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
1960 +- and more...
1961 +
1962 +Currently these features are dropped temporary from aufs3.
1963 +See design/08plan.txt in detail.
1964 +- test only the highest one for the directory permission (dirperm1)
1965 +- copyup on open (coo=)
1966 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
1967 +  (robr)
1968 +- statistics of aufs thread (/sys/fs/aufs/stat)
1969 +- delegation mode (dlgt)
1970 +  a delegation of the internal branch access to support task I/O
1971 +  accounting, which also supports Linux Security Modules (LSM) mainly
1972 +  for Suse AppArmor.
1973 +- intent.open/create (file open in a single lookup)
1974 +
1975 +Features or just an idea in the future (see also design/*.txt),
1976 +- reorder the branch index without del/re-add.
1977 +- permanent xino files for NFSD
1978 +- an option for refreshing the opened files after add/del branches
1979 +- 'move' policy for copy-up between two writable branches, after
1980 +  checking free space.
1981 +- light version, without branch manipulation. (unnecessary?)
1982 +- copyup in userspace
1983 +- inotify in userspace
1984 +- readv/writev
1985 +- xattr, acl
1986 +
1987 +
1988 +2. Download
1989 +----------------------------------------
1990 +There were three GIT trees for aufs3, aufs3-linux.git,
1991 +aufs3-standalone.git, and aufs-util.git. Note that there is no "3" in
1992 +"aufs-util.git."
1993 +While the aufs-util is always necessary, you need either of aufs3-linux
1994 +or aufs3-standalone.
1995 +
1996 +The aufs3-linux tree includes the whole linux mainline GIT tree,
1997 +git://git.kernel.org/.../torvalds/linux.git.
1998 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
1999 +build aufs3 as an external kernel module.
2000 +
2001 +On the other hand, the aufs3-standalone tree has only aufs source files
2002 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2003 +
2004 +You will find GIT branches whose name is in form of "aufs3.x" where "x"
2005 +represents the linux kernel version, "linux-3.x". For instance,
2006 +"aufs3.0" is for linux-3.0. For latest "linux-3.x-rcN", use
2007 +"aufs3.x-rcN" branch.
2008 +
2009 +o aufs3-linux tree
2010 +$ git clone --reference /your/linux/git/tree \
2011 +       git://git.code.sf.net/p/aufs/aufs3-linux aufs-aufs3-linux \
2012 +       aufs3-linux.git
2013 +- if you don't have linux GIT tree, then remove "--reference ..."
2014 +$ cd aufs3-linux.git
2015 +$ git checkout origin/aufs3.0
2016 +
2017 +o aufs3-standalone tree
2018 +$ git clone git://git.code.sf.net/p/aufs/aufs3-standalone \
2019 +       aufs3-standalone.git
2020 +$ cd aufs3-standalone.git
2021 +$ git checkout origin/aufs3.0
2022 +
2023 +o aufs-util tree
2024 +$ git clone git://git.code.sf.net/p/aufs/aufs-util \
2025 +       aufs-util.git
2026 +$ cd aufs-util.git
2027 +$ git checkout origin/aufs3.0
2028 +
2029 +Note: The 3.x-rcN branch is to be used with `rc' kernel versions ONLY.
2030 +The minor version number, 'x' in '3.x', of aufs may not always
2031 +follow the minor version number of the kernel.
2032 +Because changes in the kernel that cause the use of a new
2033 +minor version number do not always require changes to aufs-util.
2034 +
2035 +Since aufs-util has its own minor version number, you may not be
2036 +able to find a GIT branch in aufs-util for your kernel's
2037 +exact minor version number.
2038 +In this case, you should git-checkout the branch for the
2039 +nearest lower number.
2040 +
2041 +For (an unreleased) example:
2042 +If you are using "linux-3.10" and the "aufs3.10" branch
2043 +does not exist in aufs-util repository, then "aufs3.9", "aufs3.8"
2044 +or something numerically smaller is the branch for your kernel.
2045 +
2046 +Also you can view all branches by
2047 +       $ git branch -a
2048 +
2049 +
2050 +3. Configuration and Compilation
2051 +----------------------------------------
2052 +Make sure you have git-checkout'ed the correct branch.
2053 +
2054 +For aufs3-linux tree,
2055 +- enable CONFIG_AUFS_FS.
2056 +- set other aufs configurations if necessary.
2057 +
2058 +For aufs3-standalone tree,
2059 +There are several ways to build.
2060 +
2061 +1.
2062 +- apply ./aufs3-kbuild.patch to your kernel source files.
2063 +- apply ./aufs3-base.patch too.
2064 +- apply ./aufs3-mmap.patch too.
2065 +- apply ./aufs3-standalone.patch too, if you have a plan to set
2066 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs3-standalone.patch.
2067 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2068 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2069 +- enable CONFIG_AUFS_FS, you can select either
2070 +  =m or =y.
2071 +- and build your kernel as usual.
2072 +- install the built kernel.
2073 +  Note: Since linux-3.9, every filesystem module requires an alias
2074 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2075 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2076 +- install the header files too by "make headers_install" to the
2077 +  directory where you specify. By default, it is $PWD/usr.
2078 +  "make help" shows a brief note for headers_install.
2079 +- and reboot your system.
2080 +
2081 +2.
2082 +- module only (CONFIG_AUFS_FS=m).
2083 +- apply ./aufs3-base.patch to your kernel source files.
2084 +- apply ./aufs3-mmap.patch too.
2085 +- apply ./aufs3-standalone.patch too.
2086 +- build your kernel, don't forget "make headers_install", and reboot.
2087 +- edit ./config.mk and set other aufs configurations if necessary.
2088 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2089 +  every aufs configurations.
2090 +- build the module by simple "make".
2091 +  Note: Since linux-3.9, every filesystem module requires an alias
2092 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2093 +  modules.aliases file.
2094 +- you can specify ${KDIR} make variable which points to your kernel
2095 +  source tree.
2096 +- install the files
2097 +  + run "make install" to install the aufs module, or copy the built
2098 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2099 +  + run "make install_headers" (instead of headers_install) to install
2100 +    the modified aufs header file (you can specify DESTDIR which is
2101 +    available in aufs standalone version's Makefile only), or copy
2102 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2103 +    you like manually. By default, the target directory is $PWD/usr.
2104 +- no need to apply aufs3-kbuild.patch, nor copying source files to your
2105 +  kernel source tree.
2106 +
2107 +Note: The header file aufs_type.h is necessary to build aufs-util
2108 +      as well as "make headers_install" in the kernel source tree.
2109 +      headers_install is subject to be forgotten, but it is essentially
2110 +      necessary, not only for building aufs-util.
2111 +      You may not meet problems without headers_install in some older
2112 +      version though.
2113 +
2114 +And then,
2115 +- read README in aufs-util, build and install it
2116 +- note that your distribution may contain an obsoleted version of
2117 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2118 +  utilities, make sure that your compiler refers the correct aufs header
2119 +  file which is built by "make headers_install."
2120 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2121 +  then run "make install_ulib" too. And refer to the aufs manual in
2122 +  detail.
2123 +
2124 +There several other patches in aufs3-standalone.git. They are all
2125 +optional. When you meet some problems, they will help you.
2126 +- aufs3-loopback.patch
2127 +  Supports a nested loopback mount in a branch-fs. This patch is
2128 +  unnecessary until aufs produces a message like "you may want to try
2129 +  another patch for loopback file".
2130 +- vfs-ino.patch
2131 +  Modifies a system global kernel internal function get_next_ino() in
2132 +  order to stop assigning 0 for an inode-number. Not directly related to
2133 +  aufs, but recommended generally.
2134 +- tmpfs-idr.patch
2135 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2136 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2137 +  duplication of inode number, which is important for backup tools and
2138 +  other utilities. When you find aufs XINO files for tmpfs branch
2139 +  growing too much, try this patch.
2140 +
2141 +
2142 +4. Usage
2143 +----------------------------------------
2144 +At first, make sure aufs-util are installed, and please read the aufs
2145 +manual, aufs.5 in aufs-util.git tree.
2146 +$ man -l aufs.5
2147 +
2148 +And then,
2149 +$ mkdir /tmp/rw /tmp/aufs
2150 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2151 +
2152 +Here is another example. The result is equivalent.
2153 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2154 +  Or
2155 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2156 +# mount -o remount,append:${HOME} /tmp/aufs
2157 +
2158 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2159 +you modify a file under /tmp/aufs, the one on your home directory is
2160 +not affected, instead the same named file will be newly created under
2161 +/tmp/rw. And all of your modification to a file will be applied to
2162 +the one under /tmp/rw. This is called the file based Copy on Write
2163 +(COW) method.
2164 +Aufs mount options are described in aufs.5.
2165 +If you run chroot or something and make your aufs as a root directory,
2166 +then you need to customize the shutdown script. See the aufs manual in
2167 +detail.
2168 +
2169 +Additionally, there are some sample usages of aufs which are a
2170 +diskless system with network booting, and LiveCD over NFS.
2171 +See sample dir in CVS tree on SourceForge.
2172 +
2173 +
2174 +5. Contact
2175 +----------------------------------------
2176 +When you have any problems or strange behaviour in aufs, please let me
2177 +know with:
2178 +- /proc/mounts (instead of the output of mount(8))
2179 +- /sys/module/aufs/*
2180 +- /sys/fs/aufs/* (if you have them)
2181 +- /debug/aufs/* (if you have them)
2182 +- linux kernel version
2183 +  if your kernel is not plain, for example modified by distributor,
2184 +  the url where i can download its source is necessary too.
2185 +- aufs version which was printed at loading the module or booting the
2186 +  system, instead of the date you downloaded.
2187 +- configuration (define/undefine CONFIG_AUFS_xxx)
2188 +- kernel configuration or /proc/config.gz (if you have it)
2189 +- behaviour which you think to be incorrect
2190 +- actual operation, reproducible one is better
2191 +- mailto: aufs-users at lists.sourceforge.net
2192 +
2193 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2194 +and Feature Requests) on SourceForge. Please join and write to
2195 +aufs-users ML.
2196 +
2197 +
2198 +6. Acknowledgements
2199 +----------------------------------------
2200 +Thanks to everyone who have tried and are using aufs, whoever
2201 +have reported a bug or any feedback.
2202 +
2203 +Especially donators:
2204 +Tomas Matejicek(slax.org) made a donation (much more than once).
2205 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2206 +       scripts) is making "doubling" donations.
2207 +       Unfortunately I cannot list all of the donators, but I really
2208 +       appreciate.
2209 +       It ends Aug 2010, but the ordinary donation URL is still available.
2210 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2211 +Dai Itasaka made a donation (2007/8).
2212 +Chuck Smith made a donation (2008/4, 10 and 12).
2213 +Henk Schoneveld made a donation (2008/9).
2214 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2215 +Francois Dupoux made a donation (2008/11).
2216 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2217 +       aufs2 GIT tree (2009/2).
2218 +William Grant made a donation (2009/3).
2219 +Patrick Lane made a donation (2009/4).
2220 +The Mail Archive (mail-archive.com) made donations (2009/5).
2221 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2222 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2223 +Pavel Pronskiy made a donation (2011/2).
2224 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2225 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2226 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2227 +11).
2228 +Sam Liddicott made a donation (2011/9).
2229 +Era Scarecrow made a donation (2013/4).
2230 +Bor Ratajc made a donation (2013/4).
2231 +Alessandro Gorreta made a donation (2013/4).
2232 +POIRETTE Marc made a donation (2013/4).
2233 +Alessandro Gorreta made a donation (2013/4).
2234 +lauri kasvandik made a donation (2013/5).
2235 +"pemasu from Finland" made a donation (2013/7).
2236 +The Parted Magic Project made a donation (2013/9 and 11).
2237 +Pavel Barta made a donation (2013/10).
2238 +Nikolay Pertsev made a donation (2014/5).
2239 +
2240 +Thank you very much.
2241 +Donations are always, including future donations, very important and
2242 +helpful for me to keep on developing aufs.
2243 +
2244 +
2245 +7.
2246 +----------------------------------------
2247 +If you are an experienced user, no explanation is needed. Aufs is
2248 +just a linux filesystem.
2249 +
2250 +
2251 +Enjoy!
2252 +
2253 +# Local variables: ;
2254 +# mode: text;
2255 +# End: ;
2256 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2257 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2258 +++ linux/fs/aufs/aufs.h        2014-06-24 00:14:06.776052275 +0200
2259 @@ -0,0 +1,59 @@
2260 +/*
2261 + * Copyright (C) 2005-2014 Junjiro R. Okajima
2262 + *
2263 + * This program, aufs is free software; you can redistribute it and/or modify
2264 + * it under the terms of the GNU General Public License as published by
2265 + * the Free Software Foundation; either version 2 of the License, or
2266 + * (at your option) any later version.
2267 + *
2268 + * This program is distributed in the hope that it will be useful,
2269 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2270 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2271 + * GNU General Public License for more details.
2272 + *
2273 + * You should have received a copy of the GNU General Public License
2274 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2275 + */
2276 +
2277 +/*
2278 + * all header files
2279 + */
2280 +
2281 +#ifndef __AUFS_H__
2282 +#define __AUFS_H__
2283 +
2284 +#ifdef __KERNEL__
2285 +
2286 +#define AuStub(type, name, body, ...) \
2287 +       static inline type name(__VA_ARGS__) { body; }
2288 +
2289 +#define AuStubVoid(name, ...) \
2290 +       AuStub(void, name, , __VA_ARGS__)
2291 +#define AuStubInt0(name, ...) \
2292 +       AuStub(int, name, return 0, __VA_ARGS__)
2293 +
2294 +#include "debug.h"
2295 +
2296 +#include "branch.h"
2297 +#include "cpup.h"
2298 +#include "dcsub.h"
2299 +#include "dbgaufs.h"
2300 +#include "dentry.h"
2301 +#include "dir.h"
2302 +#include "dynop.h"
2303 +#include "file.h"
2304 +#include "fstype.h"
2305 +#include "inode.h"
2306 +#include "loop.h"
2307 +#include "module.h"
2308 +#include "opts.h"
2309 +#include "rwsem.h"
2310 +#include "spl.h"
2311 +#include "super.h"
2312 +#include "sysaufs.h"
2313 +#include "vfsub.h"
2314 +#include "whout.h"
2315 +#include "wkq.h"
2316 +
2317 +#endif /* __KERNEL__ */
2318 +#endif /* __AUFS_H__ */
2319 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2320 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2321 +++ linux/fs/aufs/branch.c      2014-06-24 00:14:06.776052275 +0200
2322 @@ -0,0 +1,1220 @@
2323 +/*
2324 + * Copyright (C) 2005-2014 Junjiro R. Okajima
2325 + *
2326 + * This program, aufs is free software; you can redistribute it and/or modify
2327 + * it under the terms of the GNU General Public License as published by
2328 + * the Free Software Foundation; either version 2 of the License, or
2329 + * (at your option) any later version.
2330 + *
2331 + * This program is distributed in the hope that it will be useful,
2332 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2333 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2334 + * GNU General Public License for more details.
2335 + *
2336 + * You should have received a copy of the GNU General Public License
2337 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2338 + */
2339 +
2340 +/*
2341 + * branch management
2342 + */
2343 +
2344 +#include <linux/compat.h>
2345 +#include <linux/statfs.h>
2346 +#include "aufs.h"
2347 +
2348 +/*
2349 + * free a single branch
2350 + */
2351 +
2352 +/* prohibit rmdir to the root of the branch */
2353 +/* todo: another new flag? */
2354 +static void au_br_dflags_force(struct au_branch *br)
2355 +{
2356 +       struct dentry *h_dentry;
2357 +
2358 +       h_dentry = au_br_dentry(br);
2359 +       spin_lock(&h_dentry->d_lock);
2360 +       br->br_dflags = h_dentry->d_flags & DCACHE_MOUNTED;
2361 +       h_dentry->d_flags |= DCACHE_MOUNTED;
2362 +       spin_unlock(&h_dentry->d_lock);
2363 +}
2364 +
2365 +/* restore its d_flags */
2366 +static void au_br_dflags_restore(struct au_branch *br)
2367 +{
2368 +       struct dentry *h_dentry;
2369 +
2370 +       if (br->br_dflags)
2371 +               return;
2372 +
2373 +       h_dentry = au_br_dentry(br);
2374 +       spin_lock(&h_dentry->d_lock);
2375 +       h_dentry->d_flags &= ~DCACHE_MOUNTED;
2376 +       spin_unlock(&h_dentry->d_lock);
2377 +}
2378 +
2379 +static void au_br_do_free(struct au_branch *br)
2380 +{
2381 +       int i;
2382 +       struct au_wbr *wbr;
2383 +       struct au_dykey **key;
2384 +
2385 +       au_hnotify_fin_br(br);
2386 +
2387 +       if (br->br_xino.xi_file)
2388 +               fput(br->br_xino.xi_file);
2389 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
2390 +
2391 +       AuDebugOn(atomic_read(&br->br_count));
2392 +
2393 +       wbr = br->br_wbr;
2394 +       if (wbr) {
2395 +               for (i = 0; i < AuBrWh_Last; i++)
2396 +                       dput(wbr->wbr_wh[i]);
2397 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2398 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2399 +       }
2400 +
2401 +       key = br->br_dykey;
2402 +       for (i = 0; i < AuBrDynOp; i++, key++)
2403 +               if (*key)
2404 +                       au_dy_put(*key);
2405 +               else
2406 +                       break;
2407 +
2408 +       au_br_dflags_restore(br);
2409 +
2410 +       /* recursive lock, s_umount of branch's */
2411 +       lockdep_off();
2412 +       path_put(&br->br_path);
2413 +       lockdep_on();
2414 +       kfree(wbr);
2415 +       kfree(br);
2416 +}
2417 +
2418 +/*
2419 + * frees all branches
2420 + */
2421 +void au_br_free(struct au_sbinfo *sbinfo)
2422 +{
2423 +       aufs_bindex_t bmax;
2424 +       struct au_branch **br;
2425 +
2426 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2427 +
2428 +       bmax = sbinfo->si_bend + 1;
2429 +       br = sbinfo->si_branch;
2430 +       while (bmax--)
2431 +               au_br_do_free(*br++);
2432 +}
2433 +
2434 +/*
2435 + * find the index of a branch which is specified by @br_id.
2436 + */
2437 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2438 +{
2439 +       aufs_bindex_t bindex, bend;
2440 +
2441 +       bend = au_sbend(sb);
2442 +       for (bindex = 0; bindex <= bend; bindex++)
2443 +               if (au_sbr_id(sb, bindex) == br_id)
2444 +                       return bindex;
2445 +       return -1;
2446 +}
2447 +
2448 +/* ---------------------------------------------------------------------- */
2449 +
2450 +/*
2451 + * add a branch
2452 + */
2453 +
2454 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2455 +                       struct dentry *h_root)
2456 +{
2457 +       if (unlikely(h_adding == h_root
2458 +                    || au_test_loopback_overlap(sb, h_adding)))
2459 +               return 1;
2460 +       if (h_adding->d_sb != h_root->d_sb)
2461 +               return 0;
2462 +       return au_test_subdir(h_adding, h_root)
2463 +               || au_test_subdir(h_root, h_adding);
2464 +}
2465 +
2466 +/*
2467 + * returns a newly allocated branch. @new_nbranch is a number of branches
2468 + * after adding a branch.
2469 + */
2470 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2471 +                                    int perm)
2472 +{
2473 +       struct au_branch *add_branch;
2474 +       struct dentry *root;
2475 +       int err;
2476 +
2477 +       err = -ENOMEM;
2478 +       root = sb->s_root;
2479 +       add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
2480 +       if (unlikely(!add_branch))
2481 +               goto out;
2482 +
2483 +       err = au_hnotify_init_br(add_branch, perm);
2484 +       if (unlikely(err))
2485 +               goto out_br;
2486 +
2487 +       add_branch->br_wbr = NULL;
2488 +       if (au_br_writable(perm)) {
2489 +               /* may be freed separately at changing the branch permission */
2490 +               add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
2491 +                                            GFP_NOFS);
2492 +               if (unlikely(!add_branch->br_wbr))
2493 +                       goto out_hnotify;
2494 +       }
2495 +
2496 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
2497 +       if (!err)
2498 +               err = au_di_realloc(au_di(root), new_nbranch);
2499 +       if (!err)
2500 +               err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
2501 +       if (!err)
2502 +               return add_branch; /* success */
2503 +
2504 +       kfree(add_branch->br_wbr);
2505 +
2506 +out_hnotify:
2507 +       au_hnotify_fin_br(add_branch);
2508 +out_br:
2509 +       kfree(add_branch);
2510 +out:
2511 +       return ERR_PTR(err);
2512 +}
2513 +
2514 +/*
2515 + * test if the branch permission is legal or not.
2516 + */
2517 +static int test_br(struct inode *inode, int brperm, char *path)
2518 +{
2519 +       int err;
2520 +
2521 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
2522 +       if (!err)
2523 +               goto out;
2524 +
2525 +       err = -EINVAL;
2526 +       pr_err("write permission for readonly mount or inode, %s\n", path);
2527 +
2528 +out:
2529 +       return err;
2530 +}
2531 +
2532 +/*
2533 + * returns:
2534 + * 0: success, the caller will add it
2535 + * plus: success, it is already unified, the caller should ignore it
2536 + * minus: error
2537 + */
2538 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
2539 +{
2540 +       int err;
2541 +       aufs_bindex_t bend, bindex;
2542 +       struct dentry *root;
2543 +       struct inode *inode, *h_inode;
2544 +
2545 +       root = sb->s_root;
2546 +       bend = au_sbend(sb);
2547 +       if (unlikely(bend >= 0
2548 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
2549 +               err = 1;
2550 +               if (!remount) {
2551 +                       err = -EINVAL;
2552 +                       pr_err("%s duplicated\n", add->pathname);
2553 +               }
2554 +               goto out;
2555 +       }
2556 +
2557 +       err = -ENOSPC; /* -E2BIG; */
2558 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2559 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
2560 +               pr_err("number of branches exceeded %s\n", add->pathname);
2561 +               goto out;
2562 +       }
2563 +
2564 +       err = -EDOM;
2565 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
2566 +               pr_err("bad index %d\n", add->bindex);
2567 +               goto out;
2568 +       }
2569 +
2570 +       inode = add->path.dentry->d_inode;
2571 +       err = -ENOENT;
2572 +       if (unlikely(!inode->i_nlink)) {
2573 +               pr_err("no existence %s\n", add->pathname);
2574 +               goto out;
2575 +       }
2576 +
2577 +       err = -EINVAL;
2578 +       if (unlikely(inode->i_sb == sb)) {
2579 +               pr_err("%s must be outside\n", add->pathname);
2580 +               goto out;
2581 +       }
2582 +
2583 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
2584 +               pr_err("unsupported filesystem, %s (%s)\n",
2585 +                      add->pathname, au_sbtype(inode->i_sb));
2586 +               goto out;
2587 +       }
2588 +
2589 +       err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
2590 +       if (unlikely(err))
2591 +               goto out;
2592 +
2593 +       if (bend < 0)
2594 +               return 0; /* success */
2595 +
2596 +       err = -EINVAL;
2597 +       for (bindex = 0; bindex <= bend; bindex++)
2598 +               if (unlikely(test_overlap(sb, add->path.dentry,
2599 +                                         au_h_dptr(root, bindex)))) {
2600 +                       pr_err("%s is overlapped\n", add->pathname);
2601 +                       goto out;
2602 +               }
2603 +
2604 +       err = 0;
2605 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
2606 +               h_inode = au_h_dptr(root, 0)->d_inode;
2607 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
2608 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
2609 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
2610 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
2611 +                               add->pathname,
2612 +                               i_uid_read(inode), i_gid_read(inode),
2613 +                               (inode->i_mode & S_IALLUGO),
2614 +                               i_uid_read(h_inode), i_gid_read(h_inode),
2615 +                               (h_inode->i_mode & S_IALLUGO));
2616 +       }
2617 +
2618 +out:
2619 +       return err;
2620 +}
2621 +
2622 +/*
2623 + * initialize or clean the whiteouts for an adding branch
2624 + */
2625 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
2626 +                        int new_perm)
2627 +{
2628 +       int err, old_perm;
2629 +       aufs_bindex_t bindex;
2630 +       struct mutex *h_mtx;
2631 +       struct au_wbr *wbr;
2632 +       struct au_hinode *hdir;
2633 +
2634 +       err = vfsub_mnt_want_write(au_br_mnt(br));
2635 +       if (unlikely(err))
2636 +               goto out;
2637 +
2638 +       wbr = br->br_wbr;
2639 +       old_perm = br->br_perm;
2640 +       br->br_perm = new_perm;
2641 +       hdir = NULL;
2642 +       h_mtx = NULL;
2643 +       bindex = au_br_index(sb, br->br_id);
2644 +       if (0 <= bindex) {
2645 +               hdir = au_hi(sb->s_root->d_inode, bindex);
2646 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
2647 +       } else {
2648 +               h_mtx = &au_br_dentry(br)->d_inode->i_mutex;
2649 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
2650 +       }
2651 +       if (!wbr)
2652 +               err = au_wh_init(br, sb);
2653 +       else {
2654 +               wbr_wh_write_lock(wbr);
2655 +               err = au_wh_init(br, sb);
2656 +               wbr_wh_write_unlock(wbr);
2657 +       }
2658 +       if (hdir)
2659 +               au_hn_imtx_unlock(hdir);
2660 +       else
2661 +               mutex_unlock(h_mtx);
2662 +       vfsub_mnt_drop_write(au_br_mnt(br));
2663 +       br->br_perm = old_perm;
2664 +
2665 +       if (!err && wbr && !au_br_writable(new_perm)) {
2666 +               kfree(wbr);
2667 +               br->br_wbr = NULL;
2668 +       }
2669 +
2670 +out:
2671 +       return err;
2672 +}
2673 +
2674 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
2675 +                      int perm)
2676 +{
2677 +       int err;
2678 +       struct kstatfs kst;
2679 +       struct au_wbr *wbr;
2680 +
2681 +       wbr = br->br_wbr;
2682 +       au_rw_init(&wbr->wbr_wh_rwsem);
2683 +       memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
2684 +       atomic_set(&wbr->wbr_wh_running, 0);
2685 +       wbr->wbr_bytes = 0;
2686 +
2687 +       /*
2688 +        * a limit for rmdir/rename a dir
2689 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
2690 +        */
2691 +       err = vfs_statfs(&br->br_path, &kst);
2692 +       if (unlikely(err))
2693 +               goto out;
2694 +       err = -EINVAL;
2695 +       if (kst.f_namelen >= NAME_MAX)
2696 +               err = au_br_init_wh(sb, br, perm);
2697 +       else
2698 +               pr_err("%pd(%s), unsupported namelen %ld\n",
2699 +                      au_br_dentry(br),
2700 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
2701 +
2702 +out:
2703 +       return err;
2704 +}
2705 +
2706 +/* intialize a new branch */
2707 +static int au_br_init(struct au_branch *br, struct super_block *sb,
2708 +                     struct au_opt_add *add)
2709 +{
2710 +       int err;
2711 +
2712 +       err = 0;
2713 +       memset(&br->br_xino, 0, sizeof(br->br_xino));
2714 +       mutex_init(&br->br_xino.xi_nondir_mtx);
2715 +       br->br_perm = add->perm;
2716 +       BUILD_BUG_ON(sizeof(br->br_dflags)
2717 +                    != sizeof(br->br_path.dentry->d_flags));
2718 +       br->br_dflags = DCACHE_MOUNTED;
2719 +       br->br_path = add->path; /* set first, path_get() later */
2720 +       spin_lock_init(&br->br_dykey_lock);
2721 +       memset(br->br_dykey, 0, sizeof(br->br_dykey));
2722 +       atomic_set(&br->br_count, 0);
2723 +       atomic_set(&br->br_xino_running, 0);
2724 +       br->br_id = au_new_br_id(sb);
2725 +       AuDebugOn(br->br_id < 0);
2726 +
2727 +       if (au_br_writable(add->perm)) {
2728 +               err = au_wbr_init(br, sb, add->perm);
2729 +               if (unlikely(err))
2730 +                       goto out_err;
2731 +       }
2732 +
2733 +       if (au_opt_test(au_mntflags(sb), XINO)) {
2734 +               err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
2735 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
2736 +               if (unlikely(err)) {
2737 +                       AuDebugOn(br->br_xino.xi_file);
2738 +                       goto out_err;
2739 +               }
2740 +       }
2741 +
2742 +       sysaufs_br_init(br);
2743 +       path_get(&br->br_path);
2744 +       goto out; /* success */
2745 +
2746 +out_err:
2747 +       memset(&br->br_path, 0, sizeof(br->br_path));
2748 +out:
2749 +       return err;
2750 +}
2751 +
2752 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
2753 +                            struct au_branch *br, aufs_bindex_t bend,
2754 +                            aufs_bindex_t amount)
2755 +{
2756 +       struct au_branch **brp;
2757 +
2758 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2759 +
2760 +       brp = sbinfo->si_branch + bindex;
2761 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
2762 +       *brp = br;
2763 +       sbinfo->si_bend++;
2764 +       if (unlikely(bend < 0))
2765 +               sbinfo->si_bend = 0;
2766 +}
2767 +
2768 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
2769 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2770 +{
2771 +       struct au_hdentry *hdp;
2772 +
2773 +       AuRwMustWriteLock(&dinfo->di_rwsem);
2774 +
2775 +       hdp = dinfo->di_hdentry + bindex;
2776 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
2777 +       au_h_dentry_init(hdp);
2778 +       dinfo->di_bend++;
2779 +       if (unlikely(bend < 0))
2780 +               dinfo->di_bstart = 0;
2781 +}
2782 +
2783 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
2784 +                            aufs_bindex_t bend, aufs_bindex_t amount)
2785 +{
2786 +       struct au_hinode *hip;
2787 +
2788 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
2789 +
2790 +       hip = iinfo->ii_hinode + bindex;
2791 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
2792 +       hip->hi_inode = NULL;
2793 +       au_hn_init(hip);
2794 +       iinfo->ii_bend++;
2795 +       if (unlikely(bend < 0))
2796 +               iinfo->ii_bstart = 0;
2797 +}
2798 +
2799 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
2800 +                        aufs_bindex_t bindex)
2801 +{
2802 +       struct dentry *root, *h_dentry;
2803 +       struct inode *root_inode;
2804 +       aufs_bindex_t bend, amount;
2805 +
2806 +       au_br_dflags_force(br);
2807 +
2808 +       root = sb->s_root;
2809 +       root_inode = root->d_inode;
2810 +       bend = au_sbend(sb);
2811 +       amount = bend + 1 - bindex;
2812 +       h_dentry = au_br_dentry(br);
2813 +       au_sbilist_lock();
2814 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
2815 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
2816 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
2817 +       au_set_h_dptr(root, bindex, dget(h_dentry));
2818 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
2819 +                     /*flags*/0);
2820 +       au_sbilist_unlock();
2821 +}
2822 +
2823 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
2824 +{
2825 +       int err;
2826 +       aufs_bindex_t bend, add_bindex;
2827 +       struct dentry *root, *h_dentry;
2828 +       struct inode *root_inode;
2829 +       struct au_branch *add_branch;
2830 +
2831 +       root = sb->s_root;
2832 +       root_inode = root->d_inode;
2833 +       IMustLock(root_inode);
2834 +       err = test_add(sb, add, remount);
2835 +       if (unlikely(err < 0))
2836 +               goto out;
2837 +       if (err) {
2838 +               err = 0;
2839 +               goto out; /* success */
2840 +       }
2841 +
2842 +       bend = au_sbend(sb);
2843 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
2844 +       err = PTR_ERR(add_branch);
2845 +       if (IS_ERR(add_branch))
2846 +               goto out;
2847 +
2848 +       err = au_br_init(add_branch, sb, add);
2849 +       if (unlikely(err)) {
2850 +               au_br_do_free(add_branch);
2851 +               goto out;
2852 +       }
2853 +
2854 +       add_bindex = add->bindex;
2855 +       if (!remount)
2856 +               au_br_do_add(sb, add_branch, add_bindex);
2857 +       else {
2858 +               sysaufs_brs_del(sb, add_bindex);
2859 +               au_br_do_add(sb, add_branch, add_bindex);
2860 +               sysaufs_brs_add(sb, add_bindex);
2861 +       }
2862 +
2863 +       h_dentry = add->path.dentry;
2864 +       if (!add_bindex) {
2865 +               au_cpup_attr_all(root_inode, /*force*/1);
2866 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
2867 +       } else
2868 +               au_add_nlink(root_inode, h_dentry->d_inode);
2869 +
2870 +       /*
2871 +        * this test/set prevents aufs from handling unnecesary notify events
2872 +        * of xino files, in case of re-adding a writable branch which was
2873 +        * once detached from aufs.
2874 +        */
2875 +       if (au_xino_brid(sb) < 0
2876 +           && au_br_writable(add_branch->br_perm)
2877 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
2878 +           && add_branch->br_xino.xi_file
2879 +           && add_branch->br_xino.xi_file->f_dentry->d_parent == h_dentry)
2880 +               au_xino_brid_set(sb, add_branch->br_id);
2881 +
2882 +out:
2883 +       return err;
2884 +}
2885 +
2886 +/* ---------------------------------------------------------------------- */
2887 +
2888 +/*
2889 + * delete a branch
2890 + */
2891 +
2892 +/* to show the line number, do not make it inlined function */
2893 +#define AuVerbose(do_info, fmt, ...) do { \
2894 +       if (do_info) \
2895 +               pr_info(fmt, ##__VA_ARGS__); \
2896 +} while (0)
2897 +
2898 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
2899 +                        aufs_bindex_t bend)
2900 +{
2901 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
2902 +}
2903 +
2904 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
2905 +                        aufs_bindex_t bend)
2906 +{
2907 +       return au_test_ibusy(dentry->d_inode, bstart, bend);
2908 +}
2909 +
2910 +/*
2911 + * test if the branch is deletable or not.
2912 + */
2913 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
2914 +                           unsigned int sigen, const unsigned int verbose)
2915 +{
2916 +       int err, i, j, ndentry;
2917 +       aufs_bindex_t bstart, bend;
2918 +       struct au_dcsub_pages dpages;
2919 +       struct au_dpage *dpage;
2920 +       struct dentry *d;
2921 +
2922 +       err = au_dpages_init(&dpages, GFP_NOFS);
2923 +       if (unlikely(err))
2924 +               goto out;
2925 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
2926 +       if (unlikely(err))
2927 +               goto out_dpages;
2928 +
2929 +       for (i = 0; !err && i < dpages.ndpage; i++) {
2930 +               dpage = dpages.dpages + i;
2931 +               ndentry = dpage->ndentry;
2932 +               for (j = 0; !err && j < ndentry; j++) {
2933 +                       d = dpage->dentries[j];
2934 +                       AuDebugOn(!d_count(d));
2935 +                       if (!au_digen_test(d, sigen)) {
2936 +                               di_read_lock_child(d, AuLock_IR);
2937 +                               if (unlikely(au_dbrange_test(d))) {
2938 +                                       di_read_unlock(d, AuLock_IR);
2939 +                                       continue;
2940 +                               }
2941 +                       } else {
2942 +                               di_write_lock_child(d);
2943 +                               if (unlikely(au_dbrange_test(d))) {
2944 +                                       di_write_unlock(d);
2945 +                                       continue;
2946 +                               }
2947 +                               err = au_reval_dpath(d, sigen);
2948 +                               if (!err)
2949 +                                       di_downgrade_lock(d, AuLock_IR);
2950 +                               else {
2951 +                                       di_write_unlock(d);
2952 +                                       break;
2953 +                               }
2954 +                       }
2955 +
2956 +                       /* AuDbgDentry(d); */
2957 +                       bstart = au_dbstart(d);
2958 +                       bend = au_dbend(d);
2959 +                       if (bstart <= bindex
2960 +                           && bindex <= bend
2961 +                           && au_h_dptr(d, bindex)
2962 +                           && au_test_dbusy(d, bstart, bend)) {
2963 +                               err = -EBUSY;
2964 +                               AuVerbose(verbose, "busy %pd\n", d);
2965 +                               AuDbgDentry(d);
2966 +                       }
2967 +                       di_read_unlock(d, AuLock_IR);
2968 +               }
2969 +       }
2970 +
2971 +out_dpages:
2972 +       au_dpages_free(&dpages);
2973 +out:
2974 +       return err;
2975 +}
2976 +
2977 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
2978 +                          unsigned int sigen, const unsigned int verbose)
2979 +{
2980 +       int err;
2981 +       unsigned long long max, ull;
2982 +       struct inode *i, **array;
2983 +       aufs_bindex_t bstart, bend;
2984 +
2985 +       array = au_iarray_alloc(sb, &max);
2986 +       err = PTR_ERR(array);
2987 +       if (IS_ERR(array))
2988 +               goto out;
2989 +
2990 +       err = 0;
2991 +       AuDbg("b%d\n", bindex);
2992 +       for (ull = 0; !err && ull < max; ull++) {
2993 +               i = array[ull];
2994 +               if (i->i_ino == AUFS_ROOT_INO)
2995 +                       continue;
2996 +
2997 +               /* AuDbgInode(i); */
2998 +               if (au_iigen(i, NULL) == sigen)
2999 +                       ii_read_lock_child(i);
3000 +               else {
3001 +                       ii_write_lock_child(i);
3002 +                       err = au_refresh_hinode_self(i);
3003 +                       au_iigen_dec(i);
3004 +                       if (!err)
3005 +                               ii_downgrade_lock(i);
3006 +                       else {
3007 +                               ii_write_unlock(i);
3008 +                               break;
3009 +                       }
3010 +               }
3011 +
3012 +               bstart = au_ibstart(i);
3013 +               bend = au_ibend(i);
3014 +               if (bstart <= bindex
3015 +                   && bindex <= bend
3016 +                   && au_h_iptr(i, bindex)
3017 +                   && au_test_ibusy(i, bstart, bend)) {
3018 +                       err = -EBUSY;
3019 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3020 +                       AuDbgInode(i);
3021 +               }
3022 +               ii_read_unlock(i);
3023 +       }
3024 +       au_iarray_free(array, max);
3025 +
3026 +out:
3027 +       return err;
3028 +}
3029 +
3030 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3031 +                             const unsigned int verbose)
3032 +{
3033 +       int err;
3034 +       unsigned int sigen;
3035 +
3036 +       sigen = au_sigen(root->d_sb);
3037 +       DiMustNoWaiters(root);
3038 +       IiMustNoWaiters(root->d_inode);
3039 +       di_write_unlock(root);
3040 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3041 +       if (!err)
3042 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3043 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3044 +
3045 +       return err;
3046 +}
3047 +
3048 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3049 +                            const aufs_bindex_t bindex,
3050 +                            const aufs_bindex_t bend)
3051 +{
3052 +       struct au_branch **brp, **p;
3053 +
3054 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3055 +
3056 +       brp = sbinfo->si_branch + bindex;
3057 +       if (bindex < bend)
3058 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
3059 +       sbinfo->si_branch[0 + bend] = NULL;
3060 +       sbinfo->si_bend--;
3061 +
3062 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
3063 +       if (p)
3064 +               sbinfo->si_branch = p;
3065 +       /* harmless error */
3066 +}
3067 +
3068 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3069 +                            const aufs_bindex_t bend)
3070 +{
3071 +       struct au_hdentry *hdp, *p;
3072 +
3073 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3074 +
3075 +       hdp = dinfo->di_hdentry;
3076 +       if (bindex < bend)
3077 +               memmove(hdp + bindex, hdp + bindex + 1,
3078 +                       sizeof(*hdp) * (bend - bindex));
3079 +       hdp[0 + bend].hd_dentry = NULL;
3080 +       dinfo->di_bend--;
3081 +
3082 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
3083 +       if (p)
3084 +               dinfo->di_hdentry = p;
3085 +       /* harmless error */
3086 +}
3087 +
3088 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3089 +                            const aufs_bindex_t bend)
3090 +{
3091 +       struct au_hinode *hip, *p;
3092 +
3093 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3094 +
3095 +       hip = iinfo->ii_hinode + bindex;
3096 +       if (bindex < bend)
3097 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
3098 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
3099 +       au_hn_init(iinfo->ii_hinode + bend);
3100 +       iinfo->ii_bend--;
3101 +
3102 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
3103 +       if (p)
3104 +               iinfo->ii_hinode = p;
3105 +       /* harmless error */
3106 +}
3107 +
3108 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3109 +                        struct au_branch *br)
3110 +{
3111 +       aufs_bindex_t bend;
3112 +       struct au_sbinfo *sbinfo;
3113 +       struct dentry *root, *h_root;
3114 +       struct inode *inode, *h_inode;
3115 +       struct au_hinode *hinode;
3116 +
3117 +       SiMustWriteLock(sb);
3118 +
3119 +       root = sb->s_root;
3120 +       inode = root->d_inode;
3121 +       sbinfo = au_sbi(sb);
3122 +       bend = sbinfo->si_bend;
3123 +
3124 +       h_root = au_h_dptr(root, bindex);
3125 +       hinode = au_hi(inode, bindex);
3126 +       h_inode = au_igrab(hinode->hi_inode);
3127 +       au_hiput(hinode);
3128 +
3129 +       au_sbilist_lock();
3130 +       au_br_do_del_brp(sbinfo, bindex, bend);
3131 +       au_br_do_del_hdp(au_di(root), bindex, bend);
3132 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
3133 +       au_sbilist_unlock();
3134 +
3135 +       dput(h_root);
3136 +       iput(h_inode);
3137 +       au_br_do_free(br);
3138 +}
3139 +
3140 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3141 +{
3142 +       int err, rerr, i;
3143 +       unsigned int mnt_flags;
3144 +       aufs_bindex_t bindex, bend, br_id;
3145 +       unsigned char do_wh, verbose;
3146 +       struct au_branch *br;
3147 +       struct au_wbr *wbr;
3148 +
3149 +       err = 0;
3150 +       bindex = au_find_dbindex(sb->s_root, del->h_path.dentry);
3151 +       if (bindex < 0) {
3152 +               if (remount)
3153 +                       goto out; /* success */
3154 +               err = -ENOENT;
3155 +               pr_err("%s no such branch\n", del->pathname);
3156 +               goto out;
3157 +       }
3158 +       AuDbg("bindex b%d\n", bindex);
3159 +
3160 +       err = -EBUSY;
3161 +       mnt_flags = au_mntflags(sb);
3162 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3163 +       bend = au_sbend(sb);
3164 +       if (unlikely(!bend)) {
3165 +               AuVerbose(verbose, "no more branches left\n");
3166 +               goto out;
3167 +       }
3168 +       br = au_sbr(sb, bindex);
3169 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3170 +       i = atomic_read(&br->br_count);
3171 +       if (unlikely(i)) {
3172 +               AuVerbose(verbose, "%d file(s) opened\n", i);
3173 +               goto out;
3174 +       }
3175 +
3176 +       wbr = br->br_wbr;
3177 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3178 +       if (do_wh) {
3179 +               /* instead of WbrWhMustWriteLock(wbr) */
3180 +               SiMustWriteLock(sb);
3181 +               for (i = 0; i < AuBrWh_Last; i++) {
3182 +                       dput(wbr->wbr_wh[i]);
3183 +                       wbr->wbr_wh[i] = NULL;
3184 +               }
3185 +       }
3186 +
3187 +       err = test_children_busy(sb->s_root, bindex, verbose);
3188 +       if (unlikely(err)) {
3189 +               if (do_wh)
3190 +                       goto out_wh;
3191 +               goto out;
3192 +       }
3193 +
3194 +       err = 0;
3195 +       br_id = br->br_id;
3196 +       if (!remount)
3197 +               au_br_do_del(sb, bindex, br);
3198 +       else {
3199 +               sysaufs_brs_del(sb, bindex);
3200 +               au_br_do_del(sb, bindex, br);
3201 +               sysaufs_brs_add(sb, bindex);
3202 +       }
3203 +
3204 +       if (!bindex) {
3205 +               au_cpup_attr_all(sb->s_root->d_inode, /*force*/1);
3206 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3207 +       } else
3208 +               au_sub_nlink(sb->s_root->d_inode, del->h_path.dentry->d_inode);
3209 +       if (au_opt_test(mnt_flags, PLINK))
3210 +               au_plink_half_refresh(sb, br_id);
3211 +
3212 +       if (au_xino_brid(sb) == br_id)
3213 +               au_xino_brid_set(sb, -1);
3214 +       goto out; /* success */
3215 +
3216 +out_wh:
3217 +       /* revert */
3218 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3219 +       if (rerr)
3220 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3221 +                       del->pathname, rerr);
3222 +out:
3223 +       return err;
3224 +}
3225 +
3226 +/* ---------------------------------------------------------------------- */
3227 +
3228 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3229 +{
3230 +       int err;
3231 +       aufs_bindex_t bstart, bend;
3232 +       struct aufs_ibusy ibusy;
3233 +       struct inode *inode, *h_inode;
3234 +
3235 +       err = -EPERM;
3236 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
3237 +               goto out;
3238 +
3239 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
3240 +       if (!err)
3241 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
3242 +       if (unlikely(err)) {
3243 +               err = -EFAULT;
3244 +               AuTraceErr(err);
3245 +               goto out;
3246 +       }
3247 +
3248 +       err = -EINVAL;
3249 +       si_read_lock(sb, AuLock_FLUSH);
3250 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
3251 +               goto out_unlock;
3252 +
3253 +       err = 0;
3254 +       ibusy.h_ino = 0; /* invalid */
3255 +       inode = ilookup(sb, ibusy.ino);
3256 +       if (!inode
3257 +           || inode->i_ino == AUFS_ROOT_INO
3258 +           || is_bad_inode(inode))
3259 +               goto out_unlock;
3260 +
3261 +       ii_read_lock_child(inode);
3262 +       bstart = au_ibstart(inode);
3263 +       bend = au_ibend(inode);
3264 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
3265 +               h_inode = au_h_iptr(inode, ibusy.bindex);
3266 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
3267 +                       ibusy.h_ino = h_inode->i_ino;
3268 +       }
3269 +       ii_read_unlock(inode);
3270 +       iput(inode);
3271 +
3272 +out_unlock:
3273 +       si_read_unlock(sb);
3274 +       if (!err) {
3275 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
3276 +               if (unlikely(err)) {
3277 +                       err = -EFAULT;
3278 +                       AuTraceErr(err);
3279 +               }
3280 +       }
3281 +out:
3282 +       return err;
3283 +}
3284 +
3285 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
3286 +{
3287 +       return au_ibusy(file->f_dentry->d_sb, (void __user *)arg);
3288 +}
3289 +
3290 +#ifdef CONFIG_COMPAT
3291 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
3292 +{
3293 +       return au_ibusy(file->f_dentry->d_sb, compat_ptr(arg));
3294 +}
3295 +#endif
3296 +
3297 +/* ---------------------------------------------------------------------- */
3298 +
3299 +/*
3300 + * change a branch permission
3301 + */
3302 +
3303 +static void au_warn_ima(void)
3304 +{
3305 +#ifdef CONFIG_IMA
3306 +       /* since it doesn't support mark_files_ro() */
3307 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
3308 +#endif
3309 +}
3310 +
3311 +static int do_need_sigen_inc(int a, int b)
3312 +{
3313 +       return au_br_whable(a) && !au_br_whable(b);
3314 +}
3315 +
3316 +static int need_sigen_inc(int old, int new)
3317 +{
3318 +       return do_need_sigen_inc(old, new)
3319 +               || do_need_sigen_inc(new, old);
3320 +}
3321 +
3322 +static unsigned long long au_farray_cb(void *a,
3323 +                                      unsigned long long max __maybe_unused,
3324 +                                      void *arg)
3325 +{
3326 +       unsigned long long n;
3327 +       struct file **p, *f;
3328 +       struct au_sphlhead *files;
3329 +       struct au_finfo *finfo;
3330 +       struct super_block *sb = arg;
3331 +
3332 +       n = 0;
3333 +       p = a;
3334 +       files = &au_sbi(sb)->si_files;
3335 +       spin_lock(&files->spin);
3336 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
3337 +               f = finfo->fi_file;
3338 +               if (file_count(f)
3339 +                   && !special_file(file_inode(f)->i_mode)) {
3340 +                       get_file(f);
3341 +                       *p++ = f;
3342 +                       n++;
3343 +                       AuDebugOn(n > max);
3344 +               }
3345 +       }
3346 +       spin_unlock(&files->spin);
3347 +
3348 +       return n;
3349 +}
3350 +
3351 +static struct file **au_farray_alloc(struct super_block *sb,
3352 +                                    unsigned long long *max)
3353 +{
3354 +       *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
3355 +       return au_array_alloc(max, au_farray_cb, sb);
3356 +}
3357 +
3358 +static void au_farray_free(struct file **a, unsigned long long max)
3359 +{
3360 +       unsigned long long ull;
3361 +
3362 +       for (ull = 0; ull < max; ull++)
3363 +               if (a[ull])
3364 +                       fput(a[ull]);
3365 +       au_array_free(a);
3366 +}
3367 +
3368 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
3369 +{
3370 +       int err, do_warn;
3371 +       unsigned int mnt_flags;
3372 +       unsigned long long ull, max;
3373 +       aufs_bindex_t br_id;
3374 +       unsigned char verbose, writer;
3375 +       struct file *file, *hf, **array;
3376 +       struct inode *inode;
3377 +       struct au_hfile *hfile;
3378 +
3379 +       mnt_flags = au_mntflags(sb);
3380 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3381 +
3382 +       array = au_farray_alloc(sb, &max);
3383 +       err = PTR_ERR(array);
3384 +       if (IS_ERR(array))
3385 +               goto out;
3386 +
3387 +       do_warn = 0;
3388 +       br_id = au_sbr_id(sb, bindex);
3389 +       for (ull = 0; ull < max; ull++) {
3390 +               file = array[ull];
3391 +
3392 +               /* AuDbg("%pD\n", file); */
3393 +               fi_read_lock(file);
3394 +               if (unlikely(au_test_mmapped(file))) {
3395 +                       err = -EBUSY;
3396 +                       AuVerbose(verbose, "mmapped %pD\n", file);
3397 +                       AuDbgFile(file);
3398 +                       FiMustNoWaiters(file);
3399 +                       fi_read_unlock(file);
3400 +                       goto out_array;
3401 +               }
3402 +
3403 +               inode = file_inode(file);
3404 +               hfile = &au_fi(file)->fi_htop;
3405 +               hf = hfile->hf_file;
3406 +               if (!S_ISREG(inode->i_mode)
3407 +                   || !(file->f_mode & FMODE_WRITE)
3408 +                   || hfile->hf_br->br_id != br_id
3409 +                   || !(hf->f_mode & FMODE_WRITE))
3410 +                       array[ull] = NULL;
3411 +               else {
3412 +                       do_warn = 1;
3413 +                       get_file(file);
3414 +               }
3415 +
3416 +               FiMustNoWaiters(file);
3417 +               fi_read_unlock(file);
3418 +               fput(file);
3419 +       }
3420 +
3421 +       err = 0;
3422 +       if (do_warn)
3423 +               au_warn_ima();
3424 +
3425 +       for (ull = 0; ull < max; ull++) {
3426 +               file = array[ull];
3427 +               if (!file)
3428 +                       continue;
3429 +
3430 +               /* todo: already flushed? */
3431 +               /*
3432 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
3433 +                * approach which resets f_mode and calls mnt_drop_write() and
3434 +                * file_release_write() for each file, because the branch
3435 +                * attribute in aufs world is totally different from the native
3436 +                * fs rw/ro mode.
3437 +               */
3438 +               /* fi_read_lock(file); */
3439 +               hfile = &au_fi(file)->fi_htop;
3440 +               hf = hfile->hf_file;
3441 +               /* fi_read_unlock(file); */
3442 +               spin_lock(&hf->f_lock);
3443 +               writer = !!(hf->f_mode & FMODE_WRITER);
3444 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
3445 +               spin_unlock(&hf->f_lock);
3446 +               if (writer) {
3447 +                       put_write_access(file_inode(hf));
3448 +                       __mnt_drop_write(hf->f_path.mnt);
3449 +               }
3450 +       }
3451 +
3452 +out_array:
3453 +       au_farray_free(array, max);
3454 +out:
3455 +       AuTraceErr(err);
3456 +       return err;
3457 +}
3458 +
3459 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3460 +             int *do_refresh)
3461 +{
3462 +       int err, rerr;
3463 +       aufs_bindex_t bindex;
3464 +       struct dentry *root;
3465 +       struct au_branch *br;
3466 +
3467 +       root = sb->s_root;
3468 +       bindex = au_find_dbindex(root, mod->h_root);
3469 +       if (bindex < 0) {
3470 +               if (remount)
3471 +                       return 0; /* success */
3472 +               err = -ENOENT;
3473 +               pr_err("%s no such branch\n", mod->path);
3474 +               goto out;
3475 +       }
3476 +       AuDbg("bindex b%d\n", bindex);
3477 +
3478 +       err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
3479 +       if (unlikely(err))
3480 +               goto out;
3481 +
3482 +       br = au_sbr(sb, bindex);
3483 +       AuDebugOn(mod->h_root != au_br_dentry(br));
3484 +       if (br->br_perm == mod->perm)
3485 +               return 0; /* success */
3486 +
3487 +       if (au_br_writable(br->br_perm)) {
3488 +               /* remove whiteout base */
3489 +               err = au_br_init_wh(sb, br, mod->perm);
3490 +               if (unlikely(err))
3491 +                       goto out;
3492 +
3493 +               if (!au_br_writable(mod->perm)) {
3494 +                       /* rw --> ro, file might be mmapped */
3495 +                       DiMustNoWaiters(root);
3496 +                       IiMustNoWaiters(root->d_inode);
3497 +                       di_write_unlock(root);
3498 +                       err = au_br_mod_files_ro(sb, bindex);
3499 +                       /* aufs_write_lock() calls ..._child() */
3500 +                       di_write_lock_child(root);
3501 +
3502 +                       if (unlikely(err)) {
3503 +                               rerr = -ENOMEM;
3504 +                               br->br_wbr = kmalloc(sizeof(*br->br_wbr),
3505 +                                                    GFP_NOFS);
3506 +                               if (br->br_wbr)
3507 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
3508 +                               if (unlikely(rerr)) {
3509 +                                       AuIOErr("nested error %d (%d)\n",
3510 +                                               rerr, err);
3511 +                                       br->br_perm = mod->perm;
3512 +                               }
3513 +                       }
3514 +               }
3515 +       } else if (au_br_writable(mod->perm)) {
3516 +               /* ro --> rw */
3517 +               err = -ENOMEM;
3518 +               br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
3519 +               if (br->br_wbr) {
3520 +                       err = au_wbr_init(br, sb, mod->perm);
3521 +                       if (unlikely(err)) {
3522 +                               kfree(br->br_wbr);
3523 +                               br->br_wbr = NULL;
3524 +                       }
3525 +               }
3526 +       }
3527 +
3528 +       if (!err) {
3529 +               if ((br->br_perm & AuBrAttr_UNPIN)
3530 +                   && !(mod->perm & AuBrAttr_UNPIN))
3531 +                       au_br_dflags_force(br);
3532 +               else if (!(br->br_perm & AuBrAttr_UNPIN)
3533 +                        && (mod->perm & AuBrAttr_UNPIN))
3534 +                       au_br_dflags_restore(br);
3535 +               *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
3536 +               br->br_perm = mod->perm;
3537 +       }
3538 +
3539 +out:
3540 +       AuTraceErr(err);
3541 +       return err;
3542 +}
3543 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
3544 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
3545 +++ linux/fs/aufs/branch.h      2014-06-24 00:14:06.776052275 +0200
3546 @@ -0,0 +1,264 @@
3547 +/*
3548 + * Copyright (C) 2005-2014 Junjiro R. Okajima
3549 + *
3550 + * This program, aufs is free software; you can redistribute it and/or modify
3551 + * it under the terms of the GNU General Public License as published by
3552 + * the Free Software Foundation; either version 2 of the License, or
3553 + * (at your option) any later version.
3554 + *
3555 + * This program is distributed in the hope that it will be useful,
3556 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3557 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3558 + * GNU General Public License for more details.
3559 + *
3560 + * You should have received a copy of the GNU General Public License
3561 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3562 + */
3563 +
3564 +/*
3565 + * branch filesystems and xino for them
3566 + */
3567 +
3568 +#ifndef __AUFS_BRANCH_H__
3569 +#define __AUFS_BRANCH_H__
3570 +
3571 +#ifdef __KERNEL__
3572 +
3573 +#include <linux/mount.h>
3574 +#include "dynop.h"
3575 +#include "rwsem.h"
3576 +#include "super.h"
3577 +
3578 +/* ---------------------------------------------------------------------- */
3579 +
3580 +/* a xino file */
3581 +struct au_xino_file {
3582 +       struct file             *xi_file;
3583 +       struct mutex            xi_nondir_mtx;
3584 +
3585 +       /* todo: make xino files an array to support huge inode number */
3586 +
3587 +#ifdef CONFIG_DEBUG_FS
3588 +       struct dentry            *xi_dbgaufs;
3589 +#endif
3590 +};
3591 +
3592 +/* members for writable branch only */
3593 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
3594 +struct au_wbr {
3595 +       struct au_rwsem         wbr_wh_rwsem;
3596 +       struct dentry           *wbr_wh[AuBrWh_Last];
3597 +       atomic_t                wbr_wh_running;
3598 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
3599 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
3600 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
3601 +
3602 +       /* mfs mode */
3603 +       unsigned long long      wbr_bytes;
3604 +};
3605 +
3606 +/* ext2 has 3 types of operations at least, ext3 has 4 */
3607 +#define AuBrDynOp (AuDyLast * 4)
3608 +
3609 +#ifdef CONFIG_AUFS_HFSNOTIFY
3610 +/* support for asynchronous destruction */
3611 +struct au_br_hfsnotify {
3612 +       struct fsnotify_group   *hfsn_group;
3613 +};
3614 +#endif
3615 +
3616 +/* sysfs entries */
3617 +struct au_brsysfs {
3618 +       char                    name[16];
3619 +       struct attribute        attr;
3620 +};
3621 +
3622 +enum {
3623 +       AuBrSysfs_BR,
3624 +       AuBrSysfs_BRID,
3625 +       AuBrSysfs_Last
3626 +};
3627 +
3628 +/* protected by superblock rwsem */
3629 +struct au_branch {
3630 +       struct au_xino_file     br_xino;
3631 +
3632 +       aufs_bindex_t           br_id;
3633 +
3634 +       int                     br_perm;
3635 +       unsigned int            br_dflags;
3636 +       struct path             br_path;
3637 +       spinlock_t              br_dykey_lock;
3638 +       struct au_dykey         *br_dykey[AuBrDynOp];
3639 +       atomic_t                br_count;
3640 +
3641 +       struct au_wbr           *br_wbr;
3642 +
3643 +       /* xino truncation */
3644 +       atomic_t                br_xino_running;
3645 +
3646 +#ifdef CONFIG_AUFS_HFSNOTIFY
3647 +       struct au_br_hfsnotify  *br_hfsn;
3648 +#endif
3649 +
3650 +#ifdef CONFIG_SYSFS
3651 +       /* entries under sysfs per mount-point */
3652 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
3653 +#endif
3654 +};
3655 +
3656 +/* ---------------------------------------------------------------------- */
3657 +
3658 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
3659 +{
3660 +       return br->br_path.mnt;
3661 +}
3662 +
3663 +static inline struct dentry *au_br_dentry(struct au_branch *br)
3664 +{
3665 +       return br->br_path.dentry;
3666 +}
3667 +
3668 +static inline struct super_block *au_br_sb(struct au_branch *br)
3669 +{
3670 +       return au_br_mnt(br)->mnt_sb;
3671 +}
3672 +
3673 +/* branch permissions and attributes */
3674 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
3675 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
3676 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
3677 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
3678 +
3679 +#define AuBrRAttr_WH           (1 << 3)        /* whiteout-able */
3680 +
3681 +#define AuBrWAttr_NoLinkWH     (1 << 4)        /* un-hardlinkable whiteouts */
3682 +
3683 +#define AuBrAttr_UNPIN         (1 << 5)        /* rename-able top dir of
3684 +                                                  branch */
3685 +
3686 +static inline int au_br_writable(int brperm)
3687 +{
3688 +       return brperm & AuBrPerm_RW;
3689 +}
3690 +
3691 +static inline int au_br_whable(int brperm)
3692 +{
3693 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
3694 +}
3695 +
3696 +static inline int au_br_wh_linkable(int brperm)
3697 +{
3698 +       return !(brperm & AuBrWAttr_NoLinkWH);
3699 +}
3700 +
3701 +static inline int au_br_rdonly(struct au_branch *br)
3702 +{
3703 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
3704 +               || !au_br_writable(br->br_perm))
3705 +               ? -EROFS : 0;
3706 +}
3707 +
3708 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
3709 +{
3710 +#ifdef CONFIG_AUFS_HNOTIFY
3711 +       return !(brperm & AuBrPerm_RR);
3712 +#else
3713 +       return 0;
3714 +#endif
3715 +}
3716 +
3717 +/* ---------------------------------------------------------------------- */
3718 +
3719 +/* branch.c */
3720 +struct au_sbinfo;
3721 +void au_br_free(struct au_sbinfo *sinfo);
3722 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
3723 +struct au_opt_add;
3724 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
3725 +struct au_opt_del;
3726 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
3727 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
3728 +#ifdef CONFIG_COMPAT
3729 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
3730 +#endif
3731 +struct au_opt_mod;
3732 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3733 +             int *do_refresh);
3734 +
3735 +/* xino.c */
3736 +static const loff_t au_loff_max = LLONG_MAX;
3737 +
3738 +int au_xib_trunc(struct super_block *sb);
3739 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
3740 +                  loff_t *pos);
3741 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
3742 +                   loff_t *pos);
3743 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
3744 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
3745 +ino_t au_xino_new_ino(struct super_block *sb);
3746 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
3747 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3748 +                 ino_t ino);
3749 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
3750 +                ino_t *ino);
3751 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
3752 +              struct file *base_file, int do_test);
3753 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
3754 +
3755 +struct au_opt_xino;
3756 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
3757 +void au_xino_clr(struct super_block *sb);
3758 +struct file *au_xino_def(struct super_block *sb);
3759 +int au_xino_path(struct seq_file *seq, struct file *file);
3760 +
3761 +/* ---------------------------------------------------------------------- */
3762 +
3763 +/* Superblock to branch */
3764 +static inline
3765 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
3766 +{
3767 +       return au_sbr(sb, bindex)->br_id;
3768 +}
3769 +
3770 +static inline
3771 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
3772 +{
3773 +       return au_br_mnt(au_sbr(sb, bindex));
3774 +}
3775 +
3776 +static inline
3777 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
3778 +{
3779 +       return au_br_sb(au_sbr(sb, bindex));
3780 +}
3781 +
3782 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
3783 +{
3784 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
3785 +}
3786 +
3787 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
3788 +{
3789 +       return au_sbr(sb, bindex)->br_perm;
3790 +}
3791 +
3792 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
3793 +{
3794 +       return au_br_whable(au_sbr_perm(sb, bindex));
3795 +}
3796 +
3797 +/* ---------------------------------------------------------------------- */
3798 +
3799 +/*
3800 + * wbr_wh_read_lock, wbr_wh_write_lock
3801 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
3802 + */
3803 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
3804 +
3805 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
3806 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
3807 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
3808 +
3809 +#endif /* __KERNEL__ */
3810 +#endif /* __AUFS_BRANCH_H__ */
3811 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
3812 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
3813 +++ linux/fs/aufs/conf.mk       2014-06-24 00:14:06.776052275 +0200
3814 @@ -0,0 +1,36 @@
3815 +
3816 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
3817 +
3818 +define AuConf
3819 +ifdef ${1}
3820 +AuConfStr += ${1}=${${1}}
3821 +endif
3822 +endef
3823 +
3824 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
3825 +       SBILIST \
3826 +       HNOTIFY HFSNOTIFY \
3827 +       EXPORT INO_T_64 \
3828 +       RDU \
3829 +       SHWH \
3830 +       BR_RAMFS \
3831 +       BR_FUSE POLL \
3832 +       BR_HFSPLUS \
3833 +       BDEV_LOOP \
3834 +       DEBUG MAGIC_SYSRQ
3835 +$(foreach i, ${AuConfAll}, \
3836 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
3837 +
3838 +AuConfName = ${obj}/conf.str
3839 +${AuConfName}.tmp: FORCE
3840 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
3841 +${AuConfName}: ${AuConfName}.tmp
3842 +       @diff -q $< $@ > /dev/null 2>&1 || { \
3843 +       echo '  GEN    ' $@; \
3844 +       cp -p $< $@; \
3845 +       }
3846 +FORCE:
3847 +clean-files += ${AuConfName} ${AuConfName}.tmp
3848 +${obj}/sysfs.o: ${AuConfName}
3849 +
3850 +-include ${srctree}/${src}/conf_priv.mk
3851 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
3852 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
3853 +++ linux/fs/aufs/cpup.c        2014-06-24 00:14:06.779385790 +0200
3854 @@ -0,0 +1,1289 @@
3855 +/*
3856 + * Copyright (C) 2005-2014 Junjiro R. Okajima
3857 + *
3858 + * This program, aufs is free software; you can redistribute it and/or modify
3859 + * it under the terms of the GNU General Public License as published by
3860 + * the Free Software Foundation; either version 2 of the License, or
3861 + * (at your option) any later version.
3862 + *
3863 + * This program is distributed in the hope that it will be useful,
3864 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3865 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3866 + * GNU General Public License for more details.
3867 + *
3868 + * You should have received a copy of the GNU General Public License
3869 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3870 + */
3871 +
3872 +/*
3873 + * copy-up functions, see wbr_policy.c for copy-down
3874 + */
3875 +
3876 +#include <linux/fs_stack.h>
3877 +#include <linux/mm.h>
3878 +#include "aufs.h"
3879 +
3880 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
3881 +{
3882 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
3883 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
3884 +
3885 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
3886 +
3887 +       dst->i_flags |= iflags & ~mask;
3888 +       if (au_test_fs_notime(dst->i_sb))
3889 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
3890 +}
3891 +
3892 +void au_cpup_attr_timesizes(struct inode *inode)
3893 +{
3894 +       struct inode *h_inode;
3895 +
3896 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3897 +       fsstack_copy_attr_times(inode, h_inode);
3898 +       fsstack_copy_inode_size(inode, h_inode);
3899 +}
3900 +
3901 +void au_cpup_attr_nlink(struct inode *inode, int force)
3902 +{
3903 +       struct inode *h_inode;
3904 +       struct super_block *sb;
3905 +       aufs_bindex_t bindex, bend;
3906 +
3907 +       sb = inode->i_sb;
3908 +       bindex = au_ibstart(inode);
3909 +       h_inode = au_h_iptr(inode, bindex);
3910 +       if (!force
3911 +           && !S_ISDIR(h_inode->i_mode)
3912 +           && au_opt_test(au_mntflags(sb), PLINK)
3913 +           && au_plink_test(inode))
3914 +               return;
3915 +
3916 +       /*
3917 +        * 0 can happen in revalidating.
3918 +        * h_inode->i_mutex may not be held here, but it is harmless since once
3919 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
3920 +        * case.
3921 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
3922 +        *       the incorrect link count.
3923 +        */
3924 +       set_nlink(inode, h_inode->i_nlink);
3925 +
3926 +       /*
3927 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
3928 +        * it may includes whplink directory.
3929 +        */
3930 +       if (S_ISDIR(h_inode->i_mode)) {
3931 +               bend = au_ibend(inode);
3932 +               for (bindex++; bindex <= bend; bindex++) {
3933 +                       h_inode = au_h_iptr(inode, bindex);
3934 +                       if (h_inode)
3935 +                               au_add_nlink(inode, h_inode);
3936 +               }
3937 +       }
3938 +}
3939 +
3940 +void au_cpup_attr_changeable(struct inode *inode)
3941 +{
3942 +       struct inode *h_inode;
3943 +
3944 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3945 +       inode->i_mode = h_inode->i_mode;
3946 +       inode->i_uid = h_inode->i_uid;
3947 +       inode->i_gid = h_inode->i_gid;
3948 +       au_cpup_attr_timesizes(inode);
3949 +       au_cpup_attr_flags(inode, h_inode->i_flags);
3950 +}
3951 +
3952 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
3953 +{
3954 +       struct au_iinfo *iinfo = au_ii(inode);
3955 +
3956 +       IiMustWriteLock(inode);
3957 +
3958 +       iinfo->ii_higen = h_inode->i_generation;
3959 +       iinfo->ii_hsb1 = h_inode->i_sb;
3960 +}
3961 +
3962 +void au_cpup_attr_all(struct inode *inode, int force)
3963 +{
3964 +       struct inode *h_inode;
3965 +
3966 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
3967 +       au_cpup_attr_changeable(inode);
3968 +       if (inode->i_nlink > 0)
3969 +               au_cpup_attr_nlink(inode, force);
3970 +       inode->i_rdev = h_inode->i_rdev;
3971 +       inode->i_blkbits = h_inode->i_blkbits;
3972 +       au_cpup_igen(inode, h_inode);
3973 +}
3974 +
3975 +/* ---------------------------------------------------------------------- */
3976 +
3977 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
3978 +
3979 +/* keep the timestamps of the parent dir when cpup */
3980 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
3981 +                   struct path *h_path)
3982 +{
3983 +       struct inode *h_inode;
3984 +
3985 +       dt->dt_dentry = dentry;
3986 +       dt->dt_h_path = *h_path;
3987 +       h_inode = h_path->dentry->d_inode;
3988 +       dt->dt_atime = h_inode->i_atime;
3989 +       dt->dt_mtime = h_inode->i_mtime;
3990 +       /* smp_mb(); */
3991 +}
3992 +
3993 +void au_dtime_revert(struct au_dtime *dt)
3994 +{
3995 +       struct iattr attr;
3996 +       int err;
3997 +
3998 +       attr.ia_atime = dt->dt_atime;
3999 +       attr.ia_mtime = dt->dt_mtime;
4000 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4001 +               | ATTR_ATIME | ATTR_ATIME_SET;
4002 +
4003 +       /* no delegation since this is a directory */
4004 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4005 +       if (unlikely(err))
4006 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4007 +}
4008 +
4009 +/* ---------------------------------------------------------------------- */
4010 +
4011 +/* internal use only */
4012 +struct au_cpup_reg_attr {
4013 +       int             valid;
4014 +       struct kstat    st;
4015 +       unsigned int    iflags; /* inode->i_flags */
4016 +};
4017 +
4018 +static noinline_for_stack
4019 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4020 +              struct au_cpup_reg_attr *h_src_attr)
4021 +{
4022 +       int err, sbits;
4023 +       struct iattr ia;
4024 +       struct path h_path;
4025 +       struct inode *h_isrc, *h_idst;
4026 +       struct kstat *h_st;
4027 +
4028 +       h_path.dentry = au_h_dptr(dst, bindex);
4029 +       h_idst = h_path.dentry->d_inode;
4030 +       h_path.mnt = au_sbr_mnt(dst->d_sb, bindex);
4031 +       h_isrc = h_src->d_inode;
4032 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4033 +               | ATTR_ATIME | ATTR_MTIME
4034 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4035 +       if (h_src_attr && h_src_attr->valid) {
4036 +               h_st = &h_src_attr->st;
4037 +               ia.ia_uid = h_st->uid;
4038 +               ia.ia_gid = h_st->gid;
4039 +               ia.ia_atime = h_st->atime;
4040 +               ia.ia_mtime = h_st->mtime;
4041 +               if (h_idst->i_mode != h_st->mode
4042 +                   && !S_ISLNK(h_idst->i_mode)) {
4043 +                       ia.ia_valid |= ATTR_MODE;
4044 +                       ia.ia_mode = h_st->mode;
4045 +               }
4046 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4047 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4048 +       } else {
4049 +               ia.ia_uid = h_isrc->i_uid;
4050 +               ia.ia_gid = h_isrc->i_gid;
4051 +               ia.ia_atime = h_isrc->i_atime;
4052 +               ia.ia_mtime = h_isrc->i_mtime;
4053 +               if (h_idst->i_mode != h_isrc->i_mode
4054 +                   && !S_ISLNK(h_idst->i_mode)) {
4055 +                       ia.ia_valid |= ATTR_MODE;
4056 +                       ia.ia_mode = h_isrc->i_mode;
4057 +               }
4058 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4059 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4060 +       }
4061 +       /* no delegation since it is just created */
4062 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4063 +
4064 +       /* is this nfs only? */
4065 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4066 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4067 +               ia.ia_mode = h_isrc->i_mode;
4068 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4069 +       }
4070 +
4071 +       return err;
4072 +}
4073 +
4074 +/* ---------------------------------------------------------------------- */
4075 +
4076 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4077 +                          char *buf, unsigned long blksize)
4078 +{
4079 +       int err;
4080 +       size_t sz, rbytes, wbytes;
4081 +       unsigned char all_zero;
4082 +       char *p, *zp;
4083 +       struct mutex *h_mtx;
4084 +       /* reduce stack usage */
4085 +       struct iattr *ia;
4086 +
4087 +       zp = page_address(ZERO_PAGE(0));
4088 +       if (unlikely(!zp))
4089 +               return -ENOMEM; /* possible? */
4090 +
4091 +       err = 0;
4092 +       all_zero = 0;
4093 +       while (len) {
4094 +               AuDbg("len %lld\n", len);
4095 +               sz = blksize;
4096 +               if (len < blksize)
4097 +                       sz = len;
4098 +
4099 +               rbytes = 0;
4100 +               /* todo: signal_pending? */
4101 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4102 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4103 +                       err = rbytes;
4104 +               }
4105 +               if (unlikely(err < 0))
4106 +                       break;
4107 +
4108 +               all_zero = 0;
4109 +               if (len >= rbytes && rbytes == blksize)
4110 +                       all_zero = !memcmp(buf, zp, rbytes);
4111 +               if (!all_zero) {
4112 +                       wbytes = rbytes;
4113 +                       p = buf;
4114 +                       while (wbytes) {
4115 +                               size_t b;
4116 +
4117 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4118 +                               err = b;
4119 +                               /* todo: signal_pending? */
4120 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
4121 +                                       continue;
4122 +                               if (unlikely(err < 0))
4123 +                                       break;
4124 +                               wbytes -= b;
4125 +                               p += b;
4126 +                       }
4127 +                       if (unlikely(err < 0))
4128 +                               break;
4129 +               } else {
4130 +                       loff_t res;
4131 +
4132 +                       AuLabel(hole);
4133 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4134 +                       err = res;
4135 +                       if (unlikely(res < 0))
4136 +                               break;
4137 +               }
4138 +               len -= rbytes;
4139 +               err = 0;
4140 +       }
4141 +
4142 +       /* the last block may be a hole */
4143 +       if (!err && all_zero) {
4144 +               AuLabel(last hole);
4145 +
4146 +               err = 1;
4147 +               if (au_test_nfs(dst->f_dentry->d_sb)) {
4148 +                       /* nfs requires this step to make last hole */
4149 +                       /* is this only nfs? */
4150 +                       do {
4151 +                               /* todo: signal_pending? */
4152 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4153 +                       } while (err == -EAGAIN || err == -EINTR);
4154 +                       if (err == 1)
4155 +                               dst->f_pos--;
4156 +               }
4157 +
4158 +               if (err == 1) {
4159 +                       ia = (void *)buf;
4160 +                       ia->ia_size = dst->f_pos;
4161 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4162 +                       ia->ia_file = dst;
4163 +                       h_mtx = &file_inode(dst)->i_mutex;
4164 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
4165 +                       /* no delegation since it is just created */
4166 +                       err = vfsub_notify_change(&dst->f_path, ia,
4167 +                                                 /*delegated*/NULL);
4168 +                       mutex_unlock(h_mtx);
4169 +               }
4170 +       }
4171 +
4172 +       return err;
4173 +}
4174 +
4175 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
4176 +{
4177 +       int err;
4178 +       unsigned long blksize;
4179 +       unsigned char do_kfree;
4180 +       char *buf;
4181 +
4182 +       err = -ENOMEM;
4183 +       blksize = dst->f_dentry->d_sb->s_blocksize;
4184 +       if (!blksize || PAGE_SIZE < blksize)
4185 +               blksize = PAGE_SIZE;
4186 +       AuDbg("blksize %lu\n", blksize);
4187 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4188 +       if (do_kfree)
4189 +               buf = kmalloc(blksize, GFP_NOFS);
4190 +       else
4191 +               buf = (void *)__get_free_page(GFP_NOFS);
4192 +       if (unlikely(!buf))
4193 +               goto out;
4194 +
4195 +       if (len > (1 << 22))
4196 +               AuDbg("copying a large file %lld\n", (long long)len);
4197 +
4198 +       src->f_pos = 0;
4199 +       dst->f_pos = 0;
4200 +       err = au_do_copy_file(dst, src, len, buf, blksize);
4201 +       if (do_kfree)
4202 +               kfree(buf);
4203 +       else
4204 +               free_page((unsigned long)buf);
4205 +
4206 +out:
4207 +       return err;
4208 +}
4209 +
4210 +/*
4211 + * to support a sparse file which is opened with O_APPEND,
4212 + * we need to close the file.
4213 + */
4214 +static int au_cp_regular(struct au_cp_generic *cpg)
4215 +{
4216 +       int err, i;
4217 +       enum { SRC, DST };
4218 +       struct {
4219 +               aufs_bindex_t bindex;
4220 +               unsigned int flags;
4221 +               struct dentry *dentry;
4222 +               int force_wr;
4223 +               struct file *file;
4224 +               void *label;
4225 +       } *f, file[] = {
4226 +               {
4227 +                       .bindex = cpg->bsrc,
4228 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
4229 +                       .label = &&out
4230 +               },
4231 +               {
4232 +                       .bindex = cpg->bdst,
4233 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
4234 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
4235 +                       .label = &&out_src
4236 +               }
4237 +       };
4238 +       struct super_block *sb;
4239 +
4240 +       /* bsrc branch can be ro/rw. */
4241 +       sb = cpg->dentry->d_sb;
4242 +       f = file;
4243 +       for (i = 0; i < 2; i++, f++) {
4244 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
4245 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
4246 +                                   /*file*/NULL, f->force_wr);
4247 +               err = PTR_ERR(f->file);
4248 +               if (IS_ERR(f->file))
4249 +                       goto *f->label;
4250 +       }
4251 +
4252 +       /* try stopping to update while we copyup */
4253 +       IMustLock(file[SRC].dentry->d_inode);
4254 +       err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
4255 +
4256 +       fput(file[DST].file);
4257 +       au_sbr_put(sb, file[DST].bindex);
4258 +
4259 +out_src:
4260 +       fput(file[SRC].file);
4261 +       au_sbr_put(sb, file[SRC].bindex);
4262 +out:
4263 +       return err;
4264 +}
4265 +
4266 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
4267 +                             struct au_cpup_reg_attr *h_src_attr)
4268 +{
4269 +       int err, rerr;
4270 +       loff_t l;
4271 +       struct path h_path;
4272 +       struct inode *h_src_inode, *h_dst_inode;
4273 +
4274 +       err = 0;
4275 +       h_src_inode = au_h_iptr(cpg->dentry->d_inode, cpg->bsrc);
4276 +       l = i_size_read(h_src_inode);
4277 +       if (cpg->len == -1 || l < cpg->len)
4278 +               cpg->len = l;
4279 +       if (cpg->len) {
4280 +               /* try stopping to update while we are referencing */
4281 +               mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
4282 +               au_pin_hdir_unlock(cpg->pin);
4283 +
4284 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
4285 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
4286 +               h_src_attr->iflags = h_src_inode->i_flags;
4287 +               err = vfs_getattr(&h_path, &h_src_attr->st);
4288 +               if (unlikely(err)) {
4289 +                       mutex_unlock(&h_src_inode->i_mutex);
4290 +                       goto out;
4291 +               }
4292 +               h_src_attr->valid = 1;
4293 +               err = au_cp_regular(cpg);
4294 +               mutex_unlock(&h_src_inode->i_mutex);
4295 +               rerr = au_pin_hdir_relock(cpg->pin);
4296 +               if (!err && rerr)
4297 +                       err = rerr;
4298 +       }
4299 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
4300 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
4301 +               h_dst_inode = h_path.dentry->d_inode;
4302 +               spin_lock(&h_dst_inode->i_lock);
4303 +               h_dst_inode->i_state |= I_LINKABLE;
4304 +               spin_unlock(&h_dst_inode->i_lock);
4305 +       }
4306 +
4307 +out:
4308 +       return err;
4309 +}
4310 +
4311 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
4312 +                             struct inode *h_dir)
4313 +{
4314 +       int err, symlen;
4315 +       mm_segment_t old_fs;
4316 +       union {
4317 +               char *k;
4318 +               char __user *u;
4319 +       } sym;
4320 +
4321 +       err = -ENOSYS;
4322 +       if (unlikely(!h_src->d_inode->i_op->readlink))
4323 +               goto out;
4324 +
4325 +       err = -ENOMEM;
4326 +       sym.k = (void *)__get_free_page(GFP_NOFS);
4327 +       if (unlikely(!sym.k))
4328 +               goto out;
4329 +
4330 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
4331 +       old_fs = get_fs();
4332 +       set_fs(KERNEL_DS);
4333 +       symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
4334 +       err = symlen;
4335 +       set_fs(old_fs);
4336 +
4337 +       if (symlen > 0) {
4338 +               sym.k[symlen] = 0;
4339 +               err = vfsub_symlink(h_dir, h_path, sym.k);
4340 +       }
4341 +       free_page((unsigned long)sym.k);
4342 +
4343 +out:
4344 +       return err;
4345 +}
4346 +
4347 +static noinline_for_stack
4348 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
4349 +              struct au_cpup_reg_attr *h_src_attr)
4350 +{
4351 +       int err;
4352 +       umode_t mode;
4353 +       unsigned int mnt_flags;
4354 +       unsigned char isdir;
4355 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
4356 +       struct au_dtime dt;
4357 +       struct path h_path;
4358 +       struct dentry *h_src, *h_dst, *h_parent;
4359 +       struct inode *h_inode, *h_dir;
4360 +       struct super_block *sb;
4361 +
4362 +       /* bsrc branch can be ro/rw. */
4363 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
4364 +       h_inode = h_src->d_inode;
4365 +       AuDebugOn(h_inode != au_h_iptr(cpg->dentry->d_inode, cpg->bsrc));
4366 +
4367 +       /* try stopping to be referenced while we are creating */
4368 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
4369 +       if (au_ftest_cpup(cpg->flags, RENAME))
4370 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
4371 +                                 AUFS_WH_PFX_LEN));
4372 +       h_parent = h_dst->d_parent; /* dir inode is locked */
4373 +       h_dir = h_parent->d_inode;
4374 +       IMustLock(h_dir);
4375 +       AuDebugOn(h_parent != h_dst->d_parent);
4376 +
4377 +       sb = cpg->dentry->d_sb;
4378 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
4379 +       if (do_dt) {
4380 +               h_path.dentry = h_parent;
4381 +               au_dtime_store(&dt, dst_parent, &h_path);
4382 +       }
4383 +       h_path.dentry = h_dst;
4384 +
4385 +       isdir = 0;
4386 +       mode = h_inode->i_mode;
4387 +       switch (mode & S_IFMT) {
4388 +       case S_IFREG:
4389 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
4390 +                                  /*want_excl*/true);
4391 +               if (!err)
4392 +                       err = au_do_cpup_regular(cpg, h_src_attr);
4393 +               break;
4394 +       case S_IFDIR:
4395 +               isdir = 1;
4396 +               err = vfsub_mkdir(h_dir, &h_path, mode);
4397 +               if (!err) {
4398 +                       /*
4399 +                        * strange behaviour from the users view,
4400 +                        * particularry setattr case
4401 +                        */
4402 +                       if (au_ibstart(dst_parent->d_inode) == cpg->bdst)
4403 +                               au_cpup_attr_nlink(dst_parent->d_inode,
4404 +                                                  /*force*/1);
4405 +                       au_cpup_attr_nlink(cpg->dentry->d_inode, /*force*/1);
4406 +               }
4407 +               break;
4408 +       case S_IFLNK:
4409 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
4410 +               break;
4411 +       case S_IFCHR:
4412 +       case S_IFBLK:
4413 +               AuDebugOn(!capable(CAP_MKNOD));
4414 +               /*FALLTHROUGH*/
4415 +       case S_IFIFO:
4416 +       case S_IFSOCK:
4417 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
4418 +               break;
4419 +       default:
4420 +               AuIOErr("Unknown inode type 0%o\n", mode);
4421 +               err = -EIO;
4422 +       }
4423 +
4424 +       mnt_flags = au_mntflags(sb);
4425 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
4426 +           && !isdir
4427 +           && au_opt_test(mnt_flags, XINO)
4428 +           && (h_inode->i_nlink == 1
4429 +               || (h_inode->i_state & I_LINKABLE))
4430 +           /* todo: unnecessary? */
4431 +           /* && cpg->dentry->d_inode->i_nlink == 1 */
4432 +           && cpg->bdst < cpg->bsrc
4433 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
4434 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
4435 +               /* ignore this error */
4436 +
4437 +       if (do_dt)
4438 +               au_dtime_revert(&dt);
4439 +       return err;
4440 +}
4441 +
4442 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
4443 +{
4444 +       int err;
4445 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
4446 +       struct inode *h_dir;
4447 +       aufs_bindex_t bdst;
4448 +
4449 +       dentry = cpg->dentry;
4450 +       bdst = cpg->bdst;
4451 +       h_dentry = au_h_dptr(dentry, bdst);
4452 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
4453 +               dget(h_dentry);
4454 +               au_set_h_dptr(dentry, bdst, NULL);
4455 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
4456 +               if (!err)
4457 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
4458 +               au_set_h_dptr(dentry, bdst, h_dentry);
4459 +       } else {
4460 +               err = 0;
4461 +               parent = dget_parent(dentry);
4462 +               h_parent = au_h_dptr(parent, bdst);
4463 +               dput(parent);
4464 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
4465 +               if (IS_ERR(h_path->dentry))
4466 +                       err = PTR_ERR(h_path->dentry);
4467 +       }
4468 +       if (unlikely(err))
4469 +               goto out;
4470 +
4471 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
4472 +       h_dir = h_parent->d_inode;
4473 +       IMustLock(h_dir);
4474 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
4475 +       /* no delegation since it is just created */
4476 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
4477 +       dput(h_path->dentry);
4478 +
4479 +out:
4480 +       return err;
4481 +}
4482 +
4483 +/*
4484 + * copyup the @dentry from @bsrc to @bdst.
4485 + * the caller must set the both of lower dentries.
4486 + * @len is for truncating when it is -1 copyup the entire file.
4487 + * in link/rename cases, @dst_parent may be different from the real one.
4488 + * basic->bsrc can be larger than basic->bdst.
4489 + */
4490 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
4491 +{
4492 +       int err, rerr;
4493 +       aufs_bindex_t old_ibstart;
4494 +       unsigned char isdir, plink;
4495 +       struct dentry *h_src, *h_dst, *h_parent;
4496 +       struct inode *dst_inode, *h_dir, *inode, *delegated;
4497 +       struct super_block *sb;
4498 +       struct au_branch *br;
4499 +       /* to reuduce stack size */
4500 +       struct {
4501 +               struct au_dtime dt;
4502 +               struct path h_path;
4503 +               struct au_cpup_reg_attr h_src_attr;
4504 +       } *a;
4505 +
4506 +       err = -ENOMEM;
4507 +       a = kmalloc(sizeof(*a), GFP_NOFS);
4508 +       if (unlikely(!a))
4509 +               goto out;
4510 +       a->h_src_attr.valid = 0;
4511 +
4512 +       sb = cpg->dentry->d_sb;
4513 +       br = au_sbr(sb, cpg->bdst);
4514 +       a->h_path.mnt = au_br_mnt(br);
4515 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
4516 +       h_parent = h_dst->d_parent; /* dir inode is locked */
4517 +       h_dir = h_parent->d_inode;
4518 +       IMustLock(h_dir);
4519 +
4520 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
4521 +       inode = cpg->dentry->d_inode;
4522 +
4523 +       if (!dst_parent)
4524 +               dst_parent = dget_parent(cpg->dentry);
4525 +       else
4526 +               dget(dst_parent);
4527 +
4528 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
4529 +       dst_inode = au_h_iptr(inode, cpg->bdst);
4530 +       if (dst_inode) {
4531 +               if (unlikely(!plink)) {
4532 +                       err = -EIO;
4533 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
4534 +                               "but plink is disabled\n",
4535 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
4536 +                       goto out_parent;
4537 +               }
4538 +
4539 +               if (dst_inode->i_nlink) {
4540 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
4541 +
4542 +                       h_src = au_plink_lkup(inode, cpg->bdst);
4543 +                       err = PTR_ERR(h_src);
4544 +                       if (IS_ERR(h_src))
4545 +                               goto out_parent;
4546 +                       if (unlikely(!h_src->d_inode)) {
4547 +                               err = -EIO;
4548 +                               AuIOErr("i%lu exists on a upper branch "
4549 +                                       "but not pseudo-linked\n",
4550 +                                       inode->i_ino);
4551 +                               dput(h_src);
4552 +                               goto out_parent;
4553 +                       }
4554 +
4555 +                       if (do_dt) {
4556 +                               a->h_path.dentry = h_parent;
4557 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
4558 +                       }
4559 +
4560 +                       a->h_path.dentry = h_dst;
4561 +                       delegated = NULL;
4562 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
4563 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
4564 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
4565 +                       if (do_dt)
4566 +                               au_dtime_revert(&a->dt);
4567 +                       if (unlikely(err == -EWOULDBLOCK)) {
4568 +                               pr_warn("cannot retry for NFSv4 delegation"
4569 +                                       " for an internal link\n");
4570 +                               iput(delegated);
4571 +                       }
4572 +                       dput(h_src);
4573 +                       goto out_parent;
4574 +               } else
4575 +                       /* todo: cpup_wh_file? */
4576 +                       /* udba work */
4577 +                       au_update_ibrange(inode, /*do_put_zero*/1);
4578 +       }
4579 +
4580 +       isdir = S_ISDIR(inode->i_mode);
4581 +       old_ibstart = au_ibstart(inode);
4582 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
4583 +       if (unlikely(err))
4584 +               goto out_rev;
4585 +       dst_inode = h_dst->d_inode;
4586 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
4587 +       /* todo: necessary? */
4588 +       /* au_pin_hdir_unlock(cpg->pin); */
4589 +
4590 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
4591 +       if (unlikely(err)) {
4592 +               /* todo: necessary? */
4593 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
4594 +               mutex_unlock(&dst_inode->i_mutex);
4595 +               goto out_rev;
4596 +       }
4597 +
4598 +       if (cpg->bdst < old_ibstart) {
4599 +               if (S_ISREG(inode->i_mode)) {
4600 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
4601 +                       if (unlikely(err)) {
4602 +                               /* ignore an error */
4603 +                               /* au_pin_hdir_relock(cpg->pin); */
4604 +                               mutex_unlock(&dst_inode->i_mutex);
4605 +                               goto out_rev;
4606 +                       }
4607 +               }
4608 +               au_set_ibstart(inode, cpg->bdst);
4609 +       } else
4610 +               au_set_ibend(inode, cpg->bdst);
4611 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
4612 +                     au_hi_flags(inode, isdir));
4613 +
4614 +       /* todo: necessary? */
4615 +       /* err = au_pin_hdir_relock(cpg->pin); */
4616 +       mutex_unlock(&dst_inode->i_mutex);
4617 +       if (unlikely(err))
4618 +               goto out_rev;
4619 +
4620 +       if (!isdir
4621 +           && (h_src->d_inode->i_nlink > 1
4622 +               || h_src->d_inode->i_state & I_LINKABLE)
4623 +           && plink)
4624 +               au_plink_append(inode, cpg->bdst, h_dst);
4625 +
4626 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
4627 +               a->h_path.dentry = h_dst;
4628 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
4629 +       }
4630 +       if (!err)
4631 +               goto out_parent; /* success */
4632 +
4633 +       /* revert */
4634 +out_rev:
4635 +       a->h_path.dentry = h_parent;
4636 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
4637 +       a->h_path.dentry = h_dst;
4638 +       rerr = 0;
4639 +       if (h_dst->d_inode) {
4640 +               if (!isdir) {
4641 +                       /* no delegation since it is just created */
4642 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
4643 +                                           /*delegated*/NULL, /*force*/0);
4644 +               } else
4645 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
4646 +       }
4647 +       au_dtime_revert(&a->dt);
4648 +       if (rerr) {
4649 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
4650 +               err = -EIO;
4651 +       }
4652 +out_parent:
4653 +       dput(dst_parent);
4654 +       kfree(a);
4655 +out:
4656 +       return err;
4657 +}
4658 +
4659 +#if 0 /* unused */
4660 +struct au_cpup_single_args {
4661 +       int *errp;
4662 +       struct au_cp_generic *cpg;
4663 +       struct dentry *dst_parent;
4664 +};
4665 +
4666 +static void au_call_cpup_single(void *args)
4667 +{
4668 +       struct au_cpup_single_args *a = args;
4669 +
4670 +       au_pin_hdir_acquire_nest(a->cpg->pin);
4671 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
4672 +       au_pin_hdir_release(a->cpg->pin);
4673 +}
4674 +#endif
4675 +
4676 +/*
4677 + * prevent SIGXFSZ in copy-up.
4678 + * testing CAP_MKNOD is for generic fs,
4679 + * but CAP_FSETID is for xfs only, currently.
4680 + */
4681 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
4682 +{
4683 +       int do_sio;
4684 +       struct super_block *sb;
4685 +       struct inode *h_dir;
4686 +
4687 +       do_sio = 0;
4688 +       sb = au_pinned_parent(pin)->d_sb;
4689 +       if (!au_wkq_test()
4690 +           && (!au_sbi(sb)->si_plink_maint_pid
4691 +               || au_plink_maint(sb, AuLock_NOPLM))) {
4692 +               switch (mode & S_IFMT) {
4693 +               case S_IFREG:
4694 +                       /* no condition about RLIMIT_FSIZE and the file size */
4695 +                       do_sio = 1;
4696 +                       break;
4697 +               case S_IFCHR:
4698 +               case S_IFBLK:
4699 +                       do_sio = !capable(CAP_MKNOD);
4700 +                       break;
4701 +               }
4702 +               if (!do_sio)
4703 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
4704 +                                 && !capable(CAP_FSETID));
4705 +               /* this workaround may be removed in the future */
4706 +               if (!do_sio) {
4707 +                       h_dir = au_pinned_h_dir(pin);
4708 +                       do_sio = h_dir->i_mode & S_ISVTX;
4709 +               }
4710 +       }
4711 +
4712 +       return do_sio;
4713 +}
4714 +
4715 +#if 0 /* unused */
4716 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
4717 +{
4718 +       int err, wkq_err;
4719 +       struct dentry *h_dentry;
4720 +
4721 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
4722 +       if (!au_cpup_sio_test(pin, h_dentry->d_inode->i_mode))
4723 +               err = au_cpup_single(cpg, dst_parent);
4724 +       else {
4725 +               struct au_cpup_single_args args = {
4726 +                       .errp           = &err,
4727 +                       .cpg            = cpg,
4728 +                       .dst_parent     = dst_parent
4729 +               };
4730 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
4731 +               if (unlikely(wkq_err))
4732 +                       err = wkq_err;
4733 +       }
4734 +
4735 +       return err;
4736 +}
4737 +#endif
4738 +
4739 +/*
4740 + * copyup the @dentry from the first active lower branch to @bdst,
4741 + * using au_cpup_single().
4742 + */
4743 +static int au_cpup_simple(struct au_cp_generic *cpg)
4744 +{
4745 +       int err;
4746 +       unsigned int flags_orig;
4747 +       struct dentry *dentry;
4748 +
4749 +       AuDebugOn(cpg->bsrc < 0);
4750 +
4751 +       dentry = cpg->dentry;
4752 +       DiMustWriteLock(dentry);
4753 +
4754 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
4755 +       if (!err) {
4756 +               flags_orig = cpg->flags;
4757 +               au_fset_cpup(cpg->flags, RENAME);
4758 +               err = au_cpup_single(cpg, NULL);
4759 +               cpg->flags = flags_orig;
4760 +               if (!err)
4761 +                       return 0; /* success */
4762 +
4763 +               /* revert */
4764 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
4765 +               au_set_dbstart(dentry, cpg->bsrc);
4766 +       }
4767 +
4768 +       return err;
4769 +}
4770 +
4771 +struct au_cpup_simple_args {
4772 +       int *errp;
4773 +       struct au_cp_generic *cpg;
4774 +};
4775 +
4776 +static void au_call_cpup_simple(void *args)
4777 +{
4778 +       struct au_cpup_simple_args *a = args;
4779 +
4780 +       au_pin_hdir_acquire_nest(a->cpg->pin);
4781 +       *a->errp = au_cpup_simple(a->cpg);
4782 +       au_pin_hdir_release(a->cpg->pin);
4783 +}
4784 +
4785 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
4786 +{
4787 +       int err, wkq_err;
4788 +       struct dentry *dentry, *parent;
4789 +       struct file *h_file;
4790 +       struct inode *h_dir;
4791 +
4792 +       dentry = cpg->dentry;
4793 +       h_file = NULL;
4794 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
4795 +               AuDebugOn(cpg->bsrc < 0);
4796 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
4797 +               err = PTR_ERR(h_file);
4798 +               if (IS_ERR(h_file))
4799 +                       goto out;
4800 +       }
4801 +
4802 +       parent = dget_parent(dentry);
4803 +       h_dir = au_h_iptr(parent->d_inode, cpg->bdst);
4804 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
4805 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
4806 +               err = au_cpup_simple(cpg);
4807 +       else {
4808 +               struct au_cpup_simple_args args = {
4809 +                       .errp           = &err,
4810 +                       .cpg            = cpg
4811 +               };
4812 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
4813 +               if (unlikely(wkq_err))
4814 +                       err = wkq_err;
4815 +       }
4816 +
4817 +       dput(parent);
4818 +       if (h_file)
4819 +               au_h_open_post(dentry, cpg->bsrc, h_file);
4820 +
4821 +out:
4822 +       return err;
4823 +}
4824 +
4825 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
4826 +{
4827 +       aufs_bindex_t bsrc, bend;
4828 +       struct dentry *dentry, *h_dentry;
4829 +
4830 +       if (cpg->bsrc < 0) {
4831 +               dentry = cpg->dentry;
4832 +               bend = au_dbend(dentry);
4833 +               for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
4834 +                       h_dentry = au_h_dptr(dentry, bsrc);
4835 +                       if (h_dentry) {
4836 +                               AuDebugOn(!h_dentry->d_inode);
4837 +                               break;
4838 +                       }
4839 +               }
4840 +               AuDebugOn(bsrc > bend);
4841 +               cpg->bsrc = bsrc;
4842 +       }
4843 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
4844 +       return au_do_sio_cpup_simple(cpg);
4845 +}
4846 +
4847 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
4848 +{
4849 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
4850 +       return au_do_sio_cpup_simple(cpg);
4851 +}
4852 +
4853 +/* ---------------------------------------------------------------------- */
4854 +
4855 +/*
4856 + * copyup the deleted file for writing.
4857 + */
4858 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
4859 +                        struct file *file)
4860 +{
4861 +       int err;
4862 +       unsigned int flags_orig;
4863 +       aufs_bindex_t bsrc_orig;
4864 +       struct dentry *h_d_dst, *h_d_start;
4865 +       struct au_dinfo *dinfo;
4866 +       struct au_hdentry *hdp;
4867 +
4868 +       dinfo = au_di(cpg->dentry);
4869 +       AuRwMustWriteLock(&dinfo->di_rwsem);
4870 +
4871 +       bsrc_orig = cpg->bsrc;
4872 +       cpg->bsrc = dinfo->di_bstart;
4873 +       hdp = dinfo->di_hdentry;
4874 +       h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
4875 +       dinfo->di_bstart = cpg->bdst;
4876 +       hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
4877 +       h_d_start = NULL;
4878 +       if (file) {
4879 +               h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
4880 +               hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_dentry;
4881 +       }
4882 +       flags_orig = cpg->flags;
4883 +       cpg->flags = !AuCpup_DTIME;
4884 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
4885 +       cpg->flags = flags_orig;
4886 +       if (file) {
4887 +               if (!err)
4888 +                       err = au_reopen_nondir(file);
4889 +               hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
4890 +       }
4891 +       hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
4892 +       dinfo->di_bstart = cpg->bsrc;
4893 +       cpg->bsrc = bsrc_orig;
4894 +
4895 +       return err;
4896 +}
4897 +
4898 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
4899 +{
4900 +       int err;
4901 +       aufs_bindex_t bdst;
4902 +       struct au_dtime dt;
4903 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
4904 +       struct au_branch *br;
4905 +       struct path h_path;
4906 +
4907 +       dentry = cpg->dentry;
4908 +       bdst = cpg->bdst;
4909 +       br = au_sbr(dentry->d_sb, bdst);
4910 +       parent = dget_parent(dentry);
4911 +       h_parent = au_h_dptr(parent, bdst);
4912 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
4913 +       err = PTR_ERR(wh_dentry);
4914 +       if (IS_ERR(wh_dentry))
4915 +               goto out;
4916 +
4917 +       h_path.dentry = h_parent;
4918 +       h_path.mnt = au_br_mnt(br);
4919 +       au_dtime_store(&dt, parent, &h_path);
4920 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
4921 +       if (unlikely(err))
4922 +               goto out_wh;
4923 +
4924 +       dget(wh_dentry);
4925 +       h_path.dentry = wh_dentry;
4926 +       if (!S_ISDIR(wh_dentry->d_inode->i_mode)) {
4927 +               /* no delegation since it is just created */
4928 +               err = vfsub_unlink(h_parent->d_inode, &h_path,
4929 +                                  /*delegated*/NULL, /*force*/0);
4930 +       } else
4931 +               err = vfsub_rmdir(h_parent->d_inode, &h_path);
4932 +       if (unlikely(err)) {
4933 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
4934 +                       wh_dentry, err);
4935 +               err = -EIO;
4936 +       }
4937 +       au_dtime_revert(&dt);
4938 +       au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
4939 +
4940 +out_wh:
4941 +       dput(wh_dentry);
4942 +out:
4943 +       dput(parent);
4944 +       return err;
4945 +}
4946 +
4947 +struct au_cpup_wh_args {
4948 +       int *errp;
4949 +       struct au_cp_generic *cpg;
4950 +       struct file *file;
4951 +};
4952 +
4953 +static void au_call_cpup_wh(void *args)
4954 +{
4955 +       struct au_cpup_wh_args *a = args;
4956 +
4957 +       au_pin_hdir_acquire_nest(a->cpg->pin);
4958 +       *a->errp = au_cpup_wh(a->cpg, a->file);
4959 +       au_pin_hdir_release(a->cpg->pin);
4960 +}
4961 +
4962 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
4963 +{
4964 +       int err, wkq_err;
4965 +       aufs_bindex_t bdst;
4966 +       struct dentry *dentry, *parent, *h_orph, *h_parent, *h_dentry;
4967 +       struct inode *dir, *h_dir, *h_tmpdir;
4968 +       struct au_wbr *wbr;
4969 +       struct au_pin wh_pin, *pin_orig;
4970 +
4971 +       dentry = cpg->dentry;
4972 +       bdst = cpg->bdst;
4973 +       parent = dget_parent(dentry);
4974 +       dir = parent->d_inode;
4975 +       h_orph = NULL;
4976 +       h_parent = NULL;
4977 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
4978 +       h_tmpdir = h_dir;
4979 +       pin_orig = NULL;
4980 +       if (!h_dir->i_nlink) {
4981 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
4982 +               h_orph = wbr->wbr_orph;
4983 +
4984 +               h_parent = dget(au_h_dptr(parent, bdst));
4985 +               au_set_h_dptr(parent, bdst, dget(h_orph));
4986 +               h_tmpdir = h_orph->d_inode;
4987 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
4988 +
4989 +               if (file)
4990 +                       h_dentry = au_hf_top(file)->f_dentry;
4991 +               else
4992 +                       h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
4993 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
4994 +               /* todo: au_h_open_pre()? */
4995 +
4996 +               pin_orig = cpg->pin;
4997 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
4998 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
4999 +               cpg->pin = &wh_pin;
5000 +       }
5001 +
5002 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5003 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
5004 +               err = au_cpup_wh(cpg, file);
5005 +       else {
5006 +               struct au_cpup_wh_args args = {
5007 +                       .errp   = &err,
5008 +                       .cpg    = cpg,
5009 +                       .file   = file
5010 +               };
5011 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5012 +               if (unlikely(wkq_err))
5013 +                       err = wkq_err;
5014 +       }
5015 +
5016 +       if (h_orph) {
5017 +               mutex_unlock(&h_tmpdir->i_mutex);
5018 +               /* todo: au_h_open_post()? */
5019 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5020 +               au_set_h_dptr(parent, bdst, h_parent);
5021 +               AuDebugOn(!pin_orig);
5022 +               cpg->pin = pin_orig;
5023 +       }
5024 +       iput(h_dir);
5025 +       dput(parent);
5026 +
5027 +       return err;
5028 +}
5029 +
5030 +/* ---------------------------------------------------------------------- */
5031 +
5032 +/*
5033 + * generic routine for both of copy-up and copy-down.
5034 + */
5035 +/* cf. revalidate function in file.c */
5036 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5037 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5038 +                        struct au_pin *pin,
5039 +                        struct dentry *h_parent, void *arg),
5040 +              void *arg)
5041 +{
5042 +       int err;
5043 +       struct au_pin pin;
5044 +       struct dentry *d, *parent, *h_parent, *real_parent;
5045 +
5046 +       err = 0;
5047 +       parent = dget_parent(dentry);
5048 +       if (IS_ROOT(parent))
5049 +               goto out;
5050 +
5051 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5052 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5053 +
5054 +       /* do not use au_dpage */
5055 +       real_parent = parent;
5056 +       while (1) {
5057 +               dput(parent);
5058 +               parent = dget_parent(dentry);
5059 +               h_parent = au_h_dptr(parent, bdst);
5060 +               if (h_parent)
5061 +                       goto out; /* success */
5062 +
5063 +               /* find top dir which is necessary to cpup */
5064 +               do {
5065 +                       d = parent;
5066 +                       dput(parent);
5067 +                       parent = dget_parent(d);
5068 +                       di_read_lock_parent3(parent, !AuLock_IR);
5069 +                       h_parent = au_h_dptr(parent, bdst);
5070 +                       di_read_unlock(parent, !AuLock_IR);
5071 +               } while (!h_parent);
5072 +
5073 +               if (d != real_parent)
5074 +                       di_write_lock_child3(d);
5075 +
5076 +               /* somebody else might create while we were sleeping */
5077 +               if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
5078 +                       if (au_h_dptr(d, bdst))
5079 +                               au_update_dbstart(d);
5080 +
5081 +                       au_pin_set_dentry(&pin, d);
5082 +                       err = au_do_pin(&pin);
5083 +                       if (!err) {
5084 +                               err = cp(d, bdst, &pin, h_parent, arg);
5085 +                               au_unpin(&pin);
5086 +                       }
5087 +               }
5088 +
5089 +               if (d != real_parent)
5090 +                       di_write_unlock(d);
5091 +               if (unlikely(err))
5092 +                       break;
5093 +       }
5094 +
5095 +out:
5096 +       dput(parent);
5097 +       return err;
5098 +}
5099 +
5100 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
5101 +                      struct au_pin *pin,
5102 +                      struct dentry *h_parent __maybe_unused ,
5103 +                      void *arg __maybe_unused)
5104 +{
5105 +       struct au_cp_generic cpg = {
5106 +               .dentry = dentry,
5107 +               .bdst   = bdst,
5108 +               .bsrc   = -1,
5109 +               .len    = 0,
5110 +               .pin    = pin,
5111 +               .flags  = AuCpup_DTIME
5112 +       };
5113 +       return au_sio_cpup_simple(&cpg);
5114 +}
5115 +
5116 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5117 +{
5118 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5119 +}
5120 +
5121 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5122 +{
5123 +       int err;
5124 +       struct dentry *parent;
5125 +       struct inode *dir;
5126 +
5127 +       parent = dget_parent(dentry);
5128 +       dir = parent->d_inode;
5129 +       err = 0;
5130 +       if (au_h_iptr(dir, bdst))
5131 +               goto out;
5132 +
5133 +       di_read_unlock(parent, AuLock_IR);
5134 +       di_write_lock_parent(parent);
5135 +       /* someone else might change our inode while we were sleeping */
5136 +       if (!au_h_iptr(dir, bdst))
5137 +               err = au_cpup_dirs(dentry, bdst);
5138 +       di_downgrade_lock(parent, AuLock_IR);
5139 +
5140 +out:
5141 +       dput(parent);
5142 +       return err;
5143 +}
5144 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
5145 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
5146 +++ linux/fs/aufs/cpup.h        2014-06-24 00:14:06.779385790 +0200
5147 @@ -0,0 +1,94 @@
5148 +/*
5149 + * Copyright (C) 2005-2014 Junjiro R. Okajima
5150 + *
5151 + * This program, aufs is free software; you can redistribute it and/or modify
5152 + * it under the terms of the GNU General Public License as published by
5153 + * the Free Software Foundation; either version 2 of the License, or
5154 + * (at your option) any later version.
5155 + *
5156 + * This program is distributed in the hope that it will be useful,
5157 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5158 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5159 + * GNU General Public License for more details.
5160 + *
5161 + * You should have received a copy of the GNU General Public License
5162 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5163 + */
5164 +
5165 +/*
5166 + * copy-up/down functions
5167 + */
5168 +
5169 +#ifndef __AUFS_CPUP_H__
5170 +#define __AUFS_CPUP_H__
5171 +
5172 +#ifdef __KERNEL__
5173 +
5174 +#include <linux/path.h>
5175 +
5176 +struct inode;
5177 +struct file;
5178 +struct au_pin;
5179 +
5180 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
5181 +void au_cpup_attr_timesizes(struct inode *inode);
5182 +void au_cpup_attr_nlink(struct inode *inode, int force);
5183 +void au_cpup_attr_changeable(struct inode *inode);
5184 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
5185 +void au_cpup_attr_all(struct inode *inode, int force);
5186 +
5187 +/* ---------------------------------------------------------------------- */
5188 +
5189 +struct au_cp_generic {
5190 +       struct dentry   *dentry;
5191 +       aufs_bindex_t   bdst, bsrc;
5192 +       loff_t          len;
5193 +       struct au_pin   *pin;
5194 +       unsigned int    flags;
5195 +};
5196 +
5197 +/* cpup flags */
5198 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
5199 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
5200 +                                                  for link(2) */
5201 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
5202 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
5203 +                                                  cpup */
5204 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
5205 +                                                  existing entry */
5206 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
5207 +                                                  the branch is marked as RO */
5208 +
5209 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
5210 +#define au_fset_cpup(flags, name) \
5211 +       do { (flags) |= AuCpup_##name; } while (0)
5212 +#define au_fclr_cpup(flags, name) \
5213 +       do { (flags) &= ~AuCpup_##name; } while (0)
5214 +
5215 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
5216 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
5217 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
5218 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
5219 +
5220 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5221 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5222 +                        struct au_pin *pin,
5223 +                        struct dentry *h_parent, void *arg),
5224 +              void *arg);
5225 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5226 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5227 +
5228 +/* ---------------------------------------------------------------------- */
5229 +
5230 +/* keep timestamps when copyup */
5231 +struct au_dtime {
5232 +       struct dentry *dt_dentry;
5233 +       struct path dt_h_path;
5234 +       struct timespec dt_atime, dt_mtime;
5235 +};
5236 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5237 +                   struct path *h_path);
5238 +void au_dtime_revert(struct au_dtime *dt);
5239 +
5240 +#endif /* __KERNEL__ */
5241 +#endif /* __AUFS_CPUP_H__ */
5242 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
5243 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
5244 +++ linux/fs/aufs/dbgaufs.c     2014-06-24 00:14:06.779385790 +0200
5245 @@ -0,0 +1,432 @@
5246 +/*
5247 + * Copyright (C) 2005-2014 Junjiro R. Okajima
5248 + *
5249 + * This program, aufs is free software; you can redistribute it and/or modify
5250 + * it under the terms of the GNU General Public License as published by
5251 + * the Free Software Foundation; either version 2 of the License, or
5252 + * (at your option) any later version.
5253 + *
5254 + * This program is distributed in the hope that it will be useful,
5255 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5256 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5257 + * GNU General Public License for more details.
5258 + *
5259 + * You should have received a copy of the GNU General Public License
5260 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5261 + */
5262 +
5263 +/*
5264 + * debugfs interface
5265 + */
5266 +
5267 +#include <linux/debugfs.h>
5268 +#include "aufs.h"
5269 +
5270 +#ifndef CONFIG_SYSFS
5271 +#error DEBUG_FS depends upon SYSFS
5272 +#endif
5273 +
5274 +static struct dentry *dbgaufs;
5275 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
5276 +
5277 +/* 20 is max digits length of ulong 64 */
5278 +struct dbgaufs_arg {
5279 +       int n;
5280 +       char a[20 * 4];
5281 +};
5282 +
5283 +/*
5284 + * common function for all XINO files
5285 + */
5286 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
5287 +                             struct file *file)
5288 +{
5289 +       kfree(file->private_data);
5290 +       return 0;
5291 +}
5292 +
5293 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
5294 +{
5295 +       int err;
5296 +       struct kstat st;
5297 +       struct dbgaufs_arg *p;
5298 +
5299 +       err = -ENOMEM;
5300 +       p = kmalloc(sizeof(*p), GFP_NOFS);
5301 +       if (unlikely(!p))
5302 +               goto out;
5303 +
5304 +       err = 0;
5305 +       p->n = 0;
5306 +       file->private_data = p;
5307 +       if (!xf)
5308 +               goto out;
5309 +
5310 +       err = vfs_getattr(&xf->f_path, &st);
5311 +       if (!err) {
5312 +               if (do_fcnt)
5313 +                       p->n = snprintf
5314 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
5315 +                                (long)file_count(xf), st.blocks, st.blksize,
5316 +                                (long long)st.size);
5317 +               else
5318 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
5319 +                                       st.blocks, st.blksize,
5320 +                                       (long long)st.size);
5321 +               AuDebugOn(p->n >= sizeof(p->a));
5322 +       } else {
5323 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
5324 +               err = 0;
5325 +       }
5326 +
5327 +out:
5328 +       return err;
5329 +
5330 +}
5331 +
5332 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
5333 +                              size_t count, loff_t *ppos)
5334 +{
5335 +       struct dbgaufs_arg *p;
5336 +
5337 +       p = file->private_data;
5338 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
5339 +}
5340 +
5341 +/* ---------------------------------------------------------------------- */
5342 +
5343 +struct dbgaufs_plink_arg {
5344 +       int n;
5345 +       char a[];
5346 +};
5347 +
5348 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
5349 +                                struct file *file)
5350 +{
5351 +       free_page((unsigned long)file->private_data);
5352 +       return 0;
5353 +}
5354 +
5355 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
5356 +{
5357 +       int err, i, limit;
5358 +       unsigned long n, sum;
5359 +       struct dbgaufs_plink_arg *p;
5360 +       struct au_sbinfo *sbinfo;
5361 +       struct super_block *sb;
5362 +       struct au_sphlhead *sphl;
5363 +
5364 +       err = -ENOMEM;
5365 +       p = (void *)get_zeroed_page(GFP_NOFS);
5366 +       if (unlikely(!p))
5367 +               goto out;
5368 +
5369 +       err = -EFBIG;
5370 +       sbinfo = inode->i_private;
5371 +       sb = sbinfo->si_sb;
5372 +       si_noflush_read_lock(sb);
5373 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
5374 +               limit = PAGE_SIZE - sizeof(p->n);
5375 +
5376 +               /* the number of buckets */
5377 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
5378 +               p->n += n;
5379 +               limit -= n;
5380 +
5381 +               sum = 0;
5382 +               for (i = 0, sphl = sbinfo->si_plink;
5383 +                    i < AuPlink_NHASH;
5384 +                    i++, sphl++) {
5385 +                       n = au_sphl_count(sphl);
5386 +                       sum += n;
5387 +
5388 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
5389 +                       p->n += n;
5390 +                       limit -= n;
5391 +                       if (unlikely(limit <= 0))
5392 +                               goto out_free;
5393 +               }
5394 +               p->a[p->n - 1] = '\n';
5395 +
5396 +               /* the sum of plinks */
5397 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
5398 +               p->n += n;
5399 +               limit -= n;
5400 +               if (unlikely(limit <= 0))
5401 +                       goto out_free;
5402 +       } else {
5403 +#define str "1\n0\n0\n"
5404 +               p->n = sizeof(str) - 1;
5405 +               strcpy(p->a, str);
5406 +#undef str
5407 +       }
5408 +       si_read_unlock(sb);
5409 +
5410 +       err = 0;
5411 +       file->private_data = p;
5412 +       goto out; /* success */
5413 +
5414 +out_free:
5415 +       free_page((unsigned long)p);
5416 +out:
5417 +       return err;
5418 +}
5419 +
5420 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
5421 +                                 size_t count, loff_t *ppos)
5422 +{
5423 +       struct dbgaufs_plink_arg *p;
5424 +
5425 +       p = file->private_data;
5426 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
5427 +}
5428 +
5429 +static const struct file_operations dbgaufs_plink_fop = {
5430 +       .owner          = THIS_MODULE,
5431 +       .open           = dbgaufs_plink_open,
5432 +       .release        = dbgaufs_plink_release,
5433 +       .read           = dbgaufs_plink_read
5434 +};
5435 +
5436 +/* ---------------------------------------------------------------------- */
5437 +
5438 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
5439 +{
5440 +       int err;
5441 +       struct au_sbinfo *sbinfo;
5442 +       struct super_block *sb;
5443 +
5444 +       sbinfo = inode->i_private;
5445 +       sb = sbinfo->si_sb;
5446 +       si_noflush_read_lock(sb);
5447 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
5448 +       si_read_unlock(sb);
5449 +       return err;
5450 +}
5451 +
5452 +static const struct file_operations dbgaufs_xib_fop = {
5453 +       .owner          = THIS_MODULE,
5454 +       .open           = dbgaufs_xib_open,
5455 +       .release        = dbgaufs_xi_release,
5456 +       .read           = dbgaufs_xi_read
5457 +};
5458 +
5459 +/* ---------------------------------------------------------------------- */
5460 +
5461 +#define DbgaufsXi_PREFIX "xi"
5462 +
5463 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
5464 +{
5465 +       int err;
5466 +       long l;
5467 +       struct au_sbinfo *sbinfo;
5468 +       struct super_block *sb;
5469 +       struct file *xf;
5470 +       struct qstr *name;
5471 +
5472 +       err = -ENOENT;
5473 +       xf = NULL;
5474 +       name = &file->f_dentry->d_name;
5475 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
5476 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
5477 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
5478 +               goto out;
5479 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
5480 +       if (unlikely(err))
5481 +               goto out;
5482 +
5483 +       sbinfo = inode->i_private;
5484 +       sb = sbinfo->si_sb;
5485 +       si_noflush_read_lock(sb);
5486 +       if (l <= au_sbend(sb)) {
5487 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
5488 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
5489 +       } else
5490 +               err = -ENOENT;
5491 +       si_read_unlock(sb);
5492 +
5493 +out:
5494 +       return err;
5495 +}
5496 +
5497 +static const struct file_operations dbgaufs_xino_fop = {
5498 +       .owner          = THIS_MODULE,
5499 +       .open           = dbgaufs_xino_open,
5500 +       .release        = dbgaufs_xi_release,
5501 +       .read           = dbgaufs_xi_read
5502 +};
5503 +
5504 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
5505 +{
5506 +       aufs_bindex_t bend;
5507 +       struct au_branch *br;
5508 +       struct au_xino_file *xi;
5509 +
5510 +       if (!au_sbi(sb)->si_dbgaufs)
5511 +               return;
5512 +
5513 +       bend = au_sbend(sb);
5514 +       for (; bindex <= bend; bindex++) {
5515 +               br = au_sbr(sb, bindex);
5516 +               xi = &br->br_xino;
5517 +               debugfs_remove(xi->xi_dbgaufs);
5518 +               xi->xi_dbgaufs = NULL;
5519 +       }
5520 +}
5521 +
5522 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
5523 +{
5524 +       struct au_sbinfo *sbinfo;
5525 +       struct dentry *parent;
5526 +       struct au_branch *br;
5527 +       struct au_xino_file *xi;
5528 +       aufs_bindex_t bend;
5529 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
5530 +
5531 +       sbinfo = au_sbi(sb);
5532 +       parent = sbinfo->si_dbgaufs;
5533 +       if (!parent)
5534 +               return;
5535 +
5536 +       bend = au_sbend(sb);
5537 +       for (; bindex <= bend; bindex++) {
5538 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
5539 +               br = au_sbr(sb, bindex);
5540 +               xi = &br->br_xino;
5541 +               AuDebugOn(xi->xi_dbgaufs);
5542 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
5543 +                                                    sbinfo, &dbgaufs_xino_fop);
5544 +               /* ignore an error */
5545 +               if (unlikely(!xi->xi_dbgaufs))
5546 +                       AuWarn1("failed %s under debugfs\n", name);
5547 +       }
5548 +}
5549 +
5550 +/* ---------------------------------------------------------------------- */
5551 +
5552 +#ifdef CONFIG_AUFS_EXPORT
5553 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
5554 +{
5555 +       int err;
5556 +       struct au_sbinfo *sbinfo;
5557 +       struct super_block *sb;
5558 +
5559 +       sbinfo = inode->i_private;
5560 +       sb = sbinfo->si_sb;
5561 +       si_noflush_read_lock(sb);
5562 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
5563 +       si_read_unlock(sb);
5564 +       return err;
5565 +}
5566 +
5567 +static const struct file_operations dbgaufs_xigen_fop = {
5568 +       .owner          = THIS_MODULE,
5569 +       .open           = dbgaufs_xigen_open,
5570 +       .release        = dbgaufs_xi_release,
5571 +       .read           = dbgaufs_xi_read
5572 +};
5573 +
5574 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
5575 +{
5576 +       int err;
5577 +
5578 +       /*
5579 +        * This function is a dynamic '__init' fucntion actually,
5580 +        * so the tiny check for si_rwsem is unnecessary.
5581 +        */
5582 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
5583 +
5584 +       err = -EIO;
5585 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
5586 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5587 +                &dbgaufs_xigen_fop);
5588 +       if (sbinfo->si_dbgaufs_xigen)
5589 +               err = 0;
5590 +
5591 +       return err;
5592 +}
5593 +#else
5594 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
5595 +{
5596 +       return 0;
5597 +}
5598 +#endif /* CONFIG_AUFS_EXPORT */
5599 +
5600 +/* ---------------------------------------------------------------------- */
5601 +
5602 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
5603 +{
5604 +       /*
5605 +        * This function is a dynamic '__init' fucntion actually,
5606 +        * so the tiny check for si_rwsem is unnecessary.
5607 +        */
5608 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
5609 +
5610 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
5611 +       sbinfo->si_dbgaufs = NULL;
5612 +       kobject_put(&sbinfo->si_kobj);
5613 +}
5614 +
5615 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
5616 +{
5617 +       int err;
5618 +       char name[SysaufsSiNameLen];
5619 +
5620 +       /*
5621 +        * This function is a dynamic '__init' fucntion actually,
5622 +        * so the tiny check for si_rwsem is unnecessary.
5623 +        */
5624 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
5625 +
5626 +       err = -ENOENT;
5627 +       if (!dbgaufs) {
5628 +               AuErr1("/debug/aufs is uninitialized\n");
5629 +               goto out;
5630 +       }
5631 +
5632 +       err = -EIO;
5633 +       sysaufs_name(sbinfo, name);
5634 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
5635 +       if (unlikely(!sbinfo->si_dbgaufs))
5636 +               goto out;
5637 +       kobject_get(&sbinfo->si_kobj);
5638 +
5639 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
5640 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5641 +                &dbgaufs_xib_fop);
5642 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
5643 +               goto out_dir;
5644 +
5645 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
5646 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
5647 +                &dbgaufs_plink_fop);
5648 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
5649 +               goto out_dir;
5650 +
5651 +       err = dbgaufs_xigen_init(sbinfo);
5652 +       if (!err)
5653 +               goto out; /* success */
5654 +
5655 +out_dir:
5656 +       dbgaufs_si_fin(sbinfo);
5657 +out:
5658 +       return err;
5659 +}
5660 +
5661 +/* ---------------------------------------------------------------------- */
5662 +
5663 +void dbgaufs_fin(void)
5664 +{
5665 +       debugfs_remove(dbgaufs);
5666 +}
5667 +
5668 +int __init dbgaufs_init(void)
5669 +{
5670 +       int err;
5671 +
5672 +       err = -EIO;
5673 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
5674 +       if (dbgaufs)
5675 +               err = 0;
5676 +       return err;
5677 +}
5678 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
5679 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
5680 +++ linux/fs/aufs/dbgaufs.h     2014-06-24 00:14:06.779385790 +0200
5681 @@ -0,0 +1,48 @@
5682 +/*
5683 + * Copyright (C) 2005-2014 Junjiro R. Okajima
5684 + *
5685 + * This program, aufs is free software; you can redistribute it and/or modify
5686 + * it under the terms of the GNU General Public License as published by
5687 + * the Free Software Foundation; either version 2 of the License, or
5688 + * (at your option) any later version.
5689 + *
5690 + * This program is distributed in the hope that it will be useful,
5691 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5692 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5693 + * GNU General Public License for more details.
5694 + *
5695 + * You should have received a copy of the GNU General Public License
5696 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5697 + */
5698 +
5699 +/*
5700 + * debugfs interface
5701 + */
5702 +
5703 +#ifndef __DBGAUFS_H__
5704 +#define __DBGAUFS_H__
5705 +
5706 +#ifdef __KERNEL__
5707 +
5708 +struct super_block;
5709 +struct au_sbinfo;
5710 +
5711 +#ifdef CONFIG_DEBUG_FS
5712 +/* dbgaufs.c */
5713 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
5714 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
5715 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
5716 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
5717 +void dbgaufs_fin(void);
5718 +int __init dbgaufs_init(void);
5719 +#else
5720 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
5721 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
5722 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
5723 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
5724 +AuStubVoid(dbgaufs_fin, void)
5725 +AuStubInt0(__init dbgaufs_init, void)
5726 +#endif /* CONFIG_DEBUG_FS */
5727 +
5728 +#endif /* __KERNEL__ */
5729 +#endif /* __DBGAUFS_H__ */
5730 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
5731 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
5732 +++ linux/fs/aufs/dcsub.c       2014-06-24 00:14:06.779385790 +0200
5733 @@ -0,0 +1,243 @@
5734 +/*
5735 + * Copyright (C) 2005-2014 Junjiro R. Okajima
5736 + *
5737 + * This program, aufs is free software; you can redistribute it and/or modify
5738 + * it under the terms of the GNU General Public License as published by
5739 + * the Free Software Foundation; either version 2 of the License, or
5740 + * (at your option) any later version.
5741 + *
5742 + * This program is distributed in the hope that it will be useful,
5743 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5744 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5745 + * GNU General Public License for more details.
5746 + *
5747 + * You should have received a copy of the GNU General Public License
5748 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5749 + */
5750 +
5751 +/*
5752 + * sub-routines for dentry cache
5753 + */
5754 +
5755 +#include "aufs.h"
5756 +
5757 +static void au_dpage_free(struct au_dpage *dpage)
5758 +{
5759 +       int i;
5760 +       struct dentry **p;
5761 +
5762 +       p = dpage->dentries;
5763 +       for (i = 0; i < dpage->ndentry; i++)
5764 +               dput(*p++);
5765 +       free_page((unsigned long)dpage->dentries);
5766 +}
5767 +
5768 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
5769 +{
5770 +       int err;
5771 +       void *p;
5772 +
5773 +       err = -ENOMEM;
5774 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
5775 +       if (unlikely(!dpages->dpages))
5776 +               goto out;
5777 +
5778 +       p = (void *)__get_free_page(gfp);
5779 +       if (unlikely(!p))
5780 +               goto out_dpages;
5781 +
5782 +       dpages->dpages[0].ndentry = 0;
5783 +       dpages->dpages[0].dentries = p;
5784 +       dpages->ndpage = 1;
5785 +       return 0; /* success */
5786 +
5787 +out_dpages:
5788 +       kfree(dpages->dpages);
5789 +out:
5790 +       return err;
5791 +}
5792 +
5793 +void au_dpages_free(struct au_dcsub_pages *dpages)
5794 +{
5795 +       int i;
5796 +       struct au_dpage *p;
5797 +
5798 +       p = dpages->dpages;
5799 +       for (i = 0; i < dpages->ndpage; i++)
5800 +               au_dpage_free(p++);
5801 +       kfree(dpages->dpages);
5802 +}
5803 +
5804 +static int au_dpages_append(struct au_dcsub_pages *dpages,
5805 +                           struct dentry *dentry, gfp_t gfp)
5806 +{
5807 +       int err, sz;
5808 +       struct au_dpage *dpage;
5809 +       void *p;
5810 +
5811 +       dpage = dpages->dpages + dpages->ndpage - 1;
5812 +       sz = PAGE_SIZE / sizeof(dentry);
5813 +       if (unlikely(dpage->ndentry >= sz)) {
5814 +               AuLabel(new dpage);
5815 +               err = -ENOMEM;
5816 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
5817 +               p = au_kzrealloc(dpages->dpages, sz,
5818 +                                sz + sizeof(*dpages->dpages), gfp);
5819 +               if (unlikely(!p))
5820 +                       goto out;
5821 +
5822 +               dpages->dpages = p;
5823 +               dpage = dpages->dpages + dpages->ndpage;
5824 +               p = (void *)__get_free_page(gfp);
5825 +               if (unlikely(!p))
5826 +                       goto out;
5827 +
5828 +               dpage->ndentry = 0;
5829 +               dpage->dentries = p;
5830 +               dpages->ndpage++;
5831 +       }
5832 +
5833 +       AuDebugOn(!d_count(dentry));
5834 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
5835 +       return 0; /* success */
5836 +
5837 +out:
5838 +       return err;
5839 +}
5840 +
5841 +/* try d_walk() in linux/fs/dcache.c */
5842 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
5843 +                  au_dpages_test test, void *arg)
5844 +{
5845 +       int err;
5846 +       struct dentry *this_parent;
5847 +       struct list_head *next;
5848 +       struct super_block *sb = root->d_sb;
5849 +
5850 +       err = 0;
5851 +       write_seqlock(&rename_lock);
5852 +       this_parent = root;
5853 +       spin_lock(&this_parent->d_lock);
5854 +repeat:
5855 +       next = this_parent->d_subdirs.next;
5856 +resume:
5857 +       if (this_parent->d_sb == sb
5858 +           && !IS_ROOT(this_parent)
5859 +           && au_di(this_parent)
5860 +           && d_count(this_parent)
5861 +           && (!test || test(this_parent, arg))) {
5862 +               err = au_dpages_append(dpages, this_parent, GFP_ATOMIC);
5863 +               if (unlikely(err))
5864 +                       goto out;
5865 +       }
5866 +
5867 +       while (next != &this_parent->d_subdirs) {
5868 +               struct list_head *tmp = next;
5869 +               struct dentry *dentry = list_entry(tmp, struct dentry,
5870 +                                                  d_u.d_child);
5871 +
5872 +               next = tmp->next;
5873 +               spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
5874 +               if (d_count(dentry)) {
5875 +                       if (!list_empty(&dentry->d_subdirs)) {
5876 +                               spin_unlock(&this_parent->d_lock);
5877 +                               spin_release(&dentry->d_lock.dep_map, 1,
5878 +                                            _RET_IP_);
5879 +                               this_parent = dentry;
5880 +                               spin_acquire(&this_parent->d_lock.dep_map, 0, 1,
5881 +                                            _RET_IP_);
5882 +                               goto repeat;
5883 +                       }
5884 +                       if (dentry->d_sb == sb
5885 +                           && au_di(dentry)
5886 +                           && (!test || test(dentry, arg)))
5887 +                               err = au_dpages_append(dpages, dentry,
5888 +                                                      GFP_ATOMIC);
5889 +               }
5890 +               spin_unlock(&dentry->d_lock);
5891 +               if (unlikely(err))
5892 +                       goto out;
5893 +       }
5894 +
5895 +       if (this_parent != root) {
5896 +               struct dentry *tmp;
5897 +               struct dentry *child;
5898 +
5899 +               tmp = this_parent->d_parent;
5900 +               rcu_read_lock();
5901 +               spin_unlock(&this_parent->d_lock);
5902 +               child = this_parent;
5903 +               this_parent = tmp;
5904 +               spin_lock(&this_parent->d_lock);
5905 +               rcu_read_unlock();
5906 +               next = child->d_u.d_child.next;
5907 +               goto resume;
5908 +       }
5909 +
5910 +out:
5911 +       spin_unlock(&this_parent->d_lock);
5912 +       write_sequnlock(&rename_lock);
5913 +       return err;
5914 +}
5915 +
5916 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
5917 +                      int do_include, au_dpages_test test, void *arg)
5918 +{
5919 +       int err;
5920 +
5921 +       err = 0;
5922 +       write_seqlock(&rename_lock);
5923 +       spin_lock(&dentry->d_lock);
5924 +       if (do_include
5925 +           && d_count(dentry)
5926 +           && (!test || test(dentry, arg)))
5927 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5928 +       spin_unlock(&dentry->d_lock);
5929 +       if (unlikely(err))
5930 +               goto out;
5931 +
5932 +       /*
5933 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
5934 +        * mount
5935 +        */
5936 +       while (!IS_ROOT(dentry)) {
5937 +               dentry = dentry->d_parent; /* rename_lock is locked */
5938 +               spin_lock(&dentry->d_lock);
5939 +               if (d_count(dentry)
5940 +                   && (!test || test(dentry, arg)))
5941 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
5942 +               spin_unlock(&dentry->d_lock);
5943 +               if (unlikely(err))
5944 +                       break;
5945 +       }
5946 +
5947 +out:
5948 +       write_sequnlock(&rename_lock);
5949 +       return err;
5950 +}
5951 +
5952 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
5953 +{
5954 +       return au_di(dentry) && dentry->d_sb == arg;
5955 +}
5956 +
5957 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
5958 +                           struct dentry *dentry, int do_include)
5959 +{
5960 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
5961 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
5962 +}
5963 +
5964 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
5965 +{
5966 +       struct path path[2] = {
5967 +               {
5968 +                       .dentry = d1
5969 +               },
5970 +               {
5971 +                       .dentry = d2
5972 +               }
5973 +       };
5974 +
5975 +       return path_is_under(path + 0, path + 1);
5976 +}
5977 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
5978 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
5979 +++ linux/fs/aufs/dcsub.h       2014-06-24 00:14:06.779385790 +0200
5980 @@ -0,0 +1,116 @@
5981 +/*
5982 + * Copyright (C) 2005-2014 Junjiro R. Okajima
5983 + *
5984 + * This program, aufs is free software; you can redistribute it and/or modify
5985 + * it under the terms of the GNU General Public License as published by
5986 + * the Free Software Foundation; either version 2 of the License, or
5987 + * (at your option) any later version.
5988 + *
5989 + * This program is distributed in the hope that it will be useful,
5990 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5991 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5992 + * GNU General Public License for more details.
5993 + *
5994 + * You should have received a copy of the GNU General Public License
5995 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5996 + */
5997 +
5998 +/*
5999 + * sub-routines for dentry cache
6000 + */
6001 +
6002 +#ifndef __AUFS_DCSUB_H__
6003 +#define __AUFS_DCSUB_H__
6004 +
6005 +#ifdef __KERNEL__
6006 +
6007 +#include <linux/dcache.h>
6008 +#include <linux/fs.h>
6009 +
6010 +struct dentry;
6011 +
6012 +struct au_dpage {
6013 +       int ndentry;
6014 +       struct dentry **dentries;
6015 +};
6016 +
6017 +struct au_dcsub_pages {
6018 +       int ndpage;
6019 +       struct au_dpage *dpages;
6020 +};
6021 +
6022 +/* ---------------------------------------------------------------------- */
6023 +
6024 +/* dcsub.c */
6025 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6026 +void au_dpages_free(struct au_dcsub_pages *dpages);
6027 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6028 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6029 +                  au_dpages_test test, void *arg);
6030 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6031 +                      int do_include, au_dpages_test test, void *arg);
6032 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6033 +                           struct dentry *dentry, int do_include);
6034 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
6035 +
6036 +/* ---------------------------------------------------------------------- */
6037 +
6038 +/*
6039 + * todo: in linux-3.13, several similar (but faster) helpers are added to
6040 + * include/linux/dcache.h. Try them (in the future).
6041 + */
6042 +
6043 +static inline int au_d_hashed_positive(struct dentry *d)
6044 +{
6045 +       int err;
6046 +       struct inode *inode = d->d_inode;
6047 +       err = 0;
6048 +       if (unlikely(d_unhashed(d) || !inode || !inode->i_nlink))
6049 +               err = -ENOENT;
6050 +       return err;
6051 +}
6052 +
6053 +static inline int au_d_linkable(struct dentry *d)
6054 +{
6055 +       int err;
6056 +       struct inode *inode = d->d_inode;
6057 +       err = au_d_hashed_positive(d);
6058 +       if (err
6059 +           && inode
6060 +           && (inode->i_state & I_LINKABLE))
6061 +               err = 0;
6062 +       return err;
6063 +}
6064 +
6065 +static inline int au_d_alive(struct dentry *d)
6066 +{
6067 +       int err;
6068 +       struct inode *inode;
6069 +       err = 0;
6070 +       if (!IS_ROOT(d))
6071 +               err = au_d_hashed_positive(d);
6072 +       else {
6073 +               inode = d->d_inode;
6074 +               if (unlikely(d_unlinked(d) || !inode || !inode->i_nlink))
6075 +                       err = -ENOENT;
6076 +       }
6077 +       return err;
6078 +}
6079 +
6080 +static inline int au_alive_dir(struct dentry *d)
6081 +{
6082 +       int err;
6083 +       err = au_d_alive(d);
6084 +       if (unlikely(err || IS_DEADDIR(d->d_inode)))
6085 +               err = -ENOENT;
6086 +       return err;
6087 +}
6088 +
6089 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
6090 +{
6091 +       return a->len == b->len
6092 +               && !memcmp(a->name, b->name, a->len);
6093 +}
6094 +
6095 +#endif /* __KERNEL__ */
6096 +#endif /* __AUFS_DCSUB_H__ */
6097 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
6098 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
6099 +++ linux/fs/aufs/debug.c       2014-06-24 00:14:06.779385790 +0200
6100 @@ -0,0 +1,518 @@
6101 +/*
6102 + * Copyright (C) 2005-2014 Junjiro R. Okajima
6103 + *
6104 + * This program, aufs is free software; you can redistribute it and/or modify
6105 + * it under the terms of the GNU General Public License as published by
6106 + * the Free Software Foundation; either version 2 of the License, or
6107 + * (at your option) any later version.
6108 + *
6109 + * This program is distributed in the hope that it will be useful,
6110 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6111 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6112 + * GNU General Public License for more details.
6113 + *
6114 + * You should have received a copy of the GNU General Public License
6115 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6116 + */
6117 +
6118 +/*
6119 + * debug print functions
6120 + */
6121 +
6122 +#include <linux/vt_kern.h>
6123 +#include "aufs.h"
6124 +
6125 +/* Returns 0, or -errno.  arg is in kp->arg. */
6126 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
6127 +{
6128 +       int err, n;
6129 +
6130 +       err = kstrtoint(val, 0, &n);
6131 +       if (!err) {
6132 +               if (n > 0)
6133 +                       au_debug_on();
6134 +               else
6135 +                       au_debug_off();
6136 +       }
6137 +       return err;
6138 +}
6139 +
6140 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
6141 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
6142 +{
6143 +       atomic_t *a;
6144 +
6145 +       a = kp->arg;
6146 +       return sprintf(buffer, "%d", atomic_read(a));
6147 +}
6148 +
6149 +static struct kernel_param_ops param_ops_atomic_t = {
6150 +       .set = param_atomic_t_set,
6151 +       .get = param_atomic_t_get
6152 +       /* void (*free)(void *arg) */
6153 +};
6154 +
6155 +atomic_t aufs_debug = ATOMIC_INIT(0);
6156 +MODULE_PARM_DESC(debug, "debug print");
6157 +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
6158 +
6159 +char *au_plevel = KERN_DEBUG;
6160 +#define dpri(fmt, ...) do {                                    \
6161 +       if ((au_plevel                                          \
6162 +            && strcmp(au_plevel, KERN_DEBUG))                  \
6163 +           || au_debug_test())                                 \
6164 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
6165 +} while (0)
6166 +
6167 +/* ---------------------------------------------------------------------- */
6168 +
6169 +void au_dpri_whlist(struct au_nhash *whlist)
6170 +{
6171 +       unsigned long ul, n;
6172 +       struct hlist_head *head;
6173 +       struct au_vdir_wh *pos;
6174 +
6175 +       n = whlist->nh_num;
6176 +       head = whlist->nh_head;
6177 +       for (ul = 0; ul < n; ul++) {
6178 +               hlist_for_each_entry(pos, head, wh_hash)
6179 +                       dpri("b%d, %.*s, %d\n",
6180 +                            pos->wh_bindex,
6181 +                            pos->wh_str.len, pos->wh_str.name,
6182 +                            pos->wh_str.len);
6183 +               head++;
6184 +       }
6185 +}
6186 +
6187 +void au_dpri_vdir(struct au_vdir *vdir)
6188 +{
6189 +       unsigned long ul;
6190 +       union au_vdir_deblk_p p;
6191 +       unsigned char *o;
6192 +
6193 +       if (!vdir || IS_ERR(vdir)) {
6194 +               dpri("err %ld\n", PTR_ERR(vdir));
6195 +               return;
6196 +       }
6197 +
6198 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
6199 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
6200 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
6201 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
6202 +               p.deblk = vdir->vd_deblk[ul];
6203 +               o = p.deblk;
6204 +               dpri("[%lu]: %p\n", ul, o);
6205 +       }
6206 +}
6207 +
6208 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
6209 +                       struct dentry *wh)
6210 +{
6211 +       char *n = NULL;
6212 +       int l = 0;
6213 +
6214 +       if (!inode || IS_ERR(inode)) {
6215 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
6216 +               return -1;
6217 +       }
6218 +
6219 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
6220 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
6221 +                    && sizeof(inode->i_blocks) != sizeof(u64));
6222 +       if (wh) {
6223 +               n = (void *)wh->d_name.name;
6224 +               l = wh->d_name.len;
6225 +       }
6226 +
6227 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
6228 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
6229 +            bindex, inode,
6230 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
6231 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
6232 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
6233 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
6234 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
6235 +            inode->i_state, inode->i_flags, inode->i_version,
6236 +            inode->i_generation,
6237 +            l ? ", wh " : "", l, n);
6238 +       return 0;
6239 +}
6240 +
6241 +void au_dpri_inode(struct inode *inode)
6242 +{
6243 +       struct au_iinfo *iinfo;
6244 +       aufs_bindex_t bindex;
6245 +       int err, hn;
6246 +
6247 +       err = do_pri_inode(-1, inode, -1, NULL);
6248 +       if (err || !au_test_aufs(inode->i_sb))
6249 +               return;
6250 +
6251 +       iinfo = au_ii(inode);
6252 +       if (!iinfo)
6253 +               return;
6254 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
6255 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
6256 +       if (iinfo->ii_bstart < 0)
6257 +               return;
6258 +       hn = 0;
6259 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
6260 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
6261 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
6262 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
6263 +       }
6264 +}
6265 +
6266 +void au_dpri_dalias(struct inode *inode)
6267 +{
6268 +       struct dentry *d;
6269 +
6270 +       spin_lock(&inode->i_lock);
6271 +       hlist_for_each_entry(d, &inode->i_dentry, d_alias)
6272 +               au_dpri_dentry(d);
6273 +       spin_unlock(&inode->i_lock);
6274 +}
6275 +
6276 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
6277 +{
6278 +       struct dentry *wh = NULL;
6279 +       int hn;
6280 +
6281 +       if (!dentry || IS_ERR(dentry)) {
6282 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
6283 +               return -1;
6284 +       }
6285 +       /* do not call dget_parent() here */
6286 +       /* note: access d_xxx without d_lock */
6287 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
6288 +            bindex, dentry, dentry,
6289 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
6290 +            d_count(dentry), dentry->d_flags,
6291 +            d_unhashed(dentry) ? "un" : "");
6292 +       hn = -1;
6293 +       if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
6294 +               struct au_iinfo *iinfo = au_ii(dentry->d_inode);
6295 +               if (iinfo) {
6296 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
6297 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
6298 +               }
6299 +       }
6300 +       do_pri_inode(bindex, dentry->d_inode, hn, wh);
6301 +       return 0;
6302 +}
6303 +
6304 +void au_dpri_dentry(struct dentry *dentry)
6305 +{
6306 +       struct au_dinfo *dinfo;
6307 +       aufs_bindex_t bindex;
6308 +       int err;
6309 +       struct au_hdentry *hdp;
6310 +
6311 +       err = do_pri_dentry(-1, dentry);
6312 +       if (err || !au_test_aufs(dentry->d_sb))
6313 +               return;
6314 +
6315 +       dinfo = au_di(dentry);
6316 +       if (!dinfo)
6317 +               return;
6318 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
6319 +            dinfo->di_bstart, dinfo->di_bend,
6320 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
6321 +            dinfo->di_tmpfile);
6322 +       if (dinfo->di_bstart < 0)
6323 +               return;
6324 +       hdp = dinfo->di_hdentry;
6325 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
6326 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
6327 +}
6328 +
6329 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
6330 +{
6331 +       char a[32];
6332 +
6333 +       if (!file || IS_ERR(file)) {
6334 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
6335 +               return -1;
6336 +       }
6337 +       a[0] = 0;
6338 +       if (bindex < 0
6339 +           && file->f_dentry
6340 +           && au_test_aufs(file->f_dentry->d_sb)
6341 +           && au_fi(file))
6342 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
6343 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
6344 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
6345 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
6346 +            file->f_version, file->f_pos, a);
6347 +       if (file->f_dentry)
6348 +               do_pri_dentry(bindex, file->f_dentry);
6349 +       return 0;
6350 +}
6351 +
6352 +void au_dpri_file(struct file *file)
6353 +{
6354 +       struct au_finfo *finfo;
6355 +       struct au_fidir *fidir;
6356 +       struct au_hfile *hfile;
6357 +       aufs_bindex_t bindex;
6358 +       int err;
6359 +
6360 +       err = do_pri_file(-1, file);
6361 +       if (err || !file->f_dentry || !au_test_aufs(file->f_dentry->d_sb))
6362 +               return;
6363 +
6364 +       finfo = au_fi(file);
6365 +       if (!finfo)
6366 +               return;
6367 +       if (finfo->fi_btop < 0)
6368 +               return;
6369 +       fidir = finfo->fi_hdir;
6370 +       if (!fidir)
6371 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
6372 +       else
6373 +               for (bindex = finfo->fi_btop;
6374 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
6375 +                    bindex++) {
6376 +                       hfile = fidir->fd_hfile + bindex;
6377 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
6378 +               }
6379 +}
6380 +
6381 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
6382 +{
6383 +       struct vfsmount *mnt;
6384 +       struct super_block *sb;
6385 +
6386 +       if (!br || IS_ERR(br))
6387 +               goto out;
6388 +       mnt = au_br_mnt(br);
6389 +       if (!mnt || IS_ERR(mnt))
6390 +               goto out;
6391 +       sb = mnt->mnt_sb;
6392 +       if (!sb || IS_ERR(sb))
6393 +               goto out;
6394 +
6395 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
6396 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
6397 +            "xino %d\n",
6398 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
6399 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
6400 +            sb->s_flags, sb->s_count,
6401 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
6402 +       return 0;
6403 +
6404 +out:
6405 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
6406 +       return -1;
6407 +}
6408 +
6409 +void au_dpri_sb(struct super_block *sb)
6410 +{
6411 +       struct au_sbinfo *sbinfo;
6412 +       aufs_bindex_t bindex;
6413 +       int err;
6414 +       /* to reuduce stack size */
6415 +       struct {
6416 +               struct vfsmount mnt;
6417 +               struct au_branch fake;
6418 +       } *a;
6419 +
6420 +       /* this function can be called from magic sysrq */
6421 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
6422 +       if (unlikely(!a)) {
6423 +               dpri("no memory\n");
6424 +               return;
6425 +       }
6426 +
6427 +       a->mnt.mnt_sb = sb;
6428 +       a->fake.br_perm = 0;
6429 +       a->fake.br_path.mnt = &a->mnt;
6430 +       a->fake.br_xino.xi_file = NULL;
6431 +       atomic_set(&a->fake.br_count, 0);
6432 +       smp_mb(); /* atomic_set */
6433 +       err = do_pri_br(-1, &a->fake);
6434 +       kfree(a);
6435 +       dpri("dev 0x%x\n", sb->s_dev);
6436 +       if (err || !au_test_aufs(sb))
6437 +               return;
6438 +
6439 +       sbinfo = au_sbi(sb);
6440 +       if (!sbinfo)
6441 +               return;
6442 +       dpri("nw %d, gen %u, kobj %d\n",
6443 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
6444 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
6445 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
6446 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
6447 +}
6448 +
6449 +/* ---------------------------------------------------------------------- */
6450 +
6451 +void au_dbg_sleep_jiffy(int jiffy)
6452 +{
6453 +       while (jiffy)
6454 +               jiffy = schedule_timeout_uninterruptible(jiffy);
6455 +}
6456 +
6457 +void au_dbg_iattr(struct iattr *ia)
6458 +{
6459 +#define AuBit(name)                                    \
6460 +       do {                                            \
6461 +               if (ia->ia_valid & ATTR_ ## name)       \
6462 +                       dpri(#name "\n");               \
6463 +       } while (0)
6464 +       AuBit(MODE);
6465 +       AuBit(UID);
6466 +       AuBit(GID);
6467 +       AuBit(SIZE);
6468 +       AuBit(ATIME);
6469 +       AuBit(MTIME);
6470 +       AuBit(CTIME);
6471 +       AuBit(ATIME_SET);
6472 +       AuBit(MTIME_SET);
6473 +       AuBit(FORCE);
6474 +       AuBit(ATTR_FLAG);
6475 +       AuBit(KILL_SUID);
6476 +       AuBit(KILL_SGID);
6477 +       AuBit(FILE);
6478 +       AuBit(KILL_PRIV);
6479 +       AuBit(OPEN);
6480 +       AuBit(TIMES_SET);
6481 +#undef AuBit
6482 +       dpri("ia_file %p\n", ia->ia_file);
6483 +}
6484 +
6485 +/* ---------------------------------------------------------------------- */
6486 +
6487 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
6488 +{
6489 +       struct inode *h_inode, *inode = dentry->d_inode;
6490 +       struct dentry *h_dentry;
6491 +       aufs_bindex_t bindex, bend, bi;
6492 +
6493 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
6494 +               return;
6495 +
6496 +       bend = au_dbend(dentry);
6497 +       bi = au_ibend(inode);
6498 +       if (bi < bend)
6499 +               bend = bi;
6500 +       bindex = au_dbstart(dentry);
6501 +       bi = au_ibstart(inode);
6502 +       if (bi > bindex)
6503 +               bindex = bi;
6504 +
6505 +       for (; bindex <= bend; bindex++) {
6506 +               h_dentry = au_h_dptr(dentry, bindex);
6507 +               if (!h_dentry)
6508 +                       continue;
6509 +               h_inode = au_h_iptr(inode, bindex);
6510 +               if (unlikely(h_inode != h_dentry->d_inode)) {
6511 +                       au_debug_on();
6512 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
6513 +                       AuDbgDentry(dentry);
6514 +                       AuDbgInode(inode);
6515 +                       au_debug_off();
6516 +                       BUG();
6517 +               }
6518 +       }
6519 +}
6520 +
6521 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen)
6522 +{
6523 +       struct dentry *parent;
6524 +
6525 +       parent = dget_parent(dentry);
6526 +       AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
6527 +       AuDebugOn(IS_ROOT(dentry));
6528 +       AuDebugOn(au_digen_test(parent, sigen));
6529 +       dput(parent);
6530 +}
6531 +
6532 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen)
6533 +{
6534 +       struct dentry *parent;
6535 +       struct inode *inode;
6536 +
6537 +       parent = dget_parent(dentry);
6538 +       inode = dentry->d_inode;
6539 +       AuDebugOn(inode && S_ISDIR(dentry->d_inode->i_mode));
6540 +       AuDebugOn(au_digen_test(parent, sigen));
6541 +       dput(parent);
6542 +}
6543 +
6544 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
6545 +{
6546 +       int err, i, j;
6547 +       struct au_dcsub_pages dpages;
6548 +       struct au_dpage *dpage;
6549 +       struct dentry **dentries;
6550 +
6551 +       err = au_dpages_init(&dpages, GFP_NOFS);
6552 +       AuDebugOn(err);
6553 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
6554 +       AuDebugOn(err);
6555 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
6556 +               dpage = dpages.dpages + i;
6557 +               dentries = dpage->dentries;
6558 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
6559 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
6560 +       }
6561 +       au_dpages_free(&dpages);
6562 +}
6563 +
6564 +void au_dbg_verify_kthread(void)
6565 +{
6566 +       if (au_wkq_test()) {
6567 +               au_dbg_blocked();
6568 +               /*
6569 +                * It may be recursive, but udba=notify between two aufs mounts,
6570 +                * where a single ro branch is shared, is not a problem.
6571 +                */
6572 +               /* WARN_ON(1); */
6573 +       }
6574 +}
6575 +
6576 +/* ---------------------------------------------------------------------- */
6577 +
6578 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo __maybe_unused)
6579 +{
6580 +#ifdef AuForceNoPlink
6581 +       au_opt_clr(sbinfo->si_mntflags, PLINK);
6582 +#endif
6583 +#ifdef AuForceNoXino
6584 +       au_opt_clr(sbinfo->si_mntflags, XINO);
6585 +#endif
6586 +#ifdef AuForceNoRefrof
6587 +       au_opt_clr(sbinfo->si_mntflags, REFROF);
6588 +#endif
6589 +#ifdef AuForceHnotify
6590 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_HNOTIFY);
6591 +#endif
6592 +#ifdef AuForceRd0
6593 +       sbinfo->si_rdblk = 0;
6594 +       sbinfo->si_rdhash = 0;
6595 +#endif
6596 +}
6597 +
6598 +int __init au_debug_init(void)
6599 +{
6600 +       aufs_bindex_t bindex;
6601 +       struct au_vdir_destr destr;
6602 +
6603 +       bindex = -1;
6604 +       AuDebugOn(bindex >= 0);
6605 +
6606 +       destr.len = -1;
6607 +       AuDebugOn(destr.len < NAME_MAX);
6608 +
6609 +#ifdef CONFIG_4KSTACKS
6610 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
6611 +#endif
6612 +
6613 +#ifdef AuForceNoBrs
6614 +       sysaufs_brs = 0;
6615 +#endif
6616 +
6617 +       return 0;
6618 +}
6619 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
6620 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
6621 +++ linux/fs/aufs/debug.h       2014-06-24 00:14:06.779385790 +0200
6622 @@ -0,0 +1,247 @@
6623 +/*
6624 + * Copyright (C) 2005-2014 Junjiro R. Okajima
6625 + *
6626 + * This program, aufs is free software; you can redistribute it and/or modify
6627 + * it under the terms of the GNU General Public License as published by
6628 + * the Free Software Foundation; either version 2 of the License, or
6629 + * (at your option) any later version.
6630 + *
6631 + * This program is distributed in the hope that it will be useful,
6632 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6633 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6634 + * GNU General Public License for more details.
6635 + *
6636 + * You should have received a copy of the GNU General Public License
6637 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6638 + */
6639 +
6640 +/*
6641 + * debug print functions
6642 + */
6643 +
6644 +#ifndef __AUFS_DEBUG_H__
6645 +#define __AUFS_DEBUG_H__
6646 +
6647 +#ifdef __KERNEL__
6648 +
6649 +#include <linux/atomic.h>
6650 +#include <linux/module.h>
6651 +#include <linux/kallsyms.h>
6652 +#include <linux/sysrq.h>
6653 +
6654 +#ifdef CONFIG_AUFS_DEBUG
6655 +#define AuDebugOn(a)           BUG_ON(a)
6656 +
6657 +/* module parameter */
6658 +extern atomic_t aufs_debug;
6659 +static inline void au_debug_on(void)
6660 +{
6661 +       atomic_inc(&aufs_debug);
6662 +}
6663 +static inline void au_debug_off(void)
6664 +{
6665 +       atomic_dec_if_positive(&aufs_debug);
6666 +}
6667 +
6668 +static inline int au_debug_test(void)
6669 +{
6670 +       return atomic_read(&aufs_debug) > 0;
6671 +}
6672 +#else
6673 +#define AuDebugOn(a)           do {} while (0)
6674 +AuStubVoid(au_debug_on, void)
6675 +AuStubVoid(au_debug_off, void)
6676 +AuStubInt0(au_debug_test, void)
6677 +#endif /* CONFIG_AUFS_DEBUG */
6678 +
6679 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
6680 +
6681 +/* ---------------------------------------------------------------------- */
6682 +
6683 +/* debug print */
6684 +
6685 +#define AuDbg(fmt, ...) do { \
6686 +       if (au_debug_test()) \
6687 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
6688 +} while (0)
6689 +#define AuLabel(l)             AuDbg(#l "\n")
6690 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
6691 +#define AuWarn1(fmt, ...) do { \
6692 +       static unsigned char _c; \
6693 +       if (!_c++) \
6694 +               pr_warn(fmt, ##__VA_ARGS__); \
6695 +} while (0)
6696 +
6697 +#define AuErr1(fmt, ...) do { \
6698 +       static unsigned char _c; \
6699 +       if (!_c++) \
6700 +               pr_err(fmt, ##__VA_ARGS__); \
6701 +} while (0)
6702 +
6703 +#define AuIOErr1(fmt, ...) do { \
6704 +       static unsigned char _c; \
6705 +       if (!_c++) \
6706 +               AuIOErr(fmt, ##__VA_ARGS__); \
6707 +} while (0)
6708 +
6709 +#define AuUnsupportMsg "This operation is not supported." \
6710 +                       " Please report this application to aufs-users ML."
6711 +#define AuUnsupport(fmt, ...) do { \
6712 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
6713 +       dump_stack(); \
6714 +} while (0)
6715 +
6716 +#define AuTraceErr(e) do { \
6717 +       if (unlikely((e) < 0)) \
6718 +               AuDbg("err %d\n", (int)(e)); \
6719 +} while (0)
6720 +
6721 +#define AuTraceErrPtr(p) do { \
6722 +       if (IS_ERR(p)) \
6723 +               AuDbg("err %ld\n", PTR_ERR(p)); \
6724 +} while (0)
6725 +
6726 +/* dirty macros for debug print, use with "%.*s" and caution */
6727 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
6728 +
6729 +/* ---------------------------------------------------------------------- */
6730 +
6731 +struct au_sbinfo;
6732 +struct au_finfo;
6733 +struct dentry;
6734 +#ifdef CONFIG_AUFS_DEBUG
6735 +extern char *au_plevel;
6736 +struct au_nhash;
6737 +void au_dpri_whlist(struct au_nhash *whlist);
6738 +struct au_vdir;
6739 +void au_dpri_vdir(struct au_vdir *vdir);
6740 +struct inode;
6741 +void au_dpri_inode(struct inode *inode);
6742 +void au_dpri_dalias(struct inode *inode);
6743 +void au_dpri_dentry(struct dentry *dentry);
6744 +struct file;
6745 +void au_dpri_file(struct file *filp);
6746 +struct super_block;
6747 +void au_dpri_sb(struct super_block *sb);
6748 +
6749 +void au_dbg_sleep_jiffy(int jiffy);
6750 +struct iattr;
6751 +void au_dbg_iattr(struct iattr *ia);
6752 +
6753 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
6754 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
6755 +void au_dbg_verify_dir_parent(struct dentry *dentry, unsigned int sigen);
6756 +void au_dbg_verify_nondir_parent(struct dentry *dentry, unsigned int sigen);
6757 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
6758 +void au_dbg_verify_kthread(void);
6759 +
6760 +int __init au_debug_init(void);
6761 +void au_debug_sbinfo_init(struct au_sbinfo *sbinfo);
6762 +#define AuDbgWhlist(w) do { \
6763 +       AuDbg(#w "\n"); \
6764 +       au_dpri_whlist(w); \
6765 +} while (0)
6766 +
6767 +#define AuDbgVdir(v) do { \
6768 +       AuDbg(#v "\n"); \
6769 +       au_dpri_vdir(v); \
6770 +} while (0)
6771 +
6772 +#define AuDbgInode(i) do { \
6773 +       AuDbg(#i "\n"); \
6774 +       au_dpri_inode(i); \
6775 +} while (0)
6776 +
6777 +#define AuDbgDAlias(i) do { \
6778 +       AuDbg(#i "\n"); \
6779 +       au_dpri_dalias(i); \
6780 +} while (0)
6781 +
6782 +#define AuDbgDentry(d) do { \
6783 +       AuDbg(#d "\n"); \
6784 +       au_dpri_dentry(d); \
6785 +} while (0)
6786 +
6787 +#define AuDbgFile(f) do { \
6788 +       AuDbg(#f "\n"); \
6789 +       au_dpri_file(f); \
6790 +} while (0)
6791 +
6792 +#define AuDbgSb(sb) do { \
6793 +       AuDbg(#sb "\n"); \
6794 +       au_dpri_sb(sb); \
6795 +} while (0)
6796 +
6797 +#define AuDbgSleep(sec) do { \
6798 +       AuDbg("sleep %d sec\n", sec); \
6799 +       ssleep(sec); \
6800 +} while (0)
6801 +
6802 +#define AuDbgSleepJiffy(jiffy) do { \
6803 +       AuDbg("sleep %d jiffies\n", jiffy); \
6804 +       au_dbg_sleep_jiffy(jiffy); \
6805 +} while (0)
6806 +
6807 +#define AuDbgIAttr(ia) do { \
6808 +       AuDbg("ia_valid 0x%x\n", (ia)->ia_valid); \
6809 +       au_dbg_iattr(ia); \
6810 +} while (0)
6811 +
6812 +#define AuDbgSym(addr) do {                            \
6813 +       char sym[KSYM_SYMBOL_LEN];                      \
6814 +       sprint_symbol(sym, (unsigned long)addr);        \
6815 +       AuDbg("%s\n", sym);                             \
6816 +} while (0)
6817 +
6818 +#define AuInfoSym(addr) do {                           \
6819 +       char sym[KSYM_SYMBOL_LEN];                      \
6820 +       sprint_symbol(sym, (unsigned long)addr);        \
6821 +       AuInfo("%s\n", sym);                            \
6822 +} while (0)
6823 +#else
6824 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
6825 +AuStubVoid(au_dbg_verify_dir_parent, struct dentry *dentry, unsigned int sigen)
6826 +AuStubVoid(au_dbg_verify_nondir_parent, struct dentry *dentry,
6827 +          unsigned int sigen)
6828 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
6829 +AuStubVoid(au_dbg_verify_kthread, void)
6830 +AuStubInt0(__init au_debug_init, void)
6831 +AuStubVoid(au_debug_sbinfo_init, struct au_sbinfo *sbinfo)
6832 +
6833 +#define AuDbgWhlist(w)         do {} while (0)
6834 +#define AuDbgVdir(v)           do {} while (0)
6835 +#define AuDbgInode(i)          do {} while (0)
6836 +#define AuDbgDAlias(i)         do {} while (0)
6837 +#define AuDbgDentry(d)         do {} while (0)
6838 +#define AuDbgFile(f)           do {} while (0)
6839 +#define AuDbgSb(sb)            do {} while (0)
6840 +#define AuDbgSleep(sec)                do {} while (0)
6841 +#define AuDbgSleepJiffy(jiffy) do {} while (0)
6842 +#define AuDbgIAttr(ia)         do {} while (0)
6843 +#define AuDbgSym(addr)         do {} while (0)
6844 +#define AuInfoSym(addr)                do {} while (0)
6845 +#endif /* CONFIG_AUFS_DEBUG */
6846 +
6847 +/* ---------------------------------------------------------------------- */
6848 +
6849 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
6850 +int __init au_sysrq_init(void);
6851 +void au_sysrq_fin(void);
6852 +
6853 +#ifdef CONFIG_HW_CONSOLE
6854 +#define au_dbg_blocked() do { \
6855 +       WARN_ON(1); \
6856 +       handle_sysrq('w'); \
6857 +} while (0)
6858 +#else
6859 +AuStubVoid(au_dbg_blocked, void)
6860 +#endif
6861 +
6862 +#else
6863 +AuStubInt0(__init au_sysrq_init, void)
6864 +AuStubVoid(au_sysrq_fin, void)
6865 +AuStubVoid(au_dbg_blocked, void)
6866 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
6867 +
6868 +#endif /* __KERNEL__ */
6869 +#endif /* __AUFS_DEBUG_H__ */
6870 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
6871 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
6872 +++ linux/fs/aufs/dentry.c      2014-06-24 00:14:06.779385790 +0200
6873 @@ -0,0 +1,1084 @@
6874 +/*
6875 + * Copyright (C) 2005-2014 Junjiro R. Okajima
6876 + *
6877 + * This program, aufs is free software; you can redistribute it and/or modify
6878 + * it under the terms of the GNU General Public License as published by
6879 + * the Free Software Foundation; either version 2 of the License, or
6880 + * (at your option) any later version.
6881 + *
6882 + * This program is distributed in the hope that it will be useful,
6883 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6884 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6885 + * GNU General Public License for more details.
6886 + *
6887 + * You should have received a copy of the GNU General Public License
6888 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6889 + */
6890 +
6891 +/*
6892 + * lookup and dentry operations
6893 + */
6894 +
6895 +#include <linux/namei.h>
6896 +#include "aufs.h"
6897 +
6898 +#define AuLkup_ALLOW_NEG       1
6899 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
6900 +#define au_fset_lkup(flags, name) \
6901 +       do { (flags) |= AuLkup_##name; } while (0)
6902 +#define au_fclr_lkup(flags, name) \
6903 +       do { (flags) &= ~AuLkup_##name; } while (0)
6904 +
6905 +struct au_do_lookup_args {
6906 +       unsigned int            flags;
6907 +       mode_t                  type;
6908 +};
6909 +
6910 +/*
6911 + * returns positive/negative dentry, NULL or an error.
6912 + * NULL means whiteout-ed or not-found.
6913 + */
6914 +static struct dentry*
6915 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
6916 +            aufs_bindex_t bindex, struct qstr *wh_name,
6917 +            struct au_do_lookup_args *args)
6918 +{
6919 +       struct dentry *h_dentry;
6920 +       struct inode *h_inode, *inode;
6921 +       struct au_branch *br;
6922 +       int wh_found, opq;
6923 +       unsigned char wh_able;
6924 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
6925 +
6926 +       wh_found = 0;
6927 +       br = au_sbr(dentry->d_sb, bindex);
6928 +       wh_able = !!au_br_whable(br->br_perm);
6929 +       if (wh_able)
6930 +               wh_found = au_wh_test(h_parent, wh_name, br, /*try_sio*/0);
6931 +       h_dentry = ERR_PTR(wh_found);
6932 +       if (!wh_found)
6933 +               goto real_lookup;
6934 +       if (unlikely(wh_found < 0))
6935 +               goto out;
6936 +
6937 +       /* We found a whiteout */
6938 +       /* au_set_dbend(dentry, bindex); */
6939 +       au_set_dbwh(dentry, bindex);
6940 +       if (!allow_neg)
6941 +               return NULL; /* success */
6942 +
6943 +real_lookup:
6944 +       h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
6945 +       if (IS_ERR(h_dentry))
6946 +               goto out;
6947 +
6948 +       h_inode = h_dentry->d_inode;
6949 +       if (!h_inode) {
6950 +               if (!allow_neg)
6951 +                       goto out_neg;
6952 +       } else if (wh_found
6953 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
6954 +               goto out_neg;
6955 +
6956 +       if (au_dbend(dentry) <= bindex)
6957 +               au_set_dbend(dentry, bindex);
6958 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
6959 +               au_set_dbstart(dentry, bindex);
6960 +       au_set_h_dptr(dentry, bindex, h_dentry);
6961 +
6962 +       inode = dentry->d_inode;
6963 +       if (!h_inode || !S_ISDIR(h_inode->i_mode) || !wh_able
6964 +           || (inode && !S_ISDIR(inode->i_mode)))
6965 +               goto out; /* success */
6966 +
6967 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
6968 +       opq = au_diropq_test(h_dentry, br);
6969 +       mutex_unlock(&h_inode->i_mutex);
6970 +       if (opq > 0)
6971 +               au_set_dbdiropq(dentry, bindex);
6972 +       else if (unlikely(opq < 0)) {
6973 +               au_set_h_dptr(dentry, bindex, NULL);
6974 +               h_dentry = ERR_PTR(opq);
6975 +       }
6976 +       goto out;
6977 +
6978 +out_neg:
6979 +       dput(h_dentry);
6980 +       h_dentry = NULL;
6981 +out:
6982 +       return h_dentry;
6983 +}
6984 +
6985 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
6986 +{
6987 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
6988 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
6989 +               return -EPERM;
6990 +       return 0;
6991 +}
6992 +
6993 +/*
6994 + * returns the number of lower positive dentries,
6995 + * otherwise an error.
6996 + * can be called at unlinking with @type is zero.
6997 + */
6998 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
6999 +{
7000 +       int npositive, err;
7001 +       aufs_bindex_t bindex, btail, bdiropq;
7002 +       unsigned char isdir;
7003 +       struct qstr whname;
7004 +       struct au_do_lookup_args args = {
7005 +               .flags          = 0,
7006 +               .type           = type
7007 +       };
7008 +       const struct qstr *name = &dentry->d_name;
7009 +       struct dentry *parent;
7010 +       struct inode *inode;
7011 +
7012 +       err = au_test_shwh(dentry->d_sb, name);
7013 +       if (unlikely(err))
7014 +               goto out;
7015 +
7016 +       err = au_wh_name_alloc(&whname, name);
7017 +       if (unlikely(err))
7018 +               goto out;
7019 +
7020 +       inode = dentry->d_inode;
7021 +       isdir = !!(inode && S_ISDIR(inode->i_mode));
7022 +       if (!type)
7023 +               au_fset_lkup(args.flags, ALLOW_NEG);
7024 +
7025 +       npositive = 0;
7026 +       parent = dget_parent(dentry);
7027 +       btail = au_dbtaildir(parent);
7028 +       for (bindex = bstart; bindex <= btail; bindex++) {
7029 +               struct dentry *h_parent, *h_dentry;
7030 +               struct inode *h_inode, *h_dir;
7031 +
7032 +               h_dentry = au_h_dptr(dentry, bindex);
7033 +               if (h_dentry) {
7034 +                       if (h_dentry->d_inode)
7035 +                               npositive++;
7036 +                       if (type != S_IFDIR)
7037 +                               break;
7038 +                       continue;
7039 +               }
7040 +               h_parent = au_h_dptr(parent, bindex);
7041 +               if (!h_parent)
7042 +                       continue;
7043 +               h_dir = h_parent->d_inode;
7044 +               if (!h_dir || !S_ISDIR(h_dir->i_mode))
7045 +                       continue;
7046 +
7047 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
7048 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7049 +                                       &args);
7050 +               mutex_unlock(&h_dir->i_mutex);
7051 +               err = PTR_ERR(h_dentry);
7052 +               if (IS_ERR(h_dentry))
7053 +                       goto out_parent;
7054 +               au_fclr_lkup(args.flags, ALLOW_NEG);
7055 +
7056 +               if (au_dbwh(dentry) >= 0)
7057 +                       break;
7058 +               if (!h_dentry)
7059 +                       continue;
7060 +               h_inode = h_dentry->d_inode;
7061 +               if (!h_inode)
7062 +                       continue;
7063 +               npositive++;
7064 +               if (!args.type)
7065 +                       args.type = h_inode->i_mode & S_IFMT;
7066 +               if (args.type != S_IFDIR)
7067 +                       break;
7068 +               else if (isdir) {
7069 +                       /* the type of lower may be different */
7070 +                       bdiropq = au_dbdiropq(dentry);
7071 +                       if (bdiropq >= 0 && bdiropq <= bindex)
7072 +                               break;
7073 +               }
7074 +       }
7075 +
7076 +       if (npositive) {
7077 +               AuLabel(positive);
7078 +               au_update_dbstart(dentry);
7079 +       }
7080 +       err = npositive;
7081 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
7082 +                    && au_dbstart(dentry) < 0)) {
7083 +               err = -EIO;
7084 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7085 +                       dentry, err);
7086 +       }
7087 +
7088 +out_parent:
7089 +       dput(parent);
7090 +       kfree(whname.name);
7091 +out:
7092 +       return err;
7093 +}
7094 +
7095 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
7096 +                              struct au_branch *br)
7097 +{
7098 +       struct dentry *dentry;
7099 +       int wkq_err;
7100 +
7101 +       if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
7102 +               dentry = vfsub_lkup_one(name, parent);
7103 +       else {
7104 +               struct vfsub_lkup_one_args args = {
7105 +                       .errp   = &dentry,
7106 +                       .name   = name,
7107 +                       .parent = parent
7108 +               };
7109 +
7110 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
7111 +               if (unlikely(wkq_err))
7112 +                       dentry = ERR_PTR(wkq_err);
7113 +       }
7114 +
7115 +       return dentry;
7116 +}
7117 +
7118 +/*
7119 + * lookup @dentry on @bindex which should be negative.
7120 + */
7121 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
7122 +{
7123 +       int err;
7124 +       struct dentry *parent, *h_parent, *h_dentry;
7125 +       struct au_branch *br;
7126 +
7127 +       parent = dget_parent(dentry);
7128 +       h_parent = au_h_dptr(parent, bindex);
7129 +       br = au_sbr(dentry->d_sb, bindex);
7130 +       if (wh)
7131 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
7132 +       else
7133 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent, br);
7134 +       err = PTR_ERR(h_dentry);
7135 +       if (IS_ERR(h_dentry))
7136 +               goto out;
7137 +       if (unlikely(h_dentry->d_inode)) {
7138 +               err = -EIO;
7139 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
7140 +               dput(h_dentry);
7141 +               goto out;
7142 +       }
7143 +
7144 +       err = 0;
7145 +       if (bindex < au_dbstart(dentry))
7146 +               au_set_dbstart(dentry, bindex);
7147 +       if (au_dbend(dentry) < bindex)
7148 +               au_set_dbend(dentry, bindex);
7149 +       au_set_h_dptr(dentry, bindex, h_dentry);
7150 +
7151 +out:
7152 +       dput(parent);
7153 +       return err;
7154 +}
7155 +
7156 +/* ---------------------------------------------------------------------- */
7157 +
7158 +/* subset of struct inode */
7159 +struct au_iattr {
7160 +       unsigned long           i_ino;
7161 +       /* unsigned int         i_nlink; */
7162 +       kuid_t                  i_uid;
7163 +       kgid_t                  i_gid;
7164 +       u64                     i_version;
7165 +/*
7166 +       loff_t                  i_size;
7167 +       blkcnt_t                i_blocks;
7168 +*/
7169 +       umode_t                 i_mode;
7170 +};
7171 +
7172 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7173 +{
7174 +       ia->i_ino = h_inode->i_ino;
7175 +       /* ia->i_nlink = h_inode->i_nlink; */
7176 +       ia->i_uid = h_inode->i_uid;
7177 +       ia->i_gid = h_inode->i_gid;
7178 +       ia->i_version = h_inode->i_version;
7179 +/*
7180 +       ia->i_size = h_inode->i_size;
7181 +       ia->i_blocks = h_inode->i_blocks;
7182 +*/
7183 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
7184 +}
7185 +
7186 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
7187 +{
7188 +       return ia->i_ino != h_inode->i_ino
7189 +               /* || ia->i_nlink != h_inode->i_nlink */
7190 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
7191 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
7192 +               || ia->i_version != h_inode->i_version
7193 +/*
7194 +               || ia->i_size != h_inode->i_size
7195 +               || ia->i_blocks != h_inode->i_blocks
7196 +*/
7197 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
7198 +}
7199 +
7200 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7201 +                             struct au_branch *br)
7202 +{
7203 +       int err;
7204 +       struct au_iattr ia;
7205 +       struct inode *h_inode;
7206 +       struct dentry *h_d;
7207 +       struct super_block *h_sb;
7208 +
7209 +       err = 0;
7210 +       memset(&ia, -1, sizeof(ia));
7211 +       h_sb = h_dentry->d_sb;
7212 +       h_inode = h_dentry->d_inode;
7213 +       if (h_inode)
7214 +               au_iattr_save(&ia, h_inode);
7215 +       else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
7216 +               /* nfs d_revalidate may return 0 for negative dentry */
7217 +               /* fuse d_revalidate always return 0 for negative dentry */
7218 +               goto out;
7219 +
7220 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
7221 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
7222 +       err = PTR_ERR(h_d);
7223 +       if (IS_ERR(h_d))
7224 +               goto out;
7225 +
7226 +       err = 0;
7227 +       if (unlikely(h_d != h_dentry
7228 +                    || h_d->d_inode != h_inode
7229 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
7230 +               err = au_busy_or_stale();
7231 +       dput(h_d);
7232 +
7233 +out:
7234 +       AuTraceErr(err);
7235 +       return err;
7236 +}
7237 +
7238 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7239 +               struct dentry *h_parent, struct au_branch *br)
7240 +{
7241 +       int err;
7242 +
7243 +       err = 0;
7244 +       if (udba == AuOpt_UDBA_REVAL
7245 +           && !au_test_fs_remote(h_dentry->d_sb)) {
7246 +               IMustLock(h_dir);
7247 +               err = (h_dentry->d_parent->d_inode != h_dir);
7248 +       } else if (udba != AuOpt_UDBA_NONE)
7249 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
7250 +
7251 +       return err;
7252 +}
7253 +
7254 +/* ---------------------------------------------------------------------- */
7255 +
7256 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
7257 +{
7258 +       int err;
7259 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
7260 +       struct au_hdentry tmp, *p, *q;
7261 +       struct au_dinfo *dinfo;
7262 +       struct super_block *sb;
7263 +
7264 +       DiMustWriteLock(dentry);
7265 +
7266 +       sb = dentry->d_sb;
7267 +       dinfo = au_di(dentry);
7268 +       bend = dinfo->di_bend;
7269 +       bwh = dinfo->di_bwh;
7270 +       bdiropq = dinfo->di_bdiropq;
7271 +       p = dinfo->di_hdentry + dinfo->di_bstart;
7272 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
7273 +               if (!p->hd_dentry)
7274 +                       continue;
7275 +
7276 +               new_bindex = au_br_index(sb, p->hd_id);
7277 +               if (new_bindex == bindex)
7278 +                       continue;
7279 +
7280 +               if (dinfo->di_bwh == bindex)
7281 +                       bwh = new_bindex;
7282 +               if (dinfo->di_bdiropq == bindex)
7283 +                       bdiropq = new_bindex;
7284 +               if (new_bindex < 0) {
7285 +                       au_hdput(p);
7286 +                       p->hd_dentry = NULL;
7287 +                       continue;
7288 +               }
7289 +
7290 +               /* swap two lower dentries, and loop again */
7291 +               q = dinfo->di_hdentry + new_bindex;
7292 +               tmp = *q;
7293 +               *q = *p;
7294 +               *p = tmp;
7295 +               if (tmp.hd_dentry) {
7296 +                       bindex--;
7297 +                       p--;
7298 +               }
7299 +       }
7300 +
7301 +       dinfo->di_bwh = -1;
7302 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
7303 +               dinfo->di_bwh = bwh;
7304 +
7305 +       dinfo->di_bdiropq = -1;
7306 +       if (bdiropq >= 0
7307 +           && bdiropq <= au_sbend(sb)
7308 +           && au_sbr_whable(sb, bdiropq))
7309 +               dinfo->di_bdiropq = bdiropq;
7310 +
7311 +       err = -EIO;
7312 +       dinfo->di_bstart = -1;
7313 +       dinfo->di_bend = -1;
7314 +       bend = au_dbend(parent);
7315 +       p = dinfo->di_hdentry;
7316 +       for (bindex = 0; bindex <= bend; bindex++, p++)
7317 +               if (p->hd_dentry) {
7318 +                       dinfo->di_bstart = bindex;
7319 +                       break;
7320 +               }
7321 +
7322 +       if (dinfo->di_bstart >= 0) {
7323 +               p = dinfo->di_hdentry + bend;
7324 +               for (bindex = bend; bindex >= 0; bindex--, p--)
7325 +                       if (p->hd_dentry) {
7326 +                               dinfo->di_bend = bindex;
7327 +                               err = 0;
7328 +                               break;
7329 +                       }
7330 +       }
7331 +
7332 +       return err;
7333 +}
7334 +
7335 +static void au_do_hide(struct dentry *dentry)
7336 +{
7337 +       struct inode *inode;
7338 +
7339 +       inode = dentry->d_inode;
7340 +       if (inode) {
7341 +               if (!S_ISDIR(inode->i_mode)) {
7342 +                       if (inode->i_nlink && !d_unhashed(dentry))
7343 +                               drop_nlink(inode);
7344 +               } else {
7345 +                       clear_nlink(inode);
7346 +                       /* stop next lookup */
7347 +                       inode->i_flags |= S_DEAD;
7348 +               }
7349 +               smp_mb(); /* necessary? */
7350 +       }
7351 +       d_drop(dentry);
7352 +}
7353 +
7354 +static int au_hide_children(struct dentry *parent)
7355 +{
7356 +       int err, i, j, ndentry;
7357 +       struct au_dcsub_pages dpages;
7358 +       struct au_dpage *dpage;
7359 +       struct dentry *dentry;
7360 +
7361 +       err = au_dpages_init(&dpages, GFP_NOFS);
7362 +       if (unlikely(err))
7363 +               goto out;
7364 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
7365 +       if (unlikely(err))
7366 +               goto out_dpages;
7367 +
7368 +       /* in reverse order */
7369 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
7370 +               dpage = dpages.dpages + i;
7371 +               ndentry = dpage->ndentry;
7372 +               for (j = ndentry - 1; j >= 0; j--) {
7373 +                       dentry = dpage->dentries[j];
7374 +                       if (dentry != parent)
7375 +                               au_do_hide(dentry);
7376 +               }
7377 +       }
7378 +
7379 +out_dpages:
7380 +       au_dpages_free(&dpages);
7381 +out:
7382 +       return err;
7383 +}
7384 +
7385 +static void au_hide(struct dentry *dentry)
7386 +{
7387 +       int err;
7388 +       struct inode *inode;
7389 +
7390 +       AuDbgDentry(dentry);
7391 +       inode = dentry->d_inode;
7392 +       if (inode && S_ISDIR(inode->i_mode)) {
7393 +               /* shrink_dcache_parent(dentry); */
7394 +               err = au_hide_children(dentry);
7395 +               if (unlikely(err))
7396 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
7397 +                               dentry, err);
7398 +       }
7399 +       au_do_hide(dentry);
7400 +}
7401 +
7402 +/*
7403 + * By adding a dirty branch, a cached dentry may be affected in various ways.
7404 + *
7405 + * a dirty branch is added
7406 + * - on the top of layers
7407 + * - in the middle of layers
7408 + * - to the bottom of layers
7409 + *
7410 + * on the added branch there exists
7411 + * - a whiteout
7412 + * - a diropq
7413 + * - a same named entry
7414 + *   + exist
7415 + *     * negative --> positive
7416 + *     * positive --> positive
7417 + *      - type is unchanged
7418 + *      - type is changed
7419 + *   + doesn't exist
7420 + *     * negative --> negative
7421 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
7422 + * - none
7423 + */
7424 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
7425 +                              struct au_dinfo *tmp)
7426 +{
7427 +       int err;
7428 +       aufs_bindex_t bindex, bend;
7429 +       struct {
7430 +               struct dentry *dentry;
7431 +               struct inode *inode;
7432 +               mode_t mode;
7433 +       } orig_h, tmp_h;
7434 +       struct au_hdentry *hd;
7435 +       struct inode *inode, *h_inode;
7436 +       struct dentry *h_dentry;
7437 +
7438 +       err = 0;
7439 +       AuDebugOn(dinfo->di_bstart < 0);
7440 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
7441 +       orig_h.inode = orig_h.dentry->d_inode;
7442 +       orig_h.mode = 0;
7443 +       if (orig_h.inode)
7444 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
7445 +       memset(&tmp_h, 0, sizeof(tmp_h));
7446 +       if (tmp->di_bstart >= 0) {
7447 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
7448 +               tmp_h.inode = tmp_h.dentry->d_inode;
7449 +               if (tmp_h.inode)
7450 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
7451 +       }
7452 +
7453 +       inode = dentry->d_inode;
7454 +       if (!orig_h.inode) {
7455 +               AuDbg("nagative originally\n");
7456 +               if (inode) {
7457 +                       au_hide(dentry);
7458 +                       goto out;
7459 +               }
7460 +               AuDebugOn(inode);
7461 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
7462 +               AuDebugOn(dinfo->di_bdiropq != -1);
7463 +
7464 +               if (!tmp_h.inode) {
7465 +                       AuDbg("negative --> negative\n");
7466 +                       /* should have only one negative lower */
7467 +                       if (tmp->di_bstart >= 0
7468 +                           && tmp->di_bstart < dinfo->di_bstart) {
7469 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
7470 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
7471 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
7472 +                               au_di_cp(dinfo, tmp);
7473 +                               hd = tmp->di_hdentry + tmp->di_bstart;
7474 +                               au_set_h_dptr(dentry, tmp->di_bstart,
7475 +                                             dget(hd->hd_dentry));
7476 +                       }
7477 +                       au_dbg_verify_dinode(dentry);
7478 +               } else {
7479 +                       AuDbg("negative --> positive\n");
7480 +                       /*
7481 +                        * similar to the behaviour of creating with bypassing
7482 +                        * aufs.
7483 +                        * unhash it in order to force an error in the
7484 +                        * succeeding create operation.
7485 +                        * we should not set S_DEAD here.
7486 +                        */
7487 +                       d_drop(dentry);
7488 +                       /* au_di_swap(tmp, dinfo); */
7489 +                       au_dbg_verify_dinode(dentry);
7490 +               }
7491 +       } else {
7492 +               AuDbg("positive originally\n");
7493 +               /* inode may be NULL */
7494 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
7495 +               if (!tmp_h.inode) {
7496 +                       AuDbg("positive --> negative\n");
7497 +                       /* or bypassing aufs */
7498 +                       au_hide(dentry);
7499 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
7500 +                               dinfo->di_bwh = tmp->di_bwh;
7501 +                       if (inode)
7502 +                               err = au_refresh_hinode_self(inode);
7503 +                       au_dbg_verify_dinode(dentry);
7504 +               } else if (orig_h.mode == tmp_h.mode) {
7505 +                       AuDbg("positive --> positive, same type\n");
7506 +                       if (!S_ISDIR(orig_h.mode)
7507 +                           && dinfo->di_bstart > tmp->di_bstart) {
7508 +                               /*
7509 +                                * similar to the behaviour of removing and
7510 +                                * creating.
7511 +                                */
7512 +                               au_hide(dentry);
7513 +                               if (inode)
7514 +                                       err = au_refresh_hinode_self(inode);
7515 +                               au_dbg_verify_dinode(dentry);
7516 +                       } else {
7517 +                               /* fill empty slots */
7518 +                               if (dinfo->di_bstart > tmp->di_bstart)
7519 +                                       dinfo->di_bstart = tmp->di_bstart;
7520 +                               if (dinfo->di_bend < tmp->di_bend)
7521 +                                       dinfo->di_bend = tmp->di_bend;
7522 +                               dinfo->di_bwh = tmp->di_bwh;
7523 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
7524 +                               hd = tmp->di_hdentry;
7525 +                               bend = dinfo->di_bend;
7526 +                               for (bindex = tmp->di_bstart; bindex <= bend;
7527 +                                    bindex++) {
7528 +                                       if (au_h_dptr(dentry, bindex))
7529 +                                               continue;
7530 +                                       h_dentry = hd[bindex].hd_dentry;
7531 +                                       if (!h_dentry)
7532 +                                               continue;
7533 +                                       h_inode = h_dentry->d_inode;
7534 +                                       AuDebugOn(!h_inode);
7535 +                                       AuDebugOn(orig_h.mode
7536 +                                                 != (h_inode->i_mode
7537 +                                                     & S_IFMT));
7538 +                                       au_set_h_dptr(dentry, bindex,
7539 +                                                     dget(h_dentry));
7540 +                               }
7541 +                               err = au_refresh_hinode(inode, dentry);
7542 +                               au_dbg_verify_dinode(dentry);
7543 +                       }
7544 +               } else {
7545 +                       AuDbg("positive --> positive, different type\n");
7546 +                       /* similar to the behaviour of removing and creating */
7547 +                       au_hide(dentry);
7548 +                       if (inode)
7549 +                               err = au_refresh_hinode_self(inode);
7550 +                       au_dbg_verify_dinode(dentry);
7551 +               }
7552 +       }
7553 +
7554 +out:
7555 +       return err;
7556 +}
7557 +
7558 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
7559 +{
7560 +       int err, ebrange;
7561 +       unsigned int sigen;
7562 +       struct au_dinfo *dinfo, *tmp;
7563 +       struct super_block *sb;
7564 +       struct inode *inode;
7565 +
7566 +       DiMustWriteLock(dentry);
7567 +       AuDebugOn(IS_ROOT(dentry));
7568 +       AuDebugOn(!parent->d_inode);
7569 +
7570 +       sb = dentry->d_sb;
7571 +       inode = dentry->d_inode;
7572 +       sigen = au_sigen(sb);
7573 +       err = au_digen_test(parent, sigen);
7574 +       if (unlikely(err))
7575 +               goto out;
7576 +
7577 +       dinfo = au_di(dentry);
7578 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
7579 +       if (unlikely(err))
7580 +               goto out;
7581 +       ebrange = au_dbrange_test(dentry);
7582 +       if (!ebrange)
7583 +               ebrange = au_do_refresh_hdentry(dentry, parent);
7584 +
7585 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
7586 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
7587 +               if (inode)
7588 +                       err = au_refresh_hinode_self(inode);
7589 +               au_dbg_verify_dinode(dentry);
7590 +               if (!err)
7591 +                       goto out_dgen; /* success */
7592 +               goto out;
7593 +       }
7594 +
7595 +       /* temporary dinfo */
7596 +       AuDbgDentry(dentry);
7597 +       err = -ENOMEM;
7598 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
7599 +       if (unlikely(!tmp))
7600 +               goto out;
7601 +       au_di_swap(tmp, dinfo);
7602 +       /* returns the number of positive dentries */
7603 +       /*
7604 +        * if current working dir is removed, it returns an error.
7605 +        * but the dentry is legal.
7606 +        */
7607 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
7608 +       AuDbgDentry(dentry);
7609 +       au_di_swap(tmp, dinfo);
7610 +       if (err == -ENOENT)
7611 +               err = 0;
7612 +       if (err >= 0) {
7613 +               /* compare/refresh by dinfo */
7614 +               AuDbgDentry(dentry);
7615 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
7616 +               au_dbg_verify_dinode(dentry);
7617 +               AuTraceErr(err);
7618 +       }
7619 +       au_rw_write_unlock(&tmp->di_rwsem);
7620 +       au_di_free(tmp);
7621 +       if (unlikely(err))
7622 +               goto out;
7623 +
7624 +out_dgen:
7625 +       au_update_digen(dentry);
7626 +out:
7627 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
7628 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
7629 +               AuDbgDentry(dentry);
7630 +       }
7631 +       AuTraceErr(err);
7632 +       return err;
7633 +}
7634 +
7635 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
7636 +                          struct dentry *dentry, aufs_bindex_t bindex)
7637 +{
7638 +       int err, valid;
7639 +
7640 +       err = 0;
7641 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
7642 +               goto out;
7643 +
7644 +       AuDbg("b%d\n", bindex);
7645 +       /*
7646 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
7647 +        * due to whiteout and branch permission.
7648 +        */
7649 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
7650 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
7651 +       /* it may return tri-state */
7652 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
7653 +
7654 +       if (unlikely(valid < 0))
7655 +               err = valid;
7656 +       else if (!valid)
7657 +               err = -EINVAL;
7658 +
7659 +out:
7660 +       AuTraceErr(err);
7661 +       return err;
7662 +}
7663 +
7664 +/* todo: remove this */
7665 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
7666 +                         unsigned int flags, int do_udba)
7667 +{
7668 +       int err;
7669 +       umode_t mode, h_mode;
7670 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
7671 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
7672 +       struct inode *h_inode, *h_cached_inode;
7673 +       struct dentry *h_dentry;
7674 +       struct qstr *name, *h_name;
7675 +
7676 +       err = 0;
7677 +       plus = 0;
7678 +       mode = 0;
7679 +       ibs = -1;
7680 +       ibe = -1;
7681 +       unhashed = !!d_unhashed(dentry);
7682 +       is_root = !!IS_ROOT(dentry);
7683 +       name = &dentry->d_name;
7684 +       tmpfile = au_di(dentry)->di_tmpfile;
7685 +
7686 +       /*
7687 +        * Theoretically, REVAL test should be unnecessary in case of
7688 +        * {FS,I}NOTIFY.
7689 +        * But {fs,i}notify doesn't fire some necessary events,
7690 +        *      IN_ATTRIB for atime/nlink/pageio
7691 +        * Let's do REVAL test too.
7692 +        */
7693 +       if (do_udba && inode) {
7694 +               mode = (inode->i_mode & S_IFMT);
7695 +               plus = (inode->i_nlink > 0);
7696 +               ibs = au_ibstart(inode);
7697 +               ibe = au_ibend(inode);
7698 +       }
7699 +
7700 +       bstart = au_dbstart(dentry);
7701 +       btail = bstart;
7702 +       if (inode && S_ISDIR(inode->i_mode))
7703 +               btail = au_dbtaildir(dentry);
7704 +       for (bindex = bstart; bindex <= btail; bindex++) {
7705 +               h_dentry = au_h_dptr(dentry, bindex);
7706 +               if (!h_dentry)
7707 +                       continue;
7708 +
7709 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
7710 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
7711 +               spin_lock(&h_dentry->d_lock);
7712 +               h_name = &h_dentry->d_name;
7713 +               if (unlikely(do_udba
7714 +                            && !is_root
7715 +                            && ((!h_nfs
7716 +                                 && (unhashed != !!d_unhashed(h_dentry)
7717 +                                     || (!tmpfile
7718 +                                         && !au_qstreq(name, h_name))
7719 +                                         ))
7720 +                                || (h_nfs
7721 +                                    && !(flags & LOOKUP_OPEN)
7722 +                                    && (h_dentry->d_flags
7723 +                                        & DCACHE_NFSFS_RENAMED)))
7724 +                           )) {
7725 +                       int h_unhashed;
7726 +
7727 +                       h_unhashed = d_unhashed(h_dentry);
7728 +                       spin_unlock(&h_dentry->d_lock);
7729 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
7730 +                             unhashed, h_unhashed, dentry, h_dentry);
7731 +                       goto err;
7732 +               }
7733 +               spin_unlock(&h_dentry->d_lock);
7734 +
7735 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
7736 +               if (unlikely(err))
7737 +                       /* do not goto err, to keep the errno */
7738 +                       break;
7739 +
7740 +               /* todo: plink too? */
7741 +               if (!do_udba)
7742 +                       continue;
7743 +
7744 +               /* UDBA tests */
7745 +               h_inode = h_dentry->d_inode;
7746 +               if (unlikely(!!inode != !!h_inode))
7747 +                       goto err;
7748 +
7749 +               h_plus = plus;
7750 +               h_mode = mode;
7751 +               h_cached_inode = h_inode;
7752 +               if (h_inode) {
7753 +                       h_mode = (h_inode->i_mode & S_IFMT);
7754 +                       h_plus = (h_inode->i_nlink > 0);
7755 +               }
7756 +               if (inode && ibs <= bindex && bindex <= ibe)
7757 +                       h_cached_inode = au_h_iptr(inode, bindex);
7758 +
7759 +               if (!h_nfs) {
7760 +                       if (unlikely(plus != h_plus && !tmpfile))
7761 +                               goto err;
7762 +               } else {
7763 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
7764 +                                    && !is_root
7765 +                                    && !IS_ROOT(h_dentry)
7766 +                                    && unhashed != d_unhashed(h_dentry)))
7767 +                               goto err;
7768 +               }
7769 +               if (unlikely(mode != h_mode
7770 +                            || h_cached_inode != h_inode))
7771 +                       goto err;
7772 +               continue;
7773 +
7774 +err:
7775 +               err = -EINVAL;
7776 +               break;
7777 +       }
7778 +
7779 +       AuTraceErr(err);
7780 +       return err;
7781 +}
7782 +
7783 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
7784 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
7785 +{
7786 +       int err;
7787 +       struct dentry *parent;
7788 +
7789 +       if (!au_digen_test(dentry, sigen))
7790 +               return 0;
7791 +
7792 +       parent = dget_parent(dentry);
7793 +       di_read_lock_parent(parent, AuLock_IR);
7794 +       AuDebugOn(au_digen_test(parent, sigen));
7795 +       au_dbg_verify_gen(parent, sigen);
7796 +       err = au_refresh_dentry(dentry, parent);
7797 +       di_read_unlock(parent, AuLock_IR);
7798 +       dput(parent);
7799 +       AuTraceErr(err);
7800 +       return err;
7801 +}
7802 +
7803 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
7804 +{
7805 +       int err;
7806 +       struct dentry *d, *parent;
7807 +       struct inode *inode;
7808 +
7809 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
7810 +               return simple_reval_dpath(dentry, sigen);
7811 +
7812 +       /* slow loop, keep it simple and stupid */
7813 +       /* cf: au_cpup_dirs() */
7814 +       err = 0;
7815 +       parent = NULL;
7816 +       while (au_digen_test(dentry, sigen)) {
7817 +               d = dentry;
7818 +               while (1) {
7819 +                       dput(parent);
7820 +                       parent = dget_parent(d);
7821 +                       if (!au_digen_test(parent, sigen))
7822 +                               break;
7823 +                       d = parent;
7824 +               }
7825 +
7826 +               inode = d->d_inode;
7827 +               if (d != dentry)
7828 +                       di_write_lock_child2(d);
7829 +
7830 +               /* someone might update our dentry while we were sleeping */
7831 +               if (au_digen_test(d, sigen)) {
7832 +                       /*
7833 +                        * todo: consolidate with simple_reval_dpath(),
7834 +                        * do_refresh() and au_reval_for_attr().
7835 +                        */
7836 +                       di_read_lock_parent(parent, AuLock_IR);
7837 +                       err = au_refresh_dentry(d, parent);
7838 +                       di_read_unlock(parent, AuLock_IR);
7839 +               }
7840 +
7841 +               if (d != dentry)
7842 +                       di_write_unlock(d);
7843 +               dput(parent);
7844 +               if (unlikely(err))
7845 +                       break;
7846 +       }
7847 +
7848 +       return err;
7849 +}
7850 +
7851 +/*
7852 + * if valid returns 1, otherwise 0.
7853 + */
7854 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
7855 +{
7856 +       int valid, err;
7857 +       unsigned int sigen;
7858 +       unsigned char do_udba;
7859 +       struct super_block *sb;
7860 +       struct inode *inode;
7861 +
7862 +       /* todo: support rcu-walk? */
7863 +       if (flags & LOOKUP_RCU)
7864 +               return -ECHILD;
7865 +
7866 +       valid = 0;
7867 +       if (unlikely(!au_di(dentry)))
7868 +               goto out;
7869 +
7870 +       inode = dentry->d_inode;
7871 +       if (inode && is_bad_inode(inode))
7872 +               goto out;
7873 +
7874 +       valid = 1;
7875 +       sb = dentry->d_sb;
7876 +       /*
7877 +        * todo: very ugly
7878 +        * i_mutex of parent dir may be held,
7879 +        * but we should not return 'invalid' due to busy.
7880 +        */
7881 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
7882 +       if (unlikely(err)) {
7883 +               valid = err;
7884 +               AuTraceErr(err);
7885 +               goto out;
7886 +       }
7887 +       if (unlikely(au_dbrange_test(dentry))) {
7888 +               err = -EINVAL;
7889 +               AuTraceErr(err);
7890 +               goto out_dgrade;
7891 +       }
7892 +
7893 +       sigen = au_sigen(sb);
7894 +       if (au_digen_test(dentry, sigen)) {
7895 +               AuDebugOn(IS_ROOT(dentry));
7896 +               err = au_reval_dpath(dentry, sigen);
7897 +               if (unlikely(err)) {
7898 +                       AuTraceErr(err);
7899 +                       goto out_dgrade;
7900 +               }
7901 +       }
7902 +       di_downgrade_lock(dentry, AuLock_IR);
7903 +
7904 +       err = -EINVAL;
7905 +       if (!(flags & LOOKUP_OPEN)
7906 +           && inode
7907 +           && !(inode->i_state && I_LINKABLE)
7908 +           && (IS_DEADDIR(inode) || !inode->i_nlink))
7909 +               goto out_inval;
7910 +
7911 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
7912 +       if (do_udba && inode) {
7913 +               aufs_bindex_t bstart = au_ibstart(inode);
7914 +               struct inode *h_inode;
7915 +
7916 +               if (bstart >= 0) {
7917 +                       h_inode = au_h_iptr(inode, bstart);
7918 +                       if (h_inode && au_test_higen(inode, h_inode))
7919 +                               goto out_inval;
7920 +               }
7921 +       }
7922 +
7923 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
7924 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
7925 +               err = -EIO;
7926 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
7927 +                     dentry, err);
7928 +       }
7929 +       goto out_inval;
7930 +
7931 +out_dgrade:
7932 +       di_downgrade_lock(dentry, AuLock_IR);
7933 +out_inval:
7934 +       aufs_read_unlock(dentry, AuLock_IR);
7935 +       AuTraceErr(err);
7936 +       valid = !err;
7937 +out:
7938 +       if (!valid) {
7939 +               AuDbg("%pd invalid, %d\n", dentry, valid);
7940 +               d_drop(dentry);
7941 +       }
7942 +       return valid;
7943 +}
7944 +
7945 +static void aufs_d_release(struct dentry *dentry)
7946 +{
7947 +       if (au_di(dentry)) {
7948 +               au_di_fin(dentry);
7949 +               au_hn_di_reinit(dentry);
7950 +       }
7951 +}
7952 +
7953 +const struct dentry_operations aufs_dop = {
7954 +       .d_revalidate           = aufs_d_revalidate,
7955 +       .d_weak_revalidate      = aufs_d_revalidate,
7956 +       .d_release              = aufs_d_release
7957 +};
7958 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
7959 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
7960 +++ linux/fs/aufs/dentry.h      2014-06-24 00:14:06.779385790 +0200
7961 @@ -0,0 +1,234 @@
7962 +/*
7963 + * Copyright (C) 2005-2014 Junjiro R. Okajima
7964 + *
7965 + * This program, aufs is free software; you can redistribute it and/or modify
7966 + * it under the terms of the GNU General Public License as published by
7967 + * the Free Software Foundation; either version 2 of the License, or
7968 + * (at your option) any later version.
7969 + *
7970 + * This program is distributed in the hope that it will be useful,
7971 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7972 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7973 + * GNU General Public License for more details.
7974 + *
7975 + * You should have received a copy of the GNU General Public License
7976 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7977 + */
7978 +
7979 +/*
7980 + * lookup and dentry operations
7981 + */
7982 +
7983 +#ifndef __AUFS_DENTRY_H__
7984 +#define __AUFS_DENTRY_H__
7985 +
7986 +#ifdef __KERNEL__
7987 +
7988 +#include <linux/dcache.h>
7989 +#include "rwsem.h"
7990 +
7991 +struct au_hdentry {
7992 +       struct dentry           *hd_dentry;
7993 +       aufs_bindex_t           hd_id;
7994 +};
7995 +
7996 +struct au_dinfo {
7997 +       atomic_t                di_generation;
7998 +
7999 +       struct au_rwsem         di_rwsem;
8000 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
8001 +       unsigned char           di_tmpfile; /* to allow the different name */
8002 +       struct au_hdentry       *di_hdentry;
8003 +} ____cacheline_aligned_in_smp;
8004 +
8005 +/* ---------------------------------------------------------------------- */
8006 +
8007 +/* dentry.c */
8008 +extern const struct dentry_operations aufs_dop;
8009 +struct au_branch;
8010 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
8011 +                              struct au_branch *br);
8012 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8013 +               struct dentry *h_parent, struct au_branch *br);
8014 +
8015 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
8016 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
8017 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
8018 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
8019 +
8020 +/* dinfo.c */
8021 +void au_di_init_once(void *_di);
8022 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8023 +void au_di_free(struct au_dinfo *dinfo);
8024 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8025 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
8026 +int au_di_init(struct dentry *dentry);
8027 +void au_di_fin(struct dentry *dentry);
8028 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
8029 +
8030 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8031 +void di_read_unlock(struct dentry *d, int flags);
8032 +void di_downgrade_lock(struct dentry *d, int flags);
8033 +void di_write_lock(struct dentry *d, unsigned int lsc);
8034 +void di_write_unlock(struct dentry *d);
8035 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8036 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8037 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8038 +
8039 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
8040 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
8041 +aufs_bindex_t au_dbtail(struct dentry *dentry);
8042 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8043 +
8044 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8045 +                  struct dentry *h_dentry);
8046 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
8047 +int au_dbrange_test(struct dentry *dentry);
8048 +void au_update_digen(struct dentry *dentry);
8049 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
8050 +void au_update_dbstart(struct dentry *dentry);
8051 +void au_update_dbend(struct dentry *dentry);
8052 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8053 +
8054 +/* ---------------------------------------------------------------------- */
8055 +
8056 +static inline struct au_dinfo *au_di(struct dentry *dentry)
8057 +{
8058 +       return dentry->d_fsdata;
8059 +}
8060 +
8061 +/* ---------------------------------------------------------------------- */
8062 +
8063 +/* lock subclass for dinfo */
8064 +enum {
8065 +       AuLsc_DI_CHILD,         /* child first */
8066 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
8067 +       AuLsc_DI_CHILD3,        /* copyup dirs */
8068 +       AuLsc_DI_PARENT,
8069 +       AuLsc_DI_PARENT2,
8070 +       AuLsc_DI_PARENT3,
8071 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
8072 +};
8073 +
8074 +/*
8075 + * di_read_lock_child, di_write_lock_child,
8076 + * di_read_lock_child2, di_write_lock_child2,
8077 + * di_read_lock_child3, di_write_lock_child3,
8078 + * di_read_lock_parent, di_write_lock_parent,
8079 + * di_read_lock_parent2, di_write_lock_parent2,
8080 + * di_read_lock_parent3, di_write_lock_parent3,
8081 + */
8082 +#define AuReadLockFunc(name, lsc) \
8083 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
8084 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8085 +
8086 +#define AuWriteLockFunc(name, lsc) \
8087 +static inline void di_write_lock_##name(struct dentry *d) \
8088 +{ di_write_lock(d, AuLsc_DI_##lsc); }
8089 +
8090 +#define AuRWLockFuncs(name, lsc) \
8091 +       AuReadLockFunc(name, lsc) \
8092 +       AuWriteLockFunc(name, lsc)
8093 +
8094 +AuRWLockFuncs(child, CHILD);
8095 +AuRWLockFuncs(child2, CHILD2);
8096 +AuRWLockFuncs(child3, CHILD3);
8097 +AuRWLockFuncs(parent, PARENT);
8098 +AuRWLockFuncs(parent2, PARENT2);
8099 +AuRWLockFuncs(parent3, PARENT3);
8100 +
8101 +#undef AuReadLockFunc
8102 +#undef AuWriteLockFunc
8103 +#undef AuRWLockFuncs
8104 +
8105 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
8106 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
8107 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
8108 +
8109 +/* ---------------------------------------------------------------------- */
8110 +
8111 +/* todo: memory barrier? */
8112 +static inline unsigned int au_digen(struct dentry *d)
8113 +{
8114 +       return atomic_read(&au_di(d)->di_generation);
8115 +}
8116 +
8117 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
8118 +{
8119 +       hdentry->hd_dentry = NULL;
8120 +}
8121 +
8122 +static inline void au_hdput(struct au_hdentry *hd)
8123 +{
8124 +       if (hd)
8125 +               dput(hd->hd_dentry);
8126 +}
8127 +
8128 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
8129 +{
8130 +       DiMustAnyLock(dentry);
8131 +       return au_di(dentry)->di_bstart;
8132 +}
8133 +
8134 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
8135 +{
8136 +       DiMustAnyLock(dentry);
8137 +       return au_di(dentry)->di_bend;
8138 +}
8139 +
8140 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
8141 +{
8142 +       DiMustAnyLock(dentry);
8143 +       return au_di(dentry)->di_bwh;
8144 +}
8145 +
8146 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
8147 +{
8148 +       DiMustAnyLock(dentry);
8149 +       return au_di(dentry)->di_bdiropq;
8150 +}
8151 +
8152 +/* todo: hard/soft set? */
8153 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
8154 +{
8155 +       DiMustWriteLock(dentry);
8156 +       au_di(dentry)->di_bstart = bindex;
8157 +}
8158 +
8159 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
8160 +{
8161 +       DiMustWriteLock(dentry);
8162 +       au_di(dentry)->di_bend = bindex;
8163 +}
8164 +
8165 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
8166 +{
8167 +       DiMustWriteLock(dentry);
8168 +       /* dbwh can be outside of bstart - bend range */
8169 +       au_di(dentry)->di_bwh = bindex;
8170 +}
8171 +
8172 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
8173 +{
8174 +       DiMustWriteLock(dentry);
8175 +       au_di(dentry)->di_bdiropq = bindex;
8176 +}
8177 +
8178 +/* ---------------------------------------------------------------------- */
8179 +
8180 +#ifdef CONFIG_AUFS_HNOTIFY
8181 +static inline void au_digen_dec(struct dentry *d)
8182 +{
8183 +       atomic_dec(&au_di(d)->di_generation);
8184 +}
8185 +
8186 +static inline void au_hn_di_reinit(struct dentry *dentry)
8187 +{
8188 +       dentry->d_fsdata = NULL;
8189 +}
8190 +#else
8191 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
8192 +#endif /* CONFIG_AUFS_HNOTIFY */
8193 +
8194 +#endif /* __KERNEL__ */
8195 +#endif /* __AUFS_DENTRY_H__ */
8196 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
8197 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
8198 +++ linux/fs/aufs/dinfo.c       2014-06-24 00:14:06.779385790 +0200
8199 @@ -0,0 +1,544 @@
8200 +/*
8201 + * Copyright (C) 2005-2014 Junjiro R. Okajima
8202 + *
8203 + * This program, aufs is free software; you can redistribute it and/or modify
8204 + * it under the terms of the GNU General Public License as published by
8205 + * the Free Software Foundation; either version 2 of the License, or
8206 + * (at your option) any later version.
8207 + *
8208 + * This program is distributed in the hope that it will be useful,
8209 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8210 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8211 + * GNU General Public License for more details.
8212 + *
8213 + * You should have received a copy of the GNU General Public License
8214 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8215 + */
8216 +
8217 +/*
8218 + * dentry private data
8219 + */
8220 +
8221 +#include "aufs.h"
8222 +
8223 +void au_di_init_once(void *_dinfo)
8224 +{
8225 +       struct au_dinfo *dinfo = _dinfo;
8226 +       static struct lock_class_key aufs_di;
8227 +
8228 +       au_rw_init(&dinfo->di_rwsem);
8229 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
8230 +}
8231 +
8232 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
8233 +{
8234 +       struct au_dinfo *dinfo;
8235 +       int nbr, i;
8236 +
8237 +       dinfo = au_cache_alloc_dinfo();
8238 +       if (unlikely(!dinfo))
8239 +               goto out;
8240 +
8241 +       nbr = au_sbend(sb) + 1;
8242 +       if (nbr <= 0)
8243 +               nbr = 1;
8244 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
8245 +       if (dinfo->di_hdentry) {
8246 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
8247 +               dinfo->di_bstart = -1;
8248 +               dinfo->di_bend = -1;
8249 +               dinfo->di_bwh = -1;
8250 +               dinfo->di_bdiropq = -1;
8251 +               dinfo->di_tmpfile = 0;
8252 +               for (i = 0; i < nbr; i++)
8253 +                       dinfo->di_hdentry[i].hd_id = -1;
8254 +               goto out;
8255 +       }
8256 +
8257 +       au_cache_free_dinfo(dinfo);
8258 +       dinfo = NULL;
8259 +
8260 +out:
8261 +       return dinfo;
8262 +}
8263 +
8264 +void au_di_free(struct au_dinfo *dinfo)
8265 +{
8266 +       struct au_hdentry *p;
8267 +       aufs_bindex_t bend, bindex;
8268 +
8269 +       /* dentry may not be revalidated */
8270 +       bindex = dinfo->di_bstart;
8271 +       if (bindex >= 0) {
8272 +               bend = dinfo->di_bend;
8273 +               p = dinfo->di_hdentry + bindex;
8274 +               while (bindex++ <= bend)
8275 +                       au_hdput(p++);
8276 +       }
8277 +       kfree(dinfo->di_hdentry);
8278 +       au_cache_free_dinfo(dinfo);
8279 +}
8280 +
8281 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
8282 +{
8283 +       struct au_hdentry *p;
8284 +       aufs_bindex_t bi;
8285 +
8286 +       AuRwMustWriteLock(&a->di_rwsem);
8287 +       AuRwMustWriteLock(&b->di_rwsem);
8288 +
8289 +#define DiSwap(v, name)                                \
8290 +       do {                                    \
8291 +               v = a->di_##name;               \
8292 +               a->di_##name = b->di_##name;    \
8293 +               b->di_##name = v;               \
8294 +       } while (0)
8295 +
8296 +       DiSwap(p, hdentry);
8297 +       DiSwap(bi, bstart);
8298 +       DiSwap(bi, bend);
8299 +       DiSwap(bi, bwh);
8300 +       DiSwap(bi, bdiropq);
8301 +       /* smp_mb(); */
8302 +
8303 +#undef DiSwap
8304 +}
8305 +
8306 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
8307 +{
8308 +       AuRwMustWriteLock(&dst->di_rwsem);
8309 +       AuRwMustWriteLock(&src->di_rwsem);
8310 +
8311 +       dst->di_bstart = src->di_bstart;
8312 +       dst->di_bend = src->di_bend;
8313 +       dst->di_bwh = src->di_bwh;
8314 +       dst->di_bdiropq = src->di_bdiropq;
8315 +       /* smp_mb(); */
8316 +}
8317 +
8318 +int au_di_init(struct dentry *dentry)
8319 +{
8320 +       int err;
8321 +       struct super_block *sb;
8322 +       struct au_dinfo *dinfo;
8323 +
8324 +       err = 0;
8325 +       sb = dentry->d_sb;
8326 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
8327 +       if (dinfo) {
8328 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
8329 +               /* smp_mb(); */ /* atomic_set */
8330 +               dentry->d_fsdata = dinfo;
8331 +       } else
8332 +               err = -ENOMEM;
8333 +
8334 +       return err;
8335 +}
8336 +
8337 +void au_di_fin(struct dentry *dentry)
8338 +{
8339 +       struct au_dinfo *dinfo;
8340 +
8341 +       dinfo = au_di(dentry);
8342 +       AuRwDestroy(&dinfo->di_rwsem);
8343 +       au_di_free(dinfo);
8344 +}
8345 +
8346 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
8347 +{
8348 +       int err, sz;
8349 +       struct au_hdentry *hdp;
8350 +
8351 +       AuRwMustWriteLock(&dinfo->di_rwsem);
8352 +
8353 +       err = -ENOMEM;
8354 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
8355 +       if (!sz)
8356 +               sz = sizeof(*hdp);
8357 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
8358 +       if (hdp) {
8359 +               dinfo->di_hdentry = hdp;
8360 +               err = 0;
8361 +       }
8362 +
8363 +       return err;
8364 +}
8365 +
8366 +/* ---------------------------------------------------------------------- */
8367 +
8368 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
8369 +{
8370 +       switch (lsc) {
8371 +       case AuLsc_DI_CHILD:
8372 +               ii_write_lock_child(inode);
8373 +               break;
8374 +       case AuLsc_DI_CHILD2:
8375 +               ii_write_lock_child2(inode);
8376 +               break;
8377 +       case AuLsc_DI_CHILD3:
8378 +               ii_write_lock_child3(inode);
8379 +               break;
8380 +       case AuLsc_DI_PARENT:
8381 +               ii_write_lock_parent(inode);
8382 +               break;
8383 +       case AuLsc_DI_PARENT2:
8384 +               ii_write_lock_parent2(inode);
8385 +               break;
8386 +       case AuLsc_DI_PARENT3:
8387 +               ii_write_lock_parent3(inode);
8388 +               break;
8389 +       default:
8390 +               BUG();
8391 +       }
8392 +}
8393 +
8394 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
8395 +{
8396 +       switch (lsc) {
8397 +       case AuLsc_DI_CHILD:
8398 +               ii_read_lock_child(inode);
8399 +               break;
8400 +       case AuLsc_DI_CHILD2:
8401 +               ii_read_lock_child2(inode);
8402 +               break;
8403 +       case AuLsc_DI_CHILD3:
8404 +               ii_read_lock_child3(inode);
8405 +               break;
8406 +       case AuLsc_DI_PARENT:
8407 +               ii_read_lock_parent(inode);
8408 +               break;
8409 +       case AuLsc_DI_PARENT2:
8410 +               ii_read_lock_parent2(inode);
8411 +               break;
8412 +       case AuLsc_DI_PARENT3:
8413 +               ii_read_lock_parent3(inode);
8414 +               break;
8415 +       default:
8416 +               BUG();
8417 +       }
8418 +}
8419 +
8420 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
8421 +{
8422 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
8423 +       if (d->d_inode) {
8424 +               if (au_ftest_lock(flags, IW))
8425 +                       do_ii_write_lock(d->d_inode, lsc);
8426 +               else if (au_ftest_lock(flags, IR))
8427 +                       do_ii_read_lock(d->d_inode, lsc);
8428 +       }
8429 +}
8430 +
8431 +void di_read_unlock(struct dentry *d, int flags)
8432 +{
8433 +       if (d->d_inode) {
8434 +               if (au_ftest_lock(flags, IW)) {
8435 +                       au_dbg_verify_dinode(d);
8436 +                       ii_write_unlock(d->d_inode);
8437 +               } else if (au_ftest_lock(flags, IR)) {
8438 +                       au_dbg_verify_dinode(d);
8439 +                       ii_read_unlock(d->d_inode);
8440 +               }
8441 +       }
8442 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
8443 +}
8444 +
8445 +void di_downgrade_lock(struct dentry *d, int flags)
8446 +{
8447 +       if (d->d_inode && au_ftest_lock(flags, IR))
8448 +               ii_downgrade_lock(d->d_inode);
8449 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
8450 +}
8451 +
8452 +void di_write_lock(struct dentry *d, unsigned int lsc)
8453 +{
8454 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
8455 +       if (d->d_inode)
8456 +               do_ii_write_lock(d->d_inode, lsc);
8457 +}
8458 +
8459 +void di_write_unlock(struct dentry *d)
8460 +{
8461 +       au_dbg_verify_dinode(d);
8462 +       if (d->d_inode)
8463 +               ii_write_unlock(d->d_inode);
8464 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
8465 +}
8466 +
8467 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
8468 +{
8469 +       AuDebugOn(d1 == d2
8470 +                 || d1->d_inode == d2->d_inode
8471 +                 || d1->d_sb != d2->d_sb);
8472 +
8473 +       if (isdir && au_test_subdir(d1, d2)) {
8474 +               di_write_lock_child(d1);
8475 +               di_write_lock_child2(d2);
8476 +       } else {
8477 +               /* there should be no races */
8478 +               di_write_lock_child(d2);
8479 +               di_write_lock_child2(d1);
8480 +       }
8481 +}
8482 +
8483 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
8484 +{
8485 +       AuDebugOn(d1 == d2
8486 +                 || d1->d_inode == d2->d_inode
8487 +                 || d1->d_sb != d2->d_sb);
8488 +
8489 +       if (isdir && au_test_subdir(d1, d2)) {
8490 +               di_write_lock_parent(d1);
8491 +               di_write_lock_parent2(d2);
8492 +       } else {
8493 +               /* there should be no races */
8494 +               di_write_lock_parent(d2);
8495 +               di_write_lock_parent2(d1);
8496 +       }
8497 +}
8498 +
8499 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
8500 +{
8501 +       di_write_unlock(d1);
8502 +       if (d1->d_inode == d2->d_inode)
8503 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
8504 +       else
8505 +               di_write_unlock(d2);
8506 +}
8507 +
8508 +/* ---------------------------------------------------------------------- */
8509 +
8510 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
8511 +{
8512 +       struct dentry *d;
8513 +
8514 +       DiMustAnyLock(dentry);
8515 +
8516 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
8517 +               return NULL;
8518 +       AuDebugOn(bindex < 0);
8519 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
8520 +       AuDebugOn(d && d_count(d) <= 0);
8521 +       return d;
8522 +}
8523 +
8524 +/*
8525 + * extended version of au_h_dptr().
8526 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
8527 + * error.
8528 + */
8529 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
8530 +{
8531 +       struct dentry *h_dentry;
8532 +       struct inode *inode, *h_inode;
8533 +
8534 +       inode = dentry->d_inode;
8535 +       AuDebugOn(!inode);
8536 +
8537 +       h_dentry = NULL;
8538 +       if (au_dbstart(dentry) <= bindex
8539 +           && bindex <= au_dbend(dentry))
8540 +               h_dentry = au_h_dptr(dentry, bindex);
8541 +       if (h_dentry && !au_d_linkable(h_dentry)) {
8542 +               dget(h_dentry);
8543 +               goto out; /* success */
8544 +       }
8545 +
8546 +       AuDebugOn(bindex < au_ibstart(inode));
8547 +       AuDebugOn(au_ibend(inode) < bindex);
8548 +       h_inode = au_h_iptr(inode, bindex);
8549 +       h_dentry = d_find_alias(h_inode);
8550 +       if (h_dentry) {
8551 +               if (!IS_ERR(h_dentry)) {
8552 +                       if (!au_d_linkable(h_dentry))
8553 +                               goto out; /* success */
8554 +                       dput(h_dentry);
8555 +               } else
8556 +                       goto out;
8557 +       }
8558 +
8559 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
8560 +               h_dentry = au_plink_lkup(inode, bindex);
8561 +               AuDebugOn(!h_dentry);
8562 +               if (!IS_ERR(h_dentry)) {
8563 +                       if (!au_d_hashed_positive(h_dentry))
8564 +                               goto out; /* success */
8565 +                       dput(h_dentry);
8566 +                       h_dentry = NULL;
8567 +               }
8568 +       }
8569 +
8570 +out:
8571 +       AuDbgDentry(h_dentry);
8572 +       return h_dentry;
8573 +}
8574 +
8575 +aufs_bindex_t au_dbtail(struct dentry *dentry)
8576 +{
8577 +       aufs_bindex_t bend, bwh;
8578 +
8579 +       bend = au_dbend(dentry);
8580 +       if (0 <= bend) {
8581 +               bwh = au_dbwh(dentry);
8582 +               if (!bwh)
8583 +                       return bwh;
8584 +               if (0 < bwh && bwh < bend)
8585 +                       return bwh - 1;
8586 +       }
8587 +       return bend;
8588 +}
8589 +
8590 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
8591 +{
8592 +       aufs_bindex_t bend, bopq;
8593 +
8594 +       bend = au_dbtail(dentry);
8595 +       if (0 <= bend) {
8596 +               bopq = au_dbdiropq(dentry);
8597 +               if (0 <= bopq && bopq < bend)
8598 +                       bend = bopq;
8599 +       }
8600 +       return bend;
8601 +}
8602 +
8603 +/* ---------------------------------------------------------------------- */
8604 +
8605 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8606 +                  struct dentry *h_dentry)
8607 +{
8608 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
8609 +       struct au_branch *br;
8610 +
8611 +       DiMustWriteLock(dentry);
8612 +
8613 +       au_hdput(hd);
8614 +       hd->hd_dentry = h_dentry;
8615 +       if (h_dentry) {
8616 +               br = au_sbr(dentry->d_sb, bindex);
8617 +               hd->hd_id = br->br_id;
8618 +       }
8619 +}
8620 +
8621 +int au_dbrange_test(struct dentry *dentry)
8622 +{
8623 +       int err;
8624 +       aufs_bindex_t bstart, bend;
8625 +
8626 +       err = 0;
8627 +       bstart = au_dbstart(dentry);
8628 +       bend = au_dbend(dentry);
8629 +       if (bstart >= 0)
8630 +               AuDebugOn(bend < 0 && bstart > bend);
8631 +       else {
8632 +               err = -EIO;
8633 +               AuDebugOn(bend >= 0);
8634 +       }
8635 +
8636 +       return err;
8637 +}
8638 +
8639 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
8640 +{
8641 +       int err;
8642 +
8643 +       err = 0;
8644 +       if (unlikely(au_digen(dentry) != sigen
8645 +                    || au_iigen_test(dentry->d_inode, sigen)))
8646 +               err = -EIO;
8647 +
8648 +       return err;
8649 +}
8650 +
8651 +void au_update_digen(struct dentry *dentry)
8652 +{
8653 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
8654 +       /* smp_mb(); */ /* atomic_set */
8655 +}
8656 +
8657 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
8658 +{
8659 +       struct au_dinfo *dinfo;
8660 +       struct dentry *h_d;
8661 +       struct au_hdentry *hdp;
8662 +
8663 +       DiMustWriteLock(dentry);
8664 +
8665 +       dinfo = au_di(dentry);
8666 +       if (!dinfo || dinfo->di_bstart < 0)
8667 +               return;
8668 +
8669 +       hdp = dinfo->di_hdentry;
8670 +       if (do_put_zero) {
8671 +               aufs_bindex_t bindex, bend;
8672 +
8673 +               bend = dinfo->di_bend;
8674 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
8675 +                       h_d = hdp[0 + bindex].hd_dentry;
8676 +                       if (h_d && !h_d->d_inode)
8677 +                               au_set_h_dptr(dentry, bindex, NULL);
8678 +               }
8679 +       }
8680 +
8681 +       dinfo->di_bstart = -1;
8682 +       while (++dinfo->di_bstart <= dinfo->di_bend)
8683 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
8684 +                       break;
8685 +       if (dinfo->di_bstart > dinfo->di_bend) {
8686 +               dinfo->di_bstart = -1;
8687 +               dinfo->di_bend = -1;
8688 +               return;
8689 +       }
8690 +
8691 +       dinfo->di_bend++;
8692 +       while (0 <= --dinfo->di_bend)
8693 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
8694 +                       break;
8695 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
8696 +}
8697 +
8698 +void au_update_dbstart(struct dentry *dentry)
8699 +{
8700 +       aufs_bindex_t bindex, bend;
8701 +       struct dentry *h_dentry;
8702 +
8703 +       bend = au_dbend(dentry);
8704 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
8705 +               h_dentry = au_h_dptr(dentry, bindex);
8706 +               if (!h_dentry)
8707 +                       continue;
8708 +               if (h_dentry->d_inode) {
8709 +                       au_set_dbstart(dentry, bindex);
8710 +                       return;
8711 +               }
8712 +               au_set_h_dptr(dentry, bindex, NULL);
8713 +       }
8714 +}
8715 +
8716 +void au_update_dbend(struct dentry *dentry)
8717 +{
8718 +       aufs_bindex_t bindex, bstart;
8719 +       struct dentry *h_dentry;
8720 +
8721 +       bstart = au_dbstart(dentry);
8722 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
8723 +               h_dentry = au_h_dptr(dentry, bindex);
8724 +               if (!h_dentry)
8725 +                       continue;
8726 +               if (h_dentry->d_inode) {
8727 +                       au_set_dbend(dentry, bindex);
8728 +                       return;
8729 +               }
8730 +               au_set_h_dptr(dentry, bindex, NULL);
8731 +       }
8732 +}
8733 +
8734 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
8735 +{
8736 +       aufs_bindex_t bindex, bend;
8737 +
8738 +       bend = au_dbend(dentry);
8739 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
8740 +               if (au_h_dptr(dentry, bindex) == h_dentry)
8741 +                       return bindex;
8742 +       return -1;
8743 +}
8744 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
8745 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
8746 +++ linux/fs/aufs/dir.c 2014-06-24 00:14:06.779385790 +0200
8747 @@ -0,0 +1,639 @@
8748 +/*
8749 + * Copyright (C) 2005-2014 Junjiro R. Okajima
8750 + *
8751 + * This program, aufs is free software; you can redistribute it and/or modify
8752 + * it under the terms of the GNU General Public License as published by
8753 + * the Free Software Foundation; either version 2 of the License, or
8754 + * (at your option) any later version.
8755 + *
8756 + * This program is distributed in the hope that it will be useful,
8757 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8758 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8759 + * GNU General Public License for more details.
8760 + *
8761 + * You should have received a copy of the GNU General Public License
8762 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8763 + */
8764 +
8765 +/*
8766 + * directory operations
8767 + */
8768 +
8769 +#include <linux/fs_stack.h>
8770 +#include "aufs.h"
8771 +
8772 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
8773 +{
8774 +       unsigned int nlink;
8775 +
8776 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8777 +
8778 +       nlink = dir->i_nlink;
8779 +       nlink += h_dir->i_nlink - 2;
8780 +       if (h_dir->i_nlink < 2)
8781 +               nlink += 2;
8782 +       smp_mb(); /* for i_nlink */
8783 +       /* 0 can happen in revaliding */
8784 +       set_nlink(dir, nlink);
8785 +}
8786 +
8787 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
8788 +{
8789 +       unsigned int nlink;
8790 +
8791 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
8792 +
8793 +       nlink = dir->i_nlink;
8794 +       nlink -= h_dir->i_nlink - 2;
8795 +       if (h_dir->i_nlink < 2)
8796 +               nlink -= 2;
8797 +       smp_mb(); /* for i_nlink */
8798 +       /* nlink == 0 means the branch-fs is broken */
8799 +       set_nlink(dir, nlink);
8800 +}
8801 +
8802 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
8803 +{
8804 +       loff_t sz;
8805 +       aufs_bindex_t bindex, bend;
8806 +       struct file *h_file;
8807 +       struct dentry *h_dentry;
8808 +
8809 +       sz = 0;
8810 +       if (file) {
8811 +               AuDebugOn(!file_inode(file));
8812 +               AuDebugOn(!S_ISDIR(file_inode(file)->i_mode));
8813 +
8814 +               bend = au_fbend_dir(file);
8815 +               for (bindex = au_fbstart(file);
8816 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
8817 +                    bindex++) {
8818 +                       h_file = au_hf_dir(file, bindex);
8819 +                       if (h_file && file_inode(h_file))
8820 +                               sz += vfsub_f_size_read(h_file);
8821 +               }
8822 +       } else {
8823 +               AuDebugOn(!dentry);
8824 +               AuDebugOn(!dentry->d_inode);
8825 +               AuDebugOn(!S_ISDIR(dentry->d_inode->i_mode));
8826 +
8827 +               bend = au_dbtaildir(dentry);
8828 +               for (bindex = au_dbstart(dentry);
8829 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
8830 +                    bindex++) {
8831 +                       h_dentry = au_h_dptr(dentry, bindex);
8832 +                       if (h_dentry && h_dentry->d_inode)
8833 +                               sz += i_size_read(h_dentry->d_inode);
8834 +               }
8835 +       }
8836 +       if (sz < KMALLOC_MAX_SIZE)
8837 +               sz = roundup_pow_of_two(sz);
8838 +       if (sz > KMALLOC_MAX_SIZE)
8839 +               sz = KMALLOC_MAX_SIZE;
8840 +       else if (sz < NAME_MAX) {
8841 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
8842 +               sz = AUFS_RDBLK_DEF;
8843 +       }
8844 +       return sz;
8845 +}
8846 +
8847 +/* ---------------------------------------------------------------------- */
8848 +
8849 +static int reopen_dir(struct file *file)
8850 +{
8851 +       int err;
8852 +       unsigned int flags;
8853 +       aufs_bindex_t bindex, btail, bstart;
8854 +       struct dentry *dentry, *h_dentry;
8855 +       struct file *h_file;
8856 +
8857 +       /* open all lower dirs */
8858 +       dentry = file->f_dentry;
8859 +       bstart = au_dbstart(dentry);
8860 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
8861 +               au_set_h_fptr(file, bindex, NULL);
8862 +       au_set_fbstart(file, bstart);
8863 +
8864 +       btail = au_dbtaildir(dentry);
8865 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
8866 +               au_set_h_fptr(file, bindex, NULL);
8867 +       au_set_fbend_dir(file, btail);
8868 +
8869 +       flags = vfsub_file_flags(file);
8870 +       for (bindex = bstart; bindex <= btail; bindex++) {
8871 +               h_dentry = au_h_dptr(dentry, bindex);
8872 +               if (!h_dentry)
8873 +                       continue;
8874 +               h_file = au_hf_dir(file, bindex);
8875 +               if (h_file)
8876 +                       continue;
8877 +
8878 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
8879 +               err = PTR_ERR(h_file);
8880 +               if (IS_ERR(h_file))
8881 +                       goto out; /* close all? */
8882 +               au_set_h_fptr(file, bindex, h_file);
8883 +       }
8884 +       au_update_figen(file);
8885 +       /* todo: necessary? */
8886 +       /* file->f_ra = h_file->f_ra; */
8887 +       err = 0;
8888 +
8889 +out:
8890 +       return err;
8891 +}
8892 +
8893 +static int do_open_dir(struct file *file, int flags)
8894 +{
8895 +       int err;
8896 +       aufs_bindex_t bindex, btail;
8897 +       struct dentry *dentry, *h_dentry;
8898 +       struct file *h_file;
8899 +
8900 +       FiMustWriteLock(file);
8901 +
8902 +       err = 0;
8903 +       dentry = file->f_dentry;
8904 +       file->f_version = dentry->d_inode->i_version;
8905 +       bindex = au_dbstart(dentry);
8906 +       au_set_fbstart(file, bindex);
8907 +       btail = au_dbtaildir(dentry);
8908 +       au_set_fbend_dir(file, btail);
8909 +       for (; !err && bindex <= btail; bindex++) {
8910 +               h_dentry = au_h_dptr(dentry, bindex);
8911 +               if (!h_dentry)
8912 +                       continue;
8913 +
8914 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
8915 +               if (IS_ERR(h_file)) {
8916 +                       err = PTR_ERR(h_file);
8917 +                       break;
8918 +               }
8919 +               au_set_h_fptr(file, bindex, h_file);
8920 +       }
8921 +       au_update_figen(file);
8922 +       /* todo: necessary? */
8923 +       /* file->f_ra = h_file->f_ra; */
8924 +       if (!err)
8925 +               return 0; /* success */
8926 +
8927 +       /* close all */
8928 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
8929 +               au_set_h_fptr(file, bindex, NULL);
8930 +       au_set_fbstart(file, -1);
8931 +       au_set_fbend_dir(file, -1);
8932 +
8933 +       return err;
8934 +}
8935 +
8936 +static int aufs_open_dir(struct inode *inode __maybe_unused,
8937 +                        struct file *file)
8938 +{
8939 +       int err;
8940 +       struct super_block *sb;
8941 +       struct au_fidir *fidir;
8942 +
8943 +       err = -ENOMEM;
8944 +       sb = file->f_dentry->d_sb;
8945 +       si_read_lock(sb, AuLock_FLUSH);
8946 +       fidir = au_fidir_alloc(sb);
8947 +       if (fidir) {
8948 +               err = au_do_open(file, do_open_dir, fidir);
8949 +               if (unlikely(err))
8950 +                       kfree(fidir);
8951 +       }
8952 +       si_read_unlock(sb);
8953 +       return err;
8954 +}
8955 +
8956 +static int aufs_release_dir(struct inode *inode __maybe_unused,
8957 +                           struct file *file)
8958 +{
8959 +       struct au_vdir *vdir_cache;
8960 +       struct au_finfo *finfo;
8961 +       struct au_fidir *fidir;
8962 +       aufs_bindex_t bindex, bend;
8963 +
8964 +       finfo = au_fi(file);
8965 +       fidir = finfo->fi_hdir;
8966 +       if (fidir) {
8967 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
8968 +               if (vdir_cache)
8969 +                       au_vdir_free(vdir_cache);
8970 +
8971 +               bindex = finfo->fi_btop;
8972 +               if (bindex >= 0) {
8973 +                       /*
8974 +                        * calls fput() instead of filp_close(),
8975 +                        * since no dnotify or lock for the lower file.
8976 +                        */
8977 +                       bend = fidir->fd_bbot;
8978 +                       for (; bindex <= bend; bindex++)
8979 +                               au_set_h_fptr(file, bindex, NULL);
8980 +               }
8981 +               kfree(fidir);
8982 +               finfo->fi_hdir = NULL;
8983 +       }
8984 +       au_finfo_fin(file);
8985 +       return 0;
8986 +}
8987 +
8988 +/* ---------------------------------------------------------------------- */
8989 +
8990 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
8991 +{
8992 +       int err;
8993 +       aufs_bindex_t bindex, bend;
8994 +       struct file *h_file;
8995 +
8996 +       err = 0;
8997 +       bend = au_fbend_dir(file);
8998 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
8999 +               h_file = au_hf_dir(file, bindex);
9000 +               if (h_file)
9001 +                       err = vfsub_flush(h_file, id);
9002 +       }
9003 +       return err;
9004 +}
9005 +
9006 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
9007 +{
9008 +       return au_do_flush(file, id, au_do_flush_dir);
9009 +}
9010 +
9011 +/* ---------------------------------------------------------------------- */
9012 +
9013 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
9014 +{
9015 +       int err;
9016 +       aufs_bindex_t bend, bindex;
9017 +       struct inode *inode;
9018 +       struct super_block *sb;
9019 +
9020 +       err = 0;
9021 +       sb = dentry->d_sb;
9022 +       inode = dentry->d_inode;
9023 +       IMustLock(inode);
9024 +       bend = au_dbend(dentry);
9025 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
9026 +               struct path h_path;
9027 +
9028 +               if (au_test_ro(sb, bindex, inode))
9029 +                       continue;
9030 +               h_path.dentry = au_h_dptr(dentry, bindex);
9031 +               if (!h_path.dentry)
9032 +                       continue;
9033 +
9034 +               h_path.mnt = au_sbr_mnt(sb, bindex);
9035 +               err = vfsub_fsync(NULL, &h_path, datasync);
9036 +       }
9037 +
9038 +       return err;
9039 +}
9040 +
9041 +static int au_do_fsync_dir(struct file *file, int datasync)
9042 +{
9043 +       int err;
9044 +       aufs_bindex_t bend, bindex;
9045 +       struct file *h_file;
9046 +       struct super_block *sb;
9047 +       struct inode *inode;
9048 +
9049 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9050 +       if (unlikely(err))
9051 +               goto out;
9052 +
9053 +       sb = file->f_dentry->d_sb;
9054 +       inode = file_inode(file);
9055 +       bend = au_fbend_dir(file);
9056 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9057 +               h_file = au_hf_dir(file, bindex);
9058 +               if (!h_file || au_test_ro(sb, bindex, inode))
9059 +                       continue;
9060 +
9061 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
9062 +       }
9063 +
9064 +out:
9065 +       return err;
9066 +}
9067 +
9068 +/*
9069 + * @file may be NULL
9070 + */
9071 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
9072 +                         int datasync)
9073 +{
9074 +       int err;
9075 +       struct dentry *dentry;
9076 +       struct super_block *sb;
9077 +       struct mutex *mtx;
9078 +
9079 +       err = 0;
9080 +       dentry = file->f_dentry;
9081 +       mtx = &dentry->d_inode->i_mutex;
9082 +       mutex_lock(mtx);
9083 +       sb = dentry->d_sb;
9084 +       si_noflush_read_lock(sb);
9085 +       if (file)
9086 +               err = au_do_fsync_dir(file, datasync);
9087 +       else {
9088 +               di_write_lock_child(dentry);
9089 +               err = au_do_fsync_dir_no_file(dentry, datasync);
9090 +       }
9091 +       au_cpup_attr_timesizes(dentry->d_inode);
9092 +       di_write_unlock(dentry);
9093 +       if (file)
9094 +               fi_write_unlock(file);
9095 +
9096 +       si_read_unlock(sb);
9097 +       mutex_unlock(mtx);
9098 +       return err;
9099 +}
9100 +
9101 +/* ---------------------------------------------------------------------- */
9102 +
9103 +static int aufs_iterate(struct file *file, struct dir_context *ctx)
9104 +{
9105 +       int err;
9106 +       struct dentry *dentry;
9107 +       struct inode *inode, *h_inode;
9108 +       struct super_block *sb;
9109 +
9110 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
9111 +
9112 +       dentry = file->f_dentry;
9113 +       inode = dentry->d_inode;
9114 +       IMustLock(inode);
9115 +
9116 +       sb = dentry->d_sb;
9117 +       si_read_lock(sb, AuLock_FLUSH);
9118 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9119 +       if (unlikely(err))
9120 +               goto out;
9121 +       err = au_alive_dir(dentry);
9122 +       if (!err)
9123 +               err = au_vdir_init(file);
9124 +       di_downgrade_lock(dentry, AuLock_IR);
9125 +       if (unlikely(err))
9126 +               goto out_unlock;
9127 +
9128 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
9129 +       if (!au_test_nfsd()) {
9130 +               err = au_vdir_fill_de(file, ctx);
9131 +               fsstack_copy_attr_atime(inode, h_inode);
9132 +       } else {
9133 +               /*
9134 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
9135 +                * encode_fh() and others.
9136 +                */
9137 +               atomic_inc(&h_inode->i_count);
9138 +               di_read_unlock(dentry, AuLock_IR);
9139 +               si_read_unlock(sb);
9140 +               err = au_vdir_fill_de(file, ctx);
9141 +               fsstack_copy_attr_atime(inode, h_inode);
9142 +               fi_write_unlock(file);
9143 +               iput(h_inode);
9144 +
9145 +               AuTraceErr(err);
9146 +               return err;
9147 +       }
9148 +
9149 +out_unlock:
9150 +       di_read_unlock(dentry, AuLock_IR);
9151 +       fi_write_unlock(file);
9152 +out:
9153 +       si_read_unlock(sb);
9154 +       return err;
9155 +}
9156 +
9157 +/* ---------------------------------------------------------------------- */
9158 +
9159 +#define AuTestEmpty_WHONLY     1
9160 +#define AuTestEmpty_CALLED     (1 << 1)
9161 +#define AuTestEmpty_SHWH       (1 << 2)
9162 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
9163 +#define au_fset_testempty(flags, name) \
9164 +       do { (flags) |= AuTestEmpty_##name; } while (0)
9165 +#define au_fclr_testempty(flags, name) \
9166 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
9167 +
9168 +#ifndef CONFIG_AUFS_SHWH
9169 +#undef AuTestEmpty_SHWH
9170 +#define AuTestEmpty_SHWH       0
9171 +#endif
9172 +
9173 +struct test_empty_arg {
9174 +       struct dir_context ctx;
9175 +       struct au_nhash *whlist;
9176 +       unsigned int flags;
9177 +       int err;
9178 +       aufs_bindex_t bindex;
9179 +};
9180 +
9181 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
9182 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
9183 +                        unsigned int d_type)
9184 +{
9185 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
9186 +                                                 ctx);
9187 +       char *name = (void *)__name;
9188 +
9189 +       arg->err = 0;
9190 +       au_fset_testempty(arg->flags, CALLED);
9191 +       /* smp_mb(); */
9192 +       if (name[0] == '.'
9193 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
9194 +               goto out; /* success */
9195 +
9196 +       if (namelen <= AUFS_WH_PFX_LEN
9197 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
9198 +               if (au_ftest_testempty(arg->flags, WHONLY)
9199 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
9200 +                       arg->err = -ENOTEMPTY;
9201 +               goto out;
9202 +       }
9203 +
9204 +       name += AUFS_WH_PFX_LEN;
9205 +       namelen -= AUFS_WH_PFX_LEN;
9206 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
9207 +               arg->err = au_nhash_append_wh
9208 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
9209 +                        au_ftest_testempty(arg->flags, SHWH));
9210 +
9211 +out:
9212 +       /* smp_mb(); */
9213 +       AuTraceErr(arg->err);
9214 +       return arg->err;
9215 +}
9216 +
9217 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9218 +{
9219 +       int err;
9220 +       struct file *h_file;
9221 +
9222 +       h_file = au_h_open(dentry, arg->bindex,
9223 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
9224 +                          /*file*/NULL, /*force_wr*/0);
9225 +       err = PTR_ERR(h_file);
9226 +       if (IS_ERR(h_file))
9227 +               goto out;
9228 +
9229 +       err = 0;
9230 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
9231 +           && !file_inode(h_file)->i_nlink)
9232 +               goto out_put;
9233 +
9234 +       do {
9235 +               arg->err = 0;
9236 +               au_fclr_testempty(arg->flags, CALLED);
9237 +               /* smp_mb(); */
9238 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
9239 +               if (err >= 0)
9240 +                       err = arg->err;
9241 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
9242 +
9243 +out_put:
9244 +       fput(h_file);
9245 +       au_sbr_put(dentry->d_sb, arg->bindex);
9246 +out:
9247 +       return err;
9248 +}
9249 +
9250 +struct do_test_empty_args {
9251 +       int *errp;
9252 +       struct dentry *dentry;
9253 +       struct test_empty_arg *arg;
9254 +};
9255 +
9256 +static void call_do_test_empty(void *args)
9257 +{
9258 +       struct do_test_empty_args *a = args;
9259 +       *a->errp = do_test_empty(a->dentry, a->arg);
9260 +}
9261 +
9262 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9263 +{
9264 +       int err, wkq_err;
9265 +       struct dentry *h_dentry;
9266 +       struct inode *h_inode;
9267 +
9268 +       h_dentry = au_h_dptr(dentry, arg->bindex);
9269 +       h_inode = h_dentry->d_inode;
9270 +       /* todo: i_mode changes anytime? */
9271 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
9272 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
9273 +       mutex_unlock(&h_inode->i_mutex);
9274 +       if (!err)
9275 +               err = do_test_empty(dentry, arg);
9276 +       else {
9277 +               struct do_test_empty_args args = {
9278 +                       .errp   = &err,
9279 +                       .dentry = dentry,
9280 +                       .arg    = arg
9281 +               };
9282 +               unsigned int flags = arg->flags;
9283 +
9284 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
9285 +               if (unlikely(wkq_err))
9286 +                       err = wkq_err;
9287 +               arg->flags = flags;
9288 +       }
9289 +
9290 +       return err;
9291 +}
9292 +
9293 +int au_test_empty_lower(struct dentry *dentry)
9294 +{
9295 +       int err;
9296 +       unsigned int rdhash;
9297 +       aufs_bindex_t bindex, bstart, btail;
9298 +       struct au_nhash whlist;
9299 +       struct test_empty_arg arg = {
9300 +               .ctx = {
9301 +                       .actor = au_diractor(test_empty_cb)
9302 +               }
9303 +       };
9304 +
9305 +       SiMustAnyLock(dentry->d_sb);
9306 +
9307 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
9308 +       if (!rdhash)
9309 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
9310 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
9311 +       if (unlikely(err))
9312 +               goto out;
9313 +
9314 +       arg.flags = 0;
9315 +       arg.whlist = &whlist;
9316 +       bstart = au_dbstart(dentry);
9317 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
9318 +               au_fset_testempty(arg.flags, SHWH);
9319 +       arg.bindex = bstart;
9320 +       err = do_test_empty(dentry, &arg);
9321 +       if (unlikely(err))
9322 +               goto out_whlist;
9323 +
9324 +       au_fset_testempty(arg.flags, WHONLY);
9325 +       btail = au_dbtaildir(dentry);
9326 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
9327 +               struct dentry *h_dentry;
9328 +
9329 +               h_dentry = au_h_dptr(dentry, bindex);
9330 +               if (h_dentry && h_dentry->d_inode) {
9331 +                       arg.bindex = bindex;
9332 +                       err = do_test_empty(dentry, &arg);
9333 +               }
9334 +       }
9335 +
9336 +out_whlist:
9337 +       au_nhash_wh_free(&whlist);
9338 +out:
9339 +       return err;
9340 +}
9341 +
9342 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
9343 +{
9344 +       int err;
9345 +       struct test_empty_arg arg = {
9346 +               .ctx = {
9347 +                       .actor = au_diractor(test_empty_cb)
9348 +               }
9349 +       };
9350 +       aufs_bindex_t bindex, btail;
9351 +
9352 +       err = 0;
9353 +       arg.whlist = whlist;
9354 +       arg.flags = AuTestEmpty_WHONLY;
9355 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
9356 +               au_fset_testempty(arg.flags, SHWH);
9357 +       btail = au_dbtaildir(dentry);
9358 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
9359 +               struct dentry *h_dentry;
9360 +
9361 +               h_dentry = au_h_dptr(dentry, bindex);
9362 +               if (h_dentry && h_dentry->d_inode) {
9363 +                       arg.bindex = bindex;
9364 +                       err = sio_test_empty(dentry, &arg);
9365 +               }
9366 +       }
9367 +
9368 +       return err;
9369 +}
9370 +
9371 +/* ---------------------------------------------------------------------- */
9372 +
9373 +const struct file_operations aufs_dir_fop = {
9374 +       .owner          = THIS_MODULE,
9375 +       .llseek         = default_llseek,
9376 +       .read           = generic_read_dir,
9377 +       .iterate        = aufs_iterate,
9378 +       .unlocked_ioctl = aufs_ioctl_dir,
9379 +#ifdef CONFIG_COMPAT
9380 +       .compat_ioctl   = aufs_compat_ioctl_dir,
9381 +#endif
9382 +       .open           = aufs_open_dir,
9383 +       .release        = aufs_release_dir,
9384 +       .flush          = aufs_flush_dir,
9385 +       .fsync          = aufs_fsync_dir
9386 +};
9387 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
9388 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
9389 +++ linux/fs/aufs/dir.h 2014-06-24 00:14:06.779385790 +0200
9390 @@ -0,0 +1,136 @@
9391 +/*
9392 + * Copyright (C) 2005-2014 Junjiro R. Okajima
9393 + *
9394 + * This program, aufs is free software; you can redistribute it and/or modify
9395 + * it under the terms of the GNU General Public License as published by
9396 + * the Free Software Foundation; either version 2 of the License, or
9397 + * (at your option) any later version.
9398 + *
9399 + * This program is distributed in the hope that it will be useful,
9400 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9401 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9402 + * GNU General Public License for more details.
9403 + *
9404 + * You should have received a copy of the GNU General Public License
9405 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9406 + */
9407 +
9408 +/*
9409 + * directory operations
9410 + */
9411 +
9412 +#ifndef __AUFS_DIR_H__
9413 +#define __AUFS_DIR_H__
9414 +
9415 +#ifdef __KERNEL__
9416 +
9417 +#include <linux/fs.h>
9418 +
9419 +/* ---------------------------------------------------------------------- */
9420 +
9421 +/* need to be faster and smaller */
9422 +
9423 +struct au_nhash {
9424 +       unsigned int            nh_num;
9425 +       struct hlist_head       *nh_head;
9426 +};
9427 +
9428 +struct au_vdir_destr {
9429 +       unsigned char   len;
9430 +       unsigned char   name[0];
9431 +} __packed;
9432 +
9433 +struct au_vdir_dehstr {
9434 +       struct hlist_node       hash;
9435 +       struct au_vdir_destr    *str;
9436 +} ____cacheline_aligned_in_smp;
9437 +
9438 +struct au_vdir_de {
9439 +       ino_t                   de_ino;
9440 +       unsigned char           de_type;
9441 +       /* caution: packed */
9442 +       struct au_vdir_destr    de_str;
9443 +} __packed;
9444 +
9445 +struct au_vdir_wh {
9446 +       struct hlist_node       wh_hash;
9447 +#ifdef CONFIG_AUFS_SHWH
9448 +       ino_t                   wh_ino;
9449 +       aufs_bindex_t           wh_bindex;
9450 +       unsigned char           wh_type;
9451 +#else
9452 +       aufs_bindex_t           wh_bindex;
9453 +#endif
9454 +       /* caution: packed */
9455 +       struct au_vdir_destr    wh_str;
9456 +} __packed;
9457 +
9458 +union au_vdir_deblk_p {
9459 +       unsigned char           *deblk;
9460 +       struct au_vdir_de       *de;
9461 +};
9462 +
9463 +struct au_vdir {
9464 +       unsigned char   **vd_deblk;
9465 +       unsigned long   vd_nblk;
9466 +       struct {
9467 +               unsigned long           ul;
9468 +               union au_vdir_deblk_p   p;
9469 +       } vd_last;
9470 +
9471 +       unsigned long   vd_version;
9472 +       unsigned int    vd_deblk_sz;
9473 +       unsigned long   vd_jiffy;
9474 +} ____cacheline_aligned_in_smp;
9475 +
9476 +/* ---------------------------------------------------------------------- */
9477 +
9478 +/* dir.c */
9479 +extern const struct file_operations aufs_dir_fop;
9480 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
9481 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
9482 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
9483 +int au_test_empty_lower(struct dentry *dentry);
9484 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
9485 +
9486 +/* vdir.c */
9487 +unsigned int au_rdhash_est(loff_t sz);
9488 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
9489 +void au_nhash_wh_free(struct au_nhash *whlist);
9490 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
9491 +                           int limit);
9492 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
9493 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
9494 +                      unsigned int d_type, aufs_bindex_t bindex,
9495 +                      unsigned char shwh);
9496 +void au_vdir_free(struct au_vdir *vdir);
9497 +int au_vdir_init(struct file *file);
9498 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
9499 +
9500 +/* ioctl.c */
9501 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
9502 +
9503 +#ifdef CONFIG_AUFS_RDU
9504 +/* rdu.c */
9505 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
9506 +#ifdef CONFIG_COMPAT
9507 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
9508 +                        unsigned long arg);
9509 +#endif
9510 +#else
9511 +static inline long au_rdu_ioctl(struct file *file, unsigned int cmd,
9512 +                               unsigned long arg)
9513 +{
9514 +       return -EINVAL;
9515 +}
9516 +#ifdef CONFIG_COMPAT
9517 +static inline long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
9518 +                                      unsigned long arg)
9519 +{
9520 +       return -EINVAL;
9521 +}
9522 +#endif
9523 +#endif
9524 +
9525 +#endif /* __KERNEL__ */
9526 +#endif /* __AUFS_DIR_H__ */
9527 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
9528 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
9529 +++ linux/fs/aufs/dynop.c       2014-06-24 00:14:06.782719306 +0200
9530 @@ -0,0 +1,379 @@
9531 +/*
9532 + * Copyright (C) 2010-2014 Junjiro R. Okajima
9533 + *
9534 + * This program, aufs is free software; you can redistribute it and/or modify
9535 + * it under the terms of the GNU General Public License as published by
9536 + * the Free Software Foundation; either version 2 of the License, or
9537 + * (at your option) any later version.
9538 + *
9539 + * This program is distributed in the hope that it will be useful,
9540 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9541 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9542 + * GNU General Public License for more details.
9543 + *
9544 + * You should have received a copy of the GNU General Public License
9545 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9546 + */
9547 +
9548 +/*
9549 + * dynamically customizable operations for regular files
9550 + */
9551 +
9552 +#include "aufs.h"
9553 +
9554 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
9555 +
9556 +/*
9557 + * How large will these lists be?
9558 + * Usually just a few elements, 20-30 at most for each, I guess.
9559 + */
9560 +static struct au_splhead dynop[AuDyLast];
9561 +
9562 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
9563 +{
9564 +       struct au_dykey *key, *tmp;
9565 +       struct list_head *head;
9566 +
9567 +       key = NULL;
9568 +       head = &spl->head;
9569 +       rcu_read_lock();
9570 +       list_for_each_entry_rcu(tmp, head, dk_list)
9571 +               if (tmp->dk_op.dy_hop == h_op) {
9572 +                       key = tmp;
9573 +                       kref_get(&key->dk_kref);
9574 +                       break;
9575 +               }
9576 +       rcu_read_unlock();
9577 +
9578 +       return key;
9579 +}
9580 +
9581 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
9582 +{
9583 +       struct au_dykey **k, *found;
9584 +       const void *h_op = key->dk_op.dy_hop;
9585 +       int i;
9586 +
9587 +       found = NULL;
9588 +       k = br->br_dykey;
9589 +       for (i = 0; i < AuBrDynOp; i++)
9590 +               if (k[i]) {
9591 +                       if (k[i]->dk_op.dy_hop == h_op) {
9592 +                               found = k[i];
9593 +                               break;
9594 +                       }
9595 +               } else
9596 +                       break;
9597 +       if (!found) {
9598 +               spin_lock(&br->br_dykey_lock);
9599 +               for (; i < AuBrDynOp; i++)
9600 +                       if (k[i]) {
9601 +                               if (k[i]->dk_op.dy_hop == h_op) {
9602 +                                       found = k[i];
9603 +                                       break;
9604 +                               }
9605 +                       } else {
9606 +                               k[i] = key;
9607 +                               break;
9608 +                       }
9609 +               spin_unlock(&br->br_dykey_lock);
9610 +               BUG_ON(i == AuBrDynOp); /* expand the array */
9611 +       }
9612 +
9613 +       return found;
9614 +}
9615 +
9616 +/* kref_get() if @key is already added */
9617 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
9618 +{
9619 +       struct au_dykey *tmp, *found;
9620 +       struct list_head *head;
9621 +       const void *h_op = key->dk_op.dy_hop;
9622 +
9623 +       found = NULL;
9624 +       head = &spl->head;
9625 +       spin_lock(&spl->spin);
9626 +       list_for_each_entry(tmp, head, dk_list)
9627 +               if (tmp->dk_op.dy_hop == h_op) {
9628 +                       kref_get(&tmp->dk_kref);
9629 +                       found = tmp;
9630 +                       break;
9631 +               }
9632 +       if (!found)
9633 +               list_add_rcu(&key->dk_list, head);
9634 +       spin_unlock(&spl->spin);
9635 +
9636 +       if (!found)
9637 +               DyPrSym(key);
9638 +       return found;
9639 +}
9640 +
9641 +static void dy_free_rcu(struct rcu_head *rcu)
9642 +{
9643 +       struct au_dykey *key;
9644 +
9645 +       key = container_of(rcu, struct au_dykey, dk_rcu);
9646 +       DyPrSym(key);
9647 +       kfree(key);
9648 +}
9649 +
9650 +static void dy_free(struct kref *kref)
9651 +{
9652 +       struct au_dykey *key;
9653 +       struct au_splhead *spl;
9654 +
9655 +       key = container_of(kref, struct au_dykey, dk_kref);
9656 +       spl = dynop + key->dk_op.dy_type;
9657 +       au_spl_del_rcu(&key->dk_list, spl);
9658 +       call_rcu(&key->dk_rcu, dy_free_rcu);
9659 +}
9660 +
9661 +void au_dy_put(struct au_dykey *key)
9662 +{
9663 +       kref_put(&key->dk_kref, dy_free);
9664 +}
9665 +
9666 +/* ---------------------------------------------------------------------- */
9667 +
9668 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
9669 +
9670 +#ifdef CONFIG_AUFS_DEBUG
9671 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
9672 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
9673 +#else
9674 +#define DyDbgDeclare(cnt)      do {} while (0)
9675 +#define DyDbgInc(cnt)          do {} while (0)
9676 +#endif
9677 +
9678 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
9679 +       DyDbgInc(cnt);                                                  \
9680 +       if (h_op->func) {                                               \
9681 +               if (src.func)                                           \
9682 +                       dst.func = src.func;                            \
9683 +               else                                                    \
9684 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
9685 +       }                                                               \
9686 +} while (0)
9687 +
9688 +#define DySetForce(func, dst, src) do {                \
9689 +       AuDebugOn(!src.func);                   \
9690 +       DyDbgInc(cnt);                          \
9691 +       dst.func = src.func;                    \
9692 +} while (0)
9693 +
9694 +#define DySetAop(func) \
9695 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
9696 +#define DySetAopForce(func) \
9697 +       DySetForce(func, dyaop->da_op, aufs_aop)
9698 +
9699 +static void dy_aop(struct au_dykey *key, const void *h_op,
9700 +                  struct super_block *h_sb __maybe_unused)
9701 +{
9702 +       struct au_dyaop *dyaop = (void *)key;
9703 +       const struct address_space_operations *h_aop = h_op;
9704 +       DyDbgDeclare(cnt);
9705 +
9706 +       AuDbg("%s\n", au_sbtype(h_sb));
9707 +
9708 +       DySetAop(writepage);
9709 +       DySetAopForce(readpage);        /* force */
9710 +       DySetAop(writepages);
9711 +       DySetAop(set_page_dirty);
9712 +       DySetAop(readpages);
9713 +       DySetAop(write_begin);
9714 +       DySetAop(write_end);
9715 +       DySetAop(bmap);
9716 +       DySetAop(invalidatepage);
9717 +       DySetAop(releasepage);
9718 +       DySetAop(freepage);
9719 +       /* these two will be changed according to an aufs mount option */
9720 +       DySetAop(direct_IO);
9721 +       DySetAop(get_xip_mem);
9722 +       DySetAop(migratepage);
9723 +       DySetAop(launder_page);
9724 +       DySetAop(is_partially_uptodate);
9725 +       DySetAop(is_dirty_writeback);
9726 +       DySetAop(error_remove_page);
9727 +       DySetAop(swap_activate);
9728 +       DySetAop(swap_deactivate);
9729 +
9730 +       DyDbgSize(cnt, *h_aop);
9731 +       dyaop->da_get_xip_mem = h_aop->get_xip_mem;
9732 +}
9733 +
9734 +/* ---------------------------------------------------------------------- */
9735 +
9736 +static void dy_bug(struct kref *kref)
9737 +{
9738 +       BUG();
9739 +}
9740 +
9741 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
9742 +{
9743 +       struct au_dykey *key, *old;
9744 +       struct au_splhead *spl;
9745 +       struct op {
9746 +               unsigned int sz;
9747 +               void (*set)(struct au_dykey *key, const void *h_op,
9748 +                           struct super_block *h_sb __maybe_unused);
9749 +       };
9750 +       static const struct op a[] = {
9751 +               [AuDy_AOP] = {
9752 +                       .sz     = sizeof(struct au_dyaop),
9753 +                       .set    = dy_aop
9754 +               }
9755 +       };
9756 +       const struct op *p;
9757 +
9758 +       spl = dynop + op->dy_type;
9759 +       key = dy_gfind_get(spl, op->dy_hop);
9760 +       if (key)
9761 +               goto out_add; /* success */
9762 +
9763 +       p = a + op->dy_type;
9764 +       key = kzalloc(p->sz, GFP_NOFS);
9765 +       if (unlikely(!key)) {
9766 +               key = ERR_PTR(-ENOMEM);
9767 +               goto out;
9768 +       }
9769 +
9770 +       key->dk_op.dy_hop = op->dy_hop;
9771 +       kref_init(&key->dk_kref);
9772 +       p->set(key, op->dy_hop, au_br_sb(br));
9773 +       old = dy_gadd(spl, key);
9774 +       if (old) {
9775 +               kfree(key);
9776 +               key = old;
9777 +       }
9778 +
9779 +out_add:
9780 +       old = dy_bradd(br, key);
9781 +       if (old)
9782 +               /* its ref-count should never be zero here */
9783 +               kref_put(&key->dk_kref, dy_bug);
9784 +out:
9785 +       return key;
9786 +}
9787 +
9788 +/* ---------------------------------------------------------------------- */
9789 +/*
9790 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
9791 + * This behaviour is neccessary to return an error from open(O_DIRECT) instead
9792 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
9793 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
9794 + * See the aufs manual in detail.
9795 + *
9796 + * To keep this behaviour, aufs has to set NULL to ->get_xip_mem too, and the
9797 + * performance of fadvise() and madvise() may be affected.
9798 + */
9799 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
9800 +{
9801 +       if (!do_dx) {
9802 +               dyaop->da_op.direct_IO = NULL;
9803 +               dyaop->da_op.get_xip_mem = NULL;
9804 +       } else {
9805 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
9806 +               dyaop->da_op.get_xip_mem = aufs_aop.get_xip_mem;
9807 +               if (!dyaop->da_get_xip_mem)
9808 +                       dyaop->da_op.get_xip_mem = NULL;
9809 +       }
9810 +}
9811 +
9812 +static struct au_dyaop *dy_aget(struct au_branch *br,
9813 +                               const struct address_space_operations *h_aop,
9814 +                               int do_dx)
9815 +{
9816 +       struct au_dyaop *dyaop;
9817 +       struct au_dynop op;
9818 +
9819 +       op.dy_type = AuDy_AOP;
9820 +       op.dy_haop = h_aop;
9821 +       dyaop = (void *)dy_get(&op, br);
9822 +       if (IS_ERR(dyaop))
9823 +               goto out;
9824 +       dy_adx(dyaop, do_dx);
9825 +
9826 +out:
9827 +       return dyaop;
9828 +}
9829 +
9830 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
9831 +               struct inode *h_inode)
9832 +{
9833 +       int err, do_dx;
9834 +       struct super_block *sb;
9835 +       struct au_branch *br;
9836 +       struct au_dyaop *dyaop;
9837 +
9838 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
9839 +       IiMustWriteLock(inode);
9840 +
9841 +       sb = inode->i_sb;
9842 +       br = au_sbr(sb, bindex);
9843 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
9844 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
9845 +       err = PTR_ERR(dyaop);
9846 +       if (IS_ERR(dyaop))
9847 +               /* unnecessary to call dy_fput() */
9848 +               goto out;
9849 +
9850 +       err = 0;
9851 +       inode->i_mapping->a_ops = &dyaop->da_op;
9852 +
9853 +out:
9854 +       return err;
9855 +}
9856 +
9857 +/*
9858 + * Is it safe to replace a_ops during the inode/file is in operation?
9859 + * Yes, I hope so.
9860 + */
9861 +int au_dy_irefresh(struct inode *inode)
9862 +{
9863 +       int err;
9864 +       aufs_bindex_t bstart;
9865 +       struct inode *h_inode;
9866 +
9867 +       err = 0;
9868 +       if (S_ISREG(inode->i_mode)) {
9869 +               bstart = au_ibstart(inode);
9870 +               h_inode = au_h_iptr(inode, bstart);
9871 +               err = au_dy_iaop(inode, bstart, h_inode);
9872 +       }
9873 +       return err;
9874 +}
9875 +
9876 +void au_dy_arefresh(int do_dx)
9877 +{
9878 +       struct au_splhead *spl;
9879 +       struct list_head *head;
9880 +       struct au_dykey *key;
9881 +
9882 +       spl = dynop + AuDy_AOP;
9883 +       head = &spl->head;
9884 +       spin_lock(&spl->spin);
9885 +       list_for_each_entry(key, head, dk_list)
9886 +               dy_adx((void *)key, do_dx);
9887 +       spin_unlock(&spl->spin);
9888 +}
9889 +
9890 +/* ---------------------------------------------------------------------- */
9891 +
9892 +void __init au_dy_init(void)
9893 +{
9894 +       int i;
9895 +
9896 +       /* make sure that 'struct au_dykey *' can be any type */
9897 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
9898 +
9899 +       for (i = 0; i < AuDyLast; i++)
9900 +               au_spl_init(dynop + i);
9901 +}
9902 +
9903 +void au_dy_fin(void)
9904 +{
9905 +       int i;
9906 +
9907 +       for (i = 0; i < AuDyLast; i++)
9908 +               WARN_ON(!list_empty(&dynop[i].head));
9909 +}
9910 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
9911 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
9912 +++ linux/fs/aufs/dynop.h       2014-06-24 00:14:06.782719306 +0200
9913 @@ -0,0 +1,75 @@
9914 +/*
9915 + * Copyright (C) 2010-2014 Junjiro R. Okajima
9916 + *
9917 + * This program, aufs is free software; you can redistribute it and/or modify
9918 + * it under the terms of the GNU General Public License as published by
9919 + * the Free Software Foundation; either version 2 of the License, or
9920 + * (at your option) any later version.
9921 + *
9922 + * This program is distributed in the hope that it will be useful,
9923 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9924 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9925 + * GNU General Public License for more details.
9926 + *
9927 + * You should have received a copy of the GNU General Public License
9928 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9929 + */
9930 +
9931 +/*
9932 + * dynamically customizable operations (for regular files only)
9933 + */
9934 +
9935 +#ifndef __AUFS_DYNOP_H__
9936 +#define __AUFS_DYNOP_H__
9937 +
9938 +#ifdef __KERNEL__
9939 +
9940 +#include "inode.h"
9941 +
9942 +enum {AuDy_AOP, AuDyLast};
9943 +
9944 +struct au_dynop {
9945 +       int                                             dy_type;
9946 +       union {
9947 +               const void                              *dy_hop;
9948 +               const struct address_space_operations   *dy_haop;
9949 +       };
9950 +};
9951 +
9952 +struct au_dykey {
9953 +       union {
9954 +               struct list_head        dk_list;
9955 +               struct rcu_head         dk_rcu;
9956 +       };
9957 +       struct au_dynop         dk_op;
9958 +
9959 +       /*
9960 +        * during I am in the branch local array, kref is gotten. when the
9961 +        * branch is removed, kref is put.
9962 +        */
9963 +       struct kref             dk_kref;
9964 +};
9965 +
9966 +/* stop unioning since their sizes are very different from each other */
9967 +struct au_dyaop {
9968 +       struct au_dykey                 da_key;
9969 +       struct address_space_operations da_op; /* not const */
9970 +       int (*da_get_xip_mem)(struct address_space *, pgoff_t, int,
9971 +                             void **, unsigned long *);
9972 +};
9973 +
9974 +/* ---------------------------------------------------------------------- */
9975 +
9976 +/* dynop.c */
9977 +struct au_branch;
9978 +void au_dy_put(struct au_dykey *key);
9979 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
9980 +               struct inode *h_inode);
9981 +int au_dy_irefresh(struct inode *inode);
9982 +void au_dy_arefresh(int do_dio);
9983 +
9984 +void __init au_dy_init(void);
9985 +void au_dy_fin(void);
9986 +
9987 +#endif /* __KERNEL__ */
9988 +#endif /* __AUFS_DYNOP_H__ */
9989 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
9990 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
9991 +++ linux/fs/aufs/export.c      2014-06-24 00:14:06.782719306 +0200
9992 @@ -0,0 +1,831 @@
9993 +/*
9994 + * Copyright (C) 2005-2014 Junjiro R. Okajima
9995 + *
9996 + * This program, aufs is free software; you can redistribute it and/or modify
9997 + * it under the terms of the GNU General Public License as published by
9998 + * the Free Software Foundation; either version 2 of the License, or
9999 + * (at your option) any later version.
10000 + *
10001 + * This program is distributed in the hope that it will be useful,
10002 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10003 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10004 + * GNU General Public License for more details.
10005 + *
10006 + * You should have received a copy of the GNU General Public License
10007 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10008 + */
10009 +
10010 +/*
10011 + * export via nfs
10012 + */
10013 +
10014 +#include <linux/exportfs.h>
10015 +#include <linux/fs_struct.h>
10016 +#include <linux/namei.h>
10017 +#include <linux/nsproxy.h>
10018 +#include <linux/random.h>
10019 +#include <linux/writeback.h>
10020 +#include "../fs/mount.h"
10021 +#include "aufs.h"
10022 +
10023 +union conv {
10024 +#ifdef CONFIG_AUFS_INO_T_64
10025 +       __u32 a[2];
10026 +#else
10027 +       __u32 a[1];
10028 +#endif
10029 +       ino_t ino;
10030 +};
10031 +
10032 +static ino_t decode_ino(__u32 *a)
10033 +{
10034 +       union conv u;
10035 +
10036 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
10037 +       u.a[0] = a[0];
10038 +#ifdef CONFIG_AUFS_INO_T_64
10039 +       u.a[1] = a[1];
10040 +#endif
10041 +       return u.ino;
10042 +}
10043 +
10044 +static void encode_ino(__u32 *a, ino_t ino)
10045 +{
10046 +       union conv u;
10047 +
10048 +       u.ino = ino;
10049 +       a[0] = u.a[0];
10050 +#ifdef CONFIG_AUFS_INO_T_64
10051 +       a[1] = u.a[1];
10052 +#endif
10053 +}
10054 +
10055 +/* NFS file handle */
10056 +enum {
10057 +       Fh_br_id,
10058 +       Fh_sigen,
10059 +#ifdef CONFIG_AUFS_INO_T_64
10060 +       /* support 64bit inode number */
10061 +       Fh_ino1,
10062 +       Fh_ino2,
10063 +       Fh_dir_ino1,
10064 +       Fh_dir_ino2,
10065 +#else
10066 +       Fh_ino1,
10067 +       Fh_dir_ino1,
10068 +#endif
10069 +       Fh_igen,
10070 +       Fh_h_type,
10071 +       Fh_tail,
10072 +
10073 +       Fh_ino = Fh_ino1,
10074 +       Fh_dir_ino = Fh_dir_ino1
10075 +};
10076 +
10077 +static int au_test_anon(struct dentry *dentry)
10078 +{
10079 +       /* note: read d_flags without d_lock */
10080 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
10081 +}
10082 +
10083 +int au_test_nfsd(void)
10084 +{
10085 +       int ret;
10086 +       struct task_struct *tsk = current;
10087 +       char comm[sizeof(tsk->comm)];
10088 +
10089 +       ret = 0;
10090 +       if (tsk->flags & PF_KTHREAD) {
10091 +               get_task_comm(comm, tsk);
10092 +               ret = !strcmp(comm, "nfsd");
10093 +       }
10094 +
10095 +       return ret;
10096 +}
10097 +
10098 +/* ---------------------------------------------------------------------- */
10099 +/* inode generation external table */
10100 +
10101 +void au_xigen_inc(struct inode *inode)
10102 +{
10103 +       loff_t pos;
10104 +       ssize_t sz;
10105 +       __u32 igen;
10106 +       struct super_block *sb;
10107 +       struct au_sbinfo *sbinfo;
10108 +
10109 +       sb = inode->i_sb;
10110 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
10111 +
10112 +       sbinfo = au_sbi(sb);
10113 +       pos = inode->i_ino;
10114 +       pos *= sizeof(igen);
10115 +       igen = inode->i_generation + 1;
10116 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
10117 +                        sizeof(igen), &pos);
10118 +       if (sz == sizeof(igen))
10119 +               return; /* success */
10120 +
10121 +       if (unlikely(sz >= 0))
10122 +               AuIOErr("xigen error (%zd)\n", sz);
10123 +}
10124 +
10125 +int au_xigen_new(struct inode *inode)
10126 +{
10127 +       int err;
10128 +       loff_t pos;
10129 +       ssize_t sz;
10130 +       struct super_block *sb;
10131 +       struct au_sbinfo *sbinfo;
10132 +       struct file *file;
10133 +
10134 +       err = 0;
10135 +       /* todo: dirty, at mount time */
10136 +       if (inode->i_ino == AUFS_ROOT_INO)
10137 +               goto out;
10138 +       sb = inode->i_sb;
10139 +       SiMustAnyLock(sb);
10140 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
10141 +               goto out;
10142 +
10143 +       err = -EFBIG;
10144 +       pos = inode->i_ino;
10145 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
10146 +               AuIOErr1("too large i%lld\n", pos);
10147 +               goto out;
10148 +       }
10149 +       pos *= sizeof(inode->i_generation);
10150 +
10151 +       err = 0;
10152 +       sbinfo = au_sbi(sb);
10153 +       file = sbinfo->si_xigen;
10154 +       BUG_ON(!file);
10155 +
10156 +       if (vfsub_f_size_read(file)
10157 +           < pos + sizeof(inode->i_generation)) {
10158 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
10159 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
10160 +                                sizeof(inode->i_generation), &pos);
10161 +       } else
10162 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
10163 +                               sizeof(inode->i_generation), &pos);
10164 +       if (sz == sizeof(inode->i_generation))
10165 +               goto out; /* success */
10166 +
10167 +       err = sz;
10168 +       if (unlikely(sz >= 0)) {
10169 +               err = -EIO;
10170 +               AuIOErr("xigen error (%zd)\n", sz);
10171 +       }
10172 +
10173 +out:
10174 +       return err;
10175 +}
10176 +
10177 +int au_xigen_set(struct super_block *sb, struct file *base)
10178 +{
10179 +       int err;
10180 +       struct au_sbinfo *sbinfo;
10181 +       struct file *file;
10182 +
10183 +       SiMustWriteLock(sb);
10184 +
10185 +       sbinfo = au_sbi(sb);
10186 +       file = au_xino_create2(base, sbinfo->si_xigen);
10187 +       err = PTR_ERR(file);
10188 +       if (IS_ERR(file))
10189 +               goto out;
10190 +       err = 0;
10191 +       if (sbinfo->si_xigen)
10192 +               fput(sbinfo->si_xigen);
10193 +       sbinfo->si_xigen = file;
10194 +
10195 +out:
10196 +       return err;
10197 +}
10198 +
10199 +void au_xigen_clr(struct super_block *sb)
10200 +{
10201 +       struct au_sbinfo *sbinfo;
10202 +
10203 +       SiMustWriteLock(sb);
10204 +
10205 +       sbinfo = au_sbi(sb);
10206 +       if (sbinfo->si_xigen) {
10207 +               fput(sbinfo->si_xigen);
10208 +               sbinfo->si_xigen = NULL;
10209 +       }
10210 +}
10211 +
10212 +/* ---------------------------------------------------------------------- */
10213 +
10214 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
10215 +                                   ino_t dir_ino)
10216 +{
10217 +       struct dentry *dentry, *d;
10218 +       struct inode *inode;
10219 +       unsigned int sigen;
10220 +
10221 +       dentry = NULL;
10222 +       inode = ilookup(sb, ino);
10223 +       if (!inode)
10224 +               goto out;
10225 +
10226 +       dentry = ERR_PTR(-ESTALE);
10227 +       sigen = au_sigen(sb);
10228 +       if (unlikely(is_bad_inode(inode)
10229 +                    || IS_DEADDIR(inode)
10230 +                    || sigen != au_iigen(inode, NULL)))
10231 +               goto out_iput;
10232 +
10233 +       dentry = NULL;
10234 +       if (!dir_ino || S_ISDIR(inode->i_mode))
10235 +               dentry = d_find_alias(inode);
10236 +       else {
10237 +               spin_lock(&inode->i_lock);
10238 +               hlist_for_each_entry(d, &inode->i_dentry, d_alias) {
10239 +                       spin_lock(&d->d_lock);
10240 +                       if (!au_test_anon(d)
10241 +                           && d->d_parent->d_inode->i_ino == dir_ino) {
10242 +                               dentry = dget_dlock(d);
10243 +                               spin_unlock(&d->d_lock);
10244 +                               break;
10245 +                       }
10246 +                       spin_unlock(&d->d_lock);
10247 +               }
10248 +               spin_unlock(&inode->i_lock);
10249 +       }
10250 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
10251 +               /* need to refresh */
10252 +               dput(dentry);
10253 +               dentry = NULL;
10254 +       }
10255 +
10256 +out_iput:
10257 +       iput(inode);
10258 +out:
10259 +       AuTraceErrPtr(dentry);
10260 +       return dentry;
10261 +}
10262 +
10263 +/* ---------------------------------------------------------------------- */
10264 +
10265 +/* todo: dirty? */
10266 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
10267 +
10268 +struct au_compare_mnt_args {
10269 +       /* input */
10270 +       struct super_block *sb;
10271 +
10272 +       /* output */
10273 +       struct vfsmount *mnt;
10274 +};
10275 +
10276 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
10277 +{
10278 +       struct au_compare_mnt_args *a = arg;
10279 +
10280 +       if (mnt->mnt_sb != a->sb)
10281 +               return 0;
10282 +       a->mnt = mntget(mnt);
10283 +       return 1;
10284 +}
10285 +
10286 +static struct vfsmount *au_mnt_get(struct super_block *sb)
10287 +{
10288 +       int err;
10289 +       struct path root;
10290 +       struct au_compare_mnt_args args = {
10291 +               .sb = sb
10292 +       };
10293 +
10294 +       get_fs_root(current->fs, &root);
10295 +       rcu_read_lock();
10296 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
10297 +       rcu_read_unlock();
10298 +       path_put(&root);
10299 +       AuDebugOn(!err);
10300 +       AuDebugOn(!args.mnt);
10301 +       return args.mnt;
10302 +}
10303 +
10304 +struct au_nfsd_si_lock {
10305 +       unsigned int sigen;
10306 +       aufs_bindex_t bindex, br_id;
10307 +       unsigned char force_lock;
10308 +};
10309 +
10310 +static int si_nfsd_read_lock(struct super_block *sb,
10311 +                            struct au_nfsd_si_lock *nsi_lock)
10312 +{
10313 +       int err;
10314 +       aufs_bindex_t bindex;
10315 +
10316 +       si_read_lock(sb, AuLock_FLUSH);
10317 +
10318 +       /* branch id may be wrapped around */
10319 +       err = 0;
10320 +       bindex = au_br_index(sb, nsi_lock->br_id);
10321 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
10322 +               goto out; /* success */
10323 +
10324 +       err = -ESTALE;
10325 +       bindex = -1;
10326 +       if (!nsi_lock->force_lock)
10327 +               si_read_unlock(sb);
10328 +
10329 +out:
10330 +       nsi_lock->bindex = bindex;
10331 +       return err;
10332 +}
10333 +
10334 +struct find_name_by_ino {
10335 +       struct dir_context ctx;
10336 +       int called, found;
10337 +       ino_t ino;
10338 +       char *name;
10339 +       int namelen;
10340 +};
10341 +
10342 +static int
10343 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
10344 +                loff_t offset, u64 ino, unsigned int d_type)
10345 +{
10346 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
10347 +                                                 ctx);
10348 +
10349 +       a->called++;
10350 +       if (a->ino != ino)
10351 +               return 0;
10352 +
10353 +       memcpy(a->name, name, namelen);
10354 +       a->namelen = namelen;
10355 +       a->found = 1;
10356 +       return 1;
10357 +}
10358 +
10359 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
10360 +                                    struct au_nfsd_si_lock *nsi_lock)
10361 +{
10362 +       struct dentry *dentry, *parent;
10363 +       struct file *file;
10364 +       struct inode *dir;
10365 +       struct find_name_by_ino arg = {
10366 +               .ctx = {
10367 +                       .actor = au_diractor(find_name_by_ino)
10368 +               }
10369 +       };
10370 +       int err;
10371 +
10372 +       parent = path->dentry;
10373 +       if (nsi_lock)
10374 +               si_read_unlock(parent->d_sb);
10375 +       file = vfsub_dentry_open(path, au_dir_roflags);
10376 +       dentry = (void *)file;
10377 +       if (IS_ERR(file))
10378 +               goto out;
10379 +
10380 +       dentry = ERR_PTR(-ENOMEM);
10381 +       arg.name = (void *)__get_free_page(GFP_NOFS);
10382 +       if (unlikely(!arg.name))
10383 +               goto out_file;
10384 +       arg.ino = ino;
10385 +       arg.found = 0;
10386 +       do {
10387 +               arg.called = 0;
10388 +               /* smp_mb(); */
10389 +               err = vfsub_iterate_dir(file, &arg.ctx);
10390 +       } while (!err && !arg.found && arg.called);
10391 +       dentry = ERR_PTR(err);
10392 +       if (unlikely(err))
10393 +               goto out_name;
10394 +       /* instead of ENOENT */
10395 +       dentry = ERR_PTR(-ESTALE);
10396 +       if (!arg.found)
10397 +               goto out_name;
10398 +
10399 +       /* do not call vfsub_lkup_one() */
10400 +       dir = parent->d_inode;
10401 +       mutex_lock(&dir->i_mutex);
10402 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
10403 +       mutex_unlock(&dir->i_mutex);
10404 +       AuTraceErrPtr(dentry);
10405 +       if (IS_ERR(dentry))
10406 +               goto out_name;
10407 +       AuDebugOn(au_test_anon(dentry));
10408 +       if (unlikely(!dentry->d_inode)) {
10409 +               dput(dentry);
10410 +               dentry = ERR_PTR(-ENOENT);
10411 +       }
10412 +
10413 +out_name:
10414 +       free_page((unsigned long)arg.name);
10415 +out_file:
10416 +       fput(file);
10417 +out:
10418 +       if (unlikely(nsi_lock
10419 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
10420 +               if (!IS_ERR(dentry)) {
10421 +                       dput(dentry);
10422 +                       dentry = ERR_PTR(-ESTALE);
10423 +               }
10424 +       AuTraceErrPtr(dentry);
10425 +       return dentry;
10426 +}
10427 +
10428 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
10429 +                                       ino_t dir_ino,
10430 +                                       struct au_nfsd_si_lock *nsi_lock)
10431 +{
10432 +       struct dentry *dentry;
10433 +       struct path path;
10434 +
10435 +       if (dir_ino != AUFS_ROOT_INO) {
10436 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
10437 +               dentry = path.dentry;
10438 +               if (!path.dentry || IS_ERR(path.dentry))
10439 +                       goto out;
10440 +               AuDebugOn(au_test_anon(path.dentry));
10441 +       } else
10442 +               path.dentry = dget(sb->s_root);
10443 +
10444 +       path.mnt = au_mnt_get(sb);
10445 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
10446 +       path_put(&path);
10447 +
10448 +out:
10449 +       AuTraceErrPtr(dentry);
10450 +       return dentry;
10451 +}
10452 +
10453 +/* ---------------------------------------------------------------------- */
10454 +
10455 +static int h_acceptable(void *expv, struct dentry *dentry)
10456 +{
10457 +       return 1;
10458 +}
10459 +
10460 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
10461 +                          char *buf, int len, struct super_block *sb)
10462 +{
10463 +       char *p;
10464 +       int n;
10465 +       struct path path;
10466 +
10467 +       p = d_path(h_rootpath, buf, len);
10468 +       if (IS_ERR(p))
10469 +               goto out;
10470 +       n = strlen(p);
10471 +
10472 +       path.mnt = h_rootpath->mnt;
10473 +       path.dentry = h_parent;
10474 +       p = d_path(&path, buf, len);
10475 +       if (IS_ERR(p))
10476 +               goto out;
10477 +       if (n != 1)
10478 +               p += n;
10479 +
10480 +       path.mnt = au_mnt_get(sb);
10481 +       path.dentry = sb->s_root;
10482 +       p = d_path(&path, buf, len - strlen(p));
10483 +       mntput(path.mnt);
10484 +       if (IS_ERR(p))
10485 +               goto out;
10486 +       if (n != 1)
10487 +               p[strlen(p)] = '/';
10488 +
10489 +out:
10490 +       AuTraceErrPtr(p);
10491 +       return p;
10492 +}
10493 +
10494 +static
10495 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
10496 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
10497 +{
10498 +       struct dentry *dentry, *h_parent, *root;
10499 +       struct super_block *h_sb;
10500 +       char *pathname, *p;
10501 +       struct vfsmount *h_mnt;
10502 +       struct au_branch *br;
10503 +       int err;
10504 +       struct path path;
10505 +
10506 +       br = au_sbr(sb, nsi_lock->bindex);
10507 +       h_mnt = au_br_mnt(br);
10508 +       h_sb = h_mnt->mnt_sb;
10509 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
10510 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
10511 +                                     fh_len - Fh_tail, fh[Fh_h_type],
10512 +                                     h_acceptable, /*context*/NULL);
10513 +       dentry = h_parent;
10514 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
10515 +               AuWarn1("%s decode_fh failed, %ld\n",
10516 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
10517 +               goto out;
10518 +       }
10519 +       dentry = NULL;
10520 +       if (unlikely(au_test_anon(h_parent))) {
10521 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
10522 +                       au_sbtype(h_sb));
10523 +               goto out_h_parent;
10524 +       }
10525 +
10526 +       dentry = ERR_PTR(-ENOMEM);
10527 +       pathname = (void *)__get_free_page(GFP_NOFS);
10528 +       if (unlikely(!pathname))
10529 +               goto out_h_parent;
10530 +
10531 +       root = sb->s_root;
10532 +       path.mnt = h_mnt;
10533 +       di_read_lock_parent(root, !AuLock_IR);
10534 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
10535 +       di_read_unlock(root, !AuLock_IR);
10536 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
10537 +       dentry = (void *)p;
10538 +       if (IS_ERR(p))
10539 +               goto out_pathname;
10540 +
10541 +       si_read_unlock(sb);
10542 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
10543 +       dentry = ERR_PTR(err);
10544 +       if (unlikely(err))
10545 +               goto out_relock;
10546 +
10547 +       dentry = ERR_PTR(-ENOENT);
10548 +       AuDebugOn(au_test_anon(path.dentry));
10549 +       if (unlikely(!path.dentry->d_inode))
10550 +               goto out_path;
10551 +
10552 +       if (ino != path.dentry->d_inode->i_ino)
10553 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
10554 +       else
10555 +               dentry = dget(path.dentry);
10556 +
10557 +out_path:
10558 +       path_put(&path);
10559 +out_relock:
10560 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
10561 +               if (!IS_ERR(dentry)) {
10562 +                       dput(dentry);
10563 +                       dentry = ERR_PTR(-ESTALE);
10564 +               }
10565 +out_pathname:
10566 +       free_page((unsigned long)pathname);
10567 +out_h_parent:
10568 +       dput(h_parent);
10569 +out:
10570 +       AuTraceErrPtr(dentry);
10571 +       return dentry;
10572 +}
10573 +
10574 +/* ---------------------------------------------------------------------- */
10575 +
10576 +static struct dentry *
10577 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
10578 +                 int fh_type)
10579 +{
10580 +       struct dentry *dentry;
10581 +       __u32 *fh = fid->raw;
10582 +       struct au_branch *br;
10583 +       ino_t ino, dir_ino;
10584 +       struct au_nfsd_si_lock nsi_lock = {
10585 +               .force_lock     = 0
10586 +       };
10587 +
10588 +       dentry = ERR_PTR(-ESTALE);
10589 +       /* it should never happen, but the file handle is unreliable */
10590 +       if (unlikely(fh_len < Fh_tail))
10591 +               goto out;
10592 +       nsi_lock.sigen = fh[Fh_sigen];
10593 +       nsi_lock.br_id = fh[Fh_br_id];
10594 +
10595 +       /* branch id may be wrapped around */
10596 +       br = NULL;
10597 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
10598 +               goto out;
10599 +       nsi_lock.force_lock = 1;
10600 +
10601 +       /* is this inode still cached? */
10602 +       ino = decode_ino(fh + Fh_ino);
10603 +       /* it should never happen */
10604 +       if (unlikely(ino == AUFS_ROOT_INO))
10605 +               goto out;
10606 +
10607 +       dir_ino = decode_ino(fh + Fh_dir_ino);
10608 +       dentry = decode_by_ino(sb, ino, dir_ino);
10609 +       if (IS_ERR(dentry))
10610 +               goto out_unlock;
10611 +       if (dentry)
10612 +               goto accept;
10613 +
10614 +       /* is the parent dir cached? */
10615 +       br = au_sbr(sb, nsi_lock.bindex);
10616 +       atomic_inc(&br->br_count);
10617 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
10618 +       if (IS_ERR(dentry))
10619 +               goto out_unlock;
10620 +       if (dentry)
10621 +               goto accept;
10622 +
10623 +       /* lookup path */
10624 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
10625 +       if (IS_ERR(dentry))
10626 +               goto out_unlock;
10627 +       if (unlikely(!dentry))
10628 +               /* todo?: make it ESTALE */
10629 +               goto out_unlock;
10630 +
10631 +accept:
10632 +       if (!au_digen_test(dentry, au_sigen(sb))
10633 +           && dentry->d_inode->i_generation == fh[Fh_igen])
10634 +               goto out_unlock; /* success */
10635 +
10636 +       dput(dentry);
10637 +       dentry = ERR_PTR(-ESTALE);
10638 +out_unlock:
10639 +       if (br)
10640 +               atomic_dec(&br->br_count);
10641 +       si_read_unlock(sb);
10642 +out:
10643 +       AuTraceErrPtr(dentry);
10644 +       return dentry;
10645 +}
10646 +
10647 +#if 0 /* reserved for future use */
10648 +/* support subtreecheck option */
10649 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
10650 +                                       int fh_len, int fh_type)
10651 +{
10652 +       struct dentry *parent;
10653 +       __u32 *fh = fid->raw;
10654 +       ino_t dir_ino;
10655 +
10656 +       dir_ino = decode_ino(fh + Fh_dir_ino);
10657 +       parent = decode_by_ino(sb, dir_ino, 0);
10658 +       if (IS_ERR(parent))
10659 +               goto out;
10660 +       if (!parent)
10661 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
10662 +                                       dir_ino, fh, fh_len);
10663 +
10664 +out:
10665 +       AuTraceErrPtr(parent);
10666 +       return parent;
10667 +}
10668 +#endif
10669 +
10670 +/* ---------------------------------------------------------------------- */
10671 +
10672 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
10673 +                         struct inode *dir)
10674 +{
10675 +       int err;
10676 +       aufs_bindex_t bindex;
10677 +       struct super_block *sb, *h_sb;
10678 +       struct dentry *dentry, *parent, *h_parent;
10679 +       struct inode *h_dir;
10680 +       struct au_branch *br;
10681 +
10682 +       err = -ENOSPC;
10683 +       if (unlikely(*max_len <= Fh_tail)) {
10684 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
10685 +               goto out;
10686 +       }
10687 +
10688 +       err = FILEID_ROOT;
10689 +       if (inode->i_ino == AUFS_ROOT_INO) {
10690 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
10691 +               goto out;
10692 +       }
10693 +
10694 +       h_parent = NULL;
10695 +       sb = inode->i_sb;
10696 +       err = si_read_lock(sb, AuLock_FLUSH);
10697 +       if (unlikely(err))
10698 +               goto out;
10699 +
10700 +#ifdef CONFIG_AUFS_DEBUG
10701 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
10702 +               AuWarn1("NFS-exporting requires xino\n");
10703 +#endif
10704 +       err = -EIO;
10705 +       parent = NULL;
10706 +       ii_read_lock_child(inode);
10707 +       bindex = au_ibstart(inode);
10708 +       if (!dir) {
10709 +               dentry = d_find_alias(inode);
10710 +               if (unlikely(!dentry))
10711 +                       goto out_unlock;
10712 +               AuDebugOn(au_test_anon(dentry));
10713 +               parent = dget_parent(dentry);
10714 +               dput(dentry);
10715 +               if (unlikely(!parent))
10716 +                       goto out_unlock;
10717 +               dir = parent->d_inode;
10718 +       }
10719 +
10720 +       ii_read_lock_parent(dir);
10721 +       h_dir = au_h_iptr(dir, bindex);
10722 +       ii_read_unlock(dir);
10723 +       if (unlikely(!h_dir))
10724 +               goto out_parent;
10725 +       h_parent = d_find_alias(h_dir);
10726 +       if (unlikely(!h_parent))
10727 +               goto out_hparent;
10728 +
10729 +       err = -EPERM;
10730 +       br = au_sbr(sb, bindex);
10731 +       h_sb = au_br_sb(br);
10732 +       if (unlikely(!h_sb->s_export_op)) {
10733 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
10734 +               goto out_hparent;
10735 +       }
10736 +
10737 +       fh[Fh_br_id] = br->br_id;
10738 +       fh[Fh_sigen] = au_sigen(sb);
10739 +       encode_ino(fh + Fh_ino, inode->i_ino);
10740 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
10741 +       fh[Fh_igen] = inode->i_generation;
10742 +
10743 +       *max_len -= Fh_tail;
10744 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
10745 +                                          max_len,
10746 +                                          /*connectable or subtreecheck*/0);
10747 +       err = fh[Fh_h_type];
10748 +       *max_len += Fh_tail;
10749 +       /* todo: macros? */
10750 +       if (err != FILEID_INVALID)
10751 +               err = 99;
10752 +       else
10753 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
10754 +
10755 +out_hparent:
10756 +       dput(h_parent);
10757 +out_parent:
10758 +       dput(parent);
10759 +out_unlock:
10760 +       ii_read_unlock(inode);
10761 +       si_read_unlock(sb);
10762 +out:
10763 +       if (unlikely(err < 0))
10764 +               err = FILEID_INVALID;
10765 +       return err;
10766 +}
10767 +
10768 +/* ---------------------------------------------------------------------- */
10769 +
10770 +static int aufs_commit_metadata(struct inode *inode)
10771 +{
10772 +       int err;
10773 +       aufs_bindex_t bindex;
10774 +       struct super_block *sb;
10775 +       struct inode *h_inode;
10776 +       int (*f)(struct inode *inode);
10777 +
10778 +       sb = inode->i_sb;
10779 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
10780 +       ii_write_lock_child(inode);
10781 +       bindex = au_ibstart(inode);
10782 +       AuDebugOn(bindex < 0);
10783 +       h_inode = au_h_iptr(inode, bindex);
10784 +
10785 +       f = h_inode->i_sb->s_export_op->commit_metadata;
10786 +       if (f)
10787 +               err = f(h_inode);
10788 +       else {
10789 +               struct writeback_control wbc = {
10790 +                       .sync_mode      = WB_SYNC_ALL,
10791 +                       .nr_to_write    = 0 /* metadata only */
10792 +               };
10793 +
10794 +               err = sync_inode(h_inode, &wbc);
10795 +       }
10796 +
10797 +       au_cpup_attr_timesizes(inode);
10798 +       ii_write_unlock(inode);
10799 +       si_read_unlock(sb);
10800 +       return err;
10801 +}
10802 +
10803 +/* ---------------------------------------------------------------------- */
10804 +
10805 +static struct export_operations aufs_export_op = {
10806 +       .fh_to_dentry           = aufs_fh_to_dentry,
10807 +       /* .fh_to_parent        = aufs_fh_to_parent, */
10808 +       .encode_fh              = aufs_encode_fh,
10809 +       .commit_metadata        = aufs_commit_metadata
10810 +};
10811 +
10812 +void au_export_init(struct super_block *sb)
10813 +{
10814 +       struct au_sbinfo *sbinfo;
10815 +       __u32 u;
10816 +
10817 +       sb->s_export_op = &aufs_export_op;
10818 +       sbinfo = au_sbi(sb);
10819 +       sbinfo->si_xigen = NULL;
10820 +       get_random_bytes(&u, sizeof(u));
10821 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
10822 +       atomic_set(&sbinfo->si_xigen_next, u);
10823 +}
10824 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
10825 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
10826 +++ linux/fs/aufs/file.c        2014-06-24 00:14:06.782719306 +0200
10827 @@ -0,0 +1,711 @@
10828 +/*
10829 + * Copyright (C) 2005-2014 Junjiro R. Okajima
10830 + *
10831 + * This program, aufs is free software; you can redistribute it and/or modify
10832 + * it under the terms of the GNU General Public License as published by
10833 + * the Free Software Foundation; either version 2 of the License, or
10834 + * (at your option) any later version.
10835 + *
10836 + * This program is distributed in the hope that it will be useful,
10837 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10838 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10839 + * GNU General Public License for more details.
10840 + *
10841 + * You should have received a copy of the GNU General Public License
10842 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10843 + */
10844 +
10845 +/*
10846 + * handling file/dir, and address_space operation
10847 + */
10848 +
10849 +#ifdef CONFIG_AUFS_DEBUG
10850 +#include <linux/migrate.h>
10851 +#endif
10852 +#include <linux/pagemap.h>
10853 +#include "aufs.h"
10854 +
10855 +/* drop flags for writing */
10856 +unsigned int au_file_roflags(unsigned int flags)
10857 +{
10858 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
10859 +       flags |= O_RDONLY | O_NOATIME;
10860 +       return flags;
10861 +}
10862 +
10863 +/* common functions to regular file and dir */
10864 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
10865 +                      struct file *file, int force_wr)
10866 +{
10867 +       struct file *h_file;
10868 +       struct dentry *h_dentry;
10869 +       struct inode *h_inode;
10870 +       struct super_block *sb;
10871 +       struct au_branch *br;
10872 +       struct path h_path;
10873 +       int err, exec_flag;
10874 +
10875 +       /* a race condition can happen between open and unlink/rmdir */
10876 +       h_file = ERR_PTR(-ENOENT);
10877 +       h_dentry = au_h_dptr(dentry, bindex);
10878 +       if (au_test_nfsd() && !h_dentry)
10879 +               goto out;
10880 +       h_inode = h_dentry->d_inode;
10881 +       if (au_test_nfsd() && !h_inode)
10882 +               goto out;
10883 +       spin_lock(&h_dentry->d_lock);
10884 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
10885 +               || !h_inode
10886 +               /* || !dentry->d_inode->i_nlink */
10887 +               ;
10888 +       spin_unlock(&h_dentry->d_lock);
10889 +       if (unlikely(err))
10890 +               goto out;
10891 +
10892 +       sb = dentry->d_sb;
10893 +       br = au_sbr(sb, bindex);
10894 +       h_file = ERR_PTR(-EACCES);
10895 +       exec_flag = flags & __FMODE_EXEC;
10896 +       if (exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC))
10897 +               goto out;
10898 +
10899 +       /* drop flags for writing */
10900 +       if (au_test_ro(sb, bindex, dentry->d_inode)) {
10901 +               if (force_wr && !(flags & O_WRONLY))
10902 +                       force_wr = 0;
10903 +               flags = au_file_roflags(flags);
10904 +               if (force_wr) {
10905 +                       h_file = ERR_PTR(-EROFS);
10906 +                       flags = au_file_roflags(flags);
10907 +                       if (unlikely(vfsub_native_ro(h_inode)
10908 +                                    || IS_APPEND(h_inode)))
10909 +                               goto out;
10910 +                       flags &= ~O_ACCMODE;
10911 +                       flags |= O_WRONLY;
10912 +               }
10913 +       }
10914 +       flags &= ~O_CREAT;
10915 +       atomic_inc(&br->br_count);
10916 +       h_path.dentry = h_dentry;
10917 +       h_path.mnt = au_br_mnt(br);
10918 +       h_file = vfsub_dentry_open(&h_path, flags);
10919 +       if (IS_ERR(h_file))
10920 +               goto out_br;
10921 +
10922 +       if (exec_flag) {
10923 +               err = deny_write_access(h_file);
10924 +               if (unlikely(err)) {
10925 +                       fput(h_file);
10926 +                       h_file = ERR_PTR(err);
10927 +                       goto out_br;
10928 +               }
10929 +       }
10930 +       fsnotify_open(h_file);
10931 +       goto out; /* success */
10932 +
10933 +out_br:
10934 +       atomic_dec(&br->br_count);
10935 +out:
10936 +       return h_file;
10937 +}
10938 +
10939 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
10940 +              struct au_fidir *fidir)
10941 +{
10942 +       int err;
10943 +       struct dentry *dentry;
10944 +
10945 +       err = au_finfo_init(file, fidir);
10946 +       if (unlikely(err))
10947 +               goto out;
10948 +
10949 +       dentry = file->f_dentry;
10950 +       di_read_lock_child(dentry, AuLock_IR);
10951 +       err = open(file, vfsub_file_flags(file));
10952 +       di_read_unlock(dentry, AuLock_IR);
10953 +
10954 +       fi_write_unlock(file);
10955 +       if (unlikely(err)) {
10956 +               au_fi(file)->fi_hdir = NULL;
10957 +               au_finfo_fin(file);
10958 +       }
10959 +
10960 +out:
10961 +       return err;
10962 +}
10963 +
10964 +int au_reopen_nondir(struct file *file)
10965 +{
10966 +       int err;
10967 +       aufs_bindex_t bstart;
10968 +       struct dentry *dentry;
10969 +       struct file *h_file, *h_file_tmp;
10970 +
10971 +       dentry = file->f_dentry;
10972 +       bstart = au_dbstart(dentry);
10973 +       h_file_tmp = NULL;
10974 +       if (au_fbstart(file) == bstart) {
10975 +               h_file = au_hf_top(file);
10976 +               if (file->f_mode == h_file->f_mode)
10977 +                       return 0; /* success */
10978 +               h_file_tmp = h_file;
10979 +               get_file(h_file_tmp);
10980 +               au_set_h_fptr(file, bstart, NULL);
10981 +       }
10982 +       AuDebugOn(au_fi(file)->fi_hdir);
10983 +       /*
10984 +        * it can happen
10985 +        * file exists on both of rw and ro
10986 +        * open --> dbstart and fbstart are both 0
10987 +        * prepend a branch as rw, "rw" become ro
10988 +        * remove rw/file
10989 +        * delete the top branch, "rw" becomes rw again
10990 +        *      --> dbstart is 1, fbstart is still 0
10991 +        * write --> fbstart is 0 but dbstart is 1
10992 +        */
10993 +       /* AuDebugOn(au_fbstart(file) < bstart); */
10994 +
10995 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
10996 +                          file, /*force_wr*/0);
10997 +       err = PTR_ERR(h_file);
10998 +       if (IS_ERR(h_file)) {
10999 +               if (h_file_tmp) {
11000 +                       atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
11001 +                       au_set_h_fptr(file, bstart, h_file_tmp);
11002 +                       h_file_tmp = NULL;
11003 +               }
11004 +               goto out; /* todo: close all? */
11005 +       }
11006 +
11007 +       err = 0;
11008 +       au_set_fbstart(file, bstart);
11009 +       au_set_h_fptr(file, bstart, h_file);
11010 +       au_update_figen(file);
11011 +       /* todo: necessary? */
11012 +       /* file->f_ra = h_file->f_ra; */
11013 +
11014 +out:
11015 +       if (h_file_tmp)
11016 +               fput(h_file_tmp);
11017 +       return err;
11018 +}
11019 +
11020 +/* ---------------------------------------------------------------------- */
11021 +
11022 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
11023 +                       struct dentry *hi_wh)
11024 +{
11025 +       int err;
11026 +       aufs_bindex_t bstart;
11027 +       struct au_dinfo *dinfo;
11028 +       struct dentry *h_dentry;
11029 +       struct au_hdentry *hdp;
11030 +
11031 +       dinfo = au_di(file->f_dentry);
11032 +       AuRwMustWriteLock(&dinfo->di_rwsem);
11033 +
11034 +       bstart = dinfo->di_bstart;
11035 +       dinfo->di_bstart = btgt;
11036 +       hdp = dinfo->di_hdentry;
11037 +       h_dentry = hdp[0 + btgt].hd_dentry;
11038 +       hdp[0 + btgt].hd_dentry = hi_wh;
11039 +       err = au_reopen_nondir(file);
11040 +       hdp[0 + btgt].hd_dentry = h_dentry;
11041 +       dinfo->di_bstart = bstart;
11042 +
11043 +       return err;
11044 +}
11045 +
11046 +static int au_ready_to_write_wh(struct file *file, loff_t len,
11047 +                               aufs_bindex_t bcpup, struct au_pin *pin)
11048 +{
11049 +       int err;
11050 +       struct inode *inode, *h_inode;
11051 +       struct dentry *h_dentry, *hi_wh;
11052 +       struct au_cp_generic cpg = {
11053 +               .dentry = file->f_dentry,
11054 +               .bdst   = bcpup,
11055 +               .bsrc   = -1,
11056 +               .len    = len,
11057 +               .pin    = pin
11058 +       };
11059 +
11060 +       au_update_dbstart(cpg.dentry);
11061 +       inode = cpg.dentry->d_inode;
11062 +       h_inode = NULL;
11063 +       if (au_dbstart(cpg.dentry) <= bcpup
11064 +           && au_dbend(cpg.dentry) >= bcpup) {
11065 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
11066 +               if (h_dentry)
11067 +                       h_inode = h_dentry->d_inode;
11068 +       }
11069 +       hi_wh = au_hi_wh(inode, bcpup);
11070 +       if (!hi_wh && !h_inode)
11071 +               err = au_sio_cpup_wh(&cpg, file);
11072 +       else
11073 +               /* already copied-up after unlink */
11074 +               err = au_reopen_wh(file, bcpup, hi_wh);
11075 +
11076 +       if (!err
11077 +           && (inode->i_nlink > 1
11078 +               || (inode->i_state & I_LINKABLE))
11079 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
11080 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
11081 +
11082 +       return err;
11083 +}
11084 +
11085 +/*
11086 + * prepare the @file for writing.
11087 + */
11088 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
11089 +{
11090 +       int err;
11091 +       aufs_bindex_t dbstart;
11092 +       struct dentry *parent, *h_dentry;
11093 +       struct inode *inode;
11094 +       struct super_block *sb;
11095 +       struct file *h_file;
11096 +       struct au_cp_generic cpg = {
11097 +               .dentry = file->f_dentry,
11098 +               .bdst   = -1,
11099 +               .bsrc   = -1,
11100 +               .len    = len,
11101 +               .pin    = pin,
11102 +               .flags  = AuCpup_DTIME
11103 +       };
11104 +
11105 +       sb = cpg.dentry->d_sb;
11106 +       inode = cpg.dentry->d_inode;
11107 +       cpg.bsrc = au_fbstart(file);
11108 +       err = au_test_ro(sb, cpg.bsrc, inode);
11109 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
11110 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
11111 +                            /*flags*/0);
11112 +               goto out;
11113 +       }
11114 +
11115 +       /* need to cpup or reopen */
11116 +       parent = dget_parent(cpg.dentry);
11117 +       di_write_lock_parent(parent);
11118 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
11119 +       cpg.bdst = err;
11120 +       if (unlikely(err < 0))
11121 +               goto out_dgrade;
11122 +       err = 0;
11123 +
11124 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
11125 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
11126 +               if (unlikely(err))
11127 +                       goto out_dgrade;
11128 +       }
11129 +
11130 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
11131 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
11132 +       if (unlikely(err))
11133 +               goto out_dgrade;
11134 +
11135 +       h_dentry = au_hf_top(file)->f_dentry;
11136 +       dbstart = au_dbstart(cpg.dentry);
11137 +       if (dbstart <= cpg.bdst) {
11138 +               h_dentry = au_h_dptr(cpg.dentry, cpg.bdst);
11139 +               AuDebugOn(!h_dentry);
11140 +               cpg.bsrc = cpg.bdst;
11141 +       }
11142 +
11143 +       if (dbstart <= cpg.bdst         /* just reopen */
11144 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
11145 +               ) {
11146 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
11147 +               if (IS_ERR(h_file))
11148 +                       err = PTR_ERR(h_file);
11149 +               else {
11150 +                       di_downgrade_lock(parent, AuLock_IR);
11151 +                       if (dbstart > cpg.bdst)
11152 +                               err = au_sio_cpup_simple(&cpg);
11153 +                       if (!err)
11154 +                               err = au_reopen_nondir(file);
11155 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
11156 +               }
11157 +       } else {                        /* copyup as wh and reopen */
11158 +               /*
11159 +                * since writable hfsplus branch is not supported,
11160 +                * h_open_pre/post() are unnecessary.
11161 +                */
11162 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
11163 +               di_downgrade_lock(parent, AuLock_IR);
11164 +       }
11165 +
11166 +       if (!err) {
11167 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
11168 +               goto out_dput; /* success */
11169 +       }
11170 +       au_unpin(pin);
11171 +       goto out_unlock;
11172 +
11173 +out_dgrade:
11174 +       di_downgrade_lock(parent, AuLock_IR);
11175 +out_unlock:
11176 +       di_read_unlock(parent, AuLock_IR);
11177 +out_dput:
11178 +       dput(parent);
11179 +out:
11180 +       return err;
11181 +}
11182 +
11183 +/* ---------------------------------------------------------------------- */
11184 +
11185 +int au_do_flush(struct file *file, fl_owner_t id,
11186 +               int (*flush)(struct file *file, fl_owner_t id))
11187 +{
11188 +       int err;
11189 +       struct super_block *sb;
11190 +       struct inode *inode;
11191 +
11192 +       inode = file_inode(file);
11193 +       sb = inode->i_sb;
11194 +       si_noflush_read_lock(sb);
11195 +       fi_read_lock(file);
11196 +       ii_read_lock_child(inode);
11197 +
11198 +       err = flush(file, id);
11199 +       au_cpup_attr_timesizes(inode);
11200 +
11201 +       ii_read_unlock(inode);
11202 +       fi_read_unlock(file);
11203 +       si_read_unlock(sb);
11204 +       return err;
11205 +}
11206 +
11207 +/* ---------------------------------------------------------------------- */
11208 +
11209 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
11210 +{
11211 +       int err;
11212 +       struct au_pin pin;
11213 +       struct au_finfo *finfo;
11214 +       struct dentry *parent, *hi_wh;
11215 +       struct inode *inode;
11216 +       struct super_block *sb;
11217 +       struct au_cp_generic cpg = {
11218 +               .dentry = file->f_dentry,
11219 +               .bdst   = -1,
11220 +               .bsrc   = -1,
11221 +               .len    = -1,
11222 +               .pin    = &pin,
11223 +               .flags  = AuCpup_DTIME
11224 +       };
11225 +
11226 +       FiMustWriteLock(file);
11227 +
11228 +       err = 0;
11229 +       finfo = au_fi(file);
11230 +       sb = cpg.dentry->d_sb;
11231 +       inode = cpg.dentry->d_inode;
11232 +       cpg.bdst = au_ibstart(inode);
11233 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
11234 +               goto out;
11235 +
11236 +       parent = dget_parent(cpg.dentry);
11237 +       if (au_test_ro(sb, cpg.bdst, inode)) {
11238 +               di_read_lock_parent(parent, !AuLock_IR);
11239 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
11240 +               cpg.bdst = err;
11241 +               di_read_unlock(parent, !AuLock_IR);
11242 +               if (unlikely(err < 0))
11243 +                       goto out_parent;
11244 +               err = 0;
11245 +       }
11246 +
11247 +       di_read_lock_parent(parent, AuLock_IR);
11248 +       hi_wh = au_hi_wh(inode, cpg.bdst);
11249 +       if (!S_ISDIR(inode->i_mode)
11250 +           && au_opt_test(au_mntflags(sb), PLINK)
11251 +           && au_plink_test(inode)
11252 +           && !d_unhashed(cpg.dentry)
11253 +           && cpg.bdst < au_dbstart(cpg.dentry)) {
11254 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
11255 +               if (unlikely(err))
11256 +                       goto out_unlock;
11257 +
11258 +               /* always superio. */
11259 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
11260 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
11261 +               if (!err) {
11262 +                       err = au_sio_cpup_simple(&cpg);
11263 +                       au_unpin(&pin);
11264 +               }
11265 +       } else if (hi_wh) {
11266 +               /* already copied-up after unlink */
11267 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
11268 +               *need_reopen = 0;
11269 +       }
11270 +
11271 +out_unlock:
11272 +       di_read_unlock(parent, AuLock_IR);
11273 +out_parent:
11274 +       dput(parent);
11275 +out:
11276 +       return err;
11277 +}
11278 +
11279 +static void au_do_refresh_dir(struct file *file)
11280 +{
11281 +       aufs_bindex_t bindex, bend, new_bindex, brid;
11282 +       struct au_hfile *p, tmp, *q;
11283 +       struct au_finfo *finfo;
11284 +       struct super_block *sb;
11285 +       struct au_fidir *fidir;
11286 +
11287 +       FiMustWriteLock(file);
11288 +
11289 +       sb = file->f_dentry->d_sb;
11290 +       finfo = au_fi(file);
11291 +       fidir = finfo->fi_hdir;
11292 +       AuDebugOn(!fidir);
11293 +       p = fidir->fd_hfile + finfo->fi_btop;
11294 +       brid = p->hf_br->br_id;
11295 +       bend = fidir->fd_bbot;
11296 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
11297 +               if (!p->hf_file)
11298 +                       continue;
11299 +
11300 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
11301 +               if (new_bindex == bindex)
11302 +                       continue;
11303 +               if (new_bindex < 0) {
11304 +                       au_set_h_fptr(file, bindex, NULL);
11305 +                       continue;
11306 +               }
11307 +
11308 +               /* swap two lower inode, and loop again */
11309 +               q = fidir->fd_hfile + new_bindex;
11310 +               tmp = *q;
11311 +               *q = *p;
11312 +               *p = tmp;
11313 +               if (tmp.hf_file) {
11314 +                       bindex--;
11315 +                       p--;
11316 +               }
11317 +       }
11318 +
11319 +       p = fidir->fd_hfile;
11320 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_dentry)) {
11321 +               bend = au_sbend(sb);
11322 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
11323 +                    finfo->fi_btop++, p++)
11324 +                       if (p->hf_file) {
11325 +                               if (file_inode(p->hf_file))
11326 +                                       break;
11327 +                               else
11328 +                                       au_hfput(p, file);
11329 +                       }
11330 +       } else {
11331 +               bend = au_br_index(sb, brid);
11332 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
11333 +                    finfo->fi_btop++, p++)
11334 +                       if (p->hf_file)
11335 +                               au_hfput(p, file);
11336 +               bend = au_sbend(sb);
11337 +       }
11338 +
11339 +       p = fidir->fd_hfile + bend;
11340 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
11341 +            fidir->fd_bbot--, p--)
11342 +               if (p->hf_file) {
11343 +                       if (file_inode(p->hf_file))
11344 +                               break;
11345 +                       else
11346 +                               au_hfput(p, file);
11347 +               }
11348 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
11349 +}
11350 +
11351 +/*
11352 + * after branch manipulating, refresh the file.
11353 + */
11354 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
11355 +{
11356 +       int err, need_reopen;
11357 +       aufs_bindex_t bend, bindex;
11358 +       struct dentry *dentry;
11359 +       struct au_finfo *finfo;
11360 +       struct au_hfile *hfile;
11361 +
11362 +       dentry = file->f_dentry;
11363 +       finfo = au_fi(file);
11364 +       if (!finfo->fi_hdir) {
11365 +               hfile = &finfo->fi_htop;
11366 +               AuDebugOn(!hfile->hf_file);
11367 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
11368 +               AuDebugOn(bindex < 0);
11369 +               if (bindex != finfo->fi_btop)
11370 +                       au_set_fbstart(file, bindex);
11371 +       } else {
11372 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
11373 +               if (unlikely(err))
11374 +                       goto out;
11375 +               au_do_refresh_dir(file);
11376 +       }
11377 +
11378 +       err = 0;
11379 +       need_reopen = 1;
11380 +       if (!au_test_mmapped(file))
11381 +               err = au_file_refresh_by_inode(file, &need_reopen);
11382 +       if (!err && need_reopen && !d_unlinked(dentry))
11383 +               err = reopen(file);
11384 +       if (!err) {
11385 +               au_update_figen(file);
11386 +               goto out; /* success */
11387 +       }
11388 +
11389 +       /* error, close all lower files */
11390 +       if (finfo->fi_hdir) {
11391 +               bend = au_fbend_dir(file);
11392 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
11393 +                       au_set_h_fptr(file, bindex, NULL);
11394 +       }
11395 +
11396 +out:
11397 +       return err;
11398 +}
11399 +
11400 +/* common function to regular file and dir */
11401 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
11402 +                         int wlock)
11403 +{
11404 +       int err;
11405 +       unsigned int sigen, figen;
11406 +       aufs_bindex_t bstart;
11407 +       unsigned char pseudo_link;
11408 +       struct dentry *dentry;
11409 +       struct inode *inode;
11410 +
11411 +       err = 0;
11412 +       dentry = file->f_dentry;
11413 +       inode = dentry->d_inode;
11414 +       sigen = au_sigen(dentry->d_sb);
11415 +       fi_write_lock(file);
11416 +       figen = au_figen(file);
11417 +       di_write_lock_child(dentry);
11418 +       bstart = au_dbstart(dentry);
11419 +       pseudo_link = (bstart != au_ibstart(inode));
11420 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
11421 +               if (!wlock) {
11422 +                       di_downgrade_lock(dentry, AuLock_IR);
11423 +                       fi_downgrade_lock(file);
11424 +               }
11425 +               goto out; /* success */
11426 +       }
11427 +
11428 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
11429 +       if (au_digen_test(dentry, sigen)) {
11430 +               err = au_reval_dpath(dentry, sigen);
11431 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
11432 +       }
11433 +
11434 +       if (!err)
11435 +               err = refresh_file(file, reopen);
11436 +       if (!err) {
11437 +               if (!wlock) {
11438 +                       di_downgrade_lock(dentry, AuLock_IR);
11439 +                       fi_downgrade_lock(file);
11440 +               }
11441 +       } else {
11442 +               di_write_unlock(dentry);
11443 +               fi_write_unlock(file);
11444 +       }
11445 +
11446 +out:
11447 +       return err;
11448 +}
11449 +
11450 +/* ---------------------------------------------------------------------- */
11451 +
11452 +/* cf. aufs_nopage() */
11453 +/* for madvise(2) */
11454 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
11455 +{
11456 +       unlock_page(page);
11457 +       return 0;
11458 +}
11459 +
11460 +/* it will never be called, but necessary to support O_DIRECT */
11461 +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
11462 +                             const struct iovec *iov, loff_t offset,
11463 +                             unsigned long nr_segs)
11464 +{ BUG(); return 0; }
11465 +
11466 +/*
11467 + * it will never be called, but madvise and fadvise behaves differently
11468 + * when get_xip_mem is defined
11469 + */
11470 +static int aufs_get_xip_mem(struct address_space *mapping, pgoff_t pgoff,
11471 +                           int create, void **kmem, unsigned long *pfn)
11472 +{ BUG(); return 0; }
11473 +
11474 +/* they will never be called. */
11475 +#ifdef CONFIG_AUFS_DEBUG
11476 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
11477 +                           loff_t pos, unsigned len, unsigned flags,
11478 +                           struct page **pagep, void **fsdata)
11479 +{ AuUnsupport(); return 0; }
11480 +static int aufs_write_end(struct file *file, struct address_space *mapping,
11481 +                         loff_t pos, unsigned len, unsigned copied,
11482 +                         struct page *page, void *fsdata)
11483 +{ AuUnsupport(); return 0; }
11484 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
11485 +{ AuUnsupport(); return 0; }
11486 +
11487 +static int aufs_set_page_dirty(struct page *page)
11488 +{ AuUnsupport(); return 0; }
11489 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
11490 +                               unsigned int length)
11491 +{ AuUnsupport(); }
11492 +static int aufs_releasepage(struct page *page, gfp_t gfp)
11493 +{ AuUnsupport(); return 0; }
11494 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
11495 +                           struct page *page, enum migrate_mode mode)
11496 +{ AuUnsupport(); return 0; }
11497 +static int aufs_launder_page(struct page *page)
11498 +{ AuUnsupport(); return 0; }
11499 +static int aufs_is_partially_uptodate(struct page *page,
11500 +                                     unsigned long from,
11501 +                                     unsigned long count)
11502 +{ AuUnsupport(); return 0; }
11503 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
11504 +                                   bool *writeback)
11505 +{ AuUnsupport(); }
11506 +static int aufs_error_remove_page(struct address_space *mapping,
11507 +                                 struct page *page)
11508 +{ AuUnsupport(); return 0; }
11509 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
11510 +                             sector_t *span)
11511 +{ AuUnsupport(); return 0; }
11512 +static void aufs_swap_deactivate(struct file *file)
11513 +{ AuUnsupport(); }
11514 +#endif /* CONFIG_AUFS_DEBUG */
11515 +
11516 +const struct address_space_operations aufs_aop = {
11517 +       .readpage               = aufs_readpage,
11518 +       .direct_IO              = aufs_direct_IO,
11519 +       .get_xip_mem            = aufs_get_xip_mem,
11520 +#ifdef CONFIG_AUFS_DEBUG
11521 +       .writepage              = aufs_writepage,
11522 +       /* no writepages, because of writepage */
11523 +       .set_page_dirty         = aufs_set_page_dirty,
11524 +       /* no readpages, because of readpage */
11525 +       .write_begin            = aufs_write_begin,
11526 +       .write_end              = aufs_write_end,
11527 +       /* no bmap, no block device */
11528 +       .invalidatepage         = aufs_invalidatepage,
11529 +       .releasepage            = aufs_releasepage,
11530 +       .migratepage            = aufs_migratepage,
11531 +       .launder_page           = aufs_launder_page,
11532 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
11533 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
11534 +       .error_remove_page      = aufs_error_remove_page,
11535 +       .swap_activate          = aufs_swap_activate,
11536 +       .swap_deactivate        = aufs_swap_deactivate
11537 +#endif /* CONFIG_AUFS_DEBUG */
11538 +};
11539 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
11540 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
11541 +++ linux/fs/aufs/file.h        2014-06-24 00:14:06.782719306 +0200
11542 @@ -0,0 +1,289 @@
11543 +/*
11544 + * Copyright (C) 2005-2014 Junjiro R. Okajima
11545 + *
11546 + * This program, aufs is free software; you can redistribute it and/or modify
11547 + * it under the terms of the GNU General Public License as published by
11548 + * the Free Software Foundation; either version 2 of the License, or
11549 + * (at your option) any later version.
11550 + *
11551 + * This program is distributed in the hope that it will be useful,
11552 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11553 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11554 + * GNU General Public License for more details.
11555 + *
11556 + * You should have received a copy of the GNU General Public License
11557 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11558 + */
11559 +
11560 +/*
11561 + * file operations
11562 + */
11563 +
11564 +#ifndef __AUFS_FILE_H__
11565 +#define __AUFS_FILE_H__
11566 +
11567 +#ifdef __KERNEL__
11568 +
11569 +#include <linux/file.h>
11570 +#include <linux/fs.h>
11571 +#include <linux/poll.h>
11572 +#include "rwsem.h"
11573 +
11574 +struct au_branch;
11575 +struct au_hfile {
11576 +       struct file             *hf_file;
11577 +       struct au_branch        *hf_br;
11578 +};
11579 +
11580 +struct au_vdir;
11581 +struct au_fidir {
11582 +       aufs_bindex_t           fd_bbot;
11583 +       aufs_bindex_t           fd_nent;
11584 +       struct au_vdir          *fd_vdir_cache;
11585 +       struct au_hfile         fd_hfile[];
11586 +};
11587 +
11588 +static inline int au_fidir_sz(int nent)
11589 +{
11590 +       AuDebugOn(nent < 0);
11591 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
11592 +}
11593 +
11594 +struct au_finfo {
11595 +       atomic_t                fi_generation;
11596 +
11597 +       struct au_rwsem         fi_rwsem;
11598 +       aufs_bindex_t           fi_btop;
11599 +
11600 +       /* do not union them */
11601 +       struct {                                /* for non-dir */
11602 +               struct au_hfile                 fi_htop;
11603 +               atomic_t                        fi_mmapped;
11604 +       };
11605 +       struct au_fidir         *fi_hdir;       /* for dir only */
11606 +
11607 +       struct hlist_node       fi_hlist;
11608 +       struct file             *fi_file;       /* very ugly */
11609 +} ____cacheline_aligned_in_smp;
11610 +
11611 +/* ---------------------------------------------------------------------- */
11612 +
11613 +/* file.c */
11614 +extern const struct address_space_operations aufs_aop;
11615 +unsigned int au_file_roflags(unsigned int flags);
11616 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
11617 +                      struct file *file, int force_wr);
11618 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
11619 +              struct au_fidir *fidir);
11620 +int au_reopen_nondir(struct file *file);
11621 +struct au_pin;
11622 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
11623 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
11624 +                         int wlock);
11625 +int au_do_flush(struct file *file, fl_owner_t id,
11626 +               int (*flush)(struct file *file, fl_owner_t id));
11627 +
11628 +/* poll.c */
11629 +#ifdef CONFIG_AUFS_POLL
11630 +unsigned int aufs_poll(struct file *file, poll_table *wait);
11631 +#endif
11632 +
11633 +#ifdef CONFIG_AUFS_BR_HFSPLUS
11634 +/* hfsplus.c */
11635 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
11636 +                          int force_wr);
11637 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
11638 +                   struct file *h_file);
11639 +#else
11640 +static inline
11641 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
11642 +                          int force_wr)
11643 +{
11644 +       return NULL;
11645 +}
11646 +
11647 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
11648 +          struct file *h_file);
11649 +#endif
11650 +
11651 +/* f_op.c */
11652 +extern const struct file_operations aufs_file_fop;
11653 +int au_do_open_nondir(struct file *file, int flags);
11654 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
11655 +
11656 +/* finfo.c */
11657 +void au_hfput(struct au_hfile *hf, struct file *file);
11658 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
11659 +                  struct file *h_file);
11660 +
11661 +void au_update_figen(struct file *file);
11662 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
11663 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
11664 +
11665 +void au_fi_init_once(void *_fi);
11666 +void au_finfo_fin(struct file *file);
11667 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
11668 +
11669 +/* ioctl.c */
11670 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
11671 +#ifdef CONFIG_COMPAT
11672 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
11673 +                          unsigned long arg);
11674 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
11675 +                             unsigned long arg);
11676 +#endif
11677 +
11678 +/* ---------------------------------------------------------------------- */
11679 +
11680 +static inline struct au_finfo *au_fi(struct file *file)
11681 +{
11682 +       return file->private_data;
11683 +}
11684 +
11685 +/* ---------------------------------------------------------------------- */
11686 +
11687 +/*
11688 + * fi_read_lock, fi_write_lock,
11689 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
11690 + */
11691 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
11692 +
11693 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
11694 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
11695 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
11696 +
11697 +/* ---------------------------------------------------------------------- */
11698 +
11699 +/* todo: hard/soft set? */
11700 +static inline aufs_bindex_t au_fbstart(struct file *file)
11701 +{
11702 +       FiMustAnyLock(file);
11703 +       return au_fi(file)->fi_btop;
11704 +}
11705 +
11706 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
11707 +{
11708 +       FiMustAnyLock(file);
11709 +       AuDebugOn(!au_fi(file)->fi_hdir);
11710 +       return au_fi(file)->fi_hdir->fd_bbot;
11711 +}
11712 +
11713 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
11714 +{
11715 +       FiMustAnyLock(file);
11716 +       AuDebugOn(!au_fi(file)->fi_hdir);
11717 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
11718 +}
11719 +
11720 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
11721 +{
11722 +       FiMustWriteLock(file);
11723 +       au_fi(file)->fi_btop = bindex;
11724 +}
11725 +
11726 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
11727 +{
11728 +       FiMustWriteLock(file);
11729 +       AuDebugOn(!au_fi(file)->fi_hdir);
11730 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
11731 +}
11732 +
11733 +static inline void au_set_fvdir_cache(struct file *file,
11734 +                                     struct au_vdir *vdir_cache)
11735 +{
11736 +       FiMustWriteLock(file);
11737 +       AuDebugOn(!au_fi(file)->fi_hdir);
11738 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
11739 +}
11740 +
11741 +static inline struct file *au_hf_top(struct file *file)
11742 +{
11743 +       FiMustAnyLock(file);
11744 +       AuDebugOn(au_fi(file)->fi_hdir);
11745 +       return au_fi(file)->fi_htop.hf_file;
11746 +}
11747 +
11748 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
11749 +{
11750 +       FiMustAnyLock(file);
11751 +       AuDebugOn(!au_fi(file)->fi_hdir);
11752 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
11753 +}
11754 +
11755 +/* todo: memory barrier? */
11756 +static inline unsigned int au_figen(struct file *f)
11757 +{
11758 +       return atomic_read(&au_fi(f)->fi_generation);
11759 +}
11760 +
11761 +static inline void au_set_mmapped(struct file *f)
11762 +{
11763 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
11764 +               return;
11765 +       pr_warn("fi_mmapped wrapped around\n");
11766 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
11767 +               ;
11768 +}
11769 +
11770 +static inline void au_unset_mmapped(struct file *f)
11771 +{
11772 +       atomic_dec(&au_fi(f)->fi_mmapped);
11773 +}
11774 +
11775 +static inline int au_test_mmapped(struct file *f)
11776 +{
11777 +       return atomic_read(&au_fi(f)->fi_mmapped);
11778 +}
11779 +
11780 +/* customize vma->vm_file */
11781 +
11782 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
11783 +                                      struct file *file)
11784 +{
11785 +       struct file *f;
11786 +
11787 +       f = vma->vm_file;
11788 +       get_file(file);
11789 +       vma->vm_file = file;
11790 +       fput(f);
11791 +}
11792 +
11793 +#ifdef CONFIG_MMU
11794 +#define AuDbgVmRegion(file, vma) do {} while (0)
11795 +
11796 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
11797 +                                   struct file *file)
11798 +{
11799 +       au_do_vm_file_reset(vma, file);
11800 +}
11801 +#else
11802 +#define AuDbgVmRegion(file, vma) \
11803 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
11804 +
11805 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
11806 +                                   struct file *file)
11807 +{
11808 +       struct file *f;
11809 +
11810 +       au_do_vm_file_reset(vma, file);
11811 +       f = vma->vm_region->vm_file;
11812 +       get_file(file);
11813 +       vma->vm_region->vm_file = file;
11814 +       fput(f);
11815 +}
11816 +#endif /* CONFIG_MMU */
11817 +
11818 +/* handle vma->vm_prfile */
11819 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
11820 +                                   struct file *file)
11821 +{
11822 +       get_file(file);
11823 +       vma->vm_prfile = file;
11824 +#ifndef CONFIG_MMU
11825 +       get_file(file);
11826 +       vma->vm_region->vm_prfile = file;
11827 +#endif
11828 +}
11829 +
11830 +#endif /* __KERNEL__ */
11831 +#endif /* __AUFS_FILE_H__ */
11832 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
11833 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
11834 +++ linux/fs/aufs/finfo.c       2014-06-24 00:14:06.782719306 +0200
11835 @@ -0,0 +1,156 @@
11836 +/*
11837 + * Copyright (C) 2005-2014 Junjiro R. Okajima
11838 + *
11839 + * This program, aufs is free software; you can redistribute it and/or modify
11840 + * it under the terms of the GNU General Public License as published by
11841 + * the Free Software Foundation; either version 2 of the License, or
11842 + * (at your option) any later version.
11843 + *
11844 + * This program is distributed in the hope that it will be useful,
11845 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11846 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11847 + * GNU General Public License for more details.
11848 + *
11849 + * You should have received a copy of the GNU General Public License
11850 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11851 + */
11852 +
11853 +/*
11854 + * file private data
11855 + */
11856 +
11857 +#include "aufs.h"
11858 +
11859 +void au_hfput(struct au_hfile *hf, struct file *file)
11860 +{
11861 +       /* todo: direct access f_flags */
11862 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
11863 +               allow_write_access(hf->hf_file);
11864 +       fput(hf->hf_file);
11865 +       hf->hf_file = NULL;
11866 +       atomic_dec(&hf->hf_br->br_count);
11867 +       hf->hf_br = NULL;
11868 +}
11869 +
11870 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
11871 +{
11872 +       struct au_finfo *finfo = au_fi(file);
11873 +       struct au_hfile *hf;
11874 +       struct au_fidir *fidir;
11875 +
11876 +       fidir = finfo->fi_hdir;
11877 +       if (!fidir) {
11878 +               AuDebugOn(finfo->fi_btop != bindex);
11879 +               hf = &finfo->fi_htop;
11880 +       } else
11881 +               hf = fidir->fd_hfile + bindex;
11882 +
11883 +       if (hf && hf->hf_file)
11884 +               au_hfput(hf, file);
11885 +       if (val) {
11886 +               FiMustWriteLock(file);
11887 +               hf->hf_file = val;
11888 +               hf->hf_br = au_sbr(file->f_dentry->d_sb, bindex);
11889 +       }
11890 +}
11891 +
11892 +void au_update_figen(struct file *file)
11893 +{
11894 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_dentry));
11895 +       /* smp_mb(); */ /* atomic_set */
11896 +}
11897 +
11898 +/* ---------------------------------------------------------------------- */
11899 +
11900 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
11901 +{
11902 +       struct au_fidir *fidir;
11903 +       int nbr;
11904 +
11905 +       nbr = au_sbend(sb) + 1;
11906 +       if (nbr < 2)
11907 +               nbr = 2; /* initial allocate for 2 branches */
11908 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
11909 +       if (fidir) {
11910 +               fidir->fd_bbot = -1;
11911 +               fidir->fd_nent = nbr;
11912 +               fidir->fd_vdir_cache = NULL;
11913 +       }
11914 +
11915 +       return fidir;
11916 +}
11917 +
11918 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
11919 +{
11920 +       int err;
11921 +       struct au_fidir *fidir, *p;
11922 +
11923 +       AuRwMustWriteLock(&finfo->fi_rwsem);
11924 +       fidir = finfo->fi_hdir;
11925 +       AuDebugOn(!fidir);
11926 +
11927 +       err = -ENOMEM;
11928 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
11929 +                        GFP_NOFS);
11930 +       if (p) {
11931 +               p->fd_nent = nbr;
11932 +               finfo->fi_hdir = p;
11933 +               err = 0;
11934 +       }
11935 +
11936 +       return err;
11937 +}
11938 +
11939 +/* ---------------------------------------------------------------------- */
11940 +
11941 +void au_finfo_fin(struct file *file)
11942 +{
11943 +       struct au_finfo *finfo;
11944 +
11945 +       au_nfiles_dec(file->f_dentry->d_sb);
11946 +
11947 +       finfo = au_fi(file);
11948 +       AuDebugOn(finfo->fi_hdir);
11949 +       AuRwDestroy(&finfo->fi_rwsem);
11950 +       au_cache_free_finfo(finfo);
11951 +}
11952 +
11953 +void au_fi_init_once(void *_finfo)
11954 +{
11955 +       struct au_finfo *finfo = _finfo;
11956 +       static struct lock_class_key aufs_fi;
11957 +
11958 +       au_rw_init(&finfo->fi_rwsem);
11959 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
11960 +}
11961 +
11962 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
11963 +{
11964 +       int err;
11965 +       struct au_finfo *finfo;
11966 +       struct dentry *dentry;
11967 +
11968 +       err = -ENOMEM;
11969 +       dentry = file->f_dentry;
11970 +       finfo = au_cache_alloc_finfo();
11971 +       if (unlikely(!finfo))
11972 +               goto out;
11973 +
11974 +       err = 0;
11975 +       au_nfiles_inc(dentry->d_sb);
11976 +       /* verbose coding for lock class name */
11977 +       if (!fidir)
11978 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
11979 +       else
11980 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
11981 +       au_rw_write_lock(&finfo->fi_rwsem);
11982 +       finfo->fi_btop = -1;
11983 +       finfo->fi_hdir = fidir;
11984 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
11985 +       /* smp_mb(); */ /* atomic_set */
11986 +
11987 +       file->private_data = finfo;
11988 +
11989 +out:
11990 +       return err;
11991 +}
11992 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
11993 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
11994 +++ linux/fs/aufs/f_op.c        2014-06-24 00:14:06.782719306 +0200
11995 @@ -0,0 +1,782 @@
11996 +/*
11997 + * Copyright (C) 2005-2014 Junjiro R. Okajima
11998 + *
11999 + * This program, aufs is free software; you can redistribute it and/or modify
12000 + * it under the terms of the GNU General Public License as published by
12001 + * the Free Software Foundation; either version 2 of the License, or
12002 + * (at your option) any later version.
12003 + *
12004 + * This program is distributed in the hope that it will be useful,
12005 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12006 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12007 + * GNU General Public License for more details.
12008 + *
12009 + * You should have received a copy of the GNU General Public License
12010 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12011 + */
12012 +
12013 +/*
12014 + * file and vm operations
12015 + */
12016 +
12017 +#include <linux/aio.h>
12018 +#include <linux/fs_stack.h>
12019 +#include <linux/mman.h>
12020 +#include <linux/security.h>
12021 +#include "aufs.h"
12022 +
12023 +int au_do_open_nondir(struct file *file, int flags)
12024 +{
12025 +       int err;
12026 +       aufs_bindex_t bindex;
12027 +       struct file *h_file;
12028 +       struct dentry *dentry;
12029 +       struct au_finfo *finfo;
12030 +       struct inode *h_inode;
12031 +
12032 +       FiMustWriteLock(file);
12033 +
12034 +       err = 0;
12035 +       dentry = file->f_dentry;
12036 +       finfo = au_fi(file);
12037 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
12038 +       atomic_set(&finfo->fi_mmapped, 0);
12039 +       bindex = au_dbstart(dentry);
12040 +       h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
12041 +       if (IS_ERR(h_file))
12042 +               err = PTR_ERR(h_file);
12043 +       else {
12044 +               if ((flags & __O_TMPFILE)
12045 +                   && !(flags & O_EXCL)) {
12046 +                       h_inode = file_inode(h_file);
12047 +                       spin_lock(&h_inode->i_lock);
12048 +                       h_inode->i_state |= I_LINKABLE;
12049 +                       spin_unlock(&h_inode->i_lock);
12050 +               }
12051 +               au_set_fbstart(file, bindex);
12052 +               au_set_h_fptr(file, bindex, h_file);
12053 +               au_update_figen(file);
12054 +               finfo->fi_file = file;
12055 +               au_sphl_add(&finfo->fi_hlist, &au_sbi(dentry->d_sb)->si_files);
12056 +               /* todo: necessary? */
12057 +               /* file->f_ra = h_file->f_ra; */
12058 +       }
12059 +
12060 +       return err;
12061 +}
12062 +
12063 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
12064 +                           struct file *file)
12065 +{
12066 +       int err;
12067 +       struct super_block *sb;
12068 +
12069 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
12070 +             file, vfsub_file_flags(file), file->f_mode);
12071 +
12072 +       sb = file->f_dentry->d_sb;
12073 +       si_read_lock(sb, AuLock_FLUSH);
12074 +       err = au_do_open(file, au_do_open_nondir, /*fidir*/NULL);
12075 +       si_read_unlock(sb);
12076 +       return err;
12077 +}
12078 +
12079 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
12080 +{
12081 +       struct au_finfo *finfo;
12082 +       aufs_bindex_t bindex;
12083 +
12084 +       finfo = au_fi(file);
12085 +       au_sphl_del(&finfo->fi_hlist, &au_sbi(file->f_dentry->d_sb)->si_files);
12086 +       bindex = finfo->fi_btop;
12087 +       if (bindex >= 0)
12088 +               au_set_h_fptr(file, bindex, NULL);
12089 +
12090 +       au_finfo_fin(file);
12091 +       return 0;
12092 +}
12093 +
12094 +/* ---------------------------------------------------------------------- */
12095 +
12096 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
12097 +{
12098 +       int err;
12099 +       struct file *h_file;
12100 +
12101 +       err = 0;
12102 +       h_file = au_hf_top(file);
12103 +       if (h_file)
12104 +               err = vfsub_flush(h_file, id);
12105 +       return err;
12106 +}
12107 +
12108 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
12109 +{
12110 +       return au_do_flush(file, id, au_do_flush_nondir);
12111 +}
12112 +
12113 +/* ---------------------------------------------------------------------- */
12114 +/*
12115 + * read and write functions acquire [fdi]_rwsem once, but release before
12116 + * mmap_sem. This is because to stop a race condition between mmap(2).
12117 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
12118 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
12119 + * read functions after [fdi]_rwsem are released, but it should be harmless.
12120 + */
12121 +
12122 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
12123 +                        loff_t *ppos)
12124 +{
12125 +       ssize_t err;
12126 +       struct dentry *dentry;
12127 +       struct file *h_file;
12128 +       struct super_block *sb;
12129 +
12130 +       dentry = file->f_dentry;
12131 +       sb = dentry->d_sb;
12132 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
12133 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
12134 +       if (unlikely(err))
12135 +               goto out;
12136 +
12137 +       h_file = au_hf_top(file);
12138 +       get_file(h_file);
12139 +       di_read_unlock(dentry, AuLock_IR);
12140 +       fi_read_unlock(file);
12141 +
12142 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
12143 +       err = vfsub_read_u(h_file, buf, count, ppos);
12144 +       /* todo: necessary? */
12145 +       /* file->f_ra = h_file->f_ra; */
12146 +       /* update without lock, I don't think it a problem */
12147 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
12148 +       fput(h_file);
12149 +
12150 +out:
12151 +       si_read_unlock(sb);
12152 +       return err;
12153 +}
12154 +
12155 +/*
12156 + * todo: very ugly
12157 + * it locks both of i_mutex and si_rwsem for read in safe.
12158 + * if the plink maintenance mode continues forever (that is the problem),
12159 + * may loop forever.
12160 + */
12161 +static void au_mtx_and_read_lock(struct inode *inode)
12162 +{
12163 +       int err;
12164 +       struct super_block *sb = inode->i_sb;
12165 +
12166 +       while (1) {
12167 +               mutex_lock(&inode->i_mutex);
12168 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
12169 +               if (!err)
12170 +                       break;
12171 +               mutex_unlock(&inode->i_mutex);
12172 +               si_read_lock(sb, AuLock_NOPLMW);
12173 +               si_read_unlock(sb);
12174 +       }
12175 +}
12176 +
12177 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
12178 +                         size_t count, loff_t *ppos)
12179 +{
12180 +       ssize_t err;
12181 +       struct au_pin pin;
12182 +       struct dentry *dentry;
12183 +       struct super_block *sb;
12184 +       struct inode *inode;
12185 +       struct file *h_file;
12186 +       char __user *buf = (char __user *)ubuf;
12187 +
12188 +       dentry = file->f_dentry;
12189 +       sb = dentry->d_sb;
12190 +       inode = dentry->d_inode;
12191 +       au_mtx_and_read_lock(inode);
12192 +
12193 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12194 +       if (unlikely(err))
12195 +               goto out;
12196 +
12197 +       err = au_ready_to_write(file, -1, &pin);
12198 +       di_downgrade_lock(dentry, AuLock_IR);
12199 +       if (unlikely(err)) {
12200 +               di_read_unlock(dentry, AuLock_IR);
12201 +               fi_write_unlock(file);
12202 +               goto out;
12203 +       }
12204 +
12205 +       h_file = au_hf_top(file);
12206 +       get_file(h_file);
12207 +       au_unpin(&pin);
12208 +       di_read_unlock(dentry, AuLock_IR);
12209 +       fi_write_unlock(file);
12210 +
12211 +       err = vfsub_write_u(h_file, buf, count, ppos);
12212 +       ii_write_lock_child(inode);
12213 +       au_cpup_attr_timesizes(inode);
12214 +       inode->i_mode = file_inode(h_file)->i_mode;
12215 +       ii_write_unlock(inode);
12216 +       fput(h_file);
12217 +
12218 +out:
12219 +       si_read_unlock(sb);
12220 +       mutex_unlock(&inode->i_mutex);
12221 +       return err;
12222 +}
12223 +
12224 +static ssize_t au_do_aio(struct file *h_file, int rw, struct kiocb *kio,
12225 +                        const struct iovec *iov, unsigned long nv, loff_t pos)
12226 +{
12227 +       ssize_t err;
12228 +       struct file *file;
12229 +       ssize_t (*func)(struct kiocb *, const struct iovec *, unsigned long,
12230 +                       loff_t);
12231 +
12232 +       err = security_file_permission(h_file, rw);
12233 +       if (unlikely(err))
12234 +               goto out;
12235 +
12236 +       err = -ENOSYS;
12237 +       func = NULL;
12238 +       if (rw == MAY_READ)
12239 +               func = h_file->f_op->aio_read;
12240 +       else if (rw == MAY_WRITE)
12241 +               func = h_file->f_op->aio_write;
12242 +       if (func) {
12243 +               file = kio->ki_filp;
12244 +               kio->ki_filp = h_file;
12245 +               lockdep_off();
12246 +               err = func(kio, iov, nv, pos);
12247 +               lockdep_on();
12248 +               kio->ki_filp = file;
12249 +       } else
12250 +               /* currently there is no such fs */
12251 +               WARN_ON_ONCE(1);
12252 +
12253 +out:
12254 +       return err;
12255 +}
12256 +
12257 +static ssize_t aufs_aio_read(struct kiocb *kio, const struct iovec *iov,
12258 +                            unsigned long nv, loff_t pos)
12259 +{
12260 +       ssize_t err;
12261 +       struct file *file, *h_file;
12262 +       struct dentry *dentry;
12263 +       struct super_block *sb;
12264 +
12265 +       file = kio->ki_filp;
12266 +       dentry = file->f_dentry;
12267 +       sb = dentry->d_sb;
12268 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
12269 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
12270 +       if (unlikely(err))
12271 +               goto out;
12272 +
12273 +       h_file = au_hf_top(file);
12274 +       get_file(h_file);
12275 +       di_read_unlock(dentry, AuLock_IR);
12276 +       fi_read_unlock(file);
12277 +
12278 +       err = au_do_aio(h_file, MAY_READ, kio, iov, nv, pos);
12279 +       /* todo: necessary? */
12280 +       /* file->f_ra = h_file->f_ra; */
12281 +       /* update without lock, I don't think it a problem */
12282 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
12283 +       fput(h_file);
12284 +
12285 +out:
12286 +       si_read_unlock(sb);
12287 +       return err;
12288 +}
12289 +
12290 +static ssize_t aufs_aio_write(struct kiocb *kio, const struct iovec *iov,
12291 +                             unsigned long nv, loff_t pos)
12292 +{
12293 +       ssize_t err;
12294 +       struct au_pin pin;
12295 +       struct dentry *dentry;
12296 +       struct inode *inode;
12297 +       struct file *file, *h_file;
12298 +       struct super_block *sb;
12299 +
12300 +       file = kio->ki_filp;
12301 +       dentry = file->f_dentry;
12302 +       sb = dentry->d_sb;
12303 +       inode = dentry->d_inode;
12304 +       au_mtx_and_read_lock(inode);
12305 +
12306 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12307 +       if (unlikely(err))
12308 +               goto out;
12309 +
12310 +       err = au_ready_to_write(file, -1, &pin);
12311 +       di_downgrade_lock(dentry, AuLock_IR);
12312 +       if (unlikely(err)) {
12313 +               di_read_unlock(dentry, AuLock_IR);
12314 +               fi_write_unlock(file);
12315 +               goto out;
12316 +       }
12317 +
12318 +       h_file = au_hf_top(file);
12319 +       get_file(h_file);
12320 +       au_unpin(&pin);
12321 +       di_read_unlock(dentry, AuLock_IR);
12322 +       fi_write_unlock(file);
12323 +
12324 +       err = au_do_aio(h_file, MAY_WRITE, kio, iov, nv, pos);
12325 +       ii_write_lock_child(inode);
12326 +       au_cpup_attr_timesizes(inode);
12327 +       inode->i_mode = file_inode(h_file)->i_mode;
12328 +       ii_write_unlock(inode);
12329 +       fput(h_file);
12330 +
12331 +out:
12332 +       si_read_unlock(sb);
12333 +       mutex_unlock(&inode->i_mutex);
12334 +       return err;
12335 +}
12336 +
12337 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
12338 +                               struct pipe_inode_info *pipe, size_t len,
12339 +                               unsigned int flags)
12340 +{
12341 +       ssize_t err;
12342 +       struct file *h_file;
12343 +       struct dentry *dentry;
12344 +       struct super_block *sb;
12345 +
12346 +       dentry = file->f_dentry;
12347 +       sb = dentry->d_sb;
12348 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
12349 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
12350 +       if (unlikely(err))
12351 +               goto out;
12352 +
12353 +       err = -EINVAL;
12354 +       h_file = au_hf_top(file);
12355 +       get_file(h_file);
12356 +       if (au_test_loopback_kthread()) {
12357 +               au_warn_loopback(h_file->f_dentry->d_sb);
12358 +               if (file->f_mapping != h_file->f_mapping) {
12359 +                       file->f_mapping = h_file->f_mapping;
12360 +                       smp_mb(); /* unnecessary? */
12361 +               }
12362 +       }
12363 +       di_read_unlock(dentry, AuLock_IR);
12364 +       fi_read_unlock(file);
12365 +
12366 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
12367 +       /* todo: necessasry? */
12368 +       /* file->f_ra = h_file->f_ra; */
12369 +       /* update without lock, I don't think it a problem */
12370 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
12371 +       fput(h_file);
12372 +
12373 +out:
12374 +       si_read_unlock(sb);
12375 +       return err;
12376 +}
12377 +
12378 +static ssize_t
12379 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
12380 +                 size_t len, unsigned int flags)
12381 +{
12382 +       ssize_t err;
12383 +       struct au_pin pin;
12384 +       struct dentry *dentry;
12385 +       struct inode *inode;
12386 +       struct file *h_file;
12387 +       struct super_block *sb;
12388 +
12389 +       dentry = file->f_dentry;
12390 +       sb = dentry->d_sb;
12391 +       inode = dentry->d_inode;
12392 +       au_mtx_and_read_lock(inode);
12393 +
12394 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12395 +       if (unlikely(err))
12396 +               goto out;
12397 +
12398 +       err = au_ready_to_write(file, -1, &pin);
12399 +       di_downgrade_lock(dentry, AuLock_IR);
12400 +       if (unlikely(err)) {
12401 +               di_read_unlock(dentry, AuLock_IR);
12402 +               fi_write_unlock(file);
12403 +               goto out;
12404 +       }
12405 +
12406 +       h_file = au_hf_top(file);
12407 +       get_file(h_file);
12408 +       au_unpin(&pin);
12409 +       di_read_unlock(dentry, AuLock_IR);
12410 +       fi_write_unlock(file);
12411 +
12412 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
12413 +       ii_write_lock_child(inode);
12414 +       au_cpup_attr_timesizes(inode);
12415 +       inode->i_mode = file_inode(h_file)->i_mode;
12416 +       ii_write_unlock(inode);
12417 +       fput(h_file);
12418 +
12419 +out:
12420 +       si_read_unlock(sb);
12421 +       mutex_unlock(&inode->i_mutex);
12422 +       return err;
12423 +}
12424 +
12425 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
12426 +                          loff_t len)
12427 +{
12428 +       long err;
12429 +       struct au_pin pin;
12430 +       struct dentry *dentry;
12431 +       struct super_block *sb;
12432 +       struct inode *inode;
12433 +       struct file *h_file;
12434 +
12435 +       dentry = file->f_dentry;
12436 +       sb = dentry->d_sb;
12437 +       inode = dentry->d_inode;
12438 +       au_mtx_and_read_lock(inode);
12439 +
12440 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12441 +       if (unlikely(err))
12442 +               goto out;
12443 +
12444 +       err = au_ready_to_write(file, -1, &pin);
12445 +       di_downgrade_lock(dentry, AuLock_IR);
12446 +       if (unlikely(err)) {
12447 +               di_read_unlock(dentry, AuLock_IR);
12448 +               fi_write_unlock(file);
12449 +               goto out;
12450 +       }
12451 +
12452 +       h_file = au_hf_top(file);
12453 +       get_file(h_file);
12454 +       au_unpin(&pin);
12455 +       di_read_unlock(dentry, AuLock_IR);
12456 +       fi_write_unlock(file);
12457 +
12458 +       lockdep_off();
12459 +       err = do_fallocate(h_file, mode, offset, len);
12460 +       lockdep_on();
12461 +       ii_write_lock_child(inode);
12462 +       au_cpup_attr_timesizes(inode);
12463 +       inode->i_mode = file_inode(h_file)->i_mode;
12464 +       ii_write_unlock(inode);
12465 +       fput(h_file);
12466 +
12467 +out:
12468 +       si_read_unlock(sb);
12469 +       mutex_unlock(&inode->i_mutex);
12470 +       return err;
12471 +}
12472 +
12473 +/* ---------------------------------------------------------------------- */
12474 +
12475 +/*
12476 + * The locking order around current->mmap_sem.
12477 + * - in most and regular cases
12478 + *   file I/O syscall -- aufs_read() or something
12479 + *     -- si_rwsem for read -- mmap_sem
12480 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
12481 + * - in mmap case
12482 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
12483 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
12484 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
12485 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
12486 + * It means that when aufs acquires si_rwsem for write, the process should never
12487 + * acquire mmap_sem.
12488 + *
12489 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
12490 + * problem either since any directory is not able to be mmap-ed.
12491 + * The similar scenario is applied to aufs_readlink() too.
12492 + */
12493 +
12494 +#if 0 /* stop calling security_file_mmap() */
12495 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
12496 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
12497 +
12498 +static unsigned long au_arch_prot_conv(unsigned long flags)
12499 +{
12500 +       /* currently ppc64 only */
12501 +#ifdef CONFIG_PPC64
12502 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
12503 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
12504 +       return AuConv_VM_PROT(flags, SAO);
12505 +#else
12506 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
12507 +       return 0;
12508 +#endif
12509 +}
12510 +
12511 +static unsigned long au_prot_conv(unsigned long flags)
12512 +{
12513 +       return AuConv_VM_PROT(flags, READ)
12514 +               | AuConv_VM_PROT(flags, WRITE)
12515 +               | AuConv_VM_PROT(flags, EXEC)
12516 +               | au_arch_prot_conv(flags);
12517 +}
12518 +
12519 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
12520 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
12521 +
12522 +static unsigned long au_flag_conv(unsigned long flags)
12523 +{
12524 +       return AuConv_VM_MAP(flags, GROWSDOWN)
12525 +               | AuConv_VM_MAP(flags, DENYWRITE)
12526 +               | AuConv_VM_MAP(flags, LOCKED);
12527 +}
12528 +#endif
12529 +
12530 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
12531 +{
12532 +       int err;
12533 +       aufs_bindex_t bstart;
12534 +       const unsigned char wlock
12535 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
12536 +       struct dentry *dentry;
12537 +       struct super_block *sb;
12538 +       struct file *h_file;
12539 +       struct au_branch *br;
12540 +       struct au_pin pin;
12541 +
12542 +       AuDbgVmRegion(file, vma);
12543 +
12544 +       dentry = file->f_dentry;
12545 +       sb = dentry->d_sb;
12546 +       lockdep_off();
12547 +       si_read_lock(sb, AuLock_NOPLMW);
12548 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12549 +       if (unlikely(err))
12550 +               goto out;
12551 +
12552 +       if (wlock) {
12553 +               err = au_ready_to_write(file, -1, &pin);
12554 +               di_write_unlock(dentry);
12555 +               if (unlikely(err)) {
12556 +                       fi_write_unlock(file);
12557 +                       goto out;
12558 +               }
12559 +               au_unpin(&pin);
12560 +       } else
12561 +               di_write_unlock(dentry);
12562 +
12563 +       bstart = au_fbstart(file);
12564 +       br = au_sbr(sb, bstart);
12565 +       h_file = au_hf_top(file);
12566 +       get_file(h_file);
12567 +       au_set_mmapped(file);
12568 +       fi_write_unlock(file);
12569 +       lockdep_on();
12570 +
12571 +       au_vm_file_reset(vma, h_file);
12572 +       /*
12573 +        * we cannot call security_mmap_file() here since it may acquire
12574 +        * mmap_sem or i_mutex.
12575 +        *
12576 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
12577 +        *                       au_flag_conv(vma->vm_flags));
12578 +        */
12579 +       if (!err)
12580 +               err = h_file->f_op->mmap(h_file, vma);
12581 +       if (unlikely(err))
12582 +               goto out_reset;
12583 +
12584 +       au_vm_prfile_set(vma, file);
12585 +       /* update without lock, I don't think it a problem */
12586 +       fsstack_copy_attr_atime(file_inode(file), file_inode(h_file));
12587 +       goto out_fput; /* success */
12588 +
12589 +out_reset:
12590 +       au_unset_mmapped(file);
12591 +       au_vm_file_reset(vma, file);
12592 +out_fput:
12593 +       fput(h_file);
12594 +       lockdep_off();
12595 +out:
12596 +       si_read_unlock(sb);
12597 +       lockdep_on();
12598 +       AuTraceErr(err);
12599 +       return err;
12600 +}
12601 +
12602 +/* ---------------------------------------------------------------------- */
12603 +
12604 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
12605 +                            int datasync)
12606 +{
12607 +       int err;
12608 +       struct au_pin pin;
12609 +       struct dentry *dentry;
12610 +       struct inode *inode;
12611 +       struct file *h_file;
12612 +       struct super_block *sb;
12613 +
12614 +       dentry = file->f_dentry;
12615 +       inode = dentry->d_inode;
12616 +       sb = dentry->d_sb;
12617 +       mutex_lock(&inode->i_mutex);
12618 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
12619 +       if (unlikely(err))
12620 +               goto out;
12621 +
12622 +       err = 0; /* -EBADF; */ /* posix? */
12623 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
12624 +               goto out_si;
12625 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12626 +       if (unlikely(err))
12627 +               goto out_si;
12628 +
12629 +       err = au_ready_to_write(file, -1, &pin);
12630 +       di_downgrade_lock(dentry, AuLock_IR);
12631 +       if (unlikely(err))
12632 +               goto out_unlock;
12633 +       au_unpin(&pin);
12634 +
12635 +       err = -EINVAL;
12636 +       h_file = au_hf_top(file);
12637 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
12638 +       au_cpup_attr_timesizes(inode);
12639 +
12640 +out_unlock:
12641 +       di_read_unlock(dentry, AuLock_IR);
12642 +       fi_write_unlock(file);
12643 +out_si:
12644 +       si_read_unlock(sb);
12645 +out:
12646 +       mutex_unlock(&inode->i_mutex);
12647 +       return err;
12648 +}
12649 +
12650 +/* no one supports this operation, currently */
12651 +#if 0
12652 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
12653 +{
12654 +       int err;
12655 +       struct au_pin pin;
12656 +       struct dentry *dentry;
12657 +       struct inode *inode;
12658 +       struct file *file, *h_file;
12659 +
12660 +       file = kio->ki_filp;
12661 +       dentry = file->f_dentry;
12662 +       inode = dentry->d_inode;
12663 +       au_mtx_and_read_lock(inode);
12664 +
12665 +       err = 0; /* -EBADF; */ /* posix? */
12666 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
12667 +               goto out;
12668 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
12669 +       if (unlikely(err))
12670 +               goto out;
12671 +
12672 +       err = au_ready_to_write(file, -1, &pin);
12673 +       di_downgrade_lock(dentry, AuLock_IR);
12674 +       if (unlikely(err))
12675 +               goto out_unlock;
12676 +       au_unpin(&pin);
12677 +
12678 +       err = -ENOSYS;
12679 +       h_file = au_hf_top(file);
12680 +       if (h_file->f_op->aio_fsync) {
12681 +               struct mutex *h_mtx;
12682 +
12683 +               h_mtx = &file_inode(h_file)->i_mutex;
12684 +               if (!is_sync_kiocb(kio)) {
12685 +                       get_file(h_file);
12686 +                       fput(file);
12687 +               }
12688 +               kio->ki_filp = h_file;
12689 +               err = h_file->f_op->aio_fsync(kio, datasync);
12690 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
12691 +               if (!err)
12692 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
12693 +               /*ignore*/
12694 +               au_cpup_attr_timesizes(inode);
12695 +               mutex_unlock(h_mtx);
12696 +       }
12697 +
12698 +out_unlock:
12699 +       di_read_unlock(dentry, AuLock_IR);
12700 +       fi_write_unlock(file);
12701 +out:
12702 +       si_read_unlock(inode->sb);
12703 +       mutex_unlock(&inode->i_mutex);
12704 +       return err;
12705 +}
12706 +#endif
12707 +
12708 +static int aufs_fasync(int fd, struct file *file, int flag)
12709 +{
12710 +       int err;
12711 +       struct file *h_file;
12712 +       struct dentry *dentry;
12713 +       struct super_block *sb;
12714 +
12715 +       dentry = file->f_dentry;
12716 +       sb = dentry->d_sb;
12717 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
12718 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
12719 +       if (unlikely(err))
12720 +               goto out;
12721 +
12722 +       h_file = au_hf_top(file);
12723 +       if (h_file->f_op->fasync)
12724 +               err = h_file->f_op->fasync(fd, h_file, flag);
12725 +
12726 +       di_read_unlock(dentry, AuLock_IR);
12727 +       fi_read_unlock(file);
12728 +
12729 +out:
12730 +       si_read_unlock(sb);
12731 +       return err;
12732 +}
12733 +
12734 +/* ---------------------------------------------------------------------- */
12735 +
12736 +/* no one supports this operation, currently */
12737 +#if 0
12738 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
12739 +                            size_t len, loff_t *pos , int more)
12740 +{
12741 +}
12742 +#endif
12743 +
12744 +/* ---------------------------------------------------------------------- */
12745 +
12746 +const struct file_operations aufs_file_fop = {
12747 +       .owner          = THIS_MODULE,
12748 +
12749 +       .llseek         = default_llseek,
12750 +
12751 +       .read           = aufs_read,
12752 +       .write          = aufs_write,
12753 +       .aio_read       = aufs_aio_read,
12754 +       .aio_write      = aufs_aio_write,
12755 +#ifdef CONFIG_AUFS_POLL
12756 +       .poll           = aufs_poll,
12757 +#endif
12758 +       .unlocked_ioctl = aufs_ioctl_nondir,
12759 +#ifdef CONFIG_COMPAT
12760 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
12761 +#endif
12762 +       .mmap           = aufs_mmap,
12763 +       .open           = aufs_open_nondir,
12764 +       .flush          = aufs_flush_nondir,
12765 +       .release        = aufs_release_nondir,
12766 +       .fsync          = aufs_fsync_nondir,
12767 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
12768 +       .fasync         = aufs_fasync,
12769 +       /* .sendpage    = aufs_sendpage, */
12770 +       .splice_write   = aufs_splice_write,
12771 +       .splice_read    = aufs_splice_read,
12772 +#if 0
12773 +       .aio_splice_write = aufs_aio_splice_write,
12774 +       .aio_splice_read  = aufs_aio_splice_read,
12775 +#endif
12776 +       .fallocate      = aufs_fallocate
12777 +};
12778 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
12779 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
12780 +++ linux/fs/aufs/fstype.h      2014-06-24 00:14:06.782719306 +0200
12781 @@ -0,0 +1,469 @@
12782 +/*
12783 + * Copyright (C) 2005-2014 Junjiro R. Okajima
12784 + *
12785 + * This program, aufs is free software; you can redistribute it and/or modify
12786 + * it under the terms of the GNU General Public License as published by
12787 + * the Free Software Foundation; either version 2 of the License, or
12788 + * (at your option) any later version.
12789 + *
12790 + * This program is distributed in the hope that it will be useful,
12791 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12792 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12793 + * GNU General Public License for more details.
12794 + *
12795 + * You should have received a copy of the GNU General Public License
12796 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12797 + */
12798 +
12799 +/*
12800 + * judging filesystem type
12801 + */
12802 +
12803 +#ifndef __AUFS_FSTYPE_H__
12804 +#define __AUFS_FSTYPE_H__
12805 +
12806 +#ifdef __KERNEL__
12807 +
12808 +#include <linux/fs.h>
12809 +#include <linux/magic.h>
12810 +#include <linux/romfs_fs.h>
12811 +
12812 +static inline int au_test_aufs(struct super_block *sb)
12813 +{
12814 +       return sb->s_magic == AUFS_SUPER_MAGIC;
12815 +}
12816 +
12817 +static inline const char *au_sbtype(struct super_block *sb)
12818 +{
12819 +       return sb->s_type->name;
12820 +}
12821 +
12822 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
12823 +{
12824 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
12825 +       return sb->s_magic == ROMFS_MAGIC;
12826 +#else
12827 +       return 0;
12828 +#endif
12829 +}
12830 +
12831 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
12832 +{
12833 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
12834 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
12835 +#else
12836 +       return 0;
12837 +#endif
12838 +}
12839 +
12840 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
12841 +{
12842 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
12843 +       return sb->s_magic == CRAMFS_MAGIC;
12844 +#endif
12845 +       return 0;
12846 +}
12847 +
12848 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
12849 +{
12850 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
12851 +       return sb->s_magic == NFS_SUPER_MAGIC;
12852 +#else
12853 +       return 0;
12854 +#endif
12855 +}
12856 +
12857 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
12858 +{
12859 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
12860 +       return sb->s_magic == FUSE_SUPER_MAGIC;
12861 +#else
12862 +       return 0;
12863 +#endif
12864 +}
12865 +
12866 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
12867 +{
12868 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
12869 +       return sb->s_magic == XFS_SB_MAGIC;
12870 +#else
12871 +       return 0;
12872 +#endif
12873 +}
12874 +
12875 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
12876 +{
12877 +#ifdef CONFIG_TMPFS
12878 +       return sb->s_magic == TMPFS_MAGIC;
12879 +#else
12880 +       return 0;
12881 +#endif
12882 +}
12883 +
12884 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
12885 +{
12886 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
12887 +       return !strcmp(au_sbtype(sb), "ecryptfs");
12888 +#else
12889 +       return 0;
12890 +#endif
12891 +}
12892 +
12893 +static inline int au_test_ocfs2(struct super_block *sb __maybe_unused)
12894 +{
12895 +#if defined(CONFIG_OCFS2_FS) || defined(CONFIG_OCFS2_FS_MODULE)
12896 +       return sb->s_magic == OCFS2_SUPER_MAGIC;
12897 +#else
12898 +       return 0;
12899 +#endif
12900 +}
12901 +
12902 +static inline int au_test_ocfs2_dlmfs(struct super_block *sb __maybe_unused)
12903 +{
12904 +#if defined(CONFIG_OCFS2_FS_O2CB) || defined(CONFIG_OCFS2_FS_O2CB_MODULE)
12905 +       return sb->s_magic == DLMFS_MAGIC;
12906 +#else
12907 +       return 0;
12908 +#endif
12909 +}
12910 +
12911 +static inline int au_test_coda(struct super_block *sb __maybe_unused)
12912 +{
12913 +#if defined(CONFIG_CODA_FS) || defined(CONFIG_CODA_FS_MODULE)
12914 +       return sb->s_magic == CODA_SUPER_MAGIC;
12915 +#else
12916 +       return 0;
12917 +#endif
12918 +}
12919 +
12920 +static inline int au_test_v9fs(struct super_block *sb __maybe_unused)
12921 +{
12922 +#if defined(CONFIG_9P_FS) || defined(CONFIG_9P_FS_MODULE)
12923 +       return sb->s_magic == V9FS_MAGIC;
12924 +#else
12925 +       return 0;
12926 +#endif
12927 +}
12928 +
12929 +static inline int au_test_ext4(struct super_block *sb __maybe_unused)
12930 +{
12931 +#if defined(CONFIG_EXT4_FS) || defined(CONFIG_EXT4_FS_MODULE)
12932 +       return sb->s_magic == EXT4_SUPER_MAGIC;
12933 +#else
12934 +       return 0;
12935 +#endif
12936 +}
12937 +
12938 +static inline int au_test_sysv(struct super_block *sb __maybe_unused)
12939 +{
12940 +#if defined(CONFIG_SYSV_FS) || defined(CONFIG_SYSV_FS_MODULE)
12941 +       return !strcmp(au_sbtype(sb), "sysv");
12942 +#else
12943 +       return 0;
12944 +#endif
12945 +}
12946 +
12947 +static inline int au_test_ramfs(struct super_block *sb)
12948 +{
12949 +       return sb->s_magic == RAMFS_MAGIC;
12950 +}
12951 +
12952 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
12953 +{
12954 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
12955 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
12956 +#else
12957 +       return 0;
12958 +#endif
12959 +}
12960 +
12961 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
12962 +{
12963 +#ifdef CONFIG_PROC_FS
12964 +       return sb->s_magic == PROC_SUPER_MAGIC;
12965 +#else
12966 +       return 0;
12967 +#endif
12968 +}
12969 +
12970 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
12971 +{
12972 +#ifdef CONFIG_SYSFS
12973 +       return sb->s_magic == SYSFS_MAGIC;
12974 +#else
12975 +       return 0;
12976 +#endif
12977 +}
12978 +
12979 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
12980 +{
12981 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
12982 +       return sb->s_magic == CONFIGFS_MAGIC;
12983 +#else
12984 +       return 0;
12985 +#endif
12986 +}
12987 +
12988 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
12989 +{
12990 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
12991 +       return sb->s_magic == MINIX3_SUPER_MAGIC
12992 +               || sb->s_magic == MINIX2_SUPER_MAGIC
12993 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
12994 +               || sb->s_magic == MINIX_SUPER_MAGIC
12995 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
12996 +#else
12997 +       return 0;
12998 +#endif
12999 +}
13000 +
13001 +static inline int au_test_cifs(struct super_block *sb __maybe_unused)
13002 +{
13003 +#if defined(CONFIG_CIFS_FS) || defined(CONFIGCIFS_FS_MODULE)
13004 +       return sb->s_magic == CIFS_MAGIC_NUMBER;
13005 +#else
13006 +       return 0;
13007 +#endif
13008 +}
13009 +
13010 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
13011 +{
13012 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
13013 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
13014 +#else
13015 +       return 0;
13016 +#endif
13017 +}
13018 +
13019 +static inline int au_test_msdos(struct super_block *sb)
13020 +{
13021 +       return au_test_fat(sb);
13022 +}
13023 +
13024 +static inline int au_test_vfat(struct super_block *sb)
13025 +{
13026 +       return au_test_fat(sb);
13027 +}
13028 +
13029 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
13030 +{
13031 +#ifdef CONFIG_SECURITYFS
13032 +       return sb->s_magic == SECURITYFS_MAGIC;
13033 +#else
13034 +       return 0;
13035 +#endif
13036 +}
13037 +
13038 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
13039 +{
13040 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
13041 +       return sb->s_magic == SQUASHFS_MAGIC;
13042 +#else
13043 +       return 0;
13044 +#endif
13045 +}
13046 +
13047 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
13048 +{
13049 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
13050 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
13051 +#else
13052 +       return 0;
13053 +#endif
13054 +}
13055 +
13056 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
13057 +{
13058 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
13059 +       return sb->s_magic == XENFS_SUPER_MAGIC;
13060 +#else
13061 +       return 0;
13062 +#endif
13063 +}
13064 +
13065 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
13066 +{
13067 +#ifdef CONFIG_DEBUG_FS
13068 +       return sb->s_magic == DEBUGFS_MAGIC;
13069 +#else
13070 +       return 0;
13071 +#endif
13072 +}
13073 +
13074 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
13075 +{
13076 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
13077 +       return sb->s_magic == NILFS_SUPER_MAGIC;
13078 +#else
13079 +       return 0;
13080 +#endif
13081 +}
13082 +
13083 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
13084 +{
13085 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
13086 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
13087 +#else
13088 +       return 0;
13089 +#endif
13090 +}
13091 +
13092 +/* ---------------------------------------------------------------------- */
13093 +/*
13094 + * they can't be an aufs branch.
13095 + */
13096 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
13097 +{
13098 +       return
13099 +#ifndef CONFIG_AUFS_BR_RAMFS
13100 +               au_test_ramfs(sb) ||
13101 +#endif
13102 +               au_test_procfs(sb)
13103 +               || au_test_sysfs(sb)
13104 +               || au_test_configfs(sb)
13105 +               || au_test_debugfs(sb)
13106 +               || au_test_securityfs(sb)
13107 +               || au_test_xenfs(sb)
13108 +               || au_test_ecryptfs(sb)
13109 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
13110 +               || au_test_aufs(sb); /* will be supported in next version */
13111 +}
13112 +
13113 +static inline int au_test_fs_remote(struct super_block *sb)
13114 +{
13115 +       return !au_test_tmpfs(sb)
13116 +#ifdef CONFIG_AUFS_BR_RAMFS
13117 +               && !au_test_ramfs(sb)
13118 +#endif
13119 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
13120 +}
13121 +
13122 +/* ---------------------------------------------------------------------- */
13123 +
13124 +/*
13125 + * Note: these functions (below) are created after reading ->getattr() in all
13126 + * filesystems under linux/fs. it means we have to do so in every update...
13127 + */
13128 +
13129 +/*
13130 + * some filesystems require getattr to refresh the inode attributes before
13131 + * referencing.
13132 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
13133 + * and leave the work for d_revalidate()
13134 + */
13135 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
13136 +{
13137 +       return au_test_nfs(sb)
13138 +               || au_test_fuse(sb)
13139 +               /* || au_test_ocfs2(sb) */      /* untested */
13140 +               /* || au_test_btrfs(sb) */      /* untested */
13141 +               /* || au_test_coda(sb) */       /* untested */
13142 +               /* || au_test_v9fs(sb) */       /* untested */
13143 +               ;
13144 +}
13145 +
13146 +/*
13147 + * filesystems which don't maintain i_size or i_blocks.
13148 + */
13149 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
13150 +{
13151 +       return au_test_xfs(sb)
13152 +               || au_test_btrfs(sb)
13153 +               || au_test_ubifs(sb)
13154 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
13155 +               /* || au_test_ext4(sb) */       /* untested */
13156 +               /* || au_test_ocfs2(sb) */      /* untested */
13157 +               /* || au_test_ocfs2_dlmfs(sb) */ /* untested */
13158 +               /* || au_test_sysv(sb) */       /* untested */
13159 +               /* || au_test_minix(sb) */      /* untested */
13160 +               ;
13161 +}
13162 +
13163 +/*
13164 + * filesystems which don't store the correct value in some of their inode
13165 + * attributes.
13166 + */
13167 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
13168 +{
13169 +       return au_test_fs_bad_iattr_size(sb)
13170 +               /* || au_test_cifs(sb) */       /* untested */
13171 +               || au_test_fat(sb)
13172 +               || au_test_msdos(sb)
13173 +               || au_test_vfat(sb);
13174 +}
13175 +
13176 +/* they don't check i_nlink in link(2) */
13177 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
13178 +{
13179 +       return au_test_tmpfs(sb)
13180 +#ifdef CONFIG_AUFS_BR_RAMFS
13181 +               || au_test_ramfs(sb)
13182 +#endif
13183 +               || au_test_ubifs(sb)
13184 +               || au_test_hfsplus(sb);
13185 +}
13186 +
13187 +/*
13188 + * filesystems which sets S_NOATIME and S_NOCMTIME.
13189 + */
13190 +static inline int au_test_fs_notime(struct super_block *sb)
13191 +{
13192 +       return au_test_nfs(sb)
13193 +               || au_test_fuse(sb)
13194 +               || au_test_ubifs(sb)
13195 +               /* || au_test_cifs(sb) */       /* untested */
13196 +               ;
13197 +}
13198 +
13199 +/*
13200 + * filesystems which requires replacing i_mapping.
13201 + */
13202 +static inline int au_test_fs_bad_mapping(struct super_block *sb)
13203 +{
13204 +       return au_test_fuse(sb)
13205 +               || au_test_ubifs(sb);
13206 +}
13207 +
13208 +/* temporary support for i#1 in cramfs */
13209 +static inline int au_test_fs_unique_ino(struct inode *inode)
13210 +{
13211 +       if (au_test_cramfs(inode->i_sb))
13212 +               return inode->i_ino != 1;
13213 +       return 1;
13214 +}
13215 +
13216 +/* ---------------------------------------------------------------------- */
13217 +
13218 +/*
13219 + * the filesystem where the xino files placed must support i/o after unlink and
13220 + * maintain i_size and i_blocks.
13221 + */
13222 +static inline int au_test_fs_bad_xino(struct super_block *sb)
13223 +{
13224 +       return au_test_fs_remote(sb)
13225 +               || au_test_fs_bad_iattr_size(sb)
13226 +               /* don't want unnecessary work for xino */
13227 +               || au_test_aufs(sb)
13228 +               || au_test_ecryptfs(sb)
13229 +               || au_test_nilfs(sb);
13230 +}
13231 +
13232 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
13233 +{
13234 +       return au_test_tmpfs(sb)
13235 +               || au_test_ramfs(sb);
13236 +}
13237 +
13238 +/*
13239 + * test if the @sb is real-readonly.
13240 + */
13241 +static inline int au_test_fs_rr(struct super_block *sb)
13242 +{
13243 +       return au_test_squashfs(sb)
13244 +               || au_test_iso9660(sb)
13245 +               || au_test_cramfs(sb)
13246 +               || au_test_romfs(sb);
13247 +}
13248 +
13249 +#endif /* __KERNEL__ */
13250 +#endif /* __AUFS_FSTYPE_H__ */
13251 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
13252 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
13253 +++ linux/fs/aufs/hfsnotify.c   2014-06-24 00:14:06.782719306 +0200
13254 @@ -0,0 +1,281 @@
13255 +/*
13256 + * Copyright (C) 2005-2014 Junjiro R. Okajima
13257 + *
13258 + * This program, aufs is free software; you can redistribute it and/or modify
13259 + * it under the terms of the GNU General Public License as published by
13260 + * the Free Software Foundation; either version 2 of the License, or
13261 + * (at your option) any later version.
13262 + *
13263 + * This program is distributed in the hope that it will be useful,
13264 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13265 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13266 + * GNU General Public License for more details.
13267 + *
13268 + * You should have received a copy of the GNU General Public License
13269 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13270 + */
13271 +
13272 +/*
13273 + * fsnotify for the lower directories
13274 + */
13275 +
13276 +#include "aufs.h"
13277 +
13278 +/* FS_IN_IGNORED is unnecessary */
13279 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
13280 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
13281 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
13282 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
13283 +
13284 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
13285 +{
13286 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
13287 +                                            hn_mark);
13288 +       AuDbg("here\n");
13289 +       au_cache_free_hnotify(hn);
13290 +       smp_mb__before_atomic_dec();
13291 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
13292 +               wake_up(&au_hfsn_wq);
13293 +}
13294 +
13295 +static int au_hfsn_alloc(struct au_hinode *hinode)
13296 +{
13297 +       int err;
13298 +       struct au_hnotify *hn;
13299 +       struct super_block *sb;
13300 +       struct au_branch *br;
13301 +       struct fsnotify_mark *mark;
13302 +       aufs_bindex_t bindex;
13303 +
13304 +       hn = hinode->hi_notify;
13305 +       sb = hn->hn_aufs_inode->i_sb;
13306 +       bindex = au_br_index(sb, hinode->hi_id);
13307 +       br = au_sbr(sb, bindex);
13308 +       AuDebugOn(!br->br_hfsn);
13309 +
13310 +       mark = &hn->hn_mark;
13311 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
13312 +       mark->mask = AuHfsnMask;
13313 +       /*
13314 +        * by udba rename or rmdir, aufs assign a new inode to the known
13315 +        * h_inode, so specify 1 to allow dups.
13316 +        */
13317 +       err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
13318 +                                /*mnt*/NULL, /*allow_dups*/1);
13319 +       /* even if err */
13320 +       fsnotify_put_mark(mark);
13321 +
13322 +       return err;
13323 +}
13324 +
13325 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
13326 +{
13327 +       struct fsnotify_mark *mark;
13328 +       unsigned long long ull;
13329 +       struct fsnotify_group *group;
13330 +
13331 +       ull = atomic64_inc_return(&au_hfsn_ifree);
13332 +       BUG_ON(!ull);
13333 +
13334 +       mark = &hn->hn_mark;
13335 +       spin_lock(&mark->lock);
13336 +       group = mark->group;
13337 +       fsnotify_get_group(group);
13338 +       spin_unlock(&mark->lock);
13339 +       fsnotify_destroy_mark(mark, group);
13340 +       fsnotify_put_group(group);
13341 +
13342 +       /* free hn by myself */
13343 +       return 0;
13344 +}
13345 +
13346 +/* ---------------------------------------------------------------------- */
13347 +
13348 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
13349 +{
13350 +       struct fsnotify_mark *mark;
13351 +
13352 +       mark = &hinode->hi_notify->hn_mark;
13353 +       spin_lock(&mark->lock);
13354 +       if (do_set) {
13355 +               AuDebugOn(mark->mask & AuHfsnMask);
13356 +               mark->mask |= AuHfsnMask;
13357 +       } else {
13358 +               AuDebugOn(!(mark->mask & AuHfsnMask));
13359 +               mark->mask &= ~AuHfsnMask;
13360 +       }
13361 +       spin_unlock(&mark->lock);
13362 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
13363 +}
13364 +
13365 +/* ---------------------------------------------------------------------- */
13366 +
13367 +/* #define AuDbgHnotify */
13368 +#ifdef AuDbgHnotify
13369 +static char *au_hfsn_name(u32 mask)
13370 +{
13371 +#ifdef CONFIG_AUFS_DEBUG
13372 +#define test_ret(flag)                         \
13373 +       do {                                    \
13374 +               if (mask & flag)                \
13375 +                       return #flag;           \
13376 +       } while (0)
13377 +       test_ret(FS_ACCESS);
13378 +       test_ret(FS_MODIFY);
13379 +       test_ret(FS_ATTRIB);
13380 +       test_ret(FS_CLOSE_WRITE);
13381 +       test_ret(FS_CLOSE_NOWRITE);
13382 +       test_ret(FS_OPEN);
13383 +       test_ret(FS_MOVED_FROM);
13384 +       test_ret(FS_MOVED_TO);
13385 +       test_ret(FS_CREATE);
13386 +       test_ret(FS_DELETE);
13387 +       test_ret(FS_DELETE_SELF);
13388 +       test_ret(FS_MOVE_SELF);
13389 +       test_ret(FS_UNMOUNT);
13390 +       test_ret(FS_Q_OVERFLOW);
13391 +       test_ret(FS_IN_IGNORED);
13392 +       test_ret(FS_IN_ISDIR);
13393 +       test_ret(FS_IN_ONESHOT);
13394 +       test_ret(FS_EVENT_ON_CHILD);
13395 +       return "";
13396 +#undef test_ret
13397 +#else
13398 +       return "??";
13399 +#endif
13400 +}
13401 +#endif
13402 +
13403 +/* ---------------------------------------------------------------------- */
13404 +
13405 +static void au_hfsn_free_group(struct fsnotify_group *group)
13406 +{
13407 +       struct au_br_hfsnotify *hfsn = group->private;
13408 +
13409 +       AuDbg("here\n");
13410 +       kfree(hfsn);
13411 +}
13412 +
13413 +static int au_hfsn_handle_event(struct fsnotify_group *group,
13414 +                               struct inode *inode,
13415 +                               struct fsnotify_mark *inode_mark,
13416 +                               struct fsnotify_mark *vfsmount_mark,
13417 +                               u32 mask, void *data, int data_type,
13418 +                               const unsigned char *file_name, u32 cookie)
13419 +{
13420 +       int err;
13421 +       struct au_hnotify *hnotify;
13422 +       struct inode *h_dir, *h_inode;
13423 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
13424 +
13425 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
13426 +
13427 +       err = 0;
13428 +       /* if FS_UNMOUNT happens, there must be another bug */
13429 +       AuDebugOn(mask & FS_UNMOUNT);
13430 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
13431 +               goto out;
13432 +
13433 +       h_dir = inode;
13434 +       h_inode = NULL;
13435 +#ifdef AuDbgHnotify
13436 +       au_debug_on();
13437 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
13438 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
13439 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
13440 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
13441 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
13442 +               /* WARN_ON(1); */
13443 +       }
13444 +       au_debug_off();
13445 +#endif
13446 +
13447 +       AuDebugOn(!inode_mark);
13448 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
13449 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
13450 +
13451 +out:
13452 +       return err;
13453 +}
13454 +
13455 +static struct fsnotify_ops au_hfsn_ops = {
13456 +       .handle_event           = au_hfsn_handle_event,
13457 +       .free_group_priv        = au_hfsn_free_group
13458 +};
13459 +
13460 +/* ---------------------------------------------------------------------- */
13461 +
13462 +static void au_hfsn_fin_br(struct au_branch *br)
13463 +{
13464 +       struct au_br_hfsnotify *hfsn;
13465 +
13466 +       hfsn = br->br_hfsn;
13467 +       if (hfsn)
13468 +               fsnotify_put_group(hfsn->hfsn_group);
13469 +}
13470 +
13471 +static int au_hfsn_init_br(struct au_branch *br, int perm)
13472 +{
13473 +       int err;
13474 +       struct fsnotify_group *group;
13475 +       struct au_br_hfsnotify *hfsn;
13476 +
13477 +       err = 0;
13478 +       br->br_hfsn = NULL;
13479 +       if (!au_br_hnotifyable(perm))
13480 +               goto out;
13481 +
13482 +       err = -ENOMEM;
13483 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
13484 +       if (unlikely(!hfsn))
13485 +               goto out;
13486 +
13487 +       err = 0;
13488 +       group = fsnotify_alloc_group(&au_hfsn_ops);
13489 +       if (IS_ERR(group)) {
13490 +               err = PTR_ERR(group);
13491 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
13492 +               goto out_hfsn;
13493 +       }
13494 +
13495 +       group->private = hfsn;
13496 +       hfsn->hfsn_group = group;
13497 +       br->br_hfsn = hfsn;
13498 +       goto out; /* success */
13499 +
13500 +out_hfsn:
13501 +       kfree(hfsn);
13502 +out:
13503 +       return err;
13504 +}
13505 +
13506 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
13507 +{
13508 +       int err;
13509 +
13510 +       err = 0;
13511 +       if (!br->br_hfsn)
13512 +               err = au_hfsn_init_br(br, perm);
13513 +
13514 +       return err;
13515 +}
13516 +
13517 +/* ---------------------------------------------------------------------- */
13518 +
13519 +static void au_hfsn_fin(void)
13520 +{
13521 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
13522 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
13523 +}
13524 +
13525 +const struct au_hnotify_op au_hnotify_op = {
13526 +       .ctl            = au_hfsn_ctl,
13527 +       .alloc          = au_hfsn_alloc,
13528 +       .free           = au_hfsn_free,
13529 +
13530 +       .fin            = au_hfsn_fin,
13531 +
13532 +       .reset_br       = au_hfsn_reset_br,
13533 +       .fin_br         = au_hfsn_fin_br,
13534 +       .init_br        = au_hfsn_init_br
13535 +};
13536 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
13537 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
13538 +++ linux/fs/aufs/hfsplus.c     2014-06-24 00:14:06.782719306 +0200
13539 @@ -0,0 +1,56 @@
13540 +/*
13541 + * Copyright (C) 2010-2014 Junjiro R. Okajima
13542 + *
13543 + * This program, aufs is free software; you can redistribute it and/or modify
13544 + * it under the terms of the GNU General Public License as published by
13545 + * the Free Software Foundation; either version 2 of the License, or
13546 + * (at your option) any later version.
13547 + *
13548 + * This program is distributed in the hope that it will be useful,
13549 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13550 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13551 + * GNU General Public License for more details.
13552 + *
13553 + * You should have received a copy of the GNU General Public License
13554 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13555 + */
13556 +
13557 +/*
13558 + * special support for filesystems which aqucires an inode mutex
13559 + * at final closing a file, eg, hfsplus.
13560 + *
13561 + * This trick is very simple and stupid, just to open the file before really
13562 + * neceeary open to tell hfsplus that this is not the final closing.
13563 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
13564 + * and au_h_open_post() after releasing it.
13565 + */
13566 +
13567 +#include "aufs.h"
13568 +
13569 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
13570 +                          int force_wr)
13571 +{
13572 +       struct file *h_file;
13573 +       struct dentry *h_dentry;
13574 +
13575 +       h_dentry = au_h_dptr(dentry, bindex);
13576 +       AuDebugOn(!h_dentry);
13577 +       AuDebugOn(!h_dentry->d_inode);
13578 +
13579 +       h_file = NULL;
13580 +       if (au_test_hfsplus(h_dentry->d_sb)
13581 +           && S_ISREG(h_dentry->d_inode->i_mode))
13582 +               h_file = au_h_open(dentry, bindex,
13583 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
13584 +                                  /*file*/NULL, force_wr);
13585 +       return h_file;
13586 +}
13587 +
13588 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
13589 +                   struct file *h_file)
13590 +{
13591 +       if (h_file) {
13592 +               fput(h_file);
13593 +               au_sbr_put(dentry->d_sb, bindex);
13594 +       }
13595 +}
13596 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
13597 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
13598 +++ linux/fs/aufs/hnotify.c     2014-06-24 00:14:06.782719306 +0200
13599 @@ -0,0 +1,713 @@
13600 +/*
13601 + * Copyright (C) 2005-2014 Junjiro R. Okajima
13602 + *
13603 + * This program, aufs is free software; you can redistribute it and/or modify
13604 + * it under the terms of the GNU General Public License as published by
13605 + * the Free Software Foundation; either version 2 of the License, or
13606 + * (at your option) any later version.
13607 + *
13608 + * This program is distributed in the hope that it will be useful,
13609 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13610 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13611 + * GNU General Public License for more details.
13612 + *
13613 + * You should have received a copy of the GNU General Public License
13614 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13615 + */
13616 +
13617 +/*
13618 + * abstraction to notify the direct changes on lower directories
13619 + */
13620 +
13621 +#include "aufs.h"
13622 +
13623 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
13624 +{
13625 +       int err;
13626 +       struct au_hnotify *hn;
13627 +
13628 +       err = -ENOMEM;
13629 +       hn = au_cache_alloc_hnotify();
13630 +       if (hn) {
13631 +               hn->hn_aufs_inode = inode;
13632 +               hinode->hi_notify = hn;
13633 +               err = au_hnotify_op.alloc(hinode);
13634 +               AuTraceErr(err);
13635 +               if (unlikely(err)) {
13636 +                       hinode->hi_notify = NULL;
13637 +                       au_cache_free_hnotify(hn);
13638 +                       /*
13639 +                        * The upper dir was removed by udba, but the same named
13640 +                        * dir left. In this case, aufs assignes a new inode
13641 +                        * number and set the monitor again.
13642 +                        * For the lower dir, the old monitnor is still left.
13643 +                        */
13644 +                       if (err == -EEXIST)
13645 +                               err = 0;
13646 +               }
13647 +       }
13648 +
13649 +       AuTraceErr(err);
13650 +       return err;
13651 +}
13652 +
13653 +void au_hn_free(struct au_hinode *hinode)
13654 +{
13655 +       struct au_hnotify *hn;
13656 +
13657 +       hn = hinode->hi_notify;
13658 +       if (hn) {
13659 +               hinode->hi_notify = NULL;
13660 +               if (au_hnotify_op.free(hinode, hn))
13661 +                       au_cache_free_hnotify(hn);
13662 +       }
13663 +}
13664 +
13665 +/* ---------------------------------------------------------------------- */
13666 +
13667 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
13668 +{
13669 +       if (hinode->hi_notify)
13670 +               au_hnotify_op.ctl(hinode, do_set);
13671 +}
13672 +
13673 +void au_hn_reset(struct inode *inode, unsigned int flags)
13674 +{
13675 +       aufs_bindex_t bindex, bend;
13676 +       struct inode *hi;
13677 +       struct dentry *iwhdentry;
13678 +
13679 +       bend = au_ibend(inode);
13680 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
13681 +               hi = au_h_iptr(inode, bindex);
13682 +               if (!hi)
13683 +                       continue;
13684 +
13685 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
13686 +               iwhdentry = au_hi_wh(inode, bindex);
13687 +               if (iwhdentry)
13688 +                       dget(iwhdentry);
13689 +               au_igrab(hi);
13690 +               au_set_h_iptr(inode, bindex, NULL, 0);
13691 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
13692 +                             flags & ~AuHi_XINO);
13693 +               iput(hi);
13694 +               dput(iwhdentry);
13695 +               /* mutex_unlock(&hi->i_mutex); */
13696 +       }
13697 +}
13698 +
13699 +/* ---------------------------------------------------------------------- */
13700 +
13701 +static int hn_xino(struct inode *inode, struct inode *h_inode)
13702 +{
13703 +       int err;
13704 +       aufs_bindex_t bindex, bend, bfound, bstart;
13705 +       struct inode *h_i;
13706 +
13707 +       err = 0;
13708 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13709 +               pr_warn("branch root dir was changed\n");
13710 +               goto out;
13711 +       }
13712 +
13713 +       bfound = -1;
13714 +       bend = au_ibend(inode);
13715 +       bstart = au_ibstart(inode);
13716 +#if 0 /* reserved for future use */
13717 +       if (bindex == bend) {
13718 +               /* keep this ino in rename case */
13719 +               goto out;
13720 +       }
13721 +#endif
13722 +       for (bindex = bstart; bindex <= bend; bindex++)
13723 +               if (au_h_iptr(inode, bindex) == h_inode) {
13724 +                       bfound = bindex;
13725 +                       break;
13726 +               }
13727 +       if (bfound < 0)
13728 +               goto out;
13729 +
13730 +       for (bindex = bstart; bindex <= bend; bindex++) {
13731 +               h_i = au_h_iptr(inode, bindex);
13732 +               if (!h_i)
13733 +                       continue;
13734 +
13735 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
13736 +               /* ignore this error */
13737 +               /* bad action? */
13738 +       }
13739 +
13740 +       /* children inode number will be broken */
13741 +
13742 +out:
13743 +       AuTraceErr(err);
13744 +       return err;
13745 +}
13746 +
13747 +static int hn_gen_tree(struct dentry *dentry)
13748 +{
13749 +       int err, i, j, ndentry;
13750 +       struct au_dcsub_pages dpages;
13751 +       struct au_dpage *dpage;
13752 +       struct dentry **dentries;
13753 +
13754 +       err = au_dpages_init(&dpages, GFP_NOFS);
13755 +       if (unlikely(err))
13756 +               goto out;
13757 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
13758 +       if (unlikely(err))
13759 +               goto out_dpages;
13760 +
13761 +       for (i = 0; i < dpages.ndpage; i++) {
13762 +               dpage = dpages.dpages + i;
13763 +               dentries = dpage->dentries;
13764 +               ndentry = dpage->ndentry;
13765 +               for (j = 0; j < ndentry; j++) {
13766 +                       struct dentry *d;
13767 +
13768 +                       d = dentries[j];
13769 +                       if (IS_ROOT(d))
13770 +                               continue;
13771 +
13772 +                       au_digen_dec(d);
13773 +                       if (d->d_inode)
13774 +                               /* todo: reset children xino?
13775 +                                  cached children only? */
13776 +                               au_iigen_dec(d->d_inode);
13777 +               }
13778 +       }
13779 +
13780 +out_dpages:
13781 +       au_dpages_free(&dpages);
13782 +
13783 +#if 0
13784 +       /* discard children */
13785 +       dentry_unhash(dentry);
13786 +       dput(dentry);
13787 +#endif
13788 +out:
13789 +       return err;
13790 +}
13791 +
13792 +/*
13793 + * return 0 if processed.
13794 + */
13795 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
13796 +                          const unsigned int isdir)
13797 +{
13798 +       int err;
13799 +       struct dentry *d;
13800 +       struct qstr *dname;
13801 +
13802 +       err = 1;
13803 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
13804 +               pr_warn("branch root dir was changed\n");
13805 +               err = 0;
13806 +               goto out;
13807 +       }
13808 +
13809 +       if (!isdir) {
13810 +               AuDebugOn(!name);
13811 +               au_iigen_dec(inode);
13812 +               spin_lock(&inode->i_lock);
13813 +               hlist_for_each_entry(d, &inode->i_dentry, d_alias) {
13814 +                       spin_lock(&d->d_lock);
13815 +                       dname = &d->d_name;
13816 +                       if (dname->len != nlen
13817 +                           && memcmp(dname->name, name, nlen)) {
13818 +                               spin_unlock(&d->d_lock);
13819 +                               continue;
13820 +                       }
13821 +                       err = 0;
13822 +                       au_digen_dec(d);
13823 +                       spin_unlock(&d->d_lock);
13824 +                       break;
13825 +               }
13826 +               spin_unlock(&inode->i_lock);
13827 +       } else {
13828 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
13829 +               d = d_find_alias(inode);
13830 +               if (!d) {
13831 +                       au_iigen_dec(inode);
13832 +                       goto out;
13833 +               }
13834 +
13835 +               spin_lock(&d->d_lock);
13836 +               dname = &d->d_name;
13837 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
13838 +                       spin_unlock(&d->d_lock);
13839 +                       err = hn_gen_tree(d);
13840 +                       spin_lock(&d->d_lock);
13841 +               }
13842 +               spin_unlock(&d->d_lock);
13843 +               dput(d);
13844 +       }
13845 +
13846 +out:
13847 +       AuTraceErr(err);
13848 +       return err;
13849 +}
13850 +
13851 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
13852 +{
13853 +       int err;
13854 +       struct inode *inode;
13855 +
13856 +       inode = dentry->d_inode;
13857 +       if (IS_ROOT(dentry)
13858 +           /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
13859 +               ) {
13860 +               pr_warn("branch root dir was changed\n");
13861 +               return 0;
13862 +       }
13863 +
13864 +       err = 0;
13865 +       if (!isdir) {
13866 +               au_digen_dec(dentry);
13867 +               if (inode)
13868 +                       au_iigen_dec(inode);
13869 +       } else {
13870 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
13871 +               if (inode)
13872 +                       err = hn_gen_tree(dentry);
13873 +       }
13874 +
13875 +       AuTraceErr(err);
13876 +       return err;
13877 +}
13878 +
13879 +/* ---------------------------------------------------------------------- */
13880 +
13881 +/* hnotify job flags */
13882 +#define AuHnJob_XINO0          1
13883 +#define AuHnJob_GEN            (1 << 1)
13884 +#define AuHnJob_DIRENT         (1 << 2)
13885 +#define AuHnJob_ISDIR          (1 << 3)
13886 +#define AuHnJob_TRYXINO0       (1 << 4)
13887 +#define AuHnJob_MNTPNT         (1 << 5)
13888 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
13889 +#define au_fset_hnjob(flags, name) \
13890 +       do { (flags) |= AuHnJob_##name; } while (0)
13891 +#define au_fclr_hnjob(flags, name) \
13892 +       do { (flags) &= ~AuHnJob_##name; } while (0)
13893 +
13894 +enum {
13895 +       AuHn_CHILD,
13896 +       AuHn_PARENT,
13897 +       AuHnLast
13898 +};
13899 +
13900 +struct au_hnotify_args {
13901 +       struct inode *h_dir, *dir, *h_child_inode;
13902 +       u32 mask;
13903 +       unsigned int flags[AuHnLast];
13904 +       unsigned int h_child_nlen;
13905 +       char h_child_name[];
13906 +};
13907 +
13908 +struct hn_job_args {
13909 +       unsigned int flags;
13910 +       struct inode *inode, *h_inode, *dir, *h_dir;
13911 +       struct dentry *dentry;
13912 +       char *h_name;
13913 +       int h_nlen;
13914 +};
13915 +
13916 +static int hn_job(struct hn_job_args *a)
13917 +{
13918 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
13919 +
13920 +       /* reset xino */
13921 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
13922 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
13923 +
13924 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
13925 +           && a->inode
13926 +           && a->h_inode) {
13927 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
13928 +               if (!a->h_inode->i_nlink
13929 +                   && !(a->h_inode->i_state & I_LINKABLE))
13930 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
13931 +               mutex_unlock(&a->h_inode->i_mutex);
13932 +       }
13933 +
13934 +       /* make the generation obsolete */
13935 +       if (au_ftest_hnjob(a->flags, GEN)) {
13936 +               int err = -1;
13937 +               if (a->inode)
13938 +                       err = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
13939 +                                             isdir);
13940 +               if (err && a->dentry)
13941 +                       hn_gen_by_name(a->dentry, isdir);
13942 +               /* ignore this error */
13943 +       }
13944 +
13945 +       /* make dir entries obsolete */
13946 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
13947 +               struct au_vdir *vdir;
13948 +
13949 +               vdir = au_ivdir(a->inode);
13950 +               if (vdir)
13951 +                       vdir->vd_jiffy = 0;
13952 +               /* IMustLock(a->inode); */
13953 +               /* a->inode->i_version++; */
13954 +       }
13955 +
13956 +       /* can do nothing but warn */
13957 +       if (au_ftest_hnjob(a->flags, MNTPNT)
13958 +           && a->dentry
13959 +           && d_mountpoint(a->dentry))
13960 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
13961 +
13962 +       return 0;
13963 +}
13964 +
13965 +/* ---------------------------------------------------------------------- */
13966 +
13967 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
13968 +                                          struct inode *dir)
13969 +{
13970 +       struct dentry *dentry, *d, *parent;
13971 +       struct qstr *dname;
13972 +
13973 +       parent = d_find_alias(dir);
13974 +       if (!parent)
13975 +               return NULL;
13976 +
13977 +       dentry = NULL;
13978 +       spin_lock(&parent->d_lock);
13979 +       list_for_each_entry(d, &parent->d_subdirs, d_u.d_child) {
13980 +               /* AuDbg("%pd\n", d); */
13981 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
13982 +               dname = &d->d_name;
13983 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
13984 +                       goto cont_unlock;
13985 +               if (au_di(d))
13986 +                       au_digen_dec(d);
13987 +               else
13988 +                       goto cont_unlock;
13989 +               if (d_count(d)) {
13990 +                       dentry = dget_dlock(d);
13991 +                       spin_unlock(&d->d_lock);
13992 +                       break;
13993 +               }
13994 +
13995 +cont_unlock:
13996 +               spin_unlock(&d->d_lock);
13997 +       }
13998 +       spin_unlock(&parent->d_lock);
13999 +       dput(parent);
14000 +
14001 +       if (dentry)
14002 +               di_write_lock_child(dentry);
14003 +
14004 +       return dentry;
14005 +}
14006 +
14007 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
14008 +                                        aufs_bindex_t bindex, ino_t h_ino)
14009 +{
14010 +       struct inode *inode;
14011 +       ino_t ino;
14012 +       int err;
14013 +
14014 +       inode = NULL;
14015 +       err = au_xino_read(sb, bindex, h_ino, &ino);
14016 +       if (!err && ino)
14017 +               inode = ilookup(sb, ino);
14018 +       if (!inode)
14019 +               goto out;
14020 +
14021 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14022 +               pr_warn("wrong root branch\n");
14023 +               iput(inode);
14024 +               inode = NULL;
14025 +               goto out;
14026 +       }
14027 +
14028 +       ii_write_lock_child(inode);
14029 +
14030 +out:
14031 +       return inode;
14032 +}
14033 +
14034 +static void au_hn_bh(void *_args)
14035 +{
14036 +       struct au_hnotify_args *a = _args;
14037 +       struct super_block *sb;
14038 +       aufs_bindex_t bindex, bend, bfound;
14039 +       unsigned char xino, try_iput;
14040 +       int err;
14041 +       struct inode *inode;
14042 +       ino_t h_ino;
14043 +       struct hn_job_args args;
14044 +       struct dentry *dentry;
14045 +       struct au_sbinfo *sbinfo;
14046 +
14047 +       AuDebugOn(!_args);
14048 +       AuDebugOn(!a->h_dir);
14049 +       AuDebugOn(!a->dir);
14050 +       AuDebugOn(!a->mask);
14051 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
14052 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
14053 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
14054 +
14055 +       inode = NULL;
14056 +       dentry = NULL;
14057 +       /*
14058 +        * do not lock a->dir->i_mutex here
14059 +        * because of d_revalidate() may cause a deadlock.
14060 +        */
14061 +       sb = a->dir->i_sb;
14062 +       AuDebugOn(!sb);
14063 +       sbinfo = au_sbi(sb);
14064 +       AuDebugOn(!sbinfo);
14065 +       si_write_lock(sb, AuLock_NOPLMW);
14066 +
14067 +       ii_read_lock_parent(a->dir);
14068 +       bfound = -1;
14069 +       bend = au_ibend(a->dir);
14070 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
14071 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
14072 +                       bfound = bindex;
14073 +                       break;
14074 +               }
14075 +       ii_read_unlock(a->dir);
14076 +       if (unlikely(bfound < 0))
14077 +               goto out;
14078 +
14079 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
14080 +       h_ino = 0;
14081 +       if (a->h_child_inode)
14082 +               h_ino = a->h_child_inode->i_ino;
14083 +
14084 +       if (a->h_child_nlen
14085 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
14086 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
14087 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
14088 +                                             a->dir);
14089 +       try_iput = 0;
14090 +       if (dentry)
14091 +               inode = dentry->d_inode;
14092 +       if (xino && !inode && h_ino
14093 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
14094 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
14095 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
14096 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
14097 +               try_iput = 1;
14098 +           }
14099 +
14100 +       args.flags = a->flags[AuHn_CHILD];
14101 +       args.dentry = dentry;
14102 +       args.inode = inode;
14103 +       args.h_inode = a->h_child_inode;
14104 +       args.dir = a->dir;
14105 +       args.h_dir = a->h_dir;
14106 +       args.h_name = a->h_child_name;
14107 +       args.h_nlen = a->h_child_nlen;
14108 +       err = hn_job(&args);
14109 +       if (dentry) {
14110 +               if (au_di(dentry))
14111 +                       di_write_unlock(dentry);
14112 +               dput(dentry);
14113 +       }
14114 +       if (inode && try_iput) {
14115 +               ii_write_unlock(inode);
14116 +               iput(inode);
14117 +       }
14118 +
14119 +       ii_write_lock_parent(a->dir);
14120 +       args.flags = a->flags[AuHn_PARENT];
14121 +       args.dentry = NULL;
14122 +       args.inode = a->dir;
14123 +       args.h_inode = a->h_dir;
14124 +       args.dir = NULL;
14125 +       args.h_dir = NULL;
14126 +       args.h_name = NULL;
14127 +       args.h_nlen = 0;
14128 +       err = hn_job(&args);
14129 +       ii_write_unlock(a->dir);
14130 +
14131 +out:
14132 +       iput(a->h_child_inode);
14133 +       iput(a->h_dir);
14134 +       iput(a->dir);
14135 +       si_write_unlock(sb);
14136 +       au_nwt_done(&sbinfo->si_nowait);
14137 +       kfree(a);
14138 +}
14139 +
14140 +/* ---------------------------------------------------------------------- */
14141 +
14142 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
14143 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
14144 +{
14145 +       int err, len;
14146 +       unsigned int flags[AuHnLast], f;
14147 +       unsigned char isdir, isroot, wh;
14148 +       struct inode *dir;
14149 +       struct au_hnotify_args *args;
14150 +       char *p, *h_child_name;
14151 +
14152 +       err = 0;
14153 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
14154 +       dir = igrab(hnotify->hn_aufs_inode);
14155 +       if (!dir)
14156 +               goto out;
14157 +
14158 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
14159 +       wh = 0;
14160 +       h_child_name = (void *)h_child_qstr->name;
14161 +       len = h_child_qstr->len;
14162 +       if (h_child_name) {
14163 +               if (len > AUFS_WH_PFX_LEN
14164 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
14165 +                       h_child_name += AUFS_WH_PFX_LEN;
14166 +                       len -= AUFS_WH_PFX_LEN;
14167 +                       wh = 1;
14168 +               }
14169 +       }
14170 +
14171 +       isdir = 0;
14172 +       if (h_child_inode)
14173 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
14174 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
14175 +       flags[AuHn_CHILD] = 0;
14176 +       if (isdir)
14177 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
14178 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
14179 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
14180 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
14181 +       case FS_MOVED_FROM:
14182 +       case FS_MOVED_TO:
14183 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
14184 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
14185 +               /*FALLTHROUGH*/
14186 +       case FS_CREATE:
14187 +               AuDebugOn(!h_child_name);
14188 +               break;
14189 +
14190 +       case FS_DELETE:
14191 +               /*
14192 +                * aufs never be able to get this child inode.
14193 +                * revalidation should be in d_revalidate()
14194 +                * by checking i_nlink, i_generation or d_unhashed().
14195 +                */
14196 +               AuDebugOn(!h_child_name);
14197 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
14198 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
14199 +               break;
14200 +
14201 +       default:
14202 +               AuDebugOn(1);
14203 +       }
14204 +
14205 +       if (wh)
14206 +               h_child_inode = NULL;
14207 +
14208 +       err = -ENOMEM;
14209 +       /* iput() and kfree() will be called in au_hnotify() */
14210 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
14211 +       if (unlikely(!args)) {
14212 +               AuErr1("no memory\n");
14213 +               iput(dir);
14214 +               goto out;
14215 +       }
14216 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
14217 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
14218 +       args->mask = mask;
14219 +       args->dir = dir;
14220 +       args->h_dir = igrab(h_dir);
14221 +       if (h_child_inode)
14222 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
14223 +       args->h_child_inode = h_child_inode;
14224 +       args->h_child_nlen = len;
14225 +       if (len) {
14226 +               p = (void *)args;
14227 +               p += sizeof(*args);
14228 +               memcpy(p, h_child_name, len);
14229 +               p[len] = 0;
14230 +       }
14231 +
14232 +       /* NFS fires the event for silly-renamed one from kworker */
14233 +       f = 0;
14234 +       if (!dir->i_nlink
14235 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
14236 +               f = AuWkq_NEST;
14237 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
14238 +       if (unlikely(err)) {
14239 +               pr_err("wkq %d\n", err);
14240 +               iput(args->h_child_inode);
14241 +               iput(args->h_dir);
14242 +               iput(args->dir);
14243 +               kfree(args);
14244 +       }
14245 +
14246 +out:
14247 +       return err;
14248 +}
14249 +
14250 +/* ---------------------------------------------------------------------- */
14251 +
14252 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
14253 +{
14254 +       int err;
14255 +
14256 +       AuDebugOn(!(udba & AuOptMask_UDBA));
14257 +
14258 +       err = 0;
14259 +       if (au_hnotify_op.reset_br)
14260 +               err = au_hnotify_op.reset_br(udba, br, perm);
14261 +
14262 +       return err;
14263 +}
14264 +
14265 +int au_hnotify_init_br(struct au_branch *br, int perm)
14266 +{
14267 +       int err;
14268 +
14269 +       err = 0;
14270 +       if (au_hnotify_op.init_br)
14271 +               err = au_hnotify_op.init_br(br, perm);
14272 +
14273 +       return err;
14274 +}
14275 +
14276 +void au_hnotify_fin_br(struct au_branch *br)
14277 +{
14278 +       if (au_hnotify_op.fin_br)
14279 +               au_hnotify_op.fin_br(br);
14280 +}
14281 +
14282 +static void au_hn_destroy_cache(void)
14283 +{
14284 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
14285 +       au_cachep[AuCache_HNOTIFY] = NULL;
14286 +}
14287 +
14288 +int __init au_hnotify_init(void)
14289 +{
14290 +       int err;
14291 +
14292 +       err = -ENOMEM;
14293 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
14294 +       if (au_cachep[AuCache_HNOTIFY]) {
14295 +               err = 0;
14296 +               if (au_hnotify_op.init)
14297 +                       err = au_hnotify_op.init();
14298 +               if (unlikely(err))
14299 +                       au_hn_destroy_cache();
14300 +       }
14301 +       AuTraceErr(err);
14302 +       return err;
14303 +}
14304 +
14305 +void au_hnotify_fin(void)
14306 +{
14307 +       if (au_hnotify_op.fin)
14308 +               au_hnotify_op.fin();
14309 +       /* cf. au_cache_fin() */
14310 +       if (au_cachep[AuCache_HNOTIFY])
14311 +               au_hn_destroy_cache();
14312 +}
14313 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
14314 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
14315 +++ linux/fs/aufs/iinfo.c       2014-06-24 00:14:06.786052822 +0200
14316 @@ -0,0 +1,277 @@
14317 +/*
14318 + * Copyright (C) 2005-2014 Junjiro R. Okajima
14319 + *
14320 + * This program, aufs is free software; you can redistribute it and/or modify
14321 + * it under the terms of the GNU General Public License as published by
14322 + * the Free Software Foundation; either version 2 of the License, or
14323 + * (at your option) any later version.
14324 + *
14325 + * This program is distributed in the hope that it will be useful,
14326 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14327 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14328 + * GNU General Public License for more details.
14329 + *
14330 + * You should have received a copy of the GNU General Public License
14331 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14332 + */
14333 +
14334 +/*
14335 + * inode private data
14336 + */
14337 +
14338 +#include "aufs.h"
14339 +
14340 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
14341 +{
14342 +       struct inode *h_inode;
14343 +
14344 +       IiMustAnyLock(inode);
14345 +
14346 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
14347 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
14348 +       return h_inode;
14349 +}
14350 +
14351 +/* todo: hard/soft set? */
14352 +void au_hiput(struct au_hinode *hinode)
14353 +{
14354 +       au_hn_free(hinode);
14355 +       dput(hinode->hi_whdentry);
14356 +       iput(hinode->hi_inode);
14357 +}
14358 +
14359 +unsigned int au_hi_flags(struct inode *inode, int isdir)
14360 +{
14361 +       unsigned int flags;
14362 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
14363 +
14364 +       flags = 0;
14365 +       if (au_opt_test(mnt_flags, XINO))
14366 +               au_fset_hi(flags, XINO);
14367 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
14368 +               au_fset_hi(flags, HNOTIFY);
14369 +       return flags;
14370 +}
14371 +
14372 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
14373 +                  struct inode *h_inode, unsigned int flags)
14374 +{
14375 +       struct au_hinode *hinode;
14376 +       struct inode *hi;
14377 +       struct au_iinfo *iinfo = au_ii(inode);
14378 +
14379 +       IiMustWriteLock(inode);
14380 +
14381 +       hinode = iinfo->ii_hinode + bindex;
14382 +       hi = hinode->hi_inode;
14383 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
14384 +
14385 +       if (hi)
14386 +               au_hiput(hinode);
14387 +       hinode->hi_inode = h_inode;
14388 +       if (h_inode) {
14389 +               int err;
14390 +               struct super_block *sb = inode->i_sb;
14391 +               struct au_branch *br;
14392 +
14393 +               AuDebugOn(inode->i_mode
14394 +                         && (h_inode->i_mode & S_IFMT)
14395 +                         != (inode->i_mode & S_IFMT));
14396 +               if (bindex == iinfo->ii_bstart)
14397 +                       au_cpup_igen(inode, h_inode);
14398 +               br = au_sbr(sb, bindex);
14399 +               hinode->hi_id = br->br_id;
14400 +               if (au_ftest_hi(flags, XINO)) {
14401 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
14402 +                                           inode->i_ino);
14403 +                       if (unlikely(err))
14404 +                               AuIOErr1("failed au_xino_write() %d\n", err);
14405 +               }
14406 +
14407 +               if (au_ftest_hi(flags, HNOTIFY)
14408 +                   && au_br_hnotifyable(br->br_perm)) {
14409 +                       err = au_hn_alloc(hinode, inode);
14410 +                       if (unlikely(err))
14411 +                               AuIOErr1("au_hn_alloc() %d\n", err);
14412 +               }
14413 +       }
14414 +}
14415 +
14416 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
14417 +                 struct dentry *h_wh)
14418 +{
14419 +       struct au_hinode *hinode;
14420 +
14421 +       IiMustWriteLock(inode);
14422 +
14423 +       hinode = au_ii(inode)->ii_hinode + bindex;
14424 +       AuDebugOn(hinode->hi_whdentry);
14425 +       hinode->hi_whdentry = h_wh;
14426 +}
14427 +
14428 +void au_update_iigen(struct inode *inode, int half)
14429 +{
14430 +       struct au_iinfo *iinfo;
14431 +       struct au_iigen *iigen;
14432 +       unsigned int sigen;
14433 +
14434 +       sigen = au_sigen(inode->i_sb);
14435 +       iinfo = au_ii(inode);
14436 +       iigen = &iinfo->ii_generation;
14437 +       spin_lock(&iinfo->ii_genspin);
14438 +       iigen->ig_generation = sigen;
14439 +       if (half)
14440 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
14441 +       else
14442 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
14443 +       spin_unlock(&iinfo->ii_genspin);
14444 +}
14445 +
14446 +/* it may be called at remount time, too */
14447 +void au_update_ibrange(struct inode *inode, int do_put_zero)
14448 +{
14449 +       struct au_iinfo *iinfo;
14450 +       aufs_bindex_t bindex, bend;
14451 +
14452 +       iinfo = au_ii(inode);
14453 +       if (!iinfo)
14454 +               return;
14455 +
14456 +       IiMustWriteLock(inode);
14457 +
14458 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
14459 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
14460 +                    bindex++) {
14461 +                       struct inode *h_i;
14462 +
14463 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
14464 +                       if (h_i
14465 +                           && !h_i->i_nlink
14466 +                           && !(h_i->i_state & I_LINKABLE))
14467 +                               au_set_h_iptr(inode, bindex, NULL, 0);
14468 +               }
14469 +       }
14470 +
14471 +       iinfo->ii_bstart = -1;
14472 +       iinfo->ii_bend = -1;
14473 +       bend = au_sbend(inode->i_sb);
14474 +       for (bindex = 0; bindex <= bend; bindex++)
14475 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
14476 +                       iinfo->ii_bstart = bindex;
14477 +                       break;
14478 +               }
14479 +       if (iinfo->ii_bstart >= 0)
14480 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
14481 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
14482 +                               iinfo->ii_bend = bindex;
14483 +                               break;
14484 +                       }
14485 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
14486 +}
14487 +
14488 +/* ---------------------------------------------------------------------- */
14489 +
14490 +void au_icntnr_init_once(void *_c)
14491 +{
14492 +       struct au_icntnr *c = _c;
14493 +       struct au_iinfo *iinfo = &c->iinfo;
14494 +       static struct lock_class_key aufs_ii;
14495 +
14496 +       spin_lock_init(&iinfo->ii_genspin);
14497 +       au_rw_init(&iinfo->ii_rwsem);
14498 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
14499 +       inode_init_once(&c->vfs_inode);
14500 +}
14501 +
14502 +int au_iinfo_init(struct inode *inode)
14503 +{
14504 +       struct au_iinfo *iinfo;
14505 +       struct super_block *sb;
14506 +       int nbr, i;
14507 +
14508 +       sb = inode->i_sb;
14509 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
14510 +       nbr = au_sbend(sb) + 1;
14511 +       if (unlikely(nbr <= 0))
14512 +               nbr = 1;
14513 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
14514 +       if (iinfo->ii_hinode) {
14515 +               au_ninodes_inc(sb);
14516 +               for (i = 0; i < nbr; i++)
14517 +                       iinfo->ii_hinode[i].hi_id = -1;
14518 +
14519 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
14520 +               iinfo->ii_bstart = -1;
14521 +               iinfo->ii_bend = -1;
14522 +               iinfo->ii_vdir = NULL;
14523 +               return 0;
14524 +       }
14525 +       return -ENOMEM;
14526 +}
14527 +
14528 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
14529 +{
14530 +       int err, sz;
14531 +       struct au_hinode *hip;
14532 +
14533 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
14534 +
14535 +       err = -ENOMEM;
14536 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
14537 +       if (!sz)
14538 +               sz = sizeof(*hip);
14539 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
14540 +       if (hip) {
14541 +               iinfo->ii_hinode = hip;
14542 +               err = 0;
14543 +       }
14544 +
14545 +       return err;
14546 +}
14547 +
14548 +void au_iinfo_fin(struct inode *inode)
14549 +{
14550 +       struct au_iinfo *iinfo;
14551 +       struct au_hinode *hi;
14552 +       struct super_block *sb;
14553 +       aufs_bindex_t bindex, bend;
14554 +       const unsigned char unlinked = !inode->i_nlink;
14555 +
14556 +       iinfo = au_ii(inode);
14557 +       /* bad_inode case */
14558 +       if (!iinfo)
14559 +               return;
14560 +
14561 +       sb = inode->i_sb;
14562 +       au_ninodes_dec(sb);
14563 +       if (si_pid_test(sb))
14564 +               au_xino_delete_inode(inode, unlinked);
14565 +       else {
14566 +               /*
14567 +                * it is safe to hide the dependency between sbinfo and
14568 +                * sb->s_umount.
14569 +                */
14570 +               lockdep_off();
14571 +               si_noflush_read_lock(sb);
14572 +               au_xino_delete_inode(inode, unlinked);
14573 +               si_read_unlock(sb);
14574 +               lockdep_on();
14575 +       }
14576 +
14577 +       if (iinfo->ii_vdir)
14578 +               au_vdir_free(iinfo->ii_vdir);
14579 +
14580 +       bindex = iinfo->ii_bstart;
14581 +       if (bindex >= 0) {
14582 +               hi = iinfo->ii_hinode + bindex;
14583 +               bend = iinfo->ii_bend;
14584 +               while (bindex++ <= bend) {
14585 +                       if (hi->hi_inode)
14586 +                               au_hiput(hi);
14587 +                       hi++;
14588 +               }
14589 +       }
14590 +       kfree(iinfo->ii_hinode);
14591 +       iinfo->ii_hinode = NULL;
14592 +       AuRwDestroy(&iinfo->ii_rwsem);
14593 +}
14594 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
14595 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
14596 +++ linux/fs/aufs/inode.c       2014-06-24 00:14:06.786052822 +0200
14597 @@ -0,0 +1,491 @@
14598 +/*
14599 + * Copyright (C) 2005-2014 Junjiro R. Okajima
14600 + *
14601 + * This program, aufs is free software; you can redistribute it and/or modify
14602 + * it under the terms of the GNU General Public License as published by
14603 + * the Free Software Foundation; either version 2 of the License, or
14604 + * (at your option) any later version.
14605 + *
14606 + * This program is distributed in the hope that it will be useful,
14607 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14608 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14609 + * GNU General Public License for more details.
14610 + *
14611 + * You should have received a copy of the GNU General Public License
14612 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14613 + */
14614 +
14615 +/*
14616 + * inode functions
14617 + */
14618 +
14619 +#include "aufs.h"
14620 +
14621 +struct inode *au_igrab(struct inode *inode)
14622 +{
14623 +       if (inode) {
14624 +               AuDebugOn(!atomic_read(&inode->i_count));
14625 +               ihold(inode);
14626 +       }
14627 +       return inode;
14628 +}
14629 +
14630 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
14631 +{
14632 +       au_cpup_attr_all(inode, /*force*/0);
14633 +       au_update_iigen(inode, /*half*/1);
14634 +       if (do_version)
14635 +               inode->i_version++;
14636 +}
14637 +
14638 +static int au_ii_refresh(struct inode *inode, int *update)
14639 +{
14640 +       int err, e;
14641 +       umode_t type;
14642 +       aufs_bindex_t bindex, new_bindex;
14643 +       struct super_block *sb;
14644 +       struct au_iinfo *iinfo;
14645 +       struct au_hinode *p, *q, tmp;
14646 +
14647 +       IiMustWriteLock(inode);
14648 +
14649 +       *update = 0;
14650 +       sb = inode->i_sb;
14651 +       type = inode->i_mode & S_IFMT;
14652 +       iinfo = au_ii(inode);
14653 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
14654 +       if (unlikely(err))
14655 +               goto out;
14656 +
14657 +       AuDebugOn(iinfo->ii_bstart < 0);
14658 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
14659 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
14660 +            bindex++, p++) {
14661 +               if (!p->hi_inode)
14662 +                       continue;
14663 +
14664 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
14665 +               new_bindex = au_br_index(sb, p->hi_id);
14666 +               if (new_bindex == bindex)
14667 +                       continue;
14668 +
14669 +               if (new_bindex < 0) {
14670 +                       *update = 1;
14671 +                       au_hiput(p);
14672 +                       p->hi_inode = NULL;
14673 +                       continue;
14674 +               }
14675 +
14676 +               if (new_bindex < iinfo->ii_bstart)
14677 +                       iinfo->ii_bstart = new_bindex;
14678 +               if (iinfo->ii_bend < new_bindex)
14679 +                       iinfo->ii_bend = new_bindex;
14680 +               /* swap two lower inode, and loop again */
14681 +               q = iinfo->ii_hinode + new_bindex;
14682 +               tmp = *q;
14683 +               *q = *p;
14684 +               *p = tmp;
14685 +               if (tmp.hi_inode) {
14686 +                       bindex--;
14687 +                       p--;
14688 +               }
14689 +       }
14690 +       au_update_ibrange(inode, /*do_put_zero*/0);
14691 +       e = au_dy_irefresh(inode);
14692 +       if (unlikely(e && !err))
14693 +               err = e;
14694 +
14695 +out:
14696 +       AuTraceErr(err);
14697 +       return err;
14698 +}
14699 +
14700 +int au_refresh_hinode_self(struct inode *inode)
14701 +{
14702 +       int err, update;
14703 +
14704 +       err = au_ii_refresh(inode, &update);
14705 +       if (!err)
14706 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
14707 +
14708 +       AuTraceErr(err);
14709 +       return err;
14710 +}
14711 +
14712 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
14713 +{
14714 +       int err, e, update;
14715 +       unsigned int flags;
14716 +       umode_t mode;
14717 +       aufs_bindex_t bindex, bend;
14718 +       unsigned char isdir;
14719 +       struct au_hinode *p;
14720 +       struct au_iinfo *iinfo;
14721 +
14722 +       err = au_ii_refresh(inode, &update);
14723 +       if (unlikely(err))
14724 +               goto out;
14725 +
14726 +       update = 0;
14727 +       iinfo = au_ii(inode);
14728 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
14729 +       mode = (inode->i_mode & S_IFMT);
14730 +       isdir = S_ISDIR(mode);
14731 +       flags = au_hi_flags(inode, isdir);
14732 +       bend = au_dbend(dentry);
14733 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
14734 +               struct inode *h_i;
14735 +               struct dentry *h_d;
14736 +
14737 +               h_d = au_h_dptr(dentry, bindex);
14738 +               if (!h_d || !h_d->d_inode)
14739 +                       continue;
14740 +
14741 +               AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
14742 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
14743 +                       h_i = au_h_iptr(inode, bindex);
14744 +                       if (h_i) {
14745 +                               if (h_i == h_d->d_inode)
14746 +                                       continue;
14747 +                               err = -EIO;
14748 +                               break;
14749 +                       }
14750 +               }
14751 +               if (bindex < iinfo->ii_bstart)
14752 +                       iinfo->ii_bstart = bindex;
14753 +               if (iinfo->ii_bend < bindex)
14754 +                       iinfo->ii_bend = bindex;
14755 +               au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
14756 +               update = 1;
14757 +       }
14758 +       au_update_ibrange(inode, /*do_put_zero*/0);
14759 +       e = au_dy_irefresh(inode);
14760 +       if (unlikely(e && !err))
14761 +               err = e;
14762 +       if (!err)
14763 +               au_refresh_hinode_attr(inode, update && isdir);
14764 +
14765 +out:
14766 +       AuTraceErr(err);
14767 +       return err;
14768 +}
14769 +
14770 +static int set_inode(struct inode *inode, struct dentry *dentry)
14771 +{
14772 +       int err;
14773 +       unsigned int flags;
14774 +       umode_t mode;
14775 +       aufs_bindex_t bindex, bstart, btail;
14776 +       unsigned char isdir;
14777 +       struct dentry *h_dentry;
14778 +       struct inode *h_inode;
14779 +       struct au_iinfo *iinfo;
14780 +
14781 +       IiMustWriteLock(inode);
14782 +
14783 +       err = 0;
14784 +       isdir = 0;
14785 +       bstart = au_dbstart(dentry);
14786 +       h_inode = au_h_dptr(dentry, bstart)->d_inode;
14787 +       mode = h_inode->i_mode;
14788 +       switch (mode & S_IFMT) {
14789 +       case S_IFREG:
14790 +               btail = au_dbtail(dentry);
14791 +               inode->i_op = &aufs_iop;
14792 +               inode->i_fop = &aufs_file_fop;
14793 +               err = au_dy_iaop(inode, bstart, h_inode);
14794 +               if (unlikely(err))
14795 +                       goto out;
14796 +               break;
14797 +       case S_IFDIR:
14798 +               isdir = 1;
14799 +               btail = au_dbtaildir(dentry);
14800 +               inode->i_op = &aufs_dir_iop;
14801 +               inode->i_fop = &aufs_dir_fop;
14802 +               break;
14803 +       case S_IFLNK:
14804 +               btail = au_dbtail(dentry);
14805 +               inode->i_op = &aufs_symlink_iop;
14806 +               break;
14807 +       case S_IFBLK:
14808 +       case S_IFCHR:
14809 +       case S_IFIFO:
14810 +       case S_IFSOCK:
14811 +               btail = au_dbtail(dentry);
14812 +               inode->i_op = &aufs_iop;
14813 +               init_special_inode(inode, mode, h_inode->i_rdev);
14814 +               break;
14815 +       default:
14816 +               AuIOErr("Unknown file type 0%o\n", mode);
14817 +               err = -EIO;
14818 +               goto out;
14819 +       }
14820 +
14821 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
14822 +       flags = au_hi_flags(inode, isdir);
14823 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
14824 +           && au_ftest_hi(flags, HNOTIFY)
14825 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
14826 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
14827 +               au_fclr_hi(flags, HNOTIFY);
14828 +       iinfo = au_ii(inode);
14829 +       iinfo->ii_bstart = bstart;
14830 +       iinfo->ii_bend = btail;
14831 +       for (bindex = bstart; bindex <= btail; bindex++) {
14832 +               h_dentry = au_h_dptr(dentry, bindex);
14833 +               if (h_dentry)
14834 +                       au_set_h_iptr(inode, bindex,
14835 +                                     au_igrab(h_dentry->d_inode), flags);
14836 +       }
14837 +       au_cpup_attr_all(inode, /*force*/1);
14838 +
14839 +out:
14840 +       return err;
14841 +}
14842 +
14843 +/*
14844 + * successful returns with iinfo write_locked
14845 + * minus: errno
14846 + * zero: success, matched
14847 + * plus: no error, but unmatched
14848 + */
14849 +static int reval_inode(struct inode *inode, struct dentry *dentry)
14850 +{
14851 +       int err;
14852 +       unsigned int gen;
14853 +       struct au_iigen iigen;
14854 +       aufs_bindex_t bindex, bend;
14855 +       struct inode *h_inode, *h_dinode;
14856 +
14857 +       /*
14858 +        * before this function, if aufs got any iinfo lock, it must be only
14859 +        * one, the parent dir.
14860 +        * it can happen by UDBA and the obsoleted inode number.
14861 +        */
14862 +       err = -EIO;
14863 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
14864 +               goto out;
14865 +
14866 +       err = 1;
14867 +       ii_write_lock_new_child(inode);
14868 +       h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
14869 +       bend = au_ibend(inode);
14870 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
14871 +               h_inode = au_h_iptr(inode, bindex);
14872 +               if (!h_inode || h_inode != h_dinode)
14873 +                       continue;
14874 +
14875 +               err = 0;
14876 +               gen = au_iigen(inode, &iigen);
14877 +               if (gen == au_digen(dentry)
14878 +                   && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
14879 +                       break;
14880 +
14881 +               /* fully refresh inode using dentry */
14882 +               err = au_refresh_hinode(inode, dentry);
14883 +               if (!err)
14884 +                       au_update_iigen(inode, /*half*/0);
14885 +               break;
14886 +       }
14887 +
14888 +       if (unlikely(err))
14889 +               ii_write_unlock(inode);
14890 +out:
14891 +       return err;
14892 +}
14893 +
14894 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
14895 +          unsigned int d_type, ino_t *ino)
14896 +{
14897 +       int err;
14898 +       struct mutex *mtx;
14899 +
14900 +       /* prevent hardlinked inode number from race condition */
14901 +       mtx = NULL;
14902 +       if (d_type != DT_DIR) {
14903 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
14904 +               mutex_lock(mtx);
14905 +       }
14906 +       err = au_xino_read(sb, bindex, h_ino, ino);
14907 +       if (unlikely(err))
14908 +               goto out;
14909 +
14910 +       if (!*ino) {
14911 +               err = -EIO;
14912 +               *ino = au_xino_new_ino(sb);
14913 +               if (unlikely(!*ino))
14914 +                       goto out;
14915 +               err = au_xino_write(sb, bindex, h_ino, *ino);
14916 +               if (unlikely(err))
14917 +                       goto out;
14918 +       }
14919 +
14920 +out:
14921 +       if (mtx)
14922 +               mutex_unlock(mtx);
14923 +       return err;
14924 +}
14925 +
14926 +/* successful returns with iinfo write_locked */
14927 +/* todo: return with unlocked? */
14928 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
14929 +{
14930 +       struct inode *inode, *h_inode;
14931 +       struct dentry *h_dentry;
14932 +       struct super_block *sb;
14933 +       struct mutex *mtx;
14934 +       ino_t h_ino, ino;
14935 +       int err;
14936 +       aufs_bindex_t bstart;
14937 +
14938 +       sb = dentry->d_sb;
14939 +       bstart = au_dbstart(dentry);
14940 +       h_dentry = au_h_dptr(dentry, bstart);
14941 +       h_inode = h_dentry->d_inode;
14942 +       h_ino = h_inode->i_ino;
14943 +
14944 +       /*
14945 +        * stop 'race'-ing between hardlinks under different
14946 +        * parents.
14947 +        */
14948 +       mtx = NULL;
14949 +       if (!S_ISDIR(h_inode->i_mode))
14950 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
14951 +
14952 +new_ino:
14953 +       if (mtx)
14954 +               mutex_lock(mtx);
14955 +       err = au_xino_read(sb, bstart, h_ino, &ino);
14956 +       inode = ERR_PTR(err);
14957 +       if (unlikely(err))
14958 +               goto out;
14959 +
14960 +       if (!ino) {
14961 +               ino = au_xino_new_ino(sb);
14962 +               if (unlikely(!ino)) {
14963 +                       inode = ERR_PTR(-EIO);
14964 +                       goto out;
14965 +               }
14966 +       }
14967 +
14968 +       AuDbg("i%lu\n", (unsigned long)ino);
14969 +       inode = au_iget_locked(sb, ino);
14970 +       err = PTR_ERR(inode);
14971 +       if (IS_ERR(inode))
14972 +               goto out;
14973 +
14974 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
14975 +       if (inode->i_state & I_NEW) {
14976 +               /* verbose coding for lock class name */
14977 +               if (unlikely(S_ISLNK(h_inode->i_mode)))
14978 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
14979 +                                   au_lc_key + AuLcSymlink_IIINFO);
14980 +               else if (unlikely(S_ISDIR(h_inode->i_mode)))
14981 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
14982 +                                   au_lc_key + AuLcDir_IIINFO);
14983 +               else /* likely */
14984 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
14985 +                                   au_lc_key + AuLcNonDir_IIINFO);
14986 +
14987 +               ii_write_lock_new_child(inode);
14988 +               err = set_inode(inode, dentry);
14989 +               if (!err) {
14990 +                       unlock_new_inode(inode);
14991 +                       goto out; /* success */
14992 +               }
14993 +
14994 +               /*
14995 +                * iget_failed() calls iput(), but we need to call
14996 +                * ii_write_unlock() after iget_failed(). so dirty hack for
14997 +                * i_count.
14998 +                */
14999 +               atomic_inc(&inode->i_count);
15000 +               iget_failed(inode);
15001 +               ii_write_unlock(inode);
15002 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
15003 +               /* ignore this error */
15004 +               goto out_iput;
15005 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
15006 +               /*
15007 +                * horrible race condition between lookup, readdir and copyup
15008 +                * (or something).
15009 +                */
15010 +               if (mtx)
15011 +                       mutex_unlock(mtx);
15012 +               err = reval_inode(inode, dentry);
15013 +               if (unlikely(err < 0)) {
15014 +                       mtx = NULL;
15015 +                       goto out_iput;
15016 +               }
15017 +
15018 +               if (!err) {
15019 +                       mtx = NULL;
15020 +                       goto out; /* success */
15021 +               } else if (mtx)
15022 +                       mutex_lock(mtx);
15023 +       }
15024 +
15025 +       if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
15026 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
15027 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
15028 +                       bstart, au_sbtype(h_dentry->d_sb), dentry,
15029 +                       (unsigned long)h_ino, (unsigned long)ino);
15030 +       ino = 0;
15031 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
15032 +       if (!err) {
15033 +               iput(inode);
15034 +               if (mtx)
15035 +                       mutex_unlock(mtx);
15036 +               goto new_ino;
15037 +       }
15038 +
15039 +out_iput:
15040 +       iput(inode);
15041 +       inode = ERR_PTR(err);
15042 +out:
15043 +       if (mtx)
15044 +               mutex_unlock(mtx);
15045 +       return inode;
15046 +}
15047 +
15048 +/* ---------------------------------------------------------------------- */
15049 +
15050 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
15051 +              struct inode *inode)
15052 +{
15053 +       int err;
15054 +
15055 +       err = au_br_rdonly(au_sbr(sb, bindex));
15056 +
15057 +       /* pseudo-link after flushed may happen out of bounds */
15058 +       if (!err
15059 +           && inode
15060 +           && au_ibstart(inode) <= bindex
15061 +           && bindex <= au_ibend(inode)) {
15062 +               /*
15063 +                * permission check is unnecessary since vfsub routine
15064 +                * will be called later
15065 +                */
15066 +               struct inode *hi = au_h_iptr(inode, bindex);
15067 +               if (hi)
15068 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
15069 +       }
15070 +
15071 +       return err;
15072 +}
15073 +
15074 +int au_test_h_perm(struct inode *h_inode, int mask)
15075 +{
15076 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
15077 +               return 0;
15078 +       return inode_permission(h_inode, mask);
15079 +}
15080 +
15081 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
15082 +{
15083 +       if (au_test_nfs(h_inode->i_sb)
15084 +           && (mask & MAY_WRITE)
15085 +           && S_ISDIR(h_inode->i_mode))
15086 +               mask |= MAY_READ; /* force permission check */
15087 +       return au_test_h_perm(h_inode, mask);
15088 +}
15089 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
15090 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
15091 +++ linux/fs/aufs/inode.h       2014-06-24 00:14:06.786052822 +0200
15092 @@ -0,0 +1,601 @@
15093 +/*
15094 + * Copyright (C) 2005-2014 Junjiro R. Okajima
15095 + *
15096 + * This program, aufs is free software; you can redistribute it and/or modify
15097 + * it under the terms of the GNU General Public License as published by
15098 + * the Free Software Foundation; either version 2 of the License, or
15099 + * (at your option) any later version.
15100 + *
15101 + * This program is distributed in the hope that it will be useful,
15102 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15103 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15104 + * GNU General Public License for more details.
15105 + *
15106 + * You should have received a copy of the GNU General Public License
15107 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15108 + */
15109 +
15110 +/*
15111 + * inode operations
15112 + */
15113 +
15114 +#ifndef __AUFS_INODE_H__
15115 +#define __AUFS_INODE_H__
15116 +
15117 +#ifdef __KERNEL__
15118 +
15119 +#include <linux/fsnotify.h>
15120 +#include "rwsem.h"
15121 +
15122 +struct vfsmount;
15123 +
15124 +struct au_hnotify {
15125 +#ifdef CONFIG_AUFS_HNOTIFY
15126 +#ifdef CONFIG_AUFS_HFSNOTIFY
15127 +       /* never use fsnotify_add_vfsmount_mark() */
15128 +       struct fsnotify_mark            hn_mark;
15129 +#endif
15130 +       struct inode                    *hn_aufs_inode; /* no get/put */
15131 +#endif
15132 +} ____cacheline_aligned_in_smp;
15133 +
15134 +struct au_hinode {
15135 +       struct inode            *hi_inode;
15136 +       aufs_bindex_t           hi_id;
15137 +#ifdef CONFIG_AUFS_HNOTIFY
15138 +       struct au_hnotify       *hi_notify;
15139 +#endif
15140 +
15141 +       /* reference to the copied-up whiteout with get/put */
15142 +       struct dentry           *hi_whdentry;
15143 +};
15144 +
15145 +/* ig_flags */
15146 +#define AuIG_HALF_REFRESHED            1
15147 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
15148 +#define au_ig_fset(flags, name) \
15149 +       do { (flags) |= AuIG_##name; } while (0)
15150 +#define au_ig_fclr(flags, name) \
15151 +       do { (flags) &= ~AuIG_##name; } while (0)
15152 +
15153 +struct au_iigen {
15154 +       __u32           ig_generation, ig_flags;
15155 +};
15156 +
15157 +struct au_vdir;
15158 +struct au_iinfo {
15159 +       spinlock_t              ii_genspin;
15160 +       struct au_iigen         ii_generation;
15161 +       struct super_block      *ii_hsb1;       /* no get/put */
15162 +
15163 +       struct au_rwsem         ii_rwsem;
15164 +       aufs_bindex_t           ii_bstart, ii_bend;
15165 +       __u32                   ii_higen;
15166 +       struct au_hinode        *ii_hinode;
15167 +       struct au_vdir          *ii_vdir;
15168 +};
15169 +
15170 +struct au_icntnr {
15171 +       struct au_iinfo iinfo;
15172 +       struct inode vfs_inode;
15173 +} ____cacheline_aligned_in_smp;
15174 +
15175 +/* au_pin flags */
15176 +#define AuPin_DI_LOCKED                1
15177 +#define AuPin_MNT_WRITE                (1 << 1)
15178 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
15179 +#define au_fset_pin(flags, name) \
15180 +       do { (flags) |= AuPin_##name; } while (0)
15181 +#define au_fclr_pin(flags, name) \
15182 +       do { (flags) &= ~AuPin_##name; } while (0)
15183 +
15184 +struct au_pin {
15185 +       /* input */
15186 +       struct dentry *dentry;
15187 +       unsigned int udba;
15188 +       unsigned char lsc_di, lsc_hi, flags;
15189 +       aufs_bindex_t bindex;
15190 +
15191 +       /* output */
15192 +       struct dentry *parent;
15193 +       struct au_hinode *hdir;
15194 +       struct vfsmount *h_mnt;
15195 +
15196 +       /* temporary unlock/relock for copyup */
15197 +       struct dentry *h_dentry, *h_parent;
15198 +       struct au_branch *br;
15199 +       struct task_struct *task;
15200 +};
15201 +
15202 +void au_pin_hdir_unlock(struct au_pin *p);
15203 +int au_pin_hdir_relock(struct au_pin *p);
15204 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
15205 +void au_pin_hdir_acquire_nest(struct au_pin *p);
15206 +void au_pin_hdir_release(struct au_pin *p);
15207 +
15208 +/* ---------------------------------------------------------------------- */
15209 +
15210 +static inline struct au_iinfo *au_ii(struct inode *inode)
15211 +{
15212 +       struct au_iinfo *iinfo;
15213 +
15214 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15215 +       if (iinfo->ii_hinode)
15216 +               return iinfo;
15217 +       return NULL; /* debugging bad_inode case */
15218 +}
15219 +
15220 +/* ---------------------------------------------------------------------- */
15221 +
15222 +/* inode.c */
15223 +struct inode *au_igrab(struct inode *inode);
15224 +int au_refresh_hinode_self(struct inode *inode);
15225 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
15226 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
15227 +          unsigned int d_type, ino_t *ino);
15228 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
15229 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
15230 +              struct inode *inode);
15231 +int au_test_h_perm(struct inode *h_inode, int mask);
15232 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
15233 +
15234 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
15235 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
15236 +{
15237 +#ifdef CONFIG_AUFS_SHWH
15238 +       return au_ino(sb, bindex, h_ino, d_type, ino);
15239 +#else
15240 +       return 0;
15241 +#endif
15242 +}
15243 +
15244 +/* i_op.c */
15245 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
15246 +
15247 +/* au_wr_dir flags */
15248 +#define AuWrDir_ADD_ENTRY      1
15249 +#define AuWrDir_TMP_WHENTRY    (1 << 1)
15250 +#define AuWrDir_ISDIR          (1 << 2)
15251 +#define AuWrDir_TMPFILE                (1 << 3)
15252 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
15253 +#define au_fset_wrdir(flags, name) \
15254 +       do { (flags) |= AuWrDir_##name; } while (0)
15255 +#define au_fclr_wrdir(flags, name) \
15256 +       do { (flags) &= ~AuWrDir_##name; } while (0)
15257 +
15258 +struct au_wr_dir_args {
15259 +       aufs_bindex_t force_btgt;
15260 +       unsigned char flags;
15261 +};
15262 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
15263 +             struct au_wr_dir_args *args);
15264 +
15265 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
15266 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
15267 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
15268 +                unsigned int udba, unsigned char flags);
15269 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
15270 +          unsigned int udba, unsigned char flags) __must_check;
15271 +int au_do_pin(struct au_pin *pin) __must_check;
15272 +void au_unpin(struct au_pin *pin);
15273 +
15274 +/* i_op_add.c */
15275 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
15276 +              struct dentry *h_parent, int isdir);
15277 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
15278 +              dev_t dev);
15279 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
15280 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
15281 +               bool want_excl);
15282 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
15283 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
15284 +             struct dentry *dentry);
15285 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
15286 +
15287 +/* i_op_del.c */
15288 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
15289 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
15290 +              struct dentry *h_parent, int isdir);
15291 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
15292 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
15293 +
15294 +/* i_op_ren.c */
15295 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
15296 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
15297 +               struct inode *dir, struct dentry *dentry);
15298 +
15299 +/* iinfo.c */
15300 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
15301 +void au_hiput(struct au_hinode *hinode);
15302 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15303 +                 struct dentry *h_wh);
15304 +unsigned int au_hi_flags(struct inode *inode, int isdir);
15305 +
15306 +/* hinode flags */
15307 +#define AuHi_XINO      1
15308 +#define AuHi_HNOTIFY   (1 << 1)
15309 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
15310 +#define au_fset_hi(flags, name) \
15311 +       do { (flags) |= AuHi_##name; } while (0)
15312 +#define au_fclr_hi(flags, name) \
15313 +       do { (flags) &= ~AuHi_##name; } while (0)
15314 +
15315 +#ifndef CONFIG_AUFS_HNOTIFY
15316 +#undef AuHi_HNOTIFY
15317 +#define AuHi_HNOTIFY   0
15318 +#endif
15319 +
15320 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15321 +                  struct inode *h_inode, unsigned int flags);
15322 +
15323 +void au_update_iigen(struct inode *inode, int half);
15324 +void au_update_ibrange(struct inode *inode, int do_put_zero);
15325 +
15326 +void au_icntnr_init_once(void *_c);
15327 +int au_iinfo_init(struct inode *inode);
15328 +void au_iinfo_fin(struct inode *inode);
15329 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
15330 +
15331 +#ifdef CONFIG_PROC_FS
15332 +/* plink.c */
15333 +int au_plink_maint(struct super_block *sb, int flags);
15334 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
15335 +int au_plink_maint_enter(struct super_block *sb);
15336 +#ifdef CONFIG_AUFS_DEBUG
15337 +void au_plink_list(struct super_block *sb);
15338 +#else
15339 +AuStubVoid(au_plink_list, struct super_block *sb)
15340 +#endif
15341 +int au_plink_test(struct inode *inode);
15342 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
15343 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
15344 +                    struct dentry *h_dentry);
15345 +void au_plink_put(struct super_block *sb, int verbose);
15346 +void au_plink_clean(struct super_block *sb, int verbose);
15347 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
15348 +#else
15349 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
15350 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
15351 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
15352 +AuStubVoid(au_plink_list, struct super_block *sb);
15353 +AuStubInt0(au_plink_test, struct inode *inode);
15354 +AuStub(struct dentry *, au_plink_lkup, return NULL,
15355 +       struct inode *inode, aufs_bindex_t bindex);
15356 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
15357 +          struct dentry *h_dentry);
15358 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
15359 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
15360 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
15361 +#endif /* CONFIG_PROC_FS */
15362 +
15363 +/* ---------------------------------------------------------------------- */
15364 +
15365 +/* lock subclass for iinfo */
15366 +enum {
15367 +       AuLsc_II_CHILD,         /* child first */
15368 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
15369 +       AuLsc_II_CHILD3,        /* copyup dirs */
15370 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
15371 +       AuLsc_II_PARENT2,
15372 +       AuLsc_II_PARENT3,       /* copyup dirs */
15373 +       AuLsc_II_NEW_CHILD
15374 +};
15375 +
15376 +/*
15377 + * ii_read_lock_child, ii_write_lock_child,
15378 + * ii_read_lock_child2, ii_write_lock_child2,
15379 + * ii_read_lock_child3, ii_write_lock_child3,
15380 + * ii_read_lock_parent, ii_write_lock_parent,
15381 + * ii_read_lock_parent2, ii_write_lock_parent2,
15382 + * ii_read_lock_parent3, ii_write_lock_parent3,
15383 + * ii_read_lock_new_child, ii_write_lock_new_child,
15384 + */
15385 +#define AuReadLockFunc(name, lsc) \
15386 +static inline void ii_read_lock_##name(struct inode *i) \
15387 +{ \
15388 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15389 +}
15390 +
15391 +#define AuWriteLockFunc(name, lsc) \
15392 +static inline void ii_write_lock_##name(struct inode *i) \
15393 +{ \
15394 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
15395 +}
15396 +
15397 +#define AuRWLockFuncs(name, lsc) \
15398 +       AuReadLockFunc(name, lsc) \
15399 +       AuWriteLockFunc(name, lsc)
15400 +
15401 +AuRWLockFuncs(child, CHILD);
15402 +AuRWLockFuncs(child2, CHILD2);
15403 +AuRWLockFuncs(child3, CHILD3);
15404 +AuRWLockFuncs(parent, PARENT);
15405 +AuRWLockFuncs(parent2, PARENT2);
15406 +AuRWLockFuncs(parent3, PARENT3);
15407 +AuRWLockFuncs(new_child, NEW_CHILD);
15408 +
15409 +#undef AuReadLockFunc
15410 +#undef AuWriteLockFunc
15411 +#undef AuRWLockFuncs
15412 +
15413 +/*
15414 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
15415 + */
15416 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
15417 +
15418 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
15419 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
15420 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
15421 +
15422 +/* ---------------------------------------------------------------------- */
15423 +
15424 +static inline void au_icntnr_init(struct au_icntnr *c)
15425 +{
15426 +#ifdef CONFIG_AUFS_DEBUG
15427 +       c->vfs_inode.i_mode = 0;
15428 +#endif
15429 +}
15430 +
15431 +static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
15432 +{
15433 +       unsigned int gen;
15434 +       struct au_iinfo *iinfo;
15435 +
15436 +       iinfo = au_ii(inode);
15437 +       spin_lock(&iinfo->ii_genspin);
15438 +       if (iigen)
15439 +               *iigen = iinfo->ii_generation;
15440 +       gen = iinfo->ii_generation.ig_generation;
15441 +       spin_unlock(&iinfo->ii_genspin);
15442 +
15443 +       return gen;
15444 +}
15445 +
15446 +/* tiny test for inode number */
15447 +/* tmpfs generation is too rough */
15448 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
15449 +{
15450 +       struct au_iinfo *iinfo;
15451 +
15452 +       iinfo = au_ii(inode);
15453 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
15454 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
15455 +                && iinfo->ii_higen == h_inode->i_generation);
15456 +}
15457 +
15458 +static inline void au_iigen_dec(struct inode *inode)
15459 +{
15460 +       struct au_iinfo *iinfo;
15461 +
15462 +       iinfo = au_ii(inode);
15463 +       spin_lock(&iinfo->ii_genspin);
15464 +       iinfo->ii_generation.ig_generation--;
15465 +       spin_unlock(&iinfo->ii_genspin);
15466 +}
15467 +
15468 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
15469 +{
15470 +       int err;
15471 +
15472 +       err = 0;
15473 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
15474 +               err = -EIO;
15475 +
15476 +       return err;
15477 +}
15478 +
15479 +/* ---------------------------------------------------------------------- */
15480 +
15481 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
15482 +                                       aufs_bindex_t bindex)
15483 +{
15484 +       IiMustAnyLock(inode);
15485 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
15486 +}
15487 +
15488 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
15489 +{
15490 +       IiMustAnyLock(inode);
15491 +       return au_ii(inode)->ii_bstart;
15492 +}
15493 +
15494 +static inline aufs_bindex_t au_ibend(struct inode *inode)
15495 +{
15496 +       IiMustAnyLock(inode);
15497 +       return au_ii(inode)->ii_bend;
15498 +}
15499 +
15500 +static inline struct au_vdir *au_ivdir(struct inode *inode)
15501 +{
15502 +       IiMustAnyLock(inode);
15503 +       return au_ii(inode)->ii_vdir;
15504 +}
15505 +
15506 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
15507 +{
15508 +       IiMustAnyLock(inode);
15509 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
15510 +}
15511 +
15512 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
15513 +{
15514 +       IiMustWriteLock(inode);
15515 +       au_ii(inode)->ii_bstart = bindex;
15516 +}
15517 +
15518 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
15519 +{
15520 +       IiMustWriteLock(inode);
15521 +       au_ii(inode)->ii_bend = bindex;
15522 +}
15523 +
15524 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
15525 +{
15526 +       IiMustWriteLock(inode);
15527 +       au_ii(inode)->ii_vdir = vdir;
15528 +}
15529 +
15530 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
15531 +{
15532 +       IiMustAnyLock(inode);
15533 +       return au_ii(inode)->ii_hinode + bindex;
15534 +}
15535 +
15536 +/* ---------------------------------------------------------------------- */
15537 +
15538 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
15539 +{
15540 +       if (pin)
15541 +               return pin->parent;
15542 +       return NULL;
15543 +}
15544 +
15545 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
15546 +{
15547 +       if (pin && pin->hdir)
15548 +               return pin->hdir->hi_inode;
15549 +       return NULL;
15550 +}
15551 +
15552 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
15553 +{
15554 +       if (pin)
15555 +               return pin->hdir;
15556 +       return NULL;
15557 +}
15558 +
15559 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
15560 +{
15561 +       if (pin)
15562 +               pin->dentry = dentry;
15563 +}
15564 +
15565 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
15566 +                                          unsigned char lflag)
15567 +{
15568 +       if (pin) {
15569 +               if (lflag)
15570 +                       au_fset_pin(pin->flags, DI_LOCKED);
15571 +               else
15572 +                       au_fclr_pin(pin->flags, DI_LOCKED);
15573 +       }
15574 +}
15575 +
15576 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
15577 +{
15578 +       if (pin) {
15579 +               dput(pin->parent);
15580 +               pin->parent = dget(parent);
15581 +       }
15582 +}
15583 +
15584 +/* ---------------------------------------------------------------------- */
15585 +
15586 +struct au_branch;
15587 +#ifdef CONFIG_AUFS_HNOTIFY
15588 +struct au_hnotify_op {
15589 +       void (*ctl)(struct au_hinode *hinode, int do_set);
15590 +       int (*alloc)(struct au_hinode *hinode);
15591 +
15592 +       /*
15593 +        * if it returns true, the the caller should free hinode->hi_notify,
15594 +        * otherwise ->free() frees it.
15595 +        */
15596 +       int (*free)(struct au_hinode *hinode,
15597 +                   struct au_hnotify *hn) __must_check;
15598 +
15599 +       void (*fin)(void);
15600 +       int (*init)(void);
15601 +
15602 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
15603 +       void (*fin_br)(struct au_branch *br);
15604 +       int (*init_br)(struct au_branch *br, int perm);
15605 +};
15606 +
15607 +/* hnotify.c */
15608 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
15609 +void au_hn_free(struct au_hinode *hinode);
15610 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
15611 +void au_hn_reset(struct inode *inode, unsigned int flags);
15612 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15613 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
15614 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
15615 +int au_hnotify_init_br(struct au_branch *br, int perm);
15616 +void au_hnotify_fin_br(struct au_branch *br);
15617 +int __init au_hnotify_init(void);
15618 +void au_hnotify_fin(void);
15619 +
15620 +/* hfsnotify.c */
15621 +extern const struct au_hnotify_op au_hnotify_op;
15622 +
15623 +static inline
15624 +void au_hn_init(struct au_hinode *hinode)
15625 +{
15626 +       hinode->hi_notify = NULL;
15627 +}
15628 +
15629 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
15630 +{
15631 +       return hinode->hi_notify;
15632 +}
15633 +
15634 +#else
15635 +static inline
15636 +int au_hn_alloc(struct au_hinode *hinode __maybe_unused,
15637 +               struct inode *inode __maybe_unused)
15638 +{
15639 +       return -EOPNOTSUPP;
15640 +}
15641 +
15642 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
15643 +{
15644 +       return NULL;
15645 +}
15646 +
15647 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
15648 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
15649 +          int do_set __maybe_unused)
15650 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
15651 +          unsigned int flags __maybe_unused)
15652 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
15653 +          struct au_branch *br __maybe_unused,
15654 +          int perm __maybe_unused)
15655 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
15656 +          int perm __maybe_unused)
15657 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
15658 +AuStubInt0(__init au_hnotify_init, void)
15659 +AuStubVoid(au_hnotify_fin, void)
15660 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
15661 +#endif /* CONFIG_AUFS_HNOTIFY */
15662 +
15663 +static inline void au_hn_suspend(struct au_hinode *hdir)
15664 +{
15665 +       au_hn_ctl(hdir, /*do_set*/0);
15666 +}
15667 +
15668 +static inline void au_hn_resume(struct au_hinode *hdir)
15669 +{
15670 +       au_hn_ctl(hdir, /*do_set*/1);
15671 +}
15672 +
15673 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
15674 +{
15675 +       mutex_lock(&hdir->hi_inode->i_mutex);
15676 +       au_hn_suspend(hdir);
15677 +}
15678 +
15679 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
15680 +                                         unsigned int sc __maybe_unused)
15681 +{
15682 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
15683 +       au_hn_suspend(hdir);
15684 +}
15685 +
15686 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
15687 +{
15688 +       au_hn_resume(hdir);
15689 +       mutex_unlock(&hdir->hi_inode->i_mutex);
15690 +}
15691 +
15692 +#endif /* __KERNEL__ */
15693 +#endif /* __AUFS_INODE_H__ */
15694 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
15695 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
15696 +++ linux/fs/aufs/ioctl.c       2014-06-24 00:14:06.786052822 +0200
15697 @@ -0,0 +1,201 @@
15698 +/*
15699 + * Copyright (C) 2005-2014 Junjiro R. Okajima
15700 + *
15701 + * This program, aufs is free software; you can redistribute it and/or modify
15702 + * it under the terms of the GNU General Public License as published by
15703 + * the Free Software Foundation; either version 2 of the License, or
15704 + * (at your option) any later version.
15705 + *
15706 + * This program is distributed in the hope that it will be useful,
15707 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15708 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15709 + * GNU General Public License for more details.
15710 + *
15711 + * You should have received a copy of the GNU General Public License
15712 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15713 + */
15714 +
15715 +/*
15716 + * ioctl
15717 + * plink-management and readdir in userspace.
15718 + * assist the pathconf(3) wrapper library.
15719 + * move-down
15720 + */
15721 +
15722 +#include <linux/compat.h>
15723 +#include <linux/file.h>
15724 +#include "aufs.h"
15725 +
15726 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
15727 +{
15728 +       int err, fd;
15729 +       aufs_bindex_t wbi, bindex, bend;
15730 +       struct file *h_file;
15731 +       struct super_block *sb;
15732 +       struct dentry *root;
15733 +       struct au_branch *br;
15734 +       struct aufs_wbr_fd wbrfd = {
15735 +               .oflags = au_dir_roflags,
15736 +               .brid   = -1
15737 +       };
15738 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
15739 +               | O_NOATIME | O_CLOEXEC;
15740 +
15741 +       AuDebugOn(wbrfd.oflags & ~valid);
15742 +
15743 +       if (arg) {
15744 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
15745 +               if (unlikely(err)) {
15746 +                       err = -EFAULT;
15747 +                       goto out;
15748 +               }
15749 +
15750 +               err = -EINVAL;
15751 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
15752 +               wbrfd.oflags |= au_dir_roflags;
15753 +               AuDbg("0%o\n", wbrfd.oflags);
15754 +               if (unlikely(wbrfd.oflags & ~valid))
15755 +                       goto out;
15756 +       }
15757 +
15758 +       fd = get_unused_fd();
15759 +       err = fd;
15760 +       if (unlikely(fd < 0))
15761 +               goto out;
15762 +
15763 +       h_file = ERR_PTR(-EINVAL);
15764 +       wbi = 0;
15765 +       br = NULL;
15766 +       sb = path->dentry->d_sb;
15767 +       root = sb->s_root;
15768 +       aufs_read_lock(root, AuLock_IR);
15769 +       bend = au_sbend(sb);
15770 +       if (wbrfd.brid >= 0) {
15771 +               wbi = au_br_index(sb, wbrfd.brid);
15772 +               if (unlikely(wbi < 0 || wbi > bend))
15773 +                       goto out_unlock;
15774 +       }
15775 +
15776 +       h_file = ERR_PTR(-ENOENT);
15777 +       br = au_sbr(sb, wbi);
15778 +       if (!au_br_writable(br->br_perm)) {
15779 +               if (arg)
15780 +                       goto out_unlock;
15781 +
15782 +               bindex = wbi + 1;
15783 +               wbi = -1;
15784 +               for (; bindex <= bend; bindex++) {
15785 +                       br = au_sbr(sb, bindex);
15786 +                       if (au_br_writable(br->br_perm)) {
15787 +                               wbi = bindex;
15788 +                               br = au_sbr(sb, wbi);
15789 +                               break;
15790 +                       }
15791 +               }
15792 +       }
15793 +       AuDbg("wbi %d\n", wbi);
15794 +       if (wbi >= 0)
15795 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
15796 +                                  /*force_wr*/0);
15797 +
15798 +out_unlock:
15799 +       aufs_read_unlock(root, AuLock_IR);
15800 +       err = PTR_ERR(h_file);
15801 +       if (IS_ERR(h_file))
15802 +               goto out_fd;
15803 +
15804 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
15805 +       fd_install(fd, h_file);
15806 +       err = fd;
15807 +       goto out; /* success */
15808 +
15809 +out_fd:
15810 +       put_unused_fd(fd);
15811 +out:
15812 +       AuTraceErr(err);
15813 +       return err;
15814 +}
15815 +
15816 +/* ---------------------------------------------------------------------- */
15817 +
15818 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
15819 +{
15820 +       long err;
15821 +
15822 +       switch (cmd) {
15823 +       case AUFS_CTL_RDU:
15824 +       case AUFS_CTL_RDU_INO:
15825 +               err = au_rdu_ioctl(file, cmd, arg);
15826 +               break;
15827 +
15828 +       case AUFS_CTL_WBR_FD:
15829 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15830 +               break;
15831 +
15832 +       case AUFS_CTL_IBUSY:
15833 +               err = au_ibusy_ioctl(file, arg);
15834 +               break;
15835 +
15836 +       default:
15837 +               /* do not call the lower */
15838 +               AuDbg("0x%x\n", cmd);
15839 +               err = -ENOTTY;
15840 +       }
15841 +
15842 +       AuTraceErr(err);
15843 +       return err;
15844 +}
15845 +
15846 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
15847 +{
15848 +       long err;
15849 +
15850 +       switch (cmd) {
15851 +       case AUFS_CTL_MVDOWN:
15852 +               err = au_mvdown(file->f_dentry, (void __user *)arg);
15853 +               break;
15854 +
15855 +       case AUFS_CTL_WBR_FD:
15856 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
15857 +               break;
15858 +
15859 +       default:
15860 +               /* do not call the lower */
15861 +               AuDbg("0x%x\n", cmd);
15862 +               err = -ENOTTY;
15863 +       }
15864 +
15865 +       AuTraceErr(err);
15866 +       return err;
15867 +}
15868 +
15869 +#ifdef CONFIG_COMPAT
15870 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15871 +                          unsigned long arg)
15872 +{
15873 +       long err;
15874 +
15875 +       switch (cmd) {
15876 +       case AUFS_CTL_RDU:
15877 +       case AUFS_CTL_RDU_INO:
15878 +               err = au_rdu_compat_ioctl(file, cmd, arg);
15879 +               break;
15880 +
15881 +       case AUFS_CTL_IBUSY:
15882 +               err = au_ibusy_compat_ioctl(file, arg);
15883 +               break;
15884 +
15885 +       default:
15886 +               err = aufs_ioctl_dir(file, cmd, arg);
15887 +       }
15888 +
15889 +       AuTraceErr(err);
15890 +       return err;
15891 +}
15892 +
15893 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15894 +                             unsigned long arg)
15895 +{
15896 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
15897 +}
15898 +#endif
15899 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
15900 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
15901 +++ linux/fs/aufs/i_op_add.c    2014-06-24 00:14:06.782719306 +0200
15902 @@ -0,0 +1,881 @@
15903 +/*
15904 + * Copyright (C) 2005-2014 Junjiro R. Okajima
15905 + *
15906 + * This program, aufs is free software; you can redistribute it and/or modify
15907 + * it under the terms of the GNU General Public License as published by
15908 + * the Free Software Foundation; either version 2 of the License, or
15909 + * (at your option) any later version.
15910 + *
15911 + * This program is distributed in the hope that it will be useful,
15912 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15913 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15914 + * GNU General Public License for more details.
15915 + *
15916 + * You should have received a copy of the GNU General Public License
15917 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15918 + */
15919 +
15920 +/*
15921 + * inode operations (add entry)
15922 + */
15923 +
15924 +#include "aufs.h"
15925 +
15926 +/*
15927 + * final procedure of adding a new entry, except link(2).
15928 + * remove whiteout, instantiate, copyup the parent dir's times and size
15929 + * and update version.
15930 + * if it failed, re-create the removed whiteout.
15931 + */
15932 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
15933 +                 struct dentry *wh_dentry, struct dentry *dentry)
15934 +{
15935 +       int err, rerr;
15936 +       aufs_bindex_t bwh;
15937 +       struct path h_path;
15938 +       struct inode *inode, *h_dir;
15939 +       struct dentry *wh;
15940 +
15941 +       bwh = -1;
15942 +       if (wh_dentry) {
15943 +               h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
15944 +               IMustLock(h_dir);
15945 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
15946 +               bwh = au_dbwh(dentry);
15947 +               h_path.dentry = wh_dentry;
15948 +               h_path.mnt = au_sbr_mnt(dir->i_sb, bindex);
15949 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
15950 +                                         dentry);
15951 +               if (unlikely(err))
15952 +                       goto out;
15953 +       }
15954 +
15955 +       inode = au_new_inode(dentry, /*must_new*/1);
15956 +       if (!IS_ERR(inode)) {
15957 +               d_instantiate(dentry, inode);
15958 +               dir = dentry->d_parent->d_inode; /* dir inode is locked */
15959 +               IMustLock(dir);
15960 +               if (au_ibstart(dir) == au_dbstart(dentry))
15961 +                       au_cpup_attr_timesizes(dir);
15962 +               dir->i_version++;
15963 +               return 0; /* success */
15964 +       }
15965 +
15966 +       err = PTR_ERR(inode);
15967 +       if (!wh_dentry)
15968 +               goto out;
15969 +
15970 +       /* revert */
15971 +       /* dir inode is locked */
15972 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
15973 +       rerr = PTR_ERR(wh);
15974 +       if (IS_ERR(wh)) {
15975 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
15976 +                       dentry, err, rerr);
15977 +               err = -EIO;
15978 +       } else
15979 +               dput(wh);
15980 +
15981 +out:
15982 +       return err;
15983 +}
15984 +
15985 +static int au_d_may_add(struct dentry *dentry)
15986 +{
15987 +       int err;
15988 +
15989 +       err = 0;
15990 +       if (unlikely(d_unhashed(dentry)))
15991 +               err = -ENOENT;
15992 +       if (unlikely(dentry->d_inode))
15993 +               err = -EEXIST;
15994 +       return err;
15995 +}
15996 +
15997 +/*
15998 + * simple tests for the adding inode operations.
15999 + * following the checks in vfs, plus the parent-child relationship.
16000 + */
16001 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16002 +              struct dentry *h_parent, int isdir)
16003 +{
16004 +       int err;
16005 +       umode_t h_mode;
16006 +       struct dentry *h_dentry;
16007 +       struct inode *h_inode;
16008 +
16009 +       err = -ENAMETOOLONG;
16010 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
16011 +               goto out;
16012 +
16013 +       h_dentry = au_h_dptr(dentry, bindex);
16014 +       h_inode = h_dentry->d_inode;
16015 +       if (!dentry->d_inode) {
16016 +               err = -EEXIST;
16017 +               if (unlikely(h_inode))
16018 +                       goto out;
16019 +       } else {
16020 +               /* rename(2) case */
16021 +               err = -EIO;
16022 +               if (unlikely(!h_inode || !h_inode->i_nlink))
16023 +                       goto out;
16024 +
16025 +               h_mode = h_inode->i_mode;
16026 +               if (!isdir) {
16027 +                       err = -EISDIR;
16028 +                       if (unlikely(S_ISDIR(h_mode)))
16029 +                               goto out;
16030 +               } else if (unlikely(!S_ISDIR(h_mode))) {
16031 +                       err = -ENOTDIR;
16032 +                       goto out;
16033 +               }
16034 +       }
16035 +
16036 +       err = 0;
16037 +       /* expected parent dir is locked */
16038 +       if (unlikely(h_parent != h_dentry->d_parent))
16039 +               err = -EIO;
16040 +
16041 +out:
16042 +       AuTraceErr(err);
16043 +       return err;
16044 +}
16045 +
16046 +/*
16047 + * initial procedure of adding a new entry.
16048 + * prepare writable branch and the parent dir, lock it,
16049 + * and lookup whiteout for the new entry.
16050 + */
16051 +static struct dentry*
16052 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
16053 +                 struct dentry *src_dentry, struct au_pin *pin,
16054 +                 struct au_wr_dir_args *wr_dir_args)
16055 +{
16056 +       struct dentry *wh_dentry, *h_parent;
16057 +       struct super_block *sb;
16058 +       struct au_branch *br;
16059 +       int err;
16060 +       unsigned int udba;
16061 +       aufs_bindex_t bcpup;
16062 +
16063 +       AuDbg("%pd\n", dentry);
16064 +
16065 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
16066 +       bcpup = err;
16067 +       wh_dentry = ERR_PTR(err);
16068 +       if (unlikely(err < 0))
16069 +               goto out;
16070 +
16071 +       sb = dentry->d_sb;
16072 +       udba = au_opt_udba(sb);
16073 +       err = au_pin(pin, dentry, bcpup, udba,
16074 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
16075 +       wh_dentry = ERR_PTR(err);
16076 +       if (unlikely(err))
16077 +               goto out;
16078 +
16079 +       h_parent = au_pinned_h_parent(pin);
16080 +       if (udba != AuOpt_UDBA_NONE
16081 +           && au_dbstart(dentry) == bcpup)
16082 +               err = au_may_add(dentry, bcpup, h_parent,
16083 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
16084 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
16085 +               err = -ENAMETOOLONG;
16086 +       wh_dentry = ERR_PTR(err);
16087 +       if (unlikely(err))
16088 +               goto out_unpin;
16089 +
16090 +       br = au_sbr(sb, bcpup);
16091 +       if (dt) {
16092 +               struct path tmp = {
16093 +                       .dentry = h_parent,
16094 +                       .mnt    = au_br_mnt(br)
16095 +               };
16096 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
16097 +       }
16098 +
16099 +       wh_dentry = NULL;
16100 +       if (bcpup != au_dbwh(dentry))
16101 +               goto out; /* success */
16102 +
16103 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
16104 +
16105 +out_unpin:
16106 +       if (IS_ERR(wh_dentry))
16107 +               au_unpin(pin);
16108 +out:
16109 +       return wh_dentry;
16110 +}
16111 +
16112 +/* ---------------------------------------------------------------------- */
16113 +
16114 +enum { Mknod, Symlink, Creat };
16115 +struct simple_arg {
16116 +       int type;
16117 +       union {
16118 +               struct {
16119 +                       umode_t mode;
16120 +                       bool want_excl;
16121 +               } c;
16122 +               struct {
16123 +                       const char *symname;
16124 +               } s;
16125 +               struct {
16126 +                       umode_t mode;
16127 +                       dev_t dev;
16128 +               } m;
16129 +       } u;
16130 +};
16131 +
16132 +static int add_simple(struct inode *dir, struct dentry *dentry,
16133 +                     struct simple_arg *arg)
16134 +{
16135 +       int err;
16136 +       aufs_bindex_t bstart;
16137 +       unsigned char created;
16138 +       struct dentry *wh_dentry, *parent;
16139 +       struct inode *h_dir;
16140 +       /* to reuduce stack size */
16141 +       struct {
16142 +               struct au_dtime dt;
16143 +               struct au_pin pin;
16144 +               struct path h_path;
16145 +               struct au_wr_dir_args wr_dir_args;
16146 +       } *a;
16147 +
16148 +       AuDbg("%pd\n", dentry);
16149 +       IMustLock(dir);
16150 +
16151 +       err = -ENOMEM;
16152 +       a = kmalloc(sizeof(*a), GFP_NOFS);
16153 +       if (unlikely(!a))
16154 +               goto out;
16155 +       a->wr_dir_args.force_btgt = -1;
16156 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
16157 +
16158 +       parent = dentry->d_parent; /* dir inode is locked */
16159 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
16160 +       if (unlikely(err))
16161 +               goto out_free;
16162 +       err = au_d_may_add(dentry);
16163 +       if (unlikely(err))
16164 +               goto out_unlock;
16165 +       di_write_lock_parent(parent);
16166 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
16167 +                                     &a->pin, &a->wr_dir_args);
16168 +       err = PTR_ERR(wh_dentry);
16169 +       if (IS_ERR(wh_dentry))
16170 +               goto out_parent;
16171 +
16172 +       bstart = au_dbstart(dentry);
16173 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
16174 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
16175 +       h_dir = au_pinned_h_dir(&a->pin);
16176 +       switch (arg->type) {
16177 +       case Creat:
16178 +               err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
16179 +                                  arg->u.c.want_excl);
16180 +               break;
16181 +       case Symlink:
16182 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
16183 +               break;
16184 +       case Mknod:
16185 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
16186 +                                 arg->u.m.dev);
16187 +               break;
16188 +       default:
16189 +               BUG();
16190 +       }
16191 +       created = !err;
16192 +       if (!err)
16193 +               err = epilog(dir, bstart, wh_dentry, dentry);
16194 +
16195 +       /* revert */
16196 +       if (unlikely(created && err && a->h_path.dentry->d_inode)) {
16197 +               int rerr;
16198 +               /* no delegation since it is just created */
16199 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
16200 +                                   /*force*/0);
16201 +               if (rerr) {
16202 +                       AuIOErr("%pd revert failure(%d, %d)\n",
16203 +                               dentry, err, rerr);
16204 +                       err = -EIO;
16205 +               }
16206 +               au_dtime_revert(&a->dt);
16207 +       }
16208 +
16209 +       au_unpin(&a->pin);
16210 +       dput(wh_dentry);
16211 +
16212 +out_parent:
16213 +       di_write_unlock(parent);
16214 +out_unlock:
16215 +       if (unlikely(err)) {
16216 +               au_update_dbstart(dentry);
16217 +               d_drop(dentry);
16218 +       }
16219 +       aufs_read_unlock(dentry, AuLock_DW);
16220 +out_free:
16221 +       kfree(a);
16222 +out:
16223 +       return err;
16224 +}
16225 +
16226 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16227 +              dev_t dev)
16228 +{
16229 +       struct simple_arg arg = {
16230 +               .type = Mknod,
16231 +               .u.m = {
16232 +                       .mode   = mode,
16233 +                       .dev    = dev
16234 +               }
16235 +       };
16236 +       return add_simple(dir, dentry, &arg);
16237 +}
16238 +
16239 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
16240 +{
16241 +       struct simple_arg arg = {
16242 +               .type = Symlink,
16243 +               .u.s.symname = symname
16244 +       };
16245 +       return add_simple(dir, dentry, &arg);
16246 +}
16247 +
16248 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
16249 +               bool want_excl)
16250 +{
16251 +       struct simple_arg arg = {
16252 +               .type = Creat,
16253 +               .u.c = {
16254 +                       .mode           = mode,
16255 +                       .want_excl      = want_excl
16256 +               }
16257 +       };
16258 +       return add_simple(dir, dentry, &arg);
16259 +}
16260 +
16261 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
16262 +{
16263 +       int err;
16264 +       aufs_bindex_t bindex;
16265 +       struct super_block *sb;
16266 +       struct dentry *parent, *h_parent, *h_dentry;
16267 +       struct inode *h_dir, *inode;
16268 +       struct vfsmount *h_mnt;
16269 +       struct au_wr_dir_args wr_dir_args = {
16270 +               .force_btgt     = -1,
16271 +               .flags          = AuWrDir_TMPFILE
16272 +       };
16273 +
16274 +       /* copy-up may happen */
16275 +       mutex_lock(&dir->i_mutex);
16276 +
16277 +       sb = dir->i_sb;
16278 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16279 +       if (unlikely(err))
16280 +               goto out;
16281 +
16282 +       err = au_di_init(dentry);
16283 +       if (unlikely(err))
16284 +               goto out_si;
16285 +
16286 +       err = -EBUSY;
16287 +       parent = d_find_any_alias(dir);
16288 +       AuDebugOn(!parent);
16289 +       di_write_lock_parent(parent);
16290 +       if (unlikely(parent->d_inode != dir))
16291 +               goto out_parent;
16292 +
16293 +       err = au_digen_test(parent, au_sigen(sb));
16294 +       if (unlikely(err))
16295 +               goto out_parent;
16296 +
16297 +       bindex = au_dbstart(parent);
16298 +       au_set_dbstart(dentry, bindex);
16299 +       au_set_dbend(dentry, bindex);
16300 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
16301 +       bindex = err;
16302 +       if (unlikely(err < 0))
16303 +               goto out_parent;
16304 +
16305 +       err = -EOPNOTSUPP;
16306 +       h_dir = au_h_iptr(dir, bindex);
16307 +       if (unlikely(!h_dir->i_op->tmpfile))
16308 +               goto out_parent;
16309 +
16310 +       h_mnt = au_sbr_mnt(sb, bindex);
16311 +       err = vfsub_mnt_want_write(h_mnt);
16312 +       if (unlikely(err))
16313 +               goto out_parent;
16314 +
16315 +       h_parent = au_h_dptr(parent, bindex);
16316 +       err = inode_permission(h_parent->d_inode, MAY_WRITE | MAY_EXEC);
16317 +       if (unlikely(err))
16318 +               goto out_mnt;
16319 +
16320 +       err = -ENOMEM;
16321 +       h_dentry = d_alloc(h_parent, &dentry->d_name);
16322 +       if (unlikely(!h_dentry))
16323 +               goto out_mnt;
16324 +
16325 +       err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
16326 +       if (unlikely(err))
16327 +               goto out_dentry;
16328 +
16329 +       au_set_dbstart(dentry, bindex);
16330 +       au_set_dbend(dentry, bindex);
16331 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
16332 +       inode = au_new_inode(dentry, /*must_new*/1);
16333 +       if (IS_ERR(inode)) {
16334 +               err = PTR_ERR(inode);
16335 +               au_set_h_dptr(dentry, bindex, NULL);
16336 +               au_set_dbstart(dentry, -1);
16337 +               au_set_dbend(dentry, -1);
16338 +       } else {
16339 +               if (!inode->i_nlink)
16340 +                       set_nlink(inode, 1);
16341 +               d_tmpfile(dentry, inode);
16342 +               au_di(dentry)->di_tmpfile = 1;
16343 +
16344 +               /* update without i_mutex */
16345 +               if (au_ibstart(dir) == au_dbstart(dentry))
16346 +                       au_cpup_attr_timesizes(dir);
16347 +       }
16348 +
16349 +out_dentry:
16350 +       dput(h_dentry);
16351 +out_mnt:
16352 +       vfsub_mnt_drop_write(h_mnt);
16353 +out_parent:
16354 +       di_write_unlock(parent);
16355 +       dput(parent);
16356 +       di_write_unlock(dentry);
16357 +       if (!err)
16358 +#if 0
16359 +               /* verbose coding for lock class name */
16360 +               au_rw_class(&au_di(dentry)->di_rwsem,
16361 +                           au_lc_key + AuLcNonDir_DIINFO);
16362 +#else
16363 +               ;
16364 +#endif
16365 +       else {
16366 +               au_di_fin(dentry);
16367 +               dentry->d_fsdata = NULL;
16368 +       }
16369 +out_si:
16370 +       si_read_unlock(sb);
16371 +out:
16372 +       mutex_unlock(&dir->i_mutex);
16373 +       return err;
16374 +}
16375 +
16376 +/* ---------------------------------------------------------------------- */
16377 +
16378 +struct au_link_args {
16379 +       aufs_bindex_t bdst, bsrc;
16380 +       struct au_pin pin;
16381 +       struct path h_path;
16382 +       struct dentry *src_parent, *parent;
16383 +};
16384 +
16385 +static int au_cpup_before_link(struct dentry *src_dentry,
16386 +                              struct au_link_args *a)
16387 +{
16388 +       int err;
16389 +       struct dentry *h_src_dentry;
16390 +       struct au_cp_generic cpg = {
16391 +               .dentry = src_dentry,
16392 +               .bdst   = a->bdst,
16393 +               .bsrc   = a->bsrc,
16394 +               .len    = -1,
16395 +               .pin    = &a->pin,
16396 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
16397 +       };
16398 +
16399 +       di_read_lock_parent(a->src_parent, AuLock_IR);
16400 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
16401 +       if (unlikely(err))
16402 +               goto out;
16403 +
16404 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
16405 +       err = au_pin(&a->pin, src_dentry, a->bdst,
16406 +                    au_opt_udba(src_dentry->d_sb),
16407 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
16408 +       if (unlikely(err))
16409 +               goto out;
16410 +
16411 +       err = au_sio_cpup_simple(&cpg);
16412 +       au_unpin(&a->pin);
16413 +
16414 +out:
16415 +       di_read_unlock(a->src_parent, AuLock_IR);
16416 +       return err;
16417 +}
16418 +
16419 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
16420 +                          struct au_link_args *a)
16421 +{
16422 +       int err;
16423 +       unsigned char plink;
16424 +       aufs_bindex_t bend;
16425 +       struct dentry *h_src_dentry;
16426 +       struct inode *h_inode, *inode, *delegated;
16427 +       struct super_block *sb;
16428 +       struct file *h_file;
16429 +
16430 +       plink = 0;
16431 +       h_inode = NULL;
16432 +       sb = src_dentry->d_sb;
16433 +       inode = src_dentry->d_inode;
16434 +       if (au_ibstart(inode) <= a->bdst)
16435 +               h_inode = au_h_iptr(inode, a->bdst);
16436 +       if (!h_inode || !h_inode->i_nlink) {
16437 +               /* copyup src_dentry as the name of dentry. */
16438 +               bend = au_dbend(dentry);
16439 +               if (bend < a->bsrc)
16440 +                       au_set_dbend(dentry, a->bsrc);
16441 +               au_set_h_dptr(dentry, a->bsrc,
16442 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
16443 +               dget(a->h_path.dentry);
16444 +               au_set_h_dptr(dentry, a->bdst, NULL);
16445 +               dentry->d_inode = src_dentry->d_inode; /* tmp */
16446 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
16447 +               if (IS_ERR(h_file))
16448 +                       err = PTR_ERR(h_file);
16449 +               else {
16450 +                       struct au_cp_generic cpg = {
16451 +                               .dentry = dentry,
16452 +                               .bdst   = a->bdst,
16453 +                               .bsrc   = -1,
16454 +                               .len    = -1,
16455 +                               .pin    = &a->pin,
16456 +                               .flags  = AuCpup_KEEPLINO
16457 +                       };
16458 +                       err = au_sio_cpup_simple(&cpg);
16459 +                       au_h_open_post(dentry, a->bsrc, h_file);
16460 +                       if (!err) {
16461 +                               dput(a->h_path.dentry);
16462 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
16463 +                       } else
16464 +                               au_set_h_dptr(dentry, a->bdst,
16465 +                                             a->h_path.dentry);
16466 +               }
16467 +               dentry->d_inode = NULL; /* restore */
16468 +               au_set_h_dptr(dentry, a->bsrc, NULL);
16469 +               au_set_dbend(dentry, bend);
16470 +       } else {
16471 +               /* the inode of src_dentry already exists on a.bdst branch */
16472 +               h_src_dentry = d_find_alias(h_inode);
16473 +               if (!h_src_dentry && au_plink_test(inode)) {
16474 +                       plink = 1;
16475 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
16476 +                       err = PTR_ERR(h_src_dentry);
16477 +                       if (IS_ERR(h_src_dentry))
16478 +                               goto out;
16479 +
16480 +                       if (unlikely(!h_src_dentry->d_inode)) {
16481 +                               dput(h_src_dentry);
16482 +                               h_src_dentry = NULL;
16483 +                       }
16484 +
16485 +               }
16486 +               if (h_src_dentry) {
16487 +                       delegated = NULL;
16488 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
16489 +                                        &a->h_path, &delegated);
16490 +                       if (unlikely(err == -EWOULDBLOCK)) {
16491 +                               pr_warn("cannot retry for NFSv4 delegation"
16492 +                                       " for an internal link\n");
16493 +                               iput(delegated);
16494 +                       }
16495 +                       dput(h_src_dentry);
16496 +               } else {
16497 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
16498 +                               h_inode->i_ino, a->bdst);
16499 +                       err = -EIO;
16500 +               }
16501 +       }
16502 +
16503 +       if (!err && !plink)
16504 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
16505 +
16506 +out:
16507 +       AuTraceErr(err);
16508 +       return err;
16509 +}
16510 +
16511 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
16512 +             struct dentry *dentry)
16513 +{
16514 +       int err, rerr;
16515 +       struct au_dtime dt;
16516 +       struct au_link_args *a;
16517 +       struct dentry *wh_dentry, *h_src_dentry;
16518 +       struct inode *inode, *delegated;
16519 +       struct super_block *sb;
16520 +       struct au_wr_dir_args wr_dir_args = {
16521 +               /* .force_btgt  = -1, */
16522 +               .flags          = AuWrDir_ADD_ENTRY
16523 +       };
16524 +
16525 +       IMustLock(dir);
16526 +       inode = src_dentry->d_inode;
16527 +       IMustLock(inode);
16528 +
16529 +       err = -ENOMEM;
16530 +       a = kzalloc(sizeof(*a), GFP_NOFS);
16531 +       if (unlikely(!a))
16532 +               goto out;
16533 +
16534 +       a->parent = dentry->d_parent; /* dir inode is locked */
16535 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
16536 +                                       AuLock_NOPLM | AuLock_GEN);
16537 +       if (unlikely(err))
16538 +               goto out_kfree;
16539 +       err = au_d_linkable(src_dentry);
16540 +       if (unlikely(err))
16541 +               goto out_unlock;
16542 +       err = au_d_may_add(dentry);
16543 +       if (unlikely(err))
16544 +               goto out_unlock;
16545 +
16546 +       a->src_parent = dget_parent(src_dentry);
16547 +       wr_dir_args.force_btgt = au_ibstart(inode);
16548 +
16549 +       di_write_lock_parent(a->parent);
16550 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
16551 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
16552 +                                     &wr_dir_args);
16553 +       err = PTR_ERR(wh_dentry);
16554 +       if (IS_ERR(wh_dentry))
16555 +               goto out_parent;
16556 +
16557 +       err = 0;
16558 +       sb = dentry->d_sb;
16559 +       a->bdst = au_dbstart(dentry);
16560 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
16561 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
16562 +       a->bsrc = au_ibstart(inode);
16563 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
16564 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
16565 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
16566 +       if (!h_src_dentry) {
16567 +               a->bsrc = au_dbstart(src_dentry);
16568 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
16569 +               AuDebugOn(!h_src_dentry);
16570 +       } else if (IS_ERR(h_src_dentry)) {
16571 +               err = PTR_ERR(h_src_dentry);
16572 +               goto out_parent;
16573 +       }
16574 +
16575 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
16576 +               if (a->bdst < a->bsrc
16577 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
16578 +                       err = au_cpup_or_link(src_dentry, dentry, a);
16579 +               else {
16580 +                       delegated = NULL;
16581 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
16582 +                                        &a->h_path, &delegated);
16583 +                       if (unlikely(err == -EWOULDBLOCK)) {
16584 +                               pr_warn("cannot retry for NFSv4 delegation"
16585 +                                       " for an internal link\n");
16586 +                               iput(delegated);
16587 +                       }
16588 +               }
16589 +               dput(h_src_dentry);
16590 +       } else {
16591 +               /*
16592 +                * copyup src_dentry to the branch we process,
16593 +                * and then link(2) to it.
16594 +                */
16595 +               dput(h_src_dentry);
16596 +               if (a->bdst < a->bsrc
16597 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
16598 +                       au_unpin(&a->pin);
16599 +                       di_write_unlock(a->parent);
16600 +                       err = au_cpup_before_link(src_dentry, a);
16601 +                       di_write_lock_parent(a->parent);
16602 +                       if (!err)
16603 +                               err = au_pin(&a->pin, dentry, a->bdst,
16604 +                                            au_opt_udba(sb),
16605 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
16606 +                       if (unlikely(err))
16607 +                               goto out_wh;
16608 +               }
16609 +               if (!err) {
16610 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
16611 +                       err = -ENOENT;
16612 +                       if (h_src_dentry && h_src_dentry->d_inode) {
16613 +                               delegated = NULL;
16614 +                               err = vfsub_link(h_src_dentry,
16615 +                                                au_pinned_h_dir(&a->pin),
16616 +                                                &a->h_path, &delegated);
16617 +                               if (unlikely(err == -EWOULDBLOCK)) {
16618 +                                       pr_warn("cannot retry"
16619 +                                               " for NFSv4 delegation"
16620 +                                               " for an internal link\n");
16621 +                                       iput(delegated);
16622 +                               }
16623 +                       }
16624 +               }
16625 +       }
16626 +       if (unlikely(err))
16627 +               goto out_unpin;
16628 +
16629 +       if (wh_dentry) {
16630 +               a->h_path.dentry = wh_dentry;
16631 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
16632 +                                         dentry);
16633 +               if (unlikely(err))
16634 +                       goto out_revert;
16635 +       }
16636 +
16637 +       dir->i_version++;
16638 +       if (au_ibstart(dir) == au_dbstart(dentry))
16639 +               au_cpup_attr_timesizes(dir);
16640 +       inc_nlink(inode);
16641 +       inode->i_ctime = dir->i_ctime;
16642 +       d_instantiate(dentry, au_igrab(inode));
16643 +       if (d_unhashed(a->h_path.dentry))
16644 +               /* some filesystem calls d_drop() */
16645 +               d_drop(dentry);
16646 +       goto out_unpin; /* success */
16647 +
16648 +out_revert:
16649 +       /* no delegation since it is just created */
16650 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
16651 +                           /*delegated*/NULL, /*force*/0);
16652 +       if (unlikely(rerr)) {
16653 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
16654 +               err = -EIO;
16655 +       }
16656 +       au_dtime_revert(&dt);
16657 +out_unpin:
16658 +       au_unpin(&a->pin);
16659 +out_wh:
16660 +       dput(wh_dentry);
16661 +out_parent:
16662 +       di_write_unlock(a->parent);
16663 +       dput(a->src_parent);
16664 +out_unlock:
16665 +       if (unlikely(err)) {
16666 +               au_update_dbstart(dentry);
16667 +               d_drop(dentry);
16668 +       }
16669 +       aufs_read_and_write_unlock2(dentry, src_dentry);
16670 +out_kfree:
16671 +       kfree(a);
16672 +out:
16673 +       AuTraceErr(err);
16674 +       return err;
16675 +}
16676 +
16677 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
16678 +{
16679 +       int err, rerr;
16680 +       aufs_bindex_t bindex;
16681 +       unsigned char diropq;
16682 +       struct path h_path;
16683 +       struct dentry *wh_dentry, *parent, *opq_dentry;
16684 +       struct mutex *h_mtx;
16685 +       struct super_block *sb;
16686 +       struct {
16687 +               struct au_pin pin;
16688 +               struct au_dtime dt;
16689 +       } *a; /* reduce the stack usage */
16690 +       struct au_wr_dir_args wr_dir_args = {
16691 +               .force_btgt     = -1,
16692 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
16693 +       };
16694 +
16695 +       IMustLock(dir);
16696 +
16697 +       err = -ENOMEM;
16698 +       a = kmalloc(sizeof(*a), GFP_NOFS);
16699 +       if (unlikely(!a))
16700 +               goto out;
16701 +
16702 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
16703 +       if (unlikely(err))
16704 +               goto out_free;
16705 +       err = au_d_may_add(dentry);
16706 +       if (unlikely(err))
16707 +               goto out_unlock;
16708 +
16709 +       parent = dentry->d_parent; /* dir inode is locked */
16710 +       di_write_lock_parent(parent);
16711 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
16712 +                                     &a->pin, &wr_dir_args);
16713 +       err = PTR_ERR(wh_dentry);
16714 +       if (IS_ERR(wh_dentry))
16715 +               goto out_parent;
16716 +
16717 +       sb = dentry->d_sb;
16718 +       bindex = au_dbstart(dentry);
16719 +       h_path.dentry = au_h_dptr(dentry, bindex);
16720 +       h_path.mnt = au_sbr_mnt(sb, bindex);
16721 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
16722 +       if (unlikely(err))
16723 +               goto out_unpin;
16724 +
16725 +       /* make the dir opaque */
16726 +       diropq = 0;
16727 +       h_mtx = &h_path.dentry->d_inode->i_mutex;
16728 +       if (wh_dentry
16729 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
16730 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
16731 +               opq_dentry = au_diropq_create(dentry, bindex);
16732 +               mutex_unlock(h_mtx);
16733 +               err = PTR_ERR(opq_dentry);
16734 +               if (IS_ERR(opq_dentry))
16735 +                       goto out_dir;
16736 +               dput(opq_dentry);
16737 +               diropq = 1;
16738 +       }
16739 +
16740 +       err = epilog(dir, bindex, wh_dentry, dentry);
16741 +       if (!err) {
16742 +               inc_nlink(dir);
16743 +               goto out_unpin; /* success */
16744 +       }
16745 +
16746 +       /* revert */
16747 +       if (diropq) {
16748 +               AuLabel(revert opq);
16749 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
16750 +               rerr = au_diropq_remove(dentry, bindex);
16751 +               mutex_unlock(h_mtx);
16752 +               if (rerr) {
16753 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
16754 +                               dentry, err, rerr);
16755 +                       err = -EIO;
16756 +               }
16757 +       }
16758 +
16759 +out_dir:
16760 +       AuLabel(revert dir);
16761 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
16762 +       if (rerr) {
16763 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
16764 +                       dentry, err, rerr);
16765 +               err = -EIO;
16766 +       }
16767 +       au_dtime_revert(&a->dt);
16768 +out_unpin:
16769 +       au_unpin(&a->pin);
16770 +       dput(wh_dentry);
16771 +out_parent:
16772 +       di_write_unlock(parent);
16773 +out_unlock:
16774 +       if (unlikely(err)) {
16775 +               au_update_dbstart(dentry);
16776 +               d_drop(dentry);
16777 +       }
16778 +       aufs_read_unlock(dentry, AuLock_DW);
16779 +out_free:
16780 +       kfree(a);
16781 +out:
16782 +       return err;
16783 +}
16784 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
16785 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
16786 +++ linux/fs/aufs/i_op.c        2014-06-24 00:14:06.782719306 +0200
16787 @@ -0,0 +1,1138 @@
16788 +/*
16789 + * Copyright (C) 2005-2014 Junjiro R. Okajima
16790 + *
16791 + * This program, aufs is free software; you can redistribute it and/or modify
16792 + * it under the terms of the GNU General Public License as published by
16793 + * the Free Software Foundation; either version 2 of the License, or
16794 + * (at your option) any later version.
16795 + *
16796 + * This program is distributed in the hope that it will be useful,
16797 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16798 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16799 + * GNU General Public License for more details.
16800 + *
16801 + * You should have received a copy of the GNU General Public License
16802 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16803 + */
16804 +
16805 +/*
16806 + * inode operations (except add/del/rename)
16807 + */
16808 +
16809 +#include <linux/device_cgroup.h>
16810 +#include <linux/fs_stack.h>
16811 +#include <linux/mm.h>
16812 +#include <linux/namei.h>
16813 +#include <linux/security.h>
16814 +#include "aufs.h"
16815 +
16816 +static int h_permission(struct inode *h_inode, int mask,
16817 +                       struct vfsmount *h_mnt, int brperm)
16818 +{
16819 +       int err;
16820 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
16821 +
16822 +       err = -EACCES;
16823 +       if ((write_mask && IS_IMMUTABLE(h_inode))
16824 +           || ((mask & MAY_EXEC)
16825 +               && S_ISREG(h_inode->i_mode)
16826 +               && ((h_mnt->mnt_flags & MNT_NOEXEC)
16827 +                   || !(h_inode->i_mode & S_IXUGO))))
16828 +               goto out;
16829 +
16830 +       /*
16831 +        * - skip the lower fs test in the case of write to ro branch.
16832 +        * - nfs dir permission write check is optimized, but a policy for
16833 +        *   link/rename requires a real check.
16834 +        */
16835 +       if ((write_mask && !au_br_writable(brperm))
16836 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
16837 +               && write_mask && !(mask & MAY_READ))
16838 +           || !h_inode->i_op->permission) {
16839 +               /* AuLabel(generic_permission); */
16840 +               err = generic_permission(h_inode, mask);
16841 +       } else {
16842 +               /* AuLabel(h_inode->permission); */
16843 +               err = h_inode->i_op->permission(h_inode, mask);
16844 +               AuTraceErr(err);
16845 +       }
16846 +
16847 +       if (!err)
16848 +               err = devcgroup_inode_permission(h_inode, mask);
16849 +       if (!err)
16850 +               err = security_inode_permission(h_inode, mask);
16851 +
16852 +#if 0
16853 +       if (!err) {
16854 +               /* todo: do we need to call ima_path_check()? */
16855 +               struct path h_path = {
16856 +                       .dentry =
16857 +                       .mnt    = h_mnt
16858 +               };
16859 +               err = ima_path_check(&h_path,
16860 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
16861 +                                    IMA_COUNT_LEAVE);
16862 +       }
16863 +#endif
16864 +
16865 +out:
16866 +       return err;
16867 +}
16868 +
16869 +static int aufs_permission(struct inode *inode, int mask)
16870 +{
16871 +       int err;
16872 +       aufs_bindex_t bindex, bend;
16873 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
16874 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
16875 +       struct inode *h_inode;
16876 +       struct super_block *sb;
16877 +       struct au_branch *br;
16878 +
16879 +       /* todo: support rcu-walk? */
16880 +       if (mask & MAY_NOT_BLOCK)
16881 +               return -ECHILD;
16882 +
16883 +       sb = inode->i_sb;
16884 +       si_read_lock(sb, AuLock_FLUSH);
16885 +       ii_read_lock_child(inode);
16886 +#if 0
16887 +       err = au_iigen_test(inode, au_sigen(sb));
16888 +       if (unlikely(err))
16889 +               goto out;
16890 +#endif
16891 +
16892 +       if (!isdir || write_mask) {
16893 +               err = au_busy_or_stale();
16894 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
16895 +               if (unlikely(!h_inode
16896 +                            || (h_inode->i_mode & S_IFMT)
16897 +                            != (inode->i_mode & S_IFMT)))
16898 +                       goto out;
16899 +
16900 +               err = 0;
16901 +               bindex = au_ibstart(inode);
16902 +               br = au_sbr(sb, bindex);
16903 +               err = h_permission(h_inode, mask, au_br_mnt(br), br->br_perm);
16904 +               if (write_mask
16905 +                   && !err
16906 +                   && !special_file(h_inode->i_mode)) {
16907 +                       /* test whether the upper writable branch exists */
16908 +                       err = -EROFS;
16909 +                       for (; bindex >= 0; bindex--)
16910 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
16911 +                                       err = 0;
16912 +                                       break;
16913 +                               }
16914 +               }
16915 +               goto out;
16916 +       }
16917 +
16918 +       /* non-write to dir */
16919 +       err = 0;
16920 +       bend = au_ibend(inode);
16921 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
16922 +               h_inode = au_h_iptr(inode, bindex);
16923 +               if (h_inode) {
16924 +                       err = au_busy_or_stale();
16925 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
16926 +                               break;
16927 +
16928 +                       br = au_sbr(sb, bindex);
16929 +                       err = h_permission(h_inode, mask, au_br_mnt(br),
16930 +                                          br->br_perm);
16931 +               }
16932 +       }
16933 +
16934 +out:
16935 +       ii_read_unlock(inode);
16936 +       si_read_unlock(sb);
16937 +       return err;
16938 +}
16939 +
16940 +/* ---------------------------------------------------------------------- */
16941 +
16942 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
16943 +                                 unsigned int flags)
16944 +{
16945 +       struct dentry *ret, *parent;
16946 +       struct inode *inode;
16947 +       struct super_block *sb;
16948 +       int err, npositive;
16949 +
16950 +       IMustLock(dir);
16951 +
16952 +       /* todo: support rcu-walk? */
16953 +       ret = ERR_PTR(-ECHILD);
16954 +       if (flags & LOOKUP_RCU)
16955 +               goto out;
16956 +
16957 +       ret = ERR_PTR(-ENAMETOOLONG);
16958 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
16959 +               goto out;
16960 +
16961 +       sb = dir->i_sb;
16962 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16963 +       ret = ERR_PTR(err);
16964 +       if (unlikely(err))
16965 +               goto out;
16966 +
16967 +       err = au_di_init(dentry);
16968 +       ret = ERR_PTR(err);
16969 +       if (unlikely(err))
16970 +               goto out_si;
16971 +
16972 +       inode = NULL;
16973 +       npositive = 0; /* suppress a warning */
16974 +       parent = dentry->d_parent; /* dir inode is locked */
16975 +       di_read_lock_parent(parent, AuLock_IR);
16976 +       err = au_alive_dir(parent);
16977 +       if (!err)
16978 +               err = au_digen_test(parent, au_sigen(sb));
16979 +       if (!err) {
16980 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
16981 +                                          /*type*/0);
16982 +               err = npositive;
16983 +       }
16984 +       di_read_unlock(parent, AuLock_IR);
16985 +       ret = ERR_PTR(err);
16986 +       if (unlikely(err < 0))
16987 +               goto out_unlock;
16988 +
16989 +       if (npositive) {
16990 +               inode = au_new_inode(dentry, /*must_new*/0);
16991 +               ret = (void *)inode;
16992 +       }
16993 +       if (IS_ERR(inode)) {
16994 +               inode = NULL;
16995 +               goto out_unlock;
16996 +       }
16997 +
16998 +       ret = d_splice_alias(inode, dentry);
16999 +#if 0
17000 +       if (unlikely(d_need_lookup(dentry))) {
17001 +               spin_lock(&dentry->d_lock);
17002 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
17003 +               spin_unlock(&dentry->d_lock);
17004 +       } else
17005 +#endif
17006 +       if (unlikely(IS_ERR(ret) && inode)) {
17007 +               ii_write_unlock(inode);
17008 +               iput(inode);
17009 +               inode = NULL;
17010 +       }
17011 +
17012 +out_unlock:
17013 +       di_write_unlock(dentry);
17014 +       if (inode) {
17015 +               /* verbose coding for lock class name */
17016 +               if (unlikely(S_ISLNK(inode->i_mode)))
17017 +                       au_rw_class(&au_di(dentry)->di_rwsem,
17018 +                                   au_lc_key + AuLcSymlink_DIINFO);
17019 +               else if (unlikely(S_ISDIR(inode->i_mode)))
17020 +                       au_rw_class(&au_di(dentry)->di_rwsem,
17021 +                                   au_lc_key + AuLcDir_DIINFO);
17022 +               else /* likely */
17023 +                       au_rw_class(&au_di(dentry)->di_rwsem,
17024 +                                   au_lc_key + AuLcNonDir_DIINFO);
17025 +       }
17026 +out_si:
17027 +       si_read_unlock(sb);
17028 +out:
17029 +       return ret;
17030 +}
17031 +
17032 +/* ---------------------------------------------------------------------- */
17033 +
17034 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
17035 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
17036 +                         aufs_bindex_t bstart)
17037 +{
17038 +       int err;
17039 +       struct dentry *h_parent;
17040 +       struct inode *h_dir;
17041 +
17042 +       if (add_entry)
17043 +               IMustLock(parent->d_inode);
17044 +       else
17045 +               di_write_lock_parent(parent);
17046 +
17047 +       err = 0;
17048 +       if (!au_h_dptr(parent, bcpup)) {
17049 +               if (bstart > bcpup)
17050 +                       err = au_cpup_dirs(dentry, bcpup);
17051 +               else if (bstart < bcpup)
17052 +                       err = au_cpdown_dirs(dentry, bcpup);
17053 +               else
17054 +                       BUG();
17055 +       }
17056 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
17057 +               h_parent = au_h_dptr(parent, bcpup);
17058 +               h_dir = h_parent->d_inode;
17059 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
17060 +               err = au_lkup_neg(dentry, bcpup,
17061 +                                 au_ftest_wrdir(add_entry, TMP_WHENTRY));
17062 +               /* todo: no unlock here */
17063 +               mutex_unlock(&h_dir->i_mutex);
17064 +
17065 +               AuDbg("bcpup %d\n", bcpup);
17066 +               if (!err) {
17067 +                       if (!dentry->d_inode)
17068 +                               au_set_h_dptr(dentry, bstart, NULL);
17069 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
17070 +               }
17071 +       }
17072 +
17073 +       if (!add_entry)
17074 +               di_write_unlock(parent);
17075 +       if (!err)
17076 +               err = bcpup; /* success */
17077 +
17078 +       AuTraceErr(err);
17079 +       return err;
17080 +}
17081 +
17082 +/*
17083 + * decide the branch and the parent dir where we will create a new entry.
17084 + * returns new bindex or an error.
17085 + * copyup the parent dir if needed.
17086 + */
17087 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
17088 +             struct au_wr_dir_args *args)
17089 +{
17090 +       int err;
17091 +       unsigned int flags;
17092 +       aufs_bindex_t bcpup, bstart, src_bstart;
17093 +       const unsigned char add_entry
17094 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
17095 +               | au_ftest_wrdir(args->flags, TMP_WHENTRY)
17096 +               | au_ftest_wrdir(args->flags, TMPFILE);
17097 +       struct super_block *sb;
17098 +       struct dentry *parent;
17099 +       struct au_sbinfo *sbinfo;
17100 +
17101 +       sb = dentry->d_sb;
17102 +       sbinfo = au_sbi(sb);
17103 +       parent = dget_parent(dentry);
17104 +       bstart = au_dbstart(dentry);
17105 +       bcpup = bstart;
17106 +       if (args->force_btgt < 0) {
17107 +               if (src_dentry) {
17108 +                       src_bstart = au_dbstart(src_dentry);
17109 +                       if (src_bstart < bstart)
17110 +                               bcpup = src_bstart;
17111 +               } else if (add_entry) {
17112 +                       flags = 0;
17113 +                       if (au_ftest_wrdir(args->flags, ISDIR))
17114 +                               au_fset_wbr(flags, DIR);
17115 +                       err = AuWbrCreate(sbinfo, dentry, flags);
17116 +                       bcpup = err;
17117 +               }
17118 +
17119 +               if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
17120 +                       if (add_entry)
17121 +                               err = AuWbrCopyup(sbinfo, dentry);
17122 +                       else {
17123 +                               if (!IS_ROOT(dentry)) {
17124 +                                       di_read_lock_parent(parent, !AuLock_IR);
17125 +                                       err = AuWbrCopyup(sbinfo, dentry);
17126 +                                       di_read_unlock(parent, !AuLock_IR);
17127 +                               } else
17128 +                                       err = AuWbrCopyup(sbinfo, dentry);
17129 +                       }
17130 +                       bcpup = err;
17131 +                       if (unlikely(err < 0))
17132 +                               goto out;
17133 +               }
17134 +       } else {
17135 +               bcpup = args->force_btgt;
17136 +               AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
17137 +       }
17138 +
17139 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
17140 +       err = bcpup;
17141 +       if (bcpup == bstart)
17142 +               goto out; /* success */
17143 +
17144 +       /* copyup the new parent into the branch we process */
17145 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
17146 +       if (err >= 0) {
17147 +               if (!dentry->d_inode) {
17148 +                       au_set_h_dptr(dentry, bstart, NULL);
17149 +                       au_set_dbstart(dentry, bcpup);
17150 +                       au_set_dbend(dentry, bcpup);
17151 +               }
17152 +               AuDebugOn(add_entry
17153 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
17154 +                         && !au_h_dptr(dentry, bcpup));
17155 +       }
17156 +
17157 +out:
17158 +       dput(parent);
17159 +       return err;
17160 +}
17161 +
17162 +/* ---------------------------------------------------------------------- */
17163 +
17164 +void au_pin_hdir_unlock(struct au_pin *p)
17165 +{
17166 +       if (p->hdir)
17167 +               au_hn_imtx_unlock(p->hdir);
17168 +}
17169 +
17170 +static int au_pin_hdir_lock(struct au_pin *p)
17171 +{
17172 +       int err;
17173 +
17174 +       err = 0;
17175 +       if (!p->hdir)
17176 +               goto out;
17177 +
17178 +       /* even if an error happens later, keep this lock */
17179 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
17180 +
17181 +       err = -EBUSY;
17182 +       if (unlikely(p->hdir->hi_inode != p->h_parent->d_inode))
17183 +               goto out;
17184 +
17185 +       err = 0;
17186 +       if (p->h_dentry)
17187 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
17188 +                                 p->h_parent, p->br);
17189 +
17190 +out:
17191 +       return err;
17192 +}
17193 +
17194 +int au_pin_hdir_relock(struct au_pin *p)
17195 +{
17196 +       int err, i;
17197 +       struct inode *h_i;
17198 +       struct dentry *h_d[] = {
17199 +               p->h_dentry,
17200 +               p->h_parent
17201 +       };
17202 +
17203 +       err = au_pin_hdir_lock(p);
17204 +       if (unlikely(err))
17205 +               goto out;
17206 +
17207 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
17208 +               if (!h_d[i])
17209 +                       continue;
17210 +               h_i = h_d[i]->d_inode;
17211 +               if (h_i)
17212 +                       err = !h_i->i_nlink;
17213 +       }
17214 +
17215 +out:
17216 +       return err;
17217 +}
17218 +
17219 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
17220 +{
17221 +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
17222 +       p->hdir->hi_inode->i_mutex.owner = task;
17223 +#endif
17224 +}
17225 +
17226 +void au_pin_hdir_acquire_nest(struct au_pin *p)
17227 +{
17228 +       if (p->hdir) {
17229 +               mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
17230 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
17231 +               au_pin_hdir_set_owner(p, current);
17232 +       }
17233 +}
17234 +
17235 +void au_pin_hdir_release(struct au_pin *p)
17236 +{
17237 +       if (p->hdir) {
17238 +               au_pin_hdir_set_owner(p, p->task);
17239 +               mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
17240 +       }
17241 +}
17242 +
17243 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
17244 +{
17245 +       if (pin && pin->parent)
17246 +               return au_h_dptr(pin->parent, pin->bindex);
17247 +       return NULL;
17248 +}
17249 +
17250 +void au_unpin(struct au_pin *p)
17251 +{
17252 +       if (p->hdir)
17253 +               au_pin_hdir_unlock(p);
17254 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
17255 +               vfsub_mnt_drop_write(p->h_mnt);
17256 +       if (!p->hdir)
17257 +               return;
17258 +
17259 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
17260 +               di_read_unlock(p->parent, AuLock_IR);
17261 +       iput(p->hdir->hi_inode);
17262 +       dput(p->parent);
17263 +       p->parent = NULL;
17264 +       p->hdir = NULL;
17265 +       p->h_mnt = NULL;
17266 +       /* do not clear p->task */
17267 +}
17268 +
17269 +int au_do_pin(struct au_pin *p)
17270 +{
17271 +       int err;
17272 +       struct super_block *sb;
17273 +       struct inode *h_dir;
17274 +
17275 +       err = 0;
17276 +       sb = p->dentry->d_sb;
17277 +       p->br = au_sbr(sb, p->bindex);
17278 +       if (IS_ROOT(p->dentry)) {
17279 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
17280 +                       p->h_mnt = au_br_mnt(p->br);
17281 +                       err = vfsub_mnt_want_write(p->h_mnt);
17282 +                       if (unlikely(err)) {
17283 +                               au_fclr_pin(p->flags, MNT_WRITE);
17284 +                               goto out_err;
17285 +                       }
17286 +               }
17287 +               goto out;
17288 +       }
17289 +
17290 +       p->h_dentry = NULL;
17291 +       if (p->bindex <= au_dbend(p->dentry))
17292 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
17293 +
17294 +       p->parent = dget_parent(p->dentry);
17295 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
17296 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
17297 +
17298 +       h_dir = NULL;
17299 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
17300 +       p->hdir = au_hi(p->parent->d_inode, p->bindex);
17301 +       if (p->hdir)
17302 +               h_dir = p->hdir->hi_inode;
17303 +
17304 +       /*
17305 +        * udba case, or
17306 +        * if DI_LOCKED is not set, then p->parent may be different
17307 +        * and h_parent can be NULL.
17308 +        */
17309 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
17310 +               err = -EBUSY;
17311 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
17312 +                       di_read_unlock(p->parent, AuLock_IR);
17313 +               dput(p->parent);
17314 +               p->parent = NULL;
17315 +               goto out_err;
17316 +       }
17317 +
17318 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
17319 +               p->h_mnt = au_br_mnt(p->br);
17320 +               err = vfsub_mnt_want_write(p->h_mnt);
17321 +               if (unlikely(err)) {
17322 +                       au_fclr_pin(p->flags, MNT_WRITE);
17323 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
17324 +                               di_read_unlock(p->parent, AuLock_IR);
17325 +                       dput(p->parent);
17326 +                       p->parent = NULL;
17327 +                       goto out_err;
17328 +               }
17329 +       }
17330 +
17331 +       au_igrab(h_dir);
17332 +       err = au_pin_hdir_lock(p);
17333 +       if (!err)
17334 +               goto out; /* success */
17335 +
17336 +out_err:
17337 +       pr_err("err %d\n", err);
17338 +       err = au_busy_or_stale();
17339 +out:
17340 +       return err;
17341 +}
17342 +
17343 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
17344 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
17345 +                unsigned int udba, unsigned char flags)
17346 +{
17347 +       p->dentry = dentry;
17348 +       p->udba = udba;
17349 +       p->lsc_di = lsc_di;
17350 +       p->lsc_hi = lsc_hi;
17351 +       p->flags = flags;
17352 +       p->bindex = bindex;
17353 +
17354 +       p->parent = NULL;
17355 +       p->hdir = NULL;
17356 +       p->h_mnt = NULL;
17357 +
17358 +       p->h_dentry = NULL;
17359 +       p->h_parent = NULL;
17360 +       p->br = NULL;
17361 +       p->task = current;
17362 +}
17363 +
17364 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
17365 +          unsigned int udba, unsigned char flags)
17366 +{
17367 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
17368 +                   udba, flags);
17369 +       return au_do_pin(pin);
17370 +}
17371 +
17372 +/* ---------------------------------------------------------------------- */
17373 +
17374 +/*
17375 + * ->setattr() and ->getattr() are called in various cases.
17376 + * chmod, stat: dentry is revalidated.
17377 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
17378 + *               unhashed.
17379 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
17380 + */
17381 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
17382 +static int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
17383 +{
17384 +       int err;
17385 +       struct inode *inode;
17386 +       struct dentry *parent;
17387 +
17388 +       err = 0;
17389 +       inode = dentry->d_inode;
17390 +       if (au_digen_test(dentry, sigen)) {
17391 +               parent = dget_parent(dentry);
17392 +               di_read_lock_parent(parent, AuLock_IR);
17393 +               err = au_refresh_dentry(dentry, parent);
17394 +               di_read_unlock(parent, AuLock_IR);
17395 +               dput(parent);
17396 +       }
17397 +
17398 +       AuTraceErr(err);
17399 +       return err;
17400 +}
17401 +
17402 +#define AuIcpup_DID_CPUP       1
17403 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
17404 +#define au_fset_icpup(flags, name) \
17405 +       do { (flags) |= AuIcpup_##name; } while (0)
17406 +#define au_fclr_icpup(flags, name) \
17407 +       do { (flags) &= ~AuIcpup_##name; } while (0)
17408 +
17409 +struct au_icpup_args {
17410 +       unsigned char flags;
17411 +       unsigned char pin_flags;
17412 +       aufs_bindex_t btgt;
17413 +       unsigned int udba;
17414 +       struct au_pin pin;
17415 +       struct path h_path;
17416 +       struct inode *h_inode;
17417 +};
17418 +
17419 +static int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
17420 +                           struct au_icpup_args *a)
17421 +{
17422 +       int err;
17423 +       loff_t sz;
17424 +       aufs_bindex_t bstart, ibstart;
17425 +       struct dentry *hi_wh, *parent;
17426 +       struct inode *inode;
17427 +       struct au_wr_dir_args wr_dir_args = {
17428 +               .force_btgt     = -1,
17429 +               .flags          = 0
17430 +       };
17431 +
17432 +       bstart = au_dbstart(dentry);
17433 +       inode = dentry->d_inode;
17434 +       if (S_ISDIR(inode->i_mode))
17435 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
17436 +       /* plink or hi_wh() case */
17437 +       ibstart = au_ibstart(inode);
17438 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
17439 +               wr_dir_args.force_btgt = ibstart;
17440 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17441 +       if (unlikely(err < 0))
17442 +               goto out;
17443 +       a->btgt = err;
17444 +       if (err != bstart)
17445 +               au_fset_icpup(a->flags, DID_CPUP);
17446 +
17447 +       err = 0;
17448 +       a->pin_flags = AuPin_MNT_WRITE;
17449 +       parent = NULL;
17450 +       if (!IS_ROOT(dentry)) {
17451 +               au_fset_pin(a->pin_flags, DI_LOCKED);
17452 +               parent = dget_parent(dentry);
17453 +               di_write_lock_parent(parent);
17454 +       }
17455 +
17456 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
17457 +       if (unlikely(err))
17458 +               goto out_parent;
17459 +
17460 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17461 +       a->h_inode = a->h_path.dentry->d_inode;
17462 +       mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
17463 +       sz = -1;
17464 +       if ((ia->ia_valid & ATTR_SIZE) && ia->ia_size < i_size_read(a->h_inode))
17465 +               sz = ia->ia_size;
17466 +       mutex_unlock(&a->h_inode->i_mutex);
17467 +
17468 +       hi_wh = NULL;
17469 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
17470 +               hi_wh = au_hi_wh(inode, a->btgt);
17471 +               if (!hi_wh) {
17472 +                       struct au_cp_generic cpg = {
17473 +                               .dentry = dentry,
17474 +                               .bdst   = a->btgt,
17475 +                               .bsrc   = -1,
17476 +                               .len    = sz,
17477 +                               .pin    = &a->pin
17478 +                       };
17479 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
17480 +                       if (unlikely(err))
17481 +                               goto out_unlock;
17482 +                       hi_wh = au_hi_wh(inode, a->btgt);
17483 +                       /* todo: revalidate hi_wh? */
17484 +               }
17485 +       }
17486 +
17487 +       if (parent) {
17488 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
17489 +               di_downgrade_lock(parent, AuLock_IR);
17490 +               dput(parent);
17491 +               parent = NULL;
17492 +       }
17493 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
17494 +               goto out; /* success */
17495 +
17496 +       if (!d_unhashed(dentry)) {
17497 +               struct au_cp_generic cpg = {
17498 +                       .dentry = dentry,
17499 +                       .bdst   = a->btgt,
17500 +                       .bsrc   = bstart,
17501 +                       .len    = sz,
17502 +                       .pin    = &a->pin,
17503 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
17504 +               };
17505 +               err = au_sio_cpup_simple(&cpg);
17506 +               if (!err)
17507 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
17508 +       } else if (!hi_wh)
17509 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
17510 +       else
17511 +               a->h_path.dentry = hi_wh; /* do not dget here */
17512 +
17513 +out_unlock:
17514 +       a->h_inode = a->h_path.dentry->d_inode;
17515 +       if (!err)
17516 +               goto out; /* success */
17517 +       au_unpin(&a->pin);
17518 +out_parent:
17519 +       if (parent) {
17520 +               di_write_unlock(parent);
17521 +               dput(parent);
17522 +       }
17523 +out:
17524 +       if (!err)
17525 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
17526 +       return err;
17527 +}
17528 +
17529 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
17530 +{
17531 +       int err;
17532 +       struct inode *inode, *delegated;
17533 +       struct super_block *sb;
17534 +       struct file *file;
17535 +       struct au_icpup_args *a;
17536 +
17537 +       inode = dentry->d_inode;
17538 +       IMustLock(inode);
17539 +
17540 +       err = -ENOMEM;
17541 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17542 +       if (unlikely(!a))
17543 +               goto out;
17544 +
17545 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
17546 +               ia->ia_valid &= ~ATTR_MODE;
17547 +
17548 +       file = NULL;
17549 +       sb = dentry->d_sb;
17550 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17551 +       if (unlikely(err))
17552 +               goto out_kfree;
17553 +
17554 +       if (ia->ia_valid & ATTR_FILE) {
17555 +               /* currently ftruncate(2) only */
17556 +               AuDebugOn(!S_ISREG(inode->i_mode));
17557 +               file = ia->ia_file;
17558 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
17559 +               if (unlikely(err))
17560 +                       goto out_si;
17561 +               ia->ia_file = au_hf_top(file);
17562 +               a->udba = AuOpt_UDBA_NONE;
17563 +       } else {
17564 +               /* fchmod() doesn't pass ia_file */
17565 +               a->udba = au_opt_udba(sb);
17566 +               di_write_lock_child(dentry);
17567 +               /* no d_unlinked(), to set UDBA_NONE for root */
17568 +               if (d_unhashed(dentry))
17569 +                       a->udba = AuOpt_UDBA_NONE;
17570 +               if (a->udba != AuOpt_UDBA_NONE) {
17571 +                       AuDebugOn(IS_ROOT(dentry));
17572 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
17573 +                       if (unlikely(err))
17574 +                               goto out_dentry;
17575 +               }
17576 +       }
17577 +
17578 +       err = au_pin_and_icpup(dentry, ia, a);
17579 +       if (unlikely(err < 0))
17580 +               goto out_dentry;
17581 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
17582 +               ia->ia_file = NULL;
17583 +               ia->ia_valid &= ~ATTR_FILE;
17584 +       }
17585 +
17586 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
17587 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
17588 +           == (ATTR_MODE | ATTR_CTIME)) {
17589 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
17590 +               if (unlikely(err))
17591 +                       goto out_unlock;
17592 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
17593 +                  && (ia->ia_valid & ATTR_CTIME)) {
17594 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
17595 +               if (unlikely(err))
17596 +                       goto out_unlock;
17597 +       }
17598 +
17599 +       if (ia->ia_valid & ATTR_SIZE) {
17600 +               struct file *f;
17601 +
17602 +               if (ia->ia_size < i_size_read(inode))
17603 +                       /* unmap only */
17604 +                       truncate_setsize(inode, ia->ia_size);
17605 +
17606 +               f = NULL;
17607 +               if (ia->ia_valid & ATTR_FILE)
17608 +                       f = ia->ia_file;
17609 +               mutex_unlock(&a->h_inode->i_mutex);
17610 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
17611 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
17612 +       } else {
17613 +               delegated = NULL;
17614 +               while (1) {
17615 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
17616 +                       if (delegated) {
17617 +                               err = break_deleg_wait(&delegated);
17618 +                               if (!err)
17619 +                                       continue;
17620 +                       }
17621 +                       break;
17622 +               }
17623 +       }
17624 +       if (!err)
17625 +               au_cpup_attr_changeable(inode);
17626 +
17627 +out_unlock:
17628 +       mutex_unlock(&a->h_inode->i_mutex);
17629 +       au_unpin(&a->pin);
17630 +       if (unlikely(err))
17631 +               au_update_dbstart(dentry);
17632 +out_dentry:
17633 +       di_write_unlock(dentry);
17634 +       if (file) {
17635 +               fi_write_unlock(file);
17636 +               ia->ia_file = file;
17637 +               ia->ia_valid |= ATTR_FILE;
17638 +       }
17639 +out_si:
17640 +       si_read_unlock(sb);
17641 +out_kfree:
17642 +       kfree(a);
17643 +out:
17644 +       AuTraceErr(err);
17645 +       return err;
17646 +}
17647 +
17648 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
17649 +                            unsigned int nlink)
17650 +{
17651 +       unsigned int n;
17652 +
17653 +       inode->i_mode = st->mode;
17654 +       /* don't i_[ug]id_write() here */
17655 +       inode->i_uid = st->uid;
17656 +       inode->i_gid = st->gid;
17657 +       inode->i_atime = st->atime;
17658 +       inode->i_mtime = st->mtime;
17659 +       inode->i_ctime = st->ctime;
17660 +
17661 +       au_cpup_attr_nlink(inode, /*force*/0);
17662 +       if (S_ISDIR(inode->i_mode)) {
17663 +               n = inode->i_nlink;
17664 +               n -= nlink;
17665 +               n += st->nlink;
17666 +               smp_mb(); /* for i_nlink */
17667 +               /* 0 can happen */
17668 +               set_nlink(inode, n);
17669 +       }
17670 +
17671 +       spin_lock(&inode->i_lock);
17672 +       inode->i_blocks = st->blocks;
17673 +       i_size_write(inode, st->size);
17674 +       spin_unlock(&inode->i_lock);
17675 +}
17676 +
17677 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
17678 +                       struct dentry *dentry, struct kstat *st)
17679 +{
17680 +       int err;
17681 +       unsigned int mnt_flags;
17682 +       aufs_bindex_t bindex;
17683 +       unsigned char udba_none, positive;
17684 +       struct super_block *sb, *h_sb;
17685 +       struct inode *inode;
17686 +       struct path h_path;
17687 +
17688 +       sb = dentry->d_sb;
17689 +       inode = dentry->d_inode;
17690 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17691 +       if (unlikely(err))
17692 +               goto out;
17693 +       mnt_flags = au_mntflags(sb);
17694 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
17695 +
17696 +       /* support fstat(2) */
17697 +       if (!d_unlinked(dentry) && !udba_none) {
17698 +               unsigned int sigen = au_sigen(sb);
17699 +               err = au_digen_test(dentry, sigen);
17700 +               if (!err) {
17701 +                       di_read_lock_child(dentry, AuLock_IR);
17702 +                       err = au_dbrange_test(dentry);
17703 +                       if (unlikely(err))
17704 +                               goto out_unlock;
17705 +               } else {
17706 +                       AuDebugOn(IS_ROOT(dentry));
17707 +                       di_write_lock_child(dentry);
17708 +                       err = au_dbrange_test(dentry);
17709 +                       if (!err)
17710 +                               err = au_reval_for_attr(dentry, sigen);
17711 +                       di_downgrade_lock(dentry, AuLock_IR);
17712 +                       if (unlikely(err))
17713 +                               goto out_unlock;
17714 +               }
17715 +       } else
17716 +               di_read_lock_child(dentry, AuLock_IR);
17717 +
17718 +       bindex = au_ibstart(inode);
17719 +       h_path.mnt = au_sbr_mnt(sb, bindex);
17720 +       h_sb = h_path.mnt->mnt_sb;
17721 +       if (!au_test_fs_bad_iattr(h_sb) && udba_none)
17722 +               goto out_fill; /* success */
17723 +
17724 +       h_path.dentry = NULL;
17725 +       if (au_dbstart(dentry) == bindex)
17726 +               h_path.dentry = dget(au_h_dptr(dentry, bindex));
17727 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
17728 +               h_path.dentry = au_plink_lkup(inode, bindex);
17729 +               if (IS_ERR(h_path.dentry))
17730 +                       goto out_fill; /* pretending success */
17731 +       }
17732 +       /* illegally overlapped or something */
17733 +       if (unlikely(!h_path.dentry))
17734 +               goto out_fill; /* pretending success */
17735 +
17736 +       positive = !!h_path.dentry->d_inode;
17737 +       if (positive)
17738 +               err = vfs_getattr(&h_path, st);
17739 +       dput(h_path.dentry);
17740 +       if (!err) {
17741 +               if (positive)
17742 +                       au_refresh_iattr(inode, st,
17743 +                                        h_path.dentry->d_inode->i_nlink);
17744 +               goto out_fill; /* success */
17745 +       }
17746 +       AuTraceErr(err);
17747 +       goto out_unlock;
17748 +
17749 +out_fill:
17750 +       generic_fillattr(inode, st);
17751 +out_unlock:
17752 +       di_read_unlock(dentry, AuLock_IR);
17753 +       si_read_unlock(sb);
17754 +out:
17755 +       AuTraceErr(err);
17756 +       return err;
17757 +}
17758 +
17759 +/* ---------------------------------------------------------------------- */
17760 +
17761 +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
17762 +                     int bufsiz)
17763 +{
17764 +       int err;
17765 +       struct super_block *sb;
17766 +       struct dentry *h_dentry;
17767 +
17768 +       err = -EINVAL;
17769 +       h_dentry = au_h_dptr(dentry, bindex);
17770 +       if (unlikely(!h_dentry->d_inode->i_op->readlink))
17771 +               goto out;
17772 +
17773 +       err = security_inode_readlink(h_dentry);
17774 +       if (unlikely(err))
17775 +               goto out;
17776 +
17777 +       sb = dentry->d_sb;
17778 +       if (!au_test_ro(sb, bindex, dentry->d_inode)) {
17779 +               vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
17780 +               fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
17781 +       }
17782 +       err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
17783 +
17784 +out:
17785 +       return err;
17786 +}
17787 +
17788 +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
17789 +{
17790 +       int err;
17791 +
17792 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
17793 +       if (unlikely(err))
17794 +               goto out;
17795 +       err = au_d_hashed_positive(dentry);
17796 +       if (!err)
17797 +               err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
17798 +       aufs_read_unlock(dentry, AuLock_IR);
17799 +
17800 +out:
17801 +       return err;
17802 +}
17803 +
17804 +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
17805 +{
17806 +       int err;
17807 +       mm_segment_t old_fs;
17808 +       union {
17809 +               char *k;
17810 +               char __user *u;
17811 +       } buf;
17812 +
17813 +       err = -ENOMEM;
17814 +       buf.k = (void *)__get_free_page(GFP_NOFS);
17815 +       if (unlikely(!buf.k))
17816 +               goto out;
17817 +
17818 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
17819 +       if (unlikely(err))
17820 +               goto out_name;
17821 +
17822 +       err = au_d_hashed_positive(dentry);
17823 +       if (!err) {
17824 +               old_fs = get_fs();
17825 +               set_fs(KERNEL_DS);
17826 +               err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
17827 +               set_fs(old_fs);
17828 +       }
17829 +       aufs_read_unlock(dentry, AuLock_IR);
17830 +
17831 +       if (err >= 0) {
17832 +               buf.k[err] = 0;
17833 +               /* will be freed by put_link */
17834 +               nd_set_link(nd, buf.k);
17835 +               return NULL; /* success */
17836 +       }
17837 +
17838 +out_name:
17839 +       free_page((unsigned long)buf.k);
17840 +out:
17841 +       AuTraceErr(err);
17842 +       return ERR_PTR(err);
17843 +}
17844 +
17845 +static void aufs_put_link(struct dentry *dentry __maybe_unused,
17846 +                         struct nameidata *nd, void *cookie __maybe_unused)
17847 +{
17848 +       char *p;
17849 +
17850 +       p = nd_get_link(nd);
17851 +       if (!IS_ERR_OR_NULL(p))
17852 +               free_page((unsigned long)p);
17853 +}
17854 +
17855 +/* ---------------------------------------------------------------------- */
17856 +
17857 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
17858 +{
17859 +       int err;
17860 +       struct super_block *sb;
17861 +       struct inode *h_inode;
17862 +
17863 +       sb = inode->i_sb;
17864 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
17865 +       lockdep_off();
17866 +       si_read_lock(sb, AuLock_FLUSH);
17867 +       ii_write_lock_child(inode);
17868 +       lockdep_on();
17869 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
17870 +       err = vfsub_update_time(h_inode, ts, flags);
17871 +       lockdep_off();
17872 +       if (!err)
17873 +               au_cpup_attr_timesizes(inode);
17874 +       ii_write_unlock(inode);
17875 +       si_read_unlock(sb);
17876 +       lockdep_on();
17877 +
17878 +       if (!err && (flags & S_VERSION))
17879 +               inode_inc_iversion(inode);
17880 +
17881 +       return err;
17882 +}
17883 +
17884 +/* ---------------------------------------------------------------------- */
17885 +
17886 +struct inode_operations aufs_symlink_iop = {
17887 +       .permission     = aufs_permission,
17888 +       .setattr        = aufs_setattr,
17889 +       .getattr        = aufs_getattr,
17890 +
17891 +       .readlink       = aufs_readlink,
17892 +       .follow_link    = aufs_follow_link,
17893 +       .put_link       = aufs_put_link,
17894 +
17895 +       /* .update_time = aufs_update_time */
17896 +};
17897 +
17898 +struct inode_operations aufs_dir_iop = {
17899 +       .create         = aufs_create,
17900 +       .lookup         = aufs_lookup,
17901 +       .link           = aufs_link,
17902 +       .unlink         = aufs_unlink,
17903 +       .symlink        = aufs_symlink,
17904 +       .mkdir          = aufs_mkdir,
17905 +       .rmdir          = aufs_rmdir,
17906 +       .mknod          = aufs_mknod,
17907 +       .rename         = aufs_rename,
17908 +
17909 +       .permission     = aufs_permission,
17910 +       .setattr        = aufs_setattr,
17911 +       .getattr        = aufs_getattr,
17912 +
17913 +       .update_time    = aufs_update_time,
17914 +       /* no support for atomic_open() */
17915 +
17916 +       .tmpfile        = aufs_tmpfile
17917 +};
17918 +
17919 +struct inode_operations aufs_iop = {
17920 +       .permission     = aufs_permission,
17921 +       .setattr        = aufs_setattr,
17922 +       .getattr        = aufs_getattr,
17923 +
17924 +       .update_time    = aufs_update_time
17925 +};
17926 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
17927 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
17928 +++ linux/fs/aufs/i_op_del.c    2014-06-24 00:14:06.782719306 +0200
17929 @@ -0,0 +1,506 @@
17930 +/*
17931 + * Copyright (C) 2005-2014 Junjiro R. Okajima
17932 + *
17933 + * This program, aufs is free software; you can redistribute it and/or modify
17934 + * it under the terms of the GNU General Public License as published by
17935 + * the Free Software Foundation; either version 2 of the License, or
17936 + * (at your option) any later version.
17937 + *
17938 + * This program is distributed in the hope that it will be useful,
17939 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17940 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17941 + * GNU General Public License for more details.
17942 + *
17943 + * You should have received a copy of the GNU General Public License
17944 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17945 + */
17946 +
17947 +/*
17948 + * inode operations (del entry)
17949 + */
17950 +
17951 +#include "aufs.h"
17952 +
17953 +/*
17954 + * decide if a new whiteout for @dentry is necessary or not.
17955 + * when it is necessary, prepare the parent dir for the upper branch whose
17956 + * branch index is @bcpup for creation. the actual creation of the whiteout will
17957 + * be done by caller.
17958 + * return value:
17959 + * 0: wh is unnecessary
17960 + * plus: wh is necessary
17961 + * minus: error
17962 + */
17963 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
17964 +{
17965 +       int need_wh, err;
17966 +       aufs_bindex_t bstart;
17967 +       struct super_block *sb;
17968 +
17969 +       sb = dentry->d_sb;
17970 +       bstart = au_dbstart(dentry);
17971 +       if (*bcpup < 0) {
17972 +               *bcpup = bstart;
17973 +               if (au_test_ro(sb, bstart, dentry->d_inode)) {
17974 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
17975 +                       *bcpup = err;
17976 +                       if (unlikely(err < 0))
17977 +                               goto out;
17978 +               }
17979 +       } else
17980 +               AuDebugOn(bstart < *bcpup
17981 +                         || au_test_ro(sb, *bcpup, dentry->d_inode));
17982 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
17983 +
17984 +       if (*bcpup != bstart) {
17985 +               err = au_cpup_dirs(dentry, *bcpup);
17986 +               if (unlikely(err))
17987 +                       goto out;
17988 +               need_wh = 1;
17989 +       } else {
17990 +               struct au_dinfo *dinfo, *tmp;
17991 +
17992 +               need_wh = -ENOMEM;
17993 +               dinfo = au_di(dentry);
17994 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
17995 +               if (tmp) {
17996 +                       au_di_cp(tmp, dinfo);
17997 +                       au_di_swap(tmp, dinfo);
17998 +                       /* returns the number of positive dentries */
17999 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
18000 +                       au_di_swap(tmp, dinfo);
18001 +                       au_rw_write_unlock(&tmp->di_rwsem);
18002 +                       au_di_free(tmp);
18003 +               }
18004 +       }
18005 +       AuDbg("need_wh %d\n", need_wh);
18006 +       err = need_wh;
18007 +
18008 +out:
18009 +       return err;
18010 +}
18011 +
18012 +/*
18013 + * simple tests for the del-entry operations.
18014 + * following the checks in vfs, plus the parent-child relationship.
18015 + */
18016 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18017 +              struct dentry *h_parent, int isdir)
18018 +{
18019 +       int err;
18020 +       umode_t h_mode;
18021 +       struct dentry *h_dentry, *h_latest;
18022 +       struct inode *h_inode;
18023 +
18024 +       h_dentry = au_h_dptr(dentry, bindex);
18025 +       h_inode = h_dentry->d_inode;
18026 +       if (dentry->d_inode) {
18027 +               err = -ENOENT;
18028 +               if (unlikely(!h_inode || !h_inode->i_nlink))
18029 +                       goto out;
18030 +
18031 +               h_mode = h_inode->i_mode;
18032 +               if (!isdir) {
18033 +                       err = -EISDIR;
18034 +                       if (unlikely(S_ISDIR(h_mode)))
18035 +                               goto out;
18036 +               } else if (unlikely(!S_ISDIR(h_mode))) {
18037 +                       err = -ENOTDIR;
18038 +                       goto out;
18039 +               }
18040 +       } else {
18041 +               /* rename(2) case */
18042 +               err = -EIO;
18043 +               if (unlikely(h_inode))
18044 +                       goto out;
18045 +       }
18046 +
18047 +       err = -ENOENT;
18048 +       /* expected parent dir is locked */
18049 +       if (unlikely(h_parent != h_dentry->d_parent))
18050 +               goto out;
18051 +       err = 0;
18052 +
18053 +       /*
18054 +        * rmdir a dir may break the consistency on some filesystem.
18055 +        * let's try heavy test.
18056 +        */
18057 +       err = -EACCES;
18058 +       if (unlikely(au_test_h_perm(h_parent->d_inode, MAY_EXEC | MAY_WRITE)))
18059 +               goto out;
18060 +
18061 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent,
18062 +                                  au_sbr(dentry->d_sb, bindex));
18063 +       err = -EIO;
18064 +       if (IS_ERR(h_latest))
18065 +               goto out;
18066 +       if (h_latest == h_dentry)
18067 +               err = 0;
18068 +       dput(h_latest);
18069 +
18070 +out:
18071 +       return err;
18072 +}
18073 +
18074 +/*
18075 + * decide the branch where we operate for @dentry. the branch index will be set
18076 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
18077 + * dir for reverting.
18078 + * when a new whiteout is necessary, create it.
18079 + */
18080 +static struct dentry*
18081 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
18082 +                   struct au_dtime *dt, struct au_pin *pin)
18083 +{
18084 +       struct dentry *wh_dentry;
18085 +       struct super_block *sb;
18086 +       struct path h_path;
18087 +       int err, need_wh;
18088 +       unsigned int udba;
18089 +       aufs_bindex_t bcpup;
18090 +
18091 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
18092 +       wh_dentry = ERR_PTR(need_wh);
18093 +       if (unlikely(need_wh < 0))
18094 +               goto out;
18095 +
18096 +       sb = dentry->d_sb;
18097 +       udba = au_opt_udba(sb);
18098 +       bcpup = *rbcpup;
18099 +       err = au_pin(pin, dentry, bcpup, udba,
18100 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18101 +       wh_dentry = ERR_PTR(err);
18102 +       if (unlikely(err))
18103 +               goto out;
18104 +
18105 +       h_path.dentry = au_pinned_h_parent(pin);
18106 +       if (udba != AuOpt_UDBA_NONE
18107 +           && au_dbstart(dentry) == bcpup) {
18108 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
18109 +               wh_dentry = ERR_PTR(err);
18110 +               if (unlikely(err))
18111 +                       goto out_unpin;
18112 +       }
18113 +
18114 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
18115 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
18116 +       wh_dentry = NULL;
18117 +       if (!need_wh)
18118 +               goto out; /* success, no need to create whiteout */
18119 +
18120 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
18121 +       if (IS_ERR(wh_dentry))
18122 +               goto out_unpin;
18123 +
18124 +       /* returns with the parent is locked and wh_dentry is dget-ed */
18125 +       goto out; /* success */
18126 +
18127 +out_unpin:
18128 +       au_unpin(pin);
18129 +out:
18130 +       return wh_dentry;
18131 +}
18132 +
18133 +/*
18134 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
18135 + * in order to be revertible and save time for removing many child whiteouts
18136 + * under the dir.
18137 + * returns 1 when there are too many child whiteout and caller should remove
18138 + * them asynchronously. returns 0 when the number of children is enough small to
18139 + * remove now or the branch fs is a remote fs.
18140 + * otherwise return an error.
18141 + */
18142 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
18143 +                          struct au_nhash *whlist, struct inode *dir)
18144 +{
18145 +       int rmdir_later, err, dirwh;
18146 +       struct dentry *h_dentry;
18147 +       struct super_block *sb;
18148 +
18149 +       sb = dentry->d_sb;
18150 +       SiMustAnyLock(sb);
18151 +       h_dentry = au_h_dptr(dentry, bindex);
18152 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
18153 +       if (unlikely(err))
18154 +               goto out;
18155 +
18156 +       /* stop monitoring */
18157 +       au_hn_free(au_hi(dentry->d_inode, bindex));
18158 +
18159 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
18160 +               dirwh = au_sbi(sb)->si_dirwh;
18161 +               rmdir_later = (dirwh <= 1);
18162 +               if (!rmdir_later)
18163 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
18164 +                                                             dirwh);
18165 +               if (rmdir_later)
18166 +                       return rmdir_later;
18167 +       }
18168 +
18169 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
18170 +       if (unlikely(err)) {
18171 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
18172 +                       h_dentry, bindex, err);
18173 +               err = 0;
18174 +       }
18175 +
18176 +out:
18177 +       AuTraceErr(err);
18178 +       return err;
18179 +}
18180 +
18181 +/*
18182 + * final procedure for deleting a entry.
18183 + * maintain dentry and iattr.
18184 + */
18185 +static void epilog(struct inode *dir, struct dentry *dentry,
18186 +                  aufs_bindex_t bindex)
18187 +{
18188 +       struct inode *inode;
18189 +
18190 +       inode = dentry->d_inode;
18191 +       d_drop(dentry);
18192 +       inode->i_ctime = dir->i_ctime;
18193 +
18194 +       if (au_ibstart(dir) == bindex)
18195 +               au_cpup_attr_timesizes(dir);
18196 +       dir->i_version++;
18197 +}
18198 +
18199 +/*
18200 + * when an error happened, remove the created whiteout and revert everything.
18201 + */
18202 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
18203 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
18204 +                    struct dentry *dentry, struct au_dtime *dt)
18205 +{
18206 +       int rerr;
18207 +       struct path h_path = {
18208 +               .dentry = wh_dentry,
18209 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
18210 +       };
18211 +
18212 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
18213 +       if (!rerr) {
18214 +               au_set_dbwh(dentry, bwh);
18215 +               au_dtime_revert(dt);
18216 +               return 0;
18217 +       }
18218 +
18219 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
18220 +       return -EIO;
18221 +}
18222 +
18223 +/* ---------------------------------------------------------------------- */
18224 +
18225 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
18226 +{
18227 +       int err;
18228 +       aufs_bindex_t bwh, bindex, bstart;
18229 +       struct inode *inode, *h_dir, *delegated;
18230 +       struct dentry *parent, *wh_dentry;
18231 +       /* to reuduce stack size */
18232 +       struct {
18233 +               struct au_dtime dt;
18234 +               struct au_pin pin;
18235 +               struct path h_path;
18236 +       } *a;
18237 +
18238 +       IMustLock(dir);
18239 +
18240 +       err = -ENOMEM;
18241 +       a = kmalloc(sizeof(*a), GFP_NOFS);
18242 +       if (unlikely(!a))
18243 +               goto out;
18244 +
18245 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
18246 +       if (unlikely(err))
18247 +               goto out_free;
18248 +       err = au_d_hashed_positive(dentry);
18249 +       if (unlikely(err))
18250 +               goto out_unlock;
18251 +       inode = dentry->d_inode;
18252 +       IMustLock(inode);
18253 +       err = -EISDIR;
18254 +       if (unlikely(S_ISDIR(inode->i_mode)))
18255 +               goto out_unlock; /* possible? */
18256 +
18257 +       bstart = au_dbstart(dentry);
18258 +       bwh = au_dbwh(dentry);
18259 +       bindex = -1;
18260 +       parent = dentry->d_parent; /* dir inode is locked */
18261 +       di_write_lock_parent(parent);
18262 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
18263 +                                       &a->pin);
18264 +       err = PTR_ERR(wh_dentry);
18265 +       if (IS_ERR(wh_dentry))
18266 +               goto out_parent;
18267 +
18268 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
18269 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
18270 +       dget(a->h_path.dentry);
18271 +       if (bindex == bstart) {
18272 +               h_dir = au_pinned_h_dir(&a->pin);
18273 +               delegated = NULL;
18274 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
18275 +               if (unlikely(err == -EWOULDBLOCK)) {
18276 +                       pr_warn("cannot retry for NFSv4 delegation"
18277 +                               " for an internal unlink\n");
18278 +                       iput(delegated);
18279 +               }
18280 +       } else {
18281 +               /* dir inode is locked */
18282 +               h_dir = wh_dentry->d_parent->d_inode;
18283 +               IMustLock(h_dir);
18284 +               err = 0;
18285 +       }
18286 +
18287 +       if (!err) {
18288 +               vfsub_drop_nlink(inode);
18289 +               epilog(dir, dentry, bindex);
18290 +
18291 +               /* update target timestamps */
18292 +               if (bindex == bstart) {
18293 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
18294 +                       /*ignore*/
18295 +                       inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
18296 +               } else
18297 +                       /* todo: this timestamp may be reverted later */
18298 +                       inode->i_ctime = h_dir->i_ctime;
18299 +               goto out_unpin; /* success */
18300 +       }
18301 +
18302 +       /* revert */
18303 +       if (wh_dentry) {
18304 +               int rerr;
18305 +
18306 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
18307 +                                &a->dt);
18308 +               if (rerr)
18309 +                       err = rerr;
18310 +       }
18311 +
18312 +out_unpin:
18313 +       au_unpin(&a->pin);
18314 +       dput(wh_dentry);
18315 +       dput(a->h_path.dentry);
18316 +out_parent:
18317 +       di_write_unlock(parent);
18318 +out_unlock:
18319 +       aufs_read_unlock(dentry, AuLock_DW);
18320 +out_free:
18321 +       kfree(a);
18322 +out:
18323 +       return err;
18324 +}
18325 +
18326 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
18327 +{
18328 +       int err, rmdir_later;
18329 +       aufs_bindex_t bwh, bindex, bstart;
18330 +       struct inode *inode;
18331 +       struct dentry *parent, *wh_dentry, *h_dentry;
18332 +       struct au_whtmp_rmdir *args;
18333 +       /* to reuduce stack size */
18334 +       struct {
18335 +               struct au_dtime dt;
18336 +               struct au_pin pin;
18337 +       } *a;
18338 +
18339 +       IMustLock(dir);
18340 +
18341 +       err = -ENOMEM;
18342 +       a = kmalloc(sizeof(*a), GFP_NOFS);
18343 +       if (unlikely(!a))
18344 +               goto out;
18345 +
18346 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
18347 +       if (unlikely(err))
18348 +               goto out_free;
18349 +       err = au_alive_dir(dentry);
18350 +       if (unlikely(err))
18351 +               goto out_unlock;
18352 +       inode = dentry->d_inode;
18353 +       IMustLock(inode);
18354 +       err = -ENOTDIR;
18355 +       if (unlikely(!S_ISDIR(inode->i_mode)))
18356 +               goto out_unlock; /* possible? */
18357 +
18358 +       err = -ENOMEM;
18359 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
18360 +       if (unlikely(!args))
18361 +               goto out_unlock;
18362 +
18363 +       parent = dentry->d_parent; /* dir inode is locked */
18364 +       di_write_lock_parent(parent);
18365 +       err = au_test_empty(dentry, &args->whlist);
18366 +       if (unlikely(err))
18367 +               goto out_parent;
18368 +
18369 +       bstart = au_dbstart(dentry);
18370 +       bwh = au_dbwh(dentry);
18371 +       bindex = -1;
18372 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
18373 +                                       &a->pin);
18374 +       err = PTR_ERR(wh_dentry);
18375 +       if (IS_ERR(wh_dentry))
18376 +               goto out_parent;
18377 +
18378 +       h_dentry = au_h_dptr(dentry, bstart);
18379 +       dget(h_dentry);
18380 +       rmdir_later = 0;
18381 +       if (bindex == bstart) {
18382 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
18383 +               if (err > 0) {
18384 +                       rmdir_later = err;
18385 +                       err = 0;
18386 +               }
18387 +       } else {
18388 +               /* stop monitoring */
18389 +               au_hn_free(au_hi(inode, bstart));
18390 +
18391 +               /* dir inode is locked */
18392 +               IMustLock(wh_dentry->d_parent->d_inode);
18393 +               err = 0;
18394 +       }
18395 +
18396 +       if (!err) {
18397 +               vfsub_dead_dir(inode);
18398 +               au_set_dbdiropq(dentry, -1);
18399 +               epilog(dir, dentry, bindex);
18400 +
18401 +               if (rmdir_later) {
18402 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
18403 +                       args = NULL;
18404 +               }
18405 +
18406 +               goto out_unpin; /* success */
18407 +       }
18408 +
18409 +       /* revert */
18410 +       AuLabel(revert);
18411 +       if (wh_dentry) {
18412 +               int rerr;
18413 +
18414 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
18415 +                                &a->dt);
18416 +               if (rerr)
18417 +                       err = rerr;
18418 +       }
18419 +
18420 +out_unpin:
18421 +       au_unpin(&a->pin);
18422 +       dput(wh_dentry);
18423 +       dput(h_dentry);
18424 +out_parent:
18425 +       di_write_unlock(parent);
18426 +       if (args)
18427 +               au_whtmp_rmdir_free(args);
18428 +out_unlock:
18429 +       aufs_read_unlock(dentry, AuLock_DW);
18430 +out_free:
18431 +       kfree(a);
18432 +out:
18433 +       AuTraceErr(err);
18434 +       return err;
18435 +}
18436 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
18437 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
18438 +++ linux/fs/aufs/i_op_ren.c    2014-06-24 00:14:06.786052822 +0200
18439 @@ -0,0 +1,1032 @@
18440 +/*
18441 + * Copyright (C) 2005-2014 Junjiro R. Okajima
18442 + *
18443 + * This program, aufs is free software; you can redistribute it and/or modify
18444 + * it under the terms of the GNU General Public License as published by
18445 + * the Free Software Foundation; either version 2 of the License, or
18446 + * (at your option) any later version.
18447 + *
18448 + * This program is distributed in the hope that it will be useful,
18449 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18450 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18451 + * GNU General Public License for more details.
18452 + *
18453 + * You should have received a copy of the GNU General Public License
18454 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18455 + */
18456 +
18457 +/*
18458 + * inode operation (rename entry)
18459 + * todo: this is crazy monster
18460 + */
18461 +
18462 +#include "aufs.h"
18463 +
18464 +enum { AuSRC, AuDST, AuSrcDst };
18465 +enum { AuPARENT, AuCHILD, AuParentChild };
18466 +
18467 +#define AuRen_ISDIR    1
18468 +#define AuRen_ISSAMEDIR        (1 << 1)
18469 +#define AuRen_WHSRC    (1 << 2)
18470 +#define AuRen_WHDST    (1 << 3)
18471 +#define AuRen_MNT_WRITE        (1 << 4)
18472 +#define AuRen_DT_DSTDIR        (1 << 5)
18473 +#define AuRen_DIROPQ   (1 << 6)
18474 +#define AuRen_CPUP     (1 << 7)
18475 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
18476 +#define au_fset_ren(flags, name) \
18477 +       do { (flags) |= AuRen_##name; } while (0)
18478 +#define au_fclr_ren(flags, name) \
18479 +       do { (flags) &= ~AuRen_##name; } while (0)
18480 +
18481 +struct au_ren_args {
18482 +       struct {
18483 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
18484 +                       *wh_dentry;
18485 +               struct inode *dir, *inode;
18486 +               struct au_hinode *hdir;
18487 +               struct au_dtime dt[AuParentChild];
18488 +               aufs_bindex_t bstart;
18489 +       } sd[AuSrcDst];
18490 +
18491 +#define src_dentry     sd[AuSRC].dentry
18492 +#define src_dir                sd[AuSRC].dir
18493 +#define src_inode      sd[AuSRC].inode
18494 +#define src_h_dentry   sd[AuSRC].h_dentry
18495 +#define src_parent     sd[AuSRC].parent
18496 +#define src_h_parent   sd[AuSRC].h_parent
18497 +#define src_wh_dentry  sd[AuSRC].wh_dentry
18498 +#define src_hdir       sd[AuSRC].hdir
18499 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
18500 +#define src_dt         sd[AuSRC].dt
18501 +#define src_bstart     sd[AuSRC].bstart
18502 +
18503 +#define dst_dentry     sd[AuDST].dentry
18504 +#define dst_dir                sd[AuDST].dir
18505 +#define dst_inode      sd[AuDST].inode
18506 +#define dst_h_dentry   sd[AuDST].h_dentry
18507 +#define dst_parent     sd[AuDST].parent
18508 +#define dst_h_parent   sd[AuDST].h_parent
18509 +#define dst_wh_dentry  sd[AuDST].wh_dentry
18510 +#define dst_hdir       sd[AuDST].hdir
18511 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
18512 +#define dst_dt         sd[AuDST].dt
18513 +#define dst_bstart     sd[AuDST].bstart
18514 +
18515 +       struct dentry *h_trap;
18516 +       struct au_branch *br;
18517 +       struct au_hinode *src_hinode;
18518 +       struct path h_path;
18519 +       struct au_nhash whlist;
18520 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
18521 +
18522 +       unsigned int flags;
18523 +
18524 +       struct au_whtmp_rmdir *thargs;
18525 +       struct dentry *h_dst;
18526 +};
18527 +
18528 +/* ---------------------------------------------------------------------- */
18529 +
18530 +/*
18531 + * functions for reverting.
18532 + * when an error happened in a single rename systemcall, we should revert
18533 + * everything as if nothing happend.
18534 + * we don't need to revert the copied-up/down the parent dir since they are
18535 + * harmless.
18536 + */
18537 +
18538 +#define RevertFailure(fmt, ...) do { \
18539 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
18540 +               ##__VA_ARGS__, err, rerr); \
18541 +       err = -EIO; \
18542 +} while (0)
18543 +
18544 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
18545 +{
18546 +       int rerr;
18547 +
18548 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
18549 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
18550 +       au_hn_imtx_unlock(a->src_hinode);
18551 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
18552 +       if (rerr)
18553 +               RevertFailure("remove diropq %pd", a->src_dentry);
18554 +}
18555 +
18556 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
18557 +{
18558 +       int rerr;
18559 +       struct inode *delegated;
18560 +
18561 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
18562 +                                         a->src_h_parent);
18563 +       rerr = PTR_ERR(a->h_path.dentry);
18564 +       if (IS_ERR(a->h_path.dentry)) {
18565 +               RevertFailure("lkup one %pd", a->src_dentry);
18566 +               return;
18567 +       }
18568 +
18569 +       delegated = NULL;
18570 +       rerr = vfsub_rename(a->dst_h_dir,
18571 +                           au_h_dptr(a->src_dentry, a->btgt),
18572 +                           a->src_h_dir, &a->h_path, &delegated);
18573 +       if (unlikely(rerr == -EWOULDBLOCK)) {
18574 +               pr_warn("cannot retry for NFSv4 delegation"
18575 +                       " for an internal rename\n");
18576 +               iput(delegated);
18577 +       }
18578 +       d_drop(a->h_path.dentry);
18579 +       dput(a->h_path.dentry);
18580 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
18581 +       if (rerr)
18582 +               RevertFailure("rename %pd", a->src_dentry);
18583 +}
18584 +
18585 +static void au_ren_rev_cpup(int err, struct au_ren_args *a)
18586 +{
18587 +       int rerr;
18588 +
18589 +       a->h_path.dentry = a->dst_h_dentry;
18590 +       /* no delegation since it is just created */
18591 +       rerr = vfsub_unlink(a->dst_h_dir, &a->h_path, /*delegated*/NULL,
18592 +                           /*force*/0);
18593 +       au_set_h_dptr(a->src_dentry, a->btgt, NULL);
18594 +       au_set_dbstart(a->src_dentry, a->src_bstart);
18595 +       if (rerr)
18596 +               RevertFailure("unlink %pd", a->dst_h_dentry);
18597 +}
18598 +
18599 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
18600 +{
18601 +       int rerr;
18602 +       struct inode *delegated;
18603 +
18604 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
18605 +                                         a->dst_h_parent);
18606 +       rerr = PTR_ERR(a->h_path.dentry);
18607 +       if (IS_ERR(a->h_path.dentry)) {
18608 +               RevertFailure("lkup one %pd", a->dst_dentry);
18609 +               return;
18610 +       }
18611 +       if (a->h_path.dentry->d_inode) {
18612 +               d_drop(a->h_path.dentry);
18613 +               dput(a->h_path.dentry);
18614 +               return;
18615 +       }
18616 +
18617 +       delegated = NULL;
18618 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
18619 +                           &delegated);
18620 +       if (unlikely(rerr == -EWOULDBLOCK)) {
18621 +               pr_warn("cannot retry for NFSv4 delegation"
18622 +                       " for an internal rename\n");
18623 +               iput(delegated);
18624 +       }
18625 +       d_drop(a->h_path.dentry);
18626 +       dput(a->h_path.dentry);
18627 +       if (!rerr)
18628 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
18629 +       else
18630 +               RevertFailure("rename %pd", a->h_dst);
18631 +}
18632 +
18633 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
18634 +{
18635 +       int rerr;
18636 +
18637 +       a->h_path.dentry = a->src_wh_dentry;
18638 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
18639 +       au_set_dbwh(a->src_dentry, a->src_bwh);
18640 +       if (rerr)
18641 +               RevertFailure("unlink %pd", a->src_wh_dentry);
18642 +}
18643 +#undef RevertFailure
18644 +
18645 +/* ---------------------------------------------------------------------- */
18646 +
18647 +/*
18648 + * when we have to copyup the renaming entry, do it with the rename-target name
18649 + * in order to minimize the cost (the later actual rename is unnecessary).
18650 + * otherwise rename it on the target branch.
18651 + */
18652 +static int au_ren_or_cpup(struct au_ren_args *a)
18653 +{
18654 +       int err;
18655 +       struct dentry *d;
18656 +       struct inode *delegated;
18657 +
18658 +       d = a->src_dentry;
18659 +       if (au_dbstart(d) == a->btgt) {
18660 +               a->h_path.dentry = a->dst_h_dentry;
18661 +               if (au_ftest_ren(a->flags, DIROPQ)
18662 +                   && au_dbdiropq(d) == a->btgt)
18663 +                       au_fclr_ren(a->flags, DIROPQ);
18664 +               AuDebugOn(au_dbstart(d) != a->btgt);
18665 +               delegated = NULL;
18666 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
18667 +                                  a->dst_h_dir, &a->h_path, &delegated);
18668 +               if (unlikely(err == -EWOULDBLOCK)) {
18669 +                       pr_warn("cannot retry for NFSv4 delegation"
18670 +                               " for an internal rename\n");
18671 +                       iput(delegated);
18672 +               }
18673 +       } else
18674 +               BUG();
18675 +
18676 +       if (!err && a->h_dst)
18677 +               /* it will be set to dinfo later */
18678 +               dget(a->h_dst);
18679 +
18680 +       return err;
18681 +}
18682 +
18683 +/* cf. aufs_rmdir() */
18684 +static int au_ren_del_whtmp(struct au_ren_args *a)
18685 +{
18686 +       int err;
18687 +       struct inode *dir;
18688 +
18689 +       dir = a->dst_dir;
18690 +       SiMustAnyLock(dir->i_sb);
18691 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
18692 +                                    au_sbi(dir->i_sb)->si_dirwh)
18693 +           || au_test_fs_remote(a->h_dst->d_sb)) {
18694 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
18695 +               if (unlikely(err))
18696 +                       pr_warn("failed removing whtmp dir %pd (%d), "
18697 +                               "ignored.\n", a->h_dst, err);
18698 +       } else {
18699 +               au_nhash_wh_free(&a->thargs->whlist);
18700 +               a->thargs->whlist = a->whlist;
18701 +               a->whlist.nh_num = 0;
18702 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
18703 +               dput(a->h_dst);
18704 +               a->thargs = NULL;
18705 +       }
18706 +
18707 +       return 0;
18708 +}
18709 +
18710 +/* make it 'opaque' dir. */
18711 +static int au_ren_diropq(struct au_ren_args *a)
18712 +{
18713 +       int err;
18714 +       struct dentry *diropq;
18715 +
18716 +       err = 0;
18717 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
18718 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
18719 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
18720 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
18721 +       au_hn_imtx_unlock(a->src_hinode);
18722 +       if (IS_ERR(diropq))
18723 +               err = PTR_ERR(diropq);
18724 +       dput(diropq);
18725 +
18726 +       return err;
18727 +}
18728 +
18729 +static int do_rename(struct au_ren_args *a)
18730 +{
18731 +       int err;
18732 +       struct dentry *d, *h_d;
18733 +
18734 +       /* prepare workqueue args for asynchronous rmdir */
18735 +       h_d = a->dst_h_dentry;
18736 +       if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
18737 +               err = -ENOMEM;
18738 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
18739 +               if (unlikely(!a->thargs))
18740 +                       goto out;
18741 +               a->h_dst = dget(h_d);
18742 +       }
18743 +
18744 +       /* create whiteout for src_dentry */
18745 +       if (au_ftest_ren(a->flags, WHSRC)) {
18746 +               a->src_bwh = au_dbwh(a->src_dentry);
18747 +               AuDebugOn(a->src_bwh >= 0);
18748 +               a->src_wh_dentry
18749 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
18750 +               err = PTR_ERR(a->src_wh_dentry);
18751 +               if (IS_ERR(a->src_wh_dentry))
18752 +                       goto out_thargs;
18753 +       }
18754 +
18755 +       /* lookup whiteout for dentry */
18756 +       if (au_ftest_ren(a->flags, WHDST)) {
18757 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
18758 +                                a->br);
18759 +               err = PTR_ERR(h_d);
18760 +               if (IS_ERR(h_d))
18761 +                       goto out_whsrc;
18762 +               if (!h_d->d_inode)
18763 +                       dput(h_d);
18764 +               else
18765 +                       a->dst_wh_dentry = h_d;
18766 +       }
18767 +
18768 +       /* rename dentry to tmpwh */
18769 +       if (a->thargs) {
18770 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
18771 +               if (unlikely(err))
18772 +                       goto out_whdst;
18773 +
18774 +               d = a->dst_dentry;
18775 +               au_set_h_dptr(d, a->btgt, NULL);
18776 +               err = au_lkup_neg(d, a->btgt, /*wh*/0);
18777 +               if (unlikely(err))
18778 +                       goto out_whtmp;
18779 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
18780 +       }
18781 +
18782 +       BUG_ON(a->dst_h_dentry->d_inode && a->src_bstart != a->btgt);
18783 +
18784 +       /* rename by vfs_rename or cpup */
18785 +       d = a->dst_dentry;
18786 +       if (au_ftest_ren(a->flags, ISDIR)
18787 +           && (a->dst_wh_dentry
18788 +               || au_dbdiropq(d) == a->btgt
18789 +               /* hide the lower to keep xino */
18790 +               || a->btgt < au_dbend(d)
18791 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
18792 +               au_fset_ren(a->flags, DIROPQ);
18793 +       err = au_ren_or_cpup(a);
18794 +       if (unlikely(err))
18795 +               /* leave the copied-up one */
18796 +               goto out_whtmp;
18797 +
18798 +       /* make dir opaque */
18799 +       if (au_ftest_ren(a->flags, DIROPQ)) {
18800 +               err = au_ren_diropq(a);
18801 +               if (unlikely(err))
18802 +                       goto out_rename;
18803 +       }
18804 +
18805 +       /* update target timestamps */
18806 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
18807 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
18808 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
18809 +       a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
18810 +
18811 +       /* remove whiteout for dentry */
18812 +       if (a->dst_wh_dentry) {
18813 +               a->h_path.dentry = a->dst_wh_dentry;
18814 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
18815 +                                         a->dst_dentry);
18816 +               if (unlikely(err))
18817 +                       goto out_diropq;
18818 +       }
18819 +
18820 +       /* remove whtmp */
18821 +       if (a->thargs)
18822 +               au_ren_del_whtmp(a); /* ignore this error */
18823 +
18824 +       err = 0;
18825 +       goto out_success;
18826 +
18827 +out_diropq:
18828 +       if (au_ftest_ren(a->flags, DIROPQ))
18829 +               au_ren_rev_diropq(err, a);
18830 +out_rename:
18831 +       if (!au_ftest_ren(a->flags, CPUP))
18832 +               au_ren_rev_rename(err, a);
18833 +       else
18834 +               au_ren_rev_cpup(err, a);
18835 +       dput(a->h_dst);
18836 +out_whtmp:
18837 +       if (a->thargs)
18838 +               au_ren_rev_whtmp(err, a);
18839 +out_whdst:
18840 +       dput(a->dst_wh_dentry);
18841 +       a->dst_wh_dentry = NULL;
18842 +out_whsrc:
18843 +       if (a->src_wh_dentry)
18844 +               au_ren_rev_whsrc(err, a);
18845 +out_success:
18846 +       dput(a->src_wh_dentry);
18847 +       dput(a->dst_wh_dentry);
18848 +out_thargs:
18849 +       if (a->thargs) {
18850 +               dput(a->h_dst);
18851 +               au_whtmp_rmdir_free(a->thargs);
18852 +               a->thargs = NULL;
18853 +       }
18854 +out:
18855 +       return err;
18856 +}
18857 +
18858 +/* ---------------------------------------------------------------------- */
18859 +
18860 +/*
18861 + * test if @dentry dir can be rename destination or not.
18862 + * success means, it is a logically empty dir.
18863 + */
18864 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
18865 +{
18866 +       return au_test_empty(dentry, whlist);
18867 +}
18868 +
18869 +/*
18870 + * test if @dentry dir can be rename source or not.
18871 + * if it can, return 0 and @children is filled.
18872 + * success means,
18873 + * - it is a logically empty dir.
18874 + * - or, it exists on writable branch and has no children including whiteouts
18875 + *       on the lower branch.
18876 + */
18877 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
18878 +{
18879 +       int err;
18880 +       unsigned int rdhash;
18881 +       aufs_bindex_t bstart;
18882 +
18883 +       bstart = au_dbstart(dentry);
18884 +       if (bstart != btgt) {
18885 +               struct au_nhash whlist;
18886 +
18887 +               SiMustAnyLock(dentry->d_sb);
18888 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
18889 +               if (!rdhash)
18890 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
18891 +                                                          dentry));
18892 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
18893 +               if (unlikely(err))
18894 +                       goto out;
18895 +               err = au_test_empty(dentry, &whlist);
18896 +               au_nhash_wh_free(&whlist);
18897 +               goto out;
18898 +       }
18899 +
18900 +       if (bstart == au_dbtaildir(dentry))
18901 +               return 0; /* success */
18902 +
18903 +       err = au_test_empty_lower(dentry);
18904 +
18905 +out:
18906 +       if (err == -ENOTEMPTY) {
18907 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
18908 +                       " is not supported\n");
18909 +               err = -EXDEV;
18910 +       }
18911 +       return err;
18912 +}
18913 +
18914 +/* side effect: sets whlist and h_dentry */
18915 +static int au_ren_may_dir(struct au_ren_args *a)
18916 +{
18917 +       int err;
18918 +       unsigned int rdhash;
18919 +       struct dentry *d;
18920 +
18921 +       d = a->dst_dentry;
18922 +       SiMustAnyLock(d->d_sb);
18923 +
18924 +       err = 0;
18925 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
18926 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
18927 +               if (!rdhash)
18928 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
18929 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
18930 +               if (unlikely(err))
18931 +                       goto out;
18932 +
18933 +               au_set_dbstart(d, a->dst_bstart);
18934 +               err = may_rename_dstdir(d, &a->whlist);
18935 +               au_set_dbstart(d, a->btgt);
18936 +       }
18937 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
18938 +       if (unlikely(err))
18939 +               goto out;
18940 +
18941 +       d = a->src_dentry;
18942 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
18943 +       if (au_ftest_ren(a->flags, ISDIR)) {
18944 +               err = may_rename_srcdir(d, a->btgt);
18945 +               if (unlikely(err)) {
18946 +                       au_nhash_wh_free(&a->whlist);
18947 +                       a->whlist.nh_num = 0;
18948 +               }
18949 +       }
18950 +out:
18951 +       return err;
18952 +}
18953 +
18954 +/* ---------------------------------------------------------------------- */
18955 +
18956 +/*
18957 + * simple tests for rename.
18958 + * following the checks in vfs, plus the parent-child relationship.
18959 + */
18960 +static int au_may_ren(struct au_ren_args *a)
18961 +{
18962 +       int err, isdir;
18963 +       struct inode *h_inode;
18964 +
18965 +       if (a->src_bstart == a->btgt) {
18966 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
18967 +                                au_ftest_ren(a->flags, ISDIR));
18968 +               if (unlikely(err))
18969 +                       goto out;
18970 +               err = -EINVAL;
18971 +               if (unlikely(a->src_h_dentry == a->h_trap))
18972 +                       goto out;
18973 +       }
18974 +
18975 +       err = 0;
18976 +       if (a->dst_bstart != a->btgt)
18977 +               goto out;
18978 +
18979 +       err = -ENOTEMPTY;
18980 +       if (unlikely(a->dst_h_dentry == a->h_trap))
18981 +               goto out;
18982 +
18983 +       err = -EIO;
18984 +       h_inode = a->dst_h_dentry->d_inode;
18985 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
18986 +       if (!a->dst_dentry->d_inode) {
18987 +               if (unlikely(h_inode))
18988 +                       goto out;
18989 +               err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
18990 +                                isdir);
18991 +       } else {
18992 +               if (unlikely(!h_inode || !h_inode->i_nlink))
18993 +                       goto out;
18994 +               err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
18995 +                                isdir);
18996 +               if (unlikely(err))
18997 +                       goto out;
18998 +       }
18999 +
19000 +out:
19001 +       if (unlikely(err == -ENOENT || err == -EEXIST))
19002 +               err = -EIO;
19003 +       AuTraceErr(err);
19004 +       return err;
19005 +}
19006 +
19007 +/* ---------------------------------------------------------------------- */
19008 +
19009 +/*
19010 + * locking order
19011 + * (VFS)
19012 + * - src_dir and dir by lock_rename()
19013 + * - inode if exitsts
19014 + * (aufs)
19015 + * - lock all
19016 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
19017 + *     + si_read_lock
19018 + *     + di_write_lock2_child()
19019 + *       + di_write_lock_child()
19020 + *        + ii_write_lock_child()
19021 + *       + di_write_lock_child2()
19022 + *        + ii_write_lock_child2()
19023 + *     + src_parent and parent
19024 + *       + di_write_lock_parent()
19025 + *        + ii_write_lock_parent()
19026 + *       + di_write_lock_parent2()
19027 + *        + ii_write_lock_parent2()
19028 + *   + lower src_dir and dir by vfsub_lock_rename()
19029 + *   + verify the every relationships between child and parent. if any
19030 + *     of them failed, unlock all and return -EBUSY.
19031 + */
19032 +static void au_ren_unlock(struct au_ren_args *a)
19033 +{
19034 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
19035 +                           a->dst_h_parent, a->dst_hdir);
19036 +       if (au_ftest_ren(a->flags, MNT_WRITE))
19037 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
19038 +}
19039 +
19040 +static int au_ren_lock(struct au_ren_args *a)
19041 +{
19042 +       int err;
19043 +       unsigned int udba;
19044 +
19045 +       err = 0;
19046 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
19047 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
19048 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
19049 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
19050 +
19051 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
19052 +       if (unlikely(err))
19053 +               goto out;
19054 +       au_fset_ren(a->flags, MNT_WRITE);
19055 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
19056 +                                     a->dst_h_parent, a->dst_hdir);
19057 +       udba = au_opt_udba(a->src_dentry->d_sb);
19058 +       if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
19059 +                    || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
19060 +               err = au_busy_or_stale();
19061 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
19062 +               err = au_h_verify(a->src_h_dentry, udba,
19063 +                                 a->src_h_parent->d_inode, a->src_h_parent,
19064 +                                 a->br);
19065 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
19066 +               err = au_h_verify(a->dst_h_dentry, udba,
19067 +                                 a->dst_h_parent->d_inode, a->dst_h_parent,
19068 +                                 a->br);
19069 +       if (!err)
19070 +               goto out; /* success */
19071 +
19072 +       err = au_busy_or_stale();
19073 +       au_ren_unlock(a);
19074 +
19075 +out:
19076 +       return err;
19077 +}
19078 +
19079 +/* ---------------------------------------------------------------------- */
19080 +
19081 +static void au_ren_refresh_dir(struct au_ren_args *a)
19082 +{
19083 +       struct inode *dir;
19084 +
19085 +       dir = a->dst_dir;
19086 +       dir->i_version++;
19087 +       if (au_ftest_ren(a->flags, ISDIR)) {
19088 +               /* is this updating defined in POSIX? */
19089 +               au_cpup_attr_timesizes(a->src_inode);
19090 +               au_cpup_attr_nlink(dir, /*force*/1);
19091 +       }
19092 +
19093 +       if (au_ibstart(dir) == a->btgt)
19094 +               au_cpup_attr_timesizes(dir);
19095 +
19096 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
19097 +               return;
19098 +
19099 +       dir = a->src_dir;
19100 +       dir->i_version++;
19101 +       if (au_ftest_ren(a->flags, ISDIR))
19102 +               au_cpup_attr_nlink(dir, /*force*/1);
19103 +       if (au_ibstart(dir) == a->btgt)
19104 +               au_cpup_attr_timesizes(dir);
19105 +}
19106 +
19107 +static void au_ren_refresh(struct au_ren_args *a)
19108 +{
19109 +       aufs_bindex_t bend, bindex;
19110 +       struct dentry *d, *h_d;
19111 +       struct inode *i, *h_i;
19112 +       struct super_block *sb;
19113 +
19114 +       d = a->dst_dentry;
19115 +       d_drop(d);
19116 +       if (a->h_dst)
19117 +               /* already dget-ed by au_ren_or_cpup() */
19118 +               au_set_h_dptr(d, a->btgt, a->h_dst);
19119 +
19120 +       i = a->dst_inode;
19121 +       if (i) {
19122 +               if (!au_ftest_ren(a->flags, ISDIR))
19123 +                       vfsub_drop_nlink(i);
19124 +               else {
19125 +                       vfsub_dead_dir(i);
19126 +                       au_cpup_attr_timesizes(i);
19127 +               }
19128 +               au_update_dbrange(d, /*do_put_zero*/1);
19129 +       } else {
19130 +               bend = a->btgt;
19131 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
19132 +                       au_set_h_dptr(d, bindex, NULL);
19133 +               bend = au_dbend(d);
19134 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
19135 +                       au_set_h_dptr(d, bindex, NULL);
19136 +               au_update_dbrange(d, /*do_put_zero*/0);
19137 +       }
19138 +
19139 +       d = a->src_dentry;
19140 +       au_set_dbwh(d, -1);
19141 +       bend = au_dbend(d);
19142 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
19143 +               h_d = au_h_dptr(d, bindex);
19144 +               if (h_d)
19145 +                       au_set_h_dptr(d, bindex, NULL);
19146 +       }
19147 +       au_set_dbend(d, a->btgt);
19148 +
19149 +       sb = d->d_sb;
19150 +       i = a->src_inode;
19151 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
19152 +               return; /* success */
19153 +
19154 +       bend = au_ibend(i);
19155 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
19156 +               h_i = au_h_iptr(i, bindex);
19157 +               if (h_i) {
19158 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
19159 +                       /* ignore this error */
19160 +                       au_set_h_iptr(i, bindex, NULL, 0);
19161 +               }
19162 +       }
19163 +       au_set_ibend(i, a->btgt);
19164 +}
19165 +
19166 +/* ---------------------------------------------------------------------- */
19167 +
19168 +/* mainly for link(2) and rename(2) */
19169 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
19170 +{
19171 +       aufs_bindex_t bdiropq, bwh;
19172 +       struct dentry *parent;
19173 +       struct au_branch *br;
19174 +
19175 +       parent = dentry->d_parent;
19176 +       IMustLock(parent->d_inode); /* dir is locked */
19177 +
19178 +       bdiropq = au_dbdiropq(parent);
19179 +       bwh = au_dbwh(dentry);
19180 +       br = au_sbr(dentry->d_sb, btgt);
19181 +       if (au_br_rdonly(br)
19182 +           || (0 <= bdiropq && bdiropq < btgt)
19183 +           || (0 <= bwh && bwh < btgt))
19184 +               btgt = -1;
19185 +
19186 +       AuDbg("btgt %d\n", btgt);
19187 +       return btgt;
19188 +}
19189 +
19190 +/* sets src_bstart, dst_bstart and btgt */
19191 +static int au_ren_wbr(struct au_ren_args *a)
19192 +{
19193 +       int err;
19194 +       struct au_wr_dir_args wr_dir_args = {
19195 +               /* .force_btgt  = -1, */
19196 +               .flags          = AuWrDir_ADD_ENTRY
19197 +       };
19198 +
19199 +       a->src_bstart = au_dbstart(a->src_dentry);
19200 +       a->dst_bstart = au_dbstart(a->dst_dentry);
19201 +       if (au_ftest_ren(a->flags, ISDIR))
19202 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
19203 +       wr_dir_args.force_btgt = a->src_bstart;
19204 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
19205 +               wr_dir_args.force_btgt = a->dst_bstart;
19206 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
19207 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
19208 +       a->btgt = err;
19209 +
19210 +       return err;
19211 +}
19212 +
19213 +static void au_ren_dt(struct au_ren_args *a)
19214 +{
19215 +       a->h_path.dentry = a->src_h_parent;
19216 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
19217 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
19218 +               a->h_path.dentry = a->dst_h_parent;
19219 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
19220 +       }
19221 +
19222 +       au_fclr_ren(a->flags, DT_DSTDIR);
19223 +       if (!au_ftest_ren(a->flags, ISDIR))
19224 +               return;
19225 +
19226 +       a->h_path.dentry = a->src_h_dentry;
19227 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
19228 +       if (a->dst_h_dentry->d_inode) {
19229 +               au_fset_ren(a->flags, DT_DSTDIR);
19230 +               a->h_path.dentry = a->dst_h_dentry;
19231 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
19232 +       }
19233 +}
19234 +
19235 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
19236 +{
19237 +       struct dentry *h_d;
19238 +       struct mutex *h_mtx;
19239 +
19240 +       au_dtime_revert(a->src_dt + AuPARENT);
19241 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
19242 +               au_dtime_revert(a->dst_dt + AuPARENT);
19243 +
19244 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
19245 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
19246 +               h_mtx = &h_d->d_inode->i_mutex;
19247 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
19248 +               au_dtime_revert(a->src_dt + AuCHILD);
19249 +               mutex_unlock(h_mtx);
19250 +
19251 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
19252 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
19253 +                       h_mtx = &h_d->d_inode->i_mutex;
19254 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
19255 +                       au_dtime_revert(a->dst_dt + AuCHILD);
19256 +                       mutex_unlock(h_mtx);
19257 +               }
19258 +       }
19259 +}
19260 +
19261 +/* ---------------------------------------------------------------------- */
19262 +
19263 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
19264 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
19265 +{
19266 +       int err, flags;
19267 +       /* reduce stack space */
19268 +       struct au_ren_args *a;
19269 +
19270 +       AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
19271 +       IMustLock(_src_dir);
19272 +       IMustLock(_dst_dir);
19273 +
19274 +       err = -ENOMEM;
19275 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
19276 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19277 +       if (unlikely(!a))
19278 +               goto out;
19279 +
19280 +       a->src_dir = _src_dir;
19281 +       a->src_dentry = _src_dentry;
19282 +       a->src_inode = a->src_dentry->d_inode;
19283 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
19284 +       a->dst_dir = _dst_dir;
19285 +       a->dst_dentry = _dst_dentry;
19286 +       a->dst_inode = a->dst_dentry->d_inode;
19287 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
19288 +       if (a->dst_inode) {
19289 +               IMustLock(a->dst_inode);
19290 +               au_igrab(a->dst_inode);
19291 +       }
19292 +
19293 +       err = -ENOTDIR;
19294 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
19295 +       if (S_ISDIR(a->src_inode->i_mode)) {
19296 +               au_fset_ren(a->flags, ISDIR);
19297 +               if (unlikely(a->dst_inode && !S_ISDIR(a->dst_inode->i_mode)))
19298 +                       goto out_free;
19299 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
19300 +                                               AuLock_DIR | flags);
19301 +       } else
19302 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
19303 +                                               flags);
19304 +       if (unlikely(err))
19305 +               goto out_free;
19306 +
19307 +       err = au_d_hashed_positive(a->src_dentry);
19308 +       if (unlikely(err))
19309 +               goto out_unlock;
19310 +       err = -ENOENT;
19311 +       if (a->dst_inode) {
19312 +               /*
19313 +                * If it is a dir, VFS unhash dst_dentry before this
19314 +                * function. It means we cannot rely upon d_unhashed().
19315 +                */
19316 +               if (unlikely(!a->dst_inode->i_nlink))
19317 +                       goto out_unlock;
19318 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
19319 +                       err = au_d_hashed_positive(a->dst_dentry);
19320 +                       if (unlikely(err))
19321 +                               goto out_unlock;
19322 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
19323 +                       goto out_unlock;
19324 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
19325 +               goto out_unlock;
19326 +
19327 +       /*
19328 +        * is it possible?
19329 +        * yes, it happend (in linux-3.3-rcN) but I don't know why.
19330 +        * there may exist a problem somewhere else.
19331 +        */
19332 +       err = -EINVAL;
19333 +       if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
19334 +               goto out_unlock;
19335 +
19336 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
19337 +       di_write_lock_parent(a->dst_parent);
19338 +
19339 +       /* which branch we process */
19340 +       err = au_ren_wbr(a);
19341 +       if (unlikely(err < 0))
19342 +               goto out_parent;
19343 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
19344 +       a->h_path.mnt = au_br_mnt(a->br);
19345 +
19346 +       /* are they available to be renamed */
19347 +       err = au_ren_may_dir(a);
19348 +       if (unlikely(err))
19349 +               goto out_children;
19350 +
19351 +       /* prepare the writable parent dir on the same branch */
19352 +       if (a->dst_bstart == a->btgt) {
19353 +               au_fset_ren(a->flags, WHDST);
19354 +       } else {
19355 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
19356 +               if (unlikely(err))
19357 +                       goto out_children;
19358 +       }
19359 +
19360 +       if (a->src_dir != a->dst_dir) {
19361 +               /*
19362 +                * this temporary unlock is safe,
19363 +                * because both dir->i_mutex are locked.
19364 +                */
19365 +               di_write_unlock(a->dst_parent);
19366 +               di_write_lock_parent(a->src_parent);
19367 +               err = au_wr_dir_need_wh(a->src_dentry,
19368 +                                       au_ftest_ren(a->flags, ISDIR),
19369 +                                       &a->btgt);
19370 +               di_write_unlock(a->src_parent);
19371 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
19372 +               au_fclr_ren(a->flags, ISSAMEDIR);
19373 +       } else
19374 +               err = au_wr_dir_need_wh(a->src_dentry,
19375 +                                       au_ftest_ren(a->flags, ISDIR),
19376 +                                       &a->btgt);
19377 +       if (unlikely(err < 0))
19378 +               goto out_children;
19379 +       if (err)
19380 +               au_fset_ren(a->flags, WHSRC);
19381 +
19382 +       /* cpup src */
19383 +       if (a->src_bstart != a->btgt) {
19384 +               struct au_pin pin;
19385 +
19386 +               err = au_pin(&pin, a->src_dentry, a->btgt,
19387 +                            au_opt_udba(a->src_dentry->d_sb),
19388 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19389 +               if (!err) {
19390 +                       struct au_cp_generic cpg = {
19391 +                               .dentry = a->src_dentry,
19392 +                               .bdst   = a->btgt,
19393 +                               .bsrc   = a->src_bstart,
19394 +                               .len    = -1,
19395 +                               .pin    = &pin,
19396 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
19397 +                       };
19398 +                       AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
19399 +                       err = au_sio_cpup_simple(&cpg);
19400 +                       au_unpin(&pin);
19401 +               }
19402 +               if (unlikely(err))
19403 +                       goto out_children;
19404 +               a->src_bstart = a->btgt;
19405 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
19406 +               au_fset_ren(a->flags, WHSRC);
19407 +       }
19408 +
19409 +       /* lock them all */
19410 +       err = au_ren_lock(a);
19411 +       if (unlikely(err))
19412 +               /* leave the copied-up one */
19413 +               goto out_children;
19414 +
19415 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
19416 +               err = au_may_ren(a);
19417 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
19418 +               err = -ENAMETOOLONG;
19419 +       if (unlikely(err))
19420 +               goto out_hdir;
19421 +
19422 +       /* store timestamps to be revertible */
19423 +       au_ren_dt(a);
19424 +
19425 +       /* here we go */
19426 +       err = do_rename(a);
19427 +       if (unlikely(err))
19428 +               goto out_dt;
19429 +
19430 +       /* update dir attributes */
19431 +       au_ren_refresh_dir(a);
19432 +
19433 +       /* dput/iput all lower dentries */
19434 +       au_ren_refresh(a);
19435 +
19436 +       goto out_hdir; /* success */
19437 +
19438 +out_dt:
19439 +       au_ren_rev_dt(err, a);
19440 +out_hdir:
19441 +       au_ren_unlock(a);
19442 +out_children:
19443 +       au_nhash_wh_free(&a->whlist);
19444 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
19445 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
19446 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
19447 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
19448 +       }
19449 +out_parent:
19450 +       if (!err)
19451 +               d_move(a->src_dentry, a->dst_dentry);
19452 +       else {
19453 +               au_update_dbstart(a->dst_dentry);
19454 +               if (!a->dst_inode)
19455 +                       d_drop(a->dst_dentry);
19456 +       }
19457 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
19458 +               di_write_unlock(a->dst_parent);
19459 +       else
19460 +               di_write_unlock2(a->src_parent, a->dst_parent);
19461 +out_unlock:
19462 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
19463 +out_free:
19464 +       iput(a->dst_inode);
19465 +       if (a->thargs)
19466 +               au_whtmp_rmdir_free(a->thargs);
19467 +       kfree(a);
19468 +out:
19469 +       AuTraceErr(err);
19470 +       return err;
19471 +}
19472 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
19473 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
19474 +++ linux/fs/aufs/Kconfig       2014-06-24 00:14:06.776052275 +0200
19475 @@ -0,0 +1,168 @@
19476 +config AUFS_FS
19477 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
19478 +       help
19479 +       Aufs is a stackable unification filesystem such as Unionfs,
19480 +       which unifies several directories and provides a merged single
19481 +       directory.
19482 +       In the early days, aufs was entirely re-designed and
19483 +       re-implemented Unionfs Version 1.x series. Introducing many
19484 +       original ideas, approaches and improvements, it becomes totally
19485 +       different from Unionfs while keeping the basic features.
19486 +
19487 +if AUFS_FS
19488 +choice
19489 +       prompt "Maximum number of branches"
19490 +       default AUFS_BRANCH_MAX_127
19491 +       help
19492 +       Specifies the maximum number of branches (or member directories)
19493 +       in a single aufs. The larger value consumes more system
19494 +       resources and has a minor impact to performance.
19495 +config AUFS_BRANCH_MAX_127
19496 +       bool "127"
19497 +       help
19498 +       Specifies the maximum number of branches (or member directories)
19499 +       in a single aufs. The larger value consumes more system
19500 +       resources and has a minor impact to performance.
19501 +config AUFS_BRANCH_MAX_511
19502 +       bool "511"
19503 +       help
19504 +       Specifies the maximum number of branches (or member directories)
19505 +       in a single aufs. The larger value consumes more system
19506 +       resources and has a minor impact to performance.
19507 +config AUFS_BRANCH_MAX_1023
19508 +       bool "1023"
19509 +       help
19510 +       Specifies the maximum number of branches (or member directories)
19511 +       in a single aufs. The larger value consumes more system
19512 +       resources and has a minor impact to performance.
19513 +config AUFS_BRANCH_MAX_32767
19514 +       bool "32767"
19515 +       help
19516 +       Specifies the maximum number of branches (or member directories)
19517 +       in a single aufs. The larger value consumes more system
19518 +       resources and has a minor impact to performance.
19519 +endchoice
19520 +
19521 +config AUFS_SBILIST
19522 +       bool
19523 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
19524 +       default y
19525 +       help
19526 +       Automatic configuration for internal use.
19527 +       When aufs supports Magic SysRq or /proc, enabled automatically.
19528 +
19529 +config AUFS_HNOTIFY
19530 +       bool "Detect direct branch access (bypassing aufs)"
19531 +       help
19532 +       If you want to modify files on branches directly, eg. bypassing aufs,
19533 +       and want aufs to detect the changes of them fully, then enable this
19534 +       option and use 'udba=notify' mount option.
19535 +       Currently there is only one available configuration, "fsnotify".
19536 +       It will have a negative impact to the performance.
19537 +       See detail in aufs.5.
19538 +
19539 +choice
19540 +       prompt "method" if AUFS_HNOTIFY
19541 +       default AUFS_HFSNOTIFY
19542 +config AUFS_HFSNOTIFY
19543 +       bool "fsnotify"
19544 +       select FSNOTIFY
19545 +endchoice
19546 +
19547 +config AUFS_EXPORT
19548 +       bool "NFS-exportable aufs"
19549 +       depends on EXPORTFS
19550 +       help
19551 +       If you want to export your mounted aufs via NFS, then enable this
19552 +       option. There are several requirements for this configuration.
19553 +       See detail in aufs.5.
19554 +
19555 +config AUFS_INO_T_64
19556 +       bool
19557 +       depends on AUFS_EXPORT
19558 +       depends on 64BIT && !(ALPHA || S390)
19559 +       default y
19560 +       help
19561 +       Automatic configuration for internal use.
19562 +       /* typedef unsigned long/int __kernel_ino_t */
19563 +       /* alpha and s390x are int */
19564 +
19565 +config AUFS_RDU
19566 +       bool "Readdir in userspace"
19567 +       help
19568 +       Aufs has two methods to provide a merged view for a directory,
19569 +       by a user-space library and by kernel-space natively. The latter
19570 +       is always enabled but sometimes large and slow.
19571 +       If you enable this option, install the library in aufs2-util
19572 +       package, and set some environment variables for your readdir(3),
19573 +       then the work will be handled in user-space which generally
19574 +       shows better performance in most cases.
19575 +       See detail in aufs.5.
19576 +
19577 +config AUFS_SHWH
19578 +       bool "Show whiteouts"
19579 +       help
19580 +       If you want to make the whiteouts in aufs visible, then enable
19581 +       this option and specify 'shwh' mount option. Although it may
19582 +       sounds like philosophy or something, but in technically it
19583 +       simply shows the name of whiteout with keeping its behaviour.
19584 +
19585 +config AUFS_BR_RAMFS
19586 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
19587 +       help
19588 +       If you want to use ramfs as an aufs branch fs, then enable this
19589 +       option. Generally tmpfs is recommended.
19590 +       Aufs prohibited them to be a branch fs by default, because
19591 +       initramfs becomes unusable after switch_root or something
19592 +       generally. If you sets initramfs as an aufs branch and boot your
19593 +       system by switch_root, you will meet a problem easily since the
19594 +       files in initramfs may be inaccessible.
19595 +       Unless you are going to use ramfs as an aufs branch fs without
19596 +       switch_root or something, leave it N.
19597 +
19598 +config AUFS_BR_FUSE
19599 +       bool "Fuse fs as an aufs branch"
19600 +       depends on FUSE_FS
19601 +       select AUFS_POLL
19602 +       help
19603 +       If you want to use fuse-based userspace filesystem as an aufs
19604 +       branch fs, then enable this option.
19605 +       It implements the internal poll(2) operation which is
19606 +       implemented by fuse only (curretnly).
19607 +
19608 +config AUFS_POLL
19609 +       bool
19610 +       help
19611 +       Automatic configuration for internal use.
19612 +
19613 +config AUFS_BR_HFSPLUS
19614 +       bool "Hfsplus as an aufs branch"
19615 +       depends on HFSPLUS_FS
19616 +       default y
19617 +       help
19618 +       If you want to use hfsplus fs as an aufs branch fs, then enable
19619 +       this option. This option introduces a small overhead at
19620 +       copying-up a file on hfsplus.
19621 +
19622 +config AUFS_BDEV_LOOP
19623 +       bool
19624 +       depends on BLK_DEV_LOOP
19625 +       default y
19626 +       help
19627 +       Automatic configuration for internal use.
19628 +       Convert =[ym] into =y.
19629 +
19630 +config AUFS_DEBUG
19631 +       bool "Debug aufs"
19632 +       help
19633 +       Enable this to compile aufs internal debug code.
19634 +       It will have a negative impact to the performance.
19635 +
19636 +config AUFS_MAGIC_SYSRQ
19637 +       bool
19638 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
19639 +       default y
19640 +       help
19641 +       Automatic configuration for internal use.
19642 +       When aufs supports Magic SysRq, enabled automatically.
19643 +endif
19644 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
19645 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
19646 +++ linux/fs/aufs/loop.c        2014-06-24 00:14:06.786052822 +0200
19647 @@ -0,0 +1,145 @@
19648 +/*
19649 + * Copyright (C) 2005-2014 Junjiro R. Okajima
19650 + *
19651 + * This program, aufs is free software; you can redistribute it and/or modify
19652 + * it under the terms of the GNU General Public License as published by
19653 + * the Free Software Foundation; either version 2 of the License, or
19654 + * (at your option) any later version.
19655 + *
19656 + * This program is distributed in the hope that it will be useful,
19657 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19658 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19659 + * GNU General Public License for more details.
19660 + *
19661 + * You should have received a copy of the GNU General Public License
19662 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19663 + */
19664 +
19665 +/*
19666 + * support for loopback block device as a branch
19667 + */
19668 +
19669 +#include "aufs.h"
19670 +
19671 +/* added into drivers/block/loop.c */
19672 +static struct file *(*backing_file_func)(struct super_block *sb);
19673 +
19674 +/*
19675 + * test if two lower dentries have overlapping branches.
19676 + */
19677 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
19678 +{
19679 +       struct super_block *h_sb;
19680 +       struct file *backing_file;
19681 +
19682 +       if (unlikely(!backing_file_func)) {
19683 +               /* don't load "loop" module here */
19684 +               backing_file_func = symbol_get(loop_backing_file);
19685 +               if (unlikely(!backing_file_func))
19686 +                       /* "loop" module is not loaded */
19687 +                       return 0;
19688 +       }
19689 +
19690 +       h_sb = h_adding->d_sb;
19691 +       backing_file = backing_file_func(h_sb);
19692 +       if (!backing_file)
19693 +               return 0;
19694 +
19695 +       h_adding = backing_file->f_dentry;
19696 +       /*
19697 +        * h_adding can be local NFS.
19698 +        * in this case aufs cannot detect the loop.
19699 +        */
19700 +       if (unlikely(h_adding->d_sb == sb))
19701 +               return 1;
19702 +       return !!au_test_subdir(h_adding, sb->s_root);
19703 +}
19704 +
19705 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
19706 +int au_test_loopback_kthread(void)
19707 +{
19708 +       int ret;
19709 +       struct task_struct *tsk = current;
19710 +       char c, comm[sizeof(tsk->comm)];
19711 +
19712 +       ret = 0;
19713 +       if (tsk->flags & PF_KTHREAD) {
19714 +               get_task_comm(comm, tsk);
19715 +               c = comm[4];
19716 +               ret = ('0' <= c && c <= '9'
19717 +                      && !strncmp(comm, "loop", 4));
19718 +       }
19719 +
19720 +       return ret;
19721 +}
19722 +
19723 +/* ---------------------------------------------------------------------- */
19724 +
19725 +#define au_warn_loopback_step  16
19726 +static int au_warn_loopback_nelem = au_warn_loopback_step;
19727 +static unsigned long *au_warn_loopback_array;
19728 +
19729 +void au_warn_loopback(struct super_block *h_sb)
19730 +{
19731 +       int i, new_nelem;
19732 +       unsigned long *a, magic;
19733 +       static DEFINE_SPINLOCK(spin);
19734 +
19735 +       magic = h_sb->s_magic;
19736 +       spin_lock(&spin);
19737 +       a = au_warn_loopback_array;
19738 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
19739 +               if (a[i] == magic) {
19740 +                       spin_unlock(&spin);
19741 +                       return;
19742 +               }
19743 +
19744 +       /* h_sb is new to us, print it */
19745 +       if (i < au_warn_loopback_nelem) {
19746 +               a[i] = magic;
19747 +               goto pr;
19748 +       }
19749 +
19750 +       /* expand the array */
19751 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
19752 +       a = au_kzrealloc(au_warn_loopback_array,
19753 +                        au_warn_loopback_nelem * sizeof(unsigned long),
19754 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
19755 +       if (a) {
19756 +               au_warn_loopback_nelem = new_nelem;
19757 +               au_warn_loopback_array = a;
19758 +               a[i] = magic;
19759 +               goto pr;
19760 +       }
19761 +
19762 +       spin_unlock(&spin);
19763 +       AuWarn1("realloc failed, ignored\n");
19764 +       return;
19765 +
19766 +pr:
19767 +       spin_unlock(&spin);
19768 +       pr_warn("you may want to try another patch for loopback file "
19769 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
19770 +}
19771 +
19772 +int au_loopback_init(void)
19773 +{
19774 +       int err;
19775 +       struct super_block *sb __maybe_unused;
19776 +
19777 +       AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
19778 +
19779 +       err = 0;
19780 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
19781 +                                        sizeof(unsigned long), GFP_NOFS);
19782 +       if (unlikely(!au_warn_loopback_array))
19783 +               err = -ENOMEM;
19784 +
19785 +       return err;
19786 +}
19787 +
19788 +void au_loopback_fin(void)
19789 +{
19790 +       symbol_put(loop_backing_file);
19791 +       kfree(au_warn_loopback_array);
19792 +}
19793 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
19794 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
19795 +++ linux/fs/aufs/loop.h        2014-06-24 00:14:06.786052822 +0200
19796 @@ -0,0 +1,52 @@
19797 +/*
19798 + * Copyright (C) 2005-2014 Junjiro R. Okajima
19799 + *
19800 + * This program, aufs is free software; you can redistribute it and/or modify
19801 + * it under the terms of the GNU General Public License as published by
19802 + * the Free Software Foundation; either version 2 of the License, or
19803 + * (at your option) any later version.
19804 + *
19805 + * This program is distributed in the hope that it will be useful,
19806 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19807 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19808 + * GNU General Public License for more details.
19809 + *
19810 + * You should have received a copy of the GNU General Public License
19811 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19812 + */
19813 +
19814 +/*
19815 + * support for loopback mount as a branch
19816 + */
19817 +
19818 +#ifndef __AUFS_LOOP_H__
19819 +#define __AUFS_LOOP_H__
19820 +
19821 +#ifdef __KERNEL__
19822 +
19823 +struct dentry;
19824 +struct super_block;
19825 +
19826 +#ifdef CONFIG_AUFS_BDEV_LOOP
19827 +/* drivers/block/loop.c */
19828 +struct file *loop_backing_file(struct super_block *sb);
19829 +
19830 +/* loop.c */
19831 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
19832 +int au_test_loopback_kthread(void);
19833 +void au_warn_loopback(struct super_block *h_sb);
19834 +
19835 +int au_loopback_init(void);
19836 +void au_loopback_fin(void);
19837 +#else
19838 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
19839 +          struct dentry *h_adding)
19840 +AuStubInt0(au_test_loopback_kthread, void)
19841 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
19842 +
19843 +AuStubInt0(au_loopback_init, void)
19844 +AuStubVoid(au_loopback_fin, void)
19845 +#endif /* BLK_DEV_LOOP */
19846 +
19847 +#endif /* __KERNEL__ */
19848 +#endif /* __AUFS_LOOP_H__ */
19849 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
19850 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
19851 +++ linux/fs/aufs/magic.mk      2014-06-24 00:14:06.786052822 +0200
19852 @@ -0,0 +1,54 @@
19853 +
19854 +# defined in ${srctree}/fs/fuse/inode.c
19855 +# tristate
19856 +ifdef CONFIG_FUSE_FS
19857 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
19858 +endif
19859 +
19860 +# defined in ${srctree}/fs/ocfs2/ocfs2_fs.h
19861 +# tristate
19862 +ifdef CONFIG_OCFS2_FS
19863 +ccflags-y += -DOCFS2_SUPER_MAGIC=0x7461636f
19864 +endif
19865 +
19866 +# defined in ${srctree}/fs/ocfs2/dlm/userdlm.h
19867 +# tristate
19868 +ifdef CONFIG_OCFS2_FS_O2CB
19869 +ccflags-y += -DDLMFS_MAGIC=0x76a9f425
19870 +endif
19871 +
19872 +# defined in ${srctree}/fs/cifs/cifsfs.c
19873 +# tristate
19874 +ifdef CONFIG_CIFS_FS
19875 +ccflags-y += -DCIFS_MAGIC_NUMBER=0xFF534D42
19876 +endif
19877 +
19878 +# defined in ${srctree}/fs/xfs/xfs_sb.h
19879 +# tristate
19880 +ifdef CONFIG_XFS_FS
19881 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
19882 +endif
19883 +
19884 +# defined in ${srctree}/fs/configfs/mount.c
19885 +# tristate
19886 +ifdef CONFIG_CONFIGFS_FS
19887 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
19888 +endif
19889 +
19890 +# defined in ${srctree}/fs/9p/v9fs.h
19891 +# tristate
19892 +ifdef CONFIG_9P_FS
19893 +ccflags-y += -DV9FS_MAGIC=0x01021997
19894 +endif
19895 +
19896 +# defined in ${srctree}/fs/ubifs/ubifs.h
19897 +# tristate
19898 +ifdef CONFIG_UBIFS_FS
19899 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
19900 +endif
19901 +
19902 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
19903 +# tristate
19904 +ifdef CONFIG_HFSPLUS_FS
19905 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
19906 +endif
19907 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
19908 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
19909 +++ linux/fs/aufs/Makefile      2014-06-24 00:14:06.776052275 +0200
19910 @@ -0,0 +1,41 @@
19911 +
19912 +include ${src}/magic.mk
19913 +ifeq (${CONFIG_AUFS_FS},m)
19914 +include ${src}/conf.mk
19915 +endif
19916 +-include ${src}/priv_def.mk
19917 +
19918 +# cf. include/linux/kernel.h
19919 +# enable pr_debug
19920 +ccflags-y += -DDEBUG
19921 +# sparse requires the full pathname
19922 +ifdef M
19923 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
19924 +else
19925 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
19926 +endif
19927 +
19928 +obj-$(CONFIG_AUFS_FS) += aufs.o
19929 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
19930 +       wkq.o vfsub.o dcsub.o \
19931 +       cpup.o whout.o wbr_policy.o \
19932 +       dinfo.o dentry.o \
19933 +       dynop.o \
19934 +       finfo.o file.o f_op.o \
19935 +       dir.o vdir.o \
19936 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
19937 +       mvdown.o ioctl.o
19938 +
19939 +# all are boolean
19940 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
19941 +aufs-$(CONFIG_SYSFS) += sysfs.o
19942 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
19943 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
19944 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
19945 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
19946 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
19947 +aufs-$(CONFIG_AUFS_POLL) += poll.o
19948 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
19949 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
19950 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
19951 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
19952 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
19953 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
19954 +++ linux/fs/aufs/module.c      2014-06-24 00:14:06.786052822 +0200
19955 @@ -0,0 +1,202 @@
19956 +/*
19957 + * Copyright (C) 2005-2014 Junjiro R. Okajima
19958 + *
19959 + * This program, aufs is free software; you can redistribute it and/or modify
19960 + * it under the terms of the GNU General Public License as published by
19961 + * the Free Software Foundation; either version 2 of the License, or
19962 + * (at your option) any later version.
19963 + *
19964 + * This program is distributed in the hope that it will be useful,
19965 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19966 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19967 + * GNU General Public License for more details.
19968 + *
19969 + * You should have received a copy of the GNU General Public License
19970 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19971 + */
19972 +
19973 +/*
19974 + * module global variables and operations
19975 + */
19976 +
19977 +#include <linux/module.h>
19978 +#include <linux/seq_file.h>
19979 +#include "aufs.h"
19980 +
19981 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
19982 +{
19983 +       if (new_sz <= nused)
19984 +               return p;
19985 +
19986 +       p = krealloc(p, new_sz, gfp);
19987 +       if (p)
19988 +               memset(p + nused, 0, new_sz - nused);
19989 +       return p;
19990 +}
19991 +
19992 +/* ---------------------------------------------------------------------- */
19993 +
19994 +/*
19995 + * aufs caches
19996 + */
19997 +struct kmem_cache *au_cachep[AuCache_Last];
19998 +static int __init au_cache_init(void)
19999 +{
20000 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
20001 +       if (au_cachep[AuCache_DINFO])
20002 +               /* SLAB_DESTROY_BY_RCU */
20003 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
20004 +                                                       au_icntnr_init_once);
20005 +       if (au_cachep[AuCache_ICNTNR])
20006 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
20007 +                                                      au_fi_init_once);
20008 +       if (au_cachep[AuCache_FINFO])
20009 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
20010 +       if (au_cachep[AuCache_VDIR])
20011 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
20012 +       if (au_cachep[AuCache_DEHSTR])
20013 +               return 0;
20014 +
20015 +       return -ENOMEM;
20016 +}
20017 +
20018 +static void au_cache_fin(void)
20019 +{
20020 +       int i;
20021 +
20022 +       /*
20023 +        * Make sure all delayed rcu free inodes are flushed before we
20024 +        * destroy cache.
20025 +        */
20026 +       rcu_barrier();
20027 +
20028 +       /* excluding AuCache_HNOTIFY */
20029 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
20030 +       for (i = 0; i < AuCache_HNOTIFY; i++)
20031 +               if (au_cachep[i]) {
20032 +                       kmem_cache_destroy(au_cachep[i]);
20033 +                       au_cachep[i] = NULL;
20034 +               }
20035 +}
20036 +
20037 +/* ---------------------------------------------------------------------- */
20038 +
20039 +int au_dir_roflags;
20040 +
20041 +#ifdef CONFIG_AUFS_SBILIST
20042 +/*
20043 + * iterate_supers_type() doesn't protect us from
20044 + * remounting (branch management)
20045 + */
20046 +struct au_splhead au_sbilist;
20047 +#endif
20048 +
20049 +struct lock_class_key au_lc_key[AuLcKey_Last];
20050 +
20051 +/*
20052 + * functions for module interface.
20053 + */
20054 +MODULE_LICENSE("GPL");
20055 +/* MODULE_LICENSE("GPL v2"); */
20056 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
20057 +MODULE_DESCRIPTION(AUFS_NAME
20058 +       " -- Advanced multi layered unification filesystem");
20059 +MODULE_VERSION(AUFS_VERSION);
20060 +MODULE_ALIAS_FS(AUFS_NAME);
20061 +
20062 +/* this module parameter has no meaning when SYSFS is disabled */
20063 +int sysaufs_brs = 1;
20064 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
20065 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
20066 +
20067 +/* ---------------------------------------------------------------------- */
20068 +
20069 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
20070 +
20071 +int au_seq_path(struct seq_file *seq, struct path *path)
20072 +{
20073 +       return seq_path(seq, path, au_esc_chars);
20074 +}
20075 +
20076 +/* ---------------------------------------------------------------------- */
20077 +
20078 +static int __init aufs_init(void)
20079 +{
20080 +       int err, i;
20081 +       char *p;
20082 +
20083 +       p = au_esc_chars;
20084 +       for (i = 1; i <= ' '; i++)
20085 +               *p++ = i;
20086 +       *p++ = '\\';
20087 +       *p++ = '\x7f';
20088 +       *p = 0;
20089 +
20090 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
20091 +
20092 +       au_sbilist_init();
20093 +       sysaufs_brs_init();
20094 +       au_debug_init();
20095 +       au_dy_init();
20096 +       err = sysaufs_init();
20097 +       if (unlikely(err))
20098 +               goto out;
20099 +       err = au_procfs_init();
20100 +       if (unlikely(err))
20101 +               goto out_sysaufs;
20102 +       err = au_wkq_init();
20103 +       if (unlikely(err))
20104 +               goto out_procfs;
20105 +       err = au_loopback_init();
20106 +       if (unlikely(err))
20107 +               goto out_wkq;
20108 +       err = au_hnotify_init();
20109 +       if (unlikely(err))
20110 +               goto out_loopback;
20111 +       err = au_sysrq_init();
20112 +       if (unlikely(err))
20113 +               goto out_hin;
20114 +       err = au_cache_init();
20115 +       if (unlikely(err))
20116 +               goto out_sysrq;
20117 +       err = register_filesystem(&aufs_fs_type);
20118 +       if (unlikely(err))
20119 +               goto out_cache;
20120 +       /* since we define pr_fmt, call printk directly */
20121 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
20122 +       goto out; /* success */
20123 +
20124 +out_cache:
20125 +       au_cache_fin();
20126 +out_sysrq:
20127 +       au_sysrq_fin();
20128 +out_hin:
20129 +       au_hnotify_fin();
20130 +out_loopback:
20131 +       au_loopback_fin();
20132 +out_wkq:
20133 +       au_wkq_fin();
20134 +out_procfs:
20135 +       au_procfs_fin();
20136 +out_sysaufs:
20137 +       sysaufs_fin();
20138 +       au_dy_fin();
20139 +out:
20140 +       return err;
20141 +}
20142 +
20143 +static void __exit aufs_exit(void)
20144 +{
20145 +       unregister_filesystem(&aufs_fs_type);
20146 +       au_cache_fin();
20147 +       au_sysrq_fin();
20148 +       au_hnotify_fin();
20149 +       au_loopback_fin();
20150 +       au_wkq_fin();
20151 +       au_procfs_fin();
20152 +       sysaufs_fin();
20153 +       au_dy_fin();
20154 +}
20155 +
20156 +module_init(aufs_init);
20157 +module_exit(aufs_exit);
20158 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
20159 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
20160 +++ linux/fs/aufs/module.h      2014-06-24 00:14:06.786052822 +0200
20161 @@ -0,0 +1,104 @@
20162 +/*
20163 + * Copyright (C) 2005-2014 Junjiro R. Okajima
20164 + *
20165 + * This program, aufs is free software; you can redistribute it and/or modify
20166 + * it under the terms of the GNU General Public License as published by
20167 + * the Free Software Foundation; either version 2 of the License, or
20168 + * (at your option) any later version.
20169 + *
20170 + * This program is distributed in the hope that it will be useful,
20171 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20172 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20173 + * GNU General Public License for more details.
20174 + *
20175 + * You should have received a copy of the GNU General Public License
20176 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20177 + */
20178 +
20179 +/*
20180 + * module initialization and module-global
20181 + */
20182 +
20183 +#ifndef __AUFS_MODULE_H__
20184 +#define __AUFS_MODULE_H__
20185 +
20186 +#ifdef __KERNEL__
20187 +
20188 +#include <linux/slab.h>
20189 +
20190 +struct path;
20191 +struct seq_file;
20192 +
20193 +/* module parameters */
20194 +extern int sysaufs_brs;
20195 +
20196 +/* ---------------------------------------------------------------------- */
20197 +
20198 +extern int au_dir_roflags;
20199 +
20200 +enum {
20201 +       AuLcNonDir_FIINFO,
20202 +       AuLcNonDir_DIINFO,
20203 +       AuLcNonDir_IIINFO,
20204 +
20205 +       AuLcDir_FIINFO,
20206 +       AuLcDir_DIINFO,
20207 +       AuLcDir_IIINFO,
20208 +
20209 +       AuLcSymlink_DIINFO,
20210 +       AuLcSymlink_IIINFO,
20211 +
20212 +       AuLcKey_Last
20213 +};
20214 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
20215 +
20216 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
20217 +int au_seq_path(struct seq_file *seq, struct path *path);
20218 +
20219 +#ifdef CONFIG_PROC_FS
20220 +/* procfs.c */
20221 +int __init au_procfs_init(void);
20222 +void au_procfs_fin(void);
20223 +#else
20224 +AuStubInt0(au_procfs_init, void);
20225 +AuStubVoid(au_procfs_fin, void);
20226 +#endif
20227 +
20228 +/* ---------------------------------------------------------------------- */
20229 +
20230 +/* kmem cache */
20231 +enum {
20232 +       AuCache_DINFO,
20233 +       AuCache_ICNTNR,
20234 +       AuCache_FINFO,
20235 +       AuCache_VDIR,
20236 +       AuCache_DEHSTR,
20237 +       AuCache_HNOTIFY, /* must be last */
20238 +       AuCache_Last
20239 +};
20240 +
20241 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
20242 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
20243 +#define AuCacheCtor(type, ctor)        \
20244 +       kmem_cache_create(#type, sizeof(struct type), \
20245 +                         __alignof__(struct type), AuCacheFlags, ctor)
20246 +
20247 +extern struct kmem_cache *au_cachep[];
20248 +
20249 +#define AuCacheFuncs(name, index) \
20250 +static inline struct au_##name *au_cache_alloc_##name(void) \
20251 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
20252 +static inline void au_cache_free_##name(struct au_##name *p) \
20253 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
20254 +
20255 +AuCacheFuncs(dinfo, DINFO);
20256 +AuCacheFuncs(icntnr, ICNTNR);
20257 +AuCacheFuncs(finfo, FINFO);
20258 +AuCacheFuncs(vdir, VDIR);
20259 +AuCacheFuncs(vdir_dehstr, DEHSTR);
20260 +#ifdef CONFIG_AUFS_HNOTIFY
20261 +AuCacheFuncs(hnotify, HNOTIFY);
20262 +#endif
20263 +
20264 +#endif /* __KERNEL__ */
20265 +#endif /* __AUFS_MODULE_H__ */
20266 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
20267 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
20268 +++ linux/fs/aufs/mvdown.c      2014-06-24 00:14:06.786052822 +0200
20269 @@ -0,0 +1,627 @@
20270 +/*
20271 + * Copyright (C) 2011-2014 Junjiro R. Okajima
20272 + *
20273 + * This program, aufs is free software; you can redistribute it and/or modify
20274 + * it under the terms of the GNU General Public License as published by
20275 + * the Free Software Foundation; either version 2 of the License, or
20276 + * (at your option) any later version.
20277 + *
20278 + * This program is distributed in the hope that it will be useful,
20279 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20280 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20281 + * GNU General Public License for more details.
20282 + *
20283 + * You should have received a copy of the GNU General Public License
20284 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20285 + */
20286 +
20287 +/*
20288 + * move-down, opposite of copy-up
20289 + */
20290 +
20291 +#include "aufs.h"
20292 +
20293 +struct au_mvd_args {
20294 +       struct {
20295 +               struct super_block *h_sb;
20296 +               struct dentry *h_parent;
20297 +               struct au_hinode *hdir;
20298 +               struct inode *h_dir, *h_inode;
20299 +       } info[AUFS_MVDOWN_NARRAY];
20300 +
20301 +       struct aufs_mvdown mvdown;
20302 +       struct dentry *dentry, *parent;
20303 +       struct inode *inode, *dir;
20304 +       struct super_block *sb;
20305 +       aufs_bindex_t bopq, bwh, bfound;
20306 +       unsigned char rename_lock;
20307 +       struct au_pin pin;
20308 +};
20309 +
20310 +#define mvd_errno              mvdown.au_errno
20311 +#define mvd_bsrc               mvdown.a[AUFS_MVDOWN_UPPER].bindex
20312 +#define mvd_src_brid           mvdown.a[AUFS_MVDOWN_UPPER].brid
20313 +#define mvd_bdst               mvdown.a[AUFS_MVDOWN_LOWER].bindex
20314 +#define mvd_dst_brid           mvdown.a[AUFS_MVDOWN_LOWER].brid
20315 +
20316 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
20317 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
20318 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
20319 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
20320 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
20321 +
20322 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
20323 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
20324 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
20325 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
20326 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
20327 +
20328 +#define AU_MVD_PR(flag, ...) do {                      \
20329 +               if (flag)                               \
20330 +                       pr_err(__VA_ARGS__);            \
20331 +       } while (0)
20332 +
20333 +/* make the parent dir on bdst */
20334 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
20335 +{
20336 +       int err;
20337 +
20338 +       err = 0;
20339 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
20340 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
20341 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
20342 +       a->mvd_h_dst_parent = NULL;
20343 +       if (au_dbend(a->parent) >= a->mvd_bdst)
20344 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
20345 +       if (!a->mvd_h_dst_parent) {
20346 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
20347 +               if (unlikely(err)) {
20348 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
20349 +                       goto out;
20350 +               }
20351 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
20352 +       }
20353 +
20354 +out:
20355 +       AuTraceErr(err);
20356 +       return err;
20357 +}
20358 +
20359 +/* lock them all */
20360 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
20361 +{
20362 +       int err;
20363 +       struct dentry *h_trap;
20364 +
20365 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
20366 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
20367 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
20368 +               a->rename_lock = 0;
20369 +               err = au_pin(&a->pin, a->dentry, a->mvd_bdst,
20370 +                            au_opt_udba(a->sb),
20371 +                            AuPin_MNT_WRITE | AuPin_DI_LOCKED);
20372 +               if (!err) {
20373 +                       a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
20374 +                       mutex_lock_nested(&a->mvd_h_src_dir->i_mutex,
20375 +                                         AuLsc_I_PARENT3);
20376 +               } else
20377 +                       AU_MVD_PR(dmsg, "pin failed\n");
20378 +               goto out;
20379 +       }
20380 +
20381 +       err = 0;
20382 +       a->rename_lock = 1;
20383 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
20384 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
20385 +       if (h_trap) {
20386 +               err = (h_trap != a->mvd_h_src_parent);
20387 +               if (err)
20388 +                       err = (h_trap != a->mvd_h_dst_parent);
20389 +       }
20390 +       BUG_ON(err); /* it should never happen */
20391 +
20392 +out:
20393 +       AuTraceErr(err);
20394 +       return err;
20395 +}
20396 +
20397 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
20398 +{
20399 +       if (!a->rename_lock) {
20400 +               mutex_unlock(&a->mvd_h_src_dir->i_mutex);
20401 +               au_unpin(&a->pin);
20402 +       } else
20403 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
20404 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
20405 +}
20406 +
20407 +/* copy-down the file */
20408 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
20409 +{
20410 +       int err;
20411 +       struct au_cp_generic cpg = {
20412 +               .dentry = a->dentry,
20413 +               .bdst   = a->mvd_bdst,
20414 +               .bsrc   = a->mvd_bsrc,
20415 +               .len    = -1,
20416 +               .pin    = &a->pin,
20417 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
20418 +       };
20419 +
20420 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
20421 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
20422 +               au_fset_cpup(cpg.flags, OVERWRITE);
20423 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
20424 +               au_fset_cpup(cpg.flags, RWDST);
20425 +       err = au_sio_cpdown_simple(&cpg);
20426 +       if (unlikely(err))
20427 +               AU_MVD_PR(dmsg, "cpdown failed\n");
20428 +
20429 +       AuTraceErr(err);
20430 +       return err;
20431 +}
20432 +
20433 +/*
20434 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
20435 + * were sleeping
20436 + */
20437 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
20438 +{
20439 +       int err;
20440 +       struct path h_path;
20441 +       struct au_branch *br;
20442 +       struct inode *delegated;
20443 +
20444 +       br = au_sbr(a->sb, a->mvd_bdst);
20445 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
20446 +       err = PTR_ERR(h_path.dentry);
20447 +       if (IS_ERR(h_path.dentry)) {
20448 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
20449 +               goto out;
20450 +       }
20451 +
20452 +       err = 0;
20453 +       if (h_path.dentry->d_inode) {
20454 +               h_path.mnt = au_br_mnt(br);
20455 +               delegated = NULL;
20456 +               err = vfsub_unlink(a->mvd_h_dst_parent->d_inode, &h_path,
20457 +                                  &delegated, /*force*/0);
20458 +               if (unlikely(err == -EWOULDBLOCK)) {
20459 +                       pr_warn("cannot retry for NFSv4 delegation"
20460 +                               " for an internal unlink\n");
20461 +                       iput(delegated);
20462 +               }
20463 +               if (unlikely(err))
20464 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
20465 +       }
20466 +       dput(h_path.dentry);
20467 +
20468 +out:
20469 +       AuTraceErr(err);
20470 +       return err;
20471 +}
20472 +
20473 +/*
20474 + * unlink the topmost h_dentry
20475 + * Note: the target file MAY be modified by UDBA between this mutex_unlock() and
20476 + *     mutex_lock() in vfs_unlink(). in this case, such changes may be lost.
20477 + */
20478 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
20479 +{
20480 +       int err;
20481 +       struct path h_path;
20482 +       struct inode *delegated;
20483 +
20484 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
20485 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
20486 +       delegated = NULL;
20487 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
20488 +       if (unlikely(err == -EWOULDBLOCK)) {
20489 +               pr_warn("cannot retry for NFSv4 delegation"
20490 +                       " for an internal unlink\n");
20491 +               iput(delegated);
20492 +       }
20493 +       if (unlikely(err))
20494 +               AU_MVD_PR(dmsg, "unlink failed\n");
20495 +
20496 +       AuTraceErr(err);
20497 +       return err;
20498 +}
20499 +
20500 +/*
20501 + * copy-down the file and unlink the bsrc file.
20502 + * - unlink the bdst whout if exist
20503 + * - copy-down the file (with whtmp name and rename)
20504 + * - unlink the bsrc file
20505 + */
20506 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
20507 +{
20508 +       int err;
20509 +
20510 +       err = au_do_mkdir(dmsg, a);
20511 +       if (!err)
20512 +               err = au_do_lock(dmsg, a);
20513 +       if (unlikely(err))
20514 +               goto out;
20515 +
20516 +       /*
20517 +        * do not revert the activities we made on bdst since they should be
20518 +        * harmless in aufs.
20519 +        */
20520 +
20521 +       err = au_do_cpdown(dmsg, a);
20522 +       if (!err)
20523 +               err = au_do_unlink_wh(dmsg, a);
20524 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
20525 +               err = au_do_unlink(dmsg, a);
20526 +       if (unlikely(err))
20527 +               goto out_unlock;
20528 +
20529 +       /* maintain internal array */
20530 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
20531 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
20532 +               au_set_dbstart(a->dentry, a->mvd_bdst);
20533 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
20534 +               au_set_ibstart(a->inode, a->mvd_bdst);
20535 +       }
20536 +       if (au_dbend(a->dentry) < a->mvd_bdst)
20537 +               au_set_dbend(a->dentry, a->mvd_bdst);
20538 +       if (au_ibend(a->inode) < a->mvd_bdst)
20539 +               au_set_ibend(a->inode, a->mvd_bdst);
20540 +
20541 +out_unlock:
20542 +       au_do_unlock(dmsg, a);
20543 +out:
20544 +       AuTraceErr(err);
20545 +       return err;
20546 +}
20547 +
20548 +/* ---------------------------------------------------------------------- */
20549 +
20550 +static int find_lower_writable(struct au_mvd_args *a)
20551 +{
20552 +       struct super_block *sb;
20553 +       aufs_bindex_t bindex, bend;
20554 +       struct au_branch *br;
20555 +
20556 +       sb = a->sb;
20557 +       bindex = a->mvd_bsrc;
20558 +       bend = au_sbend(sb);
20559 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER)) {
20560 +               for (bindex++; bindex <= bend; bindex++) {
20561 +                       br = au_sbr(sb, bindex);
20562 +                       if (!au_br_rdonly(br))
20563 +                               return bindex;
20564 +               }
20565 +       } else {
20566 +               for (bindex++; bindex <= bend; bindex++) {
20567 +                       br = au_sbr(sb, bindex);
20568 +                       if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
20569 +                               if (au_br_rdonly(br))
20570 +                                       a->mvdown.flags
20571 +                                               |= AUFS_MVDOWN_ROLOWER_R;
20572 +                               return bindex;
20573 +                       }
20574 +               }
20575 +       }
20576 +
20577 +       return -1;
20578 +}
20579 +
20580 +/* make sure the file is idle */
20581 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
20582 +{
20583 +       int err, plinked;
20584 +
20585 +       err = 0;
20586 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
20587 +       if (au_dbstart(a->dentry) == a->mvd_bsrc
20588 +           && d_count(a->dentry) == 1
20589 +           && atomic_read(&a->inode->i_count) == 1
20590 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
20591 +           && (!plinked || !au_plink_test(a->inode))
20592 +           && a->inode->i_nlink == 1)
20593 +               goto out;
20594 +
20595 +       err = -EBUSY;
20596 +       AU_MVD_PR(dmsg,
20597 +                 "b%d, d{b%d, c%u?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
20598 +                 a->mvd_bsrc, au_dbstart(a->dentry), d_count(a->dentry),
20599 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
20600 +                 a->mvd_h_src_inode->i_nlink,
20601 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
20602 +
20603 +out:
20604 +       AuTraceErr(err);
20605 +       return err;
20606 +}
20607 +
20608 +/* make sure the parent dir is fine */
20609 +static int au_mvd_args_parent(const unsigned char dmsg,
20610 +                             struct au_mvd_args *a)
20611 +{
20612 +       int err;
20613 +       aufs_bindex_t bindex;
20614 +
20615 +       err = 0;
20616 +       if (unlikely(au_alive_dir(a->parent))) {
20617 +               err = -ENOENT;
20618 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
20619 +               goto out;
20620 +       }
20621 +
20622 +       a->bopq = au_dbdiropq(a->parent);
20623 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
20624 +       AuDbg("b%d\n", bindex);
20625 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
20626 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
20627 +               err = -EINVAL;
20628 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
20629 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
20630 +                         a->bopq, a->mvd_bdst);
20631 +       }
20632 +
20633 +out:
20634 +       AuTraceErr(err);
20635 +       return err;
20636 +}
20637 +
20638 +static int au_mvd_args_intermediate(const unsigned char dmsg,
20639 +                                   struct au_mvd_args *a)
20640 +{
20641 +       int err;
20642 +       struct au_dinfo *dinfo, *tmp;
20643 +
20644 +       /* lookup the next lower positive entry */
20645 +       err = -ENOMEM;
20646 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
20647 +       if (unlikely(!tmp))
20648 +               goto out;
20649 +
20650 +       a->bfound = -1;
20651 +       a->bwh = -1;
20652 +       dinfo = au_di(a->dentry);
20653 +       au_di_cp(tmp, dinfo);
20654 +       au_di_swap(tmp, dinfo);
20655 +
20656 +       /* returns the number of positive dentries */
20657 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
20658 +       if (!err)
20659 +               a->bwh = au_dbwh(a->dentry);
20660 +       else if (err > 0)
20661 +               a->bfound = au_dbstart(a->dentry);
20662 +
20663 +       au_di_swap(tmp, dinfo);
20664 +       au_rw_write_unlock(&tmp->di_rwsem);
20665 +       au_di_free(tmp);
20666 +       if (unlikely(err < 0))
20667 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
20668 +
20669 +       /*
20670 +        * here, we have these cases.
20671 +        * bfound == -1
20672 +        *      no positive dentry under bsrc. there are more sub-cases.
20673 +        *      bwh < 0
20674 +        *              there no whiteout, we can safely move-down.
20675 +        *      bwh <= bsrc
20676 +        *              impossible
20677 +        *      bsrc < bwh && bwh < bdst
20678 +        *              there is a whiteout on RO branch. cannot proceed.
20679 +        *      bwh == bdst
20680 +        *              there is a whiteout on the RW target branch. it should
20681 +        *              be removed.
20682 +        *      bdst < bwh
20683 +        *              there is a whiteout somewhere unrelated branch.
20684 +        * -1 < bfound && bfound <= bsrc
20685 +        *      impossible.
20686 +        * bfound < bdst
20687 +        *      found, but it is on RO branch between bsrc and bdst. cannot
20688 +        *      proceed.
20689 +        * bfound == bdst
20690 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
20691 +        *      error.
20692 +        * bdst < bfound
20693 +        *      found, after we create the file on bdst, it will be hidden.
20694 +        */
20695 +
20696 +       AuDebugOn(a->bfound == -1
20697 +                 && a->bwh != -1
20698 +                 && a->bwh <= a->mvd_bsrc);
20699 +       AuDebugOn(-1 < a->bfound
20700 +                 && a->bfound <= a->mvd_bsrc);
20701 +
20702 +       err = -EINVAL;
20703 +       if (a->bfound == -1
20704 +           && a->mvd_bsrc < a->bwh
20705 +           && a->bwh != -1
20706 +           && a->bwh < a->mvd_bdst) {
20707 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
20708 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
20709 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
20710 +               goto out;
20711 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
20712 +               a->mvd_errno = EAU_MVDOWN_UPPER;
20713 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
20714 +                         a->mvd_bdst, a->bfound);
20715 +               goto out;
20716 +       }
20717 +
20718 +       err = 0; /* success */
20719 +
20720 +out:
20721 +       AuTraceErr(err);
20722 +       return err;
20723 +}
20724 +
20725 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
20726 +{
20727 +       int err;
20728 +
20729 +       err = 0;
20730 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
20731 +           && a->bfound == a->mvd_bdst)
20732 +               err = -EEXIST;
20733 +       AuTraceErr(err);
20734 +       return err;
20735 +}
20736 +
20737 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
20738 +{
20739 +       int err;
20740 +       struct au_branch *br;
20741 +
20742 +       err = -EISDIR;
20743 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
20744 +               goto out;
20745 +
20746 +       err = -EINVAL;
20747 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
20748 +               a->mvd_bsrc = au_ibstart(a->inode);
20749 +       else {
20750 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
20751 +               if (unlikely(a->mvd_bsrc < 0
20752 +                            || (a->mvd_bsrc < au_dbstart(a->dentry)
20753 +                                || au_dbend(a->dentry) < a->mvd_bsrc
20754 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
20755 +                            || (a->mvd_bsrc < au_ibstart(a->inode)
20756 +                                || au_ibend(a->inode) < a->mvd_bsrc
20757 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
20758 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
20759 +                       AU_MVD_PR(dmsg, "no upper\n");
20760 +                       goto out;
20761 +               }
20762 +       }
20763 +       if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
20764 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
20765 +               AU_MVD_PR(dmsg, "on the bottom\n");
20766 +               goto out;
20767 +       }
20768 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
20769 +       br = au_sbr(a->sb, a->mvd_bsrc);
20770 +       err = au_br_rdonly(br);
20771 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
20772 +               if (unlikely(err))
20773 +                       goto out;
20774 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
20775 +                    || IS_APPEND(a->mvd_h_src_inode))) {
20776 +               if (err)
20777 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
20778 +               /* go on */
20779 +       } else
20780 +               goto out;
20781 +
20782 +       err = -EINVAL;
20783 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
20784 +               a->mvd_bdst = find_lower_writable(a);
20785 +               if (unlikely(a->mvd_bdst < 0)) {
20786 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
20787 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
20788 +                       goto out;
20789 +               }
20790 +       } else {
20791 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
20792 +               if (unlikely(a->mvd_bdst < 0
20793 +                            || au_sbend(a->sb) < a->mvd_bdst)) {
20794 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
20795 +                       AU_MVD_PR(dmsg, "no lower brid\n");
20796 +                       goto out;
20797 +               }
20798 +       }
20799 +
20800 +       err = au_mvd_args_busy(dmsg, a);
20801 +       if (!err)
20802 +               err = au_mvd_args_parent(dmsg, a);
20803 +       if (!err)
20804 +               err = au_mvd_args_intermediate(dmsg, a);
20805 +       if (!err)
20806 +               err = au_mvd_args_exist(dmsg, a);
20807 +       if (!err)
20808 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
20809 +
20810 +out:
20811 +       AuTraceErr(err);
20812 +       return err;
20813 +}
20814 +
20815 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
20816 +{
20817 +       int err, e;
20818 +       unsigned char dmsg;
20819 +       struct au_mvd_args *args;
20820 +
20821 +       err = -EPERM;
20822 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
20823 +               goto out;
20824 +
20825 +       WARN_ONCE(1, "move-down is still testing...\n");
20826 +
20827 +       err = -ENOMEM;
20828 +       args = kmalloc(sizeof(*args), GFP_NOFS);
20829 +       if (unlikely(!args))
20830 +               goto out;
20831 +
20832 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
20833 +       if (!err)
20834 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
20835 +       if (unlikely(err)) {
20836 +               err = -EFAULT;
20837 +               AuTraceErr(err);
20838 +               goto out_free;
20839 +       }
20840 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
20841 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
20842 +       args->mvdown.au_errno = 0;
20843 +       args->dentry = dentry;
20844 +       args->inode = dentry->d_inode;
20845 +       args->sb = dentry->d_sb;
20846 +
20847 +       err = -ENOENT;
20848 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
20849 +       args->parent = dget_parent(dentry);
20850 +       args->dir = args->parent->d_inode;
20851 +       mutex_lock_nested(&args->dir->i_mutex, I_MUTEX_PARENT);
20852 +       dput(args->parent);
20853 +       if (unlikely(args->parent != dentry->d_parent)) {
20854 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
20855 +               goto out_dir;
20856 +       }
20857 +
20858 +       mutex_lock_nested(&args->inode->i_mutex, I_MUTEX_CHILD);
20859 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
20860 +       if (unlikely(err))
20861 +               goto out_inode;
20862 +
20863 +       di_write_lock_parent(args->parent);
20864 +       err = au_mvd_args(dmsg, args);
20865 +       if (unlikely(err))
20866 +               goto out_parent;
20867 +
20868 +       AuDbgDentry(dentry);
20869 +       AuDbgInode(args->inode);
20870 +       err = au_do_mvdown(dmsg, args);
20871 +       if (unlikely(err))
20872 +               goto out_parent;
20873 +       AuDbgDentry(dentry);
20874 +       AuDbgInode(args->inode);
20875 +
20876 +       au_cpup_attr_timesizes(args->dir);
20877 +       au_cpup_attr_timesizes(args->inode);
20878 +       au_cpup_igen(args->inode, au_h_iptr(args->inode, args->mvd_bdst));
20879 +       /* au_digen_dec(dentry); */
20880 +
20881 +out_parent:
20882 +       di_write_unlock(args->parent);
20883 +       aufs_read_unlock(dentry, AuLock_DW);
20884 +out_inode:
20885 +       mutex_unlock(&args->inode->i_mutex);
20886 +out_dir:
20887 +       mutex_unlock(&args->dir->i_mutex);
20888 +out_free:
20889 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
20890 +       if (unlikely(e))
20891 +               err = -EFAULT;
20892 +       kfree(args);
20893 +out:
20894 +       AuTraceErr(err);
20895 +       return err;
20896 +}
20897 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
20898 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
20899 +++ linux/fs/aufs/opts.c        2014-06-24 00:14:06.786052822 +0200
20900 @@ -0,0 +1,1701 @@
20901 +/*
20902 + * Copyright (C) 2005-2014 Junjiro R. Okajima
20903 + *
20904 + * This program, aufs is free software; you can redistribute it and/or modify
20905 + * it under the terms of the GNU General Public License as published by
20906 + * the Free Software Foundation; either version 2 of the License, or
20907 + * (at your option) any later version.
20908 + *
20909 + * This program is distributed in the hope that it will be useful,
20910 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20911 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20912 + * GNU General Public License for more details.
20913 + *
20914 + * You should have received a copy of the GNU General Public License
20915 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20916 + */
20917 +
20918 +/*
20919 + * mount options/flags
20920 + */
20921 +
20922 +#include <linux/namei.h>
20923 +#include <linux/types.h> /* a distribution requires */
20924 +#include <linux/parser.h>
20925 +#include "aufs.h"
20926 +
20927 +/* ---------------------------------------------------------------------- */
20928 +
20929 +enum {
20930 +       Opt_br,
20931 +       Opt_add, Opt_del, Opt_mod, Opt_reorder, Opt_append, Opt_prepend,
20932 +       Opt_idel, Opt_imod, Opt_ireorder,
20933 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash, Opt_rendir,
20934 +       Opt_rdblk_def, Opt_rdhash_def,
20935 +       Opt_xino, Opt_zxino, Opt_noxino,
20936 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
20937 +       Opt_trunc_xino_path, Opt_itrunc_xino,
20938 +       Opt_trunc_xib, Opt_notrunc_xib,
20939 +       Opt_shwh, Opt_noshwh,
20940 +       Opt_plink, Opt_noplink, Opt_list_plink,
20941 +       Opt_udba,
20942 +       Opt_dio, Opt_nodio,
20943 +       /* Opt_lock, Opt_unlock, */
20944 +       Opt_cmd, Opt_cmd_args,
20945 +       Opt_diropq_a, Opt_diropq_w,
20946 +       Opt_warn_perm, Opt_nowarn_perm,
20947 +       Opt_wbr_copyup, Opt_wbr_create,
20948 +       Opt_refrof, Opt_norefrof,
20949 +       Opt_verbose, Opt_noverbose,
20950 +       Opt_sum, Opt_nosum, Opt_wsum,
20951 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
20952 +};
20953 +
20954 +static match_table_t options = {
20955 +       {Opt_br, "br=%s"},
20956 +       {Opt_br, "br:%s"},
20957 +
20958 +       {Opt_add, "add=%d:%s"},
20959 +       {Opt_add, "add:%d:%s"},
20960 +       {Opt_add, "ins=%d:%s"},
20961 +       {Opt_add, "ins:%d:%s"},
20962 +       {Opt_append, "append=%s"},
20963 +       {Opt_append, "append:%s"},
20964 +       {Opt_prepend, "prepend=%s"},
20965 +       {Opt_prepend, "prepend:%s"},
20966 +
20967 +       {Opt_del, "del=%s"},
20968 +       {Opt_del, "del:%s"},
20969 +       /* {Opt_idel, "idel:%d"}, */
20970 +       {Opt_mod, "mod=%s"},
20971 +       {Opt_mod, "mod:%s"},
20972 +       /* {Opt_imod, "imod:%d:%s"}, */
20973 +
20974 +       {Opt_dirwh, "dirwh=%d"},
20975 +
20976 +       {Opt_xino, "xino=%s"},
20977 +       {Opt_noxino, "noxino"},
20978 +       {Opt_trunc_xino, "trunc_xino"},
20979 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
20980 +       {Opt_notrunc_xino, "notrunc_xino"},
20981 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
20982 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
20983 +       /* {Opt_zxino, "zxino=%s"}, */
20984 +       {Opt_trunc_xib, "trunc_xib"},
20985 +       {Opt_notrunc_xib, "notrunc_xib"},
20986 +
20987 +#ifdef CONFIG_PROC_FS
20988 +       {Opt_plink, "plink"},
20989 +#else
20990 +       {Opt_ignore_silent, "plink"},
20991 +#endif
20992 +
20993 +       {Opt_noplink, "noplink"},
20994 +
20995 +#ifdef CONFIG_AUFS_DEBUG
20996 +       {Opt_list_plink, "list_plink"},
20997 +#endif
20998 +
20999 +       {Opt_udba, "udba=%s"},
21000 +
21001 +       {Opt_dio, "dio"},
21002 +       {Opt_nodio, "nodio"},
21003 +
21004 +       {Opt_diropq_a, "diropq=always"},
21005 +       {Opt_diropq_a, "diropq=a"},
21006 +       {Opt_diropq_w, "diropq=whiteouted"},
21007 +       {Opt_diropq_w, "diropq=w"},
21008 +
21009 +       {Opt_warn_perm, "warn_perm"},
21010 +       {Opt_nowarn_perm, "nowarn_perm"},
21011 +
21012 +       /* keep them temporary */
21013 +       {Opt_ignore_silent, "coo=%s"},
21014 +       {Opt_ignore_silent, "nodlgt"},
21015 +       {Opt_ignore_silent, "nodirperm1"},
21016 +       {Opt_ignore_silent, "clean_plink"},
21017 +
21018 +#ifdef CONFIG_AUFS_SHWH
21019 +       {Opt_shwh, "shwh"},
21020 +#endif
21021 +       {Opt_noshwh, "noshwh"},
21022 +
21023 +       {Opt_rendir, "rendir=%d"},
21024 +
21025 +       {Opt_refrof, "refrof"},
21026 +       {Opt_norefrof, "norefrof"},
21027 +
21028 +       {Opt_verbose, "verbose"},
21029 +       {Opt_verbose, "v"},
21030 +       {Opt_noverbose, "noverbose"},
21031 +       {Opt_noverbose, "quiet"},
21032 +       {Opt_noverbose, "q"},
21033 +       {Opt_noverbose, "silent"},
21034 +
21035 +       {Opt_sum, "sum"},
21036 +       {Opt_nosum, "nosum"},
21037 +       {Opt_wsum, "wsum"},
21038 +
21039 +       {Opt_rdcache, "rdcache=%d"},
21040 +       {Opt_rdblk, "rdblk=%d"},
21041 +       {Opt_rdblk_def, "rdblk=def"},
21042 +       {Opt_rdhash, "rdhash=%d"},
21043 +       {Opt_rdhash_def, "rdhash=def"},
21044 +
21045 +       {Opt_wbr_create, "create=%s"},
21046 +       {Opt_wbr_create, "create_policy=%s"},
21047 +       {Opt_wbr_copyup, "cpup=%s"},
21048 +       {Opt_wbr_copyup, "copyup=%s"},
21049 +       {Opt_wbr_copyup, "copyup_policy=%s"},
21050 +
21051 +       /* internal use for the scripts */
21052 +       {Opt_ignore_silent, "si=%s"},
21053 +
21054 +       {Opt_br, "dirs=%s"},
21055 +       {Opt_ignore, "debug=%d"},
21056 +       {Opt_ignore, "delete=whiteout"},
21057 +       {Opt_ignore, "delete=all"},
21058 +       {Opt_ignore, "imap=%s"},
21059 +
21060 +       /* temporary workaround, due to old mount(8)? */
21061 +       {Opt_ignore_silent, "relatime"},
21062 +
21063 +       {Opt_err, NULL}
21064 +};
21065 +
21066 +/* ---------------------------------------------------------------------- */
21067 +
21068 +static const char *au_parser_pattern(int val, struct match_token *token)
21069 +{
21070 +       while (token->pattern) {
21071 +               if (token->token == val)
21072 +                       return token->pattern;
21073 +               token++;
21074 +       }
21075 +       BUG();
21076 +       return "??";
21077 +}
21078 +
21079 +/* ---------------------------------------------------------------------- */
21080 +
21081 +static match_table_t brperm = {
21082 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
21083 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
21084 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
21085 +       {0, NULL}
21086 +};
21087 +
21088 +static match_table_t brattr = {
21089 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
21090 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
21091 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
21092 +       {0, NULL}
21093 +};
21094 +
21095 +#define AuBrStr_LONGEST        AUFS_BRPERM_RW \
21096 +       "+" AUFS_BRATTR_UNPIN \
21097 +       "+" AUFS_BRWATTR_NLWH
21098 +
21099 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
21100 +{
21101 +       int attr, v;
21102 +       char *p;
21103 +
21104 +       attr = 0;
21105 +       do {
21106 +               p = strchr(str, '+');
21107 +               if (p)
21108 +                       *p = 0;
21109 +               v = match_token(str, table, args);
21110 +               if (v)
21111 +                       attr |= v;
21112 +               else {
21113 +                       if (p)
21114 +                               *p = '+';
21115 +                       pr_warn("ignored branch attribute %s\n", str);
21116 +                       break;
21117 +               }
21118 +               if (p)
21119 +                       str = p + 1;
21120 +       } while (p);
21121 +
21122 +       return attr;
21123 +}
21124 +
21125 +static int noinline_for_stack br_perm_val(char *perm)
21126 +{
21127 +       int val;
21128 +       char *p, *q;
21129 +       substring_t args[MAX_OPT_ARGS];
21130 +
21131 +       p = strchr(perm, '+');
21132 +       if (p)
21133 +               *p = 0;
21134 +       val = match_token(perm, brperm, args);
21135 +       if (!val) {
21136 +               if (p)
21137 +                       *p = '+';
21138 +               pr_warn("ignored branch permission %s\n", perm);
21139 +               val = AuBrPerm_RO;
21140 +               goto out;
21141 +       }
21142 +       if (!p)
21143 +               goto out;
21144 +
21145 +       p++;
21146 +       while (1) {
21147 +               q = strchr(p, '+');
21148 +               if (q)
21149 +                       *q = 0;
21150 +               val |= br_attr_val(p, brattr, args);
21151 +               if (q) {
21152 +                       *q = '+';
21153 +                       p = q + 1;
21154 +               } else
21155 +                       break;
21156 +       }
21157 +       switch (val & AuBrPerm_Mask) {
21158 +       case AuBrPerm_RO:
21159 +       case AuBrPerm_RR:
21160 +               if (unlikely(val & AuBrWAttr_NoLinkWH)) {
21161 +                       pr_warn("ignored branch attribute %s\n",
21162 +                               AUFS_BRWATTR_NLWH);
21163 +                       val &= ~AuBrWAttr_NoLinkWH;
21164 +               }
21165 +               break;
21166 +       case AuBrPerm_RW:
21167 +               if (unlikely(val & AuBrRAttr_WH)) {
21168 +                       pr_warn("ignored branch attribute %s\n",
21169 +                               AUFS_BRRATTR_WH);
21170 +                       val &= ~AuBrRAttr_WH;
21171 +               }
21172 +               break;
21173 +       }
21174 +
21175 +out:
21176 +       return val;
21177 +}
21178 +
21179 +/* Caller should free the return value */
21180 +char *au_optstr_br_perm(int brperm)
21181 +{
21182 +       char *p, a[sizeof(AuBrStr_LONGEST)];
21183 +       int sz;
21184 +
21185 +#define SetPerm(str) do {                      \
21186 +               sz = sizeof(str);               \
21187 +               memcpy(a, str, sz);             \
21188 +               p = a + sz - 1;                 \
21189 +       } while (0)
21190 +
21191 +#define AppendAttr(flag, str) do {                     \
21192 +               if (brperm & flag) {            \
21193 +                       sz = sizeof(str);       \
21194 +                       *p++ = '+';             \
21195 +                       memcpy(p, str, sz);     \
21196 +                       p += sz - 1;            \
21197 +               }                               \
21198 +       } while (0)
21199 +
21200 +       switch (brperm & AuBrPerm_Mask) {
21201 +       case AuBrPerm_RO:
21202 +               SetPerm(AUFS_BRPERM_RO);
21203 +               break;
21204 +       case AuBrPerm_RR:
21205 +               SetPerm(AUFS_BRPERM_RR);
21206 +               break;
21207 +       case AuBrPerm_RW:
21208 +               SetPerm(AUFS_BRPERM_RW);
21209 +               break;
21210 +       default:
21211 +               AuDebugOn(1);
21212 +       }
21213 +
21214 +       AppendAttr(AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN);
21215 +       AppendAttr(AuBrRAttr_WH, AUFS_BRRATTR_WH);
21216 +       AppendAttr(AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH);
21217 +
21218 +       AuDebugOn(strlen(a) >= sizeof(a));
21219 +       return kstrdup(a, GFP_NOFS);
21220 +#undef SetPerm
21221 +#undef AppendAttr
21222 +}
21223 +
21224 +/* ---------------------------------------------------------------------- */
21225 +
21226 +static match_table_t udbalevel = {
21227 +       {AuOpt_UDBA_REVAL, "reval"},
21228 +       {AuOpt_UDBA_NONE, "none"},
21229 +#ifdef CONFIG_AUFS_HNOTIFY
21230 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
21231 +#ifdef CONFIG_AUFS_HFSNOTIFY
21232 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
21233 +#endif
21234 +#endif
21235 +       {-1, NULL}
21236 +};
21237 +
21238 +static int noinline_for_stack udba_val(char *str)
21239 +{
21240 +       substring_t args[MAX_OPT_ARGS];
21241 +
21242 +       return match_token(str, udbalevel, args);
21243 +}
21244 +
21245 +const char *au_optstr_udba(int udba)
21246 +{
21247 +       return au_parser_pattern(udba, (void *)udbalevel);
21248 +}
21249 +
21250 +/* ---------------------------------------------------------------------- */
21251 +
21252 +static match_table_t au_wbr_create_policy = {
21253 +       {AuWbrCreate_TDP, "tdp"},
21254 +       {AuWbrCreate_TDP, "top-down-parent"},
21255 +       {AuWbrCreate_RR, "rr"},
21256 +       {AuWbrCreate_RR, "round-robin"},
21257 +       {AuWbrCreate_MFS, "mfs"},
21258 +       {AuWbrCreate_MFS, "most-free-space"},
21259 +       {AuWbrCreate_MFSV, "mfs:%d"},
21260 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
21261 +
21262 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
21263 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
21264 +       {AuWbrCreate_PMFS, "pmfs"},
21265 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
21266 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
21267 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
21268 +
21269 +       {-1, NULL}
21270 +};
21271 +
21272 +/*
21273 + * cf. linux/lib/parser.c and cmdline.c
21274 + * gave up calling memparse() since it uses simple_strtoull() instead of
21275 + * kstrto...().
21276 + */
21277 +static int noinline_for_stack
21278 +au_match_ull(substring_t *s, unsigned long long *result)
21279 +{
21280 +       int err;
21281 +       unsigned int len;
21282 +       char a[32];
21283 +
21284 +       err = -ERANGE;
21285 +       len = s->to - s->from;
21286 +       if (len + 1 <= sizeof(a)) {
21287 +               memcpy(a, s->from, len);
21288 +               a[len] = '\0';
21289 +               err = kstrtoull(a, 0, result);
21290 +       }
21291 +       return err;
21292 +}
21293 +
21294 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
21295 +                           struct au_opt_wbr_create *create)
21296 +{
21297 +       int err;
21298 +       unsigned long long ull;
21299 +
21300 +       err = 0;
21301 +       if (!au_match_ull(arg, &ull))
21302 +               create->mfsrr_watermark = ull;
21303 +       else {
21304 +               pr_err("bad integer in %s\n", str);
21305 +               err = -EINVAL;
21306 +       }
21307 +
21308 +       return err;
21309 +}
21310 +
21311 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
21312 +                         struct au_opt_wbr_create *create)
21313 +{
21314 +       int n, err;
21315 +
21316 +       err = 0;
21317 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
21318 +               create->mfs_second = n;
21319 +       else {
21320 +               pr_err("bad integer in %s\n", str);
21321 +               err = -EINVAL;
21322 +       }
21323 +
21324 +       return err;
21325 +}
21326 +
21327 +static int noinline_for_stack
21328 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
21329 +{
21330 +       int err, e;
21331 +       substring_t args[MAX_OPT_ARGS];
21332 +
21333 +       err = match_token(str, au_wbr_create_policy, args);
21334 +       create->wbr_create = err;
21335 +       switch (err) {
21336 +       case AuWbrCreate_MFSRRV:
21337 +       case AuWbrCreate_PMFSRRV:
21338 +               e = au_wbr_mfs_wmark(&args[0], str, create);
21339 +               if (!e)
21340 +                       e = au_wbr_mfs_sec(&args[1], str, create);
21341 +               if (unlikely(e))
21342 +                       err = e;
21343 +               break;
21344 +       case AuWbrCreate_MFSRR:
21345 +       case AuWbrCreate_PMFSRR:
21346 +               e = au_wbr_mfs_wmark(&args[0], str, create);
21347 +               if (unlikely(e)) {
21348 +                       err = e;
21349 +                       break;
21350 +               }
21351 +               /*FALLTHROUGH*/
21352 +       case AuWbrCreate_MFS:
21353 +       case AuWbrCreate_PMFS:
21354 +               create->mfs_second = AUFS_MFS_DEF_SEC;
21355 +               break;
21356 +       case AuWbrCreate_MFSV:
21357 +       case AuWbrCreate_PMFSV:
21358 +               e = au_wbr_mfs_sec(&args[0], str, create);
21359 +               if (unlikely(e))
21360 +                       err = e;
21361 +               break;
21362 +       }
21363 +
21364 +       return err;
21365 +}
21366 +
21367 +const char *au_optstr_wbr_create(int wbr_create)
21368 +{
21369 +       return au_parser_pattern(wbr_create, (void *)au_wbr_create_policy);
21370 +}
21371 +
21372 +static match_table_t au_wbr_copyup_policy = {
21373 +       {AuWbrCopyup_TDP, "tdp"},
21374 +       {AuWbrCopyup_TDP, "top-down-parent"},
21375 +       {AuWbrCopyup_BUP, "bup"},
21376 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
21377 +       {AuWbrCopyup_BU, "bu"},
21378 +       {AuWbrCopyup_BU, "bottom-up"},
21379 +       {-1, NULL}
21380 +};
21381 +
21382 +static int noinline_for_stack au_wbr_copyup_val(char *str)
21383 +{
21384 +       substring_t args[MAX_OPT_ARGS];
21385 +
21386 +       return match_token(str, au_wbr_copyup_policy, args);
21387 +}
21388 +
21389 +const char *au_optstr_wbr_copyup(int wbr_copyup)
21390 +{
21391 +       return au_parser_pattern(wbr_copyup, (void *)au_wbr_copyup_policy);
21392 +}
21393 +
21394 +/* ---------------------------------------------------------------------- */
21395 +
21396 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
21397 +
21398 +static void dump_opts(struct au_opts *opts)
21399 +{
21400 +#ifdef CONFIG_AUFS_DEBUG
21401 +       /* reduce stack space */
21402 +       union {
21403 +               struct au_opt_add *add;
21404 +               struct au_opt_del *del;
21405 +               struct au_opt_mod *mod;
21406 +               struct au_opt_xino *xino;
21407 +               struct au_opt_xino_itrunc *xino_itrunc;
21408 +               struct au_opt_wbr_create *create;
21409 +       } u;
21410 +       struct au_opt *opt;
21411 +
21412 +       opt = opts->opt;
21413 +       while (opt->type != Opt_tail) {
21414 +               switch (opt->type) {
21415 +               case Opt_add:
21416 +                       u.add = &opt->add;
21417 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
21418 +                                 u.add->bindex, u.add->pathname, u.add->perm,
21419 +                                 u.add->path.dentry);
21420 +                       break;
21421 +               case Opt_del:
21422 +               case Opt_idel:
21423 +                       u.del = &opt->del;
21424 +                       AuDbg("del {%s, %p}\n",
21425 +                             u.del->pathname, u.del->h_path.dentry);
21426 +                       break;
21427 +               case Opt_mod:
21428 +               case Opt_imod:
21429 +                       u.mod = &opt->mod;
21430 +                       AuDbg("mod {%s, 0x%x, %p}\n",
21431 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
21432 +                       break;
21433 +               case Opt_append:
21434 +                       u.add = &opt->add;
21435 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
21436 +                                 u.add->bindex, u.add->pathname, u.add->perm,
21437 +                                 u.add->path.dentry);
21438 +                       break;
21439 +               case Opt_prepend:
21440 +                       u.add = &opt->add;
21441 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
21442 +                                 u.add->bindex, u.add->pathname, u.add->perm,
21443 +                                 u.add->path.dentry);
21444 +                       break;
21445 +               case Opt_dirwh:
21446 +                       AuDbg("dirwh %d\n", opt->dirwh);
21447 +                       break;
21448 +               case Opt_rdcache:
21449 +                       AuDbg("rdcache %d\n", opt->rdcache);
21450 +                       break;
21451 +               case Opt_rdblk:
21452 +                       AuDbg("rdblk %u\n", opt->rdblk);
21453 +                       break;
21454 +               case Opt_rdblk_def:
21455 +                       AuDbg("rdblk_def\n");
21456 +                       break;
21457 +               case Opt_rdhash:
21458 +                       AuDbg("rdhash %u\n", opt->rdhash);
21459 +                       break;
21460 +               case Opt_rdhash_def:
21461 +                       AuDbg("rdhash_def\n");
21462 +                       break;
21463 +               case Opt_xino:
21464 +                       u.xino = &opt->xino;
21465 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
21466 +                       break;
21467 +               case Opt_trunc_xino:
21468 +                       AuLabel(trunc_xino);
21469 +                       break;
21470 +               case Opt_notrunc_xino:
21471 +                       AuLabel(notrunc_xino);
21472 +                       break;
21473 +               case Opt_trunc_xino_path:
21474 +               case Opt_itrunc_xino:
21475 +                       u.xino_itrunc = &opt->xino_itrunc;
21476 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
21477 +                       break;
21478 +
21479 +               case Opt_noxino:
21480 +                       AuLabel(noxino);
21481 +                       break;
21482 +               case Opt_trunc_xib:
21483 +                       AuLabel(trunc_xib);
21484 +                       break;
21485 +               case Opt_notrunc_xib:
21486 +                       AuLabel(notrunc_xib);
21487 +                       break;
21488 +               case Opt_shwh:
21489 +                       AuLabel(shwh);
21490 +                       break;
21491 +               case Opt_noshwh:
21492 +                       AuLabel(noshwh);
21493 +                       break;
21494 +               case Opt_plink:
21495 +                       AuLabel(plink);
21496 +                       break;
21497 +               case Opt_noplink:
21498 +                       AuLabel(noplink);
21499 +                       break;
21500 +               case Opt_list_plink:
21501 +                       AuLabel(list_plink);
21502 +                       break;
21503 +               case Opt_udba:
21504 +                       AuDbg("udba %d, %s\n",
21505 +                                 opt->udba, au_optstr_udba(opt->udba));
21506 +                       break;
21507 +               case Opt_dio:
21508 +                       AuLabel(dio);
21509 +                       break;
21510 +               case Opt_nodio:
21511 +                       AuLabel(nodio);
21512 +                       break;
21513 +               case Opt_diropq_a:
21514 +                       AuLabel(diropq_a);
21515 +                       break;
21516 +               case Opt_diropq_w:
21517 +                       AuLabel(diropq_w);
21518 +                       break;
21519 +               case Opt_warn_perm:
21520 +                       AuLabel(warn_perm);
21521 +                       break;
21522 +               case Opt_nowarn_perm:
21523 +                       AuLabel(nowarn_perm);
21524 +                       break;
21525 +               case Opt_refrof:
21526 +                       AuLabel(refrof);
21527 +                       break;
21528 +               case Opt_norefrof:
21529 +                       AuLabel(norefrof);
21530 +                       break;
21531 +               case Opt_verbose:
21532 +                       AuLabel(verbose);
21533 +                       break;
21534 +               case Opt_noverbose:
21535 +                       AuLabel(noverbose);
21536 +                       break;
21537 +               case Opt_sum:
21538 +                       AuLabel(sum);
21539 +                       break;
21540 +               case Opt_nosum:
21541 +                       AuLabel(nosum);
21542 +                       break;
21543 +               case Opt_wsum:
21544 +                       AuLabel(wsum);
21545 +                       break;
21546 +               case Opt_wbr_create:
21547 +                       u.create = &opt->wbr_create;
21548 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
21549 +                                 au_optstr_wbr_create(u.create->wbr_create));
21550 +                       switch (u.create->wbr_create) {
21551 +                       case AuWbrCreate_MFSV:
21552 +                       case AuWbrCreate_PMFSV:
21553 +                               AuDbg("%d sec\n", u.create->mfs_second);
21554 +                               break;
21555 +                       case AuWbrCreate_MFSRR:
21556 +                               AuDbg("%llu watermark\n",
21557 +                                         u.create->mfsrr_watermark);
21558 +                               break;
21559 +                       case AuWbrCreate_MFSRRV:
21560 +                       case AuWbrCreate_PMFSRRV:
21561 +                               AuDbg("%llu watermark, %d sec\n",
21562 +                                         u.create->mfsrr_watermark,
21563 +                                         u.create->mfs_second);
21564 +                               break;
21565 +                       }
21566 +                       break;
21567 +               case Opt_wbr_copyup:
21568 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
21569 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
21570 +                       break;
21571 +               default:
21572 +                       BUG();
21573 +               }
21574 +               opt++;
21575 +       }
21576 +#endif
21577 +}
21578 +
21579 +void au_opts_free(struct au_opts *opts)
21580 +{
21581 +       struct au_opt *opt;
21582 +
21583 +       opt = opts->opt;
21584 +       while (opt->type != Opt_tail) {
21585 +               switch (opt->type) {
21586 +               case Opt_add:
21587 +               case Opt_append:
21588 +               case Opt_prepend:
21589 +                       path_put(&opt->add.path);
21590 +                       break;
21591 +               case Opt_del:
21592 +               case Opt_idel:
21593 +                       path_put(&opt->del.h_path);
21594 +                       break;
21595 +               case Opt_mod:
21596 +               case Opt_imod:
21597 +                       dput(opt->mod.h_root);
21598 +                       break;
21599 +               case Opt_xino:
21600 +                       fput(opt->xino.file);
21601 +                       break;
21602 +               }
21603 +               opt++;
21604 +       }
21605 +}
21606 +
21607 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
21608 +                  aufs_bindex_t bindex)
21609 +{
21610 +       int err;
21611 +       struct au_opt_add *add = &opt->add;
21612 +       char *p;
21613 +
21614 +       add->bindex = bindex;
21615 +       add->perm = AuBrPerm_RO;
21616 +       add->pathname = opt_str;
21617 +       p = strchr(opt_str, '=');
21618 +       if (p) {
21619 +               *p++ = 0;
21620 +               if (*p)
21621 +                       add->perm = br_perm_val(p);
21622 +       }
21623 +
21624 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
21625 +       if (!err) {
21626 +               if (!p) {
21627 +                       add->perm = AuBrPerm_RO;
21628 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
21629 +                               add->perm = AuBrPerm_RR;
21630 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
21631 +                               add->perm = AuBrPerm_RW;
21632 +               }
21633 +               opt->type = Opt_add;
21634 +               goto out;
21635 +       }
21636 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
21637 +       err = -EINVAL;
21638 +
21639 +out:
21640 +       return err;
21641 +}
21642 +
21643 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
21644 +{
21645 +       int err;
21646 +
21647 +       del->pathname = args[0].from;
21648 +       AuDbg("del path %s\n", del->pathname);
21649 +
21650 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
21651 +       if (unlikely(err))
21652 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
21653 +
21654 +       return err;
21655 +}
21656 +
21657 +#if 0 /* reserved for future use */
21658 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
21659 +                             struct au_opt_del *del, substring_t args[])
21660 +{
21661 +       int err;
21662 +       struct dentry *root;
21663 +
21664 +       err = -EINVAL;
21665 +       root = sb->s_root;
21666 +       aufs_read_lock(root, AuLock_FLUSH);
21667 +       if (bindex < 0 || au_sbend(sb) < bindex) {
21668 +               pr_err("out of bounds, %d\n", bindex);
21669 +               goto out;
21670 +       }
21671 +
21672 +       err = 0;
21673 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
21674 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
21675 +
21676 +out:
21677 +       aufs_read_unlock(root, !AuLock_IR);
21678 +       return err;
21679 +}
21680 +#endif
21681 +
21682 +static int noinline_for_stack
21683 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
21684 +{
21685 +       int err;
21686 +       struct path path;
21687 +       char *p;
21688 +
21689 +       err = -EINVAL;
21690 +       mod->path = args[0].from;
21691 +       p = strchr(mod->path, '=');
21692 +       if (unlikely(!p)) {
21693 +               pr_err("no permssion %s\n", args[0].from);
21694 +               goto out;
21695 +       }
21696 +
21697 +       *p++ = 0;
21698 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
21699 +       if (unlikely(err)) {
21700 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
21701 +               goto out;
21702 +       }
21703 +
21704 +       mod->perm = br_perm_val(p);
21705 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
21706 +       mod->h_root = dget(path.dentry);
21707 +       path_put(&path);
21708 +
21709 +out:
21710 +       return err;
21711 +}
21712 +
21713 +#if 0 /* reserved for future use */
21714 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
21715 +                             struct au_opt_mod *mod, substring_t args[])
21716 +{
21717 +       int err;
21718 +       struct dentry *root;
21719 +
21720 +       err = -EINVAL;
21721 +       root = sb->s_root;
21722 +       aufs_read_lock(root, AuLock_FLUSH);
21723 +       if (bindex < 0 || au_sbend(sb) < bindex) {
21724 +               pr_err("out of bounds, %d\n", bindex);
21725 +               goto out;
21726 +       }
21727 +
21728 +       err = 0;
21729 +       mod->perm = br_perm_val(args[1].from);
21730 +       AuDbg("mod path %s, perm 0x%x, %s\n",
21731 +             mod->path, mod->perm, args[1].from);
21732 +       mod->h_root = dget(au_h_dptr(root, bindex));
21733 +
21734 +out:
21735 +       aufs_read_unlock(root, !AuLock_IR);
21736 +       return err;
21737 +}
21738 +#endif
21739 +
21740 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
21741 +                             substring_t args[])
21742 +{
21743 +       int err;
21744 +       struct file *file;
21745 +
21746 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
21747 +       err = PTR_ERR(file);
21748 +       if (IS_ERR(file))
21749 +               goto out;
21750 +
21751 +       err = -EINVAL;
21752 +       if (unlikely(file->f_dentry->d_sb == sb)) {
21753 +               fput(file);
21754 +               pr_err("%s must be outside\n", args[0].from);
21755 +               goto out;
21756 +       }
21757 +
21758 +       err = 0;
21759 +       xino->file = file;
21760 +       xino->path = args[0].from;
21761 +
21762 +out:
21763 +       return err;
21764 +}
21765 +
21766 +static int noinline_for_stack
21767 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
21768 +                              struct au_opt_xino_itrunc *xino_itrunc,
21769 +                              substring_t args[])
21770 +{
21771 +       int err;
21772 +       aufs_bindex_t bend, bindex;
21773 +       struct path path;
21774 +       struct dentry *root;
21775 +
21776 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
21777 +       if (unlikely(err)) {
21778 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
21779 +               goto out;
21780 +       }
21781 +
21782 +       xino_itrunc->bindex = -1;
21783 +       root = sb->s_root;
21784 +       aufs_read_lock(root, AuLock_FLUSH);
21785 +       bend = au_sbend(sb);
21786 +       for (bindex = 0; bindex <= bend; bindex++) {
21787 +               if (au_h_dptr(root, bindex) == path.dentry) {
21788 +                       xino_itrunc->bindex = bindex;
21789 +                       break;
21790 +               }
21791 +       }
21792 +       aufs_read_unlock(root, !AuLock_IR);
21793 +       path_put(&path);
21794 +
21795 +       if (unlikely(xino_itrunc->bindex < 0)) {
21796 +               pr_err("no such branch %s\n", args[0].from);
21797 +               err = -EINVAL;
21798 +       }
21799 +
21800 +out:
21801 +       return err;
21802 +}
21803 +
21804 +/* called without aufs lock */
21805 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
21806 +{
21807 +       int err, n, token;
21808 +       aufs_bindex_t bindex;
21809 +       unsigned char skipped;
21810 +       struct dentry *root;
21811 +       struct au_opt *opt, *opt_tail;
21812 +       char *opt_str;
21813 +       /* reduce the stack space */
21814 +       union {
21815 +               struct au_opt_xino_itrunc *xino_itrunc;
21816 +               struct au_opt_wbr_create *create;
21817 +       } u;
21818 +       struct {
21819 +               substring_t args[MAX_OPT_ARGS];
21820 +       } *a;
21821 +
21822 +       err = -ENOMEM;
21823 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21824 +       if (unlikely(!a))
21825 +               goto out;
21826 +
21827 +       root = sb->s_root;
21828 +       err = 0;
21829 +       bindex = 0;
21830 +       opt = opts->opt;
21831 +       opt_tail = opt + opts->max_opt - 1;
21832 +       opt->type = Opt_tail;
21833 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
21834 +               err = -EINVAL;
21835 +               skipped = 0;
21836 +               token = match_token(opt_str, options, a->args);
21837 +               switch (token) {
21838 +               case Opt_br:
21839 +                       err = 0;
21840 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
21841 +                              && *opt_str) {
21842 +                               err = opt_add(opt, opt_str, opts->sb_flags,
21843 +                                             bindex++);
21844 +                               if (unlikely(!err && ++opt > opt_tail)) {
21845 +                                       err = -E2BIG;
21846 +                                       break;
21847 +                               }
21848 +                               opt->type = Opt_tail;
21849 +                               skipped = 1;
21850 +                       }
21851 +                       break;
21852 +               case Opt_add:
21853 +                       if (unlikely(match_int(&a->args[0], &n))) {
21854 +                               pr_err("bad integer in %s\n", opt_str);
21855 +                               break;
21856 +                       }
21857 +                       bindex = n;
21858 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
21859 +                                     bindex);
21860 +                       if (!err)
21861 +                               opt->type = token;
21862 +                       break;
21863 +               case Opt_append:
21864 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
21865 +                                     /*dummy bindex*/1);
21866 +                       if (!err)
21867 +                               opt->type = token;
21868 +                       break;
21869 +               case Opt_prepend:
21870 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
21871 +                                     /*bindex*/0);
21872 +                       if (!err)
21873 +                               opt->type = token;
21874 +                       break;
21875 +               case Opt_del:
21876 +                       err = au_opts_parse_del(&opt->del, a->args);
21877 +                       if (!err)
21878 +                               opt->type = token;
21879 +                       break;
21880 +#if 0 /* reserved for future use */
21881 +               case Opt_idel:
21882 +                       del->pathname = "(indexed)";
21883 +                       if (unlikely(match_int(&args[0], &n))) {
21884 +                               pr_err("bad integer in %s\n", opt_str);
21885 +                               break;
21886 +                       }
21887 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
21888 +                       if (!err)
21889 +                               opt->type = token;
21890 +                       break;
21891 +#endif
21892 +               case Opt_mod:
21893 +                       err = au_opts_parse_mod(&opt->mod, a->args);
21894 +                       if (!err)
21895 +                               opt->type = token;
21896 +                       break;
21897 +#ifdef IMOD /* reserved for future use */
21898 +               case Opt_imod:
21899 +                       u.mod->path = "(indexed)";
21900 +                       if (unlikely(match_int(&a->args[0], &n))) {
21901 +                               pr_err("bad integer in %s\n", opt_str);
21902 +                               break;
21903 +                       }
21904 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
21905 +                       if (!err)
21906 +                               opt->type = token;
21907 +                       break;
21908 +#endif
21909 +               case Opt_xino:
21910 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
21911 +                       if (!err)
21912 +                               opt->type = token;
21913 +                       break;
21914 +
21915 +               case Opt_trunc_xino_path:
21916 +                       err = au_opts_parse_xino_itrunc_path
21917 +                               (sb, &opt->xino_itrunc, a->args);
21918 +                       if (!err)
21919 +                               opt->type = token;
21920 +                       break;
21921 +
21922 +               case Opt_itrunc_xino:
21923 +                       u.xino_itrunc = &opt->xino_itrunc;
21924 +                       if (unlikely(match_int(&a->args[0], &n))) {
21925 +                               pr_err("bad integer in %s\n", opt_str);
21926 +                               break;
21927 +                       }
21928 +                       u.xino_itrunc->bindex = n;
21929 +                       aufs_read_lock(root, AuLock_FLUSH);
21930 +                       if (n < 0 || au_sbend(sb) < n) {
21931 +                               pr_err("out of bounds, %d\n", n);
21932 +                               aufs_read_unlock(root, !AuLock_IR);
21933 +                               break;
21934 +                       }
21935 +                       aufs_read_unlock(root, !AuLock_IR);
21936 +                       err = 0;
21937 +                       opt->type = token;
21938 +                       break;
21939 +
21940 +               case Opt_dirwh:
21941 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
21942 +                               break;
21943 +                       err = 0;
21944 +                       opt->type = token;
21945 +                       break;
21946 +
21947 +               case Opt_rdcache:
21948 +                       if (unlikely(match_int(&a->args[0], &n))) {
21949 +                               pr_err("bad integer in %s\n", opt_str);
21950 +                               break;
21951 +                       }
21952 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
21953 +                               pr_err("rdcache must be smaller than %d\n",
21954 +                                      AUFS_RDCACHE_MAX);
21955 +                               break;
21956 +                       }
21957 +                       opt->rdcache = n;
21958 +                       err = 0;
21959 +                       opt->type = token;
21960 +                       break;
21961 +               case Opt_rdblk:
21962 +                       if (unlikely(match_int(&a->args[0], &n)
21963 +                                    || n < 0
21964 +                                    || n > KMALLOC_MAX_SIZE)) {
21965 +                               pr_err("bad integer in %s\n", opt_str);
21966 +                               break;
21967 +                       }
21968 +                       if (unlikely(n && n < NAME_MAX)) {
21969 +                               pr_err("rdblk must be larger than %d\n",
21970 +                                      NAME_MAX);
21971 +                               break;
21972 +                       }
21973 +                       opt->rdblk = n;
21974 +                       err = 0;
21975 +                       opt->type = token;
21976 +                       break;
21977 +               case Opt_rdhash:
21978 +                       if (unlikely(match_int(&a->args[0], &n)
21979 +                                    || n < 0
21980 +                                    || n * sizeof(struct hlist_head)
21981 +                                    > KMALLOC_MAX_SIZE)) {
21982 +                               pr_err("bad integer in %s\n", opt_str);
21983 +                               break;
21984 +                       }
21985 +                       opt->rdhash = n;
21986 +                       err = 0;
21987 +                       opt->type = token;
21988 +                       break;
21989 +
21990 +               case Opt_trunc_xino:
21991 +               case Opt_notrunc_xino:
21992 +               case Opt_noxino:
21993 +               case Opt_trunc_xib:
21994 +               case Opt_notrunc_xib:
21995 +               case Opt_shwh:
21996 +               case Opt_noshwh:
21997 +               case Opt_plink:
21998 +               case Opt_noplink:
21999 +               case Opt_list_plink:
22000 +               case Opt_dio:
22001 +               case Opt_nodio:
22002 +               case Opt_diropq_a:
22003 +               case Opt_diropq_w:
22004 +               case Opt_warn_perm:
22005 +               case Opt_nowarn_perm:
22006 +               case Opt_refrof:
22007 +               case Opt_norefrof:
22008 +               case Opt_verbose:
22009 +               case Opt_noverbose:
22010 +               case Opt_sum:
22011 +               case Opt_nosum:
22012 +               case Opt_wsum:
22013 +               case Opt_rdblk_def:
22014 +               case Opt_rdhash_def:
22015 +                       err = 0;
22016 +                       opt->type = token;
22017 +                       break;
22018 +
22019 +               case Opt_udba:
22020 +                       opt->udba = udba_val(a->args[0].from);
22021 +                       if (opt->udba >= 0) {
22022 +                               err = 0;
22023 +                               opt->type = token;
22024 +                       } else
22025 +                               pr_err("wrong value, %s\n", opt_str);
22026 +                       break;
22027 +
22028 +               case Opt_wbr_create:
22029 +                       u.create = &opt->wbr_create;
22030 +                       u.create->wbr_create
22031 +                               = au_wbr_create_val(a->args[0].from, u.create);
22032 +                       if (u.create->wbr_create >= 0) {
22033 +                               err = 0;
22034 +                               opt->type = token;
22035 +                       } else
22036 +                               pr_err("wrong value, %s\n", opt_str);
22037 +                       break;
22038 +               case Opt_wbr_copyup:
22039 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
22040 +                       if (opt->wbr_copyup >= 0) {
22041 +                               err = 0;
22042 +                               opt->type = token;
22043 +                       } else
22044 +                               pr_err("wrong value, %s\n", opt_str);
22045 +                       break;
22046 +
22047 +               case Opt_ignore:
22048 +                       pr_warn("ignored %s\n", opt_str);
22049 +                       /*FALLTHROUGH*/
22050 +               case Opt_ignore_silent:
22051 +                       skipped = 1;
22052 +                       err = 0;
22053 +                       break;
22054 +               case Opt_err:
22055 +                       pr_err("unknown option %s\n", opt_str);
22056 +                       break;
22057 +               }
22058 +
22059 +               if (!err && !skipped) {
22060 +                       if (unlikely(++opt > opt_tail)) {
22061 +                               err = -E2BIG;
22062 +                               opt--;
22063 +                               opt->type = Opt_tail;
22064 +                               break;
22065 +                       }
22066 +                       opt->type = Opt_tail;
22067 +               }
22068 +       }
22069 +
22070 +       kfree(a);
22071 +       dump_opts(opts);
22072 +       if (unlikely(err))
22073 +               au_opts_free(opts);
22074 +
22075 +out:
22076 +       return err;
22077 +}
22078 +
22079 +static int au_opt_wbr_create(struct super_block *sb,
22080 +                            struct au_opt_wbr_create *create)
22081 +{
22082 +       int err;
22083 +       struct au_sbinfo *sbinfo;
22084 +
22085 +       SiMustWriteLock(sb);
22086 +
22087 +       err = 1; /* handled */
22088 +       sbinfo = au_sbi(sb);
22089 +       if (sbinfo->si_wbr_create_ops->fin) {
22090 +               err = sbinfo->si_wbr_create_ops->fin(sb);
22091 +               if (!err)
22092 +                       err = 1;
22093 +       }
22094 +
22095 +       sbinfo->si_wbr_create = create->wbr_create;
22096 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
22097 +       switch (create->wbr_create) {
22098 +       case AuWbrCreate_MFSRRV:
22099 +       case AuWbrCreate_MFSRR:
22100 +       case AuWbrCreate_PMFSRR:
22101 +       case AuWbrCreate_PMFSRRV:
22102 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
22103 +               /*FALLTHROUGH*/
22104 +       case AuWbrCreate_MFS:
22105 +       case AuWbrCreate_MFSV:
22106 +       case AuWbrCreate_PMFS:
22107 +       case AuWbrCreate_PMFSV:
22108 +               sbinfo->si_wbr_mfs.mfs_expire
22109 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
22110 +               break;
22111 +       }
22112 +
22113 +       if (sbinfo->si_wbr_create_ops->init)
22114 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
22115 +
22116 +       return err;
22117 +}
22118 +
22119 +/*
22120 + * returns,
22121 + * plus: processed without an error
22122 + * zero: unprocessed
22123 + */
22124 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
22125 +                        struct au_opts *opts)
22126 +{
22127 +       int err;
22128 +       struct au_sbinfo *sbinfo;
22129 +
22130 +       SiMustWriteLock(sb);
22131 +
22132 +       err = 1; /* handled */
22133 +       sbinfo = au_sbi(sb);
22134 +       switch (opt->type) {
22135 +       case Opt_udba:
22136 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
22137 +               sbinfo->si_mntflags |= opt->udba;
22138 +               opts->given_udba |= opt->udba;
22139 +               break;
22140 +
22141 +       case Opt_plink:
22142 +               au_opt_set(sbinfo->si_mntflags, PLINK);
22143 +               break;
22144 +       case Opt_noplink:
22145 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
22146 +                       au_plink_put(sb, /*verbose*/1);
22147 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
22148 +               break;
22149 +       case Opt_list_plink:
22150 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
22151 +                       au_plink_list(sb);
22152 +               break;
22153 +
22154 +       case Opt_dio:
22155 +               au_opt_set(sbinfo->si_mntflags, DIO);
22156 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
22157 +               break;
22158 +       case Opt_nodio:
22159 +               au_opt_clr(sbinfo->si_mntflags, DIO);
22160 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
22161 +               break;
22162 +
22163 +       case Opt_diropq_a:
22164 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
22165 +               break;
22166 +       case Opt_diropq_w:
22167 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
22168 +               break;
22169 +
22170 +       case Opt_warn_perm:
22171 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
22172 +               break;
22173 +       case Opt_nowarn_perm:
22174 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
22175 +               break;
22176 +
22177 +       case Opt_refrof:
22178 +               au_opt_set(sbinfo->si_mntflags, REFROF);
22179 +               break;
22180 +       case Opt_norefrof:
22181 +               au_opt_clr(sbinfo->si_mntflags, REFROF);
22182 +               break;
22183 +
22184 +       case Opt_verbose:
22185 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
22186 +               break;
22187 +       case Opt_noverbose:
22188 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
22189 +               break;
22190 +
22191 +       case Opt_sum:
22192 +               au_opt_set(sbinfo->si_mntflags, SUM);
22193 +               break;
22194 +       case Opt_wsum:
22195 +               au_opt_clr(sbinfo->si_mntflags, SUM);
22196 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
22197 +       case Opt_nosum:
22198 +               au_opt_clr(sbinfo->si_mntflags, SUM);
22199 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
22200 +               break;
22201 +
22202 +       case Opt_wbr_create:
22203 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
22204 +               break;
22205 +       case Opt_wbr_copyup:
22206 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
22207 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
22208 +               break;
22209 +
22210 +       case Opt_dirwh:
22211 +               sbinfo->si_dirwh = opt->dirwh;
22212 +               break;
22213 +
22214 +       case Opt_rdcache:
22215 +               sbinfo->si_rdcache
22216 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
22217 +               break;
22218 +       case Opt_rdblk:
22219 +               sbinfo->si_rdblk = opt->rdblk;
22220 +               break;
22221 +       case Opt_rdblk_def:
22222 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
22223 +               break;
22224 +       case Opt_rdhash:
22225 +               sbinfo->si_rdhash = opt->rdhash;
22226 +               break;
22227 +       case Opt_rdhash_def:
22228 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
22229 +               break;
22230 +
22231 +       case Opt_shwh:
22232 +               au_opt_set(sbinfo->si_mntflags, SHWH);
22233 +               break;
22234 +       case Opt_noshwh:
22235 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
22236 +               break;
22237 +
22238 +       case Opt_trunc_xino:
22239 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
22240 +               break;
22241 +       case Opt_notrunc_xino:
22242 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
22243 +               break;
22244 +
22245 +       case Opt_trunc_xino_path:
22246 +       case Opt_itrunc_xino:
22247 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
22248 +               if (!err)
22249 +                       err = 1;
22250 +               break;
22251 +
22252 +       case Opt_trunc_xib:
22253 +               au_fset_opts(opts->flags, TRUNC_XIB);
22254 +               break;
22255 +       case Opt_notrunc_xib:
22256 +               au_fclr_opts(opts->flags, TRUNC_XIB);
22257 +               break;
22258 +
22259 +       default:
22260 +               err = 0;
22261 +               break;
22262 +       }
22263 +
22264 +       return err;
22265 +}
22266 +
22267 +/*
22268 + * returns tri-state.
22269 + * plus: processed without an error
22270 + * zero: unprocessed
22271 + * minus: error
22272 + */
22273 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
22274 +                    struct au_opts *opts)
22275 +{
22276 +       int err, do_refresh;
22277 +
22278 +       err = 0;
22279 +       switch (opt->type) {
22280 +       case Opt_append:
22281 +               opt->add.bindex = au_sbend(sb) + 1;
22282 +               if (opt->add.bindex < 0)
22283 +                       opt->add.bindex = 0;
22284 +               goto add;
22285 +       case Opt_prepend:
22286 +               opt->add.bindex = 0;
22287 +       add: /* indented label */
22288 +       case Opt_add:
22289 +               err = au_br_add(sb, &opt->add,
22290 +                               au_ftest_opts(opts->flags, REMOUNT));
22291 +               if (!err) {
22292 +                       err = 1;
22293 +                       au_fset_opts(opts->flags, REFRESH);
22294 +               }
22295 +               break;
22296 +
22297 +       case Opt_del:
22298 +       case Opt_idel:
22299 +               err = au_br_del(sb, &opt->del,
22300 +                               au_ftest_opts(opts->flags, REMOUNT));
22301 +               if (!err) {
22302 +                       err = 1;
22303 +                       au_fset_opts(opts->flags, TRUNC_XIB);
22304 +                       au_fset_opts(opts->flags, REFRESH);
22305 +               }
22306 +               break;
22307 +
22308 +       case Opt_mod:
22309 +       case Opt_imod:
22310 +               err = au_br_mod(sb, &opt->mod,
22311 +                               au_ftest_opts(opts->flags, REMOUNT),
22312 +                               &do_refresh);
22313 +               if (!err) {
22314 +                       err = 1;
22315 +                       if (do_refresh)
22316 +                               au_fset_opts(opts->flags, REFRESH);
22317 +               }
22318 +               break;
22319 +       }
22320 +
22321 +       return err;
22322 +}
22323 +
22324 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
22325 +                      struct au_opt_xino **opt_xino,
22326 +                      struct au_opts *opts)
22327 +{
22328 +       int err;
22329 +       aufs_bindex_t bend, bindex;
22330 +       struct dentry *root, *parent, *h_root;
22331 +
22332 +       err = 0;
22333 +       switch (opt->type) {
22334 +       case Opt_xino:
22335 +               err = au_xino_set(sb, &opt->xino,
22336 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
22337 +               if (unlikely(err))
22338 +                       break;
22339 +
22340 +               *opt_xino = &opt->xino;
22341 +               au_xino_brid_set(sb, -1);
22342 +
22343 +               /* safe d_parent access */
22344 +               parent = opt->xino.file->f_dentry->d_parent;
22345 +               root = sb->s_root;
22346 +               bend = au_sbend(sb);
22347 +               for (bindex = 0; bindex <= bend; bindex++) {
22348 +                       h_root = au_h_dptr(root, bindex);
22349 +                       if (h_root == parent) {
22350 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
22351 +                               break;
22352 +                       }
22353 +               }
22354 +               break;
22355 +
22356 +       case Opt_noxino:
22357 +               au_xino_clr(sb);
22358 +               au_xino_brid_set(sb, -1);
22359 +               *opt_xino = (void *)-1;
22360 +               break;
22361 +       }
22362 +
22363 +       return err;
22364 +}
22365 +
22366 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
22367 +                  unsigned int pending)
22368 +{
22369 +       int err;
22370 +       aufs_bindex_t bindex, bend;
22371 +       unsigned char do_plink, skip, do_free;
22372 +       struct au_branch *br;
22373 +       struct au_wbr *wbr;
22374 +       struct dentry *root;
22375 +       struct inode *dir, *h_dir;
22376 +       struct au_sbinfo *sbinfo;
22377 +       struct au_hinode *hdir;
22378 +
22379 +       SiMustAnyLock(sb);
22380 +
22381 +       sbinfo = au_sbi(sb);
22382 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
22383 +
22384 +       if (!(sb_flags & MS_RDONLY)) {
22385 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
22386 +                       pr_warn("first branch should be rw\n");
22387 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
22388 +                       pr_warn("shwh should be used with ro\n");
22389 +       }
22390 +
22391 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
22392 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
22393 +               pr_warn("udba=*notify requires xino\n");
22394 +
22395 +       err = 0;
22396 +       root = sb->s_root;
22397 +       dir = root->d_inode;
22398 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
22399 +       bend = au_sbend(sb);
22400 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
22401 +               skip = 0;
22402 +               h_dir = au_h_iptr(dir, bindex);
22403 +               br = au_sbr(sb, bindex);
22404 +               do_free = 0;
22405 +
22406 +               wbr = br->br_wbr;
22407 +               if (wbr)
22408 +                       wbr_wh_read_lock(wbr);
22409 +
22410 +               if (!au_br_writable(br->br_perm)) {
22411 +                       do_free = !!wbr;
22412 +                       skip = (!wbr
22413 +                               || (!wbr->wbr_whbase
22414 +                                   && !wbr->wbr_plink
22415 +                                   && !wbr->wbr_orph));
22416 +               } else if (!au_br_wh_linkable(br->br_perm)) {
22417 +                       /* skip = (!br->br_whbase && !br->br_orph); */
22418 +                       skip = (!wbr || !wbr->wbr_whbase);
22419 +                       if (skip && wbr) {
22420 +                               if (do_plink)
22421 +                                       skip = !!wbr->wbr_plink;
22422 +                               else
22423 +                                       skip = !wbr->wbr_plink;
22424 +                       }
22425 +               } else {
22426 +                       /* skip = (br->br_whbase && br->br_ohph); */
22427 +                       skip = (wbr && wbr->wbr_whbase);
22428 +                       if (skip) {
22429 +                               if (do_plink)
22430 +                                       skip = !!wbr->wbr_plink;
22431 +                               else
22432 +                                       skip = !wbr->wbr_plink;
22433 +                       }
22434 +               }
22435 +               if (wbr)
22436 +                       wbr_wh_read_unlock(wbr);
22437 +
22438 +               if (skip)
22439 +                       continue;
22440 +
22441 +               hdir = au_hi(dir, bindex);
22442 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
22443 +               if (wbr)
22444 +                       wbr_wh_write_lock(wbr);
22445 +               err = au_wh_init(br, sb);
22446 +               if (wbr)
22447 +                       wbr_wh_write_unlock(wbr);
22448 +               au_hn_imtx_unlock(hdir);
22449 +
22450 +               if (!err && do_free) {
22451 +                       kfree(wbr);
22452 +                       br->br_wbr = NULL;
22453 +               }
22454 +       }
22455 +
22456 +       return err;
22457 +}
22458 +
22459 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
22460 +{
22461 +       int err;
22462 +       unsigned int tmp;
22463 +       aufs_bindex_t bindex, bend;
22464 +       struct au_opt *opt;
22465 +       struct au_opt_xino *opt_xino, xino;
22466 +       struct au_sbinfo *sbinfo;
22467 +       struct au_branch *br;
22468 +
22469 +       SiMustWriteLock(sb);
22470 +
22471 +       err = 0;
22472 +       opt_xino = NULL;
22473 +       opt = opts->opt;
22474 +       while (err >= 0 && opt->type != Opt_tail)
22475 +               err = au_opt_simple(sb, opt++, opts);
22476 +       if (err > 0)
22477 +               err = 0;
22478 +       else if (unlikely(err < 0))
22479 +               goto out;
22480 +
22481 +       /* disable xino and udba temporary */
22482 +       sbinfo = au_sbi(sb);
22483 +       tmp = sbinfo->si_mntflags;
22484 +       au_opt_clr(sbinfo->si_mntflags, XINO);
22485 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
22486 +
22487 +       opt = opts->opt;
22488 +       while (err >= 0 && opt->type != Opt_tail)
22489 +               err = au_opt_br(sb, opt++, opts);
22490 +       if (err > 0)
22491 +               err = 0;
22492 +       else if (unlikely(err < 0))
22493 +               goto out;
22494 +
22495 +       bend = au_sbend(sb);
22496 +       if (unlikely(bend < 0)) {
22497 +               err = -EINVAL;
22498 +               pr_err("no branches\n");
22499 +               goto out;
22500 +       }
22501 +
22502 +       if (au_opt_test(tmp, XINO))
22503 +               au_opt_set(sbinfo->si_mntflags, XINO);
22504 +       opt = opts->opt;
22505 +       while (!err && opt->type != Opt_tail)
22506 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
22507 +       if (unlikely(err))
22508 +               goto out;
22509 +
22510 +       err = au_opts_verify(sb, sb->s_flags, tmp);
22511 +       if (unlikely(err))
22512 +               goto out;
22513 +
22514 +       /* restore xino */
22515 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
22516 +               xino.file = au_xino_def(sb);
22517 +               err = PTR_ERR(xino.file);
22518 +               if (IS_ERR(xino.file))
22519 +                       goto out;
22520 +
22521 +               err = au_xino_set(sb, &xino, /*remount*/0);
22522 +               fput(xino.file);
22523 +               if (unlikely(err))
22524 +                       goto out;
22525 +       }
22526 +
22527 +       /* restore udba */
22528 +       tmp &= AuOptMask_UDBA;
22529 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
22530 +       sbinfo->si_mntflags |= tmp;
22531 +       bend = au_sbend(sb);
22532 +       for (bindex = 0; bindex <= bend; bindex++) {
22533 +               br = au_sbr(sb, bindex);
22534 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
22535 +               if (unlikely(err))
22536 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
22537 +                               bindex, err);
22538 +               /* go on even if err */
22539 +       }
22540 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
22541 +               struct inode *dir = sb->s_root->d_inode;
22542 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
22543 +       }
22544 +
22545 +out:
22546 +       return err;
22547 +}
22548 +
22549 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
22550 +{
22551 +       int err, rerr;
22552 +       struct inode *dir;
22553 +       struct au_opt_xino *opt_xino;
22554 +       struct au_opt *opt;
22555 +       struct au_sbinfo *sbinfo;
22556 +
22557 +       SiMustWriteLock(sb);
22558 +
22559 +       dir = sb->s_root->d_inode;
22560 +       sbinfo = au_sbi(sb);
22561 +       err = 0;
22562 +       opt_xino = NULL;
22563 +       opt = opts->opt;
22564 +       while (err >= 0 && opt->type != Opt_tail) {
22565 +               err = au_opt_simple(sb, opt, opts);
22566 +               if (!err)
22567 +                       err = au_opt_br(sb, opt, opts);
22568 +               if (!err)
22569 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
22570 +               opt++;
22571 +       }
22572 +       if (err > 0)
22573 +               err = 0;
22574 +       AuTraceErr(err);
22575 +       /* go on even err */
22576 +
22577 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
22578 +       if (unlikely(rerr && !err))
22579 +               err = rerr;
22580 +
22581 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
22582 +               rerr = au_xib_trunc(sb);
22583 +               if (unlikely(rerr && !err))
22584 +                       err = rerr;
22585 +       }
22586 +
22587 +       /* will be handled by the caller */
22588 +       if (!au_ftest_opts(opts->flags, REFRESH)
22589 +           && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
22590 +               au_fset_opts(opts->flags, REFRESH);
22591 +
22592 +       AuDbg("status 0x%x\n", opts->flags);
22593 +       return err;
22594 +}
22595 +
22596 +/* ---------------------------------------------------------------------- */
22597 +
22598 +unsigned int au_opt_udba(struct super_block *sb)
22599 +{
22600 +       return au_mntflags(sb) & AuOptMask_UDBA;
22601 +}
22602 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
22603 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
22604 +++ linux/fs/aufs/opts.h        2014-06-24 00:14:06.786052822 +0200
22605 @@ -0,0 +1,210 @@
22606 +/*
22607 + * Copyright (C) 2005-2014 Junjiro R. Okajima
22608 + *
22609 + * This program, aufs is free software; you can redistribute it and/or modify
22610 + * it under the terms of the GNU General Public License as published by
22611 + * the Free Software Foundation; either version 2 of the License, or
22612 + * (at your option) any later version.
22613 + *
22614 + * This program is distributed in the hope that it will be useful,
22615 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22616 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22617 + * GNU General Public License for more details.
22618 + *
22619 + * You should have received a copy of the GNU General Public License
22620 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22621 + */
22622 +
22623 +/*
22624 + * mount options/flags
22625 + */
22626 +
22627 +#ifndef __AUFS_OPTS_H__
22628 +#define __AUFS_OPTS_H__
22629 +
22630 +#ifdef __KERNEL__
22631 +
22632 +#include <linux/path.h>
22633 +
22634 +struct file;
22635 +struct super_block;
22636 +
22637 +/* ---------------------------------------------------------------------- */
22638 +
22639 +/* mount flags */
22640 +#define AuOpt_XINO             1               /* external inode number bitmap
22641 +                                                  and translation table */
22642 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
22643 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
22644 +#define AuOpt_UDBA_REVAL       (1 << 3)
22645 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
22646 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
22647 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
22648 +#define AuOpt_DIRPERM1         (1 << 7)        /* unimplemented */
22649 +#define AuOpt_REFROF           (1 << 8)        /* unimplemented */
22650 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
22651 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
22652 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
22653 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
22654 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
22655 +#define AuOpt_DIO              (1 << 14)       /* direct io */
22656 +
22657 +#ifndef CONFIG_AUFS_HNOTIFY
22658 +#undef AuOpt_UDBA_HNOTIFY
22659 +#define AuOpt_UDBA_HNOTIFY     0
22660 +#endif
22661 +#ifndef CONFIG_AUFS_SHWH
22662 +#undef AuOpt_SHWH
22663 +#define AuOpt_SHWH             0
22664 +#endif
22665 +
22666 +#define AuOpt_Def      (AuOpt_XINO \
22667 +                        | AuOpt_UDBA_REVAL \
22668 +                        | AuOpt_PLINK \
22669 +                        /* | AuOpt_DIRPERM1 */ \
22670 +                        | AuOpt_WARN_PERM)
22671 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
22672 +                        | AuOpt_UDBA_REVAL \
22673 +                        | AuOpt_UDBA_HNOTIFY)
22674 +
22675 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
22676 +#define au_opt_set(flags, name) do { \
22677 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
22678 +       ((flags) |= AuOpt_##name); \
22679 +} while (0)
22680 +#define au_opt_set_udba(flags, name) do { \
22681 +       (flags) &= ~AuOptMask_UDBA; \
22682 +       ((flags) |= AuOpt_##name); \
22683 +} while (0)
22684 +#define au_opt_clr(flags, name) do { \
22685 +       ((flags) &= ~AuOpt_##name); \
22686 +} while (0)
22687 +
22688 +static inline unsigned int au_opts_plink(unsigned int mntflags)
22689 +{
22690 +#ifdef CONFIG_PROC_FS
22691 +       return mntflags;
22692 +#else
22693 +       return mntflags & ~AuOpt_PLINK;
22694 +#endif
22695 +}
22696 +
22697 +/* ---------------------------------------------------------------------- */
22698 +
22699 +/* policies to select one among multiple writable branches */
22700 +enum {
22701 +       AuWbrCreate_TDP,        /* top down parent */
22702 +       AuWbrCreate_RR,         /* round robin */
22703 +       AuWbrCreate_MFS,        /* most free space */
22704 +       AuWbrCreate_MFSV,       /* mfs with seconds */
22705 +       AuWbrCreate_MFSRR,      /* mfs then rr */
22706 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
22707 +       AuWbrCreate_PMFS,       /* parent and mfs */
22708 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
22709 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
22710 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
22711 +
22712 +       AuWbrCreate_Def = AuWbrCreate_TDP
22713 +};
22714 +
22715 +enum {
22716 +       AuWbrCopyup_TDP,        /* top down parent */
22717 +       AuWbrCopyup_BUP,        /* bottom up parent */
22718 +       AuWbrCopyup_BU,         /* bottom up */
22719 +
22720 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
22721 +};
22722 +
22723 +/* ---------------------------------------------------------------------- */
22724 +
22725 +struct au_opt_add {
22726 +       aufs_bindex_t   bindex;
22727 +       char            *pathname;
22728 +       int             perm;
22729 +       struct path     path;
22730 +};
22731 +
22732 +struct au_opt_del {
22733 +       char            *pathname;
22734 +       struct path     h_path;
22735 +};
22736 +
22737 +struct au_opt_mod {
22738 +       char            *path;
22739 +       int             perm;
22740 +       struct dentry   *h_root;
22741 +};
22742 +
22743 +struct au_opt_xino {
22744 +       char            *path;
22745 +       struct file     *file;
22746 +};
22747 +
22748 +struct au_opt_xino_itrunc {
22749 +       aufs_bindex_t   bindex;
22750 +};
22751 +
22752 +struct au_opt_wbr_create {
22753 +       int                     wbr_create;
22754 +       int                     mfs_second;
22755 +       unsigned long long      mfsrr_watermark;
22756 +};
22757 +
22758 +struct au_opt {
22759 +       int type;
22760 +       union {
22761 +               struct au_opt_xino      xino;
22762 +               struct au_opt_xino_itrunc xino_itrunc;
22763 +               struct au_opt_add       add;
22764 +               struct au_opt_del       del;
22765 +               struct au_opt_mod       mod;
22766 +               int                     dirwh;
22767 +               int                     rdcache;
22768 +               unsigned int            rdblk;
22769 +               unsigned int            rdhash;
22770 +               int                     udba;
22771 +               struct au_opt_wbr_create wbr_create;
22772 +               int                     wbr_copyup;
22773 +       };
22774 +};
22775 +
22776 +/* opts flags */
22777 +#define AuOpts_REMOUNT         1
22778 +#define AuOpts_REFRESH         (1 << 1)
22779 +#define AuOpts_TRUNC_XIB       (1 << 2)
22780 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
22781 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
22782 +#define au_fset_opts(flags, name) \
22783 +       do { (flags) |= AuOpts_##name; } while (0)
22784 +#define au_fclr_opts(flags, name) \
22785 +       do { (flags) &= ~AuOpts_##name; } while (0)
22786 +
22787 +struct au_opts {
22788 +       struct au_opt   *opt;
22789 +       int             max_opt;
22790 +
22791 +       unsigned int    given_udba;
22792 +       unsigned int    flags;
22793 +       unsigned long   sb_flags;
22794 +};
22795 +
22796 +/* ---------------------------------------------------------------------- */
22797 +
22798 +char *au_optstr_br_perm(int brperm);
22799 +const char *au_optstr_udba(int udba);
22800 +const char *au_optstr_wbr_copyup(int wbr_copyup);
22801 +const char *au_optstr_wbr_create(int wbr_create);
22802 +
22803 +void au_opts_free(struct au_opts *opts);
22804 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
22805 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
22806 +                  unsigned int pending);
22807 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
22808 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
22809 +
22810 +unsigned int au_opt_udba(struct super_block *sb);
22811 +
22812 +/* ---------------------------------------------------------------------- */
22813 +
22814 +#endif /* __KERNEL__ */
22815 +#endif /* __AUFS_OPTS_H__ */
22816 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
22817 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
22818 +++ linux/fs/aufs/plink.c       2014-06-24 00:14:06.786052822 +0200
22819 @@ -0,0 +1,532 @@
22820 +/*
22821 + * Copyright (C) 2005-2014 Junjiro R. Okajima
22822 + *
22823 + * This program, aufs is free software; you can redistribute it and/or modify
22824 + * it under the terms of the GNU General Public License as published by
22825 + * the Free Software Foundation; either version 2 of the License, or
22826 + * (at your option) any later version.
22827 + *
22828 + * This program is distributed in the hope that it will be useful,
22829 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22830 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22831 + * GNU General Public License for more details.
22832 + *
22833 + * You should have received a copy of the GNU General Public License
22834 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22835 + */
22836 +
22837 +/*
22838 + * pseudo-link
22839 + */
22840 +
22841 +#include "aufs.h"
22842 +
22843 +/*
22844 + * the pseudo-link maintenance mode.
22845 + * during a user process maintains the pseudo-links,
22846 + * prohibit adding a new plink and branch manipulation.
22847 + *
22848 + * Flags
22849 + * NOPLM:
22850 + *     For entry functions which will handle plink, and i_mutex is already held
22851 + *     in VFS.
22852 + *     They cannot wait and should return an error at once.
22853 + *     Callers has to check the error.
22854 + * NOPLMW:
22855 + *     For entry functions which will handle plink, but i_mutex is not held
22856 + *     in VFS.
22857 + *     They can wait the plink maintenance mode to finish.
22858 + *
22859 + * They behave like F_SETLK and F_SETLKW.
22860 + * If the caller never handle plink, then both flags are unnecessary.
22861 + */
22862 +
22863 +int au_plink_maint(struct super_block *sb, int flags)
22864 +{
22865 +       int err;
22866 +       pid_t pid, ppid;
22867 +       struct au_sbinfo *sbi;
22868 +
22869 +       SiMustAnyLock(sb);
22870 +
22871 +       err = 0;
22872 +       if (!au_opt_test(au_mntflags(sb), PLINK))
22873 +               goto out;
22874 +
22875 +       sbi = au_sbi(sb);
22876 +       pid = sbi->si_plink_maint_pid;
22877 +       if (!pid || pid == current->pid)
22878 +               goto out;
22879 +
22880 +       /* todo: it highly depends upon /sbin/mount.aufs */
22881 +       rcu_read_lock();
22882 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
22883 +       rcu_read_unlock();
22884 +       if (pid == ppid)
22885 +               goto out;
22886 +
22887 +       if (au_ftest_lock(flags, NOPLMW)) {
22888 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
22889 +               /* AuDebugOn(!lockdep_depth(current)); */
22890 +               while (sbi->si_plink_maint_pid) {
22891 +                       si_read_unlock(sb);
22892 +                       /* gave up wake_up_bit() */
22893 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
22894 +
22895 +                       if (au_ftest_lock(flags, FLUSH))
22896 +                               au_nwt_flush(&sbi->si_nowait);
22897 +                       si_noflush_read_lock(sb);
22898 +               }
22899 +       } else if (au_ftest_lock(flags, NOPLM)) {
22900 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
22901 +               err = -EAGAIN;
22902 +       }
22903 +
22904 +out:
22905 +       return err;
22906 +}
22907 +
22908 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
22909 +{
22910 +       spin_lock(&sbinfo->si_plink_maint_lock);
22911 +       sbinfo->si_plink_maint_pid = 0;
22912 +       spin_unlock(&sbinfo->si_plink_maint_lock);
22913 +       wake_up_all(&sbinfo->si_plink_wq);
22914 +}
22915 +
22916 +int au_plink_maint_enter(struct super_block *sb)
22917 +{
22918 +       int err;
22919 +       struct au_sbinfo *sbinfo;
22920 +
22921 +       err = 0;
22922 +       sbinfo = au_sbi(sb);
22923 +       /* make sure i am the only one in this fs */
22924 +       si_write_lock(sb, AuLock_FLUSH);
22925 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
22926 +               spin_lock(&sbinfo->si_plink_maint_lock);
22927 +               if (!sbinfo->si_plink_maint_pid)
22928 +                       sbinfo->si_plink_maint_pid = current->pid;
22929 +               else
22930 +                       err = -EBUSY;
22931 +               spin_unlock(&sbinfo->si_plink_maint_lock);
22932 +       }
22933 +       si_write_unlock(sb);
22934 +
22935 +       return err;
22936 +}
22937 +
22938 +/* ---------------------------------------------------------------------- */
22939 +
22940 +#ifdef CONFIG_AUFS_DEBUG
22941 +void au_plink_list(struct super_block *sb)
22942 +{
22943 +       int i;
22944 +       struct au_sbinfo *sbinfo;
22945 +       struct hlist_head *plink_hlist;
22946 +       struct pseudo_link *plink;
22947 +
22948 +       SiMustAnyLock(sb);
22949 +
22950 +       sbinfo = au_sbi(sb);
22951 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
22952 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
22953 +
22954 +       for (i = 0; i < AuPlink_NHASH; i++) {
22955 +               plink_hlist = &sbinfo->si_plink[i].head;
22956 +               rcu_read_lock();
22957 +               hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
22958 +                       AuDbg("%lu\n", plink->inode->i_ino);
22959 +               rcu_read_unlock();
22960 +       }
22961 +}
22962 +#endif
22963 +
22964 +/* is the inode pseudo-linked? */
22965 +int au_plink_test(struct inode *inode)
22966 +{
22967 +       int found, i;
22968 +       struct au_sbinfo *sbinfo;
22969 +       struct hlist_head *plink_hlist;
22970 +       struct pseudo_link *plink;
22971 +
22972 +       sbinfo = au_sbi(inode->i_sb);
22973 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
22974 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
22975 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
22976 +
22977 +       found = 0;
22978 +       i = au_plink_hash(inode->i_ino);
22979 +       plink_hlist = &sbinfo->si_plink[i].head;
22980 +       rcu_read_lock();
22981 +       hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
22982 +               if (plink->inode == inode) {
22983 +                       found = 1;
22984 +                       break;
22985 +               }
22986 +       rcu_read_unlock();
22987 +       return found;
22988 +}
22989 +
22990 +/* ---------------------------------------------------------------------- */
22991 +
22992 +/*
22993 + * generate a name for plink.
22994 + * the file will be stored under AUFS_WH_PLINKDIR.
22995 + */
22996 +/* 20 is max digits length of ulong 64 */
22997 +#define PLINK_NAME_LEN ((20 + 1) * 2)
22998 +
22999 +static int plink_name(char *name, int len, struct inode *inode,
23000 +                     aufs_bindex_t bindex)
23001 +{
23002 +       int rlen;
23003 +       struct inode *h_inode;
23004 +
23005 +       h_inode = au_h_iptr(inode, bindex);
23006 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
23007 +       return rlen;
23008 +}
23009 +
23010 +struct au_do_plink_lkup_args {
23011 +       struct dentry **errp;
23012 +       struct qstr *tgtname;
23013 +       struct dentry *h_parent;
23014 +       struct au_branch *br;
23015 +};
23016 +
23017 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
23018 +                                      struct dentry *h_parent,
23019 +                                      struct au_branch *br)
23020 +{
23021 +       struct dentry *h_dentry;
23022 +       struct mutex *h_mtx;
23023 +
23024 +       h_mtx = &h_parent->d_inode->i_mutex;
23025 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
23026 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
23027 +       mutex_unlock(h_mtx);
23028 +       return h_dentry;
23029 +}
23030 +
23031 +static void au_call_do_plink_lkup(void *args)
23032 +{
23033 +       struct au_do_plink_lkup_args *a = args;
23034 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
23035 +}
23036 +
23037 +/* lookup the plink-ed @inode under the branch at @bindex */
23038 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
23039 +{
23040 +       struct dentry *h_dentry, *h_parent;
23041 +       struct au_branch *br;
23042 +       struct inode *h_dir;
23043 +       int wkq_err;
23044 +       char a[PLINK_NAME_LEN];
23045 +       struct qstr tgtname = QSTR_INIT(a, 0);
23046 +
23047 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
23048 +
23049 +       br = au_sbr(inode->i_sb, bindex);
23050 +       h_parent = br->br_wbr->wbr_plink;
23051 +       h_dir = h_parent->d_inode;
23052 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
23053 +
23054 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
23055 +               struct au_do_plink_lkup_args args = {
23056 +                       .errp           = &h_dentry,
23057 +                       .tgtname        = &tgtname,
23058 +                       .h_parent       = h_parent,
23059 +                       .br             = br
23060 +               };
23061 +
23062 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
23063 +               if (unlikely(wkq_err))
23064 +                       h_dentry = ERR_PTR(wkq_err);
23065 +       } else
23066 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
23067 +
23068 +       return h_dentry;
23069 +}
23070 +
23071 +/* create a pseudo-link */
23072 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
23073 +                     struct dentry *h_dentry, struct au_branch *br)
23074 +{
23075 +       int err;
23076 +       struct path h_path = {
23077 +               .mnt = au_br_mnt(br)
23078 +       };
23079 +       struct inode *h_dir, *delegated;
23080 +
23081 +       h_dir = h_parent->d_inode;
23082 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
23083 +again:
23084 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
23085 +       err = PTR_ERR(h_path.dentry);
23086 +       if (IS_ERR(h_path.dentry))
23087 +               goto out;
23088 +
23089 +       err = 0;
23090 +       /* wh.plink dir is not monitored */
23091 +       /* todo: is it really safe? */
23092 +       if (h_path.dentry->d_inode
23093 +           && h_path.dentry->d_inode != h_dentry->d_inode) {
23094 +               delegated = NULL;
23095 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
23096 +               if (unlikely(err == -EWOULDBLOCK)) {
23097 +                       pr_warn("cannot retry for NFSv4 delegation"
23098 +                               " for an internal unlink\n");
23099 +                       iput(delegated);
23100 +               }
23101 +               dput(h_path.dentry);
23102 +               h_path.dentry = NULL;
23103 +               if (!err)
23104 +                       goto again;
23105 +       }
23106 +       if (!err && !h_path.dentry->d_inode) {
23107 +               delegated = NULL;
23108 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
23109 +               if (unlikely(err == -EWOULDBLOCK)) {
23110 +                       pr_warn("cannot retry for NFSv4 delegation"
23111 +                               " for an internal link\n");
23112 +                       iput(delegated);
23113 +               }
23114 +       }
23115 +       dput(h_path.dentry);
23116 +
23117 +out:
23118 +       mutex_unlock(&h_dir->i_mutex);
23119 +       return err;
23120 +}
23121 +
23122 +struct do_whplink_args {
23123 +       int *errp;
23124 +       struct qstr *tgt;
23125 +       struct dentry *h_parent;
23126 +       struct dentry *h_dentry;
23127 +       struct au_branch *br;
23128 +};
23129 +
23130 +static void call_do_whplink(void *args)
23131 +{
23132 +       struct do_whplink_args *a = args;
23133 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
23134 +}
23135 +
23136 +static int whplink(struct dentry *h_dentry, struct inode *inode,
23137 +                  aufs_bindex_t bindex, struct au_branch *br)
23138 +{
23139 +       int err, wkq_err;
23140 +       struct au_wbr *wbr;
23141 +       struct dentry *h_parent;
23142 +       struct inode *h_dir;
23143 +       char a[PLINK_NAME_LEN];
23144 +       struct qstr tgtname = QSTR_INIT(a, 0);
23145 +
23146 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
23147 +       h_parent = wbr->wbr_plink;
23148 +       h_dir = h_parent->d_inode;
23149 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
23150 +
23151 +       /* always superio. */
23152 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
23153 +               struct do_whplink_args args = {
23154 +                       .errp           = &err,
23155 +                       .tgt            = &tgtname,
23156 +                       .h_parent       = h_parent,
23157 +                       .h_dentry       = h_dentry,
23158 +                       .br             = br
23159 +               };
23160 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
23161 +               if (unlikely(wkq_err))
23162 +                       err = wkq_err;
23163 +       } else
23164 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
23165 +
23166 +       return err;
23167 +}
23168 +
23169 +/* free a single plink */
23170 +static void do_put_plink(struct pseudo_link *plink, int do_del)
23171 +{
23172 +       if (do_del)
23173 +               hlist_del(&plink->hlist);
23174 +       iput(plink->inode);
23175 +       kfree(plink);
23176 +}
23177 +
23178 +static void do_put_plink_rcu(struct rcu_head *rcu)
23179 +{
23180 +       struct pseudo_link *plink;
23181 +
23182 +       plink = container_of(rcu, struct pseudo_link, rcu);
23183 +       iput(plink->inode);
23184 +       kfree(plink);
23185 +}
23186 +
23187 +/*
23188 + * create a new pseudo-link for @h_dentry on @bindex.
23189 + * the linked inode is held in aufs @inode.
23190 + */
23191 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
23192 +                    struct dentry *h_dentry)
23193 +{
23194 +       struct super_block *sb;
23195 +       struct au_sbinfo *sbinfo;
23196 +       struct hlist_head *plink_hlist;
23197 +       struct pseudo_link *plink, *tmp;
23198 +       struct au_sphlhead *sphl;
23199 +       int found, err, cnt, i;
23200 +
23201 +       sb = inode->i_sb;
23202 +       sbinfo = au_sbi(sb);
23203 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
23204 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
23205 +
23206 +       found = au_plink_test(inode);
23207 +       if (found)
23208 +               return;
23209 +
23210 +       i = au_plink_hash(inode->i_ino);
23211 +       sphl = sbinfo->si_plink + i;
23212 +       plink_hlist = &sphl->head;
23213 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
23214 +       if (tmp)
23215 +               tmp->inode = au_igrab(inode);
23216 +       else {
23217 +               err = -ENOMEM;
23218 +               goto out;
23219 +       }
23220 +
23221 +       spin_lock(&sphl->spin);
23222 +       hlist_for_each_entry(plink, plink_hlist, hlist) {
23223 +               if (plink->inode == inode) {
23224 +                       found = 1;
23225 +                       break;
23226 +               }
23227 +       }
23228 +       if (!found)
23229 +               hlist_add_head_rcu(&tmp->hlist, plink_hlist);
23230 +       spin_unlock(&sphl->spin);
23231 +       if (!found) {
23232 +               cnt = au_sphl_count(sphl);
23233 +#define msg "unexpectedly unblanced or too many pseudo-links"
23234 +               if (cnt > AUFS_PLINK_WARN)
23235 +                       AuWarn1(msg ", %d\n", cnt);
23236 +#undef msg
23237 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
23238 +       } else {
23239 +               do_put_plink(tmp, 0);
23240 +               return;
23241 +       }
23242 +
23243 +out:
23244 +       if (unlikely(err)) {
23245 +               pr_warn("err %d, damaged pseudo link.\n", err);
23246 +               if (tmp) {
23247 +                       au_sphl_del_rcu(&tmp->hlist, sphl);
23248 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
23249 +               }
23250 +       }
23251 +}
23252 +
23253 +/* free all plinks */
23254 +void au_plink_put(struct super_block *sb, int verbose)
23255 +{
23256 +       int i, warned;
23257 +       struct au_sbinfo *sbinfo;
23258 +       struct hlist_head *plink_hlist;
23259 +       struct hlist_node *tmp;
23260 +       struct pseudo_link *plink;
23261 +
23262 +       SiMustWriteLock(sb);
23263 +
23264 +       sbinfo = au_sbi(sb);
23265 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
23266 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
23267 +
23268 +       /* no spin_lock since sbinfo is write-locked */
23269 +       warned = 0;
23270 +       for (i = 0; i < AuPlink_NHASH; i++) {
23271 +               plink_hlist = &sbinfo->si_plink[i].head;
23272 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
23273 +                       pr_warn("pseudo-link is not flushed");
23274 +                       warned = 1;
23275 +               }
23276 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
23277 +                       do_put_plink(plink, 0);
23278 +               INIT_HLIST_HEAD(plink_hlist);
23279 +       }
23280 +}
23281 +
23282 +void au_plink_clean(struct super_block *sb, int verbose)
23283 +{
23284 +       struct dentry *root;
23285 +
23286 +       root = sb->s_root;
23287 +       aufs_write_lock(root);
23288 +       if (au_opt_test(au_mntflags(sb), PLINK))
23289 +               au_plink_put(sb, verbose);
23290 +       aufs_write_unlock(root);
23291 +}
23292 +
23293 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
23294 +{
23295 +       int do_put;
23296 +       aufs_bindex_t bstart, bend, bindex;
23297 +
23298 +       do_put = 0;
23299 +       bstart = au_ibstart(inode);
23300 +       bend = au_ibend(inode);
23301 +       if (bstart >= 0) {
23302 +               for (bindex = bstart; bindex <= bend; bindex++) {
23303 +                       if (!au_h_iptr(inode, bindex)
23304 +                           || au_ii_br_id(inode, bindex) != br_id)
23305 +                               continue;
23306 +                       au_set_h_iptr(inode, bindex, NULL, 0);
23307 +                       do_put = 1;
23308 +                       break;
23309 +               }
23310 +               if (do_put)
23311 +                       for (bindex = bstart; bindex <= bend; bindex++)
23312 +                               if (au_h_iptr(inode, bindex)) {
23313 +                                       do_put = 0;
23314 +                                       break;
23315 +                               }
23316 +       } else
23317 +               do_put = 1;
23318 +
23319 +       return do_put;
23320 +}
23321 +
23322 +/* free the plinks on a branch specified by @br_id */
23323 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
23324 +{
23325 +       struct au_sbinfo *sbinfo;
23326 +       struct hlist_head *plink_hlist;
23327 +       struct hlist_node *tmp;
23328 +       struct pseudo_link *plink;
23329 +       struct inode *inode;
23330 +       int i, do_put;
23331 +
23332 +       SiMustWriteLock(sb);
23333 +
23334 +       sbinfo = au_sbi(sb);
23335 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
23336 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
23337 +
23338 +       /* no spin_lock since sbinfo is write-locked */
23339 +       for (i = 0; i < AuPlink_NHASH; i++) {
23340 +               plink_hlist = &sbinfo->si_plink[i].head;
23341 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
23342 +                       inode = au_igrab(plink->inode);
23343 +                       ii_write_lock_child(inode);
23344 +                       do_put = au_plink_do_half_refresh(inode, br_id);
23345 +                       if (do_put)
23346 +                               do_put_plink(plink, 1);
23347 +                       ii_write_unlock(inode);
23348 +                       iput(inode);
23349 +               }
23350 +       }
23351 +}
23352 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
23353 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
23354 +++ linux/fs/aufs/poll.c        2014-06-24 00:14:06.786052822 +0200
23355 @@ -0,0 +1,55 @@
23356 +/*
23357 + * Copyright (C) 2005-2014 Junjiro R. Okajima
23358 + *
23359 + * This program, aufs is free software; you can redistribute it and/or modify
23360 + * it under the terms of the GNU General Public License as published by
23361 + * the Free Software Foundation; either version 2 of the License, or
23362 + * (at your option) any later version.
23363 + *
23364 + * This program is distributed in the hope that it will be useful,
23365 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23366 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23367 + * GNU General Public License for more details.
23368 + *
23369 + * You should have received a copy of the GNU General Public License
23370 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23371 + */
23372 +
23373 +/*
23374 + * poll operation
23375 + * There is only one filesystem which implements ->poll operation, currently.
23376 + */
23377 +
23378 +#include "aufs.h"
23379 +
23380 +unsigned int aufs_poll(struct file *file, poll_table *wait)
23381 +{
23382 +       unsigned int mask;
23383 +       int err;
23384 +       struct file *h_file;
23385 +       struct dentry *dentry;
23386 +       struct super_block *sb;
23387 +
23388 +       /* We should pretend an error happened. */
23389 +       mask = POLLERR /* | POLLIN | POLLOUT */;
23390 +       dentry = file->f_dentry;
23391 +       sb = dentry->d_sb;
23392 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
23393 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
23394 +       if (unlikely(err))
23395 +               goto out;
23396 +
23397 +       /* it is not an error if h_file has no operation */
23398 +       mask = DEFAULT_POLLMASK;
23399 +       h_file = au_hf_top(file);
23400 +       if (h_file->f_op->poll)
23401 +               mask = h_file->f_op->poll(h_file, wait);
23402 +
23403 +       di_read_unlock(dentry, AuLock_IR);
23404 +       fi_read_unlock(file);
23405 +
23406 +out:
23407 +       si_read_unlock(sb);
23408 +       AuTraceErr((int)mask);
23409 +       return mask;
23410 +}
23411 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
23412 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
23413 +++ linux/fs/aufs/procfs.c      2014-06-24 00:14:06.786052822 +0200
23414 @@ -0,0 +1,169 @@
23415 +/*
23416 + * Copyright (C) 2010-2014 Junjiro R. Okajima
23417 + *
23418 + * This program, aufs is free software; you can redistribute it and/or modify
23419 + * it under the terms of the GNU General Public License as published by
23420 + * the Free Software Foundation; either version 2 of the License, or
23421 + * (at your option) any later version.
23422 + *
23423 + * This program is distributed in the hope that it will be useful,
23424 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23425 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23426 + * GNU General Public License for more details.
23427 + *
23428 + * You should have received a copy of the GNU General Public License
23429 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23430 + */
23431 +
23432 +/*
23433 + * procfs interfaces
23434 + */
23435 +
23436 +#include <linux/proc_fs.h>
23437 +#include "aufs.h"
23438 +
23439 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
23440 +{
23441 +       struct au_sbinfo *sbinfo;
23442 +
23443 +       sbinfo = file->private_data;
23444 +       if (sbinfo) {
23445 +               au_plink_maint_leave(sbinfo);
23446 +               kobject_put(&sbinfo->si_kobj);
23447 +       }
23448 +
23449 +       return 0;
23450 +}
23451 +
23452 +static void au_procfs_plm_write_clean(struct file *file)
23453 +{
23454 +       struct au_sbinfo *sbinfo;
23455 +
23456 +       sbinfo = file->private_data;
23457 +       if (sbinfo)
23458 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
23459 +}
23460 +
23461 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
23462 +{
23463 +       int err;
23464 +       struct super_block *sb;
23465 +       struct au_sbinfo *sbinfo;
23466 +
23467 +       err = -EBUSY;
23468 +       if (unlikely(file->private_data))
23469 +               goto out;
23470 +
23471 +       sb = NULL;
23472 +       /* don't use au_sbilist_lock() here */
23473 +       spin_lock(&au_sbilist.spin);
23474 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
23475 +               if (id == sysaufs_si_id(sbinfo)) {
23476 +                       kobject_get(&sbinfo->si_kobj);
23477 +                       sb = sbinfo->si_sb;
23478 +                       break;
23479 +               }
23480 +       spin_unlock(&au_sbilist.spin);
23481 +
23482 +       err = -EINVAL;
23483 +       if (unlikely(!sb))
23484 +               goto out;
23485 +
23486 +       err = au_plink_maint_enter(sb);
23487 +       if (!err)
23488 +               /* keep kobject_get() */
23489 +               file->private_data = sbinfo;
23490 +       else
23491 +               kobject_put(&sbinfo->si_kobj);
23492 +out:
23493 +       return err;
23494 +}
23495 +
23496 +/*
23497 + * Accept a valid "si=xxxx" only.
23498 + * Once it is accepted successfully, accept "clean" too.
23499 + */
23500 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
23501 +                                  size_t count, loff_t *ppos)
23502 +{
23503 +       ssize_t err;
23504 +       unsigned long id;
23505 +       /* last newline is allowed */
23506 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
23507 +
23508 +       err = -EACCES;
23509 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
23510 +               goto out;
23511 +
23512 +       err = -EINVAL;
23513 +       if (unlikely(count > sizeof(buf)))
23514 +               goto out;
23515 +
23516 +       err = copy_from_user(buf, ubuf, count);
23517 +       if (unlikely(err)) {
23518 +               err = -EFAULT;
23519 +               goto out;
23520 +       }
23521 +       buf[count] = 0;
23522 +
23523 +       err = -EINVAL;
23524 +       if (!strcmp("clean", buf)) {
23525 +               au_procfs_plm_write_clean(file);
23526 +               goto out_success;
23527 +       } else if (unlikely(strncmp("si=", buf, 3)))
23528 +               goto out;
23529 +
23530 +       err = kstrtoul(buf + 3, 16, &id);
23531 +       if (unlikely(err))
23532 +               goto out;
23533 +
23534 +       err = au_procfs_plm_write_si(file, id);
23535 +       if (unlikely(err))
23536 +               goto out;
23537 +
23538 +out_success:
23539 +       err = count; /* success */
23540 +out:
23541 +       return err;
23542 +}
23543 +
23544 +static const struct file_operations au_procfs_plm_fop = {
23545 +       .write          = au_procfs_plm_write,
23546 +       .release        = au_procfs_plm_release,
23547 +       .owner          = THIS_MODULE
23548 +};
23549 +
23550 +/* ---------------------------------------------------------------------- */
23551 +
23552 +static struct proc_dir_entry *au_procfs_dir;
23553 +
23554 +void au_procfs_fin(void)
23555 +{
23556 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
23557 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
23558 +}
23559 +
23560 +int __init au_procfs_init(void)
23561 +{
23562 +       int err;
23563 +       struct proc_dir_entry *entry;
23564 +
23565 +       err = -ENOMEM;
23566 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
23567 +       if (unlikely(!au_procfs_dir))
23568 +               goto out;
23569 +
23570 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
23571 +                           au_procfs_dir, &au_procfs_plm_fop);
23572 +       if (unlikely(!entry))
23573 +               goto out_dir;
23574 +
23575 +       err = 0;
23576 +       goto out; /* success */
23577 +
23578 +
23579 +out_dir:
23580 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
23581 +out:
23582 +       return err;
23583 +}
23584 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
23585 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
23586 +++ linux/fs/aufs/rdu.c 2014-06-24 00:14:06.786052822 +0200
23587 @@ -0,0 +1,388 @@
23588 +/*
23589 + * Copyright (C) 2005-2014 Junjiro R. Okajima
23590 + *
23591 + * This program, aufs is free software; you can redistribute it and/or modify
23592 + * it under the terms of the GNU General Public License as published by
23593 + * the Free Software Foundation; either version 2 of the License, or
23594 + * (at your option) any later version.
23595 + *
23596 + * This program is distributed in the hope that it will be useful,
23597 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23598 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23599 + * GNU General Public License for more details.
23600 + *
23601 + * You should have received a copy of the GNU General Public License
23602 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23603 + */
23604 +
23605 +/*
23606 + * readdir in userspace.
23607 + */
23608 +
23609 +#include <linux/compat.h>
23610 +#include <linux/fs_stack.h>
23611 +#include <linux/security.h>
23612 +#include "aufs.h"
23613 +
23614 +/* bits for struct aufs_rdu.flags */
23615 +#define        AuRdu_CALLED    1
23616 +#define        AuRdu_CONT      (1 << 1)
23617 +#define        AuRdu_FULL      (1 << 2)
23618 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
23619 +#define au_fset_rdu(flags, name) \
23620 +       do { (flags) |= AuRdu_##name; } while (0)
23621 +#define au_fclr_rdu(flags, name) \
23622 +       do { (flags) &= ~AuRdu_##name; } while (0)
23623 +
23624 +struct au_rdu_arg {
23625 +       struct dir_context              ctx;
23626 +       struct aufs_rdu                 *rdu;
23627 +       union au_rdu_ent_ul             ent;
23628 +       unsigned long                   end;
23629 +
23630 +       struct super_block              *sb;
23631 +       int                             err;
23632 +};
23633 +
23634 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
23635 +                      loff_t offset, u64 h_ino, unsigned int d_type)
23636 +{
23637 +       int err, len;
23638 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
23639 +       struct aufs_rdu *rdu = arg->rdu;
23640 +       struct au_rdu_ent ent;
23641 +
23642 +       err = 0;
23643 +       arg->err = 0;
23644 +       au_fset_rdu(rdu->cookie.flags, CALLED);
23645 +       len = au_rdu_len(nlen);
23646 +       if (arg->ent.ul + len  < arg->end) {
23647 +               ent.ino = h_ino;
23648 +               ent.bindex = rdu->cookie.bindex;
23649 +               ent.type = d_type;
23650 +               ent.nlen = nlen;
23651 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
23652 +                       ent.type = DT_UNKNOWN;
23653 +
23654 +               /* unnecessary to support mmap_sem since this is a dir */
23655 +               err = -EFAULT;
23656 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
23657 +                       goto out;
23658 +               if (copy_to_user(arg->ent.e->name, name, nlen))
23659 +                       goto out;
23660 +               /* the terminating NULL */
23661 +               if (__put_user(0, arg->ent.e->name + nlen))
23662 +                       goto out;
23663 +               err = 0;
23664 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
23665 +               arg->ent.ul += len;
23666 +               rdu->rent++;
23667 +       } else {
23668 +               err = -EFAULT;
23669 +               au_fset_rdu(rdu->cookie.flags, FULL);
23670 +               rdu->full = 1;
23671 +               rdu->tail = arg->ent;
23672 +       }
23673 +
23674 +out:
23675 +       /* AuTraceErr(err); */
23676 +       return err;
23677 +}
23678 +
23679 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
23680 +{
23681 +       int err;
23682 +       loff_t offset;
23683 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
23684 +
23685 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
23686 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
23687 +       err = offset;
23688 +       if (unlikely(offset != cookie->h_pos))
23689 +               goto out;
23690 +
23691 +       err = 0;
23692 +       do {
23693 +               arg->err = 0;
23694 +               au_fclr_rdu(cookie->flags, CALLED);
23695 +               /* smp_mb(); */
23696 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
23697 +               if (err >= 0)
23698 +                       err = arg->err;
23699 +       } while (!err
23700 +                && au_ftest_rdu(cookie->flags, CALLED)
23701 +                && !au_ftest_rdu(cookie->flags, FULL));
23702 +       cookie->h_pos = h_file->f_pos;
23703 +
23704 +out:
23705 +       AuTraceErr(err);
23706 +       return err;
23707 +}
23708 +
23709 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
23710 +{
23711 +       int err;
23712 +       aufs_bindex_t bend;
23713 +       struct au_rdu_arg arg = {
23714 +               .ctx = {
23715 +                       .actor = au_diractor(au_rdu_fill)
23716 +               }
23717 +       };
23718 +       struct dentry *dentry;
23719 +       struct inode *inode;
23720 +       struct file *h_file;
23721 +       struct au_rdu_cookie *cookie = &rdu->cookie;
23722 +
23723 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
23724 +       if (unlikely(err)) {
23725 +               err = -EFAULT;
23726 +               AuTraceErr(err);
23727 +               goto out;
23728 +       }
23729 +       rdu->rent = 0;
23730 +       rdu->tail = rdu->ent;
23731 +       rdu->full = 0;
23732 +       arg.rdu = rdu;
23733 +       arg.ent = rdu->ent;
23734 +       arg.end = arg.ent.ul;
23735 +       arg.end += rdu->sz;
23736 +
23737 +       err = -ENOTDIR;
23738 +       if (unlikely(!file->f_op->iterate))
23739 +               goto out;
23740 +
23741 +       err = security_file_permission(file, MAY_READ);
23742 +       AuTraceErr(err);
23743 +       if (unlikely(err))
23744 +               goto out;
23745 +
23746 +       dentry = file->f_dentry;
23747 +       inode = dentry->d_inode;
23748 +#if 1
23749 +       mutex_lock(&inode->i_mutex);
23750 +#else
23751 +       err = mutex_lock_killable(&inode->i_mutex);
23752 +       AuTraceErr(err);
23753 +       if (unlikely(err))
23754 +               goto out;
23755 +#endif
23756 +
23757 +       arg.sb = inode->i_sb;
23758 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
23759 +       if (unlikely(err))
23760 +               goto out_mtx;
23761 +       err = au_alive_dir(dentry);
23762 +       if (unlikely(err))
23763 +               goto out_si;
23764 +       /* todo: reval? */
23765 +       fi_read_lock(file);
23766 +
23767 +       err = -EAGAIN;
23768 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
23769 +                    && cookie->generation != au_figen(file)))
23770 +               goto out_unlock;
23771 +
23772 +       err = 0;
23773 +       if (!rdu->blk) {
23774 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
23775 +               if (!rdu->blk)
23776 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
23777 +       }
23778 +       bend = au_fbstart(file);
23779 +       if (cookie->bindex < bend)
23780 +               cookie->bindex = bend;
23781 +       bend = au_fbend_dir(file);
23782 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
23783 +       for (; !err && cookie->bindex <= bend;
23784 +            cookie->bindex++, cookie->h_pos = 0) {
23785 +               h_file = au_hf_dir(file, cookie->bindex);
23786 +               if (!h_file)
23787 +                       continue;
23788 +
23789 +               au_fclr_rdu(cookie->flags, FULL);
23790 +               err = au_rdu_do(h_file, &arg);
23791 +               AuTraceErr(err);
23792 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
23793 +                       break;
23794 +       }
23795 +       AuDbg("rent %llu\n", rdu->rent);
23796 +
23797 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
23798 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
23799 +               au_fset_rdu(cookie->flags, CONT);
23800 +               cookie->generation = au_figen(file);
23801 +       }
23802 +
23803 +       ii_read_lock_child(inode);
23804 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
23805 +       ii_read_unlock(inode);
23806 +
23807 +out_unlock:
23808 +       fi_read_unlock(file);
23809 +out_si:
23810 +       si_read_unlock(arg.sb);
23811 +out_mtx:
23812 +       mutex_unlock(&inode->i_mutex);
23813 +out:
23814 +       AuTraceErr(err);
23815 +       return err;
23816 +}
23817 +
23818 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
23819 +{
23820 +       int err;
23821 +       ino_t ino;
23822 +       unsigned long long nent;
23823 +       union au_rdu_ent_ul *u;
23824 +       struct au_rdu_ent ent;
23825 +       struct super_block *sb;
23826 +
23827 +       err = 0;
23828 +       nent = rdu->nent;
23829 +       u = &rdu->ent;
23830 +       sb = file->f_dentry->d_sb;
23831 +       si_read_lock(sb, AuLock_FLUSH);
23832 +       while (nent-- > 0) {
23833 +               /* unnecessary to support mmap_sem since this is a dir */
23834 +               err = copy_from_user(&ent, u->e, sizeof(ent));
23835 +               if (!err)
23836 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
23837 +               if (unlikely(err)) {
23838 +                       err = -EFAULT;
23839 +                       AuTraceErr(err);
23840 +                       break;
23841 +               }
23842 +
23843 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
23844 +               if (!ent.wh)
23845 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
23846 +               else
23847 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
23848 +                                       &ino);
23849 +               if (unlikely(err)) {
23850 +                       AuTraceErr(err);
23851 +                       break;
23852 +               }
23853 +
23854 +               err = __put_user(ino, &u->e->ino);
23855 +               if (unlikely(err)) {
23856 +                       err = -EFAULT;
23857 +                       AuTraceErr(err);
23858 +                       break;
23859 +               }
23860 +               u->ul += au_rdu_len(ent.nlen);
23861 +       }
23862 +       si_read_unlock(sb);
23863 +
23864 +       return err;
23865 +}
23866 +
23867 +/* ---------------------------------------------------------------------- */
23868 +
23869 +static int au_rdu_verify(struct aufs_rdu *rdu)
23870 +{
23871 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
23872 +             "%llu, b%d, 0x%x, g%u}\n",
23873 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
23874 +             rdu->blk,
23875 +             rdu->rent, rdu->shwh, rdu->full,
23876 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
23877 +             rdu->cookie.generation);
23878 +
23879 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
23880 +               return 0;
23881 +
23882 +       AuDbg("%u:%u\n",
23883 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
23884 +       return -EINVAL;
23885 +}
23886 +
23887 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
23888 +{
23889 +       long err, e;
23890 +       struct aufs_rdu rdu;
23891 +       void __user *p = (void __user *)arg;
23892 +
23893 +       err = copy_from_user(&rdu, p, sizeof(rdu));
23894 +       if (unlikely(err)) {
23895 +               err = -EFAULT;
23896 +               AuTraceErr(err);
23897 +               goto out;
23898 +       }
23899 +       err = au_rdu_verify(&rdu);
23900 +       if (unlikely(err))
23901 +               goto out;
23902 +
23903 +       switch (cmd) {
23904 +       case AUFS_CTL_RDU:
23905 +               err = au_rdu(file, &rdu);
23906 +               if (unlikely(err))
23907 +                       break;
23908 +
23909 +               e = copy_to_user(p, &rdu, sizeof(rdu));
23910 +               if (unlikely(e)) {
23911 +                       err = -EFAULT;
23912 +                       AuTraceErr(err);
23913 +               }
23914 +               break;
23915 +       case AUFS_CTL_RDU_INO:
23916 +               err = au_rdu_ino(file, &rdu);
23917 +               break;
23918 +
23919 +       default:
23920 +               /* err = -ENOTTY; */
23921 +               err = -EINVAL;
23922 +       }
23923 +
23924 +out:
23925 +       AuTraceErr(err);
23926 +       return err;
23927 +}
23928 +
23929 +#ifdef CONFIG_COMPAT
23930 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
23931 +{
23932 +       long err, e;
23933 +       struct aufs_rdu rdu;
23934 +       void __user *p = compat_ptr(arg);
23935 +
23936 +       /* todo: get_user()? */
23937 +       err = copy_from_user(&rdu, p, sizeof(rdu));
23938 +       if (unlikely(err)) {
23939 +               err = -EFAULT;
23940 +               AuTraceErr(err);
23941 +               goto out;
23942 +       }
23943 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
23944 +       err = au_rdu_verify(&rdu);
23945 +       if (unlikely(err))
23946 +               goto out;
23947 +
23948 +       switch (cmd) {
23949 +       case AUFS_CTL_RDU:
23950 +               err = au_rdu(file, &rdu);
23951 +               if (unlikely(err))
23952 +                       break;
23953 +
23954 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
23955 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
23956 +               e = copy_to_user(p, &rdu, sizeof(rdu));
23957 +               if (unlikely(e)) {
23958 +                       err = -EFAULT;
23959 +                       AuTraceErr(err);
23960 +               }
23961 +               break;
23962 +       case AUFS_CTL_RDU_INO:
23963 +               err = au_rdu_ino(file, &rdu);
23964 +               break;
23965 +
23966 +       default:
23967 +               /* err = -ENOTTY; */
23968 +               err = -EINVAL;
23969 +       }
23970 +
23971 +out:
23972 +       AuTraceErr(err);
23973 +       return err;
23974 +}
23975 +#endif
23976 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
23977 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
23978 +++ linux/fs/aufs/rwsem.h       2014-06-24 00:14:06.789386337 +0200
23979 @@ -0,0 +1,187 @@
23980 +/*
23981 + * Copyright (C) 2005-2014 Junjiro R. Okajima
23982 + *
23983 + * This program, aufs is free software; you can redistribute it and/or modify
23984 + * it under the terms of the GNU General Public License as published by
23985 + * the Free Software Foundation; either version 2 of the License, or
23986 + * (at your option) any later version.
23987 + *
23988 + * This program is distributed in the hope that it will be useful,
23989 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23990 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23991 + * GNU General Public License for more details.
23992 + *
23993 + * You should have received a copy of the GNU General Public License
23994 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23995 + */
23996 +
23997 +/*
23998 + * simple read-write semaphore wrappers
23999 + */
24000 +
24001 +#ifndef __AUFS_RWSEM_H__
24002 +#define __AUFS_RWSEM_H__
24003 +
24004 +#ifdef __KERNEL__
24005 +
24006 +#include "debug.h"
24007 +
24008 +struct au_rwsem {
24009 +       struct rw_semaphore     rwsem;
24010 +#ifdef CONFIG_AUFS_DEBUG
24011 +       /* just for debugging, not almighty counter */
24012 +       atomic_t                rcnt, wcnt;
24013 +#endif
24014 +};
24015 +
24016 +#ifdef CONFIG_AUFS_DEBUG
24017 +#define AuDbgCntInit(rw) do { \
24018 +       atomic_set(&(rw)->rcnt, 0); \
24019 +       atomic_set(&(rw)->wcnt, 0); \
24020 +       smp_mb(); /* atomic set */ \
24021 +} while (0)
24022 +
24023 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
24024 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
24025 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
24026 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
24027 +#else
24028 +#define AuDbgCntInit(rw)       do {} while (0)
24029 +#define AuDbgRcntInc(rw)       do {} while (0)
24030 +#define AuDbgRcntDec(rw)       do {} while (0)
24031 +#define AuDbgWcntInc(rw)       do {} while (0)
24032 +#define AuDbgWcntDec(rw)       do {} while (0)
24033 +#endif /* CONFIG_AUFS_DEBUG */
24034 +
24035 +/* to debug easier, do not make them inlined functions */
24036 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
24037 +/* rwsem_is_locked() is unusable */
24038 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
24039 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
24040 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
24041 +                                       && atomic_read(&(rw)->wcnt) <= 0)
24042 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
24043 +                                       || atomic_read(&(rw)->wcnt))
24044 +
24045 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
24046 +
24047 +static inline void au_rw_init(struct au_rwsem *rw)
24048 +{
24049 +       AuDbgCntInit(rw);
24050 +       init_rwsem(&rw->rwsem);
24051 +}
24052 +
24053 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
24054 +{
24055 +       au_rw_init(rw);
24056 +       down_write(&rw->rwsem);
24057 +       AuDbgWcntInc(rw);
24058 +}
24059 +
24060 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
24061 +                                          unsigned int lsc)
24062 +{
24063 +       au_rw_init(rw);
24064 +       down_write_nested(&rw->rwsem, lsc);
24065 +       AuDbgWcntInc(rw);
24066 +}
24067 +
24068 +static inline void au_rw_read_lock(struct au_rwsem *rw)
24069 +{
24070 +       down_read(&rw->rwsem);
24071 +       AuDbgRcntInc(rw);
24072 +}
24073 +
24074 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
24075 +{
24076 +       down_read_nested(&rw->rwsem, lsc);
24077 +       AuDbgRcntInc(rw);
24078 +}
24079 +
24080 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
24081 +{
24082 +       AuRwMustReadLock(rw);
24083 +       AuDbgRcntDec(rw);
24084 +       up_read(&rw->rwsem);
24085 +}
24086 +
24087 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
24088 +{
24089 +       AuRwMustWriteLock(rw);
24090 +       AuDbgRcntInc(rw);
24091 +       AuDbgWcntDec(rw);
24092 +       downgrade_write(&rw->rwsem);
24093 +}
24094 +
24095 +static inline void au_rw_write_lock(struct au_rwsem *rw)
24096 +{
24097 +       down_write(&rw->rwsem);
24098 +       AuDbgWcntInc(rw);
24099 +}
24100 +
24101 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
24102 +                                          unsigned int lsc)
24103 +{
24104 +       down_write_nested(&rw->rwsem, lsc);
24105 +       AuDbgWcntInc(rw);
24106 +}
24107 +
24108 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
24109 +{
24110 +       AuRwMustWriteLock(rw);
24111 +       AuDbgWcntDec(rw);
24112 +       up_write(&rw->rwsem);
24113 +}
24114 +
24115 +/* why is not _nested version defined */
24116 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
24117 +{
24118 +       int ret = down_read_trylock(&rw->rwsem);
24119 +       if (ret)
24120 +               AuDbgRcntInc(rw);
24121 +       return ret;
24122 +}
24123 +
24124 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
24125 +{
24126 +       int ret = down_write_trylock(&rw->rwsem);
24127 +       if (ret)
24128 +               AuDbgWcntInc(rw);
24129 +       return ret;
24130 +}
24131 +
24132 +#undef AuDbgCntInit
24133 +#undef AuDbgRcntInc
24134 +#undef AuDbgRcntDec
24135 +#undef AuDbgWcntInc
24136 +#undef AuDbgWcntDec
24137 +
24138 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
24139 +static inline void prefix##_read_lock(param) \
24140 +{ au_rw_read_lock(rwsem); } \
24141 +static inline void prefix##_write_lock(param) \
24142 +{ au_rw_write_lock(rwsem); } \
24143 +static inline int prefix##_read_trylock(param) \
24144 +{ return au_rw_read_trylock(rwsem); } \
24145 +static inline int prefix##_write_trylock(param) \
24146 +{ return au_rw_write_trylock(rwsem); }
24147 +/* why is not _nested version defined */
24148 +/* static inline void prefix##_read_trylock_nested(param, lsc)
24149 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
24150 +static inline void prefix##_write_trylock_nestd(param, lsc)
24151 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
24152 +
24153 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
24154 +static inline void prefix##_read_unlock(param) \
24155 +{ au_rw_read_unlock(rwsem); } \
24156 +static inline void prefix##_write_unlock(param) \
24157 +{ au_rw_write_unlock(rwsem); } \
24158 +static inline void prefix##_downgrade_lock(param) \
24159 +{ au_rw_dgrade_lock(rwsem); }
24160 +
24161 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
24162 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
24163 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
24164 +
24165 +#endif /* __KERNEL__ */
24166 +#endif /* __AUFS_RWSEM_H__ */
24167 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
24168 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
24169 +++ linux/fs/aufs/sbinfo.c      2014-06-24 00:14:06.789386337 +0200
24170 @@ -0,0 +1,351 @@
24171 +/*
24172 + * Copyright (C) 2005-2014 Junjiro R. Okajima
24173 + *
24174 + * This program, aufs is free software; you can redistribute it and/or modify
24175 + * it under the terms of the GNU General Public License as published by
24176 + * the Free Software Foundation; either version 2 of the License, or
24177 + * (at your option) any later version.
24178 + *
24179 + * This program is distributed in the hope that it will be useful,
24180 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24181 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24182 + * GNU General Public License for more details.
24183 + *
24184 + * You should have received a copy of the GNU General Public License
24185 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24186 + */
24187 +
24188 +/*
24189 + * superblock private data
24190 + */
24191 +
24192 +#include "aufs.h"
24193 +
24194 +/*
24195 + * they are necessary regardless sysfs is disabled.
24196 + */
24197 +void au_si_free(struct kobject *kobj)
24198 +{
24199 +       int i;
24200 +       struct au_sbinfo *sbinfo;
24201 +       char *locked __maybe_unused; /* debug only */
24202 +
24203 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
24204 +       for (i = 0; i < AuPlink_NHASH; i++)
24205 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
24206 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
24207 +
24208 +       au_rw_write_lock(&sbinfo->si_rwsem);
24209 +       au_br_free(sbinfo);
24210 +       au_rw_write_unlock(&sbinfo->si_rwsem);
24211 +
24212 +       AuDebugOn(radix_tree_gang_lookup
24213 +                 (&sbinfo->au_si_pid.tree, (void **)&locked,
24214 +                  /*first_index*/PID_MAX_DEFAULT - 1,
24215 +                  /*max_items*/sizeof(locked)/sizeof(*locked)));
24216 +
24217 +       kfree(sbinfo->si_branch);
24218 +       kfree(sbinfo->au_si_pid.bitmap);
24219 +       mutex_destroy(&sbinfo->si_xib_mtx);
24220 +       AuRwDestroy(&sbinfo->si_rwsem);
24221 +
24222 +       kfree(sbinfo);
24223 +}
24224 +
24225 +int au_si_alloc(struct super_block *sb)
24226 +{
24227 +       int err, i;
24228 +       struct au_sbinfo *sbinfo;
24229 +       static struct lock_class_key aufs_si;
24230 +
24231 +       err = -ENOMEM;
24232 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
24233 +       if (unlikely(!sbinfo))
24234 +               goto out;
24235 +
24236 +       BUILD_BUG_ON(sizeof(unsigned long) !=
24237 +                    sizeof(*sbinfo->au_si_pid.bitmap));
24238 +       sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
24239 +                                       sizeof(*sbinfo->au_si_pid.bitmap),
24240 +                                       GFP_NOFS);
24241 +       if (unlikely(!sbinfo->au_si_pid.bitmap))
24242 +               goto out_sbinfo;
24243 +
24244 +       /* will be reallocated separately */
24245 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
24246 +       if (unlikely(!sbinfo->si_branch))
24247 +               goto out_pidmap;
24248 +
24249 +       err = sysaufs_si_init(sbinfo);
24250 +       if (unlikely(err))
24251 +               goto out_br;
24252 +
24253 +       au_nwt_init(&sbinfo->si_nowait);
24254 +       au_rw_init_wlock(&sbinfo->si_rwsem);
24255 +       au_rw_class(&sbinfo->si_rwsem, &aufs_si);
24256 +       spin_lock_init(&sbinfo->au_si_pid.tree_lock);
24257 +       INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
24258 +
24259 +       atomic_long_set(&sbinfo->si_ninodes, 0);
24260 +       atomic_long_set(&sbinfo->si_nfiles, 0);
24261 +
24262 +       sbinfo->si_bend = -1;
24263 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
24264 +
24265 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
24266 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
24267 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
24268 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
24269 +
24270 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
24271 +
24272 +       sbinfo->si_xino_jiffy = jiffies;
24273 +       sbinfo->si_xino_expire
24274 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
24275 +       mutex_init(&sbinfo->si_xib_mtx);
24276 +       sbinfo->si_xino_brid = -1;
24277 +       /* leave si_xib_last_pindex and si_xib_next_bit */
24278 +
24279 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
24280 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
24281 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
24282 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
24283 +
24284 +       for (i = 0; i < AuPlink_NHASH; i++)
24285 +               au_sphl_init(sbinfo->si_plink + i);
24286 +       init_waitqueue_head(&sbinfo->si_plink_wq);
24287 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
24288 +
24289 +       au_sphl_init(&sbinfo->si_files);
24290 +
24291 +       /* leave other members for sysaufs and si_mnt. */
24292 +       sbinfo->si_sb = sb;
24293 +       sb->s_fs_info = sbinfo;
24294 +       si_pid_set(sb);
24295 +       au_debug_sbinfo_init(sbinfo);
24296 +       return 0; /* success */
24297 +
24298 +out_br:
24299 +       kfree(sbinfo->si_branch);
24300 +out_pidmap:
24301 +       kfree(sbinfo->au_si_pid.bitmap);
24302 +out_sbinfo:
24303 +       kfree(sbinfo);
24304 +out:
24305 +       return err;
24306 +}
24307 +
24308 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
24309 +{
24310 +       int err, sz;
24311 +       struct au_branch **brp;
24312 +
24313 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
24314 +
24315 +       err = -ENOMEM;
24316 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
24317 +       if (unlikely(!sz))
24318 +               sz = sizeof(*brp);
24319 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
24320 +       if (brp) {
24321 +               sbinfo->si_branch = brp;
24322 +               err = 0;
24323 +       }
24324 +
24325 +       return err;
24326 +}
24327 +
24328 +/* ---------------------------------------------------------------------- */
24329 +
24330 +unsigned int au_sigen_inc(struct super_block *sb)
24331 +{
24332 +       unsigned int gen;
24333 +
24334 +       SiMustWriteLock(sb);
24335 +
24336 +       gen = ++au_sbi(sb)->si_generation;
24337 +       au_update_digen(sb->s_root);
24338 +       au_update_iigen(sb->s_root->d_inode, /*half*/0);
24339 +       sb->s_root->d_inode->i_version++;
24340 +       return gen;
24341 +}
24342 +
24343 +aufs_bindex_t au_new_br_id(struct super_block *sb)
24344 +{
24345 +       aufs_bindex_t br_id;
24346 +       int i;
24347 +       struct au_sbinfo *sbinfo;
24348 +
24349 +       SiMustWriteLock(sb);
24350 +
24351 +       sbinfo = au_sbi(sb);
24352 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
24353 +               br_id = ++sbinfo->si_last_br_id;
24354 +               AuDebugOn(br_id < 0);
24355 +               if (br_id && au_br_index(sb, br_id) < 0)
24356 +                       return br_id;
24357 +       }
24358 +
24359 +       return -1;
24360 +}
24361 +
24362 +/* ---------------------------------------------------------------------- */
24363 +
24364 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
24365 +int si_read_lock(struct super_block *sb, int flags)
24366 +{
24367 +       int err;
24368 +
24369 +       err = 0;
24370 +       if (au_ftest_lock(flags, FLUSH))
24371 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
24372 +
24373 +       si_noflush_read_lock(sb);
24374 +       err = au_plink_maint(sb, flags);
24375 +       if (unlikely(err))
24376 +               si_read_unlock(sb);
24377 +
24378 +       return err;
24379 +}
24380 +
24381 +int si_write_lock(struct super_block *sb, int flags)
24382 +{
24383 +       int err;
24384 +
24385 +       if (au_ftest_lock(flags, FLUSH))
24386 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
24387 +
24388 +       si_noflush_write_lock(sb);
24389 +       err = au_plink_maint(sb, flags);
24390 +       if (unlikely(err))
24391 +               si_write_unlock(sb);
24392 +
24393 +       return err;
24394 +}
24395 +
24396 +/* dentry and super_block lock. call at entry point */
24397 +int aufs_read_lock(struct dentry *dentry, int flags)
24398 +{
24399 +       int err;
24400 +       struct super_block *sb;
24401 +
24402 +       sb = dentry->d_sb;
24403 +       err = si_read_lock(sb, flags);
24404 +       if (unlikely(err))
24405 +               goto out;
24406 +
24407 +       if (au_ftest_lock(flags, DW))
24408 +               di_write_lock_child(dentry);
24409 +       else
24410 +               di_read_lock_child(dentry, flags);
24411 +
24412 +       if (au_ftest_lock(flags, GEN)) {
24413 +               err = au_digen_test(dentry, au_sigen(sb));
24414 +               AuDebugOn(!err && au_dbrange_test(dentry));
24415 +               if (unlikely(err))
24416 +                       aufs_read_unlock(dentry, flags);
24417 +       }
24418 +
24419 +out:
24420 +       return err;
24421 +}
24422 +
24423 +void aufs_read_unlock(struct dentry *dentry, int flags)
24424 +{
24425 +       if (au_ftest_lock(flags, DW))
24426 +               di_write_unlock(dentry);
24427 +       else
24428 +               di_read_unlock(dentry, flags);
24429 +       si_read_unlock(dentry->d_sb);
24430 +}
24431 +
24432 +void aufs_write_lock(struct dentry *dentry)
24433 +{
24434 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
24435 +       di_write_lock_child(dentry);
24436 +}
24437 +
24438 +void aufs_write_unlock(struct dentry *dentry)
24439 +{
24440 +       di_write_unlock(dentry);
24441 +       si_write_unlock(dentry->d_sb);
24442 +}
24443 +
24444 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
24445 +{
24446 +       int err;
24447 +       unsigned int sigen;
24448 +       struct super_block *sb;
24449 +
24450 +       sb = d1->d_sb;
24451 +       err = si_read_lock(sb, flags);
24452 +       if (unlikely(err))
24453 +               goto out;
24454 +
24455 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
24456 +
24457 +       if (au_ftest_lock(flags, GEN)) {
24458 +               sigen = au_sigen(sb);
24459 +               err = au_digen_test(d1, sigen);
24460 +               AuDebugOn(!err && au_dbrange_test(d1));
24461 +               if (!err) {
24462 +                       err = au_digen_test(d2, sigen);
24463 +                       AuDebugOn(!err && au_dbrange_test(d2));
24464 +               }
24465 +               if (unlikely(err))
24466 +                       aufs_read_and_write_unlock2(d1, d2);
24467 +       }
24468 +
24469 +out:
24470 +       return err;
24471 +}
24472 +
24473 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
24474 +{
24475 +       di_write_unlock2(d1, d2);
24476 +       si_read_unlock(d1->d_sb);
24477 +}
24478 +
24479 +/* ---------------------------------------------------------------------- */
24480 +
24481 +int si_pid_test_slow(struct super_block *sb)
24482 +{
24483 +       void *p;
24484 +
24485 +       rcu_read_lock();
24486 +       p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
24487 +       rcu_read_unlock();
24488 +
24489 +       return (long)!!p;
24490 +}
24491 +
24492 +void si_pid_set_slow(struct super_block *sb)
24493 +{
24494 +       int err;
24495 +       struct au_sbinfo *sbinfo;
24496 +
24497 +       AuDebugOn(si_pid_test_slow(sb));
24498 +
24499 +       sbinfo = au_sbi(sb);
24500 +       err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
24501 +       AuDebugOn(err);
24502 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
24503 +       err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
24504 +                               /*any valid ptr*/sb);
24505 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
24506 +       AuDebugOn(err);
24507 +       radix_tree_preload_end();
24508 +}
24509 +
24510 +void si_pid_clr_slow(struct super_block *sb)
24511 +{
24512 +       void *p;
24513 +       struct au_sbinfo *sbinfo;
24514 +
24515 +       AuDebugOn(!si_pid_test_slow(sb));
24516 +
24517 +       sbinfo = au_sbi(sb);
24518 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
24519 +       p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
24520 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
24521 +}
24522 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
24523 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
24524 +++ linux/fs/aufs/spl.h 2014-06-24 00:14:06.789386337 +0200
24525 @@ -0,0 +1,111 @@
24526 +/*
24527 + * Copyright (C) 2005-2014 Junjiro R. Okajima
24528 + *
24529 + * This program, aufs is free software; you can redistribute it and/or modify
24530 + * it under the terms of the GNU General Public License as published by
24531 + * the Free Software Foundation; either version 2 of the License, or
24532 + * (at your option) any later version.
24533 + *
24534 + * This program is distributed in the hope that it will be useful,
24535 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24536 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24537 + * GNU General Public License for more details.
24538 + *
24539 + * You should have received a copy of the GNU General Public License
24540 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24541 + */
24542 +
24543 +/*
24544 + * simple list protected by a spinlock
24545 + */
24546 +
24547 +#ifndef __AUFS_SPL_H__
24548 +#define __AUFS_SPL_H__
24549 +
24550 +#ifdef __KERNEL__
24551 +
24552 +struct au_splhead {
24553 +       spinlock_t              spin;
24554 +       struct list_head        head;
24555 +};
24556 +
24557 +static inline void au_spl_init(struct au_splhead *spl)
24558 +{
24559 +       spin_lock_init(&spl->spin);
24560 +       INIT_LIST_HEAD(&spl->head);
24561 +}
24562 +
24563 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
24564 +{
24565 +       spin_lock(&spl->spin);
24566 +       list_add(list, &spl->head);
24567 +       spin_unlock(&spl->spin);
24568 +}
24569 +
24570 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
24571 +{
24572 +       spin_lock(&spl->spin);
24573 +       list_del(list);
24574 +       spin_unlock(&spl->spin);
24575 +}
24576 +
24577 +static inline void au_spl_del_rcu(struct list_head *list,
24578 +                                 struct au_splhead *spl)
24579 +{
24580 +       spin_lock(&spl->spin);
24581 +       list_del_rcu(list);
24582 +       spin_unlock(&spl->spin);
24583 +}
24584 +
24585 +/* ---------------------------------------------------------------------- */
24586 +
24587 +struct au_sphlhead {
24588 +       spinlock_t              spin;
24589 +       struct hlist_head       head;
24590 +};
24591 +
24592 +static inline void au_sphl_init(struct au_sphlhead *sphl)
24593 +{
24594 +       spin_lock_init(&sphl->spin);
24595 +       INIT_HLIST_HEAD(&sphl->head);
24596 +}
24597 +
24598 +static inline void au_sphl_add(struct hlist_node *hlist,
24599 +                              struct au_sphlhead *sphl)
24600 +{
24601 +       spin_lock(&sphl->spin);
24602 +       hlist_add_head(hlist, &sphl->head);
24603 +       spin_unlock(&sphl->spin);
24604 +}
24605 +
24606 +static inline void au_sphl_del(struct hlist_node *hlist,
24607 +                              struct au_sphlhead *sphl)
24608 +{
24609 +       spin_lock(&sphl->spin);
24610 +       hlist_del(hlist);
24611 +       spin_unlock(&sphl->spin);
24612 +}
24613 +
24614 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
24615 +                                  struct au_sphlhead *sphl)
24616 +{
24617 +       spin_lock(&sphl->spin);
24618 +       hlist_del_rcu(hlist);
24619 +       spin_unlock(&sphl->spin);
24620 +}
24621 +
24622 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
24623 +{
24624 +       unsigned long cnt;
24625 +       struct hlist_node *pos;
24626 +
24627 +       cnt = 0;
24628 +       spin_lock(&sphl->spin);
24629 +       hlist_for_each(pos, &sphl->head)
24630 +               cnt++;
24631 +       spin_unlock(&sphl->spin);
24632 +       return cnt;
24633 +}
24634 +
24635 +#endif /* __KERNEL__ */
24636 +#endif /* __AUFS_SPL_H__ */
24637 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
24638 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
24639 +++ linux/fs/aufs/super.c       2014-06-24 00:14:06.789386337 +0200
24640 @@ -0,0 +1,1001 @@
24641 +/*
24642 + * Copyright (C) 2005-2014 Junjiro R. Okajima
24643 + *
24644 + * This program, aufs is free software; you can redistribute it and/or modify
24645 + * it under the terms of the GNU General Public License as published by
24646 + * the Free Software Foundation; either version 2 of the License, or
24647 + * (at your option) any later version.
24648 + *
24649 + * This program is distributed in the hope that it will be useful,
24650 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24651 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24652 + * GNU General Public License for more details.
24653 + *
24654 + * You should have received a copy of the GNU General Public License
24655 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24656 + */
24657 +
24658 +/*
24659 + * mount and super_block operations
24660 + */
24661 +
24662 +#include <linux/mm.h>
24663 +#include <linux/module.h>
24664 +#include <linux/seq_file.h>
24665 +#include <linux/statfs.h>
24666 +#include <linux/vmalloc.h>
24667 +#include <linux/writeback.h>
24668 +#include "aufs.h"
24669 +
24670 +/*
24671 + * super_operations
24672 + */
24673 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
24674 +{
24675 +       struct au_icntnr *c;
24676 +
24677 +       c = au_cache_alloc_icntnr();
24678 +       if (c) {
24679 +               au_icntnr_init(c);
24680 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
24681 +               c->iinfo.ii_hinode = NULL;
24682 +               return &c->vfs_inode;
24683 +       }
24684 +       return NULL;
24685 +}
24686 +
24687 +static void aufs_destroy_inode_cb(struct rcu_head *head)
24688 +{
24689 +       struct inode *inode = container_of(head, struct inode, i_rcu);
24690 +
24691 +       INIT_HLIST_HEAD(&inode->i_dentry);
24692 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
24693 +}
24694 +
24695 +static void aufs_destroy_inode(struct inode *inode)
24696 +{
24697 +       au_iinfo_fin(inode);
24698 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
24699 +}
24700 +
24701 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
24702 +{
24703 +       struct inode *inode;
24704 +       int err;
24705 +
24706 +       inode = iget_locked(sb, ino);
24707 +       if (unlikely(!inode)) {
24708 +               inode = ERR_PTR(-ENOMEM);
24709 +               goto out;
24710 +       }
24711 +       if (!(inode->i_state & I_NEW))
24712 +               goto out;
24713 +
24714 +       err = au_xigen_new(inode);
24715 +       if (!err)
24716 +               err = au_iinfo_init(inode);
24717 +       if (!err)
24718 +               inode->i_version++;
24719 +       else {
24720 +               iget_failed(inode);
24721 +               inode = ERR_PTR(err);
24722 +       }
24723 +
24724 +out:
24725 +       /* never return NULL */
24726 +       AuDebugOn(!inode);
24727 +       AuTraceErrPtr(inode);
24728 +       return inode;
24729 +}
24730 +
24731 +/* lock free root dinfo */
24732 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
24733 +{
24734 +       int err;
24735 +       aufs_bindex_t bindex, bend;
24736 +       struct path path;
24737 +       struct au_hdentry *hdp;
24738 +       struct au_branch *br;
24739 +       char *perm;
24740 +
24741 +       err = 0;
24742 +       bend = au_sbend(sb);
24743 +       hdp = au_di(sb->s_root)->di_hdentry;
24744 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
24745 +               br = au_sbr(sb, bindex);
24746 +               path.mnt = au_br_mnt(br);
24747 +               path.dentry = hdp[bindex].hd_dentry;
24748 +               err = au_seq_path(seq, &path);
24749 +               if (err > 0) {
24750 +                       perm = au_optstr_br_perm(br->br_perm);
24751 +                       if (perm) {
24752 +                               err = seq_printf(seq, "=%s", perm);
24753 +                               kfree(perm);
24754 +                               if (err == -1)
24755 +                                       err = -E2BIG;
24756 +                       } else
24757 +                               err = -ENOMEM;
24758 +               }
24759 +               if (!err && bindex != bend)
24760 +                       err = seq_putc(seq, ':');
24761 +       }
24762 +
24763 +       return err;
24764 +}
24765 +
24766 +static void au_show_wbr_create(struct seq_file *m, int v,
24767 +                              struct au_sbinfo *sbinfo)
24768 +{
24769 +       const char *pat;
24770 +
24771 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24772 +
24773 +       seq_puts(m, ",create=");
24774 +       pat = au_optstr_wbr_create(v);
24775 +       switch (v) {
24776 +       case AuWbrCreate_TDP:
24777 +       case AuWbrCreate_RR:
24778 +       case AuWbrCreate_MFS:
24779 +       case AuWbrCreate_PMFS:
24780 +               seq_puts(m, pat);
24781 +               break;
24782 +       case AuWbrCreate_MFSV:
24783 +               seq_printf(m, /*pat*/"mfs:%lu",
24784 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24785 +                          / MSEC_PER_SEC);
24786 +               break;
24787 +       case AuWbrCreate_PMFSV:
24788 +               seq_printf(m, /*pat*/"pmfs:%lu",
24789 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24790 +                          / MSEC_PER_SEC);
24791 +               break;
24792 +       case AuWbrCreate_MFSRR:
24793 +               seq_printf(m, /*pat*/"mfsrr:%llu",
24794 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
24795 +               break;
24796 +       case AuWbrCreate_MFSRRV:
24797 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
24798 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
24799 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24800 +                          / MSEC_PER_SEC);
24801 +               break;
24802 +       case AuWbrCreate_PMFSRR:
24803 +               seq_printf(m, /*pat*/"pmfsrr:%llu",
24804 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
24805 +               break;
24806 +       case AuWbrCreate_PMFSRRV:
24807 +               seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
24808 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
24809 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
24810 +                          / MSEC_PER_SEC);
24811 +               break;
24812 +       }
24813 +}
24814 +
24815 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
24816 +{
24817 +#ifdef CONFIG_SYSFS
24818 +       return 0;
24819 +#else
24820 +       int err;
24821 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
24822 +       aufs_bindex_t bindex, brid;
24823 +       struct qstr *name;
24824 +       struct file *f;
24825 +       struct dentry *d, *h_root;
24826 +       struct au_hdentry *hdp;
24827 +
24828 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24829 +
24830 +       err = 0;
24831 +       f = au_sbi(sb)->si_xib;
24832 +       if (!f)
24833 +               goto out;
24834 +
24835 +       /* stop printing the default xino path on the first writable branch */
24836 +       h_root = NULL;
24837 +       brid = au_xino_brid(sb);
24838 +       if (brid >= 0) {
24839 +               bindex = au_br_index(sb, brid);
24840 +               hdp = au_di(sb->s_root)->di_hdentry;
24841 +               h_root = hdp[0 + bindex].hd_dentry;
24842 +       }
24843 +       d = f->f_dentry;
24844 +       name = &d->d_name;
24845 +       /* safe ->d_parent because the file is unlinked */
24846 +       if (d->d_parent == h_root
24847 +           && name->len == len
24848 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
24849 +               goto out;
24850 +
24851 +       seq_puts(seq, ",xino=");
24852 +       err = au_xino_path(seq, f);
24853 +
24854 +out:
24855 +       return err;
24856 +#endif
24857 +}
24858 +
24859 +/* seq_file will re-call me in case of too long string */
24860 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
24861 +{
24862 +       int err;
24863 +       unsigned int mnt_flags, v;
24864 +       struct super_block *sb;
24865 +       struct au_sbinfo *sbinfo;
24866 +
24867 +#define AuBool(name, str) do { \
24868 +       v = au_opt_test(mnt_flags, name); \
24869 +       if (v != au_opt_test(AuOpt_Def, name)) \
24870 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
24871 +} while (0)
24872 +
24873 +#define AuStr(name, str) do { \
24874 +       v = mnt_flags & AuOptMask_##name; \
24875 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
24876 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
24877 +} while (0)
24878 +
24879 +#define AuUInt(name, str, val) do { \
24880 +       if (val != AUFS_##name##_DEF) \
24881 +               seq_printf(m, "," #str "=%u", val); \
24882 +} while (0)
24883 +
24884 +       /* lock free root dinfo */
24885 +       sb = dentry->d_sb;
24886 +       si_noflush_read_lock(sb);
24887 +       sbinfo = au_sbi(sb);
24888 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
24889 +
24890 +       mnt_flags = au_mntflags(sb);
24891 +       if (au_opt_test(mnt_flags, XINO)) {
24892 +               err = au_show_xino(m, sb);
24893 +               if (unlikely(err))
24894 +                       goto out;
24895 +       } else
24896 +               seq_puts(m, ",noxino");
24897 +
24898 +       AuBool(TRUNC_XINO, trunc_xino);
24899 +       AuStr(UDBA, udba);
24900 +       AuBool(SHWH, shwh);
24901 +       AuBool(PLINK, plink);
24902 +       AuBool(DIO, dio);
24903 +       /* AuBool(DIRPERM1, dirperm1); */
24904 +       /* AuBool(REFROF, refrof); */
24905 +
24906 +       v = sbinfo->si_wbr_create;
24907 +       if (v != AuWbrCreate_Def)
24908 +               au_show_wbr_create(m, v, sbinfo);
24909 +
24910 +       v = sbinfo->si_wbr_copyup;
24911 +       if (v != AuWbrCopyup_Def)
24912 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
24913 +
24914 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
24915 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
24916 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
24917 +
24918 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
24919 +
24920 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
24921 +       AuUInt(RDCACHE, rdcache, v);
24922 +
24923 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
24924 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
24925 +
24926 +       AuBool(SUM, sum);
24927 +       /* AuBool(SUM_W, wsum); */
24928 +       AuBool(WARN_PERM, warn_perm);
24929 +       AuBool(VERBOSE, verbose);
24930 +
24931 +out:
24932 +       /* be sure to print "br:" last */
24933 +       if (!sysaufs_brs) {
24934 +               seq_puts(m, ",br:");
24935 +               au_show_brs(m, sb);
24936 +       }
24937 +       si_read_unlock(sb);
24938 +       return 0;
24939 +
24940 +#undef AuBool
24941 +#undef AuStr
24942 +#undef AuUInt
24943 +}
24944 +
24945 +/* ---------------------------------------------------------------------- */
24946 +
24947 +/* sum mode which returns the summation for statfs(2) */
24948 +
24949 +static u64 au_add_till_max(u64 a, u64 b)
24950 +{
24951 +       u64 old;
24952 +
24953 +       old = a;
24954 +       a += b;
24955 +       if (old <= a)
24956 +               return a;
24957 +       return ULLONG_MAX;
24958 +}
24959 +
24960 +static u64 au_mul_till_max(u64 a, long mul)
24961 +{
24962 +       u64 old;
24963 +
24964 +       old = a;
24965 +       a *= mul;
24966 +       if (old <= a)
24967 +               return a;
24968 +       return ULLONG_MAX;
24969 +}
24970 +
24971 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
24972 +{
24973 +       int err;
24974 +       long bsize, factor;
24975 +       u64 blocks, bfree, bavail, files, ffree;
24976 +       aufs_bindex_t bend, bindex, i;
24977 +       unsigned char shared;
24978 +       struct path h_path;
24979 +       struct super_block *h_sb;
24980 +
24981 +       err = 0;
24982 +       bsize = LONG_MAX;
24983 +       files = 0;
24984 +       ffree = 0;
24985 +       blocks = 0;
24986 +       bfree = 0;
24987 +       bavail = 0;
24988 +       bend = au_sbend(sb);
24989 +       for (bindex = 0; bindex <= bend; bindex++) {
24990 +               h_path.mnt = au_sbr_mnt(sb, bindex);
24991 +               h_sb = h_path.mnt->mnt_sb;
24992 +               shared = 0;
24993 +               for (i = 0; !shared && i < bindex; i++)
24994 +                       shared = (au_sbr_sb(sb, i) == h_sb);
24995 +               if (shared)
24996 +                       continue;
24997 +
24998 +               /* sb->s_root for NFS is unreliable */
24999 +               h_path.dentry = h_path.mnt->mnt_root;
25000 +               err = vfs_statfs(&h_path, buf);
25001 +               if (unlikely(err))
25002 +                       goto out;
25003 +
25004 +               if (bsize > buf->f_bsize) {
25005 +                       /*
25006 +                        * we will reduce bsize, so we have to expand blocks
25007 +                        * etc. to match them again
25008 +                        */
25009 +                       factor = (bsize / buf->f_bsize);
25010 +                       blocks = au_mul_till_max(blocks, factor);
25011 +                       bfree = au_mul_till_max(bfree, factor);
25012 +                       bavail = au_mul_till_max(bavail, factor);
25013 +                       bsize = buf->f_bsize;
25014 +               }
25015 +
25016 +               factor = (buf->f_bsize / bsize);
25017 +               blocks = au_add_till_max(blocks,
25018 +                               au_mul_till_max(buf->f_blocks, factor));
25019 +               bfree = au_add_till_max(bfree,
25020 +                               au_mul_till_max(buf->f_bfree, factor));
25021 +               bavail = au_add_till_max(bavail,
25022 +                               au_mul_till_max(buf->f_bavail, factor));
25023 +               files = au_add_till_max(files, buf->f_files);
25024 +               ffree = au_add_till_max(ffree, buf->f_ffree);
25025 +       }
25026 +
25027 +       buf->f_bsize = bsize;
25028 +       buf->f_blocks = blocks;
25029 +       buf->f_bfree = bfree;
25030 +       buf->f_bavail = bavail;
25031 +       buf->f_files = files;
25032 +       buf->f_ffree = ffree;
25033 +       buf->f_frsize = 0;
25034 +
25035 +out:
25036 +       return err;
25037 +}
25038 +
25039 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
25040 +{
25041 +       int err;
25042 +       struct path h_path;
25043 +       struct super_block *sb;
25044 +
25045 +       /* lock free root dinfo */
25046 +       sb = dentry->d_sb;
25047 +       si_noflush_read_lock(sb);
25048 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
25049 +               /* sb->s_root for NFS is unreliable */
25050 +               h_path.mnt = au_sbr_mnt(sb, 0);
25051 +               h_path.dentry = h_path.mnt->mnt_root;
25052 +               err = vfs_statfs(&h_path, buf);
25053 +       } else
25054 +               err = au_statfs_sum(sb, buf);
25055 +       si_read_unlock(sb);
25056 +
25057 +       if (!err) {
25058 +               buf->f_type = AUFS_SUPER_MAGIC;
25059 +               buf->f_namelen = AUFS_MAX_NAMELEN;
25060 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
25061 +       }
25062 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
25063 +
25064 +       return err;
25065 +}
25066 +
25067 +/* ---------------------------------------------------------------------- */
25068 +
25069 +static int aufs_sync_fs(struct super_block *sb, int wait)
25070 +{
25071 +       int err, e;
25072 +       aufs_bindex_t bend, bindex;
25073 +       struct au_branch *br;
25074 +       struct super_block *h_sb;
25075 +
25076 +       err = 0;
25077 +       si_noflush_read_lock(sb);
25078 +       bend = au_sbend(sb);
25079 +       for (bindex = 0; bindex <= bend; bindex++) {
25080 +               br = au_sbr(sb, bindex);
25081 +               if (!au_br_writable(br->br_perm))
25082 +                       continue;
25083 +
25084 +               h_sb = au_sbr_sb(sb, bindex);
25085 +               if (h_sb->s_op->sync_fs) {
25086 +                       e = h_sb->s_op->sync_fs(h_sb, wait);
25087 +                       if (unlikely(e && !err))
25088 +                               err = e;
25089 +                       /* go on even if an error happens */
25090 +               }
25091 +       }
25092 +       si_read_unlock(sb);
25093 +
25094 +       return err;
25095 +}
25096 +
25097 +/* ---------------------------------------------------------------------- */
25098 +
25099 +/* final actions when unmounting a file system */
25100 +static void aufs_put_super(struct super_block *sb)
25101 +{
25102 +       struct au_sbinfo *sbinfo;
25103 +
25104 +       sbinfo = au_sbi(sb);
25105 +       if (!sbinfo)
25106 +               return;
25107 +
25108 +       dbgaufs_si_fin(sbinfo);
25109 +       kobject_put(&sbinfo->si_kobj);
25110 +}
25111 +
25112 +/* ---------------------------------------------------------------------- */
25113 +
25114 +void au_array_free(void *array)
25115 +{
25116 +       if (array) {
25117 +               if (!is_vmalloc_addr(array))
25118 +                       kfree(array);
25119 +               else
25120 +                       vfree(array);
25121 +       }
25122 +}
25123 +
25124 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
25125 +{
25126 +       void *array;
25127 +       unsigned long long n;
25128 +
25129 +       array = NULL;
25130 +       n = 0;
25131 +       if (!*hint)
25132 +               goto out;
25133 +
25134 +       if (*hint > ULLONG_MAX / sizeof(array)) {
25135 +               array = ERR_PTR(-EMFILE);
25136 +               pr_err("hint %llu\n", *hint);
25137 +               goto out;
25138 +       }
25139 +
25140 +       array = kmalloc(sizeof(array) * *hint, GFP_NOFS);
25141 +       if (unlikely(!array))
25142 +               array = vmalloc(sizeof(array) * *hint);
25143 +       if (unlikely(!array)) {
25144 +               array = ERR_PTR(-ENOMEM);
25145 +               goto out;
25146 +       }
25147 +
25148 +       n = cb(array, *hint, arg);
25149 +       AuDebugOn(n > *hint);
25150 +
25151 +out:
25152 +       *hint = n;
25153 +       return array;
25154 +}
25155 +
25156 +static unsigned long long au_iarray_cb(void *a,
25157 +                                      unsigned long long max __maybe_unused,
25158 +                                      void *arg)
25159 +{
25160 +       unsigned long long n;
25161 +       struct inode **p, *inode;
25162 +       struct list_head *head;
25163 +
25164 +       n = 0;
25165 +       p = a;
25166 +       head = arg;
25167 +       spin_lock(&inode_sb_list_lock);
25168 +       list_for_each_entry(inode, head, i_sb_list) {
25169 +               if (!is_bad_inode(inode)
25170 +                   && au_ii(inode)->ii_bstart >= 0) {
25171 +                       spin_lock(&inode->i_lock);
25172 +                       if (atomic_read(&inode->i_count)) {
25173 +                               au_igrab(inode);
25174 +                               *p++ = inode;
25175 +                               n++;
25176 +                               AuDebugOn(n > max);
25177 +                       }
25178 +                       spin_unlock(&inode->i_lock);
25179 +               }
25180 +       }
25181 +       spin_unlock(&inode_sb_list_lock);
25182 +
25183 +       return n;
25184 +}
25185 +
25186 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
25187 +{
25188 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
25189 +       return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
25190 +}
25191 +
25192 +void au_iarray_free(struct inode **a, unsigned long long max)
25193 +{
25194 +       unsigned long long ull;
25195 +
25196 +       for (ull = 0; ull < max; ull++)
25197 +               iput(a[ull]);
25198 +       au_array_free(a);
25199 +}
25200 +
25201 +/* ---------------------------------------------------------------------- */
25202 +
25203 +/*
25204 + * refresh dentry and inode at remount time.
25205 + */
25206 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
25207 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
25208 +                     struct dentry *parent)
25209 +{
25210 +       int err;
25211 +
25212 +       di_write_lock_child(dentry);
25213 +       di_read_lock_parent(parent, AuLock_IR);
25214 +       err = au_refresh_dentry(dentry, parent);
25215 +       if (!err && dir_flags)
25216 +               au_hn_reset(dentry->d_inode, dir_flags);
25217 +       di_read_unlock(parent, AuLock_IR);
25218 +       di_write_unlock(dentry);
25219 +
25220 +       return err;
25221 +}
25222 +
25223 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
25224 +                          struct au_sbinfo *sbinfo,
25225 +                          const unsigned int dir_flags)
25226 +{
25227 +       int err;
25228 +       struct dentry *parent;
25229 +       struct inode *inode;
25230 +
25231 +       err = 0;
25232 +       parent = dget_parent(dentry);
25233 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
25234 +               inode = dentry->d_inode;
25235 +               if (inode) {
25236 +                       if (!S_ISDIR(inode->i_mode))
25237 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
25238 +                                                parent);
25239 +                       else {
25240 +                               err = au_do_refresh(dentry, dir_flags, parent);
25241 +                               if (unlikely(err))
25242 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
25243 +                       }
25244 +               } else
25245 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
25246 +               AuDbgDentry(dentry);
25247 +       }
25248 +       dput(parent);
25249 +
25250 +       AuTraceErr(err);
25251 +       return err;
25252 +}
25253 +
25254 +static int au_refresh_d(struct super_block *sb)
25255 +{
25256 +       int err, i, j, ndentry, e;
25257 +       unsigned int sigen;
25258 +       struct au_dcsub_pages dpages;
25259 +       struct au_dpage *dpage;
25260 +       struct dentry **dentries, *d;
25261 +       struct au_sbinfo *sbinfo;
25262 +       struct dentry *root = sb->s_root;
25263 +       const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
25264 +
25265 +       err = au_dpages_init(&dpages, GFP_NOFS);
25266 +       if (unlikely(err))
25267 +               goto out;
25268 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
25269 +       if (unlikely(err))
25270 +               goto out_dpages;
25271 +
25272 +       sigen = au_sigen(sb);
25273 +       sbinfo = au_sbi(sb);
25274 +       for (i = 0; i < dpages.ndpage; i++) {
25275 +               dpage = dpages.dpages + i;
25276 +               dentries = dpage->dentries;
25277 +               ndentry = dpage->ndentry;
25278 +               for (j = 0; j < ndentry; j++) {
25279 +                       d = dentries[j];
25280 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
25281 +                       if (unlikely(e && !err))
25282 +                               err = e;
25283 +                       /* go on even err */
25284 +               }
25285 +       }
25286 +
25287 +out_dpages:
25288 +       au_dpages_free(&dpages);
25289 +out:
25290 +       return err;
25291 +}
25292 +
25293 +static int au_refresh_i(struct super_block *sb)
25294 +{
25295 +       int err, e;
25296 +       unsigned int sigen;
25297 +       unsigned long long max, ull;
25298 +       struct inode *inode, **array;
25299 +
25300 +       array = au_iarray_alloc(sb, &max);
25301 +       err = PTR_ERR(array);
25302 +       if (IS_ERR(array))
25303 +               goto out;
25304 +
25305 +       err = 0;
25306 +       sigen = au_sigen(sb);
25307 +       for (ull = 0; ull < max; ull++) {
25308 +               inode = array[ull];
25309 +               if (au_iigen(inode, NULL) != sigen) {
25310 +                       ii_write_lock_child(inode);
25311 +                       e = au_refresh_hinode_self(inode);
25312 +                       ii_write_unlock(inode);
25313 +                       if (unlikely(e)) {
25314 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
25315 +                               if (!err)
25316 +                                       err = e;
25317 +                               /* go on even if err */
25318 +                       }
25319 +               }
25320 +       }
25321 +
25322 +       au_iarray_free(array, max);
25323 +
25324 +out:
25325 +       return err;
25326 +}
25327 +
25328 +static void au_remount_refresh(struct super_block *sb)
25329 +{
25330 +       int err, e;
25331 +       unsigned int udba;
25332 +       aufs_bindex_t bindex, bend;
25333 +       struct dentry *root;
25334 +       struct inode *inode;
25335 +       struct au_branch *br;
25336 +
25337 +       au_sigen_inc(sb);
25338 +       au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
25339 +
25340 +       root = sb->s_root;
25341 +       DiMustNoWaiters(root);
25342 +       inode = root->d_inode;
25343 +       IiMustNoWaiters(inode);
25344 +
25345 +       udba = au_opt_udba(sb);
25346 +       bend = au_sbend(sb);
25347 +       for (bindex = 0; bindex <= bend; bindex++) {
25348 +               br = au_sbr(sb, bindex);
25349 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
25350 +               if (unlikely(err))
25351 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
25352 +                               bindex, err);
25353 +               /* go on even if err */
25354 +       }
25355 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
25356 +
25357 +       di_write_unlock(root);
25358 +       err = au_refresh_d(sb);
25359 +       e = au_refresh_i(sb);
25360 +       if (unlikely(e && !err))
25361 +               err = e;
25362 +       /* aufs_write_lock() calls ..._child() */
25363 +       di_write_lock_child(root);
25364 +
25365 +       au_cpup_attr_all(inode, /*force*/1);
25366 +
25367 +       if (unlikely(err))
25368 +               AuIOErr("refresh failed, ignored, %d\n", err);
25369 +}
25370 +
25371 +/* stop extra interpretation of errno in mount(8), and strange error messages */
25372 +static int cvt_err(int err)
25373 +{
25374 +       AuTraceErr(err);
25375 +
25376 +       switch (err) {
25377 +       case -ENOENT:
25378 +       case -ENOTDIR:
25379 +       case -EEXIST:
25380 +       case -EIO:
25381 +               err = -EINVAL;
25382 +       }
25383 +       return err;
25384 +}
25385 +
25386 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
25387 +{
25388 +       int err, do_dx;
25389 +       unsigned int mntflags;
25390 +       struct au_opts opts;
25391 +       struct dentry *root;
25392 +       struct inode *inode;
25393 +       struct au_sbinfo *sbinfo;
25394 +
25395 +       err = 0;
25396 +       root = sb->s_root;
25397 +       if (!data || !*data) {
25398 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
25399 +               if (!err) {
25400 +                       di_write_lock_child(root);
25401 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
25402 +                       aufs_write_unlock(root);
25403 +               }
25404 +               goto out;
25405 +       }
25406 +
25407 +       err = -ENOMEM;
25408 +       memset(&opts, 0, sizeof(opts));
25409 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
25410 +       if (unlikely(!opts.opt))
25411 +               goto out;
25412 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
25413 +       opts.flags = AuOpts_REMOUNT;
25414 +       opts.sb_flags = *flags;
25415 +
25416 +       /* parse it before aufs lock */
25417 +       err = au_opts_parse(sb, data, &opts);
25418 +       if (unlikely(err))
25419 +               goto out_opts;
25420 +
25421 +       sbinfo = au_sbi(sb);
25422 +       inode = root->d_inode;
25423 +       mutex_lock(&inode->i_mutex);
25424 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
25425 +       if (unlikely(err))
25426 +               goto out_mtx;
25427 +       di_write_lock_child(root);
25428 +
25429 +       /* au_opts_remount() may return an error */
25430 +       err = au_opts_remount(sb, &opts);
25431 +       au_opts_free(&opts);
25432 +
25433 +       if (au_ftest_opts(opts.flags, REFRESH))
25434 +               au_remount_refresh(sb);
25435 +
25436 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
25437 +               mntflags = au_mntflags(sb);
25438 +               do_dx = !!au_opt_test(mntflags, DIO);
25439 +               au_dy_arefresh(do_dx);
25440 +       }
25441 +
25442 +       aufs_write_unlock(root);
25443 +
25444 +out_mtx:
25445 +       mutex_unlock(&inode->i_mutex);
25446 +out_opts:
25447 +       free_page((unsigned long)opts.opt);
25448 +out:
25449 +       err = cvt_err(err);
25450 +       AuTraceErr(err);
25451 +       return err;
25452 +}
25453 +
25454 +static const struct super_operations aufs_sop = {
25455 +       .alloc_inode    = aufs_alloc_inode,
25456 +       .destroy_inode  = aufs_destroy_inode,
25457 +       /* always deleting, no clearing */
25458 +       .drop_inode     = generic_delete_inode,
25459 +       .show_options   = aufs_show_options,
25460 +       .statfs         = aufs_statfs,
25461 +       .put_super      = aufs_put_super,
25462 +       .sync_fs        = aufs_sync_fs,
25463 +       .remount_fs     = aufs_remount_fs
25464 +};
25465 +
25466 +/* ---------------------------------------------------------------------- */
25467 +
25468 +static int alloc_root(struct super_block *sb)
25469 +{
25470 +       int err;
25471 +       struct inode *inode;
25472 +       struct dentry *root;
25473 +
25474 +       err = -ENOMEM;
25475 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
25476 +       err = PTR_ERR(inode);
25477 +       if (IS_ERR(inode))
25478 +               goto out;
25479 +
25480 +       inode->i_op = &aufs_dir_iop;
25481 +       inode->i_fop = &aufs_dir_fop;
25482 +       inode->i_mode = S_IFDIR;
25483 +       set_nlink(inode, 2);
25484 +       unlock_new_inode(inode);
25485 +
25486 +       root = d_make_root(inode);
25487 +       if (unlikely(!root))
25488 +               goto out;
25489 +       err = PTR_ERR(root);
25490 +       if (IS_ERR(root))
25491 +               goto out;
25492 +
25493 +       err = au_di_init(root);
25494 +       if (!err) {
25495 +               sb->s_root = root;
25496 +               return 0; /* success */
25497 +       }
25498 +       dput(root);
25499 +
25500 +out:
25501 +       return err;
25502 +}
25503 +
25504 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
25505 +                          int silent __maybe_unused)
25506 +{
25507 +       int err;
25508 +       struct au_opts opts;
25509 +       struct dentry *root;
25510 +       struct inode *inode;
25511 +       char *arg = raw_data;
25512 +
25513 +       if (unlikely(!arg || !*arg)) {
25514 +               err = -EINVAL;
25515 +               pr_err("no arg\n");
25516 +               goto out;
25517 +       }
25518 +
25519 +       err = -ENOMEM;
25520 +       memset(&opts, 0, sizeof(opts));
25521 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
25522 +       if (unlikely(!opts.opt))
25523 +               goto out;
25524 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
25525 +       opts.sb_flags = sb->s_flags;
25526 +
25527 +       err = au_si_alloc(sb);
25528 +       if (unlikely(err))
25529 +               goto out_opts;
25530 +
25531 +       /* all timestamps always follow the ones on the branch */
25532 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
25533 +       sb->s_op = &aufs_sop;
25534 +       sb->s_d_op = &aufs_dop;
25535 +       sb->s_magic = AUFS_SUPER_MAGIC;
25536 +       sb->s_maxbytes = 0;
25537 +       au_export_init(sb);
25538 +
25539 +       err = alloc_root(sb);
25540 +       if (unlikely(err)) {
25541 +               si_write_unlock(sb);
25542 +               goto out_info;
25543 +       }
25544 +       root = sb->s_root;
25545 +       inode = root->d_inode;
25546 +
25547 +       /*
25548 +        * actually we can parse options regardless aufs lock here.
25549 +        * but at remount time, parsing must be done before aufs lock.
25550 +        * so we follow the same rule.
25551 +        */
25552 +       ii_write_lock_parent(inode);
25553 +       aufs_write_unlock(root);
25554 +       err = au_opts_parse(sb, arg, &opts);
25555 +       if (unlikely(err))
25556 +               goto out_root;
25557 +
25558 +       /* lock vfs_inode first, then aufs. */
25559 +       mutex_lock(&inode->i_mutex);
25560 +       aufs_write_lock(root);
25561 +       err = au_opts_mount(sb, &opts);
25562 +       au_opts_free(&opts);
25563 +       aufs_write_unlock(root);
25564 +       mutex_unlock(&inode->i_mutex);
25565 +       if (!err)
25566 +               goto out_opts; /* success */
25567 +
25568 +out_root:
25569 +       dput(root);
25570 +       sb->s_root = NULL;
25571 +out_info:
25572 +       dbgaufs_si_fin(au_sbi(sb));
25573 +       kobject_put(&au_sbi(sb)->si_kobj);
25574 +       sb->s_fs_info = NULL;
25575 +out_opts:
25576 +       free_page((unsigned long)opts.opt);
25577 +out:
25578 +       AuTraceErr(err);
25579 +       err = cvt_err(err);
25580 +       AuTraceErr(err);
25581 +       return err;
25582 +}
25583 +
25584 +/* ---------------------------------------------------------------------- */
25585 +
25586 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
25587 +                                const char *dev_name __maybe_unused,
25588 +                                void *raw_data)
25589 +{
25590 +       struct dentry *root;
25591 +       struct super_block *sb;
25592 +
25593 +       /* all timestamps always follow the ones on the branch */
25594 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
25595 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
25596 +       if (IS_ERR(root))
25597 +               goto out;
25598 +
25599 +       sb = root->d_sb;
25600 +       si_write_lock(sb, !AuLock_FLUSH);
25601 +       sysaufs_brs_add(sb, 0);
25602 +       si_write_unlock(sb);
25603 +       au_sbilist_add(sb);
25604 +
25605 +out:
25606 +       return root;
25607 +}
25608 +
25609 +static void aufs_kill_sb(struct super_block *sb)
25610 +{
25611 +       struct au_sbinfo *sbinfo;
25612 +
25613 +       sbinfo = au_sbi(sb);
25614 +       if (sbinfo) {
25615 +               au_sbilist_del(sb);
25616 +               aufs_write_lock(sb->s_root);
25617 +               if (sbinfo->si_wbr_create_ops->fin)
25618 +                       sbinfo->si_wbr_create_ops->fin(sb);
25619 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
25620 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
25621 +                       au_remount_refresh(sb);
25622 +               }
25623 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
25624 +                       au_plink_put(sb, /*verbose*/1);
25625 +               au_xino_clr(sb);
25626 +               sbinfo->si_sb = NULL;
25627 +               aufs_write_unlock(sb->s_root);
25628 +               au_nwt_flush(&sbinfo->si_nowait);
25629 +       }
25630 +       kill_anon_super(sb);
25631 +}
25632 +
25633 +struct file_system_type aufs_fs_type = {
25634 +       .name           = AUFS_FSTYPE,
25635 +       /* a race between rename and others */
25636 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
25637 +       .mount          = aufs_mount,
25638 +       .kill_sb        = aufs_kill_sb,
25639 +       /* no need to __module_get() and module_put(). */
25640 +       .owner          = THIS_MODULE,
25641 +};
25642 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
25643 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
25644 +++ linux/fs/aufs/super.h       2014-06-24 00:14:06.789386337 +0200
25645 @@ -0,0 +1,571 @@
25646 +/*
25647 + * Copyright (C) 2005-2014 Junjiro R. Okajima
25648 + *
25649 + * This program, aufs is free software; you can redistribute it and/or modify
25650 + * it under the terms of the GNU General Public License as published by
25651 + * the Free Software Foundation; either version 2 of the License, or
25652 + * (at your option) any later version.
25653 + *
25654 + * This program is distributed in the hope that it will be useful,
25655 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25656 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25657 + * GNU General Public License for more details.
25658 + *
25659 + * You should have received a copy of the GNU General Public License
25660 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25661 + */
25662 +
25663 +/*
25664 + * super_block operations
25665 + */
25666 +
25667 +#ifndef __AUFS_SUPER_H__
25668 +#define __AUFS_SUPER_H__
25669 +
25670 +#ifdef __KERNEL__
25671 +
25672 +#include <linux/fs.h>
25673 +#include "rwsem.h"
25674 +#include "spl.h"
25675 +#include "wkq.h"
25676 +
25677 +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
25678 +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
25679 +                              loff_t *);
25680 +
25681 +/* policies to select one among multiple writable branches */
25682 +struct au_wbr_copyup_operations {
25683 +       int (*copyup)(struct dentry *dentry);
25684 +};
25685 +
25686 +#define AuWbr_DIR      1               /* target is a dir */
25687 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
25688 +
25689 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
25690 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
25691 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
25692 +
25693 +struct au_wbr_create_operations {
25694 +       int (*create)(struct dentry *dentry, unsigned int flags);
25695 +       int (*init)(struct super_block *sb);
25696 +       int (*fin)(struct super_block *sb);
25697 +};
25698 +
25699 +struct au_wbr_mfs {
25700 +       struct mutex    mfs_lock; /* protect this structure */
25701 +       unsigned long   mfs_jiffy;
25702 +       unsigned long   mfs_expire;
25703 +       aufs_bindex_t   mfs_bindex;
25704 +
25705 +       unsigned long long      mfsrr_bytes;
25706 +       unsigned long long      mfsrr_watermark;
25707 +};
25708 +
25709 +struct pseudo_link {
25710 +       union {
25711 +               struct hlist_node hlist;
25712 +               struct rcu_head rcu;
25713 +       };
25714 +       struct inode *inode;
25715 +};
25716 +
25717 +#define AuPlink_NHASH 100
25718 +static inline int au_plink_hash(ino_t ino)
25719 +{
25720 +       return ino % AuPlink_NHASH;
25721 +}
25722 +
25723 +struct au_branch;
25724 +struct au_sbinfo {
25725 +       /* nowait tasks in the system-wide workqueue */
25726 +       struct au_nowait_tasks  si_nowait;
25727 +
25728 +       /*
25729 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
25730 +        * rwsem for au_sbinfo is necessary.
25731 +        */
25732 +       struct au_rwsem         si_rwsem;
25733 +
25734 +       /* prevent recursive locking in deleting inode */
25735 +       struct {
25736 +               unsigned long           *bitmap;
25737 +               spinlock_t              tree_lock;
25738 +               struct radix_tree_root  tree;
25739 +       } au_si_pid;
25740 +
25741 +       /*
25742 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
25743 +        * remount.
25744 +        */
25745 +       atomic_long_t           si_ninodes, si_nfiles;
25746 +
25747 +       /* branch management */
25748 +       unsigned int            si_generation;
25749 +
25750 +       /* see above flags */
25751 +       unsigned char           au_si_status;
25752 +
25753 +       aufs_bindex_t           si_bend;
25754 +
25755 +       /* dirty trick to keep br_id plus */
25756 +       unsigned int            si_last_br_id :
25757 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
25758 +       struct au_branch        **si_branch;
25759 +
25760 +       /* policy to select a writable branch */
25761 +       unsigned char           si_wbr_copyup;
25762 +       unsigned char           si_wbr_create;
25763 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
25764 +       struct au_wbr_create_operations *si_wbr_create_ops;
25765 +
25766 +       /* round robin */
25767 +       atomic_t                si_wbr_rr_next;
25768 +
25769 +       /* most free space */
25770 +       struct au_wbr_mfs       si_wbr_mfs;
25771 +
25772 +       /* mount flags */
25773 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
25774 +       unsigned int            si_mntflags;
25775 +
25776 +       /* external inode number (bitmap and translation table) */
25777 +       au_readf_t              si_xread;
25778 +       au_writef_t             si_xwrite;
25779 +       struct file             *si_xib;
25780 +       struct mutex            si_xib_mtx; /* protect xib members */
25781 +       unsigned long           *si_xib_buf;
25782 +       unsigned long           si_xib_last_pindex;
25783 +       int                     si_xib_next_bit;
25784 +       aufs_bindex_t           si_xino_brid;
25785 +       unsigned long           si_xino_jiffy;
25786 +       unsigned long           si_xino_expire;
25787 +       /* reserved for future use */
25788 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
25789 +
25790 +#ifdef CONFIG_AUFS_EXPORT
25791 +       /* i_generation */
25792 +       struct file             *si_xigen;
25793 +       atomic_t                si_xigen_next;
25794 +#endif
25795 +
25796 +       /* vdir parameters */
25797 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
25798 +       unsigned int            si_rdblk;       /* deblk size */
25799 +       unsigned int            si_rdhash;      /* hash size */
25800 +
25801 +       /*
25802 +        * If the number of whiteouts are larger than si_dirwh, leave all of
25803 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
25804 +        * future fsck.aufs or kernel thread will remove them later.
25805 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
25806 +        */
25807 +       unsigned int            si_dirwh;
25808 +
25809 +       /*
25810 +        * rename(2) a directory with all children.
25811 +        */
25812 +       /* reserved for future use */
25813 +       /* int                  si_rendir; */
25814 +
25815 +       /* pseudo_link list */
25816 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
25817 +       wait_queue_head_t       si_plink_wq;
25818 +       spinlock_t              si_plink_maint_lock;
25819 +       pid_t                   si_plink_maint_pid;
25820 +
25821 +       /* file list */
25822 +       struct au_sphlhead      si_files;
25823 +
25824 +       /*
25825 +        * sysfs and lifetime management.
25826 +        * this is not a small structure and it may be a waste of memory in case
25827 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
25828 +        * but using sysfs is majority.
25829 +        */
25830 +       struct kobject          si_kobj;
25831 +#ifdef CONFIG_DEBUG_FS
25832 +       struct dentry            *si_dbgaufs;
25833 +       struct dentry            *si_dbgaufs_plink;
25834 +       struct dentry            *si_dbgaufs_xib;
25835 +#ifdef CONFIG_AUFS_EXPORT
25836 +       struct dentry            *si_dbgaufs_xigen;
25837 +#endif
25838 +#endif
25839 +
25840 +#ifdef CONFIG_AUFS_SBILIST
25841 +       struct list_head        si_list;
25842 +#endif
25843 +
25844 +       /* dirty, necessary for unmounting, sysfs and sysrq */
25845 +       struct super_block      *si_sb;
25846 +};
25847 +
25848 +/* sbinfo status flags */
25849 +/*
25850 + * set true when refresh_dirs() failed at remount time.
25851 + * then try refreshing dirs at access time again.
25852 + * if it is false, refreshing dirs at access time is unnecesary
25853 + */
25854 +#define AuSi_FAILED_REFRESH_DIR        1
25855 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
25856 +                                          unsigned int flag)
25857 +{
25858 +       AuRwMustAnyLock(&sbi->si_rwsem);
25859 +       return sbi->au_si_status & flag;
25860 +}
25861 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
25862 +#define au_fset_si(sbinfo, name) do { \
25863 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
25864 +       (sbinfo)->au_si_status |= AuSi_##name; \
25865 +} while (0)
25866 +#define au_fclr_si(sbinfo, name) do { \
25867 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
25868 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
25869 +} while (0)
25870 +
25871 +/* ---------------------------------------------------------------------- */
25872 +
25873 +/* policy to select one among writable branches */
25874 +#define AuWbrCopyup(sbinfo, ...) \
25875 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
25876 +#define AuWbrCreate(sbinfo, ...) \
25877 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
25878 +
25879 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
25880 +#define AuLock_DW              1               /* write-lock dentry */
25881 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
25882 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
25883 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
25884 +#define AuLock_DIR             (1 << 4)        /* target is a dir */
25885 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
25886 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
25887 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
25888 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
25889 +#define au_fset_lock(flags, name) \
25890 +       do { (flags) |= AuLock_##name; } while (0)
25891 +#define au_fclr_lock(flags, name) \
25892 +       do { (flags) &= ~AuLock_##name; } while (0)
25893 +
25894 +/* ---------------------------------------------------------------------- */
25895 +
25896 +/* super.c */
25897 +extern struct file_system_type aufs_fs_type;
25898 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
25899 +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
25900 +                                          void *arg);
25901 +void au_array_free(void *array);
25902 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
25903 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
25904 +void au_iarray_free(struct inode **a, unsigned long long max);
25905 +
25906 +/* sbinfo.c */
25907 +void au_si_free(struct kobject *kobj);
25908 +int au_si_alloc(struct super_block *sb);
25909 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
25910 +
25911 +unsigned int au_sigen_inc(struct super_block *sb);
25912 +aufs_bindex_t au_new_br_id(struct super_block *sb);
25913 +
25914 +int si_read_lock(struct super_block *sb, int flags);
25915 +int si_write_lock(struct super_block *sb, int flags);
25916 +int aufs_read_lock(struct dentry *dentry, int flags);
25917 +void aufs_read_unlock(struct dentry *dentry, int flags);
25918 +void aufs_write_lock(struct dentry *dentry);
25919 +void aufs_write_unlock(struct dentry *dentry);
25920 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
25921 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
25922 +
25923 +int si_pid_test_slow(struct super_block *sb);
25924 +void si_pid_set_slow(struct super_block *sb);
25925 +void si_pid_clr_slow(struct super_block *sb);
25926 +
25927 +/* wbr_policy.c */
25928 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
25929 +extern struct au_wbr_create_operations au_wbr_create_ops[];
25930 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
25931 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
25932 +
25933 +/* mvdown.c */
25934 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
25935 +
25936 +/* ---------------------------------------------------------------------- */
25937 +
25938 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
25939 +{
25940 +       return sb->s_fs_info;
25941 +}
25942 +
25943 +/* ---------------------------------------------------------------------- */
25944 +
25945 +#ifdef CONFIG_AUFS_EXPORT
25946 +int au_test_nfsd(void);
25947 +void au_export_init(struct super_block *sb);
25948 +void au_xigen_inc(struct inode *inode);
25949 +int au_xigen_new(struct inode *inode);
25950 +int au_xigen_set(struct super_block *sb, struct file *base);
25951 +void au_xigen_clr(struct super_block *sb);
25952 +
25953 +static inline int au_busy_or_stale(void)
25954 +{
25955 +       if (!au_test_nfsd())
25956 +               return -EBUSY;
25957 +       return -ESTALE;
25958 +}
25959 +#else
25960 +AuStubInt0(au_test_nfsd, void)
25961 +AuStubVoid(au_export_init, struct super_block *sb)
25962 +AuStubVoid(au_xigen_inc, struct inode *inode)
25963 +AuStubInt0(au_xigen_new, struct inode *inode)
25964 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
25965 +AuStubVoid(au_xigen_clr, struct super_block *sb)
25966 +static inline int au_busy_or_stale(void)
25967 +{
25968 +       return -EBUSY;
25969 +}
25970 +#endif /* CONFIG_AUFS_EXPORT */
25971 +
25972 +/* ---------------------------------------------------------------------- */
25973 +
25974 +#ifdef CONFIG_AUFS_SBILIST
25975 +/* module.c */
25976 +extern struct au_splhead au_sbilist;
25977 +
25978 +static inline void au_sbilist_init(void)
25979 +{
25980 +       au_spl_init(&au_sbilist);
25981 +}
25982 +
25983 +static inline void au_sbilist_add(struct super_block *sb)
25984 +{
25985 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
25986 +}
25987 +
25988 +static inline void au_sbilist_del(struct super_block *sb)
25989 +{
25990 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
25991 +}
25992 +
25993 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
25994 +static inline void au_sbilist_lock(void)
25995 +{
25996 +       spin_lock(&au_sbilist.spin);
25997 +}
25998 +
25999 +static inline void au_sbilist_unlock(void)
26000 +{
26001 +       spin_unlock(&au_sbilist.spin);
26002 +}
26003 +#define AuGFP_SBILIST  GFP_ATOMIC
26004 +#else
26005 +AuStubVoid(au_sbilist_lock, void)
26006 +AuStubVoid(au_sbilist_unlock, void)
26007 +#define AuGFP_SBILIST  GFP_NOFS
26008 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
26009 +#else
26010 +AuStubVoid(au_sbilist_init, void)
26011 +AuStubVoid(au_sbilist_add, struct super_block*)
26012 +AuStubVoid(au_sbilist_del, struct super_block*)
26013 +AuStubVoid(au_sbilist_lock, void)
26014 +AuStubVoid(au_sbilist_unlock, void)
26015 +#define AuGFP_SBILIST  GFP_NOFS
26016 +#endif
26017 +
26018 +/* ---------------------------------------------------------------------- */
26019 +
26020 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
26021 +{
26022 +       /*
26023 +        * This function is a dynamic '__init' fucntion actually,
26024 +        * so the tiny check for si_rwsem is unnecessary.
26025 +        */
26026 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
26027 +#ifdef CONFIG_DEBUG_FS
26028 +       sbinfo->si_dbgaufs = NULL;
26029 +       sbinfo->si_dbgaufs_plink = NULL;
26030 +       sbinfo->si_dbgaufs_xib = NULL;
26031 +#ifdef CONFIG_AUFS_EXPORT
26032 +       sbinfo->si_dbgaufs_xigen = NULL;
26033 +#endif
26034 +#endif
26035 +}
26036 +
26037 +/* ---------------------------------------------------------------------- */
26038 +
26039 +static inline pid_t si_pid_bit(void)
26040 +{
26041 +       /* the origin of pid is 1, but the bitmap's is 0 */
26042 +       return current->pid - 1;
26043 +}
26044 +
26045 +static inline int si_pid_test(struct super_block *sb)
26046 +{
26047 +       pid_t bit = si_pid_bit();
26048 +       if (bit < PID_MAX_DEFAULT)
26049 +               return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
26050 +       else
26051 +               return si_pid_test_slow(sb);
26052 +}
26053 +
26054 +static inline void si_pid_set(struct super_block *sb)
26055 +{
26056 +       pid_t bit = si_pid_bit();
26057 +       if (bit < PID_MAX_DEFAULT) {
26058 +               AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
26059 +               set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
26060 +               /* smp_mb(); */
26061 +       } else
26062 +               si_pid_set_slow(sb);
26063 +}
26064 +
26065 +static inline void si_pid_clr(struct super_block *sb)
26066 +{
26067 +       pid_t bit = si_pid_bit();
26068 +       if (bit < PID_MAX_DEFAULT) {
26069 +               AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
26070 +               clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
26071 +               /* smp_mb(); */
26072 +       } else
26073 +               si_pid_clr_slow(sb);
26074 +}
26075 +
26076 +/* ---------------------------------------------------------------------- */
26077 +
26078 +/* lock superblock. mainly for entry point functions */
26079 +/*
26080 + * __si_read_lock, __si_write_lock,
26081 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
26082 + */
26083 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
26084 +
26085 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
26086 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
26087 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
26088 +
26089 +static inline void si_noflush_read_lock(struct super_block *sb)
26090 +{
26091 +       __si_read_lock(sb);
26092 +       si_pid_set(sb);
26093 +}
26094 +
26095 +static inline int si_noflush_read_trylock(struct super_block *sb)
26096 +{
26097 +       int locked = __si_read_trylock(sb);
26098 +       if (locked)
26099 +               si_pid_set(sb);
26100 +       return locked;
26101 +}
26102 +
26103 +static inline void si_noflush_write_lock(struct super_block *sb)
26104 +{
26105 +       __si_write_lock(sb);
26106 +       si_pid_set(sb);
26107 +}
26108 +
26109 +static inline int si_noflush_write_trylock(struct super_block *sb)
26110 +{
26111 +       int locked = __si_write_trylock(sb);
26112 +       if (locked)
26113 +               si_pid_set(sb);
26114 +       return locked;
26115 +}
26116 +
26117 +#if 0 /* unused */
26118 +static inline int si_read_trylock(struct super_block *sb, int flags)
26119 +{
26120 +       if (au_ftest_lock(flags, FLUSH))
26121 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26122 +       return si_noflush_read_trylock(sb);
26123 +}
26124 +#endif
26125 +
26126 +static inline void si_read_unlock(struct super_block *sb)
26127 +{
26128 +       si_pid_clr(sb);
26129 +       __si_read_unlock(sb);
26130 +}
26131 +
26132 +#if 0 /* unused */
26133 +static inline int si_write_trylock(struct super_block *sb, int flags)
26134 +{
26135 +       if (au_ftest_lock(flags, FLUSH))
26136 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26137 +       return si_noflush_write_trylock(sb);
26138 +}
26139 +#endif
26140 +
26141 +static inline void si_write_unlock(struct super_block *sb)
26142 +{
26143 +       si_pid_clr(sb);
26144 +       __si_write_unlock(sb);
26145 +}
26146 +
26147 +#if 0 /* unused */
26148 +static inline void si_downgrade_lock(struct super_block *sb)
26149 +{
26150 +       __si_downgrade_lock(sb);
26151 +}
26152 +#endif
26153 +
26154 +/* ---------------------------------------------------------------------- */
26155 +
26156 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
26157 +{
26158 +       SiMustAnyLock(sb);
26159 +       return au_sbi(sb)->si_bend;
26160 +}
26161 +
26162 +static inline unsigned int au_mntflags(struct super_block *sb)
26163 +{
26164 +       SiMustAnyLock(sb);
26165 +       return au_sbi(sb)->si_mntflags;
26166 +}
26167 +
26168 +static inline unsigned int au_sigen(struct super_block *sb)
26169 +{
26170 +       SiMustAnyLock(sb);
26171 +       return au_sbi(sb)->si_generation;
26172 +}
26173 +
26174 +static inline void au_ninodes_inc(struct super_block *sb)
26175 +{
26176 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
26177 +}
26178 +
26179 +static inline void au_ninodes_dec(struct super_block *sb)
26180 +{
26181 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
26182 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
26183 +}
26184 +
26185 +static inline void au_nfiles_inc(struct super_block *sb)
26186 +{
26187 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
26188 +}
26189 +
26190 +static inline void au_nfiles_dec(struct super_block *sb)
26191 +{
26192 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
26193 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
26194 +}
26195 +
26196 +static inline struct au_branch *au_sbr(struct super_block *sb,
26197 +                                      aufs_bindex_t bindex)
26198 +{
26199 +       SiMustAnyLock(sb);
26200 +       return au_sbi(sb)->si_branch[0 + bindex];
26201 +}
26202 +
26203 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
26204 +{
26205 +       SiMustWriteLock(sb);
26206 +       au_sbi(sb)->si_xino_brid = brid;
26207 +}
26208 +
26209 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
26210 +{
26211 +       SiMustAnyLock(sb);
26212 +       return au_sbi(sb)->si_xino_brid;
26213 +}
26214 +
26215 +#endif /* __KERNEL__ */
26216 +#endif /* __AUFS_SUPER_H__ */
26217 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
26218 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
26219 +++ linux/fs/aufs/sysaufs.c     2014-06-24 00:14:06.789386337 +0200
26220 @@ -0,0 +1,104 @@
26221 +/*
26222 + * Copyright (C) 2005-2014 Junjiro R. Okajima
26223 + *
26224 + * This program, aufs is free software; you can redistribute it and/or modify
26225 + * it under the terms of the GNU General Public License as published by
26226 + * the Free Software Foundation; either version 2 of the License, or
26227 + * (at your option) any later version.
26228 + *
26229 + * This program is distributed in the hope that it will be useful,
26230 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26231 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26232 + * GNU General Public License for more details.
26233 + *
26234 + * You should have received a copy of the GNU General Public License
26235 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26236 + */
26237 +
26238 +/*
26239 + * sysfs interface and lifetime management
26240 + * they are necessary regardless sysfs is disabled.
26241 + */
26242 +
26243 +#include <linux/random.h>
26244 +#include "aufs.h"
26245 +
26246 +unsigned long sysaufs_si_mask;
26247 +struct kset *sysaufs_kset;
26248 +
26249 +#define AuSiAttr(_name) { \
26250 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
26251 +       .show   = sysaufs_si_##_name,                           \
26252 +}
26253 +
26254 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
26255 +struct attribute *sysaufs_si_attrs[] = {
26256 +       &sysaufs_si_attr_xi_path.attr,
26257 +       NULL,
26258 +};
26259 +
26260 +static const struct sysfs_ops au_sbi_ops = {
26261 +       .show   = sysaufs_si_show
26262 +};
26263 +
26264 +static struct kobj_type au_sbi_ktype = {
26265 +       .release        = au_si_free,
26266 +       .sysfs_ops      = &au_sbi_ops,
26267 +       .default_attrs  = sysaufs_si_attrs
26268 +};
26269 +
26270 +/* ---------------------------------------------------------------------- */
26271 +
26272 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
26273 +{
26274 +       int err;
26275 +
26276 +       sbinfo->si_kobj.kset = sysaufs_kset;
26277 +       /* cf. sysaufs_name() */
26278 +       err = kobject_init_and_add
26279 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
26280 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
26281 +
26282 +       dbgaufs_si_null(sbinfo);
26283 +       if (!err) {
26284 +               err = dbgaufs_si_init(sbinfo);
26285 +               if (unlikely(err))
26286 +                       kobject_put(&sbinfo->si_kobj);
26287 +       }
26288 +       return err;
26289 +}
26290 +
26291 +void sysaufs_fin(void)
26292 +{
26293 +       dbgaufs_fin();
26294 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
26295 +       kset_unregister(sysaufs_kset);
26296 +}
26297 +
26298 +int __init sysaufs_init(void)
26299 +{
26300 +       int err;
26301 +
26302 +       do {
26303 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
26304 +       } while (!sysaufs_si_mask);
26305 +
26306 +       err = -EINVAL;
26307 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
26308 +       if (unlikely(!sysaufs_kset))
26309 +               goto out;
26310 +       err = PTR_ERR(sysaufs_kset);
26311 +       if (IS_ERR(sysaufs_kset))
26312 +               goto out;
26313 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
26314 +       if (unlikely(err)) {
26315 +               kset_unregister(sysaufs_kset);
26316 +               goto out;
26317 +       }
26318 +
26319 +       err = dbgaufs_init();
26320 +       if (unlikely(err))
26321 +               sysaufs_fin();
26322 +out:
26323 +       return err;
26324 +}
26325 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
26326 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
26327 +++ linux/fs/aufs/sysaufs.h     2014-06-24 00:14:06.789386337 +0200
26328 @@ -0,0 +1,103 @@
26329 +/*
26330 + * Copyright (C) 2005-2014 Junjiro R. Okajima
26331 + *
26332 + * This program, aufs is free software; you can redistribute it and/or modify
26333 + * it under the terms of the GNU General Public License as published by
26334 + * the Free Software Foundation; either version 2 of the License, or
26335 + * (at your option) any later version.
26336 + *
26337 + * This program is distributed in the hope that it will be useful,
26338 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26339 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26340 + * GNU General Public License for more details.
26341 + *
26342 + * You should have received a copy of the GNU General Public License
26343 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26344 + */
26345 +
26346 +/*
26347 + * sysfs interface and mount lifetime management
26348 + */
26349 +
26350 +#ifndef __SYSAUFS_H__
26351 +#define __SYSAUFS_H__
26352 +
26353 +#ifdef __KERNEL__
26354 +
26355 +#include <linux/sysfs.h>
26356 +#include "module.h"
26357 +
26358 +struct super_block;
26359 +struct au_sbinfo;
26360 +
26361 +struct sysaufs_si_attr {
26362 +       struct attribute attr;
26363 +       int (*show)(struct seq_file *seq, struct super_block *sb);
26364 +};
26365 +
26366 +/* ---------------------------------------------------------------------- */
26367 +
26368 +/* sysaufs.c */
26369 +extern unsigned long sysaufs_si_mask;
26370 +extern struct kset *sysaufs_kset;
26371 +extern struct attribute *sysaufs_si_attrs[];
26372 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
26373 +int __init sysaufs_init(void);
26374 +void sysaufs_fin(void);
26375 +
26376 +/* ---------------------------------------------------------------------- */
26377 +
26378 +/* some people doesn't like to show a pointer in kernel */
26379 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
26380 +{
26381 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
26382 +}
26383 +
26384 +#define SysaufsSiNamePrefix    "si_"
26385 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
26386 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
26387 +{
26388 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
26389 +                sysaufs_si_id(sbinfo));
26390 +}
26391 +
26392 +struct au_branch;
26393 +#ifdef CONFIG_SYSFS
26394 +/* sysfs.c */
26395 +extern struct attribute_group *sysaufs_attr_group;
26396 +
26397 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
26398 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
26399 +                        char *buf);
26400 +
26401 +void sysaufs_br_init(struct au_branch *br);
26402 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
26403 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
26404 +
26405 +#define sysaufs_brs_init()     do {} while (0)
26406 +
26407 +#else
26408 +#define sysaufs_attr_group     NULL
26409 +
26410 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
26411 +
26412 +static inline
26413 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
26414 +                        char *buf)
26415 +{
26416 +       return 0;
26417 +}
26418 +
26419 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
26420 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
26421 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
26422 +
26423 +static inline void sysaufs_brs_init(void)
26424 +{
26425 +       sysaufs_brs = 0;
26426 +}
26427 +
26428 +#endif /* CONFIG_SYSFS */
26429 +
26430 +#endif /* __KERNEL__ */
26431 +#endif /* __SYSAUFS_H__ */
26432 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
26433 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
26434 +++ linux/fs/aufs/sysfs.c       2014-06-24 00:14:06.789386337 +0200
26435 @@ -0,0 +1,296 @@
26436 +/*
26437 + * Copyright (C) 2005-2014 Junjiro R. Okajima
26438 + *
26439 + * This program, aufs is free software; you can redistribute it and/or modify
26440 + * it under the terms of the GNU General Public License as published by
26441 + * the Free Software Foundation; either version 2 of the License, or
26442 + * (at your option) any later version.
26443 + *
26444 + * This program is distributed in the hope that it will be useful,
26445 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26446 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26447 + * GNU General Public License for more details.
26448 + *
26449 + * You should have received a copy of the GNU General Public License
26450 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26451 + */
26452 +
26453 +/*
26454 + * sysfs interface
26455 + */
26456 +
26457 +#include <linux/seq_file.h>
26458 +#include "aufs.h"
26459 +
26460 +#ifdef CONFIG_AUFS_FS_MODULE
26461 +/* this entry violates the "one line per file" policy of sysfs */
26462 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
26463 +                          char *buf)
26464 +{
26465 +       ssize_t err;
26466 +       static char *conf =
26467 +/* this file is generated at compiling */
26468 +#include "conf.str"
26469 +               ;
26470 +
26471 +       err = snprintf(buf, PAGE_SIZE, conf);
26472 +       if (unlikely(err >= PAGE_SIZE))
26473 +               err = -EFBIG;
26474 +       return err;
26475 +}
26476 +
26477 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
26478 +#endif
26479 +
26480 +static struct attribute *au_attr[] = {
26481 +#ifdef CONFIG_AUFS_FS_MODULE
26482 +       &au_config_attr.attr,
26483 +#endif
26484 +       NULL,   /* need to NULL terminate the list of attributes */
26485 +};
26486 +
26487 +static struct attribute_group sysaufs_attr_group_body = {
26488 +       .attrs = au_attr
26489 +};
26490 +
26491 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
26492 +
26493 +/* ---------------------------------------------------------------------- */
26494 +
26495 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
26496 +{
26497 +       int err;
26498 +
26499 +       SiMustAnyLock(sb);
26500 +
26501 +       err = 0;
26502 +       if (au_opt_test(au_mntflags(sb), XINO)) {
26503 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
26504 +               seq_putc(seq, '\n');
26505 +       }
26506 +       return err;
26507 +}
26508 +
26509 +/*
26510 + * the lifetime of branch is independent from the entry under sysfs.
26511 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
26512 + * unlinked.
26513 + */
26514 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
26515 +                        aufs_bindex_t bindex, int idx)
26516 +{
26517 +       int err;
26518 +       struct path path;
26519 +       struct dentry *root;
26520 +       struct au_branch *br;
26521 +       char *perm;
26522 +
26523 +       AuDbg("b%d\n", bindex);
26524 +
26525 +       err = 0;
26526 +       root = sb->s_root;
26527 +       di_read_lock_parent(root, !AuLock_IR);
26528 +       br = au_sbr(sb, bindex);
26529 +
26530 +       switch (idx) {
26531 +       case AuBrSysfs_BR:
26532 +               path.mnt = au_br_mnt(br);
26533 +               path.dentry = au_h_dptr(root, bindex);
26534 +               au_seq_path(seq, &path);
26535 +               di_read_unlock(root, !AuLock_IR);
26536 +               perm = au_optstr_br_perm(br->br_perm);
26537 +               if (perm) {
26538 +                       err = seq_printf(seq, "=%s\n", perm);
26539 +                       kfree(perm);
26540 +                       if (err == -1)
26541 +                               err = -E2BIG;
26542 +               } else
26543 +                       err = -ENOMEM;
26544 +               break;
26545 +       case AuBrSysfs_BRID:
26546 +               err = seq_printf(seq, "%d\n", br->br_id);
26547 +               di_read_unlock(root, !AuLock_IR);
26548 +               if (err == -1)
26549 +                       err = -E2BIG;
26550 +               break;
26551 +       }
26552 +
26553 +       return err;
26554 +}
26555 +
26556 +/* ---------------------------------------------------------------------- */
26557 +
26558 +static struct seq_file *au_seq(char *p, ssize_t len)
26559 +{
26560 +       struct seq_file *seq;
26561 +
26562 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
26563 +       if (seq) {
26564 +               /* mutex_init(&seq.lock); */
26565 +               seq->buf = p;
26566 +               seq->size = len;
26567 +               return seq; /* success */
26568 +       }
26569 +
26570 +       seq = ERR_PTR(-ENOMEM);
26571 +       return seq;
26572 +}
26573 +
26574 +#define SysaufsBr_PREFIX       "br"
26575 +#define SysaufsBrid_PREFIX     "brid"
26576 +
26577 +/* todo: file size may exceed PAGE_SIZE */
26578 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
26579 +                       char *buf)
26580 +{
26581 +       ssize_t err;
26582 +       int idx;
26583 +       long l;
26584 +       aufs_bindex_t bend;
26585 +       struct au_sbinfo *sbinfo;
26586 +       struct super_block *sb;
26587 +       struct seq_file *seq;
26588 +       char *name;
26589 +       struct attribute **cattr;
26590 +
26591 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
26592 +       sb = sbinfo->si_sb;
26593 +
26594 +       /*
26595 +        * prevent a race condition between sysfs and aufs.
26596 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
26597 +        * prohibits maintaining the sysfs entries.
26598 +        * hew we acquire read lock after sysfs_get_active_two().
26599 +        * on the other hand, the remount process may maintain the sysfs/aufs
26600 +        * entries after acquiring write lock.
26601 +        * it can cause a deadlock.
26602 +        * simply we gave up processing read here.
26603 +        */
26604 +       err = -EBUSY;
26605 +       if (unlikely(!si_noflush_read_trylock(sb)))
26606 +               goto out;
26607 +
26608 +       seq = au_seq(buf, PAGE_SIZE);
26609 +       err = PTR_ERR(seq);
26610 +       if (IS_ERR(seq))
26611 +               goto out_unlock;
26612 +
26613 +       name = (void *)attr->name;
26614 +       cattr = sysaufs_si_attrs;
26615 +       while (*cattr) {
26616 +               if (!strcmp(name, (*cattr)->name)) {
26617 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
26618 +                               ->show(seq, sb);
26619 +                       goto out_seq;
26620 +               }
26621 +               cattr++;
26622 +       }
26623 +
26624 +       if (!strncmp(name, SysaufsBrid_PREFIX,
26625 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
26626 +               idx = AuBrSysfs_BRID;
26627 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
26628 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
26629 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
26630 +               idx = AuBrSysfs_BR;
26631 +               name += sizeof(SysaufsBr_PREFIX) - 1;
26632 +       } else
26633 +                 BUG();
26634 +
26635 +       err = kstrtol(name, 10, &l);
26636 +       if (!err) {
26637 +               bend = au_sbend(sb);
26638 +               if (l <= bend)
26639 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
26640 +               else
26641 +                       err = -ENOENT;
26642 +       }
26643 +
26644 +out_seq:
26645 +       if (!err) {
26646 +               err = seq->count;
26647 +               /* sysfs limit */
26648 +               if (unlikely(err == PAGE_SIZE))
26649 +                       err = -EFBIG;
26650 +       }
26651 +       kfree(seq);
26652 +out_unlock:
26653 +       si_read_unlock(sb);
26654 +out:
26655 +       return err;
26656 +}
26657 +
26658 +/* ---------------------------------------------------------------------- */
26659 +
26660 +void sysaufs_br_init(struct au_branch *br)
26661 +{
26662 +       int i;
26663 +       struct au_brsysfs *br_sysfs;
26664 +       struct attribute *attr;
26665 +
26666 +       br_sysfs = br->br_sysfs;
26667 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
26668 +               attr = &br_sysfs->attr;
26669 +               sysfs_attr_init(attr);
26670 +               attr->name = br_sysfs->name;
26671 +               attr->mode = S_IRUGO;
26672 +               br_sysfs++;
26673 +       }
26674 +}
26675 +
26676 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
26677 +{
26678 +       struct au_branch *br;
26679 +       struct kobject *kobj;
26680 +       struct au_brsysfs *br_sysfs;
26681 +       int i;
26682 +       aufs_bindex_t bend;
26683 +
26684 +       dbgaufs_brs_del(sb, bindex);
26685 +
26686 +       if (!sysaufs_brs)
26687 +               return;
26688 +
26689 +       kobj = &au_sbi(sb)->si_kobj;
26690 +       bend = au_sbend(sb);
26691 +       for (; bindex <= bend; bindex++) {
26692 +               br = au_sbr(sb, bindex);
26693 +               br_sysfs = br->br_sysfs;
26694 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
26695 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
26696 +                       br_sysfs++;
26697 +               }
26698 +       }
26699 +}
26700 +
26701 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
26702 +{
26703 +       int err, i;
26704 +       aufs_bindex_t bend;
26705 +       struct kobject *kobj;
26706 +       struct au_branch *br;
26707 +       struct au_brsysfs *br_sysfs;
26708 +
26709 +       dbgaufs_brs_add(sb, bindex);
26710 +
26711 +       if (!sysaufs_brs)
26712 +               return;
26713 +
26714 +       kobj = &au_sbi(sb)->si_kobj;
26715 +       bend = au_sbend(sb);
26716 +       for (; bindex <= bend; bindex++) {
26717 +               br = au_sbr(sb, bindex);
26718 +               br_sysfs = br->br_sysfs;
26719 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
26720 +                        SysaufsBr_PREFIX "%d", bindex);
26721 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
26722 +                        SysaufsBrid_PREFIX "%d", bindex);
26723 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
26724 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
26725 +                       if (unlikely(err))
26726 +                               pr_warn("failed %s under sysfs(%d)\n",
26727 +                                       br_sysfs->name, err);
26728 +                       br_sysfs++;
26729 +               }
26730 +       }
26731 +}
26732 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
26733 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
26734 +++ linux/fs/aufs/sysrq.c       2014-06-24 00:14:06.789386337 +0200
26735 @@ -0,0 +1,154 @@
26736 +/*
26737 + * Copyright (C) 2005-2014 Junjiro R. Okajima
26738 + *
26739 + * This program, aufs is free software; you can redistribute it and/or modify
26740 + * it under the terms of the GNU General Public License as published by
26741 + * the Free Software Foundation; either version 2 of the License, or
26742 + * (at your option) any later version.
26743 + *
26744 + * This program is distributed in the hope that it will be useful,
26745 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26746 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26747 + * GNU General Public License for more details.
26748 + *
26749 + * You should have received a copy of the GNU General Public License
26750 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26751 + */
26752 +
26753 +/*
26754 + * magic sysrq hanlder
26755 + */
26756 +
26757 +/* #include <linux/sysrq.h> */
26758 +#include <linux/writeback.h>
26759 +#include "aufs.h"
26760 +
26761 +/* ---------------------------------------------------------------------- */
26762 +
26763 +static void sysrq_sb(struct super_block *sb)
26764 +{
26765 +       char *plevel;
26766 +       struct au_sbinfo *sbinfo;
26767 +       struct file *file;
26768 +       struct au_sphlhead *files;
26769 +       struct au_finfo *finfo;
26770 +
26771 +       plevel = au_plevel;
26772 +       au_plevel = KERN_WARNING;
26773 +
26774 +       /* since we define pr_fmt, call printk directly */
26775 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
26776 +
26777 +       sbinfo = au_sbi(sb);
26778 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
26779 +       pr("superblock\n");
26780 +       au_dpri_sb(sb);
26781 +
26782 +#if 0
26783 +       pr("root dentry\n");
26784 +       au_dpri_dentry(sb->s_root);
26785 +       pr("root inode\n");
26786 +       au_dpri_inode(sb->s_root->d_inode);
26787 +#endif
26788 +
26789 +#if 0
26790 +       do {
26791 +               int err, i, j, ndentry;
26792 +               struct au_dcsub_pages dpages;
26793 +               struct au_dpage *dpage;
26794 +
26795 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
26796 +               if (unlikely(err))
26797 +                       break;
26798 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
26799 +               if (!err)
26800 +                       for (i = 0; i < dpages.ndpage; i++) {
26801 +                               dpage = dpages.dpages + i;
26802 +                               ndentry = dpage->ndentry;
26803 +                               for (j = 0; j < ndentry; j++)
26804 +                                       au_dpri_dentry(dpage->dentries[j]);
26805 +                       }
26806 +               au_dpages_free(&dpages);
26807 +       } while (0);
26808 +#endif
26809 +
26810 +#if 1
26811 +       {
26812 +               struct inode *i;
26813 +               pr("isolated inode\n");
26814 +               spin_lock(&inode_sb_list_lock);
26815 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
26816 +                       spin_lock(&i->i_lock);
26817 +                       if (1 || hlist_empty(&i->i_dentry))
26818 +                               au_dpri_inode(i);
26819 +                       spin_unlock(&i->i_lock);
26820 +               }
26821 +               spin_unlock(&inode_sb_list_lock);
26822 +       }
26823 +#endif
26824 +       pr("files\n");
26825 +       files = &au_sbi(sb)->si_files;
26826 +       spin_lock(&files->spin);
26827 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
26828 +               umode_t mode;
26829 +               file = finfo->fi_file;
26830 +               mode = file_inode(file)->i_mode;
26831 +               if (!special_file(mode))
26832 +                       au_dpri_file(file);
26833 +       }
26834 +       spin_unlock(&files->spin);
26835 +       pr("done\n");
26836 +
26837 +#undef pr
26838 +       au_plevel = plevel;
26839 +}
26840 +
26841 +/* ---------------------------------------------------------------------- */
26842 +
26843 +/* module parameter */
26844 +static char *aufs_sysrq_key = "a";
26845 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
26846 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
26847 +
26848 +static void au_sysrq(int key __maybe_unused)
26849 +{
26850 +       struct au_sbinfo *sbinfo;
26851 +
26852 +       lockdep_off();
26853 +       au_sbilist_lock();
26854 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
26855 +               sysrq_sb(sbinfo->si_sb);
26856 +       au_sbilist_unlock();
26857 +       lockdep_on();
26858 +}
26859 +
26860 +static struct sysrq_key_op au_sysrq_op = {
26861 +       .handler        = au_sysrq,
26862 +       .help_msg       = "Aufs",
26863 +       .action_msg     = "Aufs",
26864 +       .enable_mask    = SYSRQ_ENABLE_DUMP
26865 +};
26866 +
26867 +/* ---------------------------------------------------------------------- */
26868 +
26869 +int __init au_sysrq_init(void)
26870 +{
26871 +       int err;
26872 +       char key;
26873 +
26874 +       err = -1;
26875 +       key = *aufs_sysrq_key;
26876 +       if ('a' <= key && key <= 'z')
26877 +               err = register_sysrq_key(key, &au_sysrq_op);
26878 +       if (unlikely(err))
26879 +               pr_err("err %d, sysrq=%c\n", err, key);
26880 +       return err;
26881 +}
26882 +
26883 +void au_sysrq_fin(void)
26884 +{
26885 +       int err;
26886 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
26887 +       if (unlikely(err))
26888 +               pr_err("err %d (ignored)\n", err);
26889 +}
26890 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
26891 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
26892 +++ linux/fs/aufs/vdir.c        2014-06-24 00:14:06.789386337 +0200
26893 @@ -0,0 +1,887 @@
26894 +/*
26895 + * Copyright (C) 2005-2014 Junjiro R. Okajima
26896 + *
26897 + * This program, aufs is free software; you can redistribute it and/or modify
26898 + * it under the terms of the GNU General Public License as published by
26899 + * the Free Software Foundation; either version 2 of the License, or
26900 + * (at your option) any later version.
26901 + *
26902 + * This program is distributed in the hope that it will be useful,
26903 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26904 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26905 + * GNU General Public License for more details.
26906 + *
26907 + * You should have received a copy of the GNU General Public License
26908 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26909 + */
26910 +
26911 +/*
26912 + * virtual or vertical directory
26913 + */
26914 +
26915 +#include "aufs.h"
26916 +
26917 +static unsigned int calc_size(int nlen)
26918 +{
26919 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
26920 +}
26921 +
26922 +static int set_deblk_end(union au_vdir_deblk_p *p,
26923 +                        union au_vdir_deblk_p *deblk_end)
26924 +{
26925 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
26926 +               p->de->de_str.len = 0;
26927 +               /* smp_mb(); */
26928 +               return 0;
26929 +       }
26930 +       return -1; /* error */
26931 +}
26932 +
26933 +/* returns true or false */
26934 +static int is_deblk_end(union au_vdir_deblk_p *p,
26935 +                       union au_vdir_deblk_p *deblk_end)
26936 +{
26937 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
26938 +               return !p->de->de_str.len;
26939 +       return 1;
26940 +}
26941 +
26942 +static unsigned char *last_deblk(struct au_vdir *vdir)
26943 +{
26944 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
26945 +}
26946 +
26947 +/* ---------------------------------------------------------------------- */
26948 +
26949 +/* estimate the apropriate size for name hash table */
26950 +unsigned int au_rdhash_est(loff_t sz)
26951 +{
26952 +       unsigned int n;
26953 +
26954 +       n = UINT_MAX;
26955 +       sz >>= 10;
26956 +       if (sz < n)
26957 +               n = sz;
26958 +       if (sz < AUFS_RDHASH_DEF)
26959 +               n = AUFS_RDHASH_DEF;
26960 +       /* pr_info("n %u\n", n); */
26961 +       return n;
26962 +}
26963 +
26964 +/*
26965 + * the allocated memory has to be freed by
26966 + * au_nhash_wh_free() or au_nhash_de_free().
26967 + */
26968 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
26969 +{
26970 +       struct hlist_head *head;
26971 +       unsigned int u;
26972 +
26973 +       head = kmalloc(sizeof(*nhash->nh_head) * num_hash, gfp);
26974 +       if (head) {
26975 +               nhash->nh_num = num_hash;
26976 +               nhash->nh_head = head;
26977 +               for (u = 0; u < num_hash; u++)
26978 +                       INIT_HLIST_HEAD(head++);
26979 +               return 0; /* success */
26980 +       }
26981 +
26982 +       return -ENOMEM;
26983 +}
26984 +
26985 +static void nhash_count(struct hlist_head *head)
26986 +{
26987 +#if 0
26988 +       unsigned long n;
26989 +       struct hlist_node *pos;
26990 +
26991 +       n = 0;
26992 +       hlist_for_each(pos, head)
26993 +               n++;
26994 +       pr_info("%lu\n", n);
26995 +#endif
26996 +}
26997 +
26998 +static void au_nhash_wh_do_free(struct hlist_head *head)
26999 +{
27000 +       struct au_vdir_wh *pos;
27001 +       struct hlist_node *node;
27002 +
27003 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
27004 +               kfree(pos);
27005 +}
27006 +
27007 +static void au_nhash_de_do_free(struct hlist_head *head)
27008 +{
27009 +       struct au_vdir_dehstr *pos;
27010 +       struct hlist_node *node;
27011 +
27012 +       hlist_for_each_entry_safe(pos, node, head, hash)
27013 +               au_cache_free_vdir_dehstr(pos);
27014 +}
27015 +
27016 +static void au_nhash_do_free(struct au_nhash *nhash,
27017 +                            void (*free)(struct hlist_head *head))
27018 +{
27019 +       unsigned int n;
27020 +       struct hlist_head *head;
27021 +
27022 +       n = nhash->nh_num;
27023 +       if (!n)
27024 +               return;
27025 +
27026 +       head = nhash->nh_head;
27027 +       while (n-- > 0) {
27028 +               nhash_count(head);
27029 +               free(head++);
27030 +       }
27031 +       kfree(nhash->nh_head);
27032 +}
27033 +
27034 +void au_nhash_wh_free(struct au_nhash *whlist)
27035 +{
27036 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
27037 +}
27038 +
27039 +static void au_nhash_de_free(struct au_nhash *delist)
27040 +{
27041 +       au_nhash_do_free(delist, au_nhash_de_do_free);
27042 +}
27043 +
27044 +/* ---------------------------------------------------------------------- */
27045 +
27046 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
27047 +                           int limit)
27048 +{
27049 +       int num;
27050 +       unsigned int u, n;
27051 +       struct hlist_head *head;
27052 +       struct au_vdir_wh *pos;
27053 +
27054 +       num = 0;
27055 +       n = whlist->nh_num;
27056 +       head = whlist->nh_head;
27057 +       for (u = 0; u < n; u++, head++)
27058 +               hlist_for_each_entry(pos, head, wh_hash)
27059 +                       if (pos->wh_bindex == btgt && ++num > limit)
27060 +                               return 1;
27061 +       return 0;
27062 +}
27063 +
27064 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
27065 +                                      unsigned char *name,
27066 +                                      unsigned int len)
27067 +{
27068 +       unsigned int v;
27069 +       /* const unsigned int magic_bit = 12; */
27070 +
27071 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
27072 +
27073 +       v = 0;
27074 +       while (len--)
27075 +               v += *name++;
27076 +       /* v = hash_long(v, magic_bit); */
27077 +       v %= nhash->nh_num;
27078 +       return nhash->nh_head + v;
27079 +}
27080 +
27081 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
27082 +                             int nlen)
27083 +{
27084 +       return str->len == nlen && !memcmp(str->name, name, nlen);
27085 +}
27086 +
27087 +/* returns found or not */
27088 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
27089 +{
27090 +       struct hlist_head *head;
27091 +       struct au_vdir_wh *pos;
27092 +       struct au_vdir_destr *str;
27093 +
27094 +       head = au_name_hash(whlist, name, nlen);
27095 +       hlist_for_each_entry(pos, head, wh_hash) {
27096 +               str = &pos->wh_str;
27097 +               AuDbg("%.*s\n", str->len, str->name);
27098 +               if (au_nhash_test_name(str, name, nlen))
27099 +                       return 1;
27100 +       }
27101 +       return 0;
27102 +}
27103 +
27104 +/* returns found(true) or not */
27105 +static int test_known(struct au_nhash *delist, char *name, int nlen)
27106 +{
27107 +       struct hlist_head *head;
27108 +       struct au_vdir_dehstr *pos;
27109 +       struct au_vdir_destr *str;
27110 +
27111 +       head = au_name_hash(delist, name, nlen);
27112 +       hlist_for_each_entry(pos, head, hash) {
27113 +               str = pos->str;
27114 +               AuDbg("%.*s\n", str->len, str->name);
27115 +               if (au_nhash_test_name(str, name, nlen))
27116 +                       return 1;
27117 +       }
27118 +       return 0;
27119 +}
27120 +
27121 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
27122 +                           unsigned char d_type)
27123 +{
27124 +#ifdef CONFIG_AUFS_SHWH
27125 +       wh->wh_ino = ino;
27126 +       wh->wh_type = d_type;
27127 +#endif
27128 +}
27129 +
27130 +/* ---------------------------------------------------------------------- */
27131 +
27132 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
27133 +                      unsigned int d_type, aufs_bindex_t bindex,
27134 +                      unsigned char shwh)
27135 +{
27136 +       int err;
27137 +       struct au_vdir_destr *str;
27138 +       struct au_vdir_wh *wh;
27139 +
27140 +       AuDbg("%.*s\n", nlen, name);
27141 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
27142 +
27143 +       err = -ENOMEM;
27144 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
27145 +       if (unlikely(!wh))
27146 +               goto out;
27147 +
27148 +       err = 0;
27149 +       wh->wh_bindex = bindex;
27150 +       if (shwh)
27151 +               au_shwh_init_wh(wh, ino, d_type);
27152 +       str = &wh->wh_str;
27153 +       str->len = nlen;
27154 +       memcpy(str->name, name, nlen);
27155 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
27156 +       /* smp_mb(); */
27157 +
27158 +out:
27159 +       return err;
27160 +}
27161 +
27162 +static int append_deblk(struct au_vdir *vdir)
27163 +{
27164 +       int err;
27165 +       unsigned long ul;
27166 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
27167 +       union au_vdir_deblk_p p, deblk_end;
27168 +       unsigned char **o;
27169 +
27170 +       err = -ENOMEM;
27171 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
27172 +                    GFP_NOFS);
27173 +       if (unlikely(!o))
27174 +               goto out;
27175 +
27176 +       vdir->vd_deblk = o;
27177 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
27178 +       if (p.deblk) {
27179 +               ul = vdir->vd_nblk++;
27180 +               vdir->vd_deblk[ul] = p.deblk;
27181 +               vdir->vd_last.ul = ul;
27182 +               vdir->vd_last.p.deblk = p.deblk;
27183 +               deblk_end.deblk = p.deblk + deblk_sz;
27184 +               err = set_deblk_end(&p, &deblk_end);
27185 +       }
27186 +
27187 +out:
27188 +       return err;
27189 +}
27190 +
27191 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
27192 +                    unsigned int d_type, struct au_nhash *delist)
27193 +{
27194 +       int err;
27195 +       unsigned int sz;
27196 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
27197 +       union au_vdir_deblk_p p, *room, deblk_end;
27198 +       struct au_vdir_dehstr *dehstr;
27199 +
27200 +       p.deblk = last_deblk(vdir);
27201 +       deblk_end.deblk = p.deblk + deblk_sz;
27202 +       room = &vdir->vd_last.p;
27203 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
27204 +                 || !is_deblk_end(room, &deblk_end));
27205 +
27206 +       sz = calc_size(nlen);
27207 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
27208 +               err = append_deblk(vdir);
27209 +               if (unlikely(err))
27210 +                       goto out;
27211 +
27212 +               p.deblk = last_deblk(vdir);
27213 +               deblk_end.deblk = p.deblk + deblk_sz;
27214 +               /* smp_mb(); */
27215 +               AuDebugOn(room->deblk != p.deblk);
27216 +       }
27217 +
27218 +       err = -ENOMEM;
27219 +       dehstr = au_cache_alloc_vdir_dehstr();
27220 +       if (unlikely(!dehstr))
27221 +               goto out;
27222 +
27223 +       dehstr->str = &room->de->de_str;
27224 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
27225 +       room->de->de_ino = ino;
27226 +       room->de->de_type = d_type;
27227 +       room->de->de_str.len = nlen;
27228 +       memcpy(room->de->de_str.name, name, nlen);
27229 +
27230 +       err = 0;
27231 +       room->deblk += sz;
27232 +       if (unlikely(set_deblk_end(room, &deblk_end)))
27233 +               err = append_deblk(vdir);
27234 +       /* smp_mb(); */
27235 +
27236 +out:
27237 +       return err;
27238 +}
27239 +
27240 +/* ---------------------------------------------------------------------- */
27241 +
27242 +void au_vdir_free(struct au_vdir *vdir)
27243 +{
27244 +       unsigned char **deblk;
27245 +
27246 +       deblk = vdir->vd_deblk;
27247 +       while (vdir->vd_nblk--)
27248 +               kfree(*deblk++);
27249 +       kfree(vdir->vd_deblk);
27250 +       au_cache_free_vdir(vdir);
27251 +}
27252 +
27253 +static struct au_vdir *alloc_vdir(struct file *file)
27254 +{
27255 +       struct au_vdir *vdir;
27256 +       struct super_block *sb;
27257 +       int err;
27258 +
27259 +       sb = file->f_dentry->d_sb;
27260 +       SiMustAnyLock(sb);
27261 +
27262 +       err = -ENOMEM;
27263 +       vdir = au_cache_alloc_vdir();
27264 +       if (unlikely(!vdir))
27265 +               goto out;
27266 +
27267 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
27268 +       if (unlikely(!vdir->vd_deblk))
27269 +               goto out_free;
27270 +
27271 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
27272 +       if (!vdir->vd_deblk_sz) {
27273 +               /* estimate the apropriate size for deblk */
27274 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
27275 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
27276 +       }
27277 +       vdir->vd_nblk = 0;
27278 +       vdir->vd_version = 0;
27279 +       vdir->vd_jiffy = 0;
27280 +       err = append_deblk(vdir);
27281 +       if (!err)
27282 +               return vdir; /* success */
27283 +
27284 +       kfree(vdir->vd_deblk);
27285 +
27286 +out_free:
27287 +       au_cache_free_vdir(vdir);
27288 +out:
27289 +       vdir = ERR_PTR(err);
27290 +       return vdir;
27291 +}
27292 +
27293 +static int reinit_vdir(struct au_vdir *vdir)
27294 +{
27295 +       int err;
27296 +       union au_vdir_deblk_p p, deblk_end;
27297 +
27298 +       while (vdir->vd_nblk > 1) {
27299 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
27300 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
27301 +               vdir->vd_nblk--;
27302 +       }
27303 +       p.deblk = vdir->vd_deblk[0];
27304 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
27305 +       err = set_deblk_end(&p, &deblk_end);
27306 +       /* keep vd_dblk_sz */
27307 +       vdir->vd_last.ul = 0;
27308 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
27309 +       vdir->vd_version = 0;
27310 +       vdir->vd_jiffy = 0;
27311 +       /* smp_mb(); */
27312 +       return err;
27313 +}
27314 +
27315 +/* ---------------------------------------------------------------------- */
27316 +
27317 +#define AuFillVdir_CALLED      1
27318 +#define AuFillVdir_WHABLE      (1 << 1)
27319 +#define AuFillVdir_SHWH                (1 << 2)
27320 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
27321 +#define au_fset_fillvdir(flags, name) \
27322 +       do { (flags) |= AuFillVdir_##name; } while (0)
27323 +#define au_fclr_fillvdir(flags, name) \
27324 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
27325 +
27326 +#ifndef CONFIG_AUFS_SHWH
27327 +#undef AuFillVdir_SHWH
27328 +#define AuFillVdir_SHWH                0
27329 +#endif
27330 +
27331 +struct fillvdir_arg {
27332 +       struct dir_context      ctx;
27333 +       struct file             *file;
27334 +       struct au_vdir          *vdir;
27335 +       struct au_nhash         delist;
27336 +       struct au_nhash         whlist;
27337 +       aufs_bindex_t           bindex;
27338 +       unsigned int            flags;
27339 +       int                     err;
27340 +};
27341 +
27342 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
27343 +                   loff_t offset __maybe_unused, u64 h_ino,
27344 +                   unsigned int d_type)
27345 +{
27346 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
27347 +       char *name = (void *)__name;
27348 +       struct super_block *sb;
27349 +       ino_t ino;
27350 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
27351 +
27352 +       arg->err = 0;
27353 +       sb = arg->file->f_dentry->d_sb;
27354 +       au_fset_fillvdir(arg->flags, CALLED);
27355 +       /* smp_mb(); */
27356 +       if (nlen <= AUFS_WH_PFX_LEN
27357 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
27358 +               if (test_known(&arg->delist, name, nlen)
27359 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
27360 +                       goto out; /* already exists or whiteouted */
27361 +
27362 +               sb = arg->file->f_dentry->d_sb;
27363 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
27364 +               if (!arg->err) {
27365 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
27366 +                               d_type = DT_UNKNOWN;
27367 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
27368 +                                            d_type, &arg->delist);
27369 +               }
27370 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
27371 +               name += AUFS_WH_PFX_LEN;
27372 +               nlen -= AUFS_WH_PFX_LEN;
27373 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
27374 +                       goto out; /* already whiteouted */
27375 +
27376 +               if (shwh)
27377 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
27378 +                                            &ino);
27379 +               if (!arg->err) {
27380 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
27381 +                               d_type = DT_UNKNOWN;
27382 +                       arg->err = au_nhash_append_wh
27383 +                               (&arg->whlist, name, nlen, ino, d_type,
27384 +                                arg->bindex, shwh);
27385 +               }
27386 +       }
27387 +
27388 +out:
27389 +       if (!arg->err)
27390 +               arg->vdir->vd_jiffy = jiffies;
27391 +       /* smp_mb(); */
27392 +       AuTraceErr(arg->err);
27393 +       return arg->err;
27394 +}
27395 +
27396 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
27397 +                         struct au_nhash *whlist, struct au_nhash *delist)
27398 +{
27399 +#ifdef CONFIG_AUFS_SHWH
27400 +       int err;
27401 +       unsigned int nh, u;
27402 +       struct hlist_head *head;
27403 +       struct au_vdir_wh *pos;
27404 +       struct hlist_node *n;
27405 +       char *p, *o;
27406 +       struct au_vdir_destr *destr;
27407 +
27408 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
27409 +
27410 +       err = -ENOMEM;
27411 +       o = p = (void *)__get_free_page(GFP_NOFS);
27412 +       if (unlikely(!p))
27413 +               goto out;
27414 +
27415 +       err = 0;
27416 +       nh = whlist->nh_num;
27417 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
27418 +       p += AUFS_WH_PFX_LEN;
27419 +       for (u = 0; u < nh; u++) {
27420 +               head = whlist->nh_head + u;
27421 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
27422 +                       destr = &pos->wh_str;
27423 +                       memcpy(p, destr->name, destr->len);
27424 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
27425 +                                       pos->wh_ino, pos->wh_type, delist);
27426 +                       if (unlikely(err))
27427 +                               break;
27428 +               }
27429 +       }
27430 +
27431 +       free_page((unsigned long)o);
27432 +
27433 +out:
27434 +       AuTraceErr(err);
27435 +       return err;
27436 +#else
27437 +       return 0;
27438 +#endif
27439 +}
27440 +
27441 +static int au_do_read_vdir(struct fillvdir_arg *arg)
27442 +{
27443 +       int err;
27444 +       unsigned int rdhash;
27445 +       loff_t offset;
27446 +       aufs_bindex_t bend, bindex, bstart;
27447 +       unsigned char shwh;
27448 +       struct file *hf, *file;
27449 +       struct super_block *sb;
27450 +
27451 +       file = arg->file;
27452 +       sb = file->f_dentry->d_sb;
27453 +       SiMustAnyLock(sb);
27454 +
27455 +       rdhash = au_sbi(sb)->si_rdhash;
27456 +       if (!rdhash)
27457 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
27458 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
27459 +       if (unlikely(err))
27460 +               goto out;
27461 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
27462 +       if (unlikely(err))
27463 +               goto out_delist;
27464 +
27465 +       err = 0;
27466 +       arg->flags = 0;
27467 +       shwh = 0;
27468 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
27469 +               shwh = 1;
27470 +               au_fset_fillvdir(arg->flags, SHWH);
27471 +       }
27472 +       bstart = au_fbstart(file);
27473 +       bend = au_fbend_dir(file);
27474 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
27475 +               hf = au_hf_dir(file, bindex);
27476 +               if (!hf)
27477 +                       continue;
27478 +
27479 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
27480 +               err = offset;
27481 +               if (unlikely(offset))
27482 +                       break;
27483 +
27484 +               arg->bindex = bindex;
27485 +               au_fclr_fillvdir(arg->flags, WHABLE);
27486 +               if (shwh
27487 +                   || (bindex != bend
27488 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
27489 +                       au_fset_fillvdir(arg->flags, WHABLE);
27490 +               do {
27491 +                       arg->err = 0;
27492 +                       au_fclr_fillvdir(arg->flags, CALLED);
27493 +                       /* smp_mb(); */
27494 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
27495 +                       if (err >= 0)
27496 +                               err = arg->err;
27497 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
27498 +
27499 +               /*
27500 +                * dir_relax() may be good for concurrency, but aufs should not
27501 +                * use it since it will cause a lockdep problem.
27502 +                */
27503 +       }
27504 +
27505 +       if (!err && shwh)
27506 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
27507 +
27508 +       au_nhash_wh_free(&arg->whlist);
27509 +
27510 +out_delist:
27511 +       au_nhash_de_free(&arg->delist);
27512 +out:
27513 +       return err;
27514 +}
27515 +
27516 +static int read_vdir(struct file *file, int may_read)
27517 +{
27518 +       int err;
27519 +       unsigned long expire;
27520 +       unsigned char do_read;
27521 +       struct fillvdir_arg arg = {
27522 +               .ctx = {
27523 +                       .actor = au_diractor(fillvdir)
27524 +               }
27525 +       };
27526 +       struct inode *inode;
27527 +       struct au_vdir *vdir, *allocated;
27528 +
27529 +       err = 0;
27530 +       inode = file_inode(file);
27531 +       IMustLock(inode);
27532 +       SiMustAnyLock(inode->i_sb);
27533 +
27534 +       allocated = NULL;
27535 +       do_read = 0;
27536 +       expire = au_sbi(inode->i_sb)->si_rdcache;
27537 +       vdir = au_ivdir(inode);
27538 +       if (!vdir) {
27539 +               do_read = 1;
27540 +               vdir = alloc_vdir(file);
27541 +               err = PTR_ERR(vdir);
27542 +               if (IS_ERR(vdir))
27543 +                       goto out;
27544 +               err = 0;
27545 +               allocated = vdir;
27546 +       } else if (may_read
27547 +                  && (inode->i_version != vdir->vd_version
27548 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
27549 +               do_read = 1;
27550 +               err = reinit_vdir(vdir);
27551 +               if (unlikely(err))
27552 +                       goto out;
27553 +       }
27554 +
27555 +       if (!do_read)
27556 +               return 0; /* success */
27557 +
27558 +       arg.file = file;
27559 +       arg.vdir = vdir;
27560 +       err = au_do_read_vdir(&arg);
27561 +       if (!err) {
27562 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
27563 +               vdir->vd_version = inode->i_version;
27564 +               vdir->vd_last.ul = 0;
27565 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
27566 +               if (allocated)
27567 +                       au_set_ivdir(inode, allocated);
27568 +       } else if (allocated)
27569 +               au_vdir_free(allocated);
27570 +
27571 +out:
27572 +       return err;
27573 +}
27574 +
27575 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
27576 +{
27577 +       int err, rerr;
27578 +       unsigned long ul, n;
27579 +       const unsigned int deblk_sz = src->vd_deblk_sz;
27580 +
27581 +       AuDebugOn(tgt->vd_nblk != 1);
27582 +
27583 +       err = -ENOMEM;
27584 +       if (tgt->vd_nblk < src->vd_nblk) {
27585 +               unsigned char **p;
27586 +
27587 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
27588 +                            GFP_NOFS);
27589 +               if (unlikely(!p))
27590 +                       goto out;
27591 +               tgt->vd_deblk = p;
27592 +       }
27593 +
27594 +       if (tgt->vd_deblk_sz != deblk_sz) {
27595 +               unsigned char *p;
27596 +
27597 +               tgt->vd_deblk_sz = deblk_sz;
27598 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
27599 +               if (unlikely(!p))
27600 +                       goto out;
27601 +               tgt->vd_deblk[0] = p;
27602 +       }
27603 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
27604 +       tgt->vd_version = src->vd_version;
27605 +       tgt->vd_jiffy = src->vd_jiffy;
27606 +
27607 +       n = src->vd_nblk;
27608 +       for (ul = 1; ul < n; ul++) {
27609 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
27610 +                                           GFP_NOFS);
27611 +               if (unlikely(!tgt->vd_deblk[ul]))
27612 +                       goto out;
27613 +               tgt->vd_nblk++;
27614 +       }
27615 +       tgt->vd_nblk = n;
27616 +       tgt->vd_last.ul = tgt->vd_last.ul;
27617 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
27618 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
27619 +               - src->vd_deblk[src->vd_last.ul];
27620 +       /* smp_mb(); */
27621 +       return 0; /* success */
27622 +
27623 +out:
27624 +       rerr = reinit_vdir(tgt);
27625 +       BUG_ON(rerr);
27626 +       return err;
27627 +}
27628 +
27629 +int au_vdir_init(struct file *file)
27630 +{
27631 +       int err;
27632 +       struct inode *inode;
27633 +       struct au_vdir *vdir_cache, *allocated;
27634 +
27635 +       /* test file->f_pos here instead of ctx->pos */
27636 +       err = read_vdir(file, !file->f_pos);
27637 +       if (unlikely(err))
27638 +               goto out;
27639 +
27640 +       allocated = NULL;
27641 +       vdir_cache = au_fvdir_cache(file);
27642 +       if (!vdir_cache) {
27643 +               vdir_cache = alloc_vdir(file);
27644 +               err = PTR_ERR(vdir_cache);
27645 +               if (IS_ERR(vdir_cache))
27646 +                       goto out;
27647 +               allocated = vdir_cache;
27648 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
27649 +               /* test file->f_pos here instead of ctx->pos */
27650 +               err = reinit_vdir(vdir_cache);
27651 +               if (unlikely(err))
27652 +                       goto out;
27653 +       } else
27654 +               return 0; /* success */
27655 +
27656 +       inode = file_inode(file);
27657 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
27658 +       if (!err) {
27659 +               file->f_version = inode->i_version;
27660 +               if (allocated)
27661 +                       au_set_fvdir_cache(file, allocated);
27662 +       } else if (allocated)
27663 +               au_vdir_free(allocated);
27664 +
27665 +out:
27666 +       return err;
27667 +}
27668 +
27669 +static loff_t calc_offset(struct au_vdir *vdir)
27670 +{
27671 +       loff_t offset;
27672 +       union au_vdir_deblk_p p;
27673 +
27674 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
27675 +       offset = vdir->vd_last.p.deblk - p.deblk;
27676 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
27677 +       return offset;
27678 +}
27679 +
27680 +/* returns true or false */
27681 +static int seek_vdir(struct file *file, struct dir_context *ctx)
27682 +{
27683 +       int valid;
27684 +       unsigned int deblk_sz;
27685 +       unsigned long ul, n;
27686 +       loff_t offset;
27687 +       union au_vdir_deblk_p p, deblk_end;
27688 +       struct au_vdir *vdir_cache;
27689 +
27690 +       valid = 1;
27691 +       vdir_cache = au_fvdir_cache(file);
27692 +       offset = calc_offset(vdir_cache);
27693 +       AuDbg("offset %lld\n", offset);
27694 +       if (ctx->pos == offset)
27695 +               goto out;
27696 +
27697 +       vdir_cache->vd_last.ul = 0;
27698 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
27699 +       if (!ctx->pos)
27700 +               goto out;
27701 +
27702 +       valid = 0;
27703 +       deblk_sz = vdir_cache->vd_deblk_sz;
27704 +       ul = div64_u64(ctx->pos, deblk_sz);
27705 +       AuDbg("ul %lu\n", ul);
27706 +       if (ul >= vdir_cache->vd_nblk)
27707 +               goto out;
27708 +
27709 +       n = vdir_cache->vd_nblk;
27710 +       for (; ul < n; ul++) {
27711 +               p.deblk = vdir_cache->vd_deblk[ul];
27712 +               deblk_end.deblk = p.deblk + deblk_sz;
27713 +               offset = ul;
27714 +               offset *= deblk_sz;
27715 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
27716 +                       unsigned int l;
27717 +
27718 +                       l = calc_size(p.de->de_str.len);
27719 +                       offset += l;
27720 +                       p.deblk += l;
27721 +               }
27722 +               if (!is_deblk_end(&p, &deblk_end)) {
27723 +                       valid = 1;
27724 +                       vdir_cache->vd_last.ul = ul;
27725 +                       vdir_cache->vd_last.p = p;
27726 +                       break;
27727 +               }
27728 +       }
27729 +
27730 +out:
27731 +       /* smp_mb(); */
27732 +       AuTraceErr(!valid);
27733 +       return valid;
27734 +}
27735 +
27736 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
27737 +{
27738 +       unsigned int l, deblk_sz;
27739 +       union au_vdir_deblk_p deblk_end;
27740 +       struct au_vdir *vdir_cache;
27741 +       struct au_vdir_de *de;
27742 +
27743 +       vdir_cache = au_fvdir_cache(file);
27744 +       if (!seek_vdir(file, ctx))
27745 +               return 0;
27746 +
27747 +       deblk_sz = vdir_cache->vd_deblk_sz;
27748 +       while (1) {
27749 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
27750 +               deblk_end.deblk += deblk_sz;
27751 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
27752 +                       de = vdir_cache->vd_last.p.de;
27753 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
27754 +                             de->de_str.len, de->de_str.name, ctx->pos,
27755 +                             (unsigned long)de->de_ino, de->de_type);
27756 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
27757 +                                              de->de_str.len, de->de_ino,
27758 +                                              de->de_type))) {
27759 +                               /* todo: ignore the error caused by udba? */
27760 +                               /* return err; */
27761 +                               return 0;
27762 +                       }
27763 +
27764 +                       l = calc_size(de->de_str.len);
27765 +                       vdir_cache->vd_last.p.deblk += l;
27766 +                       ctx->pos += l;
27767 +               }
27768 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
27769 +                       vdir_cache->vd_last.ul++;
27770 +                       vdir_cache->vd_last.p.deblk
27771 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
27772 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
27773 +                       continue;
27774 +               }
27775 +               break;
27776 +       }
27777 +
27778 +       /* smp_mb(); */
27779 +       return 0;
27780 +}
27781 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
27782 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
27783 +++ linux/fs/aufs/vfsub.c       2014-06-24 00:14:06.789386337 +0200
27784 @@ -0,0 +1,782 @@
27785 +/*
27786 + * Copyright (C) 2005-2014 Junjiro R. Okajima
27787 + *
27788 + * This program, aufs is free software; you can redistribute it and/or modify
27789 + * it under the terms of the GNU General Public License as published by
27790 + * the Free Software Foundation; either version 2 of the License, or
27791 + * (at your option) any later version.
27792 + *
27793 + * This program is distributed in the hope that it will be useful,
27794 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27795 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27796 + * GNU General Public License for more details.
27797 + *
27798 + * You should have received a copy of the GNU General Public License
27799 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27800 + */
27801 +
27802 +/*
27803 + * sub-routines for VFS
27804 + */
27805 +
27806 +#include <linux/ima.h>
27807 +#include <linux/namei.h>
27808 +#include <linux/security.h>
27809 +#include <linux/splice.h>
27810 +#include "aufs.h"
27811 +
27812 +int vfsub_update_h_iattr(struct path *h_path, int *did)
27813 +{
27814 +       int err;
27815 +       struct kstat st;
27816 +       struct super_block *h_sb;
27817 +
27818 +       /* for remote fs, leave work for its getattr or d_revalidate */
27819 +       /* for bad i_attr fs, handle them in aufs_getattr() */
27820 +       /* still some fs may acquire i_mutex. we need to skip them */
27821 +       err = 0;
27822 +       if (!did)
27823 +               did = &err;
27824 +       h_sb = h_path->dentry->d_sb;
27825 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
27826 +       if (*did)
27827 +               err = vfs_getattr(h_path, &st);
27828 +
27829 +       return err;
27830 +}
27831 +
27832 +/* ---------------------------------------------------------------------- */
27833 +
27834 +struct file *vfsub_dentry_open(struct path *path, int flags)
27835 +{
27836 +       struct file *file;
27837 +
27838 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
27839 +                          current_cred());
27840 +       if (!IS_ERR_OR_NULL(file)
27841 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
27842 +               i_readcount_inc(path->dentry->d_inode);
27843 +
27844 +       return file;
27845 +}
27846 +
27847 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
27848 +{
27849 +       struct file *file;
27850 +
27851 +       lockdep_off();
27852 +       file = filp_open(path,
27853 +                        oflags /* | __FMODE_NONOTIFY */,
27854 +                        mode);
27855 +       lockdep_on();
27856 +       if (IS_ERR(file))
27857 +               goto out;
27858 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
27859 +
27860 +out:
27861 +       return file;
27862 +}
27863 +
27864 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
27865 +{
27866 +       int err;
27867 +
27868 +       err = kern_path(name, flags, path);
27869 +       if (!err && path->dentry->d_inode)
27870 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
27871 +       return err;
27872 +}
27873 +
27874 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
27875 +                                   int len)
27876 +{
27877 +       struct path path = {
27878 +               .mnt = NULL
27879 +       };
27880 +
27881 +       /* VFS checks it too, but by WARN_ON_ONCE() */
27882 +       IMustLock(parent->d_inode);
27883 +
27884 +       path.dentry = lookup_one_len(name, parent, len);
27885 +       if (IS_ERR(path.dentry))
27886 +               goto out;
27887 +       if (path.dentry->d_inode)
27888 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
27889 +
27890 +out:
27891 +       AuTraceErrPtr(path.dentry);
27892 +       return path.dentry;
27893 +}
27894 +
27895 +void vfsub_call_lkup_one(void *args)
27896 +{
27897 +       struct vfsub_lkup_one_args *a = args;
27898 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
27899 +}
27900 +
27901 +/* ---------------------------------------------------------------------- */
27902 +
27903 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
27904 +                                struct dentry *d2, struct au_hinode *hdir2)
27905 +{
27906 +       struct dentry *d;
27907 +
27908 +       lockdep_off();
27909 +       d = lock_rename(d1, d2);
27910 +       lockdep_on();
27911 +       au_hn_suspend(hdir1);
27912 +       if (hdir1 != hdir2)
27913 +               au_hn_suspend(hdir2);
27914 +
27915 +       return d;
27916 +}
27917 +
27918 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
27919 +                        struct dentry *d2, struct au_hinode *hdir2)
27920 +{
27921 +       au_hn_resume(hdir1);
27922 +       if (hdir1 != hdir2)
27923 +               au_hn_resume(hdir2);
27924 +       lockdep_off();
27925 +       unlock_rename(d1, d2);
27926 +       lockdep_on();
27927 +}
27928 +
27929 +/* ---------------------------------------------------------------------- */
27930 +
27931 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
27932 +{
27933 +       int err;
27934 +       struct dentry *d;
27935 +
27936 +       IMustLock(dir);
27937 +
27938 +       d = path->dentry;
27939 +       path->dentry = d->d_parent;
27940 +       err = security_path_mknod(path, d, mode, 0);
27941 +       path->dentry = d;
27942 +       if (unlikely(err))
27943 +               goto out;
27944 +
27945 +       err = vfs_create(dir, path->dentry, mode, want_excl);
27946 +       if (!err) {
27947 +               struct path tmp = *path;
27948 +               int did;
27949 +
27950 +               vfsub_update_h_iattr(&tmp, &did);
27951 +               if (did) {
27952 +                       tmp.dentry = path->dentry->d_parent;
27953 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27954 +               }
27955 +               /*ignore*/
27956 +       }
27957 +
27958 +out:
27959 +       return err;
27960 +}
27961 +
27962 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
27963 +{
27964 +       int err;
27965 +       struct dentry *d;
27966 +
27967 +       IMustLock(dir);
27968 +
27969 +       d = path->dentry;
27970 +       path->dentry = d->d_parent;
27971 +       err = security_path_symlink(path, d, symname);
27972 +       path->dentry = d;
27973 +       if (unlikely(err))
27974 +               goto out;
27975 +
27976 +       err = vfs_symlink(dir, path->dentry, symname);
27977 +       if (!err) {
27978 +               struct path tmp = *path;
27979 +               int did;
27980 +
27981 +               vfsub_update_h_iattr(&tmp, &did);
27982 +               if (did) {
27983 +                       tmp.dentry = path->dentry->d_parent;
27984 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
27985 +               }
27986 +               /*ignore*/
27987 +       }
27988 +
27989 +out:
27990 +       return err;
27991 +}
27992 +
27993 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
27994 +{
27995 +       int err;
27996 +       struct dentry *d;
27997 +
27998 +       IMustLock(dir);
27999 +
28000 +       d = path->dentry;
28001 +       path->dentry = d->d_parent;
28002 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
28003 +       path->dentry = d;
28004 +       if (unlikely(err))
28005 +               goto out;
28006 +
28007 +       err = vfs_mknod(dir, path->dentry, mode, dev);
28008 +       if (!err) {
28009 +               struct path tmp = *path;
28010 +               int did;
28011 +
28012 +               vfsub_update_h_iattr(&tmp, &did);
28013 +               if (did) {
28014 +                       tmp.dentry = path->dentry->d_parent;
28015 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
28016 +               }
28017 +               /*ignore*/
28018 +       }
28019 +
28020 +out:
28021 +       return err;
28022 +}
28023 +
28024 +static int au_test_nlink(struct inode *inode)
28025 +{
28026 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
28027 +
28028 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
28029 +           || inode->i_nlink < link_max)
28030 +               return 0;
28031 +       return -EMLINK;
28032 +}
28033 +
28034 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
28035 +              struct inode **delegated_inode)
28036 +{
28037 +       int err;
28038 +       struct dentry *d;
28039 +
28040 +       IMustLock(dir);
28041 +
28042 +       err = au_test_nlink(src_dentry->d_inode);
28043 +       if (unlikely(err))
28044 +               return err;
28045 +
28046 +       /* we don't call may_linkat() */
28047 +       d = path->dentry;
28048 +       path->dentry = d->d_parent;
28049 +       err = security_path_link(src_dentry, path, d);
28050 +       path->dentry = d;
28051 +       if (unlikely(err))
28052 +               goto out;
28053 +
28054 +       lockdep_off();
28055 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
28056 +       lockdep_on();
28057 +       if (!err) {
28058 +               struct path tmp = *path;
28059 +               int did;
28060 +
28061 +               /* fuse has different memory inode for the same inumber */
28062 +               vfsub_update_h_iattr(&tmp, &did);
28063 +               if (did) {
28064 +                       tmp.dentry = path->dentry->d_parent;
28065 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
28066 +                       tmp.dentry = src_dentry;
28067 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
28068 +               }
28069 +               /*ignore*/
28070 +       }
28071 +
28072 +out:
28073 +       return err;
28074 +}
28075 +
28076 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
28077 +                struct inode *dir, struct path *path,
28078 +                struct inode **delegated_inode)
28079 +{
28080 +       int err;
28081 +       struct path tmp = {
28082 +               .mnt    = path->mnt
28083 +       };
28084 +       struct dentry *d;
28085 +
28086 +       IMustLock(dir);
28087 +       IMustLock(src_dir);
28088 +
28089 +       d = path->dentry;
28090 +       path->dentry = d->d_parent;
28091 +       tmp.dentry = src_dentry->d_parent;
28092 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
28093 +       path->dentry = d;
28094 +       if (unlikely(err))
28095 +               goto out;
28096 +
28097 +       lockdep_off();
28098 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
28099 +                        delegated_inode, /*flags*/0);
28100 +       lockdep_on();
28101 +       if (!err) {
28102 +               int did;
28103 +
28104 +               tmp.dentry = d->d_parent;
28105 +               vfsub_update_h_iattr(&tmp, &did);
28106 +               if (did) {
28107 +                       tmp.dentry = src_dentry;
28108 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
28109 +                       tmp.dentry = src_dentry->d_parent;
28110 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
28111 +               }
28112 +               /*ignore*/
28113 +       }
28114 +
28115 +out:
28116 +       return err;
28117 +}
28118 +
28119 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
28120 +{
28121 +       int err;
28122 +       struct dentry *d;
28123 +
28124 +       IMustLock(dir);
28125 +
28126 +       d = path->dentry;
28127 +       path->dentry = d->d_parent;
28128 +       err = security_path_mkdir(path, d, mode);
28129 +       path->dentry = d;
28130 +       if (unlikely(err))
28131 +               goto out;
28132 +
28133 +       err = vfs_mkdir(dir, path->dentry, mode);
28134 +       if (!err) {
28135 +               struct path tmp = *path;
28136 +               int did;
28137 +
28138 +               vfsub_update_h_iattr(&tmp, &did);
28139 +               if (did) {
28140 +                       tmp.dentry = path->dentry->d_parent;
28141 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
28142 +               }
28143 +               /*ignore*/
28144 +       }
28145 +
28146 +out:
28147 +       return err;
28148 +}
28149 +
28150 +int vfsub_rmdir(struct inode *dir, struct path *path)
28151 +{
28152 +       int err;
28153 +       struct dentry *d;
28154 +
28155 +       IMustLock(dir);
28156 +
28157 +       d = path->dentry;
28158 +       path->dentry = d->d_parent;
28159 +       err = security_path_rmdir(path, d);
28160 +       path->dentry = d;
28161 +       if (unlikely(err))
28162 +               goto out;
28163 +
28164 +       lockdep_off();
28165 +       err = vfs_rmdir(dir, path->dentry);
28166 +       lockdep_on();
28167 +       if (!err) {
28168 +               struct path tmp = {
28169 +                       .dentry = path->dentry->d_parent,
28170 +                       .mnt    = path->mnt
28171 +               };
28172 +
28173 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
28174 +       }
28175 +
28176 +out:
28177 +       return err;
28178 +}
28179 +
28180 +/* ---------------------------------------------------------------------- */
28181 +
28182 +/* todo: support mmap_sem? */
28183 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
28184 +                    loff_t *ppos)
28185 +{
28186 +       ssize_t err;
28187 +
28188 +       lockdep_off();
28189 +       err = vfs_read(file, ubuf, count, ppos);
28190 +       lockdep_on();
28191 +       if (err >= 0)
28192 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
28193 +       return err;
28194 +}
28195 +
28196 +/* todo: kernel_read()? */
28197 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
28198 +                    loff_t *ppos)
28199 +{
28200 +       ssize_t err;
28201 +       mm_segment_t oldfs;
28202 +       union {
28203 +               void *k;
28204 +               char __user *u;
28205 +       } buf;
28206 +
28207 +       buf.k = kbuf;
28208 +       oldfs = get_fs();
28209 +       set_fs(KERNEL_DS);
28210 +       err = vfsub_read_u(file, buf.u, count, ppos);
28211 +       set_fs(oldfs);
28212 +       return err;
28213 +}
28214 +
28215 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
28216 +                     loff_t *ppos)
28217 +{
28218 +       ssize_t err;
28219 +
28220 +       lockdep_off();
28221 +       err = vfs_write(file, ubuf, count, ppos);
28222 +       lockdep_on();
28223 +       if (err >= 0)
28224 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
28225 +       return err;
28226 +}
28227 +
28228 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
28229 +{
28230 +       ssize_t err;
28231 +       mm_segment_t oldfs;
28232 +       union {
28233 +               void *k;
28234 +               const char __user *u;
28235 +       } buf;
28236 +
28237 +       buf.k = kbuf;
28238 +       oldfs = get_fs();
28239 +       set_fs(KERNEL_DS);
28240 +       err = vfsub_write_u(file, buf.u, count, ppos);
28241 +       set_fs(oldfs);
28242 +       return err;
28243 +}
28244 +
28245 +int vfsub_flush(struct file *file, fl_owner_t id)
28246 +{
28247 +       int err;
28248 +
28249 +       err = 0;
28250 +       if (file->f_op->flush) {
28251 +               if (!au_test_nfs(file->f_dentry->d_sb))
28252 +                       err = file->f_op->flush(file, id);
28253 +               else {
28254 +                       lockdep_off();
28255 +                       err = file->f_op->flush(file, id);
28256 +                       lockdep_on();
28257 +               }
28258 +               if (!err)
28259 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
28260 +               /*ignore*/
28261 +       }
28262 +       return err;
28263 +}
28264 +
28265 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
28266 +{
28267 +       int err;
28268 +
28269 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
28270 +
28271 +       lockdep_off();
28272 +       err = iterate_dir(file, ctx);
28273 +       lockdep_on();
28274 +       if (err >= 0)
28275 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
28276 +       return err;
28277 +}
28278 +
28279 +long vfsub_splice_to(struct file *in, loff_t *ppos,
28280 +                    struct pipe_inode_info *pipe, size_t len,
28281 +                    unsigned int flags)
28282 +{
28283 +       long err;
28284 +
28285 +       lockdep_off();
28286 +       err = do_splice_to(in, ppos, pipe, len, flags);
28287 +       lockdep_on();
28288 +       file_accessed(in);
28289 +       if (err >= 0)
28290 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
28291 +       return err;
28292 +}
28293 +
28294 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
28295 +                      loff_t *ppos, size_t len, unsigned int flags)
28296 +{
28297 +       long err;
28298 +
28299 +       lockdep_off();
28300 +       err = do_splice_from(pipe, out, ppos, len, flags);
28301 +       lockdep_on();
28302 +       if (err >= 0)
28303 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
28304 +       return err;
28305 +}
28306 +
28307 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
28308 +{
28309 +       int err;
28310 +
28311 +       /* file can be NULL */
28312 +       lockdep_off();
28313 +       err = vfs_fsync(file, datasync);
28314 +       lockdep_on();
28315 +       if (!err) {
28316 +               if (!path) {
28317 +                       AuDebugOn(!file);
28318 +                       path = &file->f_path;
28319 +               }
28320 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
28321 +       }
28322 +       return err;
28323 +}
28324 +
28325 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
28326 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
28327 +               struct file *h_file)
28328 +{
28329 +       int err;
28330 +       struct inode *h_inode;
28331 +       struct super_block *h_sb;
28332 +
28333 +       if (!h_file) {
28334 +               err = vfsub_truncate(h_path, length);
28335 +               goto out;
28336 +       }
28337 +
28338 +       h_inode = h_path->dentry->d_inode;
28339 +       h_sb = h_inode->i_sb;
28340 +       lockdep_off();
28341 +       sb_start_write(h_sb);
28342 +       lockdep_on();
28343 +       err = locks_verify_truncate(h_inode, h_file, length);
28344 +       if (!err)
28345 +               err = security_path_truncate(h_path);
28346 +       if (!err) {
28347 +               lockdep_off();
28348 +               err = do_truncate(h_path->dentry, length, attr, h_file);
28349 +               lockdep_on();
28350 +       }
28351 +       lockdep_off();
28352 +       sb_end_write(h_sb);
28353 +       lockdep_on();
28354 +
28355 +out:
28356 +       return err;
28357 +}
28358 +
28359 +/* ---------------------------------------------------------------------- */
28360 +
28361 +struct au_vfsub_mkdir_args {
28362 +       int *errp;
28363 +       struct inode *dir;
28364 +       struct path *path;
28365 +       int mode;
28366 +};
28367 +
28368 +static void au_call_vfsub_mkdir(void *args)
28369 +{
28370 +       struct au_vfsub_mkdir_args *a = args;
28371 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
28372 +}
28373 +
28374 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
28375 +{
28376 +       int err, do_sio, wkq_err;
28377 +
28378 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
28379 +       if (!do_sio)
28380 +               err = vfsub_mkdir(dir, path, mode);
28381 +       else {
28382 +               struct au_vfsub_mkdir_args args = {
28383 +                       .errp   = &err,
28384 +                       .dir    = dir,
28385 +                       .path   = path,
28386 +                       .mode   = mode
28387 +               };
28388 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
28389 +               if (unlikely(wkq_err))
28390 +                       err = wkq_err;
28391 +       }
28392 +
28393 +       return err;
28394 +}
28395 +
28396 +struct au_vfsub_rmdir_args {
28397 +       int *errp;
28398 +       struct inode *dir;
28399 +       struct path *path;
28400 +};
28401 +
28402 +static void au_call_vfsub_rmdir(void *args)
28403 +{
28404 +       struct au_vfsub_rmdir_args *a = args;
28405 +       *a->errp = vfsub_rmdir(a->dir, a->path);
28406 +}
28407 +
28408 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
28409 +{
28410 +       int err, do_sio, wkq_err;
28411 +
28412 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
28413 +       if (!do_sio)
28414 +               err = vfsub_rmdir(dir, path);
28415 +       else {
28416 +               struct au_vfsub_rmdir_args args = {
28417 +                       .errp   = &err,
28418 +                       .dir    = dir,
28419 +                       .path   = path
28420 +               };
28421 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
28422 +               if (unlikely(wkq_err))
28423 +                       err = wkq_err;
28424 +       }
28425 +
28426 +       return err;
28427 +}
28428 +
28429 +/* ---------------------------------------------------------------------- */
28430 +
28431 +struct notify_change_args {
28432 +       int *errp;
28433 +       struct path *path;
28434 +       struct iattr *ia;
28435 +       struct inode **delegated_inode;
28436 +};
28437 +
28438 +static void call_notify_change(void *args)
28439 +{
28440 +       struct notify_change_args *a = args;
28441 +       struct inode *h_inode;
28442 +
28443 +       h_inode = a->path->dentry->d_inode;
28444 +       IMustLock(h_inode);
28445 +
28446 +       *a->errp = -EPERM;
28447 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
28448 +               *a->errp = notify_change(a->path->dentry, a->ia,
28449 +                                        a->delegated_inode);
28450 +               if (!*a->errp)
28451 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
28452 +       }
28453 +       AuTraceErr(*a->errp);
28454 +}
28455 +
28456 +int vfsub_notify_change(struct path *path, struct iattr *ia,
28457 +                       struct inode **delegated_inode)
28458 +{
28459 +       int err;
28460 +       struct notify_change_args args = {
28461 +               .errp                   = &err,
28462 +               .path                   = path,
28463 +               .ia                     = ia,
28464 +               .delegated_inode        = delegated_inode
28465 +       };
28466 +
28467 +       call_notify_change(&args);
28468 +
28469 +       return err;
28470 +}
28471 +
28472 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
28473 +                           struct inode **delegated_inode)
28474 +{
28475 +       int err, wkq_err;
28476 +       struct notify_change_args args = {
28477 +               .errp                   = &err,
28478 +               .path                   = path,
28479 +               .ia                     = ia,
28480 +               .delegated_inode        = delegated_inode
28481 +       };
28482 +
28483 +       wkq_err = au_wkq_wait(call_notify_change, &args);
28484 +       if (unlikely(wkq_err))
28485 +               err = wkq_err;
28486 +
28487 +       return err;
28488 +}
28489 +
28490 +/* ---------------------------------------------------------------------- */
28491 +
28492 +struct unlink_args {
28493 +       int *errp;
28494 +       struct inode *dir;
28495 +       struct path *path;
28496 +       struct inode **delegated_inode;
28497 +};
28498 +
28499 +static void call_unlink(void *args)
28500 +{
28501 +       struct unlink_args *a = args;
28502 +       struct dentry *d = a->path->dentry;
28503 +       struct inode *h_inode;
28504 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
28505 +                                     && d_count(d) == 1);
28506 +
28507 +       IMustLock(a->dir);
28508 +
28509 +       a->path->dentry = d->d_parent;
28510 +       *a->errp = security_path_unlink(a->path, d);
28511 +       a->path->dentry = d;
28512 +       if (unlikely(*a->errp))
28513 +               return;
28514 +
28515 +       if (!stop_sillyrename)
28516 +               dget(d);
28517 +       h_inode = d->d_inode;
28518 +       if (h_inode)
28519 +               ihold(h_inode);
28520 +
28521 +       lockdep_off();
28522 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
28523 +       lockdep_on();
28524 +       if (!*a->errp) {
28525 +               struct path tmp = {
28526 +                       .dentry = d->d_parent,
28527 +                       .mnt    = a->path->mnt
28528 +               };
28529 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
28530 +       }
28531 +
28532 +       if (!stop_sillyrename)
28533 +               dput(d);
28534 +       if (h_inode)
28535 +               iput(h_inode);
28536 +
28537 +       AuTraceErr(*a->errp);
28538 +}
28539 +
28540 +/*
28541 + * @dir: must be locked.
28542 + * @dentry: target dentry.
28543 + */
28544 +int vfsub_unlink(struct inode *dir, struct path *path,
28545 +                struct inode **delegated_inode, int force)
28546 +{
28547 +       int err;
28548 +       struct unlink_args args = {
28549 +               .errp                   = &err,
28550 +               .dir                    = dir,
28551 +               .path                   = path,
28552 +               .delegated_inode        = delegated_inode
28553 +       };
28554 +
28555 +       if (!force)
28556 +               call_unlink(&args);
28557 +       else {
28558 +               int wkq_err;
28559 +
28560 +               wkq_err = au_wkq_wait(call_unlink, &args);
28561 +               if (unlikely(wkq_err))
28562 +                       err = wkq_err;
28563 +       }
28564 +
28565 +       return err;
28566 +}
28567 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
28568 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
28569 +++ linux/fs/aufs/vfsub.h       2014-06-24 00:14:06.789386337 +0200
28570 @@ -0,0 +1,282 @@
28571 +/*
28572 + * Copyright (C) 2005-2014 Junjiro R. Okajima
28573 + *
28574 + * This program, aufs is free software; you can redistribute it and/or modify
28575 + * it under the terms of the GNU General Public License as published by
28576 + * the Free Software Foundation; either version 2 of the License, or
28577 + * (at your option) any later version.
28578 + *
28579 + * This program is distributed in the hope that it will be useful,
28580 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28581 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28582 + * GNU General Public License for more details.
28583 + *
28584 + * You should have received a copy of the GNU General Public License
28585 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28586 + */
28587 +
28588 +/*
28589 + * sub-routines for VFS
28590 + */
28591 +
28592 +#ifndef __AUFS_VFSUB_H__
28593 +#define __AUFS_VFSUB_H__
28594 +
28595 +#ifdef __KERNEL__
28596 +
28597 +#include <linux/fs.h>
28598 +#include <linux/lglock.h>
28599 +#include <linux/mount.h>
28600 +#include "debug.h"
28601 +
28602 +/* copied from linux/fs/internal.h */
28603 +/* todo: BAD approach!! */
28604 +extern void __mnt_drop_write(struct vfsmount *);
28605 +extern spinlock_t inode_sb_list_lock;
28606 +
28607 +/* ---------------------------------------------------------------------- */
28608 +
28609 +/* lock subclass for lower inode */
28610 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
28611 +/* reduce? gave up. */
28612 +enum {
28613 +       AuLsc_I_Begin = I_MUTEX_NONDIR2, /* 4 */
28614 +       AuLsc_I_PARENT,         /* lower inode, parent first */
28615 +       AuLsc_I_PARENT2,        /* copyup dirs */
28616 +       AuLsc_I_PARENT3,        /* copyup wh */
28617 +       AuLsc_I_CHILD,
28618 +       AuLsc_I_CHILD2,
28619 +       AuLsc_I_End
28620 +};
28621 +
28622 +/* to debug easier, do not make them inlined functions */
28623 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
28624 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
28625 +
28626 +/* ---------------------------------------------------------------------- */
28627 +
28628 +static inline void vfsub_drop_nlink(struct inode *inode)
28629 +{
28630 +       AuDebugOn(!inode->i_nlink);
28631 +       drop_nlink(inode);
28632 +}
28633 +
28634 +static inline void vfsub_dead_dir(struct inode *inode)
28635 +{
28636 +       AuDebugOn(!S_ISDIR(inode->i_mode));
28637 +       inode->i_flags |= S_DEAD;
28638 +       clear_nlink(inode);
28639 +}
28640 +
28641 +static inline int vfsub_native_ro(struct inode *inode)
28642 +{
28643 +       return (inode->i_sb->s_flags & MS_RDONLY)
28644 +               || IS_RDONLY(inode)
28645 +               /* || IS_APPEND(inode) */
28646 +               || IS_IMMUTABLE(inode);
28647 +}
28648 +
28649 +/* ---------------------------------------------------------------------- */
28650 +
28651 +int vfsub_update_h_iattr(struct path *h_path, int *did);
28652 +struct file *vfsub_dentry_open(struct path *path, int flags);
28653 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
28654 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
28655 +
28656 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
28657 +                                   int len);
28658 +
28659 +struct vfsub_lkup_one_args {
28660 +       struct dentry **errp;
28661 +       struct qstr *name;
28662 +       struct dentry *parent;
28663 +};
28664 +
28665 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
28666 +                                           struct dentry *parent)
28667 +{
28668 +       return vfsub_lookup_one_len(name->name, parent, name->len);
28669 +}
28670 +
28671 +void vfsub_call_lkup_one(void *args);
28672 +
28673 +/* ---------------------------------------------------------------------- */
28674 +
28675 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
28676 +{
28677 +       int err;
28678 +       lockdep_off();
28679 +       err = mnt_want_write(mnt);
28680 +       lockdep_on();
28681 +       return err;
28682 +}
28683 +
28684 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
28685 +{
28686 +       lockdep_off();
28687 +       mnt_drop_write(mnt);
28688 +       lockdep_on();
28689 +}
28690 +
28691 +static inline void vfsub_mnt_drop_write_file(struct file *file)
28692 +{
28693 +       lockdep_off();
28694 +       mnt_drop_write_file(file);
28695 +       lockdep_on();
28696 +}
28697 +
28698 +/* ---------------------------------------------------------------------- */
28699 +
28700 +struct au_hinode;
28701 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
28702 +                                struct dentry *d2, struct au_hinode *hdir2);
28703 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
28704 +                        struct dentry *d2, struct au_hinode *hdir2);
28705 +
28706 +int vfsub_create(struct inode *dir, struct path *path, int mode,
28707 +                bool want_excl);
28708 +int vfsub_symlink(struct inode *dir, struct path *path,
28709 +                 const char *symname);
28710 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
28711 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
28712 +              struct path *path, struct inode **delegated_inode);
28713 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
28714 +                struct inode *hdir, struct path *path,
28715 +                struct inode **delegated_inode);
28716 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
28717 +int vfsub_rmdir(struct inode *dir, struct path *path);
28718 +
28719 +/* ---------------------------------------------------------------------- */
28720 +
28721 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
28722 +                    loff_t *ppos);
28723 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
28724 +                       loff_t *ppos);
28725 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
28726 +                     loff_t *ppos);
28727 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
28728 +                     loff_t *ppos);
28729 +int vfsub_flush(struct file *file, fl_owner_t id);
28730 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
28731 +
28732 +/* just for type-check */
28733 +static inline filldir_t au_diractor(int (*func)(struct dir_context *,
28734 +                                               const char *, int, loff_t, u64,
28735 +                                               unsigned))
28736 +{
28737 +       return (filldir_t)func;
28738 +}
28739 +
28740 +
28741 +static inline loff_t vfsub_f_size_read(struct file *file)
28742 +{
28743 +       return i_size_read(file_inode(file));
28744 +}
28745 +
28746 +static inline unsigned int vfsub_file_flags(struct file *file)
28747 +{
28748 +       unsigned int flags;
28749 +
28750 +       spin_lock(&file->f_lock);
28751 +       flags = file->f_flags;
28752 +       spin_unlock(&file->f_lock);
28753 +
28754 +       return flags;
28755 +}
28756 +
28757 +static inline void vfsub_file_accessed(struct file *h_file)
28758 +{
28759 +       file_accessed(h_file);
28760 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
28761 +}
28762 +
28763 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
28764 +                                    struct dentry *h_dentry)
28765 +{
28766 +       struct path h_path = {
28767 +               .dentry = h_dentry,
28768 +               .mnt    = h_mnt
28769 +       };
28770 +       touch_atime(&h_path);
28771 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
28772 +}
28773 +
28774 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
28775 +                                   int flags)
28776 +{
28777 +       return update_time(h_inode, ts, flags);
28778 +       /* no vfsub_update_h_iattr() since we don't have struct path */
28779 +}
28780 +
28781 +long vfsub_splice_to(struct file *in, loff_t *ppos,
28782 +                    struct pipe_inode_info *pipe, size_t len,
28783 +                    unsigned int flags);
28784 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
28785 +                      loff_t *ppos, size_t len, unsigned int flags);
28786 +
28787 +static inline long vfsub_truncate(struct path *path, loff_t length)
28788 +{
28789 +       long err;
28790 +       lockdep_off();
28791 +       err = vfs_truncate(path, length);
28792 +       lockdep_on();
28793 +       return err;
28794 +}
28795 +
28796 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
28797 +               struct file *h_file);
28798 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
28799 +
28800 +/* ---------------------------------------------------------------------- */
28801 +
28802 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
28803 +{
28804 +       loff_t err;
28805 +
28806 +       lockdep_off();
28807 +       err = vfs_llseek(file, offset, origin);
28808 +       lockdep_on();
28809 +       return err;
28810 +}
28811 +
28812 +/* ---------------------------------------------------------------------- */
28813 +
28814 +/* dirty workaround for strict type of fmode_t */
28815 +union vfsub_fmu {
28816 +       fmode_t fm;
28817 +       unsigned int ui;
28818 +};
28819 +
28820 +static inline unsigned int vfsub_fmode_to_uint(fmode_t fm)
28821 +{
28822 +       union vfsub_fmu u = {
28823 +               .fm = fm
28824 +       };
28825 +
28826 +       BUILD_BUG_ON(sizeof(u.fm) != sizeof(u.ui));
28827 +
28828 +       return u.ui;
28829 +}
28830 +
28831 +static inline fmode_t vfsub_uint_to_fmode(unsigned int ui)
28832 +{
28833 +       union vfsub_fmu u = {
28834 +               .ui = ui
28835 +       };
28836 +
28837 +       return u.fm;
28838 +}
28839 +
28840 +/* ---------------------------------------------------------------------- */
28841 +
28842 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
28843 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
28844 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
28845 +                           struct inode **delegated_inode);
28846 +int vfsub_notify_change(struct path *path, struct iattr *ia,
28847 +                       struct inode **delegated_inode);
28848 +int vfsub_unlink(struct inode *dir, struct path *path,
28849 +                struct inode **delegated_inode, int force);
28850 +
28851 +#endif /* __KERNEL__ */
28852 +#endif /* __AUFS_VFSUB_H__ */
28853 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
28854 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
28855 +++ linux/fs/aufs/wbr_policy.c  2014-06-24 00:14:06.789386337 +0200
28856 @@ -0,0 +1,756 @@
28857 +/*
28858 + * Copyright (C) 2005-2014 Junjiro R. Okajima
28859 + *
28860 + * This program, aufs is free software; you can redistribute it and/or modify
28861 + * it under the terms of the GNU General Public License as published by
28862 + * the Free Software Foundation; either version 2 of the License, or
28863 + * (at your option) any later version.
28864 + *
28865 + * This program is distributed in the hope that it will be useful,
28866 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28867 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28868 + * GNU General Public License for more details.
28869 + *
28870 + * You should have received a copy of the GNU General Public License
28871 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28872 + */
28873 +
28874 +/*
28875 + * policies for selecting one among multiple writable branches
28876 + */
28877 +
28878 +#include <linux/statfs.h>
28879 +#include "aufs.h"
28880 +
28881 +/* subset of cpup_attr() */
28882 +static noinline_for_stack
28883 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
28884 +{
28885 +       int err, sbits;
28886 +       struct iattr ia;
28887 +       struct inode *h_isrc;
28888 +
28889 +       h_isrc = h_src->d_inode;
28890 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
28891 +       ia.ia_mode = h_isrc->i_mode;
28892 +       ia.ia_uid = h_isrc->i_uid;
28893 +       ia.ia_gid = h_isrc->i_gid;
28894 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
28895 +       au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc->i_flags);
28896 +       /* no delegation since it is just created */
28897 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
28898 +
28899 +       /* is this nfs only? */
28900 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
28901 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
28902 +               ia.ia_mode = h_isrc->i_mode;
28903 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
28904 +       }
28905 +
28906 +       return err;
28907 +}
28908 +
28909 +#define AuCpdown_PARENT_OPQ    1
28910 +#define AuCpdown_WHED          (1 << 1)
28911 +#define AuCpdown_MADE_DIR      (1 << 2)
28912 +#define AuCpdown_DIROPQ                (1 << 3)
28913 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
28914 +#define au_fset_cpdown(flags, name) \
28915 +       do { (flags) |= AuCpdown_##name; } while (0)
28916 +#define au_fclr_cpdown(flags, name) \
28917 +       do { (flags) &= ~AuCpdown_##name; } while (0)
28918 +
28919 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
28920 +                            unsigned int *flags)
28921 +{
28922 +       int err;
28923 +       struct dentry *opq_dentry;
28924 +
28925 +       opq_dentry = au_diropq_create(dentry, bdst);
28926 +       err = PTR_ERR(opq_dentry);
28927 +       if (IS_ERR(opq_dentry))
28928 +               goto out;
28929 +       dput(opq_dentry);
28930 +       au_fset_cpdown(*flags, DIROPQ);
28931 +
28932 +out:
28933 +       return err;
28934 +}
28935 +
28936 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
28937 +                           struct inode *dir, aufs_bindex_t bdst)
28938 +{
28939 +       int err;
28940 +       struct path h_path;
28941 +       struct au_branch *br;
28942 +
28943 +       br = au_sbr(dentry->d_sb, bdst);
28944 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
28945 +       err = PTR_ERR(h_path.dentry);
28946 +       if (IS_ERR(h_path.dentry))
28947 +               goto out;
28948 +
28949 +       err = 0;
28950 +       if (h_path.dentry->d_inode) {
28951 +               h_path.mnt = au_br_mnt(br);
28952 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
28953 +                                         dentry);
28954 +       }
28955 +       dput(h_path.dentry);
28956 +
28957 +out:
28958 +       return err;
28959 +}
28960 +
28961 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
28962 +                        struct au_pin *pin,
28963 +                        struct dentry *h_parent, void *arg)
28964 +{
28965 +       int err, rerr;
28966 +       aufs_bindex_t bopq, bstart;
28967 +       struct path h_path;
28968 +       struct dentry *parent;
28969 +       struct inode *h_dir, *h_inode, *inode, *dir;
28970 +       unsigned int *flags = arg;
28971 +
28972 +       bstart = au_dbstart(dentry);
28973 +       /* dentry is di-locked */
28974 +       parent = dget_parent(dentry);
28975 +       dir = parent->d_inode;
28976 +       h_dir = h_parent->d_inode;
28977 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
28978 +       IMustLock(h_dir);
28979 +
28980 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
28981 +       if (unlikely(err < 0))
28982 +               goto out;
28983 +       h_path.dentry = au_h_dptr(dentry, bdst);
28984 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
28985 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
28986 +                             S_IRWXU | S_IRUGO | S_IXUGO);
28987 +       if (unlikely(err))
28988 +               goto out_put;
28989 +       au_fset_cpdown(*flags, MADE_DIR);
28990 +
28991 +       bopq = au_dbdiropq(dentry);
28992 +       au_fclr_cpdown(*flags, WHED);
28993 +       au_fclr_cpdown(*flags, DIROPQ);
28994 +       if (au_dbwh(dentry) == bdst)
28995 +               au_fset_cpdown(*flags, WHED);
28996 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
28997 +               au_fset_cpdown(*flags, PARENT_OPQ);
28998 +       h_inode = h_path.dentry->d_inode;
28999 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
29000 +       if (au_ftest_cpdown(*flags, WHED)) {
29001 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
29002 +               if (unlikely(err)) {
29003 +                       mutex_unlock(&h_inode->i_mutex);
29004 +                       goto out_dir;
29005 +               }
29006 +       }
29007 +
29008 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
29009 +       mutex_unlock(&h_inode->i_mutex);
29010 +       if (unlikely(err))
29011 +               goto out_opq;
29012 +
29013 +       if (au_ftest_cpdown(*flags, WHED)) {
29014 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
29015 +               if (unlikely(err))
29016 +                       goto out_opq;
29017 +       }
29018 +
29019 +       inode = dentry->d_inode;
29020 +       if (au_ibend(inode) < bdst)
29021 +               au_set_ibend(inode, bdst);
29022 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
29023 +                     au_hi_flags(inode, /*isdir*/1));
29024 +       goto out; /* success */
29025 +
29026 +       /* revert */
29027 +out_opq:
29028 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
29029 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
29030 +               rerr = au_diropq_remove(dentry, bdst);
29031 +               mutex_unlock(&h_inode->i_mutex);
29032 +               if (unlikely(rerr)) {
29033 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
29034 +                               dentry, bdst, rerr);
29035 +                       err = -EIO;
29036 +                       goto out;
29037 +               }
29038 +       }
29039 +out_dir:
29040 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
29041 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
29042 +               if (unlikely(rerr)) {
29043 +                       AuIOErr("failed removing %pd b%d (%d)\n",
29044 +                               dentry, bdst, rerr);
29045 +                       err = -EIO;
29046 +               }
29047 +       }
29048 +out_put:
29049 +       au_set_h_dptr(dentry, bdst, NULL);
29050 +       if (au_dbend(dentry) == bdst)
29051 +               au_update_dbend(dentry);
29052 +out:
29053 +       dput(parent);
29054 +       return err;
29055 +}
29056 +
29057 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
29058 +{
29059 +       int err;
29060 +       unsigned int flags;
29061 +
29062 +       flags = 0;
29063 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
29064 +
29065 +       return err;
29066 +}
29067 +
29068 +/* ---------------------------------------------------------------------- */
29069 +
29070 +/* policies for create */
29071 +
29072 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
29073 +{
29074 +       int err, i, j, ndentry;
29075 +       aufs_bindex_t bopq;
29076 +       struct au_dcsub_pages dpages;
29077 +       struct au_dpage *dpage;
29078 +       struct dentry **dentries, *parent, *d;
29079 +
29080 +       err = au_dpages_init(&dpages, GFP_NOFS);
29081 +       if (unlikely(err))
29082 +               goto out;
29083 +       parent = dget_parent(dentry);
29084 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
29085 +       if (unlikely(err))
29086 +               goto out_free;
29087 +
29088 +       err = bindex;
29089 +       for (i = 0; i < dpages.ndpage; i++) {
29090 +               dpage = dpages.dpages + i;
29091 +               dentries = dpage->dentries;
29092 +               ndentry = dpage->ndentry;
29093 +               for (j = 0; j < ndentry; j++) {
29094 +                       d = dentries[j];
29095 +                       di_read_lock_parent2(d, !AuLock_IR);
29096 +                       bopq = au_dbdiropq(d);
29097 +                       di_read_unlock(d, !AuLock_IR);
29098 +                       if (bopq >= 0 && bopq < err)
29099 +                               err = bopq;
29100 +               }
29101 +       }
29102 +
29103 +out_free:
29104 +       dput(parent);
29105 +       au_dpages_free(&dpages);
29106 +out:
29107 +       return err;
29108 +}
29109 +
29110 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
29111 +{
29112 +       for (; bindex >= 0; bindex--)
29113 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
29114 +                       return bindex;
29115 +       return -EROFS;
29116 +}
29117 +
29118 +/* top down parent */
29119 +static int au_wbr_create_tdp(struct dentry *dentry,
29120 +                            unsigned int flags __maybe_unused)
29121 +{
29122 +       int err;
29123 +       aufs_bindex_t bstart, bindex;
29124 +       struct super_block *sb;
29125 +       struct dentry *parent, *h_parent;
29126 +
29127 +       sb = dentry->d_sb;
29128 +       bstart = au_dbstart(dentry);
29129 +       err = bstart;
29130 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
29131 +               goto out;
29132 +
29133 +       err = -EROFS;
29134 +       parent = dget_parent(dentry);
29135 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
29136 +               h_parent = au_h_dptr(parent, bindex);
29137 +               if (!h_parent || !h_parent->d_inode)
29138 +                       continue;
29139 +
29140 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
29141 +                       err = bindex;
29142 +                       break;
29143 +               }
29144 +       }
29145 +       dput(parent);
29146 +
29147 +       /* bottom up here */
29148 +       if (unlikely(err < 0)) {
29149 +               err = au_wbr_bu(sb, bstart - 1);
29150 +               if (err >= 0)
29151 +                       err = au_wbr_nonopq(dentry, err);
29152 +       }
29153 +
29154 +out:
29155 +       AuDbg("b%d\n", err);
29156 +       return err;
29157 +}
29158 +
29159 +/* ---------------------------------------------------------------------- */
29160 +
29161 +/* an exception for the policy other than tdp */
29162 +static int au_wbr_create_exp(struct dentry *dentry)
29163 +{
29164 +       int err;
29165 +       aufs_bindex_t bwh, bdiropq;
29166 +       struct dentry *parent;
29167 +
29168 +       err = -1;
29169 +       bwh = au_dbwh(dentry);
29170 +       parent = dget_parent(dentry);
29171 +       bdiropq = au_dbdiropq(parent);
29172 +       if (bwh >= 0) {
29173 +               if (bdiropq >= 0)
29174 +                       err = min(bdiropq, bwh);
29175 +               else
29176 +                       err = bwh;
29177 +               AuDbg("%d\n", err);
29178 +       } else if (bdiropq >= 0) {
29179 +               err = bdiropq;
29180 +               AuDbg("%d\n", err);
29181 +       }
29182 +       dput(parent);
29183 +
29184 +       if (err >= 0)
29185 +               err = au_wbr_nonopq(dentry, err);
29186 +
29187 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
29188 +               err = -1;
29189 +
29190 +       AuDbg("%d\n", err);
29191 +       return err;
29192 +}
29193 +
29194 +/* ---------------------------------------------------------------------- */
29195 +
29196 +/* round robin */
29197 +static int au_wbr_create_init_rr(struct super_block *sb)
29198 +{
29199 +       int err;
29200 +
29201 +       err = au_wbr_bu(sb, au_sbend(sb));
29202 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
29203 +       /* smp_mb(); */
29204 +
29205 +       AuDbg("b%d\n", err);
29206 +       return err;
29207 +}
29208 +
29209 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
29210 +{
29211 +       int err, nbr;
29212 +       unsigned int u;
29213 +       aufs_bindex_t bindex, bend;
29214 +       struct super_block *sb;
29215 +       atomic_t *next;
29216 +
29217 +       err = au_wbr_create_exp(dentry);
29218 +       if (err >= 0)
29219 +               goto out;
29220 +
29221 +       sb = dentry->d_sb;
29222 +       next = &au_sbi(sb)->si_wbr_rr_next;
29223 +       bend = au_sbend(sb);
29224 +       nbr = bend + 1;
29225 +       for (bindex = 0; bindex <= bend; bindex++) {
29226 +               if (!au_ftest_wbr(flags, DIR)) {
29227 +                       err = atomic_dec_return(next) + 1;
29228 +                       /* modulo for 0 is meaningless */
29229 +                       if (unlikely(!err))
29230 +                               err = atomic_dec_return(next) + 1;
29231 +               } else
29232 +                       err = atomic_read(next);
29233 +               AuDbg("%d\n", err);
29234 +               u = err;
29235 +               err = u % nbr;
29236 +               AuDbg("%d\n", err);
29237 +               if (!au_br_rdonly(au_sbr(sb, err)))
29238 +                       break;
29239 +               err = -EROFS;
29240 +       }
29241 +
29242 +       if (err >= 0)
29243 +               err = au_wbr_nonopq(dentry, err);
29244 +
29245 +out:
29246 +       AuDbg("%d\n", err);
29247 +       return err;
29248 +}
29249 +
29250 +/* ---------------------------------------------------------------------- */
29251 +
29252 +/* most free space */
29253 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
29254 +{
29255 +       struct super_block *sb;
29256 +       struct au_branch *br;
29257 +       struct au_wbr_mfs *mfs;
29258 +       struct dentry *h_parent;
29259 +       aufs_bindex_t bindex, bend;
29260 +       int err;
29261 +       unsigned long long b, bavail;
29262 +       struct path h_path;
29263 +       /* reduce the stack usage */
29264 +       struct kstatfs *st;
29265 +
29266 +       st = kmalloc(sizeof(*st), GFP_NOFS);
29267 +       if (unlikely(!st)) {
29268 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
29269 +               return;
29270 +       }
29271 +
29272 +       bavail = 0;
29273 +       sb = dentry->d_sb;
29274 +       mfs = &au_sbi(sb)->si_wbr_mfs;
29275 +       MtxMustLock(&mfs->mfs_lock);
29276 +       mfs->mfs_bindex = -EROFS;
29277 +       mfs->mfsrr_bytes = 0;
29278 +       if (!parent) {
29279 +               bindex = 0;
29280 +               bend = au_sbend(sb);
29281 +       } else {
29282 +               bindex = au_dbstart(parent);
29283 +               bend = au_dbtaildir(parent);
29284 +       }
29285 +
29286 +       for (; bindex <= bend; bindex++) {
29287 +               if (parent) {
29288 +                       h_parent = au_h_dptr(parent, bindex);
29289 +                       if (!h_parent || !h_parent->d_inode)
29290 +                               continue;
29291 +               }
29292 +               br = au_sbr(sb, bindex);
29293 +               if (au_br_rdonly(br))
29294 +                       continue;
29295 +
29296 +               /* sb->s_root for NFS is unreliable */
29297 +               h_path.mnt = au_br_mnt(br);
29298 +               h_path.dentry = h_path.mnt->mnt_root;
29299 +               err = vfs_statfs(&h_path, st);
29300 +               if (unlikely(err)) {
29301 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
29302 +                       continue;
29303 +               }
29304 +
29305 +               /* when the available size is equal, select the lower one */
29306 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
29307 +                            || sizeof(b) < sizeof(st->f_bsize));
29308 +               b = st->f_bavail * st->f_bsize;
29309 +               br->br_wbr->wbr_bytes = b;
29310 +               if (b >= bavail) {
29311 +                       bavail = b;
29312 +                       mfs->mfs_bindex = bindex;
29313 +                       mfs->mfs_jiffy = jiffies;
29314 +               }
29315 +       }
29316 +
29317 +       mfs->mfsrr_bytes = bavail;
29318 +       AuDbg("b%d\n", mfs->mfs_bindex);
29319 +       kfree(st);
29320 +}
29321 +
29322 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
29323 +{
29324 +       int err;
29325 +       struct dentry *parent;
29326 +       struct super_block *sb;
29327 +       struct au_wbr_mfs *mfs;
29328 +
29329 +       err = au_wbr_create_exp(dentry);
29330 +       if (err >= 0)
29331 +               goto out;
29332 +
29333 +       sb = dentry->d_sb;
29334 +       parent = NULL;
29335 +       if (au_ftest_wbr(flags, PARENT))
29336 +               parent = dget_parent(dentry);
29337 +       mfs = &au_sbi(sb)->si_wbr_mfs;
29338 +       mutex_lock(&mfs->mfs_lock);
29339 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
29340 +           || mfs->mfs_bindex < 0
29341 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
29342 +               au_mfs(dentry, parent);
29343 +       mutex_unlock(&mfs->mfs_lock);
29344 +       err = mfs->mfs_bindex;
29345 +       dput(parent);
29346 +
29347 +       if (err >= 0)
29348 +               err = au_wbr_nonopq(dentry, err);
29349 +
29350 +out:
29351 +       AuDbg("b%d\n", err);
29352 +       return err;
29353 +}
29354 +
29355 +static int au_wbr_create_init_mfs(struct super_block *sb)
29356 +{
29357 +       struct au_wbr_mfs *mfs;
29358 +
29359 +       mfs = &au_sbi(sb)->si_wbr_mfs;
29360 +       mutex_init(&mfs->mfs_lock);
29361 +       mfs->mfs_jiffy = 0;
29362 +       mfs->mfs_bindex = -EROFS;
29363 +
29364 +       return 0;
29365 +}
29366 +
29367 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
29368 +{
29369 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
29370 +       return 0;
29371 +}
29372 +
29373 +/* ---------------------------------------------------------------------- */
29374 +
29375 +/* most free space and then round robin */
29376 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
29377 +{
29378 +       int err;
29379 +       struct au_wbr_mfs *mfs;
29380 +
29381 +       err = au_wbr_create_mfs(dentry, flags);
29382 +       if (err >= 0) {
29383 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
29384 +               mutex_lock(&mfs->mfs_lock);
29385 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
29386 +                       err = au_wbr_create_rr(dentry, flags);
29387 +               mutex_unlock(&mfs->mfs_lock);
29388 +       }
29389 +
29390 +       AuDbg("b%d\n", err);
29391 +       return err;
29392 +}
29393 +
29394 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
29395 +{
29396 +       int err;
29397 +
29398 +       au_wbr_create_init_mfs(sb); /* ignore */
29399 +       err = au_wbr_create_init_rr(sb);
29400 +
29401 +       return err;
29402 +}
29403 +
29404 +/* ---------------------------------------------------------------------- */
29405 +
29406 +/* top down parent and most free space */
29407 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
29408 +{
29409 +       int err, e2;
29410 +       unsigned long long b;
29411 +       aufs_bindex_t bindex, bstart, bend;
29412 +       struct super_block *sb;
29413 +       struct dentry *parent, *h_parent;
29414 +       struct au_branch *br;
29415 +
29416 +       err = au_wbr_create_tdp(dentry, flags);
29417 +       if (unlikely(err < 0))
29418 +               goto out;
29419 +       parent = dget_parent(dentry);
29420 +       bstart = au_dbstart(parent);
29421 +       bend = au_dbtaildir(parent);
29422 +       if (bstart == bend)
29423 +               goto out_parent; /* success */
29424 +
29425 +       e2 = au_wbr_create_mfs(dentry, flags);
29426 +       if (e2 < 0)
29427 +               goto out_parent; /* success */
29428 +
29429 +       /* when the available size is equal, select upper one */
29430 +       sb = dentry->d_sb;
29431 +       br = au_sbr(sb, err);
29432 +       b = br->br_wbr->wbr_bytes;
29433 +       AuDbg("b%d, %llu\n", err, b);
29434 +
29435 +       for (bindex = bstart; bindex <= bend; bindex++) {
29436 +               h_parent = au_h_dptr(parent, bindex);
29437 +               if (!h_parent || !h_parent->d_inode)
29438 +                       continue;
29439 +
29440 +               br = au_sbr(sb, bindex);
29441 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
29442 +                       b = br->br_wbr->wbr_bytes;
29443 +                       err = bindex;
29444 +                       AuDbg("b%d, %llu\n", err, b);
29445 +               }
29446 +       }
29447 +
29448 +       if (err >= 0)
29449 +               err = au_wbr_nonopq(dentry, err);
29450 +
29451 +out_parent:
29452 +       dput(parent);
29453 +out:
29454 +       AuDbg("b%d\n", err);
29455 +       return err;
29456 +}
29457 +
29458 +/* ---------------------------------------------------------------------- */
29459 +
29460 +/*
29461 + * - top down parent
29462 + * - most free space with parent
29463 + * - most free space round-robin regardless parent
29464 + */
29465 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
29466 +{
29467 +       int err;
29468 +       unsigned long long watermark;
29469 +       struct super_block *sb;
29470 +       struct au_branch *br;
29471 +       struct au_wbr_mfs *mfs;
29472 +
29473 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
29474 +       if (unlikely(err < 0))
29475 +               goto out;
29476 +
29477 +       sb = dentry->d_sb;
29478 +       br = au_sbr(sb, err);
29479 +       mfs = &au_sbi(sb)->si_wbr_mfs;
29480 +       mutex_lock(&mfs->mfs_lock);
29481 +       watermark = mfs->mfsrr_watermark;
29482 +       mutex_unlock(&mfs->mfs_lock);
29483 +       if (br->br_wbr->wbr_bytes < watermark)
29484 +               /* regardless the parent dir */
29485 +               err = au_wbr_create_mfsrr(dentry, flags);
29486 +
29487 +out:
29488 +       AuDbg("b%d\n", err);
29489 +       return err;
29490 +}
29491 +
29492 +/* ---------------------------------------------------------------------- */
29493 +
29494 +/* policies for copyup */
29495 +
29496 +/* top down parent */
29497 +static int au_wbr_copyup_tdp(struct dentry *dentry)
29498 +{
29499 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
29500 +}
29501 +
29502 +/* bottom up parent */
29503 +static int au_wbr_copyup_bup(struct dentry *dentry)
29504 +{
29505 +       int err;
29506 +       aufs_bindex_t bindex, bstart;
29507 +       struct dentry *parent, *h_parent;
29508 +       struct super_block *sb;
29509 +
29510 +       err = -EROFS;
29511 +       sb = dentry->d_sb;
29512 +       parent = dget_parent(dentry);
29513 +       bstart = au_dbstart(parent);
29514 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
29515 +               h_parent = au_h_dptr(parent, bindex);
29516 +               if (!h_parent || !h_parent->d_inode)
29517 +                       continue;
29518 +
29519 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
29520 +                       err = bindex;
29521 +                       break;
29522 +               }
29523 +       }
29524 +       dput(parent);
29525 +
29526 +       /* bottom up here */
29527 +       if (unlikely(err < 0))
29528 +               err = au_wbr_bu(sb, bstart - 1);
29529 +
29530 +       AuDbg("b%d\n", err);
29531 +       return err;
29532 +}
29533 +
29534 +/* bottom up */
29535 +static int au_wbr_copyup_bu(struct dentry *dentry)
29536 +{
29537 +       int err;
29538 +       aufs_bindex_t bstart;
29539 +
29540 +       bstart = au_dbstart(dentry);
29541 +       err = au_wbr_bu(dentry->d_sb, bstart);
29542 +       AuDbg("b%d\n", err);
29543 +       if (err > bstart)
29544 +               err = au_wbr_nonopq(dentry, err);
29545 +
29546 +       AuDbg("b%d\n", err);
29547 +       return err;
29548 +}
29549 +
29550 +/* ---------------------------------------------------------------------- */
29551 +
29552 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
29553 +       [AuWbrCopyup_TDP] = {
29554 +               .copyup = au_wbr_copyup_tdp
29555 +       },
29556 +       [AuWbrCopyup_BUP] = {
29557 +               .copyup = au_wbr_copyup_bup
29558 +       },
29559 +       [AuWbrCopyup_BU] = {
29560 +               .copyup = au_wbr_copyup_bu
29561 +       }
29562 +};
29563 +
29564 +struct au_wbr_create_operations au_wbr_create_ops[] = {
29565 +       [AuWbrCreate_TDP] = {
29566 +               .create = au_wbr_create_tdp
29567 +       },
29568 +       [AuWbrCreate_RR] = {
29569 +               .create = au_wbr_create_rr,
29570 +               .init   = au_wbr_create_init_rr
29571 +       },
29572 +       [AuWbrCreate_MFS] = {
29573 +               .create = au_wbr_create_mfs,
29574 +               .init   = au_wbr_create_init_mfs,
29575 +               .fin    = au_wbr_create_fin_mfs
29576 +       },
29577 +       [AuWbrCreate_MFSV] = {
29578 +               .create = au_wbr_create_mfs,
29579 +               .init   = au_wbr_create_init_mfs,
29580 +               .fin    = au_wbr_create_fin_mfs
29581 +       },
29582 +       [AuWbrCreate_MFSRR] = {
29583 +               .create = au_wbr_create_mfsrr,
29584 +               .init   = au_wbr_create_init_mfsrr,
29585 +               .fin    = au_wbr_create_fin_mfs
29586 +       },
29587 +       [AuWbrCreate_MFSRRV] = {
29588 +               .create = au_wbr_create_mfsrr,
29589 +               .init   = au_wbr_create_init_mfsrr,
29590 +               .fin    = au_wbr_create_fin_mfs
29591 +       },
29592 +       [AuWbrCreate_PMFS] = {
29593 +               .create = au_wbr_create_pmfs,
29594 +               .init   = au_wbr_create_init_mfs,
29595 +               .fin    = au_wbr_create_fin_mfs
29596 +       },
29597 +       [AuWbrCreate_PMFSV] = {
29598 +               .create = au_wbr_create_pmfs,
29599 +               .init   = au_wbr_create_init_mfs,
29600 +               .fin    = au_wbr_create_fin_mfs
29601 +       },
29602 +       [AuWbrCreate_PMFSRR] = {
29603 +               .create = au_wbr_create_pmfsrr,
29604 +               .init   = au_wbr_create_init_mfsrr,
29605 +               .fin    = au_wbr_create_fin_mfs
29606 +       },
29607 +       [AuWbrCreate_PMFSRRV] = {
29608 +               .create = au_wbr_create_pmfsrr,
29609 +               .init   = au_wbr_create_init_mfsrr,
29610 +               .fin    = au_wbr_create_fin_mfs
29611 +       }
29612 +};
29613 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
29614 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
29615 +++ linux/fs/aufs/whout.c       2014-06-24 00:14:06.789386337 +0200
29616 @@ -0,0 +1,1052 @@
29617 +/*
29618 + * Copyright (C) 2005-2014 Junjiro R. Okajima
29619 + *
29620 + * This program, aufs is free software; you can redistribute it and/or modify
29621 + * it under the terms of the GNU General Public License as published by
29622 + * the Free Software Foundation; either version 2 of the License, or
29623 + * (at your option) any later version.
29624 + *
29625 + * This program is distributed in the hope that it will be useful,
29626 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29627 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29628 + * GNU General Public License for more details.
29629 + *
29630 + * You should have received a copy of the GNU General Public License
29631 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29632 + */
29633 +
29634 +/*
29635 + * whiteout for logical deletion and opaque directory
29636 + */
29637 +
29638 +#include "aufs.h"
29639 +
29640 +#define WH_MASK                        S_IRUGO
29641 +
29642 +/*
29643 + * If a directory contains this file, then it is opaque.  We start with the
29644 + * .wh. flag so that it is blocked by lookup.
29645 + */
29646 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
29647 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
29648 +
29649 +/*
29650 + * generate whiteout name, which is NOT terminated by NULL.
29651 + * @name: original d_name.name
29652 + * @len: original d_name.len
29653 + * @wh: whiteout qstr
29654 + * returns zero when succeeds, otherwise error.
29655 + * succeeded value as wh->name should be freed by kfree().
29656 + */
29657 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
29658 +{
29659 +       char *p;
29660 +
29661 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
29662 +               return -ENAMETOOLONG;
29663 +
29664 +       wh->len = name->len + AUFS_WH_PFX_LEN;
29665 +       p = kmalloc(wh->len, GFP_NOFS);
29666 +       wh->name = p;
29667 +       if (p) {
29668 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29669 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
29670 +               /* smp_mb(); */
29671 +               return 0;
29672 +       }
29673 +       return -ENOMEM;
29674 +}
29675 +
29676 +/* ---------------------------------------------------------------------- */
29677 +
29678 +/*
29679 + * test if the @wh_name exists under @h_parent.
29680 + * @try_sio specifies the necessary of super-io.
29681 + */
29682 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
29683 +              struct au_branch *br, int try_sio)
29684 +{
29685 +       int err;
29686 +       struct dentry *wh_dentry;
29687 +
29688 +       if (!try_sio)
29689 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
29690 +       else
29691 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent, br);
29692 +       err = PTR_ERR(wh_dentry);
29693 +       if (IS_ERR(wh_dentry))
29694 +               goto out;
29695 +
29696 +       err = 0;
29697 +       if (!wh_dentry->d_inode)
29698 +               goto out_wh; /* success */
29699 +
29700 +       err = 1;
29701 +       if (S_ISREG(wh_dentry->d_inode->i_mode))
29702 +               goto out_wh; /* success */
29703 +
29704 +       err = -EIO;
29705 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
29706 +               wh_dentry, wh_dentry->d_inode->i_mode);
29707 +
29708 +out_wh:
29709 +       dput(wh_dentry);
29710 +out:
29711 +       return err;
29712 +}
29713 +
29714 +/*
29715 + * test if the @h_dentry sets opaque or not.
29716 + */
29717 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br)
29718 +{
29719 +       int err;
29720 +       struct inode *h_dir;
29721 +
29722 +       h_dir = h_dentry->d_inode;
29723 +       err = au_wh_test(h_dentry, &diropq_name, br,
29724 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
29725 +       return err;
29726 +}
29727 +
29728 +/*
29729 + * returns a negative dentry whose name is unique and temporary.
29730 + */
29731 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
29732 +                            struct qstr *prefix)
29733 +{
29734 +       struct dentry *dentry;
29735 +       int i;
29736 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
29737 +               *name, *p;
29738 +       /* strict atomic_t is unnecessary here */
29739 +       static unsigned short cnt;
29740 +       struct qstr qs;
29741 +
29742 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
29743 +
29744 +       name = defname;
29745 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
29746 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
29747 +               dentry = ERR_PTR(-ENAMETOOLONG);
29748 +               if (unlikely(qs.len > NAME_MAX))
29749 +                       goto out;
29750 +               dentry = ERR_PTR(-ENOMEM);
29751 +               name = kmalloc(qs.len + 1, GFP_NOFS);
29752 +               if (unlikely(!name))
29753 +                       goto out;
29754 +       }
29755 +
29756 +       /* doubly whiteout-ed */
29757 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
29758 +       p = name + AUFS_WH_PFX_LEN * 2;
29759 +       memcpy(p, prefix->name, prefix->len);
29760 +       p += prefix->len;
29761 +       *p++ = '.';
29762 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
29763 +
29764 +       qs.name = name;
29765 +       for (i = 0; i < 3; i++) {
29766 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
29767 +               dentry = au_sio_lkup_one(&qs, h_parent, br);
29768 +               if (IS_ERR(dentry) || !dentry->d_inode)
29769 +                       goto out_name;
29770 +               dput(dentry);
29771 +       }
29772 +       /* pr_warn("could not get random name\n"); */
29773 +       dentry = ERR_PTR(-EEXIST);
29774 +       AuDbg("%.*s\n", AuLNPair(&qs));
29775 +       BUG();
29776 +
29777 +out_name:
29778 +       if (name != defname)
29779 +               kfree(name);
29780 +out:
29781 +       AuTraceErrPtr(dentry);
29782 +       return dentry;
29783 +}
29784 +
29785 +/*
29786 + * rename the @h_dentry on @br to the whiteouted temporary name.
29787 + */
29788 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
29789 +{
29790 +       int err;
29791 +       struct path h_path = {
29792 +               .mnt = au_br_mnt(br)
29793 +       };
29794 +       struct inode *h_dir, *delegated;
29795 +       struct dentry *h_parent;
29796 +
29797 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
29798 +       h_dir = h_parent->d_inode;
29799 +       IMustLock(h_dir);
29800 +
29801 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
29802 +       err = PTR_ERR(h_path.dentry);
29803 +       if (IS_ERR(h_path.dentry))
29804 +               goto out;
29805 +
29806 +       /* under the same dir, no need to lock_rename() */
29807 +       delegated = NULL;
29808 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
29809 +       AuTraceErr(err);
29810 +       if (unlikely(err == -EWOULDBLOCK)) {
29811 +               pr_warn("cannot retry for NFSv4 delegation"
29812 +                       " for an internal rename\n");
29813 +               iput(delegated);
29814 +       }
29815 +       dput(h_path.dentry);
29816 +
29817 +out:
29818 +       AuTraceErr(err);
29819 +       return err;
29820 +}
29821 +
29822 +/* ---------------------------------------------------------------------- */
29823 +/*
29824 + * functions for removing a whiteout
29825 + */
29826 +
29827 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
29828 +{
29829 +       int err, force;
29830 +       struct inode *delegated;
29831 +
29832 +       /*
29833 +        * forces superio when the dir has a sticky bit.
29834 +        * this may be a violation of unix fs semantics.
29835 +        */
29836 +       force = (h_dir->i_mode & S_ISVTX)
29837 +               && !uid_eq(current_fsuid(), h_path->dentry->d_inode->i_uid);
29838 +       delegated = NULL;
29839 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
29840 +       if (unlikely(err == -EWOULDBLOCK)) {
29841 +               pr_warn("cannot retry for NFSv4 delegation"
29842 +                       " for an internal unlink\n");
29843 +               iput(delegated);
29844 +       }
29845 +       return err;
29846 +}
29847 +
29848 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
29849 +                       struct dentry *dentry)
29850 +{
29851 +       int err;
29852 +
29853 +       err = do_unlink_wh(h_dir, h_path);
29854 +       if (!err && dentry)
29855 +               au_set_dbwh(dentry, -1);
29856 +
29857 +       return err;
29858 +}
29859 +
29860 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
29861 +                         struct au_branch *br)
29862 +{
29863 +       int err;
29864 +       struct path h_path = {
29865 +               .mnt = au_br_mnt(br)
29866 +       };
29867 +
29868 +       err = 0;
29869 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
29870 +       if (IS_ERR(h_path.dentry))
29871 +               err = PTR_ERR(h_path.dentry);
29872 +       else {
29873 +               if (h_path.dentry->d_inode
29874 +                   && S_ISREG(h_path.dentry->d_inode->i_mode))
29875 +                       err = do_unlink_wh(h_parent->d_inode, &h_path);
29876 +               dput(h_path.dentry);
29877 +       }
29878 +
29879 +       return err;
29880 +}
29881 +
29882 +/* ---------------------------------------------------------------------- */
29883 +/*
29884 + * initialize/clean whiteout for a branch
29885 + */
29886 +
29887 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
29888 +                       const int isdir)
29889 +{
29890 +       int err;
29891 +       struct inode *delegated;
29892 +
29893 +       if (!whpath->dentry->d_inode)
29894 +               return;
29895 +
29896 +       if (isdir)
29897 +               err = vfsub_rmdir(h_dir, whpath);
29898 +       else {
29899 +               delegated = NULL;
29900 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
29901 +               if (unlikely(err == -EWOULDBLOCK)) {
29902 +                       pr_warn("cannot retry for NFSv4 delegation"
29903 +                               " for an internal unlink\n");
29904 +                       iput(delegated);
29905 +               }
29906 +       }
29907 +       if (unlikely(err))
29908 +               pr_warn("failed removing %pd (%d), ignored.\n",
29909 +                       whpath->dentry, err);
29910 +}
29911 +
29912 +static int test_linkable(struct dentry *h_root)
29913 +{
29914 +       struct inode *h_dir = h_root->d_inode;
29915 +
29916 +       if (h_dir->i_op->link)
29917 +               return 0;
29918 +
29919 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
29920 +              h_root, au_sbtype(h_root->d_sb));
29921 +       return -ENOSYS;
29922 +}
29923 +
29924 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
29925 +static int au_whdir(struct inode *h_dir, struct path *path)
29926 +{
29927 +       int err;
29928 +
29929 +       err = -EEXIST;
29930 +       if (!path->dentry->d_inode) {
29931 +               int mode = S_IRWXU;
29932 +
29933 +               if (au_test_nfs(path->dentry->d_sb))
29934 +                       mode |= S_IXUGO;
29935 +               err = vfsub_mkdir(h_dir, path, mode);
29936 +       } else if (S_ISDIR(path->dentry->d_inode->i_mode))
29937 +               err = 0;
29938 +       else
29939 +               pr_err("unknown %pd exists\n", path->dentry);
29940 +
29941 +       return err;
29942 +}
29943 +
29944 +struct au_wh_base {
29945 +       const struct qstr *name;
29946 +       struct dentry *dentry;
29947 +};
29948 +
29949 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
29950 +                         struct path *h_path)
29951 +{
29952 +       h_path->dentry = base[AuBrWh_BASE].dentry;
29953 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
29954 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
29955 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
29956 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
29957 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
29958 +}
29959 +
29960 +/*
29961 + * returns tri-state,
29962 + * minus: error, caller should print the mesage
29963 + * zero: succuess
29964 + * plus: error, caller should NOT print the mesage
29965 + */
29966 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
29967 +                               int do_plink, struct au_wh_base base[],
29968 +                               struct path *h_path)
29969 +{
29970 +       int err;
29971 +       struct inode *h_dir;
29972 +
29973 +       h_dir = h_root->d_inode;
29974 +       h_path->dentry = base[AuBrWh_BASE].dentry;
29975 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
29976 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
29977 +       if (do_plink) {
29978 +               err = test_linkable(h_root);
29979 +               if (unlikely(err)) {
29980 +                       err = 1;
29981 +                       goto out;
29982 +               }
29983 +
29984 +               err = au_whdir(h_dir, h_path);
29985 +               if (unlikely(err))
29986 +                       goto out;
29987 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
29988 +       } else
29989 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
29990 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
29991 +       err = au_whdir(h_dir, h_path);
29992 +       if (unlikely(err))
29993 +               goto out;
29994 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
29995 +
29996 +out:
29997 +       return err;
29998 +}
29999 +
30000 +/*
30001 + * for the moment, aufs supports the branch filesystem which does not support
30002 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
30003 + * copyup failed. finally, such filesystem will not be used as the writable
30004 + * branch.
30005 + *
30006 + * returns tri-state, see above.
30007 + */
30008 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
30009 +                        int do_plink, struct au_wh_base base[],
30010 +                        struct path *h_path)
30011 +{
30012 +       int err;
30013 +       struct inode *h_dir;
30014 +
30015 +       WbrWhMustWriteLock(wbr);
30016 +
30017 +       err = test_linkable(h_root);
30018 +       if (unlikely(err)) {
30019 +               err = 1;
30020 +               goto out;
30021 +       }
30022 +
30023 +       /*
30024 +        * todo: should this create be done in /sbin/mount.aufs helper?
30025 +        */
30026 +       err = -EEXIST;
30027 +       h_dir = h_root->d_inode;
30028 +       if (!base[AuBrWh_BASE].dentry->d_inode) {
30029 +               h_path->dentry = base[AuBrWh_BASE].dentry;
30030 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
30031 +       } else if (S_ISREG(base[AuBrWh_BASE].dentry->d_inode->i_mode))
30032 +               err = 0;
30033 +       else
30034 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
30035 +       if (unlikely(err))
30036 +               goto out;
30037 +
30038 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
30039 +       if (do_plink) {
30040 +               err = au_whdir(h_dir, h_path);
30041 +               if (unlikely(err))
30042 +                       goto out;
30043 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
30044 +       } else
30045 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
30046 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
30047 +
30048 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
30049 +       err = au_whdir(h_dir, h_path);
30050 +       if (unlikely(err))
30051 +               goto out;
30052 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
30053 +
30054 +out:
30055 +       return err;
30056 +}
30057 +
30058 +/*
30059 + * initialize the whiteout base file/dir for @br.
30060 + */
30061 +int au_wh_init(struct au_branch *br, struct super_block *sb)
30062 +{
30063 +       int err, i;
30064 +       const unsigned char do_plink
30065 +               = !!au_opt_test(au_mntflags(sb), PLINK);
30066 +       struct inode *h_dir;
30067 +       struct path path = br->br_path;
30068 +       struct dentry *h_root = path.dentry;
30069 +       struct au_wbr *wbr = br->br_wbr;
30070 +       static const struct qstr base_name[] = {
30071 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
30072 +                                         sizeof(AUFS_BASE_NAME) - 1),
30073 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
30074 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
30075 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
30076 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
30077 +       };
30078 +       struct au_wh_base base[] = {
30079 +               [AuBrWh_BASE] = {
30080 +                       .name   = base_name + AuBrWh_BASE,
30081 +                       .dentry = NULL
30082 +               },
30083 +               [AuBrWh_PLINK] = {
30084 +                       .name   = base_name + AuBrWh_PLINK,
30085 +                       .dentry = NULL
30086 +               },
30087 +               [AuBrWh_ORPH] = {
30088 +                       .name   = base_name + AuBrWh_ORPH,
30089 +                       .dentry = NULL
30090 +               }
30091 +       };
30092 +
30093 +       if (wbr)
30094 +               WbrWhMustWriteLock(wbr);
30095 +
30096 +       for (i = 0; i < AuBrWh_Last; i++) {
30097 +               /* doubly whiteouted */
30098 +               struct dentry *d;
30099 +
30100 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
30101 +               err = PTR_ERR(d);
30102 +               if (IS_ERR(d))
30103 +                       goto out;
30104 +
30105 +               base[i].dentry = d;
30106 +               AuDebugOn(wbr
30107 +                         && wbr->wbr_wh[i]
30108 +                         && wbr->wbr_wh[i] != base[i].dentry);
30109 +       }
30110 +
30111 +       if (wbr)
30112 +               for (i = 0; i < AuBrWh_Last; i++) {
30113 +                       dput(wbr->wbr_wh[i]);
30114 +                       wbr->wbr_wh[i] = NULL;
30115 +               }
30116 +
30117 +       err = 0;
30118 +       if (!au_br_writable(br->br_perm)) {
30119 +               h_dir = h_root->d_inode;
30120 +               au_wh_init_ro(h_dir, base, &path);
30121 +       } else if (!au_br_wh_linkable(br->br_perm)) {
30122 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
30123 +               if (err > 0)
30124 +                       goto out;
30125 +               else if (err)
30126 +                       goto out_err;
30127 +       } else {
30128 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
30129 +               if (err > 0)
30130 +                       goto out;
30131 +               else if (err)
30132 +                       goto out_err;
30133 +       }
30134 +       goto out; /* success */
30135 +
30136 +out_err:
30137 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
30138 +              err, h_root, au_sbtype(h_root->d_sb));
30139 +out:
30140 +       for (i = 0; i < AuBrWh_Last; i++)
30141 +               dput(base[i].dentry);
30142 +       return err;
30143 +}
30144 +
30145 +/* ---------------------------------------------------------------------- */
30146 +/*
30147 + * whiteouts are all hard-linked usually.
30148 + * when its link count reaches a ceiling, we create a new whiteout base
30149 + * asynchronously.
30150 + */
30151 +
30152 +struct reinit_br_wh {
30153 +       struct super_block *sb;
30154 +       struct au_branch *br;
30155 +};
30156 +
30157 +static void reinit_br_wh(void *arg)
30158 +{
30159 +       int err;
30160 +       aufs_bindex_t bindex;
30161 +       struct path h_path;
30162 +       struct reinit_br_wh *a = arg;
30163 +       struct au_wbr *wbr;
30164 +       struct inode *dir, *delegated;
30165 +       struct dentry *h_root;
30166 +       struct au_hinode *hdir;
30167 +
30168 +       err = 0;
30169 +       wbr = a->br->br_wbr;
30170 +       /* big aufs lock */
30171 +       si_noflush_write_lock(a->sb);
30172 +       if (!au_br_writable(a->br->br_perm))
30173 +               goto out;
30174 +       bindex = au_br_index(a->sb, a->br->br_id);
30175 +       if (unlikely(bindex < 0))
30176 +               goto out;
30177 +
30178 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
30179 +       dir = a->sb->s_root->d_inode;
30180 +       hdir = au_hi(dir, bindex);
30181 +       h_root = au_h_dptr(a->sb->s_root, bindex);
30182 +       AuDebugOn(h_root != au_br_dentry(a->br));
30183 +
30184 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
30185 +       wbr_wh_write_lock(wbr);
30186 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
30187 +                         h_root, a->br);
30188 +       if (!err) {
30189 +               h_path.dentry = wbr->wbr_whbase;
30190 +               h_path.mnt = au_br_mnt(a->br);
30191 +               delegated = NULL;
30192 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
30193 +                                  /*force*/0);
30194 +               if (unlikely(err == -EWOULDBLOCK)) {
30195 +                       pr_warn("cannot retry for NFSv4 delegation"
30196 +                               " for an internal unlink\n");
30197 +                       iput(delegated);
30198 +               }
30199 +       } else {
30200 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
30201 +               err = 0;
30202 +       }
30203 +       dput(wbr->wbr_whbase);
30204 +       wbr->wbr_whbase = NULL;
30205 +       if (!err)
30206 +               err = au_wh_init(a->br, a->sb);
30207 +       wbr_wh_write_unlock(wbr);
30208 +       au_hn_imtx_unlock(hdir);
30209 +       di_read_unlock(a->sb->s_root, AuLock_IR);
30210 +
30211 +out:
30212 +       if (wbr)
30213 +               atomic_dec(&wbr->wbr_wh_running);
30214 +       atomic_dec(&a->br->br_count);
30215 +       si_write_unlock(a->sb);
30216 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
30217 +       kfree(arg);
30218 +       if (unlikely(err))
30219 +               AuIOErr("err %d\n", err);
30220 +}
30221 +
30222 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
30223 +{
30224 +       int do_dec, wkq_err;
30225 +       struct reinit_br_wh *arg;
30226 +
30227 +       do_dec = 1;
30228 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
30229 +               goto out;
30230 +
30231 +       /* ignore ENOMEM */
30232 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
30233 +       if (arg) {
30234 +               /*
30235 +                * dec(wh_running), kfree(arg) and dec(br_count)
30236 +                * in reinit function
30237 +                */
30238 +               arg->sb = sb;
30239 +               arg->br = br;
30240 +               atomic_inc(&br->br_count);
30241 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
30242 +               if (unlikely(wkq_err)) {
30243 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
30244 +                       atomic_dec(&br->br_count);
30245 +                       kfree(arg);
30246 +               }
30247 +               do_dec = 0;
30248 +       }
30249 +
30250 +out:
30251 +       if (do_dec)
30252 +               atomic_dec(&br->br_wbr->wbr_wh_running);
30253 +}
30254 +
30255 +/* ---------------------------------------------------------------------- */
30256 +
30257 +/*
30258 + * create the whiteout @wh.
30259 + */
30260 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
30261 +                            struct dentry *wh)
30262 +{
30263 +       int err;
30264 +       struct path h_path = {
30265 +               .dentry = wh
30266 +       };
30267 +       struct au_branch *br;
30268 +       struct au_wbr *wbr;
30269 +       struct dentry *h_parent;
30270 +       struct inode *h_dir, *delegated;
30271 +
30272 +       h_parent = wh->d_parent; /* dir inode is locked */
30273 +       h_dir = h_parent->d_inode;
30274 +       IMustLock(h_dir);
30275 +
30276 +       br = au_sbr(sb, bindex);
30277 +       h_path.mnt = au_br_mnt(br);
30278 +       wbr = br->br_wbr;
30279 +       wbr_wh_read_lock(wbr);
30280 +       if (wbr->wbr_whbase) {
30281 +               delegated = NULL;
30282 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
30283 +               if (unlikely(err == -EWOULDBLOCK)) {
30284 +                       pr_warn("cannot retry for NFSv4 delegation"
30285 +                               " for an internal link\n");
30286 +                       iput(delegated);
30287 +               }
30288 +               if (!err || err != -EMLINK)
30289 +                       goto out;
30290 +
30291 +               /* link count full. re-initialize br_whbase. */
30292 +               kick_reinit_br_wh(sb, br);
30293 +       }
30294 +
30295 +       /* return this error in this context */
30296 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
30297 +
30298 +out:
30299 +       wbr_wh_read_unlock(wbr);
30300 +       return err;
30301 +}
30302 +
30303 +/* ---------------------------------------------------------------------- */
30304 +
30305 +/*
30306 + * create or remove the diropq.
30307 + */
30308 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
30309 +                               unsigned int flags)
30310 +{
30311 +       struct dentry *opq_dentry, *h_dentry;
30312 +       struct super_block *sb;
30313 +       struct au_branch *br;
30314 +       int err;
30315 +
30316 +       sb = dentry->d_sb;
30317 +       br = au_sbr(sb, bindex);
30318 +       h_dentry = au_h_dptr(dentry, bindex);
30319 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
30320 +       if (IS_ERR(opq_dentry))
30321 +               goto out;
30322 +
30323 +       if (au_ftest_diropq(flags, CREATE)) {
30324 +               err = link_or_create_wh(sb, bindex, opq_dentry);
30325 +               if (!err) {
30326 +                       au_set_dbdiropq(dentry, bindex);
30327 +                       goto out; /* success */
30328 +               }
30329 +       } else {
30330 +               struct path tmp = {
30331 +                       .dentry = opq_dentry,
30332 +                       .mnt    = au_br_mnt(br)
30333 +               };
30334 +               err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
30335 +               if (!err)
30336 +                       au_set_dbdiropq(dentry, -1);
30337 +       }
30338 +       dput(opq_dentry);
30339 +       opq_dentry = ERR_PTR(err);
30340 +
30341 +out:
30342 +       return opq_dentry;
30343 +}
30344 +
30345 +struct do_diropq_args {
30346 +       struct dentry **errp;
30347 +       struct dentry *dentry;
30348 +       aufs_bindex_t bindex;
30349 +       unsigned int flags;
30350 +};
30351 +
30352 +static void call_do_diropq(void *args)
30353 +{
30354 +       struct do_diropq_args *a = args;
30355 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
30356 +}
30357 +
30358 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
30359 +                            unsigned int flags)
30360 +{
30361 +       struct dentry *diropq, *h_dentry;
30362 +
30363 +       h_dentry = au_h_dptr(dentry, bindex);
30364 +       if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
30365 +               diropq = do_diropq(dentry, bindex, flags);
30366 +       else {
30367 +               int wkq_err;
30368 +               struct do_diropq_args args = {
30369 +                       .errp           = &diropq,
30370 +                       .dentry         = dentry,
30371 +                       .bindex         = bindex,
30372 +                       .flags          = flags
30373 +               };
30374 +
30375 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
30376 +               if (unlikely(wkq_err))
30377 +                       diropq = ERR_PTR(wkq_err);
30378 +       }
30379 +
30380 +       return diropq;
30381 +}
30382 +
30383 +/* ---------------------------------------------------------------------- */
30384 +
30385 +/*
30386 + * lookup whiteout dentry.
30387 + * @h_parent: lower parent dentry which must exist and be locked
30388 + * @base_name: name of dentry which will be whiteouted
30389 + * returns dentry for whiteout.
30390 + */
30391 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
30392 +                         struct au_branch *br)
30393 +{
30394 +       int err;
30395 +       struct qstr wh_name;
30396 +       struct dentry *wh_dentry;
30397 +
30398 +       err = au_wh_name_alloc(&wh_name, base_name);
30399 +       wh_dentry = ERR_PTR(err);
30400 +       if (!err) {
30401 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
30402 +               kfree(wh_name.name);
30403 +       }
30404 +       return wh_dentry;
30405 +}
30406 +
30407 +/*
30408 + * link/create a whiteout for @dentry on @bindex.
30409 + */
30410 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
30411 +                           struct dentry *h_parent)
30412 +{
30413 +       struct dentry *wh_dentry;
30414 +       struct super_block *sb;
30415 +       int err;
30416 +
30417 +       sb = dentry->d_sb;
30418 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
30419 +       if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
30420 +               err = link_or_create_wh(sb, bindex, wh_dentry);
30421 +               if (!err)
30422 +                       au_set_dbwh(dentry, bindex);
30423 +               else {
30424 +                       dput(wh_dentry);
30425 +                       wh_dentry = ERR_PTR(err);
30426 +               }
30427 +       }
30428 +
30429 +       return wh_dentry;
30430 +}
30431 +
30432 +/* ---------------------------------------------------------------------- */
30433 +
30434 +/* Delete all whiteouts in this directory on branch bindex. */
30435 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
30436 +                          aufs_bindex_t bindex, struct au_branch *br)
30437 +{
30438 +       int err;
30439 +       unsigned long ul, n;
30440 +       struct qstr wh_name;
30441 +       char *p;
30442 +       struct hlist_head *head;
30443 +       struct au_vdir_wh *pos;
30444 +       struct au_vdir_destr *str;
30445 +
30446 +       err = -ENOMEM;
30447 +       p = (void *)__get_free_page(GFP_NOFS);
30448 +       wh_name.name = p;
30449 +       if (unlikely(!wh_name.name))
30450 +               goto out;
30451 +
30452 +       err = 0;
30453 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
30454 +       p += AUFS_WH_PFX_LEN;
30455 +       n = whlist->nh_num;
30456 +       head = whlist->nh_head;
30457 +       for (ul = 0; !err && ul < n; ul++, head++) {
30458 +               hlist_for_each_entry(pos, head, wh_hash) {
30459 +                       if (pos->wh_bindex != bindex)
30460 +                               continue;
30461 +
30462 +                       str = &pos->wh_str;
30463 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
30464 +                               memcpy(p, str->name, str->len);
30465 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
30466 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
30467 +                               if (!err)
30468 +                                       continue;
30469 +                               break;
30470 +                       }
30471 +                       AuIOErr("whiteout name too long %.*s\n",
30472 +                               str->len, str->name);
30473 +                       err = -EIO;
30474 +                       break;
30475 +               }
30476 +       }
30477 +       free_page((unsigned long)wh_name.name);
30478 +
30479 +out:
30480 +       return err;
30481 +}
30482 +
30483 +struct del_wh_children_args {
30484 +       int *errp;
30485 +       struct dentry *h_dentry;
30486 +       struct au_nhash *whlist;
30487 +       aufs_bindex_t bindex;
30488 +       struct au_branch *br;
30489 +};
30490 +
30491 +static void call_del_wh_children(void *args)
30492 +{
30493 +       struct del_wh_children_args *a = args;
30494 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
30495 +}
30496 +
30497 +/* ---------------------------------------------------------------------- */
30498 +
30499 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
30500 +{
30501 +       struct au_whtmp_rmdir *whtmp;
30502 +       int err;
30503 +       unsigned int rdhash;
30504 +
30505 +       SiMustAnyLock(sb);
30506 +
30507 +       whtmp = kmalloc(sizeof(*whtmp), gfp);
30508 +       if (unlikely(!whtmp)) {
30509 +               whtmp = ERR_PTR(-ENOMEM);
30510 +               goto out;
30511 +       }
30512 +
30513 +       whtmp->dir = NULL;
30514 +       whtmp->br = NULL;
30515 +       whtmp->wh_dentry = NULL;
30516 +       /* no estimation for dir size */
30517 +       rdhash = au_sbi(sb)->si_rdhash;
30518 +       if (!rdhash)
30519 +               rdhash = AUFS_RDHASH_DEF;
30520 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
30521 +       if (unlikely(err)) {
30522 +               kfree(whtmp);
30523 +               whtmp = ERR_PTR(err);
30524 +       }
30525 +
30526 +out:
30527 +       return whtmp;
30528 +}
30529 +
30530 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
30531 +{
30532 +       if (whtmp->br)
30533 +               atomic_dec(&whtmp->br->br_count);
30534 +       dput(whtmp->wh_dentry);
30535 +       iput(whtmp->dir);
30536 +       au_nhash_wh_free(&whtmp->whlist);
30537 +       kfree(whtmp);
30538 +}
30539 +
30540 +/*
30541 + * rmdir the whiteouted temporary named dir @h_dentry.
30542 + * @whlist: whiteouted children.
30543 + */
30544 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
30545 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
30546 +{
30547 +       int err;
30548 +       struct path h_tmp;
30549 +       struct inode *wh_inode, *h_dir;
30550 +       struct au_branch *br;
30551 +
30552 +       h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
30553 +       IMustLock(h_dir);
30554 +
30555 +       br = au_sbr(dir->i_sb, bindex);
30556 +       wh_inode = wh_dentry->d_inode;
30557 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
30558 +
30559 +       /*
30560 +        * someone else might change some whiteouts while we were sleeping.
30561 +        * it means this whlist may have an obsoleted entry.
30562 +        */
30563 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
30564 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
30565 +       else {
30566 +               int wkq_err;
30567 +               struct del_wh_children_args args = {
30568 +                       .errp           = &err,
30569 +                       .h_dentry       = wh_dentry,
30570 +                       .whlist         = whlist,
30571 +                       .bindex         = bindex,
30572 +                       .br             = br
30573 +               };
30574 +
30575 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
30576 +               if (unlikely(wkq_err))
30577 +                       err = wkq_err;
30578 +       }
30579 +       mutex_unlock(&wh_inode->i_mutex);
30580 +
30581 +       if (!err) {
30582 +               h_tmp.dentry = wh_dentry;
30583 +               h_tmp.mnt = au_br_mnt(br);
30584 +               err = vfsub_rmdir(h_dir, &h_tmp);
30585 +       }
30586 +
30587 +       if (!err) {
30588 +               if (au_ibstart(dir) == bindex) {
30589 +                       /* todo: dir->i_mutex is necessary */
30590 +                       au_cpup_attr_timesizes(dir);
30591 +                       vfsub_drop_nlink(dir);
30592 +               }
30593 +               return 0; /* success */
30594 +       }
30595 +
30596 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
30597 +       return err;
30598 +}
30599 +
30600 +static void call_rmdir_whtmp(void *args)
30601 +{
30602 +       int err;
30603 +       aufs_bindex_t bindex;
30604 +       struct au_whtmp_rmdir *a = args;
30605 +       struct super_block *sb;
30606 +       struct dentry *h_parent;
30607 +       struct inode *h_dir;
30608 +       struct au_hinode *hdir;
30609 +
30610 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
30611 +       /* mutex_lock(&a->dir->i_mutex); */
30612 +       err = -EROFS;
30613 +       sb = a->dir->i_sb;
30614 +       si_read_lock(sb, !AuLock_FLUSH);
30615 +       if (!au_br_writable(a->br->br_perm))
30616 +               goto out;
30617 +       bindex = au_br_index(sb, a->br->br_id);
30618 +       if (unlikely(bindex < 0))
30619 +               goto out;
30620 +
30621 +       err = -EIO;
30622 +       ii_write_lock_parent(a->dir);
30623 +       h_parent = dget_parent(a->wh_dentry);
30624 +       h_dir = h_parent->d_inode;
30625 +       hdir = au_hi(a->dir, bindex);
30626 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
30627 +       if (unlikely(err))
30628 +               goto out_mnt;
30629 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
30630 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
30631 +                         a->br);
30632 +       if (!err)
30633 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
30634 +       au_hn_imtx_unlock(hdir);
30635 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
30636 +
30637 +out_mnt:
30638 +       dput(h_parent);
30639 +       ii_write_unlock(a->dir);
30640 +out:
30641 +       /* mutex_unlock(&a->dir->i_mutex); */
30642 +       au_whtmp_rmdir_free(a);
30643 +       si_read_unlock(sb);
30644 +       au_nwt_done(&au_sbi(sb)->si_nowait);
30645 +       if (unlikely(err))
30646 +               AuIOErr("err %d\n", err);
30647 +}
30648 +
30649 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
30650 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
30651 +{
30652 +       int wkq_err;
30653 +       struct super_block *sb;
30654 +
30655 +       IMustLock(dir);
30656 +
30657 +       /* all post-process will be done in do_rmdir_whtmp(). */
30658 +       sb = dir->i_sb;
30659 +       args->dir = au_igrab(dir);
30660 +       args->br = au_sbr(sb, bindex);
30661 +       atomic_inc(&args->br->br_count);
30662 +       args->wh_dentry = dget(wh_dentry);
30663 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
30664 +       if (unlikely(wkq_err)) {
30665 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
30666 +               au_whtmp_rmdir_free(args);
30667 +       }
30668 +}
30669 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
30670 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
30671 +++ linux/fs/aufs/whout.h       2014-06-24 00:14:06.789386337 +0200
30672 @@ -0,0 +1,86 @@
30673 +/*
30674 + * Copyright (C) 2005-2014 Junjiro R. Okajima
30675 + *
30676 + * This program, aufs is free software; you can redistribute it and/or modify
30677 + * it under the terms of the GNU General Public License as published by
30678 + * the Free Software Foundation; either version 2 of the License, or
30679 + * (at your option) any later version.
30680 + *
30681 + * This program is distributed in the hope that it will be useful,
30682 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30683 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30684 + * GNU General Public License for more details.
30685 + *
30686 + * You should have received a copy of the GNU General Public License
30687 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30688 + */
30689 +
30690 +/*
30691 + * whiteout for logical deletion and opaque directory
30692 + */
30693 +
30694 +#ifndef __AUFS_WHOUT_H__
30695 +#define __AUFS_WHOUT_H__
30696 +
30697 +#ifdef __KERNEL__
30698 +
30699 +#include "dir.h"
30700 +
30701 +/* whout.c */
30702 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
30703 +struct au_branch;
30704 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
30705 +              struct au_branch *br, int try_sio);
30706 +int au_diropq_test(struct dentry *h_dentry, struct au_branch *br);
30707 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
30708 +                            struct qstr *prefix);
30709 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
30710 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
30711 +                       struct dentry *dentry);
30712 +int au_wh_init(struct au_branch *br, struct super_block *sb);
30713 +
30714 +/* diropq flags */
30715 +#define AuDiropq_CREATE        1
30716 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
30717 +#define au_fset_diropq(flags, name) \
30718 +       do { (flags) |= AuDiropq_##name; } while (0)
30719 +#define au_fclr_diropq(flags, name) \
30720 +       do { (flags) &= ~AuDiropq_##name; } while (0)
30721 +
30722 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
30723 +                            unsigned int flags);
30724 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
30725 +                         struct au_branch *br);
30726 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
30727 +                           struct dentry *h_parent);
30728 +
30729 +/* real rmdir for the whiteout-ed dir */
30730 +struct au_whtmp_rmdir {
30731 +       struct inode *dir;
30732 +       struct au_branch *br;
30733 +       struct dentry *wh_dentry;
30734 +       struct au_nhash whlist;
30735 +};
30736 +
30737 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
30738 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
30739 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
30740 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
30741 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
30742 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
30743 +
30744 +/* ---------------------------------------------------------------------- */
30745 +
30746 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
30747 +                                             aufs_bindex_t bindex)
30748 +{
30749 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
30750 +}
30751 +
30752 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
30753 +{
30754 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
30755 +}
30756 +
30757 +#endif /* __KERNEL__ */
30758 +#endif /* __AUFS_WHOUT_H__ */
30759 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
30760 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
30761 +++ linux/fs/aufs/wkq.c 2014-06-24 00:14:06.789386337 +0200
30762 @@ -0,0 +1,213 @@
30763 +/*
30764 + * Copyright (C) 2005-2014 Junjiro R. Okajima
30765 + *
30766 + * This program, aufs is free software; you can redistribute it and/or modify
30767 + * it under the terms of the GNU General Public License as published by
30768 + * the Free Software Foundation; either version 2 of the License, or
30769 + * (at your option) any later version.
30770 + *
30771 + * This program is distributed in the hope that it will be useful,
30772 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30773 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30774 + * GNU General Public License for more details.
30775 + *
30776 + * You should have received a copy of the GNU General Public License
30777 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30778 + */
30779 +
30780 +/*
30781 + * workqueue for asynchronous/super-io operations
30782 + * todo: try new dredential scheme
30783 + */
30784 +
30785 +#include <linux/module.h>
30786 +#include "aufs.h"
30787 +
30788 +/* internal workqueue named AUFS_WKQ_NAME */
30789 +
30790 +static struct workqueue_struct *au_wkq;
30791 +
30792 +struct au_wkinfo {
30793 +       struct work_struct wk;
30794 +       struct kobject *kobj;
30795 +
30796 +       unsigned int flags; /* see wkq.h */
30797 +
30798 +       au_wkq_func_t func;
30799 +       void *args;
30800 +
30801 +       struct completion *comp;
30802 +};
30803 +
30804 +/* ---------------------------------------------------------------------- */
30805 +
30806 +static void wkq_func(struct work_struct *wk)
30807 +{
30808 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
30809 +
30810 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
30811 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
30812 +
30813 +       wkinfo->func(wkinfo->args);
30814 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
30815 +               complete(wkinfo->comp);
30816 +       else {
30817 +               kobject_put(wkinfo->kobj);
30818 +               module_put(THIS_MODULE); /* todo: ?? */
30819 +               kfree(wkinfo);
30820 +       }
30821 +}
30822 +
30823 +/*
30824 + * Since struct completion is large, try allocating it dynamically.
30825 + */
30826 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
30827 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
30828 +
30829 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
30830 +{
30831 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
30832 +       if (*comp) {
30833 +               init_completion(*comp);
30834 +               wkinfo->comp = *comp;
30835 +               return 0;
30836 +       }
30837 +       return -ENOMEM;
30838 +}
30839 +
30840 +static void au_wkq_comp_free(struct completion *comp)
30841 +{
30842 +       kfree(comp);
30843 +}
30844 +
30845 +#else
30846 +
30847 +/* no braces */
30848 +#define AuWkqCompDeclare(name) \
30849 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
30850 +       struct completion *comp = &_ ## name
30851 +
30852 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
30853 +{
30854 +       wkinfo->comp = *comp;
30855 +       return 0;
30856 +}
30857 +
30858 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
30859 +{
30860 +       /* empty */
30861 +}
30862 +#endif /* 4KSTACKS */
30863 +
30864 +static void au_wkq_run(struct au_wkinfo *wkinfo)
30865 +{
30866 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
30867 +               if (au_wkq_test()) {
30868 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
30869 +                               " due to a dead dir by UDBA?\n");
30870 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
30871 +               }
30872 +       } else
30873 +               au_dbg_verify_kthread();
30874 +
30875 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
30876 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
30877 +               queue_work(au_wkq, &wkinfo->wk);
30878 +       } else {
30879 +               INIT_WORK(&wkinfo->wk, wkq_func);
30880 +               schedule_work(&wkinfo->wk);
30881 +       }
30882 +}
30883 +
30884 +/*
30885 + * Be careful. It is easy to make deadlock happen.
30886 + * processA: lock, wkq and wait
30887 + * processB: wkq and wait, lock in wkq
30888 + * --> deadlock
30889 + */
30890 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
30891 +{
30892 +       int err;
30893 +       AuWkqCompDeclare(comp);
30894 +       struct au_wkinfo wkinfo = {
30895 +               .flags  = flags,
30896 +               .func   = func,
30897 +               .args   = args
30898 +       };
30899 +
30900 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
30901 +       if (!err) {
30902 +               au_wkq_run(&wkinfo);
30903 +               /* no timeout, no interrupt */
30904 +               wait_for_completion(wkinfo.comp);
30905 +               au_wkq_comp_free(comp);
30906 +               destroy_work_on_stack(&wkinfo.wk);
30907 +       }
30908 +
30909 +       return err;
30910 +
30911 +}
30912 +
30913 +/*
30914 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
30915 + * problem in a concurrent umounting.
30916 + */
30917 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
30918 +                 unsigned int flags)
30919 +{
30920 +       int err;
30921 +       struct au_wkinfo *wkinfo;
30922 +
30923 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
30924 +
30925 +       /*
30926 +        * wkq_func() must free this wkinfo.
30927 +        * it highly depends upon the implementation of workqueue.
30928 +        */
30929 +       err = 0;
30930 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
30931 +       if (wkinfo) {
30932 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
30933 +               wkinfo->flags = flags & ~AuWkq_WAIT;
30934 +               wkinfo->func = func;
30935 +               wkinfo->args = args;
30936 +               wkinfo->comp = NULL;
30937 +               kobject_get(wkinfo->kobj);
30938 +               __module_get(THIS_MODULE); /* todo: ?? */
30939 +
30940 +               au_wkq_run(wkinfo);
30941 +       } else {
30942 +               err = -ENOMEM;
30943 +               au_nwt_done(&au_sbi(sb)->si_nowait);
30944 +       }
30945 +
30946 +       return err;
30947 +}
30948 +
30949 +/* ---------------------------------------------------------------------- */
30950 +
30951 +void au_nwt_init(struct au_nowait_tasks *nwt)
30952 +{
30953 +       atomic_set(&nwt->nw_len, 0);
30954 +       /* smp_mb(); */ /* atomic_set */
30955 +       init_waitqueue_head(&nwt->nw_wq);
30956 +}
30957 +
30958 +void au_wkq_fin(void)
30959 +{
30960 +       destroy_workqueue(au_wkq);
30961 +}
30962 +
30963 +int __init au_wkq_init(void)
30964 +{
30965 +       int err;
30966 +
30967 +       err = 0;
30968 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
30969 +       if (IS_ERR(au_wkq))
30970 +               err = PTR_ERR(au_wkq);
30971 +       else if (!au_wkq)
30972 +               err = -ENOMEM;
30973 +
30974 +       return err;
30975 +}
30976 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
30977 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
30978 +++ linux/fs/aufs/wkq.h 2014-06-24 00:14:06.789386337 +0200
30979 @@ -0,0 +1,91 @@
30980 +/*
30981 + * Copyright (C) 2005-2014 Junjiro R. Okajima
30982 + *
30983 + * This program, aufs is free software; you can redistribute it and/or modify
30984 + * it under the terms of the GNU General Public License as published by
30985 + * the Free Software Foundation; either version 2 of the License, or
30986 + * (at your option) any later version.
30987 + *
30988 + * This program is distributed in the hope that it will be useful,
30989 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30990 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30991 + * GNU General Public License for more details.
30992 + *
30993 + * You should have received a copy of the GNU General Public License
30994 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30995 + */
30996 +
30997 +/*
30998 + * workqueue for asynchronous/super-io operations
30999 + * todo: try new credentials management scheme
31000 + */
31001 +
31002 +#ifndef __AUFS_WKQ_H__
31003 +#define __AUFS_WKQ_H__
31004 +
31005 +#ifdef __KERNEL__
31006 +
31007 +struct super_block;
31008 +
31009 +/* ---------------------------------------------------------------------- */
31010 +
31011 +/*
31012 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
31013 + */
31014 +struct au_nowait_tasks {
31015 +       atomic_t                nw_len;
31016 +       wait_queue_head_t       nw_wq;
31017 +};
31018 +
31019 +/* ---------------------------------------------------------------------- */
31020 +
31021 +typedef void (*au_wkq_func_t)(void *args);
31022 +
31023 +/* wkq flags */
31024 +#define AuWkq_WAIT     1
31025 +#define AuWkq_NEST     (1 << 1)
31026 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
31027 +#define au_fset_wkq(flags, name) \
31028 +       do { (flags) |= AuWkq_##name; } while (0)
31029 +#define au_fclr_wkq(flags, name) \
31030 +       do { (flags) &= ~AuWkq_##name; } while (0)
31031 +
31032 +#ifndef CONFIG_AUFS_HNOTIFY
31033 +#undef AuWkq_NEST
31034 +#define AuWkq_NEST     0
31035 +#endif
31036 +
31037 +/* wkq.c */
31038 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
31039 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
31040 +                 unsigned int flags);
31041 +void au_nwt_init(struct au_nowait_tasks *nwt);
31042 +int __init au_wkq_init(void);
31043 +void au_wkq_fin(void);
31044 +
31045 +/* ---------------------------------------------------------------------- */
31046 +
31047 +static inline int au_wkq_test(void)
31048 +{
31049 +       return current->flags & PF_WQ_WORKER;
31050 +}
31051 +
31052 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
31053 +{
31054 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
31055 +}
31056 +
31057 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
31058 +{
31059 +       if (atomic_dec_and_test(&nwt->nw_len))
31060 +               wake_up_all(&nwt->nw_wq);
31061 +}
31062 +
31063 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
31064 +{
31065 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
31066 +       return 0;
31067 +}
31068 +
31069 +#endif /* __KERNEL__ */
31070 +#endif /* __AUFS_WKQ_H__ */
31071 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
31072 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
31073 +++ linux/fs/aufs/xino.c        2014-06-24 00:14:06.792719853 +0200
31074 @@ -0,0 +1,1312 @@
31075 +/*
31076 + * Copyright (C) 2005-2014 Junjiro R. Okajima
31077 + *
31078 + * This program, aufs is free software; you can redistribute it and/or modify
31079 + * it under the terms of the GNU General Public License as published by
31080 + * the Free Software Foundation; either version 2 of the License, or
31081 + * (at your option) any later version.
31082 + *
31083 + * This program is distributed in the hope that it will be useful,
31084 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31085 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31086 + * GNU General Public License for more details.
31087 + *
31088 + * You should have received a copy of the GNU General Public License
31089 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31090 + */
31091 +
31092 +/*
31093 + * external inode number translation table and bitmap
31094 + */
31095 +
31096 +#include <linux/seq_file.h>
31097 +#include <linux/statfs.h>
31098 +#include "aufs.h"
31099 +
31100 +/* todo: unnecessary to support mmap_sem since kernel-space? */
31101 +ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
31102 +                  loff_t *pos)
31103 +{
31104 +       ssize_t err;
31105 +       mm_segment_t oldfs;
31106 +       union {
31107 +               void *k;
31108 +               char __user *u;
31109 +       } buf;
31110 +
31111 +       buf.k = kbuf;
31112 +       oldfs = get_fs();
31113 +       set_fs(KERNEL_DS);
31114 +       do {
31115 +               /* todo: signal_pending? */
31116 +               err = func(file, buf.u, size, pos);
31117 +       } while (err == -EAGAIN || err == -EINTR);
31118 +       set_fs(oldfs);
31119 +
31120 +#if 0 /* reserved for future use */
31121 +       if (err > 0)
31122 +               fsnotify_access(file->f_dentry);
31123 +#endif
31124 +
31125 +       return err;
31126 +}
31127 +
31128 +/* ---------------------------------------------------------------------- */
31129 +
31130 +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
31131 +                             size_t size, loff_t *pos)
31132 +{
31133 +       ssize_t err;
31134 +       mm_segment_t oldfs;
31135 +       union {
31136 +               void *k;
31137 +               const char __user *u;
31138 +       } buf;
31139 +
31140 +       buf.k = kbuf;
31141 +       oldfs = get_fs();
31142 +       set_fs(KERNEL_DS);
31143 +       do {
31144 +               /* todo: signal_pending? */
31145 +               err = func(file, buf.u, size, pos);
31146 +       } while (err == -EAGAIN || err == -EINTR);
31147 +       set_fs(oldfs);
31148 +
31149 +#if 0 /* reserved for future use */
31150 +       if (err > 0)
31151 +               fsnotify_modify(file->f_dentry);
31152 +#endif
31153 +
31154 +       return err;
31155 +}
31156 +
31157 +struct do_xino_fwrite_args {
31158 +       ssize_t *errp;
31159 +       au_writef_t func;
31160 +       struct file *file;
31161 +       void *buf;
31162 +       size_t size;
31163 +       loff_t *pos;
31164 +};
31165 +
31166 +static void call_do_xino_fwrite(void *args)
31167 +{
31168 +       struct do_xino_fwrite_args *a = args;
31169 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
31170 +}
31171 +
31172 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
31173 +                   loff_t *pos)
31174 +{
31175 +       ssize_t err;
31176 +
31177 +       /* todo: signal block and no wkq? */
31178 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
31179 +               lockdep_off();
31180 +               err = do_xino_fwrite(func, file, buf, size, pos);
31181 +               lockdep_on();
31182 +       } else {
31183 +               /*
31184 +                * it breaks RLIMIT_FSIZE and normal user's limit,
31185 +                * users should care about quota and real 'filesystem full.'
31186 +                */
31187 +               int wkq_err;
31188 +               struct do_xino_fwrite_args args = {
31189 +                       .errp   = &err,
31190 +                       .func   = func,
31191 +                       .file   = file,
31192 +                       .buf    = buf,
31193 +                       .size   = size,
31194 +                       .pos    = pos
31195 +               };
31196 +
31197 +               wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
31198 +               if (unlikely(wkq_err))
31199 +                       err = wkq_err;
31200 +       }
31201 +
31202 +       return err;
31203 +}
31204 +
31205 +/* ---------------------------------------------------------------------- */
31206 +
31207 +/*
31208 + * create a new xinofile at the same place/path as @base_file.
31209 + */
31210 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
31211 +{
31212 +       struct file *file;
31213 +       struct dentry *base, *parent;
31214 +       struct inode *dir, *delegated;
31215 +       struct qstr *name;
31216 +       struct path path;
31217 +       int err;
31218 +
31219 +       base = base_file->f_dentry;
31220 +       parent = base->d_parent; /* dir inode is locked */
31221 +       dir = parent->d_inode;
31222 +       IMustLock(dir);
31223 +
31224 +       file = ERR_PTR(-EINVAL);
31225 +       name = &base->d_name;
31226 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
31227 +       if (IS_ERR(path.dentry)) {
31228 +               file = (void *)path.dentry;
31229 +               pr_err("%pd lookup err %ld\n",
31230 +                      base, PTR_ERR(path.dentry));
31231 +               goto out;
31232 +       }
31233 +
31234 +       /* no need to mnt_want_write() since we call dentry_open() later */
31235 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
31236 +       if (unlikely(err)) {
31237 +               file = ERR_PTR(err);
31238 +               pr_err("%pd create err %d\n", base, err);
31239 +               goto out_dput;
31240 +       }
31241 +
31242 +       path.mnt = base_file->f_path.mnt;
31243 +       file = vfsub_dentry_open(&path,
31244 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
31245 +                                /* | __FMODE_NONOTIFY */);
31246 +       if (IS_ERR(file)) {
31247 +               pr_err("%pd open err %ld\n", base, PTR_ERR(file));
31248 +               goto out_dput;
31249 +       }
31250 +
31251 +       delegated = NULL;
31252 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
31253 +       if (unlikely(err == -EWOULDBLOCK)) {
31254 +               pr_warn("cannot retry for NFSv4 delegation"
31255 +                       " for an internal unlink\n");
31256 +               iput(delegated);
31257 +       }
31258 +       if (unlikely(err)) {
31259 +               pr_err("%pd unlink err %d\n", base, err);
31260 +               goto out_fput;
31261 +       }
31262 +
31263 +       if (copy_src) {
31264 +               /* no one can touch copy_src xino */
31265 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
31266 +               if (unlikely(err)) {
31267 +                       pr_err("%pd copy err %d\n", base, err);
31268 +                       goto out_fput;
31269 +               }
31270 +       }
31271 +       goto out_dput; /* success */
31272 +
31273 +out_fput:
31274 +       fput(file);
31275 +       file = ERR_PTR(err);
31276 +out_dput:
31277 +       dput(path.dentry);
31278 +out:
31279 +       return file;
31280 +}
31281 +
31282 +struct au_xino_lock_dir {
31283 +       struct au_hinode *hdir;
31284 +       struct dentry *parent;
31285 +       struct mutex *mtx;
31286 +};
31287 +
31288 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
31289 +                            struct au_xino_lock_dir *ldir)
31290 +{
31291 +       aufs_bindex_t brid, bindex;
31292 +
31293 +       ldir->hdir = NULL;
31294 +       bindex = -1;
31295 +       brid = au_xino_brid(sb);
31296 +       if (brid >= 0)
31297 +               bindex = au_br_index(sb, brid);
31298 +       if (bindex >= 0) {
31299 +               ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
31300 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
31301 +       } else {
31302 +               ldir->parent = dget_parent(xino->f_dentry);
31303 +               ldir->mtx = &ldir->parent->d_inode->i_mutex;
31304 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
31305 +       }
31306 +}
31307 +
31308 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
31309 +{
31310 +       if (ldir->hdir)
31311 +               au_hn_imtx_unlock(ldir->hdir);
31312 +       else {
31313 +               mutex_unlock(ldir->mtx);
31314 +               dput(ldir->parent);
31315 +       }
31316 +}
31317 +
31318 +/* ---------------------------------------------------------------------- */
31319 +
31320 +/* trucate xino files asynchronously */
31321 +
31322 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
31323 +{
31324 +       int err;
31325 +       unsigned long jiffy;
31326 +       blkcnt_t blocks;
31327 +       aufs_bindex_t bi, bend;
31328 +       struct kstatfs *st;
31329 +       struct au_branch *br;
31330 +       struct file *new_xino, *file;
31331 +       struct super_block *h_sb;
31332 +       struct au_xino_lock_dir ldir;
31333 +
31334 +       err = -ENOMEM;
31335 +       st = kzalloc(sizeof(*st), GFP_NOFS);
31336 +       if (unlikely(!st))
31337 +               goto out;
31338 +
31339 +       err = -EINVAL;
31340 +       bend = au_sbend(sb);
31341 +       if (unlikely(bindex < 0 || bend < bindex))
31342 +               goto out_st;
31343 +       br = au_sbr(sb, bindex);
31344 +       file = br->br_xino.xi_file;
31345 +       if (!file)
31346 +               goto out_st;
31347 +
31348 +       err = vfs_statfs(&file->f_path, st);
31349 +       if (unlikely(err))
31350 +               AuErr1("statfs err %d, ignored\n", err);
31351 +       jiffy = jiffies;
31352 +       blocks = file_inode(file)->i_blocks;
31353 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
31354 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
31355 +
31356 +       au_xino_lock_dir(sb, file, &ldir);
31357 +       /* mnt_want_write() is unnecessary here */
31358 +       new_xino = au_xino_create2(file, file);
31359 +       au_xino_unlock_dir(&ldir);
31360 +       err = PTR_ERR(new_xino);
31361 +       if (IS_ERR(new_xino)) {
31362 +               pr_err("err %d, ignored\n", err);
31363 +               goto out_st;
31364 +       }
31365 +       err = 0;
31366 +       fput(file);
31367 +       br->br_xino.xi_file = new_xino;
31368 +
31369 +       h_sb = au_br_sb(br);
31370 +       for (bi = 0; bi <= bend; bi++) {
31371 +               if (unlikely(bi == bindex))
31372 +                       continue;
31373 +               br = au_sbr(sb, bi);
31374 +               if (au_br_sb(br) != h_sb)
31375 +                       continue;
31376 +
31377 +               fput(br->br_xino.xi_file);
31378 +               br->br_xino.xi_file = new_xino;
31379 +               get_file(new_xino);
31380 +       }
31381 +
31382 +       err = vfs_statfs(&new_xino->f_path, st);
31383 +       if (!err) {
31384 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
31385 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
31386 +                       st->f_bfree, st->f_blocks);
31387 +               if (file_inode(new_xino)->i_blocks < blocks)
31388 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
31389 +       } else
31390 +               AuErr1("statfs err %d, ignored\n", err);
31391 +
31392 +out_st:
31393 +       kfree(st);
31394 +out:
31395 +       return err;
31396 +}
31397 +
31398 +struct xino_do_trunc_args {
31399 +       struct super_block *sb;
31400 +       struct au_branch *br;
31401 +};
31402 +
31403 +static void xino_do_trunc(void *_args)
31404 +{
31405 +       struct xino_do_trunc_args *args = _args;
31406 +       struct super_block *sb;
31407 +       struct au_branch *br;
31408 +       struct inode *dir;
31409 +       int err;
31410 +       aufs_bindex_t bindex;
31411 +
31412 +       err = 0;
31413 +       sb = args->sb;
31414 +       dir = sb->s_root->d_inode;
31415 +       br = args->br;
31416 +
31417 +       si_noflush_write_lock(sb);
31418 +       ii_read_lock_parent(dir);
31419 +       bindex = au_br_index(sb, br->br_id);
31420 +       err = au_xino_trunc(sb, bindex);
31421 +       ii_read_unlock(dir);
31422 +       if (unlikely(err))
31423 +               pr_warn("err b%d, (%d)\n", bindex, err);
31424 +       atomic_dec(&br->br_xino_running);
31425 +       atomic_dec(&br->br_count);
31426 +       si_write_unlock(sb);
31427 +       au_nwt_done(&au_sbi(sb)->si_nowait);
31428 +       kfree(args);
31429 +}
31430 +
31431 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
31432 +{
31433 +       int err;
31434 +       struct kstatfs st;
31435 +       struct au_sbinfo *sbinfo;
31436 +
31437 +       /* todo: si_xino_expire and the ratio should be customizable */
31438 +       sbinfo = au_sbi(sb);
31439 +       if (time_before(jiffies,
31440 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
31441 +               return 0;
31442 +
31443 +       /* truncation border */
31444 +       err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
31445 +       if (unlikely(err)) {
31446 +               AuErr1("statfs err %d, ignored\n", err);
31447 +               return 0;
31448 +       }
31449 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
31450 +               return 0;
31451 +
31452 +       return 1;
31453 +}
31454 +
31455 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
31456 +{
31457 +       struct xino_do_trunc_args *args;
31458 +       int wkq_err;
31459 +
31460 +       if (!xino_trunc_test(sb, br))
31461 +               return;
31462 +
31463 +       if (atomic_inc_return(&br->br_xino_running) > 1)
31464 +               goto out;
31465 +
31466 +       /* lock and kfree() will be called in trunc_xino() */
31467 +       args = kmalloc(sizeof(*args), GFP_NOFS);
31468 +       if (unlikely(!args)) {
31469 +               AuErr1("no memory\n");
31470 +               goto out_args;
31471 +       }
31472 +
31473 +       atomic_inc(&br->br_count);
31474 +       args->sb = sb;
31475 +       args->br = br;
31476 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
31477 +       if (!wkq_err)
31478 +               return; /* success */
31479 +
31480 +       pr_err("wkq %d\n", wkq_err);
31481 +       atomic_dec(&br->br_count);
31482 +
31483 +out_args:
31484 +       kfree(args);
31485 +out:
31486 +       atomic_dec(&br->br_xino_running);
31487 +}
31488 +
31489 +/* ---------------------------------------------------------------------- */
31490 +
31491 +static int au_xino_do_write(au_writef_t write, struct file *file,
31492 +                           ino_t h_ino, ino_t ino)
31493 +{
31494 +       loff_t pos;
31495 +       ssize_t sz;
31496 +
31497 +       pos = h_ino;
31498 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
31499 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
31500 +               return -EFBIG;
31501 +       }
31502 +       pos *= sizeof(ino);
31503 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
31504 +       if (sz == sizeof(ino))
31505 +               return 0; /* success */
31506 +
31507 +       AuIOErr("write failed (%zd)\n", sz);
31508 +       return -EIO;
31509 +}
31510 +
31511 +/*
31512 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
31513 + * at the position of @h_ino.
31514 + * even if @ino is zero, it is written to the xinofile and means no entry.
31515 + * if the size of the xino file on a specific filesystem exceeds the watermark,
31516 + * try truncating it.
31517 + */
31518 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
31519 +                 ino_t ino)
31520 +{
31521 +       int err;
31522 +       unsigned int mnt_flags;
31523 +       struct au_branch *br;
31524 +
31525 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
31526 +                    || ((loff_t)-1) > 0);
31527 +       SiMustAnyLock(sb);
31528 +
31529 +       mnt_flags = au_mntflags(sb);
31530 +       if (!au_opt_test(mnt_flags, XINO))
31531 +               return 0;
31532 +
31533 +       br = au_sbr(sb, bindex);
31534 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
31535 +                              h_ino, ino);
31536 +       if (!err) {
31537 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
31538 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
31539 +                       xino_try_trunc(sb, br);
31540 +               return 0; /* success */
31541 +       }
31542 +
31543 +       AuIOErr("write failed (%d)\n", err);
31544 +       return -EIO;
31545 +}
31546 +
31547 +/* ---------------------------------------------------------------------- */
31548 +
31549 +/* aufs inode number bitmap */
31550 +
31551 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
31552 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
31553 +{
31554 +       ino_t ino;
31555 +
31556 +       AuDebugOn(bit < 0 || page_bits <= bit);
31557 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
31558 +       return ino;
31559 +}
31560 +
31561 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
31562 +{
31563 +       AuDebugOn(ino < AUFS_FIRST_INO);
31564 +       ino -= AUFS_FIRST_INO;
31565 +       *pindex = ino / page_bits;
31566 +       *bit = ino % page_bits;
31567 +}
31568 +
31569 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
31570 +{
31571 +       int err;
31572 +       loff_t pos;
31573 +       ssize_t sz;
31574 +       struct au_sbinfo *sbinfo;
31575 +       struct file *xib;
31576 +       unsigned long *p;
31577 +
31578 +       sbinfo = au_sbi(sb);
31579 +       MtxMustLock(&sbinfo->si_xib_mtx);
31580 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
31581 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
31582 +
31583 +       if (pindex == sbinfo->si_xib_last_pindex)
31584 +               return 0;
31585 +
31586 +       xib = sbinfo->si_xib;
31587 +       p = sbinfo->si_xib_buf;
31588 +       pos = sbinfo->si_xib_last_pindex;
31589 +       pos *= PAGE_SIZE;
31590 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
31591 +       if (unlikely(sz != PAGE_SIZE))
31592 +               goto out;
31593 +
31594 +       pos = pindex;
31595 +       pos *= PAGE_SIZE;
31596 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
31597 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
31598 +       else {
31599 +               memset(p, 0, PAGE_SIZE);
31600 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
31601 +       }
31602 +       if (sz == PAGE_SIZE) {
31603 +               sbinfo->si_xib_last_pindex = pindex;
31604 +               return 0; /* success */
31605 +       }
31606 +
31607 +out:
31608 +       AuIOErr1("write failed (%zd)\n", sz);
31609 +       err = sz;
31610 +       if (sz >= 0)
31611 +               err = -EIO;
31612 +       return err;
31613 +}
31614 +
31615 +/* ---------------------------------------------------------------------- */
31616 +
31617 +static void au_xib_clear_bit(struct inode *inode)
31618 +{
31619 +       int err, bit;
31620 +       unsigned long pindex;
31621 +       struct super_block *sb;
31622 +       struct au_sbinfo *sbinfo;
31623 +
31624 +       AuDebugOn(inode->i_nlink);
31625 +
31626 +       sb = inode->i_sb;
31627 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
31628 +       AuDebugOn(page_bits <= bit);
31629 +       sbinfo = au_sbi(sb);
31630 +       mutex_lock(&sbinfo->si_xib_mtx);
31631 +       err = xib_pindex(sb, pindex);
31632 +       if (!err) {
31633 +               clear_bit(bit, sbinfo->si_xib_buf);
31634 +               sbinfo->si_xib_next_bit = bit;
31635 +       }
31636 +       mutex_unlock(&sbinfo->si_xib_mtx);
31637 +}
31638 +
31639 +/* for s_op->delete_inode() */
31640 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
31641 +{
31642 +       int err;
31643 +       unsigned int mnt_flags;
31644 +       aufs_bindex_t bindex, bend, bi;
31645 +       unsigned char try_trunc;
31646 +       struct au_iinfo *iinfo;
31647 +       struct super_block *sb;
31648 +       struct au_hinode *hi;
31649 +       struct inode *h_inode;
31650 +       struct au_branch *br;
31651 +       au_writef_t xwrite;
31652 +
31653 +       sb = inode->i_sb;
31654 +       mnt_flags = au_mntflags(sb);
31655 +       if (!au_opt_test(mnt_flags, XINO)
31656 +           || inode->i_ino == AUFS_ROOT_INO)
31657 +               return;
31658 +
31659 +       if (unlinked) {
31660 +               au_xigen_inc(inode);
31661 +               au_xib_clear_bit(inode);
31662 +       }
31663 +
31664 +       iinfo = au_ii(inode);
31665 +       if (!iinfo)
31666 +               return;
31667 +
31668 +       bindex = iinfo->ii_bstart;
31669 +       if (bindex < 0)
31670 +               return;
31671 +
31672 +       xwrite = au_sbi(sb)->si_xwrite;
31673 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
31674 +       hi = iinfo->ii_hinode + bindex;
31675 +       bend = iinfo->ii_bend;
31676 +       for (; bindex <= bend; bindex++, hi++) {
31677 +               h_inode = hi->hi_inode;
31678 +               if (!h_inode
31679 +                   || (!unlinked && h_inode->i_nlink))
31680 +                       continue;
31681 +
31682 +               /* inode may not be revalidated */
31683 +               bi = au_br_index(sb, hi->hi_id);
31684 +               if (bi < 0)
31685 +                       continue;
31686 +
31687 +               br = au_sbr(sb, bi);
31688 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
31689 +                                      h_inode->i_ino, /*ino*/0);
31690 +               if (!err && try_trunc
31691 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
31692 +                       xino_try_trunc(sb, br);
31693 +       }
31694 +}
31695 +
31696 +/* get an unused inode number from bitmap */
31697 +ino_t au_xino_new_ino(struct super_block *sb)
31698 +{
31699 +       ino_t ino;
31700 +       unsigned long *p, pindex, ul, pend;
31701 +       struct au_sbinfo *sbinfo;
31702 +       struct file *file;
31703 +       int free_bit, err;
31704 +
31705 +       if (!au_opt_test(au_mntflags(sb), XINO))
31706 +               return iunique(sb, AUFS_FIRST_INO);
31707 +
31708 +       sbinfo = au_sbi(sb);
31709 +       mutex_lock(&sbinfo->si_xib_mtx);
31710 +       p = sbinfo->si_xib_buf;
31711 +       free_bit = sbinfo->si_xib_next_bit;
31712 +       if (free_bit < page_bits && !test_bit(free_bit, p))
31713 +               goto out; /* success */
31714 +       free_bit = find_first_zero_bit(p, page_bits);
31715 +       if (free_bit < page_bits)
31716 +               goto out; /* success */
31717 +
31718 +       pindex = sbinfo->si_xib_last_pindex;
31719 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
31720 +               err = xib_pindex(sb, ul);
31721 +               if (unlikely(err))
31722 +                       goto out_err;
31723 +               free_bit = find_first_zero_bit(p, page_bits);
31724 +               if (free_bit < page_bits)
31725 +                       goto out; /* success */
31726 +       }
31727 +
31728 +       file = sbinfo->si_xib;
31729 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
31730 +       for (ul = pindex + 1; ul <= pend; ul++) {
31731 +               err = xib_pindex(sb, ul);
31732 +               if (unlikely(err))
31733 +                       goto out_err;
31734 +               free_bit = find_first_zero_bit(p, page_bits);
31735 +               if (free_bit < page_bits)
31736 +                       goto out; /* success */
31737 +       }
31738 +       BUG();
31739 +
31740 +out:
31741 +       set_bit(free_bit, p);
31742 +       sbinfo->si_xib_next_bit = free_bit + 1;
31743 +       pindex = sbinfo->si_xib_last_pindex;
31744 +       mutex_unlock(&sbinfo->si_xib_mtx);
31745 +       ino = xib_calc_ino(pindex, free_bit);
31746 +       AuDbg("i%lu\n", (unsigned long)ino);
31747 +       return ino;
31748 +out_err:
31749 +       mutex_unlock(&sbinfo->si_xib_mtx);
31750 +       AuDbg("i0\n");
31751 +       return 0;
31752 +}
31753 +
31754 +/*
31755 + * read @ino from xinofile for the specified branch{@sb, @bindex}
31756 + * at the position of @h_ino.
31757 + * if @ino does not exist and @do_new is true, get new one.
31758 + */
31759 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
31760 +                ino_t *ino)
31761 +{
31762 +       int err;
31763 +       ssize_t sz;
31764 +       loff_t pos;
31765 +       struct file *file;
31766 +       struct au_sbinfo *sbinfo;
31767 +
31768 +       *ino = 0;
31769 +       if (!au_opt_test(au_mntflags(sb), XINO))
31770 +               return 0; /* no xino */
31771 +
31772 +       err = 0;
31773 +       sbinfo = au_sbi(sb);
31774 +       pos = h_ino;
31775 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
31776 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
31777 +               return -EFBIG;
31778 +       }
31779 +       pos *= sizeof(*ino);
31780 +
31781 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
31782 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
31783 +               return 0; /* no ino */
31784 +
31785 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
31786 +       if (sz == sizeof(*ino))
31787 +               return 0; /* success */
31788 +
31789 +       err = sz;
31790 +       if (unlikely(sz >= 0)) {
31791 +               err = -EIO;
31792 +               AuIOErr("xino read error (%zd)\n", sz);
31793 +       }
31794 +
31795 +       return err;
31796 +}
31797 +
31798 +/* ---------------------------------------------------------------------- */
31799 +
31800 +/* create and set a new xino file */
31801 +
31802 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
31803 +{
31804 +       struct file *file;
31805 +       struct dentry *h_parent, *d;
31806 +       struct inode *h_dir;
31807 +       int err;
31808 +
31809 +       /*
31810 +        * at mount-time, and the xino file is the default path,
31811 +        * hnotify is disabled so we have no notify events to ignore.
31812 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
31813 +        */
31814 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
31815 +                              /* | __FMODE_NONOTIFY */,
31816 +                              S_IRUGO | S_IWUGO);
31817 +       if (IS_ERR(file)) {
31818 +               if (!silent)
31819 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
31820 +               return file;
31821 +       }
31822 +
31823 +       /* keep file count */
31824 +       h_parent = dget_parent(file->f_dentry);
31825 +       h_dir = h_parent->d_inode;
31826 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
31827 +       /* mnt_want_write() is unnecessary here */
31828 +       /* no delegation since it is just created */
31829 +       err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL, /*force*/0);
31830 +       mutex_unlock(&h_dir->i_mutex);
31831 +       dput(h_parent);
31832 +       if (unlikely(err)) {
31833 +               if (!silent)
31834 +                       pr_err("unlink %s(%d)\n", fname, err);
31835 +               goto out;
31836 +       }
31837 +
31838 +       err = -EINVAL;
31839 +       d = file->f_dentry;
31840 +       if (unlikely(sb == d->d_sb)) {
31841 +               if (!silent)
31842 +                       pr_err("%s must be outside\n", fname);
31843 +               goto out;
31844 +       }
31845 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
31846 +               if (!silent)
31847 +                       pr_err("xino doesn't support %s(%s)\n",
31848 +                              fname, au_sbtype(d->d_sb));
31849 +               goto out;
31850 +       }
31851 +       return file; /* success */
31852 +
31853 +out:
31854 +       fput(file);
31855 +       file = ERR_PTR(err);
31856 +       return file;
31857 +}
31858 +
31859 +/*
31860 + * find another branch who is on the same filesystem of the specified
31861 + * branch{@btgt}. search until @bend.
31862 + */
31863 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
31864 +                       aufs_bindex_t bend)
31865 +{
31866 +       aufs_bindex_t bindex;
31867 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
31868 +
31869 +       for (bindex = 0; bindex < btgt; bindex++)
31870 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
31871 +                       return bindex;
31872 +       for (bindex++; bindex <= bend; bindex++)
31873 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
31874 +                       return bindex;
31875 +       return -1;
31876 +}
31877 +
31878 +/* ---------------------------------------------------------------------- */
31879 +
31880 +/*
31881 + * initialize the xinofile for the specified branch @br
31882 + * at the place/path where @base_file indicates.
31883 + * test whether another branch is on the same filesystem or not,
31884 + * if @do_test is true.
31885 + */
31886 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
31887 +              struct file *base_file, int do_test)
31888 +{
31889 +       int err;
31890 +       ino_t ino;
31891 +       aufs_bindex_t bend, bindex;
31892 +       struct au_branch *shared_br, *b;
31893 +       struct file *file;
31894 +       struct super_block *tgt_sb;
31895 +
31896 +       shared_br = NULL;
31897 +       bend = au_sbend(sb);
31898 +       if (do_test) {
31899 +               tgt_sb = au_br_sb(br);
31900 +               for (bindex = 0; bindex <= bend; bindex++) {
31901 +                       b = au_sbr(sb, bindex);
31902 +                       if (tgt_sb == au_br_sb(b)) {
31903 +                               shared_br = b;
31904 +                               break;
31905 +                       }
31906 +               }
31907 +       }
31908 +
31909 +       if (!shared_br || !shared_br->br_xino.xi_file) {
31910 +               struct au_xino_lock_dir ldir;
31911 +
31912 +               au_xino_lock_dir(sb, base_file, &ldir);
31913 +               /* mnt_want_write() is unnecessary here */
31914 +               file = au_xino_create2(base_file, NULL);
31915 +               au_xino_unlock_dir(&ldir);
31916 +               err = PTR_ERR(file);
31917 +               if (IS_ERR(file))
31918 +                       goto out;
31919 +               br->br_xino.xi_file = file;
31920 +       } else {
31921 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
31922 +               get_file(br->br_xino.xi_file);
31923 +       }
31924 +
31925 +       ino = AUFS_ROOT_INO;
31926 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
31927 +                              h_ino, ino);
31928 +       if (unlikely(err)) {
31929 +               fput(br->br_xino.xi_file);
31930 +               br->br_xino.xi_file = NULL;
31931 +       }
31932 +
31933 +out:
31934 +       return err;
31935 +}
31936 +
31937 +/* ---------------------------------------------------------------------- */
31938 +
31939 +/* trucate a xino bitmap file */
31940 +
31941 +/* todo: slow */
31942 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
31943 +{
31944 +       int err, bit;
31945 +       ssize_t sz;
31946 +       unsigned long pindex;
31947 +       loff_t pos, pend;
31948 +       struct au_sbinfo *sbinfo;
31949 +       au_readf_t func;
31950 +       ino_t *ino;
31951 +       unsigned long *p;
31952 +
31953 +       err = 0;
31954 +       sbinfo = au_sbi(sb);
31955 +       MtxMustLock(&sbinfo->si_xib_mtx);
31956 +       p = sbinfo->si_xib_buf;
31957 +       func = sbinfo->si_xread;
31958 +       pend = vfsub_f_size_read(file);
31959 +       pos = 0;
31960 +       while (pos < pend) {
31961 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
31962 +               err = sz;
31963 +               if (unlikely(sz <= 0))
31964 +                       goto out;
31965 +
31966 +               err = 0;
31967 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
31968 +                       if (unlikely(*ino < AUFS_FIRST_INO))
31969 +                               continue;
31970 +
31971 +                       xib_calc_bit(*ino, &pindex, &bit);
31972 +                       AuDebugOn(page_bits <= bit);
31973 +                       err = xib_pindex(sb, pindex);
31974 +                       if (!err)
31975 +                               set_bit(bit, p);
31976 +                       else
31977 +                               goto out;
31978 +               }
31979 +       }
31980 +
31981 +out:
31982 +       return err;
31983 +}
31984 +
31985 +static int xib_restore(struct super_block *sb)
31986 +{
31987 +       int err;
31988 +       aufs_bindex_t bindex, bend;
31989 +       void *page;
31990 +
31991 +       err = -ENOMEM;
31992 +       page = (void *)__get_free_page(GFP_NOFS);
31993 +       if (unlikely(!page))
31994 +               goto out;
31995 +
31996 +       err = 0;
31997 +       bend = au_sbend(sb);
31998 +       for (bindex = 0; !err && bindex <= bend; bindex++)
31999 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
32000 +                       err = do_xib_restore
32001 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
32002 +               else
32003 +                       AuDbg("b%d\n", bindex);
32004 +       free_page((unsigned long)page);
32005 +
32006 +out:
32007 +       return err;
32008 +}
32009 +
32010 +int au_xib_trunc(struct super_block *sb)
32011 +{
32012 +       int err;
32013 +       ssize_t sz;
32014 +       loff_t pos;
32015 +       struct au_xino_lock_dir ldir;
32016 +       struct au_sbinfo *sbinfo;
32017 +       unsigned long *p;
32018 +       struct file *file;
32019 +
32020 +       SiMustWriteLock(sb);
32021 +
32022 +       err = 0;
32023 +       sbinfo = au_sbi(sb);
32024 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
32025 +               goto out;
32026 +
32027 +       file = sbinfo->si_xib;
32028 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
32029 +               goto out;
32030 +
32031 +       au_xino_lock_dir(sb, file, &ldir);
32032 +       /* mnt_want_write() is unnecessary here */
32033 +       file = au_xino_create2(sbinfo->si_xib, NULL);
32034 +       au_xino_unlock_dir(&ldir);
32035 +       err = PTR_ERR(file);
32036 +       if (IS_ERR(file))
32037 +               goto out;
32038 +       fput(sbinfo->si_xib);
32039 +       sbinfo->si_xib = file;
32040 +
32041 +       p = sbinfo->si_xib_buf;
32042 +       memset(p, 0, PAGE_SIZE);
32043 +       pos = 0;
32044 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
32045 +       if (unlikely(sz != PAGE_SIZE)) {
32046 +               err = sz;
32047 +               AuIOErr("err %d\n", err);
32048 +               if (sz >= 0)
32049 +                       err = -EIO;
32050 +               goto out;
32051 +       }
32052 +
32053 +       mutex_lock(&sbinfo->si_xib_mtx);
32054 +       /* mnt_want_write() is unnecessary here */
32055 +       err = xib_restore(sb);
32056 +       mutex_unlock(&sbinfo->si_xib_mtx);
32057 +
32058 +out:
32059 +       return err;
32060 +}
32061 +
32062 +/* ---------------------------------------------------------------------- */
32063 +
32064 +/*
32065 + * xino mount option handlers
32066 + */
32067 +static au_readf_t find_readf(struct file *h_file)
32068 +{
32069 +       const struct file_operations *fop = h_file->f_op;
32070 +
32071 +       if (fop->read)
32072 +               return fop->read;
32073 +       if (fop->aio_read)
32074 +               return do_sync_read;
32075 +       return ERR_PTR(-ENOSYS);
32076 +}
32077 +
32078 +static au_writef_t find_writef(struct file *h_file)
32079 +{
32080 +       const struct file_operations *fop = h_file->f_op;
32081 +
32082 +       if (fop->write)
32083 +               return fop->write;
32084 +       if (fop->aio_write)
32085 +               return do_sync_write;
32086 +       return ERR_PTR(-ENOSYS);
32087 +}
32088 +
32089 +/* xino bitmap */
32090 +static void xino_clear_xib(struct super_block *sb)
32091 +{
32092 +       struct au_sbinfo *sbinfo;
32093 +
32094 +       SiMustWriteLock(sb);
32095 +
32096 +       sbinfo = au_sbi(sb);
32097 +       sbinfo->si_xread = NULL;
32098 +       sbinfo->si_xwrite = NULL;
32099 +       if (sbinfo->si_xib)
32100 +               fput(sbinfo->si_xib);
32101 +       sbinfo->si_xib = NULL;
32102 +       free_page((unsigned long)sbinfo->si_xib_buf);
32103 +       sbinfo->si_xib_buf = NULL;
32104 +}
32105 +
32106 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
32107 +{
32108 +       int err;
32109 +       loff_t pos;
32110 +       struct au_sbinfo *sbinfo;
32111 +       struct file *file;
32112 +
32113 +       SiMustWriteLock(sb);
32114 +
32115 +       sbinfo = au_sbi(sb);
32116 +       file = au_xino_create2(base, sbinfo->si_xib);
32117 +       err = PTR_ERR(file);
32118 +       if (IS_ERR(file))
32119 +               goto out;
32120 +       if (sbinfo->si_xib)
32121 +               fput(sbinfo->si_xib);
32122 +       sbinfo->si_xib = file;
32123 +       sbinfo->si_xread = find_readf(file);
32124 +       sbinfo->si_xwrite = find_writef(file);
32125 +
32126 +       err = -ENOMEM;
32127 +       if (!sbinfo->si_xib_buf)
32128 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
32129 +       if (unlikely(!sbinfo->si_xib_buf))
32130 +               goto out_unset;
32131 +
32132 +       sbinfo->si_xib_last_pindex = 0;
32133 +       sbinfo->si_xib_next_bit = 0;
32134 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
32135 +               pos = 0;
32136 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
32137 +                                 PAGE_SIZE, &pos);
32138 +               if (unlikely(err != PAGE_SIZE))
32139 +                       goto out_free;
32140 +       }
32141 +       err = 0;
32142 +       goto out; /* success */
32143 +
32144 +out_free:
32145 +       free_page((unsigned long)sbinfo->si_xib_buf);
32146 +       sbinfo->si_xib_buf = NULL;
32147 +       if (err >= 0)
32148 +               err = -EIO;
32149 +out_unset:
32150 +       fput(sbinfo->si_xib);
32151 +       sbinfo->si_xib = NULL;
32152 +       sbinfo->si_xread = NULL;
32153 +       sbinfo->si_xwrite = NULL;
32154 +out:
32155 +       return err;
32156 +}
32157 +
32158 +/* xino for each branch */
32159 +static void xino_clear_br(struct super_block *sb)
32160 +{
32161 +       aufs_bindex_t bindex, bend;
32162 +       struct au_branch *br;
32163 +
32164 +       bend = au_sbend(sb);
32165 +       for (bindex = 0; bindex <= bend; bindex++) {
32166 +               br = au_sbr(sb, bindex);
32167 +               if (!br || !br->br_xino.xi_file)
32168 +                       continue;
32169 +
32170 +               fput(br->br_xino.xi_file);
32171 +               br->br_xino.xi_file = NULL;
32172 +       }
32173 +}
32174 +
32175 +static int au_xino_set_br(struct super_block *sb, struct file *base)
32176 +{
32177 +       int err;
32178 +       ino_t ino;
32179 +       aufs_bindex_t bindex, bend, bshared;
32180 +       struct {
32181 +               struct file *old, *new;
32182 +       } *fpair, *p;
32183 +       struct au_branch *br;
32184 +       struct inode *inode;
32185 +       au_writef_t writef;
32186 +
32187 +       SiMustWriteLock(sb);
32188 +
32189 +       err = -ENOMEM;
32190 +       bend = au_sbend(sb);
32191 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
32192 +       if (unlikely(!fpair))
32193 +               goto out;
32194 +
32195 +       inode = sb->s_root->d_inode;
32196 +       ino = AUFS_ROOT_INO;
32197 +       writef = au_sbi(sb)->si_xwrite;
32198 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
32199 +               br = au_sbr(sb, bindex);
32200 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
32201 +               if (bshared >= 0) {
32202 +                       /* shared xino */
32203 +                       *p = fpair[bshared];
32204 +                       get_file(p->new);
32205 +               }
32206 +
32207 +               if (!p->new) {
32208 +                       /* new xino */
32209 +                       p->old = br->br_xino.xi_file;
32210 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
32211 +                       err = PTR_ERR(p->new);
32212 +                       if (IS_ERR(p->new)) {
32213 +                               p->new = NULL;
32214 +                               goto out_pair;
32215 +                       }
32216 +               }
32217 +
32218 +               err = au_xino_do_write(writef, p->new,
32219 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
32220 +               if (unlikely(err))
32221 +                       goto out_pair;
32222 +       }
32223 +
32224 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
32225 +               br = au_sbr(sb, bindex);
32226 +               if (br->br_xino.xi_file)
32227 +                       fput(br->br_xino.xi_file);
32228 +               get_file(p->new);
32229 +               br->br_xino.xi_file = p->new;
32230 +       }
32231 +
32232 +out_pair:
32233 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
32234 +               if (p->new)
32235 +                       fput(p->new);
32236 +               else
32237 +                       break;
32238 +       kfree(fpair);
32239 +out:
32240 +       return err;
32241 +}
32242 +
32243 +void au_xino_clr(struct super_block *sb)
32244 +{
32245 +       struct au_sbinfo *sbinfo;
32246 +
32247 +       au_xigen_clr(sb);
32248 +       xino_clear_xib(sb);
32249 +       xino_clear_br(sb);
32250 +       sbinfo = au_sbi(sb);
32251 +       /* lvalue, do not call au_mntflags() */
32252 +       au_opt_clr(sbinfo->si_mntflags, XINO);
32253 +}
32254 +
32255 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
32256 +{
32257 +       int err, skip;
32258 +       struct dentry *parent, *cur_parent;
32259 +       struct qstr *dname, *cur_name;
32260 +       struct file *cur_xino;
32261 +       struct inode *dir;
32262 +       struct au_sbinfo *sbinfo;
32263 +
32264 +       SiMustWriteLock(sb);
32265 +
32266 +       err = 0;
32267 +       sbinfo = au_sbi(sb);
32268 +       parent = dget_parent(xino->file->f_dentry);
32269 +       if (remount) {
32270 +               skip = 0;
32271 +               dname = &xino->file->f_dentry->d_name;
32272 +               cur_xino = sbinfo->si_xib;
32273 +               if (cur_xino) {
32274 +                       cur_parent = dget_parent(cur_xino->f_dentry);
32275 +                       cur_name = &cur_xino->f_dentry->d_name;
32276 +                       skip = (cur_parent == parent
32277 +                               && au_qstreq(dname, cur_name));
32278 +                       dput(cur_parent);
32279 +               }
32280 +               if (skip)
32281 +                       goto out;
32282 +       }
32283 +
32284 +       au_opt_set(sbinfo->si_mntflags, XINO);
32285 +       dir = parent->d_inode;
32286 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
32287 +       /* mnt_want_write() is unnecessary here */
32288 +       err = au_xino_set_xib(sb, xino->file);
32289 +       if (!err)
32290 +               err = au_xigen_set(sb, xino->file);
32291 +       if (!err)
32292 +               err = au_xino_set_br(sb, xino->file);
32293 +       mutex_unlock(&dir->i_mutex);
32294 +       if (!err)
32295 +               goto out; /* success */
32296 +
32297 +       /* reset all */
32298 +       AuIOErr("failed creating xino(%d).\n", err);
32299 +
32300 +out:
32301 +       dput(parent);
32302 +       return err;
32303 +}
32304 +
32305 +/* ---------------------------------------------------------------------- */
32306 +
32307 +/*
32308 + * create a xinofile at the default place/path.
32309 + */
32310 +struct file *au_xino_def(struct super_block *sb)
32311 +{
32312 +       struct file *file;
32313 +       char *page, *p;
32314 +       struct au_branch *br;
32315 +       struct super_block *h_sb;
32316 +       struct path path;
32317 +       aufs_bindex_t bend, bindex, bwr;
32318 +
32319 +       br = NULL;
32320 +       bend = au_sbend(sb);
32321 +       bwr = -1;
32322 +       for (bindex = 0; bindex <= bend; bindex++) {
32323 +               br = au_sbr(sb, bindex);
32324 +               if (au_br_writable(br->br_perm)
32325 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
32326 +                       bwr = bindex;
32327 +                       break;
32328 +               }
32329 +       }
32330 +
32331 +       if (bwr >= 0) {
32332 +               file = ERR_PTR(-ENOMEM);
32333 +               page = (void *)__get_free_page(GFP_NOFS);
32334 +               if (unlikely(!page))
32335 +                       goto out;
32336 +               path.mnt = au_br_mnt(br);
32337 +               path.dentry = au_h_dptr(sb->s_root, bwr);
32338 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
32339 +               file = (void *)p;
32340 +               if (!IS_ERR(p)) {
32341 +                       strcat(p, "/" AUFS_XINO_FNAME);
32342 +                       AuDbg("%s\n", p);
32343 +                       file = au_xino_create(sb, p, /*silent*/0);
32344 +                       if (!IS_ERR(file))
32345 +                               au_xino_brid_set(sb, br->br_id);
32346 +               }
32347 +               free_page((unsigned long)page);
32348 +       } else {
32349 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
32350 +               if (IS_ERR(file))
32351 +                       goto out;
32352 +               h_sb = file->f_dentry->d_sb;
32353 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
32354 +                       pr_err("xino doesn't support %s(%s)\n",
32355 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
32356 +                       fput(file);
32357 +                       file = ERR_PTR(-EINVAL);
32358 +               }
32359 +               if (!IS_ERR(file))
32360 +                       au_xino_brid_set(sb, -1);
32361 +       }
32362 +
32363 +out:
32364 +       return file;
32365 +}
32366 +
32367 +/* ---------------------------------------------------------------------- */
32368 +
32369 +int au_xino_path(struct seq_file *seq, struct file *file)
32370 +{
32371 +       int err;
32372 +
32373 +       err = au_seq_path(seq, &file->f_path);
32374 +       if (unlikely(err < 0))
32375 +               goto out;
32376 +
32377 +       err = 0;
32378 +#define Deleted "\\040(deleted)"
32379 +       seq->count -= sizeof(Deleted) - 1;
32380 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
32381 +                        sizeof(Deleted) - 1));
32382 +#undef Deleted
32383 +
32384 +out:
32385 +       return err;
32386 +}
32387 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
32388 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
32389 +++ linux/include/uapi/linux/aufs_type.h        2014-06-24 00:14:06.792719853 +0200
32390 @@ -0,0 +1,281 @@
32391 +/*
32392 + * Copyright (C) 2005-2014 Junjiro R. Okajima
32393 + *
32394 + * This program, aufs is free software; you can redistribute it and/or modify
32395 + * it under the terms of the GNU General Public License as published by
32396 + * the Free Software Foundation; either version 2 of the License, or
32397 + * (at your option) any later version.
32398 + *
32399 + * This program is distributed in the hope that it will be useful,
32400 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32401 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32402 + * GNU General Public License for more details.
32403 + *
32404 + * You should have received a copy of the GNU General Public License
32405 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32406 + */
32407 +
32408 +#ifndef __AUFS_TYPE_H__
32409 +#define __AUFS_TYPE_H__
32410 +
32411 +#define AUFS_NAME      "aufs"
32412 +
32413 +#ifdef __KERNEL__
32414 +/*
32415 + * define it before including all other headers.
32416 + * sched.h may use pr_* macros before defining "current", so define the
32417 + * no-current version first, and re-define later.
32418 + */
32419 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
32420 +#include <linux/sched.h>
32421 +#undef pr_fmt
32422 +#define pr_fmt(fmt) \
32423 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
32424 +               (int)sizeof(current->comm), current->comm, current->pid
32425 +#else
32426 +#include <stdint.h>
32427 +#include <sys/types.h>
32428 +#endif /* __KERNEL__ */
32429 +
32430 +#include <linux/limits.h>
32431 +
32432 +#define AUFS_VERSION   "3.15-20140623"
32433 +
32434 +/* todo? move this to linux-2.6.19/include/magic.h */
32435 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
32436 +
32437 +/* ---------------------------------------------------------------------- */
32438 +
32439 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
32440 +typedef int8_t aufs_bindex_t;
32441 +#define AUFS_BRANCH_MAX 127
32442 +#else
32443 +typedef int16_t aufs_bindex_t;
32444 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
32445 +#define AUFS_BRANCH_MAX 511
32446 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
32447 +#define AUFS_BRANCH_MAX 1023
32448 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
32449 +#define AUFS_BRANCH_MAX 32767
32450 +#endif
32451 +#endif
32452 +
32453 +#ifdef __KERNEL__
32454 +#ifndef AUFS_BRANCH_MAX
32455 +#error unknown CONFIG_AUFS_BRANCH_MAX value
32456 +#endif
32457 +#endif /* __KERNEL__ */
32458 +
32459 +/* ---------------------------------------------------------------------- */
32460 +
32461 +#define AUFS_FSTYPE            AUFS_NAME
32462 +
32463 +#define AUFS_ROOT_INO          2
32464 +#define AUFS_FIRST_INO         11
32465 +
32466 +#define AUFS_WH_PFX            ".wh."
32467 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
32468 +#define AUFS_WH_TMP_LEN                4
32469 +/* a limit for rmdir/rename a dir and copyup */
32470 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
32471 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
32472 +                               - 1                     /* dot */\
32473 +                               - AUFS_WH_TMP_LEN)      /* hex */
32474 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
32475 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
32476 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
32477 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
32478 +#define AUFS_DIRWH_DEF         3
32479 +#define AUFS_RDCACHE_DEF       10 /* seconds */
32480 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
32481 +#define AUFS_RDBLK_DEF         512 /* bytes */
32482 +#define AUFS_RDHASH_DEF                32
32483 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
32484 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
32485 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
32486 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
32487 +
32488 +/* pseudo-link maintenace under /proc */
32489 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
32490 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
32491 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
32492 +
32493 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
32494 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
32495 +
32496 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
32497 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
32498 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
32499 +
32500 +/* doubly whiteouted */
32501 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
32502 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
32503 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
32504 +
32505 +/* branch permissions and attributes */
32506 +#define AUFS_BRPERM_RW         "rw"
32507 +#define AUFS_BRPERM_RO         "ro"
32508 +#define AUFS_BRPERM_RR         "rr"
32509 +#define AUFS_BRRATTR_WH                "wh"
32510 +#define AUFS_BRWATTR_NLWH      "nolwh"
32511 +#define AUFS_BRATTR_UNPIN      "unpin"
32512 +
32513 +/* ---------------------------------------------------------------------- */
32514 +
32515 +/* ioctl */
32516 +enum {
32517 +       /* readdir in userspace */
32518 +       AuCtl_RDU,
32519 +       AuCtl_RDU_INO,
32520 +
32521 +       /* pathconf wrapper */
32522 +       AuCtl_WBR_FD,
32523 +
32524 +       /* busy inode */
32525 +       AuCtl_IBUSY,
32526 +
32527 +       /* move-down */
32528 +       AuCtl_MVDOWN
32529 +};
32530 +
32531 +/* borrowed from linux/include/linux/kernel.h */
32532 +#ifndef ALIGN
32533 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
32534 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
32535 +#endif
32536 +
32537 +/* borrowed from linux/include/linux/compiler-gcc3.h */
32538 +#ifndef __aligned
32539 +#define __aligned(x)                   __attribute__((aligned(x)))
32540 +#endif
32541 +
32542 +#ifdef __KERNEL__
32543 +#ifndef __packed
32544 +#define __packed                       __attribute__((packed))
32545 +#endif
32546 +#endif
32547 +
32548 +struct au_rdu_cookie {
32549 +       uint64_t        h_pos;
32550 +       int16_t         bindex;
32551 +       uint8_t         flags;
32552 +       uint8_t         pad;
32553 +       uint32_t        generation;
32554 +} __aligned(8);
32555 +
32556 +struct au_rdu_ent {
32557 +       uint64_t        ino;
32558 +       int16_t         bindex;
32559 +       uint8_t         type;
32560 +       uint8_t         nlen;
32561 +       uint8_t         wh;
32562 +       char            name[0];
32563 +} __aligned(8);
32564 +
32565 +static inline int au_rdu_len(int nlen)
32566 +{
32567 +       /* include the terminating NULL */
32568 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
32569 +                    sizeof(uint64_t));
32570 +}
32571 +
32572 +union au_rdu_ent_ul {
32573 +       struct au_rdu_ent __user        *e;
32574 +       uint64_t                        ul;
32575 +};
32576 +
32577 +enum {
32578 +       AufsCtlRduV_SZ,
32579 +       AufsCtlRduV_End
32580 +};
32581 +
32582 +struct aufs_rdu {
32583 +       /* input */
32584 +       union {
32585 +               uint64_t        sz;     /* AuCtl_RDU */
32586 +               uint64_t        nent;   /* AuCtl_RDU_INO */
32587 +       };
32588 +       union au_rdu_ent_ul     ent;
32589 +       uint16_t                verify[AufsCtlRduV_End];
32590 +
32591 +       /* input/output */
32592 +       uint32_t                blk;
32593 +
32594 +       /* output */
32595 +       union au_rdu_ent_ul     tail;
32596 +       /* number of entries which were added in a single call */
32597 +       uint64_t                rent;
32598 +       uint8_t                 full;
32599 +       uint8_t                 shwh;
32600 +
32601 +       struct au_rdu_cookie    cookie;
32602 +} __aligned(8);
32603 +
32604 +/* ---------------------------------------------------------------------- */
32605 +
32606 +struct aufs_wbr_fd {
32607 +       uint32_t        oflags;
32608 +       int16_t         brid;
32609 +} __aligned(8);
32610 +
32611 +/* ---------------------------------------------------------------------- */
32612 +
32613 +struct aufs_ibusy {
32614 +       uint64_t        ino, h_ino;
32615 +       int16_t         bindex;
32616 +} __aligned(8);
32617 +
32618 +/* ---------------------------------------------------------------------- */
32619 +
32620 +/* error code for move-down */
32621 +/* the actual message strings are implemented in aufs-util.git */
32622 +enum {
32623 +       EAU_MVDOWN_OPAQUE = 1,
32624 +       EAU_MVDOWN_WHITEOUT,
32625 +       EAU_MVDOWN_UPPER,
32626 +       EAU_MVDOWN_BOTTOM,
32627 +       EAU_MVDOWN_NOUPPER,
32628 +       EAU_MVDOWN_NOLOWERBR,
32629 +       EAU_Last
32630 +};
32631 +
32632 +/* flags for move-down */
32633 +#define AUFS_MVDOWN_DMSG       1
32634 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
32635 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
32636 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
32637 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
32638 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
32639 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
32640 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
32641 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
32642 +/* will be added more */
32643 +
32644 +enum {
32645 +       AUFS_MVDOWN_UPPER,
32646 +       AUFS_MVDOWN_LOWER,
32647 +       AUFS_MVDOWN_NARRAY
32648 +};
32649 +
32650 +struct aufs_mvdown {
32651 +       uint32_t        flags;
32652 +       struct {
32653 +               int16_t         bindex;
32654 +               int16_t         brid;
32655 +       } a[AUFS_MVDOWN_NARRAY];
32656 +       int8_t          au_errno;
32657 +       /* will be added more */
32658 +} __aligned(8);
32659 +
32660 +/* ---------------------------------------------------------------------- */
32661 +
32662 +#define AuCtlType              'A'
32663 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
32664 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
32665 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
32666 +                                    struct aufs_wbr_fd)
32667 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
32668 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
32669 +                                     struct aufs_mvdown)
32670 +
32671 +#endif /* __AUFS_TYPE_H__ */
32672 aufs3.15 loopback patch
32673
32674 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
32675 index 138104b..227f7fa 100644
32676 --- a/drivers/block/loop.c
32677 +++ b/drivers/block/loop.c
32678 @@ -514,7 +514,7 @@ out:
32679  }
32680  
32681  struct switch_request {
32682 -       struct file *file;
32683 +       struct file *file, *virt_file;
32684         struct completion wait;
32685  };
32686  
32687 @@ -576,7 +576,8 @@ static int loop_thread(void *data)
32688   * First it needs to flush existing IO, it does this by sending a magic
32689   * BIO down the pipe. The completion of this BIO does the actual switch.
32690   */
32691 -static int loop_switch(struct loop_device *lo, struct file *file)
32692 +static int loop_switch(struct loop_device *lo, struct file *file,
32693 +                      struct file *virt_file)
32694  {
32695         struct switch_request w;
32696         struct bio *bio = bio_alloc(GFP_KERNEL, 0);
32697 @@ -584,6 +585,7 @@ static int loop_switch(struct loop_device *lo, struct file *file)
32698                 return -ENOMEM;
32699         init_completion(&w.wait);
32700         w.file = file;
32701 +       w.virt_file = virt_file;
32702         bio->bi_private = &w;
32703         bio->bi_bdev = NULL;
32704         loop_make_request(lo->lo_queue, bio);
32705 @@ -600,7 +602,7 @@ static int loop_flush(struct loop_device *lo)
32706         if (!lo->lo_thread)
32707                 return 0;
32708  
32709 -       return loop_switch(lo, NULL);
32710 +       return loop_switch(lo, NULL, NULL);
32711  }
32712  
32713  /*
32714 @@ -619,6 +621,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
32715         mapping = file->f_mapping;
32716         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
32717         lo->lo_backing_file = file;
32718 +       lo->lo_backing_virt_file = p->virt_file;
32719         lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
32720                 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
32721         lo->old_gfp_mask = mapping_gfp_mask(mapping);
32722 @@ -627,6 +630,13 @@ out:
32723         complete(&p->wait);
32724  }
32725  
32726 +static struct file *loop_real_file(struct file *file)
32727 +{
32728 +       struct file *f = NULL;
32729 +       if (file->f_dentry->d_sb->s_op->real_loop)
32730 +               f = file->f_dentry->d_sb->s_op->real_loop(file);
32731 +       return f;
32732 +}
32733  
32734  /*
32735   * loop_change_fd switched the backing store of a loopback device to
32736 @@ -640,6 +650,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
32737                           unsigned int arg)
32738  {
32739         struct file     *file, *old_file;
32740 +       struct file     *f, *virt_file = NULL, *old_virt_file;
32741         struct inode    *inode;
32742         int             error;
32743  
32744 @@ -656,9 +667,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
32745         file = fget(arg);
32746         if (!file)
32747                 goto out;
32748 +       f = loop_real_file(file);
32749 +       if (f) {
32750 +               virt_file = file;
32751 +               file = f;
32752 +               get_file(file);
32753 +       }
32754  
32755         inode = file->f_mapping->host;
32756         old_file = lo->lo_backing_file;
32757 +       old_virt_file = lo->lo_backing_virt_file;
32758  
32759         error = -EINVAL;
32760  
32761 @@ -670,17 +688,21 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
32762                 goto out_putf;
32763  
32764         /* and ... switch */
32765 -       error = loop_switch(lo, file);
32766 +       error = loop_switch(lo, file, virt_file);
32767         if (error)
32768                 goto out_putf;
32769  
32770         fput(old_file);
32771 +       if (old_virt_file)
32772 +               fput(old_virt_file);
32773         if (lo->lo_flags & LO_FLAGS_PARTSCAN)
32774                 ioctl_by_bdev(bdev, BLKRRPART, 0);
32775         return 0;
32776  
32777   out_putf:
32778         fput(file);
32779 +       if (virt_file)
32780 +               fput(virt_file);
32781   out:
32782         return error;
32783  }
32784 @@ -841,7 +863,7 @@ static void loop_config_discard(struct loop_device *lo)
32785  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
32786                        struct block_device *bdev, unsigned int arg)
32787  {
32788 -       struct file     *file, *f;
32789 +       struct file     *file, *f, *virt_file = NULL;
32790         struct inode    *inode;
32791         struct address_space *mapping;
32792         unsigned lo_blocksize;
32793 @@ -856,6 +878,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
32794         file = fget(arg);
32795         if (!file)
32796                 goto out;
32797 +       f = loop_real_file(file);
32798 +       if (f) {
32799 +               virt_file = file;
32800 +               file = f;
32801 +               get_file(file);
32802 +       }
32803  
32804         error = -EBUSY;
32805         if (lo->lo_state != Lo_unbound)
32806 @@ -904,6 +932,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
32807         lo->lo_device = bdev;
32808         lo->lo_flags = lo_flags;
32809         lo->lo_backing_file = file;
32810 +       lo->lo_backing_virt_file = virt_file;
32811         lo->transfer = transfer_none;
32812         lo->ioctl = NULL;
32813         lo->lo_sizelimit = 0;
32814 @@ -948,6 +977,7 @@ out_clr:
32815         lo->lo_thread = NULL;
32816         lo->lo_device = NULL;
32817         lo->lo_backing_file = NULL;
32818 +       lo->lo_backing_virt_file = NULL;
32819         lo->lo_flags = 0;
32820         set_capacity(lo->lo_disk, 0);
32821         invalidate_bdev(bdev);
32822 @@ -957,6 +987,8 @@ out_clr:
32823         lo->lo_state = Lo_unbound;
32824   out_putf:
32825         fput(file);
32826 +       if (virt_file)
32827 +               fput(virt_file);
32828   out:
32829         /* This is safe: open() is still holding a reference. */
32830         module_put(THIS_MODULE);
32831 @@ -1003,6 +1035,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
32832  static int loop_clr_fd(struct loop_device *lo)
32833  {
32834         struct file *filp = lo->lo_backing_file;
32835 +       struct file *virt_filp = lo->lo_backing_virt_file;
32836         gfp_t gfp = lo->old_gfp_mask;
32837         struct block_device *bdev = lo->lo_device;
32838  
32839 @@ -1036,6 +1069,7 @@ static int loop_clr_fd(struct loop_device *lo)
32840  
32841         spin_lock_irq(&lo->lo_lock);
32842         lo->lo_backing_file = NULL;
32843 +       lo->lo_backing_virt_file = NULL;
32844         spin_unlock_irq(&lo->lo_lock);
32845  
32846         loop_release_xfer(lo);
32847 @@ -1078,6 +1112,8 @@ static int loop_clr_fd(struct loop_device *lo)
32848          * bd_mutex which is usually taken before lo_ctl_mutex.
32849          */
32850         fput(filp);
32851 +       if (virt_filp)
32852 +               fput(virt_filp);
32853         return 0;
32854  }
32855  
32856 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
32857 index 90df5d6..cb91822 100644
32858 --- a/drivers/block/loop.h
32859 +++ b/drivers/block/loop.h
32860 @@ -44,7 +44,7 @@ struct loop_device {
32861         int             (*ioctl)(struct loop_device *, int cmd, 
32862                                  unsigned long arg); 
32863  
32864 -       struct file *   lo_backing_file;
32865 +       struct file *   lo_backing_file, *lo_backing_virt_file;
32866         struct block_device *lo_device;
32867         unsigned        lo_blocksize;
32868         void            *key_data; 
32869 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
32870 index 510919e..f9317c9 100644
32871 --- a/fs/aufs/f_op.c
32872 +++ b/fs/aufs/f_op.c
32873 @@ -345,7 +345,7 @@ static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
32874         err = -EINVAL;
32875         h_file = au_hf_top(file);
32876         get_file(h_file);
32877 -       if (au_test_loopback_kthread()) {
32878 +       if (0 && au_test_loopback_kthread()) {
32879                 au_warn_loopback(h_file->f_dentry->d_sb);
32880                 if (file->f_mapping != h_file->f_mapping) {
32881                         file->f_mapping = h_file->f_mapping;
32882 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
32883 index 3b03b52..4ab749d 100644
32884 --- a/fs/aufs/loop.c
32885 +++ b/fs/aufs/loop.c
32886 @@ -130,3 +130,19 @@ void au_loopback_fin(void)
32887         symbol_put(loop_backing_file);
32888         kfree(au_warn_loopback_array);
32889  }
32890 +
32891 +/* ---------------------------------------------------------------------- */
32892 +
32893 +/* support the loopback block device insude aufs */
32894 +
32895 +struct file *aufs_real_loop(struct file *file)
32896 +{
32897 +       struct file *f;
32898 +
32899 +       BUG_ON(!au_test_aufs(file->f_dentry->d_sb));
32900 +       fi_read_lock(file);
32901 +       f = au_hf_top(file);
32902 +       fi_read_unlock(file);
32903 +       AuDebugOn(!f);
32904 +       return f;
32905 +}
32906 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
32907 index da8b756..28cb7ea 100644
32908 --- a/fs/aufs/loop.h
32909 +++ b/fs/aufs/loop.h
32910 @@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
32911  
32912  int au_loopback_init(void);
32913  void au_loopback_fin(void);
32914 +
32915 +struct file *aufs_real_loop(struct file *file);
32916  #else
32917 +AuStub(struct file *, loop_backing_file, return NULL)
32918 +
32919  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
32920            struct dentry *h_adding)
32921  AuStubInt0(au_test_loopback_kthread, void)
32922 @@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
32923  
32924  AuStubInt0(au_loopback_init, void)
32925  AuStubVoid(au_loopback_fin, void)
32926 +
32927 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
32928  #endif /* BLK_DEV_LOOP */
32929  
32930  #endif /* __KERNEL__ */
32931 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
32932 index b609e5a..e3909ed 100644
32933 --- a/fs/aufs/super.c
32934 +++ b/fs/aufs/super.c
32935 @@ -807,7 +807,10 @@ static const struct super_operations aufs_sop = {
32936         .statfs         = aufs_statfs,
32937         .put_super      = aufs_put_super,
32938         .sync_fs        = aufs_sync_fs,
32939 -       .remount_fs     = aufs_remount_fs
32940 +       .remount_fs     = aufs_remount_fs,
32941 +#ifdef CONFIG_AUFS_BDEV_LOOP
32942 +       .real_loop      = aufs_real_loop
32943 +#endif
32944  };
32945  
32946  /* ---------------------------------------------------------------------- */
32947 diff --git a/include/linux/fs.h b/include/linux/fs.h
32948 index 3ff3e89..13017e6 100644
32949 --- a/include/linux/fs.h
32950 +++ b/include/linux/fs.h
32951 @@ -1554,6 +1554,10 @@ struct super_operations {
32952         int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
32953         long (*nr_cached_objects)(struct super_block *, int);
32954         long (*free_cached_objects)(struct super_block *, long, int);
32955 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
32956 +       /* and aufs */
32957 +       struct file *(*real_loop)(struct file *);
32958 +#endif
32959  };
32960  
32961  /*
This page took 2.665175 seconds and 3 git commands to generate.