]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs3.patch
- updated imq patch to latest upstream
[packages/kernel.git] / kernel-aufs3.patch
1 aufs3.x-rcN kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index ec35851..a059d62 100644
5 --- a/fs/Kconfig
6 +++ b/fs/Kconfig
7 @@ -218,6 +218,7 @@ source "fs/sysv/Kconfig"
8  source "fs/ufs/Kconfig"
9  source "fs/exofs/Kconfig"
10  source "fs/f2fs/Kconfig"
11 +source "fs/aufs/Kconfig"
12  
13  endif # MISC_FILESYSTEMS
14  
15 diff --git a/fs/Makefile b/fs/Makefile
16 index a88ac48..162500e 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 68ceb97..0352fa4 100644
26 --- a/include/uapi/linux/Kbuild
27 +++ b/include/uapi/linux/Kbuild
28 @@ -58,6 +58,7 @@ header-y += atmsvc.h
29  header-y += atm_tcp.h
30  header-y += atm_zatm.h
31  header-y += audit.h
32 +header-y += aufs_type.h
33  header-y += auto_fs4.h
34  header-y += auto_fs.h
35  header-y += auxvec.h
36 aufs3.x-rcN base patch
37
38 diff --git a/MAINTAINERS b/MAINTAINERS
39 index efbcb50..30a1654 100644
40 --- a/MAINTAINERS
41 +++ b/MAINTAINERS
42 @@ -1864,6 +1864,20 @@ F:       include/linux/audit.h
43  F:     include/uapi/linux/audit.h
44  F:     kernel/audit*
45  
46 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
47 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
48 +L:     linux-unionfs@vger.kernel.org
49 +L:     aufs-users@lists.sourceforge.net (members only)
50 +W:     http://aufs.sourceforge.net
51 +T:     git://git.code.sf.net/p/aufs/aufs3-linux
52 +T:     git://github.com/sfjro/aufs3-linux.git
53 +S:     Supported
54 +F:     Documentation/filesystems/aufs/
55 +F:     Documentation/ABI/testing/debugfs-aufs
56 +F:     Documentation/ABI/testing/sysfs-aufs
57 +F:     fs/aufs/
58 +F:     include/uapi/linux/aufs_type.h
59 +
60  AUXILIARY DISPLAY DRIVERS
61  M:     Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
62  W:     http://miguelojeda.es/auxdisplay.htm
63 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
64 index d1f168b..a0da519 100644
65 --- a/drivers/block/loop.c
66 +++ b/drivers/block/loop.c
67 @@ -592,6 +592,24 @@ static inline int is_loop_device(struct file *file)
68         return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
69  }
70  
71 +/*
72 + * for AUFS
73 + * no get/put for file.
74 + */
75 +struct file *loop_backing_file(struct super_block *sb)
76 +{
77 +       struct file *ret;
78 +       struct loop_device *l;
79 +
80 +       ret = NULL;
81 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
82 +               l = sb->s_bdev->bd_disk->private_data;
83 +               ret = l->lo_backing_file;
84 +       }
85 +       return ret;
86 +}
87 +EXPORT_SYMBOL(loop_backing_file);
88 +
89  /* loop sysfs attributes */
90  
91  static ssize_t loop_attr_show(struct device *dev, char *page,
92 diff --git a/fs/dcache.c b/fs/dcache.c
93 index c71e373..b93a9f2 100644
94 --- a/fs/dcache.c
95 +++ b/fs/dcache.c
96 @@ -1130,7 +1130,7 @@ enum d_walk_ret {
97   *
98   * The @enter() and @finish() callbacks are called with d_lock held.
99   */
100 -static void d_walk(struct dentry *parent, void *data,
101 +void d_walk(struct dentry *parent, void *data,
102                    enum d_walk_ret (*enter)(void *, struct dentry *),
103                    void (*finish)(void *))
104  {
105 diff --git a/fs/splice.c b/fs/splice.c
106 index 7968da9..beb9993 100644
107 --- a/fs/splice.c
108 +++ b/fs/splice.c
109 @@ -1099,8 +1099,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
110  /*
111   * Attempt to initiate a splice from pipe to file.
112   */
113 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
114 -                          loff_t *ppos, size_t len, unsigned int flags)
115 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
116 +                   loff_t *ppos, size_t len, unsigned int flags)
117  {
118         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
119                                 loff_t *, size_t, unsigned int);
120 @@ -1116,9 +1116,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
121  /*
122   * Attempt to initiate a splice from a file to a pipe.
123   */
124 -static long do_splice_to(struct file *in, loff_t *ppos,
125 -                        struct pipe_inode_info *pipe, size_t len,
126 -                        unsigned int flags)
127 +long do_splice_to(struct file *in, loff_t *ppos,
128 +                 struct pipe_inode_info *pipe, size_t len,
129 +                 unsigned int flags)
130  {
131         ssize_t (*splice_read)(struct file *, loff_t *,
132                                struct pipe_inode_info *, size_t, unsigned int);
133 diff --git a/include/linux/file.h b/include/linux/file.h
134 index f87d308..9a290b3 100644
135 --- a/include/linux/file.h
136 +++ b/include/linux/file.h
137 @@ -19,6 +19,7 @@ struct dentry;
138  struct path;
139  extern struct file *alloc_file(struct path *, fmode_t mode,
140         const struct file_operations *fop);
141 +extern struct file *get_empty_filp(void);
142  
143  static inline void fput_light(struct file *file, int fput_needed)
144  {
145 diff --git a/include/linux/splice.h b/include/linux/splice.h
146 index da2751d..2e0fca6 100644
147 --- a/include/linux/splice.h
148 +++ b/include/linux/splice.h
149 @@ -83,4 +83,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
150  extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
151  
152  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
153 +
154 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
155 +                          loff_t *ppos, size_t len, unsigned int flags);
156 +extern long do_splice_to(struct file *in, loff_t *ppos,
157 +                        struct pipe_inode_info *pipe, size_t len,
158 +                        unsigned int flags);
159  #endif
160 aufs3.x-rcN mmap patch
161
162 diff --git a/fs/buffer.c b/fs/buffer.c
163 index 20805db..363569f 100644
164 --- a/fs/buffer.c
165 +++ b/fs/buffer.c
166 @@ -2450,7 +2450,7 @@ int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
167          * Update file times before taking page lock. We may end up failing the
168          * fault so this update may be superfluous but who really cares...
169          */
170 -       file_update_time(vma->vm_file);
171 +       vma_file_update_time(vma);
172  
173         ret = __block_page_mkwrite(vma, vmf, get_block);
174         sb_end_pagefault(sb);
175 diff --git a/fs/proc/base.c b/fs/proc/base.c
176 index 3f3d7ae..426bcc7 100644
177 --- a/fs/proc/base.c
178 +++ b/fs/proc/base.c
179 @@ -1735,7 +1735,7 @@ static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
180         down_read(&mm->mmap_sem);
181         vma = find_exact_vma(mm, vm_start, vm_end);
182         if (vma && vma->vm_file) {
183 -               *path = vma->vm_file->f_path;
184 +               *path = vma_pr_or_file(vma)->f_path;
185                 path_get(path);
186                 rc = 0;
187         }
188 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
189 index d4a3574..1397181 100644
190 --- a/fs/proc/nommu.c
191 +++ b/fs/proc/nommu.c
192 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
193         file = region->vm_file;
194  
195         if (file) {
196 -               struct inode *inode = file_inode(region->vm_file);
197 +               struct inode *inode;
198 +
199 +               file = vmr_pr_or_file(region);
200 +               inode = file_inode(file);
201                 dev = inode->i_sb->s_dev;
202                 ino = inode->i_ino;
203         }
204 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
205 index 6dee68d..9afa35d 100644
206 --- a/fs/proc/task_mmu.c
207 +++ b/fs/proc/task_mmu.c
208 @@ -279,7 +279,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
209         const char *name = NULL;
210  
211         if (file) {
212 -               struct inode *inode = file_inode(vma->vm_file);
213 +               struct inode *inode;
214 +
215 +               file = vma_pr_or_file(vma);
216 +               inode = file_inode(file);
217                 dev = inode->i_sb->s_dev;
218                 ino = inode->i_ino;
219                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
220 @@ -1479,7 +1482,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
221         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
222         struct vm_area_struct *vma = v;
223         struct numa_maps *md = &numa_priv->md;
224 -       struct file *file = vma->vm_file;
225 +       struct file *file = vma_pr_or_file(vma);
226         struct mm_struct *mm = vma->vm_mm;
227         struct mm_walk walk = {
228                 .hugetlb_entry = gather_hugetlb_stats,
229 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
230 index 599ec2e..1740207 100644
231 --- a/fs/proc/task_nommu.c
232 +++ b/fs/proc/task_nommu.c
233 @@ -160,7 +160,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
234         file = vma->vm_file;
235  
236         if (file) {
237 -               struct inode *inode = file_inode(vma->vm_file);
238 +               struct inode *inode;
239 +
240 +               file = vma_pr_or_file(vma);
241 +               inode = file_inode(file);
242                 dev = inode->i_sb->s_dev;
243                 ino = inode->i_ino;
244                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
245 diff --git a/include/linux/mm.h b/include/linux/mm.h
246 index 47a9392..c8f8b46 100644
247 --- a/include/linux/mm.h
248 +++ b/include/linux/mm.h
249 @@ -1254,6 +1254,28 @@ static inline int fixup_user_fault(struct task_struct *tsk,
250  }
251  #endif
252  
253 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
254 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
255 +                                     int);
256 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
257 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
258 +
259 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
260 +                                                               __LINE__)
261 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
262 +                                                         __LINE__)
263 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
264 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
265 +
266 +#ifndef CONFIG_MMU
267 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
268 +extern void vmr_do_fput(struct vm_region *, const char[], int);
269 +
270 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
271 +                                                         __LINE__)
272 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
273 +#endif /* !CONFIG_MMU */
274 +
275  extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
276  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
277                 void *buf, int len, int write);
278 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
279 index 199a03a..ed38ea3 100644
280 --- a/include/linux/mm_types.h
281 +++ b/include/linux/mm_types.h
282 @@ -241,6 +241,7 @@ struct vm_region {
283         unsigned long   vm_top;         /* region allocated to here */
284         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
285         struct file     *vm_file;       /* the backing file or NULL */
286 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
287  
288         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
289         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
290 @@ -305,6 +306,7 @@ struct vm_area_struct {
291         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
292                                            units, *not* PAGE_CACHE_SIZE */
293         struct file * vm_file;          /* File we map to (can be NULL). */
294 +       struct file *vm_prfile;         /* shadow of vm_file */
295         void * vm_private_data;         /* was vm_pte (shared mem) */
296  
297  #ifndef CONFIG_MMU
298 diff --git a/kernel/fork.c b/kernel/fork.c
299 index cf65139..f28a048 100644
300 --- a/kernel/fork.c
301 +++ b/kernel/fork.c
302 @@ -430,7 +430,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
303                         struct inode *inode = file_inode(file);
304                         struct address_space *mapping = file->f_mapping;
305  
306 -                       get_file(file);
307 +                       vma_get_file(tmp);
308                         if (tmp->vm_flags & VM_DENYWRITE)
309                                 atomic_dec(&inode->i_writecount);
310                         i_mmap_lock_write(mapping);
311 diff --git a/mm/Makefile b/mm/Makefile
312 index 15dbe99..88e25b5 100644
313 --- a/mm/Makefile
314 +++ b/mm/Makefile
315 @@ -21,7 +21,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o \
316                            mm_init.o mmu_context.o percpu.o slab_common.o \
317                            compaction.o vmacache.o \
318                            interval_tree.o list_lru.o workingset.o \
319 -                          debug.o $(mmu-y)
320 +                          prfile.o debug.o $(mmu-y)
321  
322  obj-y += init-mm.o
323  
324 diff --git a/mm/filemap.c b/mm/filemap.c
325 index ad72420..8885258 100644
326 --- a/mm/filemap.c
327 +++ b/mm/filemap.c
328 @@ -2064,7 +2064,7 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
329         int ret = VM_FAULT_LOCKED;
330  
331         sb_start_pagefault(inode->i_sb);
332 -       file_update_time(vma->vm_file);
333 +       vma_file_update_time(vma);
334         lock_page(page);
335         if (page->mapping != inode->i_mapping) {
336                 unlock_page(page);
337 diff --git a/mm/madvise.c b/mm/madvise.c
338 index d551475..1ebf71b 100644
339 --- a/mm/madvise.c
340 +++ b/mm/madvise.c
341 @@ -320,12 +320,12 @@ static long madvise_remove(struct vm_area_struct *vma,
342          * vma's reference to the file) can go away as soon as we drop
343          * mmap_sem.
344          */
345 -       get_file(f);
346 +       vma_get_file(vma);
347         up_read(&current->mm->mmap_sem);
348         error = vfs_fallocate(f,
349                                 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
350                                 offset, end - start);
351 -       fput(f);
352 +       vma_fput(vma);
353         down_read(&current->mm->mmap_sem);
354         return error;
355  }
356 diff --git a/mm/memory.c b/mm/memory.c
357 index 97839f5..5e5d491 100644
358 --- a/mm/memory.c
359 +++ b/mm/memory.c
360 @@ -2034,7 +2034,7 @@ static inline int wp_page_reuse(struct m
361                 }
362  
363                 if (!page_mkwrite)
364 -                       file_update_time(vma->vm_file);
365 +                       vma_file_update_time(vma);
366         }
367  
368         return VM_FAULT_WRITE;
369 diff --git a/mm/mmap.c b/mm/mmap.c
370 index 9ec50a3..4a6a641 100644
371 --- a/mm/mmap.c
372 +++ b/mm/mmap.c
373 @@ -274,7 +274,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
374         if (vma->vm_ops && vma->vm_ops->close)
375                 vma->vm_ops->close(vma);
376         if (vma->vm_file)
377 -               fput(vma->vm_file);
378 +               vma_fput(vma);
379         mpol_put(vma_policy(vma));
380         kmem_cache_free(vm_area_cachep, vma);
381         return next;
382 @@ -886,7 +886,7 @@ again:                      remove_next = 1 + (end > next->vm_end);
383         if (remove_next) {
384                 if (file) {
385                         uprobe_munmap(next, next->vm_start, next->vm_end);
386 -                       fput(file);
387 +                       vma_fput(vma);
388                 }
389                 if (next->anon_vma)
390                         anon_vma_merge(vma, next);
391 @@ -1671,8 +1671,8 @@ out:
392         return addr;
393  
394  unmap_and_free_vma:
395 +       vma_fput(vma);
396         vma->vm_file = NULL;
397 -       fput(file);
398  
399         /* Undo any partial mapping done by a device driver. */
400         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
401 @@ -2473,7 +2473,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
402                 goto out_free_mpol;
403  
404         if (new->vm_file)
405 -               get_file(new->vm_file);
406 +               vma_get_file(new);
407  
408         if (new->vm_ops && new->vm_ops->open)
409                 new->vm_ops->open(new);
410 @@ -2492,7 +2492,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
411         if (new->vm_ops && new->vm_ops->close)
412                 new->vm_ops->close(new);
413         if (new->vm_file)
414 -               fput(new->vm_file);
415 +               vma_fput(new);
416         unlink_anon_vmas(new);
417   out_free_mpol:
418         mpol_put(vma_policy(new));
419 @@ -2635,7 +2635,6 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
420         struct vm_area_struct *vma;
421         unsigned long populate = 0;
422         unsigned long ret = -EINVAL;
423 -       struct file *file;
424  
425         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. "
426                         "See Documentation/vm/remap_file_pages.txt.\n",
427 @@ -2679,10 +2678,10 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
428                 munlock_vma_pages_range(vma, start, start + size);
429         }
430  
431 -       file = get_file(vma->vm_file);
432 +       vma_get_file(vma);
433         ret = do_mmap_pgoff(vma->vm_file, start, size,
434                         prot, flags, pgoff, &populate);
435 -       fput(file);
436 +       vma_fput(vma);
437  out:
438         up_write(&mm->mmap_sem);
439         if (populate)
440 @@ -2950,7 +2949,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
441                         if (anon_vma_clone(new_vma, vma))
442                                 goto out_free_mempol;
443                         if (new_vma->vm_file)
444 -                               get_file(new_vma->vm_file);
445 +                               vma_get_file(new_vma);
446                         if (new_vma->vm_ops && new_vma->vm_ops->open)
447                                 new_vma->vm_ops->open(new_vma);
448                         vma_link(mm, new_vma, prev, rb_link, rb_parent);
449 diff --git a/mm/msync.c b/mm/msync.c
450 index bb04d53..5c24c54 100644
451 --- a/mm/msync.c
452 +++ b/mm/msync.c
453 @@ -84,10 +84,10 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
454                 start = vma->vm_end;
455                 if ((flags & MS_SYNC) && file &&
456                                 (vma->vm_flags & VM_SHARED)) {
457 -                       get_file(file);
458 +                       vma_get_file(vma);
459                         up_read(&mm->mmap_sem);
460                         error = vfs_fsync_range(file, fstart, fend, 1);
461 -                       fput(file);
462 +                       vma_fput(vma);
463                         if (error || start >= end)
464                                 goto out;
465                         down_read(&mm->mmap_sem);
466 diff --git a/mm/nommu.c b/mm/nommu.c
467 index 3fba2dc..870ce96 100644
468 --- a/mm/nommu.c
469 +++ b/mm/nommu.c
470 @@ -693,7 +693,7 @@ static void __put_nommu_region(struct vm_region *region)
471                 up_write(&nommu_region_sem);
472  
473                 if (region->vm_file)
474 -                       fput(region->vm_file);
475 +                       vmr_fput(region);
476  
477                 /* IO memory and memory shared directly out of the pagecache
478                  * from ramfs/tmpfs mustn't be released here */
479 @@ -858,7 +858,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
480         if (vma->vm_ops && vma->vm_ops->close)
481                 vma->vm_ops->close(vma);
482         if (vma->vm_file)
483 -               fput(vma->vm_file);
484 +               vma_fput(vma);
485         put_nommu_region(vma->vm_region);
486         kmem_cache_free(vm_area_cachep, vma);
487  }
488 @@ -1398,7 +1398,7 @@ unsigned long do_mmap_pgoff(struct file *file,
489                                         goto error_just_free;
490                                 }
491                         }
492 -                       fput(region->vm_file);
493 +                       vmr_fput(region);
494                         kmem_cache_free(vm_region_jar, region);
495                         region = pregion;
496                         result = start;
497 @@ -1474,10 +1474,10 @@ error_just_free:
498         up_write(&nommu_region_sem);
499  error:
500         if (region->vm_file)
501 -               fput(region->vm_file);
502 +               vmr_fput(region);
503         kmem_cache_free(vm_region_jar, region);
504         if (vma->vm_file)
505 -               fput(vma->vm_file);
506 +               vma_fput(vma);
507         kmem_cache_free(vm_area_cachep, vma);
508         kleave(" = %d", ret);
509         return ret;
510 diff --git a/mm/prfile.c b/mm/prfile.c
511 new file mode 100644
512 index 0000000..6aa5ab5
513 --- /dev/null
514 +++ b/mm/prfile.c
515 @@ -0,0 +1,86 @@
516 +/*
517 + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
518 + * in /proc/PID/maps.
519 + * Call these functions via macros defined in linux/mm.h.
520 + *
521 + * See Documentation/filesystems/aufs/design/06mmap.txt
522 + *
523 + * Copyright (c) 2014 Junjro R. Okajima
524 + * Copyright (c) 2014 Ian Campbell
525 + */
526 +
527 +#include <linux/mm.h>
528 +#include <linux/file.h>
529 +#include <linux/fs.h>
530 +
531 +/* #define PRFILE_TRACE */
532 +static inline void prfile_trace(struct file *f, struct file *pr,
533 +                             const char func[], int line, const char func2[])
534 +{
535 +#ifdef PRFILE_TRACE
536 +       if (pr)
537 +               pr_info("%s:%d: %s, %p\n", func, line, func2,
538 +                       f ? (char *)f->f_path.dentry->d_name.name : "(null)");
539 +#endif
540 +}
541 +
542 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
543 +                            int line)
544 +{
545 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
546 +
547 +       prfile_trace(f, pr, func, line, __func__);
548 +       file_update_time(f);
549 +       if (f && pr)
550 +               file_update_time(pr);
551 +}
552 +
553 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
554 +                              int line)
555 +{
556 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
557 +
558 +       prfile_trace(f, pr, func, line, __func__);
559 +       return (f && pr) ? pr : f;
560 +}
561 +
562 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
563 +{
564 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
565 +
566 +       prfile_trace(f, pr, func, line, __func__);
567 +       get_file(f);
568 +       if (f && pr)
569 +               get_file(pr);
570 +}
571 +
572 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
573 +{
574 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
575 +
576 +       prfile_trace(f, pr, func, line, __func__);
577 +       fput(f);
578 +       if (f && pr)
579 +               fput(pr);
580 +}
581 +
582 +#ifndef CONFIG_MMU
583 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
584 +                              int line)
585 +{
586 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
587 +
588 +       prfile_trace(f, pr, func, line, __func__);
589 +       return (f && pr) ? pr : f;
590 +}
591 +
592 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
593 +{
594 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
595 +
596 +       prfile_trace(f, pr, func, line, __func__);
597 +       fput(f);
598 +       if (f && pr)
599 +               fput(pr);
600 +}
601 +#endif /* !CONFIG_MMU */
602 aufs3.x-rcN standalone patch
603
604 diff --git a/fs/dcache.c b/fs/dcache.c
605 index b93a9f2..5a010ee 100644
606 --- a/fs/dcache.c
607 +++ b/fs/dcache.c
608 @@ -1235,6 +1235,7 @@ rename_retry:
609         seq = 1;
610         goto again;
611  }
612 +EXPORT_SYMBOL(d_walk);
613  
614  /*
615   * Search for at least 1 mount point in the dentry's subdirs.
616 diff --git a/fs/file_table.c b/fs/file_table.c
617 index 3f85411..00e9dcf 100644
618 --- a/fs/file_table.c
619 +++ b/fs/file_table.c
620 @@ -147,6 +147,7 @@ over:
621         }
622         return ERR_PTR(-ENFILE);
623  }
624 +EXPORT_SYMBOL(get_empty_filp);
625  
626  /**
627   * alloc_file - allocate and initialize a 'struct file'
628 @@ -308,6 +309,7 @@ void put_filp(struct file *file)
629                 file_free(file);
630         }
631  }
632 +EXPORT_SYMBOL(put_filp);
633  
634  void __init files_init(unsigned long mempages)
635  { 
636 diff --git a/fs/inode.c b/fs/inode.c
637 index f00b16f..eb7b8b5 100644
638 --- a/fs/inode.c
639 +++ b/fs/inode.c
640 @@ -58,6 +58,7 @@ static struct hlist_head *inode_hashtable __read_mostly;
641  static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
642  
643  __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
644 +EXPORT_SYMBOL(inode_sb_list_lock);
645  
646  /*
647   * Empty aops. Can be used for the cases where the user does not
648 diff --git a/fs/namespace.c b/fs/namespace.c
649 index 82ef140..a3aa4b9 100644
650 --- a/fs/namespace.c
651 +++ b/fs/namespace.c
652 @@ -463,6 +463,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
653         mnt_dec_writers(real_mount(mnt));
654         preempt_enable();
655  }
656 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
657  
658  /**
659   * mnt_drop_write - give up write access to a mount
660 @@ -1718,6 +1719,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
661         }
662         return 0;
663  }
664 +EXPORT_SYMBOL(iterate_mounts);
665  
666  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
667  {
668 diff --git a/fs/notify/group.c b/fs/notify/group.c
669 index d16b62c..06ca6bc 100644
670 --- a/fs/notify/group.c
671 +++ b/fs/notify/group.c
672 @@ -22,6 +22,7 @@
673  #include <linux/srcu.h>
674  #include <linux/rculist.h>
675  #include <linux/wait.h>
676 +#include <linux/module.h>
677  
678  #include <linux/fsnotify_backend.h>
679  #include "fsnotify.h"
680 @@ -72,6 +73,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
681  {
682         atomic_inc(&group->refcnt);
683  }
684 +EXPORT_SYMBOL(fsnotify_get_group);
685  
686  /*
687   * Drop a reference to a group.  Free it if it's through.
688 @@ -81,6 +83,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
689         if (atomic_dec_and_test(&group->refcnt))
690                 fsnotify_final_destroy_group(group);
691  }
692 +EXPORT_SYMBOL(fsnotify_put_group);
693  
694  /*
695   * Create a new fsnotify_group and hold a reference for the group returned.
696 @@ -109,6 +112,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
697  
698         return group;
699  }
700 +EXPORT_SYMBOL(fsnotify_alloc_group);
701  
702  int fsnotify_fasync(int fd, struct file *file, int on)
703  {
704 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
705 index 92e48c7..d2c4b68 100644
706 --- a/fs/notify/mark.c
707 +++ b/fs/notify/mark.c
708 @@ -109,6 +109,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
709                 mark->free_mark(mark);
710         }
711  }
712 +EXPORT_SYMBOL(fsnotify_put_mark);
713  
714  /* Calculate mask of events for a list of marks */
715  u32 fsnotify_recalc_mask(struct hlist_head *head)
716 @@ -202,6 +203,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
717         fsnotify_destroy_mark_locked(mark, group);
718         mutex_unlock(&group->mark_mutex);
719  }
720 +EXPORT_SYMBOL(fsnotify_destroy_mark);
721  
722  /*
723   * Destroy all marks in the given list. The marks must be already detached from
724 @@ -376,6 +378,7 @@ err:
725  
726         return ret;
727  }
728 +EXPORT_SYMBOL(fsnotify_add_mark);
729  
730  int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
731                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
732 @@ -455,6 +458,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
733         atomic_set(&mark->refcnt, 1);
734         mark->free_mark = free_mark;
735  }
736 +EXPORT_SYMBOL(fsnotify_init_mark);
737  
738  static int fsnotify_mark_destroy(void *ignored)
739  {
740 diff --git a/fs/open.c b/fs/open.c
741 index 33f9cbf..fcc7eb4 100644
742 --- a/fs/open.c
743 +++ b/fs/open.c
744 @@ -62,6 +62,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
745         mutex_unlock(&dentry->d_inode->i_mutex);
746         return ret;
747  }
748 +EXPORT_SYMBOL(do_truncate);
749  
750  long vfs_truncate(struct path *path, loff_t length)
751  {
752 @@ -672,6 +673,7 @@ int open_check_o_direct(struct file *f)
753         }
754         return 0;
755  }
756 +EXPORT_SYMBOL(open_check_o_direct);
757  
758  static int do_dentry_open(struct file *f,
759                           int (*open)(struct inode *, struct file *),
760 diff --git a/fs/splice.c b/fs/splice.c
761 index beb9993..813275a 100644
762 --- a/fs/splice.c
763 +++ b/fs/splice.c
764 @@ -1112,6 +1112,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
765  
766         return splice_write(pipe, out, ppos, len, flags);
767  }
768 +EXPORT_SYMBOL(do_splice_from);
769  
770  /*
771   * Attempt to initiate a splice from a file to a pipe.
772 @@ -1138,6 +1139,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
773  
774         return splice_read(in, ppos, pipe, len, flags);
775  }
776 +EXPORT_SYMBOL(do_splice_to);
777  
778  /**
779   * splice_direct_to_actor - splices data directly between two non-pipes
780 diff --git a/fs/xattr.c b/fs/xattr.c
781 index 4ef6985..6bb6303 100644
782 --- a/fs/xattr.c
783 +++ b/fs/xattr.c
784 @@ -207,6 +207,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
785         *xattr_value = value;
786         return error;
787  }
788 +EXPORT_SYMBOL(vfs_getxattr_alloc);
789  
790  /* Compare an extended attribute value with the given value */
791  int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
792 diff --git a/security/commoncap.c b/security/commoncap.c
793 index f66713b..fd4c4b8 100644
794 --- a/security/commoncap.c
795 +++ b/security/commoncap.c
796 @@ -975,9 +975,11 @@ int cap_mmap_addr(unsigned long addr)
797         }
798         return ret;
799  }
800 +EXPORT_SYMBOL(cap_mmap_addr);
801  
802  int cap_mmap_file(struct file *file, unsigned long reqprot,
803                   unsigned long prot, unsigned long flags)
804  {
805         return 0;
806  }
807 +EXPORT_SYMBOL(cap_mmap_file);
808 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
809 index 188c1d2..426d9af 100644
810 --- a/security/device_cgroup.c
811 +++ b/security/device_cgroup.c
812 @@ -7,6 +7,7 @@
813  #include <linux/device_cgroup.h>
814  #include <linux/cgroup.h>
815  #include <linux/ctype.h>
816 +#include <linux/export.h>
817  #include <linux/list.h>
818  #include <linux/uaccess.h>
819  #include <linux/seq_file.h>
820 @@ -849,6 +850,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
821         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
822                         access);
823  }
824 +EXPORT_SYMBOL(__devcgroup_inode_permission);
825  
826  int devcgroup_inode_mknod(int mode, dev_t dev)
827  {
828 diff --git a/security/security.c b/security/security.c
829 index e81d5bb..0b7b840 100644
830 --- a/security/security.c
831 +++ b/security/security.c
832 @@ -430,6 +430,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
833                 return 0;
834         return security_ops->path_rmdir(dir, dentry);
835  }
836 +EXPORT_SYMBOL(security_path_rmdir);
837  
838  int security_path_unlink(struct path *dir, struct dentry *dentry)
839  {
840 @@ -446,6 +447,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
841                 return 0;
842         return security_ops->path_symlink(dir, dentry, old_name);
843  }
844 +EXPORT_SYMBOL(security_path_symlink);
845  
846  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
847                        struct dentry *new_dentry)
848 @@ -454,6 +456,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
849                 return 0;
850         return security_ops->path_link(old_dentry, new_dir, new_dentry);
851  }
852 +EXPORT_SYMBOL(security_path_link);
853  
854  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
855                          struct path *new_dir, struct dentry *new_dentry,
856 @@ -481,6 +484,7 @@ int security_path_truncate(struct path *path)
857                 return 0;
858         return security_ops->path_truncate(path);
859  }
860 +EXPORT_SYMBOL(security_path_truncate);
861  
862  int security_path_chmod(struct path *path, umode_t mode)
863  {
864 @@ -488,6 +492,7 @@ int security_path_chmod(struct path *path, umode_t mode)
865                 return 0;
866         return security_ops->path_chmod(path, mode);
867  }
868 +EXPORT_SYMBOL(security_path_chmod);
869  
870  int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
871  {
872 @@ -495,6 +500,7 @@ int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
873                 return 0;
874         return security_ops->path_chown(path, uid, gid);
875  }
876 +EXPORT_SYMBOL(security_path_chown);
877  
878  int security_path_chroot(struct path *path)
879  {
880 @@ -580,6 +586,7 @@ int security_inode_readlink(struct dentry *dentry)
881                 return 0;
882         return security_ops->inode_readlink(dentry);
883  }
884 +EXPORT_SYMBOL(security_inode_readlink);
885  
886  int security_inode_follow_link(struct dentry *dentry, struct nameidata *nd)
887  {
888 @@ -594,6 +601,7 @@ int security_inode_permission(struct inode *inode, int mask)
889                 return 0;
890         return security_ops->inode_permission(inode, mask);
891  }
892 +EXPORT_SYMBOL(security_inode_permission);
893  
894  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
895  {
896 @@ -716,6 +724,7 @@ int security_file_permission(struct file *file, int mask)
897  
898         return fsnotify_perm(file, mask);
899  }
900 +EXPORT_SYMBOL(security_file_permission);
901  
902  int security_file_alloc(struct file *file)
903  {
904 @@ -775,6 +784,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
905                 return ret;
906         return ima_file_mmap(file, prot);
907  }
908 +EXPORT_SYMBOL(security_mmap_file);
909  
910  int security_mmap_addr(unsigned long addr)
911  {
912 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
913 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
914 +++ linux/Documentation/ABI/testing/debugfs-aufs        2014-01-30 21:10:02.794146538 +0100
915 @@ -0,0 +1,50 @@
916 +What:          /debug/aufs/si_<id>/
917 +Date:          March 2009
918 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
919 +Description:
920 +               Under /debug/aufs, a directory named si_<id> is created
921 +               per aufs mount, where <id> is a unique id generated
922 +               internally.
923 +
924 +What:          /debug/aufs/si_<id>/plink
925 +Date:          Apr 2013
926 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
927 +Description:
928 +               It has three lines and shows the information about the
929 +               pseudo-link. The first line is a single number
930 +               representing a number of buckets. The second line is a
931 +               number of pseudo-links per buckets (separated by a
932 +               blank). The last line is a single number representing a
933 +               total number of psedo-links.
934 +               When the aufs mount option 'noplink' is specified, it
935 +               will show "1\n0\n0\n".
936 +
937 +What:          /debug/aufs/si_<id>/xib
938 +Date:          March 2009
939 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
940 +Description:
941 +               It shows the consumed blocks by xib (External Inode Number
942 +               Bitmap), its block size and file size.
943 +               When the aufs mount option 'noxino' is specified, it
944 +               will be empty. About XINO files, see the aufs manual.
945 +
946 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
947 +Date:          March 2009
948 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
949 +Description:
950 +               It shows the consumed blocks by xino (External Inode Number
951 +               Translation Table), its link count, block size and file
952 +               size.
953 +               When the aufs mount option 'noxino' is specified, it
954 +               will be empty. About XINO files, see the aufs manual.
955 +
956 +What:          /debug/aufs/si_<id>/xigen
957 +Date:          March 2009
958 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
959 +Description:
960 +               It shows the consumed blocks by xigen (External Inode
961 +               Generation Table), its block size and file size.
962 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
963 +               be created.
964 +               When the aufs mount option 'noxino' is specified, it
965 +               will be empty. About XINO files, see the aufs manual.
966 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
967 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
968 +++ linux/Documentation/ABI/testing/sysfs-aufs  2014-01-30 21:10:02.794146538 +0100
969 @@ -0,0 +1,31 @@
970 +What:          /sys/fs/aufs/si_<id>/
971 +Date:          March 2009
972 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
973 +Description:
974 +               Under /sys/fs/aufs, a directory named si_<id> is created
975 +               per aufs mount, where <id> is a unique id generated
976 +               internally.
977 +
978 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
979 +Date:          March 2009
980 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
981 +Description:
982 +               It shows the abolute path of a member directory (which
983 +               is called branch) in aufs, and its permission.
984 +
985 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
986 +Date:          July 2013
987 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
988 +Description:
989 +               It shows the id of a member directory (which is called
990 +               branch) in aufs.
991 +
992 +What:          /sys/fs/aufs/si_<id>/xi_path
993 +Date:          March 2009
994 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
995 +Description:
996 +               It shows the abolute path of XINO (External Inode Number
997 +               Bitmap, Translation Table and Generation Table) file
998 +               even if it is the default path.
999 +               When the aufs mount option 'noxino' is specified, it
1000 +               will be empty. About XINO files, see the aufs manual.
1001 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1002 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1003 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2015-06-22 08:27:37.867501461 +0200
1004 @@ -0,0 +1,170 @@
1005 +
1006 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1007 +# 
1008 +# This program is free software; you can redistribute it and/or modify
1009 +# it under the terms of the GNU General Public License as published by
1010 +# the Free Software Foundation; either version 2 of the License, or
1011 +# (at your option) any later version.
1012 +# 
1013 +# This program is distributed in the hope that it will be useful,
1014 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1015 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1016 +# GNU General Public License for more details.
1017 +# 
1018 +# You should have received a copy of the GNU General Public License
1019 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1020 +
1021 +Introduction
1022 +----------------------------------------
1023 +
1024 +aufs [ei ju: ef es] | [a u f s]
1025 +1. abbrev. for "advanced multi-layered unification filesystem".
1026 +2. abbrev. for "another unionfs".
1027 +3. abbrev. for "auf das" in German which means "on the" in English.
1028 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1029 +   But "Filesystem aufs Filesystem" is hard to understand.
1030 +
1031 +AUFS is a filesystem with features:
1032 +- multi layered stackable unification filesystem, the member directory
1033 +  is called as a branch.
1034 +- branch permission and attribute, 'readonly', 'real-readonly',
1035 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1036 +  combination.
1037 +- internal "file copy-on-write".
1038 +- logical deletion, whiteout.
1039 +- dynamic branch manipulation, adding, deleting and changing permission.
1040 +- allow bypassing aufs, user's direct branch access.
1041 +- external inode number translation table and bitmap which maintains the
1042 +  persistent aufs inode number.
1043 +- seekable directory, including NFS readdir.
1044 +- file mapping, mmap and sharing pages.
1045 +- pseudo-link, hardlink over branches.
1046 +- loopback mounted filesystem as a branch.
1047 +- several policies to select one among multiple writable branches.
1048 +- revert a single systemcall when an error occurs in aufs.
1049 +- and more...
1050 +
1051 +
1052 +Multi Layered Stackable Unification Filesystem
1053 +----------------------------------------------------------------------
1054 +Most people already knows what it is.
1055 +It is a filesystem which unifies several directories and provides a
1056 +merged single directory. When users access a file, the access will be
1057 +passed/re-directed/converted (sorry, I am not sure which English word is
1058 +correct) to the real file on the member filesystem. The member
1059 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1060 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1061 +readonly branch is handled by creating 'whiteout' on the upper writable
1062 +branch.
1063 +
1064 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1065 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1066 +different approaches to implement the merged-view.
1067 +The former tries putting it into VFS, and the latter implements as a
1068 +separate filesystem.
1069 +(If I misunderstand about these implementations, please let me know and
1070 +I shall correct it. Because it is a long time ago when I read their
1071 +source files last time).
1072 +
1073 +UnionMount's approach will be able to small, but may be hard to share
1074 +branches between several UnionMount since the whiteout in it is
1075 +implemented in the inode on branch filesystem and always
1076 +shared. According to Bharata's post, readdir does not seems to be
1077 +finished yet.
1078 +There are several missing features known in this implementations such as
1079 +- for users, the inode number may change silently. eg. copy-up.
1080 +- link(2) may break by copy-up.
1081 +- read(2) may get an obsoleted filedata (fstat(2) too).
1082 +- fcntl(F_SETLK) may be broken by copy-up.
1083 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1084 +  open(O_RDWR).
1085 +
1086 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1087 +merged into mainline. This is another implementation of UnionMount as a
1088 +separated filesystem. All the limitations and known problems which
1089 +UnionMount are equally inherited to "overlay" filesystem.
1090 +
1091 +Unionfs has a longer history. When I started implementing a stackable
1092 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1093 +inode, dentry and file objects and they have an array pointing lower
1094 +same kind objects. After contributing many patches for Unionfs, I
1095 +re-started my project AUFS (Jun 2006).
1096 +
1097 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1098 +implemented my own ideas, approaches and enhancements and it became
1099 +totally different one.
1100 +
1101 +Comparing DM snapshot and fs based implementation
1102 +- the number of bytes to be copied between devices is much smaller.
1103 +- the type of filesystem must be one and only.
1104 +- the fs must be writable, no readonly fs, even for the lower original
1105 +  device. so the compression fs will not be usable. but if we use
1106 +  loopback mount, we may address this issue.
1107 +  for instance,
1108 +       mount /cdrom/squashfs.img /sq
1109 +       losetup /sq/ext2.img
1110 +       losetup /somewhere/cow
1111 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1112 +- it will be difficult (or needs more operations) to extract the
1113 +  difference between the original device and COW.
1114 +- DM snapshot-merge may help a lot when users try merging. in the
1115 +  fs-layer union, users will use rsync(1).
1116 +
1117 +You may want to read my old paper "Filesystems in LiveCD"
1118 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1119 +
1120 +
1121 +Several characters/aspects/persona of aufs
1122 +----------------------------------------------------------------------
1123 +
1124 +Aufs has several characters, aspects or persona.
1125 +1. a filesystem, callee of VFS helper
1126 +2. sub-VFS, caller of VFS helper for branches
1127 +3. a virtual filesystem which maintains persistent inode number
1128 +4. reader/writer of files on branches such like an application
1129 +
1130 +1. Callee of VFS Helper
1131 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1132 +unlink(2) from an application reaches sys_unlink() kernel function and
1133 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1134 +calls filesystem specific unlink operation. Actually aufs implements the
1135 +unlink operation but it behaves like a redirector.
1136 +
1137 +2. Caller of VFS Helper for Branches
1138 +aufs_unlink() passes the unlink request to the branch filesystem as if
1139 +it were called from VFS. So the called unlink operation of the branch
1140 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1141 +every necessary pre/post operation for the branch filesystem.
1142 +- acquire the lock for the parent dir on a branch
1143 +- lookup in a branch
1144 +- revalidate dentry on a branch
1145 +- mnt_want_write() for a branch
1146 +- vfs_unlink() for a branch
1147 +- mnt_drop_write() for a branch
1148 +- release the lock on a branch
1149 +
1150 +3. Persistent Inode Number
1151 +One of the most important issue for a filesystem is to maintain inode
1152 +numbers. This is particularly important to support exporting a
1153 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1154 +backend block device for its own. But some storage is necessary to
1155 +keep and maintain the inode numbers. It may be a large space and may not
1156 +suit to keep in memory. Aufs rents some space from its first writable
1157 +branch filesystem (by default) and creates file(s) on it. These files
1158 +are created by aufs internally and removed soon (currently) keeping
1159 +opened.
1160 +Note: Because these files are removed, they are totally gone after
1161 +      unmounting aufs. It means the inode numbers are not persistent
1162 +      across unmount or reboot. I have a plan to make them really
1163 +      persistent which will be important for aufs on NFS server.
1164 +
1165 +4. Read/Write Files Internally (copy-on-write)
1166 +Because a branch can be readonly, when you write a file on it, aufs will
1167 +"copy-up" it to the upper writable branch internally. And then write the
1168 +originally requested thing to the file. Generally kernel doesn't
1169 +open/read/write file actively. In aufs, even a single write may cause a
1170 +internal "file copy". This behaviour is very similar to cp(1) command.
1171 +
1172 +Some people may think it is better to pass such work to user space
1173 +helper, instead of doing in kernel space. Actually I am still thinking
1174 +about it. But currently I have implemented it in kernel space.
1175 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1176 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1177 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2015-06-22 08:27:37.867501461 +0200
1178 @@ -0,0 +1,258 @@
1179 +
1180 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1181 +# 
1182 +# This program is free software; you can redistribute it and/or modify
1183 +# it under the terms of the GNU General Public License as published by
1184 +# the Free Software Foundation; either version 2 of the License, or
1185 +# (at your option) any later version.
1186 +# 
1187 +# This program is distributed in the hope that it will be useful,
1188 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1189 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1190 +# GNU General Public License for more details.
1191 +# 
1192 +# You should have received a copy of the GNU General Public License
1193 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1194 +
1195 +Basic Aufs Internal Structure
1196 +
1197 +Superblock/Inode/Dentry/File Objects
1198 +----------------------------------------------------------------------
1199 +As like an ordinary filesystem, aufs has its own
1200 +superblock/inode/dentry/file objects. All these objects have a
1201 +dynamically allocated array and store the same kind of pointers to the
1202 +lower filesystem, branch.
1203 +For example, when you build a union with one readwrite branch and one
1204 +readonly, mounted /au, /rw and /ro respectively.
1205 +- /au = /rw + /ro
1206 +- /ro/fileA exists but /rw/fileA
1207 +
1208 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1209 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1210 +- [0] = NULL (because /rw/fileA doesn't exist)
1211 +- [1] = /ro/fileA
1212 +
1213 +This style of an array is essentially same to the aufs
1214 +superblock/inode/dentry/file objects.
1215 +
1216 +Because aufs supports manipulating branches, ie. add/delete/change
1217 +branches dynamically, these objects has its own generation. When
1218 +branches are changed, the generation in aufs superblock is
1219 +incremented. And a generation in other object are compared when it is
1220 +accessed. When a generation in other objects are obsoleted, aufs
1221 +refreshes the internal array.
1222 +
1223 +
1224 +Superblock
1225 +----------------------------------------------------------------------
1226 +Additionally aufs superblock has some data for policies to select one
1227 +among multiple writable branches, XIB files, pseudo-links and kobject.
1228 +See below in detail.
1229 +About the policies which supports copy-down a directory, see
1230 +wbr_policy.txt too.
1231 +
1232 +
1233 +Branch and XINO(External Inode Number Translation Table)
1234 +----------------------------------------------------------------------
1235 +Every branch has its own xino (external inode number translation table)
1236 +file. The xino file is created and unlinked by aufs internally. When two
1237 +members of a union exist on the same filesystem, they share the single
1238 +xino file.
1239 +The struct of a xino file is simple, just a sequence of aufs inode
1240 +numbers which is indexed by the lower inode number.
1241 +In the above sample, assume the inode number of /ro/fileA is i111 and
1242 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1243 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1244 +
1245 +When the inode numbers are not contiguous, the xino file will be sparse
1246 +which has a hole in it and doesn't consume as much disk space as it
1247 +might appear. If your branch filesystem consumes disk space for such
1248 +holes, then you should specify 'xino=' option at mounting aufs.
1249 +
1250 +Aufs has a mount option to free the disk blocks for such holes in XINO
1251 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1252 +meet a problem of disk shortage due to XINO files, then you should try
1253 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1254 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1255 +the holes in XINO files.
1256 +
1257 +Also a writable branch has three kinds of "whiteout bases". All these
1258 +are existed when the branch is joined to aufs, and their names are
1259 +whiteout-ed doubly, so that users will never see their names in aufs
1260 +hierarchy.
1261 +1. a regular file which will be hardlinked to all whiteouts.
1262 +2. a directory to store a pseudo-link.
1263 +3. a directory to store an "orphan"-ed file temporary.
1264 +
1265 +1. Whiteout Base
1266 +   When you remove a file on a readonly branch, aufs handles it as a
1267 +   logical deletion and creates a whiteout on the upper writable branch
1268 +   as a hardlink of this file in order not to consume inode on the
1269 +   writable branch.
1270 +2. Pseudo-link Dir
1271 +   See below, Pseudo-link.
1272 +3. Step-Parent Dir
1273 +   When "fileC" exists on the lower readonly branch only and it is
1274 +   opened and removed with its parent dir, and then user writes
1275 +   something into it, then aufs copies-up fileC to this
1276 +   directory. Because there is no other dir to store fileC. After
1277 +   creating a file under this dir, the file is unlinked.
1278 +
1279 +Because aufs supports manipulating branches, ie. add/delete/change
1280 +dynamically, a branch has its own id. When the branch order changes,
1281 +aufs finds the new index by searching the branch id.
1282 +
1283 +
1284 +Pseudo-link
1285 +----------------------------------------------------------------------
1286 +Assume "fileA" exists on the lower readonly branch only and it is
1287 +hardlinked to "fileB" on the branch. When you write something to fileA,
1288 +aufs copies-up it to the upper writable branch. Additionally aufs
1289 +creates a hardlink under the Pseudo-link Directory of the writable
1290 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1291 +simple list. If fileB is read after unlinking fileA, aufs returns
1292 +filedata from the pseudo-link instead of the lower readonly
1293 +branch. Because the pseudo-link is based upon the inode, to keep the
1294 +inode number by xino (see above) is essentially necessary.
1295 +
1296 +All the hardlinks under the Pseudo-link Directory of the writable branch
1297 +should be restored in a proper location later. Aufs provides a utility
1298 +to do this. The userspace helpers executed at remounting and unmounting
1299 +aufs by default.
1300 +During this utility is running, it puts aufs into the pseudo-link
1301 +maintenance mode. In this mode, only the process which began the
1302 +maintenance mode (and its child processes) is allowed to operate in
1303 +aufs. Some other processes which are not related to the pseudo-link will
1304 +be allowed to run too, but the rest have to return an error or wait
1305 +until the maintenance mode ends. If a process already acquires an inode
1306 +mutex (in VFS), it has to return an error.
1307 +
1308 +
1309 +XIB(external inode number bitmap)
1310 +----------------------------------------------------------------------
1311 +Addition to the xino file per a branch, aufs has an external inode number
1312 +bitmap in a superblock object. It is also an internal file such like a
1313 +xino file.
1314 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1315 +not.
1316 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1317 +
1318 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1319 +reduce the number of consumed disk blocks for these files.
1320 +
1321 +
1322 +Virtual or Vertical Dir, and Readdir in Userspace
1323 +----------------------------------------------------------------------
1324 +In order to support multiple layers (branches), aufs readdir operation
1325 +constructs a virtual dir block on memory. For readdir, aufs calls
1326 +vfs_readdir() internally for each dir on branches, merges their entries
1327 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1328 +object. So the file object has its entry list until it is closed. The
1329 +entry list will be updated when the file position is zero and becomes
1330 +obsoleted. This decision is made in aufs automatically.
1331 +
1332 +The dynamically allocated memory block for the name of entries has a
1333 +unit of 512 bytes (by default) and stores the names contiguously (no
1334 +padding). Another block for each entry is handled by kmem_cache too.
1335 +During building dir blocks, aufs creates hash list and judging whether
1336 +the entry is whiteouted by its upper branch or already listed.
1337 +The merged result is cached in the corresponding inode object and
1338 +maintained by a customizable life-time option.
1339 +
1340 +Some people may call it can be a security hole or invite DoS attack
1341 +since the opened and once readdir-ed dir (file object) holds its entry
1342 +list and becomes a pressure for system memory. But I'd say it is similar
1343 +to files under /proc or /sys. The virtual files in them also holds a
1344 +memory page (generally) while they are opened. When an idea to reduce
1345 +memory for them is introduced, it will be applied to aufs too.
1346 +For those who really hate this situation, I've developed readdir(3)
1347 +library which operates this merging in userspace. You just need to set
1348 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1349 +kernel space for readdir(3).
1350 +
1351 +
1352 +Workqueue
1353 +----------------------------------------------------------------------
1354 +Aufs sometimes requires privilege access to a branch. For instance,
1355 +in copy-up/down operation. When a user process is going to make changes
1356 +to a file which exists in the lower readonly branch only, and the mode
1357 +of one of ancestor directories may not be writable by a user
1358 +process. Here aufs copy-up the file with its ancestors and they may
1359 +require privilege to set its owner/group/mode/etc.
1360 +This is a typical case of a application character of aufs (see
1361 +Introduction).
1362 +
1363 +Aufs uses workqueue synchronously for this case. It creates its own
1364 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1365 +passes the request to call mkdir or write (for example), and wait for
1366 +its completion. This approach solves a problem of a signal handler
1367 +simply.
1368 +If aufs didn't adopt the workqueue and changed the privilege of the
1369 +process, then the process may receive the unexpected SIGXFSZ or other
1370 +signals.
1371 +
1372 +Also aufs uses the system global workqueue ("events" kernel thread) too
1373 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1374 +whiteout base and etc. This is unrelated to a privilege.
1375 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1376 +superblock at the beginning, at the same time waits for the completion
1377 +of all queued asynchronous tasks.
1378 +
1379 +
1380 +Whiteout
1381 +----------------------------------------------------------------------
1382 +The whiteout in aufs is very similar to Unionfs's. That is represented
1383 +by its filename. UnionMount takes an approach of a file mode, but I am
1384 +afraid several utilities (find(1) or something) will have to support it.
1385 +
1386 +Basically the whiteout represents "logical deletion" which stops aufs to
1387 +lookup further, but also it represents "dir is opaque" which also stop
1388 +further lookup.
1389 +
1390 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1391 +In order to make several functions in a single systemcall to be
1392 +revertible, aufs adopts an approach to rename a directory to a temporary
1393 +unique whiteouted name.
1394 +For example, in rename(2) dir where the target dir already existed, aufs
1395 +renames the target dir to a temporary unique whiteouted name before the
1396 +actual rename on a branch, and then handles other actions (make it opaque,
1397 +update the attributes, etc). If an error happens in these actions, aufs
1398 +simply renames the whiteouted name back and returns an error. If all are
1399 +succeeded, aufs registers a function to remove the whiteouted unique
1400 +temporary name completely and asynchronously to the system global
1401 +workqueue.
1402 +
1403 +
1404 +Copy-up
1405 +----------------------------------------------------------------------
1406 +It is a well-known feature or concept.
1407 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1408 +internally and makes change to the new file on the upper writable branch.
1409 +When the trigger systemcall does not update the timestamps of the parent
1410 +dir, aufs reverts it after copy-up.
1411 +
1412 +
1413 +Move-down (aufs3.9 and later)
1414 +----------------------------------------------------------------------
1415 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1416 +the lower readonly branch to the upper writable branch when a user
1417 +changes something about the file.
1418 +"Move-down" is an opposite action of copy-up. Basically this action is
1419 +ran manually instead of automatically and internally.
1420 +For desgin and implementation, aufs has to consider these issues.
1421 +- whiteout for the file may exist on the lower branch.
1422 +- ancestor directories may not exist on the lower branch.
1423 +- diropq for the ancestor directories may exist on the upper branch.
1424 +- free space on the lower branch will reduce.
1425 +- another access to the file may happen during moving-down, including
1426 +  UDBA (see "Revalidate Dentry and UDBA").
1427 +- the file should not be hard-linked nor pseudo-linked. they should be
1428 +  handled by auplink utility later.
1429 +
1430 +Sometimes users want to move-down a file from the upper writable branch
1431 +to the lower readonly or writable branch. For instance,
1432 +- the free space of the upper writable branch is going to run out.
1433 +- create a new intermediate branch between the upper and lower branch.
1434 +- etc.
1435 +
1436 +For this purpose, use "aumvdown" command in aufs-util.git.
1437 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1438 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1439 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2015-06-22 08:27:37.870834875 +0200
1440 @@ -0,0 +1,85 @@
1441 +
1442 +# Copyright (C) 2015 Junjiro R. Okajima
1443 +# 
1444 +# This program is free software; you can redistribute it and/or modify
1445 +# it under the terms of the GNU General Public License as published by
1446 +# the Free Software Foundation; either version 2 of the License, or
1447 +# (at your option) any later version.
1448 +# 
1449 +# This program is distributed in the hope that it will be useful,
1450 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1451 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1452 +# GNU General Public License for more details.
1453 +# 
1454 +# You should have received a copy of the GNU General Public License
1455 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1456 +
1457 +Support for a branch who has its ->atomic_open()
1458 +----------------------------------------------------------------------
1459 +The filesystems who implement its ->atomic_open() are not majority. For
1460 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1461 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1462 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1463 +sure whether all filesystems who have ->atomic_open() behave like this,
1464 +but NFSv4 surely returns the error.
1465 +
1466 +In order to support ->atomic_open() for aufs, there are a few
1467 +approaches.
1468 +
1469 +A. Introduce aufs_atomic_open()
1470 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1471 +     branch fs.
1472 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1473 +   an aufs user Pip Cet's approach
1474 +   - calls aufs_create(), VFS finish_open() and notify_change().
1475 +   - pass fake-mode to finish_open(), and then correct the mode by
1476 +     notify_change().
1477 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1478 +   - no aufs_atomic_open().
1479 +   - aufs_lookup() registers the TID to an aufs internal object.
1480 +   - aufs_create() does nothing when the matching TID is registered, but
1481 +     registers the mode.
1482 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1483 +     TID is registered.
1484 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1485 +   credential
1486 +   - no aufs_atomic_open().
1487 +   - aufs_create() registers the TID to an internal object. this info
1488 +     represents "this process created this file just now."
1489 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1490 +     registered TID and re-try open() with superuser's credential.
1491 +
1492 +Pros and cons for each approach.
1493 +
1494 +A.
1495 +   - straightforward but highly depends upon VFS internal.
1496 +   - the atomic behavaiour is kept.
1497 +   - some of parameters such as nameidata are hard to reproduce for
1498 +     branch fs.
1499 +   - large overhead.
1500 +B.
1501 +   - easy to implement.
1502 +   - the atomic behavaiour is lost.
1503 +C.
1504 +   - the atomic behavaiour is kept.
1505 +   - dirty and tricky.
1506 +   - VFS checks whether the file is created correctly after calling
1507 +     ->create(), which means this approach doesn't work.
1508 +D.
1509 +   - easy to implement.
1510 +   - the atomic behavaiour is lost.
1511 +   - to open a file with superuser's credential and give it to a user
1512 +     process is a bad idea, since the file object keeps the credential
1513 +     in it. It may affect LSM or something. This approach doesn't work
1514 +     either.
1515 +
1516 +The approach A is ideal, but it hard to implement. So here is a
1517 +variation of A, which is to be implemented.
1518 +
1519 +A-1. Introduce aufs_atomic_open()
1520 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1521 +       vfs_create() and finish_open().
1522 +     - the demerit is that the several checks after branch fs
1523 +       ->atomic_open() are lost. in the ordinary case, the checks are
1524 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1525 +       be implemented in aufs, but not all I am afraid.
1526 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1527 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1528 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2015-06-22 08:27:37.870834875 +0200
1529 @@ -0,0 +1,113 @@
1530 +
1531 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1532 +# 
1533 +# This program is free software; you can redistribute it and/or modify
1534 +# it under the terms of the GNU General Public License as published by
1535 +# the Free Software Foundation; either version 2 of the License, or
1536 +# (at your option) any later version.
1537 +# 
1538 +# This program is distributed in the hope that it will be useful,
1539 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1540 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1541 +# GNU General Public License for more details.
1542 +# 
1543 +# You should have received a copy of the GNU General Public License
1544 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1545 +
1546 +Lookup in a Branch
1547 +----------------------------------------------------------------------
1548 +Since aufs has a character of sub-VFS (see Introduction), it operates
1549 +lookup for branches as VFS does. It may be a heavy work. But almost all
1550 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1551 +directly connected to its parent. Digging down the directory hierarchy
1552 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1553 +aufs calls it.
1554 +
1555 +When a branch is a remote filesystem, aufs basically relies upon its
1556 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1557 +them.
1558 +For d_revalidate, aufs implements three levels of revalidate tests. See
1559 +"Revalidate Dentry and UDBA" in detail.
1560 +
1561 +
1562 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1563 +----------------------------------------------------------------------
1564 +Let's try case study.
1565 +- aufs has two branches, upper readwrite and lower readonly.
1566 +  /au = /rw + /ro
1567 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1568 +- user invoked "chmod a+rx /au/dirA"
1569 +- the internal copy-up is activated and "/rw/dirA" is created and its
1570 +  permission bits are set to world readable.
1571 +- then "/au/dirA" becomes world readable?
1572 +
1573 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1574 +or it may be a natively readonly filesystem. If aufs respects the lower
1575 +branch, it should not respond readdir request from other users. But user
1576 +allowed it by chmod. Should really aufs rejects showing the entries
1577 +under /ro/dirA?
1578 +
1579 +To be honest, I don't have a good solution for this case. So aufs
1580 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1581 +users.
1582 +When dirperm1 is specified, aufs checks only the highest one for the
1583 +directory permission, and shows the entries. Otherwise, as usual, checks
1584 +every dir existing on all branches and rejects the request.
1585 +
1586 +As a side effect, dirperm1 option improves the performance of aufs
1587 +because the number of permission check is reduced when the number of
1588 +branch is many.
1589 +
1590 +
1591 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1592 +----------------------------------------------------------------------
1593 +Generally VFS helpers re-validate a dentry as a part of lookup.
1594 +0. digging down the directory hierarchy.
1595 +1. lock the parent dir by its i_mutex.
1596 +2. lookup the final (child) entry.
1597 +3. revalidate it.
1598 +4. call the actual operation (create, unlink, etc.)
1599 +5. unlock the parent dir
1600 +
1601 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1602 +called. Actually aufs implements it and checks the dentry on a branch is
1603 +still valid.
1604 +But it is not enough. Because aufs has to release the lock for the
1605 +parent dir on a branch at the end of ->lookup() (step 2) and
1606 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1607 +held by VFS.
1608 +If the file on a branch is changed directly, eg. bypassing aufs, after
1609 +aufs released the lock, then the subsequent operation may cause
1610 +something unpleasant result.
1611 +
1612 +This situation is a result of VFS architecture, ->lookup() and
1613 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1614 +design from VFS's point of view. It is just not suitable for sub-VFS
1615 +character in aufs.
1616 +
1617 +Aufs supports such case by three level of revalidation which is
1618 +selectable by user.
1619 +1. Simple Revalidate
1620 +   Addition to the native flow in VFS's, confirm the child-parent
1621 +   relationship on the branch just after locking the parent dir on the
1622 +   branch in the "actual operation" (step 4). When this validation
1623 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1624 +   checks the validation of the dentry on branches.
1625 +2. Monitor Changes Internally by Inotify/Fsnotify
1626 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1627 +   the dentry on the branch, and returns EBUSY if it finds different
1628 +   dentry.
1629 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1630 +   during it is in cache. When the event is notified, aufs registers a
1631 +   function to kernel 'events' thread by schedule_work(). And the
1632 +   function sets some special status to the cached aufs dentry and inode
1633 +   private data. If they are not cached, then aufs has nothing to
1634 +   do. When the same file is accessed through aufs (step 0-3) later,
1635 +   aufs will detect the status and refresh all necessary data.
1636 +   In this mode, aufs has to ignore the event which is fired by aufs
1637 +   itself.
1638 +3. No Extra Validation
1639 +   This is the simplest test and doesn't add any additional revalidation
1640 +   test, and skip the revalidation in step 4. It is useful and improves
1641 +   aufs performance when system surely hide the aufs branches from user,
1642 +   by over-mounting something (or another method).
1643 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1644 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1645 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2015-06-22 08:27:37.870834875 +0200
1646 @@ -0,0 +1,74 @@
1647 +
1648 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1649 +# 
1650 +# This program is free software; you can redistribute it and/or modify
1651 +# it under the terms of the GNU General Public License as published by
1652 +# the Free Software Foundation; either version 2 of the License, or
1653 +# (at your option) any later version.
1654 +# 
1655 +# This program is distributed in the hope that it will be useful,
1656 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1657 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1658 +# GNU General Public License for more details.
1659 +# 
1660 +# You should have received a copy of the GNU General Public License
1661 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1662 +
1663 +Branch Manipulation
1664 +
1665 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1666 +and changing its permission/attribute, there are a lot of works to do.
1667 +
1668 +
1669 +Add a Branch
1670 +----------------------------------------------------------------------
1671 +o Confirm the adding dir exists outside of aufs, including loopback
1672 +  mount, and its various attributes.
1673 +o Initialize the xino file and whiteout bases if necessary.
1674 +  See struct.txt.
1675 +
1676 +o Check the owner/group/mode of the directory
1677 +  When the owner/group/mode of the adding directory differs from the
1678 +  existing branch, aufs issues a warning because it may impose a
1679 +  security risk.
1680 +  For example, when a upper writable branch has a world writable empty
1681 +  top directory, a malicious user can create any files on the writable
1682 +  branch directly, like copy-up and modify manually. If something like
1683 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1684 +  writable branch, and the writable branch is world-writable, then a
1685 +  malicious guy may create /etc/passwd on the writable branch directly
1686 +  and the infected file will be valid in aufs.
1687 +  I am afraid it can be a security issue, but aufs can do nothing except
1688 +  producing a warning.
1689 +
1690 +
1691 +Delete a Branch
1692 +----------------------------------------------------------------------
1693 +o Confirm the deleting branch is not busy
1694 +  To be general, there is one merit to adopt "remount" interface to
1695 +  manipulate branches. It is to discard caches. At deleting a branch,
1696 +  aufs checks the still cached (and connected) dentries and inodes. If
1697 +  there are any, then they are all in-use. An inode without its
1698 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1699 +
1700 +  For the cached one, aufs checks whether the same named entry exists on
1701 +  other branches.
1702 +  If the cached one is a directory, because aufs provides a merged view
1703 +  to users, as long as one dir is left on any branch aufs can show the
1704 +  dir to users. In this case, the branch can be removed from aufs.
1705 +  Otherwise aufs rejects deleting the branch.
1706 +
1707 +  If any file on the deleting branch is opened by aufs, then aufs
1708 +  rejects deleting.
1709 +
1710 +
1711 +Modify the Permission of a Branch
1712 +----------------------------------------------------------------------
1713 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1714 +  See struct.txt.
1715 +
1716 +o rw --> ro: Confirm the modifying branch is not busy
1717 +  Aufs rejects the request if any of these conditions are true.
1718 +  - a file on the branch is mmap-ed.
1719 +  - a regular file on the branch is opened for write and there is no
1720 +    same named entry on the upper branch.
1721 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1722 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1723 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2015-06-22 08:27:37.870834875 +0200
1724 @@ -0,0 +1,64 @@
1725 +
1726 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1727 +# 
1728 +# This program is free software; you can redistribute it and/or modify
1729 +# it under the terms of the GNU General Public License as published by
1730 +# the Free Software Foundation; either version 2 of the License, or
1731 +# (at your option) any later version.
1732 +# 
1733 +# This program is distributed in the hope that it will be useful,
1734 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1735 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1736 +# GNU General Public License for more details.
1737 +# 
1738 +# You should have received a copy of the GNU General Public License
1739 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1740 +
1741 +Policies to Select One among Multiple Writable Branches
1742 +----------------------------------------------------------------------
1743 +When the number of writable branch is more than one, aufs has to decide
1744 +the target branch for file creation or copy-up. By default, the highest
1745 +writable branch which has the parent (or ancestor) dir of the target
1746 +file is chosen (top-down-parent policy).
1747 +By user's request, aufs implements some other policies to select the
1748 +writable branch, for file creation several policies, round-robin,
1749 +most-free-space, and other policies. For copy-up, top-down-parent,
1750 +bottom-up-parent, bottom-up and others.
1751 +
1752 +As expected, the round-robin policy selects the branch in circular. When
1753 +you have two writable branches and creates 10 new files, 5 files will be
1754 +created for each branch. mkdir(2) systemcall is an exception. When you
1755 +create 10 new directories, all will be created on the same branch.
1756 +And the most-free-space policy selects the one which has most free
1757 +space among the writable branches. The amount of free space will be
1758 +checked by aufs internally, and users can specify its time interval.
1759 +
1760 +The policies for copy-up is more simple,
1761 +top-down-parent is equivalent to the same named on in create policy,
1762 +bottom-up-parent selects the writable branch where the parent dir
1763 +exists and the nearest upper one from the copyup-source,
1764 +bottom-up selects the nearest upper writable branch from the
1765 +copyup-source, regardless the existence of the parent dir.
1766 +
1767 +There are some rules or exceptions to apply these policies.
1768 +- If there is a readonly branch above the policy-selected branch and
1769 +  the parent dir is marked as opaque (a variation of whiteout), or the
1770 +  target (creating) file is whiteout-ed on the upper readonly branch,
1771 +  then the result of the policy is ignored and the target file will be
1772 +  created on the nearest upper writable branch than the readonly branch.
1773 +- If there is a writable branch above the policy-selected branch and
1774 +  the parent dir is marked as opaque or the target file is whiteouted
1775 +  on the branch, then the result of the policy is ignored and the target
1776 +  file will be created on the highest one among the upper writable
1777 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1778 +  it as usual.
1779 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1780 +  They try selecting the branch where the source exists as possible
1781 +  since copyup a large file will take long time. If it can't be,
1782 +  ie. the branch where the source exists is readonly, then they will
1783 +  follow the copyup policy.
1784 +- There is an exception for rename(2) when the target exists.
1785 +  If the rename target exists, aufs compares the index of the branches
1786 +  where the source and the target exists and selects the higher
1787 +  one. If the selected branch is readonly, then aufs follows the
1788 +  copyup policy.
1789 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1790 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1791 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2015-06-22 08:27:37.870834875 +0200
1792 @@ -0,0 +1,120 @@
1793 +
1794 +# Copyright (C) 2011-2015 Junjiro R. Okajima
1795 +# 
1796 +# This program is free software; you can redistribute it and/or modify
1797 +# it under the terms of the GNU General Public License as published by
1798 +# the Free Software Foundation; either version 2 of the License, or
1799 +# (at your option) any later version.
1800 +# 
1801 +# This program is distributed in the hope that it will be useful,
1802 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1803 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1804 +# GNU General Public License for more details.
1805 +# 
1806 +# You should have received a copy of the GNU General Public License
1807 +# along with this program; if not, write to the Free Software
1808 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1809 +
1810 +
1811 +File-based Hierarchical Storage Management (FHSM)
1812 +----------------------------------------------------------------------
1813 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1814 +storage world. Aufs provides this feature as file-based with multiple
1815 +writable branches, based upon the principle of "Colder, the Lower".
1816 +Here the word "colder" means that the less used files, and "lower" means
1817 +that the position in the order of the stacked branches vertically.
1818 +These multiple writable branches are prioritized, ie. the topmost one
1819 +should be the fastest drive and be used heavily.
1820 +
1821 +o Characters in aufs FHSM story
1822 +- aufs itself and a new branch attribute.
1823 +- a new ioctl interface to move-down and to establish a connection with
1824 +  the daemon ("move-down" is a converse of "copy-up").
1825 +- userspace tool and daemon.
1826 +
1827 +The userspace daemon establishes a connection with aufs and waits for
1828 +the notification. The notified information is very similar to struct
1829 +statfs containing the number of consumed blocks and inodes.
1830 +When the consumed blocks/inodes of a branch exceeds the user-specified
1831 +upper watermark, the daemon activates its move-down process until the
1832 +consumed blocks/inodes reaches the user-specified lower watermark.
1833 +
1834 +The actual move-down is done by aufs based upon the request from
1835 +user-space since we need to maintain the inode number and the internal
1836 +pointer arrays in aufs.
1837 +
1838 +Currently aufs FHSM handles the regular files only. Additionally they
1839 +must not be hard-linked nor pseudo-linked.
1840 +
1841 +
1842 +o Cowork of aufs and the user-space daemon
1843 +  During the userspace daemon established the connection, aufs sends a
1844 +  small notification to it whenever aufs writes something into the
1845 +  writable branch. But it may cost high since aufs issues statfs(2)
1846 +  internally. So user can specify a new option to cache the
1847 +  info. Actually the notification is controlled by these factors.
1848 +  + the specified cache time.
1849 +  + classified as "force" by aufs internally.
1850 +  Until the specified time expires, aufs doesn't send the info
1851 +  except the forced cases. When aufs decide forcing, the info is always
1852 +  notified to userspace.
1853 +  For example, the number of free inodes is generally large enough and
1854 +  the shortage of it happens rarely. So aufs doesn't force the
1855 +  notification when creating a new file, directory and others. This is
1856 +  the typical case which aufs doesn't force.
1857 +  When aufs writes the actual filedata and the files consumes any of new
1858 +  blocks, the aufs forces notifying.
1859 +
1860 +
1861 +o Interfaces in aufs
1862 +- New branch attribute.
1863 +  + fhsm
1864 +    Specifies that the branch is managed by FHSM feature. In other word,
1865 +    participant in the FHSM.
1866 +    When nofhsm is set to the branch, it will not be the source/target
1867 +    branch of the move-down operation. This attribute is set
1868 +    independently from coo and moo attributes, and if you want full
1869 +    FHSM, you should specify them as well.
1870 +- New mount option.
1871 +  + fhsm_sec
1872 +    Specifies a second to suppress many less important info to be
1873 +    notified.
1874 +- New ioctl.
1875 +  + AUFS_CTL_FHSM_FD
1876 +    create a new file descriptor which userspace can read the notification
1877 +    (a subset of struct statfs) from aufs.
1878 +- Module parameter 'brs'
1879 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
1880 +  be set.
1881 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
1882 +  When there are two or more branches with fhsm attributes,
1883 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
1884 +  terminates it. As a result of remounting and branch-manipulation, the
1885 +  number of branches with fhsm attribute can be one. In this case,
1886 +  /sbin/mount.aufs will terminate the user-space daemon.
1887 +
1888 +
1889 +Finally the operation is done as these steps in kernel-space.
1890 +- make sure that,
1891 +  + no one else is using the file.
1892 +  + the file is not hard-linked.
1893 +  + the file is not pseudo-linked.
1894 +  + the file is a regular file.
1895 +  + the parent dir is not opaqued.
1896 +- find the target writable branch.
1897 +- make sure the file is not whiteout-ed by the upper (than the target)
1898 +  branch.
1899 +- make the parent dir on the target branch.
1900 +- mutex lock the inode on the branch.
1901 +- unlink the whiteout on the target branch (if exists).
1902 +- lookup and create the whiteout-ed temporary name on the target branch.
1903 +- copy the file as the whiteout-ed temporary name on the target branch.
1904 +- rename the whiteout-ed temporary name to the original name.
1905 +- unlink the file on the source branch.
1906 +- maintain the internal pointer array and the external inode number
1907 +  table (XINO).
1908 +- maintain the timestamps and other attributes of the parent dir and the
1909 +  file.
1910 +
1911 +And of course, in every step, an error may happen. So the operation
1912 +should restore the original file state after an error happens.
1913 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
1914 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
1915 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2015-06-22 08:27:37.870834875 +0200
1916 @@ -0,0 +1,72 @@
1917 +
1918 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1919 +# 
1920 +# This program is free software; you can redistribute it and/or modify
1921 +# it under the terms of the GNU General Public License as published by
1922 +# the Free Software Foundation; either version 2 of the License, or
1923 +# (at your option) any later version.
1924 +# 
1925 +# This program is distributed in the hope that it will be useful,
1926 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1927 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1928 +# GNU General Public License for more details.
1929 +# 
1930 +# You should have received a copy of the GNU General Public License
1931 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1932 +
1933 +mmap(2) -- File Memory Mapping
1934 +----------------------------------------------------------------------
1935 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1936 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1937 +->mmap().
1938 +This approach is simple and good, but there is one problem.
1939 +Under /proc, several entries show the mmapped files by its path (with
1940 +device and inode number), and the printed path will be the path on the
1941 +branch fs's instead of virtual aufs's.
1942 +This is not a problem in most cases, but some utilities lsof(1) (and its
1943 +user) may expect the path on aufs.
1944 +
1945 +To address this issue, aufs adds a new member called vm_prfile in struct
1946 +vm_area_struct (and struct vm_region). The original vm_file points to
1947 +the file on the branch fs in order to handle everything correctly as
1948 +usual. The new vm_prfile points to a virtual file in aufs, and the
1949 +show-functions in procfs refers to vm_prfile if it is set.
1950 +Also we need to maintain several other places where touching vm_file
1951 +such like
1952 +- fork()/clone() copies vma and the reference count of vm_file is
1953 +  incremented.
1954 +- merging vma maintains the ref count too.
1955 +
1956 +This is not a good approach. It just fakes the printed path. But it
1957 +leaves all behaviour around f_mapping unchanged. This is surely an
1958 +advantage.
1959 +Actually aufs had adopted another complicated approach which calls
1960 +generic_file_mmap() and handles struct vm_operations_struct. In this
1961 +approach, aufs met a hard problem and I could not solve it without
1962 +switching the approach.
1963 +
1964 +There may be one more another approach which is
1965 +- bind-mount the branch-root onto the aufs-root internally
1966 +- grab the new vfsmount (ie. struct mount)
1967 +- lazy-umount the branch-root internally
1968 +- in open(2) the aufs-file, open the branch-file with the hidden
1969 +  vfsmount (instead of the original branch's vfsmount)
1970 +- ideally this "bind-mount and lazy-umount" should be done atomically,
1971 +  but it may be possible from userspace by the mount helper.
1972 +
1973 +Adding the internal hidden vfsmount and using it in opening a file, the
1974 +file path under /proc will be printed correctly. This approach looks
1975 +smarter, but is not possible I am afraid.
1976 +- aufs-root may be bind-mount later. when it happens, another hidden
1977 +  vfsmount will be required.
1978 +- it is hard to get the chance to bind-mount and lazy-umount
1979 +  + in kernel-space, FS can have vfsmount in open(2) via
1980 +    file->f_path, and aufs can know its vfsmount. But several locks are
1981 +    already acquired, and if aufs tries to bind-mount and lazy-umount
1982 +    here, then it may cause a deadlock.
1983 +  + in user-space, bind-mount doesn't invoke the mount helper.
1984 +- since /proc shows dev and ino, aufs has to give vma these info. it
1985 +  means a new member vm_prinode will be necessary. this is essentially
1986 +  equivalent to vm_prfile described above.
1987 +
1988 +I have to give up this "looks-smater" approach.
1989 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
1990 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
1991 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2015-06-22 08:27:37.870834875 +0200
1992 @@ -0,0 +1,96 @@
1993 +
1994 +# Copyright (C) 2014-2015 Junjiro R. Okajima
1995 +#
1996 +# This program is free software; you can redistribute it and/or modify
1997 +# it under the terms of the GNU General Public License as published by
1998 +# the Free Software Foundation; either version 2 of the License, or
1999 +# (at your option) any later version.
2000 +#
2001 +# This program is distributed in the hope that it will be useful,
2002 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2003 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2004 +# GNU General Public License for more details.
2005 +#
2006 +# You should have received a copy of the GNU General Public License
2007 +# along with this program; if not, write to the Free Software
2008 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2009 +
2010 +
2011 +Listing XATTR/EA and getting the value
2012 +----------------------------------------------------------------------
2013 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2014 +shows the values from the topmost existing file. This behaviour is good
2015 +for the non-dir entries since the bahaviour exactly matches the shown
2016 +information. But for the directories, aufs considers all the same named
2017 +entries on the lower branches. Which means, if one of the lower entry
2018 +rejects readdir call, then aufs returns an error even if the topmost
2019 +entry allows it. This behaviour is necessary to respect the branch fs's
2020 +security, but can make users confused since the user-visible standard
2021 +attributes don't match the behaviour.
2022 +To address this issue, aufs has a mount option called dirperm1 which
2023 +checks the permission for the topmost entry only, and ignores the lower
2024 +entry's permission.
2025 +
2026 +A similar issue can happen around XATTR.
2027 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2028 +always set. Otherwise these very unpleasant situation would happen.
2029 +- listxattr(2) may return the duplicated entries.
2030 +- users may not be able to remove or reset the XATTR forever,
2031 +
2032 +
2033 +XATTR/EA support in the internal (copy,move)-(up,down)
2034 +----------------------------------------------------------------------
2035 +Generally the extended attributes of inode are categorized as these.
2036 +- "security" for LSM and capability.
2037 +- "system" for posix ACL, 'acl' mount option is required for the branch
2038 +  fs generally.
2039 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2040 +- "user" for userspace, 'user_xattr' mount option is required for the
2041 +  branch fs generally.
2042 +
2043 +Moreover there are some other categories. Aufs handles these rather
2044 +unpopular categories as the ordinary ones, ie. there is no special
2045 +condition nor exception.
2046 +
2047 +In copy-up, the support for XATTR on the dst branch may differ from the
2048 +src branch. In this case, the copy-up operation will get an error and
2049 +the original user operation which triggered the copy-up will fail. It
2050 +can happen that even all copy-up will fail.
2051 +When both of src and dst branches support XATTR and if an error occurs
2052 +during copying XATTR, then the copy-up should fail obviously. That is a
2053 +good reason and aufs should return an error to userspace. But when only
2054 +the src branch support that XATTR, aufs should not return an error.
2055 +For example, the src branch supports ACL but the dst branch doesn't
2056 +because the dst branch may natively un-support it or temporary
2057 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2058 +may NOT return an error even if the XATTR is not supported. It is
2059 +totally up to the branch fs.
2060 +
2061 +Anyway when the aufs internal copy-up gets an error from the dst branch
2062 +fs, then aufs tries removing the just copied entry and returns the error
2063 +to the userspace. The worst case of this situation will be all copy-up
2064 +will fail.
2065 +
2066 +For the copy-up operation, there two basic approaches.
2067 +- copy the specified XATTR only (by category above), and return the
2068 +  error unconditionally if it happens.
2069 +- copy all XATTR, and ignore the error on the specified category only.
2070 +
2071 +In order to support XATTR and to implement the correct behaviour, aufs
2072 +chooses the latter approach and introduces some new branch attributes,
2073 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2074 +They correspond to the XATTR namespaces (see above). Additionally, to be
2075 +convenient, "icex" is also provided which means all "icex*" attributes
2076 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2077 +
2078 +The meaning of these attributes is to ignore the error from setting
2079 +XATTR on that branch.
2080 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2081 +error from the dst branch according to the specified attributes.
2082 +
2083 +Some XATTR may have its default value. The default value may come from
2084 +the parent dir or the environment. If the default value is set at the
2085 +file creating-time, it will be overwritten by copy-up.
2086 +Some contradiction may happen I am afraid.
2087 +Do we need another attribute to stop copying XATTR? I am unsure. For
2088 +now, aufs implements the branch attributes to ignore the error.
2089 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2090 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2091 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2015-06-22 08:27:37.870834875 +0200
2092 @@ -0,0 +1,58 @@
2093 +
2094 +# Copyright (C) 2005-2015 Junjiro R. Okajima
2095 +# 
2096 +# This program is free software; you can redistribute it and/or modify
2097 +# it under the terms of the GNU General Public License as published by
2098 +# the Free Software Foundation; either version 2 of the License, or
2099 +# (at your option) any later version.
2100 +# 
2101 +# This program is distributed in the hope that it will be useful,
2102 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2103 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2104 +# GNU General Public License for more details.
2105 +# 
2106 +# You should have received a copy of the GNU General Public License
2107 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2108 +
2109 +Export Aufs via NFS
2110 +----------------------------------------------------------------------
2111 +Here is an approach.
2112 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2113 +  generation.
2114 +- iget_locked(): initialize aufs inode generation for a new inode, and
2115 +  store it in xigen file.
2116 +- destroy_inode(): increment aufs inode generation and store it in xigen
2117 +  file. it is necessary even if it is not unlinked, because any data of
2118 +  inode may be changed by UDBA.
2119 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2120 +  build file handle by
2121 +  + branch id (4 bytes)
2122 +  + superblock generation (4 bytes)
2123 +  + inode number (4 or 8 bytes)
2124 +  + parent dir inode number (4 or 8 bytes)
2125 +  + inode generation (4 bytes))
2126 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2127 +    bytes)
2128 +  + file handle for a branch (by exportfs_encode_fh())
2129 +- fh_to_dentry():
2130 +  + find the index of a branch from its id in handle, and check it is
2131 +    still exist in aufs.
2132 +  + 1st level: get the inode number from handle and search it in cache.
2133 +  + 2nd level: if not found in cache, get the parent inode number from
2134 +    the handle and search it in cache. and then open the found parent
2135 +    dir, find the matching inode number by vfs_readdir() and get its
2136 +    name, and call lookup_one_len() for the target dentry.
2137 +  + 3rd level: if the parent dir is not cached, call
2138 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2139 +    build a pathname of it, convert it a pathname in aufs, call
2140 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2141 +    the 2nd level.
2142 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2143 +    for every branch, but not itself. to get this, (currently) aufs
2144 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2145 +    idea, but I didn't get other approach.
2146 +  + test the generation of the gotten inode.
2147 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2148 +  convert it into ESTALE for NFSD.
2149 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2150 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2151 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2152 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2153 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2015-06-22 08:27:37.870834875 +0200
2154 @@ -0,0 +1,52 @@
2155 +
2156 +# Copyright (C) 2005-2015 Junjiro R. Okajima
2157 +# 
2158 +# This program is free software; you can redistribute it and/or modify
2159 +# it under the terms of the GNU General Public License as published by
2160 +# the Free Software Foundation; either version 2 of the License, or
2161 +# (at your option) any later version.
2162 +# 
2163 +# This program is distributed in the hope that it will be useful,
2164 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2165 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2166 +# GNU General Public License for more details.
2167 +# 
2168 +# You should have received a copy of the GNU General Public License
2169 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2170 +
2171 +Show Whiteout Mode (shwh)
2172 +----------------------------------------------------------------------
2173 +Generally aufs hides the name of whiteouts. But in some cases, to show
2174 +them is very useful for users. For instance, creating a new middle layer
2175 +(branch) by merging existing layers.
2176 +
2177 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2178 +When you have three branches,
2179 +- Bottom: 'system', squashfs (underlying base system), read-only
2180 +- Middle: 'mods', squashfs, read-only
2181 +- Top: 'overlay', ram (tmpfs), read-write
2182 +
2183 +The top layer is loaded at boot time and saved at shutdown, to preserve
2184 +the changes made to the system during the session.
2185 +When larger changes have been made, or smaller changes have accumulated,
2186 +the size of the saved top layer data grows. At this point, it would be
2187 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2188 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2189 +restoring save and load speed.
2190 +
2191 +This merging is simplified by the use of another aufs mount, of just the
2192 +two overlay branches using the 'shwh' option.
2193 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2194 +       aufs /livesys/merge_union
2195 +
2196 +A merged view of these two branches is then available at
2197 +/livesys/merge_union, and the new feature is that the whiteouts are
2198 +visible!
2199 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2200 +writing to all branches. Also the default mode for all branches is 'ro'.
2201 +It is now possible to save the combined contents of the two overlay
2202 +branches to a new squashfs, e.g.:
2203 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2204 +
2205 +This new squashfs archive can be stored on the boot device and the
2206 +initramfs will use it to replace the old one at the next boot.
2207 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2208 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2209 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2015-06-22 08:27:37.870834875 +0200
2210 @@ -0,0 +1,47 @@
2211 +
2212 +# Copyright (C) 2010-2015 Junjiro R. Okajima
2213 +#
2214 +# This program is free software; you can redistribute it and/or modify
2215 +# it under the terms of the GNU General Public License as published by
2216 +# the Free Software Foundation; either version 2 of the License, or
2217 +# (at your option) any later version.
2218 +#
2219 +# This program is distributed in the hope that it will be useful,
2220 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2221 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2222 +# GNU General Public License for more details.
2223 +#
2224 +# You should have received a copy of the GNU General Public License
2225 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2226 +
2227 +Dynamically customizable FS operations
2228 +----------------------------------------------------------------------
2229 +Generally FS operations (struct inode_operations, struct
2230 +address_space_operations, struct file_operations, etc.) are defined as
2231 +"static const", but it never means that FS have only one set of
2232 +operation. Some FS have multiple sets of them. For instance, ext2 has
2233 +three sets, one for XIP, for NOBH, and for normal.
2234 +Since aufs overrides and redirects these operations, sometimes aufs has
2235 +to change its behaviour according to the branch FS type. More importantly
2236 +VFS acts differently if a function (member in the struct) is set or
2237 +not. It means aufs should have several sets of operations and select one
2238 +among them according to the branch FS definition.
2239 +
2240 +In order to solve this problem and not to affect the behaviour of VFS,
2241 +aufs defines these operations dynamically. For instance, aufs defines
2242 +dummy direct_IO function for struct address_space_operations, but it may
2243 +not be set to the address_space_operations actually. When the branch FS
2244 +doesn't have it, aufs doesn't set it to its address_space_operations
2245 +while the function definition itself is still alive. So the behaviour
2246 +itself will not change, and it will return an error when direct_IO is
2247 +not set.
2248 +
2249 +The lifetime of these dynamically generated operation object is
2250 +maintained by aufs branch object. When the branch is removed from aufs,
2251 +the reference counter of the object is decremented. When it reaches
2252 +zero, the dynamically generated operation object will be freed.
2253 +
2254 +This approach is designed to support AIO (io_submit), Direct I/O and
2255 +XIP (DAX) mainly.
2256 +Currently this approach is applied to address_space_operations for
2257 +regular files only.
2258 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt linux/Documentation/filesystems/aufs/design/99plan.txt
2259 --- /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt   1970-01-01 01:00:00.000000000 +0100
2260 +++ linux/Documentation/filesystems/aufs/design/99plan.txt      2015-06-22 08:27:37.870834875 +0200
2261 @@ -0,0 +1,57 @@
2262 +
2263 +# Copyright (C) 2005-2015 Junjiro R. Okajima
2264 +# 
2265 +# This program is free software; you can redistribute it and/or modify
2266 +# it under the terms of the GNU General Public License as published by
2267 +# the Free Software Foundation; either version 2 of the License, or
2268 +# (at your option) any later version.
2269 +# 
2270 +# This program is distributed in the hope that it will be useful,
2271 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2272 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2273 +# GNU General Public License for more details.
2274 +# 
2275 +# You should have received a copy of the GNU General Public License
2276 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2277 +
2278 +Plan
2279 +
2280 +Restoring some features which was implemented in aufs1.
2281 +They were dropped in aufs2 in order to make source files simpler and
2282 +easier to be reviewed.
2283 +
2284 +
2285 +Being Another Aufs's Readonly Branch (robr)
2286 +----------------------------------------------------------------------
2287 +Aufs1 allows aufs to be another aufs's readonly branch.
2288 +This feature was developed by a user's request. But it may not be used
2289 +currently.
2290 +
2291 +
2292 +Refresh the Opened File (refrof)
2293 +----------------------------------------------------------------------
2294 +This option is implemented in aufs1 but incomplete.
2295 +
2296 +When user reads from a file, he expects to get its latest filedata
2297 +generally. If the file is removed and a new same named file is created,
2298 +the content he gets is unchanged, ie. the unlinked filedata.
2299 +
2300 +Let's try case study again.
2301 +- aufs has two branches.
2302 +  /au = /rw + /ro
2303 +- "fileA" exists under /ro, but /rw.
2304 +- user opened "/au/fileA".
2305 +- he or someone else inserts a branch (/new) between /rw and /ro.
2306 +  /au = /rw + /new + /ro
2307 +- the new branch contains "fileA".
2308 +- user reads from the opened "fileA"
2309 +- which filedata should aufs return, from /ro or /new?
2310 +
2311 +Some people says it has to be "from /ro" and it is a semantics of Unix.
2312 +The others say it should be "from /new" because the file is not removed
2313 +and it is equivalent to the case of someone else modifies the file.
2314 +
2315 +Here again I don't have a best and final answer. I got an idea to
2316 +implement 'refrof' and 'norefrof' option. When 'refrof' (REFResh the
2317 +Opened File) is specified (by default), aufs returns the filedata from
2318 +/new. Otherwise from /ro.
2319 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2320 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2321 +++ linux/Documentation/filesystems/aufs/README 2015-06-22 08:27:37.867501461 +0200
2322 @@ -0,0 +1,384 @@
2323 +
2324 +Aufs3 -- advanced multi layered unification filesystem version 3.x
2325 +http://aufs.sf.net
2326 +Junjiro R. Okajima
2327 +
2328 +
2329 +0. Introduction
2330 +----------------------------------------
2331 +In the early days, aufs was entirely re-designed and re-implemented
2332 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2333 +improvements and implementations, it becomes totally different from
2334 +Unionfs while keeping the basic features.
2335 +Recently, Unionfs Version 2.x series begin taking some of the same
2336 +approaches to aufs1's.
2337 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2338 +University and his team.
2339 +
2340 +Aufs3 supports linux-3.0 and later.
2341 +If you want older kernel version support, try aufs2-2.6.git or
2342 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2343 +
2344 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2345 +      According to Christoph Hellwig, linux rejects all union-type
2346 +      filesystems but UnionMount.
2347 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2348 +
2349 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2350 +    UnionMount, and he pointed out an issue around a directory mutex
2351 +    lock and aufs addressed it. But it is still unsure whether aufs will
2352 +    be merged (or any other union solution).
2353 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2354 +
2355 +
2356 +1. Features
2357 +----------------------------------------
2358 +- unite several directories into a single virtual filesystem. The member
2359 +  directory is called as a branch.
2360 +- you can specify the permission flags to the branch, which are 'readonly',
2361 +  'readwrite' and 'whiteout-able.'
2362 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2363 +  readonly branch are modifiable logically.
2364 +- dynamic branch manipulation, add, del.
2365 +- etc...
2366 +
2367 +Also there are many enhancements in aufs, such as:
2368 +- test only the highest one for the directory permission (dirperm1)
2369 +- copyup on open (coo=)
2370 +- 'move' policy for copy-up between two writable branches, after
2371 +  checking free space.
2372 +- xattr, acl
2373 +- readdir(3) in userspace.
2374 +- keep inode number by external inode number table
2375 +- keep the timestamps of file/dir in internal copyup operation
2376 +- seekable directory, supporting NFS readdir.
2377 +- whiteout is hardlinked in order to reduce the consumption of inodes
2378 +  on branch
2379 +- do not copyup, nor create a whiteout when it is unnecessary
2380 +- revert a single systemcall when an error occurs in aufs
2381 +- remount interface instead of ioctl
2382 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2383 +- loopback mounted filesystem as a branch
2384 +- kernel thread for removing the dir who has a plenty of whiteouts
2385 +- support copyup sparse file (a file which has a 'hole' in it)
2386 +- default permission flags for branches
2387 +- selectable permission flags for ro branch, whether whiteout can
2388 +  exist or not
2389 +- export via NFS.
2390 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2391 +- support multiple writable branches, some policies to select one
2392 +  among multiple writable branches.
2393 +- a new semantics for link(2) and rename(2) to support multiple
2394 +  writable branches.
2395 +- no glibc changes are required.
2396 +- pseudo hardlink (hardlink over branches)
2397 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2398 +  including NFS or remote filesystem branch.
2399 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2400 +- and more...
2401 +
2402 +Currently these features are dropped temporary from aufs3.
2403 +See design/08plan.txt in detail.
2404 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2405 +  (robr)
2406 +- statistics of aufs thread (/sys/fs/aufs/stat)
2407 +
2408 +Features or just an idea in the future (see also design/*.txt),
2409 +- reorder the branch index without del/re-add.
2410 +- permanent xino files for NFSD
2411 +- an option for refreshing the opened files after add/del branches
2412 +- light version, without branch manipulation. (unnecessary?)
2413 +- copyup in userspace
2414 +- inotify in userspace
2415 +- readv/writev
2416 +
2417 +
2418 +2. Download
2419 +----------------------------------------
2420 +There were three GIT trees for aufs3, aufs3-linux.git,
2421 +aufs3-standalone.git, and aufs-util.git. Note that there is no "3" in
2422 +"aufs-util.git."
2423 +While the aufs-util is always necessary, you need either of aufs3-linux
2424 +or aufs3-standalone.
2425 +
2426 +The aufs3-linux tree includes the whole linux mainline GIT tree,
2427 +git://git.kernel.org/.../torvalds/linux.git.
2428 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2429 +build aufs3 as an external kernel module.
2430 +Several extra patches are not included in this tree. Only
2431 +aufs3-standalong tree contains them. They are describe in the later
2432 +section "Configuration and Compilation."
2433 +
2434 +On the other hand, the aufs3-standalone tree has only aufs source files
2435 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2436 +But you need to apply all aufs patches manually.
2437 +
2438 +You will find GIT branches whose name is in form of "aufs3.x" where "x"
2439 +represents the linux kernel version, "linux-3.x". For instance,
2440 +"aufs3.0" is for linux-3.0. For latest "linux-3.x-rcN", use
2441 +"aufs3.x-rcN" branch.
2442 +
2443 +o aufs3-linux tree
2444 +$ git clone --reference /your/linux/git/tree \
2445 +       git://git.code.sf.net/p/aufs/aufs3-linux aufs3-linux.git
2446 +- if you don't have linux GIT tree, then remove "--reference ..."
2447 +$ cd aufs3-linux.git
2448 +$ git checkout origin/aufs3.0
2449 +
2450 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2451 +leave the patch-work to GIT.
2452 +$ cd /your/linux/git/tree
2453 +$ git remote add aufs3 https://github.com/sfjro/aufs3-linux.git
2454 +- aufs3-linux.git tree also exists on github.
2455 +$ git fetch aufs3
2456 +$ git checkout -b my3.14 v3.14
2457 +$ (add your change...)
2458 +$ git pull aufs3 aufs3.14
2459 +- now you have v3.14 + your_changes + aufs3.14 in you my3.14 branch.
2460 +- you may need to solve some conflicts between your_changes and
2461 +  aufs3.14. in this case, git-rerere is recommended so that you can
2462 +  solve the similar confilicts automatically when you upgrade to 3.15 or
2463 +  later in the future.
2464 +
2465 +o aufs3-standalone tree
2466 +$ git clone git://git.code.sf.net/p/aufs/aufs3-standalone \
2467 +       aufs3-standalone.git
2468 +$ cd aufs3-standalone.git
2469 +$ git checkout origin/aufs3.0
2470 +
2471 +o aufs-util tree
2472 +$ git clone git://git.code.sf.net/p/aufs/aufs-util \
2473 +       aufs-util.git
2474 +$ cd aufs-util.git
2475 +$ git checkout origin/aufs3.0
2476 +
2477 +Note: The 3.x-rcN branch is to be used with `rc' kernel versions ONLY.
2478 +The minor version number, 'x' in '3.x', of aufs may not always
2479 +follow the minor version number of the kernel.
2480 +Because changes in the kernel that cause the use of a new
2481 +minor version number do not always require changes to aufs-util.
2482 +
2483 +Since aufs-util has its own minor version number, you may not be
2484 +able to find a GIT branch in aufs-util for your kernel's
2485 +exact minor version number.
2486 +In this case, you should git-checkout the branch for the
2487 +nearest lower number.
2488 +
2489 +For (an unreleased) example:
2490 +If you are using "linux-3.10" and the "aufs3.10" branch
2491 +does not exist in aufs-util repository, then "aufs3.9", "aufs3.8"
2492 +or something numerically smaller is the branch for your kernel.
2493 +
2494 +Also you can view all branches by
2495 +       $ git branch -a
2496 +
2497 +
2498 +3. Configuration and Compilation
2499 +----------------------------------------
2500 +Make sure you have git-checkout'ed the correct branch.
2501 +
2502 +For aufs3-linux tree,
2503 +- enable CONFIG_AUFS_FS.
2504 +- set other aufs configurations if necessary.
2505 +
2506 +For aufs3-standalone tree,
2507 +There are several ways to build.
2508 +
2509 +1.
2510 +- apply ./aufs3-kbuild.patch to your kernel source files.
2511 +- apply ./aufs3-base.patch too.
2512 +- apply ./aufs3-mmap.patch too.
2513 +- apply ./aufs3-standalone.patch too, if you have a plan to set
2514 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs3-standalone.patch.
2515 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2516 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2517 +- enable CONFIG_AUFS_FS, you can select either
2518 +  =m or =y.
2519 +- and build your kernel as usual.
2520 +- install the built kernel.
2521 +  Note: Since linux-3.9, every filesystem module requires an alias
2522 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2523 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2524 +- install the header files too by "make headers_install" to the
2525 +  directory where you specify. By default, it is $PWD/usr.
2526 +  "make help" shows a brief note for headers_install.
2527 +- and reboot your system.
2528 +
2529 +2.
2530 +- module only (CONFIG_AUFS_FS=m).
2531 +- apply ./aufs3-base.patch to your kernel source files.
2532 +- apply ./aufs3-mmap.patch too.
2533 +- apply ./aufs3-standalone.patch too.
2534 +- build your kernel, don't forget "make headers_install", and reboot.
2535 +- edit ./config.mk and set other aufs configurations if necessary.
2536 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2537 +  every aufs configurations.
2538 +- build the module by simple "make".
2539 +  Note: Since linux-3.9, every filesystem module requires an alias
2540 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2541 +  modules.aliases file.
2542 +- you can specify ${KDIR} make variable which points to your kernel
2543 +  source tree.
2544 +- install the files
2545 +  + run "make install" to install the aufs module, or copy the built
2546 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2547 +  + run "make install_headers" (instead of headers_install) to install
2548 +    the modified aufs header file (you can specify DESTDIR which is
2549 +    available in aufs standalone version's Makefile only), or copy
2550 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2551 +    you like manually. By default, the target directory is $PWD/usr.
2552 +- no need to apply aufs3-kbuild.patch, nor copying source files to your
2553 +  kernel source tree.
2554 +
2555 +Note: The header file aufs_type.h is necessary to build aufs-util
2556 +      as well as "make headers_install" in the kernel source tree.
2557 +      headers_install is subject to be forgotten, but it is essentially
2558 +      necessary, not only for building aufs-util.
2559 +      You may not meet problems without headers_install in some older
2560 +      version though.
2561 +
2562 +And then,
2563 +- read README in aufs-util, build and install it
2564 +- note that your distribution may contain an obsoleted version of
2565 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2566 +  utilities, make sure that your compiler refers the correct aufs header
2567 +  file which is built by "make headers_install."
2568 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2569 +  then run "make install_ulib" too. And refer to the aufs manual in
2570 +  detail.
2571 +
2572 +There several other patches in aufs3-standalone.git. They are all
2573 +optional. When you meet some problems, they will help you.
2574 +- aufs3-loopback.patch
2575 +  Supports a nested loopback mount in a branch-fs. This patch is
2576 +  unnecessary until aufs produces a message like "you may want to try
2577 +  another patch for loopback file".
2578 +- vfs-ino.patch
2579 +  Modifies a system global kernel internal function get_next_ino() in
2580 +  order to stop assigning 0 for an inode-number. Not directly related to
2581 +  aufs, but recommended generally.
2582 +- tmpfs-idr.patch
2583 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2584 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2585 +  duplication of inode number, which is important for backup tools and
2586 +  other utilities. When you find aufs XINO files for tmpfs branch
2587 +  growing too much, try this patch.
2588 +
2589 +
2590 +4. Usage
2591 +----------------------------------------
2592 +At first, make sure aufs-util are installed, and please read the aufs
2593 +manual, aufs.5 in aufs-util.git tree.
2594 +$ man -l aufs.5
2595 +
2596 +And then,
2597 +$ mkdir /tmp/rw /tmp/aufs
2598 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2599 +
2600 +Here is another example. The result is equivalent.
2601 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2602 +  Or
2603 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2604 +# mount -o remount,append:${HOME} /tmp/aufs
2605 +
2606 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2607 +you modify a file under /tmp/aufs, the one on your home directory is
2608 +not affected, instead the same named file will be newly created under
2609 +/tmp/rw. And all of your modification to a file will be applied to
2610 +the one under /tmp/rw. This is called the file based Copy on Write
2611 +(COW) method.
2612 +Aufs mount options are described in aufs.5.
2613 +If you run chroot or something and make your aufs as a root directory,
2614 +then you need to customize the shutdown script. See the aufs manual in
2615 +detail.
2616 +
2617 +Additionally, there are some sample usages of aufs which are a
2618 +diskless system with network booting, and LiveCD over NFS.
2619 +See sample dir in CVS tree on SourceForge.
2620 +
2621 +
2622 +5. Contact
2623 +----------------------------------------
2624 +When you have any problems or strange behaviour in aufs, please let me
2625 +know with:
2626 +- /proc/mounts (instead of the output of mount(8))
2627 +- /sys/module/aufs/*
2628 +- /sys/fs/aufs/* (if you have them)
2629 +- /debug/aufs/* (if you have them)
2630 +- linux kernel version
2631 +  if your kernel is not plain, for example modified by distributor,
2632 +  the url where i can download its source is necessary too.
2633 +- aufs version which was printed at loading the module or booting the
2634 +  system, instead of the date you downloaded.
2635 +- configuration (define/undefine CONFIG_AUFS_xxx)
2636 +- kernel configuration or /proc/config.gz (if you have it)
2637 +- behaviour which you think to be incorrect
2638 +- actual operation, reproducible one is better
2639 +- mailto: aufs-users at lists.sourceforge.net
2640 +
2641 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2642 +and Feature Requests) on SourceForge. Please join and write to
2643 +aufs-users ML.
2644 +
2645 +
2646 +6. Acknowledgements
2647 +----------------------------------------
2648 +Thanks to everyone who have tried and are using aufs, whoever
2649 +have reported a bug or any feedback.
2650 +
2651 +Especially donators:
2652 +Tomas Matejicek(slax.org) made a donation (much more than once).
2653 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2654 +       scripts) is making "doubling" donations.
2655 +       Unfortunately I cannot list all of the donators, but I really
2656 +       appreciate.
2657 +       It ends Aug 2010, but the ordinary donation URL is still available.
2658 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2659 +Dai Itasaka made a donation (2007/8).
2660 +Chuck Smith made a donation (2008/4, 10 and 12).
2661 +Henk Schoneveld made a donation (2008/9).
2662 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2663 +Francois Dupoux made a donation (2008/11).
2664 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2665 +       aufs2 GIT tree (2009/2).
2666 +William Grant made a donation (2009/3).
2667 +Patrick Lane made a donation (2009/4).
2668 +The Mail Archive (mail-archive.com) made donations (2009/5).
2669 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2670 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2671 +Pavel Pronskiy made a donation (2011/2).
2672 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2673 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2674 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2675 +11).
2676 +Sam Liddicott made a donation (2011/9).
2677 +Era Scarecrow made a donation (2013/4).
2678 +Bor Ratajc made a donation (2013/4).
2679 +Alessandro Gorreta made a donation (2013/4).
2680 +POIRETTE Marc made a donation (2013/4).
2681 +Alessandro Gorreta made a donation (2013/4).
2682 +lauri kasvandik made a donation (2013/5).
2683 +"pemasu from Finland" made a donation (2013/7).
2684 +The Parted Magic Project made a donation (2013/9 and 11).
2685 +Pavel Barta made a donation (2013/10).
2686 +Nikolay Pertsev made a donation (2014/5).
2687 +James B made a donation (2014/7).
2688 +Stefano Di Biase made a donation (2014/8).
2689 +Daniel Epellei made a donation (2015/1).
2690 +
2691 +Thank you very much.
2692 +Donations are always, including future donations, very important and
2693 +helpful for me to keep on developing aufs.
2694 +
2695 +
2696 +7.
2697 +----------------------------------------
2698 +If you are an experienced user, no explanation is needed. Aufs is
2699 +just a linux filesystem.
2700 +
2701 +
2702 +Enjoy!
2703 +
2704 +# Local variables: ;
2705 +# mode: text;
2706 +# End: ;
2707 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2708 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2709 +++ linux/fs/aufs/aufs.h        2015-06-22 08:27:37.874168290 +0200
2710 @@ -0,0 +1,59 @@
2711 +/*
2712 + * Copyright (C) 2005-2015 Junjiro R. Okajima
2713 + *
2714 + * This program, aufs is free software; you can redistribute it and/or modify
2715 + * it under the terms of the GNU General Public License as published by
2716 + * the Free Software Foundation; either version 2 of the License, or
2717 + * (at your option) any later version.
2718 + *
2719 + * This program is distributed in the hope that it will be useful,
2720 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2721 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2722 + * GNU General Public License for more details.
2723 + *
2724 + * You should have received a copy of the GNU General Public License
2725 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2726 + */
2727 +
2728 +/*
2729 + * all header files
2730 + */
2731 +
2732 +#ifndef __AUFS_H__
2733 +#define __AUFS_H__
2734 +
2735 +#ifdef __KERNEL__
2736 +
2737 +#define AuStub(type, name, body, ...) \
2738 +       static inline type name(__VA_ARGS__) { body; }
2739 +
2740 +#define AuStubVoid(name, ...) \
2741 +       AuStub(void, name, , __VA_ARGS__)
2742 +#define AuStubInt0(name, ...) \
2743 +       AuStub(int, name, return 0, __VA_ARGS__)
2744 +
2745 +#include "debug.h"
2746 +
2747 +#include "branch.h"
2748 +#include "cpup.h"
2749 +#include "dcsub.h"
2750 +#include "dbgaufs.h"
2751 +#include "dentry.h"
2752 +#include "dir.h"
2753 +#include "dynop.h"
2754 +#include "file.h"
2755 +#include "fstype.h"
2756 +#include "inode.h"
2757 +#include "loop.h"
2758 +#include "module.h"
2759 +#include "opts.h"
2760 +#include "rwsem.h"
2761 +#include "spl.h"
2762 +#include "super.h"
2763 +#include "sysaufs.h"
2764 +#include "vfsub.h"
2765 +#include "whout.h"
2766 +#include "wkq.h"
2767 +
2768 +#endif /* __KERNEL__ */
2769 +#endif /* __AUFS_H__ */
2770 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2771 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2772 +++ linux/fs/aufs/branch.c      2015-06-22 08:29:23.703424266 +0200
2773 @@ -0,0 +1,1406 @@
2774 +/*
2775 + * Copyright (C) 2005-2015 Junjiro R. Okajima
2776 + *
2777 + * This program, aufs is free software; you can redistribute it and/or modify
2778 + * it under the terms of the GNU General Public License as published by
2779 + * the Free Software Foundation; either version 2 of the License, or
2780 + * (at your option) any later version.
2781 + *
2782 + * This program is distributed in the hope that it will be useful,
2783 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2784 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2785 + * GNU General Public License for more details.
2786 + *
2787 + * You should have received a copy of the GNU General Public License
2788 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2789 + */
2790 +
2791 +/*
2792 + * branch management
2793 + */
2794 +
2795 +#include <linux/compat.h>
2796 +#include <linux/statfs.h>
2797 +#include "aufs.h"
2798 +
2799 +/*
2800 + * free a single branch
2801 + */
2802 +static void au_br_do_free(struct au_branch *br)
2803 +{
2804 +       int i;
2805 +       struct au_wbr *wbr;
2806 +       struct au_dykey **key;
2807 +
2808 +       au_hnotify_fin_br(br);
2809 +
2810 +       if (br->br_xino.xi_file)
2811 +               fput(br->br_xino.xi_file);
2812 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
2813 +
2814 +       AuDebugOn(atomic_read(&br->br_count));
2815 +
2816 +       wbr = br->br_wbr;
2817 +       if (wbr) {
2818 +               for (i = 0; i < AuBrWh_Last; i++)
2819 +                       dput(wbr->wbr_wh[i]);
2820 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2821 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2822 +       }
2823 +
2824 +       if (br->br_fhsm) {
2825 +               au_br_fhsm_fin(br->br_fhsm);
2826 +               kfree(br->br_fhsm);
2827 +       }
2828 +
2829 +       key = br->br_dykey;
2830 +       for (i = 0; i < AuBrDynOp; i++, key++)
2831 +               if (*key)
2832 +                       au_dy_put(*key);
2833 +               else
2834 +                       break;
2835 +
2836 +       /* recursive lock, s_umount of branch's */
2837 +       lockdep_off();
2838 +       path_put(&br->br_path);
2839 +       lockdep_on();
2840 +       kfree(wbr);
2841 +       kfree(br);
2842 +}
2843 +
2844 +/*
2845 + * frees all branches
2846 + */
2847 +void au_br_free(struct au_sbinfo *sbinfo)
2848 +{
2849 +       aufs_bindex_t bmax;
2850 +       struct au_branch **br;
2851 +
2852 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2853 +
2854 +       bmax = sbinfo->si_bend + 1;
2855 +       br = sbinfo->si_branch;
2856 +       while (bmax--)
2857 +               au_br_do_free(*br++);
2858 +}
2859 +
2860 +/*
2861 + * find the index of a branch which is specified by @br_id.
2862 + */
2863 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2864 +{
2865 +       aufs_bindex_t bindex, bend;
2866 +
2867 +       bend = au_sbend(sb);
2868 +       for (bindex = 0; bindex <= bend; bindex++)
2869 +               if (au_sbr_id(sb, bindex) == br_id)
2870 +                       return bindex;
2871 +       return -1;
2872 +}
2873 +
2874 +/* ---------------------------------------------------------------------- */
2875 +
2876 +/*
2877 + * add a branch
2878 + */
2879 +
2880 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2881 +                       struct dentry *h_root)
2882 +{
2883 +       if (unlikely(h_adding == h_root
2884 +                    || au_test_loopback_overlap(sb, h_adding)))
2885 +               return 1;
2886 +       if (h_adding->d_sb != h_root->d_sb)
2887 +               return 0;
2888 +       return au_test_subdir(h_adding, h_root)
2889 +               || au_test_subdir(h_root, h_adding);
2890 +}
2891 +
2892 +/*
2893 + * returns a newly allocated branch. @new_nbranch is a number of branches
2894 + * after adding a branch.
2895 + */
2896 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2897 +                                    int perm)
2898 +{
2899 +       struct au_branch *add_branch;
2900 +       struct dentry *root;
2901 +       int err;
2902 +
2903 +       err = -ENOMEM;
2904 +       root = sb->s_root;
2905 +       add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
2906 +       if (unlikely(!add_branch))
2907 +               goto out;
2908 +
2909 +       err = au_hnotify_init_br(add_branch, perm);
2910 +       if (unlikely(err))
2911 +               goto out_br;
2912 +
2913 +       add_branch->br_wbr = NULL;
2914 +       if (au_br_writable(perm)) {
2915 +               /* may be freed separately at changing the branch permission */
2916 +               add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
2917 +                                            GFP_NOFS);
2918 +               if (unlikely(!add_branch->br_wbr))
2919 +                       goto out_hnotify;
2920 +       }
2921 +
2922 +       add_branch->br_fhsm = NULL;
2923 +       if (au_br_fhsm(perm)) {
2924 +               err = au_fhsm_br_alloc(add_branch);
2925 +               if (unlikely(err))
2926 +                       goto out_wbr;
2927 +       }
2928 +
2929 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
2930 +       if (!err)
2931 +               err = au_di_realloc(au_di(root), new_nbranch);
2932 +       if (!err)
2933 +               err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
2934 +       if (!err)
2935 +               return add_branch; /* success */
2936 +
2937 +out_wbr:
2938 +       kfree(add_branch->br_wbr);
2939 +out_hnotify:
2940 +       au_hnotify_fin_br(add_branch);
2941 +out_br:
2942 +       kfree(add_branch);
2943 +out:
2944 +       return ERR_PTR(err);
2945 +}
2946 +
2947 +/*
2948 + * test if the branch permission is legal or not.
2949 + */
2950 +static int test_br(struct inode *inode, int brperm, char *path)
2951 +{
2952 +       int err;
2953 +
2954 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
2955 +       if (!err)
2956 +               goto out;
2957 +
2958 +       err = -EINVAL;
2959 +       pr_err("write permission for readonly mount or inode, %s\n", path);
2960 +
2961 +out:
2962 +       return err;
2963 +}
2964 +
2965 +/*
2966 + * returns:
2967 + * 0: success, the caller will add it
2968 + * plus: success, it is already unified, the caller should ignore it
2969 + * minus: error
2970 + */
2971 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
2972 +{
2973 +       int err;
2974 +       aufs_bindex_t bend, bindex;
2975 +       struct dentry *root;
2976 +       struct inode *inode, *h_inode;
2977 +
2978 +       root = sb->s_root;
2979 +       bend = au_sbend(sb);
2980 +       if (unlikely(bend >= 0
2981 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
2982 +               err = 1;
2983 +               if (!remount) {
2984 +                       err = -EINVAL;
2985 +                       pr_err("%s duplicated\n", add->pathname);
2986 +               }
2987 +               goto out;
2988 +       }
2989 +
2990 +       err = -ENOSPC; /* -E2BIG; */
2991 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2992 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
2993 +               pr_err("number of branches exceeded %s\n", add->pathname);
2994 +               goto out;
2995 +       }
2996 +
2997 +       err = -EDOM;
2998 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
2999 +               pr_err("bad index %d\n", add->bindex);
3000 +               goto out;
3001 +       }
3002 +
3003 +       inode = add->path.dentry->d_inode;
3004 +       err = -ENOENT;
3005 +       if (unlikely(!inode->i_nlink)) {
3006 +               pr_err("no existence %s\n", add->pathname);
3007 +               goto out;
3008 +       }
3009 +
3010 +       err = -EINVAL;
3011 +       if (unlikely(inode->i_sb == sb)) {
3012 +               pr_err("%s must be outside\n", add->pathname);
3013 +               goto out;
3014 +       }
3015 +
3016 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3017 +               pr_err("unsupported filesystem, %s (%s)\n",
3018 +                      add->pathname, au_sbtype(inode->i_sb));
3019 +               goto out;
3020 +       }
3021 +
3022 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3023 +               pr_err("already stacked, %s (%s)\n",
3024 +                      add->pathname, au_sbtype(inode->i_sb));
3025 +               goto out;
3026 +       }
3027 +
3028 +       err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
3029 +       if (unlikely(err))
3030 +               goto out;
3031 +
3032 +       if (bend < 0)
3033 +               return 0; /* success */
3034 +
3035 +       err = -EINVAL;
3036 +       for (bindex = 0; bindex <= bend; bindex++)
3037 +               if (unlikely(test_overlap(sb, add->path.dentry,
3038 +                                         au_h_dptr(root, bindex)))) {
3039 +                       pr_err("%s is overlapped\n", add->pathname);
3040 +                       goto out;
3041 +               }
3042 +
3043 +       err = 0;
3044 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3045 +               h_inode = au_h_dptr(root, 0)->d_inode;
3046 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3047 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3048 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3049 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3050 +                               add->pathname,
3051 +                               i_uid_read(inode), i_gid_read(inode),
3052 +                               (inode->i_mode & S_IALLUGO),
3053 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3054 +                               (h_inode->i_mode & S_IALLUGO));
3055 +       }
3056 +
3057 +out:
3058 +       return err;
3059 +}
3060 +
3061 +/*
3062 + * initialize or clean the whiteouts for an adding branch
3063 + */
3064 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3065 +                        int new_perm)
3066 +{
3067 +       int err, old_perm;
3068 +       aufs_bindex_t bindex;
3069 +       struct mutex *h_mtx;
3070 +       struct au_wbr *wbr;
3071 +       struct au_hinode *hdir;
3072 +
3073 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3074 +       if (unlikely(err))
3075 +               goto out;
3076 +
3077 +       wbr = br->br_wbr;
3078 +       old_perm = br->br_perm;
3079 +       br->br_perm = new_perm;
3080 +       hdir = NULL;
3081 +       h_mtx = NULL;
3082 +       bindex = au_br_index(sb, br->br_id);
3083 +       if (0 <= bindex) {
3084 +               hdir = au_hi(sb->s_root->d_inode, bindex);
3085 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
3086 +       } else {
3087 +               h_mtx = &au_br_dentry(br)->d_inode->i_mutex;
3088 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
3089 +       }
3090 +       if (!wbr)
3091 +               err = au_wh_init(br, sb);
3092 +       else {
3093 +               wbr_wh_write_lock(wbr);
3094 +               err = au_wh_init(br, sb);
3095 +               wbr_wh_write_unlock(wbr);
3096 +       }
3097 +       if (hdir)
3098 +               au_hn_imtx_unlock(hdir);
3099 +       else
3100 +               mutex_unlock(h_mtx);
3101 +       vfsub_mnt_drop_write(au_br_mnt(br));
3102 +       br->br_perm = old_perm;
3103 +
3104 +       if (!err && wbr && !au_br_writable(new_perm)) {
3105 +               kfree(wbr);
3106 +               br->br_wbr = NULL;
3107 +       }
3108 +
3109 +out:
3110 +       return err;
3111 +}
3112 +
3113 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3114 +                      int perm)
3115 +{
3116 +       int err;
3117 +       struct kstatfs kst;
3118 +       struct au_wbr *wbr;
3119 +
3120 +       wbr = br->br_wbr;
3121 +       au_rw_init(&wbr->wbr_wh_rwsem);
3122 +       memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
3123 +       atomic_set(&wbr->wbr_wh_running, 0);
3124 +       wbr->wbr_bytes = 0;
3125 +
3126 +       /*
3127 +        * a limit for rmdir/rename a dir
3128 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3129 +        */
3130 +       err = vfs_statfs(&br->br_path, &kst);
3131 +       if (unlikely(err))
3132 +               goto out;
3133 +       err = -EINVAL;
3134 +       if (kst.f_namelen >= NAME_MAX)
3135 +               err = au_br_init_wh(sb, br, perm);
3136 +       else
3137 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3138 +                      au_br_dentry(br),
3139 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3140 +
3141 +out:
3142 +       return err;
3143 +}
3144 +
3145 +/* initialize a new branch */
3146 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3147 +                     struct au_opt_add *add)
3148 +{
3149 +       int err;
3150 +
3151 +       err = 0;
3152 +       memset(&br->br_xino, 0, sizeof(br->br_xino));
3153 +       mutex_init(&br->br_xino.xi_nondir_mtx);
3154 +       br->br_perm = add->perm;
3155 +       br->br_path = add->path; /* set first, path_get() later */
3156 +       spin_lock_init(&br->br_dykey_lock);
3157 +       memset(br->br_dykey, 0, sizeof(br->br_dykey));
3158 +       atomic_set(&br->br_count, 0);
3159 +       atomic_set(&br->br_xino_running, 0);
3160 +       br->br_id = au_new_br_id(sb);
3161 +       AuDebugOn(br->br_id < 0);
3162 +
3163 +       if (au_br_writable(add->perm)) {
3164 +               err = au_wbr_init(br, sb, add->perm);
3165 +               if (unlikely(err))
3166 +                       goto out_err;
3167 +       }
3168 +
3169 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3170 +               err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
3171 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3172 +               if (unlikely(err)) {
3173 +                       AuDebugOn(br->br_xino.xi_file);
3174 +                       goto out_err;
3175 +               }
3176 +       }
3177 +
3178 +       sysaufs_br_init(br);
3179 +       path_get(&br->br_path);
3180 +       goto out; /* success */
3181 +
3182 +out_err:
3183 +       memset(&br->br_path, 0, sizeof(br->br_path));
3184 +out:
3185 +       return err;
3186 +}
3187 +
3188 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3189 +                            struct au_branch *br, aufs_bindex_t bend,
3190 +                            aufs_bindex_t amount)
3191 +{
3192 +       struct au_branch **brp;
3193 +
3194 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3195 +
3196 +       brp = sbinfo->si_branch + bindex;
3197 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3198 +       *brp = br;
3199 +       sbinfo->si_bend++;
3200 +       if (unlikely(bend < 0))
3201 +               sbinfo->si_bend = 0;
3202 +}
3203 +
3204 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3205 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3206 +{
3207 +       struct au_hdentry *hdp;
3208 +
3209 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3210 +
3211 +       hdp = dinfo->di_hdentry + bindex;
3212 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3213 +       au_h_dentry_init(hdp);
3214 +       dinfo->di_bend++;
3215 +       if (unlikely(bend < 0))
3216 +               dinfo->di_bstart = 0;
3217 +}
3218 +
3219 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3220 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3221 +{
3222 +       struct au_hinode *hip;
3223 +
3224 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3225 +
3226 +       hip = iinfo->ii_hinode + bindex;
3227 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3228 +       hip->hi_inode = NULL;
3229 +       au_hn_init(hip);
3230 +       iinfo->ii_bend++;
3231 +       if (unlikely(bend < 0))
3232 +               iinfo->ii_bstart = 0;
3233 +}
3234 +
3235 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3236 +                        aufs_bindex_t bindex)
3237 +{
3238 +       struct dentry *root, *h_dentry;
3239 +       struct inode *root_inode;
3240 +       aufs_bindex_t bend, amount;
3241 +
3242 +       root = sb->s_root;
3243 +       root_inode = root->d_inode;
3244 +       bend = au_sbend(sb);
3245 +       amount = bend + 1 - bindex;
3246 +       h_dentry = au_br_dentry(br);
3247 +       au_sbilist_lock();
3248 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
3249 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
3250 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
3251 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3252 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
3253 +                     /*flags*/0);
3254 +       au_sbilist_unlock();
3255 +}
3256 +
3257 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3258 +{
3259 +       int err;
3260 +       aufs_bindex_t bend, add_bindex;
3261 +       struct dentry *root, *h_dentry;
3262 +       struct inode *root_inode;
3263 +       struct au_branch *add_branch;
3264 +
3265 +       root = sb->s_root;
3266 +       root_inode = root->d_inode;
3267 +       IMustLock(root_inode);
3268 +       err = test_add(sb, add, remount);
3269 +       if (unlikely(err < 0))
3270 +               goto out;
3271 +       if (err) {
3272 +               err = 0;
3273 +               goto out; /* success */
3274 +       }
3275 +
3276 +       bend = au_sbend(sb);
3277 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
3278 +       err = PTR_ERR(add_branch);
3279 +       if (IS_ERR(add_branch))
3280 +               goto out;
3281 +
3282 +       err = au_br_init(add_branch, sb, add);
3283 +       if (unlikely(err)) {
3284 +               au_br_do_free(add_branch);
3285 +               goto out;
3286 +       }
3287 +
3288 +       add_bindex = add->bindex;
3289 +       if (!remount)
3290 +               au_br_do_add(sb, add_branch, add_bindex);
3291 +       else {
3292 +               sysaufs_brs_del(sb, add_bindex);
3293 +               au_br_do_add(sb, add_branch, add_bindex);
3294 +               sysaufs_brs_add(sb, add_bindex);
3295 +       }
3296 +
3297 +       h_dentry = add->path.dentry;
3298 +       if (!add_bindex) {
3299 +               au_cpup_attr_all(root_inode, /*force*/1);
3300 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3301 +       } else
3302 +               au_add_nlink(root_inode, h_dentry->d_inode);
3303 +
3304 +       /*
3305 +        * this test/set prevents aufs from handling unnecesary notify events
3306 +        * of xino files, in case of re-adding a writable branch which was
3307 +        * once detached from aufs.
3308 +        */
3309 +       if (au_xino_brid(sb) < 0
3310 +           && au_br_writable(add_branch->br_perm)
3311 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
3312 +           && add_branch->br_xino.xi_file
3313 +           && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
3314 +               au_xino_brid_set(sb, add_branch->br_id);
3315 +
3316 +out:
3317 +       return err;
3318 +}
3319 +
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 +/* ---------------------------------------------------------------------- */
3369 +
3370 +/*
3371 + * delete a branch
3372 + */
3373 +
3374 +/* to show the line number, do not make it inlined function */
3375 +#define AuVerbose(do_info, fmt, ...) do { \
3376 +       if (do_info) \
3377 +               pr_info(fmt, ##__VA_ARGS__); \
3378 +} while (0)
3379 +
3380 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
3381 +                        aufs_bindex_t bend)
3382 +{
3383 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
3384 +}
3385 +
3386 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
3387 +                        aufs_bindex_t bend)
3388 +{
3389 +       return au_test_ibusy(dentry->d_inode, bstart, bend);
3390 +}
3391 +
3392 +/*
3393 + * test if the branch is deletable or not.
3394 + */
3395 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3396 +                           unsigned int sigen, const unsigned int verbose)
3397 +{
3398 +       int err, i, j, ndentry;
3399 +       aufs_bindex_t bstart, bend;
3400 +       struct au_dcsub_pages dpages;
3401 +       struct au_dpage *dpage;
3402 +       struct dentry *d;
3403 +
3404 +       err = au_dpages_init(&dpages, GFP_NOFS);
3405 +       if (unlikely(err))
3406 +               goto out;
3407 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3408 +       if (unlikely(err))
3409 +               goto out_dpages;
3410 +
3411 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3412 +               dpage = dpages.dpages + i;
3413 +               ndentry = dpage->ndentry;
3414 +               for (j = 0; !err && j < ndentry; j++) {
3415 +                       d = dpage->dentries[j];
3416 +                       AuDebugOn(au_dcount(d) <= 0);
3417 +                       if (!au_digen_test(d, sigen)) {
3418 +                               di_read_lock_child(d, AuLock_IR);
3419 +                               if (unlikely(au_dbrange_test(d))) {
3420 +                                       di_read_unlock(d, AuLock_IR);
3421 +                                       continue;
3422 +                               }
3423 +                       } else {
3424 +                               di_write_lock_child(d);
3425 +                               if (unlikely(au_dbrange_test(d))) {
3426 +                                       di_write_unlock(d);
3427 +                                       continue;
3428 +                               }
3429 +                               err = au_reval_dpath(d, sigen);
3430 +                               if (!err)
3431 +                                       di_downgrade_lock(d, AuLock_IR);
3432 +                               else {
3433 +                                       di_write_unlock(d);
3434 +                                       break;
3435 +                               }
3436 +                       }
3437 +
3438 +                       /* AuDbgDentry(d); */
3439 +                       bstart = au_dbstart(d);
3440 +                       bend = au_dbend(d);
3441 +                       if (bstart <= bindex
3442 +                           && bindex <= bend
3443 +                           && au_h_dptr(d, bindex)
3444 +                           && au_test_dbusy(d, bstart, bend)) {
3445 +                               err = -EBUSY;
3446 +                               AuVerbose(verbose, "busy %pd\n", d);
3447 +                               AuDbgDentry(d);
3448 +                       }
3449 +                       di_read_unlock(d, AuLock_IR);
3450 +               }
3451 +       }
3452 +
3453 +out_dpages:
3454 +       au_dpages_free(&dpages);
3455 +out:
3456 +       return err;
3457 +}
3458 +
3459 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3460 +                          unsigned int sigen, const unsigned int verbose)
3461 +{
3462 +       int err;
3463 +       unsigned long long max, ull;
3464 +       struct inode *i, **array;
3465 +       aufs_bindex_t bstart, bend;
3466 +
3467 +       array = au_iarray_alloc(sb, &max);
3468 +       err = PTR_ERR(array);
3469 +       if (IS_ERR(array))
3470 +               goto out;
3471 +
3472 +       err = 0;
3473 +       AuDbg("b%d\n", bindex);
3474 +       for (ull = 0; !err && ull < max; ull++) {
3475 +               i = array[ull];
3476 +               if (unlikely(!i))
3477 +                       break;
3478 +               if (i->i_ino == AUFS_ROOT_INO)
3479 +                       continue;
3480 +
3481 +               /* AuDbgInode(i); */
3482 +               if (au_iigen(i, NULL) == sigen)
3483 +                       ii_read_lock_child(i);
3484 +               else {
3485 +                       ii_write_lock_child(i);
3486 +                       err = au_refresh_hinode_self(i);
3487 +                       au_iigen_dec(i);
3488 +                       if (!err)
3489 +                               ii_downgrade_lock(i);
3490 +                       else {
3491 +                               ii_write_unlock(i);
3492 +                               break;
3493 +                       }
3494 +               }
3495 +
3496 +               bstart = au_ibstart(i);
3497 +               bend = au_ibend(i);
3498 +               if (bstart <= bindex
3499 +                   && bindex <= bend
3500 +                   && au_h_iptr(i, bindex)
3501 +                   && au_test_ibusy(i, bstart, bend)) {
3502 +                       err = -EBUSY;
3503 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3504 +                       AuDbgInode(i);
3505 +               }
3506 +               ii_read_unlock(i);
3507 +       }
3508 +       au_iarray_free(array, max);
3509 +
3510 +out:
3511 +       return err;
3512 +}
3513 +
3514 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3515 +                             const unsigned int verbose)
3516 +{
3517 +       int err;
3518 +       unsigned int sigen;
3519 +
3520 +       sigen = au_sigen(root->d_sb);
3521 +       DiMustNoWaiters(root);
3522 +       IiMustNoWaiters(root->d_inode);
3523 +       di_write_unlock(root);
3524 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3525 +       if (!err)
3526 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3527 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3528 +
3529 +       return err;
3530 +}
3531 +
3532 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3533 +                        struct file **to_free, int *idx)
3534 +{
3535 +       int err;
3536 +       unsigned char matched, root;
3537 +       aufs_bindex_t bindex, bend;
3538 +       struct au_fidir *fidir;
3539 +       struct au_hfile *hfile;
3540 +
3541 +       err = 0;
3542 +       root = IS_ROOT(file->f_path.dentry);
3543 +       if (root) {
3544 +               get_file(file);
3545 +               to_free[*idx] = file;
3546 +               (*idx)++;
3547 +               goto out;
3548 +       }
3549 +
3550 +       matched = 0;
3551 +       fidir = au_fi(file)->fi_hdir;
3552 +       AuDebugOn(!fidir);
3553 +       bend = au_fbend_dir(file);
3554 +       for (bindex = au_fbstart(file); bindex <= bend; bindex++) {
3555 +               hfile = fidir->fd_hfile + bindex;
3556 +               if (!hfile->hf_file)
3557 +                       continue;
3558 +
3559 +               if (hfile->hf_br->br_id == br_id) {
3560 +                       matched = 1;
3561 +                       break;
3562 +               }
3563 +       }
3564 +       if (matched)
3565 +               err = -EBUSY;
3566 +
3567 +out:
3568 +       return err;
3569 +}
3570 +
3571 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3572 +                         struct file **to_free, int opened)
3573 +{
3574 +       int err, idx;
3575 +       unsigned long long ull, max;
3576 +       aufs_bindex_t bstart;
3577 +       struct file *file, **array;
3578 +       struct dentry *root;
3579 +       struct au_hfile *hfile;
3580 +
3581 +       array = au_farray_alloc(sb, &max);
3582 +       err = PTR_ERR(array);
3583 +       if (IS_ERR(array))
3584 +               goto out;
3585 +
3586 +       err = 0;
3587 +       idx = 0;
3588 +       root = sb->s_root;
3589 +       di_write_unlock(root);
3590 +       for (ull = 0; ull < max; ull++) {
3591 +               file = array[ull];
3592 +               if (unlikely(!file))
3593 +                       break;
3594 +
3595 +               /* AuDbg("%pD\n", file); */
3596 +               fi_read_lock(file);
3597 +               bstart = au_fbstart(file);
3598 +               if (!d_is_dir(file->f_path.dentry)) {
3599 +                       hfile = &au_fi(file)->fi_htop;
3600 +                       if (hfile->hf_br->br_id == br_id)
3601 +                               err = -EBUSY;
3602 +               } else
3603 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3604 +               fi_read_unlock(file);
3605 +               if (unlikely(err))
3606 +                       break;
3607 +       }
3608 +       di_write_lock_child(root);
3609 +       au_farray_free(array, max);
3610 +       AuDebugOn(idx > opened);
3611 +
3612 +out:
3613 +       return err;
3614 +}
3615 +
3616 +static void br_del_file(struct file **to_free, unsigned long long opened,
3617 +                         aufs_bindex_t br_id)
3618 +{
3619 +       unsigned long long ull;
3620 +       aufs_bindex_t bindex, bstart, bend, bfound;
3621 +       struct file *file;
3622 +       struct au_fidir *fidir;
3623 +       struct au_hfile *hfile;
3624 +
3625 +       for (ull = 0; ull < opened; ull++) {
3626 +               file = to_free[ull];
3627 +               if (unlikely(!file))
3628 +                       break;
3629 +
3630 +               /* AuDbg("%pD\n", file); */
3631 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3632 +               bfound = -1;
3633 +               fidir = au_fi(file)->fi_hdir;
3634 +               AuDebugOn(!fidir);
3635 +               fi_write_lock(file);
3636 +               bstart = au_fbstart(file);
3637 +               bend = au_fbend_dir(file);
3638 +               for (bindex = bstart; bindex <= bend; bindex++) {
3639 +                       hfile = fidir->fd_hfile + bindex;
3640 +                       if (!hfile->hf_file)
3641 +                               continue;
3642 +
3643 +                       if (hfile->hf_br->br_id == br_id) {
3644 +                               bfound = bindex;
3645 +                               break;
3646 +                       }
3647 +               }
3648 +               AuDebugOn(bfound < 0);
3649 +               au_set_h_fptr(file, bfound, NULL);
3650 +               if (bfound == bstart) {
3651 +                       for (bstart++; bstart <= bend; bstart++)
3652 +                               if (au_hf_dir(file, bstart)) {
3653 +                                       au_set_fbstart(file, bstart);
3654 +                                       break;
3655 +                               }
3656 +               }
3657 +               fi_write_unlock(file);
3658 +       }
3659 +}
3660 +
3661 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3662 +                            const aufs_bindex_t bindex,
3663 +                            const aufs_bindex_t bend)
3664 +{
3665 +       struct au_branch **brp, **p;
3666 +
3667 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3668 +
3669 +       brp = sbinfo->si_branch + bindex;
3670 +       if (bindex < bend)
3671 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
3672 +       sbinfo->si_branch[0 + bend] = NULL;
3673 +       sbinfo->si_bend--;
3674 +
3675 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
3676 +       if (p)
3677 +               sbinfo->si_branch = p;
3678 +       /* harmless error */
3679 +}
3680 +
3681 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3682 +                            const aufs_bindex_t bend)
3683 +{
3684 +       struct au_hdentry *hdp, *p;
3685 +
3686 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3687 +
3688 +       hdp = dinfo->di_hdentry;
3689 +       if (bindex < bend)
3690 +               memmove(hdp + bindex, hdp + bindex + 1,
3691 +                       sizeof(*hdp) * (bend - bindex));
3692 +       hdp[0 + bend].hd_dentry = NULL;
3693 +       dinfo->di_bend--;
3694 +
3695 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
3696 +       if (p)
3697 +               dinfo->di_hdentry = p;
3698 +       /* harmless error */
3699 +}
3700 +
3701 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3702 +                            const aufs_bindex_t bend)
3703 +{
3704 +       struct au_hinode *hip, *p;
3705 +
3706 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3707 +
3708 +       hip = iinfo->ii_hinode + bindex;
3709 +       if (bindex < bend)
3710 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
3711 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
3712 +       au_hn_init(iinfo->ii_hinode + bend);
3713 +       iinfo->ii_bend--;
3714 +
3715 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
3716 +       if (p)
3717 +               iinfo->ii_hinode = p;
3718 +       /* harmless error */
3719 +}
3720 +
3721 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3722 +                        struct au_branch *br)
3723 +{
3724 +       aufs_bindex_t bend;
3725 +       struct au_sbinfo *sbinfo;
3726 +       struct dentry *root, *h_root;
3727 +       struct inode *inode, *h_inode;
3728 +       struct au_hinode *hinode;
3729 +
3730 +       SiMustWriteLock(sb);
3731 +
3732 +       root = sb->s_root;
3733 +       inode = root->d_inode;
3734 +       sbinfo = au_sbi(sb);
3735 +       bend = sbinfo->si_bend;
3736 +
3737 +       h_root = au_h_dptr(root, bindex);
3738 +       hinode = au_hi(inode, bindex);
3739 +       h_inode = au_igrab(hinode->hi_inode);
3740 +       au_hiput(hinode);
3741 +
3742 +       au_sbilist_lock();
3743 +       au_br_do_del_brp(sbinfo, bindex, bend);
3744 +       au_br_do_del_hdp(au_di(root), bindex, bend);
3745 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
3746 +       au_sbilist_unlock();
3747 +
3748 +       dput(h_root);
3749 +       iput(h_inode);
3750 +       au_br_do_free(br);
3751 +}
3752 +
3753 +static unsigned long long empty_cb(void *array, unsigned long long max,
3754 +                                  void *arg)
3755 +{
3756 +       return max;
3757 +}
3758 +
3759 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3760 +{
3761 +       int err, rerr, i;
3762 +       unsigned long long opened;
3763 +       unsigned int mnt_flags;
3764 +       aufs_bindex_t bindex, bend, br_id;
3765 +       unsigned char do_wh, verbose;
3766 +       struct au_branch *br;
3767 +       struct au_wbr *wbr;
3768 +       struct dentry *root;
3769 +       struct file **to_free;
3770 +
3771 +       err = 0;
3772 +       opened = 0;
3773 +       to_free = NULL;
3774 +       root = sb->s_root;
3775 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3776 +       if (bindex < 0) {
3777 +               if (remount)
3778 +                       goto out; /* success */
3779 +               err = -ENOENT;
3780 +               pr_err("%s no such branch\n", del->pathname);
3781 +               goto out;
3782 +       }
3783 +       AuDbg("bindex b%d\n", bindex);
3784 +
3785 +       err = -EBUSY;
3786 +       mnt_flags = au_mntflags(sb);
3787 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3788 +       bend = au_sbend(sb);
3789 +       if (unlikely(!bend)) {
3790 +               AuVerbose(verbose, "no more branches left\n");
3791 +               goto out;
3792 +       }
3793 +       br = au_sbr(sb, bindex);
3794 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3795 +
3796 +       br_id = br->br_id;
3797 +       opened = atomic_read(&br->br_count);
3798 +       if (unlikely(opened)) {
3799 +               to_free = au_array_alloc(&opened, empty_cb, NULL);
3800 +               err = PTR_ERR(to_free);
3801 +               if (IS_ERR(to_free))
3802 +                       goto out;
3803 +
3804 +               err = test_file_busy(sb, br_id, to_free, opened);
3805 +               if (unlikely(err)) {
3806 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3807 +                       goto out;
3808 +               }
3809 +       }
3810 +
3811 +       wbr = br->br_wbr;
3812 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3813 +       if (do_wh) {
3814 +               /* instead of WbrWhMustWriteLock(wbr) */
3815 +               SiMustWriteLock(sb);
3816 +               for (i = 0; i < AuBrWh_Last; i++) {
3817 +                       dput(wbr->wbr_wh[i]);
3818 +                       wbr->wbr_wh[i] = NULL;
3819 +               }
3820 +       }
3821 +
3822 +       err = test_children_busy(root, bindex, verbose);
3823 +       if (unlikely(err)) {
3824 +               if (do_wh)
3825 +                       goto out_wh;
3826 +               goto out;
3827 +       }
3828 +
3829 +       err = 0;
3830 +       if (to_free) {
3831 +               /*
3832 +                * now we confirmed the branch is deletable.
3833 +                * let's free the remaining opened dirs on the branch.
3834 +                */
3835 +               di_write_unlock(root);
3836 +               br_del_file(to_free, opened, br_id);
3837 +               di_write_lock_child(root);
3838 +       }
3839 +
3840 +       if (!remount)
3841 +               au_br_do_del(sb, bindex, br);
3842 +       else {
3843 +               sysaufs_brs_del(sb, bindex);
3844 +               au_br_do_del(sb, bindex, br);
3845 +               sysaufs_brs_add(sb, bindex);
3846 +       }
3847 +
3848 +       if (!bindex) {
3849 +               au_cpup_attr_all(root->d_inode, /*force*/1);
3850 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3851 +       } else
3852 +               au_sub_nlink(root->d_inode, del->h_path.dentry->d_inode);
3853 +       if (au_opt_test(mnt_flags, PLINK))
3854 +               au_plink_half_refresh(sb, br_id);
3855 +
3856 +       if (au_xino_brid(sb) == br_id)
3857 +               au_xino_brid_set(sb, -1);
3858 +       goto out; /* success */
3859 +
3860 +out_wh:
3861 +       /* revert */
3862 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3863 +       if (rerr)
3864 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3865 +                       del->pathname, rerr);
3866 +out:
3867 +       if (to_free)
3868 +               au_farray_free(to_free, opened);
3869 +       return err;
3870 +}
3871 +
3872 +/* ---------------------------------------------------------------------- */
3873 +
3874 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3875 +{
3876 +       int err;
3877 +       aufs_bindex_t bstart, bend;
3878 +       struct aufs_ibusy ibusy;
3879 +       struct inode *inode, *h_inode;
3880 +
3881 +       err = -EPERM;
3882 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
3883 +               goto out;
3884 +
3885 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
3886 +       if (!err)
3887 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
3888 +       if (unlikely(err)) {
3889 +               err = -EFAULT;
3890 +               AuTraceErr(err);
3891 +               goto out;
3892 +       }
3893 +
3894 +       err = -EINVAL;
3895 +       si_read_lock(sb, AuLock_FLUSH);
3896 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
3897 +               goto out_unlock;
3898 +
3899 +       err = 0;
3900 +       ibusy.h_ino = 0; /* invalid */
3901 +       inode = ilookup(sb, ibusy.ino);
3902 +       if (!inode
3903 +           || inode->i_ino == AUFS_ROOT_INO
3904 +           || is_bad_inode(inode))
3905 +               goto out_unlock;
3906 +
3907 +       ii_read_lock_child(inode);
3908 +       bstart = au_ibstart(inode);
3909 +       bend = au_ibend(inode);
3910 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
3911 +               h_inode = au_h_iptr(inode, ibusy.bindex);
3912 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
3913 +                       ibusy.h_ino = h_inode->i_ino;
3914 +       }
3915 +       ii_read_unlock(inode);
3916 +       iput(inode);
3917 +
3918 +out_unlock:
3919 +       si_read_unlock(sb);
3920 +       if (!err) {
3921 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
3922 +               if (unlikely(err)) {
3923 +                       err = -EFAULT;
3924 +                       AuTraceErr(err);
3925 +               }
3926 +       }
3927 +out:
3928 +       return err;
3929 +}
3930 +
3931 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
3932 +{
3933 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
3934 +}
3935 +
3936 +#ifdef CONFIG_COMPAT
3937 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
3938 +{
3939 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
3940 +}
3941 +#endif
3942 +
3943 +/* ---------------------------------------------------------------------- */
3944 +
3945 +/*
3946 + * change a branch permission
3947 + */
3948 +
3949 +static void au_warn_ima(void)
3950 +{
3951 +#ifdef CONFIG_IMA
3952 +       /* since it doesn't support mark_files_ro() */
3953 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
3954 +#endif
3955 +}
3956 +
3957 +static int do_need_sigen_inc(int a, int b)
3958 +{
3959 +       return au_br_whable(a) && !au_br_whable(b);
3960 +}
3961 +
3962 +static int need_sigen_inc(int old, int new)
3963 +{
3964 +       return do_need_sigen_inc(old, new)
3965 +               || do_need_sigen_inc(new, old);
3966 +}
3967 +
3968 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
3969 +{
3970 +       int err, do_warn;
3971 +       unsigned int mnt_flags;
3972 +       unsigned long long ull, max;
3973 +       aufs_bindex_t br_id;
3974 +       unsigned char verbose, writer;
3975 +       struct file *file, *hf, **array;
3976 +       struct au_hfile *hfile;
3977 +
3978 +       mnt_flags = au_mntflags(sb);
3979 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3980 +
3981 +       array = au_farray_alloc(sb, &max);
3982 +       err = PTR_ERR(array);
3983 +       if (IS_ERR(array))
3984 +               goto out;
3985 +
3986 +       do_warn = 0;
3987 +       br_id = au_sbr_id(sb, bindex);
3988 +       for (ull = 0; ull < max; ull++) {
3989 +               file = array[ull];
3990 +               if (unlikely(!file))
3991 +                       break;
3992 +
3993 +               /* AuDbg("%pD\n", file); */
3994 +               fi_read_lock(file);
3995 +               if (unlikely(au_test_mmapped(file))) {
3996 +                       err = -EBUSY;
3997 +                       AuVerbose(verbose, "mmapped %pD\n", file);
3998 +                       AuDbgFile(file);
3999 +                       FiMustNoWaiters(file);
4000 +                       fi_read_unlock(file);
4001 +                       goto out_array;
4002 +               }
4003 +
4004 +               hfile = &au_fi(file)->fi_htop;
4005 +               hf = hfile->hf_file;
4006 +               if (!d_is_reg(file->f_path.dentry)
4007 +                   || !(file->f_mode & FMODE_WRITE)
4008 +                   || hfile->hf_br->br_id != br_id
4009 +                   || !(hf->f_mode & FMODE_WRITE))
4010 +                       array[ull] = NULL;
4011 +               else {
4012 +                       do_warn = 1;
4013 +                       get_file(file);
4014 +               }
4015 +
4016 +               FiMustNoWaiters(file);
4017 +               fi_read_unlock(file);
4018 +               fput(file);
4019 +       }
4020 +
4021 +       err = 0;
4022 +       if (do_warn)
4023 +               au_warn_ima();
4024 +
4025 +       for (ull = 0; ull < max; ull++) {
4026 +               file = array[ull];
4027 +               if (!file)
4028 +                       continue;
4029 +
4030 +               /* todo: already flushed? */
4031 +               /*
4032 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4033 +                * approach which resets f_mode and calls mnt_drop_write() and
4034 +                * file_release_write() for each file, because the branch
4035 +                * attribute in aufs world is totally different from the native
4036 +                * fs rw/ro mode.
4037 +               */
4038 +               /* fi_read_lock(file); */
4039 +               hfile = &au_fi(file)->fi_htop;
4040 +               hf = hfile->hf_file;
4041 +               /* fi_read_unlock(file); */
4042 +               spin_lock(&hf->f_lock);
4043 +               writer = !!(hf->f_mode & FMODE_WRITER);
4044 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4045 +               spin_unlock(&hf->f_lock);
4046 +               if (writer) {
4047 +                       put_write_access(file_inode(hf));
4048 +                       __mnt_drop_write(hf->f_path.mnt);
4049 +               }
4050 +       }
4051 +
4052 +out_array:
4053 +       au_farray_free(array, max);
4054 +out:
4055 +       AuTraceErr(err);
4056 +       return err;
4057 +}
4058 +
4059 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4060 +             int *do_refresh)
4061 +{
4062 +       int err, rerr;
4063 +       aufs_bindex_t bindex;
4064 +       struct dentry *root;
4065 +       struct au_branch *br;
4066 +       struct au_br_fhsm *bf;
4067 +
4068 +       root = sb->s_root;
4069 +       bindex = au_find_dbindex(root, mod->h_root);
4070 +       if (bindex < 0) {
4071 +               if (remount)
4072 +                       return 0; /* success */
4073 +               err = -ENOENT;
4074 +               pr_err("%s no such branch\n", mod->path);
4075 +               goto out;
4076 +       }
4077 +       AuDbg("bindex b%d\n", bindex);
4078 +
4079 +       err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
4080 +       if (unlikely(err))
4081 +               goto out;
4082 +
4083 +       br = au_sbr(sb, bindex);
4084 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4085 +       if (br->br_perm == mod->perm)
4086 +               return 0; /* success */
4087 +
4088 +       /* pre-allocate for non-fhsm --> fhsm */
4089 +       bf = NULL;
4090 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4091 +               err = au_fhsm_br_alloc(br);
4092 +               if (unlikely(err))
4093 +                       goto out;
4094 +               bf = br->br_fhsm;
4095 +               br->br_fhsm = NULL;
4096 +       }
4097 +
4098 +       if (au_br_writable(br->br_perm)) {
4099 +               /* remove whiteout base */
4100 +               err = au_br_init_wh(sb, br, mod->perm);
4101 +               if (unlikely(err))
4102 +                       goto out_bf;
4103 +
4104 +               if (!au_br_writable(mod->perm)) {
4105 +                       /* rw --> ro, file might be mmapped */
4106 +                       DiMustNoWaiters(root);
4107 +                       IiMustNoWaiters(root->d_inode);
4108 +                       di_write_unlock(root);
4109 +                       err = au_br_mod_files_ro(sb, bindex);
4110 +                       /* aufs_write_lock() calls ..._child() */
4111 +                       di_write_lock_child(root);
4112 +
4113 +                       if (unlikely(err)) {
4114 +                               rerr = -ENOMEM;
4115 +                               br->br_wbr = kmalloc(sizeof(*br->br_wbr),
4116 +                                                    GFP_NOFS);
4117 +                               if (br->br_wbr)
4118 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4119 +                               if (unlikely(rerr)) {
4120 +                                       AuIOErr("nested error %d (%d)\n",
4121 +                                               rerr, err);
4122 +                                       br->br_perm = mod->perm;
4123 +                               }
4124 +                       }
4125 +               }
4126 +       } else if (au_br_writable(mod->perm)) {
4127 +               /* ro --> rw */
4128 +               err = -ENOMEM;
4129 +               br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
4130 +               if (br->br_wbr) {
4131 +                       err = au_wbr_init(br, sb, mod->perm);
4132 +                       if (unlikely(err)) {
4133 +                               kfree(br->br_wbr);
4134 +                               br->br_wbr = NULL;
4135 +                       }
4136 +               }
4137 +       }
4138 +       if (unlikely(err))
4139 +               goto out_bf;
4140 +
4141 +       if (au_br_fhsm(br->br_perm)) {
4142 +               if (!au_br_fhsm(mod->perm)) {
4143 +                       /* fhsm --> non-fhsm */
4144 +                       au_br_fhsm_fin(br->br_fhsm);
4145 +                       kfree(br->br_fhsm);
4146 +                       br->br_fhsm = NULL;
4147 +               }
4148 +       } else if (au_br_fhsm(mod->perm))
4149 +               /* non-fhsm --> fhsm */
4150 +               br->br_fhsm = bf;
4151 +
4152 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4153 +       br->br_perm = mod->perm;
4154 +       goto out; /* success */
4155 +
4156 +out_bf:
4157 +       kfree(bf);
4158 +out:
4159 +       AuTraceErr(err);
4160 +       return err;
4161 +}
4162 +
4163 +/* ---------------------------------------------------------------------- */
4164 +
4165 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4166 +{
4167 +       int err;
4168 +       struct kstatfs kstfs;
4169 +
4170 +       err = vfs_statfs(&br->br_path, &kstfs);
4171 +       if (!err) {
4172 +               stfs->f_blocks = kstfs.f_blocks;
4173 +               stfs->f_bavail = kstfs.f_bavail;
4174 +               stfs->f_files = kstfs.f_files;
4175 +               stfs->f_ffree = kstfs.f_ffree;
4176 +       }
4177 +
4178 +       return err;
4179 +}
4180 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4181 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4182 +++ linux/fs/aufs/branch.h      2015-06-22 08:29:23.703424266 +0200
4183 @@ -0,0 +1,279 @@
4184 +/*
4185 + * Copyright (C) 2005-2015 Junjiro R. Okajima
4186 + *
4187 + * This program, aufs is free software; you can redistribute it and/or modify
4188 + * it under the terms of the GNU General Public License as published by
4189 + * the Free Software Foundation; either version 2 of the License, or
4190 + * (at your option) any later version.
4191 + *
4192 + * This program is distributed in the hope that it will be useful,
4193 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4194 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4195 + * GNU General Public License for more details.
4196 + *
4197 + * You should have received a copy of the GNU General Public License
4198 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4199 + */
4200 +
4201 +/*
4202 + * branch filesystems and xino for them
4203 + */
4204 +
4205 +#ifndef __AUFS_BRANCH_H__
4206 +#define __AUFS_BRANCH_H__
4207 +
4208 +#ifdef __KERNEL__
4209 +
4210 +#include <linux/mount.h>
4211 +#include "dynop.h"
4212 +#include "rwsem.h"
4213 +#include "super.h"
4214 +
4215 +/* ---------------------------------------------------------------------- */
4216 +
4217 +/* a xino file */
4218 +struct au_xino_file {
4219 +       struct file             *xi_file;
4220 +       struct mutex            xi_nondir_mtx;
4221 +
4222 +       /* todo: make xino files an array to support huge inode number */
4223 +
4224 +#ifdef CONFIG_DEBUG_FS
4225 +       struct dentry            *xi_dbgaufs;
4226 +#endif
4227 +};
4228 +
4229 +/* File-based Hierarchical Storage Management */
4230 +struct au_br_fhsm {
4231 +#ifdef CONFIG_AUFS_FHSM
4232 +       struct mutex            bf_lock;
4233 +       unsigned long           bf_jiffy;
4234 +       struct aufs_stfs        bf_stfs;
4235 +       int                     bf_readable;
4236 +#endif
4237 +};
4238 +
4239 +/* members for writable branch only */
4240 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4241 +struct au_wbr {
4242 +       struct au_rwsem         wbr_wh_rwsem;
4243 +       struct dentry           *wbr_wh[AuBrWh_Last];
4244 +       atomic_t                wbr_wh_running;
4245 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4246 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4247 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4248 +
4249 +       /* mfs mode */
4250 +       unsigned long long      wbr_bytes;
4251 +};
4252 +
4253 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4254 +#define AuBrDynOp (AuDyLast * 4)
4255 +
4256 +#ifdef CONFIG_AUFS_HFSNOTIFY
4257 +/* support for asynchronous destruction */
4258 +struct au_br_hfsnotify {
4259 +       struct fsnotify_group   *hfsn_group;
4260 +};
4261 +#endif
4262 +
4263 +/* sysfs entries */
4264 +struct au_brsysfs {
4265 +       char                    name[16];
4266 +       struct attribute        attr;
4267 +};
4268 +
4269 +enum {
4270 +       AuBrSysfs_BR,
4271 +       AuBrSysfs_BRID,
4272 +       AuBrSysfs_Last
4273 +};
4274 +
4275 +/* protected by superblock rwsem */
4276 +struct au_branch {
4277 +       struct au_xino_file     br_xino;
4278 +
4279 +       aufs_bindex_t           br_id;
4280 +
4281 +       int                     br_perm;
4282 +       struct path             br_path;
4283 +       spinlock_t              br_dykey_lock;
4284 +       struct au_dykey         *br_dykey[AuBrDynOp];
4285 +       atomic_t                br_count;
4286 +
4287 +       struct au_wbr           *br_wbr;
4288 +       struct au_br_fhsm       *br_fhsm;
4289 +
4290 +       /* xino truncation */
4291 +       atomic_t                br_xino_running;
4292 +
4293 +#ifdef CONFIG_AUFS_HFSNOTIFY
4294 +       struct au_br_hfsnotify  *br_hfsn;
4295 +#endif
4296 +
4297 +#ifdef CONFIG_SYSFS
4298 +       /* entries under sysfs per mount-point */
4299 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4300 +#endif
4301 +};
4302 +
4303 +/* ---------------------------------------------------------------------- */
4304 +
4305 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4306 +{
4307 +       return br->br_path.mnt;
4308 +}
4309 +
4310 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4311 +{
4312 +       return br->br_path.dentry;
4313 +}
4314 +
4315 +static inline struct super_block *au_br_sb(struct au_branch *br)
4316 +{
4317 +       return au_br_mnt(br)->mnt_sb;
4318 +}
4319 +
4320 +static inline int au_br_rdonly(struct au_branch *br)
4321 +{
4322 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
4323 +               || !au_br_writable(br->br_perm))
4324 +               ? -EROFS : 0;
4325 +}
4326 +
4327 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4328 +{
4329 +#ifdef CONFIG_AUFS_HNOTIFY
4330 +       return !(brperm & AuBrPerm_RR);
4331 +#else
4332 +       return 0;
4333 +#endif
4334 +}
4335 +
4336 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4337 +{
4338 +       int err, exec_flag;
4339 +
4340 +       err = 0;
4341 +       exec_flag = oflag & __FMODE_EXEC;
4342 +       if (unlikely(exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC)))
4343 +               err = -EACCES;
4344 +
4345 +       return err;
4346 +}
4347 +
4348 +/* ---------------------------------------------------------------------- */
4349 +
4350 +/* branch.c */
4351 +struct au_sbinfo;
4352 +void au_br_free(struct au_sbinfo *sinfo);
4353 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4354 +struct au_opt_add;
4355 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4356 +struct au_opt_del;
4357 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4358 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4359 +#ifdef CONFIG_COMPAT
4360 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4361 +#endif
4362 +struct au_opt_mod;
4363 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4364 +             int *do_refresh);
4365 +struct aufs_stfs;
4366 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4367 +
4368 +/* xino.c */
4369 +static const loff_t au_loff_max = LLONG_MAX;
4370 +
4371 +int au_xib_trunc(struct super_block *sb);
4372 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
4373 +                  loff_t *pos);
4374 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
4375 +                   loff_t *pos);
4376 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4377 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4378 +ino_t au_xino_new_ino(struct super_block *sb);
4379 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4380 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4381 +                 ino_t ino);
4382 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4383 +                ino_t *ino);
4384 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4385 +              struct file *base_file, int do_test);
4386 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4387 +
4388 +struct au_opt_xino;
4389 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4390 +void au_xino_clr(struct super_block *sb);
4391 +struct file *au_xino_def(struct super_block *sb);
4392 +int au_xino_path(struct seq_file *seq, struct file *file);
4393 +
4394 +/* ---------------------------------------------------------------------- */
4395 +
4396 +/* Superblock to branch */
4397 +static inline
4398 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4399 +{
4400 +       return au_sbr(sb, bindex)->br_id;
4401 +}
4402 +
4403 +static inline
4404 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4405 +{
4406 +       return au_br_mnt(au_sbr(sb, bindex));
4407 +}
4408 +
4409 +static inline
4410 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4411 +{
4412 +       return au_br_sb(au_sbr(sb, bindex));
4413 +}
4414 +
4415 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4416 +{
4417 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
4418 +}
4419 +
4420 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4421 +{
4422 +       return au_sbr(sb, bindex)->br_perm;
4423 +}
4424 +
4425 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4426 +{
4427 +       return au_br_whable(au_sbr_perm(sb, bindex));
4428 +}
4429 +
4430 +/* ---------------------------------------------------------------------- */
4431 +
4432 +/*
4433 + * wbr_wh_read_lock, wbr_wh_write_lock
4434 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4435 + */
4436 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4437 +
4438 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
4439 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
4440 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
4441 +
4442 +/* ---------------------------------------------------------------------- */
4443 +
4444 +#ifdef CONFIG_AUFS_FHSM
4445 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4446 +{
4447 +       mutex_init(&brfhsm->bf_lock);
4448 +       brfhsm->bf_jiffy = 0;
4449 +       brfhsm->bf_readable = 0;
4450 +}
4451 +
4452 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4453 +{
4454 +       mutex_destroy(&brfhsm->bf_lock);
4455 +}
4456 +#else
4457 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4458 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4459 +#endif
4460 +
4461 +#endif /* __KERNEL__ */
4462 +#endif /* __AUFS_BRANCH_H__ */
4463 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4464 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4465 +++ linux/fs/aufs/conf.mk       2015-06-22 08:27:37.874168290 +0200
4466 @@ -0,0 +1,38 @@
4467 +
4468 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4469 +
4470 +define AuConf
4471 +ifdef ${1}
4472 +AuConfStr += ${1}=${${1}}
4473 +endif
4474 +endef
4475 +
4476 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4477 +       SBILIST \
4478 +       HNOTIFY HFSNOTIFY \
4479 +       EXPORT INO_T_64 \
4480 +       XATTR \
4481 +       FHSM \
4482 +       RDU \
4483 +       SHWH \
4484 +       BR_RAMFS \
4485 +       BR_FUSE POLL \
4486 +       BR_HFSPLUS \
4487 +       BDEV_LOOP \
4488 +       DEBUG MAGIC_SYSRQ
4489 +$(foreach i, ${AuConfAll}, \
4490 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4491 +
4492 +AuConfName = ${obj}/conf.str
4493 +${AuConfName}.tmp: FORCE
4494 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4495 +${AuConfName}: ${AuConfName}.tmp
4496 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4497 +       echo '  GEN    ' $@; \
4498 +       cp -p $< $@; \
4499 +       }
4500 +FORCE:
4501 +clean-files += ${AuConfName} ${AuConfName}.tmp
4502 +${obj}/sysfs.o: ${AuConfName}
4503 +
4504 +-include ${srctree}/${src}/conf_priv.mk
4505 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4506 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4507 +++ linux/fs/aufs/cpup.c        2015-06-22 08:29:23.703424266 +0200
4508 @@ -0,0 +1,1308 @@
4509 +/*
4510 + * Copyright (C) 2005-2015 Junjiro R. Okajima
4511 + *
4512 + * This program, aufs is free software; you can redistribute it and/or modify
4513 + * it under the terms of the GNU General Public License as published by
4514 + * the Free Software Foundation; either version 2 of the License, or
4515 + * (at your option) any later version.
4516 + *
4517 + * This program is distributed in the hope that it will be useful,
4518 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4519 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4520 + * GNU General Public License for more details.
4521 + *
4522 + * You should have received a copy of the GNU General Public License
4523 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4524 + */
4525 +
4526 +/*
4527 + * copy-up functions, see wbr_policy.c for copy-down
4528 + */
4529 +
4530 +#include <linux/fs_stack.h>
4531 +#include <linux/mm.h>
4532 +#include "aufs.h"
4533 +
4534 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4535 +{
4536 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4537 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4538 +
4539 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4540 +
4541 +       dst->i_flags |= iflags & ~mask;
4542 +       if (au_test_fs_notime(dst->i_sb))
4543 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4544 +}
4545 +
4546 +void au_cpup_attr_timesizes(struct inode *inode)
4547 +{
4548 +       struct inode *h_inode;
4549 +
4550 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4551 +       fsstack_copy_attr_times(inode, h_inode);
4552 +       fsstack_copy_inode_size(inode, h_inode);
4553 +}
4554 +
4555 +void au_cpup_attr_nlink(struct inode *inode, int force)
4556 +{
4557 +       struct inode *h_inode;
4558 +       struct super_block *sb;
4559 +       aufs_bindex_t bindex, bend;
4560 +
4561 +       sb = inode->i_sb;
4562 +       bindex = au_ibstart(inode);
4563 +       h_inode = au_h_iptr(inode, bindex);
4564 +       if (!force
4565 +           && !S_ISDIR(h_inode->i_mode)
4566 +           && au_opt_test(au_mntflags(sb), PLINK)
4567 +           && au_plink_test(inode))
4568 +               return;
4569 +
4570 +       /*
4571 +        * 0 can happen in revalidating.
4572 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4573 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4574 +        * case.
4575 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4576 +        *       the incorrect link count.
4577 +        */
4578 +       set_nlink(inode, h_inode->i_nlink);
4579 +
4580 +       /*
4581 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4582 +        * it may includes whplink directory.
4583 +        */
4584 +       if (S_ISDIR(h_inode->i_mode)) {
4585 +               bend = au_ibend(inode);
4586 +               for (bindex++; bindex <= bend; bindex++) {
4587 +                       h_inode = au_h_iptr(inode, bindex);
4588 +                       if (h_inode)
4589 +                               au_add_nlink(inode, h_inode);
4590 +               }
4591 +       }
4592 +}
4593 +
4594 +void au_cpup_attr_changeable(struct inode *inode)
4595 +{
4596 +       struct inode *h_inode;
4597 +
4598 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4599 +       inode->i_mode = h_inode->i_mode;
4600 +       inode->i_uid = h_inode->i_uid;
4601 +       inode->i_gid = h_inode->i_gid;
4602 +       au_cpup_attr_timesizes(inode);
4603 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4604 +}
4605 +
4606 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4607 +{
4608 +       struct au_iinfo *iinfo = au_ii(inode);
4609 +
4610 +       IiMustWriteLock(inode);
4611 +
4612 +       iinfo->ii_higen = h_inode->i_generation;
4613 +       iinfo->ii_hsb1 = h_inode->i_sb;
4614 +}
4615 +
4616 +void au_cpup_attr_all(struct inode *inode, int force)
4617 +{
4618 +       struct inode *h_inode;
4619 +
4620 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4621 +       au_cpup_attr_changeable(inode);
4622 +       if (inode->i_nlink > 0)
4623 +               au_cpup_attr_nlink(inode, force);
4624 +       inode->i_rdev = h_inode->i_rdev;
4625 +       inode->i_blkbits = h_inode->i_blkbits;
4626 +       au_cpup_igen(inode, h_inode);
4627 +}
4628 +
4629 +/* ---------------------------------------------------------------------- */
4630 +
4631 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4632 +
4633 +/* keep the timestamps of the parent dir when cpup */
4634 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4635 +                   struct path *h_path)
4636 +{
4637 +       struct inode *h_inode;
4638 +
4639 +       dt->dt_dentry = dentry;
4640 +       dt->dt_h_path = *h_path;
4641 +       h_inode = h_path->dentry->d_inode;
4642 +       dt->dt_atime = h_inode->i_atime;
4643 +       dt->dt_mtime = h_inode->i_mtime;
4644 +       /* smp_mb(); */
4645 +}
4646 +
4647 +void au_dtime_revert(struct au_dtime *dt)
4648 +{
4649 +       struct iattr attr;
4650 +       int err;
4651 +
4652 +       attr.ia_atime = dt->dt_atime;
4653 +       attr.ia_mtime = dt->dt_mtime;
4654 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4655 +               | ATTR_ATIME | ATTR_ATIME_SET;
4656 +
4657 +       /* no delegation since this is a directory */
4658 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4659 +       if (unlikely(err))
4660 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4661 +}
4662 +
4663 +/* ---------------------------------------------------------------------- */
4664 +
4665 +/* internal use only */
4666 +struct au_cpup_reg_attr {
4667 +       int             valid;
4668 +       struct kstat    st;
4669 +       unsigned int    iflags; /* inode->i_flags */
4670 +};
4671 +
4672 +static noinline_for_stack
4673 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4674 +              struct au_cpup_reg_attr *h_src_attr)
4675 +{
4676 +       int err, sbits, icex;
4677 +       unsigned int mnt_flags;
4678 +       unsigned char verbose;
4679 +       struct iattr ia;
4680 +       struct path h_path;
4681 +       struct inode *h_isrc, *h_idst;
4682 +       struct kstat *h_st;
4683 +       struct au_branch *br;
4684 +
4685 +       h_path.dentry = au_h_dptr(dst, bindex);
4686 +       h_idst = h_path.dentry->d_inode;
4687 +       br = au_sbr(dst->d_sb, bindex);
4688 +       h_path.mnt = au_br_mnt(br);
4689 +       h_isrc = h_src->d_inode;
4690 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4691 +               | ATTR_ATIME | ATTR_MTIME
4692 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4693 +       if (h_src_attr && h_src_attr->valid) {
4694 +               h_st = &h_src_attr->st;
4695 +               ia.ia_uid = h_st->uid;
4696 +               ia.ia_gid = h_st->gid;
4697 +               ia.ia_atime = h_st->atime;
4698 +               ia.ia_mtime = h_st->mtime;
4699 +               if (h_idst->i_mode != h_st->mode
4700 +                   && !S_ISLNK(h_idst->i_mode)) {
4701 +                       ia.ia_valid |= ATTR_MODE;
4702 +                       ia.ia_mode = h_st->mode;
4703 +               }
4704 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4705 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4706 +       } else {
4707 +               ia.ia_uid = h_isrc->i_uid;
4708 +               ia.ia_gid = h_isrc->i_gid;
4709 +               ia.ia_atime = h_isrc->i_atime;
4710 +               ia.ia_mtime = h_isrc->i_mtime;
4711 +               if (h_idst->i_mode != h_isrc->i_mode
4712 +                   && !S_ISLNK(h_idst->i_mode)) {
4713 +                       ia.ia_valid |= ATTR_MODE;
4714 +                       ia.ia_mode = h_isrc->i_mode;
4715 +               }
4716 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4717 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4718 +       }
4719 +       /* no delegation since it is just created */
4720 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4721 +
4722 +       /* is this nfs only? */
4723 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4724 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4725 +               ia.ia_mode = h_isrc->i_mode;
4726 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4727 +       }
4728 +
4729 +       icex = br->br_perm & AuBrAttr_ICEX;
4730 +       if (!err) {
4731 +               mnt_flags = au_mntflags(dst->d_sb);
4732 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4733 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
4734 +       }
4735 +
4736 +       return err;
4737 +}
4738 +
4739 +/* ---------------------------------------------------------------------- */
4740 +
4741 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4742 +                          char *buf, unsigned long blksize)
4743 +{
4744 +       int err;
4745 +       size_t sz, rbytes, wbytes;
4746 +       unsigned char all_zero;
4747 +       char *p, *zp;
4748 +       struct mutex *h_mtx;
4749 +       /* reduce stack usage */
4750 +       struct iattr *ia;
4751 +
4752 +       zp = page_address(ZERO_PAGE(0));
4753 +       if (unlikely(!zp))
4754 +               return -ENOMEM; /* possible? */
4755 +
4756 +       err = 0;
4757 +       all_zero = 0;
4758 +       while (len) {
4759 +               AuDbg("len %lld\n", len);
4760 +               sz = blksize;
4761 +               if (len < blksize)
4762 +                       sz = len;
4763 +
4764 +               rbytes = 0;
4765 +               /* todo: signal_pending? */
4766 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4767 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4768 +                       err = rbytes;
4769 +               }
4770 +               if (unlikely(err < 0))
4771 +                       break;
4772 +
4773 +               all_zero = 0;
4774 +               if (len >= rbytes && rbytes == blksize)
4775 +                       all_zero = !memcmp(buf, zp, rbytes);
4776 +               if (!all_zero) {
4777 +                       wbytes = rbytes;
4778 +                       p = buf;
4779 +                       while (wbytes) {
4780 +                               size_t b;
4781 +
4782 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4783 +                               err = b;
4784 +                               /* todo: signal_pending? */
4785 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
4786 +                                       continue;
4787 +                               if (unlikely(err < 0))
4788 +                                       break;
4789 +                               wbytes -= b;
4790 +                               p += b;
4791 +                       }
4792 +                       if (unlikely(err < 0))
4793 +                               break;
4794 +               } else {
4795 +                       loff_t res;
4796 +
4797 +                       AuLabel(hole);
4798 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4799 +                       err = res;
4800 +                       if (unlikely(res < 0))
4801 +                               break;
4802 +               }
4803 +               len -= rbytes;
4804 +               err = 0;
4805 +       }
4806 +
4807 +       /* the last block may be a hole */
4808 +       if (!err && all_zero) {
4809 +               AuLabel(last hole);
4810 +
4811 +               err = 1;
4812 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
4813 +                       /* nfs requires this step to make last hole */
4814 +                       /* is this only nfs? */
4815 +                       do {
4816 +                               /* todo: signal_pending? */
4817 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4818 +                       } while (err == -EAGAIN || err == -EINTR);
4819 +                       if (err == 1)
4820 +                               dst->f_pos--;
4821 +               }
4822 +
4823 +               if (err == 1) {
4824 +                       ia = (void *)buf;
4825 +                       ia->ia_size = dst->f_pos;
4826 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4827 +                       ia->ia_file = dst;
4828 +                       h_mtx = &file_inode(dst)->i_mutex;
4829 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
4830 +                       /* no delegation since it is just created */
4831 +                       err = vfsub_notify_change(&dst->f_path, ia,
4832 +                                                 /*delegated*/NULL);
4833 +                       mutex_unlock(h_mtx);
4834 +               }
4835 +       }
4836 +
4837 +       return err;
4838 +}
4839 +
4840 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
4841 +{
4842 +       int err;
4843 +       unsigned long blksize;
4844 +       unsigned char do_kfree;
4845 +       char *buf;
4846 +
4847 +       err = -ENOMEM;
4848 +       blksize = dst->f_path.dentry->d_sb->s_blocksize;
4849 +       if (!blksize || PAGE_SIZE < blksize)
4850 +               blksize = PAGE_SIZE;
4851 +       AuDbg("blksize %lu\n", blksize);
4852 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4853 +       if (do_kfree)
4854 +               buf = kmalloc(blksize, GFP_NOFS);
4855 +       else
4856 +               buf = (void *)__get_free_page(GFP_NOFS);
4857 +       if (unlikely(!buf))
4858 +               goto out;
4859 +
4860 +       if (len > (1 << 22))
4861 +               AuDbg("copying a large file %lld\n", (long long)len);
4862 +
4863 +       src->f_pos = 0;
4864 +       dst->f_pos = 0;
4865 +       err = au_do_copy_file(dst, src, len, buf, blksize);
4866 +       if (do_kfree)
4867 +               kfree(buf);
4868 +       else
4869 +               free_page((unsigned long)buf);
4870 +
4871 +out:
4872 +       return err;
4873 +}
4874 +
4875 +/*
4876 + * to support a sparse file which is opened with O_APPEND,
4877 + * we need to close the file.
4878 + */
4879 +static int au_cp_regular(struct au_cp_generic *cpg)
4880 +{
4881 +       int err, i;
4882 +       enum { SRC, DST };
4883 +       struct {
4884 +               aufs_bindex_t bindex;
4885 +               unsigned int flags;
4886 +               struct dentry *dentry;
4887 +               int force_wr;
4888 +               struct file *file;
4889 +               void *label;
4890 +       } *f, file[] = {
4891 +               {
4892 +                       .bindex = cpg->bsrc,
4893 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
4894 +                       .label = &&out
4895 +               },
4896 +               {
4897 +                       .bindex = cpg->bdst,
4898 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
4899 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
4900 +                       .label = &&out_src
4901 +               }
4902 +       };
4903 +       struct super_block *sb;
4904 +
4905 +       /* bsrc branch can be ro/rw. */
4906 +       sb = cpg->dentry->d_sb;
4907 +       f = file;
4908 +       for (i = 0; i < 2; i++, f++) {
4909 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
4910 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
4911 +                                   /*file*/NULL, f->force_wr);
4912 +               err = PTR_ERR(f->file);
4913 +               if (IS_ERR(f->file))
4914 +                       goto *f->label;
4915 +       }
4916 +
4917 +       /* try stopping to update while we copyup */
4918 +       IMustLock(file[SRC].dentry->d_inode);
4919 +       err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
4920 +
4921 +       fput(file[DST].file);
4922 +       au_sbr_put(sb, file[DST].bindex);
4923 +
4924 +out_src:
4925 +       fput(file[SRC].file);
4926 +       au_sbr_put(sb, file[SRC].bindex);
4927 +out:
4928 +       return err;
4929 +}
4930 +
4931 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
4932 +                             struct au_cpup_reg_attr *h_src_attr)
4933 +{
4934 +       int err, rerr;
4935 +       loff_t l;
4936 +       struct path h_path;
4937 +       struct inode *h_src_inode, *h_dst_inode;
4938 +
4939 +       err = 0;
4940 +       h_src_inode = au_h_iptr(cpg->dentry->d_inode, cpg->bsrc);
4941 +       l = i_size_read(h_src_inode);
4942 +       if (cpg->len == -1 || l < cpg->len)
4943 +               cpg->len = l;
4944 +       if (cpg->len) {
4945 +               /* try stopping to update while we are referencing */
4946 +               mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
4947 +               au_pin_hdir_unlock(cpg->pin);
4948 +
4949 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
4950 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
4951 +               h_src_attr->iflags = h_src_inode->i_flags;
4952 +               err = vfs_getattr(&h_path, &h_src_attr->st);
4953 +               if (unlikely(err)) {
4954 +                       mutex_unlock(&h_src_inode->i_mutex);
4955 +                       goto out;
4956 +               }
4957 +               h_src_attr->valid = 1;
4958 +               err = au_cp_regular(cpg);
4959 +               mutex_unlock(&h_src_inode->i_mutex);
4960 +               rerr = au_pin_hdir_relock(cpg->pin);
4961 +               if (!err && rerr)
4962 +                       err = rerr;
4963 +       }
4964 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
4965 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
4966 +               h_dst_inode = h_path.dentry->d_inode;
4967 +               spin_lock(&h_dst_inode->i_lock);
4968 +               h_dst_inode->i_state |= I_LINKABLE;
4969 +               spin_unlock(&h_dst_inode->i_lock);
4970 +       }
4971 +
4972 +out:
4973 +       return err;
4974 +}
4975 +
4976 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
4977 +                             struct inode *h_dir)
4978 +{
4979 +       int err, symlen;
4980 +       mm_segment_t old_fs;
4981 +       union {
4982 +               char *k;
4983 +               char __user *u;
4984 +       } sym;
4985 +
4986 +       err = -ENOSYS;
4987 +       if (unlikely(!h_src->d_inode->i_op->readlink))
4988 +               goto out;
4989 +
4990 +       err = -ENOMEM;
4991 +       sym.k = (void *)__get_free_page(GFP_NOFS);
4992 +       if (unlikely(!sym.k))
4993 +               goto out;
4994 +
4995 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
4996 +       old_fs = get_fs();
4997 +       set_fs(KERNEL_DS);
4998 +       symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
4999 +       err = symlen;
5000 +       set_fs(old_fs);
5001 +
5002 +       if (symlen > 0) {
5003 +               sym.k[symlen] = 0;
5004 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5005 +       }
5006 +       free_page((unsigned long)sym.k);
5007 +
5008 +out:
5009 +       return err;
5010 +}
5011 +
5012 +static noinline_for_stack
5013 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5014 +              struct au_cpup_reg_attr *h_src_attr)
5015 +{
5016 +       int err;
5017 +       umode_t mode;
5018 +       unsigned int mnt_flags;
5019 +       unsigned char isdir, isreg, force;
5020 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5021 +       struct au_dtime dt;
5022 +       struct path h_path;
5023 +       struct dentry *h_src, *h_dst, *h_parent;
5024 +       struct inode *h_inode, *h_dir;
5025 +       struct super_block *sb;
5026 +
5027 +       /* bsrc branch can be ro/rw. */
5028 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5029 +       h_inode = h_src->d_inode;
5030 +       AuDebugOn(h_inode != au_h_iptr(cpg->dentry->d_inode, cpg->bsrc));
5031 +
5032 +       /* try stopping to be referenced while we are creating */
5033 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5034 +       if (au_ftest_cpup(cpg->flags, RENAME))
5035 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5036 +                                 AUFS_WH_PFX_LEN));
5037 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5038 +       h_dir = h_parent->d_inode;
5039 +       IMustLock(h_dir);
5040 +       AuDebugOn(h_parent != h_dst->d_parent);
5041 +
5042 +       sb = cpg->dentry->d_sb;
5043 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5044 +       if (do_dt) {
5045 +               h_path.dentry = h_parent;
5046 +               au_dtime_store(&dt, dst_parent, &h_path);
5047 +       }
5048 +       h_path.dentry = h_dst;
5049 +
5050 +       isreg = 0;
5051 +       isdir = 0;
5052 +       mode = h_inode->i_mode;
5053 +       switch (mode & S_IFMT) {
5054 +       case S_IFREG:
5055 +               isreg = 1;
5056 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
5057 +                                  /*want_excl*/true);
5058 +               if (!err)
5059 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5060 +               break;
5061 +       case S_IFDIR:
5062 +               isdir = 1;
5063 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5064 +               if (!err) {
5065 +                       /*
5066 +                        * strange behaviour from the users view,
5067 +                        * particularry setattr case
5068 +                        */
5069 +                       if (au_ibstart(dst_parent->d_inode) == cpg->bdst)
5070 +                               au_cpup_attr_nlink(dst_parent->d_inode,
5071 +                                                  /*force*/1);
5072 +                       au_cpup_attr_nlink(cpg->dentry->d_inode, /*force*/1);
5073 +               }
5074 +               break;
5075 +       case S_IFLNK:
5076 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5077 +               break;
5078 +       case S_IFCHR:
5079 +       case S_IFBLK:
5080 +               AuDebugOn(!capable(CAP_MKNOD));
5081 +               /*FALLTHROUGH*/
5082 +       case S_IFIFO:
5083 +       case S_IFSOCK:
5084 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5085 +               break;
5086 +       default:
5087 +               AuIOErr("Unknown inode type 0%o\n", mode);
5088 +               err = -EIO;
5089 +       }
5090 +
5091 +       mnt_flags = au_mntflags(sb);
5092 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5093 +           && !isdir
5094 +           && au_opt_test(mnt_flags, XINO)
5095 +           && (h_inode->i_nlink == 1
5096 +               || (h_inode->i_state & I_LINKABLE))
5097 +           /* todo: unnecessary? */
5098 +           /* && cpg->dentry->d_inode->i_nlink == 1 */
5099 +           && cpg->bdst < cpg->bsrc
5100 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5101 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5102 +               /* ignore this error */
5103 +
5104 +       if (!err) {
5105 +               force = 0;
5106 +               if (isreg) {
5107 +                       force = !!cpg->len;
5108 +                       if (cpg->len == -1)
5109 +                               force = !!i_size_read(h_inode);
5110 +               }
5111 +               au_fhsm_wrote(sb, cpg->bdst, force);
5112 +       }
5113 +
5114 +       if (do_dt)
5115 +               au_dtime_revert(&dt);
5116 +       return err;
5117 +}
5118 +
5119 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5120 +{
5121 +       int err;
5122 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5123 +       struct inode *h_dir;
5124 +       aufs_bindex_t bdst;
5125 +
5126 +       dentry = cpg->dentry;
5127 +       bdst = cpg->bdst;
5128 +       h_dentry = au_h_dptr(dentry, bdst);
5129 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5130 +               dget(h_dentry);
5131 +               au_set_h_dptr(dentry, bdst, NULL);
5132 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5133 +               if (!err)
5134 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5135 +               au_set_h_dptr(dentry, bdst, h_dentry);
5136 +       } else {
5137 +               err = 0;
5138 +               parent = dget_parent(dentry);
5139 +               h_parent = au_h_dptr(parent, bdst);
5140 +               dput(parent);
5141 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5142 +               if (IS_ERR(h_path->dentry))
5143 +                       err = PTR_ERR(h_path->dentry);
5144 +       }
5145 +       if (unlikely(err))
5146 +               goto out;
5147 +
5148 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5149 +       h_dir = h_parent->d_inode;
5150 +       IMustLock(h_dir);
5151 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5152 +       /* no delegation since it is just created */
5153 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
5154 +       dput(h_path->dentry);
5155 +
5156 +out:
5157 +       return err;
5158 +}
5159 +
5160 +/*
5161 + * copyup the @dentry from @bsrc to @bdst.
5162 + * the caller must set the both of lower dentries.
5163 + * @len is for truncating when it is -1 copyup the entire file.
5164 + * in link/rename cases, @dst_parent may be different from the real one.
5165 + * basic->bsrc can be larger than basic->bdst.
5166 + */
5167 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5168 +{
5169 +       int err, rerr;
5170 +       aufs_bindex_t old_ibstart;
5171 +       unsigned char isdir, plink;
5172 +       struct dentry *h_src, *h_dst, *h_parent;
5173 +       struct inode *dst_inode, *h_dir, *inode, *delegated;
5174 +       struct super_block *sb;
5175 +       struct au_branch *br;
5176 +       /* to reuduce stack size */
5177 +       struct {
5178 +               struct au_dtime dt;
5179 +               struct path h_path;
5180 +               struct au_cpup_reg_attr h_src_attr;
5181 +       } *a;
5182 +
5183 +       err = -ENOMEM;
5184 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5185 +       if (unlikely(!a))
5186 +               goto out;
5187 +       a->h_src_attr.valid = 0;
5188 +
5189 +       sb = cpg->dentry->d_sb;
5190 +       br = au_sbr(sb, cpg->bdst);
5191 +       a->h_path.mnt = au_br_mnt(br);
5192 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5193 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5194 +       h_dir = h_parent->d_inode;
5195 +       IMustLock(h_dir);
5196 +
5197 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5198 +       inode = cpg->dentry->d_inode;
5199 +
5200 +       if (!dst_parent)
5201 +               dst_parent = dget_parent(cpg->dentry);
5202 +       else
5203 +               dget(dst_parent);
5204 +
5205 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5206 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5207 +       if (dst_inode) {
5208 +               if (unlikely(!plink)) {
5209 +                       err = -EIO;
5210 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5211 +                               "but plink is disabled\n",
5212 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5213 +                       goto out_parent;
5214 +               }
5215 +
5216 +               if (dst_inode->i_nlink) {
5217 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5218 +
5219 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5220 +                       err = PTR_ERR(h_src);
5221 +                       if (IS_ERR(h_src))
5222 +                               goto out_parent;
5223 +                       if (unlikely(!h_src->d_inode)) {
5224 +                               err = -EIO;
5225 +                               AuIOErr("i%lu exists on a upper branch "
5226 +                                       "but not pseudo-linked\n",
5227 +                                       inode->i_ino);
5228 +                               dput(h_src);
5229 +                               goto out_parent;
5230 +                       }
5231 +
5232 +                       if (do_dt) {
5233 +                               a->h_path.dentry = h_parent;
5234 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5235 +                       }
5236 +
5237 +                       a->h_path.dentry = h_dst;
5238 +                       delegated = NULL;
5239 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5240 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5241 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5242 +                       if (do_dt)
5243 +                               au_dtime_revert(&a->dt);
5244 +                       if (unlikely(err == -EWOULDBLOCK)) {
5245 +                               pr_warn("cannot retry for NFSv4 delegation"
5246 +                                       " for an internal link\n");
5247 +                               iput(delegated);
5248 +                       }
5249 +                       dput(h_src);
5250 +                       goto out_parent;
5251 +               } else
5252 +                       /* todo: cpup_wh_file? */
5253 +                       /* udba work */
5254 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5255 +       }
5256 +
5257 +       isdir = S_ISDIR(inode->i_mode);
5258 +       old_ibstart = au_ibstart(inode);
5259 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5260 +       if (unlikely(err))
5261 +               goto out_rev;
5262 +       dst_inode = h_dst->d_inode;
5263 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
5264 +       /* todo: necessary? */
5265 +       /* au_pin_hdir_unlock(cpg->pin); */
5266 +
5267 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5268 +       if (unlikely(err)) {
5269 +               /* todo: necessary? */
5270 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5271 +               mutex_unlock(&dst_inode->i_mutex);
5272 +               goto out_rev;
5273 +       }
5274 +
5275 +       if (cpg->bdst < old_ibstart) {
5276 +               if (S_ISREG(inode->i_mode)) {
5277 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5278 +                       if (unlikely(err)) {
5279 +                               /* ignore an error */
5280 +                               /* au_pin_hdir_relock(cpg->pin); */
5281 +                               mutex_unlock(&dst_inode->i_mutex);
5282 +                               goto out_rev;
5283 +                       }
5284 +               }
5285 +               au_set_ibstart(inode, cpg->bdst);
5286 +       } else
5287 +               au_set_ibend(inode, cpg->bdst);
5288 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5289 +                     au_hi_flags(inode, isdir));
5290 +
5291 +       /* todo: necessary? */
5292 +       /* err = au_pin_hdir_relock(cpg->pin); */
5293 +       mutex_unlock(&dst_inode->i_mutex);
5294 +       if (unlikely(err))
5295 +               goto out_rev;
5296 +
5297 +       if (!isdir
5298 +           && (h_src->d_inode->i_nlink > 1
5299 +               || h_src->d_inode->i_state & I_LINKABLE)
5300 +           && plink)
5301 +               au_plink_append(inode, cpg->bdst, h_dst);
5302 +
5303 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5304 +               a->h_path.dentry = h_dst;
5305 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5306 +       }
5307 +       if (!err)
5308 +               goto out_parent; /* success */
5309 +
5310 +       /* revert */
5311 +out_rev:
5312 +       a->h_path.dentry = h_parent;
5313 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5314 +       a->h_path.dentry = h_dst;
5315 +       rerr = 0;
5316 +       if (h_dst->d_inode) {
5317 +               if (!isdir) {
5318 +                       /* no delegation since it is just created */
5319 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5320 +                                           /*delegated*/NULL, /*force*/0);
5321 +               } else
5322 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5323 +       }
5324 +       au_dtime_revert(&a->dt);
5325 +       if (rerr) {
5326 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5327 +               err = -EIO;
5328 +       }
5329 +out_parent:
5330 +       dput(dst_parent);
5331 +       kfree(a);
5332 +out:
5333 +       return err;
5334 +}
5335 +
5336 +#if 0 /* reserved */
5337 +struct au_cpup_single_args {
5338 +       int *errp;
5339 +       struct au_cp_generic *cpg;
5340 +       struct dentry *dst_parent;
5341 +};
5342 +
5343 +static void au_call_cpup_single(void *args)
5344 +{
5345 +       struct au_cpup_single_args *a = args;
5346 +
5347 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5348 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5349 +       au_pin_hdir_release(a->cpg->pin);
5350 +}
5351 +#endif
5352 +
5353 +/*
5354 + * prevent SIGXFSZ in copy-up.
5355 + * testing CAP_MKNOD is for generic fs,
5356 + * but CAP_FSETID is for xfs only, currently.
5357 + */
5358 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5359 +{
5360 +       int do_sio;
5361 +       struct super_block *sb;
5362 +       struct inode *h_dir;
5363 +
5364 +       do_sio = 0;
5365 +       sb = au_pinned_parent(pin)->d_sb;
5366 +       if (!au_wkq_test()
5367 +           && (!au_sbi(sb)->si_plink_maint_pid
5368 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5369 +               switch (mode & S_IFMT) {
5370 +               case S_IFREG:
5371 +                       /* no condition about RLIMIT_FSIZE and the file size */
5372 +                       do_sio = 1;
5373 +                       break;
5374 +               case S_IFCHR:
5375 +               case S_IFBLK:
5376 +                       do_sio = !capable(CAP_MKNOD);
5377 +                       break;
5378 +               }
5379 +               if (!do_sio)
5380 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5381 +                                 && !capable(CAP_FSETID));
5382 +               /* this workaround may be removed in the future */
5383 +               if (!do_sio) {
5384 +                       h_dir = au_pinned_h_dir(pin);
5385 +                       do_sio = h_dir->i_mode & S_ISVTX;
5386 +               }
5387 +       }
5388 +
5389 +       return do_sio;
5390 +}
5391 +
5392 +#if 0 /* reserved */
5393 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5394 +{
5395 +       int err, wkq_err;
5396 +       struct dentry *h_dentry;
5397 +
5398 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5399 +       if (!au_cpup_sio_test(pin, h_dentry->d_inode->i_mode))
5400 +               err = au_cpup_single(cpg, dst_parent);
5401 +       else {
5402 +               struct au_cpup_single_args args = {
5403 +                       .errp           = &err,
5404 +                       .cpg            = cpg,
5405 +                       .dst_parent     = dst_parent
5406 +               };
5407 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5408 +               if (unlikely(wkq_err))
5409 +                       err = wkq_err;
5410 +       }
5411 +
5412 +       return err;
5413 +}
5414 +#endif
5415 +
5416 +/*
5417 + * copyup the @dentry from the first active lower branch to @bdst,
5418 + * using au_cpup_single().
5419 + */
5420 +static int au_cpup_simple(struct au_cp_generic *cpg)
5421 +{
5422 +       int err;
5423 +       unsigned int flags_orig;
5424 +       struct dentry *dentry;
5425 +
5426 +       AuDebugOn(cpg->bsrc < 0);
5427 +
5428 +       dentry = cpg->dentry;
5429 +       DiMustWriteLock(dentry);
5430 +
5431 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5432 +       if (!err) {
5433 +               flags_orig = cpg->flags;
5434 +               au_fset_cpup(cpg->flags, RENAME);
5435 +               err = au_cpup_single(cpg, NULL);
5436 +               cpg->flags = flags_orig;
5437 +               if (!err)
5438 +                       return 0; /* success */
5439 +
5440 +               /* revert */
5441 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5442 +               au_set_dbstart(dentry, cpg->bsrc);
5443 +       }
5444 +
5445 +       return err;
5446 +}
5447 +
5448 +struct au_cpup_simple_args {
5449 +       int *errp;
5450 +       struct au_cp_generic *cpg;
5451 +};
5452 +
5453 +static void au_call_cpup_simple(void *args)
5454 +{
5455 +       struct au_cpup_simple_args *a = args;
5456 +
5457 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5458 +       *a->errp = au_cpup_simple(a->cpg);
5459 +       au_pin_hdir_release(a->cpg->pin);
5460 +}
5461 +
5462 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5463 +{
5464 +       int err, wkq_err;
5465 +       struct dentry *dentry, *parent;
5466 +       struct file *h_file;
5467 +       struct inode *h_dir;
5468 +
5469 +       dentry = cpg->dentry;
5470 +       h_file = NULL;
5471 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5472 +               AuDebugOn(cpg->bsrc < 0);
5473 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5474 +               err = PTR_ERR(h_file);
5475 +               if (IS_ERR(h_file))
5476 +                       goto out;
5477 +       }
5478 +
5479 +       parent = dget_parent(dentry);
5480 +       h_dir = au_h_iptr(parent->d_inode, cpg->bdst);
5481 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5482 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
5483 +               err = au_cpup_simple(cpg);
5484 +       else {
5485 +               struct au_cpup_simple_args args = {
5486 +                       .errp           = &err,
5487 +                       .cpg            = cpg
5488 +               };
5489 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5490 +               if (unlikely(wkq_err))
5491 +                       err = wkq_err;
5492 +       }
5493 +
5494 +       dput(parent);
5495 +       if (h_file)
5496 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5497 +
5498 +out:
5499 +       return err;
5500 +}
5501 +
5502 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5503 +{
5504 +       aufs_bindex_t bsrc, bend;
5505 +       struct dentry *dentry, *h_dentry;
5506 +
5507 +       if (cpg->bsrc < 0) {
5508 +               dentry = cpg->dentry;
5509 +               bend = au_dbend(dentry);
5510 +               for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
5511 +                       h_dentry = au_h_dptr(dentry, bsrc);
5512 +                       if (h_dentry) {
5513 +                               AuDebugOn(!h_dentry->d_inode);
5514 +                               break;
5515 +                       }
5516 +               }
5517 +               AuDebugOn(bsrc > bend);
5518 +               cpg->bsrc = bsrc;
5519 +       }
5520 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5521 +       return au_do_sio_cpup_simple(cpg);
5522 +}
5523 +
5524 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5525 +{
5526 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5527 +       return au_do_sio_cpup_simple(cpg);
5528 +}
5529 +
5530 +/* ---------------------------------------------------------------------- */
5531 +
5532 +/*
5533 + * copyup the deleted file for writing.
5534 + */
5535 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5536 +                        struct file *file)
5537 +{
5538 +       int err;
5539 +       unsigned int flags_orig;
5540 +       aufs_bindex_t bsrc_orig;
5541 +       struct dentry *h_d_dst, *h_d_start;
5542 +       struct au_dinfo *dinfo;
5543 +       struct au_hdentry *hdp;
5544 +
5545 +       dinfo = au_di(cpg->dentry);
5546 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5547 +
5548 +       bsrc_orig = cpg->bsrc;
5549 +       cpg->bsrc = dinfo->di_bstart;
5550 +       hdp = dinfo->di_hdentry;
5551 +       h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
5552 +       dinfo->di_bstart = cpg->bdst;
5553 +       hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
5554 +       h_d_start = NULL;
5555 +       if (file) {
5556 +               h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
5557 +               hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_path.dentry;
5558 +       }
5559 +       flags_orig = cpg->flags;
5560 +       cpg->flags = !AuCpup_DTIME;
5561 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5562 +       cpg->flags = flags_orig;
5563 +       if (file) {
5564 +               if (!err)
5565 +                       err = au_reopen_nondir(file);
5566 +               hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
5567 +       }
5568 +       hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
5569 +       dinfo->di_bstart = cpg->bsrc;
5570 +       cpg->bsrc = bsrc_orig;
5571 +
5572 +       return err;
5573 +}
5574 +
5575 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5576 +{
5577 +       int err;
5578 +       aufs_bindex_t bdst;
5579 +       struct au_dtime dt;
5580 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5581 +       struct au_branch *br;
5582 +       struct path h_path;
5583 +
5584 +       dentry = cpg->dentry;
5585 +       bdst = cpg->bdst;
5586 +       br = au_sbr(dentry->d_sb, bdst);
5587 +       parent = dget_parent(dentry);
5588 +       h_parent = au_h_dptr(parent, bdst);
5589 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5590 +       err = PTR_ERR(wh_dentry);
5591 +       if (IS_ERR(wh_dentry))
5592 +               goto out;
5593 +
5594 +       h_path.dentry = h_parent;
5595 +       h_path.mnt = au_br_mnt(br);
5596 +       au_dtime_store(&dt, parent, &h_path);
5597 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5598 +       if (unlikely(err))
5599 +               goto out_wh;
5600 +
5601 +       dget(wh_dentry);
5602 +       h_path.dentry = wh_dentry;
5603 +       if (!d_is_dir(wh_dentry)) {
5604 +               /* no delegation since it is just created */
5605 +               err = vfsub_unlink(h_parent->d_inode, &h_path,
5606 +                                  /*delegated*/NULL, /*force*/0);
5607 +       } else
5608 +               err = vfsub_rmdir(h_parent->d_inode, &h_path);
5609 +       if (unlikely(err)) {
5610 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5611 +                       wh_dentry, err);
5612 +               err = -EIO;
5613 +       }
5614 +       au_dtime_revert(&dt);
5615 +       au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
5616 +
5617 +out_wh:
5618 +       dput(wh_dentry);
5619 +out:
5620 +       dput(parent);
5621 +       return err;
5622 +}
5623 +
5624 +struct au_cpup_wh_args {
5625 +       int *errp;
5626 +       struct au_cp_generic *cpg;
5627 +       struct file *file;
5628 +};
5629 +
5630 +static void au_call_cpup_wh(void *args)
5631 +{
5632 +       struct au_cpup_wh_args *a = args;
5633 +
5634 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5635 +       *a->errp = au_cpup_wh(a->cpg, a->file);
5636 +       au_pin_hdir_release(a->cpg->pin);
5637 +}
5638 +
5639 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5640 +{
5641 +       int err, wkq_err;
5642 +       aufs_bindex_t bdst;
5643 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
5644 +       struct inode *dir, *h_dir, *h_tmpdir;
5645 +       struct au_wbr *wbr;
5646 +       struct au_pin wh_pin, *pin_orig;
5647 +
5648 +       dentry = cpg->dentry;
5649 +       bdst = cpg->bdst;
5650 +       parent = dget_parent(dentry);
5651 +       dir = parent->d_inode;
5652 +       h_orph = NULL;
5653 +       h_parent = NULL;
5654 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
5655 +       h_tmpdir = h_dir;
5656 +       pin_orig = NULL;
5657 +       if (!h_dir->i_nlink) {
5658 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5659 +               h_orph = wbr->wbr_orph;
5660 +
5661 +               h_parent = dget(au_h_dptr(parent, bdst));
5662 +               au_set_h_dptr(parent, bdst, dget(h_orph));
5663 +               h_tmpdir = h_orph->d_inode;
5664 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5665 +
5666 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
5667 +               /* todo: au_h_open_pre()? */
5668 +
5669 +               pin_orig = cpg->pin;
5670 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
5671 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5672 +               cpg->pin = &wh_pin;
5673 +       }
5674 +
5675 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5676 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
5677 +               err = au_cpup_wh(cpg, file);
5678 +       else {
5679 +               struct au_cpup_wh_args args = {
5680 +                       .errp   = &err,
5681 +                       .cpg    = cpg,
5682 +                       .file   = file
5683 +               };
5684 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5685 +               if (unlikely(wkq_err))
5686 +                       err = wkq_err;
5687 +       }
5688 +
5689 +       if (h_orph) {
5690 +               mutex_unlock(&h_tmpdir->i_mutex);
5691 +               /* todo: au_h_open_post()? */
5692 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5693 +               au_set_h_dptr(parent, bdst, h_parent);
5694 +               AuDebugOn(!pin_orig);
5695 +               cpg->pin = pin_orig;
5696 +       }
5697 +       iput(h_dir);
5698 +       dput(parent);
5699 +
5700 +       return err;
5701 +}
5702 +
5703 +/* ---------------------------------------------------------------------- */
5704 +
5705 +/*
5706 + * generic routine for both of copy-up and copy-down.
5707 + */
5708 +/* cf. revalidate function in file.c */
5709 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5710 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5711 +                        struct au_pin *pin,
5712 +                        struct dentry *h_parent, void *arg),
5713 +              void *arg)
5714 +{
5715 +       int err;
5716 +       struct au_pin pin;
5717 +       struct dentry *d, *parent, *h_parent, *real_parent;
5718 +
5719 +       err = 0;
5720 +       parent = dget_parent(dentry);
5721 +       if (IS_ROOT(parent))
5722 +               goto out;
5723 +
5724 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5725 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5726 +
5727 +       /* do not use au_dpage */
5728 +       real_parent = parent;
5729 +       while (1) {
5730 +               dput(parent);
5731 +               parent = dget_parent(dentry);
5732 +               h_parent = au_h_dptr(parent, bdst);
5733 +               if (h_parent)
5734 +                       goto out; /* success */
5735 +
5736 +               /* find top dir which is necessary to cpup */
5737 +               do {
5738 +                       d = parent;
5739 +                       dput(parent);
5740 +                       parent = dget_parent(d);
5741 +                       di_read_lock_parent3(parent, !AuLock_IR);
5742 +                       h_parent = au_h_dptr(parent, bdst);
5743 +                       di_read_unlock(parent, !AuLock_IR);
5744 +               } while (!h_parent);
5745 +
5746 +               if (d != real_parent)
5747 +                       di_write_lock_child3(d);
5748 +
5749 +               /* somebody else might create while we were sleeping */
5750 +               if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
5751 +                       if (au_h_dptr(d, bdst))
5752 +                               au_update_dbstart(d);
5753 +
5754 +                       au_pin_set_dentry(&pin, d);
5755 +                       err = au_do_pin(&pin);
5756 +                       if (!err) {
5757 +                               err = cp(d, bdst, &pin, h_parent, arg);
5758 +                               au_unpin(&pin);
5759 +                       }
5760 +               }
5761 +
5762 +               if (d != real_parent)
5763 +                       di_write_unlock(d);
5764 +               if (unlikely(err))
5765 +                       break;
5766 +       }
5767 +
5768 +out:
5769 +       dput(parent);
5770 +       return err;
5771 +}
5772 +
5773 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
5774 +                      struct au_pin *pin,
5775 +                      struct dentry *h_parent __maybe_unused,
5776 +                      void *arg __maybe_unused)
5777 +{
5778 +       struct au_cp_generic cpg = {
5779 +               .dentry = dentry,
5780 +               .bdst   = bdst,
5781 +               .bsrc   = -1,
5782 +               .len    = 0,
5783 +               .pin    = pin,
5784 +               .flags  = AuCpup_DTIME
5785 +       };
5786 +       return au_sio_cpup_simple(&cpg);
5787 +}
5788 +
5789 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5790 +{
5791 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5792 +}
5793 +
5794 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5795 +{
5796 +       int err;
5797 +       struct dentry *parent;
5798 +       struct inode *dir;
5799 +
5800 +       parent = dget_parent(dentry);
5801 +       dir = parent->d_inode;
5802 +       err = 0;
5803 +       if (au_h_iptr(dir, bdst))
5804 +               goto out;
5805 +
5806 +       di_read_unlock(parent, AuLock_IR);
5807 +       di_write_lock_parent(parent);
5808 +       /* someone else might change our inode while we were sleeping */
5809 +       if (!au_h_iptr(dir, bdst))
5810 +               err = au_cpup_dirs(dentry, bdst);
5811 +       di_downgrade_lock(parent, AuLock_IR);
5812 +
5813 +out:
5814 +       dput(parent);
5815 +       return err;
5816 +}
5817 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
5818 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
5819 +++ linux/fs/aufs/cpup.h        2015-06-22 08:27:37.874168290 +0200
5820 @@ -0,0 +1,94 @@
5821 +/*
5822 + * Copyright (C) 2005-2015 Junjiro R. Okajima
5823 + *
5824 + * This program, aufs is free software; you can redistribute it and/or modify
5825 + * it under the terms of the GNU General Public License as published by
5826 + * the Free Software Foundation; either version 2 of the License, or
5827 + * (at your option) any later version.
5828 + *
5829 + * This program is distributed in the hope that it will be useful,
5830 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5831 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5832 + * GNU General Public License for more details.
5833 + *
5834 + * You should have received a copy of the GNU General Public License
5835 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5836 + */
5837 +
5838 +/*
5839 + * copy-up/down functions
5840 + */
5841 +
5842 +#ifndef __AUFS_CPUP_H__
5843 +#define __AUFS_CPUP_H__
5844 +
5845 +#ifdef __KERNEL__
5846 +
5847 +#include <linux/path.h>
5848 +
5849 +struct inode;
5850 +struct file;
5851 +struct au_pin;
5852 +
5853 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
5854 +void au_cpup_attr_timesizes(struct inode *inode);
5855 +void au_cpup_attr_nlink(struct inode *inode, int force);
5856 +void au_cpup_attr_changeable(struct inode *inode);
5857 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
5858 +void au_cpup_attr_all(struct inode *inode, int force);
5859 +
5860 +/* ---------------------------------------------------------------------- */
5861 +
5862 +struct au_cp_generic {
5863 +       struct dentry   *dentry;
5864 +       aufs_bindex_t   bdst, bsrc;
5865 +       loff_t          len;
5866 +       struct au_pin   *pin;
5867 +       unsigned int    flags;
5868 +};
5869 +
5870 +/* cpup flags */
5871 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
5872 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
5873 +                                                  for link(2) */
5874 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
5875 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
5876 +                                                  cpup */
5877 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
5878 +                                                  existing entry */
5879 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
5880 +                                                  the branch is marked as RO */
5881 +
5882 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
5883 +#define au_fset_cpup(flags, name) \
5884 +       do { (flags) |= AuCpup_##name; } while (0)
5885 +#define au_fclr_cpup(flags, name) \
5886 +       do { (flags) &= ~AuCpup_##name; } while (0)
5887 +
5888 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
5889 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
5890 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
5891 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
5892 +
5893 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5894 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5895 +                        struct au_pin *pin,
5896 +                        struct dentry *h_parent, void *arg),
5897 +              void *arg);
5898 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5899 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5900 +
5901 +/* ---------------------------------------------------------------------- */
5902 +
5903 +/* keep timestamps when copyup */
5904 +struct au_dtime {
5905 +       struct dentry *dt_dentry;
5906 +       struct path dt_h_path;
5907 +       struct timespec dt_atime, dt_mtime;
5908 +};
5909 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5910 +                   struct path *h_path);
5911 +void au_dtime_revert(struct au_dtime *dt);
5912 +
5913 +#endif /* __KERNEL__ */
5914 +#endif /* __AUFS_CPUP_H__ */
5915 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
5916 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
5917 +++ linux/fs/aufs/dbgaufs.c     2015-06-22 08:29:23.706757681 +0200
5918 @@ -0,0 +1,432 @@
5919 +/*
5920 + * Copyright (C) 2005-2015 Junjiro R. Okajima
5921 + *
5922 + * This program, aufs is free software; you can redistribute it and/or modify
5923 + * it under the terms of the GNU General Public License as published by
5924 + * the Free Software Foundation; either version 2 of the License, or
5925 + * (at your option) any later version.
5926 + *
5927 + * This program is distributed in the hope that it will be useful,
5928 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5929 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5930 + * GNU General Public License for more details.
5931 + *
5932 + * You should have received a copy of the GNU General Public License
5933 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5934 + */
5935 +
5936 +/*
5937 + * debugfs interface
5938 + */
5939 +
5940 +#include <linux/debugfs.h>
5941 +#include "aufs.h"
5942 +
5943 +#ifndef CONFIG_SYSFS
5944 +#error DEBUG_FS depends upon SYSFS
5945 +#endif
5946 +
5947 +static struct dentry *dbgaufs;
5948 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
5949 +
5950 +/* 20 is max digits length of ulong 64 */
5951 +struct dbgaufs_arg {
5952 +       int n;
5953 +       char a[20 * 4];
5954 +};
5955 +
5956 +/*
5957 + * common function for all XINO files
5958 + */
5959 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
5960 +                             struct file *file)
5961 +{
5962 +       kfree(file->private_data);
5963 +       return 0;
5964 +}
5965 +
5966 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
5967 +{
5968 +       int err;
5969 +       struct kstat st;
5970 +       struct dbgaufs_arg *p;
5971 +
5972 +       err = -ENOMEM;
5973 +       p = kmalloc(sizeof(*p), GFP_NOFS);
5974 +       if (unlikely(!p))
5975 +               goto out;
5976 +
5977 +       err = 0;
5978 +       p->n = 0;
5979 +       file->private_data = p;
5980 +       if (!xf)
5981 +               goto out;
5982 +
5983 +       err = vfs_getattr(&xf->f_path, &st);
5984 +       if (!err) {
5985 +               if (do_fcnt)
5986 +                       p->n = snprintf
5987 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
5988 +                                (long)file_count(xf), st.blocks, st.blksize,
5989 +                                (long long)st.size);
5990 +               else
5991 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
5992 +                                       st.blocks, st.blksize,
5993 +                                       (long long)st.size);
5994 +               AuDebugOn(p->n >= sizeof(p->a));
5995 +       } else {
5996 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
5997 +               err = 0;
5998 +       }
5999 +
6000 +out:
6001 +       return err;
6002 +
6003 +}
6004 +
6005 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6006 +                              size_t count, loff_t *ppos)
6007 +{
6008 +       struct dbgaufs_arg *p;
6009 +
6010 +       p = file->private_data;
6011 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6012 +}
6013 +
6014 +/* ---------------------------------------------------------------------- */
6015 +
6016 +struct dbgaufs_plink_arg {
6017 +       int n;
6018 +       char a[];
6019 +};
6020 +
6021 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6022 +                                struct file *file)
6023 +{
6024 +       free_page((unsigned long)file->private_data);
6025 +       return 0;
6026 +}
6027 +
6028 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6029 +{
6030 +       int err, i, limit;
6031 +       unsigned long n, sum;
6032 +       struct dbgaufs_plink_arg *p;
6033 +       struct au_sbinfo *sbinfo;
6034 +       struct super_block *sb;
6035 +       struct au_sphlhead *sphl;
6036 +
6037 +       err = -ENOMEM;
6038 +       p = (void *)get_zeroed_page(GFP_NOFS);
6039 +       if (unlikely(!p))
6040 +               goto out;
6041 +
6042 +       err = -EFBIG;
6043 +       sbinfo = inode->i_private;
6044 +       sb = sbinfo->si_sb;
6045 +       si_noflush_read_lock(sb);
6046 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6047 +               limit = PAGE_SIZE - sizeof(p->n);
6048 +
6049 +               /* the number of buckets */
6050 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6051 +               p->n += n;
6052 +               limit -= n;
6053 +
6054 +               sum = 0;
6055 +               for (i = 0, sphl = sbinfo->si_plink;
6056 +                    i < AuPlink_NHASH;
6057 +                    i++, sphl++) {
6058 +                       n = au_sphl_count(sphl);
6059 +                       sum += n;
6060 +
6061 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6062 +                       p->n += n;
6063 +                       limit -= n;
6064 +                       if (unlikely(limit <= 0))
6065 +                               goto out_free;
6066 +               }
6067 +               p->a[p->n - 1] = '\n';
6068 +
6069 +               /* the sum of plinks */
6070 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6071 +               p->n += n;
6072 +               limit -= n;
6073 +               if (unlikely(limit <= 0))
6074 +                       goto out_free;
6075 +       } else {
6076 +#define str "1\n0\n0\n"
6077 +               p->n = sizeof(str) - 1;
6078 +               strcpy(p->a, str);
6079 +#undef str
6080 +       }
6081 +       si_read_unlock(sb);
6082 +
6083 +       err = 0;
6084 +       file->private_data = p;
6085 +       goto out; /* success */
6086 +
6087 +out_free:
6088 +       free_page((unsigned long)p);
6089 +out:
6090 +       return err;
6091 +}
6092 +
6093 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6094 +                                 size_t count, loff_t *ppos)
6095 +{
6096 +       struct dbgaufs_plink_arg *p;
6097 +
6098 +       p = file->private_data;
6099 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6100 +}
6101 +
6102 +static const struct file_operations dbgaufs_plink_fop = {
6103 +       .owner          = THIS_MODULE,
6104 +       .open           = dbgaufs_plink_open,
6105 +       .release        = dbgaufs_plink_release,
6106 +       .read           = dbgaufs_plink_read
6107 +};
6108 +
6109 +/* ---------------------------------------------------------------------- */
6110 +
6111 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6112 +{
6113 +       int err;
6114 +       struct au_sbinfo *sbinfo;
6115 +       struct super_block *sb;
6116 +
6117 +       sbinfo = inode->i_private;
6118 +       sb = sbinfo->si_sb;
6119 +       si_noflush_read_lock(sb);
6120 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6121 +       si_read_unlock(sb);
6122 +       return err;
6123 +}
6124 +
6125 +static const struct file_operations dbgaufs_xib_fop = {
6126 +       .owner          = THIS_MODULE,
6127 +       .open           = dbgaufs_xib_open,
6128 +       .release        = dbgaufs_xi_release,
6129 +       .read           = dbgaufs_xi_read
6130 +};
6131 +
6132 +/* ---------------------------------------------------------------------- */
6133 +
6134 +#define DbgaufsXi_PREFIX "xi"
6135 +
6136 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6137 +{
6138 +       int err;
6139 +       long l;
6140 +       struct au_sbinfo *sbinfo;
6141 +       struct super_block *sb;
6142 +       struct file *xf;
6143 +       struct qstr *name;
6144 +
6145 +       err = -ENOENT;
6146 +       xf = NULL;
6147 +       name = &file->f_path.dentry->d_name;
6148 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6149 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6150 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6151 +               goto out;
6152 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6153 +       if (unlikely(err))
6154 +               goto out;
6155 +
6156 +       sbinfo = inode->i_private;
6157 +       sb = sbinfo->si_sb;
6158 +       si_noflush_read_lock(sb);
6159 +       if (l <= au_sbend(sb)) {
6160 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6161 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6162 +       } else
6163 +               err = -ENOENT;
6164 +       si_read_unlock(sb);
6165 +
6166 +out:
6167 +       return err;
6168 +}
6169 +
6170 +static const struct file_operations dbgaufs_xino_fop = {
6171 +       .owner          = THIS_MODULE,
6172 +       .open           = dbgaufs_xino_open,
6173 +       .release        = dbgaufs_xi_release,
6174 +       .read           = dbgaufs_xi_read
6175 +};
6176 +
6177 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6178 +{
6179 +       aufs_bindex_t bend;
6180 +       struct au_branch *br;
6181 +       struct au_xino_file *xi;
6182 +
6183 +       if (!au_sbi(sb)->si_dbgaufs)
6184 +               return;
6185 +
6186 +       bend = au_sbend(sb);
6187 +       for (; bindex <= bend; bindex++) {
6188 +               br = au_sbr(sb, bindex);
6189 +               xi = &br->br_xino;
6190 +               debugfs_remove(xi->xi_dbgaufs);
6191 +               xi->xi_dbgaufs = NULL;
6192 +       }
6193 +}
6194 +
6195 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6196 +{
6197 +       struct au_sbinfo *sbinfo;
6198 +       struct dentry *parent;
6199 +       struct au_branch *br;
6200 +       struct au_xino_file *xi;
6201 +       aufs_bindex_t bend;
6202 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6203 +
6204 +       sbinfo = au_sbi(sb);
6205 +       parent = sbinfo->si_dbgaufs;
6206 +       if (!parent)
6207 +               return;
6208 +
6209 +       bend = au_sbend(sb);
6210 +       for (; bindex <= bend; bindex++) {
6211 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6212 +               br = au_sbr(sb, bindex);
6213 +               xi = &br->br_xino;
6214 +               AuDebugOn(xi->xi_dbgaufs);
6215 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6216 +                                                    sbinfo, &dbgaufs_xino_fop);
6217 +               /* ignore an error */
6218 +               if (unlikely(!xi->xi_dbgaufs))
6219 +                       AuWarn1("failed %s under debugfs\n", name);
6220 +       }
6221 +}
6222 +
6223 +/* ---------------------------------------------------------------------- */
6224 +
6225 +#ifdef CONFIG_AUFS_EXPORT
6226 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6227 +{
6228 +       int err;
6229 +       struct au_sbinfo *sbinfo;
6230 +       struct super_block *sb;
6231 +
6232 +       sbinfo = inode->i_private;
6233 +       sb = sbinfo->si_sb;
6234 +       si_noflush_read_lock(sb);
6235 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6236 +       si_read_unlock(sb);
6237 +       return err;
6238 +}
6239 +
6240 +static const struct file_operations dbgaufs_xigen_fop = {
6241 +       .owner          = THIS_MODULE,
6242 +       .open           = dbgaufs_xigen_open,
6243 +       .release        = dbgaufs_xi_release,
6244 +       .read           = dbgaufs_xi_read
6245 +};
6246 +
6247 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6248 +{
6249 +       int err;
6250 +
6251 +       /*
6252 +        * This function is a dynamic '__init' function actually,
6253 +        * so the tiny check for si_rwsem is unnecessary.
6254 +        */
6255 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6256 +
6257 +       err = -EIO;
6258 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6259 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6260 +                &dbgaufs_xigen_fop);
6261 +       if (sbinfo->si_dbgaufs_xigen)
6262 +               err = 0;
6263 +
6264 +       return err;
6265 +}
6266 +#else
6267 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6268 +{
6269 +       return 0;
6270 +}
6271 +#endif /* CONFIG_AUFS_EXPORT */
6272 +
6273 +/* ---------------------------------------------------------------------- */
6274 +
6275 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6276 +{
6277 +       /*
6278 +        * This function is a dynamic '__fin' function actually,
6279 +        * so the tiny check for si_rwsem is unnecessary.
6280 +        */
6281 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6282 +
6283 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6284 +       sbinfo->si_dbgaufs = NULL;
6285 +       kobject_put(&sbinfo->si_kobj);
6286 +}
6287 +
6288 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6289 +{
6290 +       int err;
6291 +       char name[SysaufsSiNameLen];
6292 +
6293 +       /*
6294 +        * This function is a dynamic '__init' function actually,
6295 +        * so the tiny check for si_rwsem is unnecessary.
6296 +        */
6297 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6298 +
6299 +       err = -ENOENT;
6300 +       if (!dbgaufs) {
6301 +               AuErr1("/debug/aufs is uninitialized\n");
6302 +               goto out;
6303 +       }
6304 +
6305 +       err = -EIO;
6306 +       sysaufs_name(sbinfo, name);
6307 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6308 +       if (unlikely(!sbinfo->si_dbgaufs))
6309 +               goto out;
6310 +       kobject_get(&sbinfo->si_kobj);
6311 +
6312 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6313 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6314 +                &dbgaufs_xib_fop);
6315 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6316 +               goto out_dir;
6317 +
6318 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6319 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6320 +                &dbgaufs_plink_fop);
6321 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6322 +               goto out_dir;
6323 +
6324 +       err = dbgaufs_xigen_init(sbinfo);
6325 +       if (!err)
6326 +               goto out; /* success */
6327 +
6328 +out_dir:
6329 +       dbgaufs_si_fin(sbinfo);
6330 +out:
6331 +       return err;
6332 +}
6333 +
6334 +/* ---------------------------------------------------------------------- */
6335 +
6336 +void dbgaufs_fin(void)
6337 +{
6338 +       debugfs_remove(dbgaufs);
6339 +}
6340 +
6341 +int __init dbgaufs_init(void)
6342 +{
6343 +       int err;
6344 +
6345 +       err = -EIO;
6346 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6347 +       if (dbgaufs)
6348 +               err = 0;
6349 +       return err;
6350 +}
6351 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6352 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6353 +++ linux/fs/aufs/dbgaufs.h     2015-06-22 08:27:37.874168290 +0200
6354 @@ -0,0 +1,48 @@
6355 +/*
6356 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6357 + *
6358 + * This program, aufs is free software; you can redistribute it and/or modify
6359 + * it under the terms of the GNU General Public License as published by
6360 + * the Free Software Foundation; either version 2 of the License, or
6361 + * (at your option) any later version.
6362 + *
6363 + * This program is distributed in the hope that it will be useful,
6364 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6365 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6366 + * GNU General Public License for more details.
6367 + *
6368 + * You should have received a copy of the GNU General Public License
6369 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6370 + */
6371 +
6372 +/*
6373 + * debugfs interface
6374 + */
6375 +
6376 +#ifndef __DBGAUFS_H__
6377 +#define __DBGAUFS_H__
6378 +
6379 +#ifdef __KERNEL__
6380 +
6381 +struct super_block;
6382 +struct au_sbinfo;
6383 +
6384 +#ifdef CONFIG_DEBUG_FS
6385 +/* dbgaufs.c */
6386 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6387 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6388 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6389 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6390 +void dbgaufs_fin(void);
6391 +int __init dbgaufs_init(void);
6392 +#else
6393 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6394 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6395 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6396 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6397 +AuStubVoid(dbgaufs_fin, void)
6398 +AuStubInt0(__init dbgaufs_init, void)
6399 +#endif /* CONFIG_DEBUG_FS */
6400 +
6401 +#endif /* __KERNEL__ */
6402 +#endif /* __DBGAUFS_H__ */
6403 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6404 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6405 +++ linux/fs/aufs/dcsub.c       2015-06-22 08:27:37.877501704 +0200
6406 @@ -0,0 +1,224 @@
6407 +/*
6408 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6409 + *
6410 + * This program, aufs is free software; you can redistribute it and/or modify
6411 + * it under the terms of the GNU General Public License as published by
6412 + * the Free Software Foundation; either version 2 of the License, or
6413 + * (at your option) any later version.
6414 + *
6415 + * This program is distributed in the hope that it will be useful,
6416 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6417 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6418 + * GNU General Public License for more details.
6419 + *
6420 + * You should have received a copy of the GNU General Public License
6421 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6422 + */
6423 +
6424 +/*
6425 + * sub-routines for dentry cache
6426 + */
6427 +
6428 +#include "aufs.h"
6429 +
6430 +static void au_dpage_free(struct au_dpage *dpage)
6431 +{
6432 +       int i;
6433 +       struct dentry **p;
6434 +
6435 +       p = dpage->dentries;
6436 +       for (i = 0; i < dpage->ndentry; i++)
6437 +               dput(*p++);
6438 +       free_page((unsigned long)dpage->dentries);
6439 +}
6440 +
6441 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6442 +{
6443 +       int err;
6444 +       void *p;
6445 +
6446 +       err = -ENOMEM;
6447 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6448 +       if (unlikely(!dpages->dpages))
6449 +               goto out;
6450 +
6451 +       p = (void *)__get_free_page(gfp);
6452 +       if (unlikely(!p))
6453 +               goto out_dpages;
6454 +
6455 +       dpages->dpages[0].ndentry = 0;
6456 +       dpages->dpages[0].dentries = p;
6457 +       dpages->ndpage = 1;
6458 +       return 0; /* success */
6459 +
6460 +out_dpages:
6461 +       kfree(dpages->dpages);
6462 +out:
6463 +       return err;
6464 +}
6465 +
6466 +void au_dpages_free(struct au_dcsub_pages *dpages)
6467 +{
6468 +       int i;
6469 +       struct au_dpage *p;
6470 +
6471 +       p = dpages->dpages;
6472 +       for (i = 0; i < dpages->ndpage; i++)
6473 +               au_dpage_free(p++);
6474 +       kfree(dpages->dpages);
6475 +}
6476 +
6477 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6478 +                           struct dentry *dentry, gfp_t gfp)
6479 +{
6480 +       int err, sz;
6481 +       struct au_dpage *dpage;
6482 +       void *p;
6483 +
6484 +       dpage = dpages->dpages + dpages->ndpage - 1;
6485 +       sz = PAGE_SIZE / sizeof(dentry);
6486 +       if (unlikely(dpage->ndentry >= sz)) {
6487 +               AuLabel(new dpage);
6488 +               err = -ENOMEM;
6489 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6490 +               p = au_kzrealloc(dpages->dpages, sz,
6491 +                                sz + sizeof(*dpages->dpages), gfp);
6492 +               if (unlikely(!p))
6493 +                       goto out;
6494 +
6495 +               dpages->dpages = p;
6496 +               dpage = dpages->dpages + dpages->ndpage;
6497 +               p = (void *)__get_free_page(gfp);
6498 +               if (unlikely(!p))
6499 +                       goto out;
6500 +
6501 +               dpage->ndentry = 0;
6502 +               dpage->dentries = p;
6503 +               dpages->ndpage++;
6504 +       }
6505 +
6506 +       AuDebugOn(au_dcount(dentry) <= 0);
6507 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6508 +       return 0; /* success */
6509 +
6510 +out:
6511 +       return err;
6512 +}
6513 +
6514 +/* todo: BAD approach */
6515 +/* copied from linux/fs/dcache.c */
6516 +enum d_walk_ret {
6517 +       D_WALK_CONTINUE,
6518 +       D_WALK_QUIT,
6519 +       D_WALK_NORETRY,
6520 +       D_WALK_SKIP,
6521 +};
6522 +
6523 +extern void d_walk(struct dentry *parent, void *data,
6524 +                  enum d_walk_ret (*enter)(void *, struct dentry *),
6525 +                  void (*finish)(void *));
6526 +
6527 +struct ac_dpages_arg {
6528 +       int err;
6529 +       struct au_dcsub_pages *dpages;
6530 +       struct super_block *sb;
6531 +       au_dpages_test test;
6532 +       void *arg;
6533 +};
6534 +
6535 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6536 +{
6537 +       enum d_walk_ret ret;
6538 +       struct ac_dpages_arg *arg = _arg;
6539 +
6540 +       ret = D_WALK_CONTINUE;
6541 +       if (dentry->d_sb == arg->sb
6542 +           && !IS_ROOT(dentry)
6543 +           && au_dcount(dentry) > 0
6544 +           && au_di(dentry)
6545 +           && (!arg->test || arg->test(dentry, arg->arg))) {
6546 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6547 +               if (unlikely(arg->err))
6548 +                       ret = D_WALK_QUIT;
6549 +       }
6550 +
6551 +       return ret;
6552 +}
6553 +
6554 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6555 +                  au_dpages_test test, void *arg)
6556 +{
6557 +       struct ac_dpages_arg args = {
6558 +               .err    = 0,
6559 +               .dpages = dpages,
6560 +               .sb     = root->d_sb,
6561 +               .test   = test,
6562 +               .arg    = arg
6563 +       };
6564 +
6565 +       d_walk(root, &args, au_call_dpages_append, NULL);
6566 +
6567 +       return args.err;
6568 +}
6569 +
6570 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6571 +                      int do_include, au_dpages_test test, void *arg)
6572 +{
6573 +       int err;
6574 +
6575 +       err = 0;
6576 +       write_seqlock(&rename_lock);
6577 +       spin_lock(&dentry->d_lock);
6578 +       if (do_include
6579 +           && au_dcount(dentry) > 0
6580 +           && (!test || test(dentry, arg)))
6581 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6582 +       spin_unlock(&dentry->d_lock);
6583 +       if (unlikely(err))
6584 +               goto out;
6585 +
6586 +       /*
6587 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
6588 +        * mount
6589 +        */
6590 +       while (!IS_ROOT(dentry)) {
6591 +               dentry = dentry->d_parent; /* rename_lock is locked */
6592 +               spin_lock(&dentry->d_lock);
6593 +               if (au_dcount(dentry) > 0
6594 +                   && (!test || test(dentry, arg)))
6595 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6596 +               spin_unlock(&dentry->d_lock);
6597 +               if (unlikely(err))
6598 +                       break;
6599 +       }
6600 +
6601 +out:
6602 +       write_sequnlock(&rename_lock);
6603 +       return err;
6604 +}
6605 +
6606 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6607 +{
6608 +       return au_di(dentry) && dentry->d_sb == arg;
6609 +}
6610 +
6611 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6612 +                           struct dentry *dentry, int do_include)
6613 +{
6614 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
6615 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
6616 +}
6617 +
6618 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
6619 +{
6620 +       struct path path[2] = {
6621 +               {
6622 +                       .dentry = d1
6623 +               },
6624 +               {
6625 +                       .dentry = d2
6626 +               }
6627 +       };
6628 +
6629 +       return path_is_under(path + 0, path + 1);
6630 +}
6631 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
6632 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
6633 +++ linux/fs/aufs/dcsub.h       2015-06-22 08:29:23.706757681 +0200
6634 @@ -0,0 +1,132 @@
6635 +/*
6636 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6637 + *
6638 + * This program, aufs is free software; you can redistribute it and/or modify
6639 + * it under the terms of the GNU General Public License as published by
6640 + * the Free Software Foundation; either version 2 of the License, or
6641 + * (at your option) any later version.
6642 + *
6643 + * This program is distributed in the hope that it will be useful,
6644 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6645 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6646 + * GNU General Public License for more details.
6647 + *
6648 + * You should have received a copy of the GNU General Public License
6649 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6650 + */
6651 +
6652 +/*
6653 + * sub-routines for dentry cache
6654 + */
6655 +
6656 +#ifndef __AUFS_DCSUB_H__
6657 +#define __AUFS_DCSUB_H__
6658 +
6659 +#ifdef __KERNEL__
6660 +
6661 +#include <linux/dcache.h>
6662 +#include <linux/fs.h>
6663 +
6664 +struct au_dpage {
6665 +       int ndentry;
6666 +       struct dentry **dentries;
6667 +};
6668 +
6669 +struct au_dcsub_pages {
6670 +       int ndpage;
6671 +       struct au_dpage *dpages;
6672 +};
6673 +
6674 +/* ---------------------------------------------------------------------- */
6675 +
6676 +/* dcsub.c */
6677 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6678 +void au_dpages_free(struct au_dcsub_pages *dpages);
6679 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6680 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6681 +                  au_dpages_test test, void *arg);
6682 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6683 +                      int do_include, au_dpages_test test, void *arg);
6684 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6685 +                           struct dentry *dentry, int do_include);
6686 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
6687 +
6688 +/* ---------------------------------------------------------------------- */
6689 +
6690 +/*
6691 + * todo: in linux-3.13, several similar (but faster) helpers are added to
6692 + * include/linux/dcache.h. Try them (in the future).
6693 + */
6694 +
6695 +static inline int au_d_hashed_positive(struct dentry *d)
6696 +{
6697 +       int err;
6698 +       struct inode *inode = d->d_inode;
6699 +
6700 +       err = 0;
6701 +       if (unlikely(d_unhashed(d) || !inode || !inode->i_nlink))
6702 +               err = -ENOENT;
6703 +       return err;
6704 +}
6705 +
6706 +static inline int au_d_linkable(struct dentry *d)
6707 +{
6708 +       int err;
6709 +       struct inode *inode = d->d_inode;
6710 +
6711 +       err = au_d_hashed_positive(d);
6712 +       if (err
6713 +           && inode
6714 +           && (inode->i_state & I_LINKABLE))
6715 +               err = 0;
6716 +       return err;
6717 +}
6718 +
6719 +static inline int au_d_alive(struct dentry *d)
6720 +{
6721 +       int err;
6722 +       struct inode *inode;
6723 +
6724 +       err = 0;
6725 +       if (!IS_ROOT(d))
6726 +               err = au_d_hashed_positive(d);
6727 +       else {
6728 +               inode = d->d_inode;
6729 +               if (unlikely(d_unlinked(d) || !inode || !inode->i_nlink))
6730 +                       err = -ENOENT;
6731 +       }
6732 +       return err;
6733 +}
6734 +
6735 +static inline int au_alive_dir(struct dentry *d)
6736 +{
6737 +       int err;
6738 +
6739 +       err = au_d_alive(d);
6740 +       if (unlikely(err || IS_DEADDIR(d->d_inode)))
6741 +               err = -ENOENT;
6742 +       return err;
6743 +}
6744 +
6745 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
6746 +{
6747 +       return a->len == b->len
6748 +               && !memcmp(a->name, b->name, a->len);
6749 +}
6750 +
6751 +/*
6752 + * by the commit
6753 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
6754 + *                     taking d_lock
6755 + * the type of d_lockref.count became int, but the inlined function d_count()
6756 + * still returns unsigned int.
6757 + * I don't know why. Maybe it is for every d_count() users?
6758 + * Anyway au_dcount() lives on.
6759 + */
6760 +static inline int au_dcount(struct dentry *d)
6761 +{
6762 +       return (int)d_count(d);
6763 +}
6764 +
6765 +#endif /* __KERNEL__ */
6766 +#endif /* __AUFS_DCSUB_H__ */
6767 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
6768 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
6769 +++ linux/fs/aufs/debug.c       2015-06-22 08:29:23.706757681 +0200
6770 @@ -0,0 +1,438 @@
6771 +/*
6772 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6773 + *
6774 + * This program, aufs is free software; you can redistribute it and/or modify
6775 + * it under the terms of the GNU General Public License as published by
6776 + * the Free Software Foundation; either version 2 of the License, or
6777 + * (at your option) any later version.
6778 + *
6779 + * This program is distributed in the hope that it will be useful,
6780 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6781 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6782 + * GNU General Public License for more details.
6783 + *
6784 + * You should have received a copy of the GNU General Public License
6785 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6786 + */
6787 +
6788 +/*
6789 + * debug print functions
6790 + */
6791 +
6792 +#include "aufs.h"
6793 +
6794 +/* Returns 0, or -errno.  arg is in kp->arg. */
6795 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
6796 +{
6797 +       int err, n;
6798 +
6799 +       err = kstrtoint(val, 0, &n);
6800 +       if (!err) {
6801 +               if (n > 0)
6802 +                       au_debug_on();
6803 +               else
6804 +                       au_debug_off();
6805 +       }
6806 +       return err;
6807 +}
6808 +
6809 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
6810 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
6811 +{
6812 +       atomic_t *a;
6813 +
6814 +       a = kp->arg;
6815 +       return sprintf(buffer, "%d", atomic_read(a));
6816 +}
6817 +
6818 +static struct kernel_param_ops param_ops_atomic_t = {
6819 +       .set = param_atomic_t_set,
6820 +       .get = param_atomic_t_get
6821 +       /* void (*free)(void *arg) */
6822 +};
6823 +
6824 +atomic_t aufs_debug = ATOMIC_INIT(0);
6825 +MODULE_PARM_DESC(debug, "debug print");
6826 +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
6827 +
6828 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
6829 +char *au_plevel = KERN_DEBUG;
6830 +#define dpri(fmt, ...) do {                                    \
6831 +       if ((au_plevel                                          \
6832 +            && strcmp(au_plevel, KERN_DEBUG))                  \
6833 +           || au_debug_test())                                 \
6834 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
6835 +} while (0)
6836 +
6837 +/* ---------------------------------------------------------------------- */
6838 +
6839 +void au_dpri_whlist(struct au_nhash *whlist)
6840 +{
6841 +       unsigned long ul, n;
6842 +       struct hlist_head *head;
6843 +       struct au_vdir_wh *pos;
6844 +
6845 +       n = whlist->nh_num;
6846 +       head = whlist->nh_head;
6847 +       for (ul = 0; ul < n; ul++) {
6848 +               hlist_for_each_entry(pos, head, wh_hash)
6849 +                       dpri("b%d, %.*s, %d\n",
6850 +                            pos->wh_bindex,
6851 +                            pos->wh_str.len, pos->wh_str.name,
6852 +                            pos->wh_str.len);
6853 +               head++;
6854 +       }
6855 +}
6856 +
6857 +void au_dpri_vdir(struct au_vdir *vdir)
6858 +{
6859 +       unsigned long ul;
6860 +       union au_vdir_deblk_p p;
6861 +       unsigned char *o;
6862 +
6863 +       if (!vdir || IS_ERR(vdir)) {
6864 +               dpri("err %ld\n", PTR_ERR(vdir));
6865 +               return;
6866 +       }
6867 +
6868 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
6869 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
6870 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
6871 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
6872 +               p.deblk = vdir->vd_deblk[ul];
6873 +               o = p.deblk;
6874 +               dpri("[%lu]: %p\n", ul, o);
6875 +       }
6876 +}
6877 +
6878 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
6879 +                       struct dentry *wh)
6880 +{
6881 +       char *n = NULL;
6882 +       int l = 0;
6883 +
6884 +       if (!inode || IS_ERR(inode)) {
6885 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
6886 +               return -1;
6887 +       }
6888 +
6889 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
6890 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
6891 +                    && sizeof(inode->i_blocks) != sizeof(u64));
6892 +       if (wh) {
6893 +               n = (void *)wh->d_name.name;
6894 +               l = wh->d_name.len;
6895 +       }
6896 +
6897 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
6898 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
6899 +            bindex, inode,
6900 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
6901 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
6902 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
6903 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
6904 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
6905 +            inode->i_state, inode->i_flags, inode->i_version,
6906 +            inode->i_generation,
6907 +            l ? ", wh " : "", l, n);
6908 +       return 0;
6909 +}
6910 +
6911 +void au_dpri_inode(struct inode *inode)
6912 +{
6913 +       struct au_iinfo *iinfo;
6914 +       aufs_bindex_t bindex;
6915 +       int err, hn;
6916 +
6917 +       err = do_pri_inode(-1, inode, -1, NULL);
6918 +       if (err || !au_test_aufs(inode->i_sb))
6919 +               return;
6920 +
6921 +       iinfo = au_ii(inode);
6922 +       if (!iinfo)
6923 +               return;
6924 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
6925 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
6926 +       if (iinfo->ii_bstart < 0)
6927 +               return;
6928 +       hn = 0;
6929 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
6930 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
6931 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
6932 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
6933 +       }
6934 +}
6935 +
6936 +void au_dpri_dalias(struct inode *inode)
6937 +{
6938 +       struct dentry *d;
6939 +
6940 +       spin_lock(&inode->i_lock);
6941 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
6942 +               au_dpri_dentry(d);
6943 +       spin_unlock(&inode->i_lock);
6944 +}
6945 +
6946 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
6947 +{
6948 +       struct dentry *wh = NULL;
6949 +       int hn;
6950 +       struct au_iinfo *iinfo;
6951 +
6952 +       if (!dentry || IS_ERR(dentry)) {
6953 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
6954 +               return -1;
6955 +       }
6956 +       /* do not call dget_parent() here */
6957 +       /* note: access d_xxx without d_lock */
6958 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
6959 +            bindex, dentry, dentry,
6960 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
6961 +            au_dcount(dentry), dentry->d_flags,
6962 +            d_unhashed(dentry) ? "un" : "");
6963 +       hn = -1;
6964 +       if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
6965 +               iinfo = au_ii(dentry->d_inode);
6966 +               if (iinfo) {
6967 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
6968 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
6969 +               }
6970 +       }
6971 +       do_pri_inode(bindex, dentry->d_inode, hn, wh);
6972 +       return 0;
6973 +}
6974 +
6975 +void au_dpri_dentry(struct dentry *dentry)
6976 +{
6977 +       struct au_dinfo *dinfo;
6978 +       aufs_bindex_t bindex;
6979 +       int err;
6980 +       struct au_hdentry *hdp;
6981 +
6982 +       err = do_pri_dentry(-1, dentry);
6983 +       if (err || !au_test_aufs(dentry->d_sb))
6984 +               return;
6985 +
6986 +       dinfo = au_di(dentry);
6987 +       if (!dinfo)
6988 +               return;
6989 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
6990 +            dinfo->di_bstart, dinfo->di_bend,
6991 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
6992 +            dinfo->di_tmpfile);
6993 +       if (dinfo->di_bstart < 0)
6994 +               return;
6995 +       hdp = dinfo->di_hdentry;
6996 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
6997 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
6998 +}
6999 +
7000 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7001 +{
7002 +       char a[32];
7003 +
7004 +       if (!file || IS_ERR(file)) {
7005 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7006 +               return -1;
7007 +       }
7008 +       a[0] = 0;
7009 +       if (bindex < 0
7010 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7011 +           && au_test_aufs(file->f_path.dentry->d_sb)
7012 +           && au_fi(file))
7013 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7014 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7015 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7016 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7017 +            file->f_version, file->f_pos, a);
7018 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7019 +               do_pri_dentry(bindex, file->f_path.dentry);
7020 +       return 0;
7021 +}
7022 +
7023 +void au_dpri_file(struct file *file)
7024 +{
7025 +       struct au_finfo *finfo;
7026 +       struct au_fidir *fidir;
7027 +       struct au_hfile *hfile;
7028 +       aufs_bindex_t bindex;
7029 +       int err;
7030 +
7031 +       err = do_pri_file(-1, file);
7032 +       if (err
7033 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7034 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7035 +               return;
7036 +
7037 +       finfo = au_fi(file);
7038 +       if (!finfo)
7039 +               return;
7040 +       if (finfo->fi_btop < 0)
7041 +               return;
7042 +       fidir = finfo->fi_hdir;
7043 +       if (!fidir)
7044 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7045 +       else
7046 +               for (bindex = finfo->fi_btop;
7047 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7048 +                    bindex++) {
7049 +                       hfile = fidir->fd_hfile + bindex;
7050 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7051 +               }
7052 +}
7053 +
7054 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7055 +{
7056 +       struct vfsmount *mnt;
7057 +       struct super_block *sb;
7058 +
7059 +       if (!br || IS_ERR(br))
7060 +               goto out;
7061 +       mnt = au_br_mnt(br);
7062 +       if (!mnt || IS_ERR(mnt))
7063 +               goto out;
7064 +       sb = mnt->mnt_sb;
7065 +       if (!sb || IS_ERR(sb))
7066 +               goto out;
7067 +
7068 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
7069 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7070 +            "xino %d\n",
7071 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
7072 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7073 +            sb->s_flags, sb->s_count,
7074 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7075 +       return 0;
7076 +
7077 +out:
7078 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7079 +       return -1;
7080 +}
7081 +
7082 +void au_dpri_sb(struct super_block *sb)
7083 +{
7084 +       struct au_sbinfo *sbinfo;
7085 +       aufs_bindex_t bindex;
7086 +       int err;
7087 +       /* to reuduce stack size */
7088 +       struct {
7089 +               struct vfsmount mnt;
7090 +               struct au_branch fake;
7091 +       } *a;
7092 +
7093 +       /* this function can be called from magic sysrq */
7094 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7095 +       if (unlikely(!a)) {
7096 +               dpri("no memory\n");
7097 +               return;
7098 +       }
7099 +
7100 +       a->mnt.mnt_sb = sb;
7101 +       a->fake.br_perm = 0;
7102 +       a->fake.br_path.mnt = &a->mnt;
7103 +       a->fake.br_xino.xi_file = NULL;
7104 +       atomic_set(&a->fake.br_count, 0);
7105 +       smp_mb(); /* atomic_set */
7106 +       err = do_pri_br(-1, &a->fake);
7107 +       kfree(a);
7108 +       dpri("dev 0x%x\n", sb->s_dev);
7109 +       if (err || !au_test_aufs(sb))
7110 +               return;
7111 +
7112 +       sbinfo = au_sbi(sb);
7113 +       if (!sbinfo)
7114 +               return;
7115 +       dpri("nw %d, gen %u, kobj %d\n",
7116 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7117 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
7118 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
7119 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7120 +}
7121 +
7122 +/* ---------------------------------------------------------------------- */
7123 +
7124 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7125 +{
7126 +       struct inode *h_inode, *inode = dentry->d_inode;
7127 +       struct dentry *h_dentry;
7128 +       aufs_bindex_t bindex, bend, bi;
7129 +
7130 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7131 +               return;
7132 +
7133 +       bend = au_dbend(dentry);
7134 +       bi = au_ibend(inode);
7135 +       if (bi < bend)
7136 +               bend = bi;
7137 +       bindex = au_dbstart(dentry);
7138 +       bi = au_ibstart(inode);
7139 +       if (bi > bindex)
7140 +               bindex = bi;
7141 +
7142 +       for (; bindex <= bend; bindex++) {
7143 +               h_dentry = au_h_dptr(dentry, bindex);
7144 +               if (!h_dentry)
7145 +                       continue;
7146 +               h_inode = au_h_iptr(inode, bindex);
7147 +               if (unlikely(h_inode != h_dentry->d_inode)) {
7148 +                       au_debug_on();
7149 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7150 +                       AuDbgDentry(dentry);
7151 +                       AuDbgInode(inode);
7152 +                       au_debug_off();
7153 +                       BUG();
7154 +               }
7155 +       }
7156 +}
7157 +
7158 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7159 +{
7160 +       int err, i, j;
7161 +       struct au_dcsub_pages dpages;
7162 +       struct au_dpage *dpage;
7163 +       struct dentry **dentries;
7164 +
7165 +       err = au_dpages_init(&dpages, GFP_NOFS);
7166 +       AuDebugOn(err);
7167 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7168 +       AuDebugOn(err);
7169 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7170 +               dpage = dpages.dpages + i;
7171 +               dentries = dpage->dentries;
7172 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7173 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7174 +       }
7175 +       au_dpages_free(&dpages);
7176 +}
7177 +
7178 +void au_dbg_verify_kthread(void)
7179 +{
7180 +       if (au_wkq_test()) {
7181 +               au_dbg_blocked();
7182 +               /*
7183 +                * It may be recursive, but udba=notify between two aufs mounts,
7184 +                * where a single ro branch is shared, is not a problem.
7185 +                */
7186 +               /* WARN_ON(1); */
7187 +       }
7188 +}
7189 +
7190 +/* ---------------------------------------------------------------------- */
7191 +
7192 +int __init au_debug_init(void)
7193 +{
7194 +       aufs_bindex_t bindex;
7195 +       struct au_vdir_destr destr;
7196 +
7197 +       bindex = -1;
7198 +       AuDebugOn(bindex >= 0);
7199 +
7200 +       destr.len = -1;
7201 +       AuDebugOn(destr.len < NAME_MAX);
7202 +
7203 +#ifdef CONFIG_4KSTACKS
7204 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7205 +#endif
7206 +
7207 +       return 0;
7208 +}
7209 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7210 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7211 +++ linux/fs/aufs/debug.h       2015-06-22 08:27:37.877501704 +0200
7212 @@ -0,0 +1,228 @@
7213 +/*
7214 + * Copyright (C) 2005-2015 Junjiro R. Okajima
7215 + *
7216 + * This program, aufs is free software; you can redistribute it and/or modify
7217 + * it under the terms of the GNU General Public License as published by
7218 + * the Free Software Foundation; either version 2 of the License, or
7219 + * (at your option) any later version.
7220 + *
7221 + * This program is distributed in the hope that it will be useful,
7222 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7223 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7224 + * GNU General Public License for more details.
7225 + *
7226 + * You should have received a copy of the GNU General Public License
7227 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7228 + */
7229 +
7230 +/*
7231 + * debug print functions
7232 + */
7233 +
7234 +#ifndef __AUFS_DEBUG_H__
7235 +#define __AUFS_DEBUG_H__
7236 +
7237 +#ifdef __KERNEL__
7238 +
7239 +#include <linux/atomic.h>
7240 +#include <linux/module.h>
7241 +#include <linux/kallsyms.h>
7242 +#include <linux/sysrq.h>
7243 +
7244 +#ifdef CONFIG_AUFS_DEBUG
7245 +#define AuDebugOn(a)           BUG_ON(a)
7246 +
7247 +/* module parameter */
7248 +extern atomic_t aufs_debug;
7249 +static inline void au_debug_on(void)
7250 +{
7251 +       atomic_inc(&aufs_debug);
7252 +}
7253 +static inline void au_debug_off(void)
7254 +{
7255 +       atomic_dec_if_positive(&aufs_debug);
7256 +}
7257 +
7258 +static inline int au_debug_test(void)
7259 +{
7260 +       return atomic_read(&aufs_debug) > 0;
7261 +}
7262 +#else
7263 +#define AuDebugOn(a)           do {} while (0)
7264 +AuStubVoid(au_debug_on, void)
7265 +AuStubVoid(au_debug_off, void)
7266 +AuStubInt0(au_debug_test, void)
7267 +#endif /* CONFIG_AUFS_DEBUG */
7268 +
7269 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7270 +
7271 +/* ---------------------------------------------------------------------- */
7272 +
7273 +/* debug print */
7274 +
7275 +#define AuDbg(fmt, ...) do { \
7276 +       if (au_debug_test()) \
7277 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7278 +} while (0)
7279 +#define AuLabel(l)             AuDbg(#l "\n")
7280 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7281 +#define AuWarn1(fmt, ...) do { \
7282 +       static unsigned char _c; \
7283 +       if (!_c++) \
7284 +               pr_warn(fmt, ##__VA_ARGS__); \
7285 +} while (0)
7286 +
7287 +#define AuErr1(fmt, ...) do { \
7288 +       static unsigned char _c; \
7289 +       if (!_c++) \
7290 +               pr_err(fmt, ##__VA_ARGS__); \
7291 +} while (0)
7292 +
7293 +#define AuIOErr1(fmt, ...) do { \
7294 +       static unsigned char _c; \
7295 +       if (!_c++) \
7296 +               AuIOErr(fmt, ##__VA_ARGS__); \
7297 +} while (0)
7298 +
7299 +#define AuUnsupportMsg "This operation is not supported." \
7300 +                       " Please report this application to aufs-users ML."
7301 +#define AuUnsupport(fmt, ...) do { \
7302 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7303 +       dump_stack(); \
7304 +} while (0)
7305 +
7306 +#define AuTraceErr(e) do { \
7307 +       if (unlikely((e) < 0)) \
7308 +               AuDbg("err %d\n", (int)(e)); \
7309 +} while (0)
7310 +
7311 +#define AuTraceErrPtr(p) do { \
7312 +       if (IS_ERR(p)) \
7313 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7314 +} while (0)
7315 +
7316 +/* dirty macros for debug print, use with "%.*s" and caution */
7317 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7318 +
7319 +/* ---------------------------------------------------------------------- */
7320 +
7321 +struct dentry;
7322 +#ifdef CONFIG_AUFS_DEBUG
7323 +extern struct mutex au_dbg_mtx;
7324 +extern char *au_plevel;
7325 +struct au_nhash;
7326 +void au_dpri_whlist(struct au_nhash *whlist);
7327 +struct au_vdir;
7328 +void au_dpri_vdir(struct au_vdir *vdir);
7329 +struct inode;
7330 +void au_dpri_inode(struct inode *inode);
7331 +void au_dpri_dalias(struct inode *inode);
7332 +void au_dpri_dentry(struct dentry *dentry);
7333 +struct file;
7334 +void au_dpri_file(struct file *filp);
7335 +struct super_block;
7336 +void au_dpri_sb(struct super_block *sb);
7337 +
7338 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7339 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7340 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7341 +void au_dbg_verify_kthread(void);
7342 +
7343 +int __init au_debug_init(void);
7344 +
7345 +#define AuDbgWhlist(w) do { \
7346 +       mutex_lock(&au_dbg_mtx); \
7347 +       AuDbg(#w "\n"); \
7348 +       au_dpri_whlist(w); \
7349 +       mutex_unlock(&au_dbg_mtx); \
7350 +} while (0)
7351 +
7352 +#define AuDbgVdir(v) do { \
7353 +       mutex_lock(&au_dbg_mtx); \
7354 +       AuDbg(#v "\n"); \
7355 +       au_dpri_vdir(v); \
7356 +       mutex_unlock(&au_dbg_mtx); \
7357 +} while (0)
7358 +
7359 +#define AuDbgInode(i) do { \
7360 +       mutex_lock(&au_dbg_mtx); \
7361 +       AuDbg(#i "\n"); \
7362 +       au_dpri_inode(i); \
7363 +       mutex_unlock(&au_dbg_mtx); \
7364 +} while (0)
7365 +
7366 +#define AuDbgDAlias(i) do { \
7367 +       mutex_lock(&au_dbg_mtx); \
7368 +       AuDbg(#i "\n"); \
7369 +       au_dpri_dalias(i); \
7370 +       mutex_unlock(&au_dbg_mtx); \
7371 +} while (0)
7372 +
7373 +#define AuDbgDentry(d) do { \
7374 +       mutex_lock(&au_dbg_mtx); \
7375 +       AuDbg(#d "\n"); \
7376 +       au_dpri_dentry(d); \
7377 +       mutex_unlock(&au_dbg_mtx); \
7378 +} while (0)
7379 +
7380 +#define AuDbgFile(f) do { \
7381 +       mutex_lock(&au_dbg_mtx); \
7382 +       AuDbg(#f "\n"); \
7383 +       au_dpri_file(f); \
7384 +       mutex_unlock(&au_dbg_mtx); \
7385 +} while (0)
7386 +
7387 +#define AuDbgSb(sb) do { \
7388 +       mutex_lock(&au_dbg_mtx); \
7389 +       AuDbg(#sb "\n"); \
7390 +       au_dpri_sb(sb); \
7391 +       mutex_unlock(&au_dbg_mtx); \
7392 +} while (0)
7393 +
7394 +#define AuDbgSym(addr) do {                            \
7395 +       char sym[KSYM_SYMBOL_LEN];                      \
7396 +       sprint_symbol(sym, (unsigned long)addr);        \
7397 +       AuDbg("%s\n", sym);                             \
7398 +} while (0)
7399 +#else
7400 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7401 +AuStubVoid(au_dbg_verify_dir_parent, struct dentry *dentry, unsigned int sigen)
7402 +AuStubVoid(au_dbg_verify_nondir_parent, struct dentry *dentry,
7403 +          unsigned int sigen)
7404 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7405 +AuStubVoid(au_dbg_verify_kthread, void)
7406 +AuStubInt0(__init au_debug_init, void)
7407 +
7408 +#define AuDbgWhlist(w)         do {} while (0)
7409 +#define AuDbgVdir(v)           do {} while (0)
7410 +#define AuDbgInode(i)          do {} while (0)
7411 +#define AuDbgDAlias(i)         do {} while (0)
7412 +#define AuDbgDentry(d)         do {} while (0)
7413 +#define AuDbgFile(f)           do {} while (0)
7414 +#define AuDbgSb(sb)            do {} while (0)
7415 +#define AuDbgSym(addr)         do {} while (0)
7416 +#endif /* CONFIG_AUFS_DEBUG */
7417 +
7418 +/* ---------------------------------------------------------------------- */
7419 +
7420 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7421 +int __init au_sysrq_init(void);
7422 +void au_sysrq_fin(void);
7423 +
7424 +#ifdef CONFIG_HW_CONSOLE
7425 +#define au_dbg_blocked() do { \
7426 +       WARN_ON(1); \
7427 +       handle_sysrq('w'); \
7428 +} while (0)
7429 +#else
7430 +AuStubVoid(au_dbg_blocked, void)
7431 +#endif
7432 +
7433 +#else
7434 +AuStubInt0(__init au_sysrq_init, void)
7435 +AuStubVoid(au_sysrq_fin, void)
7436 +AuStubVoid(au_dbg_blocked, void)
7437 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7438 +
7439 +#endif /* __KERNEL__ */
7440 +#endif /* __AUFS_DEBUG_H__ */
7441 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7442 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7443 +++ linux/fs/aufs/dentry.c      2015-06-22 08:27:37.877501704 +0200
7444 @@ -0,0 +1,1097 @@
7445 +/*
7446 + * Copyright (C) 2005-2015 Junjiro R. Okajima
7447 + *
7448 + * This program, aufs is free software; you can redistribute it and/or modify
7449 + * it under the terms of the GNU General Public License as published by
7450 + * the Free Software Foundation; either version 2 of the License, or
7451 + * (at your option) any later version.
7452 + *
7453 + * This program is distributed in the hope that it will be useful,
7454 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7455 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7456 + * GNU General Public License for more details.
7457 + *
7458 + * You should have received a copy of the GNU General Public License
7459 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7460 + */
7461 +
7462 +/*
7463 + * lookup and dentry operations
7464 + */
7465 +
7466 +#include <linux/namei.h>
7467 +#include "aufs.h"
7468 +
7469 +#define AuLkup_ALLOW_NEG       1
7470 +#define AuLkup_IGNORE_PERM     (1 << 1)
7471 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
7472 +#define au_fset_lkup(flags, name) \
7473 +       do { (flags) |= AuLkup_##name; } while (0)
7474 +#define au_fclr_lkup(flags, name) \
7475 +       do { (flags) &= ~AuLkup_##name; } while (0)
7476 +
7477 +struct au_do_lookup_args {
7478 +       unsigned int            flags;
7479 +       mode_t                  type;
7480 +};
7481 +
7482 +/*
7483 + * returns positive/negative dentry, NULL or an error.
7484 + * NULL means whiteout-ed or not-found.
7485 + */
7486 +static struct dentry*
7487 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7488 +            aufs_bindex_t bindex, struct qstr *wh_name,
7489 +            struct au_do_lookup_args *args)
7490 +{
7491 +       struct dentry *h_dentry;
7492 +       struct inode *h_inode;
7493 +       struct au_branch *br;
7494 +       int wh_found, opq;
7495 +       unsigned char wh_able;
7496 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7497 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7498 +                                                         IGNORE_PERM);
7499 +
7500 +       wh_found = 0;
7501 +       br = au_sbr(dentry->d_sb, bindex);
7502 +       wh_able = !!au_br_whable(br->br_perm);
7503 +       if (wh_able)
7504 +               wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
7505 +       h_dentry = ERR_PTR(wh_found);
7506 +       if (!wh_found)
7507 +               goto real_lookup;
7508 +       if (unlikely(wh_found < 0))
7509 +               goto out;
7510 +
7511 +       /* We found a whiteout */
7512 +       /* au_set_dbend(dentry, bindex); */
7513 +       au_set_dbwh(dentry, bindex);
7514 +       if (!allow_neg)
7515 +               return NULL; /* success */
7516 +
7517 +real_lookup:
7518 +       if (!ignore_perm)
7519 +               h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
7520 +       else
7521 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7522 +       if (IS_ERR(h_dentry)) {
7523 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7524 +                   && !allow_neg)
7525 +                       h_dentry = NULL;
7526 +               goto out;
7527 +       }
7528 +
7529 +       h_inode = h_dentry->d_inode;
7530 +       if (!h_inode) {
7531 +               if (!allow_neg)
7532 +                       goto out_neg;
7533 +       } else if (wh_found
7534 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7535 +               goto out_neg;
7536 +
7537 +       if (au_dbend(dentry) <= bindex)
7538 +               au_set_dbend(dentry, bindex);
7539 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7540 +               au_set_dbstart(dentry, bindex);
7541 +       au_set_h_dptr(dentry, bindex, h_dentry);
7542 +
7543 +       if (!d_is_dir(h_dentry)
7544 +           || !wh_able
7545 +           || (d_is_positive(dentry) && !d_is_dir(dentry)))
7546 +               goto out; /* success */
7547 +
7548 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
7549 +       opq = au_diropq_test(h_dentry);
7550 +       mutex_unlock(&h_inode->i_mutex);
7551 +       if (opq > 0)
7552 +               au_set_dbdiropq(dentry, bindex);
7553 +       else if (unlikely(opq < 0)) {
7554 +               au_set_h_dptr(dentry, bindex, NULL);
7555 +               h_dentry = ERR_PTR(opq);
7556 +       }
7557 +       goto out;
7558 +
7559 +out_neg:
7560 +       dput(h_dentry);
7561 +       h_dentry = NULL;
7562 +out:
7563 +       return h_dentry;
7564 +}
7565 +
7566 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7567 +{
7568 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7569 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7570 +               return -EPERM;
7571 +       return 0;
7572 +}
7573 +
7574 +/*
7575 + * returns the number of lower positive dentries,
7576 + * otherwise an error.
7577 + * can be called at unlinking with @type is zero.
7578 + */
7579 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
7580 +{
7581 +       int npositive, err;
7582 +       aufs_bindex_t bindex, btail, bdiropq;
7583 +       unsigned char isdir, dirperm1;
7584 +       struct qstr whname;
7585 +       struct au_do_lookup_args args = {
7586 +               .flags          = 0,
7587 +               .type           = type
7588 +       };
7589 +       const struct qstr *name = &dentry->d_name;
7590 +       struct dentry *parent;
7591 +       struct inode *inode;
7592 +       struct super_block *sb;
7593 +
7594 +       sb = dentry->d_sb;
7595 +       err = au_test_shwh(sb, name);
7596 +       if (unlikely(err))
7597 +               goto out;
7598 +
7599 +       err = au_wh_name_alloc(&whname, name);
7600 +       if (unlikely(err))
7601 +               goto out;
7602 +
7603 +       inode = dentry->d_inode;
7604 +       isdir = !!d_is_dir(dentry);
7605 +       if (!type)
7606 +               au_fset_lkup(args.flags, ALLOW_NEG);
7607 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
7608 +
7609 +       npositive = 0;
7610 +       parent = dget_parent(dentry);
7611 +       btail = au_dbtaildir(parent);
7612 +       for (bindex = bstart; bindex <= btail; bindex++) {
7613 +               struct dentry *h_parent, *h_dentry;
7614 +               struct inode *h_inode, *h_dir;
7615 +
7616 +               h_dentry = au_h_dptr(dentry, bindex);
7617 +               if (h_dentry) {
7618 +                       if (h_dentry->d_inode)
7619 +                               npositive++;
7620 +                       if (type != S_IFDIR)
7621 +                               break;
7622 +                       continue;
7623 +               }
7624 +               h_parent = au_h_dptr(parent, bindex);
7625 +               if (!h_parent || !d_is_dir(h_parent))
7626 +                       continue;
7627 +
7628 +               h_dir = h_parent->d_inode;
7629 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
7630 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7631 +                                       &args);
7632 +               mutex_unlock(&h_dir->i_mutex);
7633 +               err = PTR_ERR(h_dentry);
7634 +               if (IS_ERR(h_dentry))
7635 +                       goto out_parent;
7636 +               if (h_dentry)
7637 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
7638 +               if (dirperm1)
7639 +                       au_fset_lkup(args.flags, IGNORE_PERM);
7640 +
7641 +               if (au_dbwh(dentry) >= 0)
7642 +                       break;
7643 +               if (!h_dentry)
7644 +                       continue;
7645 +               h_inode = h_dentry->d_inode;
7646 +               if (!h_inode)
7647 +                       continue;
7648 +               npositive++;
7649 +               if (!args.type)
7650 +                       args.type = h_inode->i_mode & S_IFMT;
7651 +               if (args.type != S_IFDIR)
7652 +                       break;
7653 +               else if (isdir) {
7654 +                       /* the type of lower may be different */
7655 +                       bdiropq = au_dbdiropq(dentry);
7656 +                       if (bdiropq >= 0 && bdiropq <= bindex)
7657 +                               break;
7658 +               }
7659 +       }
7660 +
7661 +       if (npositive) {
7662 +               AuLabel(positive);
7663 +               au_update_dbstart(dentry);
7664 +       }
7665 +       err = npositive;
7666 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
7667 +                    && au_dbstart(dentry) < 0)) {
7668 +               err = -EIO;
7669 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7670 +                       dentry, err);
7671 +       }
7672 +
7673 +out_parent:
7674 +       dput(parent);
7675 +       kfree(whname.name);
7676 +out:
7677 +       return err;
7678 +}
7679 +
7680 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
7681 +{
7682 +       struct dentry *dentry;
7683 +       int wkq_err;
7684 +
7685 +       if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
7686 +               dentry = vfsub_lkup_one(name, parent);
7687 +       else {
7688 +               struct vfsub_lkup_one_args args = {
7689 +                       .errp   = &dentry,
7690 +                       .name   = name,
7691 +                       .parent = parent
7692 +               };
7693 +
7694 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
7695 +               if (unlikely(wkq_err))
7696 +                       dentry = ERR_PTR(wkq_err);
7697 +       }
7698 +
7699 +       return dentry;
7700 +}
7701 +
7702 +/*
7703 + * lookup @dentry on @bindex which should be negative.
7704 + */
7705 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
7706 +{
7707 +       int err;
7708 +       struct dentry *parent, *h_parent, *h_dentry;
7709 +       struct au_branch *br;
7710 +
7711 +       parent = dget_parent(dentry);
7712 +       h_parent = au_h_dptr(parent, bindex);
7713 +       br = au_sbr(dentry->d_sb, bindex);
7714 +       if (wh)
7715 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
7716 +       else
7717 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7718 +       err = PTR_ERR(h_dentry);
7719 +       if (IS_ERR(h_dentry))
7720 +               goto out;
7721 +       if (unlikely(h_dentry->d_inode)) {
7722 +               err = -EIO;
7723 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
7724 +               dput(h_dentry);
7725 +               goto out;
7726 +       }
7727 +
7728 +       err = 0;
7729 +       if (bindex < au_dbstart(dentry))
7730 +               au_set_dbstart(dentry, bindex);
7731 +       if (au_dbend(dentry) < bindex)
7732 +               au_set_dbend(dentry, bindex);
7733 +       au_set_h_dptr(dentry, bindex, h_dentry);
7734 +
7735 +out:
7736 +       dput(parent);
7737 +       return err;
7738 +}
7739 +
7740 +/* ---------------------------------------------------------------------- */
7741 +
7742 +/* subset of struct inode */
7743 +struct au_iattr {
7744 +       unsigned long           i_ino;
7745 +       /* unsigned int         i_nlink; */
7746 +       kuid_t                  i_uid;
7747 +       kgid_t                  i_gid;
7748 +       u64                     i_version;
7749 +/*
7750 +       loff_t                  i_size;
7751 +       blkcnt_t                i_blocks;
7752 +*/
7753 +       umode_t                 i_mode;
7754 +};
7755 +
7756 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7757 +{
7758 +       ia->i_ino = h_inode->i_ino;
7759 +       /* ia->i_nlink = h_inode->i_nlink; */
7760 +       ia->i_uid = h_inode->i_uid;
7761 +       ia->i_gid = h_inode->i_gid;
7762 +       ia->i_version = h_inode->i_version;
7763 +/*
7764 +       ia->i_size = h_inode->i_size;
7765 +       ia->i_blocks = h_inode->i_blocks;
7766 +*/
7767 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
7768 +}
7769 +
7770 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
7771 +{
7772 +       return ia->i_ino != h_inode->i_ino
7773 +               /* || ia->i_nlink != h_inode->i_nlink */
7774 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
7775 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
7776 +               || ia->i_version != h_inode->i_version
7777 +/*
7778 +               || ia->i_size != h_inode->i_size
7779 +               || ia->i_blocks != h_inode->i_blocks
7780 +*/
7781 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
7782 +}
7783 +
7784 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7785 +                             struct au_branch *br)
7786 +{
7787 +       int err;
7788 +       struct au_iattr ia;
7789 +       struct inode *h_inode;
7790 +       struct dentry *h_d;
7791 +       struct super_block *h_sb;
7792 +
7793 +       err = 0;
7794 +       memset(&ia, -1, sizeof(ia));
7795 +       h_sb = h_dentry->d_sb;
7796 +       h_inode = h_dentry->d_inode;
7797 +       if (h_inode)
7798 +               au_iattr_save(&ia, h_inode);
7799 +       else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
7800 +               /* nfs d_revalidate may return 0 for negative dentry */
7801 +               /* fuse d_revalidate always return 0 for negative dentry */
7802 +               goto out;
7803 +
7804 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
7805 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
7806 +       err = PTR_ERR(h_d);
7807 +       if (IS_ERR(h_d))
7808 +               goto out;
7809 +
7810 +       err = 0;
7811 +       if (unlikely(h_d != h_dentry
7812 +                    || h_d->d_inode != h_inode
7813 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
7814 +               err = au_busy_or_stale();
7815 +       dput(h_d);
7816 +
7817 +out:
7818 +       AuTraceErr(err);
7819 +       return err;
7820 +}
7821 +
7822 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7823 +               struct dentry *h_parent, struct au_branch *br)
7824 +{
7825 +       int err;
7826 +
7827 +       err = 0;
7828 +       if (udba == AuOpt_UDBA_REVAL
7829 +           && !au_test_fs_remote(h_dentry->d_sb)) {
7830 +               IMustLock(h_dir);
7831 +               err = (h_dentry->d_parent->d_inode != h_dir);
7832 +       } else if (udba != AuOpt_UDBA_NONE)
7833 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
7834 +
7835 +       return err;
7836 +}
7837 +
7838 +/* ---------------------------------------------------------------------- */
7839 +
7840 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
7841 +{
7842 +       int err;
7843 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
7844 +       struct au_hdentry tmp, *p, *q;
7845 +       struct au_dinfo *dinfo;
7846 +       struct super_block *sb;
7847 +
7848 +       DiMustWriteLock(dentry);
7849 +
7850 +       sb = dentry->d_sb;
7851 +       dinfo = au_di(dentry);
7852 +       bend = dinfo->di_bend;
7853 +       bwh = dinfo->di_bwh;
7854 +       bdiropq = dinfo->di_bdiropq;
7855 +       p = dinfo->di_hdentry + dinfo->di_bstart;
7856 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
7857 +               if (!p->hd_dentry)
7858 +                       continue;
7859 +
7860 +               new_bindex = au_br_index(sb, p->hd_id);
7861 +               if (new_bindex == bindex)
7862 +                       continue;
7863 +
7864 +               if (dinfo->di_bwh == bindex)
7865 +                       bwh = new_bindex;
7866 +               if (dinfo->di_bdiropq == bindex)
7867 +                       bdiropq = new_bindex;
7868 +               if (new_bindex < 0) {
7869 +                       au_hdput(p);
7870 +                       p->hd_dentry = NULL;
7871 +                       continue;
7872 +               }
7873 +
7874 +               /* swap two lower dentries, and loop again */
7875 +               q = dinfo->di_hdentry + new_bindex;
7876 +               tmp = *q;
7877 +               *q = *p;
7878 +               *p = tmp;
7879 +               if (tmp.hd_dentry) {
7880 +                       bindex--;
7881 +                       p--;
7882 +               }
7883 +       }
7884 +
7885 +       dinfo->di_bwh = -1;
7886 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
7887 +               dinfo->di_bwh = bwh;
7888 +
7889 +       dinfo->di_bdiropq = -1;
7890 +       if (bdiropq >= 0
7891 +           && bdiropq <= au_sbend(sb)
7892 +           && au_sbr_whable(sb, bdiropq))
7893 +               dinfo->di_bdiropq = bdiropq;
7894 +
7895 +       err = -EIO;
7896 +       dinfo->di_bstart = -1;
7897 +       dinfo->di_bend = -1;
7898 +       bend = au_dbend(parent);
7899 +       p = dinfo->di_hdentry;
7900 +       for (bindex = 0; bindex <= bend; bindex++, p++)
7901 +               if (p->hd_dentry) {
7902 +                       dinfo->di_bstart = bindex;
7903 +                       break;
7904 +               }
7905 +
7906 +       if (dinfo->di_bstart >= 0) {
7907 +               p = dinfo->di_hdentry + bend;
7908 +               for (bindex = bend; bindex >= 0; bindex--, p--)
7909 +                       if (p->hd_dentry) {
7910 +                               dinfo->di_bend = bindex;
7911 +                               err = 0;
7912 +                               break;
7913 +                       }
7914 +       }
7915 +
7916 +       return err;
7917 +}
7918 +
7919 +static void au_do_hide(struct dentry *dentry)
7920 +{
7921 +       struct inode *inode;
7922 +
7923 +       inode = dentry->d_inode;
7924 +       if (inode) {
7925 +               if (!S_ISDIR(inode->i_mode)) {
7926 +                       if (inode->i_nlink && !d_unhashed(dentry))
7927 +                               drop_nlink(inode);
7928 +               } else {
7929 +                       clear_nlink(inode);
7930 +                       /* stop next lookup */
7931 +                       inode->i_flags |= S_DEAD;
7932 +               }
7933 +               smp_mb(); /* necessary? */
7934 +       }
7935 +       d_drop(dentry);
7936 +}
7937 +
7938 +static int au_hide_children(struct dentry *parent)
7939 +{
7940 +       int err, i, j, ndentry;
7941 +       struct au_dcsub_pages dpages;
7942 +       struct au_dpage *dpage;
7943 +       struct dentry *dentry;
7944 +
7945 +       err = au_dpages_init(&dpages, GFP_NOFS);
7946 +       if (unlikely(err))
7947 +               goto out;
7948 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
7949 +       if (unlikely(err))
7950 +               goto out_dpages;
7951 +
7952 +       /* in reverse order */
7953 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
7954 +               dpage = dpages.dpages + i;
7955 +               ndentry = dpage->ndentry;
7956 +               for (j = ndentry - 1; j >= 0; j--) {
7957 +                       dentry = dpage->dentries[j];
7958 +                       if (dentry != parent)
7959 +                               au_do_hide(dentry);
7960 +               }
7961 +       }
7962 +
7963 +out_dpages:
7964 +       au_dpages_free(&dpages);
7965 +out:
7966 +       return err;
7967 +}
7968 +
7969 +static void au_hide(struct dentry *dentry)
7970 +{
7971 +       int err;
7972 +
7973 +       AuDbgDentry(dentry);
7974 +       if (d_is_dir(dentry)) {
7975 +               /* shrink_dcache_parent(dentry); */
7976 +               err = au_hide_children(dentry);
7977 +               if (unlikely(err))
7978 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
7979 +                               dentry, err);
7980 +       }
7981 +       au_do_hide(dentry);
7982 +}
7983 +
7984 +/*
7985 + * By adding a dirty branch, a cached dentry may be affected in various ways.
7986 + *
7987 + * a dirty branch is added
7988 + * - on the top of layers
7989 + * - in the middle of layers
7990 + * - to the bottom of layers
7991 + *
7992 + * on the added branch there exists
7993 + * - a whiteout
7994 + * - a diropq
7995 + * - a same named entry
7996 + *   + exist
7997 + *     * negative --> positive
7998 + *     * positive --> positive
7999 + *      - type is unchanged
8000 + *      - type is changed
8001 + *   + doesn't exist
8002 + *     * negative --> negative
8003 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8004 + * - none
8005 + */
8006 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8007 +                              struct au_dinfo *tmp)
8008 +{
8009 +       int err;
8010 +       aufs_bindex_t bindex, bend;
8011 +       struct {
8012 +               struct dentry *dentry;
8013 +               struct inode *inode;
8014 +               mode_t mode;
8015 +       } orig_h, tmp_h;
8016 +       struct au_hdentry *hd;
8017 +       struct inode *inode, *h_inode;
8018 +       struct dentry *h_dentry;
8019 +
8020 +       err = 0;
8021 +       AuDebugOn(dinfo->di_bstart < 0);
8022 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
8023 +       orig_h.inode = orig_h.dentry->d_inode;
8024 +       orig_h.mode = 0;
8025 +       if (orig_h.inode)
8026 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8027 +       memset(&tmp_h, 0, sizeof(tmp_h));
8028 +       if (tmp->di_bstart >= 0) {
8029 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
8030 +               tmp_h.inode = tmp_h.dentry->d_inode;
8031 +               if (tmp_h.inode)
8032 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8033 +       }
8034 +
8035 +       inode = dentry->d_inode;
8036 +       if (!orig_h.inode) {
8037 +               AuDbg("nagative originally\n");
8038 +               if (inode) {
8039 +                       au_hide(dentry);
8040 +                       goto out;
8041 +               }
8042 +               AuDebugOn(inode);
8043 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
8044 +               AuDebugOn(dinfo->di_bdiropq != -1);
8045 +
8046 +               if (!tmp_h.inode) {
8047 +                       AuDbg("negative --> negative\n");
8048 +                       /* should have only one negative lower */
8049 +                       if (tmp->di_bstart >= 0
8050 +                           && tmp->di_bstart < dinfo->di_bstart) {
8051 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
8052 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
8053 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
8054 +                               au_di_cp(dinfo, tmp);
8055 +                               hd = tmp->di_hdentry + tmp->di_bstart;
8056 +                               au_set_h_dptr(dentry, tmp->di_bstart,
8057 +                                             dget(hd->hd_dentry));
8058 +                       }
8059 +                       au_dbg_verify_dinode(dentry);
8060 +               } else {
8061 +                       AuDbg("negative --> positive\n");
8062 +                       /*
8063 +                        * similar to the behaviour of creating with bypassing
8064 +                        * aufs.
8065 +                        * unhash it in order to force an error in the
8066 +                        * succeeding create operation.
8067 +                        * we should not set S_DEAD here.
8068 +                        */
8069 +                       d_drop(dentry);
8070 +                       /* au_di_swap(tmp, dinfo); */
8071 +                       au_dbg_verify_dinode(dentry);
8072 +               }
8073 +       } else {
8074 +               AuDbg("positive originally\n");
8075 +               /* inode may be NULL */
8076 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8077 +               if (!tmp_h.inode) {
8078 +                       AuDbg("positive --> negative\n");
8079 +                       /* or bypassing aufs */
8080 +                       au_hide(dentry);
8081 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
8082 +                               dinfo->di_bwh = tmp->di_bwh;
8083 +                       if (inode)
8084 +                               err = au_refresh_hinode_self(inode);
8085 +                       au_dbg_verify_dinode(dentry);
8086 +               } else if (orig_h.mode == tmp_h.mode) {
8087 +                       AuDbg("positive --> positive, same type\n");
8088 +                       if (!S_ISDIR(orig_h.mode)
8089 +                           && dinfo->di_bstart > tmp->di_bstart) {
8090 +                               /*
8091 +                                * similar to the behaviour of removing and
8092 +                                * creating.
8093 +                                */
8094 +                               au_hide(dentry);
8095 +                               if (inode)
8096 +                                       err = au_refresh_hinode_self(inode);
8097 +                               au_dbg_verify_dinode(dentry);
8098 +                       } else {
8099 +                               /* fill empty slots */
8100 +                               if (dinfo->di_bstart > tmp->di_bstart)
8101 +                                       dinfo->di_bstart = tmp->di_bstart;
8102 +                               if (dinfo->di_bend < tmp->di_bend)
8103 +                                       dinfo->di_bend = tmp->di_bend;
8104 +                               dinfo->di_bwh = tmp->di_bwh;
8105 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8106 +                               hd = tmp->di_hdentry;
8107 +                               bend = dinfo->di_bend;
8108 +                               for (bindex = tmp->di_bstart; bindex <= bend;
8109 +                                    bindex++) {
8110 +                                       if (au_h_dptr(dentry, bindex))
8111 +                                               continue;
8112 +                                       h_dentry = hd[bindex].hd_dentry;
8113 +                                       if (!h_dentry)
8114 +                                               continue;
8115 +                                       h_inode = h_dentry->d_inode;
8116 +                                       AuDebugOn(!h_inode);
8117 +                                       AuDebugOn(orig_h.mode
8118 +                                                 != (h_inode->i_mode
8119 +                                                     & S_IFMT));
8120 +                                       au_set_h_dptr(dentry, bindex,
8121 +                                                     dget(h_dentry));
8122 +                               }
8123 +                               err = au_refresh_hinode(inode, dentry);
8124 +                               au_dbg_verify_dinode(dentry);
8125 +                       }
8126 +               } else {
8127 +                       AuDbg("positive --> positive, different type\n");
8128 +                       /* similar to the behaviour of removing and creating */
8129 +                       au_hide(dentry);
8130 +                       if (inode)
8131 +                               err = au_refresh_hinode_self(inode);
8132 +                       au_dbg_verify_dinode(dentry);
8133 +               }
8134 +       }
8135 +
8136 +out:
8137 +       return err;
8138 +}
8139 +
8140 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8141 +{
8142 +       int err, ebrange;
8143 +       unsigned int sigen;
8144 +       struct au_dinfo *dinfo, *tmp;
8145 +       struct super_block *sb;
8146 +       struct inode *inode;
8147 +
8148 +       DiMustWriteLock(dentry);
8149 +       AuDebugOn(IS_ROOT(dentry));
8150 +       AuDebugOn(!parent->d_inode);
8151 +
8152 +       sb = dentry->d_sb;
8153 +       inode = dentry->d_inode;
8154 +       sigen = au_sigen(sb);
8155 +       err = au_digen_test(parent, sigen);
8156 +       if (unlikely(err))
8157 +               goto out;
8158 +
8159 +       dinfo = au_di(dentry);
8160 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
8161 +       if (unlikely(err))
8162 +               goto out;
8163 +       ebrange = au_dbrange_test(dentry);
8164 +       if (!ebrange)
8165 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8166 +
8167 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8168 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
8169 +               if (inode)
8170 +                       err = au_refresh_hinode_self(inode);
8171 +               au_dbg_verify_dinode(dentry);
8172 +               if (!err)
8173 +                       goto out_dgen; /* success */
8174 +               goto out;
8175 +       }
8176 +
8177 +       /* temporary dinfo */
8178 +       AuDbgDentry(dentry);
8179 +       err = -ENOMEM;
8180 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8181 +       if (unlikely(!tmp))
8182 +               goto out;
8183 +       au_di_swap(tmp, dinfo);
8184 +       /* returns the number of positive dentries */
8185 +       /*
8186 +        * if current working dir is removed, it returns an error.
8187 +        * but the dentry is legal.
8188 +        */
8189 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
8190 +       AuDbgDentry(dentry);
8191 +       au_di_swap(tmp, dinfo);
8192 +       if (err == -ENOENT)
8193 +               err = 0;
8194 +       if (err >= 0) {
8195 +               /* compare/refresh by dinfo */
8196 +               AuDbgDentry(dentry);
8197 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8198 +               au_dbg_verify_dinode(dentry);
8199 +               AuTraceErr(err);
8200 +       }
8201 +       au_rw_write_unlock(&tmp->di_rwsem);
8202 +       au_di_free(tmp);
8203 +       if (unlikely(err))
8204 +               goto out;
8205 +
8206 +out_dgen:
8207 +       au_update_digen(dentry);
8208 +out:
8209 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8210 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8211 +               AuDbgDentry(dentry);
8212 +       }
8213 +       AuTraceErr(err);
8214 +       return err;
8215 +}
8216 +
8217 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8218 +                          struct dentry *dentry, aufs_bindex_t bindex)
8219 +{
8220 +       int err, valid;
8221 +
8222 +       err = 0;
8223 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8224 +               goto out;
8225 +
8226 +       AuDbg("b%d\n", bindex);
8227 +       /*
8228 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8229 +        * due to whiteout and branch permission.
8230 +        */
8231 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8232 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8233 +       /* it may return tri-state */
8234 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8235 +
8236 +       if (unlikely(valid < 0))
8237 +               err = valid;
8238 +       else if (!valid)
8239 +               err = -EINVAL;
8240 +
8241 +out:
8242 +       AuTraceErr(err);
8243 +       return err;
8244 +}
8245 +
8246 +/* todo: remove this */
8247 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8248 +                         unsigned int flags, int do_udba)
8249 +{
8250 +       int err;
8251 +       umode_t mode, h_mode;
8252 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
8253 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8254 +       struct inode *h_inode, *h_cached_inode;
8255 +       struct dentry *h_dentry;
8256 +       struct qstr *name, *h_name;
8257 +
8258 +       err = 0;
8259 +       plus = 0;
8260 +       mode = 0;
8261 +       ibs = -1;
8262 +       ibe = -1;
8263 +       unhashed = !!d_unhashed(dentry);
8264 +       is_root = !!IS_ROOT(dentry);
8265 +       name = &dentry->d_name;
8266 +       tmpfile = au_di(dentry)->di_tmpfile;
8267 +
8268 +       /*
8269 +        * Theoretically, REVAL test should be unnecessary in case of
8270 +        * {FS,I}NOTIFY.
8271 +        * But {fs,i}notify doesn't fire some necessary events,
8272 +        *      IN_ATTRIB for atime/nlink/pageio
8273 +        * Let's do REVAL test too.
8274 +        */
8275 +       if (do_udba && inode) {
8276 +               mode = (inode->i_mode & S_IFMT);
8277 +               plus = (inode->i_nlink > 0);
8278 +               ibs = au_ibstart(inode);
8279 +               ibe = au_ibend(inode);
8280 +       }
8281 +
8282 +       bstart = au_dbstart(dentry);
8283 +       btail = bstart;
8284 +       if (inode && S_ISDIR(inode->i_mode))
8285 +               btail = au_dbtaildir(dentry);
8286 +       for (bindex = bstart; bindex <= btail; bindex++) {
8287 +               h_dentry = au_h_dptr(dentry, bindex);
8288 +               if (!h_dentry)
8289 +                       continue;
8290 +
8291 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8292 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8293 +               spin_lock(&h_dentry->d_lock);
8294 +               h_name = &h_dentry->d_name;
8295 +               if (unlikely(do_udba
8296 +                            && !is_root
8297 +                            && ((!h_nfs
8298 +                                 && (unhashed != !!d_unhashed(h_dentry)
8299 +                                     || (!tmpfile
8300 +                                         && !au_qstreq(name, h_name))
8301 +                                         ))
8302 +                                || (h_nfs
8303 +                                    && !(flags & LOOKUP_OPEN)
8304 +                                    && (h_dentry->d_flags
8305 +                                        & DCACHE_NFSFS_RENAMED)))
8306 +                           )) {
8307 +                       int h_unhashed;
8308 +
8309 +                       h_unhashed = d_unhashed(h_dentry);
8310 +                       spin_unlock(&h_dentry->d_lock);
8311 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8312 +                             unhashed, h_unhashed, dentry, h_dentry);
8313 +                       goto err;
8314 +               }
8315 +               spin_unlock(&h_dentry->d_lock);
8316 +
8317 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8318 +               if (unlikely(err))
8319 +                       /* do not goto err, to keep the errno */
8320 +                       break;
8321 +
8322 +               /* todo: plink too? */
8323 +               if (!do_udba)
8324 +                       continue;
8325 +
8326 +               /* UDBA tests */
8327 +               h_inode = h_dentry->d_inode;
8328 +               if (unlikely(!!inode != !!h_inode))
8329 +                       goto err;
8330 +
8331 +               h_plus = plus;
8332 +               h_mode = mode;
8333 +               h_cached_inode = h_inode;
8334 +               if (h_inode) {
8335 +                       h_mode = (h_inode->i_mode & S_IFMT);
8336 +                       h_plus = (h_inode->i_nlink > 0);
8337 +               }
8338 +               if (inode && ibs <= bindex && bindex <= ibe)
8339 +                       h_cached_inode = au_h_iptr(inode, bindex);
8340 +
8341 +               if (!h_nfs) {
8342 +                       if (unlikely(plus != h_plus && !tmpfile))
8343 +                               goto err;
8344 +               } else {
8345 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8346 +                                    && !is_root
8347 +                                    && !IS_ROOT(h_dentry)
8348 +                                    && unhashed != d_unhashed(h_dentry)))
8349 +                               goto err;
8350 +               }
8351 +               if (unlikely(mode != h_mode
8352 +                            || h_cached_inode != h_inode))
8353 +                       goto err;
8354 +               continue;
8355 +
8356 +err:
8357 +               err = -EINVAL;
8358 +               break;
8359 +       }
8360 +
8361 +       AuTraceErr(err);
8362 +       return err;
8363 +}
8364 +
8365 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8366 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8367 +{
8368 +       int err;
8369 +       struct dentry *parent;
8370 +
8371 +       if (!au_digen_test(dentry, sigen))
8372 +               return 0;
8373 +
8374 +       parent = dget_parent(dentry);
8375 +       di_read_lock_parent(parent, AuLock_IR);
8376 +       AuDebugOn(au_digen_test(parent, sigen));
8377 +       au_dbg_verify_gen(parent, sigen);
8378 +       err = au_refresh_dentry(dentry, parent);
8379 +       di_read_unlock(parent, AuLock_IR);
8380 +       dput(parent);
8381 +       AuTraceErr(err);
8382 +       return err;
8383 +}
8384 +
8385 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8386 +{
8387 +       int err;
8388 +       struct dentry *d, *parent;
8389 +       struct inode *inode;
8390 +
8391 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8392 +               return simple_reval_dpath(dentry, sigen);
8393 +
8394 +       /* slow loop, keep it simple and stupid */
8395 +       /* cf: au_cpup_dirs() */
8396 +       err = 0;
8397 +       parent = NULL;
8398 +       while (au_digen_test(dentry, sigen)) {
8399 +               d = dentry;
8400 +               while (1) {
8401 +                       dput(parent);
8402 +                       parent = dget_parent(d);
8403 +                       if (!au_digen_test(parent, sigen))
8404 +                               break;
8405 +                       d = parent;
8406 +               }
8407 +
8408 +               inode = d->d_inode;
8409 +               if (d != dentry)
8410 +                       di_write_lock_child2(d);
8411 +
8412 +               /* someone might update our dentry while we were sleeping */
8413 +               if (au_digen_test(d, sigen)) {
8414 +                       /*
8415 +                        * todo: consolidate with simple_reval_dpath(),
8416 +                        * do_refresh() and au_reval_for_attr().
8417 +                        */
8418 +                       di_read_lock_parent(parent, AuLock_IR);
8419 +                       err = au_refresh_dentry(d, parent);
8420 +                       di_read_unlock(parent, AuLock_IR);
8421 +               }
8422 +
8423 +               if (d != dentry)
8424 +                       di_write_unlock(d);
8425 +               dput(parent);
8426 +               if (unlikely(err))
8427 +                       break;
8428 +       }
8429 +
8430 +       return err;
8431 +}
8432 +
8433 +/*
8434 + * if valid returns 1, otherwise 0.
8435 + */
8436 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8437 +{
8438 +       int valid, err;
8439 +       unsigned int sigen;
8440 +       unsigned char do_udba;
8441 +       struct super_block *sb;
8442 +       struct inode *inode;
8443 +
8444 +       /* todo: support rcu-walk? */
8445 +       if (flags & LOOKUP_RCU)
8446 +               return -ECHILD;
8447 +
8448 +       valid = 0;
8449 +       if (unlikely(!au_di(dentry)))
8450 +               goto out;
8451 +
8452 +       valid = 1;
8453 +       sb = dentry->d_sb;
8454 +       /*
8455 +        * todo: very ugly
8456 +        * i_mutex of parent dir may be held,
8457 +        * but we should not return 'invalid' due to busy.
8458 +        */
8459 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8460 +       if (unlikely(err)) {
8461 +               valid = err;
8462 +               AuTraceErr(err);
8463 +               goto out;
8464 +       }
8465 +       inode = dentry->d_inode;
8466 +       if (unlikely(inode && is_bad_inode(inode))) {
8467 +               err = -EINVAL;
8468 +               AuTraceErr(err);
8469 +               goto out_dgrade;
8470 +       }
8471 +       if (unlikely(au_dbrange_test(dentry))) {
8472 +               err = -EINVAL;
8473 +               AuTraceErr(err);
8474 +               goto out_dgrade;
8475 +       }
8476 +
8477 +       sigen = au_sigen(sb);
8478 +       if (au_digen_test(dentry, sigen)) {
8479 +               AuDebugOn(IS_ROOT(dentry));
8480 +               err = au_reval_dpath(dentry, sigen);
8481 +               if (unlikely(err)) {
8482 +                       AuTraceErr(err);
8483 +                       goto out_dgrade;
8484 +               }
8485 +       }
8486 +       di_downgrade_lock(dentry, AuLock_IR);
8487 +
8488 +       err = -EINVAL;
8489 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
8490 +           && inode
8491 +           && !(inode->i_state && I_LINKABLE)
8492 +           && (IS_DEADDIR(inode) || !inode->i_nlink))
8493 +               goto out_inval;
8494 +
8495 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8496 +       if (do_udba && inode) {
8497 +               aufs_bindex_t bstart = au_ibstart(inode);
8498 +               struct inode *h_inode;
8499 +
8500 +               if (bstart >= 0) {
8501 +                       h_inode = au_h_iptr(inode, bstart);
8502 +                       if (h_inode && au_test_higen(inode, h_inode))
8503 +                               goto out_inval;
8504 +               }
8505 +       }
8506 +
8507 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
8508 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
8509 +               err = -EIO;
8510 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
8511 +                     dentry, err);
8512 +       }
8513 +       goto out_inval;
8514 +
8515 +out_dgrade:
8516 +       di_downgrade_lock(dentry, AuLock_IR);
8517 +out_inval:
8518 +       aufs_read_unlock(dentry, AuLock_IR);
8519 +       AuTraceErr(err);
8520 +       valid = !err;
8521 +out:
8522 +       if (!valid) {
8523 +               AuDbg("%pd invalid, %d\n", dentry, valid);
8524 +               d_drop(dentry);
8525 +       }
8526 +       return valid;
8527 +}
8528 +
8529 +static void aufs_d_release(struct dentry *dentry)
8530 +{
8531 +       if (au_di(dentry)) {
8532 +               au_di_fin(dentry);
8533 +               au_hn_di_reinit(dentry);
8534 +       }
8535 +}
8536 +
8537 +const struct dentry_operations aufs_dop = {
8538 +       .d_revalidate           = aufs_d_revalidate,
8539 +       .d_weak_revalidate      = aufs_d_revalidate,
8540 +       .d_release              = aufs_d_release
8541 +};
8542 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
8543 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
8544 +++ linux/fs/aufs/dentry.h      2015-06-22 08:27:37.877501704 +0200
8545 @@ -0,0 +1,233 @@
8546 +/*
8547 + * Copyright (C) 2005-2015 Junjiro R. Okajima
8548 + *
8549 + * This program, aufs is free software; you can redistribute it and/or modify
8550 + * it under the terms of the GNU General Public License as published by
8551 + * the Free Software Foundation; either version 2 of the License, or
8552 + * (at your option) any later version.
8553 + *
8554 + * This program is distributed in the hope that it will be useful,
8555 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8556 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8557 + * GNU General Public License for more details.
8558 + *
8559 + * You should have received a copy of the GNU General Public License
8560 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8561 + */
8562 +
8563 +/*
8564 + * lookup and dentry operations
8565 + */
8566 +
8567 +#ifndef __AUFS_DENTRY_H__
8568 +#define __AUFS_DENTRY_H__
8569 +
8570 +#ifdef __KERNEL__
8571 +
8572 +#include <linux/dcache.h>
8573 +#include "rwsem.h"
8574 +
8575 +struct au_hdentry {
8576 +       struct dentry           *hd_dentry;
8577 +       aufs_bindex_t           hd_id;
8578 +};
8579 +
8580 +struct au_dinfo {
8581 +       atomic_t                di_generation;
8582 +
8583 +       struct au_rwsem         di_rwsem;
8584 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
8585 +       unsigned char           di_tmpfile; /* to allow the different name */
8586 +       struct au_hdentry       *di_hdentry;
8587 +} ____cacheline_aligned_in_smp;
8588 +
8589 +/* ---------------------------------------------------------------------- */
8590 +
8591 +/* dentry.c */
8592 +extern const struct dentry_operations aufs_dop;
8593 +struct au_branch;
8594 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
8595 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8596 +               struct dentry *h_parent, struct au_branch *br);
8597 +
8598 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
8599 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
8600 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
8601 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
8602 +
8603 +/* dinfo.c */
8604 +void au_di_init_once(void *_di);
8605 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8606 +void au_di_free(struct au_dinfo *dinfo);
8607 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8608 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
8609 +int au_di_init(struct dentry *dentry);
8610 +void au_di_fin(struct dentry *dentry);
8611 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
8612 +
8613 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8614 +void di_read_unlock(struct dentry *d, int flags);
8615 +void di_downgrade_lock(struct dentry *d, int flags);
8616 +void di_write_lock(struct dentry *d, unsigned int lsc);
8617 +void di_write_unlock(struct dentry *d);
8618 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8619 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8620 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8621 +
8622 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
8623 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
8624 +aufs_bindex_t au_dbtail(struct dentry *dentry);
8625 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8626 +
8627 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8628 +                  struct dentry *h_dentry);
8629 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
8630 +int au_dbrange_test(struct dentry *dentry);
8631 +void au_update_digen(struct dentry *dentry);
8632 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
8633 +void au_update_dbstart(struct dentry *dentry);
8634 +void au_update_dbend(struct dentry *dentry);
8635 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8636 +
8637 +/* ---------------------------------------------------------------------- */
8638 +
8639 +static inline struct au_dinfo *au_di(struct dentry *dentry)
8640 +{
8641 +       return dentry->d_fsdata;
8642 +}
8643 +
8644 +/* ---------------------------------------------------------------------- */
8645 +
8646 +/* lock subclass for dinfo */
8647 +enum {
8648 +       AuLsc_DI_CHILD,         /* child first */
8649 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
8650 +       AuLsc_DI_CHILD3,        /* copyup dirs */
8651 +       AuLsc_DI_PARENT,
8652 +       AuLsc_DI_PARENT2,
8653 +       AuLsc_DI_PARENT3,
8654 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
8655 +};
8656 +
8657 +/*
8658 + * di_read_lock_child, di_write_lock_child,
8659 + * di_read_lock_child2, di_write_lock_child2,
8660 + * di_read_lock_child3, di_write_lock_child3,
8661 + * di_read_lock_parent, di_write_lock_parent,
8662 + * di_read_lock_parent2, di_write_lock_parent2,
8663 + * di_read_lock_parent3, di_write_lock_parent3,
8664 + */
8665 +#define AuReadLockFunc(name, lsc) \
8666 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
8667 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8668 +
8669 +#define AuWriteLockFunc(name, lsc) \
8670 +static inline void di_write_lock_##name(struct dentry *d) \
8671 +{ di_write_lock(d, AuLsc_DI_##lsc); }
8672 +
8673 +#define AuRWLockFuncs(name, lsc) \
8674 +       AuReadLockFunc(name, lsc) \
8675 +       AuWriteLockFunc(name, lsc)
8676 +
8677 +AuRWLockFuncs(child, CHILD);
8678 +AuRWLockFuncs(child2, CHILD2);
8679 +AuRWLockFuncs(child3, CHILD3);
8680 +AuRWLockFuncs(parent, PARENT);
8681 +AuRWLockFuncs(parent2, PARENT2);
8682 +AuRWLockFuncs(parent3, PARENT3);
8683 +
8684 +#undef AuReadLockFunc
8685 +#undef AuWriteLockFunc
8686 +#undef AuRWLockFuncs
8687 +
8688 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
8689 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
8690 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
8691 +
8692 +/* ---------------------------------------------------------------------- */
8693 +
8694 +/* todo: memory barrier? */
8695 +static inline unsigned int au_digen(struct dentry *d)
8696 +{
8697 +       return atomic_read(&au_di(d)->di_generation);
8698 +}
8699 +
8700 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
8701 +{
8702 +       hdentry->hd_dentry = NULL;
8703 +}
8704 +
8705 +static inline void au_hdput(struct au_hdentry *hd)
8706 +{
8707 +       if (hd)
8708 +               dput(hd->hd_dentry);
8709 +}
8710 +
8711 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
8712 +{
8713 +       DiMustAnyLock(dentry);
8714 +       return au_di(dentry)->di_bstart;
8715 +}
8716 +
8717 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
8718 +{
8719 +       DiMustAnyLock(dentry);
8720 +       return au_di(dentry)->di_bend;
8721 +}
8722 +
8723 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
8724 +{
8725 +       DiMustAnyLock(dentry);
8726 +       return au_di(dentry)->di_bwh;
8727 +}
8728 +
8729 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
8730 +{
8731 +       DiMustAnyLock(dentry);
8732 +       return au_di(dentry)->di_bdiropq;
8733 +}
8734 +
8735 +/* todo: hard/soft set? */
8736 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
8737 +{
8738 +       DiMustWriteLock(dentry);
8739 +       au_di(dentry)->di_bstart = bindex;
8740 +}
8741 +
8742 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
8743 +{
8744 +       DiMustWriteLock(dentry);
8745 +       au_di(dentry)->di_bend = bindex;
8746 +}
8747 +
8748 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
8749 +{
8750 +       DiMustWriteLock(dentry);
8751 +       /* dbwh can be outside of bstart - bend range */
8752 +       au_di(dentry)->di_bwh = bindex;
8753 +}
8754 +
8755 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
8756 +{
8757 +       DiMustWriteLock(dentry);
8758 +       au_di(dentry)->di_bdiropq = bindex;
8759 +}
8760 +
8761 +/* ---------------------------------------------------------------------- */
8762 +
8763 +#ifdef CONFIG_AUFS_HNOTIFY
8764 +static inline void au_digen_dec(struct dentry *d)
8765 +{
8766 +       atomic_dec(&au_di(d)->di_generation);
8767 +}
8768 +
8769 +static inline void au_hn_di_reinit(struct dentry *dentry)
8770 +{
8771 +       dentry->d_fsdata = NULL;
8772 +}
8773 +#else
8774 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
8775 +#endif /* CONFIG_AUFS_HNOTIFY */
8776 +
8777 +#endif /* __KERNEL__ */
8778 +#endif /* __AUFS_DENTRY_H__ */
8779 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
8780 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
8781 +++ linux/fs/aufs/dinfo.c       2015-06-22 08:27:37.877501704 +0200
8782 @@ -0,0 +1,544 @@
8783 +/*
8784 + * Copyright (C) 2005-2015 Junjiro R. Okajima
8785 + *
8786 + * This program, aufs is free software; you can redistribute it and/or modify
8787 + * it under the terms of the GNU General Public License as published by
8788 + * the Free Software Foundation; either version 2 of the License, or
8789 + * (at your option) any later version.
8790 + *
8791 + * This program is distributed in the hope that it will be useful,
8792 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8793 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8794 + * GNU General Public License for more details.
8795 + *
8796 + * You should have received a copy of the GNU General Public License
8797 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8798 + */
8799 +
8800 +/*
8801 + * dentry private data
8802 + */
8803 +
8804 +#include "aufs.h"
8805 +
8806 +void au_di_init_once(void *_dinfo)
8807 +{
8808 +       struct au_dinfo *dinfo = _dinfo;
8809 +       static struct lock_class_key aufs_di;
8810 +
8811 +       au_rw_init(&dinfo->di_rwsem);
8812 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
8813 +}
8814 +
8815 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
8816 +{
8817 +       struct au_dinfo *dinfo;
8818 +       int nbr, i;
8819 +
8820 +       dinfo = au_cache_alloc_dinfo();
8821 +       if (unlikely(!dinfo))
8822 +               goto out;
8823 +
8824 +       nbr = au_sbend(sb) + 1;
8825 +       if (nbr <= 0)
8826 +               nbr = 1;
8827 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
8828 +       if (dinfo->di_hdentry) {
8829 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
8830 +               dinfo->di_bstart = -1;
8831 +               dinfo->di_bend = -1;
8832 +               dinfo->di_bwh = -1;
8833 +               dinfo->di_bdiropq = -1;
8834 +               dinfo->di_tmpfile = 0;
8835 +               for (i = 0; i < nbr; i++)
8836 +                       dinfo->di_hdentry[i].hd_id = -1;
8837 +               goto out;
8838 +       }
8839 +
8840 +       au_cache_free_dinfo(dinfo);
8841 +       dinfo = NULL;
8842 +
8843 +out:
8844 +       return dinfo;
8845 +}
8846 +
8847 +void au_di_free(struct au_dinfo *dinfo)
8848 +{
8849 +       struct au_hdentry *p;
8850 +       aufs_bindex_t bend, bindex;
8851 +
8852 +       /* dentry may not be revalidated */
8853 +       bindex = dinfo->di_bstart;
8854 +       if (bindex >= 0) {
8855 +               bend = dinfo->di_bend;
8856 +               p = dinfo->di_hdentry + bindex;
8857 +               while (bindex++ <= bend)
8858 +                       au_hdput(p++);
8859 +       }
8860 +       kfree(dinfo->di_hdentry);
8861 +       au_cache_free_dinfo(dinfo);
8862 +}
8863 +
8864 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
8865 +{
8866 +       struct au_hdentry *p;
8867 +       aufs_bindex_t bi;
8868 +
8869 +       AuRwMustWriteLock(&a->di_rwsem);
8870 +       AuRwMustWriteLock(&b->di_rwsem);
8871 +
8872 +#define DiSwap(v, name)                                \
8873 +       do {                                    \
8874 +               v = a->di_##name;               \
8875 +               a->di_##name = b->di_##name;    \
8876 +               b->di_##name = v;               \
8877 +       } while (0)
8878 +
8879 +       DiSwap(p, hdentry);
8880 +       DiSwap(bi, bstart);
8881 +       DiSwap(bi, bend);
8882 +       DiSwap(bi, bwh);
8883 +       DiSwap(bi, bdiropq);
8884 +       /* smp_mb(); */
8885 +
8886 +#undef DiSwap
8887 +}
8888 +
8889 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
8890 +{
8891 +       AuRwMustWriteLock(&dst->di_rwsem);
8892 +       AuRwMustWriteLock(&src->di_rwsem);
8893 +
8894 +       dst->di_bstart = src->di_bstart;
8895 +       dst->di_bend = src->di_bend;
8896 +       dst->di_bwh = src->di_bwh;
8897 +       dst->di_bdiropq = src->di_bdiropq;
8898 +       /* smp_mb(); */
8899 +}
8900 +
8901 +int au_di_init(struct dentry *dentry)
8902 +{
8903 +       int err;
8904 +       struct super_block *sb;
8905 +       struct au_dinfo *dinfo;
8906 +
8907 +       err = 0;
8908 +       sb = dentry->d_sb;
8909 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
8910 +       if (dinfo) {
8911 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
8912 +               /* smp_mb(); */ /* atomic_set */
8913 +               dentry->d_fsdata = dinfo;
8914 +       } else
8915 +               err = -ENOMEM;
8916 +
8917 +       return err;
8918 +}
8919 +
8920 +void au_di_fin(struct dentry *dentry)
8921 +{
8922 +       struct au_dinfo *dinfo;
8923 +
8924 +       dinfo = au_di(dentry);
8925 +       AuRwDestroy(&dinfo->di_rwsem);
8926 +       au_di_free(dinfo);
8927 +}
8928 +
8929 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
8930 +{
8931 +       int err, sz;
8932 +       struct au_hdentry *hdp;
8933 +
8934 +       AuRwMustWriteLock(&dinfo->di_rwsem);
8935 +
8936 +       err = -ENOMEM;
8937 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
8938 +       if (!sz)
8939 +               sz = sizeof(*hdp);
8940 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
8941 +       if (hdp) {
8942 +               dinfo->di_hdentry = hdp;
8943 +               err = 0;
8944 +       }
8945 +
8946 +       return err;
8947 +}
8948 +
8949 +/* ---------------------------------------------------------------------- */
8950 +
8951 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
8952 +{
8953 +       switch (lsc) {
8954 +       case AuLsc_DI_CHILD:
8955 +               ii_write_lock_child(inode);
8956 +               break;
8957 +       case AuLsc_DI_CHILD2:
8958 +               ii_write_lock_child2(inode);
8959 +               break;
8960 +       case AuLsc_DI_CHILD3:
8961 +               ii_write_lock_child3(inode);
8962 +               break;
8963 +       case AuLsc_DI_PARENT:
8964 +               ii_write_lock_parent(inode);
8965 +               break;
8966 +       case AuLsc_DI_PARENT2:
8967 +               ii_write_lock_parent2(inode);
8968 +               break;
8969 +       case AuLsc_DI_PARENT3:
8970 +               ii_write_lock_parent3(inode);
8971 +               break;
8972 +       default:
8973 +               BUG();
8974 +       }
8975 +}
8976 +
8977 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
8978 +{
8979 +       switch (lsc) {
8980 +       case AuLsc_DI_CHILD:
8981 +               ii_read_lock_child(inode);
8982 +               break;
8983 +       case AuLsc_DI_CHILD2:
8984 +               ii_read_lock_child2(inode);
8985 +               break;
8986 +       case AuLsc_DI_CHILD3:
8987 +               ii_read_lock_child3(inode);
8988 +               break;
8989 +       case AuLsc_DI_PARENT:
8990 +               ii_read_lock_parent(inode);
8991 +               break;
8992 +       case AuLsc_DI_PARENT2:
8993 +               ii_read_lock_parent2(inode);
8994 +               break;
8995 +       case AuLsc_DI_PARENT3:
8996 +               ii_read_lock_parent3(inode);
8997 +               break;
8998 +       default:
8999 +               BUG();
9000 +       }
9001 +}
9002 +
9003 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9004 +{
9005 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9006 +       if (d->d_inode) {
9007 +               if (au_ftest_lock(flags, IW))
9008 +                       do_ii_write_lock(d->d_inode, lsc);
9009 +               else if (au_ftest_lock(flags, IR))
9010 +                       do_ii_read_lock(d->d_inode, lsc);
9011 +       }
9012 +}
9013 +
9014 +void di_read_unlock(struct dentry *d, int flags)
9015 +{
9016 +       if (d->d_inode) {
9017 +               if (au_ftest_lock(flags, IW)) {
9018 +                       au_dbg_verify_dinode(d);
9019 +                       ii_write_unlock(d->d_inode);
9020 +               } else if (au_ftest_lock(flags, IR)) {
9021 +                       au_dbg_verify_dinode(d);
9022 +                       ii_read_unlock(d->d_inode);
9023 +               }
9024 +       }
9025 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9026 +}
9027 +
9028 +void di_downgrade_lock(struct dentry *d, int flags)
9029 +{
9030 +       if (d->d_inode && au_ftest_lock(flags, IR))
9031 +               ii_downgrade_lock(d->d_inode);
9032 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9033 +}
9034 +
9035 +void di_write_lock(struct dentry *d, unsigned int lsc)
9036 +{
9037 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9038 +       if (d->d_inode)
9039 +               do_ii_write_lock(d->d_inode, lsc);
9040 +}
9041 +
9042 +void di_write_unlock(struct dentry *d)
9043 +{
9044 +       au_dbg_verify_dinode(d);
9045 +       if (d->d_inode)
9046 +               ii_write_unlock(d->d_inode);
9047 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9048 +}
9049 +
9050 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9051 +{
9052 +       AuDebugOn(d1 == d2
9053 +                 || d1->d_inode == d2->d_inode
9054 +                 || d1->d_sb != d2->d_sb);
9055 +
9056 +       if (isdir && au_test_subdir(d1, d2)) {
9057 +               di_write_lock_child(d1);
9058 +               di_write_lock_child2(d2);
9059 +       } else {
9060 +               /* there should be no races */
9061 +               di_write_lock_child(d2);
9062 +               di_write_lock_child2(d1);
9063 +       }
9064 +}
9065 +
9066 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9067 +{
9068 +       AuDebugOn(d1 == d2
9069 +                 || d1->d_inode == d2->d_inode
9070 +                 || d1->d_sb != d2->d_sb);
9071 +
9072 +       if (isdir && au_test_subdir(d1, d2)) {
9073 +               di_write_lock_parent(d1);
9074 +               di_write_lock_parent2(d2);
9075 +       } else {
9076 +               /* there should be no races */
9077 +               di_write_lock_parent(d2);
9078 +               di_write_lock_parent2(d1);
9079 +       }
9080 +}
9081 +
9082 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9083 +{
9084 +       di_write_unlock(d1);
9085 +       if (d1->d_inode == d2->d_inode)
9086 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9087 +       else
9088 +               di_write_unlock(d2);
9089 +}
9090 +
9091 +/* ---------------------------------------------------------------------- */
9092 +
9093 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9094 +{
9095 +       struct dentry *d;
9096 +
9097 +       DiMustAnyLock(dentry);
9098 +
9099 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
9100 +               return NULL;
9101 +       AuDebugOn(bindex < 0);
9102 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
9103 +       AuDebugOn(d && au_dcount(d) <= 0);
9104 +       return d;
9105 +}
9106 +
9107 +/*
9108 + * extended version of au_h_dptr().
9109 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9110 + * error.
9111 + */
9112 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9113 +{
9114 +       struct dentry *h_dentry;
9115 +       struct inode *inode, *h_inode;
9116 +
9117 +       inode = dentry->d_inode;
9118 +       AuDebugOn(!inode);
9119 +
9120 +       h_dentry = NULL;
9121 +       if (au_dbstart(dentry) <= bindex
9122 +           && bindex <= au_dbend(dentry))
9123 +               h_dentry = au_h_dptr(dentry, bindex);
9124 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9125 +               dget(h_dentry);
9126 +               goto out; /* success */
9127 +       }
9128 +
9129 +       AuDebugOn(bindex < au_ibstart(inode));
9130 +       AuDebugOn(au_ibend(inode) < bindex);
9131 +       h_inode = au_h_iptr(inode, bindex);
9132 +       h_dentry = d_find_alias(h_inode);
9133 +       if (h_dentry) {
9134 +               if (!IS_ERR(h_dentry)) {
9135 +                       if (!au_d_linkable(h_dentry))
9136 +                               goto out; /* success */
9137 +                       dput(h_dentry);
9138 +               } else
9139 +                       goto out;
9140 +       }
9141 +
9142 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9143 +               h_dentry = au_plink_lkup(inode, bindex);
9144 +               AuDebugOn(!h_dentry);
9145 +               if (!IS_ERR(h_dentry)) {
9146 +                       if (!au_d_hashed_positive(h_dentry))
9147 +                               goto out; /* success */
9148 +                       dput(h_dentry);
9149 +                       h_dentry = NULL;
9150 +               }
9151 +       }
9152 +
9153 +out:
9154 +       AuDbgDentry(h_dentry);
9155 +       return h_dentry;
9156 +}
9157 +
9158 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9159 +{
9160 +       aufs_bindex_t bend, bwh;
9161 +
9162 +       bend = au_dbend(dentry);
9163 +       if (0 <= bend) {
9164 +               bwh = au_dbwh(dentry);
9165 +               if (!bwh)
9166 +                       return bwh;
9167 +               if (0 < bwh && bwh < bend)
9168 +                       return bwh - 1;
9169 +       }
9170 +       return bend;
9171 +}
9172 +
9173 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9174 +{
9175 +       aufs_bindex_t bend, bopq;
9176 +
9177 +       bend = au_dbtail(dentry);
9178 +       if (0 <= bend) {
9179 +               bopq = au_dbdiropq(dentry);
9180 +               if (0 <= bopq && bopq < bend)
9181 +                       bend = bopq;
9182 +       }
9183 +       return bend;
9184 +}
9185 +
9186 +/* ---------------------------------------------------------------------- */
9187 +
9188 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9189 +                  struct dentry *h_dentry)
9190 +{
9191 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
9192 +       struct au_branch *br;
9193 +
9194 +       DiMustWriteLock(dentry);
9195 +
9196 +       au_hdput(hd);
9197 +       hd->hd_dentry = h_dentry;
9198 +       if (h_dentry) {
9199 +               br = au_sbr(dentry->d_sb, bindex);
9200 +               hd->hd_id = br->br_id;
9201 +       }
9202 +}
9203 +
9204 +int au_dbrange_test(struct dentry *dentry)
9205 +{
9206 +       int err;
9207 +       aufs_bindex_t bstart, bend;
9208 +
9209 +       err = 0;
9210 +       bstart = au_dbstart(dentry);
9211 +       bend = au_dbend(dentry);
9212 +       if (bstart >= 0)
9213 +               AuDebugOn(bend < 0 && bstart > bend);
9214 +       else {
9215 +               err = -EIO;
9216 +               AuDebugOn(bend >= 0);
9217 +       }
9218 +
9219 +       return err;
9220 +}
9221 +
9222 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9223 +{
9224 +       int err;
9225 +
9226 +       err = 0;
9227 +       if (unlikely(au_digen(dentry) != sigen
9228 +                    || au_iigen_test(dentry->d_inode, sigen)))
9229 +               err = -EIO;
9230 +
9231 +       return err;
9232 +}
9233 +
9234 +void au_update_digen(struct dentry *dentry)
9235 +{
9236 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9237 +       /* smp_mb(); */ /* atomic_set */
9238 +}
9239 +
9240 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9241 +{
9242 +       struct au_dinfo *dinfo;
9243 +       struct dentry *h_d;
9244 +       struct au_hdentry *hdp;
9245 +
9246 +       DiMustWriteLock(dentry);
9247 +
9248 +       dinfo = au_di(dentry);
9249 +       if (!dinfo || dinfo->di_bstart < 0)
9250 +               return;
9251 +
9252 +       hdp = dinfo->di_hdentry;
9253 +       if (do_put_zero) {
9254 +               aufs_bindex_t bindex, bend;
9255 +
9256 +               bend = dinfo->di_bend;
9257 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
9258 +                       h_d = hdp[0 + bindex].hd_dentry;
9259 +                       if (h_d && !h_d->d_inode)
9260 +                               au_set_h_dptr(dentry, bindex, NULL);
9261 +               }
9262 +       }
9263 +
9264 +       dinfo->di_bstart = -1;
9265 +       while (++dinfo->di_bstart <= dinfo->di_bend)
9266 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
9267 +                       break;
9268 +       if (dinfo->di_bstart > dinfo->di_bend) {
9269 +               dinfo->di_bstart = -1;
9270 +               dinfo->di_bend = -1;
9271 +               return;
9272 +       }
9273 +
9274 +       dinfo->di_bend++;
9275 +       while (0 <= --dinfo->di_bend)
9276 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
9277 +                       break;
9278 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
9279 +}
9280 +
9281 +void au_update_dbstart(struct dentry *dentry)
9282 +{
9283 +       aufs_bindex_t bindex, bend;
9284 +       struct dentry *h_dentry;
9285 +
9286 +       bend = au_dbend(dentry);
9287 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
9288 +               h_dentry = au_h_dptr(dentry, bindex);
9289 +               if (!h_dentry)
9290 +                       continue;
9291 +               if (h_dentry->d_inode) {
9292 +                       au_set_dbstart(dentry, bindex);
9293 +                       return;
9294 +               }
9295 +               au_set_h_dptr(dentry, bindex, NULL);
9296 +       }
9297 +}
9298 +
9299 +void au_update_dbend(struct dentry *dentry)
9300 +{
9301 +       aufs_bindex_t bindex, bstart;
9302 +       struct dentry *h_dentry;
9303 +
9304 +       bstart = au_dbstart(dentry);
9305 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
9306 +               h_dentry = au_h_dptr(dentry, bindex);
9307 +               if (!h_dentry)
9308 +                       continue;
9309 +               if (h_dentry->d_inode) {
9310 +                       au_set_dbend(dentry, bindex);
9311 +                       return;
9312 +               }
9313 +               au_set_h_dptr(dentry, bindex, NULL);
9314 +       }
9315 +}
9316 +
9317 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9318 +{
9319 +       aufs_bindex_t bindex, bend;
9320 +
9321 +       bend = au_dbend(dentry);
9322 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
9323 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9324 +                       return bindex;
9325 +       return -1;
9326 +}
9327 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9328 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9329 +++ linux/fs/aufs/dir.c 2015-06-22 08:29:23.706757681 +0200
9330 @@ -0,0 +1,751 @@
9331 +/*
9332 + * Copyright (C) 2005-2015 Junjiro R. Okajima
9333 + *
9334 + * This program, aufs is free software; you can redistribute it and/or modify
9335 + * it under the terms of the GNU General Public License as published by
9336 + * the Free Software Foundation; either version 2 of the License, or
9337 + * (at your option) any later version.
9338 + *
9339 + * This program is distributed in the hope that it will be useful,
9340 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9341 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9342 + * GNU General Public License for more details.
9343 + *
9344 + * You should have received a copy of the GNU General Public License
9345 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9346 + */
9347 +
9348 +/*
9349 + * directory operations
9350 + */
9351 +
9352 +#include <linux/fs_stack.h>
9353 +#include "aufs.h"
9354 +
9355 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9356 +{
9357 +       unsigned int nlink;
9358 +
9359 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9360 +
9361 +       nlink = dir->i_nlink;
9362 +       nlink += h_dir->i_nlink - 2;
9363 +       if (h_dir->i_nlink < 2)
9364 +               nlink += 2;
9365 +       smp_mb(); /* for i_nlink */
9366 +       /* 0 can happen in revaliding */
9367 +       set_nlink(dir, nlink);
9368 +}
9369 +
9370 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9371 +{
9372 +       unsigned int nlink;
9373 +
9374 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9375 +
9376 +       nlink = dir->i_nlink;
9377 +       nlink -= h_dir->i_nlink - 2;
9378 +       if (h_dir->i_nlink < 2)
9379 +               nlink -= 2;
9380 +       smp_mb(); /* for i_nlink */
9381 +       /* nlink == 0 means the branch-fs is broken */
9382 +       set_nlink(dir, nlink);
9383 +}
9384 +
9385 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
9386 +{
9387 +       loff_t sz;
9388 +       aufs_bindex_t bindex, bend;
9389 +       struct file *h_file;
9390 +       struct dentry *h_dentry;
9391 +
9392 +       sz = 0;
9393 +       if (file) {
9394 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
9395 +
9396 +               bend = au_fbend_dir(file);
9397 +               for (bindex = au_fbstart(file);
9398 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9399 +                    bindex++) {
9400 +                       h_file = au_hf_dir(file, bindex);
9401 +                       if (h_file && file_inode(h_file))
9402 +                               sz += vfsub_f_size_read(h_file);
9403 +               }
9404 +       } else {
9405 +               AuDebugOn(!dentry);
9406 +               AuDebugOn(!d_is_dir(dentry));
9407 +
9408 +               bend = au_dbtaildir(dentry);
9409 +               for (bindex = au_dbstart(dentry);
9410 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9411 +                    bindex++) {
9412 +                       h_dentry = au_h_dptr(dentry, bindex);
9413 +                       if (h_dentry && h_dentry->d_inode)
9414 +                               sz += i_size_read(h_dentry->d_inode);
9415 +               }
9416 +       }
9417 +       if (sz < KMALLOC_MAX_SIZE)
9418 +               sz = roundup_pow_of_two(sz);
9419 +       if (sz > KMALLOC_MAX_SIZE)
9420 +               sz = KMALLOC_MAX_SIZE;
9421 +       else if (sz < NAME_MAX) {
9422 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9423 +               sz = AUFS_RDBLK_DEF;
9424 +       }
9425 +       return sz;
9426 +}
9427 +
9428 +struct au_dir_ts_arg {
9429 +       struct dentry *dentry;
9430 +       aufs_bindex_t brid;
9431 +};
9432 +
9433 +static void au_do_dir_ts(void *arg)
9434 +{
9435 +       struct au_dir_ts_arg *a = arg;
9436 +       struct au_dtime dt;
9437 +       struct path h_path;
9438 +       struct inode *dir, *h_dir;
9439 +       struct super_block *sb;
9440 +       struct au_branch *br;
9441 +       struct au_hinode *hdir;
9442 +       int err;
9443 +       aufs_bindex_t bstart, bindex;
9444 +
9445 +       sb = a->dentry->d_sb;
9446 +       dir = a->dentry->d_inode;
9447 +       if (!dir)
9448 +               goto out;
9449 +       /* no dir->i_mutex lock */
9450 +       aufs_read_lock(a->dentry, AuLock_DW | AuLock_DIR); /* noflush */
9451 +
9452 +       bstart = au_ibstart(dir);
9453 +       bindex = au_br_index(sb, a->brid);
9454 +       if (bindex < bstart)
9455 +               goto out_unlock;
9456 +
9457 +       br = au_sbr(sb, bindex);
9458 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
9459 +       if (!h_path.dentry)
9460 +               goto out_unlock;
9461 +       h_path.mnt = au_br_mnt(br);
9462 +       au_dtime_store(&dt, a->dentry, &h_path);
9463 +
9464 +       br = au_sbr(sb, bstart);
9465 +       if (!au_br_writable(br->br_perm))
9466 +               goto out_unlock;
9467 +       h_path.dentry = au_h_dptr(a->dentry, bstart);
9468 +       h_path.mnt = au_br_mnt(br);
9469 +       err = vfsub_mnt_want_write(h_path.mnt);
9470 +       if (err)
9471 +               goto out_unlock;
9472 +       hdir = au_hi(dir, bstart);
9473 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
9474 +       h_dir = au_h_iptr(dir, bstart);
9475 +       if (h_dir->i_nlink
9476 +           && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
9477 +               dt.dt_h_path = h_path;
9478 +               au_dtime_revert(&dt);
9479 +       }
9480 +       au_hn_imtx_unlock(hdir);
9481 +       vfsub_mnt_drop_write(h_path.mnt);
9482 +       au_cpup_attr_timesizes(dir);
9483 +
9484 +out_unlock:
9485 +       aufs_read_unlock(a->dentry, AuLock_DW);
9486 +out:
9487 +       dput(a->dentry);
9488 +       au_nwt_done(&au_sbi(sb)->si_nowait);
9489 +       kfree(arg);
9490 +}
9491 +
9492 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
9493 +{
9494 +       int perm, wkq_err;
9495 +       aufs_bindex_t bstart;
9496 +       struct au_dir_ts_arg *arg;
9497 +       struct dentry *dentry;
9498 +       struct super_block *sb;
9499 +
9500 +       IMustLock(dir);
9501 +
9502 +       dentry = d_find_any_alias(dir);
9503 +       AuDebugOn(!dentry);
9504 +       sb = dentry->d_sb;
9505 +       bstart = au_ibstart(dir);
9506 +       if (bstart == bindex) {
9507 +               au_cpup_attr_timesizes(dir);
9508 +               goto out;
9509 +       }
9510 +
9511 +       perm = au_sbr_perm(sb, bstart);
9512 +       if (!au_br_writable(perm))
9513 +               goto out;
9514 +
9515 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
9516 +       if (!arg)
9517 +               goto out;
9518 +
9519 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
9520 +       arg->brid = au_sbr_id(sb, bindex);
9521 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
9522 +       if (unlikely(wkq_err)) {
9523 +               pr_err("wkq %d\n", wkq_err);
9524 +               dput(dentry);
9525 +               kfree(arg);
9526 +       }
9527 +
9528 +out:
9529 +       dput(dentry);
9530 +}
9531 +
9532 +/* ---------------------------------------------------------------------- */
9533 +
9534 +static int reopen_dir(struct file *file)
9535 +{
9536 +       int err;
9537 +       unsigned int flags;
9538 +       aufs_bindex_t bindex, btail, bstart;
9539 +       struct dentry *dentry, *h_dentry;
9540 +       struct file *h_file;
9541 +
9542 +       /* open all lower dirs */
9543 +       dentry = file->f_path.dentry;
9544 +       bstart = au_dbstart(dentry);
9545 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
9546 +               au_set_h_fptr(file, bindex, NULL);
9547 +       au_set_fbstart(file, bstart);
9548 +
9549 +       btail = au_dbtaildir(dentry);
9550 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
9551 +               au_set_h_fptr(file, bindex, NULL);
9552 +       au_set_fbend_dir(file, btail);
9553 +
9554 +       flags = vfsub_file_flags(file);
9555 +       for (bindex = bstart; bindex <= btail; bindex++) {
9556 +               h_dentry = au_h_dptr(dentry, bindex);
9557 +               if (!h_dentry)
9558 +                       continue;
9559 +               h_file = au_hf_dir(file, bindex);
9560 +               if (h_file)
9561 +                       continue;
9562 +
9563 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9564 +               err = PTR_ERR(h_file);
9565 +               if (IS_ERR(h_file))
9566 +                       goto out; /* close all? */
9567 +               au_set_h_fptr(file, bindex, h_file);
9568 +       }
9569 +       au_update_figen(file);
9570 +       /* todo: necessary? */
9571 +       /* file->f_ra = h_file->f_ra; */
9572 +       err = 0;
9573 +
9574 +out:
9575 +       return err;
9576 +}
9577 +
9578 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
9579 +{
9580 +       int err;
9581 +       aufs_bindex_t bindex, btail;
9582 +       struct dentry *dentry, *h_dentry;
9583 +
9584 +       FiMustWriteLock(file);
9585 +       AuDebugOn(h_file);
9586 +
9587 +       err = 0;
9588 +       dentry = file->f_path.dentry;
9589 +       file->f_version = dentry->d_inode->i_version;
9590 +       bindex = au_dbstart(dentry);
9591 +       au_set_fbstart(file, bindex);
9592 +       btail = au_dbtaildir(dentry);
9593 +       au_set_fbend_dir(file, btail);
9594 +       for (; !err && bindex <= btail; bindex++) {
9595 +               h_dentry = au_h_dptr(dentry, bindex);
9596 +               if (!h_dentry)
9597 +                       continue;
9598 +
9599 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9600 +               if (IS_ERR(h_file)) {
9601 +                       err = PTR_ERR(h_file);
9602 +                       break;
9603 +               }
9604 +               au_set_h_fptr(file, bindex, h_file);
9605 +       }
9606 +       au_update_figen(file);
9607 +       /* todo: necessary? */
9608 +       /* file->f_ra = h_file->f_ra; */
9609 +       if (!err)
9610 +               return 0; /* success */
9611 +
9612 +       /* close all */
9613 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
9614 +               au_set_h_fptr(file, bindex, NULL);
9615 +       au_set_fbstart(file, -1);
9616 +       au_set_fbend_dir(file, -1);
9617 +
9618 +       return err;
9619 +}
9620 +
9621 +static int aufs_open_dir(struct inode *inode __maybe_unused,
9622 +                        struct file *file)
9623 +{
9624 +       int err;
9625 +       struct super_block *sb;
9626 +       struct au_fidir *fidir;
9627 +
9628 +       err = -ENOMEM;
9629 +       sb = file->f_path.dentry->d_sb;
9630 +       si_read_lock(sb, AuLock_FLUSH);
9631 +       fidir = au_fidir_alloc(sb);
9632 +       if (fidir) {
9633 +               struct au_do_open_args args = {
9634 +                       .open   = do_open_dir,
9635 +                       .fidir  = fidir
9636 +               };
9637 +               err = au_do_open(file, &args);
9638 +               if (unlikely(err))
9639 +                       kfree(fidir);
9640 +       }
9641 +       si_read_unlock(sb);
9642 +       return err;
9643 +}
9644 +
9645 +static int aufs_release_dir(struct inode *inode __maybe_unused,
9646 +                           struct file *file)
9647 +{
9648 +       struct au_vdir *vdir_cache;
9649 +       struct au_finfo *finfo;
9650 +       struct au_fidir *fidir;
9651 +       aufs_bindex_t bindex, bend;
9652 +
9653 +       finfo = au_fi(file);
9654 +       fidir = finfo->fi_hdir;
9655 +       if (fidir) {
9656 +               au_sphl_del(&finfo->fi_hlist,
9657 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
9658 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
9659 +               if (vdir_cache)
9660 +                       au_vdir_free(vdir_cache);
9661 +
9662 +               bindex = finfo->fi_btop;
9663 +               if (bindex >= 0) {
9664 +                       /*
9665 +                        * calls fput() instead of filp_close(),
9666 +                        * since no dnotify or lock for the lower file.
9667 +                        */
9668 +                       bend = fidir->fd_bbot;
9669 +                       for (; bindex <= bend; bindex++)
9670 +                               au_set_h_fptr(file, bindex, NULL);
9671 +               }
9672 +               kfree(fidir);
9673 +               finfo->fi_hdir = NULL;
9674 +       }
9675 +       au_finfo_fin(file);
9676 +       return 0;
9677 +}
9678 +
9679 +/* ---------------------------------------------------------------------- */
9680 +
9681 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
9682 +{
9683 +       int err;
9684 +       aufs_bindex_t bindex, bend;
9685 +       struct file *h_file;
9686 +
9687 +       err = 0;
9688 +       bend = au_fbend_dir(file);
9689 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9690 +               h_file = au_hf_dir(file, bindex);
9691 +               if (h_file)
9692 +                       err = vfsub_flush(h_file, id);
9693 +       }
9694 +       return err;
9695 +}
9696 +
9697 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
9698 +{
9699 +       return au_do_flush(file, id, au_do_flush_dir);
9700 +}
9701 +
9702 +/* ---------------------------------------------------------------------- */
9703 +
9704 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
9705 +{
9706 +       int err;
9707 +       aufs_bindex_t bend, bindex;
9708 +       struct inode *inode;
9709 +       struct super_block *sb;
9710 +
9711 +       err = 0;
9712 +       sb = dentry->d_sb;
9713 +       inode = dentry->d_inode;
9714 +       IMustLock(inode);
9715 +       bend = au_dbend(dentry);
9716 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
9717 +               struct path h_path;
9718 +
9719 +               if (au_test_ro(sb, bindex, inode))
9720 +                       continue;
9721 +               h_path.dentry = au_h_dptr(dentry, bindex);
9722 +               if (!h_path.dentry)
9723 +                       continue;
9724 +
9725 +               h_path.mnt = au_sbr_mnt(sb, bindex);
9726 +               err = vfsub_fsync(NULL, &h_path, datasync);
9727 +       }
9728 +
9729 +       return err;
9730 +}
9731 +
9732 +static int au_do_fsync_dir(struct file *file, int datasync)
9733 +{
9734 +       int err;
9735 +       aufs_bindex_t bend, bindex;
9736 +       struct file *h_file;
9737 +       struct super_block *sb;
9738 +       struct inode *inode;
9739 +
9740 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9741 +       if (unlikely(err))
9742 +               goto out;
9743 +
9744 +       inode = file_inode(file);
9745 +       sb = inode->i_sb;
9746 +       bend = au_fbend_dir(file);
9747 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9748 +               h_file = au_hf_dir(file, bindex);
9749 +               if (!h_file || au_test_ro(sb, bindex, inode))
9750 +                       continue;
9751 +
9752 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
9753 +       }
9754 +
9755 +out:
9756 +       return err;
9757 +}
9758 +
9759 +/*
9760 + * @file may be NULL
9761 + */
9762 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
9763 +                         int datasync)
9764 +{
9765 +       int err;
9766 +       struct dentry *dentry;
9767 +       struct super_block *sb;
9768 +       struct mutex *mtx;
9769 +
9770 +       err = 0;
9771 +       dentry = file->f_path.dentry;
9772 +       mtx = &dentry->d_inode->i_mutex;
9773 +       mutex_lock(mtx);
9774 +       sb = dentry->d_sb;
9775 +       si_noflush_read_lock(sb);
9776 +       if (file)
9777 +               err = au_do_fsync_dir(file, datasync);
9778 +       else {
9779 +               di_write_lock_child(dentry);
9780 +               err = au_do_fsync_dir_no_file(dentry, datasync);
9781 +       }
9782 +       au_cpup_attr_timesizes(dentry->d_inode);
9783 +       di_write_unlock(dentry);
9784 +       if (file)
9785 +               fi_write_unlock(file);
9786 +
9787 +       si_read_unlock(sb);
9788 +       mutex_unlock(mtx);
9789 +       return err;
9790 +}
9791 +
9792 +/* ---------------------------------------------------------------------- */
9793 +
9794 +static int aufs_iterate(struct file *file, struct dir_context *ctx)
9795 +{
9796 +       int err;
9797 +       struct dentry *dentry;
9798 +       struct inode *inode, *h_inode;
9799 +       struct super_block *sb;
9800 +
9801 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
9802 +
9803 +       dentry = file->f_path.dentry;
9804 +       inode = dentry->d_inode;
9805 +       IMustLock(inode);
9806 +
9807 +       sb = dentry->d_sb;
9808 +       si_read_lock(sb, AuLock_FLUSH);
9809 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9810 +       if (unlikely(err))
9811 +               goto out;
9812 +       err = au_alive_dir(dentry);
9813 +       if (!err)
9814 +               err = au_vdir_init(file);
9815 +       di_downgrade_lock(dentry, AuLock_IR);
9816 +       if (unlikely(err))
9817 +               goto out_unlock;
9818 +
9819 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
9820 +       if (!au_test_nfsd()) {
9821 +               err = au_vdir_fill_de(file, ctx);
9822 +               fsstack_copy_attr_atime(inode, h_inode);
9823 +       } else {
9824 +               /*
9825 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
9826 +                * encode_fh() and others.
9827 +                */
9828 +               atomic_inc(&h_inode->i_count);
9829 +               di_read_unlock(dentry, AuLock_IR);
9830 +               si_read_unlock(sb);
9831 +               err = au_vdir_fill_de(file, ctx);
9832 +               fsstack_copy_attr_atime(inode, h_inode);
9833 +               fi_write_unlock(file);
9834 +               iput(h_inode);
9835 +
9836 +               AuTraceErr(err);
9837 +               return err;
9838 +       }
9839 +
9840 +out_unlock:
9841 +       di_read_unlock(dentry, AuLock_IR);
9842 +       fi_write_unlock(file);
9843 +out:
9844 +       si_read_unlock(sb);
9845 +       return err;
9846 +}
9847 +
9848 +/* ---------------------------------------------------------------------- */
9849 +
9850 +#define AuTestEmpty_WHONLY     1
9851 +#define AuTestEmpty_CALLED     (1 << 1)
9852 +#define AuTestEmpty_SHWH       (1 << 2)
9853 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
9854 +#define au_fset_testempty(flags, name) \
9855 +       do { (flags) |= AuTestEmpty_##name; } while (0)
9856 +#define au_fclr_testempty(flags, name) \
9857 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
9858 +
9859 +#ifndef CONFIG_AUFS_SHWH
9860 +#undef AuTestEmpty_SHWH
9861 +#define AuTestEmpty_SHWH       0
9862 +#endif
9863 +
9864 +struct test_empty_arg {
9865 +       struct dir_context ctx;
9866 +       struct au_nhash *whlist;
9867 +       unsigned int flags;
9868 +       int err;
9869 +       aufs_bindex_t bindex;
9870 +};
9871 +
9872 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
9873 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
9874 +                        unsigned int d_type)
9875 +{
9876 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
9877 +                                                 ctx);
9878 +       char *name = (void *)__name;
9879 +
9880 +       arg->err = 0;
9881 +       au_fset_testempty(arg->flags, CALLED);
9882 +       /* smp_mb(); */
9883 +       if (name[0] == '.'
9884 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
9885 +               goto out; /* success */
9886 +
9887 +       if (namelen <= AUFS_WH_PFX_LEN
9888 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
9889 +               if (au_ftest_testempty(arg->flags, WHONLY)
9890 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
9891 +                       arg->err = -ENOTEMPTY;
9892 +               goto out;
9893 +       }
9894 +
9895 +       name += AUFS_WH_PFX_LEN;
9896 +       namelen -= AUFS_WH_PFX_LEN;
9897 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
9898 +               arg->err = au_nhash_append_wh
9899 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
9900 +                        au_ftest_testempty(arg->flags, SHWH));
9901 +
9902 +out:
9903 +       /* smp_mb(); */
9904 +       AuTraceErr(arg->err);
9905 +       return arg->err;
9906 +}
9907 +
9908 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9909 +{
9910 +       int err;
9911 +       struct file *h_file;
9912 +
9913 +       h_file = au_h_open(dentry, arg->bindex,
9914 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
9915 +                          /*file*/NULL, /*force_wr*/0);
9916 +       err = PTR_ERR(h_file);
9917 +       if (IS_ERR(h_file))
9918 +               goto out;
9919 +
9920 +       err = 0;
9921 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
9922 +           && !file_inode(h_file)->i_nlink)
9923 +               goto out_put;
9924 +
9925 +       do {
9926 +               arg->err = 0;
9927 +               au_fclr_testempty(arg->flags, CALLED);
9928 +               /* smp_mb(); */
9929 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
9930 +               if (err >= 0)
9931 +                       err = arg->err;
9932 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
9933 +
9934 +out_put:
9935 +       fput(h_file);
9936 +       au_sbr_put(dentry->d_sb, arg->bindex);
9937 +out:
9938 +       return err;
9939 +}
9940 +
9941 +struct do_test_empty_args {
9942 +       int *errp;
9943 +       struct dentry *dentry;
9944 +       struct test_empty_arg *arg;
9945 +};
9946 +
9947 +static void call_do_test_empty(void *args)
9948 +{
9949 +       struct do_test_empty_args *a = args;
9950 +       *a->errp = do_test_empty(a->dentry, a->arg);
9951 +}
9952 +
9953 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9954 +{
9955 +       int err, wkq_err;
9956 +       struct dentry *h_dentry;
9957 +       struct inode *h_inode;
9958 +
9959 +       h_dentry = au_h_dptr(dentry, arg->bindex);
9960 +       h_inode = h_dentry->d_inode;
9961 +       /* todo: i_mode changes anytime? */
9962 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
9963 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
9964 +       mutex_unlock(&h_inode->i_mutex);
9965 +       if (!err)
9966 +               err = do_test_empty(dentry, arg);
9967 +       else {
9968 +               struct do_test_empty_args args = {
9969 +                       .errp   = &err,
9970 +                       .dentry = dentry,
9971 +                       .arg    = arg
9972 +               };
9973 +               unsigned int flags = arg->flags;
9974 +
9975 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
9976 +               if (unlikely(wkq_err))
9977 +                       err = wkq_err;
9978 +               arg->flags = flags;
9979 +       }
9980 +
9981 +       return err;
9982 +}
9983 +
9984 +int au_test_empty_lower(struct dentry *dentry)
9985 +{
9986 +       int err;
9987 +       unsigned int rdhash;
9988 +       aufs_bindex_t bindex, bstart, btail;
9989 +       struct au_nhash whlist;
9990 +       struct test_empty_arg arg = {
9991 +               .ctx = {
9992 +                       .actor = test_empty_cb
9993 +               }
9994 +       };
9995 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
9996 +
9997 +       SiMustAnyLock(dentry->d_sb);
9998 +
9999 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10000 +       if (!rdhash)
10001 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10002 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10003 +       if (unlikely(err))
10004 +               goto out;
10005 +
10006 +       arg.flags = 0;
10007 +       arg.whlist = &whlist;
10008 +       bstart = au_dbstart(dentry);
10009 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10010 +               au_fset_testempty(arg.flags, SHWH);
10011 +       test_empty = do_test_empty;
10012 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10013 +               test_empty = sio_test_empty;
10014 +       arg.bindex = bstart;
10015 +       err = test_empty(dentry, &arg);
10016 +       if (unlikely(err))
10017 +               goto out_whlist;
10018 +
10019 +       au_fset_testempty(arg.flags, WHONLY);
10020 +       btail = au_dbtaildir(dentry);
10021 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
10022 +               struct dentry *h_dentry;
10023 +
10024 +               h_dentry = au_h_dptr(dentry, bindex);
10025 +               if (h_dentry && h_dentry->d_inode) {
10026 +                       arg.bindex = bindex;
10027 +                       err = test_empty(dentry, &arg);
10028 +               }
10029 +       }
10030 +
10031 +out_whlist:
10032 +       au_nhash_wh_free(&whlist);
10033 +out:
10034 +       return err;
10035 +}
10036 +
10037 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10038 +{
10039 +       int err;
10040 +       struct test_empty_arg arg = {
10041 +               .ctx = {
10042 +                       .actor = test_empty_cb
10043 +               }
10044 +       };
10045 +       aufs_bindex_t bindex, btail;
10046 +
10047 +       err = 0;
10048 +       arg.whlist = whlist;
10049 +       arg.flags = AuTestEmpty_WHONLY;
10050 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10051 +               au_fset_testempty(arg.flags, SHWH);
10052 +       btail = au_dbtaildir(dentry);
10053 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
10054 +               struct dentry *h_dentry;
10055 +
10056 +               h_dentry = au_h_dptr(dentry, bindex);
10057 +               if (h_dentry && h_dentry->d_inode) {
10058 +                       arg.bindex = bindex;
10059 +                       err = sio_test_empty(dentry, &arg);
10060 +               }
10061 +       }
10062 +
10063 +       return err;
10064 +}
10065 +
10066 +/* ---------------------------------------------------------------------- */
10067 +
10068 +const struct file_operations aufs_dir_fop = {
10069 +       .owner          = THIS_MODULE,
10070 +       .llseek         = default_llseek,
10071 +       .read           = generic_read_dir,
10072 +       .iterate        = aufs_iterate,
10073 +       .unlocked_ioctl = aufs_ioctl_dir,
10074 +#ifdef CONFIG_COMPAT
10075 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10076 +#endif
10077 +       .open           = aufs_open_dir,
10078 +       .release        = aufs_release_dir,
10079 +       .flush          = aufs_flush_dir,
10080 +       .fsync          = aufs_fsync_dir
10081 +};
10082 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10083 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10084 +++ linux/fs/aufs/dir.h 2015-06-22 08:27:37.877501704 +0200
10085 @@ -0,0 +1,131 @@
10086 +/*
10087 + * Copyright (C) 2005-2015 Junjiro R. Okajima
10088 + *
10089 + * This program, aufs is free software; you can redistribute it and/or modify
10090 + * it under the terms of the GNU General Public License as published by
10091 + * the Free Software Foundation; either version 2 of the License, or
10092 + * (at your option) any later version.
10093 + *
10094 + * This program is distributed in the hope that it will be useful,
10095 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10096 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10097 + * GNU General Public License for more details.
10098 + *
10099 + * You should have received a copy of the GNU General Public License
10100 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10101 + */
10102 +
10103 +/*
10104 + * directory operations
10105 + */
10106 +
10107 +#ifndef __AUFS_DIR_H__
10108 +#define __AUFS_DIR_H__
10109 +
10110 +#ifdef __KERNEL__
10111 +
10112 +#include <linux/fs.h>
10113 +
10114 +/* ---------------------------------------------------------------------- */
10115 +
10116 +/* need to be faster and smaller */
10117 +
10118 +struct au_nhash {
10119 +       unsigned int            nh_num;
10120 +       struct hlist_head       *nh_head;
10121 +};
10122 +
10123 +struct au_vdir_destr {
10124 +       unsigned char   len;
10125 +       unsigned char   name[0];
10126 +} __packed;
10127 +
10128 +struct au_vdir_dehstr {
10129 +       struct hlist_node       hash;
10130 +       struct au_vdir_destr    *str;
10131 +} ____cacheline_aligned_in_smp;
10132 +
10133 +struct au_vdir_de {
10134 +       ino_t                   de_ino;
10135 +       unsigned char           de_type;
10136 +       /* caution: packed */
10137 +       struct au_vdir_destr    de_str;
10138 +} __packed;
10139 +
10140 +struct au_vdir_wh {
10141 +       struct hlist_node       wh_hash;
10142 +#ifdef CONFIG_AUFS_SHWH
10143 +       ino_t                   wh_ino;
10144 +       aufs_bindex_t           wh_bindex;
10145 +       unsigned char           wh_type;
10146 +#else
10147 +       aufs_bindex_t           wh_bindex;
10148 +#endif
10149 +       /* caution: packed */
10150 +       struct au_vdir_destr    wh_str;
10151 +} __packed;
10152 +
10153 +union au_vdir_deblk_p {
10154 +       unsigned char           *deblk;
10155 +       struct au_vdir_de       *de;
10156 +};
10157 +
10158 +struct au_vdir {
10159 +       unsigned char   **vd_deblk;
10160 +       unsigned long   vd_nblk;
10161 +       struct {
10162 +               unsigned long           ul;
10163 +               union au_vdir_deblk_p   p;
10164 +       } vd_last;
10165 +
10166 +       unsigned long   vd_version;
10167 +       unsigned int    vd_deblk_sz;
10168 +       unsigned long   vd_jiffy;
10169 +} ____cacheline_aligned_in_smp;
10170 +
10171 +/* ---------------------------------------------------------------------- */
10172 +
10173 +/* dir.c */
10174 +extern const struct file_operations aufs_dir_fop;
10175 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10176 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10177 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10178 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10179 +int au_test_empty_lower(struct dentry *dentry);
10180 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10181 +
10182 +/* vdir.c */
10183 +unsigned int au_rdhash_est(loff_t sz);
10184 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10185 +void au_nhash_wh_free(struct au_nhash *whlist);
10186 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10187 +                           int limit);
10188 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10189 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10190 +                      unsigned int d_type, aufs_bindex_t bindex,
10191 +                      unsigned char shwh);
10192 +void au_vdir_free(struct au_vdir *vdir);
10193 +int au_vdir_init(struct file *file);
10194 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10195 +
10196 +/* ioctl.c */
10197 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10198 +
10199 +#ifdef CONFIG_AUFS_RDU
10200 +/* rdu.c */
10201 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10202 +#ifdef CONFIG_COMPAT
10203 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10204 +                        unsigned long arg);
10205 +#endif
10206 +#else
10207 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10208 +       unsigned int cmd, unsigned long arg)
10209 +#ifdef CONFIG_COMPAT
10210 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10211 +       unsigned int cmd, unsigned long arg)
10212 +#endif
10213 +#endif
10214 +
10215 +#endif /* __KERNEL__ */
10216 +#endif /* __AUFS_DIR_H__ */
10217 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
10218 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
10219 +++ linux/fs/aufs/dynop.c       2015-06-22 08:29:23.706757681 +0200
10220 @@ -0,0 +1,369 @@
10221 +/*
10222 + * Copyright (C) 2010-2015 Junjiro R. Okajima
10223 + *
10224 + * This program, aufs is free software; you can redistribute it and/or modify
10225 + * it under the terms of the GNU General Public License as published by
10226 + * the Free Software Foundation; either version 2 of the License, or
10227 + * (at your option) any later version.
10228 + *
10229 + * This program is distributed in the hope that it will be useful,
10230 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10231 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10232 + * GNU General Public License for more details.
10233 + *
10234 + * You should have received a copy of the GNU General Public License
10235 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10236 + */
10237 +
10238 +/*
10239 + * dynamically customizable operations for regular files
10240 + */
10241 +
10242 +#include "aufs.h"
10243 +
10244 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
10245 +
10246 +/*
10247 + * How large will these lists be?
10248 + * Usually just a few elements, 20-30 at most for each, I guess.
10249 + */
10250 +static struct au_splhead dynop[AuDyLast];
10251 +
10252 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
10253 +{
10254 +       struct au_dykey *key, *tmp;
10255 +       struct list_head *head;
10256 +
10257 +       key = NULL;
10258 +       head = &spl->head;
10259 +       rcu_read_lock();
10260 +       list_for_each_entry_rcu(tmp, head, dk_list)
10261 +               if (tmp->dk_op.dy_hop == h_op) {
10262 +                       key = tmp;
10263 +                       kref_get(&key->dk_kref);
10264 +                       break;
10265 +               }
10266 +       rcu_read_unlock();
10267 +
10268 +       return key;
10269 +}
10270 +
10271 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
10272 +{
10273 +       struct au_dykey **k, *found;
10274 +       const void *h_op = key->dk_op.dy_hop;
10275 +       int i;
10276 +
10277 +       found = NULL;
10278 +       k = br->br_dykey;
10279 +       for (i = 0; i < AuBrDynOp; i++)
10280 +               if (k[i]) {
10281 +                       if (k[i]->dk_op.dy_hop == h_op) {
10282 +                               found = k[i];
10283 +                               break;
10284 +                       }
10285 +               } else
10286 +                       break;
10287 +       if (!found) {
10288 +               spin_lock(&br->br_dykey_lock);
10289 +               for (; i < AuBrDynOp; i++)
10290 +                       if (k[i]) {
10291 +                               if (k[i]->dk_op.dy_hop == h_op) {
10292 +                                       found = k[i];
10293 +                                       break;
10294 +                               }
10295 +                       } else {
10296 +                               k[i] = key;
10297 +                               break;
10298 +                       }
10299 +               spin_unlock(&br->br_dykey_lock);
10300 +               BUG_ON(i == AuBrDynOp); /* expand the array */
10301 +       }
10302 +
10303 +       return found;
10304 +}
10305 +
10306 +/* kref_get() if @key is already added */
10307 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
10308 +{
10309 +       struct au_dykey *tmp, *found;
10310 +       struct list_head *head;
10311 +       const void *h_op = key->dk_op.dy_hop;
10312 +
10313 +       found = NULL;
10314 +       head = &spl->head;
10315 +       spin_lock(&spl->spin);
10316 +       list_for_each_entry(tmp, head, dk_list)
10317 +               if (tmp->dk_op.dy_hop == h_op) {
10318 +                       kref_get(&tmp->dk_kref);
10319 +                       found = tmp;
10320 +                       break;
10321 +               }
10322 +       if (!found)
10323 +               list_add_rcu(&key->dk_list, head);
10324 +       spin_unlock(&spl->spin);
10325 +
10326 +       if (!found)
10327 +               DyPrSym(key);
10328 +       return found;
10329 +}
10330 +
10331 +static void dy_free_rcu(struct rcu_head *rcu)
10332 +{
10333 +       struct au_dykey *key;
10334 +
10335 +       key = container_of(rcu, struct au_dykey, dk_rcu);
10336 +       DyPrSym(key);
10337 +       kfree(key);
10338 +}
10339 +
10340 +static void dy_free(struct kref *kref)
10341 +{
10342 +       struct au_dykey *key;
10343 +       struct au_splhead *spl;
10344 +
10345 +       key = container_of(kref, struct au_dykey, dk_kref);
10346 +       spl = dynop + key->dk_op.dy_type;
10347 +       au_spl_del_rcu(&key->dk_list, spl);
10348 +       call_rcu(&key->dk_rcu, dy_free_rcu);
10349 +}
10350 +
10351 +void au_dy_put(struct au_dykey *key)
10352 +{
10353 +       kref_put(&key->dk_kref, dy_free);
10354 +}
10355 +
10356 +/* ---------------------------------------------------------------------- */
10357 +
10358 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
10359 +
10360 +#ifdef CONFIG_AUFS_DEBUG
10361 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
10362 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
10363 +#else
10364 +#define DyDbgDeclare(cnt)      do {} while (0)
10365 +#define DyDbgInc(cnt)          do {} while (0)
10366 +#endif
10367 +
10368 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
10369 +       DyDbgInc(cnt);                                                  \
10370 +       if (h_op->func) {                                               \
10371 +               if (src.func)                                           \
10372 +                       dst.func = src.func;                            \
10373 +               else                                                    \
10374 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
10375 +       }                                                               \
10376 +} while (0)
10377 +
10378 +#define DySetForce(func, dst, src) do {                \
10379 +       AuDebugOn(!src.func);                   \
10380 +       DyDbgInc(cnt);                          \
10381 +       dst.func = src.func;                    \
10382 +} while (0)
10383 +
10384 +#define DySetAop(func) \
10385 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
10386 +#define DySetAopForce(func) \
10387 +       DySetForce(func, dyaop->da_op, aufs_aop)
10388 +
10389 +static void dy_aop(struct au_dykey *key, const void *h_op,
10390 +                  struct super_block *h_sb __maybe_unused)
10391 +{
10392 +       struct au_dyaop *dyaop = (void *)key;
10393 +       const struct address_space_operations *h_aop = h_op;
10394 +       DyDbgDeclare(cnt);
10395 +
10396 +       AuDbg("%s\n", au_sbtype(h_sb));
10397 +
10398 +       DySetAop(writepage);
10399 +       DySetAopForce(readpage);        /* force */
10400 +       DySetAop(writepages);
10401 +       DySetAop(set_page_dirty);
10402 +       DySetAop(readpages);
10403 +       DySetAop(write_begin);
10404 +       DySetAop(write_end);
10405 +       DySetAop(bmap);
10406 +       DySetAop(invalidatepage);
10407 +       DySetAop(releasepage);
10408 +       DySetAop(freepage);
10409 +       /* this one will be changed according to an aufs mount option */
10410 +       DySetAop(direct_IO);
10411 +       DySetAop(migratepage);
10412 +       DySetAop(launder_page);
10413 +       DySetAop(is_partially_uptodate);
10414 +       DySetAop(is_dirty_writeback);
10415 +       DySetAop(error_remove_page);
10416 +       DySetAop(swap_activate);
10417 +       DySetAop(swap_deactivate);
10418 +
10419 +       DyDbgSize(cnt, *h_aop);
10420 +}
10421 +
10422 +/* ---------------------------------------------------------------------- */
10423 +
10424 +static void dy_bug(struct kref *kref)
10425 +{
10426 +       BUG();
10427 +}
10428 +
10429 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
10430 +{
10431 +       struct au_dykey *key, *old;
10432 +       struct au_splhead *spl;
10433 +       struct op {
10434 +               unsigned int sz;
10435 +               void (*set)(struct au_dykey *key, const void *h_op,
10436 +                           struct super_block *h_sb __maybe_unused);
10437 +       };
10438 +       static const struct op a[] = {
10439 +               [AuDy_AOP] = {
10440 +                       .sz     = sizeof(struct au_dyaop),
10441 +                       .set    = dy_aop
10442 +               }
10443 +       };
10444 +       const struct op *p;
10445 +
10446 +       spl = dynop + op->dy_type;
10447 +       key = dy_gfind_get(spl, op->dy_hop);
10448 +       if (key)
10449 +               goto out_add; /* success */
10450 +
10451 +       p = a + op->dy_type;
10452 +       key = kzalloc(p->sz, GFP_NOFS);
10453 +       if (unlikely(!key)) {
10454 +               key = ERR_PTR(-ENOMEM);
10455 +               goto out;
10456 +       }
10457 +
10458 +       key->dk_op.dy_hop = op->dy_hop;
10459 +       kref_init(&key->dk_kref);
10460 +       p->set(key, op->dy_hop, au_br_sb(br));
10461 +       old = dy_gadd(spl, key);
10462 +       if (old) {
10463 +               kfree(key);
10464 +               key = old;
10465 +       }
10466 +
10467 +out_add:
10468 +       old = dy_bradd(br, key);
10469 +       if (old)
10470 +               /* its ref-count should never be zero here */
10471 +               kref_put(&key->dk_kref, dy_bug);
10472 +out:
10473 +       return key;
10474 +}
10475 +
10476 +/* ---------------------------------------------------------------------- */
10477 +/*
10478 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
10479 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
10480 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
10481 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
10482 + * See the aufs manual in detail.
10483 + */
10484 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
10485 +{
10486 +       if (!do_dx)
10487 +               dyaop->da_op.direct_IO = NULL;
10488 +       else
10489 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
10490 +}
10491 +
10492 +static struct au_dyaop *dy_aget(struct au_branch *br,
10493 +                               const struct address_space_operations *h_aop,
10494 +                               int do_dx)
10495 +{
10496 +       struct au_dyaop *dyaop;
10497 +       struct au_dynop op;
10498 +
10499 +       op.dy_type = AuDy_AOP;
10500 +       op.dy_haop = h_aop;
10501 +       dyaop = (void *)dy_get(&op, br);
10502 +       if (IS_ERR(dyaop))
10503 +               goto out;
10504 +       dy_adx(dyaop, do_dx);
10505 +
10506 +out:
10507 +       return dyaop;
10508 +}
10509 +
10510 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10511 +               struct inode *h_inode)
10512 +{
10513 +       int err, do_dx;
10514 +       struct super_block *sb;
10515 +       struct au_branch *br;
10516 +       struct au_dyaop *dyaop;
10517 +
10518 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
10519 +       IiMustWriteLock(inode);
10520 +
10521 +       sb = inode->i_sb;
10522 +       br = au_sbr(sb, bindex);
10523 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
10524 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
10525 +       err = PTR_ERR(dyaop);
10526 +       if (IS_ERR(dyaop))
10527 +               /* unnecessary to call dy_fput() */
10528 +               goto out;
10529 +
10530 +       err = 0;
10531 +       inode->i_mapping->a_ops = &dyaop->da_op;
10532 +
10533 +out:
10534 +       return err;
10535 +}
10536 +
10537 +/*
10538 + * Is it safe to replace a_ops during the inode/file is in operation?
10539 + * Yes, I hope so.
10540 + */
10541 +int au_dy_irefresh(struct inode *inode)
10542 +{
10543 +       int err;
10544 +       aufs_bindex_t bstart;
10545 +       struct inode *h_inode;
10546 +
10547 +       err = 0;
10548 +       if (S_ISREG(inode->i_mode)) {
10549 +               bstart = au_ibstart(inode);
10550 +               h_inode = au_h_iptr(inode, bstart);
10551 +               err = au_dy_iaop(inode, bstart, h_inode);
10552 +       }
10553 +       return err;
10554 +}
10555 +
10556 +void au_dy_arefresh(int do_dx)
10557 +{
10558 +       struct au_splhead *spl;
10559 +       struct list_head *head;
10560 +       struct au_dykey *key;
10561 +
10562 +       spl = dynop + AuDy_AOP;
10563 +       head = &spl->head;
10564 +       spin_lock(&spl->spin);
10565 +       list_for_each_entry(key, head, dk_list)
10566 +               dy_adx((void *)key, do_dx);
10567 +       spin_unlock(&spl->spin);
10568 +}
10569 +
10570 +/* ---------------------------------------------------------------------- */
10571 +
10572 +void __init au_dy_init(void)
10573 +{
10574 +       int i;
10575 +
10576 +       /* make sure that 'struct au_dykey *' can be any type */
10577 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
10578 +
10579 +       for (i = 0; i < AuDyLast; i++)
10580 +               au_spl_init(dynop + i);
10581 +}
10582 +
10583 +void au_dy_fin(void)
10584 +{
10585 +       int i;
10586 +
10587 +       for (i = 0; i < AuDyLast; i++)
10588 +               WARN_ON(!list_empty(&dynop[i].head));
10589 +}
10590 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
10591 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
10592 +++ linux/fs/aufs/dynop.h       2015-06-22 08:29:23.706757681 +0200
10593 @@ -0,0 +1,74 @@
10594 +/*
10595 + * Copyright (C) 2010-2015 Junjiro R. Okajima
10596 + *
10597 + * This program, aufs is free software; you can redistribute it and/or modify
10598 + * it under the terms of the GNU General Public License as published by
10599 + * the Free Software Foundation; either version 2 of the License, or
10600 + * (at your option) any later version.
10601 + *
10602 + * This program is distributed in the hope that it will be useful,
10603 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10604 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10605 + * GNU General Public License for more details.
10606 + *
10607 + * You should have received a copy of the GNU General Public License
10608 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10609 + */
10610 +
10611 +/*
10612 + * dynamically customizable operations (for regular files only)
10613 + */
10614 +
10615 +#ifndef __AUFS_DYNOP_H__
10616 +#define __AUFS_DYNOP_H__
10617 +
10618 +#ifdef __KERNEL__
10619 +
10620 +#include <linux/fs.h>
10621 +#include <linux/kref.h>
10622 +
10623 +enum {AuDy_AOP, AuDyLast};
10624 +
10625 +struct au_dynop {
10626 +       int                                             dy_type;
10627 +       union {
10628 +               const void                              *dy_hop;
10629 +               const struct address_space_operations   *dy_haop;
10630 +       };
10631 +};
10632 +
10633 +struct au_dykey {
10634 +       union {
10635 +               struct list_head        dk_list;
10636 +               struct rcu_head         dk_rcu;
10637 +       };
10638 +       struct au_dynop         dk_op;
10639 +
10640 +       /*
10641 +        * during I am in the branch local array, kref is gotten. when the
10642 +        * branch is removed, kref is put.
10643 +        */
10644 +       struct kref             dk_kref;
10645 +};
10646 +
10647 +/* stop unioning since their sizes are very different from each other */
10648 +struct au_dyaop {
10649 +       struct au_dykey                 da_key;
10650 +       struct address_space_operations da_op; /* not const */
10651 +};
10652 +
10653 +/* ---------------------------------------------------------------------- */
10654 +
10655 +/* dynop.c */
10656 +struct au_branch;
10657 +void au_dy_put(struct au_dykey *key);
10658 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10659 +               struct inode *h_inode);
10660 +int au_dy_irefresh(struct inode *inode);
10661 +void au_dy_arefresh(int do_dio);
10662 +
10663 +void __init au_dy_init(void);
10664 +void au_dy_fin(void);
10665 +
10666 +#endif /* __KERNEL__ */
10667 +#endif /* __AUFS_DYNOP_H__ */
10668 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
10669 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
10670 +++ linux/fs/aufs/export.c      2015-06-22 08:29:23.706757681 +0200
10671 @@ -0,0 +1,831 @@
10672 +/*
10673 + * Copyright (C) 2005-2015 Junjiro R. Okajima
10674 + *
10675 + * This program, aufs is free software; you can redistribute it and/or modify
10676 + * it under the terms of the GNU General Public License as published by
10677 + * the Free Software Foundation; either version 2 of the License, or
10678 + * (at your option) any later version.
10679 + *
10680 + * This program is distributed in the hope that it will be useful,
10681 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10682 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10683 + * GNU General Public License for more details.
10684 + *
10685 + * You should have received a copy of the GNU General Public License
10686 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10687 + */
10688 +
10689 +/*
10690 + * export via nfs
10691 + */
10692 +
10693 +#include <linux/exportfs.h>
10694 +#include <linux/fs_struct.h>
10695 +#include <linux/namei.h>
10696 +#include <linux/nsproxy.h>
10697 +#include <linux/random.h>
10698 +#include <linux/writeback.h>
10699 +#include "../fs/mount.h"
10700 +#include "aufs.h"
10701 +
10702 +union conv {
10703 +#ifdef CONFIG_AUFS_INO_T_64
10704 +       __u32 a[2];
10705 +#else
10706 +       __u32 a[1];
10707 +#endif
10708 +       ino_t ino;
10709 +};
10710 +
10711 +static ino_t decode_ino(__u32 *a)
10712 +{
10713 +       union conv u;
10714 +
10715 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
10716 +       u.a[0] = a[0];
10717 +#ifdef CONFIG_AUFS_INO_T_64
10718 +       u.a[1] = a[1];
10719 +#endif
10720 +       return u.ino;
10721 +}
10722 +
10723 +static void encode_ino(__u32 *a, ino_t ino)
10724 +{
10725 +       union conv u;
10726 +
10727 +       u.ino = ino;
10728 +       a[0] = u.a[0];
10729 +#ifdef CONFIG_AUFS_INO_T_64
10730 +       a[1] = u.a[1];
10731 +#endif
10732 +}
10733 +
10734 +/* NFS file handle */
10735 +enum {
10736 +       Fh_br_id,
10737 +       Fh_sigen,
10738 +#ifdef CONFIG_AUFS_INO_T_64
10739 +       /* support 64bit inode number */
10740 +       Fh_ino1,
10741 +       Fh_ino2,
10742 +       Fh_dir_ino1,
10743 +       Fh_dir_ino2,
10744 +#else
10745 +       Fh_ino1,
10746 +       Fh_dir_ino1,
10747 +#endif
10748 +       Fh_igen,
10749 +       Fh_h_type,
10750 +       Fh_tail,
10751 +
10752 +       Fh_ino = Fh_ino1,
10753 +       Fh_dir_ino = Fh_dir_ino1
10754 +};
10755 +
10756 +static int au_test_anon(struct dentry *dentry)
10757 +{
10758 +       /* note: read d_flags without d_lock */
10759 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
10760 +}
10761 +
10762 +int au_test_nfsd(void)
10763 +{
10764 +       int ret;
10765 +       struct task_struct *tsk = current;
10766 +       char comm[sizeof(tsk->comm)];
10767 +
10768 +       ret = 0;
10769 +       if (tsk->flags & PF_KTHREAD) {
10770 +               get_task_comm(comm, tsk);
10771 +               ret = !strcmp(comm, "nfsd");
10772 +       }
10773 +
10774 +       return ret;
10775 +}
10776 +
10777 +/* ---------------------------------------------------------------------- */
10778 +/* inode generation external table */
10779 +
10780 +void au_xigen_inc(struct inode *inode)
10781 +{
10782 +       loff_t pos;
10783 +       ssize_t sz;
10784 +       __u32 igen;
10785 +       struct super_block *sb;
10786 +       struct au_sbinfo *sbinfo;
10787 +
10788 +       sb = inode->i_sb;
10789 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
10790 +
10791 +       sbinfo = au_sbi(sb);
10792 +       pos = inode->i_ino;
10793 +       pos *= sizeof(igen);
10794 +       igen = inode->i_generation + 1;
10795 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
10796 +                        sizeof(igen), &pos);
10797 +       if (sz == sizeof(igen))
10798 +               return; /* success */
10799 +
10800 +       if (unlikely(sz >= 0))
10801 +               AuIOErr("xigen error (%zd)\n", sz);
10802 +}
10803 +
10804 +int au_xigen_new(struct inode *inode)
10805 +{
10806 +       int err;
10807 +       loff_t pos;
10808 +       ssize_t sz;
10809 +       struct super_block *sb;
10810 +       struct au_sbinfo *sbinfo;
10811 +       struct file *file;
10812 +
10813 +       err = 0;
10814 +       /* todo: dirty, at mount time */
10815 +       if (inode->i_ino == AUFS_ROOT_INO)
10816 +               goto out;
10817 +       sb = inode->i_sb;
10818 +       SiMustAnyLock(sb);
10819 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
10820 +               goto out;
10821 +
10822 +       err = -EFBIG;
10823 +       pos = inode->i_ino;
10824 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
10825 +               AuIOErr1("too large i%lld\n", pos);
10826 +               goto out;
10827 +       }
10828 +       pos *= sizeof(inode->i_generation);
10829 +
10830 +       err = 0;
10831 +       sbinfo = au_sbi(sb);
10832 +       file = sbinfo->si_xigen;
10833 +       BUG_ON(!file);
10834 +
10835 +       if (vfsub_f_size_read(file)
10836 +           < pos + sizeof(inode->i_generation)) {
10837 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
10838 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
10839 +                                sizeof(inode->i_generation), &pos);
10840 +       } else
10841 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
10842 +                               sizeof(inode->i_generation), &pos);
10843 +       if (sz == sizeof(inode->i_generation))
10844 +               goto out; /* success */
10845 +
10846 +       err = sz;
10847 +       if (unlikely(sz >= 0)) {
10848 +               err = -EIO;
10849 +               AuIOErr("xigen error (%zd)\n", sz);
10850 +       }
10851 +
10852 +out:
10853 +       return err;
10854 +}
10855 +
10856 +int au_xigen_set(struct super_block *sb, struct file *base)
10857 +{
10858 +       int err;
10859 +       struct au_sbinfo *sbinfo;
10860 +       struct file *file;
10861 +
10862 +       SiMustWriteLock(sb);
10863 +
10864 +       sbinfo = au_sbi(sb);
10865 +       file = au_xino_create2(base, sbinfo->si_xigen);
10866 +       err = PTR_ERR(file);
10867 +       if (IS_ERR(file))
10868 +               goto out;
10869 +       err = 0;
10870 +       if (sbinfo->si_xigen)
10871 +               fput(sbinfo->si_xigen);
10872 +       sbinfo->si_xigen = file;
10873 +
10874 +out:
10875 +       return err;
10876 +}
10877 +
10878 +void au_xigen_clr(struct super_block *sb)
10879 +{
10880 +       struct au_sbinfo *sbinfo;
10881 +
10882 +       SiMustWriteLock(sb);
10883 +
10884 +       sbinfo = au_sbi(sb);
10885 +       if (sbinfo->si_xigen) {
10886 +               fput(sbinfo->si_xigen);
10887 +               sbinfo->si_xigen = NULL;
10888 +       }
10889 +}
10890 +
10891 +/* ---------------------------------------------------------------------- */
10892 +
10893 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
10894 +                                   ino_t dir_ino)
10895 +{
10896 +       struct dentry *dentry, *d;
10897 +       struct inode *inode;
10898 +       unsigned int sigen;
10899 +
10900 +       dentry = NULL;
10901 +       inode = ilookup(sb, ino);
10902 +       if (!inode)
10903 +               goto out;
10904 +
10905 +       dentry = ERR_PTR(-ESTALE);
10906 +       sigen = au_sigen(sb);
10907 +       if (unlikely(is_bad_inode(inode)
10908 +                    || IS_DEADDIR(inode)
10909 +                    || sigen != au_iigen(inode, NULL)))
10910 +               goto out_iput;
10911 +
10912 +       dentry = NULL;
10913 +       if (!dir_ino || S_ISDIR(inode->i_mode))
10914 +               dentry = d_find_alias(inode);
10915 +       else {
10916 +               spin_lock(&inode->i_lock);
10917 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
10918 +                       spin_lock(&d->d_lock);
10919 +                       if (!au_test_anon(d)
10920 +                           && d->d_parent->d_inode->i_ino == dir_ino) {
10921 +                               dentry = dget_dlock(d);
10922 +                               spin_unlock(&d->d_lock);
10923 +                               break;
10924 +                       }
10925 +                       spin_unlock(&d->d_lock);
10926 +               }
10927 +               spin_unlock(&inode->i_lock);
10928 +       }
10929 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
10930 +               /* need to refresh */
10931 +               dput(dentry);
10932 +               dentry = NULL;
10933 +       }
10934 +
10935 +out_iput:
10936 +       iput(inode);
10937 +out:
10938 +       AuTraceErrPtr(dentry);
10939 +       return dentry;
10940 +}
10941 +
10942 +/* ---------------------------------------------------------------------- */
10943 +
10944 +/* todo: dirty? */
10945 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
10946 +
10947 +struct au_compare_mnt_args {
10948 +       /* input */
10949 +       struct super_block *sb;
10950 +
10951 +       /* output */
10952 +       struct vfsmount *mnt;
10953 +};
10954 +
10955 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
10956 +{
10957 +       struct au_compare_mnt_args *a = arg;
10958 +
10959 +       if (mnt->mnt_sb != a->sb)
10960 +               return 0;
10961 +       a->mnt = mntget(mnt);
10962 +       return 1;
10963 +}
10964 +
10965 +static struct vfsmount *au_mnt_get(struct super_block *sb)
10966 +{
10967 +       int err;
10968 +       struct path root;
10969 +       struct au_compare_mnt_args args = {
10970 +               .sb = sb
10971 +       };
10972 +
10973 +       get_fs_root(current->fs, &root);
10974 +       rcu_read_lock();
10975 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
10976 +       rcu_read_unlock();
10977 +       path_put(&root);
10978 +       AuDebugOn(!err);
10979 +       AuDebugOn(!args.mnt);
10980 +       return args.mnt;
10981 +}
10982 +
10983 +struct au_nfsd_si_lock {
10984 +       unsigned int sigen;
10985 +       aufs_bindex_t bindex, br_id;
10986 +       unsigned char force_lock;
10987 +};
10988 +
10989 +static int si_nfsd_read_lock(struct super_block *sb,
10990 +                            struct au_nfsd_si_lock *nsi_lock)
10991 +{
10992 +       int err;
10993 +       aufs_bindex_t bindex;
10994 +
10995 +       si_read_lock(sb, AuLock_FLUSH);
10996 +
10997 +       /* branch id may be wrapped around */
10998 +       err = 0;
10999 +       bindex = au_br_index(sb, nsi_lock->br_id);
11000 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
11001 +               goto out; /* success */
11002 +
11003 +       err = -ESTALE;
11004 +       bindex = -1;
11005 +       if (!nsi_lock->force_lock)
11006 +               si_read_unlock(sb);
11007 +
11008 +out:
11009 +       nsi_lock->bindex = bindex;
11010 +       return err;
11011 +}
11012 +
11013 +struct find_name_by_ino {
11014 +       struct dir_context ctx;
11015 +       int called, found;
11016 +       ino_t ino;
11017 +       char *name;
11018 +       int namelen;
11019 +};
11020 +
11021 +static int
11022 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
11023 +                loff_t offset, u64 ino, unsigned int d_type)
11024 +{
11025 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
11026 +                                                 ctx);
11027 +
11028 +       a->called++;
11029 +       if (a->ino != ino)
11030 +               return 0;
11031 +
11032 +       memcpy(a->name, name, namelen);
11033 +       a->namelen = namelen;
11034 +       a->found = 1;
11035 +       return 1;
11036 +}
11037 +
11038 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
11039 +                                    struct au_nfsd_si_lock *nsi_lock)
11040 +{
11041 +       struct dentry *dentry, *parent;
11042 +       struct file *file;
11043 +       struct inode *dir;
11044 +       struct find_name_by_ino arg = {
11045 +               .ctx = {
11046 +                       .actor = find_name_by_ino
11047 +               }
11048 +       };
11049 +       int err;
11050 +
11051 +       parent = path->dentry;
11052 +       if (nsi_lock)
11053 +               si_read_unlock(parent->d_sb);
11054 +       file = vfsub_dentry_open(path, au_dir_roflags);
11055 +       dentry = (void *)file;
11056 +       if (IS_ERR(file))
11057 +               goto out;
11058 +
11059 +       dentry = ERR_PTR(-ENOMEM);
11060 +       arg.name = (void *)__get_free_page(GFP_NOFS);
11061 +       if (unlikely(!arg.name))
11062 +               goto out_file;
11063 +       arg.ino = ino;
11064 +       arg.found = 0;
11065 +       do {
11066 +               arg.called = 0;
11067 +               /* smp_mb(); */
11068 +               err = vfsub_iterate_dir(file, &arg.ctx);
11069 +       } while (!err && !arg.found && arg.called);
11070 +       dentry = ERR_PTR(err);
11071 +       if (unlikely(err))
11072 +               goto out_name;
11073 +       /* instead of ENOENT */
11074 +       dentry = ERR_PTR(-ESTALE);
11075 +       if (!arg.found)
11076 +               goto out_name;
11077 +
11078 +       /* do not call vfsub_lkup_one() */
11079 +       dir = parent->d_inode;
11080 +       mutex_lock(&dir->i_mutex);
11081 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
11082 +       mutex_unlock(&dir->i_mutex);
11083 +       AuTraceErrPtr(dentry);
11084 +       if (IS_ERR(dentry))
11085 +               goto out_name;
11086 +       AuDebugOn(au_test_anon(dentry));
11087 +       if (unlikely(!dentry->d_inode)) {
11088 +               dput(dentry);
11089 +               dentry = ERR_PTR(-ENOENT);
11090 +       }
11091 +
11092 +out_name:
11093 +       free_page((unsigned long)arg.name);
11094 +out_file:
11095 +       fput(file);
11096 +out:
11097 +       if (unlikely(nsi_lock
11098 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
11099 +               if (!IS_ERR(dentry)) {
11100 +                       dput(dentry);
11101 +                       dentry = ERR_PTR(-ESTALE);
11102 +               }
11103 +       AuTraceErrPtr(dentry);
11104 +       return dentry;
11105 +}
11106 +
11107 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
11108 +                                       ino_t dir_ino,
11109 +                                       struct au_nfsd_si_lock *nsi_lock)
11110 +{
11111 +       struct dentry *dentry;
11112 +       struct path path;
11113 +
11114 +       if (dir_ino != AUFS_ROOT_INO) {
11115 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
11116 +               dentry = path.dentry;
11117 +               if (!path.dentry || IS_ERR(path.dentry))
11118 +                       goto out;
11119 +               AuDebugOn(au_test_anon(path.dentry));
11120 +       } else
11121 +               path.dentry = dget(sb->s_root);
11122 +
11123 +       path.mnt = au_mnt_get(sb);
11124 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
11125 +       path_put(&path);
11126 +
11127 +out:
11128 +       AuTraceErrPtr(dentry);
11129 +       return dentry;
11130 +}
11131 +
11132 +/* ---------------------------------------------------------------------- */
11133 +
11134 +static int h_acceptable(void *expv, struct dentry *dentry)
11135 +{
11136 +       return 1;
11137 +}
11138 +
11139 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
11140 +                          char *buf, int len, struct super_block *sb)
11141 +{
11142 +       char *p;
11143 +       int n;
11144 +       struct path path;
11145 +
11146 +       p = d_path(h_rootpath, buf, len);
11147 +       if (IS_ERR(p))
11148 +               goto out;
11149 +       n = strlen(p);
11150 +
11151 +       path.mnt = h_rootpath->mnt;
11152 +       path.dentry = h_parent;
11153 +       p = d_path(&path, buf, len);
11154 +       if (IS_ERR(p))
11155 +               goto out;
11156 +       if (n != 1)
11157 +               p += n;
11158 +
11159 +       path.mnt = au_mnt_get(sb);
11160 +       path.dentry = sb->s_root;
11161 +       p = d_path(&path, buf, len - strlen(p));
11162 +       mntput(path.mnt);
11163 +       if (IS_ERR(p))
11164 +               goto out;
11165 +       if (n != 1)
11166 +               p[strlen(p)] = '/';
11167 +
11168 +out:
11169 +       AuTraceErrPtr(p);
11170 +       return p;
11171 +}
11172 +
11173 +static
11174 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
11175 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
11176 +{
11177 +       struct dentry *dentry, *h_parent, *root;
11178 +       struct super_block *h_sb;
11179 +       char *pathname, *p;
11180 +       struct vfsmount *h_mnt;
11181 +       struct au_branch *br;
11182 +       int err;
11183 +       struct path path;
11184 +
11185 +       br = au_sbr(sb, nsi_lock->bindex);
11186 +       h_mnt = au_br_mnt(br);
11187 +       h_sb = h_mnt->mnt_sb;
11188 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
11189 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
11190 +                                     fh_len - Fh_tail, fh[Fh_h_type],
11191 +                                     h_acceptable, /*context*/NULL);
11192 +       dentry = h_parent;
11193 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
11194 +               AuWarn1("%s decode_fh failed, %ld\n",
11195 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
11196 +               goto out;
11197 +       }
11198 +       dentry = NULL;
11199 +       if (unlikely(au_test_anon(h_parent))) {
11200 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
11201 +                       au_sbtype(h_sb));
11202 +               goto out_h_parent;
11203 +       }
11204 +
11205 +       dentry = ERR_PTR(-ENOMEM);
11206 +       pathname = (void *)__get_free_page(GFP_NOFS);
11207 +       if (unlikely(!pathname))
11208 +               goto out_h_parent;
11209 +
11210 +       root = sb->s_root;
11211 +       path.mnt = h_mnt;
11212 +       di_read_lock_parent(root, !AuLock_IR);
11213 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
11214 +       di_read_unlock(root, !AuLock_IR);
11215 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
11216 +       dentry = (void *)p;
11217 +       if (IS_ERR(p))
11218 +               goto out_pathname;
11219 +
11220 +       si_read_unlock(sb);
11221 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
11222 +       dentry = ERR_PTR(err);
11223 +       if (unlikely(err))
11224 +               goto out_relock;
11225 +
11226 +       dentry = ERR_PTR(-ENOENT);
11227 +       AuDebugOn(au_test_anon(path.dentry));
11228 +       if (unlikely(!path.dentry->d_inode))
11229 +               goto out_path;
11230 +
11231 +       if (ino != path.dentry->d_inode->i_ino)
11232 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
11233 +       else
11234 +               dentry = dget(path.dentry);
11235 +
11236 +out_path:
11237 +       path_put(&path);
11238 +out_relock:
11239 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
11240 +               if (!IS_ERR(dentry)) {
11241 +                       dput(dentry);
11242 +                       dentry = ERR_PTR(-ESTALE);
11243 +               }
11244 +out_pathname:
11245 +       free_page((unsigned long)pathname);
11246 +out_h_parent:
11247 +       dput(h_parent);
11248 +out:
11249 +       AuTraceErrPtr(dentry);
11250 +       return dentry;
11251 +}
11252 +
11253 +/* ---------------------------------------------------------------------- */
11254 +
11255 +static struct dentry *
11256 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
11257 +                 int fh_type)
11258 +{
11259 +       struct dentry *dentry;
11260 +       __u32 *fh = fid->raw;
11261 +       struct au_branch *br;
11262 +       ino_t ino, dir_ino;
11263 +       struct au_nfsd_si_lock nsi_lock = {
11264 +               .force_lock     = 0
11265 +       };
11266 +
11267 +       dentry = ERR_PTR(-ESTALE);
11268 +       /* it should never happen, but the file handle is unreliable */
11269 +       if (unlikely(fh_len < Fh_tail))
11270 +               goto out;
11271 +       nsi_lock.sigen = fh[Fh_sigen];
11272 +       nsi_lock.br_id = fh[Fh_br_id];
11273 +
11274 +       /* branch id may be wrapped around */
11275 +       br = NULL;
11276 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
11277 +               goto out;
11278 +       nsi_lock.force_lock = 1;
11279 +
11280 +       /* is this inode still cached? */
11281 +       ino = decode_ino(fh + Fh_ino);
11282 +       /* it should never happen */
11283 +       if (unlikely(ino == AUFS_ROOT_INO))
11284 +               goto out;
11285 +
11286 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11287 +       dentry = decode_by_ino(sb, ino, dir_ino);
11288 +       if (IS_ERR(dentry))
11289 +               goto out_unlock;
11290 +       if (dentry)
11291 +               goto accept;
11292 +
11293 +       /* is the parent dir cached? */
11294 +       br = au_sbr(sb, nsi_lock.bindex);
11295 +       atomic_inc(&br->br_count);
11296 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
11297 +       if (IS_ERR(dentry))
11298 +               goto out_unlock;
11299 +       if (dentry)
11300 +               goto accept;
11301 +
11302 +       /* lookup path */
11303 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
11304 +       if (IS_ERR(dentry))
11305 +               goto out_unlock;
11306 +       if (unlikely(!dentry))
11307 +               /* todo?: make it ESTALE */
11308 +               goto out_unlock;
11309 +
11310 +accept:
11311 +       if (!au_digen_test(dentry, au_sigen(sb))
11312 +           && dentry->d_inode->i_generation == fh[Fh_igen])
11313 +               goto out_unlock; /* success */
11314 +
11315 +       dput(dentry);
11316 +       dentry = ERR_PTR(-ESTALE);
11317 +out_unlock:
11318 +       if (br)
11319 +               atomic_dec(&br->br_count);
11320 +       si_read_unlock(sb);
11321 +out:
11322 +       AuTraceErrPtr(dentry);
11323 +       return dentry;
11324 +}
11325 +
11326 +#if 0 /* reserved for future use */
11327 +/* support subtreecheck option */
11328 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
11329 +                                       int fh_len, int fh_type)
11330 +{
11331 +       struct dentry *parent;
11332 +       __u32 *fh = fid->raw;
11333 +       ino_t dir_ino;
11334 +
11335 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11336 +       parent = decode_by_ino(sb, dir_ino, 0);
11337 +       if (IS_ERR(parent))
11338 +               goto out;
11339 +       if (!parent)
11340 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
11341 +                                       dir_ino, fh, fh_len);
11342 +
11343 +out:
11344 +       AuTraceErrPtr(parent);
11345 +       return parent;
11346 +}
11347 +#endif
11348 +
11349 +/* ---------------------------------------------------------------------- */
11350 +
11351 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
11352 +                         struct inode *dir)
11353 +{
11354 +       int err;
11355 +       aufs_bindex_t bindex;
11356 +       struct super_block *sb, *h_sb;
11357 +       struct dentry *dentry, *parent, *h_parent;
11358 +       struct inode *h_dir;
11359 +       struct au_branch *br;
11360 +
11361 +       err = -ENOSPC;
11362 +       if (unlikely(*max_len <= Fh_tail)) {
11363 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
11364 +               goto out;
11365 +       }
11366 +
11367 +       err = FILEID_ROOT;
11368 +       if (inode->i_ino == AUFS_ROOT_INO) {
11369 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
11370 +               goto out;
11371 +       }
11372 +
11373 +       h_parent = NULL;
11374 +       sb = inode->i_sb;
11375 +       err = si_read_lock(sb, AuLock_FLUSH);
11376 +       if (unlikely(err))
11377 +               goto out;
11378 +
11379 +#ifdef CONFIG_AUFS_DEBUG
11380 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11381 +               AuWarn1("NFS-exporting requires xino\n");
11382 +#endif
11383 +       err = -EIO;
11384 +       parent = NULL;
11385 +       ii_read_lock_child(inode);
11386 +       bindex = au_ibstart(inode);
11387 +       if (!dir) {
11388 +               dentry = d_find_any_alias(inode);
11389 +               if (unlikely(!dentry))
11390 +                       goto out_unlock;
11391 +               AuDebugOn(au_test_anon(dentry));
11392 +               parent = dget_parent(dentry);
11393 +               dput(dentry);
11394 +               if (unlikely(!parent))
11395 +                       goto out_unlock;
11396 +               dir = parent->d_inode;
11397 +       }
11398 +
11399 +       ii_read_lock_parent(dir);
11400 +       h_dir = au_h_iptr(dir, bindex);
11401 +       ii_read_unlock(dir);
11402 +       if (unlikely(!h_dir))
11403 +               goto out_parent;
11404 +       h_parent = d_find_any_alias(h_dir);
11405 +       if (unlikely(!h_parent))
11406 +               goto out_hparent;
11407 +
11408 +       err = -EPERM;
11409 +       br = au_sbr(sb, bindex);
11410 +       h_sb = au_br_sb(br);
11411 +       if (unlikely(!h_sb->s_export_op)) {
11412 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
11413 +               goto out_hparent;
11414 +       }
11415 +
11416 +       fh[Fh_br_id] = br->br_id;
11417 +       fh[Fh_sigen] = au_sigen(sb);
11418 +       encode_ino(fh + Fh_ino, inode->i_ino);
11419 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
11420 +       fh[Fh_igen] = inode->i_generation;
11421 +
11422 +       *max_len -= Fh_tail;
11423 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
11424 +                                          max_len,
11425 +                                          /*connectable or subtreecheck*/0);
11426 +       err = fh[Fh_h_type];
11427 +       *max_len += Fh_tail;
11428 +       /* todo: macros? */
11429 +       if (err != FILEID_INVALID)
11430 +               err = 99;
11431 +       else
11432 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
11433 +
11434 +out_hparent:
11435 +       dput(h_parent);
11436 +out_parent:
11437 +       dput(parent);
11438 +out_unlock:
11439 +       ii_read_unlock(inode);
11440 +       si_read_unlock(sb);
11441 +out:
11442 +       if (unlikely(err < 0))
11443 +               err = FILEID_INVALID;
11444 +       return err;
11445 +}
11446 +
11447 +/* ---------------------------------------------------------------------- */
11448 +
11449 +static int aufs_commit_metadata(struct inode *inode)
11450 +{
11451 +       int err;
11452 +       aufs_bindex_t bindex;
11453 +       struct super_block *sb;
11454 +       struct inode *h_inode;
11455 +       int (*f)(struct inode *inode);
11456 +
11457 +       sb = inode->i_sb;
11458 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11459 +       ii_write_lock_child(inode);
11460 +       bindex = au_ibstart(inode);
11461 +       AuDebugOn(bindex < 0);
11462 +       h_inode = au_h_iptr(inode, bindex);
11463 +
11464 +       f = h_inode->i_sb->s_export_op->commit_metadata;
11465 +       if (f)
11466 +               err = f(h_inode);
11467 +       else {
11468 +               struct writeback_control wbc = {
11469 +                       .sync_mode      = WB_SYNC_ALL,
11470 +                       .nr_to_write    = 0 /* metadata only */
11471 +               };
11472 +
11473 +               err = sync_inode(h_inode, &wbc);
11474 +       }
11475 +
11476 +       au_cpup_attr_timesizes(inode);
11477 +       ii_write_unlock(inode);
11478 +       si_read_unlock(sb);
11479 +       return err;
11480 +}
11481 +
11482 +/* ---------------------------------------------------------------------- */
11483 +
11484 +static struct export_operations aufs_export_op = {
11485 +       .fh_to_dentry           = aufs_fh_to_dentry,
11486 +       /* .fh_to_parent        = aufs_fh_to_parent, */
11487 +       .encode_fh              = aufs_encode_fh,
11488 +       .commit_metadata        = aufs_commit_metadata
11489 +};
11490 +
11491 +void au_export_init(struct super_block *sb)
11492 +{
11493 +       struct au_sbinfo *sbinfo;
11494 +       __u32 u;
11495 +
11496 +       sb->s_export_op = &aufs_export_op;
11497 +       sbinfo = au_sbi(sb);
11498 +       sbinfo->si_xigen = NULL;
11499 +       get_random_bytes(&u, sizeof(u));
11500 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
11501 +       atomic_set(&sbinfo->si_xigen_next, u);
11502 +}
11503 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
11504 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
11505 +++ linux/fs/aufs/fhsm.c        2015-06-22 08:27:37.880835119 +0200
11506 @@ -0,0 +1,426 @@
11507 +/*
11508 + * Copyright (C) 2011-2015 Junjiro R. Okajima
11509 + *
11510 + * This program, aufs is free software; you can redistribute it and/or modify
11511 + * it under the terms of the GNU General Public License as published by
11512 + * the Free Software Foundation; either version 2 of the License, or
11513 + * (at your option) any later version.
11514 + *
11515 + * This program is distributed in the hope that it will be useful,
11516 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11517 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11518 + * GNU General Public License for more details.
11519 + *
11520 + * You should have received a copy of the GNU General Public License
11521 + * along with this program; if not, write to the Free Software
11522 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11523 + */
11524 +
11525 +/*
11526 + * File-based Hierarchy Storage Management
11527 + */
11528 +
11529 +#include <linux/anon_inodes.h>
11530 +#include <linux/poll.h>
11531 +#include <linux/seq_file.h>
11532 +#include <linux/statfs.h>
11533 +#include "aufs.h"
11534 +
11535 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
11536 +{
11537 +       struct au_sbinfo *sbinfo;
11538 +       struct au_fhsm *fhsm;
11539 +
11540 +       SiMustAnyLock(sb);
11541 +
11542 +       sbinfo = au_sbi(sb);
11543 +       fhsm = &sbinfo->si_fhsm;
11544 +       AuDebugOn(!fhsm);
11545 +       return fhsm->fhsm_bottom;
11546 +}
11547 +
11548 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
11549 +{
11550 +       struct au_sbinfo *sbinfo;
11551 +       struct au_fhsm *fhsm;
11552 +
11553 +       SiMustWriteLock(sb);
11554 +
11555 +       sbinfo = au_sbi(sb);
11556 +       fhsm = &sbinfo->si_fhsm;
11557 +       AuDebugOn(!fhsm);
11558 +       fhsm->fhsm_bottom = bindex;
11559 +}
11560 +
11561 +/* ---------------------------------------------------------------------- */
11562 +
11563 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
11564 +{
11565 +       struct au_br_fhsm *bf;
11566 +
11567 +       bf = br->br_fhsm;
11568 +       MtxMustLock(&bf->bf_lock);
11569 +
11570 +       return !bf->bf_readable
11571 +               || time_after(jiffies,
11572 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
11573 +}
11574 +
11575 +/* ---------------------------------------------------------------------- */
11576 +
11577 +static void au_fhsm_notify(struct super_block *sb, int val)
11578 +{
11579 +       struct au_sbinfo *sbinfo;
11580 +       struct au_fhsm *fhsm;
11581 +
11582 +       SiMustAnyLock(sb);
11583 +
11584 +       sbinfo = au_sbi(sb);
11585 +       fhsm = &sbinfo->si_fhsm;
11586 +       if (au_fhsm_pid(fhsm)
11587 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
11588 +               atomic_set(&fhsm->fhsm_readable, val);
11589 +               if (val)
11590 +                       wake_up(&fhsm->fhsm_wqh);
11591 +       }
11592 +}
11593 +
11594 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
11595 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
11596 +{
11597 +       int err;
11598 +       struct au_branch *br;
11599 +       struct au_br_fhsm *bf;
11600 +
11601 +       br = au_sbr(sb, bindex);
11602 +       AuDebugOn(au_br_rdonly(br));
11603 +       bf = br->br_fhsm;
11604 +       AuDebugOn(!bf);
11605 +
11606 +       if (do_lock)
11607 +               mutex_lock(&bf->bf_lock);
11608 +       else
11609 +               MtxMustLock(&bf->bf_lock);
11610 +
11611 +       /* sb->s_root for NFS is unreliable */
11612 +       err = au_br_stfs(br, &bf->bf_stfs);
11613 +       if (unlikely(err)) {
11614 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
11615 +               goto out;
11616 +       }
11617 +
11618 +       bf->bf_jiffy = jiffies;
11619 +       bf->bf_readable = 1;
11620 +       if (do_notify)
11621 +               au_fhsm_notify(sb, /*val*/1);
11622 +       if (rstfs)
11623 +               *rstfs = bf->bf_stfs;
11624 +
11625 +out:
11626 +       if (do_lock)
11627 +               mutex_unlock(&bf->bf_lock);
11628 +       au_fhsm_notify(sb, /*val*/1);
11629 +
11630 +       return err;
11631 +}
11632 +
11633 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
11634 +{
11635 +       int err;
11636 +       struct au_sbinfo *sbinfo;
11637 +       struct au_fhsm *fhsm;
11638 +       struct au_branch *br;
11639 +       struct au_br_fhsm *bf;
11640 +
11641 +       AuDbg("b%d, force %d\n", bindex, force);
11642 +       SiMustAnyLock(sb);
11643 +
11644 +       sbinfo = au_sbi(sb);
11645 +       fhsm = &sbinfo->si_fhsm;
11646 +       if (!au_ftest_si(sbinfo, FHSM)
11647 +           || fhsm->fhsm_bottom == bindex)
11648 +               return;
11649 +
11650 +       br = au_sbr(sb, bindex);
11651 +       bf = br->br_fhsm;
11652 +       AuDebugOn(!bf);
11653 +       mutex_lock(&bf->bf_lock);
11654 +       if (force
11655 +           || au_fhsm_pid(fhsm)
11656 +           || au_fhsm_test_jiffy(sbinfo, br))
11657 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
11658 +                                 /*do_notify*/1);
11659 +       mutex_unlock(&bf->bf_lock);
11660 +}
11661 +
11662 +void au_fhsm_wrote_all(struct super_block *sb, int force)
11663 +{
11664 +       aufs_bindex_t bindex, bend;
11665 +       struct au_branch *br;
11666 +
11667 +       /* exclude the bottom */
11668 +       bend = au_fhsm_bottom(sb);
11669 +       for (bindex = 0; bindex < bend; bindex++) {
11670 +               br = au_sbr(sb, bindex);
11671 +               if (au_br_fhsm(br->br_perm))
11672 +                       au_fhsm_wrote(sb, bindex, force);
11673 +       }
11674 +}
11675 +
11676 +/* ---------------------------------------------------------------------- */
11677 +
11678 +static unsigned int au_fhsm_poll(struct file *file,
11679 +                                struct poll_table_struct *wait)
11680 +{
11681 +       unsigned int mask;
11682 +       struct au_sbinfo *sbinfo;
11683 +       struct au_fhsm *fhsm;
11684 +
11685 +       mask = 0;
11686 +       sbinfo = file->private_data;
11687 +       fhsm = &sbinfo->si_fhsm;
11688 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
11689 +       if (atomic_read(&fhsm->fhsm_readable))
11690 +               mask = POLLIN /* | POLLRDNORM */;
11691 +
11692 +       AuTraceErr((int)mask);
11693 +       return mask;
11694 +}
11695 +
11696 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
11697 +                             struct aufs_stfs *stfs, __s16 brid)
11698 +{
11699 +       int err;
11700 +
11701 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
11702 +       if (!err)
11703 +               err = __put_user(brid, &stbr->brid);
11704 +       if (unlikely(err))
11705 +               err = -EFAULT;
11706 +
11707 +       return err;
11708 +}
11709 +
11710 +static ssize_t au_fhsm_do_read(struct super_block *sb,
11711 +                              struct aufs_stbr __user *stbr, size_t count)
11712 +{
11713 +       ssize_t err;
11714 +       int nstbr;
11715 +       aufs_bindex_t bindex, bend;
11716 +       struct au_branch *br;
11717 +       struct au_br_fhsm *bf;
11718 +
11719 +       /* except the bottom branch */
11720 +       err = 0;
11721 +       nstbr = 0;
11722 +       bend = au_fhsm_bottom(sb);
11723 +       for (bindex = 0; !err && bindex < bend; bindex++) {
11724 +               br = au_sbr(sb, bindex);
11725 +               if (!au_br_fhsm(br->br_perm))
11726 +                       continue;
11727 +
11728 +               bf = br->br_fhsm;
11729 +               mutex_lock(&bf->bf_lock);
11730 +               if (bf->bf_readable) {
11731 +                       err = -EFAULT;
11732 +                       if (count >= sizeof(*stbr))
11733 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
11734 +                                                         br->br_id);
11735 +                       if (!err) {
11736 +                               bf->bf_readable = 0;
11737 +                               count -= sizeof(*stbr);
11738 +                               nstbr++;
11739 +                       }
11740 +               }
11741 +               mutex_unlock(&bf->bf_lock);
11742 +       }
11743 +       if (!err)
11744 +               err = sizeof(*stbr) * nstbr;
11745 +
11746 +       return err;
11747 +}
11748 +
11749 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
11750 +                          loff_t *pos)
11751 +{
11752 +       ssize_t err;
11753 +       int readable;
11754 +       aufs_bindex_t nfhsm, bindex, bend;
11755 +       struct au_sbinfo *sbinfo;
11756 +       struct au_fhsm *fhsm;
11757 +       struct au_branch *br;
11758 +       struct super_block *sb;
11759 +
11760 +       err = 0;
11761 +       sbinfo = file->private_data;
11762 +       fhsm = &sbinfo->si_fhsm;
11763 +need_data:
11764 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
11765 +       if (!atomic_read(&fhsm->fhsm_readable)) {
11766 +               if (vfsub_file_flags(file) & O_NONBLOCK)
11767 +                       err = -EAGAIN;
11768 +               else
11769 +                       err = wait_event_interruptible_locked_irq
11770 +                               (fhsm->fhsm_wqh,
11771 +                                atomic_read(&fhsm->fhsm_readable));
11772 +       }
11773 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
11774 +       if (unlikely(err))
11775 +               goto out;
11776 +
11777 +       /* sb may already be dead */
11778 +       au_rw_read_lock(&sbinfo->si_rwsem);
11779 +       readable = atomic_read(&fhsm->fhsm_readable);
11780 +       if (readable > 0) {
11781 +               sb = sbinfo->si_sb;
11782 +               AuDebugOn(!sb);
11783 +               /* exclude the bottom branch */
11784 +               nfhsm = 0;
11785 +               bend = au_fhsm_bottom(sb);
11786 +               for (bindex = 0; bindex < bend; bindex++) {
11787 +                       br = au_sbr(sb, bindex);
11788 +                       if (au_br_fhsm(br->br_perm))
11789 +                               nfhsm++;
11790 +               }
11791 +               err = -EMSGSIZE;
11792 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
11793 +                       atomic_set(&fhsm->fhsm_readable, 0);
11794 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
11795 +                                            count);
11796 +               }
11797 +       }
11798 +       au_rw_read_unlock(&sbinfo->si_rwsem);
11799 +       if (!readable)
11800 +               goto need_data;
11801 +
11802 +out:
11803 +       return err;
11804 +}
11805 +
11806 +static int au_fhsm_release(struct inode *inode, struct file *file)
11807 +{
11808 +       struct au_sbinfo *sbinfo;
11809 +       struct au_fhsm *fhsm;
11810 +
11811 +       /* sb may already be dead */
11812 +       sbinfo = file->private_data;
11813 +       fhsm = &sbinfo->si_fhsm;
11814 +       spin_lock(&fhsm->fhsm_spin);
11815 +       fhsm->fhsm_pid = 0;
11816 +       spin_unlock(&fhsm->fhsm_spin);
11817 +       kobject_put(&sbinfo->si_kobj);
11818 +
11819 +       return 0;
11820 +}
11821 +
11822 +static const struct file_operations au_fhsm_fops = {
11823 +       .owner          = THIS_MODULE,
11824 +       .llseek         = noop_llseek,
11825 +       .read           = au_fhsm_read,
11826 +       .poll           = au_fhsm_poll,
11827 +       .release        = au_fhsm_release
11828 +};
11829 +
11830 +int au_fhsm_fd(struct super_block *sb, int oflags)
11831 +{
11832 +       int err, fd;
11833 +       struct au_sbinfo *sbinfo;
11834 +       struct au_fhsm *fhsm;
11835 +
11836 +       err = -EPERM;
11837 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
11838 +               goto out;
11839 +
11840 +       err = -EINVAL;
11841 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
11842 +               goto out;
11843 +
11844 +       err = 0;
11845 +       sbinfo = au_sbi(sb);
11846 +       fhsm = &sbinfo->si_fhsm;
11847 +       spin_lock(&fhsm->fhsm_spin);
11848 +       if (!fhsm->fhsm_pid)
11849 +               fhsm->fhsm_pid = current->pid;
11850 +       else
11851 +               err = -EBUSY;
11852 +       spin_unlock(&fhsm->fhsm_spin);
11853 +       if (unlikely(err))
11854 +               goto out;
11855 +
11856 +       oflags |= O_RDONLY;
11857 +       /* oflags |= FMODE_NONOTIFY; */
11858 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
11859 +       err = fd;
11860 +       if (unlikely(fd < 0))
11861 +               goto out_pid;
11862 +
11863 +       /* succeed reglardless 'fhsm' status */
11864 +       kobject_get(&sbinfo->si_kobj);
11865 +       si_noflush_read_lock(sb);
11866 +       if (au_ftest_si(sbinfo, FHSM))
11867 +               au_fhsm_wrote_all(sb, /*force*/0);
11868 +       si_read_unlock(sb);
11869 +       goto out; /* success */
11870 +
11871 +out_pid:
11872 +       spin_lock(&fhsm->fhsm_spin);
11873 +       fhsm->fhsm_pid = 0;
11874 +       spin_unlock(&fhsm->fhsm_spin);
11875 +out:
11876 +       AuTraceErr(err);
11877 +       return err;
11878 +}
11879 +
11880 +/* ---------------------------------------------------------------------- */
11881 +
11882 +int au_fhsm_br_alloc(struct au_branch *br)
11883 +{
11884 +       int err;
11885 +
11886 +       err = 0;
11887 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
11888 +       if (br->br_fhsm)
11889 +               au_br_fhsm_init(br->br_fhsm);
11890 +       else
11891 +               err = -ENOMEM;
11892 +
11893 +       return err;
11894 +}
11895 +
11896 +/* ---------------------------------------------------------------------- */
11897 +
11898 +void au_fhsm_fin(struct super_block *sb)
11899 +{
11900 +       au_fhsm_notify(sb, /*val*/-1);
11901 +}
11902 +
11903 +void au_fhsm_init(struct au_sbinfo *sbinfo)
11904 +{
11905 +       struct au_fhsm *fhsm;
11906 +
11907 +       fhsm = &sbinfo->si_fhsm;
11908 +       spin_lock_init(&fhsm->fhsm_spin);
11909 +       init_waitqueue_head(&fhsm->fhsm_wqh);
11910 +       atomic_set(&fhsm->fhsm_readable, 0);
11911 +       fhsm->fhsm_expire
11912 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
11913 +       fhsm->fhsm_bottom = -1;
11914 +}
11915 +
11916 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
11917 +{
11918 +       sbinfo->si_fhsm.fhsm_expire
11919 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
11920 +}
11921 +
11922 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
11923 +{
11924 +       unsigned int u;
11925 +
11926 +       if (!au_ftest_si(sbinfo, FHSM))
11927 +               return;
11928 +
11929 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
11930 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
11931 +               seq_printf(seq, ",fhsm_sec=%u", u);
11932 +}
11933 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
11934 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
11935 +++ linux/fs/aufs/file.c        2015-06-22 08:29:23.706757681 +0200
11936 @@ -0,0 +1,844 @@
11937 +/*
11938 + * Copyright (C) 2005-2015 Junjiro R. Okajima
11939 + *
11940 + * This program, aufs is free software; you can redistribute it and/or modify
11941 + * it under the terms of the GNU General Public License as published by
11942 + * the Free Software Foundation; either version 2 of the License, or
11943 + * (at your option) any later version.
11944 + *
11945 + * This program is distributed in the hope that it will be useful,
11946 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11947 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11948 + * GNU General Public License for more details.
11949 + *
11950 + * You should have received a copy of the GNU General Public License
11951 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11952 + */
11953 +
11954 +/*
11955 + * handling file/dir, and address_space operation
11956 + */
11957 +
11958 +#ifdef CONFIG_AUFS_DEBUG
11959 +#include <linux/migrate.h>
11960 +#endif
11961 +#include <linux/pagemap.h>
11962 +#include "aufs.h"
11963 +
11964 +/* drop flags for writing */
11965 +unsigned int au_file_roflags(unsigned int flags)
11966 +{
11967 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
11968 +       flags |= O_RDONLY | O_NOATIME;
11969 +       return flags;
11970 +}
11971 +
11972 +/* common functions to regular file and dir */
11973 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
11974 +                      struct file *file, int force_wr)
11975 +{
11976 +       struct file *h_file;
11977 +       struct dentry *h_dentry;
11978 +       struct inode *h_inode;
11979 +       struct super_block *sb;
11980 +       struct au_branch *br;
11981 +       struct path h_path;
11982 +       int err;
11983 +
11984 +       /* a race condition can happen between open and unlink/rmdir */
11985 +       h_file = ERR_PTR(-ENOENT);
11986 +       h_dentry = au_h_dptr(dentry, bindex);
11987 +       if (au_test_nfsd() && !h_dentry)
11988 +               goto out;
11989 +       h_inode = h_dentry->d_inode;
11990 +       if (au_test_nfsd() && !h_inode)
11991 +               goto out;
11992 +       spin_lock(&h_dentry->d_lock);
11993 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
11994 +               || !h_inode
11995 +               /* || !dentry->d_inode->i_nlink */
11996 +               ;
11997 +       spin_unlock(&h_dentry->d_lock);
11998 +       if (unlikely(err))
11999 +               goto out;
12000 +
12001 +       sb = dentry->d_sb;
12002 +       br = au_sbr(sb, bindex);
12003 +       err = au_br_test_oflag(flags, br);
12004 +       h_file = ERR_PTR(err);
12005 +       if (unlikely(err))
12006 +               goto out;
12007 +
12008 +       /* drop flags for writing */
12009 +       if (au_test_ro(sb, bindex, dentry->d_inode)) {
12010 +               if (force_wr && !(flags & O_WRONLY))
12011 +                       force_wr = 0;
12012 +               flags = au_file_roflags(flags);
12013 +               if (force_wr) {
12014 +                       h_file = ERR_PTR(-EROFS);
12015 +                       flags = au_file_roflags(flags);
12016 +                       if (unlikely(vfsub_native_ro(h_inode)
12017 +                                    || IS_APPEND(h_inode)))
12018 +                               goto out;
12019 +                       flags &= ~O_ACCMODE;
12020 +                       flags |= O_WRONLY;
12021 +               }
12022 +       }
12023 +       flags &= ~O_CREAT;
12024 +       atomic_inc(&br->br_count);
12025 +       h_path.dentry = h_dentry;
12026 +       h_path.mnt = au_br_mnt(br);
12027 +       h_file = vfsub_dentry_open(&h_path, flags);
12028 +       if (IS_ERR(h_file))
12029 +               goto out_br;
12030 +
12031 +       if (flags & __FMODE_EXEC) {
12032 +               err = deny_write_access(h_file);
12033 +               if (unlikely(err)) {
12034 +                       fput(h_file);
12035 +                       h_file = ERR_PTR(err);
12036 +                       goto out_br;
12037 +               }
12038 +       }
12039 +       fsnotify_open(h_file);
12040 +       goto out; /* success */
12041 +
12042 +out_br:
12043 +       atomic_dec(&br->br_count);
12044 +out:
12045 +       return h_file;
12046 +}
12047 +
12048 +static int au_cmoo(struct dentry *dentry)
12049 +{
12050 +       int err, cmoo;
12051 +       unsigned int udba;
12052 +       struct path h_path;
12053 +       struct au_pin pin;
12054 +       struct au_cp_generic cpg = {
12055 +               .dentry = dentry,
12056 +               .bdst   = -1,
12057 +               .bsrc   = -1,
12058 +               .len    = -1,
12059 +               .pin    = &pin,
12060 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
12061 +       };
12062 +       struct inode *delegated;
12063 +       struct super_block *sb;
12064 +       struct au_sbinfo *sbinfo;
12065 +       struct au_fhsm *fhsm;
12066 +       pid_t pid;
12067 +       struct au_branch *br;
12068 +       struct dentry *parent;
12069 +       struct au_hinode *hdir;
12070 +
12071 +       DiMustWriteLock(dentry);
12072 +       IiMustWriteLock(dentry->d_inode);
12073 +
12074 +       err = 0;
12075 +       if (IS_ROOT(dentry))
12076 +               goto out;
12077 +       cpg.bsrc = au_dbstart(dentry);
12078 +       if (!cpg.bsrc)
12079 +               goto out;
12080 +
12081 +       sb = dentry->d_sb;
12082 +       sbinfo = au_sbi(sb);
12083 +       fhsm = &sbinfo->si_fhsm;
12084 +       pid = au_fhsm_pid(fhsm);
12085 +       if (pid
12086 +           && (current->pid == pid
12087 +               || current->real_parent->pid == pid))
12088 +               goto out;
12089 +
12090 +       br = au_sbr(sb, cpg.bsrc);
12091 +       cmoo = au_br_cmoo(br->br_perm);
12092 +       if (!cmoo)
12093 +               goto out;
12094 +       if (!d_is_reg(dentry))
12095 +               cmoo &= AuBrAttr_COO_ALL;
12096 +       if (!cmoo)
12097 +               goto out;
12098 +
12099 +       parent = dget_parent(dentry);
12100 +       di_write_lock_parent(parent);
12101 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
12102 +       cpg.bdst = err;
12103 +       if (unlikely(err < 0)) {
12104 +               err = 0;        /* there is no upper writable branch */
12105 +               goto out_dgrade;
12106 +       }
12107 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
12108 +
12109 +       /* do not respect the coo attrib for the target branch */
12110 +       err = au_cpup_dirs(dentry, cpg.bdst);
12111 +       if (unlikely(err))
12112 +               goto out_dgrade;
12113 +
12114 +       di_downgrade_lock(parent, AuLock_IR);
12115 +       udba = au_opt_udba(sb);
12116 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
12117 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12118 +       if (unlikely(err))
12119 +               goto out_parent;
12120 +
12121 +       err = au_sio_cpup_simple(&cpg);
12122 +       au_unpin(&pin);
12123 +       if (unlikely(err))
12124 +               goto out_parent;
12125 +       if (!(cmoo & AuBrWAttr_MOO))
12126 +               goto out_parent; /* success */
12127 +
12128 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
12129 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12130 +       if (unlikely(err))
12131 +               goto out_parent;
12132 +
12133 +       h_path.mnt = au_br_mnt(br);
12134 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
12135 +       hdir = au_hi(parent->d_inode, cpg.bsrc);
12136 +       delegated = NULL;
12137 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
12138 +       au_unpin(&pin);
12139 +       /* todo: keep h_dentry or not? */
12140 +       if (unlikely(err == -EWOULDBLOCK)) {
12141 +               pr_warn("cannot retry for NFSv4 delegation"
12142 +                       " for an internal unlink\n");
12143 +               iput(delegated);
12144 +       }
12145 +       if (unlikely(err)) {
12146 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
12147 +                      dentry, err);
12148 +               err = 0;
12149 +       }
12150 +       goto out_parent; /* success */
12151 +
12152 +out_dgrade:
12153 +       di_downgrade_lock(parent, AuLock_IR);
12154 +out_parent:
12155 +       di_read_unlock(parent, AuLock_IR);
12156 +       dput(parent);
12157 +out:
12158 +       AuTraceErr(err);
12159 +       return err;
12160 +}
12161 +
12162 +int au_do_open(struct file *file, struct au_do_open_args *args)
12163 +{
12164 +       int err, no_lock = args->no_lock;
12165 +       struct dentry *dentry;
12166 +       struct au_finfo *finfo;
12167 +
12168 +       if (!no_lock)
12169 +               err = au_finfo_init(file, args->fidir);
12170 +       else {
12171 +               lockdep_off();
12172 +               err = au_finfo_init(file, args->fidir);
12173 +               lockdep_on();
12174 +       }
12175 +       if (unlikely(err))
12176 +               goto out;
12177 +
12178 +       dentry = file->f_path.dentry;
12179 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
12180 +       if (!no_lock) {
12181 +               di_write_lock_child(dentry);
12182 +               err = au_cmoo(dentry);
12183 +               di_downgrade_lock(dentry, AuLock_IR);
12184 +               if (!err)
12185 +                       err = args->open(file, vfsub_file_flags(file), NULL);
12186 +               di_read_unlock(dentry, AuLock_IR);
12187 +       } else {
12188 +               err = au_cmoo(dentry);
12189 +               if (!err)
12190 +                       err = args->open(file, vfsub_file_flags(file),
12191 +                                        args->h_file);
12192 +               if (!err && au_fbstart(file) != au_dbstart(dentry))
12193 +                       /*
12194 +                        * cmoo happens after h_file was opened.
12195 +                        * need to refresh file later.
12196 +                        */
12197 +                       atomic_dec(&au_fi(file)->fi_generation);
12198 +       }
12199 +
12200 +       finfo = au_fi(file);
12201 +       if (!err) {
12202 +               finfo->fi_file = file;
12203 +               au_sphl_add(&finfo->fi_hlist,
12204 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
12205 +       }
12206 +       if (!no_lock)
12207 +               fi_write_unlock(file);
12208 +       else {
12209 +               lockdep_off();
12210 +               fi_write_unlock(file);
12211 +               lockdep_on();
12212 +       }
12213 +       if (unlikely(err)) {
12214 +               finfo->fi_hdir = NULL;
12215 +               au_finfo_fin(file);
12216 +       }
12217 +
12218 +out:
12219 +       return err;
12220 +}
12221 +
12222 +int au_reopen_nondir(struct file *file)
12223 +{
12224 +       int err;
12225 +       aufs_bindex_t bstart;
12226 +       struct dentry *dentry;
12227 +       struct file *h_file, *h_file_tmp;
12228 +
12229 +       dentry = file->f_path.dentry;
12230 +       bstart = au_dbstart(dentry);
12231 +       h_file_tmp = NULL;
12232 +       if (au_fbstart(file) == bstart) {
12233 +               h_file = au_hf_top(file);
12234 +               if (file->f_mode == h_file->f_mode)
12235 +                       return 0; /* success */
12236 +               h_file_tmp = h_file;
12237 +               get_file(h_file_tmp);
12238 +               au_set_h_fptr(file, bstart, NULL);
12239 +       }
12240 +       AuDebugOn(au_fi(file)->fi_hdir);
12241 +       /*
12242 +        * it can happen
12243 +        * file exists on both of rw and ro
12244 +        * open --> dbstart and fbstart are both 0
12245 +        * prepend a branch as rw, "rw" become ro
12246 +        * remove rw/file
12247 +        * delete the top branch, "rw" becomes rw again
12248 +        *      --> dbstart is 1, fbstart is still 0
12249 +        * write --> fbstart is 0 but dbstart is 1
12250 +        */
12251 +       /* AuDebugOn(au_fbstart(file) < bstart); */
12252 +
12253 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
12254 +                          file, /*force_wr*/0);
12255 +       err = PTR_ERR(h_file);
12256 +       if (IS_ERR(h_file)) {
12257 +               if (h_file_tmp) {
12258 +                       atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
12259 +                       au_set_h_fptr(file, bstart, h_file_tmp);
12260 +                       h_file_tmp = NULL;
12261 +               }
12262 +               goto out; /* todo: close all? */
12263 +       }
12264 +
12265 +       err = 0;
12266 +       au_set_fbstart(file, bstart);
12267 +       au_set_h_fptr(file, bstart, h_file);
12268 +       au_update_figen(file);
12269 +       /* todo: necessary? */
12270 +       /* file->f_ra = h_file->f_ra; */
12271 +
12272 +out:
12273 +       if (h_file_tmp)
12274 +               fput(h_file_tmp);
12275 +       return err;
12276 +}
12277 +
12278 +/* ---------------------------------------------------------------------- */
12279 +
12280 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
12281 +                       struct dentry *hi_wh)
12282 +{
12283 +       int err;
12284 +       aufs_bindex_t bstart;
12285 +       struct au_dinfo *dinfo;
12286 +       struct dentry *h_dentry;
12287 +       struct au_hdentry *hdp;
12288 +
12289 +       dinfo = au_di(file->f_path.dentry);
12290 +       AuRwMustWriteLock(&dinfo->di_rwsem);
12291 +
12292 +       bstart = dinfo->di_bstart;
12293 +       dinfo->di_bstart = btgt;
12294 +       hdp = dinfo->di_hdentry;
12295 +       h_dentry = hdp[0 + btgt].hd_dentry;
12296 +       hdp[0 + btgt].hd_dentry = hi_wh;
12297 +       err = au_reopen_nondir(file);
12298 +       hdp[0 + btgt].hd_dentry = h_dentry;
12299 +       dinfo->di_bstart = bstart;
12300 +
12301 +       return err;
12302 +}
12303 +
12304 +static int au_ready_to_write_wh(struct file *file, loff_t len,
12305 +                               aufs_bindex_t bcpup, struct au_pin *pin)
12306 +{
12307 +       int err;
12308 +       struct inode *inode, *h_inode;
12309 +       struct dentry *h_dentry, *hi_wh;
12310 +       struct au_cp_generic cpg = {
12311 +               .dentry = file->f_path.dentry,
12312 +               .bdst   = bcpup,
12313 +               .bsrc   = -1,
12314 +               .len    = len,
12315 +               .pin    = pin
12316 +       };
12317 +
12318 +       au_update_dbstart(cpg.dentry);
12319 +       inode = cpg.dentry->d_inode;
12320 +       h_inode = NULL;
12321 +       if (au_dbstart(cpg.dentry) <= bcpup
12322 +           && au_dbend(cpg.dentry) >= bcpup) {
12323 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
12324 +               if (h_dentry)
12325 +                       h_inode = h_dentry->d_inode;
12326 +       }
12327 +       hi_wh = au_hi_wh(inode, bcpup);
12328 +       if (!hi_wh && !h_inode)
12329 +               err = au_sio_cpup_wh(&cpg, file);
12330 +       else
12331 +               /* already copied-up after unlink */
12332 +               err = au_reopen_wh(file, bcpup, hi_wh);
12333 +
12334 +       if (!err
12335 +           && (inode->i_nlink > 1
12336 +               || (inode->i_state & I_LINKABLE))
12337 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
12338 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
12339 +
12340 +       return err;
12341 +}
12342 +
12343 +/*
12344 + * prepare the @file for writing.
12345 + */
12346 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
12347 +{
12348 +       int err;
12349 +       aufs_bindex_t dbstart;
12350 +       struct dentry *parent;
12351 +       struct inode *inode;
12352 +       struct super_block *sb;
12353 +       struct file *h_file;
12354 +       struct au_cp_generic cpg = {
12355 +               .dentry = file->f_path.dentry,
12356 +               .bdst   = -1,
12357 +               .bsrc   = -1,
12358 +               .len    = len,
12359 +               .pin    = pin,
12360 +               .flags  = AuCpup_DTIME
12361 +       };
12362 +
12363 +       sb = cpg.dentry->d_sb;
12364 +       inode = cpg.dentry->d_inode;
12365 +       cpg.bsrc = au_fbstart(file);
12366 +       err = au_test_ro(sb, cpg.bsrc, inode);
12367 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
12368 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
12369 +                            /*flags*/0);
12370 +               goto out;
12371 +       }
12372 +
12373 +       /* need to cpup or reopen */
12374 +       parent = dget_parent(cpg.dentry);
12375 +       di_write_lock_parent(parent);
12376 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12377 +       cpg.bdst = err;
12378 +       if (unlikely(err < 0))
12379 +               goto out_dgrade;
12380 +       err = 0;
12381 +
12382 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
12383 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
12384 +               if (unlikely(err))
12385 +                       goto out_dgrade;
12386 +       }
12387 +
12388 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12389 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12390 +       if (unlikely(err))
12391 +               goto out_dgrade;
12392 +
12393 +       dbstart = au_dbstart(cpg.dentry);
12394 +       if (dbstart <= cpg.bdst)
12395 +               cpg.bsrc = cpg.bdst;
12396 +
12397 +       if (dbstart <= cpg.bdst         /* just reopen */
12398 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
12399 +               ) {
12400 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
12401 +               if (IS_ERR(h_file))
12402 +                       err = PTR_ERR(h_file);
12403 +               else {
12404 +                       di_downgrade_lock(parent, AuLock_IR);
12405 +                       if (dbstart > cpg.bdst)
12406 +                               err = au_sio_cpup_simple(&cpg);
12407 +                       if (!err)
12408 +                               err = au_reopen_nondir(file);
12409 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
12410 +               }
12411 +       } else {                        /* copyup as wh and reopen */
12412 +               /*
12413 +                * since writable hfsplus branch is not supported,
12414 +                * h_open_pre/post() are unnecessary.
12415 +                */
12416 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
12417 +               di_downgrade_lock(parent, AuLock_IR);
12418 +       }
12419 +
12420 +       if (!err) {
12421 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
12422 +               goto out_dput; /* success */
12423 +       }
12424 +       au_unpin(pin);
12425 +       goto out_unlock;
12426 +
12427 +out_dgrade:
12428 +       di_downgrade_lock(parent, AuLock_IR);
12429 +out_unlock:
12430 +       di_read_unlock(parent, AuLock_IR);
12431 +out_dput:
12432 +       dput(parent);
12433 +out:
12434 +       return err;
12435 +}
12436 +
12437 +/* ---------------------------------------------------------------------- */
12438 +
12439 +int au_do_flush(struct file *file, fl_owner_t id,
12440 +               int (*flush)(struct file *file, fl_owner_t id))
12441 +{
12442 +       int err;
12443 +       struct super_block *sb;
12444 +       struct inode *inode;
12445 +
12446 +       inode = file_inode(file);
12447 +       sb = inode->i_sb;
12448 +       si_noflush_read_lock(sb);
12449 +       fi_read_lock(file);
12450 +       ii_read_lock_child(inode);
12451 +
12452 +       err = flush(file, id);
12453 +       au_cpup_attr_timesizes(inode);
12454 +
12455 +       ii_read_unlock(inode);
12456 +       fi_read_unlock(file);
12457 +       si_read_unlock(sb);
12458 +       return err;
12459 +}
12460 +
12461 +/* ---------------------------------------------------------------------- */
12462 +
12463 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
12464 +{
12465 +       int err;
12466 +       struct au_pin pin;
12467 +       struct au_finfo *finfo;
12468 +       struct dentry *parent, *hi_wh;
12469 +       struct inode *inode;
12470 +       struct super_block *sb;
12471 +       struct au_cp_generic cpg = {
12472 +               .dentry = file->f_path.dentry,
12473 +               .bdst   = -1,
12474 +               .bsrc   = -1,
12475 +               .len    = -1,
12476 +               .pin    = &pin,
12477 +               .flags  = AuCpup_DTIME
12478 +       };
12479 +
12480 +       FiMustWriteLock(file);
12481 +
12482 +       err = 0;
12483 +       finfo = au_fi(file);
12484 +       sb = cpg.dentry->d_sb;
12485 +       inode = cpg.dentry->d_inode;
12486 +       cpg.bdst = au_ibstart(inode);
12487 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
12488 +               goto out;
12489 +
12490 +       parent = dget_parent(cpg.dentry);
12491 +       if (au_test_ro(sb, cpg.bdst, inode)) {
12492 +               di_read_lock_parent(parent, !AuLock_IR);
12493 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12494 +               cpg.bdst = err;
12495 +               di_read_unlock(parent, !AuLock_IR);
12496 +               if (unlikely(err < 0))
12497 +                       goto out_parent;
12498 +               err = 0;
12499 +       }
12500 +
12501 +       di_read_lock_parent(parent, AuLock_IR);
12502 +       hi_wh = au_hi_wh(inode, cpg.bdst);
12503 +       if (!S_ISDIR(inode->i_mode)
12504 +           && au_opt_test(au_mntflags(sb), PLINK)
12505 +           && au_plink_test(inode)
12506 +           && !d_unhashed(cpg.dentry)
12507 +           && cpg.bdst < au_dbstart(cpg.dentry)) {
12508 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
12509 +               if (unlikely(err))
12510 +                       goto out_unlock;
12511 +
12512 +               /* always superio. */
12513 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12514 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12515 +               if (!err) {
12516 +                       err = au_sio_cpup_simple(&cpg);
12517 +                       au_unpin(&pin);
12518 +               }
12519 +       } else if (hi_wh) {
12520 +               /* already copied-up after unlink */
12521 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
12522 +               *need_reopen = 0;
12523 +       }
12524 +
12525 +out_unlock:
12526 +       di_read_unlock(parent, AuLock_IR);
12527 +out_parent:
12528 +       dput(parent);
12529 +out:
12530 +       return err;
12531 +}
12532 +
12533 +static void au_do_refresh_dir(struct file *file)
12534 +{
12535 +       aufs_bindex_t bindex, bend, new_bindex, brid;
12536 +       struct au_hfile *p, tmp, *q;
12537 +       struct au_finfo *finfo;
12538 +       struct super_block *sb;
12539 +       struct au_fidir *fidir;
12540 +
12541 +       FiMustWriteLock(file);
12542 +
12543 +       sb = file->f_path.dentry->d_sb;
12544 +       finfo = au_fi(file);
12545 +       fidir = finfo->fi_hdir;
12546 +       AuDebugOn(!fidir);
12547 +       p = fidir->fd_hfile + finfo->fi_btop;
12548 +       brid = p->hf_br->br_id;
12549 +       bend = fidir->fd_bbot;
12550 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
12551 +               if (!p->hf_file)
12552 +                       continue;
12553 +
12554 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
12555 +               if (new_bindex == bindex)
12556 +                       continue;
12557 +               if (new_bindex < 0) {
12558 +                       au_set_h_fptr(file, bindex, NULL);
12559 +                       continue;
12560 +               }
12561 +
12562 +               /* swap two lower inode, and loop again */
12563 +               q = fidir->fd_hfile + new_bindex;
12564 +               tmp = *q;
12565 +               *q = *p;
12566 +               *p = tmp;
12567 +               if (tmp.hf_file) {
12568 +                       bindex--;
12569 +                       p--;
12570 +               }
12571 +       }
12572 +
12573 +       p = fidir->fd_hfile;
12574 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
12575 +               bend = au_sbend(sb);
12576 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
12577 +                    finfo->fi_btop++, p++)
12578 +                       if (p->hf_file) {
12579 +                               if (file_inode(p->hf_file))
12580 +                                       break;
12581 +                               au_hfput(p, file);
12582 +                       }
12583 +       } else {
12584 +               bend = au_br_index(sb, brid);
12585 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
12586 +                    finfo->fi_btop++, p++)
12587 +                       if (p->hf_file)
12588 +                               au_hfput(p, file);
12589 +               bend = au_sbend(sb);
12590 +       }
12591 +
12592 +       p = fidir->fd_hfile + bend;
12593 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
12594 +            fidir->fd_bbot--, p--)
12595 +               if (p->hf_file) {
12596 +                       if (file_inode(p->hf_file))
12597 +                               break;
12598 +                       au_hfput(p, file);
12599 +               }
12600 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
12601 +}
12602 +
12603 +/*
12604 + * after branch manipulating, refresh the file.
12605 + */
12606 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
12607 +{
12608 +       int err, need_reopen;
12609 +       aufs_bindex_t bend, bindex;
12610 +       struct dentry *dentry;
12611 +       struct au_finfo *finfo;
12612 +       struct au_hfile *hfile;
12613 +
12614 +       dentry = file->f_path.dentry;
12615 +       finfo = au_fi(file);
12616 +       if (!finfo->fi_hdir) {
12617 +               hfile = &finfo->fi_htop;
12618 +               AuDebugOn(!hfile->hf_file);
12619 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
12620 +               AuDebugOn(bindex < 0);
12621 +               if (bindex != finfo->fi_btop)
12622 +                       au_set_fbstart(file, bindex);
12623 +       } else {
12624 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
12625 +               if (unlikely(err))
12626 +                       goto out;
12627 +               au_do_refresh_dir(file);
12628 +       }
12629 +
12630 +       err = 0;
12631 +       need_reopen = 1;
12632 +       if (!au_test_mmapped(file))
12633 +               err = au_file_refresh_by_inode(file, &need_reopen);
12634 +       if (!err && need_reopen && !d_unlinked(dentry))
12635 +               err = reopen(file);
12636 +       if (!err) {
12637 +               au_update_figen(file);
12638 +               goto out; /* success */
12639 +       }
12640 +
12641 +       /* error, close all lower files */
12642 +       if (finfo->fi_hdir) {
12643 +               bend = au_fbend_dir(file);
12644 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
12645 +                       au_set_h_fptr(file, bindex, NULL);
12646 +       }
12647 +
12648 +out:
12649 +       return err;
12650 +}
12651 +
12652 +/* common function to regular file and dir */
12653 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12654 +                         int wlock)
12655 +{
12656 +       int err;
12657 +       unsigned int sigen, figen;
12658 +       aufs_bindex_t bstart;
12659 +       unsigned char pseudo_link;
12660 +       struct dentry *dentry;
12661 +       struct inode *inode;
12662 +
12663 +       err = 0;
12664 +       dentry = file->f_path.dentry;
12665 +       inode = dentry->d_inode;
12666 +       sigen = au_sigen(dentry->d_sb);
12667 +       fi_write_lock(file);
12668 +       figen = au_figen(file);
12669 +       di_write_lock_child(dentry);
12670 +       bstart = au_dbstart(dentry);
12671 +       pseudo_link = (bstart != au_ibstart(inode));
12672 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
12673 +               if (!wlock) {
12674 +                       di_downgrade_lock(dentry, AuLock_IR);
12675 +                       fi_downgrade_lock(file);
12676 +               }
12677 +               goto out; /* success */
12678 +       }
12679 +
12680 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
12681 +       if (au_digen_test(dentry, sigen)) {
12682 +               err = au_reval_dpath(dentry, sigen);
12683 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
12684 +       }
12685 +
12686 +       if (!err)
12687 +               err = refresh_file(file, reopen);
12688 +       if (!err) {
12689 +               if (!wlock) {
12690 +                       di_downgrade_lock(dentry, AuLock_IR);
12691 +                       fi_downgrade_lock(file);
12692 +               }
12693 +       } else {
12694 +               di_write_unlock(dentry);
12695 +               fi_write_unlock(file);
12696 +       }
12697 +
12698 +out:
12699 +       return err;
12700 +}
12701 +
12702 +/* ---------------------------------------------------------------------- */
12703 +
12704 +/* cf. aufs_nopage() */
12705 +/* for madvise(2) */
12706 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
12707 +{
12708 +       unlock_page(page);
12709 +       return 0;
12710 +}
12711 +
12712 +/* it will never be called, but necessary to support O_DIRECT */
12713 +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
12714 +                             struct iov_iter *iter, loff_t offset)
12715 +{ BUG(); return 0; }
12716 +
12717 +/* they will never be called. */
12718 +#ifdef CONFIG_AUFS_DEBUG
12719 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
12720 +                           loff_t pos, unsigned len, unsigned flags,
12721 +                           struct page **pagep, void **fsdata)
12722 +{ AuUnsupport(); return 0; }
12723 +static int aufs_write_end(struct file *file, struct address_space *mapping,
12724 +                         loff_t pos, unsigned len, unsigned copied,
12725 +                         struct page *page, void *fsdata)
12726 +{ AuUnsupport(); return 0; }
12727 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
12728 +{ AuUnsupport(); return 0; }
12729 +
12730 +static int aufs_set_page_dirty(struct page *page)
12731 +{ AuUnsupport(); return 0; }
12732 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
12733 +                               unsigned int length)
12734 +{ AuUnsupport(); }
12735 +static int aufs_releasepage(struct page *page, gfp_t gfp)
12736 +{ AuUnsupport(); return 0; }
12737 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
12738 +                           struct page *page, enum migrate_mode mode)
12739 +{ AuUnsupport(); return 0; }
12740 +static int aufs_launder_page(struct page *page)
12741 +{ AuUnsupport(); return 0; }
12742 +static int aufs_is_partially_uptodate(struct page *page,
12743 +                                     unsigned long from,
12744 +                                     unsigned long count)
12745 +{ AuUnsupport(); return 0; }
12746 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
12747 +                                   bool *writeback)
12748 +{ AuUnsupport(); }
12749 +static int aufs_error_remove_page(struct address_space *mapping,
12750 +                                 struct page *page)
12751 +{ AuUnsupport(); return 0; }
12752 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
12753 +                             sector_t *span)
12754 +{ AuUnsupport(); return 0; }
12755 +static void aufs_swap_deactivate(struct file *file)
12756 +{ AuUnsupport(); }
12757 +#endif /* CONFIG_AUFS_DEBUG */
12758 +
12759 +const struct address_space_operations aufs_aop = {
12760 +       .readpage               = aufs_readpage,
12761 +       .direct_IO              = aufs_direct_IO,
12762 +#ifdef CONFIG_AUFS_DEBUG
12763 +       .writepage              = aufs_writepage,
12764 +       /* no writepages, because of writepage */
12765 +       .set_page_dirty         = aufs_set_page_dirty,
12766 +       /* no readpages, because of readpage */
12767 +       .write_begin            = aufs_write_begin,
12768 +       .write_end              = aufs_write_end,
12769 +       /* no bmap, no block device */
12770 +       .invalidatepage         = aufs_invalidatepage,
12771 +       .releasepage            = aufs_releasepage,
12772 +       .migratepage            = aufs_migratepage,
12773 +       .launder_page           = aufs_launder_page,
12774 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
12775 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
12776 +       .error_remove_page      = aufs_error_remove_page,
12777 +       .swap_activate          = aufs_swap_activate,
12778 +       .swap_deactivate        = aufs_swap_deactivate
12779 +#endif /* CONFIG_AUFS_DEBUG */
12780 +};
12781 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
12782 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
12783 +++ linux/fs/aufs/file.h        2015-06-22 08:27:37.880835119 +0200
12784 @@ -0,0 +1,291 @@
12785 +/*
12786 + * Copyright (C) 2005-2015 Junjiro R. Okajima
12787 + *
12788 + * This program, aufs is free software; you can redistribute it and/or modify
12789 + * it under the terms of the GNU General Public License as published by
12790 + * the Free Software Foundation; either version 2 of the License, or
12791 + * (at your option) any later version.
12792 + *
12793 + * This program is distributed in the hope that it will be useful,
12794 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12795 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12796 + * GNU General Public License for more details.
12797 + *
12798 + * You should have received a copy of the GNU General Public License
12799 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12800 + */
12801 +
12802 +/*
12803 + * file operations
12804 + */
12805 +
12806 +#ifndef __AUFS_FILE_H__
12807 +#define __AUFS_FILE_H__
12808 +
12809 +#ifdef __KERNEL__
12810 +
12811 +#include <linux/file.h>
12812 +#include <linux/fs.h>
12813 +#include <linux/poll.h>
12814 +#include "rwsem.h"
12815 +
12816 +struct au_branch;
12817 +struct au_hfile {
12818 +       struct file             *hf_file;
12819 +       struct au_branch        *hf_br;
12820 +};
12821 +
12822 +struct au_vdir;
12823 +struct au_fidir {
12824 +       aufs_bindex_t           fd_bbot;
12825 +       aufs_bindex_t           fd_nent;
12826 +       struct au_vdir          *fd_vdir_cache;
12827 +       struct au_hfile         fd_hfile[];
12828 +};
12829 +
12830 +static inline int au_fidir_sz(int nent)
12831 +{
12832 +       AuDebugOn(nent < 0);
12833 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
12834 +}
12835 +
12836 +struct au_finfo {
12837 +       atomic_t                fi_generation;
12838 +
12839 +       struct au_rwsem         fi_rwsem;
12840 +       aufs_bindex_t           fi_btop;
12841 +
12842 +       /* do not union them */
12843 +       struct {                                /* for non-dir */
12844 +               struct au_hfile                 fi_htop;
12845 +               atomic_t                        fi_mmapped;
12846 +       };
12847 +       struct au_fidir         *fi_hdir;       /* for dir only */
12848 +
12849 +       struct hlist_node       fi_hlist;
12850 +       struct file             *fi_file;       /* very ugly */
12851 +} ____cacheline_aligned_in_smp;
12852 +
12853 +/* ---------------------------------------------------------------------- */
12854 +
12855 +/* file.c */
12856 +extern const struct address_space_operations aufs_aop;
12857 +unsigned int au_file_roflags(unsigned int flags);
12858 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
12859 +                      struct file *file, int force_wr);
12860 +struct au_do_open_args {
12861 +       int             no_lock;
12862 +       int             (*open)(struct file *file, int flags,
12863 +                               struct file *h_file);
12864 +       struct au_fidir *fidir;
12865 +       struct file     *h_file;
12866 +};
12867 +int au_do_open(struct file *file, struct au_do_open_args *args);
12868 +int au_reopen_nondir(struct file *file);
12869 +struct au_pin;
12870 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
12871 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12872 +                         int wlock);
12873 +int au_do_flush(struct file *file, fl_owner_t id,
12874 +               int (*flush)(struct file *file, fl_owner_t id));
12875 +
12876 +/* poll.c */
12877 +#ifdef CONFIG_AUFS_POLL
12878 +unsigned int aufs_poll(struct file *file, poll_table *wait);
12879 +#endif
12880 +
12881 +#ifdef CONFIG_AUFS_BR_HFSPLUS
12882 +/* hfsplus.c */
12883 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
12884 +                          int force_wr);
12885 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
12886 +                   struct file *h_file);
12887 +#else
12888 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
12889 +       aufs_bindex_t bindex, int force_wr)
12890 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
12891 +          struct file *h_file);
12892 +#endif
12893 +
12894 +/* f_op.c */
12895 +extern const struct file_operations aufs_file_fop;
12896 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
12897 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
12898 +struct file *au_read_pre(struct file *file, int keep_fi);
12899 +
12900 +/* finfo.c */
12901 +void au_hfput(struct au_hfile *hf, struct file *file);
12902 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
12903 +                  struct file *h_file);
12904 +
12905 +void au_update_figen(struct file *file);
12906 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
12907 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
12908 +
12909 +void au_fi_init_once(void *_fi);
12910 +void au_finfo_fin(struct file *file);
12911 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
12912 +
12913 +/* ioctl.c */
12914 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
12915 +#ifdef CONFIG_COMPAT
12916 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
12917 +                          unsigned long arg);
12918 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
12919 +                             unsigned long arg);
12920 +#endif
12921 +
12922 +/* ---------------------------------------------------------------------- */
12923 +
12924 +static inline struct au_finfo *au_fi(struct file *file)
12925 +{
12926 +       return file->private_data;
12927 +}
12928 +
12929 +/* ---------------------------------------------------------------------- */
12930 +
12931 +/*
12932 + * fi_read_lock, fi_write_lock,
12933 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
12934 + */
12935 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
12936 +
12937 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
12938 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
12939 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
12940 +
12941 +/* ---------------------------------------------------------------------- */
12942 +
12943 +/* todo: hard/soft set? */
12944 +static inline aufs_bindex_t au_fbstart(struct file *file)
12945 +{
12946 +       FiMustAnyLock(file);
12947 +       return au_fi(file)->fi_btop;
12948 +}
12949 +
12950 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
12951 +{
12952 +       FiMustAnyLock(file);
12953 +       AuDebugOn(!au_fi(file)->fi_hdir);
12954 +       return au_fi(file)->fi_hdir->fd_bbot;
12955 +}
12956 +
12957 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
12958 +{
12959 +       FiMustAnyLock(file);
12960 +       AuDebugOn(!au_fi(file)->fi_hdir);
12961 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
12962 +}
12963 +
12964 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
12965 +{
12966 +       FiMustWriteLock(file);
12967 +       au_fi(file)->fi_btop = bindex;
12968 +}
12969 +
12970 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
12971 +{
12972 +       FiMustWriteLock(file);
12973 +       AuDebugOn(!au_fi(file)->fi_hdir);
12974 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
12975 +}
12976 +
12977 +static inline void au_set_fvdir_cache(struct file *file,
12978 +                                     struct au_vdir *vdir_cache)
12979 +{
12980 +       FiMustWriteLock(file);
12981 +       AuDebugOn(!au_fi(file)->fi_hdir);
12982 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
12983 +}
12984 +
12985 +static inline struct file *au_hf_top(struct file *file)
12986 +{
12987 +       FiMustAnyLock(file);
12988 +       AuDebugOn(au_fi(file)->fi_hdir);
12989 +       return au_fi(file)->fi_htop.hf_file;
12990 +}
12991 +
12992 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
12993 +{
12994 +       FiMustAnyLock(file);
12995 +       AuDebugOn(!au_fi(file)->fi_hdir);
12996 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
12997 +}
12998 +
12999 +/* todo: memory barrier? */
13000 +static inline unsigned int au_figen(struct file *f)
13001 +{
13002 +       return atomic_read(&au_fi(f)->fi_generation);
13003 +}
13004 +
13005 +static inline void au_set_mmapped(struct file *f)
13006 +{
13007 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
13008 +               return;
13009 +       pr_warn("fi_mmapped wrapped around\n");
13010 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
13011 +               ;
13012 +}
13013 +
13014 +static inline void au_unset_mmapped(struct file *f)
13015 +{
13016 +       atomic_dec(&au_fi(f)->fi_mmapped);
13017 +}
13018 +
13019 +static inline int au_test_mmapped(struct file *f)
13020 +{
13021 +       return atomic_read(&au_fi(f)->fi_mmapped);
13022 +}
13023 +
13024 +/* customize vma->vm_file */
13025 +
13026 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
13027 +                                      struct file *file)
13028 +{
13029 +       struct file *f;
13030 +
13031 +       f = vma->vm_file;
13032 +       get_file(file);
13033 +       vma->vm_file = file;
13034 +       fput(f);
13035 +}
13036 +
13037 +#ifdef CONFIG_MMU
13038 +#define AuDbgVmRegion(file, vma) do {} while (0)
13039 +
13040 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13041 +                                   struct file *file)
13042 +{
13043 +       au_do_vm_file_reset(vma, file);
13044 +}
13045 +#else
13046 +#define AuDbgVmRegion(file, vma) \
13047 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
13048 +
13049 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13050 +                                   struct file *file)
13051 +{
13052 +       struct file *f;
13053 +
13054 +       au_do_vm_file_reset(vma, file);
13055 +       f = vma->vm_region->vm_file;
13056 +       get_file(file);
13057 +       vma->vm_region->vm_file = file;
13058 +       fput(f);
13059 +}
13060 +#endif /* CONFIG_MMU */
13061 +
13062 +/* handle vma->vm_prfile */
13063 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
13064 +                                   struct file *file)
13065 +{
13066 +       get_file(file);
13067 +       vma->vm_prfile = file;
13068 +#ifndef CONFIG_MMU
13069 +       get_file(file);
13070 +       vma->vm_region->vm_prfile = file;
13071 +#endif
13072 +}
13073 +
13074 +#endif /* __KERNEL__ */
13075 +#endif /* __AUFS_FILE_H__ */
13076 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
13077 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
13078 +++ linux/fs/aufs/finfo.c       2015-06-22 08:29:23.706757681 +0200
13079 @@ -0,0 +1,157 @@
13080 +/*
13081 + * Copyright (C) 2005-2015 Junjiro R. Okajima
13082 + *
13083 + * This program, aufs is free software; you can redistribute it and/or modify
13084 + * it under the terms of the GNU General Public License as published by
13085 + * the Free Software Foundation; either version 2 of the License, or
13086 + * (at your option) any later version.
13087 + *
13088 + * This program is distributed in the hope that it will be useful,
13089 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13090 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13091 + * GNU General Public License for more details.
13092 + *
13093 + * You should have received a copy of the GNU General Public License
13094 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13095 + */
13096 +
13097 +/*
13098 + * file private data
13099 + */
13100 +
13101 +#include "aufs.h"
13102 +
13103 +void au_hfput(struct au_hfile *hf, struct file *file)
13104 +{
13105 +       /* todo: direct access f_flags */
13106 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
13107 +               allow_write_access(hf->hf_file);
13108 +       fput(hf->hf_file);
13109 +       hf->hf_file = NULL;
13110 +       atomic_dec(&hf->hf_br->br_count);
13111 +       hf->hf_br = NULL;
13112 +}
13113 +
13114 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
13115 +{
13116 +       struct au_finfo *finfo = au_fi(file);
13117 +       struct au_hfile *hf;
13118 +       struct au_fidir *fidir;
13119 +
13120 +       fidir = finfo->fi_hdir;
13121 +       if (!fidir) {
13122 +               AuDebugOn(finfo->fi_btop != bindex);
13123 +               hf = &finfo->fi_htop;
13124 +       } else
13125 +               hf = fidir->fd_hfile + bindex;
13126 +
13127 +       if (hf && hf->hf_file)
13128 +               au_hfput(hf, file);
13129 +       if (val) {
13130 +               FiMustWriteLock(file);
13131 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
13132 +               hf->hf_file = val;
13133 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
13134 +       }
13135 +}
13136 +
13137 +void au_update_figen(struct file *file)
13138 +{
13139 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
13140 +       /* smp_mb(); */ /* atomic_set */
13141 +}
13142 +
13143 +/* ---------------------------------------------------------------------- */
13144 +
13145 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
13146 +{
13147 +       struct au_fidir *fidir;
13148 +       int nbr;
13149 +
13150 +       nbr = au_sbend(sb) + 1;
13151 +       if (nbr < 2)
13152 +               nbr = 2; /* initial allocate for 2 branches */
13153 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
13154 +       if (fidir) {
13155 +               fidir->fd_bbot = -1;
13156 +               fidir->fd_nent = nbr;
13157 +               fidir->fd_vdir_cache = NULL;
13158 +       }
13159 +
13160 +       return fidir;
13161 +}
13162 +
13163 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
13164 +{
13165 +       int err;
13166 +       struct au_fidir *fidir, *p;
13167 +
13168 +       AuRwMustWriteLock(&finfo->fi_rwsem);
13169 +       fidir = finfo->fi_hdir;
13170 +       AuDebugOn(!fidir);
13171 +
13172 +       err = -ENOMEM;
13173 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
13174 +                        GFP_NOFS);
13175 +       if (p) {
13176 +               p->fd_nent = nbr;
13177 +               finfo->fi_hdir = p;
13178 +               err = 0;
13179 +       }
13180 +
13181 +       return err;
13182 +}
13183 +
13184 +/* ---------------------------------------------------------------------- */
13185 +
13186 +void au_finfo_fin(struct file *file)
13187 +{
13188 +       struct au_finfo *finfo;
13189 +
13190 +       au_nfiles_dec(file->f_path.dentry->d_sb);
13191 +
13192 +       finfo = au_fi(file);
13193 +       AuDebugOn(finfo->fi_hdir);
13194 +       AuRwDestroy(&finfo->fi_rwsem);
13195 +       au_cache_free_finfo(finfo);
13196 +}
13197 +
13198 +void au_fi_init_once(void *_finfo)
13199 +{
13200 +       struct au_finfo *finfo = _finfo;
13201 +       static struct lock_class_key aufs_fi;
13202 +
13203 +       au_rw_init(&finfo->fi_rwsem);
13204 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
13205 +}
13206 +
13207 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
13208 +{
13209 +       int err;
13210 +       struct au_finfo *finfo;
13211 +       struct dentry *dentry;
13212 +
13213 +       err = -ENOMEM;
13214 +       dentry = file->f_path.dentry;
13215 +       finfo = au_cache_alloc_finfo();
13216 +       if (unlikely(!finfo))
13217 +               goto out;
13218 +
13219 +       err = 0;
13220 +       au_nfiles_inc(dentry->d_sb);
13221 +       /* verbose coding for lock class name */
13222 +       if (!fidir)
13223 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
13224 +       else
13225 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
13226 +       au_rw_write_lock(&finfo->fi_rwsem);
13227 +       finfo->fi_btop = -1;
13228 +       finfo->fi_hdir = fidir;
13229 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
13230 +       /* smp_mb(); */ /* atomic_set */
13231 +
13232 +       file->private_data = finfo;
13233 +
13234 +out:
13235 +       return err;
13236 +}
13237 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
13238 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
13239 +++ linux/fs/aufs/f_op.c        2015-06-22 08:29:23.706757681 +0200
13240 @@ -0,0 +1,748 @@
13241 +/*
13242 + * Copyright (C) 2005-2015 Junjiro R. Okajima
13243 + *
13244 + * This program, aufs is free software; you can redistribute it and/or modify
13245 + * it under the terms of the GNU General Public License as published by
13246 + * the Free Software Foundation; either version 2 of the License, or
13247 + * (at your option) any later version.
13248 + *
13249 + * This program is distributed in the hope that it will be useful,
13250 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13251 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13252 + * GNU General Public License for more details.
13253 + *
13254 + * You should have received a copy of the GNU General Public License
13255 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13256 + */
13257 +
13258 +/*
13259 + * file and vm operations
13260 + */
13261 +
13262 +#include <linux/aio.h>
13263 +#include <linux/fs_stack.h>
13264 +#include <linux/mman.h>
13265 +#include <linux/security.h>
13266 +#include "aufs.h"
13267 +
13268 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
13269 +{
13270 +       int err;
13271 +       aufs_bindex_t bindex;
13272 +       struct dentry *dentry;
13273 +       struct au_finfo *finfo;
13274 +       struct inode *h_inode;
13275 +
13276 +       FiMustWriteLock(file);
13277 +
13278 +       err = 0;
13279 +       dentry = file->f_path.dentry;
13280 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
13281 +       finfo = au_fi(file);
13282 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
13283 +       atomic_set(&finfo->fi_mmapped, 0);
13284 +       bindex = au_dbstart(dentry);
13285 +       if (!h_file)
13286 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
13287 +       else
13288 +               get_file(h_file);
13289 +       if (IS_ERR(h_file))
13290 +               err = PTR_ERR(h_file);
13291 +       else {
13292 +               if ((flags & __O_TMPFILE)
13293 +                   && !(flags & O_EXCL)) {
13294 +                       h_inode = file_inode(h_file);
13295 +                       spin_lock(&h_inode->i_lock);
13296 +                       h_inode->i_state |= I_LINKABLE;
13297 +                       spin_unlock(&h_inode->i_lock);
13298 +               }
13299 +               au_set_fbstart(file, bindex);
13300 +               au_set_h_fptr(file, bindex, h_file);
13301 +               au_update_figen(file);
13302 +               /* todo: necessary? */
13303 +               /* file->f_ra = h_file->f_ra; */
13304 +       }
13305 +
13306 +       return err;
13307 +}
13308 +
13309 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
13310 +                           struct file *file)
13311 +{
13312 +       int err;
13313 +       struct super_block *sb;
13314 +       struct au_do_open_args args = {
13315 +               .open   = au_do_open_nondir
13316 +       };
13317 +
13318 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13319 +             file, vfsub_file_flags(file), file->f_mode);
13320 +
13321 +       sb = file->f_path.dentry->d_sb;
13322 +       si_read_lock(sb, AuLock_FLUSH);
13323 +       err = au_do_open(file, &args);
13324 +       si_read_unlock(sb);
13325 +       return err;
13326 +}
13327 +
13328 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13329 +{
13330 +       struct au_finfo *finfo;
13331 +       aufs_bindex_t bindex;
13332 +
13333 +       finfo = au_fi(file);
13334 +       au_sphl_del(&finfo->fi_hlist,
13335 +                   &au_sbi(file->f_path.dentry->d_sb)->si_files);
13336 +       bindex = finfo->fi_btop;
13337 +       if (bindex >= 0)
13338 +               au_set_h_fptr(file, bindex, NULL);
13339 +
13340 +       au_finfo_fin(file);
13341 +       return 0;
13342 +}
13343 +
13344 +/* ---------------------------------------------------------------------- */
13345 +
13346 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
13347 +{
13348 +       int err;
13349 +       struct file *h_file;
13350 +
13351 +       err = 0;
13352 +       h_file = au_hf_top(file);
13353 +       if (h_file)
13354 +               err = vfsub_flush(h_file, id);
13355 +       return err;
13356 +}
13357 +
13358 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13359 +{
13360 +       return au_do_flush(file, id, au_do_flush_nondir);
13361 +}
13362 +
13363 +/* ---------------------------------------------------------------------- */
13364 +/*
13365 + * read and write functions acquire [fdi]_rwsem once, but release before
13366 + * mmap_sem. This is because to stop a race condition between mmap(2).
13367 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
13368 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13369 + * read functions after [fdi]_rwsem are released, but it should be harmless.
13370 + */
13371 +
13372 +/* Callers should call au_read_post() or fput() in the end */
13373 +struct file *au_read_pre(struct file *file, int keep_fi)
13374 +{
13375 +       struct file *h_file;
13376 +       int err;
13377 +
13378 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
13379 +       if (!err) {
13380 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
13381 +               h_file = au_hf_top(file);
13382 +               get_file(h_file);
13383 +               if (!keep_fi)
13384 +                       fi_read_unlock(file);
13385 +       } else
13386 +               h_file = ERR_PTR(err);
13387 +
13388 +       return h_file;
13389 +}
13390 +
13391 +static void au_read_post(struct inode *inode, struct file *h_file)
13392 +{
13393 +       /* update without lock, I don't think it a problem */
13394 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
13395 +       fput(h_file);
13396 +}
13397 +
13398 +struct au_write_pre {
13399 +       blkcnt_t blks;
13400 +       aufs_bindex_t bstart;
13401 +};
13402 +
13403 +/*
13404 + * return with iinfo is write-locked
13405 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
13406 + * end
13407 + */
13408 +static struct file *au_write_pre(struct file *file, int do_ready,
13409 +                                struct au_write_pre *wpre)
13410 +{
13411 +       struct file *h_file;
13412 +       struct dentry *dentry;
13413 +       int err;
13414 +       struct au_pin pin;
13415 +
13416 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13417 +       h_file = ERR_PTR(err);
13418 +       if (unlikely(err))
13419 +               goto out;
13420 +
13421 +       dentry = file->f_path.dentry;
13422 +       if (do_ready) {
13423 +               err = au_ready_to_write(file, -1, &pin);
13424 +               if (unlikely(err)) {
13425 +                       h_file = ERR_PTR(err);
13426 +                       di_write_unlock(dentry);
13427 +                       goto out_fi;
13428 +               }
13429 +       }
13430 +
13431 +       di_downgrade_lock(dentry, /*flags*/0);
13432 +       if (wpre)
13433 +               wpre->bstart = au_fbstart(file);
13434 +       h_file = au_hf_top(file);
13435 +       get_file(h_file);
13436 +       if (wpre)
13437 +               wpre->blks = file_inode(h_file)->i_blocks;
13438 +       if (do_ready)
13439 +               au_unpin(&pin);
13440 +       di_read_unlock(dentry, /*flags*/0);
13441 +
13442 +out_fi:
13443 +       fi_write_unlock(file);
13444 +out:
13445 +       return h_file;
13446 +}
13447 +
13448 +static void au_write_post(struct inode *inode, struct file *h_file,
13449 +                         struct au_write_pre *wpre, ssize_t written)
13450 +{
13451 +       struct inode *h_inode;
13452 +
13453 +       au_cpup_attr_timesizes(inode);
13454 +       AuDebugOn(au_ibstart(inode) != wpre->bstart);
13455 +       h_inode = file_inode(h_file);
13456 +       inode->i_mode = h_inode->i_mode;
13457 +       ii_write_unlock(inode);
13458 +       fput(h_file);
13459 +
13460 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
13461 +       if (written > 0)
13462 +               au_fhsm_wrote(inode->i_sb, wpre->bstart,
13463 +                             /*force*/h_inode->i_blocks > wpre->blks);
13464 +}
13465 +
13466 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
13467 +                        loff_t *ppos)
13468 +{
13469 +       ssize_t err;
13470 +       struct inode *inode;
13471 +       struct file *h_file;
13472 +       struct super_block *sb;
13473 +
13474 +       inode = file_inode(file);
13475 +       sb = inode->i_sb;
13476 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13477 +
13478 +       h_file = au_read_pre(file, /*keep_fi*/0);
13479 +       err = PTR_ERR(h_file);
13480 +       if (IS_ERR(h_file))
13481 +               goto out;
13482 +
13483 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
13484 +       err = vfsub_read_u(h_file, buf, count, ppos);
13485 +       /* todo: necessary? */
13486 +       /* file->f_ra = h_file->f_ra; */
13487 +       au_read_post(inode, h_file);
13488 +
13489 +out:
13490 +       si_read_unlock(sb);
13491 +       return err;
13492 +}
13493 +
13494 +/*
13495 + * todo: very ugly
13496 + * it locks both of i_mutex and si_rwsem for read in safe.
13497 + * if the plink maintenance mode continues forever (that is the problem),
13498 + * may loop forever.
13499 + */
13500 +static void au_mtx_and_read_lock(struct inode *inode)
13501 +{
13502 +       int err;
13503 +       struct super_block *sb = inode->i_sb;
13504 +
13505 +       while (1) {
13506 +               mutex_lock(&inode->i_mutex);
13507 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13508 +               if (!err)
13509 +                       break;
13510 +               mutex_unlock(&inode->i_mutex);
13511 +               si_read_lock(sb, AuLock_NOPLMW);
13512 +               si_read_unlock(sb);
13513 +       }
13514 +}
13515 +
13516 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
13517 +                         size_t count, loff_t *ppos)
13518 +{
13519 +       ssize_t err;
13520 +       struct au_write_pre wpre;
13521 +       struct inode *inode;
13522 +       struct file *h_file;
13523 +       char __user *buf = (char __user *)ubuf;
13524 +
13525 +       inode = file_inode(file);
13526 +       au_mtx_and_read_lock(inode);
13527 +
13528 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13529 +       err = PTR_ERR(h_file);
13530 +       if (IS_ERR(h_file))
13531 +               goto out;
13532 +
13533 +       err = vfsub_write_u(h_file, buf, count, ppos);
13534 +       au_write_post(inode, h_file, &wpre, err);
13535 +
13536 +out:
13537 +       si_read_unlock(inode->i_sb);
13538 +       mutex_unlock(&inode->i_mutex);
13539 +       return err;
13540 +}
13541 +
13542 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13543 +                         struct iov_iter *iov_iter)
13544 +{
13545 +       ssize_t err;
13546 +       struct file *file;
13547 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
13548 +       ssize_t (*aio)(struct kiocb *, const struct iovec *, unsigned long,
13549 +                      loff_t);
13550 +
13551 +       err = security_file_permission(h_file, rw);
13552 +       if (unlikely(err))
13553 +               goto out;
13554 +
13555 +       err = -ENOSYS;
13556 +       iter = NULL;
13557 +       aio = NULL;
13558 +       if (rw == MAY_READ) {
13559 +               iter = h_file->f_op->read_iter;
13560 +               aio = h_file->f_op->aio_read;
13561 +       } else if (rw == MAY_WRITE) {
13562 +               iter = h_file->f_op->write_iter;
13563 +               aio = h_file->f_op->aio_write;
13564 +       }
13565 +
13566 +       file = kio->ki_filp;
13567 +       kio->ki_filp = h_file;
13568 +       if (iter) {
13569 +               lockdep_off();
13570 +               err = iter(kio, iov_iter);
13571 +               lockdep_on();
13572 +       } else if (aio) {
13573 +               lockdep_off();
13574 +               err = aio(kio, iov_iter->iov, iov_iter->nr_segs, kio->ki_pos);
13575 +               lockdep_on();
13576 +       } else
13577 +               /* currently there is no such fs */
13578 +               WARN_ON_ONCE(1);
13579 +       kio->ki_filp = file;
13580 +
13581 +out:
13582 +       return err;
13583 +}
13584 +
13585 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13586 +{
13587 +       ssize_t err;
13588 +       struct file *file, *h_file;
13589 +       struct inode *inode;
13590 +       struct super_block *sb;
13591 +
13592 +       file = kio->ki_filp;
13593 +       inode = file_inode(file);
13594 +       sb = inode->i_sb;
13595 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13596 +
13597 +       h_file = au_read_pre(file, /*keep_fi*/0);
13598 +       err = PTR_ERR(h_file);
13599 +       if (IS_ERR(h_file))
13600 +               goto out;
13601 +
13602 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
13603 +       /* todo: necessary? */
13604 +       /* file->f_ra = h_file->f_ra; */
13605 +       au_read_post(inode, h_file);
13606 +
13607 +out:
13608 +       si_read_unlock(sb);
13609 +       return err;
13610 +}
13611 +
13612 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13613 +{
13614 +       ssize_t err;
13615 +       struct au_write_pre wpre;
13616 +       struct inode *inode;
13617 +       struct file *file, *h_file;
13618 +
13619 +       file = kio->ki_filp;
13620 +       inode = file_inode(file);
13621 +       au_mtx_and_read_lock(inode);
13622 +
13623 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13624 +       err = PTR_ERR(h_file);
13625 +       if (IS_ERR(h_file))
13626 +               goto out;
13627 +
13628 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
13629 +       au_write_post(inode, h_file, &wpre, err);
13630 +
13631 +out:
13632 +       si_read_unlock(inode->i_sb);
13633 +       mutex_unlock(&inode->i_mutex);
13634 +       return err;
13635 +}
13636 +
13637 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
13638 +                               struct pipe_inode_info *pipe, size_t len,
13639 +                               unsigned int flags)
13640 +{
13641 +       ssize_t err;
13642 +       struct file *h_file;
13643 +       struct inode *inode;
13644 +       struct super_block *sb;
13645 +
13646 +       inode = file_inode(file);
13647 +       sb = inode->i_sb;
13648 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13649 +
13650 +       h_file = au_read_pre(file, /*keep_fi*/1);
13651 +       err = PTR_ERR(h_file);
13652 +       if (IS_ERR(h_file))
13653 +               goto out;
13654 +
13655 +       if (au_test_loopback_kthread()) {
13656 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
13657 +               if (file->f_mapping != h_file->f_mapping) {
13658 +                       file->f_mapping = h_file->f_mapping;
13659 +                       smp_mb(); /* unnecessary? */
13660 +               }
13661 +       }
13662 +       fi_read_unlock(file);
13663 +
13664 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
13665 +       /* todo: necessasry? */
13666 +       /* file->f_ra = h_file->f_ra; */
13667 +       au_read_post(inode, h_file);
13668 +
13669 +out:
13670 +       si_read_unlock(sb);
13671 +       return err;
13672 +}
13673 +
13674 +static ssize_t
13675 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
13676 +                 size_t len, unsigned int flags)
13677 +{
13678 +       ssize_t err;
13679 +       struct au_write_pre wpre;
13680 +       struct inode *inode;
13681 +       struct file *h_file;
13682 +
13683 +       inode = file_inode(file);
13684 +       au_mtx_and_read_lock(inode);
13685 +
13686 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13687 +       err = PTR_ERR(h_file);
13688 +       if (IS_ERR(h_file))
13689 +               goto out;
13690 +
13691 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
13692 +       au_write_post(inode, h_file, &wpre, err);
13693 +
13694 +out:
13695 +       si_read_unlock(inode->i_sb);
13696 +       mutex_unlock(&inode->i_mutex);
13697 +       return err;
13698 +}
13699 +
13700 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
13701 +                          loff_t len)
13702 +{
13703 +       long err;
13704 +       struct au_write_pre wpre;
13705 +       struct inode *inode;
13706 +       struct file *h_file;
13707 +
13708 +       inode = file_inode(file);
13709 +       au_mtx_and_read_lock(inode);
13710 +
13711 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13712 +       err = PTR_ERR(h_file);
13713 +       if (IS_ERR(h_file))
13714 +               goto out;
13715 +
13716 +       lockdep_off();
13717 +       err = vfs_fallocate(h_file, mode, offset, len);
13718 +       lockdep_on();
13719 +       au_write_post(inode, h_file, &wpre, /*written*/1);
13720 +
13721 +out:
13722 +       si_read_unlock(inode->i_sb);
13723 +       mutex_unlock(&inode->i_mutex);
13724 +       return err;
13725 +}
13726 +
13727 +/* ---------------------------------------------------------------------- */
13728 +
13729 +/*
13730 + * The locking order around current->mmap_sem.
13731 + * - in most and regular cases
13732 + *   file I/O syscall -- aufs_read() or something
13733 + *     -- si_rwsem for read -- mmap_sem
13734 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
13735 + * - in mmap case
13736 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
13737 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
13738 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
13739 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
13740 + * It means that when aufs acquires si_rwsem for write, the process should never
13741 + * acquire mmap_sem.
13742 + *
13743 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
13744 + * problem either since any directory is not able to be mmap-ed.
13745 + * The similar scenario is applied to aufs_readlink() too.
13746 + */
13747 +
13748 +#if 0 /* stop calling security_file_mmap() */
13749 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
13750 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
13751 +
13752 +static unsigned long au_arch_prot_conv(unsigned long flags)
13753 +{
13754 +       /* currently ppc64 only */
13755 +#ifdef CONFIG_PPC64
13756 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
13757 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
13758 +       return AuConv_VM_PROT(flags, SAO);
13759 +#else
13760 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
13761 +       return 0;
13762 +#endif
13763 +}
13764 +
13765 +static unsigned long au_prot_conv(unsigned long flags)
13766 +{
13767 +       return AuConv_VM_PROT(flags, READ)
13768 +               | AuConv_VM_PROT(flags, WRITE)
13769 +               | AuConv_VM_PROT(flags, EXEC)
13770 +               | au_arch_prot_conv(flags);
13771 +}
13772 +
13773 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
13774 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
13775 +
13776 +static unsigned long au_flag_conv(unsigned long flags)
13777 +{
13778 +       return AuConv_VM_MAP(flags, GROWSDOWN)
13779 +               | AuConv_VM_MAP(flags, DENYWRITE)
13780 +               | AuConv_VM_MAP(flags, LOCKED);
13781 +}
13782 +#endif
13783 +
13784 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
13785 +{
13786 +       int err;
13787 +       const unsigned char wlock
13788 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
13789 +       struct super_block *sb;
13790 +       struct file *h_file;
13791 +       struct inode *inode;
13792 +
13793 +       AuDbgVmRegion(file, vma);
13794 +
13795 +       inode = file_inode(file);
13796 +       sb = inode->i_sb;
13797 +       lockdep_off();
13798 +       si_read_lock(sb, AuLock_NOPLMW);
13799 +
13800 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
13801 +       lockdep_on();
13802 +       err = PTR_ERR(h_file);
13803 +       if (IS_ERR(h_file))
13804 +               goto out;
13805 +
13806 +       err = 0;
13807 +       au_set_mmapped(file);
13808 +       au_vm_file_reset(vma, h_file);
13809 +       /*
13810 +        * we cannot call security_mmap_file() here since it may acquire
13811 +        * mmap_sem or i_mutex.
13812 +        *
13813 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
13814 +        *                       au_flag_conv(vma->vm_flags));
13815 +        */
13816 +       if (!err)
13817 +               err = h_file->f_op->mmap(h_file, vma);
13818 +       if (!err) {
13819 +               au_vm_prfile_set(vma, file);
13820 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
13821 +               goto out_fput; /* success */
13822 +       }
13823 +       au_unset_mmapped(file);
13824 +       au_vm_file_reset(vma, file);
13825 +
13826 +out_fput:
13827 +       lockdep_off();
13828 +       ii_write_unlock(inode);
13829 +       lockdep_on();
13830 +       fput(h_file);
13831 +out:
13832 +       lockdep_off();
13833 +       si_read_unlock(sb);
13834 +       lockdep_on();
13835 +       AuTraceErr(err);
13836 +       return err;
13837 +}
13838 +
13839 +/* ---------------------------------------------------------------------- */
13840 +
13841 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
13842 +                            int datasync)
13843 +{
13844 +       int err;
13845 +       struct au_write_pre wpre;
13846 +       struct inode *inode;
13847 +       struct file *h_file;
13848 +
13849 +       err = 0; /* -EBADF; */ /* posix? */
13850 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
13851 +               goto out;
13852 +
13853 +       inode = file_inode(file);
13854 +       au_mtx_and_read_lock(inode);
13855 +
13856 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13857 +       err = PTR_ERR(h_file);
13858 +       if (IS_ERR(h_file))
13859 +               goto out_unlock;
13860 +
13861 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
13862 +       au_write_post(inode, h_file, &wpre, /*written*/0);
13863 +
13864 +out_unlock:
13865 +       si_read_unlock(inode->i_sb);
13866 +       mutex_unlock(&inode->i_mutex);
13867 +out:
13868 +       return err;
13869 +}
13870 +
13871 +/* no one supports this operation, currently */
13872 +#if 0
13873 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
13874 +{
13875 +       int err;
13876 +       struct au_write_pre wpre;
13877 +       struct inode *inode;
13878 +       struct file *file, *h_file;
13879 +
13880 +       err = 0; /* -EBADF; */ /* posix? */
13881 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
13882 +               goto out;
13883 +
13884 +       file = kio->ki_filp;
13885 +       inode = file_inode(file);
13886 +       au_mtx_and_read_lock(inode);
13887 +
13888 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13889 +       err = PTR_ERR(h_file);
13890 +       if (IS_ERR(h_file))
13891 +               goto out_unlock;
13892 +
13893 +       err = -ENOSYS;
13894 +       h_file = au_hf_top(file);
13895 +       if (h_file->f_op->aio_fsync) {
13896 +               struct mutex *h_mtx;
13897 +
13898 +               h_mtx = &file_inode(h_file)->i_mutex;
13899 +               if (!is_sync_kiocb(kio)) {
13900 +                       get_file(h_file);
13901 +                       fput(file);
13902 +               }
13903 +               kio->ki_filp = h_file;
13904 +               err = h_file->f_op->aio_fsync(kio, datasync);
13905 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
13906 +               if (!err)
13907 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
13908 +               /*ignore*/
13909 +               mutex_unlock(h_mtx);
13910 +       }
13911 +       au_write_post(inode, h_file, &wpre, /*written*/0);
13912 +
13913 +out_unlock:
13914 +       si_read_unlock(inode->sb);
13915 +       mutex_unlock(&inode->i_mutex);
13916 +out:
13917 +       return err;
13918 +}
13919 +#endif
13920 +
13921 +static int aufs_fasync(int fd, struct file *file, int flag)
13922 +{
13923 +       int err;
13924 +       struct file *h_file;
13925 +       struct super_block *sb;
13926 +
13927 +       sb = file->f_path.dentry->d_sb;
13928 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13929 +
13930 +       h_file = au_read_pre(file, /*keep_fi*/0);
13931 +       err = PTR_ERR(h_file);
13932 +       if (IS_ERR(h_file))
13933 +               goto out;
13934 +
13935 +       if (h_file->f_op->fasync)
13936 +               err = h_file->f_op->fasync(fd, h_file, flag);
13937 +       fput(h_file); /* instead of au_read_post() */
13938 +
13939 +out:
13940 +       si_read_unlock(sb);
13941 +       return err;
13942 +}
13943 +
13944 +/* ---------------------------------------------------------------------- */
13945 +
13946 +/* no one supports this operation, currently */
13947 +#if 0
13948 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
13949 +                            size_t len, loff_t *pos, int more)
13950 +{
13951 +}
13952 +#endif
13953 +
13954 +/* ---------------------------------------------------------------------- */
13955 +
13956 +const struct file_operations aufs_file_fop = {
13957 +       .owner          = THIS_MODULE,
13958 +
13959 +       .llseek         = default_llseek,
13960 +
13961 +       .read           = aufs_read,
13962 +       .write          = aufs_write,
13963 +       .read_iter      = aufs_read_iter,
13964 +       .write_iter     = aufs_write_iter,
13965 +
13966 +#ifdef CONFIG_AUFS_POLL
13967 +       .poll           = aufs_poll,
13968 +#endif
13969 +       .unlocked_ioctl = aufs_ioctl_nondir,
13970 +#ifdef CONFIG_COMPAT
13971 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
13972 +#endif
13973 +       .mmap           = aufs_mmap,
13974 +       .open           = aufs_open_nondir,
13975 +       .flush          = aufs_flush_nondir,
13976 +       .release        = aufs_release_nondir,
13977 +       .fsync          = aufs_fsync_nondir,
13978 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
13979 +       .fasync         = aufs_fasync,
13980 +       /* .sendpage    = aufs_sendpage, */
13981 +       .splice_write   = aufs_splice_write,
13982 +       .splice_read    = aufs_splice_read,
13983 +#if 0
13984 +       .aio_splice_write = aufs_aio_splice_write,
13985 +       .aio_splice_read  = aufs_aio_splice_read,
13986 +#endif
13987 +       .fallocate      = aufs_fallocate
13988 +};
13989 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
13990 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
13991 +++ linux/fs/aufs/fstype.h      2015-06-22 08:27:37.880835119 +0200
13992 @@ -0,0 +1,400 @@
13993 +/*
13994 + * Copyright (C) 2005-2015 Junjiro R. Okajima
13995 + *
13996 + * This program, aufs is free software; you can redistribute it and/or modify
13997 + * it under the terms of the GNU General Public License as published by
13998 + * the Free Software Foundation; either version 2 of the License, or
13999 + * (at your option) any later version.
14000 + *
14001 + * This program is distributed in the hope that it will be useful,
14002 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14003 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14004 + * GNU General Public License for more details.
14005 + *
14006 + * You should have received a copy of the GNU General Public License
14007 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14008 + */
14009 +
14010 +/*
14011 + * judging filesystem type
14012 + */
14013 +
14014 +#ifndef __AUFS_FSTYPE_H__
14015 +#define __AUFS_FSTYPE_H__
14016 +
14017 +#ifdef __KERNEL__
14018 +
14019 +#include <linux/fs.h>
14020 +#include <linux/magic.h>
14021 +#include <linux/romfs_fs.h>
14022 +#include <linux/nfs_fs.h>
14023 +
14024 +static inline int au_test_aufs(struct super_block *sb)
14025 +{
14026 +       return sb->s_magic == AUFS_SUPER_MAGIC;
14027 +}
14028 +
14029 +static inline const char *au_sbtype(struct super_block *sb)
14030 +{
14031 +       return sb->s_type->name;
14032 +}
14033 +
14034 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
14035 +{
14036 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
14037 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
14038 +#else
14039 +       return 0;
14040 +#endif
14041 +}
14042 +
14043 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
14044 +{
14045 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
14046 +       return sb->s_magic == ROMFS_MAGIC;
14047 +#else
14048 +       return 0;
14049 +#endif
14050 +}
14051 +
14052 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
14053 +{
14054 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
14055 +       return sb->s_magic == CRAMFS_MAGIC;
14056 +#endif
14057 +       return 0;
14058 +}
14059 +
14060 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
14061 +{
14062 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
14063 +       return sb->s_magic == NFS_SUPER_MAGIC;
14064 +#else
14065 +       return 0;
14066 +#endif
14067 +}
14068 +
14069 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
14070 +{
14071 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
14072 +       return sb->s_magic == FUSE_SUPER_MAGIC;
14073 +#else
14074 +       return 0;
14075 +#endif
14076 +}
14077 +
14078 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
14079 +{
14080 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
14081 +       return sb->s_magic == XFS_SB_MAGIC;
14082 +#else
14083 +       return 0;
14084 +#endif
14085 +}
14086 +
14087 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
14088 +{
14089 +#ifdef CONFIG_TMPFS
14090 +       return sb->s_magic == TMPFS_MAGIC;
14091 +#else
14092 +       return 0;
14093 +#endif
14094 +}
14095 +
14096 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
14097 +{
14098 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
14099 +       return !strcmp(au_sbtype(sb), "ecryptfs");
14100 +#else
14101 +       return 0;
14102 +#endif
14103 +}
14104 +
14105 +static inline int au_test_ramfs(struct super_block *sb)
14106 +{
14107 +       return sb->s_magic == RAMFS_MAGIC;
14108 +}
14109 +
14110 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
14111 +{
14112 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
14113 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
14114 +#else
14115 +       return 0;
14116 +#endif
14117 +}
14118 +
14119 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
14120 +{
14121 +#ifdef CONFIG_PROC_FS
14122 +       return sb->s_magic == PROC_SUPER_MAGIC;
14123 +#else
14124 +       return 0;
14125 +#endif
14126 +}
14127 +
14128 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
14129 +{
14130 +#ifdef CONFIG_SYSFS
14131 +       return sb->s_magic == SYSFS_MAGIC;
14132 +#else
14133 +       return 0;
14134 +#endif
14135 +}
14136 +
14137 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
14138 +{
14139 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
14140 +       return sb->s_magic == CONFIGFS_MAGIC;
14141 +#else
14142 +       return 0;
14143 +#endif
14144 +}
14145 +
14146 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
14147 +{
14148 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
14149 +       return sb->s_magic == MINIX3_SUPER_MAGIC
14150 +               || sb->s_magic == MINIX2_SUPER_MAGIC
14151 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
14152 +               || sb->s_magic == MINIX_SUPER_MAGIC
14153 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
14154 +#else
14155 +       return 0;
14156 +#endif
14157 +}
14158 +
14159 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
14160 +{
14161 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
14162 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
14163 +#else
14164 +       return 0;
14165 +#endif
14166 +}
14167 +
14168 +static inline int au_test_msdos(struct super_block *sb)
14169 +{
14170 +       return au_test_fat(sb);
14171 +}
14172 +
14173 +static inline int au_test_vfat(struct super_block *sb)
14174 +{
14175 +       return au_test_fat(sb);
14176 +}
14177 +
14178 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
14179 +{
14180 +#ifdef CONFIG_SECURITYFS
14181 +       return sb->s_magic == SECURITYFS_MAGIC;
14182 +#else
14183 +       return 0;
14184 +#endif
14185 +}
14186 +
14187 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
14188 +{
14189 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
14190 +       return sb->s_magic == SQUASHFS_MAGIC;
14191 +#else
14192 +       return 0;
14193 +#endif
14194 +}
14195 +
14196 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
14197 +{
14198 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
14199 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
14200 +#else
14201 +       return 0;
14202 +#endif
14203 +}
14204 +
14205 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
14206 +{
14207 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
14208 +       return sb->s_magic == XENFS_SUPER_MAGIC;
14209 +#else
14210 +       return 0;
14211 +#endif
14212 +}
14213 +
14214 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
14215 +{
14216 +#ifdef CONFIG_DEBUG_FS
14217 +       return sb->s_magic == DEBUGFS_MAGIC;
14218 +#else
14219 +       return 0;
14220 +#endif
14221 +}
14222 +
14223 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
14224 +{
14225 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
14226 +       return sb->s_magic == NILFS_SUPER_MAGIC;
14227 +#else
14228 +       return 0;
14229 +#endif
14230 +}
14231 +
14232 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
14233 +{
14234 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
14235 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
14236 +#else
14237 +       return 0;
14238 +#endif
14239 +}
14240 +
14241 +/* ---------------------------------------------------------------------- */
14242 +/*
14243 + * they can't be an aufs branch.
14244 + */
14245 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
14246 +{
14247 +       return
14248 +#ifndef CONFIG_AUFS_BR_RAMFS
14249 +               au_test_ramfs(sb) ||
14250 +#endif
14251 +               au_test_procfs(sb)
14252 +               || au_test_sysfs(sb)
14253 +               || au_test_configfs(sb)
14254 +               || au_test_debugfs(sb)
14255 +               || au_test_securityfs(sb)
14256 +               || au_test_xenfs(sb)
14257 +               || au_test_ecryptfs(sb)
14258 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
14259 +               || au_test_aufs(sb); /* will be supported in next version */
14260 +}
14261 +
14262 +static inline int au_test_fs_remote(struct super_block *sb)
14263 +{
14264 +       return !au_test_tmpfs(sb)
14265 +#ifdef CONFIG_AUFS_BR_RAMFS
14266 +               && !au_test_ramfs(sb)
14267 +#endif
14268 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
14269 +}
14270 +
14271 +/* ---------------------------------------------------------------------- */
14272 +
14273 +/*
14274 + * Note: these functions (below) are created after reading ->getattr() in all
14275 + * filesystems under linux/fs. it means we have to do so in every update...
14276 + */
14277 +
14278 +/*
14279 + * some filesystems require getattr to refresh the inode attributes before
14280 + * referencing.
14281 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
14282 + * and leave the work for d_revalidate()
14283 + */
14284 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
14285 +{
14286 +       return au_test_nfs(sb)
14287 +               || au_test_fuse(sb)
14288 +               /* || au_test_btrfs(sb) */      /* untested */
14289 +               ;
14290 +}
14291 +
14292 +/*
14293 + * filesystems which don't maintain i_size or i_blocks.
14294 + */
14295 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
14296 +{
14297 +       return au_test_xfs(sb)
14298 +               || au_test_btrfs(sb)
14299 +               || au_test_ubifs(sb)
14300 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
14301 +               /* || au_test_minix(sb) */      /* untested */
14302 +               ;
14303 +}
14304 +
14305 +/*
14306 + * filesystems which don't store the correct value in some of their inode
14307 + * attributes.
14308 + */
14309 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
14310 +{
14311 +       return au_test_fs_bad_iattr_size(sb)
14312 +               || au_test_fat(sb)
14313 +               || au_test_msdos(sb)
14314 +               || au_test_vfat(sb);
14315 +}
14316 +
14317 +/* they don't check i_nlink in link(2) */
14318 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
14319 +{
14320 +       return au_test_tmpfs(sb)
14321 +#ifdef CONFIG_AUFS_BR_RAMFS
14322 +               || au_test_ramfs(sb)
14323 +#endif
14324 +               || au_test_ubifs(sb)
14325 +               || au_test_hfsplus(sb);
14326 +}
14327 +
14328 +/*
14329 + * filesystems which sets S_NOATIME and S_NOCMTIME.
14330 + */
14331 +static inline int au_test_fs_notime(struct super_block *sb)
14332 +{
14333 +       return au_test_nfs(sb)
14334 +               || au_test_fuse(sb)
14335 +               || au_test_ubifs(sb)
14336 +               ;
14337 +}
14338 +
14339 +/* temporary support for i#1 in cramfs */
14340 +static inline int au_test_fs_unique_ino(struct inode *inode)
14341 +{
14342 +       if (au_test_cramfs(inode->i_sb))
14343 +               return inode->i_ino != 1;
14344 +       return 1;
14345 +}
14346 +
14347 +/* ---------------------------------------------------------------------- */
14348 +
14349 +/*
14350 + * the filesystem where the xino files placed must support i/o after unlink and
14351 + * maintain i_size and i_blocks.
14352 + */
14353 +static inline int au_test_fs_bad_xino(struct super_block *sb)
14354 +{
14355 +       return au_test_fs_remote(sb)
14356 +               || au_test_fs_bad_iattr_size(sb)
14357 +               /* don't want unnecessary work for xino */
14358 +               || au_test_aufs(sb)
14359 +               || au_test_ecryptfs(sb)
14360 +               || au_test_nilfs(sb);
14361 +}
14362 +
14363 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
14364 +{
14365 +       return au_test_tmpfs(sb)
14366 +               || au_test_ramfs(sb);
14367 +}
14368 +
14369 +/*
14370 + * test if the @sb is real-readonly.
14371 + */
14372 +static inline int au_test_fs_rr(struct super_block *sb)
14373 +{
14374 +       return au_test_squashfs(sb)
14375 +               || au_test_iso9660(sb)
14376 +               || au_test_cramfs(sb)
14377 +               || au_test_romfs(sb);
14378 +}
14379 +
14380 +/*
14381 + * test if the @inode is nfs with 'noacl' option
14382 + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
14383 + */
14384 +static inline int au_test_nfs_noacl(struct inode *inode)
14385 +{
14386 +       return au_test_nfs(inode->i_sb)
14387 +               /* && IS_POSIXACL(inode) */
14388 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
14389 +}
14390 +
14391 +#endif /* __KERNEL__ */
14392 +#endif /* __AUFS_FSTYPE_H__ */
14393 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
14394 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
14395 +++ linux/fs/aufs/hfsnotify.c   2015-06-22 08:27:37.880835119 +0200
14396 @@ -0,0 +1,288 @@
14397 +/*
14398 + * Copyright (C) 2005-2015 Junjiro R. Okajima
14399 + *
14400 + * This program, aufs is free software; you can redistribute it and/or modify
14401 + * it under the terms of the GNU General Public License as published by
14402 + * the Free Software Foundation; either version 2 of the License, or
14403 + * (at your option) any later version.
14404 + *
14405 + * This program is distributed in the hope that it will be useful,
14406 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14407 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14408 + * GNU General Public License for more details.
14409 + *
14410 + * You should have received a copy of the GNU General Public License
14411 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14412 + */
14413 +
14414 +/*
14415 + * fsnotify for the lower directories
14416 + */
14417 +
14418 +#include "aufs.h"
14419 +
14420 +/* FS_IN_IGNORED is unnecessary */
14421 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
14422 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
14423 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
14424 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
14425 +
14426 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
14427 +{
14428 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
14429 +                                            hn_mark);
14430 +       AuDbg("here\n");
14431 +       au_cache_free_hnotify(hn);
14432 +       smp_mb__before_atomic();
14433 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
14434 +               wake_up(&au_hfsn_wq);
14435 +}
14436 +
14437 +static int au_hfsn_alloc(struct au_hinode *hinode)
14438 +{
14439 +       int err;
14440 +       struct au_hnotify *hn;
14441 +       struct super_block *sb;
14442 +       struct au_branch *br;
14443 +       struct fsnotify_mark *mark;
14444 +       aufs_bindex_t bindex;
14445 +
14446 +       hn = hinode->hi_notify;
14447 +       sb = hn->hn_aufs_inode->i_sb;
14448 +       bindex = au_br_index(sb, hinode->hi_id);
14449 +       br = au_sbr(sb, bindex);
14450 +       AuDebugOn(!br->br_hfsn);
14451 +
14452 +       mark = &hn->hn_mark;
14453 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
14454 +       mark->mask = AuHfsnMask;
14455 +       /*
14456 +        * by udba rename or rmdir, aufs assign a new inode to the known
14457 +        * h_inode, so specify 1 to allow dups.
14458 +        */
14459 +       lockdep_off();
14460 +       err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
14461 +                                /*mnt*/NULL, /*allow_dups*/1);
14462 +       /* even if err */
14463 +       fsnotify_put_mark(mark);
14464 +       lockdep_on();
14465 +
14466 +       return err;
14467 +}
14468 +
14469 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
14470 +{
14471 +       struct fsnotify_mark *mark;
14472 +       unsigned long long ull;
14473 +       struct fsnotify_group *group;
14474 +
14475 +       ull = atomic64_inc_return(&au_hfsn_ifree);
14476 +       BUG_ON(!ull);
14477 +
14478 +       mark = &hn->hn_mark;
14479 +       spin_lock(&mark->lock);
14480 +       group = mark->group;
14481 +       fsnotify_get_group(group);
14482 +       spin_unlock(&mark->lock);
14483 +       lockdep_off();
14484 +       fsnotify_destroy_mark(mark, group);
14485 +       fsnotify_put_group(group);
14486 +       lockdep_on();
14487 +
14488 +       /* free hn by myself */
14489 +       return 0;
14490 +}
14491 +
14492 +/* ---------------------------------------------------------------------- */
14493 +
14494 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
14495 +{
14496 +       struct fsnotify_mark *mark;
14497 +
14498 +       mark = &hinode->hi_notify->hn_mark;
14499 +       spin_lock(&mark->lock);
14500 +       if (do_set) {
14501 +               AuDebugOn(mark->mask & AuHfsnMask);
14502 +               mark->mask |= AuHfsnMask;
14503 +       } else {
14504 +               AuDebugOn(!(mark->mask & AuHfsnMask));
14505 +               mark->mask &= ~AuHfsnMask;
14506 +       }
14507 +       spin_unlock(&mark->lock);
14508 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
14509 +}
14510 +
14511 +/* ---------------------------------------------------------------------- */
14512 +
14513 +/* #define AuDbgHnotify */
14514 +#ifdef AuDbgHnotify
14515 +static char *au_hfsn_name(u32 mask)
14516 +{
14517 +#ifdef CONFIG_AUFS_DEBUG
14518 +#define test_ret(flag)                         \
14519 +       do {                                    \
14520 +               if (mask & flag)                \
14521 +                       return #flag;           \
14522 +       } while (0)
14523 +       test_ret(FS_ACCESS);
14524 +       test_ret(FS_MODIFY);
14525 +       test_ret(FS_ATTRIB);
14526 +       test_ret(FS_CLOSE_WRITE);
14527 +       test_ret(FS_CLOSE_NOWRITE);
14528 +       test_ret(FS_OPEN);
14529 +       test_ret(FS_MOVED_FROM);
14530 +       test_ret(FS_MOVED_TO);
14531 +       test_ret(FS_CREATE);
14532 +       test_ret(FS_DELETE);
14533 +       test_ret(FS_DELETE_SELF);
14534 +       test_ret(FS_MOVE_SELF);
14535 +       test_ret(FS_UNMOUNT);
14536 +       test_ret(FS_Q_OVERFLOW);
14537 +       test_ret(FS_IN_IGNORED);
14538 +       test_ret(FS_ISDIR);
14539 +       test_ret(FS_IN_ONESHOT);
14540 +       test_ret(FS_EVENT_ON_CHILD);
14541 +       return "";
14542 +#undef test_ret
14543 +#else
14544 +       return "??";
14545 +#endif
14546 +}
14547 +#endif
14548 +
14549 +/* ---------------------------------------------------------------------- */
14550 +
14551 +static void au_hfsn_free_group(struct fsnotify_group *group)
14552 +{
14553 +       struct au_br_hfsnotify *hfsn = group->private;
14554 +
14555 +       AuDbg("here\n");
14556 +       kfree(hfsn);
14557 +}
14558 +
14559 +static int au_hfsn_handle_event(struct fsnotify_group *group,
14560 +                               struct inode *inode,
14561 +                               struct fsnotify_mark *inode_mark,
14562 +                               struct fsnotify_mark *vfsmount_mark,
14563 +                               u32 mask, void *data, int data_type,
14564 +                               const unsigned char *file_name, u32 cookie)
14565 +{
14566 +       int err;
14567 +       struct au_hnotify *hnotify;
14568 +       struct inode *h_dir, *h_inode;
14569 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
14570 +
14571 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
14572 +
14573 +       err = 0;
14574 +       /* if FS_UNMOUNT happens, there must be another bug */
14575 +       AuDebugOn(mask & FS_UNMOUNT);
14576 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
14577 +               goto out;
14578 +
14579 +       h_dir = inode;
14580 +       h_inode = NULL;
14581 +#ifdef AuDbgHnotify
14582 +       au_debug_on();
14583 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
14584 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
14585 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
14586 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
14587 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
14588 +               /* WARN_ON(1); */
14589 +       }
14590 +       au_debug_off();
14591 +#endif
14592 +
14593 +       AuDebugOn(!inode_mark);
14594 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
14595 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
14596 +
14597 +out:
14598 +       return err;
14599 +}
14600 +
14601 +static struct fsnotify_ops au_hfsn_ops = {
14602 +       .handle_event           = au_hfsn_handle_event,
14603 +       .free_group_priv        = au_hfsn_free_group
14604 +};
14605 +
14606 +/* ---------------------------------------------------------------------- */
14607 +
14608 +static void au_hfsn_fin_br(struct au_branch *br)
14609 +{
14610 +       struct au_br_hfsnotify *hfsn;
14611 +
14612 +       hfsn = br->br_hfsn;
14613 +       if (hfsn) {
14614 +               lockdep_off();
14615 +               fsnotify_put_group(hfsn->hfsn_group);
14616 +               lockdep_on();
14617 +       }
14618 +}
14619 +
14620 +static int au_hfsn_init_br(struct au_branch *br, int perm)
14621 +{
14622 +       int err;
14623 +       struct fsnotify_group *group;
14624 +       struct au_br_hfsnotify *hfsn;
14625 +
14626 +       err = 0;
14627 +       br->br_hfsn = NULL;
14628 +       if (!au_br_hnotifyable(perm))
14629 +               goto out;
14630 +
14631 +       err = -ENOMEM;
14632 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
14633 +       if (unlikely(!hfsn))
14634 +               goto out;
14635 +
14636 +       err = 0;
14637 +       group = fsnotify_alloc_group(&au_hfsn_ops);
14638 +       if (IS_ERR(group)) {
14639 +               err = PTR_ERR(group);
14640 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
14641 +               goto out_hfsn;
14642 +       }
14643 +
14644 +       group->private = hfsn;
14645 +       hfsn->hfsn_group = group;
14646 +       br->br_hfsn = hfsn;
14647 +       goto out; /* success */
14648 +
14649 +out_hfsn:
14650 +       kfree(hfsn);
14651 +out:
14652 +       return err;
14653 +}
14654 +
14655 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
14656 +{
14657 +       int err;
14658 +
14659 +       err = 0;
14660 +       if (!br->br_hfsn)
14661 +               err = au_hfsn_init_br(br, perm);
14662 +
14663 +       return err;
14664 +}
14665 +
14666 +/* ---------------------------------------------------------------------- */
14667 +
14668 +static void au_hfsn_fin(void)
14669 +{
14670 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
14671 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
14672 +}
14673 +
14674 +const struct au_hnotify_op au_hnotify_op = {
14675 +       .ctl            = au_hfsn_ctl,
14676 +       .alloc          = au_hfsn_alloc,
14677 +       .free           = au_hfsn_free,
14678 +
14679 +       .fin            = au_hfsn_fin,
14680 +
14681 +       .reset_br       = au_hfsn_reset_br,
14682 +       .fin_br         = au_hfsn_fin_br,
14683 +       .init_br        = au_hfsn_init_br
14684 +};
14685 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
14686 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
14687 +++ linux/fs/aufs/hfsplus.c     2015-06-22 08:29:23.706757681 +0200
14688 @@ -0,0 +1,56 @@
14689 +/*
14690 + * Copyright (C) 2010-2015 Junjiro R. Okajima
14691 + *
14692 + * This program, aufs is free software; you can redistribute it and/or modify
14693 + * it under the terms of the GNU General Public License as published by
14694 + * the Free Software Foundation; either version 2 of the License, or
14695 + * (at your option) any later version.
14696 + *
14697 + * This program is distributed in the hope that it will be useful,
14698 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14699 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14700 + * GNU General Public License for more details.
14701 + *
14702 + * You should have received a copy of the GNU General Public License
14703 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14704 + */
14705 +
14706 +/*
14707 + * special support for filesystems which aqucires an inode mutex
14708 + * at final closing a file, eg, hfsplus.
14709 + *
14710 + * This trick is very simple and stupid, just to open the file before really
14711 + * neceeary open to tell hfsplus that this is not the final closing.
14712 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
14713 + * and au_h_open_post() after releasing it.
14714 + */
14715 +
14716 +#include "aufs.h"
14717 +
14718 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
14719 +                          int force_wr)
14720 +{
14721 +       struct file *h_file;
14722 +       struct dentry *h_dentry;
14723 +
14724 +       h_dentry = au_h_dptr(dentry, bindex);
14725 +       AuDebugOn(!h_dentry);
14726 +       AuDebugOn(!h_dentry->d_inode);
14727 +
14728 +       h_file = NULL;
14729 +       if (au_test_hfsplus(h_dentry->d_sb)
14730 +           && d_is_reg(h_dentry))
14731 +               h_file = au_h_open(dentry, bindex,
14732 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
14733 +                                  /*file*/NULL, force_wr);
14734 +       return h_file;
14735 +}
14736 +
14737 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
14738 +                   struct file *h_file)
14739 +{
14740 +       if (h_file) {
14741 +               fput(h_file);
14742 +               au_sbr_put(dentry->d_sb, bindex);
14743 +       }
14744 +}
14745 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
14746 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
14747 +++ linux/fs/aufs/hnotify.c     2015-06-22 08:29:23.706757681 +0200
14748 @@ -0,0 +1,714 @@
14749 +/*
14750 + * Copyright (C) 2005-2015 Junjiro R. Okajima
14751 + *
14752 + * This program, aufs is free software; you can redistribute it and/or modify
14753 + * it under the terms of the GNU General Public License as published by
14754 + * the Free Software Foundation; either version 2 of the License, or
14755 + * (at your option) any later version.
14756 + *
14757 + * This program is distributed in the hope that it will be useful,
14758 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14759 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14760 + * GNU General Public License for more details.
14761 + *
14762 + * You should have received a copy of the GNU General Public License
14763 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14764 + */
14765 +
14766 +/*
14767 + * abstraction to notify the direct changes on lower directories
14768 + */
14769 +
14770 +#include "aufs.h"
14771 +
14772 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
14773 +{
14774 +       int err;
14775 +       struct au_hnotify *hn;
14776 +
14777 +       err = -ENOMEM;
14778 +       hn = au_cache_alloc_hnotify();
14779 +       if (hn) {
14780 +               hn->hn_aufs_inode = inode;
14781 +               hinode->hi_notify = hn;
14782 +               err = au_hnotify_op.alloc(hinode);
14783 +               AuTraceErr(err);
14784 +               if (unlikely(err)) {
14785 +                       hinode->hi_notify = NULL;
14786 +                       au_cache_free_hnotify(hn);
14787 +                       /*
14788 +                        * The upper dir was removed by udba, but the same named
14789 +                        * dir left. In this case, aufs assignes a new inode
14790 +                        * number and set the monitor again.
14791 +                        * For the lower dir, the old monitnor is still left.
14792 +                        */
14793 +                       if (err == -EEXIST)
14794 +                               err = 0;
14795 +               }
14796 +       }
14797 +
14798 +       AuTraceErr(err);
14799 +       return err;
14800 +}
14801 +
14802 +void au_hn_free(struct au_hinode *hinode)
14803 +{
14804 +       struct au_hnotify *hn;
14805 +
14806 +       hn = hinode->hi_notify;
14807 +       if (hn) {
14808 +               hinode->hi_notify = NULL;
14809 +               if (au_hnotify_op.free(hinode, hn))
14810 +                       au_cache_free_hnotify(hn);
14811 +       }
14812 +}
14813 +
14814 +/* ---------------------------------------------------------------------- */
14815 +
14816 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
14817 +{
14818 +       if (hinode->hi_notify)
14819 +               au_hnotify_op.ctl(hinode, do_set);
14820 +}
14821 +
14822 +void au_hn_reset(struct inode *inode, unsigned int flags)
14823 +{
14824 +       aufs_bindex_t bindex, bend;
14825 +       struct inode *hi;
14826 +       struct dentry *iwhdentry;
14827 +
14828 +       bend = au_ibend(inode);
14829 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
14830 +               hi = au_h_iptr(inode, bindex);
14831 +               if (!hi)
14832 +                       continue;
14833 +
14834 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
14835 +               iwhdentry = au_hi_wh(inode, bindex);
14836 +               if (iwhdentry)
14837 +                       dget(iwhdentry);
14838 +               au_igrab(hi);
14839 +               au_set_h_iptr(inode, bindex, NULL, 0);
14840 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
14841 +                             flags & ~AuHi_XINO);
14842 +               iput(hi);
14843 +               dput(iwhdentry);
14844 +               /* mutex_unlock(&hi->i_mutex); */
14845 +       }
14846 +}
14847 +
14848 +/* ---------------------------------------------------------------------- */
14849 +
14850 +static int hn_xino(struct inode *inode, struct inode *h_inode)
14851 +{
14852 +       int err;
14853 +       aufs_bindex_t bindex, bend, bfound, bstart;
14854 +       struct inode *h_i;
14855 +
14856 +       err = 0;
14857 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14858 +               pr_warn("branch root dir was changed\n");
14859 +               goto out;
14860 +       }
14861 +
14862 +       bfound = -1;
14863 +       bend = au_ibend(inode);
14864 +       bstart = au_ibstart(inode);
14865 +#if 0 /* reserved for future use */
14866 +       if (bindex == bend) {
14867 +               /* keep this ino in rename case */
14868 +               goto out;
14869 +       }
14870 +#endif
14871 +       for (bindex = bstart; bindex <= bend; bindex++)
14872 +               if (au_h_iptr(inode, bindex) == h_inode) {
14873 +                       bfound = bindex;
14874 +                       break;
14875 +               }
14876 +       if (bfound < 0)
14877 +               goto out;
14878 +
14879 +       for (bindex = bstart; bindex <= bend; bindex++) {
14880 +               h_i = au_h_iptr(inode, bindex);
14881 +               if (!h_i)
14882 +                       continue;
14883 +
14884 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
14885 +               /* ignore this error */
14886 +               /* bad action? */
14887 +       }
14888 +
14889 +       /* children inode number will be broken */
14890 +
14891 +out:
14892 +       AuTraceErr(err);
14893 +       return err;
14894 +}
14895 +
14896 +static int hn_gen_tree(struct dentry *dentry)
14897 +{
14898 +       int err, i, j, ndentry;
14899 +       struct au_dcsub_pages dpages;
14900 +       struct au_dpage *dpage;
14901 +       struct dentry **dentries;
14902 +
14903 +       err = au_dpages_init(&dpages, GFP_NOFS);
14904 +       if (unlikely(err))
14905 +               goto out;
14906 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
14907 +       if (unlikely(err))
14908 +               goto out_dpages;
14909 +
14910 +       for (i = 0; i < dpages.ndpage; i++) {
14911 +               dpage = dpages.dpages + i;
14912 +               dentries = dpage->dentries;
14913 +               ndentry = dpage->ndentry;
14914 +               for (j = 0; j < ndentry; j++) {
14915 +                       struct dentry *d;
14916 +
14917 +                       d = dentries[j];
14918 +                       if (IS_ROOT(d))
14919 +                               continue;
14920 +
14921 +                       au_digen_dec(d);
14922 +                       if (d->d_inode)
14923 +                               /* todo: reset children xino?
14924 +                                  cached children only? */
14925 +                               au_iigen_dec(d->d_inode);
14926 +               }
14927 +       }
14928 +
14929 +out_dpages:
14930 +       au_dpages_free(&dpages);
14931 +
14932 +#if 0
14933 +       /* discard children */
14934 +       dentry_unhash(dentry);
14935 +       dput(dentry);
14936 +#endif
14937 +out:
14938 +       return err;
14939 +}
14940 +
14941 +/*
14942 + * return 0 if processed.
14943 + */
14944 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
14945 +                          const unsigned int isdir)
14946 +{
14947 +       int err;
14948 +       struct dentry *d;
14949 +       struct qstr *dname;
14950 +
14951 +       err = 1;
14952 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14953 +               pr_warn("branch root dir was changed\n");
14954 +               err = 0;
14955 +               goto out;
14956 +       }
14957 +
14958 +       if (!isdir) {
14959 +               AuDebugOn(!name);
14960 +               au_iigen_dec(inode);
14961 +               spin_lock(&inode->i_lock);
14962 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
14963 +                       spin_lock(&d->d_lock);
14964 +                       dname = &d->d_name;
14965 +                       if (dname->len != nlen
14966 +                           && memcmp(dname->name, name, nlen)) {
14967 +                               spin_unlock(&d->d_lock);
14968 +                               continue;
14969 +                       }
14970 +                       err = 0;
14971 +                       au_digen_dec(d);
14972 +                       spin_unlock(&d->d_lock);
14973 +                       break;
14974 +               }
14975 +               spin_unlock(&inode->i_lock);
14976 +       } else {
14977 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
14978 +               d = d_find_any_alias(inode);
14979 +               if (!d) {
14980 +                       au_iigen_dec(inode);
14981 +                       goto out;
14982 +               }
14983 +
14984 +               spin_lock(&d->d_lock);
14985 +               dname = &d->d_name;
14986 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
14987 +                       spin_unlock(&d->d_lock);
14988 +                       err = hn_gen_tree(d);
14989 +                       spin_lock(&d->d_lock);
14990 +               }
14991 +               spin_unlock(&d->d_lock);
14992 +               dput(d);
14993 +       }
14994 +
14995 +out:
14996 +       AuTraceErr(err);
14997 +       return err;
14998 +}
14999 +
15000 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
15001 +{
15002 +       int err;
15003 +       struct inode *inode;
15004 +
15005 +       inode = dentry->d_inode;
15006 +       if (IS_ROOT(dentry)
15007 +           /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
15008 +               ) {
15009 +               pr_warn("branch root dir was changed\n");
15010 +               return 0;
15011 +       }
15012 +
15013 +       err = 0;
15014 +       if (!isdir) {
15015 +               au_digen_dec(dentry);
15016 +               if (inode)
15017 +                       au_iigen_dec(inode);
15018 +       } else {
15019 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
15020 +               if (inode)
15021 +                       err = hn_gen_tree(dentry);
15022 +       }
15023 +
15024 +       AuTraceErr(err);
15025 +       return err;
15026 +}
15027 +
15028 +/* ---------------------------------------------------------------------- */
15029 +
15030 +/* hnotify job flags */
15031 +#define AuHnJob_XINO0          1
15032 +#define AuHnJob_GEN            (1 << 1)
15033 +#define AuHnJob_DIRENT         (1 << 2)
15034 +#define AuHnJob_ISDIR          (1 << 3)
15035 +#define AuHnJob_TRYXINO0       (1 << 4)
15036 +#define AuHnJob_MNTPNT         (1 << 5)
15037 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
15038 +#define au_fset_hnjob(flags, name) \
15039 +       do { (flags) |= AuHnJob_##name; } while (0)
15040 +#define au_fclr_hnjob(flags, name) \
15041 +       do { (flags) &= ~AuHnJob_##name; } while (0)
15042 +
15043 +enum {
15044 +       AuHn_CHILD,
15045 +       AuHn_PARENT,
15046 +       AuHnLast
15047 +};
15048 +
15049 +struct au_hnotify_args {
15050 +       struct inode *h_dir, *dir, *h_child_inode;
15051 +       u32 mask;
15052 +       unsigned int flags[AuHnLast];
15053 +       unsigned int h_child_nlen;
15054 +       char h_child_name[];
15055 +};
15056 +
15057 +struct hn_job_args {
15058 +       unsigned int flags;
15059 +       struct inode *inode, *h_inode, *dir, *h_dir;
15060 +       struct dentry *dentry;
15061 +       char *h_name;
15062 +       int h_nlen;
15063 +};
15064 +
15065 +static int hn_job(struct hn_job_args *a)
15066 +{
15067 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
15068 +       int e;
15069 +
15070 +       /* reset xino */
15071 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
15072 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
15073 +
15074 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
15075 +           && a->inode
15076 +           && a->h_inode) {
15077 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
15078 +               if (!a->h_inode->i_nlink
15079 +                   && !(a->h_inode->i_state & I_LINKABLE))
15080 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
15081 +               mutex_unlock(&a->h_inode->i_mutex);
15082 +       }
15083 +
15084 +       /* make the generation obsolete */
15085 +       if (au_ftest_hnjob(a->flags, GEN)) {
15086 +               e = -1;
15087 +               if (a->inode)
15088 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
15089 +                                             isdir);
15090 +               if (e && a->dentry)
15091 +                       hn_gen_by_name(a->dentry, isdir);
15092 +               /* ignore this error */
15093 +       }
15094 +
15095 +       /* make dir entries obsolete */
15096 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
15097 +               struct au_vdir *vdir;
15098 +
15099 +               vdir = au_ivdir(a->inode);
15100 +               if (vdir)
15101 +                       vdir->vd_jiffy = 0;
15102 +               /* IMustLock(a->inode); */
15103 +               /* a->inode->i_version++; */
15104 +       }
15105 +
15106 +       /* can do nothing but warn */
15107 +       if (au_ftest_hnjob(a->flags, MNTPNT)
15108 +           && a->dentry
15109 +           && d_mountpoint(a->dentry))
15110 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
15111 +
15112 +       return 0;
15113 +}
15114 +
15115 +/* ---------------------------------------------------------------------- */
15116 +
15117 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
15118 +                                          struct inode *dir)
15119 +{
15120 +       struct dentry *dentry, *d, *parent;
15121 +       struct qstr *dname;
15122 +
15123 +       parent = d_find_any_alias(dir);
15124 +       if (!parent)
15125 +               return NULL;
15126 +
15127 +       dentry = NULL;
15128 +       spin_lock(&parent->d_lock);
15129 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
15130 +               /* AuDbg("%pd\n", d); */
15131 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
15132 +               dname = &d->d_name;
15133 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
15134 +                       goto cont_unlock;
15135 +               if (au_di(d))
15136 +                       au_digen_dec(d);
15137 +               else
15138 +                       goto cont_unlock;
15139 +               if (au_dcount(d) > 0) {
15140 +                       dentry = dget_dlock(d);
15141 +                       spin_unlock(&d->d_lock);
15142 +                       break;
15143 +               }
15144 +
15145 +cont_unlock:
15146 +               spin_unlock(&d->d_lock);
15147 +       }
15148 +       spin_unlock(&parent->d_lock);
15149 +       dput(parent);
15150 +
15151 +       if (dentry)
15152 +               di_write_lock_child(dentry);
15153 +
15154 +       return dentry;
15155 +}
15156 +
15157 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
15158 +                                        aufs_bindex_t bindex, ino_t h_ino)
15159 +{
15160 +       struct inode *inode;
15161 +       ino_t ino;
15162 +       int err;
15163 +
15164 +       inode = NULL;
15165 +       err = au_xino_read(sb, bindex, h_ino, &ino);
15166 +       if (!err && ino)
15167 +               inode = ilookup(sb, ino);
15168 +       if (!inode)
15169 +               goto out;
15170 +
15171 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15172 +               pr_warn("wrong root branch\n");
15173 +               iput(inode);
15174 +               inode = NULL;
15175 +               goto out;
15176 +       }
15177 +
15178 +       ii_write_lock_child(inode);
15179 +
15180 +out:
15181 +       return inode;
15182 +}
15183 +
15184 +static void au_hn_bh(void *_args)
15185 +{
15186 +       struct au_hnotify_args *a = _args;
15187 +       struct super_block *sb;
15188 +       aufs_bindex_t bindex, bend, bfound;
15189 +       unsigned char xino, try_iput;
15190 +       int err;
15191 +       struct inode *inode;
15192 +       ino_t h_ino;
15193 +       struct hn_job_args args;
15194 +       struct dentry *dentry;
15195 +       struct au_sbinfo *sbinfo;
15196 +
15197 +       AuDebugOn(!_args);
15198 +       AuDebugOn(!a->h_dir);
15199 +       AuDebugOn(!a->dir);
15200 +       AuDebugOn(!a->mask);
15201 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
15202 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
15203 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
15204 +
15205 +       inode = NULL;
15206 +       dentry = NULL;
15207 +       /*
15208 +        * do not lock a->dir->i_mutex here
15209 +        * because of d_revalidate() may cause a deadlock.
15210 +        */
15211 +       sb = a->dir->i_sb;
15212 +       AuDebugOn(!sb);
15213 +       sbinfo = au_sbi(sb);
15214 +       AuDebugOn(!sbinfo);
15215 +       si_write_lock(sb, AuLock_NOPLMW);
15216 +
15217 +       ii_read_lock_parent(a->dir);
15218 +       bfound = -1;
15219 +       bend = au_ibend(a->dir);
15220 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
15221 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
15222 +                       bfound = bindex;
15223 +                       break;
15224 +               }
15225 +       ii_read_unlock(a->dir);
15226 +       if (unlikely(bfound < 0))
15227 +               goto out;
15228 +
15229 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
15230 +       h_ino = 0;
15231 +       if (a->h_child_inode)
15232 +               h_ino = a->h_child_inode->i_ino;
15233 +
15234 +       if (a->h_child_nlen
15235 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
15236 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
15237 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
15238 +                                             a->dir);
15239 +       try_iput = 0;
15240 +       if (dentry)
15241 +               inode = dentry->d_inode;
15242 +       if (xino && !inode && h_ino
15243 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
15244 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
15245 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
15246 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
15247 +               try_iput = 1;
15248 +           }
15249 +
15250 +       args.flags = a->flags[AuHn_CHILD];
15251 +       args.dentry = dentry;
15252 +       args.inode = inode;
15253 +       args.h_inode = a->h_child_inode;
15254 +       args.dir = a->dir;
15255 +       args.h_dir = a->h_dir;
15256 +       args.h_name = a->h_child_name;
15257 +       args.h_nlen = a->h_child_nlen;
15258 +       err = hn_job(&args);
15259 +       if (dentry) {
15260 +               if (au_di(dentry))
15261 +                       di_write_unlock(dentry);
15262 +               dput(dentry);
15263 +       }
15264 +       if (inode && try_iput) {
15265 +               ii_write_unlock(inode);
15266 +               iput(inode);
15267 +       }
15268 +
15269 +       ii_write_lock_parent(a->dir);
15270 +       args.flags = a->flags[AuHn_PARENT];
15271 +       args.dentry = NULL;
15272 +       args.inode = a->dir;
15273 +       args.h_inode = a->h_dir;
15274 +       args.dir = NULL;
15275 +       args.h_dir = NULL;
15276 +       args.h_name = NULL;
15277 +       args.h_nlen = 0;
15278 +       err = hn_job(&args);
15279 +       ii_write_unlock(a->dir);
15280 +
15281 +out:
15282 +       iput(a->h_child_inode);
15283 +       iput(a->h_dir);
15284 +       iput(a->dir);
15285 +       si_write_unlock(sb);
15286 +       au_nwt_done(&sbinfo->si_nowait);
15287 +       kfree(a);
15288 +}
15289 +
15290 +/* ---------------------------------------------------------------------- */
15291 +
15292 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15293 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
15294 +{
15295 +       int err, len;
15296 +       unsigned int flags[AuHnLast], f;
15297 +       unsigned char isdir, isroot, wh;
15298 +       struct inode *dir;
15299 +       struct au_hnotify_args *args;
15300 +       char *p, *h_child_name;
15301 +
15302 +       err = 0;
15303 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
15304 +       dir = igrab(hnotify->hn_aufs_inode);
15305 +       if (!dir)
15306 +               goto out;
15307 +
15308 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
15309 +       wh = 0;
15310 +       h_child_name = (void *)h_child_qstr->name;
15311 +       len = h_child_qstr->len;
15312 +       if (h_child_name) {
15313 +               if (len > AUFS_WH_PFX_LEN
15314 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
15315 +                       h_child_name += AUFS_WH_PFX_LEN;
15316 +                       len -= AUFS_WH_PFX_LEN;
15317 +                       wh = 1;
15318 +               }
15319 +       }
15320 +
15321 +       isdir = 0;
15322 +       if (h_child_inode)
15323 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
15324 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
15325 +       flags[AuHn_CHILD] = 0;
15326 +       if (isdir)
15327 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
15328 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
15329 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
15330 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
15331 +       case FS_MOVED_FROM:
15332 +       case FS_MOVED_TO:
15333 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
15334 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15335 +               /*FALLTHROUGH*/
15336 +       case FS_CREATE:
15337 +               AuDebugOn(!h_child_name);
15338 +               break;
15339 +
15340 +       case FS_DELETE:
15341 +               /*
15342 +                * aufs never be able to get this child inode.
15343 +                * revalidation should be in d_revalidate()
15344 +                * by checking i_nlink, i_generation or d_unhashed().
15345 +                */
15346 +               AuDebugOn(!h_child_name);
15347 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
15348 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15349 +               break;
15350 +
15351 +       default:
15352 +               AuDebugOn(1);
15353 +       }
15354 +
15355 +       if (wh)
15356 +               h_child_inode = NULL;
15357 +
15358 +       err = -ENOMEM;
15359 +       /* iput() and kfree() will be called in au_hnotify() */
15360 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
15361 +       if (unlikely(!args)) {
15362 +               AuErr1("no memory\n");
15363 +               iput(dir);
15364 +               goto out;
15365 +       }
15366 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
15367 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
15368 +       args->mask = mask;
15369 +       args->dir = dir;
15370 +       args->h_dir = igrab(h_dir);
15371 +       if (h_child_inode)
15372 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
15373 +       args->h_child_inode = h_child_inode;
15374 +       args->h_child_nlen = len;
15375 +       if (len) {
15376 +               p = (void *)args;
15377 +               p += sizeof(*args);
15378 +               memcpy(p, h_child_name, len);
15379 +               p[len] = 0;
15380 +       }
15381 +
15382 +       /* NFS fires the event for silly-renamed one from kworker */
15383 +       f = 0;
15384 +       if (!dir->i_nlink
15385 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
15386 +               f = AuWkq_NEST;
15387 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
15388 +       if (unlikely(err)) {
15389 +               pr_err("wkq %d\n", err);
15390 +               iput(args->h_child_inode);
15391 +               iput(args->h_dir);
15392 +               iput(args->dir);
15393 +               kfree(args);
15394 +       }
15395 +
15396 +out:
15397 +       return err;
15398 +}
15399 +
15400 +/* ---------------------------------------------------------------------- */
15401 +
15402 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
15403 +{
15404 +       int err;
15405 +
15406 +       AuDebugOn(!(udba & AuOptMask_UDBA));
15407 +
15408 +       err = 0;
15409 +       if (au_hnotify_op.reset_br)
15410 +               err = au_hnotify_op.reset_br(udba, br, perm);
15411 +
15412 +       return err;
15413 +}
15414 +
15415 +int au_hnotify_init_br(struct au_branch *br, int perm)
15416 +{
15417 +       int err;
15418 +
15419 +       err = 0;
15420 +       if (au_hnotify_op.init_br)
15421 +               err = au_hnotify_op.init_br(br, perm);
15422 +
15423 +       return err;
15424 +}
15425 +
15426 +void au_hnotify_fin_br(struct au_branch *br)
15427 +{
15428 +       if (au_hnotify_op.fin_br)
15429 +               au_hnotify_op.fin_br(br);
15430 +}
15431 +
15432 +static void au_hn_destroy_cache(void)
15433 +{
15434 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
15435 +       au_cachep[AuCache_HNOTIFY] = NULL;
15436 +}
15437 +
15438 +int __init au_hnotify_init(void)
15439 +{
15440 +       int err;
15441 +
15442 +       err = -ENOMEM;
15443 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
15444 +       if (au_cachep[AuCache_HNOTIFY]) {
15445 +               err = 0;
15446 +               if (au_hnotify_op.init)
15447 +                       err = au_hnotify_op.init();
15448 +               if (unlikely(err))
15449 +                       au_hn_destroy_cache();
15450 +       }
15451 +       AuTraceErr(err);
15452 +       return err;
15453 +}
15454 +
15455 +void au_hnotify_fin(void)
15456 +{
15457 +       if (au_hnotify_op.fin)
15458 +               au_hnotify_op.fin();
15459 +       /* cf. au_cache_fin() */
15460 +       if (au_cachep[AuCache_HNOTIFY])
15461 +               au_hn_destroy_cache();
15462 +}
15463 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
15464 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
15465 +++ linux/fs/aufs/iinfo.c       2015-06-22 08:27:37.884168534 +0200
15466 @@ -0,0 +1,277 @@
15467 +/*
15468 + * Copyright (C) 2005-2015 Junjiro R. Okajima
15469 + *
15470 + * This program, aufs is free software; you can redistribute it and/or modify
15471 + * it under the terms of the GNU General Public License as published by
15472 + * the Free Software Foundation; either version 2 of the License, or
15473 + * (at your option) any later version.
15474 + *
15475 + * This program is distributed in the hope that it will be useful,
15476 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15477 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15478 + * GNU General Public License for more details.
15479 + *
15480 + * You should have received a copy of the GNU General Public License
15481 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15482 + */
15483 +
15484 +/*
15485 + * inode private data
15486 + */
15487 +
15488 +#include "aufs.h"
15489 +
15490 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
15491 +{
15492 +       struct inode *h_inode;
15493 +
15494 +       IiMustAnyLock(inode);
15495 +
15496 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
15497 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15498 +       return h_inode;
15499 +}
15500 +
15501 +/* todo: hard/soft set? */
15502 +void au_hiput(struct au_hinode *hinode)
15503 +{
15504 +       au_hn_free(hinode);
15505 +       dput(hinode->hi_whdentry);
15506 +       iput(hinode->hi_inode);
15507 +}
15508 +
15509 +unsigned int au_hi_flags(struct inode *inode, int isdir)
15510 +{
15511 +       unsigned int flags;
15512 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
15513 +
15514 +       flags = 0;
15515 +       if (au_opt_test(mnt_flags, XINO))
15516 +               au_fset_hi(flags, XINO);
15517 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
15518 +               au_fset_hi(flags, HNOTIFY);
15519 +       return flags;
15520 +}
15521 +
15522 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15523 +                  struct inode *h_inode, unsigned int flags)
15524 +{
15525 +       struct au_hinode *hinode;
15526 +       struct inode *hi;
15527 +       struct au_iinfo *iinfo = au_ii(inode);
15528 +
15529 +       IiMustWriteLock(inode);
15530 +
15531 +       hinode = iinfo->ii_hinode + bindex;
15532 +       hi = hinode->hi_inode;
15533 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15534 +
15535 +       if (hi)
15536 +               au_hiput(hinode);
15537 +       hinode->hi_inode = h_inode;
15538 +       if (h_inode) {
15539 +               int err;
15540 +               struct super_block *sb = inode->i_sb;
15541 +               struct au_branch *br;
15542 +
15543 +               AuDebugOn(inode->i_mode
15544 +                         && (h_inode->i_mode & S_IFMT)
15545 +                         != (inode->i_mode & S_IFMT));
15546 +               if (bindex == iinfo->ii_bstart)
15547 +                       au_cpup_igen(inode, h_inode);
15548 +               br = au_sbr(sb, bindex);
15549 +               hinode->hi_id = br->br_id;
15550 +               if (au_ftest_hi(flags, XINO)) {
15551 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
15552 +                                           inode->i_ino);
15553 +                       if (unlikely(err))
15554 +                               AuIOErr1("failed au_xino_write() %d\n", err);
15555 +               }
15556 +
15557 +               if (au_ftest_hi(flags, HNOTIFY)
15558 +                   && au_br_hnotifyable(br->br_perm)) {
15559 +                       err = au_hn_alloc(hinode, inode);
15560 +                       if (unlikely(err))
15561 +                               AuIOErr1("au_hn_alloc() %d\n", err);
15562 +               }
15563 +       }
15564 +}
15565 +
15566 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15567 +                 struct dentry *h_wh)
15568 +{
15569 +       struct au_hinode *hinode;
15570 +
15571 +       IiMustWriteLock(inode);
15572 +
15573 +       hinode = au_ii(inode)->ii_hinode + bindex;
15574 +       AuDebugOn(hinode->hi_whdentry);
15575 +       hinode->hi_whdentry = h_wh;
15576 +}
15577 +
15578 +void au_update_iigen(struct inode *inode, int half)
15579 +{
15580 +       struct au_iinfo *iinfo;
15581 +       struct au_iigen *iigen;
15582 +       unsigned int sigen;
15583 +
15584 +       sigen = au_sigen(inode->i_sb);
15585 +       iinfo = au_ii(inode);
15586 +       iigen = &iinfo->ii_generation;
15587 +       spin_lock(&iinfo->ii_genspin);
15588 +       iigen->ig_generation = sigen;
15589 +       if (half)
15590 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
15591 +       else
15592 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
15593 +       spin_unlock(&iinfo->ii_genspin);
15594 +}
15595 +
15596 +/* it may be called at remount time, too */
15597 +void au_update_ibrange(struct inode *inode, int do_put_zero)
15598 +{
15599 +       struct au_iinfo *iinfo;
15600 +       aufs_bindex_t bindex, bend;
15601 +
15602 +       iinfo = au_ii(inode);
15603 +       if (!iinfo)
15604 +               return;
15605 +
15606 +       IiMustWriteLock(inode);
15607 +
15608 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
15609 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15610 +                    bindex++) {
15611 +                       struct inode *h_i;
15612 +
15613 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
15614 +                       if (h_i
15615 +                           && !h_i->i_nlink
15616 +                           && !(h_i->i_state & I_LINKABLE))
15617 +                               au_set_h_iptr(inode, bindex, NULL, 0);
15618 +               }
15619 +       }
15620 +
15621 +       iinfo->ii_bstart = -1;
15622 +       iinfo->ii_bend = -1;
15623 +       bend = au_sbend(inode->i_sb);
15624 +       for (bindex = 0; bindex <= bend; bindex++)
15625 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15626 +                       iinfo->ii_bstart = bindex;
15627 +                       break;
15628 +               }
15629 +       if (iinfo->ii_bstart >= 0)
15630 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
15631 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15632 +                               iinfo->ii_bend = bindex;
15633 +                               break;
15634 +                       }
15635 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
15636 +}
15637 +
15638 +/* ---------------------------------------------------------------------- */
15639 +
15640 +void au_icntnr_init_once(void *_c)
15641 +{
15642 +       struct au_icntnr *c = _c;
15643 +       struct au_iinfo *iinfo = &c->iinfo;
15644 +       static struct lock_class_key aufs_ii;
15645 +
15646 +       spin_lock_init(&iinfo->ii_genspin);
15647 +       au_rw_init(&iinfo->ii_rwsem);
15648 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
15649 +       inode_init_once(&c->vfs_inode);
15650 +}
15651 +
15652 +int au_iinfo_init(struct inode *inode)
15653 +{
15654 +       struct au_iinfo *iinfo;
15655 +       struct super_block *sb;
15656 +       int nbr, i;
15657 +
15658 +       sb = inode->i_sb;
15659 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15660 +       nbr = au_sbend(sb) + 1;
15661 +       if (unlikely(nbr <= 0))
15662 +               nbr = 1;
15663 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
15664 +       if (iinfo->ii_hinode) {
15665 +               au_ninodes_inc(sb);
15666 +               for (i = 0; i < nbr; i++)
15667 +                       iinfo->ii_hinode[i].hi_id = -1;
15668 +
15669 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
15670 +               iinfo->ii_bstart = -1;
15671 +               iinfo->ii_bend = -1;
15672 +               iinfo->ii_vdir = NULL;
15673 +               return 0;
15674 +       }
15675 +       return -ENOMEM;
15676 +}
15677 +
15678 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
15679 +{
15680 +       int err, sz;
15681 +       struct au_hinode *hip;
15682 +
15683 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
15684 +
15685 +       err = -ENOMEM;
15686 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
15687 +       if (!sz)
15688 +               sz = sizeof(*hip);
15689 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
15690 +       if (hip) {
15691 +               iinfo->ii_hinode = hip;
15692 +               err = 0;
15693 +       }
15694 +
15695 +       return err;
15696 +}
15697 +
15698 +void au_iinfo_fin(struct inode *inode)
15699 +{
15700 +       struct au_iinfo *iinfo;
15701 +       struct au_hinode *hi;
15702 +       struct super_block *sb;
15703 +       aufs_bindex_t bindex, bend;
15704 +       const unsigned char unlinked = !inode->i_nlink;
15705 +
15706 +       iinfo = au_ii(inode);
15707 +       /* bad_inode case */
15708 +       if (!iinfo)
15709 +               return;
15710 +
15711 +       sb = inode->i_sb;
15712 +       au_ninodes_dec(sb);
15713 +       if (si_pid_test(sb))
15714 +               au_xino_delete_inode(inode, unlinked);
15715 +       else {
15716 +               /*
15717 +                * it is safe to hide the dependency between sbinfo and
15718 +                * sb->s_umount.
15719 +                */
15720 +               lockdep_off();
15721 +               si_noflush_read_lock(sb);
15722 +               au_xino_delete_inode(inode, unlinked);
15723 +               si_read_unlock(sb);
15724 +               lockdep_on();
15725 +       }
15726 +
15727 +       if (iinfo->ii_vdir)
15728 +               au_vdir_free(iinfo->ii_vdir);
15729 +
15730 +       bindex = iinfo->ii_bstart;
15731 +       if (bindex >= 0) {
15732 +               hi = iinfo->ii_hinode + bindex;
15733 +               bend = iinfo->ii_bend;
15734 +               while (bindex++ <= bend) {
15735 +                       if (hi->hi_inode)
15736 +                               au_hiput(hi);
15737 +                       hi++;
15738 +               }
15739 +       }
15740 +       kfree(iinfo->ii_hinode);
15741 +       iinfo->ii_hinode = NULL;
15742 +       AuRwDestroy(&iinfo->ii_rwsem);
15743 +}
15744 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
15745 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
15746 +++ linux/fs/aufs/inode.c       2015-06-22 08:27:37.884168534 +0200
15747 @@ -0,0 +1,495 @@
15748 +/*
15749 + * Copyright (C) 2005-2015 Junjiro R. Okajima
15750 + *
15751 + * This program, aufs is free software; you can redistribute it and/or modify
15752 + * it under the terms of the GNU General Public License as published by
15753 + * the Free Software Foundation; either version 2 of the License, or
15754 + * (at your option) any later version.
15755 + *
15756 + * This program is distributed in the hope that it will be useful,
15757 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15758 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15759 + * GNU General Public License for more details.
15760 + *
15761 + * You should have received a copy of the GNU General Public License
15762 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15763 + */
15764 +
15765 +/*
15766 + * inode functions
15767 + */
15768 +
15769 +#include "aufs.h"
15770 +
15771 +struct inode *au_igrab(struct inode *inode)
15772 +{
15773 +       if (inode) {
15774 +               AuDebugOn(!atomic_read(&inode->i_count));
15775 +               ihold(inode);
15776 +       }
15777 +       return inode;
15778 +}
15779 +
15780 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
15781 +{
15782 +       au_cpup_attr_all(inode, /*force*/0);
15783 +       au_update_iigen(inode, /*half*/1);
15784 +       if (do_version)
15785 +               inode->i_version++;
15786 +}
15787 +
15788 +static int au_ii_refresh(struct inode *inode, int *update)
15789 +{
15790 +       int err, e;
15791 +       umode_t type;
15792 +       aufs_bindex_t bindex, new_bindex;
15793 +       struct super_block *sb;
15794 +       struct au_iinfo *iinfo;
15795 +       struct au_hinode *p, *q, tmp;
15796 +
15797 +       IiMustWriteLock(inode);
15798 +
15799 +       *update = 0;
15800 +       sb = inode->i_sb;
15801 +       type = inode->i_mode & S_IFMT;
15802 +       iinfo = au_ii(inode);
15803 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
15804 +       if (unlikely(err))
15805 +               goto out;
15806 +
15807 +       AuDebugOn(iinfo->ii_bstart < 0);
15808 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15809 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15810 +            bindex++, p++) {
15811 +               if (!p->hi_inode)
15812 +                       continue;
15813 +
15814 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
15815 +               new_bindex = au_br_index(sb, p->hi_id);
15816 +               if (new_bindex == bindex)
15817 +                       continue;
15818 +
15819 +               if (new_bindex < 0) {
15820 +                       *update = 1;
15821 +                       au_hiput(p);
15822 +                       p->hi_inode = NULL;
15823 +                       continue;
15824 +               }
15825 +
15826 +               if (new_bindex < iinfo->ii_bstart)
15827 +                       iinfo->ii_bstart = new_bindex;
15828 +               if (iinfo->ii_bend < new_bindex)
15829 +                       iinfo->ii_bend = new_bindex;
15830 +               /* swap two lower inode, and loop again */
15831 +               q = iinfo->ii_hinode + new_bindex;
15832 +               tmp = *q;
15833 +               *q = *p;
15834 +               *p = tmp;
15835 +               if (tmp.hi_inode) {
15836 +                       bindex--;
15837 +                       p--;
15838 +               }
15839 +       }
15840 +       au_update_ibrange(inode, /*do_put_zero*/0);
15841 +       e = au_dy_irefresh(inode);
15842 +       if (unlikely(e && !err))
15843 +               err = e;
15844 +
15845 +out:
15846 +       AuTraceErr(err);
15847 +       return err;
15848 +}
15849 +
15850 +int au_refresh_hinode_self(struct inode *inode)
15851 +{
15852 +       int err, update;
15853 +
15854 +       err = au_ii_refresh(inode, &update);
15855 +       if (!err)
15856 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
15857 +
15858 +       AuTraceErr(err);
15859 +       return err;
15860 +}
15861 +
15862 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
15863 +{
15864 +       int err, e, update;
15865 +       unsigned int flags;
15866 +       umode_t mode;
15867 +       aufs_bindex_t bindex, bend;
15868 +       unsigned char isdir;
15869 +       struct au_hinode *p;
15870 +       struct au_iinfo *iinfo;
15871 +
15872 +       err = au_ii_refresh(inode, &update);
15873 +       if (unlikely(err))
15874 +               goto out;
15875 +
15876 +       update = 0;
15877 +       iinfo = au_ii(inode);
15878 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15879 +       mode = (inode->i_mode & S_IFMT);
15880 +       isdir = S_ISDIR(mode);
15881 +       flags = au_hi_flags(inode, isdir);
15882 +       bend = au_dbend(dentry);
15883 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
15884 +               struct inode *h_i;
15885 +               struct dentry *h_d;
15886 +
15887 +               h_d = au_h_dptr(dentry, bindex);
15888 +               if (!h_d || !h_d->d_inode)
15889 +                       continue;
15890 +
15891 +               AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
15892 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
15893 +                       h_i = au_h_iptr(inode, bindex);
15894 +                       if (h_i) {
15895 +                               if (h_i == h_d->d_inode)
15896 +                                       continue;
15897 +                               err = -EIO;
15898 +                               break;
15899 +                       }
15900 +               }
15901 +               if (bindex < iinfo->ii_bstart)
15902 +                       iinfo->ii_bstart = bindex;
15903 +               if (iinfo->ii_bend < bindex)
15904 +                       iinfo->ii_bend = bindex;
15905 +               au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
15906 +               update = 1;
15907 +       }
15908 +       au_update_ibrange(inode, /*do_put_zero*/0);
15909 +       e = au_dy_irefresh(inode);
15910 +       if (unlikely(e && !err))
15911 +               err = e;
15912 +       if (!err)
15913 +               au_refresh_hinode_attr(inode, update && isdir);
15914 +
15915 +out:
15916 +       AuTraceErr(err);
15917 +       return err;
15918 +}
15919 +
15920 +static int set_inode(struct inode *inode, struct dentry *dentry)
15921 +{
15922 +       int err;
15923 +       unsigned int flags;
15924 +       umode_t mode;
15925 +       aufs_bindex_t bindex, bstart, btail;
15926 +       unsigned char isdir;
15927 +       struct dentry *h_dentry;
15928 +       struct inode *h_inode;
15929 +       struct au_iinfo *iinfo;
15930 +
15931 +       IiMustWriteLock(inode);
15932 +
15933 +       err = 0;
15934 +       isdir = 0;
15935 +       bstart = au_dbstart(dentry);
15936 +       h_inode = au_h_dptr(dentry, bstart)->d_inode;
15937 +       mode = h_inode->i_mode;
15938 +       switch (mode & S_IFMT) {
15939 +       case S_IFREG:
15940 +               btail = au_dbtail(dentry);
15941 +               inode->i_op = &aufs_iop;
15942 +               inode->i_fop = &aufs_file_fop;
15943 +               err = au_dy_iaop(inode, bstart, h_inode);
15944 +               if (unlikely(err))
15945 +                       goto out;
15946 +               break;
15947 +       case S_IFDIR:
15948 +               isdir = 1;
15949 +               btail = au_dbtaildir(dentry);
15950 +               inode->i_op = &aufs_dir_iop;
15951 +               inode->i_fop = &aufs_dir_fop;
15952 +               break;
15953 +       case S_IFLNK:
15954 +               btail = au_dbtail(dentry);
15955 +               inode->i_op = &aufs_symlink_iop;
15956 +               break;
15957 +       case S_IFBLK:
15958 +       case S_IFCHR:
15959 +       case S_IFIFO:
15960 +       case S_IFSOCK:
15961 +               btail = au_dbtail(dentry);
15962 +               inode->i_op = &aufs_iop;
15963 +               init_special_inode(inode, mode, h_inode->i_rdev);
15964 +               break;
15965 +       default:
15966 +               AuIOErr("Unknown file type 0%o\n", mode);
15967 +               err = -EIO;
15968 +               goto out;
15969 +       }
15970 +
15971 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
15972 +       flags = au_hi_flags(inode, isdir);
15973 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
15974 +           && au_ftest_hi(flags, HNOTIFY)
15975 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
15976 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
15977 +               au_fclr_hi(flags, HNOTIFY);
15978 +       iinfo = au_ii(inode);
15979 +       iinfo->ii_bstart = bstart;
15980 +       iinfo->ii_bend = btail;
15981 +       for (bindex = bstart; bindex <= btail; bindex++) {
15982 +               h_dentry = au_h_dptr(dentry, bindex);
15983 +               if (h_dentry)
15984 +                       au_set_h_iptr(inode, bindex,
15985 +                                     au_igrab(h_dentry->d_inode), flags);
15986 +       }
15987 +       au_cpup_attr_all(inode, /*force*/1);
15988 +       /*
15989 +        * to force calling aufs_get_acl() every time,
15990 +        * do not call cache_no_acl() for aufs inode.
15991 +        */
15992 +
15993 +out:
15994 +       return err;
15995 +}
15996 +
15997 +/*
15998 + * successful returns with iinfo write_locked
15999 + * minus: errno
16000 + * zero: success, matched
16001 + * plus: no error, but unmatched
16002 + */
16003 +static int reval_inode(struct inode *inode, struct dentry *dentry)
16004 +{
16005 +       int err;
16006 +       unsigned int gen;
16007 +       struct au_iigen iigen;
16008 +       aufs_bindex_t bindex, bend;
16009 +       struct inode *h_inode, *h_dinode;
16010 +
16011 +       /*
16012 +        * before this function, if aufs got any iinfo lock, it must be only
16013 +        * one, the parent dir.
16014 +        * it can happen by UDBA and the obsoleted inode number.
16015 +        */
16016 +       err = -EIO;
16017 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
16018 +               goto out;
16019 +
16020 +       err = 1;
16021 +       ii_write_lock_new_child(inode);
16022 +       h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
16023 +       bend = au_ibend(inode);
16024 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
16025 +               h_inode = au_h_iptr(inode, bindex);
16026 +               if (!h_inode || h_inode != h_dinode)
16027 +                       continue;
16028 +
16029 +               err = 0;
16030 +               gen = au_iigen(inode, &iigen);
16031 +               if (gen == au_digen(dentry)
16032 +                   && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
16033 +                       break;
16034 +
16035 +               /* fully refresh inode using dentry */
16036 +               err = au_refresh_hinode(inode, dentry);
16037 +               if (!err)
16038 +                       au_update_iigen(inode, /*half*/0);
16039 +               break;
16040 +       }
16041 +
16042 +       if (unlikely(err))
16043 +               ii_write_unlock(inode);
16044 +out:
16045 +       return err;
16046 +}
16047 +
16048 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16049 +          unsigned int d_type, ino_t *ino)
16050 +{
16051 +       int err;
16052 +       struct mutex *mtx;
16053 +
16054 +       /* prevent hardlinked inode number from race condition */
16055 +       mtx = NULL;
16056 +       if (d_type != DT_DIR) {
16057 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
16058 +               mutex_lock(mtx);
16059 +       }
16060 +       err = au_xino_read(sb, bindex, h_ino, ino);
16061 +       if (unlikely(err))
16062 +               goto out;
16063 +
16064 +       if (!*ino) {
16065 +               err = -EIO;
16066 +               *ino = au_xino_new_ino(sb);
16067 +               if (unlikely(!*ino))
16068 +                       goto out;
16069 +               err = au_xino_write(sb, bindex, h_ino, *ino);
16070 +               if (unlikely(err))
16071 +                       goto out;
16072 +       }
16073 +
16074 +out:
16075 +       if (mtx)
16076 +               mutex_unlock(mtx);
16077 +       return err;
16078 +}
16079 +
16080 +/* successful returns with iinfo write_locked */
16081 +/* todo: return with unlocked? */
16082 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
16083 +{
16084 +       struct inode *inode;
16085 +       struct dentry *h_dentry;
16086 +       struct super_block *sb;
16087 +       struct mutex *mtx;
16088 +       ino_t h_ino, ino;
16089 +       int err;
16090 +       aufs_bindex_t bstart;
16091 +
16092 +       sb = dentry->d_sb;
16093 +       bstart = au_dbstart(dentry);
16094 +       h_dentry = au_h_dptr(dentry, bstart);
16095 +       h_ino = h_dentry->d_inode->i_ino;
16096 +
16097 +       /*
16098 +        * stop 'race'-ing between hardlinks under different
16099 +        * parents.
16100 +        */
16101 +       mtx = NULL;
16102 +       if (!d_is_dir(h_dentry))
16103 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
16104 +
16105 +new_ino:
16106 +       if (mtx)
16107 +               mutex_lock(mtx);
16108 +       err = au_xino_read(sb, bstart, h_ino, &ino);
16109 +       inode = ERR_PTR(err);
16110 +       if (unlikely(err))
16111 +               goto out;
16112 +
16113 +       if (!ino) {
16114 +               ino = au_xino_new_ino(sb);
16115 +               if (unlikely(!ino)) {
16116 +                       inode = ERR_PTR(-EIO);
16117 +                       goto out;
16118 +               }
16119 +       }
16120 +
16121 +       AuDbg("i%lu\n", (unsigned long)ino);
16122 +       inode = au_iget_locked(sb, ino);
16123 +       err = PTR_ERR(inode);
16124 +       if (IS_ERR(inode))
16125 +               goto out;
16126 +
16127 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
16128 +       if (inode->i_state & I_NEW) {
16129 +               /* verbose coding for lock class name */
16130 +               if (unlikely(d_is_symlink(h_dentry)))
16131 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16132 +                                   au_lc_key + AuLcSymlink_IIINFO);
16133 +               else if (unlikely(d_is_dir(h_dentry)))
16134 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16135 +                                   au_lc_key + AuLcDir_IIINFO);
16136 +               else /* likely */
16137 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16138 +                                   au_lc_key + AuLcNonDir_IIINFO);
16139 +
16140 +               ii_write_lock_new_child(inode);
16141 +               err = set_inode(inode, dentry);
16142 +               if (!err) {
16143 +                       unlock_new_inode(inode);
16144 +                       goto out; /* success */
16145 +               }
16146 +
16147 +               /*
16148 +                * iget_failed() calls iput(), but we need to call
16149 +                * ii_write_unlock() after iget_failed(). so dirty hack for
16150 +                * i_count.
16151 +                */
16152 +               atomic_inc(&inode->i_count);
16153 +               iget_failed(inode);
16154 +               ii_write_unlock(inode);
16155 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
16156 +               /* ignore this error */
16157 +               goto out_iput;
16158 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
16159 +               /*
16160 +                * horrible race condition between lookup, readdir and copyup
16161 +                * (or something).
16162 +                */
16163 +               if (mtx)
16164 +                       mutex_unlock(mtx);
16165 +               err = reval_inode(inode, dentry);
16166 +               if (unlikely(err < 0)) {
16167 +                       mtx = NULL;
16168 +                       goto out_iput;
16169 +               }
16170 +
16171 +               if (!err) {
16172 +                       mtx = NULL;
16173 +                       goto out; /* success */
16174 +               } else if (mtx)
16175 +                       mutex_lock(mtx);
16176 +       }
16177 +
16178 +       if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
16179 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
16180 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
16181 +                       bstart, au_sbtype(h_dentry->d_sb), dentry,
16182 +                       (unsigned long)h_ino, (unsigned long)ino);
16183 +       ino = 0;
16184 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
16185 +       if (!err) {
16186 +               iput(inode);
16187 +               if (mtx)
16188 +                       mutex_unlock(mtx);
16189 +               goto new_ino;
16190 +       }
16191 +
16192 +out_iput:
16193 +       iput(inode);
16194 +       inode = ERR_PTR(err);
16195 +out:
16196 +       if (mtx)
16197 +               mutex_unlock(mtx);
16198 +       return inode;
16199 +}
16200 +
16201 +/* ---------------------------------------------------------------------- */
16202 +
16203 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16204 +              struct inode *inode)
16205 +{
16206 +       int err;
16207 +       struct inode *hi;
16208 +
16209 +       err = au_br_rdonly(au_sbr(sb, bindex));
16210 +
16211 +       /* pseudo-link after flushed may happen out of bounds */
16212 +       if (!err
16213 +           && inode
16214 +           && au_ibstart(inode) <= bindex
16215 +           && bindex <= au_ibend(inode)) {
16216 +               /*
16217 +                * permission check is unnecessary since vfsub routine
16218 +                * will be called later
16219 +                */
16220 +               hi = au_h_iptr(inode, bindex);
16221 +               if (hi)
16222 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
16223 +       }
16224 +
16225 +       return err;
16226 +}
16227 +
16228 +int au_test_h_perm(struct inode *h_inode, int mask)
16229 +{
16230 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
16231 +               return 0;
16232 +       return inode_permission(h_inode, mask);
16233 +}
16234 +
16235 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
16236 +{
16237 +       if (au_test_nfs(h_inode->i_sb)
16238 +           && (mask & MAY_WRITE)
16239 +           && S_ISDIR(h_inode->i_mode))
16240 +               mask |= MAY_READ; /* force permission check */
16241 +       return au_test_h_perm(h_inode, mask);
16242 +}
16243 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
16244 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
16245 +++ linux/fs/aufs/inode.h       2015-06-22 08:27:37.884168534 +0200
16246 @@ -0,0 +1,673 @@
16247 +/*
16248 + * Copyright (C) 2005-2015 Junjiro R. Okajima
16249 + *
16250 + * This program, aufs is free software; you can redistribute it and/or modify
16251 + * it under the terms of the GNU General Public License as published by
16252 + * the Free Software Foundation; either version 2 of the License, or
16253 + * (at your option) any later version.
16254 + *
16255 + * This program is distributed in the hope that it will be useful,
16256 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16257 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16258 + * GNU General Public License for more details.
16259 + *
16260 + * You should have received a copy of the GNU General Public License
16261 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16262 + */
16263 +
16264 +/*
16265 + * inode operations
16266 + */
16267 +
16268 +#ifndef __AUFS_INODE_H__
16269 +#define __AUFS_INODE_H__
16270 +
16271 +#ifdef __KERNEL__
16272 +
16273 +#include <linux/fsnotify.h>
16274 +#include "rwsem.h"
16275 +
16276 +struct vfsmount;
16277 +
16278 +struct au_hnotify {
16279 +#ifdef CONFIG_AUFS_HNOTIFY
16280 +#ifdef CONFIG_AUFS_HFSNOTIFY
16281 +       /* never use fsnotify_add_vfsmount_mark() */
16282 +       struct fsnotify_mark            hn_mark;
16283 +#endif
16284 +       struct inode                    *hn_aufs_inode; /* no get/put */
16285 +#endif
16286 +} ____cacheline_aligned_in_smp;
16287 +
16288 +struct au_hinode {
16289 +       struct inode            *hi_inode;
16290 +       aufs_bindex_t           hi_id;
16291 +#ifdef CONFIG_AUFS_HNOTIFY
16292 +       struct au_hnotify       *hi_notify;
16293 +#endif
16294 +
16295 +       /* reference to the copied-up whiteout with get/put */
16296 +       struct dentry           *hi_whdentry;
16297 +};
16298 +
16299 +/* ig_flags */
16300 +#define AuIG_HALF_REFRESHED            1
16301 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
16302 +#define au_ig_fset(flags, name) \
16303 +       do { (flags) |= AuIG_##name; } while (0)
16304 +#define au_ig_fclr(flags, name) \
16305 +       do { (flags) &= ~AuIG_##name; } while (0)
16306 +
16307 +struct au_iigen {
16308 +       __u32           ig_generation, ig_flags;
16309 +};
16310 +
16311 +struct au_vdir;
16312 +struct au_iinfo {
16313 +       spinlock_t              ii_genspin;
16314 +       struct au_iigen         ii_generation;
16315 +       struct super_block      *ii_hsb1;       /* no get/put */
16316 +
16317 +       struct au_rwsem         ii_rwsem;
16318 +       aufs_bindex_t           ii_bstart, ii_bend;
16319 +       __u32                   ii_higen;
16320 +       struct au_hinode        *ii_hinode;
16321 +       struct au_vdir          *ii_vdir;
16322 +};
16323 +
16324 +struct au_icntnr {
16325 +       struct au_iinfo iinfo;
16326 +       struct inode vfs_inode;
16327 +} ____cacheline_aligned_in_smp;
16328 +
16329 +/* au_pin flags */
16330 +#define AuPin_DI_LOCKED                1
16331 +#define AuPin_MNT_WRITE                (1 << 1)
16332 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
16333 +#define au_fset_pin(flags, name) \
16334 +       do { (flags) |= AuPin_##name; } while (0)
16335 +#define au_fclr_pin(flags, name) \
16336 +       do { (flags) &= ~AuPin_##name; } while (0)
16337 +
16338 +struct au_pin {
16339 +       /* input */
16340 +       struct dentry *dentry;
16341 +       unsigned int udba;
16342 +       unsigned char lsc_di, lsc_hi, flags;
16343 +       aufs_bindex_t bindex;
16344 +
16345 +       /* output */
16346 +       struct dentry *parent;
16347 +       struct au_hinode *hdir;
16348 +       struct vfsmount *h_mnt;
16349 +
16350 +       /* temporary unlock/relock for copyup */
16351 +       struct dentry *h_dentry, *h_parent;
16352 +       struct au_branch *br;
16353 +       struct task_struct *task;
16354 +};
16355 +
16356 +void au_pin_hdir_unlock(struct au_pin *p);
16357 +int au_pin_hdir_lock(struct au_pin *p);
16358 +int au_pin_hdir_relock(struct au_pin *p);
16359 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
16360 +void au_pin_hdir_acquire_nest(struct au_pin *p);
16361 +void au_pin_hdir_release(struct au_pin *p);
16362 +
16363 +/* ---------------------------------------------------------------------- */
16364 +
16365 +static inline struct au_iinfo *au_ii(struct inode *inode)
16366 +{
16367 +       struct au_iinfo *iinfo;
16368 +
16369 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
16370 +       if (iinfo->ii_hinode)
16371 +               return iinfo;
16372 +       return NULL; /* debugging bad_inode case */
16373 +}
16374 +
16375 +/* ---------------------------------------------------------------------- */
16376 +
16377 +/* inode.c */
16378 +struct inode *au_igrab(struct inode *inode);
16379 +int au_refresh_hinode_self(struct inode *inode);
16380 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
16381 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16382 +          unsigned int d_type, ino_t *ino);
16383 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
16384 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16385 +              struct inode *inode);
16386 +int au_test_h_perm(struct inode *h_inode, int mask);
16387 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
16388 +
16389 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
16390 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
16391 +{
16392 +#ifdef CONFIG_AUFS_SHWH
16393 +       return au_ino(sb, bindex, h_ino, d_type, ino);
16394 +#else
16395 +       return 0;
16396 +#endif
16397 +}
16398 +
16399 +/* i_op.c */
16400 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
16401 +
16402 +/* au_wr_dir flags */
16403 +#define AuWrDir_ADD_ENTRY      1
16404 +#define AuWrDir_ISDIR          (1 << 1)
16405 +#define AuWrDir_TMPFILE                (1 << 2)
16406 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
16407 +#define au_fset_wrdir(flags, name) \
16408 +       do { (flags) |= AuWrDir_##name; } while (0)
16409 +#define au_fclr_wrdir(flags, name) \
16410 +       do { (flags) &= ~AuWrDir_##name; } while (0)
16411 +
16412 +struct au_wr_dir_args {
16413 +       aufs_bindex_t force_btgt;
16414 +       unsigned char flags;
16415 +};
16416 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16417 +             struct au_wr_dir_args *args);
16418 +
16419 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
16420 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
16421 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16422 +                unsigned int udba, unsigned char flags);
16423 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16424 +          unsigned int udba, unsigned char flags) __must_check;
16425 +int au_do_pin(struct au_pin *pin) __must_check;
16426 +void au_unpin(struct au_pin *pin);
16427 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
16428 +
16429 +#define AuIcpup_DID_CPUP       1
16430 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
16431 +#define au_fset_icpup(flags, name) \
16432 +       do { (flags) |= AuIcpup_##name; } while (0)
16433 +#define au_fclr_icpup(flags, name) \
16434 +       do { (flags) &= ~AuIcpup_##name; } while (0)
16435 +
16436 +struct au_icpup_args {
16437 +       unsigned char flags;
16438 +       unsigned char pin_flags;
16439 +       aufs_bindex_t btgt;
16440 +       unsigned int udba;
16441 +       struct au_pin pin;
16442 +       struct path h_path;
16443 +       struct inode *h_inode;
16444 +};
16445 +
16446 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16447 +                    struct au_icpup_args *a);
16448 +
16449 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
16450 +
16451 +/* i_op_add.c */
16452 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16453 +              struct dentry *h_parent, int isdir);
16454 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16455 +              dev_t dev);
16456 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
16457 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
16458 +               bool want_excl);
16459 +struct vfsub_aopen_args;
16460 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
16461 +                      struct vfsub_aopen_args *args);
16462 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
16463 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
16464 +             struct dentry *dentry);
16465 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
16466 +
16467 +/* i_op_del.c */
16468 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
16469 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16470 +              struct dentry *h_parent, int isdir);
16471 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
16472 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
16473 +
16474 +/* i_op_ren.c */
16475 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
16476 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
16477 +               struct inode *dir, struct dentry *dentry);
16478 +
16479 +/* iinfo.c */
16480 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
16481 +void au_hiput(struct au_hinode *hinode);
16482 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
16483 +                 struct dentry *h_wh);
16484 +unsigned int au_hi_flags(struct inode *inode, int isdir);
16485 +
16486 +/* hinode flags */
16487 +#define AuHi_XINO      1
16488 +#define AuHi_HNOTIFY   (1 << 1)
16489 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
16490 +#define au_fset_hi(flags, name) \
16491 +       do { (flags) |= AuHi_##name; } while (0)
16492 +#define au_fclr_hi(flags, name) \
16493 +       do { (flags) &= ~AuHi_##name; } while (0)
16494 +
16495 +#ifndef CONFIG_AUFS_HNOTIFY
16496 +#undef AuHi_HNOTIFY
16497 +#define AuHi_HNOTIFY   0
16498 +#endif
16499 +
16500 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
16501 +                  struct inode *h_inode, unsigned int flags);
16502 +
16503 +void au_update_iigen(struct inode *inode, int half);
16504 +void au_update_ibrange(struct inode *inode, int do_put_zero);
16505 +
16506 +void au_icntnr_init_once(void *_c);
16507 +int au_iinfo_init(struct inode *inode);
16508 +void au_iinfo_fin(struct inode *inode);
16509 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
16510 +
16511 +#ifdef CONFIG_PROC_FS
16512 +/* plink.c */
16513 +int au_plink_maint(struct super_block *sb, int flags);
16514 +struct au_sbinfo;
16515 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
16516 +int au_plink_maint_enter(struct super_block *sb);
16517 +#ifdef CONFIG_AUFS_DEBUG
16518 +void au_plink_list(struct super_block *sb);
16519 +#else
16520 +AuStubVoid(au_plink_list, struct super_block *sb)
16521 +#endif
16522 +int au_plink_test(struct inode *inode);
16523 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
16524 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
16525 +                    struct dentry *h_dentry);
16526 +void au_plink_put(struct super_block *sb, int verbose);
16527 +void au_plink_clean(struct super_block *sb, int verbose);
16528 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
16529 +#else
16530 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
16531 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
16532 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
16533 +AuStubVoid(au_plink_list, struct super_block *sb);
16534 +AuStubInt0(au_plink_test, struct inode *inode);
16535 +AuStub(struct dentry *, au_plink_lkup, return NULL,
16536 +       struct inode *inode, aufs_bindex_t bindex);
16537 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
16538 +          struct dentry *h_dentry);
16539 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
16540 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
16541 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
16542 +#endif /* CONFIG_PROC_FS */
16543 +
16544 +#ifdef CONFIG_AUFS_XATTR
16545 +/* xattr.c */
16546 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
16547 +                 unsigned int verbose);
16548 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
16549 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
16550 +                     size_t size);
16551 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
16552 +                 size_t size, int flags);
16553 +int aufs_removexattr(struct dentry *dentry, const char *name);
16554 +
16555 +/* void au_xattr_init(struct super_block *sb); */
16556 +#else
16557 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
16558 +          int ignore_flags, unsigned int verbose);
16559 +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
16560 +#endif
16561 +
16562 +#ifdef CONFIG_FS_POSIX_ACL
16563 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
16564 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
16565 +#endif
16566 +
16567 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
16568 +enum {
16569 +       AU_XATTR_SET,
16570 +       AU_XATTR_REMOVE,
16571 +       AU_ACL_SET
16572 +};
16573 +
16574 +struct au_srxattr {
16575 +       int type;
16576 +       union {
16577 +               struct {
16578 +                       const char      *name;
16579 +                       const void      *value;
16580 +                       size_t          size;
16581 +                       int             flags;
16582 +               } set;
16583 +               struct {
16584 +                       const char      *name;
16585 +               } remove;
16586 +               struct {
16587 +                       struct posix_acl *acl;
16588 +                       int             type;
16589 +               } acl_set;
16590 +       } u;
16591 +};
16592 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg);
16593 +#endif
16594 +
16595 +/* ---------------------------------------------------------------------- */
16596 +
16597 +/* lock subclass for iinfo */
16598 +enum {
16599 +       AuLsc_II_CHILD,         /* child first */
16600 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
16601 +       AuLsc_II_CHILD3,        /* copyup dirs */
16602 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
16603 +       AuLsc_II_PARENT2,
16604 +       AuLsc_II_PARENT3,       /* copyup dirs */
16605 +       AuLsc_II_NEW_CHILD
16606 +};
16607 +
16608 +/*
16609 + * ii_read_lock_child, ii_write_lock_child,
16610 + * ii_read_lock_child2, ii_write_lock_child2,
16611 + * ii_read_lock_child3, ii_write_lock_child3,
16612 + * ii_read_lock_parent, ii_write_lock_parent,
16613 + * ii_read_lock_parent2, ii_write_lock_parent2,
16614 + * ii_read_lock_parent3, ii_write_lock_parent3,
16615 + * ii_read_lock_new_child, ii_write_lock_new_child,
16616 + */
16617 +#define AuReadLockFunc(name, lsc) \
16618 +static inline void ii_read_lock_##name(struct inode *i) \
16619 +{ \
16620 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16621 +}
16622 +
16623 +#define AuWriteLockFunc(name, lsc) \
16624 +static inline void ii_write_lock_##name(struct inode *i) \
16625 +{ \
16626 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16627 +}
16628 +
16629 +#define AuRWLockFuncs(name, lsc) \
16630 +       AuReadLockFunc(name, lsc) \
16631 +       AuWriteLockFunc(name, lsc)
16632 +
16633 +AuRWLockFuncs(child, CHILD);
16634 +AuRWLockFuncs(child2, CHILD2);
16635 +AuRWLockFuncs(child3, CHILD3);
16636 +AuRWLockFuncs(parent, PARENT);
16637 +AuRWLockFuncs(parent2, PARENT2);
16638 +AuRWLockFuncs(parent3, PARENT3);
16639 +AuRWLockFuncs(new_child, NEW_CHILD);
16640 +
16641 +#undef AuReadLockFunc
16642 +#undef AuWriteLockFunc
16643 +#undef AuRWLockFuncs
16644 +
16645 +/*
16646 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
16647 + */
16648 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
16649 +
16650 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
16651 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
16652 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
16653 +
16654 +/* ---------------------------------------------------------------------- */
16655 +
16656 +static inline void au_icntnr_init(struct au_icntnr *c)
16657 +{
16658 +#ifdef CONFIG_AUFS_DEBUG
16659 +       c->vfs_inode.i_mode = 0;
16660 +#endif
16661 +}
16662 +
16663 +static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
16664 +{
16665 +       unsigned int gen;
16666 +       struct au_iinfo *iinfo;
16667 +
16668 +       iinfo = au_ii(inode);
16669 +       spin_lock(&iinfo->ii_genspin);
16670 +       if (iigen)
16671 +               *iigen = iinfo->ii_generation;
16672 +       gen = iinfo->ii_generation.ig_generation;
16673 +       spin_unlock(&iinfo->ii_genspin);
16674 +
16675 +       return gen;
16676 +}
16677 +
16678 +/* tiny test for inode number */
16679 +/* tmpfs generation is too rough */
16680 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
16681 +{
16682 +       struct au_iinfo *iinfo;
16683 +
16684 +       iinfo = au_ii(inode);
16685 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
16686 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
16687 +                && iinfo->ii_higen == h_inode->i_generation);
16688 +}
16689 +
16690 +static inline void au_iigen_dec(struct inode *inode)
16691 +{
16692 +       struct au_iinfo *iinfo;
16693 +
16694 +       iinfo = au_ii(inode);
16695 +       spin_lock(&iinfo->ii_genspin);
16696 +       iinfo->ii_generation.ig_generation--;
16697 +       spin_unlock(&iinfo->ii_genspin);
16698 +}
16699 +
16700 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
16701 +{
16702 +       int err;
16703 +
16704 +       err = 0;
16705 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
16706 +               err = -EIO;
16707 +
16708 +       return err;
16709 +}
16710 +
16711 +/* ---------------------------------------------------------------------- */
16712 +
16713 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
16714 +                                       aufs_bindex_t bindex)
16715 +{
16716 +       IiMustAnyLock(inode);
16717 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
16718 +}
16719 +
16720 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
16721 +{
16722 +       IiMustAnyLock(inode);
16723 +       return au_ii(inode)->ii_bstart;
16724 +}
16725 +
16726 +static inline aufs_bindex_t au_ibend(struct inode *inode)
16727 +{
16728 +       IiMustAnyLock(inode);
16729 +       return au_ii(inode)->ii_bend;
16730 +}
16731 +
16732 +static inline struct au_vdir *au_ivdir(struct inode *inode)
16733 +{
16734 +       IiMustAnyLock(inode);
16735 +       return au_ii(inode)->ii_vdir;
16736 +}
16737 +
16738 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
16739 +{
16740 +       IiMustAnyLock(inode);
16741 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
16742 +}
16743 +
16744 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
16745 +{
16746 +       IiMustWriteLock(inode);
16747 +       au_ii(inode)->ii_bstart = bindex;
16748 +}
16749 +
16750 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
16751 +{
16752 +       IiMustWriteLock(inode);
16753 +       au_ii(inode)->ii_bend = bindex;
16754 +}
16755 +
16756 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
16757 +{
16758 +       IiMustWriteLock(inode);
16759 +       au_ii(inode)->ii_vdir = vdir;
16760 +}
16761 +
16762 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
16763 +{
16764 +       IiMustAnyLock(inode);
16765 +       return au_ii(inode)->ii_hinode + bindex;
16766 +}
16767 +
16768 +/* ---------------------------------------------------------------------- */
16769 +
16770 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
16771 +{
16772 +       if (pin)
16773 +               return pin->parent;
16774 +       return NULL;
16775 +}
16776 +
16777 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
16778 +{
16779 +       if (pin && pin->hdir)
16780 +               return pin->hdir->hi_inode;
16781 +       return NULL;
16782 +}
16783 +
16784 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
16785 +{
16786 +       if (pin)
16787 +               return pin->hdir;
16788 +       return NULL;
16789 +}
16790 +
16791 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
16792 +{
16793 +       if (pin)
16794 +               pin->dentry = dentry;
16795 +}
16796 +
16797 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
16798 +                                          unsigned char lflag)
16799 +{
16800 +       if (pin) {
16801 +               if (lflag)
16802 +                       au_fset_pin(pin->flags, DI_LOCKED);
16803 +               else
16804 +                       au_fclr_pin(pin->flags, DI_LOCKED);
16805 +       }
16806 +}
16807 +
16808 +#if 0 /* reserved */
16809 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
16810 +{
16811 +       if (pin) {
16812 +               dput(pin->parent);
16813 +               pin->parent = dget(parent);
16814 +       }
16815 +}
16816 +#endif
16817 +
16818 +/* ---------------------------------------------------------------------- */
16819 +
16820 +struct au_branch;
16821 +#ifdef CONFIG_AUFS_HNOTIFY
16822 +struct au_hnotify_op {
16823 +       void (*ctl)(struct au_hinode *hinode, int do_set);
16824 +       int (*alloc)(struct au_hinode *hinode);
16825 +
16826 +       /*
16827 +        * if it returns true, the the caller should free hinode->hi_notify,
16828 +        * otherwise ->free() frees it.
16829 +        */
16830 +       int (*free)(struct au_hinode *hinode,
16831 +                   struct au_hnotify *hn) __must_check;
16832 +
16833 +       void (*fin)(void);
16834 +       int (*init)(void);
16835 +
16836 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
16837 +       void (*fin_br)(struct au_branch *br);
16838 +       int (*init_br)(struct au_branch *br, int perm);
16839 +};
16840 +
16841 +/* hnotify.c */
16842 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
16843 +void au_hn_free(struct au_hinode *hinode);
16844 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
16845 +void au_hn_reset(struct inode *inode, unsigned int flags);
16846 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
16847 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
16848 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
16849 +int au_hnotify_init_br(struct au_branch *br, int perm);
16850 +void au_hnotify_fin_br(struct au_branch *br);
16851 +int __init au_hnotify_init(void);
16852 +void au_hnotify_fin(void);
16853 +
16854 +/* hfsnotify.c */
16855 +extern const struct au_hnotify_op au_hnotify_op;
16856 +
16857 +static inline
16858 +void au_hn_init(struct au_hinode *hinode)
16859 +{
16860 +       hinode->hi_notify = NULL;
16861 +}
16862 +
16863 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
16864 +{
16865 +       return hinode->hi_notify;
16866 +}
16867 +
16868 +#else
16869 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
16870 +       struct au_hinode *hinode __maybe_unused,
16871 +       struct inode *inode __maybe_unused)
16872 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
16873 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
16874 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
16875 +          int do_set __maybe_unused)
16876 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
16877 +          unsigned int flags __maybe_unused)
16878 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
16879 +          struct au_branch *br __maybe_unused,
16880 +          int perm __maybe_unused)
16881 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
16882 +          int perm __maybe_unused)
16883 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
16884 +AuStubInt0(__init au_hnotify_init, void)
16885 +AuStubVoid(au_hnotify_fin, void)
16886 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
16887 +#endif /* CONFIG_AUFS_HNOTIFY */
16888 +
16889 +static inline void au_hn_suspend(struct au_hinode *hdir)
16890 +{
16891 +       au_hn_ctl(hdir, /*do_set*/0);
16892 +}
16893 +
16894 +static inline void au_hn_resume(struct au_hinode *hdir)
16895 +{
16896 +       au_hn_ctl(hdir, /*do_set*/1);
16897 +}
16898 +
16899 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
16900 +{
16901 +       mutex_lock(&hdir->hi_inode->i_mutex);
16902 +       au_hn_suspend(hdir);
16903 +}
16904 +
16905 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
16906 +                                         unsigned int sc __maybe_unused)
16907 +{
16908 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
16909 +       au_hn_suspend(hdir);
16910 +}
16911 +
16912 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
16913 +{
16914 +       au_hn_resume(hdir);
16915 +       mutex_unlock(&hdir->hi_inode->i_mutex);
16916 +}
16917 +
16918 +#endif /* __KERNEL__ */
16919 +#endif /* __AUFS_INODE_H__ */
16920 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
16921 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
16922 +++ linux/fs/aufs/ioctl.c       2015-06-22 08:29:23.706757681 +0200
16923 @@ -0,0 +1,219 @@
16924 +/*
16925 + * Copyright (C) 2005-2015 Junjiro R. Okajima
16926 + *
16927 + * This program, aufs is free software; you can redistribute it and/or modify
16928 + * it under the terms of the GNU General Public License as published by
16929 + * the Free Software Foundation; either version 2 of the License, or
16930 + * (at your option) any later version.
16931 + *
16932 + * This program is distributed in the hope that it will be useful,
16933 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16934 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16935 + * GNU General Public License for more details.
16936 + *
16937 + * You should have received a copy of the GNU General Public License
16938 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16939 + */
16940 +
16941 +/*
16942 + * ioctl
16943 + * plink-management and readdir in userspace.
16944 + * assist the pathconf(3) wrapper library.
16945 + * move-down
16946 + * File-based Hierarchical Storage Management.
16947 + */
16948 +
16949 +#include <linux/compat.h>
16950 +#include <linux/file.h>
16951 +#include "aufs.h"
16952 +
16953 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
16954 +{
16955 +       int err, fd;
16956 +       aufs_bindex_t wbi, bindex, bend;
16957 +       struct file *h_file;
16958 +       struct super_block *sb;
16959 +       struct dentry *root;
16960 +       struct au_branch *br;
16961 +       struct aufs_wbr_fd wbrfd = {
16962 +               .oflags = au_dir_roflags,
16963 +               .brid   = -1
16964 +       };
16965 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
16966 +               | O_NOATIME | O_CLOEXEC;
16967 +
16968 +       AuDebugOn(wbrfd.oflags & ~valid);
16969 +
16970 +       if (arg) {
16971 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
16972 +               if (unlikely(err)) {
16973 +                       err = -EFAULT;
16974 +                       goto out;
16975 +               }
16976 +
16977 +               err = -EINVAL;
16978 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
16979 +               wbrfd.oflags |= au_dir_roflags;
16980 +               AuDbg("0%o\n", wbrfd.oflags);
16981 +               if (unlikely(wbrfd.oflags & ~valid))
16982 +                       goto out;
16983 +       }
16984 +
16985 +       fd = get_unused_fd_flags(0);
16986 +       err = fd;
16987 +       if (unlikely(fd < 0))
16988 +               goto out;
16989 +
16990 +       h_file = ERR_PTR(-EINVAL);
16991 +       wbi = 0;
16992 +       br = NULL;
16993 +       sb = path->dentry->d_sb;
16994 +       root = sb->s_root;
16995 +       aufs_read_lock(root, AuLock_IR);
16996 +       bend = au_sbend(sb);
16997 +       if (wbrfd.brid >= 0) {
16998 +               wbi = au_br_index(sb, wbrfd.brid);
16999 +               if (unlikely(wbi < 0 || wbi > bend))
17000 +                       goto out_unlock;
17001 +       }
17002 +
17003 +       h_file = ERR_PTR(-ENOENT);
17004 +       br = au_sbr(sb, wbi);
17005 +       if (!au_br_writable(br->br_perm)) {
17006 +               if (arg)
17007 +                       goto out_unlock;
17008 +
17009 +               bindex = wbi + 1;
17010 +               wbi = -1;
17011 +               for (; bindex <= bend; bindex++) {
17012 +                       br = au_sbr(sb, bindex);
17013 +                       if (au_br_writable(br->br_perm)) {
17014 +                               wbi = bindex;
17015 +                               br = au_sbr(sb, wbi);
17016 +                               break;
17017 +                       }
17018 +               }
17019 +       }
17020 +       AuDbg("wbi %d\n", wbi);
17021 +       if (wbi >= 0)
17022 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
17023 +                                  /*force_wr*/0);
17024 +
17025 +out_unlock:
17026 +       aufs_read_unlock(root, AuLock_IR);
17027 +       err = PTR_ERR(h_file);
17028 +       if (IS_ERR(h_file))
17029 +               goto out_fd;
17030 +
17031 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
17032 +       fd_install(fd, h_file);
17033 +       err = fd;
17034 +       goto out; /* success */
17035 +
17036 +out_fd:
17037 +       put_unused_fd(fd);
17038 +out:
17039 +       AuTraceErr(err);
17040 +       return err;
17041 +}
17042 +
17043 +/* ---------------------------------------------------------------------- */
17044 +
17045 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
17046 +{
17047 +       long err;
17048 +       struct dentry *dentry;
17049 +
17050 +       switch (cmd) {
17051 +       case AUFS_CTL_RDU:
17052 +       case AUFS_CTL_RDU_INO:
17053 +               err = au_rdu_ioctl(file, cmd, arg);
17054 +               break;
17055 +
17056 +       case AUFS_CTL_WBR_FD:
17057 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17058 +               break;
17059 +
17060 +       case AUFS_CTL_IBUSY:
17061 +               err = au_ibusy_ioctl(file, arg);
17062 +               break;
17063 +
17064 +       case AUFS_CTL_BRINFO:
17065 +               err = au_brinfo_ioctl(file, arg);
17066 +               break;
17067 +
17068 +       case AUFS_CTL_FHSM_FD:
17069 +               dentry = file->f_path.dentry;
17070 +               if (IS_ROOT(dentry))
17071 +                       err = au_fhsm_fd(dentry->d_sb, arg);
17072 +               else
17073 +                       err = -ENOTTY;
17074 +               break;
17075 +
17076 +       default:
17077 +               /* do not call the lower */
17078 +               AuDbg("0x%x\n", cmd);
17079 +               err = -ENOTTY;
17080 +       }
17081 +
17082 +       AuTraceErr(err);
17083 +       return err;
17084 +}
17085 +
17086 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
17087 +{
17088 +       long err;
17089 +
17090 +       switch (cmd) {
17091 +       case AUFS_CTL_MVDOWN:
17092 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
17093 +               break;
17094 +
17095 +       case AUFS_CTL_WBR_FD:
17096 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17097 +               break;
17098 +
17099 +       default:
17100 +               /* do not call the lower */
17101 +               AuDbg("0x%x\n", cmd);
17102 +               err = -ENOTTY;
17103 +       }
17104 +
17105 +       AuTraceErr(err);
17106 +       return err;
17107 +}
17108 +
17109 +#ifdef CONFIG_COMPAT
17110 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
17111 +                          unsigned long arg)
17112 +{
17113 +       long err;
17114 +
17115 +       switch (cmd) {
17116 +       case AUFS_CTL_RDU:
17117 +       case AUFS_CTL_RDU_INO:
17118 +               err = au_rdu_compat_ioctl(file, cmd, arg);
17119 +               break;
17120 +
17121 +       case AUFS_CTL_IBUSY:
17122 +               err = au_ibusy_compat_ioctl(file, arg);
17123 +               break;
17124 +
17125 +       case AUFS_CTL_BRINFO:
17126 +               err = au_brinfo_compat_ioctl(file, arg);
17127 +               break;
17128 +
17129 +       default:
17130 +               err = aufs_ioctl_dir(file, cmd, arg);
17131 +       }
17132 +
17133 +       AuTraceErr(err);
17134 +       return err;
17135 +}
17136 +
17137 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
17138 +                             unsigned long arg)
17139 +{
17140 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
17141 +}
17142 +#endif
17143 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
17144 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
17145 +++ linux/fs/aufs/i_op_add.c    2015-06-22 08:27:37.880835119 +0200
17146 @@ -0,0 +1,930 @@
17147 +/*
17148 + * Copyright (C) 2005-2015 Junjiro R. Okajima
17149 + *
17150 + * This program, aufs is free software; you can redistribute it and/or modify
17151 + * it under the terms of the GNU General Public License as published by
17152 + * the Free Software Foundation; either version 2 of the License, or
17153 + * (at your option) any later version.
17154 + *
17155 + * This program is distributed in the hope that it will be useful,
17156 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17157 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17158 + * GNU General Public License for more details.
17159 + *
17160 + * You should have received a copy of the GNU General Public License
17161 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17162 + */
17163 +
17164 +/*
17165 + * inode operations (add entry)
17166 + */
17167 +
17168 +#include "aufs.h"
17169 +
17170 +/*
17171 + * final procedure of adding a new entry, except link(2).
17172 + * remove whiteout, instantiate, copyup the parent dir's times and size
17173 + * and update version.
17174 + * if it failed, re-create the removed whiteout.
17175 + */
17176 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
17177 +                 struct dentry *wh_dentry, struct dentry *dentry)
17178 +{
17179 +       int err, rerr;
17180 +       aufs_bindex_t bwh;
17181 +       struct path h_path;
17182 +       struct super_block *sb;
17183 +       struct inode *inode, *h_dir;
17184 +       struct dentry *wh;
17185 +
17186 +       bwh = -1;
17187 +       sb = dir->i_sb;
17188 +       if (wh_dentry) {
17189 +               h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
17190 +               IMustLock(h_dir);
17191 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
17192 +               bwh = au_dbwh(dentry);
17193 +               h_path.dentry = wh_dentry;
17194 +               h_path.mnt = au_sbr_mnt(sb, bindex);
17195 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
17196 +                                         dentry);
17197 +               if (unlikely(err))
17198 +                       goto out;
17199 +       }
17200 +
17201 +       inode = au_new_inode(dentry, /*must_new*/1);
17202 +       if (!IS_ERR(inode)) {
17203 +               d_instantiate(dentry, inode);
17204 +               dir = dentry->d_parent->d_inode; /* dir inode is locked */
17205 +               IMustLock(dir);
17206 +               au_dir_ts(dir, bindex);
17207 +               dir->i_version++;
17208 +               au_fhsm_wrote(sb, bindex, /*force*/0);
17209 +               return 0; /* success */
17210 +       }
17211 +
17212 +       err = PTR_ERR(inode);
17213 +       if (!wh_dentry)
17214 +               goto out;
17215 +
17216 +       /* revert */
17217 +       /* dir inode is locked */
17218 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
17219 +       rerr = PTR_ERR(wh);
17220 +       if (IS_ERR(wh)) {
17221 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
17222 +                       dentry, err, rerr);
17223 +               err = -EIO;
17224 +       } else
17225 +               dput(wh);
17226 +
17227 +out:
17228 +       return err;
17229 +}
17230 +
17231 +static int au_d_may_add(struct dentry *dentry)
17232 +{
17233 +       int err;
17234 +
17235 +       err = 0;
17236 +       if (unlikely(d_unhashed(dentry)))
17237 +               err = -ENOENT;
17238 +       if (unlikely(dentry->d_inode))
17239 +               err = -EEXIST;
17240 +       return err;
17241 +}
17242 +
17243 +/*
17244 + * simple tests for the adding inode operations.
17245 + * following the checks in vfs, plus the parent-child relationship.
17246 + */
17247 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
17248 +              struct dentry *h_parent, int isdir)
17249 +{
17250 +       int err;
17251 +       umode_t h_mode;
17252 +       struct dentry *h_dentry;
17253 +       struct inode *h_inode;
17254 +
17255 +       err = -ENAMETOOLONG;
17256 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17257 +               goto out;
17258 +
17259 +       h_dentry = au_h_dptr(dentry, bindex);
17260 +       h_inode = h_dentry->d_inode;
17261 +       if (!dentry->d_inode) {
17262 +               err = -EEXIST;
17263 +               if (unlikely(h_inode))
17264 +                       goto out;
17265 +       } else {
17266 +               /* rename(2) case */
17267 +               err = -EIO;
17268 +               if (unlikely(!h_inode || !h_inode->i_nlink))
17269 +                       goto out;
17270 +
17271 +               h_mode = h_inode->i_mode;
17272 +               if (!isdir) {
17273 +                       err = -EISDIR;
17274 +                       if (unlikely(S_ISDIR(h_mode)))
17275 +                               goto out;
17276 +               } else if (unlikely(!S_ISDIR(h_mode))) {
17277 +                       err = -ENOTDIR;
17278 +                       goto out;
17279 +               }
17280 +       }
17281 +
17282 +       err = 0;
17283 +       /* expected parent dir is locked */
17284 +       if (unlikely(h_parent != h_dentry->d_parent))
17285 +               err = -EIO;
17286 +
17287 +out:
17288 +       AuTraceErr(err);
17289 +       return err;
17290 +}
17291 +
17292 +/*
17293 + * initial procedure of adding a new entry.
17294 + * prepare writable branch and the parent dir, lock it,
17295 + * and lookup whiteout for the new entry.
17296 + */
17297 +static struct dentry*
17298 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
17299 +                 struct dentry *src_dentry, struct au_pin *pin,
17300 +                 struct au_wr_dir_args *wr_dir_args)
17301 +{
17302 +       struct dentry *wh_dentry, *h_parent;
17303 +       struct super_block *sb;
17304 +       struct au_branch *br;
17305 +       int err;
17306 +       unsigned int udba;
17307 +       aufs_bindex_t bcpup;
17308 +
17309 +       AuDbg("%pd\n", dentry);
17310 +
17311 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
17312 +       bcpup = err;
17313 +       wh_dentry = ERR_PTR(err);
17314 +       if (unlikely(err < 0))
17315 +               goto out;
17316 +
17317 +       sb = dentry->d_sb;
17318 +       udba = au_opt_udba(sb);
17319 +       err = au_pin(pin, dentry, bcpup, udba,
17320 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17321 +       wh_dentry = ERR_PTR(err);
17322 +       if (unlikely(err))
17323 +               goto out;
17324 +
17325 +       h_parent = au_pinned_h_parent(pin);
17326 +       if (udba != AuOpt_UDBA_NONE
17327 +           && au_dbstart(dentry) == bcpup)
17328 +               err = au_may_add(dentry, bcpup, h_parent,
17329 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
17330 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17331 +               err = -ENAMETOOLONG;
17332 +       wh_dentry = ERR_PTR(err);
17333 +       if (unlikely(err))
17334 +               goto out_unpin;
17335 +
17336 +       br = au_sbr(sb, bcpup);
17337 +       if (dt) {
17338 +               struct path tmp = {
17339 +                       .dentry = h_parent,
17340 +                       .mnt    = au_br_mnt(br)
17341 +               };
17342 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
17343 +       }
17344 +
17345 +       wh_dentry = NULL;
17346 +       if (bcpup != au_dbwh(dentry))
17347 +               goto out; /* success */
17348 +
17349 +       /*
17350 +        * ENAMETOOLONG here means that if we allowed create such name, then it
17351 +        * would not be able to removed in the future. So we don't allow such
17352 +        * name here and we don't handle ENAMETOOLONG differently here.
17353 +        */
17354 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
17355 +
17356 +out_unpin:
17357 +       if (IS_ERR(wh_dentry))
17358 +               au_unpin(pin);
17359 +out:
17360 +       return wh_dentry;
17361 +}
17362 +
17363 +/* ---------------------------------------------------------------------- */
17364 +
17365 +enum { Mknod, Symlink, Creat };
17366 +struct simple_arg {
17367 +       int type;
17368 +       union {
17369 +               struct {
17370 +                       umode_t                 mode;
17371 +                       bool                    want_excl;
17372 +                       bool                    try_aopen;
17373 +                       struct vfsub_aopen_args *aopen;
17374 +               } c;
17375 +               struct {
17376 +                       const char *symname;
17377 +               } s;
17378 +               struct {
17379 +                       umode_t mode;
17380 +                       dev_t dev;
17381 +               } m;
17382 +       } u;
17383 +};
17384 +
17385 +static int add_simple(struct inode *dir, struct dentry *dentry,
17386 +                     struct simple_arg *arg)
17387 +{
17388 +       int err, rerr;
17389 +       aufs_bindex_t bstart;
17390 +       unsigned char created;
17391 +       const unsigned char try_aopen
17392 +               = (arg->type == Creat && arg->u.c.try_aopen);
17393 +       struct dentry *wh_dentry, *parent;
17394 +       struct inode *h_dir;
17395 +       struct super_block *sb;
17396 +       struct au_branch *br;
17397 +       /* to reuduce stack size */
17398 +       struct {
17399 +               struct au_dtime dt;
17400 +               struct au_pin pin;
17401 +               struct path h_path;
17402 +               struct au_wr_dir_args wr_dir_args;
17403 +       } *a;
17404 +
17405 +       AuDbg("%pd\n", dentry);
17406 +       IMustLock(dir);
17407 +
17408 +       err = -ENOMEM;
17409 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17410 +       if (unlikely(!a))
17411 +               goto out;
17412 +       a->wr_dir_args.force_btgt = -1;
17413 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
17414 +
17415 +       parent = dentry->d_parent; /* dir inode is locked */
17416 +       if (!try_aopen) {
17417 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17418 +               if (unlikely(err))
17419 +                       goto out_free;
17420 +       }
17421 +       err = au_d_may_add(dentry);
17422 +       if (unlikely(err))
17423 +               goto out_unlock;
17424 +       if (!try_aopen)
17425 +               di_write_lock_parent(parent);
17426 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17427 +                                     &a->pin, &a->wr_dir_args);
17428 +       err = PTR_ERR(wh_dentry);
17429 +       if (IS_ERR(wh_dentry))
17430 +               goto out_parent;
17431 +
17432 +       bstart = au_dbstart(dentry);
17433 +       sb = dentry->d_sb;
17434 +       br = au_sbr(sb, bstart);
17435 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17436 +       a->h_path.mnt = au_br_mnt(br);
17437 +       h_dir = au_pinned_h_dir(&a->pin);
17438 +       switch (arg->type) {
17439 +       case Creat:
17440 +               err = 0;
17441 +               if (!try_aopen || !h_dir->i_op->atomic_open)
17442 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
17443 +                                          arg->u.c.want_excl);
17444 +               else
17445 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry,
17446 +                                               arg->u.c.aopen, br);
17447 +               break;
17448 +       case Symlink:
17449 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
17450 +               break;
17451 +       case Mknod:
17452 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
17453 +                                 arg->u.m.dev);
17454 +               break;
17455 +       default:
17456 +               BUG();
17457 +       }
17458 +       created = !err;
17459 +       if (!err)
17460 +               err = epilog(dir, bstart, wh_dentry, dentry);
17461 +
17462 +       /* revert */
17463 +       if (unlikely(created && err && a->h_path.dentry->d_inode)) {
17464 +               /* no delegation since it is just created */
17465 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
17466 +                                   /*force*/0);
17467 +               if (rerr) {
17468 +                       AuIOErr("%pd revert failure(%d, %d)\n",
17469 +                               dentry, err, rerr);
17470 +                       err = -EIO;
17471 +               }
17472 +               au_dtime_revert(&a->dt);
17473 +       }
17474 +
17475 +       if (!err && try_aopen && !h_dir->i_op->atomic_open)
17476 +               *arg->u.c.aopen->opened |= FILE_CREATED;
17477 +
17478 +       au_unpin(&a->pin);
17479 +       dput(wh_dentry);
17480 +
17481 +out_parent:
17482 +       if (!try_aopen)
17483 +               di_write_unlock(parent);
17484 +out_unlock:
17485 +       if (unlikely(err)) {
17486 +               au_update_dbstart(dentry);
17487 +               d_drop(dentry);
17488 +       }
17489 +       if (!try_aopen)
17490 +               aufs_read_unlock(dentry, AuLock_DW);
17491 +out_free:
17492 +       kfree(a);
17493 +out:
17494 +       return err;
17495 +}
17496 +
17497 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
17498 +              dev_t dev)
17499 +{
17500 +       struct simple_arg arg = {
17501 +               .type = Mknod,
17502 +               .u.m = {
17503 +                       .mode   = mode,
17504 +                       .dev    = dev
17505 +               }
17506 +       };
17507 +       return add_simple(dir, dentry, &arg);
17508 +}
17509 +
17510 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17511 +{
17512 +       struct simple_arg arg = {
17513 +               .type = Symlink,
17514 +               .u.s.symname = symname
17515 +       };
17516 +       return add_simple(dir, dentry, &arg);
17517 +}
17518 +
17519 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
17520 +               bool want_excl)
17521 +{
17522 +       struct simple_arg arg = {
17523 +               .type = Creat,
17524 +               .u.c = {
17525 +                       .mode           = mode,
17526 +                       .want_excl      = want_excl
17527 +               }
17528 +       };
17529 +       return add_simple(dir, dentry, &arg);
17530 +}
17531 +
17532 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
17533 +                      struct vfsub_aopen_args *aopen_args)
17534 +{
17535 +       struct simple_arg arg = {
17536 +               .type = Creat,
17537 +               .u.c = {
17538 +                       .mode           = aopen_args->create_mode,
17539 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
17540 +                       .try_aopen      = true,
17541 +                       .aopen          = aopen_args
17542 +               }
17543 +       };
17544 +       return add_simple(dir, dentry, &arg);
17545 +}
17546 +
17547 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
17548 +{
17549 +       int err;
17550 +       aufs_bindex_t bindex;
17551 +       struct super_block *sb;
17552 +       struct dentry *parent, *h_parent, *h_dentry;
17553 +       struct inode *h_dir, *inode;
17554 +       struct vfsmount *h_mnt;
17555 +       struct au_wr_dir_args wr_dir_args = {
17556 +               .force_btgt     = -1,
17557 +               .flags          = AuWrDir_TMPFILE
17558 +       };
17559 +
17560 +       /* copy-up may happen */
17561 +       mutex_lock(&dir->i_mutex);
17562 +
17563 +       sb = dir->i_sb;
17564 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17565 +       if (unlikely(err))
17566 +               goto out;
17567 +
17568 +       err = au_di_init(dentry);
17569 +       if (unlikely(err))
17570 +               goto out_si;
17571 +
17572 +       err = -EBUSY;
17573 +       parent = d_find_any_alias(dir);
17574 +       AuDebugOn(!parent);
17575 +       di_write_lock_parent(parent);
17576 +       if (unlikely(parent->d_inode != dir))
17577 +               goto out_parent;
17578 +
17579 +       err = au_digen_test(parent, au_sigen(sb));
17580 +       if (unlikely(err))
17581 +               goto out_parent;
17582 +
17583 +       bindex = au_dbstart(parent);
17584 +       au_set_dbstart(dentry, bindex);
17585 +       au_set_dbend(dentry, bindex);
17586 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17587 +       bindex = err;
17588 +       if (unlikely(err < 0))
17589 +               goto out_parent;
17590 +
17591 +       err = -EOPNOTSUPP;
17592 +       h_dir = au_h_iptr(dir, bindex);
17593 +       if (unlikely(!h_dir->i_op->tmpfile))
17594 +               goto out_parent;
17595 +
17596 +       h_mnt = au_sbr_mnt(sb, bindex);
17597 +       err = vfsub_mnt_want_write(h_mnt);
17598 +       if (unlikely(err))
17599 +               goto out_parent;
17600 +
17601 +       h_parent = au_h_dptr(parent, bindex);
17602 +       err = inode_permission(h_parent->d_inode, MAY_WRITE | MAY_EXEC);
17603 +       if (unlikely(err))
17604 +               goto out_mnt;
17605 +
17606 +       err = -ENOMEM;
17607 +       h_dentry = d_alloc(h_parent, &dentry->d_name);
17608 +       if (unlikely(!h_dentry))
17609 +               goto out_mnt;
17610 +
17611 +       err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
17612 +       if (unlikely(err))
17613 +               goto out_dentry;
17614 +
17615 +       au_set_dbstart(dentry, bindex);
17616 +       au_set_dbend(dentry, bindex);
17617 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
17618 +       inode = au_new_inode(dentry, /*must_new*/1);
17619 +       if (IS_ERR(inode)) {
17620 +               err = PTR_ERR(inode);
17621 +               au_set_h_dptr(dentry, bindex, NULL);
17622 +               au_set_dbstart(dentry, -1);
17623 +               au_set_dbend(dentry, -1);
17624 +       } else {
17625 +               if (!inode->i_nlink)
17626 +                       set_nlink(inode, 1);
17627 +               d_tmpfile(dentry, inode);
17628 +               au_di(dentry)->di_tmpfile = 1;
17629 +
17630 +               /* update without i_mutex */
17631 +               if (au_ibstart(dir) == au_dbstart(dentry))
17632 +                       au_cpup_attr_timesizes(dir);
17633 +       }
17634 +
17635 +out_dentry:
17636 +       dput(h_dentry);
17637 +out_mnt:
17638 +       vfsub_mnt_drop_write(h_mnt);
17639 +out_parent:
17640 +       di_write_unlock(parent);
17641 +       dput(parent);
17642 +       di_write_unlock(dentry);
17643 +       if (!err)
17644 +#if 0
17645 +               /* verbose coding for lock class name */
17646 +               au_rw_class(&au_di(dentry)->di_rwsem,
17647 +                           au_lc_key + AuLcNonDir_DIINFO);
17648 +#else
17649 +               ;
17650 +#endif
17651 +       else {
17652 +               au_di_fin(dentry);
17653 +               dentry->d_fsdata = NULL;
17654 +       }
17655 +out_si:
17656 +       si_read_unlock(sb);
17657 +out:
17658 +       mutex_unlock(&dir->i_mutex);
17659 +       return err;
17660 +}
17661 +
17662 +/* ---------------------------------------------------------------------- */
17663 +
17664 +struct au_link_args {
17665 +       aufs_bindex_t bdst, bsrc;
17666 +       struct au_pin pin;
17667 +       struct path h_path;
17668 +       struct dentry *src_parent, *parent;
17669 +};
17670 +
17671 +static int au_cpup_before_link(struct dentry *src_dentry,
17672 +                              struct au_link_args *a)
17673 +{
17674 +       int err;
17675 +       struct dentry *h_src_dentry;
17676 +       struct au_cp_generic cpg = {
17677 +               .dentry = src_dentry,
17678 +               .bdst   = a->bdst,
17679 +               .bsrc   = a->bsrc,
17680 +               .len    = -1,
17681 +               .pin    = &a->pin,
17682 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
17683 +       };
17684 +
17685 +       di_read_lock_parent(a->src_parent, AuLock_IR);
17686 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
17687 +       if (unlikely(err))
17688 +               goto out;
17689 +
17690 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
17691 +       err = au_pin(&a->pin, src_dentry, a->bdst,
17692 +                    au_opt_udba(src_dentry->d_sb),
17693 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17694 +       if (unlikely(err))
17695 +               goto out;
17696 +
17697 +       err = au_sio_cpup_simple(&cpg);
17698 +       au_unpin(&a->pin);
17699 +
17700 +out:
17701 +       di_read_unlock(a->src_parent, AuLock_IR);
17702 +       return err;
17703 +}
17704 +
17705 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
17706 +                          struct au_link_args *a)
17707 +{
17708 +       int err;
17709 +       unsigned char plink;
17710 +       aufs_bindex_t bend;
17711 +       struct dentry *h_src_dentry;
17712 +       struct inode *h_inode, *inode, *delegated;
17713 +       struct super_block *sb;
17714 +       struct file *h_file;
17715 +
17716 +       plink = 0;
17717 +       h_inode = NULL;
17718 +       sb = src_dentry->d_sb;
17719 +       inode = src_dentry->d_inode;
17720 +       if (au_ibstart(inode) <= a->bdst)
17721 +               h_inode = au_h_iptr(inode, a->bdst);
17722 +       if (!h_inode || !h_inode->i_nlink) {
17723 +               /* copyup src_dentry as the name of dentry. */
17724 +               bend = au_dbend(dentry);
17725 +               if (bend < a->bsrc)
17726 +                       au_set_dbend(dentry, a->bsrc);
17727 +               au_set_h_dptr(dentry, a->bsrc,
17728 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
17729 +               dget(a->h_path.dentry);
17730 +               au_set_h_dptr(dentry, a->bdst, NULL);
17731 +               AuDbg("temporary d_inode...\n");
17732 +               spin_lock(&dentry->d_lock);
17733 +               dentry->d_inode = src_dentry->d_inode; /* tmp */
17734 +               spin_unlock(&dentry->d_lock);
17735 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
17736 +               if (IS_ERR(h_file))
17737 +                       err = PTR_ERR(h_file);
17738 +               else {
17739 +                       struct au_cp_generic cpg = {
17740 +                               .dentry = dentry,
17741 +                               .bdst   = a->bdst,
17742 +                               .bsrc   = -1,
17743 +                               .len    = -1,
17744 +                               .pin    = &a->pin,
17745 +                               .flags  = AuCpup_KEEPLINO
17746 +                       };
17747 +                       err = au_sio_cpup_simple(&cpg);
17748 +                       au_h_open_post(dentry, a->bsrc, h_file);
17749 +                       if (!err) {
17750 +                               dput(a->h_path.dentry);
17751 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
17752 +                       } else
17753 +                               au_set_h_dptr(dentry, a->bdst,
17754 +                                             a->h_path.dentry);
17755 +               }
17756 +               spin_lock(&dentry->d_lock);
17757 +               dentry->d_inode = NULL; /* restore */
17758 +               spin_unlock(&dentry->d_lock);
17759 +               AuDbg("temporary d_inode...done\n");
17760 +               au_set_h_dptr(dentry, a->bsrc, NULL);
17761 +               au_set_dbend(dentry, bend);
17762 +       } else {
17763 +               /* the inode of src_dentry already exists on a.bdst branch */
17764 +               h_src_dentry = d_find_alias(h_inode);
17765 +               if (!h_src_dentry && au_plink_test(inode)) {
17766 +                       plink = 1;
17767 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
17768 +                       err = PTR_ERR(h_src_dentry);
17769 +                       if (IS_ERR(h_src_dentry))
17770 +                               goto out;
17771 +
17772 +                       if (unlikely(!h_src_dentry->d_inode)) {
17773 +                               dput(h_src_dentry);
17774 +                               h_src_dentry = NULL;
17775 +                       }
17776 +
17777 +               }
17778 +               if (h_src_dentry) {
17779 +                       delegated = NULL;
17780 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
17781 +                                        &a->h_path, &delegated);
17782 +                       if (unlikely(err == -EWOULDBLOCK)) {
17783 +                               pr_warn("cannot retry for NFSv4 delegation"
17784 +                                       " for an internal link\n");
17785 +                               iput(delegated);
17786 +                       }
17787 +                       dput(h_src_dentry);
17788 +               } else {
17789 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
17790 +                               h_inode->i_ino, a->bdst);
17791 +                       err = -EIO;
17792 +               }
17793 +       }
17794 +
17795 +       if (!err && !plink)
17796 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
17797 +
17798 +out:
17799 +       AuTraceErr(err);
17800 +       return err;
17801 +}
17802 +
17803 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
17804 +             struct dentry *dentry)
17805 +{
17806 +       int err, rerr;
17807 +       struct au_dtime dt;
17808 +       struct au_link_args *a;
17809 +       struct dentry *wh_dentry, *h_src_dentry;
17810 +       struct inode *inode, *delegated;
17811 +       struct super_block *sb;
17812 +       struct au_wr_dir_args wr_dir_args = {
17813 +               /* .force_btgt  = -1, */
17814 +               .flags          = AuWrDir_ADD_ENTRY
17815 +       };
17816 +
17817 +       IMustLock(dir);
17818 +       inode = src_dentry->d_inode;
17819 +       IMustLock(inode);
17820 +
17821 +       err = -ENOMEM;
17822 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17823 +       if (unlikely(!a))
17824 +               goto out;
17825 +
17826 +       a->parent = dentry->d_parent; /* dir inode is locked */
17827 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
17828 +                                       AuLock_NOPLM | AuLock_GEN);
17829 +       if (unlikely(err))
17830 +               goto out_kfree;
17831 +       err = au_d_linkable(src_dentry);
17832 +       if (unlikely(err))
17833 +               goto out_unlock;
17834 +       err = au_d_may_add(dentry);
17835 +       if (unlikely(err))
17836 +               goto out_unlock;
17837 +
17838 +       a->src_parent = dget_parent(src_dentry);
17839 +       wr_dir_args.force_btgt = au_ibstart(inode);
17840 +
17841 +       di_write_lock_parent(a->parent);
17842 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
17843 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
17844 +                                     &wr_dir_args);
17845 +       err = PTR_ERR(wh_dentry);
17846 +       if (IS_ERR(wh_dentry))
17847 +               goto out_parent;
17848 +
17849 +       err = 0;
17850 +       sb = dentry->d_sb;
17851 +       a->bdst = au_dbstart(dentry);
17852 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
17853 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
17854 +       a->bsrc = au_ibstart(inode);
17855 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
17856 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
17857 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
17858 +       if (!h_src_dentry) {
17859 +               a->bsrc = au_dbstart(src_dentry);
17860 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
17861 +               AuDebugOn(!h_src_dentry);
17862 +       } else if (IS_ERR(h_src_dentry)) {
17863 +               err = PTR_ERR(h_src_dentry);
17864 +               goto out_parent;
17865 +       }
17866 +
17867 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
17868 +               if (a->bdst < a->bsrc
17869 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
17870 +                       err = au_cpup_or_link(src_dentry, dentry, a);
17871 +               else {
17872 +                       delegated = NULL;
17873 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
17874 +                                        &a->h_path, &delegated);
17875 +                       if (unlikely(err == -EWOULDBLOCK)) {
17876 +                               pr_warn("cannot retry for NFSv4 delegation"
17877 +                                       " for an internal link\n");
17878 +                               iput(delegated);
17879 +                       }
17880 +               }
17881 +               dput(h_src_dentry);
17882 +       } else {
17883 +               /*
17884 +                * copyup src_dentry to the branch we process,
17885 +                * and then link(2) to it.
17886 +                */
17887 +               dput(h_src_dentry);
17888 +               if (a->bdst < a->bsrc
17889 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
17890 +                       au_unpin(&a->pin);
17891 +                       di_write_unlock(a->parent);
17892 +                       err = au_cpup_before_link(src_dentry, a);
17893 +                       di_write_lock_parent(a->parent);
17894 +                       if (!err)
17895 +                               err = au_pin(&a->pin, dentry, a->bdst,
17896 +                                            au_opt_udba(sb),
17897 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17898 +                       if (unlikely(err))
17899 +                               goto out_wh;
17900 +               }
17901 +               if (!err) {
17902 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
17903 +                       err = -ENOENT;
17904 +                       if (h_src_dentry && h_src_dentry->d_inode) {
17905 +                               delegated = NULL;
17906 +                               err = vfsub_link(h_src_dentry,
17907 +                                                au_pinned_h_dir(&a->pin),
17908 +                                                &a->h_path, &delegated);
17909 +                               if (unlikely(err == -EWOULDBLOCK)) {
17910 +                                       pr_warn("cannot retry"
17911 +                                               " for NFSv4 delegation"
17912 +                                               " for an internal link\n");
17913 +                                       iput(delegated);
17914 +                               }
17915 +                       }
17916 +               }
17917 +       }
17918 +       if (unlikely(err))
17919 +               goto out_unpin;
17920 +
17921 +       if (wh_dentry) {
17922 +               a->h_path.dentry = wh_dentry;
17923 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
17924 +                                         dentry);
17925 +               if (unlikely(err))
17926 +                       goto out_revert;
17927 +       }
17928 +
17929 +       au_dir_ts(dir, a->bdst);
17930 +       dir->i_version++;
17931 +       inc_nlink(inode);
17932 +       inode->i_ctime = dir->i_ctime;
17933 +       d_instantiate(dentry, au_igrab(inode));
17934 +       if (d_unhashed(a->h_path.dentry))
17935 +               /* some filesystem calls d_drop() */
17936 +               d_drop(dentry);
17937 +       /* some filesystems consume an inode even hardlink */
17938 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
17939 +       goto out_unpin; /* success */
17940 +
17941 +out_revert:
17942 +       /* no delegation since it is just created */
17943 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
17944 +                           /*delegated*/NULL, /*force*/0);
17945 +       if (unlikely(rerr)) {
17946 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
17947 +               err = -EIO;
17948 +       }
17949 +       au_dtime_revert(&dt);
17950 +out_unpin:
17951 +       au_unpin(&a->pin);
17952 +out_wh:
17953 +       dput(wh_dentry);
17954 +out_parent:
17955 +       di_write_unlock(a->parent);
17956 +       dput(a->src_parent);
17957 +out_unlock:
17958 +       if (unlikely(err)) {
17959 +               au_update_dbstart(dentry);
17960 +               d_drop(dentry);
17961 +       }
17962 +       aufs_read_and_write_unlock2(dentry, src_dentry);
17963 +out_kfree:
17964 +       kfree(a);
17965 +out:
17966 +       AuTraceErr(err);
17967 +       return err;
17968 +}
17969 +
17970 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
17971 +{
17972 +       int err, rerr;
17973 +       aufs_bindex_t bindex;
17974 +       unsigned char diropq;
17975 +       struct path h_path;
17976 +       struct dentry *wh_dentry, *parent, *opq_dentry;
17977 +       struct mutex *h_mtx;
17978 +       struct super_block *sb;
17979 +       struct {
17980 +               struct au_pin pin;
17981 +               struct au_dtime dt;
17982 +       } *a; /* reduce the stack usage */
17983 +       struct au_wr_dir_args wr_dir_args = {
17984 +               .force_btgt     = -1,
17985 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
17986 +       };
17987 +
17988 +       IMustLock(dir);
17989 +
17990 +       err = -ENOMEM;
17991 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17992 +       if (unlikely(!a))
17993 +               goto out;
17994 +
17995 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17996 +       if (unlikely(err))
17997 +               goto out_free;
17998 +       err = au_d_may_add(dentry);
17999 +       if (unlikely(err))
18000 +               goto out_unlock;
18001 +
18002 +       parent = dentry->d_parent; /* dir inode is locked */
18003 +       di_write_lock_parent(parent);
18004 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
18005 +                                     &a->pin, &wr_dir_args);
18006 +       err = PTR_ERR(wh_dentry);
18007 +       if (IS_ERR(wh_dentry))
18008 +               goto out_parent;
18009 +
18010 +       sb = dentry->d_sb;
18011 +       bindex = au_dbstart(dentry);
18012 +       h_path.dentry = au_h_dptr(dentry, bindex);
18013 +       h_path.mnt = au_sbr_mnt(sb, bindex);
18014 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
18015 +       if (unlikely(err))
18016 +               goto out_unpin;
18017 +
18018 +       /* make the dir opaque */
18019 +       diropq = 0;
18020 +       h_mtx = &h_path.dentry->d_inode->i_mutex;
18021 +       if (wh_dentry
18022 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
18023 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18024 +               opq_dentry = au_diropq_create(dentry, bindex);
18025 +               mutex_unlock(h_mtx);
18026 +               err = PTR_ERR(opq_dentry);
18027 +               if (IS_ERR(opq_dentry))
18028 +                       goto out_dir;
18029 +               dput(opq_dentry);
18030 +               diropq = 1;
18031 +       }
18032 +
18033 +       err = epilog(dir, bindex, wh_dentry, dentry);
18034 +       if (!err) {
18035 +               inc_nlink(dir);
18036 +               goto out_unpin; /* success */
18037 +       }
18038 +
18039 +       /* revert */
18040 +       if (diropq) {
18041 +               AuLabel(revert opq);
18042 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18043 +               rerr = au_diropq_remove(dentry, bindex);
18044 +               mutex_unlock(h_mtx);
18045 +               if (rerr) {
18046 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
18047 +                               dentry, err, rerr);
18048 +                       err = -EIO;
18049 +               }
18050 +       }
18051 +
18052 +out_dir:
18053 +       AuLabel(revert dir);
18054 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
18055 +       if (rerr) {
18056 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
18057 +                       dentry, err, rerr);
18058 +               err = -EIO;
18059 +       }
18060 +       au_dtime_revert(&a->dt);
18061 +out_unpin:
18062 +       au_unpin(&a->pin);
18063 +       dput(wh_dentry);
18064 +out_parent:
18065 +       di_write_unlock(parent);
18066 +out_unlock:
18067 +       if (unlikely(err)) {
18068 +               au_update_dbstart(dentry);
18069 +               d_drop(dentry);
18070 +       }
18071 +       aufs_read_unlock(dentry, AuLock_DW);
18072 +out_free:
18073 +       kfree(a);
18074 +out:
18075 +       return err;
18076 +}
18077 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
18078 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
18079 +++ linux/fs/aufs/i_op.c        2015-06-22 08:29:23.706757681 +0200
18080 @@ -0,0 +1,1445 @@
18081 +/*
18082 + * Copyright (C) 2005-2015 Junjiro R. Okajima
18083 + *
18084 + * This program, aufs is free software; you can redistribute it and/or modify
18085 + * it under the terms of the GNU General Public License as published by
18086 + * the Free Software Foundation; either version 2 of the License, or
18087 + * (at your option) any later version.
18088 + *
18089 + * This program is distributed in the hope that it will be useful,
18090 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18091 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18092 + * GNU General Public License for more details.
18093 + *
18094 + * You should have received a copy of the GNU General Public License
18095 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18096 + */
18097 +
18098 +/*
18099 + * inode operations (except add/del/rename)
18100 + */
18101 +
18102 +#include <linux/device_cgroup.h>
18103 +#include <linux/fs_stack.h>
18104 +#include <linux/mm.h>
18105 +#include <linux/namei.h>
18106 +#include <linux/security.h>
18107 +#include "aufs.h"
18108 +
18109 +static int h_permission(struct inode *h_inode, int mask,
18110 +                       struct vfsmount *h_mnt, int brperm)
18111 +{
18112 +       int err;
18113 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18114 +
18115 +       err = -EACCES;
18116 +       if ((write_mask && IS_IMMUTABLE(h_inode))
18117 +           || ((mask & MAY_EXEC)
18118 +               && S_ISREG(h_inode->i_mode)
18119 +               && ((h_mnt->mnt_flags & MNT_NOEXEC)
18120 +                   || !(h_inode->i_mode & S_IXUGO))))
18121 +               goto out;
18122 +
18123 +       /*
18124 +        * - skip the lower fs test in the case of write to ro branch.
18125 +        * - nfs dir permission write check is optimized, but a policy for
18126 +        *   link/rename requires a real check.
18127 +        * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
18128 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
18129 +        */
18130 +       if ((write_mask && !au_br_writable(brperm))
18131 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
18132 +               && write_mask && !(mask & MAY_READ))
18133 +           || !h_inode->i_op->permission) {
18134 +               /* AuLabel(generic_permission); */
18135 +               /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
18136 +               err = generic_permission(h_inode, mask);
18137 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
18138 +                       err = h_inode->i_op->permission(h_inode, mask);
18139 +               AuTraceErr(err);
18140 +       } else {
18141 +               /* AuLabel(h_inode->permission); */
18142 +               err = h_inode->i_op->permission(h_inode, mask);
18143 +               AuTraceErr(err);
18144 +       }
18145 +
18146 +       if (!err)
18147 +               err = devcgroup_inode_permission(h_inode, mask);
18148 +       if (!err)
18149 +               err = security_inode_permission(h_inode, mask);
18150 +
18151 +#if 0
18152 +       if (!err) {
18153 +               /* todo: do we need to call ima_path_check()? */
18154 +               struct path h_path = {
18155 +                       .dentry =
18156 +                       .mnt    = h_mnt
18157 +               };
18158 +               err = ima_path_check(&h_path,
18159 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
18160 +                                    IMA_COUNT_LEAVE);
18161 +       }
18162 +#endif
18163 +
18164 +out:
18165 +       return err;
18166 +}
18167 +
18168 +static int aufs_permission(struct inode *inode, int mask)
18169 +{
18170 +       int err;
18171 +       aufs_bindex_t bindex, bend;
18172 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
18173 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18174 +       struct inode *h_inode;
18175 +       struct super_block *sb;
18176 +       struct au_branch *br;
18177 +
18178 +       /* todo: support rcu-walk? */
18179 +       if (mask & MAY_NOT_BLOCK)
18180 +               return -ECHILD;
18181 +
18182 +       sb = inode->i_sb;
18183 +       si_read_lock(sb, AuLock_FLUSH);
18184 +       ii_read_lock_child(inode);
18185 +#if 0
18186 +       err = au_iigen_test(inode, au_sigen(sb));
18187 +       if (unlikely(err))
18188 +               goto out;
18189 +#endif
18190 +
18191 +       if (!isdir
18192 +           || write_mask
18193 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
18194 +               err = au_busy_or_stale();
18195 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
18196 +               if (unlikely(!h_inode
18197 +                            || (h_inode->i_mode & S_IFMT)
18198 +                            != (inode->i_mode & S_IFMT)))
18199 +                       goto out;
18200 +
18201 +               err = 0;
18202 +               bindex = au_ibstart(inode);
18203 +               br = au_sbr(sb, bindex);
18204 +               err = h_permission(h_inode, mask, au_br_mnt(br), br->br_perm);
18205 +               if (write_mask
18206 +                   && !err
18207 +                   && !special_file(h_inode->i_mode)) {
18208 +                       /* test whether the upper writable branch exists */
18209 +                       err = -EROFS;
18210 +                       for (; bindex >= 0; bindex--)
18211 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
18212 +                                       err = 0;
18213 +                                       break;
18214 +                               }
18215 +               }
18216 +               goto out;
18217 +       }
18218 +
18219 +       /* non-write to dir */
18220 +       err = 0;
18221 +       bend = au_ibend(inode);
18222 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
18223 +               h_inode = au_h_iptr(inode, bindex);
18224 +               if (h_inode) {
18225 +                       err = au_busy_or_stale();
18226 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
18227 +                               break;
18228 +
18229 +                       br = au_sbr(sb, bindex);
18230 +                       err = h_permission(h_inode, mask, au_br_mnt(br),
18231 +                                          br->br_perm);
18232 +               }
18233 +       }
18234 +
18235 +out:
18236 +       ii_read_unlock(inode);
18237 +       si_read_unlock(sb);
18238 +       return err;
18239 +}
18240 +
18241 +/* ---------------------------------------------------------------------- */
18242 +
18243 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
18244 +                                 unsigned int flags)
18245 +{
18246 +       struct dentry *ret, *parent;
18247 +       struct inode *inode;
18248 +       struct super_block *sb;
18249 +       int err, npositive;
18250 +
18251 +       IMustLock(dir);
18252 +
18253 +       /* todo: support rcu-walk? */
18254 +       ret = ERR_PTR(-ECHILD);
18255 +       if (flags & LOOKUP_RCU)
18256 +               goto out;
18257 +
18258 +       ret = ERR_PTR(-ENAMETOOLONG);
18259 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
18260 +               goto out;
18261 +
18262 +       sb = dir->i_sb;
18263 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18264 +       ret = ERR_PTR(err);
18265 +       if (unlikely(err))
18266 +               goto out;
18267 +
18268 +       err = au_di_init(dentry);
18269 +       ret = ERR_PTR(err);
18270 +       if (unlikely(err))
18271 +               goto out_si;
18272 +
18273 +       inode = NULL;
18274 +       npositive = 0; /* suppress a warning */
18275 +       parent = dentry->d_parent; /* dir inode is locked */
18276 +       di_read_lock_parent(parent, AuLock_IR);
18277 +       err = au_alive_dir(parent);
18278 +       if (!err)
18279 +               err = au_digen_test(parent, au_sigen(sb));
18280 +       if (!err) {
18281 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
18282 +                                          /*type*/0);
18283 +               err = npositive;
18284 +       }
18285 +       di_read_unlock(parent, AuLock_IR);
18286 +       ret = ERR_PTR(err);
18287 +       if (unlikely(err < 0))
18288 +               goto out_unlock;
18289 +
18290 +       if (npositive) {
18291 +               inode = au_new_inode(dentry, /*must_new*/0);
18292 +               if (IS_ERR(inode)) {
18293 +                       ret = (void *)inode;
18294 +                       inode = NULL;
18295 +                       goto out_unlock;
18296 +               }
18297 +       }
18298 +
18299 +       if (inode)
18300 +               atomic_inc(&inode->i_count);
18301 +       ret = d_splice_alias(inode, dentry);
18302 +#if 0
18303 +       if (unlikely(d_need_lookup(dentry))) {
18304 +               spin_lock(&dentry->d_lock);
18305 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18306 +               spin_unlock(&dentry->d_lock);
18307 +       } else
18308 +#endif
18309 +       if (inode) {
18310 +               if (!IS_ERR(ret)) {
18311 +                       iput(inode);
18312 +                       if (ret && ret != dentry)
18313 +                               ii_write_unlock(inode);
18314 +               } else {
18315 +                       ii_write_unlock(inode);
18316 +                       iput(inode);
18317 +                       inode = NULL;
18318 +               }
18319 +       }
18320 +
18321 +out_unlock:
18322 +       di_write_unlock(dentry);
18323 +       if (inode) {
18324 +               /* verbose coding for lock class name */
18325 +               if (unlikely(S_ISLNK(inode->i_mode)))
18326 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18327 +                                   au_lc_key + AuLcSymlink_DIINFO);
18328 +               else if (unlikely(S_ISDIR(inode->i_mode)))
18329 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18330 +                                   au_lc_key + AuLcDir_DIINFO);
18331 +               else /* likely */
18332 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18333 +                                   au_lc_key + AuLcNonDir_DIINFO);
18334 +       }
18335 +out_si:
18336 +       si_read_unlock(sb);
18337 +out:
18338 +       return ret;
18339 +}
18340 +
18341 +/* ---------------------------------------------------------------------- */
18342 +
18343 +struct aopen_node {
18344 +       struct hlist_node hlist;
18345 +       struct file *file, *h_file;
18346 +};
18347 +
18348 +static int au_do_aopen(struct inode *inode, struct file *file)
18349 +{
18350 +       struct au_sphlhead *aopen;
18351 +       struct aopen_node *node;
18352 +       struct au_do_open_args args = {
18353 +               .no_lock        = 1,
18354 +               .open           = au_do_open_nondir
18355 +       };
18356 +
18357 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
18358 +       spin_lock(&aopen->spin);
18359 +       hlist_for_each_entry(node, &aopen->head, hlist)
18360 +               if (node->file == file) {
18361 +                       args.h_file = node->h_file;
18362 +                       break;
18363 +               }
18364 +       spin_unlock(&aopen->spin);
18365 +       /* AuDebugOn(!args.h_file); */
18366 +
18367 +       return au_do_open(file, &args);
18368 +}
18369 +
18370 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
18371 +                           struct file *file, unsigned int open_flag,
18372 +                           umode_t create_mode, int *opened)
18373 +{
18374 +       int err, h_opened = *opened;
18375 +       struct dentry *parent;
18376 +       struct dentry *d;
18377 +       struct au_sphlhead *aopen;
18378 +       struct vfsub_aopen_args args = {
18379 +               .open_flag      = open_flag,
18380 +               .create_mode    = create_mode,
18381 +               .opened         = &h_opened
18382 +       };
18383 +       struct aopen_node aopen_node = {
18384 +               .file   = file
18385 +       };
18386 +
18387 +       IMustLock(dir);
18388 +       AuDbg("open_flag 0x%x\n", open_flag);
18389 +       AuDbgDentry(dentry);
18390 +
18391 +       err = 0;
18392 +       if (!au_di(dentry)) {
18393 +               d = aufs_lookup(dir, dentry, /*flags*/0);
18394 +               if (IS_ERR(d)) {
18395 +                       err = PTR_ERR(d);
18396 +                       goto out;
18397 +               } else if (d) {
18398 +                       /*
18399 +                        * obsoleted dentry found.
18400 +                        * another error will be returned later.
18401 +                        */
18402 +                       d_drop(d);
18403 +                       dput(d);
18404 +                       AuDbgDentry(d);
18405 +               }
18406 +               AuDbgDentry(dentry);
18407 +       }
18408 +
18409 +       if (d_is_positive(dentry)
18410 +           || d_unhashed(dentry)
18411 +           || d_unlinked(dentry)
18412 +           || !(open_flag & O_CREAT))
18413 +               goto out_no_open;
18414 +
18415 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
18416 +       if (unlikely(err))
18417 +               goto out;
18418 +
18419 +       parent = dentry->d_parent;      /* dir is locked */
18420 +       di_write_lock_parent(parent);
18421 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
18422 +       if (unlikely(err))
18423 +               goto out_unlock;
18424 +
18425 +       AuDbgDentry(dentry);
18426 +       if (d_is_positive(dentry))
18427 +               goto out_unlock;
18428 +
18429 +       args.file = get_empty_filp();
18430 +       err = PTR_ERR(args.file);
18431 +       if (IS_ERR(args.file))
18432 +               goto out_unlock;
18433 +
18434 +       args.file->f_flags = file->f_flags;
18435 +       err = au_aopen_or_create(dir, dentry, &args);
18436 +       AuTraceErr(err);
18437 +       AuDbgFile(args.file);
18438 +       if (unlikely(err < 0)) {
18439 +               if (h_opened & FILE_OPENED)
18440 +                       fput(args.file);
18441 +               else
18442 +                       put_filp(args.file);
18443 +               goto out_unlock;
18444 +       }
18445 +
18446 +       /* some filesystems don't set FILE_CREATED while succeeded? */
18447 +       *opened |= FILE_CREATED;
18448 +       if (h_opened & FILE_OPENED)
18449 +               aopen_node.h_file = args.file;
18450 +       else {
18451 +               put_filp(args.file);
18452 +               args.file = NULL;
18453 +       }
18454 +       aopen = &au_sbi(dir->i_sb)->si_aopen;
18455 +       au_sphl_add(&aopen_node.hlist, aopen);
18456 +       err = finish_open(file, dentry, au_do_aopen, opened);
18457 +       au_sphl_del(&aopen_node.hlist, aopen);
18458 +       AuTraceErr(err);
18459 +       AuDbgFile(file);
18460 +       if (aopen_node.h_file)
18461 +               fput(aopen_node.h_file);
18462 +
18463 +out_unlock:
18464 +       di_write_unlock(parent);
18465 +       aufs_read_unlock(dentry, AuLock_DW);
18466 +       AuDbgDentry(dentry);
18467 +       if (unlikely(err))
18468 +               goto out;
18469 +out_no_open:
18470 +       if (!err && !(*opened & FILE_CREATED)) {
18471 +               AuLabel(out_no_open);
18472 +               dget(dentry);
18473 +               err = finish_no_open(file, dentry);
18474 +       }
18475 +out:
18476 +       AuDbg("%pd%s%s\n", dentry,
18477 +             (*opened & FILE_CREATED) ? " created" : "",
18478 +             (*opened & FILE_OPENED) ? " opened" : "");
18479 +       AuTraceErr(err);
18480 +       return err;
18481 +}
18482 +
18483 +
18484 +/* ---------------------------------------------------------------------- */
18485 +
18486 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
18487 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
18488 +                         aufs_bindex_t bstart)
18489 +{
18490 +       int err;
18491 +       struct dentry *h_parent;
18492 +       struct inode *h_dir;
18493 +
18494 +       if (add_entry)
18495 +               IMustLock(parent->d_inode);
18496 +       else
18497 +               di_write_lock_parent(parent);
18498 +
18499 +       err = 0;
18500 +       if (!au_h_dptr(parent, bcpup)) {
18501 +               if (bstart > bcpup)
18502 +                       err = au_cpup_dirs(dentry, bcpup);
18503 +               else if (bstart < bcpup)
18504 +                       err = au_cpdown_dirs(dentry, bcpup);
18505 +               else
18506 +                       BUG();
18507 +       }
18508 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
18509 +               h_parent = au_h_dptr(parent, bcpup);
18510 +               h_dir = h_parent->d_inode;
18511 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
18512 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
18513 +               /* todo: no unlock here */
18514 +               mutex_unlock(&h_dir->i_mutex);
18515 +
18516 +               AuDbg("bcpup %d\n", bcpup);
18517 +               if (!err) {
18518 +                       if (!dentry->d_inode)
18519 +                               au_set_h_dptr(dentry, bstart, NULL);
18520 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
18521 +               }
18522 +       }
18523 +
18524 +       if (!add_entry)
18525 +               di_write_unlock(parent);
18526 +       if (!err)
18527 +               err = bcpup; /* success */
18528 +
18529 +       AuTraceErr(err);
18530 +       return err;
18531 +}
18532 +
18533 +/*
18534 + * decide the branch and the parent dir where we will create a new entry.
18535 + * returns new bindex or an error.
18536 + * copyup the parent dir if needed.
18537 + */
18538 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18539 +             struct au_wr_dir_args *args)
18540 +{
18541 +       int err;
18542 +       unsigned int flags;
18543 +       aufs_bindex_t bcpup, bstart, src_bstart;
18544 +       const unsigned char add_entry
18545 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
18546 +               | au_ftest_wrdir(args->flags, TMPFILE);
18547 +       struct super_block *sb;
18548 +       struct dentry *parent;
18549 +       struct au_sbinfo *sbinfo;
18550 +
18551 +       sb = dentry->d_sb;
18552 +       sbinfo = au_sbi(sb);
18553 +       parent = dget_parent(dentry);
18554 +       bstart = au_dbstart(dentry);
18555 +       bcpup = bstart;
18556 +       if (args->force_btgt < 0) {
18557 +               if (src_dentry) {
18558 +                       src_bstart = au_dbstart(src_dentry);
18559 +                       if (src_bstart < bstart)
18560 +                               bcpup = src_bstart;
18561 +               } else if (add_entry) {
18562 +                       flags = 0;
18563 +                       if (au_ftest_wrdir(args->flags, ISDIR))
18564 +                               au_fset_wbr(flags, DIR);
18565 +                       err = AuWbrCreate(sbinfo, dentry, flags);
18566 +                       bcpup = err;
18567 +               }
18568 +
18569 +               if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
18570 +                       if (add_entry)
18571 +                               err = AuWbrCopyup(sbinfo, dentry);
18572 +                       else {
18573 +                               if (!IS_ROOT(dentry)) {
18574 +                                       di_read_lock_parent(parent, !AuLock_IR);
18575 +                                       err = AuWbrCopyup(sbinfo, dentry);
18576 +                                       di_read_unlock(parent, !AuLock_IR);
18577 +                               } else
18578 +                                       err = AuWbrCopyup(sbinfo, dentry);
18579 +                       }
18580 +                       bcpup = err;
18581 +                       if (unlikely(err < 0))
18582 +                               goto out;
18583 +               }
18584 +       } else {
18585 +               bcpup = args->force_btgt;
18586 +               AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
18587 +       }
18588 +
18589 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
18590 +       err = bcpup;
18591 +       if (bcpup == bstart)
18592 +               goto out; /* success */
18593 +
18594 +       /* copyup the new parent into the branch we process */
18595 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
18596 +       if (err >= 0) {
18597 +               if (!dentry->d_inode) {
18598 +                       au_set_h_dptr(dentry, bstart, NULL);
18599 +                       au_set_dbstart(dentry, bcpup);
18600 +                       au_set_dbend(dentry, bcpup);
18601 +               }
18602 +               AuDebugOn(add_entry
18603 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
18604 +                         && !au_h_dptr(dentry, bcpup));
18605 +       }
18606 +
18607 +out:
18608 +       dput(parent);
18609 +       return err;
18610 +}
18611 +
18612 +/* ---------------------------------------------------------------------- */
18613 +
18614 +void au_pin_hdir_unlock(struct au_pin *p)
18615 +{
18616 +       if (p->hdir)
18617 +               au_hn_imtx_unlock(p->hdir);
18618 +}
18619 +
18620 +int au_pin_hdir_lock(struct au_pin *p)
18621 +{
18622 +       int err;
18623 +
18624 +       err = 0;
18625 +       if (!p->hdir)
18626 +               goto out;
18627 +
18628 +       /* even if an error happens later, keep this lock */
18629 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
18630 +
18631 +       err = -EBUSY;
18632 +       if (unlikely(p->hdir->hi_inode != p->h_parent->d_inode))
18633 +               goto out;
18634 +
18635 +       err = 0;
18636 +       if (p->h_dentry)
18637 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
18638 +                                 p->h_parent, p->br);
18639 +
18640 +out:
18641 +       return err;
18642 +}
18643 +
18644 +int au_pin_hdir_relock(struct au_pin *p)
18645 +{
18646 +       int err, i;
18647 +       struct inode *h_i;
18648 +       struct dentry *h_d[] = {
18649 +               p->h_dentry,
18650 +               p->h_parent
18651 +       };
18652 +
18653 +       err = au_pin_hdir_lock(p);
18654 +       if (unlikely(err))
18655 +               goto out;
18656 +
18657 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
18658 +               if (!h_d[i])
18659 +                       continue;
18660 +               h_i = h_d[i]->d_inode;
18661 +               if (h_i)
18662 +                       err = !h_i->i_nlink;
18663 +       }
18664 +
18665 +out:
18666 +       return err;
18667 +}
18668 +
18669 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
18670 +{
18671 +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
18672 +       p->hdir->hi_inode->i_mutex.owner = task;
18673 +#endif
18674 +}
18675 +
18676 +void au_pin_hdir_acquire_nest(struct au_pin *p)
18677 +{
18678 +       if (p->hdir) {
18679 +               mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
18680 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
18681 +               au_pin_hdir_set_owner(p, current);
18682 +       }
18683 +}
18684 +
18685 +void au_pin_hdir_release(struct au_pin *p)
18686 +{
18687 +       if (p->hdir) {
18688 +               au_pin_hdir_set_owner(p, p->task);
18689 +               mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
18690 +       }
18691 +}
18692 +
18693 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
18694 +{
18695 +       if (pin && pin->parent)
18696 +               return au_h_dptr(pin->parent, pin->bindex);
18697 +       return NULL;
18698 +}
18699 +
18700 +void au_unpin(struct au_pin *p)
18701 +{
18702 +       if (p->hdir)
18703 +               au_pin_hdir_unlock(p);
18704 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
18705 +               vfsub_mnt_drop_write(p->h_mnt);
18706 +       if (!p->hdir)
18707 +               return;
18708 +
18709 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18710 +               di_read_unlock(p->parent, AuLock_IR);
18711 +       iput(p->hdir->hi_inode);
18712 +       dput(p->parent);
18713 +       p->parent = NULL;
18714 +       p->hdir = NULL;
18715 +       p->h_mnt = NULL;
18716 +       /* do not clear p->task */
18717 +}
18718 +
18719 +int au_do_pin(struct au_pin *p)
18720 +{
18721 +       int err;
18722 +       struct super_block *sb;
18723 +       struct inode *h_dir;
18724 +
18725 +       err = 0;
18726 +       sb = p->dentry->d_sb;
18727 +       p->br = au_sbr(sb, p->bindex);
18728 +       if (IS_ROOT(p->dentry)) {
18729 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
18730 +                       p->h_mnt = au_br_mnt(p->br);
18731 +                       err = vfsub_mnt_want_write(p->h_mnt);
18732 +                       if (unlikely(err)) {
18733 +                               au_fclr_pin(p->flags, MNT_WRITE);
18734 +                               goto out_err;
18735 +                       }
18736 +               }
18737 +               goto out;
18738 +       }
18739 +
18740 +       p->h_dentry = NULL;
18741 +       if (p->bindex <= au_dbend(p->dentry))
18742 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
18743 +
18744 +       p->parent = dget_parent(p->dentry);
18745 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18746 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
18747 +
18748 +       h_dir = NULL;
18749 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
18750 +       p->hdir = au_hi(p->parent->d_inode, p->bindex);
18751 +       if (p->hdir)
18752 +               h_dir = p->hdir->hi_inode;
18753 +
18754 +       /*
18755 +        * udba case, or
18756 +        * if DI_LOCKED is not set, then p->parent may be different
18757 +        * and h_parent can be NULL.
18758 +        */
18759 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
18760 +               err = -EBUSY;
18761 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
18762 +                       di_read_unlock(p->parent, AuLock_IR);
18763 +               dput(p->parent);
18764 +               p->parent = NULL;
18765 +               goto out_err;
18766 +       }
18767 +
18768 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
18769 +               p->h_mnt = au_br_mnt(p->br);
18770 +               err = vfsub_mnt_want_write(p->h_mnt);
18771 +               if (unlikely(err)) {
18772 +                       au_fclr_pin(p->flags, MNT_WRITE);
18773 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
18774 +                               di_read_unlock(p->parent, AuLock_IR);
18775 +                       dput(p->parent);
18776 +                       p->parent = NULL;
18777 +                       goto out_err;
18778 +               }
18779 +       }
18780 +
18781 +       au_igrab(h_dir);
18782 +       err = au_pin_hdir_lock(p);
18783 +       if (!err)
18784 +               goto out; /* success */
18785 +
18786 +       au_unpin(p);
18787 +
18788 +out_err:
18789 +       pr_err("err %d\n", err);
18790 +       err = au_busy_or_stale();
18791 +out:
18792 +       return err;
18793 +}
18794 +
18795 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
18796 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18797 +                unsigned int udba, unsigned char flags)
18798 +{
18799 +       p->dentry = dentry;
18800 +       p->udba = udba;
18801 +       p->lsc_di = lsc_di;
18802 +       p->lsc_hi = lsc_hi;
18803 +       p->flags = flags;
18804 +       p->bindex = bindex;
18805 +
18806 +       p->parent = NULL;
18807 +       p->hdir = NULL;
18808 +       p->h_mnt = NULL;
18809 +
18810 +       p->h_dentry = NULL;
18811 +       p->h_parent = NULL;
18812 +       p->br = NULL;
18813 +       p->task = current;
18814 +}
18815 +
18816 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18817 +          unsigned int udba, unsigned char flags)
18818 +{
18819 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
18820 +                   udba, flags);
18821 +       return au_do_pin(pin);
18822 +}
18823 +
18824 +/* ---------------------------------------------------------------------- */
18825 +
18826 +/*
18827 + * ->setattr() and ->getattr() are called in various cases.
18828 + * chmod, stat: dentry is revalidated.
18829 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
18830 + *               unhashed.
18831 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
18832 + */
18833 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
18834 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
18835 +{
18836 +       int err;
18837 +       struct inode *inode;
18838 +       struct dentry *parent;
18839 +
18840 +       err = 0;
18841 +       inode = dentry->d_inode;
18842 +       if (au_digen_test(dentry, sigen)) {
18843 +               parent = dget_parent(dentry);
18844 +               di_read_lock_parent(parent, AuLock_IR);
18845 +               err = au_refresh_dentry(dentry, parent);
18846 +               di_read_unlock(parent, AuLock_IR);
18847 +               dput(parent);
18848 +       }
18849 +
18850 +       AuTraceErr(err);
18851 +       return err;
18852 +}
18853 +
18854 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18855 +                    struct au_icpup_args *a)
18856 +{
18857 +       int err;
18858 +       loff_t sz;
18859 +       aufs_bindex_t bstart, ibstart;
18860 +       struct dentry *hi_wh, *parent;
18861 +       struct inode *inode;
18862 +       struct au_wr_dir_args wr_dir_args = {
18863 +               .force_btgt     = -1,
18864 +               .flags          = 0
18865 +       };
18866 +
18867 +       if (d_is_dir(dentry))
18868 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
18869 +       /* plink or hi_wh() case */
18870 +       bstart = au_dbstart(dentry);
18871 +       inode = dentry->d_inode;
18872 +       ibstart = au_ibstart(inode);
18873 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
18874 +               wr_dir_args.force_btgt = ibstart;
18875 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
18876 +       if (unlikely(err < 0))
18877 +               goto out;
18878 +       a->btgt = err;
18879 +       if (err != bstart)
18880 +               au_fset_icpup(a->flags, DID_CPUP);
18881 +
18882 +       err = 0;
18883 +       a->pin_flags = AuPin_MNT_WRITE;
18884 +       parent = NULL;
18885 +       if (!IS_ROOT(dentry)) {
18886 +               au_fset_pin(a->pin_flags, DI_LOCKED);
18887 +               parent = dget_parent(dentry);
18888 +               di_write_lock_parent(parent);
18889 +       }
18890 +
18891 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
18892 +       if (unlikely(err))
18893 +               goto out_parent;
18894 +
18895 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
18896 +       a->h_inode = a->h_path.dentry->d_inode;
18897 +       sz = -1;
18898 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
18899 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
18900 +               if (ia->ia_size < i_size_read(a->h_inode))
18901 +                       sz = ia->ia_size;
18902 +               mutex_unlock(&a->h_inode->i_mutex);
18903 +       }
18904 +
18905 +       hi_wh = NULL;
18906 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
18907 +               hi_wh = au_hi_wh(inode, a->btgt);
18908 +               if (!hi_wh) {
18909 +                       struct au_cp_generic cpg = {
18910 +                               .dentry = dentry,
18911 +                               .bdst   = a->btgt,
18912 +                               .bsrc   = -1,
18913 +                               .len    = sz,
18914 +                               .pin    = &a->pin
18915 +                       };
18916 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
18917 +                       if (unlikely(err))
18918 +                               goto out_unlock;
18919 +                       hi_wh = au_hi_wh(inode, a->btgt);
18920 +                       /* todo: revalidate hi_wh? */
18921 +               }
18922 +       }
18923 +
18924 +       if (parent) {
18925 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
18926 +               di_downgrade_lock(parent, AuLock_IR);
18927 +               dput(parent);
18928 +               parent = NULL;
18929 +       }
18930 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
18931 +               goto out; /* success */
18932 +
18933 +       if (!d_unhashed(dentry)) {
18934 +               struct au_cp_generic cpg = {
18935 +                       .dentry = dentry,
18936 +                       .bdst   = a->btgt,
18937 +                       .bsrc   = bstart,
18938 +                       .len    = sz,
18939 +                       .pin    = &a->pin,
18940 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
18941 +               };
18942 +               err = au_sio_cpup_simple(&cpg);
18943 +               if (!err)
18944 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
18945 +       } else if (!hi_wh)
18946 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
18947 +       else
18948 +               a->h_path.dentry = hi_wh; /* do not dget here */
18949 +
18950 +out_unlock:
18951 +       a->h_inode = a->h_path.dentry->d_inode;
18952 +       if (!err)
18953 +               goto out; /* success */
18954 +       au_unpin(&a->pin);
18955 +out_parent:
18956 +       if (parent) {
18957 +               di_write_unlock(parent);
18958 +               dput(parent);
18959 +       }
18960 +out:
18961 +       if (!err)
18962 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
18963 +       return err;
18964 +}
18965 +
18966 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
18967 +{
18968 +       int err;
18969 +       struct inode *inode, *delegated;
18970 +       struct super_block *sb;
18971 +       struct file *file;
18972 +       struct au_icpup_args *a;
18973 +
18974 +       inode = dentry->d_inode;
18975 +       IMustLock(inode);
18976 +
18977 +       err = -ENOMEM;
18978 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18979 +       if (unlikely(!a))
18980 +               goto out;
18981 +
18982 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
18983 +               ia->ia_valid &= ~ATTR_MODE;
18984 +
18985 +       file = NULL;
18986 +       sb = dentry->d_sb;
18987 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18988 +       if (unlikely(err))
18989 +               goto out_kfree;
18990 +
18991 +       if (ia->ia_valid & ATTR_FILE) {
18992 +               /* currently ftruncate(2) only */
18993 +               AuDebugOn(!d_is_reg(dentry));
18994 +               file = ia->ia_file;
18995 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
18996 +               if (unlikely(err))
18997 +                       goto out_si;
18998 +               ia->ia_file = au_hf_top(file);
18999 +               a->udba = AuOpt_UDBA_NONE;
19000 +       } else {
19001 +               /* fchmod() doesn't pass ia_file */
19002 +               a->udba = au_opt_udba(sb);
19003 +               di_write_lock_child(dentry);
19004 +               /* no d_unlinked(), to set UDBA_NONE for root */
19005 +               if (d_unhashed(dentry))
19006 +                       a->udba = AuOpt_UDBA_NONE;
19007 +               if (a->udba != AuOpt_UDBA_NONE) {
19008 +                       AuDebugOn(IS_ROOT(dentry));
19009 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
19010 +                       if (unlikely(err))
19011 +                               goto out_dentry;
19012 +               }
19013 +       }
19014 +
19015 +       err = au_pin_and_icpup(dentry, ia, a);
19016 +       if (unlikely(err < 0))
19017 +               goto out_dentry;
19018 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
19019 +               ia->ia_file = NULL;
19020 +               ia->ia_valid &= ~ATTR_FILE;
19021 +       }
19022 +
19023 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
19024 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
19025 +           == (ATTR_MODE | ATTR_CTIME)) {
19026 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
19027 +               if (unlikely(err))
19028 +                       goto out_unlock;
19029 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
19030 +                  && (ia->ia_valid & ATTR_CTIME)) {
19031 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
19032 +               if (unlikely(err))
19033 +                       goto out_unlock;
19034 +       }
19035 +
19036 +       if (ia->ia_valid & ATTR_SIZE) {
19037 +               struct file *f;
19038 +
19039 +               if (ia->ia_size < i_size_read(inode))
19040 +                       /* unmap only */
19041 +                       truncate_setsize(inode, ia->ia_size);
19042 +
19043 +               f = NULL;
19044 +               if (ia->ia_valid & ATTR_FILE)
19045 +                       f = ia->ia_file;
19046 +               mutex_unlock(&a->h_inode->i_mutex);
19047 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
19048 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
19049 +       } else {
19050 +               delegated = NULL;
19051 +               while (1) {
19052 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
19053 +                       if (delegated) {
19054 +                               err = break_deleg_wait(&delegated);
19055 +                               if (!err)
19056 +                                       continue;
19057 +                       }
19058 +                       break;
19059 +               }
19060 +       }
19061 +       if (!err)
19062 +               au_cpup_attr_changeable(inode);
19063 +
19064 +out_unlock:
19065 +       mutex_unlock(&a->h_inode->i_mutex);
19066 +       au_unpin(&a->pin);
19067 +       if (unlikely(err))
19068 +               au_update_dbstart(dentry);
19069 +out_dentry:
19070 +       di_write_unlock(dentry);
19071 +       if (file) {
19072 +               fi_write_unlock(file);
19073 +               ia->ia_file = file;
19074 +               ia->ia_valid |= ATTR_FILE;
19075 +       }
19076 +out_si:
19077 +       si_read_unlock(sb);
19078 +out_kfree:
19079 +       kfree(a);
19080 +out:
19081 +       AuTraceErr(err);
19082 +       return err;
19083 +}
19084 +
19085 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19086 +static int au_h_path_to_set_attr(struct dentry *dentry,
19087 +                                struct au_icpup_args *a, struct path *h_path)
19088 +{
19089 +       int err;
19090 +       struct super_block *sb;
19091 +
19092 +       sb = dentry->d_sb;
19093 +       a->udba = au_opt_udba(sb);
19094 +       /* no d_unlinked(), to set UDBA_NONE for root */
19095 +       if (d_unhashed(dentry))
19096 +               a->udba = AuOpt_UDBA_NONE;
19097 +       if (a->udba != AuOpt_UDBA_NONE) {
19098 +               AuDebugOn(IS_ROOT(dentry));
19099 +               err = au_reval_for_attr(dentry, au_sigen(sb));
19100 +               if (unlikely(err))
19101 +                       goto out;
19102 +       }
19103 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
19104 +       if (unlikely(err < 0))
19105 +               goto out;
19106 +
19107 +       h_path->dentry = a->h_path.dentry;
19108 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
19109 +
19110 +out:
19111 +       return err;
19112 +}
19113 +
19114 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg)
19115 +{
19116 +       int err;
19117 +       struct path h_path;
19118 +       struct super_block *sb;
19119 +       struct au_icpup_args *a;
19120 +       struct inode *inode, *h_inode;
19121 +
19122 +       inode = dentry->d_inode;
19123 +       IMustLock(inode);
19124 +
19125 +       err = -ENOMEM;
19126 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19127 +       if (unlikely(!a))
19128 +               goto out;
19129 +
19130 +       sb = dentry->d_sb;
19131 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19132 +       if (unlikely(err))
19133 +               goto out_kfree;
19134 +
19135 +       h_path.dentry = NULL;   /* silence gcc */
19136 +       di_write_lock_child(dentry);
19137 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
19138 +       if (unlikely(err))
19139 +               goto out_di;
19140 +
19141 +       mutex_unlock(&a->h_inode->i_mutex);
19142 +       switch (arg->type) {
19143 +       case AU_XATTR_SET:
19144 +               err = vfsub_setxattr(h_path.dentry,
19145 +                                    arg->u.set.name, arg->u.set.value,
19146 +                                    arg->u.set.size, arg->u.set.flags);
19147 +               break;
19148 +       case AU_XATTR_REMOVE:
19149 +               err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
19150 +               break;
19151 +       case AU_ACL_SET:
19152 +               err = -EOPNOTSUPP;
19153 +               h_inode = h_path.dentry->d_inode;
19154 +               if (h_inode->i_op->set_acl)
19155 +                       err = h_inode->i_op->set_acl(h_inode,
19156 +                                                    arg->u.acl_set.acl,
19157 +                                                    arg->u.acl_set.type);
19158 +               break;
19159 +       }
19160 +       if (!err)
19161 +               au_cpup_attr_timesizes(inode);
19162 +
19163 +       au_unpin(&a->pin);
19164 +       if (unlikely(err))
19165 +               au_update_dbstart(dentry);
19166 +
19167 +out_di:
19168 +       di_write_unlock(dentry);
19169 +       si_read_unlock(sb);
19170 +out_kfree:
19171 +       kfree(a);
19172 +out:
19173 +       AuTraceErr(err);
19174 +       return err;
19175 +}
19176 +#endif
19177 +
19178 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
19179 +                            unsigned int nlink)
19180 +{
19181 +       unsigned int n;
19182 +
19183 +       inode->i_mode = st->mode;
19184 +       /* don't i_[ug]id_write() here */
19185 +       inode->i_uid = st->uid;
19186 +       inode->i_gid = st->gid;
19187 +       inode->i_atime = st->atime;
19188 +       inode->i_mtime = st->mtime;
19189 +       inode->i_ctime = st->ctime;
19190 +
19191 +       au_cpup_attr_nlink(inode, /*force*/0);
19192 +       if (S_ISDIR(inode->i_mode)) {
19193 +               n = inode->i_nlink;
19194 +               n -= nlink;
19195 +               n += st->nlink;
19196 +               smp_mb(); /* for i_nlink */
19197 +               /* 0 can happen */
19198 +               set_nlink(inode, n);
19199 +       }
19200 +
19201 +       spin_lock(&inode->i_lock);
19202 +       inode->i_blocks = st->blocks;
19203 +       i_size_write(inode, st->size);
19204 +       spin_unlock(&inode->i_lock);
19205 +}
19206 +
19207 +/*
19208 + * common routine for aufs_getattr() and aufs_getxattr().
19209 + * returns zero or negative (an error).
19210 + * @dentry will be read-locked in success.
19211 + */
19212 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
19213 +{
19214 +       int err;
19215 +       unsigned int mnt_flags, sigen;
19216 +       unsigned char udba_none;
19217 +       aufs_bindex_t bindex;
19218 +       struct super_block *sb, *h_sb;
19219 +       struct inode *inode;
19220 +
19221 +       h_path->mnt = NULL;
19222 +       h_path->dentry = NULL;
19223 +
19224 +       err = 0;
19225 +       sb = dentry->d_sb;
19226 +       mnt_flags = au_mntflags(sb);
19227 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
19228 +
19229 +       /* support fstat(2) */
19230 +       if (!d_unlinked(dentry) && !udba_none) {
19231 +               sigen = au_sigen(sb);
19232 +               err = au_digen_test(dentry, sigen);
19233 +               if (!err) {
19234 +                       di_read_lock_child(dentry, AuLock_IR);
19235 +                       err = au_dbrange_test(dentry);
19236 +                       if (unlikely(err)) {
19237 +                               di_read_unlock(dentry, AuLock_IR);
19238 +                               goto out;
19239 +                       }
19240 +               } else {
19241 +                       AuDebugOn(IS_ROOT(dentry));
19242 +                       di_write_lock_child(dentry);
19243 +                       err = au_dbrange_test(dentry);
19244 +                       if (!err)
19245 +                               err = au_reval_for_attr(dentry, sigen);
19246 +                       if (!err)
19247 +                               di_downgrade_lock(dentry, AuLock_IR);
19248 +                       else {
19249 +                               di_write_unlock(dentry);
19250 +                               goto out;
19251 +                       }
19252 +               }
19253 +       } else
19254 +               di_read_lock_child(dentry, AuLock_IR);
19255 +
19256 +       inode = dentry->d_inode;
19257 +       bindex = au_ibstart(inode);
19258 +       h_path->mnt = au_sbr_mnt(sb, bindex);
19259 +       h_sb = h_path->mnt->mnt_sb;
19260 +       if (!force
19261 +           && !au_test_fs_bad_iattr(h_sb)
19262 +           && udba_none)
19263 +               goto out; /* success */
19264 +
19265 +       if (au_dbstart(dentry) == bindex)
19266 +               h_path->dentry = au_h_dptr(dentry, bindex);
19267 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
19268 +               h_path->dentry = au_plink_lkup(inode, bindex);
19269 +               if (IS_ERR(h_path->dentry))
19270 +                       /* pretending success */
19271 +                       h_path->dentry = NULL;
19272 +               else
19273 +                       dput(h_path->dentry);
19274 +       }
19275 +
19276 +out:
19277 +       return err;
19278 +}
19279 +
19280 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
19281 +                       struct dentry *dentry, struct kstat *st)
19282 +{
19283 +       int err;
19284 +       unsigned char positive;
19285 +       struct path h_path;
19286 +       struct inode *inode;
19287 +       struct super_block *sb;
19288 +
19289 +       inode = dentry->d_inode;
19290 +       sb = dentry->d_sb;
19291 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19292 +       if (unlikely(err))
19293 +               goto out;
19294 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path);
19295 +       if (unlikely(err))
19296 +               goto out_si;
19297 +       if (unlikely(!h_path.dentry))
19298 +               /* illegally overlapped or something */
19299 +               goto out_fill; /* pretending success */
19300 +
19301 +       positive = !!h_path.dentry->d_inode;
19302 +       if (positive)
19303 +               err = vfs_getattr(&h_path, st);
19304 +       if (!err) {
19305 +               if (positive)
19306 +                       au_refresh_iattr(inode, st,
19307 +                                        h_path.dentry->d_inode->i_nlink);
19308 +               goto out_fill; /* success */
19309 +       }
19310 +       AuTraceErr(err);
19311 +       goto out_di;
19312 +
19313 +out_fill:
19314 +       generic_fillattr(inode, st);
19315 +out_di:
19316 +       di_read_unlock(dentry, AuLock_IR);
19317 +out_si:
19318 +       si_read_unlock(sb);
19319 +out:
19320 +       AuTraceErr(err);
19321 +       return err;
19322 +}
19323 +
19324 +/* ---------------------------------------------------------------------- */
19325 +
19326 +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
19327 +                     int bufsiz)
19328 +{
19329 +       int err;
19330 +       struct super_block *sb;
19331 +       struct dentry *h_dentry;
19332 +
19333 +       err = -EINVAL;
19334 +       h_dentry = au_h_dptr(dentry, bindex);
19335 +       if (unlikely(!h_dentry->d_inode->i_op->readlink))
19336 +               goto out;
19337 +
19338 +       err = security_inode_readlink(h_dentry);
19339 +       if (unlikely(err))
19340 +               goto out;
19341 +
19342 +       sb = dentry->d_sb;
19343 +       if (!au_test_ro(sb, bindex, dentry->d_inode)) {
19344 +               vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
19345 +               fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
19346 +       }
19347 +       err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
19348 +
19349 +out:
19350 +       return err;
19351 +}
19352 +
19353 +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
19354 +{
19355 +       int err;
19356 +
19357 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
19358 +       if (unlikely(err))
19359 +               goto out;
19360 +       err = au_d_hashed_positive(dentry);
19361 +       if (!err)
19362 +               err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
19363 +       aufs_read_unlock(dentry, AuLock_IR);
19364 +
19365 +out:
19366 +       return err;
19367 +}
19368 +
19369 +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
19370 +{
19371 +       int err;
19372 +       mm_segment_t old_fs;
19373 +       union {
19374 +               char *k;
19375 +               char __user *u;
19376 +       } buf;
19377 +
19378 +       err = -ENOMEM;
19379 +       buf.k = (void *)__get_free_page(GFP_NOFS);
19380 +       if (unlikely(!buf.k))
19381 +               goto out;
19382 +
19383 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
19384 +       if (unlikely(err))
19385 +               goto out_name;
19386 +
19387 +       err = au_d_hashed_positive(dentry);
19388 +       if (!err) {
19389 +               old_fs = get_fs();
19390 +               set_fs(KERNEL_DS);
19391 +               err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
19392 +               set_fs(old_fs);
19393 +       }
19394 +       aufs_read_unlock(dentry, AuLock_IR);
19395 +
19396 +       if (err >= 0) {
19397 +               buf.k[err] = 0;
19398 +               /* will be freed by put_link */
19399 +               nd_set_link(nd, buf.k);
19400 +               return NULL; /* success */
19401 +       }
19402 +
19403 +out_name:
19404 +       free_page((unsigned long)buf.k);
19405 +out:
19406 +       AuTraceErr(err);
19407 +       return ERR_PTR(err);
19408 +}
19409 +
19410 +static void aufs_put_link(struct dentry *dentry __maybe_unused,
19411 +                         struct nameidata *nd, void *cookie __maybe_unused)
19412 +{
19413 +       char *p;
19414 +
19415 +       p = nd_get_link(nd);
19416 +       if (!IS_ERR_OR_NULL(p))
19417 +               free_page((unsigned long)p);
19418 +}
19419 +
19420 +/* ---------------------------------------------------------------------- */
19421 +
19422 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
19423 +{
19424 +       int err;
19425 +       struct super_block *sb;
19426 +       struct inode *h_inode;
19427 +
19428 +       sb = inode->i_sb;
19429 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
19430 +       lockdep_off();
19431 +       si_read_lock(sb, AuLock_FLUSH);
19432 +       ii_write_lock_child(inode);
19433 +       lockdep_on();
19434 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
19435 +       err = vfsub_update_time(h_inode, ts, flags);
19436 +       lockdep_off();
19437 +       if (!err)
19438 +               au_cpup_attr_timesizes(inode);
19439 +       ii_write_unlock(inode);
19440 +       si_read_unlock(sb);
19441 +       lockdep_on();
19442 +
19443 +       if (!err && (flags & S_VERSION))
19444 +               inode_inc_iversion(inode);
19445 +
19446 +       return err;
19447 +}
19448 +
19449 +/* ---------------------------------------------------------------------- */
19450 +
19451 +struct inode_operations aufs_symlink_iop = {
19452 +       .permission     = aufs_permission,
19453 +#ifdef CONFIG_FS_POSIX_ACL
19454 +       .get_acl        = aufs_get_acl,
19455 +       .set_acl        = aufs_set_acl, /* unsupport for symlink? */
19456 +#endif
19457 +
19458 +       .setattr        = aufs_setattr,
19459 +       .getattr        = aufs_getattr,
19460 +
19461 +#ifdef CONFIG_AUFS_XATTR
19462 +       .setxattr       = aufs_setxattr,
19463 +       .getxattr       = aufs_getxattr,
19464 +       .listxattr      = aufs_listxattr,
19465 +       .removexattr    = aufs_removexattr,
19466 +#endif
19467 +
19468 +       .readlink       = aufs_readlink,
19469 +       .follow_link    = aufs_follow_link,
19470 +       .put_link       = aufs_put_link,
19471 +
19472 +       /* .update_time = aufs_update_time */
19473 +};
19474 +
19475 +struct inode_operations aufs_dir_iop = {
19476 +       .create         = aufs_create,
19477 +       .lookup         = aufs_lookup,
19478 +       .link           = aufs_link,
19479 +       .unlink         = aufs_unlink,
19480 +       .symlink        = aufs_symlink,
19481 +       .mkdir          = aufs_mkdir,
19482 +       .rmdir          = aufs_rmdir,
19483 +       .mknod          = aufs_mknod,
19484 +       .rename         = aufs_rename,
19485 +
19486 +       .permission     = aufs_permission,
19487 +#ifdef CONFIG_FS_POSIX_ACL
19488 +       .get_acl        = aufs_get_acl,
19489 +       .set_acl        = aufs_set_acl,
19490 +#endif
19491 +
19492 +       .setattr        = aufs_setattr,
19493 +       .getattr        = aufs_getattr,
19494 +
19495 +#ifdef CONFIG_AUFS_XATTR
19496 +       .setxattr       = aufs_setxattr,
19497 +       .getxattr       = aufs_getxattr,
19498 +       .listxattr      = aufs_listxattr,
19499 +       .removexattr    = aufs_removexattr,
19500 +#endif
19501 +
19502 +       .update_time    = aufs_update_time,
19503 +       .atomic_open    = aufs_atomic_open,
19504 +       .tmpfile        = aufs_tmpfile
19505 +};
19506 +
19507 +struct inode_operations aufs_iop = {
19508 +       .permission     = aufs_permission,
19509 +#ifdef CONFIG_FS_POSIX_ACL
19510 +       .get_acl        = aufs_get_acl,
19511 +       .set_acl        = aufs_set_acl,
19512 +#endif
19513 +
19514 +       .setattr        = aufs_setattr,
19515 +       .getattr        = aufs_getattr,
19516 +
19517 +#ifdef CONFIG_AUFS_XATTR
19518 +       .setxattr       = aufs_setxattr,
19519 +       .getxattr       = aufs_getxattr,
19520 +       .listxattr      = aufs_listxattr,
19521 +       .removexattr    = aufs_removexattr,
19522 +#endif
19523 +
19524 +       .update_time    = aufs_update_time
19525 +};
19526 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
19527 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
19528 +++ linux/fs/aufs/i_op_del.c    2015-06-22 08:27:37.884168534 +0200
19529 @@ -0,0 +1,506 @@
19530 +/*
19531 + * Copyright (C) 2005-2015 Junjiro R. Okajima
19532 + *
19533 + * This program, aufs is free software; you can redistribute it and/or modify
19534 + * it under the terms of the GNU General Public License as published by
19535 + * the Free Software Foundation; either version 2 of the License, or
19536 + * (at your option) any later version.
19537 + *
19538 + * This program is distributed in the hope that it will be useful,
19539 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19540 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19541 + * GNU General Public License for more details.
19542 + *
19543 + * You should have received a copy of the GNU General Public License
19544 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19545 + */
19546 +
19547 +/*
19548 + * inode operations (del entry)
19549 + */
19550 +
19551 +#include "aufs.h"
19552 +
19553 +/*
19554 + * decide if a new whiteout for @dentry is necessary or not.
19555 + * when it is necessary, prepare the parent dir for the upper branch whose
19556 + * branch index is @bcpup for creation. the actual creation of the whiteout will
19557 + * be done by caller.
19558 + * return value:
19559 + * 0: wh is unnecessary
19560 + * plus: wh is necessary
19561 + * minus: error
19562 + */
19563 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
19564 +{
19565 +       int need_wh, err;
19566 +       aufs_bindex_t bstart;
19567 +       struct super_block *sb;
19568 +
19569 +       sb = dentry->d_sb;
19570 +       bstart = au_dbstart(dentry);
19571 +       if (*bcpup < 0) {
19572 +               *bcpup = bstart;
19573 +               if (au_test_ro(sb, bstart, dentry->d_inode)) {
19574 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
19575 +                       *bcpup = err;
19576 +                       if (unlikely(err < 0))
19577 +                               goto out;
19578 +               }
19579 +       } else
19580 +               AuDebugOn(bstart < *bcpup
19581 +                         || au_test_ro(sb, *bcpup, dentry->d_inode));
19582 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
19583 +
19584 +       if (*bcpup != bstart) {
19585 +               err = au_cpup_dirs(dentry, *bcpup);
19586 +               if (unlikely(err))
19587 +                       goto out;
19588 +               need_wh = 1;
19589 +       } else {
19590 +               struct au_dinfo *dinfo, *tmp;
19591 +
19592 +               need_wh = -ENOMEM;
19593 +               dinfo = au_di(dentry);
19594 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
19595 +               if (tmp) {
19596 +                       au_di_cp(tmp, dinfo);
19597 +                       au_di_swap(tmp, dinfo);
19598 +                       /* returns the number of positive dentries */
19599 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
19600 +                       au_di_swap(tmp, dinfo);
19601 +                       au_rw_write_unlock(&tmp->di_rwsem);
19602 +                       au_di_free(tmp);
19603 +               }
19604 +       }
19605 +       AuDbg("need_wh %d\n", need_wh);
19606 +       err = need_wh;
19607 +
19608 +out:
19609 +       return err;
19610 +}
19611 +
19612 +/*
19613 + * simple tests for the del-entry operations.
19614 + * following the checks in vfs, plus the parent-child relationship.
19615 + */
19616 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19617 +              struct dentry *h_parent, int isdir)
19618 +{
19619 +       int err;
19620 +       umode_t h_mode;
19621 +       struct dentry *h_dentry, *h_latest;
19622 +       struct inode *h_inode;
19623 +
19624 +       h_dentry = au_h_dptr(dentry, bindex);
19625 +       h_inode = h_dentry->d_inode;
19626 +       if (dentry->d_inode) {
19627 +               err = -ENOENT;
19628 +               if (unlikely(!h_inode || !h_inode->i_nlink))
19629 +                       goto out;
19630 +
19631 +               h_mode = h_inode->i_mode;
19632 +               if (!isdir) {
19633 +                       err = -EISDIR;
19634 +                       if (unlikely(S_ISDIR(h_mode)))
19635 +                               goto out;
19636 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19637 +                       err = -ENOTDIR;
19638 +                       goto out;
19639 +               }
19640 +       } else {
19641 +               /* rename(2) case */
19642 +               err = -EIO;
19643 +               if (unlikely(h_inode))
19644 +                       goto out;
19645 +       }
19646 +
19647 +       err = -ENOENT;
19648 +       /* expected parent dir is locked */
19649 +       if (unlikely(h_parent != h_dentry->d_parent))
19650 +               goto out;
19651 +       err = 0;
19652 +
19653 +       /*
19654 +        * rmdir a dir may break the consistency on some filesystem.
19655 +        * let's try heavy test.
19656 +        */
19657 +       err = -EACCES;
19658 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
19659 +                    && au_test_h_perm(h_parent->d_inode,
19660 +                                      MAY_EXEC | MAY_WRITE)))
19661 +               goto out;
19662 +
19663 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
19664 +       err = -EIO;
19665 +       if (IS_ERR(h_latest))
19666 +               goto out;
19667 +       if (h_latest == h_dentry)
19668 +               err = 0;
19669 +       dput(h_latest);
19670 +
19671 +out:
19672 +       return err;
19673 +}
19674 +
19675 +/*
19676 + * decide the branch where we operate for @dentry. the branch index will be set
19677 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
19678 + * dir for reverting.
19679 + * when a new whiteout is necessary, create it.
19680 + */
19681 +static struct dentry*
19682 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
19683 +                   struct au_dtime *dt, struct au_pin *pin)
19684 +{
19685 +       struct dentry *wh_dentry;
19686 +       struct super_block *sb;
19687 +       struct path h_path;
19688 +       int err, need_wh;
19689 +       unsigned int udba;
19690 +       aufs_bindex_t bcpup;
19691 +
19692 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
19693 +       wh_dentry = ERR_PTR(need_wh);
19694 +       if (unlikely(need_wh < 0))
19695 +               goto out;
19696 +
19697 +       sb = dentry->d_sb;
19698 +       udba = au_opt_udba(sb);
19699 +       bcpup = *rbcpup;
19700 +       err = au_pin(pin, dentry, bcpup, udba,
19701 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19702 +       wh_dentry = ERR_PTR(err);
19703 +       if (unlikely(err))
19704 +               goto out;
19705 +
19706 +       h_path.dentry = au_pinned_h_parent(pin);
19707 +       if (udba != AuOpt_UDBA_NONE
19708 +           && au_dbstart(dentry) == bcpup) {
19709 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
19710 +               wh_dentry = ERR_PTR(err);
19711 +               if (unlikely(err))
19712 +                       goto out_unpin;
19713 +       }
19714 +
19715 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
19716 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
19717 +       wh_dentry = NULL;
19718 +       if (!need_wh)
19719 +               goto out; /* success, no need to create whiteout */
19720 +
19721 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
19722 +       if (IS_ERR(wh_dentry))
19723 +               goto out_unpin;
19724 +
19725 +       /* returns with the parent is locked and wh_dentry is dget-ed */
19726 +       goto out; /* success */
19727 +
19728 +out_unpin:
19729 +       au_unpin(pin);
19730 +out:
19731 +       return wh_dentry;
19732 +}
19733 +
19734 +/*
19735 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
19736 + * in order to be revertible and save time for removing many child whiteouts
19737 + * under the dir.
19738 + * returns 1 when there are too many child whiteout and caller should remove
19739 + * them asynchronously. returns 0 when the number of children is enough small to
19740 + * remove now or the branch fs is a remote fs.
19741 + * otherwise return an error.
19742 + */
19743 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
19744 +                          struct au_nhash *whlist, struct inode *dir)
19745 +{
19746 +       int rmdir_later, err, dirwh;
19747 +       struct dentry *h_dentry;
19748 +       struct super_block *sb;
19749 +
19750 +       sb = dentry->d_sb;
19751 +       SiMustAnyLock(sb);
19752 +       h_dentry = au_h_dptr(dentry, bindex);
19753 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
19754 +       if (unlikely(err))
19755 +               goto out;
19756 +
19757 +       /* stop monitoring */
19758 +       au_hn_free(au_hi(dentry->d_inode, bindex));
19759 +
19760 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
19761 +               dirwh = au_sbi(sb)->si_dirwh;
19762 +               rmdir_later = (dirwh <= 1);
19763 +               if (!rmdir_later)
19764 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
19765 +                                                             dirwh);
19766 +               if (rmdir_later)
19767 +                       return rmdir_later;
19768 +       }
19769 +
19770 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
19771 +       if (unlikely(err)) {
19772 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
19773 +                       h_dentry, bindex, err);
19774 +               err = 0;
19775 +       }
19776 +
19777 +out:
19778 +       AuTraceErr(err);
19779 +       return err;
19780 +}
19781 +
19782 +/*
19783 + * final procedure for deleting a entry.
19784 + * maintain dentry and iattr.
19785 + */
19786 +static void epilog(struct inode *dir, struct dentry *dentry,
19787 +                  aufs_bindex_t bindex)
19788 +{
19789 +       struct inode *inode;
19790 +
19791 +       inode = dentry->d_inode;
19792 +       d_drop(dentry);
19793 +       inode->i_ctime = dir->i_ctime;
19794 +
19795 +       au_dir_ts(dir, bindex);
19796 +       dir->i_version++;
19797 +}
19798 +
19799 +/*
19800 + * when an error happened, remove the created whiteout and revert everything.
19801 + */
19802 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
19803 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
19804 +                    struct dentry *dentry, struct au_dtime *dt)
19805 +{
19806 +       int rerr;
19807 +       struct path h_path = {
19808 +               .dentry = wh_dentry,
19809 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
19810 +       };
19811 +
19812 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
19813 +       if (!rerr) {
19814 +               au_set_dbwh(dentry, bwh);
19815 +               au_dtime_revert(dt);
19816 +               return 0;
19817 +       }
19818 +
19819 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
19820 +       return -EIO;
19821 +}
19822 +
19823 +/* ---------------------------------------------------------------------- */
19824 +
19825 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
19826 +{
19827 +       int err;
19828 +       aufs_bindex_t bwh, bindex, bstart;
19829 +       struct inode *inode, *h_dir, *delegated;
19830 +       struct dentry *parent, *wh_dentry;
19831 +       /* to reuduce stack size */
19832 +       struct {
19833 +               struct au_dtime dt;
19834 +               struct au_pin pin;
19835 +               struct path h_path;
19836 +       } *a;
19837 +
19838 +       IMustLock(dir);
19839 +
19840 +       err = -ENOMEM;
19841 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19842 +       if (unlikely(!a))
19843 +               goto out;
19844 +
19845 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19846 +       if (unlikely(err))
19847 +               goto out_free;
19848 +       err = au_d_hashed_positive(dentry);
19849 +       if (unlikely(err))
19850 +               goto out_unlock;
19851 +       inode = dentry->d_inode;
19852 +       IMustLock(inode);
19853 +       err = -EISDIR;
19854 +       if (unlikely(d_is_dir(dentry)))
19855 +               goto out_unlock; /* possible? */
19856 +
19857 +       bstart = au_dbstart(dentry);
19858 +       bwh = au_dbwh(dentry);
19859 +       bindex = -1;
19860 +       parent = dentry->d_parent; /* dir inode is locked */
19861 +       di_write_lock_parent(parent);
19862 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
19863 +                                       &a->pin);
19864 +       err = PTR_ERR(wh_dentry);
19865 +       if (IS_ERR(wh_dentry))
19866 +               goto out_parent;
19867 +
19868 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
19869 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
19870 +       dget(a->h_path.dentry);
19871 +       if (bindex == bstart) {
19872 +               h_dir = au_pinned_h_dir(&a->pin);
19873 +               delegated = NULL;
19874 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
19875 +               if (unlikely(err == -EWOULDBLOCK)) {
19876 +                       pr_warn("cannot retry for NFSv4 delegation"
19877 +                               " for an internal unlink\n");
19878 +                       iput(delegated);
19879 +               }
19880 +       } else {
19881 +               /* dir inode is locked */
19882 +               h_dir = wh_dentry->d_parent->d_inode;
19883 +               IMustLock(h_dir);
19884 +               err = 0;
19885 +       }
19886 +
19887 +       if (!err) {
19888 +               vfsub_drop_nlink(inode);
19889 +               epilog(dir, dentry, bindex);
19890 +
19891 +               /* update target timestamps */
19892 +               if (bindex == bstart) {
19893 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
19894 +                       /*ignore*/
19895 +                       inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
19896 +               } else
19897 +                       /* todo: this timestamp may be reverted later */
19898 +                       inode->i_ctime = h_dir->i_ctime;
19899 +               goto out_unpin; /* success */
19900 +       }
19901 +
19902 +       /* revert */
19903 +       if (wh_dentry) {
19904 +               int rerr;
19905 +
19906 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
19907 +                                &a->dt);
19908 +               if (rerr)
19909 +                       err = rerr;
19910 +       }
19911 +
19912 +out_unpin:
19913 +       au_unpin(&a->pin);
19914 +       dput(wh_dentry);
19915 +       dput(a->h_path.dentry);
19916 +out_parent:
19917 +       di_write_unlock(parent);
19918 +out_unlock:
19919 +       aufs_read_unlock(dentry, AuLock_DW);
19920 +out_free:
19921 +       kfree(a);
19922 +out:
19923 +       return err;
19924 +}
19925 +
19926 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
19927 +{
19928 +       int err, rmdir_later;
19929 +       aufs_bindex_t bwh, bindex, bstart;
19930 +       struct inode *inode;
19931 +       struct dentry *parent, *wh_dentry, *h_dentry;
19932 +       struct au_whtmp_rmdir *args;
19933 +       /* to reuduce stack size */
19934 +       struct {
19935 +               struct au_dtime dt;
19936 +               struct au_pin pin;
19937 +       } *a;
19938 +
19939 +       IMustLock(dir);
19940 +
19941 +       err = -ENOMEM;
19942 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19943 +       if (unlikely(!a))
19944 +               goto out;
19945 +
19946 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
19947 +       if (unlikely(err))
19948 +               goto out_free;
19949 +       err = au_alive_dir(dentry);
19950 +       if (unlikely(err))
19951 +               goto out_unlock;
19952 +       inode = dentry->d_inode;
19953 +       IMustLock(inode);
19954 +       err = -ENOTDIR;
19955 +       if (unlikely(!d_is_dir(dentry)))
19956 +               goto out_unlock; /* possible? */
19957 +
19958 +       err = -ENOMEM;
19959 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
19960 +       if (unlikely(!args))
19961 +               goto out_unlock;
19962 +
19963 +       parent = dentry->d_parent; /* dir inode is locked */
19964 +       di_write_lock_parent(parent);
19965 +       err = au_test_empty(dentry, &args->whlist);
19966 +       if (unlikely(err))
19967 +               goto out_parent;
19968 +
19969 +       bstart = au_dbstart(dentry);
19970 +       bwh = au_dbwh(dentry);
19971 +       bindex = -1;
19972 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
19973 +                                       &a->pin);
19974 +       err = PTR_ERR(wh_dentry);
19975 +       if (IS_ERR(wh_dentry))
19976 +               goto out_parent;
19977 +
19978 +       h_dentry = au_h_dptr(dentry, bstart);
19979 +       dget(h_dentry);
19980 +       rmdir_later = 0;
19981 +       if (bindex == bstart) {
19982 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
19983 +               if (err > 0) {
19984 +                       rmdir_later = err;
19985 +                       err = 0;
19986 +               }
19987 +       } else {
19988 +               /* stop monitoring */
19989 +               au_hn_free(au_hi(inode, bstart));
19990 +
19991 +               /* dir inode is locked */
19992 +               IMustLock(wh_dentry->d_parent->d_inode);
19993 +               err = 0;
19994 +       }
19995 +
19996 +       if (!err) {
19997 +               vfsub_dead_dir(inode);
19998 +               au_set_dbdiropq(dentry, -1);
19999 +               epilog(dir, dentry, bindex);
20000 +
20001 +               if (rmdir_later) {
20002 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
20003 +                       args = NULL;
20004 +               }
20005 +
20006 +               goto out_unpin; /* success */
20007 +       }
20008 +
20009 +       /* revert */
20010 +       AuLabel(revert);
20011 +       if (wh_dentry) {
20012 +               int rerr;
20013 +
20014 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20015 +                                &a->dt);
20016 +               if (rerr)
20017 +                       err = rerr;
20018 +       }
20019 +
20020 +out_unpin:
20021 +       au_unpin(&a->pin);
20022 +       dput(wh_dentry);
20023 +       dput(h_dentry);
20024 +out_parent:
20025 +       di_write_unlock(parent);
20026 +       if (args)
20027 +               au_whtmp_rmdir_free(args);
20028 +out_unlock:
20029 +       aufs_read_unlock(dentry, AuLock_DW);
20030 +out_free:
20031 +       kfree(a);
20032 +out:
20033 +       AuTraceErr(err);
20034 +       return err;
20035 +}
20036 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
20037 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
20038 +++ linux/fs/aufs/i_op_ren.c    2015-06-22 08:27:37.884168534 +0200
20039 @@ -0,0 +1,1015 @@
20040 +/*
20041 + * Copyright (C) 2005-2015 Junjiro R. Okajima
20042 + *
20043 + * This program, aufs is free software; you can redistribute it and/or modify
20044 + * it under the terms of the GNU General Public License as published by
20045 + * the Free Software Foundation; either version 2 of the License, or
20046 + * (at your option) any later version.
20047 + *
20048 + * This program is distributed in the hope that it will be useful,
20049 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20050 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20051 + * GNU General Public License for more details.
20052 + *
20053 + * You should have received a copy of the GNU General Public License
20054 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20055 + */
20056 +
20057 +/*
20058 + * inode operation (rename entry)
20059 + * todo: this is crazy monster
20060 + */
20061 +
20062 +#include "aufs.h"
20063 +
20064 +enum { AuSRC, AuDST, AuSrcDst };
20065 +enum { AuPARENT, AuCHILD, AuParentChild };
20066 +
20067 +#define AuRen_ISDIR    1
20068 +#define AuRen_ISSAMEDIR        (1 << 1)
20069 +#define AuRen_WHSRC    (1 << 2)
20070 +#define AuRen_WHDST    (1 << 3)
20071 +#define AuRen_MNT_WRITE        (1 << 4)
20072 +#define AuRen_DT_DSTDIR        (1 << 5)
20073 +#define AuRen_DIROPQ   (1 << 6)
20074 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
20075 +#define au_fset_ren(flags, name) \
20076 +       do { (flags) |= AuRen_##name; } while (0)
20077 +#define au_fclr_ren(flags, name) \
20078 +       do { (flags) &= ~AuRen_##name; } while (0)
20079 +
20080 +struct au_ren_args {
20081 +       struct {
20082 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
20083 +                       *wh_dentry;
20084 +               struct inode *dir, *inode;
20085 +               struct au_hinode *hdir;
20086 +               struct au_dtime dt[AuParentChild];
20087 +               aufs_bindex_t bstart;
20088 +       } sd[AuSrcDst];
20089 +
20090 +#define src_dentry     sd[AuSRC].dentry
20091 +#define src_dir                sd[AuSRC].dir
20092 +#define src_inode      sd[AuSRC].inode
20093 +#define src_h_dentry   sd[AuSRC].h_dentry
20094 +#define src_parent     sd[AuSRC].parent
20095 +#define src_h_parent   sd[AuSRC].h_parent
20096 +#define src_wh_dentry  sd[AuSRC].wh_dentry
20097 +#define src_hdir       sd[AuSRC].hdir
20098 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
20099 +#define src_dt         sd[AuSRC].dt
20100 +#define src_bstart     sd[AuSRC].bstart
20101 +
20102 +#define dst_dentry     sd[AuDST].dentry
20103 +#define dst_dir                sd[AuDST].dir
20104 +#define dst_inode      sd[AuDST].inode
20105 +#define dst_h_dentry   sd[AuDST].h_dentry
20106 +#define dst_parent     sd[AuDST].parent
20107 +#define dst_h_parent   sd[AuDST].h_parent
20108 +#define dst_wh_dentry  sd[AuDST].wh_dentry
20109 +#define dst_hdir       sd[AuDST].hdir
20110 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
20111 +#define dst_dt         sd[AuDST].dt
20112 +#define dst_bstart     sd[AuDST].bstart
20113 +
20114 +       struct dentry *h_trap;
20115 +       struct au_branch *br;
20116 +       struct au_hinode *src_hinode;
20117 +       struct path h_path;
20118 +       struct au_nhash whlist;
20119 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
20120 +
20121 +       unsigned int flags;
20122 +
20123 +       struct au_whtmp_rmdir *thargs;
20124 +       struct dentry *h_dst;
20125 +};
20126 +
20127 +/* ---------------------------------------------------------------------- */
20128 +
20129 +/*
20130 + * functions for reverting.
20131 + * when an error happened in a single rename systemcall, we should revert
20132 + * everything as if nothing happend.
20133 + * we don't need to revert the copied-up/down the parent dir since they are
20134 + * harmless.
20135 + */
20136 +
20137 +#define RevertFailure(fmt, ...) do { \
20138 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
20139 +               ##__VA_ARGS__, err, rerr); \
20140 +       err = -EIO; \
20141 +} while (0)
20142 +
20143 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
20144 +{
20145 +       int rerr;
20146 +
20147 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
20148 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
20149 +       au_hn_imtx_unlock(a->src_hinode);
20150 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
20151 +       if (rerr)
20152 +               RevertFailure("remove diropq %pd", a->src_dentry);
20153 +}
20154 +
20155 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
20156 +{
20157 +       int rerr;
20158 +       struct inode *delegated;
20159 +
20160 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
20161 +                                         a->src_h_parent);
20162 +       rerr = PTR_ERR(a->h_path.dentry);
20163 +       if (IS_ERR(a->h_path.dentry)) {
20164 +               RevertFailure("lkup one %pd", a->src_dentry);
20165 +               return;
20166 +       }
20167 +
20168 +       delegated = NULL;
20169 +       rerr = vfsub_rename(a->dst_h_dir,
20170 +                           au_h_dptr(a->src_dentry, a->btgt),
20171 +                           a->src_h_dir, &a->h_path, &delegated);
20172 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20173 +               pr_warn("cannot retry for NFSv4 delegation"
20174 +                       " for an internal rename\n");
20175 +               iput(delegated);
20176 +       }
20177 +       d_drop(a->h_path.dentry);
20178 +       dput(a->h_path.dentry);
20179 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
20180 +       if (rerr)
20181 +               RevertFailure("rename %pd", a->src_dentry);
20182 +}
20183 +
20184 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
20185 +{
20186 +       int rerr;
20187 +       struct inode *delegated;
20188 +
20189 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
20190 +                                         a->dst_h_parent);
20191 +       rerr = PTR_ERR(a->h_path.dentry);
20192 +       if (IS_ERR(a->h_path.dentry)) {
20193 +               RevertFailure("lkup one %pd", a->dst_dentry);
20194 +               return;
20195 +       }
20196 +       if (a->h_path.dentry->d_inode) {
20197 +               d_drop(a->h_path.dentry);
20198 +               dput(a->h_path.dentry);
20199 +               return;
20200 +       }
20201 +
20202 +       delegated = NULL;
20203 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
20204 +                           &delegated);
20205 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20206 +               pr_warn("cannot retry for NFSv4 delegation"
20207 +                       " for an internal rename\n");
20208 +               iput(delegated);
20209 +       }
20210 +       d_drop(a->h_path.dentry);
20211 +       dput(a->h_path.dentry);
20212 +       if (!rerr)
20213 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
20214 +       else
20215 +               RevertFailure("rename %pd", a->h_dst);
20216 +}
20217 +
20218 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
20219 +{
20220 +       int rerr;
20221 +
20222 +       a->h_path.dentry = a->src_wh_dentry;
20223 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
20224 +       au_set_dbwh(a->src_dentry, a->src_bwh);
20225 +       if (rerr)
20226 +               RevertFailure("unlink %pd", a->src_wh_dentry);
20227 +}
20228 +#undef RevertFailure
20229 +
20230 +/* ---------------------------------------------------------------------- */
20231 +
20232 +/*
20233 + * when we have to copyup the renaming entry, do it with the rename-target name
20234 + * in order to minimize the cost (the later actual rename is unnecessary).
20235 + * otherwise rename it on the target branch.
20236 + */
20237 +static int au_ren_or_cpup(struct au_ren_args *a)
20238 +{
20239 +       int err;
20240 +       struct dentry *d;
20241 +       struct inode *delegated;
20242 +
20243 +       d = a->src_dentry;
20244 +       if (au_dbstart(d) == a->btgt) {
20245 +               a->h_path.dentry = a->dst_h_dentry;
20246 +               if (au_ftest_ren(a->flags, DIROPQ)
20247 +                   && au_dbdiropq(d) == a->btgt)
20248 +                       au_fclr_ren(a->flags, DIROPQ);
20249 +               AuDebugOn(au_dbstart(d) != a->btgt);
20250 +               delegated = NULL;
20251 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
20252 +                                  a->dst_h_dir, &a->h_path, &delegated);
20253 +               if (unlikely(err == -EWOULDBLOCK)) {
20254 +                       pr_warn("cannot retry for NFSv4 delegation"
20255 +                               " for an internal rename\n");
20256 +                       iput(delegated);
20257 +               }
20258 +       } else
20259 +               BUG();
20260 +
20261 +       if (!err && a->h_dst)
20262 +               /* it will be set to dinfo later */
20263 +               dget(a->h_dst);
20264 +
20265 +       return err;
20266 +}
20267 +
20268 +/* cf. aufs_rmdir() */
20269 +static int au_ren_del_whtmp(struct au_ren_args *a)
20270 +{
20271 +       int err;
20272 +       struct inode *dir;
20273 +
20274 +       dir = a->dst_dir;
20275 +       SiMustAnyLock(dir->i_sb);
20276 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
20277 +                                    au_sbi(dir->i_sb)->si_dirwh)
20278 +           || au_test_fs_remote(a->h_dst->d_sb)) {
20279 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
20280 +               if (unlikely(err))
20281 +                       pr_warn("failed removing whtmp dir %pd (%d), "
20282 +                               "ignored.\n", a->h_dst, err);
20283 +       } else {
20284 +               au_nhash_wh_free(&a->thargs->whlist);
20285 +               a->thargs->whlist = a->whlist;
20286 +               a->whlist.nh_num = 0;
20287 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
20288 +               dput(a->h_dst);
20289 +               a->thargs = NULL;
20290 +       }
20291 +
20292 +       return 0;
20293 +}
20294 +
20295 +/* make it 'opaque' dir. */
20296 +static int au_ren_diropq(struct au_ren_args *a)
20297 +{
20298 +       int err;
20299 +       struct dentry *diropq;
20300 +
20301 +       err = 0;
20302 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
20303 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
20304 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
20305 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
20306 +       au_hn_imtx_unlock(a->src_hinode);
20307 +       if (IS_ERR(diropq))
20308 +               err = PTR_ERR(diropq);
20309 +       else
20310 +               dput(diropq);
20311 +
20312 +       return err;
20313 +}
20314 +
20315 +static int do_rename(struct au_ren_args *a)
20316 +{
20317 +       int err;
20318 +       struct dentry *d, *h_d;
20319 +
20320 +       /* prepare workqueue args for asynchronous rmdir */
20321 +       h_d = a->dst_h_dentry;
20322 +       if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
20323 +               err = -ENOMEM;
20324 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
20325 +               if (unlikely(!a->thargs))
20326 +                       goto out;
20327 +               a->h_dst = dget(h_d);
20328 +       }
20329 +
20330 +       /* create whiteout for src_dentry */
20331 +       if (au_ftest_ren(a->flags, WHSRC)) {
20332 +               a->src_bwh = au_dbwh(a->src_dentry);
20333 +               AuDebugOn(a->src_bwh >= 0);
20334 +               a->src_wh_dentry
20335 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
20336 +               err = PTR_ERR(a->src_wh_dentry);
20337 +               if (IS_ERR(a->src_wh_dentry))
20338 +                       goto out_thargs;
20339 +       }
20340 +
20341 +       /* lookup whiteout for dentry */
20342 +       if (au_ftest_ren(a->flags, WHDST)) {
20343 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
20344 +                                a->br);
20345 +               err = PTR_ERR(h_d);
20346 +               if (IS_ERR(h_d))
20347 +                       goto out_whsrc;
20348 +               if (!h_d->d_inode)
20349 +                       dput(h_d);
20350 +               else
20351 +                       a->dst_wh_dentry = h_d;
20352 +       }
20353 +
20354 +       /* rename dentry to tmpwh */
20355 +       if (a->thargs) {
20356 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
20357 +               if (unlikely(err))
20358 +                       goto out_whdst;
20359 +
20360 +               d = a->dst_dentry;
20361 +               au_set_h_dptr(d, a->btgt, NULL);
20362 +               err = au_lkup_neg(d, a->btgt, /*wh*/0);
20363 +               if (unlikely(err))
20364 +                       goto out_whtmp;
20365 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
20366 +       }
20367 +
20368 +       BUG_ON(a->dst_h_dentry->d_inode && a->src_bstart != a->btgt);
20369 +
20370 +       /* rename by vfs_rename or cpup */
20371 +       d = a->dst_dentry;
20372 +       if (au_ftest_ren(a->flags, ISDIR)
20373 +           && (a->dst_wh_dentry
20374 +               || au_dbdiropq(d) == a->btgt
20375 +               /* hide the lower to keep xino */
20376 +               || a->btgt < au_dbend(d)
20377 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
20378 +               au_fset_ren(a->flags, DIROPQ);
20379 +       err = au_ren_or_cpup(a);
20380 +       if (unlikely(err))
20381 +               /* leave the copied-up one */
20382 +               goto out_whtmp;
20383 +
20384 +       /* make dir opaque */
20385 +       if (au_ftest_ren(a->flags, DIROPQ)) {
20386 +               err = au_ren_diropq(a);
20387 +               if (unlikely(err))
20388 +                       goto out_rename;
20389 +       }
20390 +
20391 +       /* update target timestamps */
20392 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
20393 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
20394 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
20395 +       a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
20396 +
20397 +       /* remove whiteout for dentry */
20398 +       if (a->dst_wh_dentry) {
20399 +               a->h_path.dentry = a->dst_wh_dentry;
20400 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
20401 +                                         a->dst_dentry);
20402 +               if (unlikely(err))
20403 +                       goto out_diropq;
20404 +       }
20405 +
20406 +       /* remove whtmp */
20407 +       if (a->thargs)
20408 +               au_ren_del_whtmp(a); /* ignore this error */
20409 +
20410 +       au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
20411 +       err = 0;
20412 +       goto out_success;
20413 +
20414 +out_diropq:
20415 +       if (au_ftest_ren(a->flags, DIROPQ))
20416 +               au_ren_rev_diropq(err, a);
20417 +out_rename:
20418 +       au_ren_rev_rename(err, a);
20419 +       dput(a->h_dst);
20420 +out_whtmp:
20421 +       if (a->thargs)
20422 +               au_ren_rev_whtmp(err, a);
20423 +out_whdst:
20424 +       dput(a->dst_wh_dentry);
20425 +       a->dst_wh_dentry = NULL;
20426 +out_whsrc:
20427 +       if (a->src_wh_dentry)
20428 +               au_ren_rev_whsrc(err, a);
20429 +out_success:
20430 +       dput(a->src_wh_dentry);
20431 +       dput(a->dst_wh_dentry);
20432 +out_thargs:
20433 +       if (a->thargs) {
20434 +               dput(a->h_dst);
20435 +               au_whtmp_rmdir_free(a->thargs);
20436 +               a->thargs = NULL;
20437 +       }
20438 +out:
20439 +       return err;
20440 +}
20441 +
20442 +/* ---------------------------------------------------------------------- */
20443 +
20444 +/*
20445 + * test if @dentry dir can be rename destination or not.
20446 + * success means, it is a logically empty dir.
20447 + */
20448 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
20449 +{
20450 +       return au_test_empty(dentry, whlist);
20451 +}
20452 +
20453 +/*
20454 + * test if @dentry dir can be rename source or not.
20455 + * if it can, return 0 and @children is filled.
20456 + * success means,
20457 + * - it is a logically empty dir.
20458 + * - or, it exists on writable branch and has no children including whiteouts
20459 + *       on the lower branch.
20460 + */
20461 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
20462 +{
20463 +       int err;
20464 +       unsigned int rdhash;
20465 +       aufs_bindex_t bstart;
20466 +
20467 +       bstart = au_dbstart(dentry);
20468 +       if (bstart != btgt) {
20469 +               struct au_nhash whlist;
20470 +
20471 +               SiMustAnyLock(dentry->d_sb);
20472 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
20473 +               if (!rdhash)
20474 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
20475 +                                                          dentry));
20476 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
20477 +               if (unlikely(err))
20478 +                       goto out;
20479 +               err = au_test_empty(dentry, &whlist);
20480 +               au_nhash_wh_free(&whlist);
20481 +               goto out;
20482 +       }
20483 +
20484 +       if (bstart == au_dbtaildir(dentry))
20485 +               return 0; /* success */
20486 +
20487 +       err = au_test_empty_lower(dentry);
20488 +
20489 +out:
20490 +       if (err == -ENOTEMPTY) {
20491 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
20492 +                       " is not supported\n");
20493 +               err = -EXDEV;
20494 +       }
20495 +       return err;
20496 +}
20497 +
20498 +/* side effect: sets whlist and h_dentry */
20499 +static int au_ren_may_dir(struct au_ren_args *a)
20500 +{
20501 +       int err;
20502 +       unsigned int rdhash;
20503 +       struct dentry *d;
20504 +
20505 +       d = a->dst_dentry;
20506 +       SiMustAnyLock(d->d_sb);
20507 +
20508 +       err = 0;
20509 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
20510 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
20511 +               if (!rdhash)
20512 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
20513 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
20514 +               if (unlikely(err))
20515 +                       goto out;
20516 +
20517 +               au_set_dbstart(d, a->dst_bstart);
20518 +               err = may_rename_dstdir(d, &a->whlist);
20519 +               au_set_dbstart(d, a->btgt);
20520 +       }
20521 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
20522 +       if (unlikely(err))
20523 +               goto out;
20524 +
20525 +       d = a->src_dentry;
20526 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
20527 +       if (au_ftest_ren(a->flags, ISDIR)) {
20528 +               err = may_rename_srcdir(d, a->btgt);
20529 +               if (unlikely(err)) {
20530 +                       au_nhash_wh_free(&a->whlist);
20531 +                       a->whlist.nh_num = 0;
20532 +               }
20533 +       }
20534 +out:
20535 +       return err;
20536 +}
20537 +
20538 +/* ---------------------------------------------------------------------- */
20539 +
20540 +/*
20541 + * simple tests for rename.
20542 + * following the checks in vfs, plus the parent-child relationship.
20543 + */
20544 +static int au_may_ren(struct au_ren_args *a)
20545 +{
20546 +       int err, isdir;
20547 +       struct inode *h_inode;
20548 +
20549 +       if (a->src_bstart == a->btgt) {
20550 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
20551 +                                au_ftest_ren(a->flags, ISDIR));
20552 +               if (unlikely(err))
20553 +                       goto out;
20554 +               err = -EINVAL;
20555 +               if (unlikely(a->src_h_dentry == a->h_trap))
20556 +                       goto out;
20557 +       }
20558 +
20559 +       err = 0;
20560 +       if (a->dst_bstart != a->btgt)
20561 +               goto out;
20562 +
20563 +       err = -ENOTEMPTY;
20564 +       if (unlikely(a->dst_h_dentry == a->h_trap))
20565 +               goto out;
20566 +
20567 +       err = -EIO;
20568 +       h_inode = a->dst_h_dentry->d_inode;
20569 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
20570 +       if (!a->dst_dentry->d_inode) {
20571 +               if (unlikely(h_inode))
20572 +                       goto out;
20573 +               err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
20574 +                                isdir);
20575 +       } else {
20576 +               if (unlikely(!h_inode || !h_inode->i_nlink))
20577 +                       goto out;
20578 +               err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
20579 +                                isdir);
20580 +               if (unlikely(err))
20581 +                       goto out;
20582 +       }
20583 +
20584 +out:
20585 +       if (unlikely(err == -ENOENT || err == -EEXIST))
20586 +               err = -EIO;
20587 +       AuTraceErr(err);
20588 +       return err;
20589 +}
20590 +
20591 +/* ---------------------------------------------------------------------- */
20592 +
20593 +/*
20594 + * locking order
20595 + * (VFS)
20596 + * - src_dir and dir by lock_rename()
20597 + * - inode if exitsts
20598 + * (aufs)
20599 + * - lock all
20600 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
20601 + *     + si_read_lock
20602 + *     + di_write_lock2_child()
20603 + *       + di_write_lock_child()
20604 + *        + ii_write_lock_child()
20605 + *       + di_write_lock_child2()
20606 + *        + ii_write_lock_child2()
20607 + *     + src_parent and parent
20608 + *       + di_write_lock_parent()
20609 + *        + ii_write_lock_parent()
20610 + *       + di_write_lock_parent2()
20611 + *        + ii_write_lock_parent2()
20612 + *   + lower src_dir and dir by vfsub_lock_rename()
20613 + *   + verify the every relationships between child and parent. if any
20614 + *     of them failed, unlock all and return -EBUSY.
20615 + */
20616 +static void au_ren_unlock(struct au_ren_args *a)
20617 +{
20618 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
20619 +                           a->dst_h_parent, a->dst_hdir);
20620 +       if (au_ftest_ren(a->flags, MNT_WRITE))
20621 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
20622 +}
20623 +
20624 +static int au_ren_lock(struct au_ren_args *a)
20625 +{
20626 +       int err;
20627 +       unsigned int udba;
20628 +
20629 +       err = 0;
20630 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
20631 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
20632 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
20633 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
20634 +
20635 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
20636 +       if (unlikely(err))
20637 +               goto out;
20638 +       au_fset_ren(a->flags, MNT_WRITE);
20639 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
20640 +                                     a->dst_h_parent, a->dst_hdir);
20641 +       udba = au_opt_udba(a->src_dentry->d_sb);
20642 +       if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
20643 +                    || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
20644 +               err = au_busy_or_stale();
20645 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
20646 +               err = au_h_verify(a->src_h_dentry, udba,
20647 +                                 a->src_h_parent->d_inode, a->src_h_parent,
20648 +                                 a->br);
20649 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
20650 +               err = au_h_verify(a->dst_h_dentry, udba,
20651 +                                 a->dst_h_parent->d_inode, a->dst_h_parent,
20652 +                                 a->br);
20653 +       if (!err)
20654 +               goto out; /* success */
20655 +
20656 +       err = au_busy_or_stale();
20657 +       au_ren_unlock(a);
20658 +
20659 +out:
20660 +       return err;
20661 +}
20662 +
20663 +/* ---------------------------------------------------------------------- */
20664 +
20665 +static void au_ren_refresh_dir(struct au_ren_args *a)
20666 +{
20667 +       struct inode *dir;
20668 +
20669 +       dir = a->dst_dir;
20670 +       dir->i_version++;
20671 +       if (au_ftest_ren(a->flags, ISDIR)) {
20672 +               /* is this updating defined in POSIX? */
20673 +               au_cpup_attr_timesizes(a->src_inode);
20674 +               au_cpup_attr_nlink(dir, /*force*/1);
20675 +       }
20676 +
20677 +       au_dir_ts(dir, a->btgt);
20678 +
20679 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
20680 +               return;
20681 +
20682 +       dir = a->src_dir;
20683 +       dir->i_version++;
20684 +       if (au_ftest_ren(a->flags, ISDIR))
20685 +               au_cpup_attr_nlink(dir, /*force*/1);
20686 +       au_dir_ts(dir, a->btgt);
20687 +}
20688 +
20689 +static void au_ren_refresh(struct au_ren_args *a)
20690 +{
20691 +       aufs_bindex_t bend, bindex;
20692 +       struct dentry *d, *h_d;
20693 +       struct inode *i, *h_i;
20694 +       struct super_block *sb;
20695 +
20696 +       d = a->dst_dentry;
20697 +       d_drop(d);
20698 +       if (a->h_dst)
20699 +               /* already dget-ed by au_ren_or_cpup() */
20700 +               au_set_h_dptr(d, a->btgt, a->h_dst);
20701 +
20702 +       i = a->dst_inode;
20703 +       if (i) {
20704 +               if (!au_ftest_ren(a->flags, ISDIR))
20705 +                       vfsub_drop_nlink(i);
20706 +               else {
20707 +                       vfsub_dead_dir(i);
20708 +                       au_cpup_attr_timesizes(i);
20709 +               }
20710 +               au_update_dbrange(d, /*do_put_zero*/1);
20711 +       } else {
20712 +               bend = a->btgt;
20713 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
20714 +                       au_set_h_dptr(d, bindex, NULL);
20715 +               bend = au_dbend(d);
20716 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
20717 +                       au_set_h_dptr(d, bindex, NULL);
20718 +               au_update_dbrange(d, /*do_put_zero*/0);
20719 +       }
20720 +
20721 +       d = a->src_dentry;
20722 +       au_set_dbwh(d, -1);
20723 +       bend = au_dbend(d);
20724 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20725 +               h_d = au_h_dptr(d, bindex);
20726 +               if (h_d)
20727 +                       au_set_h_dptr(d, bindex, NULL);
20728 +       }
20729 +       au_set_dbend(d, a->btgt);
20730 +
20731 +       sb = d->d_sb;
20732 +       i = a->src_inode;
20733 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
20734 +               return; /* success */
20735 +
20736 +       bend = au_ibend(i);
20737 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20738 +               h_i = au_h_iptr(i, bindex);
20739 +               if (h_i) {
20740 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
20741 +                       /* ignore this error */
20742 +                       au_set_h_iptr(i, bindex, NULL, 0);
20743 +               }
20744 +       }
20745 +       au_set_ibend(i, a->btgt);
20746 +}
20747 +
20748 +/* ---------------------------------------------------------------------- */
20749 +
20750 +/* mainly for link(2) and rename(2) */
20751 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
20752 +{
20753 +       aufs_bindex_t bdiropq, bwh;
20754 +       struct dentry *parent;
20755 +       struct au_branch *br;
20756 +
20757 +       parent = dentry->d_parent;
20758 +       IMustLock(parent->d_inode); /* dir is locked */
20759 +
20760 +       bdiropq = au_dbdiropq(parent);
20761 +       bwh = au_dbwh(dentry);
20762 +       br = au_sbr(dentry->d_sb, btgt);
20763 +       if (au_br_rdonly(br)
20764 +           || (0 <= bdiropq && bdiropq < btgt)
20765 +           || (0 <= bwh && bwh < btgt))
20766 +               btgt = -1;
20767 +
20768 +       AuDbg("btgt %d\n", btgt);
20769 +       return btgt;
20770 +}
20771 +
20772 +/* sets src_bstart, dst_bstart and btgt */
20773 +static int au_ren_wbr(struct au_ren_args *a)
20774 +{
20775 +       int err;
20776 +       struct au_wr_dir_args wr_dir_args = {
20777 +               /* .force_btgt  = -1, */
20778 +               .flags          = AuWrDir_ADD_ENTRY
20779 +       };
20780 +
20781 +       a->src_bstart = au_dbstart(a->src_dentry);
20782 +       a->dst_bstart = au_dbstart(a->dst_dentry);
20783 +       if (au_ftest_ren(a->flags, ISDIR))
20784 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
20785 +       wr_dir_args.force_btgt = a->src_bstart;
20786 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
20787 +               wr_dir_args.force_btgt = a->dst_bstart;
20788 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
20789 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
20790 +       a->btgt = err;
20791 +
20792 +       return err;
20793 +}
20794 +
20795 +static void au_ren_dt(struct au_ren_args *a)
20796 +{
20797 +       a->h_path.dentry = a->src_h_parent;
20798 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
20799 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
20800 +               a->h_path.dentry = a->dst_h_parent;
20801 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
20802 +       }
20803 +
20804 +       au_fclr_ren(a->flags, DT_DSTDIR);
20805 +       if (!au_ftest_ren(a->flags, ISDIR))
20806 +               return;
20807 +
20808 +       a->h_path.dentry = a->src_h_dentry;
20809 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
20810 +       if (a->dst_h_dentry->d_inode) {
20811 +               au_fset_ren(a->flags, DT_DSTDIR);
20812 +               a->h_path.dentry = a->dst_h_dentry;
20813 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
20814 +       }
20815 +}
20816 +
20817 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
20818 +{
20819 +       struct dentry *h_d;
20820 +       struct mutex *h_mtx;
20821 +
20822 +       au_dtime_revert(a->src_dt + AuPARENT);
20823 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
20824 +               au_dtime_revert(a->dst_dt + AuPARENT);
20825 +
20826 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
20827 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
20828 +               h_mtx = &h_d->d_inode->i_mutex;
20829 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
20830 +               au_dtime_revert(a->src_dt + AuCHILD);
20831 +               mutex_unlock(h_mtx);
20832 +
20833 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
20834 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
20835 +                       h_mtx = &h_d->d_inode->i_mutex;
20836 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
20837 +                       au_dtime_revert(a->dst_dt + AuCHILD);
20838 +                       mutex_unlock(h_mtx);
20839 +               }
20840 +       }
20841 +}
20842 +
20843 +/* ---------------------------------------------------------------------- */
20844 +
20845 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
20846 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
20847 +{
20848 +       int err, flags;
20849 +       /* reduce stack space */
20850 +       struct au_ren_args *a;
20851 +
20852 +       AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
20853 +       IMustLock(_src_dir);
20854 +       IMustLock(_dst_dir);
20855 +
20856 +       err = -ENOMEM;
20857 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
20858 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20859 +       if (unlikely(!a))
20860 +               goto out;
20861 +
20862 +       a->src_dir = _src_dir;
20863 +       a->src_dentry = _src_dentry;
20864 +       a->src_inode = a->src_dentry->d_inode;
20865 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
20866 +       a->dst_dir = _dst_dir;
20867 +       a->dst_dentry = _dst_dentry;
20868 +       a->dst_inode = a->dst_dentry->d_inode;
20869 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
20870 +       if (a->dst_inode) {
20871 +               IMustLock(a->dst_inode);
20872 +               au_igrab(a->dst_inode);
20873 +       }
20874 +
20875 +       err = -ENOTDIR;
20876 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
20877 +       if (d_is_dir(a->src_dentry)) {
20878 +               au_fset_ren(a->flags, ISDIR);
20879 +               if (unlikely(d_is_positive(a->dst_dentry)
20880 +                            && !d_is_dir(a->dst_dentry)))
20881 +                       goto out_free;
20882 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
20883 +                                               AuLock_DIR | flags);
20884 +       } else
20885 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
20886 +                                               flags);
20887 +       if (unlikely(err))
20888 +               goto out_free;
20889 +
20890 +       err = au_d_hashed_positive(a->src_dentry);
20891 +       if (unlikely(err))
20892 +               goto out_unlock;
20893 +       err = -ENOENT;
20894 +       if (a->dst_inode) {
20895 +               /*
20896 +                * If it is a dir, VFS unhash dst_dentry before this
20897 +                * function. It means we cannot rely upon d_unhashed().
20898 +                */
20899 +               if (unlikely(!a->dst_inode->i_nlink))
20900 +                       goto out_unlock;
20901 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
20902 +                       err = au_d_hashed_positive(a->dst_dentry);
20903 +                       if (unlikely(err))
20904 +                               goto out_unlock;
20905 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
20906 +                       goto out_unlock;
20907 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
20908 +               goto out_unlock;
20909 +
20910 +       /*
20911 +        * is it possible?
20912 +        * yes, it happend (in linux-3.3-rcN) but I don't know why.
20913 +        * there may exist a problem somewhere else.
20914 +        */
20915 +       err = -EINVAL;
20916 +       if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
20917 +               goto out_unlock;
20918 +
20919 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
20920 +       di_write_lock_parent(a->dst_parent);
20921 +
20922 +       /* which branch we process */
20923 +       err = au_ren_wbr(a);
20924 +       if (unlikely(err < 0))
20925 +               goto out_parent;
20926 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
20927 +       a->h_path.mnt = au_br_mnt(a->br);
20928 +
20929 +       /* are they available to be renamed */
20930 +       err = au_ren_may_dir(a);
20931 +       if (unlikely(err))
20932 +               goto out_children;
20933 +
20934 +       /* prepare the writable parent dir on the same branch */
20935 +       if (a->dst_bstart == a->btgt) {
20936 +               au_fset_ren(a->flags, WHDST);
20937 +       } else {
20938 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
20939 +               if (unlikely(err))
20940 +                       goto out_children;
20941 +       }
20942 +
20943 +       if (a->src_dir != a->dst_dir) {
20944 +               /*
20945 +                * this temporary unlock is safe,
20946 +                * because both dir->i_mutex are locked.
20947 +                */
20948 +               di_write_unlock(a->dst_parent);
20949 +               di_write_lock_parent(a->src_parent);
20950 +               err = au_wr_dir_need_wh(a->src_dentry,
20951 +                                       au_ftest_ren(a->flags, ISDIR),
20952 +                                       &a->btgt);
20953 +               di_write_unlock(a->src_parent);
20954 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
20955 +               au_fclr_ren(a->flags, ISSAMEDIR);
20956 +       } else
20957 +               err = au_wr_dir_need_wh(a->src_dentry,
20958 +                                       au_ftest_ren(a->flags, ISDIR),
20959 +                                       &a->btgt);
20960 +       if (unlikely(err < 0))
20961 +               goto out_children;
20962 +       if (err)
20963 +               au_fset_ren(a->flags, WHSRC);
20964 +
20965 +       /* cpup src */
20966 +       if (a->src_bstart != a->btgt) {
20967 +               struct au_pin pin;
20968 +
20969 +               err = au_pin(&pin, a->src_dentry, a->btgt,
20970 +                            au_opt_udba(a->src_dentry->d_sb),
20971 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20972 +               if (!err) {
20973 +                       struct au_cp_generic cpg = {
20974 +                               .dentry = a->src_dentry,
20975 +                               .bdst   = a->btgt,
20976 +                               .bsrc   = a->src_bstart,
20977 +                               .len    = -1,
20978 +                               .pin    = &pin,
20979 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
20980 +                       };
20981 +                       AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
20982 +                       err = au_sio_cpup_simple(&cpg);
20983 +                       au_unpin(&pin);
20984 +               }
20985 +               if (unlikely(err))
20986 +                       goto out_children;
20987 +               a->src_bstart = a->btgt;
20988 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
20989 +               au_fset_ren(a->flags, WHSRC);
20990 +       }
20991 +
20992 +       /* lock them all */
20993 +       err = au_ren_lock(a);
20994 +       if (unlikely(err))
20995 +               /* leave the copied-up one */
20996 +               goto out_children;
20997 +
20998 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
20999 +               err = au_may_ren(a);
21000 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
21001 +               err = -ENAMETOOLONG;
21002 +       if (unlikely(err))
21003 +               goto out_hdir;
21004 +
21005 +       /* store timestamps to be revertible */
21006 +       au_ren_dt(a);
21007 +
21008 +       /* here we go */
21009 +       err = do_rename(a);
21010 +       if (unlikely(err))
21011 +               goto out_dt;
21012 +
21013 +       /* update dir attributes */
21014 +       au_ren_refresh_dir(a);
21015 +
21016 +       /* dput/iput all lower dentries */
21017 +       au_ren_refresh(a);
21018 +
21019 +       goto out_hdir; /* success */
21020 +
21021 +out_dt:
21022 +       au_ren_rev_dt(err, a);
21023 +out_hdir:
21024 +       au_ren_unlock(a);
21025 +out_children:
21026 +       au_nhash_wh_free(&a->whlist);
21027 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
21028 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
21029 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
21030 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
21031 +       }
21032 +out_parent:
21033 +       if (!err)
21034 +               d_move(a->src_dentry, a->dst_dentry);
21035 +       else {
21036 +               au_update_dbstart(a->dst_dentry);
21037 +               if (!a->dst_inode)
21038 +                       d_drop(a->dst_dentry);
21039 +       }
21040 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
21041 +               di_write_unlock(a->dst_parent);
21042 +       else
21043 +               di_write_unlock2(a->src_parent, a->dst_parent);
21044 +out_unlock:
21045 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
21046 +out_free:
21047 +       iput(a->dst_inode);
21048 +       if (a->thargs)
21049 +               au_whtmp_rmdir_free(a->thargs);
21050 +       kfree(a);
21051 +out:
21052 +       AuTraceErr(err);
21053 +       return err;
21054 +}
21055 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
21056 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
21057 +++ linux/fs/aufs/Kconfig       2015-06-22 08:27:37.874168290 +0200
21058 @@ -0,0 +1,185 @@
21059 +config AUFS_FS
21060 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
21061 +       help
21062 +       Aufs is a stackable unification filesystem such as Unionfs,
21063 +       which unifies several directories and provides a merged single
21064 +       directory.
21065 +       In the early days, aufs was entirely re-designed and
21066 +       re-implemented Unionfs Version 1.x series. Introducing many
21067 +       original ideas, approaches and improvements, it becomes totally
21068 +       different from Unionfs while keeping the basic features.
21069 +
21070 +if AUFS_FS
21071 +choice
21072 +       prompt "Maximum number of branches"
21073 +       default AUFS_BRANCH_MAX_127
21074 +       help
21075 +       Specifies the maximum number of branches (or member directories)
21076 +       in a single aufs. The larger value consumes more system
21077 +       resources and has a minor impact to performance.
21078 +config AUFS_BRANCH_MAX_127
21079 +       bool "127"
21080 +       help
21081 +       Specifies the maximum number of branches (or member directories)
21082 +       in a single aufs. The larger value consumes more system
21083 +       resources and has a minor impact to performance.
21084 +config AUFS_BRANCH_MAX_511
21085 +       bool "511"
21086 +       help
21087 +       Specifies the maximum number of branches (or member directories)
21088 +       in a single aufs. The larger value consumes more system
21089 +       resources and has a minor impact to performance.
21090 +config AUFS_BRANCH_MAX_1023
21091 +       bool "1023"
21092 +       help
21093 +       Specifies the maximum number of branches (or member directories)
21094 +       in a single aufs. The larger value consumes more system
21095 +       resources and has a minor impact to performance.
21096 +config AUFS_BRANCH_MAX_32767
21097 +       bool "32767"
21098 +       help
21099 +       Specifies the maximum number of branches (or member directories)
21100 +       in a single aufs. The larger value consumes more system
21101 +       resources and has a minor impact to performance.
21102 +endchoice
21103 +
21104 +config AUFS_SBILIST
21105 +       bool
21106 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
21107 +       default y
21108 +       help
21109 +       Automatic configuration for internal use.
21110 +       When aufs supports Magic SysRq or /proc, enabled automatically.
21111 +
21112 +config AUFS_HNOTIFY
21113 +       bool "Detect direct branch access (bypassing aufs)"
21114 +       help
21115 +       If you want to modify files on branches directly, eg. bypassing aufs,
21116 +       and want aufs to detect the changes of them fully, then enable this
21117 +       option and use 'udba=notify' mount option.
21118 +       Currently there is only one available configuration, "fsnotify".
21119 +       It will have a negative impact to the performance.
21120 +       See detail in aufs.5.
21121 +
21122 +choice
21123 +       prompt "method" if AUFS_HNOTIFY
21124 +       default AUFS_HFSNOTIFY
21125 +config AUFS_HFSNOTIFY
21126 +       bool "fsnotify"
21127 +       select FSNOTIFY
21128 +endchoice
21129 +
21130 +config AUFS_EXPORT
21131 +       bool "NFS-exportable aufs"
21132 +       depends on EXPORTFS
21133 +       help
21134 +       If you want to export your mounted aufs via NFS, then enable this
21135 +       option. There are several requirements for this configuration.
21136 +       See detail in aufs.5.
21137 +
21138 +config AUFS_INO_T_64
21139 +       bool
21140 +       depends on AUFS_EXPORT
21141 +       depends on 64BIT && !(ALPHA || S390)
21142 +       default y
21143 +       help
21144 +       Automatic configuration for internal use.
21145 +       /* typedef unsigned long/int __kernel_ino_t */
21146 +       /* alpha and s390x are int */
21147 +
21148 +config AUFS_XATTR
21149 +       bool "support for XATTR/EA (including Security Labels)"
21150 +       help
21151 +       If your branch fs supports XATTR/EA and you want to make them
21152 +       available in aufs too, then enable this opsion and specify the
21153 +       branch attributes for EA.
21154 +       See detail in aufs.5.
21155 +
21156 +config AUFS_FHSM
21157 +       bool "File-based Hierarchical Storage Management"
21158 +       help
21159 +       Hierarchical Storage Management (or HSM) is a well-known feature
21160 +       in the storage world. Aufs provides this feature as file-based.
21161 +       with multiple branches.
21162 +       These multiple branches are prioritized, ie. the topmost one
21163 +       should be the fastest drive and be used heavily.
21164 +
21165 +config AUFS_RDU
21166 +       bool "Readdir in userspace"
21167 +       help
21168 +       Aufs has two methods to provide a merged view for a directory,
21169 +       by a user-space library and by kernel-space natively. The latter
21170 +       is always enabled but sometimes large and slow.
21171 +       If you enable this option, install the library in aufs2-util
21172 +       package, and set some environment variables for your readdir(3),
21173 +       then the work will be handled in user-space which generally
21174 +       shows better performance in most cases.
21175 +       See detail in aufs.5.
21176 +
21177 +config AUFS_SHWH
21178 +       bool "Show whiteouts"
21179 +       help
21180 +       If you want to make the whiteouts in aufs visible, then enable
21181 +       this option and specify 'shwh' mount option. Although it may
21182 +       sounds like philosophy or something, but in technically it
21183 +       simply shows the name of whiteout with keeping its behaviour.
21184 +
21185 +config AUFS_BR_RAMFS
21186 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
21187 +       help
21188 +       If you want to use ramfs as an aufs branch fs, then enable this
21189 +       option. Generally tmpfs is recommended.
21190 +       Aufs prohibited them to be a branch fs by default, because
21191 +       initramfs becomes unusable after switch_root or something
21192 +       generally. If you sets initramfs as an aufs branch and boot your
21193 +       system by switch_root, you will meet a problem easily since the
21194 +       files in initramfs may be inaccessible.
21195 +       Unless you are going to use ramfs as an aufs branch fs without
21196 +       switch_root or something, leave it N.
21197 +
21198 +config AUFS_BR_FUSE
21199 +       bool "Fuse fs as an aufs branch"
21200 +       depends on FUSE_FS
21201 +       select AUFS_POLL
21202 +       help
21203 +       If you want to use fuse-based userspace filesystem as an aufs
21204 +       branch fs, then enable this option.
21205 +       It implements the internal poll(2) operation which is
21206 +       implemented by fuse only (curretnly).
21207 +
21208 +config AUFS_POLL
21209 +       bool
21210 +       help
21211 +       Automatic configuration for internal use.
21212 +
21213 +config AUFS_BR_HFSPLUS
21214 +       bool "Hfsplus as an aufs branch"
21215 +       depends on HFSPLUS_FS
21216 +       default y
21217 +       help
21218 +       If you want to use hfsplus fs as an aufs branch fs, then enable
21219 +       this option. This option introduces a small overhead at
21220 +       copying-up a file on hfsplus.
21221 +
21222 +config AUFS_BDEV_LOOP
21223 +       bool
21224 +       depends on BLK_DEV_LOOP
21225 +       default y
21226 +       help
21227 +       Automatic configuration for internal use.
21228 +       Convert =[ym] into =y.
21229 +
21230 +config AUFS_DEBUG
21231 +       bool "Debug aufs"
21232 +       help
21233 +       Enable this to compile aufs internal debug code.
21234 +       It will have a negative impact to the performance.
21235 +
21236 +config AUFS_MAGIC_SYSRQ
21237 +       bool
21238 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
21239 +       default y
21240 +       help
21241 +       Automatic configuration for internal use.
21242 +       When aufs supports Magic SysRq, enabled automatically.
21243 +endif
21244 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
21245 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
21246 +++ linux/fs/aufs/loop.c        2015-06-22 08:29:23.706757681 +0200
21247 @@ -0,0 +1,145 @@
21248 +/*
21249 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21250 + *
21251 + * This program, aufs is free software; you can redistribute it and/or modify
21252 + * it under the terms of the GNU General Public License as published by
21253 + * the Free Software Foundation; either version 2 of the License, or
21254 + * (at your option) any later version.
21255 + *
21256 + * This program is distributed in the hope that it will be useful,
21257 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21258 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21259 + * GNU General Public License for more details.
21260 + *
21261 + * You should have received a copy of the GNU General Public License
21262 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21263 + */
21264 +
21265 +/*
21266 + * support for loopback block device as a branch
21267 + */
21268 +
21269 +#include "aufs.h"
21270 +
21271 +/* added into drivers/block/loop.c */
21272 +static struct file *(*backing_file_func)(struct super_block *sb);
21273 +
21274 +/*
21275 + * test if two lower dentries have overlapping branches.
21276 + */
21277 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
21278 +{
21279 +       struct super_block *h_sb;
21280 +       struct file *backing_file;
21281 +
21282 +       if (unlikely(!backing_file_func)) {
21283 +               /* don't load "loop" module here */
21284 +               backing_file_func = symbol_get(loop_backing_file);
21285 +               if (unlikely(!backing_file_func))
21286 +                       /* "loop" module is not loaded */
21287 +                       return 0;
21288 +       }
21289 +
21290 +       h_sb = h_adding->d_sb;
21291 +       backing_file = backing_file_func(h_sb);
21292 +       if (!backing_file)
21293 +               return 0;
21294 +
21295 +       h_adding = backing_file->f_path.dentry;
21296 +       /*
21297 +        * h_adding can be local NFS.
21298 +        * in this case aufs cannot detect the loop.
21299 +        */
21300 +       if (unlikely(h_adding->d_sb == sb))
21301 +               return 1;
21302 +       return !!au_test_subdir(h_adding, sb->s_root);
21303 +}
21304 +
21305 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
21306 +int au_test_loopback_kthread(void)
21307 +{
21308 +       int ret;
21309 +       struct task_struct *tsk = current;
21310 +       char c, comm[sizeof(tsk->comm)];
21311 +
21312 +       ret = 0;
21313 +       if (tsk->flags & PF_KTHREAD) {
21314 +               get_task_comm(comm, tsk);
21315 +               c = comm[4];
21316 +               ret = ('0' <= c && c <= '9'
21317 +                      && !strncmp(comm, "loop", 4));
21318 +       }
21319 +
21320 +       return ret;
21321 +}
21322 +
21323 +/* ---------------------------------------------------------------------- */
21324 +
21325 +#define au_warn_loopback_step  16
21326 +static int au_warn_loopback_nelem = au_warn_loopback_step;
21327 +static unsigned long *au_warn_loopback_array;
21328 +
21329 +void au_warn_loopback(struct super_block *h_sb)
21330 +{
21331 +       int i, new_nelem;
21332 +       unsigned long *a, magic;
21333 +       static DEFINE_SPINLOCK(spin);
21334 +
21335 +       magic = h_sb->s_magic;
21336 +       spin_lock(&spin);
21337 +       a = au_warn_loopback_array;
21338 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
21339 +               if (a[i] == magic) {
21340 +                       spin_unlock(&spin);
21341 +                       return;
21342 +               }
21343 +
21344 +       /* h_sb is new to us, print it */
21345 +       if (i < au_warn_loopback_nelem) {
21346 +               a[i] = magic;
21347 +               goto pr;
21348 +       }
21349 +
21350 +       /* expand the array */
21351 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
21352 +       a = au_kzrealloc(au_warn_loopback_array,
21353 +                        au_warn_loopback_nelem * sizeof(unsigned long),
21354 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
21355 +       if (a) {
21356 +               au_warn_loopback_nelem = new_nelem;
21357 +               au_warn_loopback_array = a;
21358 +               a[i] = magic;
21359 +               goto pr;
21360 +       }
21361 +
21362 +       spin_unlock(&spin);
21363 +       AuWarn1("realloc failed, ignored\n");
21364 +       return;
21365 +
21366 +pr:
21367 +       spin_unlock(&spin);
21368 +       pr_warn("you may want to try another patch for loopback file "
21369 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
21370 +}
21371 +
21372 +int au_loopback_init(void)
21373 +{
21374 +       int err;
21375 +       struct super_block *sb __maybe_unused;
21376 +
21377 +       AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
21378 +
21379 +       err = 0;
21380 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
21381 +                                        sizeof(unsigned long), GFP_NOFS);
21382 +       if (unlikely(!au_warn_loopback_array))
21383 +               err = -ENOMEM;
21384 +
21385 +       return err;
21386 +}
21387 +
21388 +void au_loopback_fin(void)
21389 +{
21390 +       symbol_put(loop_backing_file);
21391 +       kfree(au_warn_loopback_array);
21392 +}
21393 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
21394 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
21395 +++ linux/fs/aufs/loop.h        2015-06-22 08:27:37.884168534 +0200
21396 @@ -0,0 +1,52 @@
21397 +/*
21398 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21399 + *
21400 + * This program, aufs is free software; you can redistribute it and/or modify
21401 + * it under the terms of the GNU General Public License as published by
21402 + * the Free Software Foundation; either version 2 of the License, or
21403 + * (at your option) any later version.
21404 + *
21405 + * This program is distributed in the hope that it will be useful,
21406 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21407 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21408 + * GNU General Public License for more details.
21409 + *
21410 + * You should have received a copy of the GNU General Public License
21411 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21412 + */
21413 +
21414 +/*
21415 + * support for loopback mount as a branch
21416 + */
21417 +
21418 +#ifndef __AUFS_LOOP_H__
21419 +#define __AUFS_LOOP_H__
21420 +
21421 +#ifdef __KERNEL__
21422 +
21423 +struct dentry;
21424 +struct super_block;
21425 +
21426 +#ifdef CONFIG_AUFS_BDEV_LOOP
21427 +/* drivers/block/loop.c */
21428 +struct file *loop_backing_file(struct super_block *sb);
21429 +
21430 +/* loop.c */
21431 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
21432 +int au_test_loopback_kthread(void);
21433 +void au_warn_loopback(struct super_block *h_sb);
21434 +
21435 +int au_loopback_init(void);
21436 +void au_loopback_fin(void);
21437 +#else
21438 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
21439 +          struct dentry *h_adding)
21440 +AuStubInt0(au_test_loopback_kthread, void)
21441 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
21442 +
21443 +AuStubInt0(au_loopback_init, void)
21444 +AuStubVoid(au_loopback_fin, void)
21445 +#endif /* BLK_DEV_LOOP */
21446 +
21447 +#endif /* __KERNEL__ */
21448 +#endif /* __AUFS_LOOP_H__ */
21449 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
21450 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
21451 +++ linux/fs/aufs/magic.mk      2015-06-22 08:27:37.884168534 +0200
21452 @@ -0,0 +1,30 @@
21453 +
21454 +# defined in ${srctree}/fs/fuse/inode.c
21455 +# tristate
21456 +ifdef CONFIG_FUSE_FS
21457 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
21458 +endif
21459 +
21460 +# defined in ${srctree}/fs/xfs/xfs_sb.h
21461 +# tristate
21462 +ifdef CONFIG_XFS_FS
21463 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
21464 +endif
21465 +
21466 +# defined in ${srctree}/fs/configfs/mount.c
21467 +# tristate
21468 +ifdef CONFIG_CONFIGFS_FS
21469 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
21470 +endif
21471 +
21472 +# defined in ${srctree}/fs/ubifs/ubifs.h
21473 +# tristate
21474 +ifdef CONFIG_UBIFS_FS
21475 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
21476 +endif
21477 +
21478 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
21479 +# tristate
21480 +ifdef CONFIG_HFSPLUS_FS
21481 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
21482 +endif
21483 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
21484 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
21485 +++ linux/fs/aufs/Makefile      2015-06-22 08:27:37.874168290 +0200
21486 @@ -0,0 +1,44 @@
21487 +
21488 +include ${src}/magic.mk
21489 +ifeq (${CONFIG_AUFS_FS},m)
21490 +include ${src}/conf.mk
21491 +endif
21492 +-include ${src}/priv_def.mk
21493 +
21494 +# cf. include/linux/kernel.h
21495 +# enable pr_debug
21496 +ccflags-y += -DDEBUG
21497 +# sparse requires the full pathname
21498 +ifdef M
21499 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
21500 +else
21501 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
21502 +endif
21503 +
21504 +obj-$(CONFIG_AUFS_FS) += aufs.o
21505 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
21506 +       wkq.o vfsub.o dcsub.o \
21507 +       cpup.o whout.o wbr_policy.o \
21508 +       dinfo.o dentry.o \
21509 +       dynop.o \
21510 +       finfo.o file.o f_op.o \
21511 +       dir.o vdir.o \
21512 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
21513 +       mvdown.o ioctl.o
21514 +
21515 +# all are boolean
21516 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
21517 +aufs-$(CONFIG_SYSFS) += sysfs.o
21518 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
21519 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
21520 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
21521 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
21522 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
21523 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
21524 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
21525 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
21526 +aufs-$(CONFIG_AUFS_POLL) += poll.o
21527 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
21528 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
21529 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
21530 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
21531 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
21532 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
21533 +++ linux/fs/aufs/module.c      2015-06-22 08:27:37.884168534 +0200
21534 @@ -0,0 +1,210 @@
21535 +/*
21536 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21537 + *
21538 + * This program, aufs is free software; you can redistribute it and/or modify
21539 + * it under the terms of the GNU General Public License as published by
21540 + * the Free Software Foundation; either version 2 of the License, or
21541 + * (at your option) any later version.
21542 + *
21543 + * This program is distributed in the hope that it will be useful,
21544 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21545 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21546 + * GNU General Public License for more details.
21547 + *
21548 + * You should have received a copy of the GNU General Public License
21549 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21550 + */
21551 +
21552 +/*
21553 + * module global variables and operations
21554 + */
21555 +
21556 +#include <linux/module.h>
21557 +#include <linux/seq_file.h>
21558 +#include "aufs.h"
21559 +
21560 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
21561 +{
21562 +       if (new_sz <= nused)
21563 +               return p;
21564 +
21565 +       p = krealloc(p, new_sz, gfp);
21566 +       if (p)
21567 +               memset(p + nused, 0, new_sz - nused);
21568 +       return p;
21569 +}
21570 +
21571 +/* ---------------------------------------------------------------------- */
21572 +
21573 +/*
21574 + * aufs caches
21575 + */
21576 +struct kmem_cache *au_cachep[AuCache_Last];
21577 +static int __init au_cache_init(void)
21578 +{
21579 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
21580 +       if (au_cachep[AuCache_DINFO])
21581 +               /* SLAB_DESTROY_BY_RCU */
21582 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
21583 +                                                       au_icntnr_init_once);
21584 +       if (au_cachep[AuCache_ICNTNR])
21585 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
21586 +                                                      au_fi_init_once);
21587 +       if (au_cachep[AuCache_FINFO])
21588 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
21589 +       if (au_cachep[AuCache_VDIR])
21590 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
21591 +       if (au_cachep[AuCache_DEHSTR])
21592 +               return 0;
21593 +
21594 +       return -ENOMEM;
21595 +}
21596 +
21597 +static void au_cache_fin(void)
21598 +{
21599 +       int i;
21600 +
21601 +       /*
21602 +        * Make sure all delayed rcu free inodes are flushed before we
21603 +        * destroy cache.
21604 +        */
21605 +       rcu_barrier();
21606 +
21607 +       /* excluding AuCache_HNOTIFY */
21608 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
21609 +       for (i = 0; i < AuCache_HNOTIFY; i++)
21610 +               if (au_cachep[i]) {
21611 +                       kmem_cache_destroy(au_cachep[i]);
21612 +                       au_cachep[i] = NULL;
21613 +               }
21614 +}
21615 +
21616 +/* ---------------------------------------------------------------------- */
21617 +
21618 +int au_dir_roflags;
21619 +
21620 +#ifdef CONFIG_AUFS_SBILIST
21621 +/*
21622 + * iterate_supers_type() doesn't protect us from
21623 + * remounting (branch management)
21624 + */
21625 +struct au_splhead au_sbilist;
21626 +#endif
21627 +
21628 +struct lock_class_key au_lc_key[AuLcKey_Last];
21629 +
21630 +/*
21631 + * functions for module interface.
21632 + */
21633 +MODULE_LICENSE("GPL");
21634 +/* MODULE_LICENSE("GPL v2"); */
21635 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
21636 +MODULE_DESCRIPTION(AUFS_NAME
21637 +       " -- Advanced multi layered unification filesystem");
21638 +MODULE_VERSION(AUFS_VERSION);
21639 +MODULE_ALIAS_FS(AUFS_NAME);
21640 +
21641 +/* this module parameter has no meaning when SYSFS is disabled */
21642 +int sysaufs_brs = 1;
21643 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
21644 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
21645 +
21646 +/* this module parameter has no meaning when USER_NS is disabled */
21647 +static bool au_userns;
21648 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
21649 +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
21650 +
21651 +/* ---------------------------------------------------------------------- */
21652 +
21653 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
21654 +
21655 +int au_seq_path(struct seq_file *seq, struct path *path)
21656 +{
21657 +       return seq_path(seq, path, au_esc_chars);
21658 +}
21659 +
21660 +/* ---------------------------------------------------------------------- */
21661 +
21662 +static int __init aufs_init(void)
21663 +{
21664 +       int err, i;
21665 +       char *p;
21666 +
21667 +       p = au_esc_chars;
21668 +       for (i = 1; i <= ' '; i++)
21669 +               *p++ = i;
21670 +       *p++ = '\\';
21671 +       *p++ = '\x7f';
21672 +       *p = 0;
21673 +
21674 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
21675 +
21676 +       au_sbilist_init();
21677 +       sysaufs_brs_init();
21678 +       au_debug_init();
21679 +       au_dy_init();
21680 +       err = sysaufs_init();
21681 +       if (unlikely(err))
21682 +               goto out;
21683 +       err = au_procfs_init();
21684 +       if (unlikely(err))
21685 +               goto out_sysaufs;
21686 +       err = au_wkq_init();
21687 +       if (unlikely(err))
21688 +               goto out_procfs;
21689 +       err = au_loopback_init();
21690 +       if (unlikely(err))
21691 +               goto out_wkq;
21692 +       err = au_hnotify_init();
21693 +       if (unlikely(err))
21694 +               goto out_loopback;
21695 +       err = au_sysrq_init();
21696 +       if (unlikely(err))
21697 +               goto out_hin;
21698 +       err = au_cache_init();
21699 +       if (unlikely(err))
21700 +               goto out_sysrq;
21701 +
21702 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
21703 +       err = register_filesystem(&aufs_fs_type);
21704 +       if (unlikely(err))
21705 +               goto out_cache;
21706 +
21707 +       /* since we define pr_fmt, call printk directly */
21708 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
21709 +       goto out; /* success */
21710 +
21711 +out_cache:
21712 +       au_cache_fin();
21713 +out_sysrq:
21714 +       au_sysrq_fin();
21715 +out_hin:
21716 +       au_hnotify_fin();
21717 +out_loopback:
21718 +       au_loopback_fin();
21719 +out_wkq:
21720 +       au_wkq_fin();
21721 +out_procfs:
21722 +       au_procfs_fin();
21723 +out_sysaufs:
21724 +       sysaufs_fin();
21725 +       au_dy_fin();
21726 +out:
21727 +       return err;
21728 +}
21729 +
21730 +static void __exit aufs_exit(void)
21731 +{
21732 +       unregister_filesystem(&aufs_fs_type);
21733 +       au_cache_fin();
21734 +       au_sysrq_fin();
21735 +       au_hnotify_fin();
21736 +       au_loopback_fin();
21737 +       au_wkq_fin();
21738 +       au_procfs_fin();
21739 +       sysaufs_fin();
21740 +       au_dy_fin();
21741 +}
21742 +
21743 +module_init(aufs_init);
21744 +module_exit(aufs_exit);
21745 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
21746 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
21747 +++ linux/fs/aufs/module.h      2015-06-22 08:27:37.884168534 +0200
21748 @@ -0,0 +1,104 @@
21749 +/*
21750 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21751 + *
21752 + * This program, aufs is free software; you can redistribute it and/or modify
21753 + * it under the terms of the GNU General Public License as published by
21754 + * the Free Software Foundation; either version 2 of the License, or
21755 + * (at your option) any later version.
21756 + *
21757 + * This program is distributed in the hope that it will be useful,
21758 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21759 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21760 + * GNU General Public License for more details.
21761 + *
21762 + * You should have received a copy of the GNU General Public License
21763 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21764 + */
21765 +
21766 +/*
21767 + * module initialization and module-global
21768 + */
21769 +
21770 +#ifndef __AUFS_MODULE_H__
21771 +#define __AUFS_MODULE_H__
21772 +
21773 +#ifdef __KERNEL__
21774 +
21775 +#include <linux/slab.h>
21776 +
21777 +struct path;
21778 +struct seq_file;
21779 +
21780 +/* module parameters */
21781 +extern int sysaufs_brs;
21782 +
21783 +/* ---------------------------------------------------------------------- */
21784 +
21785 +extern int au_dir_roflags;
21786 +
21787 +enum {
21788 +       AuLcNonDir_FIINFO,
21789 +       AuLcNonDir_DIINFO,
21790 +       AuLcNonDir_IIINFO,
21791 +
21792 +       AuLcDir_FIINFO,
21793 +       AuLcDir_DIINFO,
21794 +       AuLcDir_IIINFO,
21795 +
21796 +       AuLcSymlink_DIINFO,
21797 +       AuLcSymlink_IIINFO,
21798 +
21799 +       AuLcKey_Last
21800 +};
21801 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
21802 +
21803 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
21804 +int au_seq_path(struct seq_file *seq, struct path *path);
21805 +
21806 +#ifdef CONFIG_PROC_FS
21807 +/* procfs.c */
21808 +int __init au_procfs_init(void);
21809 +void au_procfs_fin(void);
21810 +#else
21811 +AuStubInt0(au_procfs_init, void);
21812 +AuStubVoid(au_procfs_fin, void);
21813 +#endif
21814 +
21815 +/* ---------------------------------------------------------------------- */
21816 +
21817 +/* kmem cache */
21818 +enum {
21819 +       AuCache_DINFO,
21820 +       AuCache_ICNTNR,
21821 +       AuCache_FINFO,
21822 +       AuCache_VDIR,
21823 +       AuCache_DEHSTR,
21824 +       AuCache_HNOTIFY, /* must be last */
21825 +       AuCache_Last
21826 +};
21827 +
21828 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
21829 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
21830 +#define AuCacheCtor(type, ctor)        \
21831 +       kmem_cache_create(#type, sizeof(struct type), \
21832 +                         __alignof__(struct type), AuCacheFlags, ctor)
21833 +
21834 +extern struct kmem_cache *au_cachep[];
21835 +
21836 +#define AuCacheFuncs(name, index) \
21837 +static inline struct au_##name *au_cache_alloc_##name(void) \
21838 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
21839 +static inline void au_cache_free_##name(struct au_##name *p) \
21840 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
21841 +
21842 +AuCacheFuncs(dinfo, DINFO);
21843 +AuCacheFuncs(icntnr, ICNTNR);
21844 +AuCacheFuncs(finfo, FINFO);
21845 +AuCacheFuncs(vdir, VDIR);
21846 +AuCacheFuncs(vdir_dehstr, DEHSTR);
21847 +#ifdef CONFIG_AUFS_HNOTIFY
21848 +AuCacheFuncs(hnotify, HNOTIFY);
21849 +#endif
21850 +
21851 +#endif /* __KERNEL__ */
21852 +#endif /* __AUFS_MODULE_H__ */
21853 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
21854 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
21855 +++ linux/fs/aufs/mvdown.c      2015-06-22 08:27:37.884168534 +0200
21856 @@ -0,0 +1,694 @@
21857 +/*
21858 + * Copyright (C) 2011-2015 Junjiro R. Okajima
21859 + *
21860 + * This program, aufs is free software; you can redistribute it and/or modify
21861 + * it under the terms of the GNU General Public License as published by
21862 + * the Free Software Foundation; either version 2 of the License, or
21863 + * (at your option) any later version.
21864 + *
21865 + * This program is distributed in the hope that it will be useful,
21866 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21867 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21868 + * GNU General Public License for more details.
21869 + *
21870 + * You should have received a copy of the GNU General Public License
21871 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21872 + */
21873 +
21874 +/*
21875 + * move-down, opposite of copy-up
21876 + */
21877 +
21878 +#include "aufs.h"
21879 +
21880 +struct au_mvd_args {
21881 +       struct {
21882 +               struct super_block *h_sb;
21883 +               struct dentry *h_parent;
21884 +               struct au_hinode *hdir;
21885 +               struct inode *h_dir, *h_inode;
21886 +               struct au_pin pin;
21887 +       } info[AUFS_MVDOWN_NARRAY];
21888 +
21889 +       struct aufs_mvdown mvdown;
21890 +       struct dentry *dentry, *parent;
21891 +       struct inode *inode, *dir;
21892 +       struct super_block *sb;
21893 +       aufs_bindex_t bopq, bwh, bfound;
21894 +       unsigned char rename_lock;
21895 +};
21896 +
21897 +#define mvd_errno              mvdown.au_errno
21898 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
21899 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
21900 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
21901 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
21902 +
21903 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
21904 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
21905 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
21906 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
21907 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
21908 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
21909 +
21910 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
21911 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
21912 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
21913 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
21914 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
21915 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
21916 +
21917 +#define AU_MVD_PR(flag, ...) do {                      \
21918 +               if (flag)                               \
21919 +                       pr_err(__VA_ARGS__);            \
21920 +       } while (0)
21921 +
21922 +static int find_lower_writable(struct au_mvd_args *a)
21923 +{
21924 +       struct super_block *sb;
21925 +       aufs_bindex_t bindex, bend;
21926 +       struct au_branch *br;
21927 +
21928 +       sb = a->sb;
21929 +       bindex = a->mvd_bsrc;
21930 +       bend = au_sbend(sb);
21931 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
21932 +               for (bindex++; bindex <= bend; bindex++) {
21933 +                       br = au_sbr(sb, bindex);
21934 +                       if (au_br_fhsm(br->br_perm)
21935 +                           && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
21936 +                               return bindex;
21937 +               }
21938 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
21939 +               for (bindex++; bindex <= bend; bindex++) {
21940 +                       br = au_sbr(sb, bindex);
21941 +                       if (!au_br_rdonly(br))
21942 +                               return bindex;
21943 +               }
21944 +       else
21945 +               for (bindex++; bindex <= bend; bindex++) {
21946 +                       br = au_sbr(sb, bindex);
21947 +                       if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
21948 +                               if (au_br_rdonly(br))
21949 +                                       a->mvdown.flags
21950 +                                               |= AUFS_MVDOWN_ROLOWER_R;
21951 +                               return bindex;
21952 +                       }
21953 +               }
21954 +
21955 +       return -1;
21956 +}
21957 +
21958 +/* make the parent dir on bdst */
21959 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
21960 +{
21961 +       int err;
21962 +
21963 +       err = 0;
21964 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
21965 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
21966 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
21967 +       a->mvd_h_dst_parent = NULL;
21968 +       if (au_dbend(a->parent) >= a->mvd_bdst)
21969 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
21970 +       if (!a->mvd_h_dst_parent) {
21971 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
21972 +               if (unlikely(err)) {
21973 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
21974 +                       goto out;
21975 +               }
21976 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
21977 +       }
21978 +
21979 +out:
21980 +       AuTraceErr(err);
21981 +       return err;
21982 +}
21983 +
21984 +/* lock them all */
21985 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
21986 +{
21987 +       int err;
21988 +       struct dentry *h_trap;
21989 +
21990 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
21991 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
21992 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
21993 +                    au_opt_udba(a->sb),
21994 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
21995 +       AuTraceErr(err);
21996 +       if (unlikely(err)) {
21997 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
21998 +               goto out;
21999 +       }
22000 +
22001 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
22002 +               a->rename_lock = 0;
22003 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22004 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
22005 +                           au_opt_udba(a->sb),
22006 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22007 +               err = au_do_pin(&a->mvd_pin_src);
22008 +               AuTraceErr(err);
22009 +               a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
22010 +               if (unlikely(err)) {
22011 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
22012 +                       goto out_dst;
22013 +               }
22014 +               goto out; /* success */
22015 +       }
22016 +
22017 +       a->rename_lock = 1;
22018 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
22019 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22020 +                    au_opt_udba(a->sb),
22021 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22022 +       AuTraceErr(err);
22023 +       a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
22024 +       if (unlikely(err)) {
22025 +               AU_MVD_PR(dmsg, "pin_src failed\n");
22026 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22027 +               goto out_dst;
22028 +       }
22029 +       au_pin_hdir_unlock(&a->mvd_pin_src);
22030 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22031 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
22032 +       if (h_trap) {
22033 +               err = (h_trap != a->mvd_h_src_parent);
22034 +               if (err)
22035 +                       err = (h_trap != a->mvd_h_dst_parent);
22036 +       }
22037 +       BUG_ON(err); /* it should never happen */
22038 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
22039 +               err = -EBUSY;
22040 +               AuTraceErr(err);
22041 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22042 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22043 +               au_pin_hdir_lock(&a->mvd_pin_src);
22044 +               au_unpin(&a->mvd_pin_src);
22045 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22046 +               goto out_dst;
22047 +       }
22048 +       goto out; /* success */
22049 +
22050 +out_dst:
22051 +       au_unpin(&a->mvd_pin_dst);
22052 +out:
22053 +       AuTraceErr(err);
22054 +       return err;
22055 +}
22056 +
22057 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
22058 +{
22059 +       if (!a->rename_lock)
22060 +               au_unpin(&a->mvd_pin_src);
22061 +       else {
22062 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22063 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22064 +               au_pin_hdir_lock(&a->mvd_pin_src);
22065 +               au_unpin(&a->mvd_pin_src);
22066 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22067 +       }
22068 +       au_unpin(&a->mvd_pin_dst);
22069 +}
22070 +
22071 +/* copy-down the file */
22072 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
22073 +{
22074 +       int err;
22075 +       struct au_cp_generic cpg = {
22076 +               .dentry = a->dentry,
22077 +               .bdst   = a->mvd_bdst,
22078 +               .bsrc   = a->mvd_bsrc,
22079 +               .len    = -1,
22080 +               .pin    = &a->mvd_pin_dst,
22081 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
22082 +       };
22083 +
22084 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
22085 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22086 +               au_fset_cpup(cpg.flags, OVERWRITE);
22087 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
22088 +               au_fset_cpup(cpg.flags, RWDST);
22089 +       err = au_sio_cpdown_simple(&cpg);
22090 +       if (unlikely(err))
22091 +               AU_MVD_PR(dmsg, "cpdown failed\n");
22092 +
22093 +       AuTraceErr(err);
22094 +       return err;
22095 +}
22096 +
22097 +/*
22098 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
22099 + * were sleeping
22100 + */
22101 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
22102 +{
22103 +       int err;
22104 +       struct path h_path;
22105 +       struct au_branch *br;
22106 +       struct inode *delegated;
22107 +
22108 +       br = au_sbr(a->sb, a->mvd_bdst);
22109 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
22110 +       err = PTR_ERR(h_path.dentry);
22111 +       if (IS_ERR(h_path.dentry)) {
22112 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
22113 +               goto out;
22114 +       }
22115 +
22116 +       err = 0;
22117 +       if (h_path.dentry->d_inode) {
22118 +               h_path.mnt = au_br_mnt(br);
22119 +               delegated = NULL;
22120 +               err = vfsub_unlink(a->mvd_h_dst_parent->d_inode, &h_path,
22121 +                                  &delegated, /*force*/0);
22122 +               if (unlikely(err == -EWOULDBLOCK)) {
22123 +                       pr_warn("cannot retry for NFSv4 delegation"
22124 +                               " for an internal unlink\n");
22125 +                       iput(delegated);
22126 +               }
22127 +               if (unlikely(err))
22128 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
22129 +       }
22130 +       dput(h_path.dentry);
22131 +
22132 +out:
22133 +       AuTraceErr(err);
22134 +       return err;
22135 +}
22136 +
22137 +/*
22138 + * unlink the topmost h_dentry
22139 + */
22140 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
22141 +{
22142 +       int err;
22143 +       struct path h_path;
22144 +       struct inode *delegated;
22145 +
22146 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
22147 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
22148 +       delegated = NULL;
22149 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
22150 +       if (unlikely(err == -EWOULDBLOCK)) {
22151 +               pr_warn("cannot retry for NFSv4 delegation"
22152 +                       " for an internal unlink\n");
22153 +               iput(delegated);
22154 +       }
22155 +       if (unlikely(err))
22156 +               AU_MVD_PR(dmsg, "unlink failed\n");
22157 +
22158 +       AuTraceErr(err);
22159 +       return err;
22160 +}
22161 +
22162 +/* Since mvdown succeeded, we ignore an error of this function */
22163 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
22164 +{
22165 +       int err;
22166 +       struct au_branch *br;
22167 +
22168 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
22169 +       br = au_sbr(a->sb, a->mvd_bsrc);
22170 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
22171 +       if (!err) {
22172 +               br = au_sbr(a->sb, a->mvd_bdst);
22173 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
22174 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
22175 +       }
22176 +       if (!err)
22177 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
22178 +       else
22179 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
22180 +}
22181 +
22182 +/*
22183 + * copy-down the file and unlink the bsrc file.
22184 + * - unlink the bdst whout if exist
22185 + * - copy-down the file (with whtmp name and rename)
22186 + * - unlink the bsrc file
22187 + */
22188 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
22189 +{
22190 +       int err;
22191 +
22192 +       err = au_do_mkdir(dmsg, a);
22193 +       if (!err)
22194 +               err = au_do_lock(dmsg, a);
22195 +       if (unlikely(err))
22196 +               goto out;
22197 +
22198 +       /*
22199 +        * do not revert the activities we made on bdst since they should be
22200 +        * harmless in aufs.
22201 +        */
22202 +
22203 +       err = au_do_cpdown(dmsg, a);
22204 +       if (!err)
22205 +               err = au_do_unlink_wh(dmsg, a);
22206 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
22207 +               err = au_do_unlink(dmsg, a);
22208 +       if (unlikely(err))
22209 +               goto out_unlock;
22210 +
22211 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
22212 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
22213 +       if (find_lower_writable(a) < 0)
22214 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
22215 +
22216 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
22217 +               au_do_stfs(dmsg, a);
22218 +
22219 +       /* maintain internal array */
22220 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
22221 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
22222 +               au_set_dbstart(a->dentry, a->mvd_bdst);
22223 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
22224 +               au_set_ibstart(a->inode, a->mvd_bdst);
22225 +       }
22226 +       if (au_dbend(a->dentry) < a->mvd_bdst)
22227 +               au_set_dbend(a->dentry, a->mvd_bdst);
22228 +       if (au_ibend(a->inode) < a->mvd_bdst)
22229 +               au_set_ibend(a->inode, a->mvd_bdst);
22230 +
22231 +out_unlock:
22232 +       au_do_unlock(dmsg, a);
22233 +out:
22234 +       AuTraceErr(err);
22235 +       return err;
22236 +}
22237 +
22238 +/* ---------------------------------------------------------------------- */
22239 +
22240 +/* make sure the file is idle */
22241 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
22242 +{
22243 +       int err, plinked;
22244 +
22245 +       err = 0;
22246 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
22247 +       if (au_dbstart(a->dentry) == a->mvd_bsrc
22248 +           && au_dcount(a->dentry) == 1
22249 +           && atomic_read(&a->inode->i_count) == 1
22250 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
22251 +           && (!plinked || !au_plink_test(a->inode))
22252 +           && a->inode->i_nlink == 1)
22253 +               goto out;
22254 +
22255 +       err = -EBUSY;
22256 +       AU_MVD_PR(dmsg,
22257 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
22258 +                 a->mvd_bsrc, au_dbstart(a->dentry), au_dcount(a->dentry),
22259 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
22260 +                 a->mvd_h_src_inode->i_nlink,
22261 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
22262 +
22263 +out:
22264 +       AuTraceErr(err);
22265 +       return err;
22266 +}
22267 +
22268 +/* make sure the parent dir is fine */
22269 +static int au_mvd_args_parent(const unsigned char dmsg,
22270 +                             struct au_mvd_args *a)
22271 +{
22272 +       int err;
22273 +       aufs_bindex_t bindex;
22274 +
22275 +       err = 0;
22276 +       if (unlikely(au_alive_dir(a->parent))) {
22277 +               err = -ENOENT;
22278 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
22279 +               goto out;
22280 +       }
22281 +
22282 +       a->bopq = au_dbdiropq(a->parent);
22283 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
22284 +       AuDbg("b%d\n", bindex);
22285 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
22286 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
22287 +               err = -EINVAL;
22288 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
22289 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
22290 +                         a->bopq, a->mvd_bdst);
22291 +       }
22292 +
22293 +out:
22294 +       AuTraceErr(err);
22295 +       return err;
22296 +}
22297 +
22298 +static int au_mvd_args_intermediate(const unsigned char dmsg,
22299 +                                   struct au_mvd_args *a)
22300 +{
22301 +       int err;
22302 +       struct au_dinfo *dinfo, *tmp;
22303 +
22304 +       /* lookup the next lower positive entry */
22305 +       err = -ENOMEM;
22306 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
22307 +       if (unlikely(!tmp))
22308 +               goto out;
22309 +
22310 +       a->bfound = -1;
22311 +       a->bwh = -1;
22312 +       dinfo = au_di(a->dentry);
22313 +       au_di_cp(tmp, dinfo);
22314 +       au_di_swap(tmp, dinfo);
22315 +
22316 +       /* returns the number of positive dentries */
22317 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
22318 +       if (!err)
22319 +               a->bwh = au_dbwh(a->dentry);
22320 +       else if (err > 0)
22321 +               a->bfound = au_dbstart(a->dentry);
22322 +
22323 +       au_di_swap(tmp, dinfo);
22324 +       au_rw_write_unlock(&tmp->di_rwsem);
22325 +       au_di_free(tmp);
22326 +       if (unlikely(err < 0))
22327 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
22328 +
22329 +       /*
22330 +        * here, we have these cases.
22331 +        * bfound == -1
22332 +        *      no positive dentry under bsrc. there are more sub-cases.
22333 +        *      bwh < 0
22334 +        *              there no whiteout, we can safely move-down.
22335 +        *      bwh <= bsrc
22336 +        *              impossible
22337 +        *      bsrc < bwh && bwh < bdst
22338 +        *              there is a whiteout on RO branch. cannot proceed.
22339 +        *      bwh == bdst
22340 +        *              there is a whiteout on the RW target branch. it should
22341 +        *              be removed.
22342 +        *      bdst < bwh
22343 +        *              there is a whiteout somewhere unrelated branch.
22344 +        * -1 < bfound && bfound <= bsrc
22345 +        *      impossible.
22346 +        * bfound < bdst
22347 +        *      found, but it is on RO branch between bsrc and bdst. cannot
22348 +        *      proceed.
22349 +        * bfound == bdst
22350 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
22351 +        *      error.
22352 +        * bdst < bfound
22353 +        *      found, after we create the file on bdst, it will be hidden.
22354 +        */
22355 +
22356 +       AuDebugOn(a->bfound == -1
22357 +                 && a->bwh != -1
22358 +                 && a->bwh <= a->mvd_bsrc);
22359 +       AuDebugOn(-1 < a->bfound
22360 +                 && a->bfound <= a->mvd_bsrc);
22361 +
22362 +       err = -EINVAL;
22363 +       if (a->bfound == -1
22364 +           && a->mvd_bsrc < a->bwh
22365 +           && a->bwh != -1
22366 +           && a->bwh < a->mvd_bdst) {
22367 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
22368 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
22369 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
22370 +               goto out;
22371 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
22372 +               a->mvd_errno = EAU_MVDOWN_UPPER;
22373 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
22374 +                         a->mvd_bdst, a->bfound);
22375 +               goto out;
22376 +       }
22377 +
22378 +       err = 0; /* success */
22379 +
22380 +out:
22381 +       AuTraceErr(err);
22382 +       return err;
22383 +}
22384 +
22385 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
22386 +{
22387 +       int err;
22388 +
22389 +       err = 0;
22390 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22391 +           && a->bfound == a->mvd_bdst)
22392 +               err = -EEXIST;
22393 +       AuTraceErr(err);
22394 +       return err;
22395 +}
22396 +
22397 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
22398 +{
22399 +       int err;
22400 +       struct au_branch *br;
22401 +
22402 +       err = -EISDIR;
22403 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
22404 +               goto out;
22405 +
22406 +       err = -EINVAL;
22407 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
22408 +               a->mvd_bsrc = au_ibstart(a->inode);
22409 +       else {
22410 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
22411 +               if (unlikely(a->mvd_bsrc < 0
22412 +                            || (a->mvd_bsrc < au_dbstart(a->dentry)
22413 +                                || au_dbend(a->dentry) < a->mvd_bsrc
22414 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
22415 +                            || (a->mvd_bsrc < au_ibstart(a->inode)
22416 +                                || au_ibend(a->inode) < a->mvd_bsrc
22417 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
22418 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
22419 +                       AU_MVD_PR(dmsg, "no upper\n");
22420 +                       goto out;
22421 +               }
22422 +       }
22423 +       if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
22424 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
22425 +               AU_MVD_PR(dmsg, "on the bottom\n");
22426 +               goto out;
22427 +       }
22428 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
22429 +       br = au_sbr(a->sb, a->mvd_bsrc);
22430 +       err = au_br_rdonly(br);
22431 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
22432 +               if (unlikely(err))
22433 +                       goto out;
22434 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
22435 +                    || IS_APPEND(a->mvd_h_src_inode))) {
22436 +               if (err)
22437 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
22438 +               /* go on */
22439 +       } else
22440 +               goto out;
22441 +
22442 +       err = -EINVAL;
22443 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
22444 +               a->mvd_bdst = find_lower_writable(a);
22445 +               if (unlikely(a->mvd_bdst < 0)) {
22446 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
22447 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
22448 +                       goto out;
22449 +               }
22450 +       } else {
22451 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
22452 +               if (unlikely(a->mvd_bdst < 0
22453 +                            || au_sbend(a->sb) < a->mvd_bdst)) {
22454 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
22455 +                       AU_MVD_PR(dmsg, "no lower brid\n");
22456 +                       goto out;
22457 +               }
22458 +       }
22459 +
22460 +       err = au_mvd_args_busy(dmsg, a);
22461 +       if (!err)
22462 +               err = au_mvd_args_parent(dmsg, a);
22463 +       if (!err)
22464 +               err = au_mvd_args_intermediate(dmsg, a);
22465 +       if (!err)
22466 +               err = au_mvd_args_exist(dmsg, a);
22467 +       if (!err)
22468 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
22469 +
22470 +out:
22471 +       AuTraceErr(err);
22472 +       return err;
22473 +}
22474 +
22475 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
22476 +{
22477 +       int err, e;
22478 +       unsigned char dmsg;
22479 +       struct au_mvd_args *args;
22480 +
22481 +       err = -EPERM;
22482 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
22483 +               goto out;
22484 +
22485 +       err = -ENOMEM;
22486 +       args = kmalloc(sizeof(*args), GFP_NOFS);
22487 +       if (unlikely(!args))
22488 +               goto out;
22489 +
22490 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
22491 +       if (!err)
22492 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
22493 +       if (unlikely(err)) {
22494 +               err = -EFAULT;
22495 +               AuTraceErr(err);
22496 +               goto out_free;
22497 +       }
22498 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
22499 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
22500 +       args->mvdown.au_errno = 0;
22501 +       args->dentry = dentry;
22502 +       args->inode = dentry->d_inode;
22503 +       args->sb = dentry->d_sb;
22504 +
22505 +       err = -ENOENT;
22506 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
22507 +       args->parent = dget_parent(dentry);
22508 +       args->dir = args->parent->d_inode;
22509 +       mutex_lock_nested(&args->dir->i_mutex, I_MUTEX_PARENT);
22510 +       dput(args->parent);
22511 +       if (unlikely(args->parent != dentry->d_parent)) {
22512 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
22513 +               goto out_dir;
22514 +       }
22515 +
22516 +       mutex_lock_nested(&args->inode->i_mutex, I_MUTEX_CHILD);
22517 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
22518 +       if (unlikely(err))
22519 +               goto out_inode;
22520 +
22521 +       di_write_lock_parent(args->parent);
22522 +       err = au_mvd_args(dmsg, args);
22523 +       if (unlikely(err))
22524 +               goto out_parent;
22525 +
22526 +       err = au_do_mvdown(dmsg, args);
22527 +       if (unlikely(err))
22528 +               goto out_parent;
22529 +
22530 +       au_cpup_attr_timesizes(args->dir);
22531 +       au_cpup_attr_timesizes(args->inode);
22532 +       au_cpup_igen(args->inode, au_h_iptr(args->inode, args->mvd_bdst));
22533 +       /* au_digen_dec(dentry); */
22534 +
22535 +out_parent:
22536 +       di_write_unlock(args->parent);
22537 +       aufs_read_unlock(dentry, AuLock_DW);
22538 +out_inode:
22539 +       mutex_unlock(&args->inode->i_mutex);
22540 +out_dir:
22541 +       mutex_unlock(&args->dir->i_mutex);
22542 +out_free:
22543 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
22544 +       if (unlikely(e))
22545 +               err = -EFAULT;
22546 +       kfree(args);
22547 +out:
22548 +       AuTraceErr(err);
22549 +       return err;
22550 +}
22551 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
22552 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
22553 +++ linux/fs/aufs/opts.c        2015-06-22 08:29:23.710091096 +0200
22554 @@ -0,0 +1,1854 @@
22555 +/*
22556 + * Copyright (C) 2005-2015 Junjiro R. Okajima
22557 + *
22558 + * This program, aufs is free software; you can redistribute it and/or modify
22559 + * it under the terms of the GNU General Public License as published by
22560 + * the Free Software Foundation; either version 2 of the License, or
22561 + * (at your option) any later version.
22562 + *
22563 + * This program is distributed in the hope that it will be useful,
22564 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22565 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22566 + * GNU General Public License for more details.
22567 + *
22568 + * You should have received a copy of the GNU General Public License
22569 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22570 + */
22571 +
22572 +/*
22573 + * mount options/flags
22574 + */
22575 +
22576 +#include <linux/namei.h>
22577 +#include <linux/types.h> /* a distribution requires */
22578 +#include <linux/parser.h>
22579 +#include "aufs.h"
22580 +
22581 +/* ---------------------------------------------------------------------- */
22582 +
22583 +enum {
22584 +       Opt_br,
22585 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
22586 +       Opt_idel, Opt_imod,
22587 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
22588 +       Opt_rdblk_def, Opt_rdhash_def,
22589 +       Opt_xino, Opt_noxino,
22590 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
22591 +       Opt_trunc_xino_path, Opt_itrunc_xino,
22592 +       Opt_trunc_xib, Opt_notrunc_xib,
22593 +       Opt_shwh, Opt_noshwh,
22594 +       Opt_plink, Opt_noplink, Opt_list_plink,
22595 +       Opt_udba,
22596 +       Opt_dio, Opt_nodio,
22597 +       Opt_diropq_a, Opt_diropq_w,
22598 +       Opt_warn_perm, Opt_nowarn_perm,
22599 +       Opt_wbr_copyup, Opt_wbr_create,
22600 +       Opt_fhsm_sec,
22601 +       Opt_refrof, Opt_norefrof,
22602 +       Opt_verbose, Opt_noverbose,
22603 +       Opt_sum, Opt_nosum, Opt_wsum,
22604 +       Opt_dirperm1, Opt_nodirperm1,
22605 +       Opt_acl, Opt_noacl,
22606 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
22607 +};
22608 +
22609 +static match_table_t options = {
22610 +       {Opt_br, "br=%s"},
22611 +       {Opt_br, "br:%s"},
22612 +
22613 +       {Opt_add, "add=%d:%s"},
22614 +       {Opt_add, "add:%d:%s"},
22615 +       {Opt_add, "ins=%d:%s"},
22616 +       {Opt_add, "ins:%d:%s"},
22617 +       {Opt_append, "append=%s"},
22618 +       {Opt_append, "append:%s"},
22619 +       {Opt_prepend, "prepend=%s"},
22620 +       {Opt_prepend, "prepend:%s"},
22621 +
22622 +       {Opt_del, "del=%s"},
22623 +       {Opt_del, "del:%s"},
22624 +       /* {Opt_idel, "idel:%d"}, */
22625 +       {Opt_mod, "mod=%s"},
22626 +       {Opt_mod, "mod:%s"},
22627 +       /* {Opt_imod, "imod:%d:%s"}, */
22628 +
22629 +       {Opt_dirwh, "dirwh=%d"},
22630 +
22631 +       {Opt_xino, "xino=%s"},
22632 +       {Opt_noxino, "noxino"},
22633 +       {Opt_trunc_xino, "trunc_xino"},
22634 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
22635 +       {Opt_notrunc_xino, "notrunc_xino"},
22636 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
22637 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
22638 +       /* {Opt_zxino, "zxino=%s"}, */
22639 +       {Opt_trunc_xib, "trunc_xib"},
22640 +       {Opt_notrunc_xib, "notrunc_xib"},
22641 +
22642 +#ifdef CONFIG_PROC_FS
22643 +       {Opt_plink, "plink"},
22644 +#else
22645 +       {Opt_ignore_silent, "plink"},
22646 +#endif
22647 +
22648 +       {Opt_noplink, "noplink"},
22649 +
22650 +#ifdef CONFIG_AUFS_DEBUG
22651 +       {Opt_list_plink, "list_plink"},
22652 +#endif
22653 +
22654 +       {Opt_udba, "udba=%s"},
22655 +
22656 +       {Opt_dio, "dio"},
22657 +       {Opt_nodio, "nodio"},
22658 +
22659 +#ifdef CONFIG_AUFS_FHSM
22660 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
22661 +#else
22662 +       {Opt_ignore_silent, "fhsm_sec=%d"},
22663 +#endif
22664 +
22665 +       {Opt_diropq_a, "diropq=always"},
22666 +       {Opt_diropq_a, "diropq=a"},
22667 +       {Opt_diropq_w, "diropq=whiteouted"},
22668 +       {Opt_diropq_w, "diropq=w"},
22669 +
22670 +       {Opt_warn_perm, "warn_perm"},
22671 +       {Opt_nowarn_perm, "nowarn_perm"},
22672 +
22673 +       /* keep them temporary */
22674 +       {Opt_ignore_silent, "nodlgt"},
22675 +       {Opt_ignore_silent, "clean_plink"},
22676 +
22677 +#ifdef CONFIG_AUFS_SHWH
22678 +       {Opt_shwh, "shwh"},
22679 +#endif
22680 +       {Opt_noshwh, "noshwh"},
22681 +
22682 +       {Opt_dirperm1, "dirperm1"},
22683 +       {Opt_nodirperm1, "nodirperm1"},
22684 +
22685 +       {Opt_refrof, "refrof"},
22686 +       {Opt_norefrof, "norefrof"},
22687 +
22688 +       {Opt_verbose, "verbose"},
22689 +       {Opt_verbose, "v"},
22690 +       {Opt_noverbose, "noverbose"},
22691 +       {Opt_noverbose, "quiet"},
22692 +       {Opt_noverbose, "q"},
22693 +       {Opt_noverbose, "silent"},
22694 +
22695 +       {Opt_sum, "sum"},
22696 +       {Opt_nosum, "nosum"},
22697 +       {Opt_wsum, "wsum"},
22698 +
22699 +       {Opt_rdcache, "rdcache=%d"},
22700 +       {Opt_rdblk, "rdblk=%d"},
22701 +       {Opt_rdblk_def, "rdblk=def"},
22702 +       {Opt_rdhash, "rdhash=%d"},
22703 +       {Opt_rdhash_def, "rdhash=def"},
22704 +
22705 +       {Opt_wbr_create, "create=%s"},
22706 +       {Opt_wbr_create, "create_policy=%s"},
22707 +       {Opt_wbr_copyup, "cpup=%s"},
22708 +       {Opt_wbr_copyup, "copyup=%s"},
22709 +       {Opt_wbr_copyup, "copyup_policy=%s"},
22710 +
22711 +       /* generic VFS flag */
22712 +#ifdef CONFIG_FS_POSIX_ACL
22713 +       {Opt_acl, "acl"},
22714 +       {Opt_noacl, "noacl"},
22715 +#else
22716 +       {Opt_ignore_silent, "acl"},
22717 +       {Opt_ignore_silent, "noacl"},
22718 +#endif
22719 +
22720 +       /* internal use for the scripts */
22721 +       {Opt_ignore_silent, "si=%s"},
22722 +
22723 +       {Opt_br, "dirs=%s"},
22724 +       {Opt_ignore, "debug=%d"},
22725 +       {Opt_ignore, "delete=whiteout"},
22726 +       {Opt_ignore, "delete=all"},
22727 +       {Opt_ignore, "imap=%s"},
22728 +
22729 +       /* temporary workaround, due to old mount(8)? */
22730 +       {Opt_ignore_silent, "relatime"},
22731 +
22732 +       {Opt_err, NULL}
22733 +};
22734 +
22735 +/* ---------------------------------------------------------------------- */
22736 +
22737 +static const char *au_parser_pattern(int val, match_table_t tbl)
22738 +{
22739 +       struct match_token *p;
22740 +
22741 +       p = tbl;
22742 +       while (p->pattern) {
22743 +               if (p->token == val)
22744 +                       return p->pattern;
22745 +               p++;
22746 +       }
22747 +       BUG();
22748 +       return "??";
22749 +}
22750 +
22751 +static const char *au_optstr(int *val, match_table_t tbl)
22752 +{
22753 +       struct match_token *p;
22754 +       int v;
22755 +
22756 +       v = *val;
22757 +       if (!v)
22758 +               goto out;
22759 +       p = tbl;
22760 +       while (p->pattern) {
22761 +               if (p->token
22762 +                   && (v & p->token) == p->token) {
22763 +                       *val &= ~p->token;
22764 +                       return p->pattern;
22765 +               }
22766 +               p++;
22767 +       }
22768 +
22769 +out:
22770 +       return NULL;
22771 +}
22772 +
22773 +/* ---------------------------------------------------------------------- */
22774 +
22775 +static match_table_t brperm = {
22776 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
22777 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
22778 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
22779 +       {0, NULL}
22780 +};
22781 +
22782 +static match_table_t brattr = {
22783 +       /* general */
22784 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
22785 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
22786 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
22787 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
22788 +#ifdef CONFIG_AUFS_FHSM
22789 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
22790 +#endif
22791 +#ifdef CONFIG_AUFS_XATTR
22792 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
22793 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
22794 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
22795 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
22796 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
22797 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
22798 +#endif
22799 +
22800 +       /* ro/rr branch */
22801 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
22802 +
22803 +       /* rw branch */
22804 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
22805 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
22806 +
22807 +       {0, NULL}
22808 +};
22809 +
22810 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
22811 +{
22812 +       int attr, v;
22813 +       char *p;
22814 +
22815 +       attr = 0;
22816 +       do {
22817 +               p = strchr(str, '+');
22818 +               if (p)
22819 +                       *p = 0;
22820 +               v = match_token(str, table, args);
22821 +               if (v) {
22822 +                       if (v & AuBrAttr_CMOO_Mask)
22823 +                               attr &= ~AuBrAttr_CMOO_Mask;
22824 +                       attr |= v;
22825 +               } else {
22826 +                       if (p)
22827 +                               *p = '+';
22828 +                       pr_warn("ignored branch attribute %s\n", str);
22829 +                       break;
22830 +               }
22831 +               if (p)
22832 +                       str = p + 1;
22833 +       } while (p);
22834 +
22835 +       return attr;
22836 +}
22837 +
22838 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
22839 +{
22840 +       int sz;
22841 +       const char *p;
22842 +       char *q;
22843 +
22844 +       q = str->a;
22845 +       *q = 0;
22846 +       p = au_optstr(&perm, brattr);
22847 +       if (p) {
22848 +               sz = strlen(p);
22849 +               memcpy(q, p, sz + 1);
22850 +               q += sz;
22851 +       } else
22852 +               goto out;
22853 +
22854 +       do {
22855 +               p = au_optstr(&perm, brattr);
22856 +               if (p) {
22857 +                       *q++ = '+';
22858 +                       sz = strlen(p);
22859 +                       memcpy(q, p, sz + 1);
22860 +                       q += sz;
22861 +               }
22862 +       } while (p);
22863 +
22864 +out:
22865 +       return q - str->a;
22866 +}
22867 +
22868 +static int noinline_for_stack br_perm_val(char *perm)
22869 +{
22870 +       int val, bad, sz;
22871 +       char *p;
22872 +       substring_t args[MAX_OPT_ARGS];
22873 +       au_br_perm_str_t attr;
22874 +
22875 +       p = strchr(perm, '+');
22876 +       if (p)
22877 +               *p = 0;
22878 +       val = match_token(perm, brperm, args);
22879 +       if (!val) {
22880 +               if (p)
22881 +                       *p = '+';
22882 +               pr_warn("ignored branch permission %s\n", perm);
22883 +               val = AuBrPerm_RO;
22884 +               goto out;
22885 +       }
22886 +       if (!p)
22887 +               goto out;
22888 +
22889 +       val |= br_attr_val(p + 1, brattr, args);
22890 +
22891 +       bad = 0;
22892 +       switch (val & AuBrPerm_Mask) {
22893 +       case AuBrPerm_RO:
22894 +       case AuBrPerm_RR:
22895 +               bad = val & AuBrWAttr_Mask;
22896 +               val &= ~AuBrWAttr_Mask;
22897 +               break;
22898 +       case AuBrPerm_RW:
22899 +               bad = val & AuBrRAttr_Mask;
22900 +               val &= ~AuBrRAttr_Mask;
22901 +               break;
22902 +       }
22903 +
22904 +       /*
22905 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
22906 +        * does not treat it as an error, just warning.
22907 +        * this is a tiny guard for the user operation.
22908 +        */
22909 +       if (val & AuBrAttr_UNPIN) {
22910 +               bad |= AuBrAttr_UNPIN;
22911 +               val &= ~AuBrAttr_UNPIN;
22912 +       }
22913 +
22914 +       if (unlikely(bad)) {
22915 +               sz = au_do_optstr_br_attr(&attr, bad);
22916 +               AuDebugOn(!sz);
22917 +               pr_warn("ignored branch attribute %s\n", attr.a);
22918 +       }
22919 +
22920 +out:
22921 +       return val;
22922 +}
22923 +
22924 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
22925 +{
22926 +       au_br_perm_str_t attr;
22927 +       const char *p;
22928 +       char *q;
22929 +       int sz;
22930 +
22931 +       q = str->a;
22932 +       p = au_optstr(&perm, brperm);
22933 +       AuDebugOn(!p || !*p);
22934 +       sz = strlen(p);
22935 +       memcpy(q, p, sz + 1);
22936 +       q += sz;
22937 +
22938 +       sz = au_do_optstr_br_attr(&attr, perm);
22939 +       if (sz) {
22940 +               *q++ = '+';
22941 +               memcpy(q, attr.a, sz + 1);
22942 +       }
22943 +
22944 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
22945 +}
22946 +
22947 +/* ---------------------------------------------------------------------- */
22948 +
22949 +static match_table_t udbalevel = {
22950 +       {AuOpt_UDBA_REVAL, "reval"},
22951 +       {AuOpt_UDBA_NONE, "none"},
22952 +#ifdef CONFIG_AUFS_HNOTIFY
22953 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
22954 +#ifdef CONFIG_AUFS_HFSNOTIFY
22955 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
22956 +#endif
22957 +#endif
22958 +       {-1, NULL}
22959 +};
22960 +
22961 +static int noinline_for_stack udba_val(char *str)
22962 +{
22963 +       substring_t args[MAX_OPT_ARGS];
22964 +
22965 +       return match_token(str, udbalevel, args);
22966 +}
22967 +
22968 +const char *au_optstr_udba(int udba)
22969 +{
22970 +       return au_parser_pattern(udba, udbalevel);
22971 +}
22972 +
22973 +/* ---------------------------------------------------------------------- */
22974 +
22975 +static match_table_t au_wbr_create_policy = {
22976 +       {AuWbrCreate_TDP, "tdp"},
22977 +       {AuWbrCreate_TDP, "top-down-parent"},
22978 +       {AuWbrCreate_RR, "rr"},
22979 +       {AuWbrCreate_RR, "round-robin"},
22980 +       {AuWbrCreate_MFS, "mfs"},
22981 +       {AuWbrCreate_MFS, "most-free-space"},
22982 +       {AuWbrCreate_MFSV, "mfs:%d"},
22983 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
22984 +
22985 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
22986 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
22987 +       {AuWbrCreate_PMFS, "pmfs"},
22988 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
22989 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
22990 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
22991 +
22992 +       {-1, NULL}
22993 +};
22994 +
22995 +/*
22996 + * cf. linux/lib/parser.c and cmdline.c
22997 + * gave up calling memparse() since it uses simple_strtoull() instead of
22998 + * kstrto...().
22999 + */
23000 +static int noinline_for_stack
23001 +au_match_ull(substring_t *s, unsigned long long *result)
23002 +{
23003 +       int err;
23004 +       unsigned int len;
23005 +       char a[32];
23006 +
23007 +       err = -ERANGE;
23008 +       len = s->to - s->from;
23009 +       if (len + 1 <= sizeof(a)) {
23010 +               memcpy(a, s->from, len);
23011 +               a[len] = '\0';
23012 +               err = kstrtoull(a, 0, result);
23013 +       }
23014 +       return err;
23015 +}
23016 +
23017 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
23018 +                           struct au_opt_wbr_create *create)
23019 +{
23020 +       int err;
23021 +       unsigned long long ull;
23022 +
23023 +       err = 0;
23024 +       if (!au_match_ull(arg, &ull))
23025 +               create->mfsrr_watermark = ull;
23026 +       else {
23027 +               pr_err("bad integer in %s\n", str);
23028 +               err = -EINVAL;
23029 +       }
23030 +
23031 +       return err;
23032 +}
23033 +
23034 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
23035 +                         struct au_opt_wbr_create *create)
23036 +{
23037 +       int n, err;
23038 +
23039 +       err = 0;
23040 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
23041 +               create->mfs_second = n;
23042 +       else {
23043 +               pr_err("bad integer in %s\n", str);
23044 +               err = -EINVAL;
23045 +       }
23046 +
23047 +       return err;
23048 +}
23049 +
23050 +static int noinline_for_stack
23051 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
23052 +{
23053 +       int err, e;
23054 +       substring_t args[MAX_OPT_ARGS];
23055 +
23056 +       err = match_token(str, au_wbr_create_policy, args);
23057 +       create->wbr_create = err;
23058 +       switch (err) {
23059 +       case AuWbrCreate_MFSRRV:
23060 +       case AuWbrCreate_PMFSRRV:
23061 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23062 +               if (!e)
23063 +                       e = au_wbr_mfs_sec(&args[1], str, create);
23064 +               if (unlikely(e))
23065 +                       err = e;
23066 +               break;
23067 +       case AuWbrCreate_MFSRR:
23068 +       case AuWbrCreate_PMFSRR:
23069 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23070 +               if (unlikely(e)) {
23071 +                       err = e;
23072 +                       break;
23073 +               }
23074 +               /*FALLTHROUGH*/
23075 +       case AuWbrCreate_MFS:
23076 +       case AuWbrCreate_PMFS:
23077 +               create->mfs_second = AUFS_MFS_DEF_SEC;
23078 +               break;
23079 +       case AuWbrCreate_MFSV:
23080 +       case AuWbrCreate_PMFSV:
23081 +               e = au_wbr_mfs_sec(&args[0], str, create);
23082 +               if (unlikely(e))
23083 +                       err = e;
23084 +               break;
23085 +       }
23086 +
23087 +       return err;
23088 +}
23089 +
23090 +const char *au_optstr_wbr_create(int wbr_create)
23091 +{
23092 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
23093 +}
23094 +
23095 +static match_table_t au_wbr_copyup_policy = {
23096 +       {AuWbrCopyup_TDP, "tdp"},
23097 +       {AuWbrCopyup_TDP, "top-down-parent"},
23098 +       {AuWbrCopyup_BUP, "bup"},
23099 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
23100 +       {AuWbrCopyup_BU, "bu"},
23101 +       {AuWbrCopyup_BU, "bottom-up"},
23102 +       {-1, NULL}
23103 +};
23104 +
23105 +static int noinline_for_stack au_wbr_copyup_val(char *str)
23106 +{
23107 +       substring_t args[MAX_OPT_ARGS];
23108 +
23109 +       return match_token(str, au_wbr_copyup_policy, args);
23110 +}
23111 +
23112 +const char *au_optstr_wbr_copyup(int wbr_copyup)
23113 +{
23114 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
23115 +}
23116 +
23117 +/* ---------------------------------------------------------------------- */
23118 +
23119 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
23120 +
23121 +static void dump_opts(struct au_opts *opts)
23122 +{
23123 +#ifdef CONFIG_AUFS_DEBUG
23124 +       /* reduce stack space */
23125 +       union {
23126 +               struct au_opt_add *add;
23127 +               struct au_opt_del *del;
23128 +               struct au_opt_mod *mod;
23129 +               struct au_opt_xino *xino;
23130 +               struct au_opt_xino_itrunc *xino_itrunc;
23131 +               struct au_opt_wbr_create *create;
23132 +       } u;
23133 +       struct au_opt *opt;
23134 +
23135 +       opt = opts->opt;
23136 +       while (opt->type != Opt_tail) {
23137 +               switch (opt->type) {
23138 +               case Opt_add:
23139 +                       u.add = &opt->add;
23140 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
23141 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23142 +                                 u.add->path.dentry);
23143 +                       break;
23144 +               case Opt_del:
23145 +               case Opt_idel:
23146 +                       u.del = &opt->del;
23147 +                       AuDbg("del {%s, %p}\n",
23148 +                             u.del->pathname, u.del->h_path.dentry);
23149 +                       break;
23150 +               case Opt_mod:
23151 +               case Opt_imod:
23152 +                       u.mod = &opt->mod;
23153 +                       AuDbg("mod {%s, 0x%x, %p}\n",
23154 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
23155 +                       break;
23156 +               case Opt_append:
23157 +                       u.add = &opt->add;
23158 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
23159 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23160 +                                 u.add->path.dentry);
23161 +                       break;
23162 +               case Opt_prepend:
23163 +                       u.add = &opt->add;
23164 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
23165 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23166 +                                 u.add->path.dentry);
23167 +                       break;
23168 +               case Opt_dirwh:
23169 +                       AuDbg("dirwh %d\n", opt->dirwh);
23170 +                       break;
23171 +               case Opt_rdcache:
23172 +                       AuDbg("rdcache %d\n", opt->rdcache);
23173 +                       break;
23174 +               case Opt_rdblk:
23175 +                       AuDbg("rdblk %u\n", opt->rdblk);
23176 +                       break;
23177 +               case Opt_rdblk_def:
23178 +                       AuDbg("rdblk_def\n");
23179 +                       break;
23180 +               case Opt_rdhash:
23181 +                       AuDbg("rdhash %u\n", opt->rdhash);
23182 +                       break;
23183 +               case Opt_rdhash_def:
23184 +                       AuDbg("rdhash_def\n");
23185 +                       break;
23186 +               case Opt_xino:
23187 +                       u.xino = &opt->xino;
23188 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
23189 +                       break;
23190 +               case Opt_trunc_xino:
23191 +                       AuLabel(trunc_xino);
23192 +                       break;
23193 +               case Opt_notrunc_xino:
23194 +                       AuLabel(notrunc_xino);
23195 +                       break;
23196 +               case Opt_trunc_xino_path:
23197 +               case Opt_itrunc_xino:
23198 +                       u.xino_itrunc = &opt->xino_itrunc;
23199 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
23200 +                       break;
23201 +               case Opt_noxino:
23202 +                       AuLabel(noxino);
23203 +                       break;
23204 +               case Opt_trunc_xib:
23205 +                       AuLabel(trunc_xib);
23206 +                       break;
23207 +               case Opt_notrunc_xib:
23208 +                       AuLabel(notrunc_xib);
23209 +                       break;
23210 +               case Opt_shwh:
23211 +                       AuLabel(shwh);
23212 +                       break;
23213 +               case Opt_noshwh:
23214 +                       AuLabel(noshwh);
23215 +                       break;
23216 +               case Opt_dirperm1:
23217 +                       AuLabel(dirperm1);
23218 +                       break;
23219 +               case Opt_nodirperm1:
23220 +                       AuLabel(nodirperm1);
23221 +                       break;
23222 +               case Opt_plink:
23223 +                       AuLabel(plink);
23224 +                       break;
23225 +               case Opt_noplink:
23226 +                       AuLabel(noplink);
23227 +                       break;
23228 +               case Opt_list_plink:
23229 +                       AuLabel(list_plink);
23230 +                       break;
23231 +               case Opt_udba:
23232 +                       AuDbg("udba %d, %s\n",
23233 +                                 opt->udba, au_optstr_udba(opt->udba));
23234 +                       break;
23235 +               case Opt_dio:
23236 +                       AuLabel(dio);
23237 +                       break;
23238 +               case Opt_nodio:
23239 +                       AuLabel(nodio);
23240 +                       break;
23241 +               case Opt_diropq_a:
23242 +                       AuLabel(diropq_a);
23243 +                       break;
23244 +               case Opt_diropq_w:
23245 +                       AuLabel(diropq_w);
23246 +                       break;
23247 +               case Opt_warn_perm:
23248 +                       AuLabel(warn_perm);
23249 +                       break;
23250 +               case Opt_nowarn_perm:
23251 +                       AuLabel(nowarn_perm);
23252 +                       break;
23253 +               case Opt_refrof:
23254 +                       AuLabel(refrof);
23255 +                       break;
23256 +               case Opt_norefrof:
23257 +                       AuLabel(norefrof);
23258 +                       break;
23259 +               case Opt_verbose:
23260 +                       AuLabel(verbose);
23261 +                       break;
23262 +               case Opt_noverbose:
23263 +                       AuLabel(noverbose);
23264 +                       break;
23265 +               case Opt_sum:
23266 +                       AuLabel(sum);
23267 +                       break;
23268 +               case Opt_nosum:
23269 +                       AuLabel(nosum);
23270 +                       break;
23271 +               case Opt_wsum:
23272 +                       AuLabel(wsum);
23273 +                       break;
23274 +               case Opt_wbr_create:
23275 +                       u.create = &opt->wbr_create;
23276 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
23277 +                                 au_optstr_wbr_create(u.create->wbr_create));
23278 +                       switch (u.create->wbr_create) {
23279 +                       case AuWbrCreate_MFSV:
23280 +                       case AuWbrCreate_PMFSV:
23281 +                               AuDbg("%d sec\n", u.create->mfs_second);
23282 +                               break;
23283 +                       case AuWbrCreate_MFSRR:
23284 +                               AuDbg("%llu watermark\n",
23285 +                                         u.create->mfsrr_watermark);
23286 +                               break;
23287 +                       case AuWbrCreate_MFSRRV:
23288 +                       case AuWbrCreate_PMFSRRV:
23289 +                               AuDbg("%llu watermark, %d sec\n",
23290 +                                         u.create->mfsrr_watermark,
23291 +                                         u.create->mfs_second);
23292 +                               break;
23293 +                       }
23294 +                       break;
23295 +               case Opt_wbr_copyup:
23296 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
23297 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
23298 +                       break;
23299 +               case Opt_fhsm_sec:
23300 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
23301 +                       break;
23302 +               case Opt_acl:
23303 +                       AuLabel(acl);
23304 +                       break;
23305 +               case Opt_noacl:
23306 +                       AuLabel(noacl);
23307 +                       break;
23308 +               default:
23309 +                       BUG();
23310 +               }
23311 +               opt++;
23312 +       }
23313 +#endif
23314 +}
23315 +
23316 +void au_opts_free(struct au_opts *opts)
23317 +{
23318 +       struct au_opt *opt;
23319 +
23320 +       opt = opts->opt;
23321 +       while (opt->type != Opt_tail) {
23322 +               switch (opt->type) {
23323 +               case Opt_add:
23324 +               case Opt_append:
23325 +               case Opt_prepend:
23326 +                       path_put(&opt->add.path);
23327 +                       break;
23328 +               case Opt_del:
23329 +               case Opt_idel:
23330 +                       path_put(&opt->del.h_path);
23331 +                       break;
23332 +               case Opt_mod:
23333 +               case Opt_imod:
23334 +                       dput(opt->mod.h_root);
23335 +                       break;
23336 +               case Opt_xino:
23337 +                       fput(opt->xino.file);
23338 +                       break;
23339 +               }
23340 +               opt++;
23341 +       }
23342 +}
23343 +
23344 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
23345 +                  aufs_bindex_t bindex)
23346 +{
23347 +       int err;
23348 +       struct au_opt_add *add = &opt->add;
23349 +       char *p;
23350 +
23351 +       add->bindex = bindex;
23352 +       add->perm = AuBrPerm_RO;
23353 +       add->pathname = opt_str;
23354 +       p = strchr(opt_str, '=');
23355 +       if (p) {
23356 +               *p++ = 0;
23357 +               if (*p)
23358 +                       add->perm = br_perm_val(p);
23359 +       }
23360 +
23361 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
23362 +       if (!err) {
23363 +               if (!p) {
23364 +                       add->perm = AuBrPerm_RO;
23365 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
23366 +                               add->perm = AuBrPerm_RR;
23367 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
23368 +                               add->perm = AuBrPerm_RW;
23369 +               }
23370 +               opt->type = Opt_add;
23371 +               goto out;
23372 +       }
23373 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
23374 +       err = -EINVAL;
23375 +
23376 +out:
23377 +       return err;
23378 +}
23379 +
23380 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
23381 +{
23382 +       int err;
23383 +
23384 +       del->pathname = args[0].from;
23385 +       AuDbg("del path %s\n", del->pathname);
23386 +
23387 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
23388 +       if (unlikely(err))
23389 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
23390 +
23391 +       return err;
23392 +}
23393 +
23394 +#if 0 /* reserved for future use */
23395 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
23396 +                             struct au_opt_del *del, substring_t args[])
23397 +{
23398 +       int err;
23399 +       struct dentry *root;
23400 +
23401 +       err = -EINVAL;
23402 +       root = sb->s_root;
23403 +       aufs_read_lock(root, AuLock_FLUSH);
23404 +       if (bindex < 0 || au_sbend(sb) < bindex) {
23405 +               pr_err("out of bounds, %d\n", bindex);
23406 +               goto out;
23407 +       }
23408 +
23409 +       err = 0;
23410 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
23411 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
23412 +
23413 +out:
23414 +       aufs_read_unlock(root, !AuLock_IR);
23415 +       return err;
23416 +}
23417 +#endif
23418 +
23419 +static int noinline_for_stack
23420 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
23421 +{
23422 +       int err;
23423 +       struct path path;
23424 +       char *p;
23425 +
23426 +       err = -EINVAL;
23427 +       mod->path = args[0].from;
23428 +       p = strchr(mod->path, '=');
23429 +       if (unlikely(!p)) {
23430 +               pr_err("no permssion %s\n", args[0].from);
23431 +               goto out;
23432 +       }
23433 +
23434 +       *p++ = 0;
23435 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
23436 +       if (unlikely(err)) {
23437 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
23438 +               goto out;
23439 +       }
23440 +
23441 +       mod->perm = br_perm_val(p);
23442 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
23443 +       mod->h_root = dget(path.dentry);
23444 +       path_put(&path);
23445 +
23446 +out:
23447 +       return err;
23448 +}
23449 +
23450 +#if 0 /* reserved for future use */
23451 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
23452 +                             struct au_opt_mod *mod, substring_t args[])
23453 +{
23454 +       int err;
23455 +       struct dentry *root;
23456 +
23457 +       err = -EINVAL;
23458 +       root = sb->s_root;
23459 +       aufs_read_lock(root, AuLock_FLUSH);
23460 +       if (bindex < 0 || au_sbend(sb) < bindex) {
23461 +               pr_err("out of bounds, %d\n", bindex);
23462 +               goto out;
23463 +       }
23464 +
23465 +       err = 0;
23466 +       mod->perm = br_perm_val(args[1].from);
23467 +       AuDbg("mod path %s, perm 0x%x, %s\n",
23468 +             mod->path, mod->perm, args[1].from);
23469 +       mod->h_root = dget(au_h_dptr(root, bindex));
23470 +
23471 +out:
23472 +       aufs_read_unlock(root, !AuLock_IR);
23473 +       return err;
23474 +}
23475 +#endif
23476 +
23477 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
23478 +                             substring_t args[])
23479 +{
23480 +       int err;
23481 +       struct file *file;
23482 +
23483 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
23484 +       err = PTR_ERR(file);
23485 +       if (IS_ERR(file))
23486 +               goto out;
23487 +
23488 +       err = -EINVAL;
23489 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
23490 +               fput(file);
23491 +               pr_err("%s must be outside\n", args[0].from);
23492 +               goto out;
23493 +       }
23494 +
23495 +       err = 0;
23496 +       xino->file = file;
23497 +       xino->path = args[0].from;
23498 +
23499 +out:
23500 +       return err;
23501 +}
23502 +
23503 +static int noinline_for_stack
23504 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
23505 +                              struct au_opt_xino_itrunc *xino_itrunc,
23506 +                              substring_t args[])
23507 +{
23508 +       int err;
23509 +       aufs_bindex_t bend, bindex;
23510 +       struct path path;
23511 +       struct dentry *root;
23512 +
23513 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
23514 +       if (unlikely(err)) {
23515 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
23516 +               goto out;
23517 +       }
23518 +
23519 +       xino_itrunc->bindex = -1;
23520 +       root = sb->s_root;
23521 +       aufs_read_lock(root, AuLock_FLUSH);
23522 +       bend = au_sbend(sb);
23523 +       for (bindex = 0; bindex <= bend; bindex++) {
23524 +               if (au_h_dptr(root, bindex) == path.dentry) {
23525 +                       xino_itrunc->bindex = bindex;
23526 +                       break;
23527 +               }
23528 +       }
23529 +       aufs_read_unlock(root, !AuLock_IR);
23530 +       path_put(&path);
23531 +
23532 +       if (unlikely(xino_itrunc->bindex < 0)) {
23533 +               pr_err("no such branch %s\n", args[0].from);
23534 +               err = -EINVAL;
23535 +       }
23536 +
23537 +out:
23538 +       return err;
23539 +}
23540 +
23541 +/* called without aufs lock */
23542 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
23543 +{
23544 +       int err, n, token;
23545 +       aufs_bindex_t bindex;
23546 +       unsigned char skipped;
23547 +       struct dentry *root;
23548 +       struct au_opt *opt, *opt_tail;
23549 +       char *opt_str;
23550 +       /* reduce the stack space */
23551 +       union {
23552 +               struct au_opt_xino_itrunc *xino_itrunc;
23553 +               struct au_opt_wbr_create *create;
23554 +       } u;
23555 +       struct {
23556 +               substring_t args[MAX_OPT_ARGS];
23557 +       } *a;
23558 +
23559 +       err = -ENOMEM;
23560 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23561 +       if (unlikely(!a))
23562 +               goto out;
23563 +
23564 +       root = sb->s_root;
23565 +       err = 0;
23566 +       bindex = 0;
23567 +       opt = opts->opt;
23568 +       opt_tail = opt + opts->max_opt - 1;
23569 +       opt->type = Opt_tail;
23570 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
23571 +               err = -EINVAL;
23572 +               skipped = 0;
23573 +               token = match_token(opt_str, options, a->args);
23574 +               switch (token) {
23575 +               case Opt_br:
23576 +                       err = 0;
23577 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
23578 +                              && *opt_str) {
23579 +                               err = opt_add(opt, opt_str, opts->sb_flags,
23580 +                                             bindex++);
23581 +                               if (unlikely(!err && ++opt > opt_tail)) {
23582 +                                       err = -E2BIG;
23583 +                                       break;
23584 +                               }
23585 +                               opt->type = Opt_tail;
23586 +                               skipped = 1;
23587 +                       }
23588 +                       break;
23589 +               case Opt_add:
23590 +                       if (unlikely(match_int(&a->args[0], &n))) {
23591 +                               pr_err("bad integer in %s\n", opt_str);
23592 +                               break;
23593 +                       }
23594 +                       bindex = n;
23595 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
23596 +                                     bindex);
23597 +                       if (!err)
23598 +                               opt->type = token;
23599 +                       break;
23600 +               case Opt_append:
23601 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23602 +                                     /*dummy bindex*/1);
23603 +                       if (!err)
23604 +                               opt->type = token;
23605 +                       break;
23606 +               case Opt_prepend:
23607 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23608 +                                     /*bindex*/0);
23609 +                       if (!err)
23610 +                               opt->type = token;
23611 +                       break;
23612 +               case Opt_del:
23613 +                       err = au_opts_parse_del(&opt->del, a->args);
23614 +                       if (!err)
23615 +                               opt->type = token;
23616 +                       break;
23617 +#if 0 /* reserved for future use */
23618 +               case Opt_idel:
23619 +                       del->pathname = "(indexed)";
23620 +                       if (unlikely(match_int(&args[0], &n))) {
23621 +                               pr_err("bad integer in %s\n", opt_str);
23622 +                               break;
23623 +                       }
23624 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
23625 +                       if (!err)
23626 +                               opt->type = token;
23627 +                       break;
23628 +#endif
23629 +               case Opt_mod:
23630 +                       err = au_opts_parse_mod(&opt->mod, a->args);
23631 +                       if (!err)
23632 +                               opt->type = token;
23633 +                       break;
23634 +#ifdef IMOD /* reserved for future use */
23635 +               case Opt_imod:
23636 +                       u.mod->path = "(indexed)";
23637 +                       if (unlikely(match_int(&a->args[0], &n))) {
23638 +                               pr_err("bad integer in %s\n", opt_str);
23639 +                               break;
23640 +                       }
23641 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
23642 +                       if (!err)
23643 +                               opt->type = token;
23644 +                       break;
23645 +#endif
23646 +               case Opt_xino:
23647 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
23648 +                       if (!err)
23649 +                               opt->type = token;
23650 +                       break;
23651 +
23652 +               case Opt_trunc_xino_path:
23653 +                       err = au_opts_parse_xino_itrunc_path
23654 +                               (sb, &opt->xino_itrunc, a->args);
23655 +                       if (!err)
23656 +                               opt->type = token;
23657 +                       break;
23658 +
23659 +               case Opt_itrunc_xino:
23660 +                       u.xino_itrunc = &opt->xino_itrunc;
23661 +                       if (unlikely(match_int(&a->args[0], &n))) {
23662 +                               pr_err("bad integer in %s\n", opt_str);
23663 +                               break;
23664 +                       }
23665 +                       u.xino_itrunc->bindex = n;
23666 +                       aufs_read_lock(root, AuLock_FLUSH);
23667 +                       if (n < 0 || au_sbend(sb) < n) {
23668 +                               pr_err("out of bounds, %d\n", n);
23669 +                               aufs_read_unlock(root, !AuLock_IR);
23670 +                               break;
23671 +                       }
23672 +                       aufs_read_unlock(root, !AuLock_IR);
23673 +                       err = 0;
23674 +                       opt->type = token;
23675 +                       break;
23676 +
23677 +               case Opt_dirwh:
23678 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
23679 +                               break;
23680 +                       err = 0;
23681 +                       opt->type = token;
23682 +                       break;
23683 +
23684 +               case Opt_rdcache:
23685 +                       if (unlikely(match_int(&a->args[0], &n))) {
23686 +                               pr_err("bad integer in %s\n", opt_str);
23687 +                               break;
23688 +                       }
23689 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
23690 +                               pr_err("rdcache must be smaller than %d\n",
23691 +                                      AUFS_RDCACHE_MAX);
23692 +                               break;
23693 +                       }
23694 +                       opt->rdcache = n;
23695 +                       err = 0;
23696 +                       opt->type = token;
23697 +                       break;
23698 +               case Opt_rdblk:
23699 +                       if (unlikely(match_int(&a->args[0], &n)
23700 +                                    || n < 0
23701 +                                    || n > KMALLOC_MAX_SIZE)) {
23702 +                               pr_err("bad integer in %s\n", opt_str);
23703 +                               break;
23704 +                       }
23705 +                       if (unlikely(n && n < NAME_MAX)) {
23706 +                               pr_err("rdblk must be larger than %d\n",
23707 +                                      NAME_MAX);
23708 +                               break;
23709 +                       }
23710 +                       opt->rdblk = n;
23711 +                       err = 0;
23712 +                       opt->type = token;
23713 +                       break;
23714 +               case Opt_rdhash:
23715 +                       if (unlikely(match_int(&a->args[0], &n)
23716 +                                    || n < 0
23717 +                                    || n * sizeof(struct hlist_head)
23718 +                                    > KMALLOC_MAX_SIZE)) {
23719 +                               pr_err("bad integer in %s\n", opt_str);
23720 +                               break;
23721 +                       }
23722 +                       opt->rdhash = n;
23723 +                       err = 0;
23724 +                       opt->type = token;
23725 +                       break;
23726 +
23727 +               case Opt_trunc_xino:
23728 +               case Opt_notrunc_xino:
23729 +               case Opt_noxino:
23730 +               case Opt_trunc_xib:
23731 +               case Opt_notrunc_xib:
23732 +               case Opt_shwh:
23733 +               case Opt_noshwh:
23734 +               case Opt_dirperm1:
23735 +               case Opt_nodirperm1:
23736 +               case Opt_plink:
23737 +               case Opt_noplink:
23738 +               case Opt_list_plink:
23739 +               case Opt_dio:
23740 +               case Opt_nodio:
23741 +               case Opt_diropq_a:
23742 +               case Opt_diropq_w:
23743 +               case Opt_warn_perm:
23744 +               case Opt_nowarn_perm:
23745 +               case Opt_refrof:
23746 +               case Opt_norefrof:
23747 +               case Opt_verbose:
23748 +               case Opt_noverbose:
23749 +               case Opt_sum:
23750 +               case Opt_nosum:
23751 +               case Opt_wsum:
23752 +               case Opt_rdblk_def:
23753 +               case Opt_rdhash_def:
23754 +               case Opt_acl:
23755 +               case Opt_noacl:
23756 +                       err = 0;
23757 +                       opt->type = token;
23758 +                       break;
23759 +
23760 +               case Opt_udba:
23761 +                       opt->udba = udba_val(a->args[0].from);
23762 +                       if (opt->udba >= 0) {
23763 +                               err = 0;
23764 +                               opt->type = token;
23765 +                       } else
23766 +                               pr_err("wrong value, %s\n", opt_str);
23767 +                       break;
23768 +
23769 +               case Opt_wbr_create:
23770 +                       u.create = &opt->wbr_create;
23771 +                       u.create->wbr_create
23772 +                               = au_wbr_create_val(a->args[0].from, u.create);
23773 +                       if (u.create->wbr_create >= 0) {
23774 +                               err = 0;
23775 +                               opt->type = token;
23776 +                       } else
23777 +                               pr_err("wrong value, %s\n", opt_str);
23778 +                       break;
23779 +               case Opt_wbr_copyup:
23780 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
23781 +                       if (opt->wbr_copyup >= 0) {
23782 +                               err = 0;
23783 +                               opt->type = token;
23784 +                       } else
23785 +                               pr_err("wrong value, %s\n", opt_str);
23786 +                       break;
23787 +
23788 +               case Opt_fhsm_sec:
23789 +                       if (unlikely(match_int(&a->args[0], &n)
23790 +                                    || n < 0)) {
23791 +                               pr_err("bad integer in %s\n", opt_str);
23792 +                               break;
23793 +                       }
23794 +                       if (sysaufs_brs) {
23795 +                               opt->fhsm_second = n;
23796 +                               opt->type = token;
23797 +                       } else
23798 +                               pr_warn("ignored %s\n", opt_str);
23799 +                       err = 0;
23800 +                       break;
23801 +
23802 +               case Opt_ignore:
23803 +                       pr_warn("ignored %s\n", opt_str);
23804 +                       /*FALLTHROUGH*/
23805 +               case Opt_ignore_silent:
23806 +                       skipped = 1;
23807 +                       err = 0;
23808 +                       break;
23809 +               case Opt_err:
23810 +                       pr_err("unknown option %s\n", opt_str);
23811 +                       break;
23812 +               }
23813 +
23814 +               if (!err && !skipped) {
23815 +                       if (unlikely(++opt > opt_tail)) {
23816 +                               err = -E2BIG;
23817 +                               opt--;
23818 +                               opt->type = Opt_tail;
23819 +                               break;
23820 +                       }
23821 +                       opt->type = Opt_tail;
23822 +               }
23823 +       }
23824 +
23825 +       kfree(a);
23826 +       dump_opts(opts);
23827 +       if (unlikely(err))
23828 +               au_opts_free(opts);
23829 +
23830 +out:
23831 +       return err;
23832 +}
23833 +
23834 +static int au_opt_wbr_create(struct super_block *sb,
23835 +                            struct au_opt_wbr_create *create)
23836 +{
23837 +       int err;
23838 +       struct au_sbinfo *sbinfo;
23839 +
23840 +       SiMustWriteLock(sb);
23841 +
23842 +       err = 1; /* handled */
23843 +       sbinfo = au_sbi(sb);
23844 +       if (sbinfo->si_wbr_create_ops->fin) {
23845 +               err = sbinfo->si_wbr_create_ops->fin(sb);
23846 +               if (!err)
23847 +                       err = 1;
23848 +       }
23849 +
23850 +       sbinfo->si_wbr_create = create->wbr_create;
23851 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
23852 +       switch (create->wbr_create) {
23853 +       case AuWbrCreate_MFSRRV:
23854 +       case AuWbrCreate_MFSRR:
23855 +       case AuWbrCreate_PMFSRR:
23856 +       case AuWbrCreate_PMFSRRV:
23857 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
23858 +               /*FALLTHROUGH*/
23859 +       case AuWbrCreate_MFS:
23860 +       case AuWbrCreate_MFSV:
23861 +       case AuWbrCreate_PMFS:
23862 +       case AuWbrCreate_PMFSV:
23863 +               sbinfo->si_wbr_mfs.mfs_expire
23864 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
23865 +               break;
23866 +       }
23867 +
23868 +       if (sbinfo->si_wbr_create_ops->init)
23869 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
23870 +
23871 +       return err;
23872 +}
23873 +
23874 +/*
23875 + * returns,
23876 + * plus: processed without an error
23877 + * zero: unprocessed
23878 + */
23879 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
23880 +                        struct au_opts *opts)
23881 +{
23882 +       int err;
23883 +       struct au_sbinfo *sbinfo;
23884 +
23885 +       SiMustWriteLock(sb);
23886 +
23887 +       err = 1; /* handled */
23888 +       sbinfo = au_sbi(sb);
23889 +       switch (opt->type) {
23890 +       case Opt_udba:
23891 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
23892 +               sbinfo->si_mntflags |= opt->udba;
23893 +               opts->given_udba |= opt->udba;
23894 +               break;
23895 +
23896 +       case Opt_plink:
23897 +               au_opt_set(sbinfo->si_mntflags, PLINK);
23898 +               break;
23899 +       case Opt_noplink:
23900 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
23901 +                       au_plink_put(sb, /*verbose*/1);
23902 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
23903 +               break;
23904 +       case Opt_list_plink:
23905 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
23906 +                       au_plink_list(sb);
23907 +               break;
23908 +
23909 +       case Opt_dio:
23910 +               au_opt_set(sbinfo->si_mntflags, DIO);
23911 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
23912 +               break;
23913 +       case Opt_nodio:
23914 +               au_opt_clr(sbinfo->si_mntflags, DIO);
23915 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
23916 +               break;
23917 +
23918 +       case Opt_fhsm_sec:
23919 +               au_fhsm_set(sbinfo, opt->fhsm_second);
23920 +               break;
23921 +
23922 +       case Opt_diropq_a:
23923 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
23924 +               break;
23925 +       case Opt_diropq_w:
23926 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
23927 +               break;
23928 +
23929 +       case Opt_warn_perm:
23930 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
23931 +               break;
23932 +       case Opt_nowarn_perm:
23933 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
23934 +               break;
23935 +
23936 +       case Opt_refrof:
23937 +               au_opt_set(sbinfo->si_mntflags, REFROF);
23938 +               break;
23939 +       case Opt_norefrof:
23940 +               au_opt_clr(sbinfo->si_mntflags, REFROF);
23941 +               break;
23942 +
23943 +       case Opt_verbose:
23944 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
23945 +               break;
23946 +       case Opt_noverbose:
23947 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
23948 +               break;
23949 +
23950 +       case Opt_sum:
23951 +               au_opt_set(sbinfo->si_mntflags, SUM);
23952 +               break;
23953 +       case Opt_wsum:
23954 +               au_opt_clr(sbinfo->si_mntflags, SUM);
23955 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
23956 +       case Opt_nosum:
23957 +               au_opt_clr(sbinfo->si_mntflags, SUM);
23958 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
23959 +               break;
23960 +
23961 +       case Opt_wbr_create:
23962 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
23963 +               break;
23964 +       case Opt_wbr_copyup:
23965 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
23966 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
23967 +               break;
23968 +
23969 +       case Opt_dirwh:
23970 +               sbinfo->si_dirwh = opt->dirwh;
23971 +               break;
23972 +
23973 +       case Opt_rdcache:
23974 +               sbinfo->si_rdcache
23975 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
23976 +               break;
23977 +       case Opt_rdblk:
23978 +               sbinfo->si_rdblk = opt->rdblk;
23979 +               break;
23980 +       case Opt_rdblk_def:
23981 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
23982 +               break;
23983 +       case Opt_rdhash:
23984 +               sbinfo->si_rdhash = opt->rdhash;
23985 +               break;
23986 +       case Opt_rdhash_def:
23987 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
23988 +               break;
23989 +
23990 +       case Opt_shwh:
23991 +               au_opt_set(sbinfo->si_mntflags, SHWH);
23992 +               break;
23993 +       case Opt_noshwh:
23994 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
23995 +               break;
23996 +
23997 +       case Opt_dirperm1:
23998 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
23999 +               break;
24000 +       case Opt_nodirperm1:
24001 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
24002 +               break;
24003 +
24004 +       case Opt_trunc_xino:
24005 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
24006 +               break;
24007 +       case Opt_notrunc_xino:
24008 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
24009 +               break;
24010 +
24011 +       case Opt_trunc_xino_path:
24012 +       case Opt_itrunc_xino:
24013 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
24014 +               if (!err)
24015 +                       err = 1;
24016 +               break;
24017 +
24018 +       case Opt_trunc_xib:
24019 +               au_fset_opts(opts->flags, TRUNC_XIB);
24020 +               break;
24021 +       case Opt_notrunc_xib:
24022 +               au_fclr_opts(opts->flags, TRUNC_XIB);
24023 +               break;
24024 +
24025 +       case Opt_acl:
24026 +               sb->s_flags |= MS_POSIXACL;
24027 +               break;
24028 +       case Opt_noacl:
24029 +               sb->s_flags &= ~MS_POSIXACL;
24030 +               break;
24031 +
24032 +       default:
24033 +               err = 0;
24034 +               break;
24035 +       }
24036 +
24037 +       return err;
24038 +}
24039 +
24040 +/*
24041 + * returns tri-state.
24042 + * plus: processed without an error
24043 + * zero: unprocessed
24044 + * minus: error
24045 + */
24046 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
24047 +                    struct au_opts *opts)
24048 +{
24049 +       int err, do_refresh;
24050 +
24051 +       err = 0;
24052 +       switch (opt->type) {
24053 +       case Opt_append:
24054 +               opt->add.bindex = au_sbend(sb) + 1;
24055 +               if (opt->add.bindex < 0)
24056 +                       opt->add.bindex = 0;
24057 +               goto add;
24058 +       case Opt_prepend:
24059 +               opt->add.bindex = 0;
24060 +       add: /* indented label */
24061 +       case Opt_add:
24062 +               err = au_br_add(sb, &opt->add,
24063 +                               au_ftest_opts(opts->flags, REMOUNT));
24064 +               if (!err) {
24065 +                       err = 1;
24066 +                       au_fset_opts(opts->flags, REFRESH);
24067 +               }
24068 +               break;
24069 +
24070 +       case Opt_del:
24071 +       case Opt_idel:
24072 +               err = au_br_del(sb, &opt->del,
24073 +                               au_ftest_opts(opts->flags, REMOUNT));
24074 +               if (!err) {
24075 +                       err = 1;
24076 +                       au_fset_opts(opts->flags, TRUNC_XIB);
24077 +                       au_fset_opts(opts->flags, REFRESH);
24078 +               }
24079 +               break;
24080 +
24081 +       case Opt_mod:
24082 +       case Opt_imod:
24083 +               err = au_br_mod(sb, &opt->mod,
24084 +                               au_ftest_opts(opts->flags, REMOUNT),
24085 +                               &do_refresh);
24086 +               if (!err) {
24087 +                       err = 1;
24088 +                       if (do_refresh)
24089 +                               au_fset_opts(opts->flags, REFRESH);
24090 +               }
24091 +               break;
24092 +       }
24093 +
24094 +       return err;
24095 +}
24096 +
24097 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
24098 +                      struct au_opt_xino **opt_xino,
24099 +                      struct au_opts *opts)
24100 +{
24101 +       int err;
24102 +       aufs_bindex_t bend, bindex;
24103 +       struct dentry *root, *parent, *h_root;
24104 +
24105 +       err = 0;
24106 +       switch (opt->type) {
24107 +       case Opt_xino:
24108 +               err = au_xino_set(sb, &opt->xino,
24109 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
24110 +               if (unlikely(err))
24111 +                       break;
24112 +
24113 +               *opt_xino = &opt->xino;
24114 +               au_xino_brid_set(sb, -1);
24115 +
24116 +               /* safe d_parent access */
24117 +               parent = opt->xino.file->f_path.dentry->d_parent;
24118 +               root = sb->s_root;
24119 +               bend = au_sbend(sb);
24120 +               for (bindex = 0; bindex <= bend; bindex++) {
24121 +                       h_root = au_h_dptr(root, bindex);
24122 +                       if (h_root == parent) {
24123 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
24124 +                               break;
24125 +                       }
24126 +               }
24127 +               break;
24128 +
24129 +       case Opt_noxino:
24130 +               au_xino_clr(sb);
24131 +               au_xino_brid_set(sb, -1);
24132 +               *opt_xino = (void *)-1;
24133 +               break;
24134 +       }
24135 +
24136 +       return err;
24137 +}
24138 +
24139 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24140 +                  unsigned int pending)
24141 +{
24142 +       int err, fhsm;
24143 +       aufs_bindex_t bindex, bend;
24144 +       unsigned char do_plink, skip, do_free;
24145 +       struct au_branch *br;
24146 +       struct au_wbr *wbr;
24147 +       struct dentry *root;
24148 +       struct inode *dir, *h_dir;
24149 +       struct au_sbinfo *sbinfo;
24150 +       struct au_hinode *hdir;
24151 +
24152 +       SiMustAnyLock(sb);
24153 +
24154 +       sbinfo = au_sbi(sb);
24155 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
24156 +
24157 +       if (!(sb_flags & MS_RDONLY)) {
24158 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
24159 +                       pr_warn("first branch should be rw\n");
24160 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
24161 +                       pr_warn("shwh should be used with ro\n");
24162 +       }
24163 +
24164 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
24165 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
24166 +               pr_warn("udba=*notify requires xino\n");
24167 +
24168 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
24169 +               pr_warn("dirperm1 breaks the protection"
24170 +                       " by the permission bits on the lower branch\n");
24171 +
24172 +       err = 0;
24173 +       fhsm = 0;
24174 +       root = sb->s_root;
24175 +       dir = root->d_inode;
24176 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
24177 +       bend = au_sbend(sb);
24178 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
24179 +               skip = 0;
24180 +               h_dir = au_h_iptr(dir, bindex);
24181 +               br = au_sbr(sb, bindex);
24182 +
24183 +               if ((br->br_perm & AuBrAttr_ICEX)
24184 +                   && !h_dir->i_op->listxattr)
24185 +                       br->br_perm &= ~AuBrAttr_ICEX;
24186 +#if 0
24187 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
24188 +                   && (au_br_sb(br)->s_flags & MS_NOSEC))
24189 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
24190 +#endif
24191 +
24192 +               do_free = 0;
24193 +               wbr = br->br_wbr;
24194 +               if (wbr)
24195 +                       wbr_wh_read_lock(wbr);
24196 +
24197 +               if (!au_br_writable(br->br_perm)) {
24198 +                       do_free = !!wbr;
24199 +                       skip = (!wbr
24200 +                               || (!wbr->wbr_whbase
24201 +                                   && !wbr->wbr_plink
24202 +                                   && !wbr->wbr_orph));
24203 +               } else if (!au_br_wh_linkable(br->br_perm)) {
24204 +                       /* skip = (!br->br_whbase && !br->br_orph); */
24205 +                       skip = (!wbr || !wbr->wbr_whbase);
24206 +                       if (skip && wbr) {
24207 +                               if (do_plink)
24208 +                                       skip = !!wbr->wbr_plink;
24209 +                               else
24210 +                                       skip = !wbr->wbr_plink;
24211 +                       }
24212 +               } else {
24213 +                       /* skip = (br->br_whbase && br->br_ohph); */
24214 +                       skip = (wbr && wbr->wbr_whbase);
24215 +                       if (skip) {
24216 +                               if (do_plink)
24217 +                                       skip = !!wbr->wbr_plink;
24218 +                               else
24219 +                                       skip = !wbr->wbr_plink;
24220 +                       }
24221 +               }
24222 +               if (wbr)
24223 +                       wbr_wh_read_unlock(wbr);
24224 +
24225 +               if (au_br_fhsm(br->br_perm)) {
24226 +                       fhsm++;
24227 +                       AuDebugOn(!br->br_fhsm);
24228 +               }
24229 +
24230 +               if (skip)
24231 +                       continue;
24232 +
24233 +               hdir = au_hi(dir, bindex);
24234 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
24235 +               if (wbr)
24236 +                       wbr_wh_write_lock(wbr);
24237 +               err = au_wh_init(br, sb);
24238 +               if (wbr)
24239 +                       wbr_wh_write_unlock(wbr);
24240 +               au_hn_imtx_unlock(hdir);
24241 +
24242 +               if (!err && do_free) {
24243 +                       kfree(wbr);
24244 +                       br->br_wbr = NULL;
24245 +               }
24246 +       }
24247 +
24248 +       if (fhsm >= 2) {
24249 +               au_fset_si(sbinfo, FHSM);
24250 +               for (bindex = bend; bindex >= 0; bindex--) {
24251 +                       br = au_sbr(sb, bindex);
24252 +                       if (au_br_fhsm(br->br_perm)) {
24253 +                               au_fhsm_set_bottom(sb, bindex);
24254 +                               break;
24255 +                       }
24256 +               }
24257 +       } else {
24258 +               au_fclr_si(sbinfo, FHSM);
24259 +               au_fhsm_set_bottom(sb, -1);
24260 +       }
24261 +
24262 +       return err;
24263 +}
24264 +
24265 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
24266 +{
24267 +       int err;
24268 +       unsigned int tmp;
24269 +       aufs_bindex_t bindex, bend;
24270 +       struct au_opt *opt;
24271 +       struct au_opt_xino *opt_xino, xino;
24272 +       struct au_sbinfo *sbinfo;
24273 +       struct au_branch *br;
24274 +       struct inode *dir;
24275 +
24276 +       SiMustWriteLock(sb);
24277 +
24278 +       err = 0;
24279 +       opt_xino = NULL;
24280 +       opt = opts->opt;
24281 +       while (err >= 0 && opt->type != Opt_tail)
24282 +               err = au_opt_simple(sb, opt++, opts);
24283 +       if (err > 0)
24284 +               err = 0;
24285 +       else if (unlikely(err < 0))
24286 +               goto out;
24287 +
24288 +       /* disable xino and udba temporary */
24289 +       sbinfo = au_sbi(sb);
24290 +       tmp = sbinfo->si_mntflags;
24291 +       au_opt_clr(sbinfo->si_mntflags, XINO);
24292 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
24293 +
24294 +       opt = opts->opt;
24295 +       while (err >= 0 && opt->type != Opt_tail)
24296 +               err = au_opt_br(sb, opt++, opts);
24297 +       if (err > 0)
24298 +               err = 0;
24299 +       else if (unlikely(err < 0))
24300 +               goto out;
24301 +
24302 +       bend = au_sbend(sb);
24303 +       if (unlikely(bend < 0)) {
24304 +               err = -EINVAL;
24305 +               pr_err("no branches\n");
24306 +               goto out;
24307 +       }
24308 +
24309 +       if (au_opt_test(tmp, XINO))
24310 +               au_opt_set(sbinfo->si_mntflags, XINO);
24311 +       opt = opts->opt;
24312 +       while (!err && opt->type != Opt_tail)
24313 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
24314 +       if (unlikely(err))
24315 +               goto out;
24316 +
24317 +       err = au_opts_verify(sb, sb->s_flags, tmp);
24318 +       if (unlikely(err))
24319 +               goto out;
24320 +
24321 +       /* restore xino */
24322 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
24323 +               xino.file = au_xino_def(sb);
24324 +               err = PTR_ERR(xino.file);
24325 +               if (IS_ERR(xino.file))
24326 +                       goto out;
24327 +
24328 +               err = au_xino_set(sb, &xino, /*remount*/0);
24329 +               fput(xino.file);
24330 +               if (unlikely(err))
24331 +                       goto out;
24332 +       }
24333 +
24334 +       /* restore udba */
24335 +       tmp &= AuOptMask_UDBA;
24336 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24337 +       sbinfo->si_mntflags |= tmp;
24338 +       bend = au_sbend(sb);
24339 +       for (bindex = 0; bindex <= bend; bindex++) {
24340 +               br = au_sbr(sb, bindex);
24341 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
24342 +               if (unlikely(err))
24343 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
24344 +                               bindex, err);
24345 +               /* go on even if err */
24346 +       }
24347 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
24348 +               dir = sb->s_root->d_inode;
24349 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
24350 +       }
24351 +
24352 +out:
24353 +       return err;
24354 +}
24355 +
24356 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
24357 +{
24358 +       int err, rerr;
24359 +       struct inode *dir;
24360 +       struct au_opt_xino *opt_xino;
24361 +       struct au_opt *opt;
24362 +       struct au_sbinfo *sbinfo;
24363 +
24364 +       SiMustWriteLock(sb);
24365 +
24366 +       dir = sb->s_root->d_inode;
24367 +       sbinfo = au_sbi(sb);
24368 +       err = 0;
24369 +       opt_xino = NULL;
24370 +       opt = opts->opt;
24371 +       while (err >= 0 && opt->type != Opt_tail) {
24372 +               err = au_opt_simple(sb, opt, opts);
24373 +               if (!err)
24374 +                       err = au_opt_br(sb, opt, opts);
24375 +               if (!err)
24376 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
24377 +               opt++;
24378 +       }
24379 +       if (err > 0)
24380 +               err = 0;
24381 +       AuTraceErr(err);
24382 +       /* go on even err */
24383 +
24384 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
24385 +       if (unlikely(rerr && !err))
24386 +               err = rerr;
24387 +
24388 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
24389 +               rerr = au_xib_trunc(sb);
24390 +               if (unlikely(rerr && !err))
24391 +                       err = rerr;
24392 +       }
24393 +
24394 +       /* will be handled by the caller */
24395 +       if (!au_ftest_opts(opts->flags, REFRESH)
24396 +           && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
24397 +               au_fset_opts(opts->flags, REFRESH);
24398 +
24399 +       AuDbg("status 0x%x\n", opts->flags);
24400 +       return err;
24401 +}
24402 +
24403 +/* ---------------------------------------------------------------------- */
24404 +
24405 +unsigned int au_opt_udba(struct super_block *sb)
24406 +{
24407 +       return au_mntflags(sb) & AuOptMask_UDBA;
24408 +}
24409 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
24410 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
24411 +++ linux/fs/aufs/opts.h        2015-06-22 08:27:37.887501948 +0200
24412 @@ -0,0 +1,211 @@
24413 +/*
24414 + * Copyright (C) 2005-2015 Junjiro R. Okajima
24415 + *
24416 + * This program, aufs is free software; you can redistribute it and/or modify
24417 + * it under the terms of the GNU General Public License as published by
24418 + * the Free Software Foundation; either version 2 of the License, or
24419 + * (at your option) any later version.
24420 + *
24421 + * This program is distributed in the hope that it will be useful,
24422 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24423 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24424 + * GNU General Public License for more details.
24425 + *
24426 + * You should have received a copy of the GNU General Public License
24427 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24428 + */
24429 +
24430 +/*
24431 + * mount options/flags
24432 + */
24433 +
24434 +#ifndef __AUFS_OPTS_H__
24435 +#define __AUFS_OPTS_H__
24436 +
24437 +#ifdef __KERNEL__
24438 +
24439 +#include <linux/path.h>
24440 +
24441 +struct file;
24442 +struct super_block;
24443 +
24444 +/* ---------------------------------------------------------------------- */
24445 +
24446 +/* mount flags */
24447 +#define AuOpt_XINO             1               /* external inode number bitmap
24448 +                                                  and translation table */
24449 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
24450 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
24451 +#define AuOpt_UDBA_REVAL       (1 << 3)
24452 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
24453 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
24454 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
24455 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
24456 +                                                  bits */
24457 +#define AuOpt_REFROF           (1 << 8)        /* unimplemented */
24458 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
24459 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
24460 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
24461 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
24462 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
24463 +#define AuOpt_DIO              (1 << 14)       /* direct io */
24464 +
24465 +#ifndef CONFIG_AUFS_HNOTIFY
24466 +#undef AuOpt_UDBA_HNOTIFY
24467 +#define AuOpt_UDBA_HNOTIFY     0
24468 +#endif
24469 +#ifndef CONFIG_AUFS_SHWH
24470 +#undef AuOpt_SHWH
24471 +#define AuOpt_SHWH             0
24472 +#endif
24473 +
24474 +#define AuOpt_Def      (AuOpt_XINO \
24475 +                        | AuOpt_UDBA_REVAL \
24476 +                        | AuOpt_PLINK \
24477 +                        /* | AuOpt_DIRPERM1 */ \
24478 +                        | AuOpt_WARN_PERM)
24479 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
24480 +                        | AuOpt_UDBA_REVAL \
24481 +                        | AuOpt_UDBA_HNOTIFY)
24482 +
24483 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
24484 +#define au_opt_set(flags, name) do { \
24485 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
24486 +       ((flags) |= AuOpt_##name); \
24487 +} while (0)
24488 +#define au_opt_set_udba(flags, name) do { \
24489 +       (flags) &= ~AuOptMask_UDBA; \
24490 +       ((flags) |= AuOpt_##name); \
24491 +} while (0)
24492 +#define au_opt_clr(flags, name) do { \
24493 +       ((flags) &= ~AuOpt_##name); \
24494 +} while (0)
24495 +
24496 +static inline unsigned int au_opts_plink(unsigned int mntflags)
24497 +{
24498 +#ifdef CONFIG_PROC_FS
24499 +       return mntflags;
24500 +#else
24501 +       return mntflags & ~AuOpt_PLINK;
24502 +#endif
24503 +}
24504 +
24505 +/* ---------------------------------------------------------------------- */
24506 +
24507 +/* policies to select one among multiple writable branches */
24508 +enum {
24509 +       AuWbrCreate_TDP,        /* top down parent */
24510 +       AuWbrCreate_RR,         /* round robin */
24511 +       AuWbrCreate_MFS,        /* most free space */
24512 +       AuWbrCreate_MFSV,       /* mfs with seconds */
24513 +       AuWbrCreate_MFSRR,      /* mfs then rr */
24514 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
24515 +       AuWbrCreate_PMFS,       /* parent and mfs */
24516 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
24517 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
24518 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
24519 +
24520 +       AuWbrCreate_Def = AuWbrCreate_TDP
24521 +};
24522 +
24523 +enum {
24524 +       AuWbrCopyup_TDP,        /* top down parent */
24525 +       AuWbrCopyup_BUP,        /* bottom up parent */
24526 +       AuWbrCopyup_BU,         /* bottom up */
24527 +
24528 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
24529 +};
24530 +
24531 +/* ---------------------------------------------------------------------- */
24532 +
24533 +struct au_opt_add {
24534 +       aufs_bindex_t   bindex;
24535 +       char            *pathname;
24536 +       int             perm;
24537 +       struct path     path;
24538 +};
24539 +
24540 +struct au_opt_del {
24541 +       char            *pathname;
24542 +       struct path     h_path;
24543 +};
24544 +
24545 +struct au_opt_mod {
24546 +       char            *path;
24547 +       int             perm;
24548 +       struct dentry   *h_root;
24549 +};
24550 +
24551 +struct au_opt_xino {
24552 +       char            *path;
24553 +       struct file     *file;
24554 +};
24555 +
24556 +struct au_opt_xino_itrunc {
24557 +       aufs_bindex_t   bindex;
24558 +};
24559 +
24560 +struct au_opt_wbr_create {
24561 +       int                     wbr_create;
24562 +       int                     mfs_second;
24563 +       unsigned long long      mfsrr_watermark;
24564 +};
24565 +
24566 +struct au_opt {
24567 +       int type;
24568 +       union {
24569 +               struct au_opt_xino      xino;
24570 +               struct au_opt_xino_itrunc xino_itrunc;
24571 +               struct au_opt_add       add;
24572 +               struct au_opt_del       del;
24573 +               struct au_opt_mod       mod;
24574 +               int                     dirwh;
24575 +               int                     rdcache;
24576 +               unsigned int            rdblk;
24577 +               unsigned int            rdhash;
24578 +               int                     udba;
24579 +               struct au_opt_wbr_create wbr_create;
24580 +               int                     wbr_copyup;
24581 +               unsigned int            fhsm_second;
24582 +       };
24583 +};
24584 +
24585 +/* opts flags */
24586 +#define AuOpts_REMOUNT         1
24587 +#define AuOpts_REFRESH         (1 << 1)
24588 +#define AuOpts_TRUNC_XIB       (1 << 2)
24589 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
24590 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
24591 +#define au_fset_opts(flags, name) \
24592 +       do { (flags) |= AuOpts_##name; } while (0)
24593 +#define au_fclr_opts(flags, name) \
24594 +       do { (flags) &= ~AuOpts_##name; } while (0)
24595 +
24596 +struct au_opts {
24597 +       struct au_opt   *opt;
24598 +       int             max_opt;
24599 +
24600 +       unsigned int    given_udba;
24601 +       unsigned int    flags;
24602 +       unsigned long   sb_flags;
24603 +};
24604 +
24605 +/* ---------------------------------------------------------------------- */
24606 +
24607 +/* opts.c */
24608 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
24609 +const char *au_optstr_udba(int udba);
24610 +const char *au_optstr_wbr_copyup(int wbr_copyup);
24611 +const char *au_optstr_wbr_create(int wbr_create);
24612 +
24613 +void au_opts_free(struct au_opts *opts);
24614 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
24615 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24616 +                  unsigned int pending);
24617 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
24618 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
24619 +
24620 +unsigned int au_opt_udba(struct super_block *sb);
24621 +
24622 +#endif /* __KERNEL__ */
24623 +#endif /* __AUFS_OPTS_H__ */
24624 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
24625 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
24626 +++ linux/fs/aufs/plink.c       2015-06-22 08:27:37.887501948 +0200
24627 @@ -0,0 +1,532 @@
24628 +/*
24629 + * Copyright (C) 2005-2015 Junjiro R. Okajima
24630 + *
24631 + * This program, aufs is free software; you can redistribute it and/or modify
24632 + * it under the terms of the GNU General Public License as published by
24633 + * the Free Software Foundation; either version 2 of the License, or
24634 + * (at your option) any later version.
24635 + *
24636 + * This program is distributed in the hope that it will be useful,
24637 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24638 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24639 + * GNU General Public License for more details.
24640 + *
24641 + * You should have received a copy of the GNU General Public License
24642 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24643 + */
24644 +
24645 +/*
24646 + * pseudo-link
24647 + */
24648 +
24649 +#include "aufs.h"
24650 +
24651 +/*
24652 + * the pseudo-link maintenance mode.
24653 + * during a user process maintains the pseudo-links,
24654 + * prohibit adding a new plink and branch manipulation.
24655 + *
24656 + * Flags
24657 + * NOPLM:
24658 + *     For entry functions which will handle plink, and i_mutex is already held
24659 + *     in VFS.
24660 + *     They cannot wait and should return an error at once.
24661 + *     Callers has to check the error.
24662 + * NOPLMW:
24663 + *     For entry functions which will handle plink, but i_mutex is not held
24664 + *     in VFS.
24665 + *     They can wait the plink maintenance mode to finish.
24666 + *
24667 + * They behave like F_SETLK and F_SETLKW.
24668 + * If the caller never handle plink, then both flags are unnecessary.
24669 + */
24670 +
24671 +int au_plink_maint(struct super_block *sb, int flags)
24672 +{
24673 +       int err;
24674 +       pid_t pid, ppid;
24675 +       struct au_sbinfo *sbi;
24676 +
24677 +       SiMustAnyLock(sb);
24678 +
24679 +       err = 0;
24680 +       if (!au_opt_test(au_mntflags(sb), PLINK))
24681 +               goto out;
24682 +
24683 +       sbi = au_sbi(sb);
24684 +       pid = sbi->si_plink_maint_pid;
24685 +       if (!pid || pid == current->pid)
24686 +               goto out;
24687 +
24688 +       /* todo: it highly depends upon /sbin/mount.aufs */
24689 +       rcu_read_lock();
24690 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
24691 +       rcu_read_unlock();
24692 +       if (pid == ppid)
24693 +               goto out;
24694 +
24695 +       if (au_ftest_lock(flags, NOPLMW)) {
24696 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
24697 +               /* AuDebugOn(!lockdep_depth(current)); */
24698 +               while (sbi->si_plink_maint_pid) {
24699 +                       si_read_unlock(sb);
24700 +                       /* gave up wake_up_bit() */
24701 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
24702 +
24703 +                       if (au_ftest_lock(flags, FLUSH))
24704 +                               au_nwt_flush(&sbi->si_nowait);
24705 +                       si_noflush_read_lock(sb);
24706 +               }
24707 +       } else if (au_ftest_lock(flags, NOPLM)) {
24708 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
24709 +               err = -EAGAIN;
24710 +       }
24711 +
24712 +out:
24713 +       return err;
24714 +}
24715 +
24716 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
24717 +{
24718 +       spin_lock(&sbinfo->si_plink_maint_lock);
24719 +       sbinfo->si_plink_maint_pid = 0;
24720 +       spin_unlock(&sbinfo->si_plink_maint_lock);
24721 +       wake_up_all(&sbinfo->si_plink_wq);
24722 +}
24723 +
24724 +int au_plink_maint_enter(struct super_block *sb)
24725 +{
24726 +       int err;
24727 +       struct au_sbinfo *sbinfo;
24728 +
24729 +       err = 0;
24730 +       sbinfo = au_sbi(sb);
24731 +       /* make sure i am the only one in this fs */
24732 +       si_write_lock(sb, AuLock_FLUSH);
24733 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
24734 +               spin_lock(&sbinfo->si_plink_maint_lock);
24735 +               if (!sbinfo->si_plink_maint_pid)
24736 +                       sbinfo->si_plink_maint_pid = current->pid;
24737 +               else
24738 +                       err = -EBUSY;
24739 +               spin_unlock(&sbinfo->si_plink_maint_lock);
24740 +       }
24741 +       si_write_unlock(sb);
24742 +
24743 +       return err;
24744 +}
24745 +
24746 +/* ---------------------------------------------------------------------- */
24747 +
24748 +#ifdef CONFIG_AUFS_DEBUG
24749 +void au_plink_list(struct super_block *sb)
24750 +{
24751 +       int i;
24752 +       struct au_sbinfo *sbinfo;
24753 +       struct hlist_head *plink_hlist;
24754 +       struct pseudo_link *plink;
24755 +
24756 +       SiMustAnyLock(sb);
24757 +
24758 +       sbinfo = au_sbi(sb);
24759 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
24760 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
24761 +
24762 +       for (i = 0; i < AuPlink_NHASH; i++) {
24763 +               plink_hlist = &sbinfo->si_plink[i].head;
24764 +               rcu_read_lock();
24765 +               hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
24766 +                       AuDbg("%lu\n", plink->inode->i_ino);
24767 +               rcu_read_unlock();
24768 +       }
24769 +}
24770 +#endif
24771 +
24772 +/* is the inode pseudo-linked? */
24773 +int au_plink_test(struct inode *inode)
24774 +{
24775 +       int found, i;
24776 +       struct au_sbinfo *sbinfo;
24777 +       struct hlist_head *plink_hlist;
24778 +       struct pseudo_link *plink;
24779 +
24780 +       sbinfo = au_sbi(inode->i_sb);
24781 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24782 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
24783 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
24784 +
24785 +       found = 0;
24786 +       i = au_plink_hash(inode->i_ino);
24787 +       plink_hlist = &sbinfo->si_plink[i].head;
24788 +       rcu_read_lock();
24789 +       hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
24790 +               if (plink->inode == inode) {
24791 +                       found = 1;
24792 +                       break;
24793 +               }
24794 +       rcu_read_unlock();
24795 +       return found;
24796 +}
24797 +
24798 +/* ---------------------------------------------------------------------- */
24799 +
24800 +/*
24801 + * generate a name for plink.
24802 + * the file will be stored under AUFS_WH_PLINKDIR.
24803 + */
24804 +/* 20 is max digits length of ulong 64 */
24805 +#define PLINK_NAME_LEN ((20 + 1) * 2)
24806 +
24807 +static int plink_name(char *name, int len, struct inode *inode,
24808 +                     aufs_bindex_t bindex)
24809 +{
24810 +       int rlen;
24811 +       struct inode *h_inode;
24812 +
24813 +       h_inode = au_h_iptr(inode, bindex);
24814 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
24815 +       return rlen;
24816 +}
24817 +
24818 +struct au_do_plink_lkup_args {
24819 +       struct dentry **errp;
24820 +       struct qstr *tgtname;
24821 +       struct dentry *h_parent;
24822 +       struct au_branch *br;
24823 +};
24824 +
24825 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
24826 +                                      struct dentry *h_parent,
24827 +                                      struct au_branch *br)
24828 +{
24829 +       struct dentry *h_dentry;
24830 +       struct mutex *h_mtx;
24831 +
24832 +       h_mtx = &h_parent->d_inode->i_mutex;
24833 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
24834 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
24835 +       mutex_unlock(h_mtx);
24836 +       return h_dentry;
24837 +}
24838 +
24839 +static void au_call_do_plink_lkup(void *args)
24840 +{
24841 +       struct au_do_plink_lkup_args *a = args;
24842 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
24843 +}
24844 +
24845 +/* lookup the plink-ed @inode under the branch at @bindex */
24846 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
24847 +{
24848 +       struct dentry *h_dentry, *h_parent;
24849 +       struct au_branch *br;
24850 +       struct inode *h_dir;
24851 +       int wkq_err;
24852 +       char a[PLINK_NAME_LEN];
24853 +       struct qstr tgtname = QSTR_INIT(a, 0);
24854 +
24855 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
24856 +
24857 +       br = au_sbr(inode->i_sb, bindex);
24858 +       h_parent = br->br_wbr->wbr_plink;
24859 +       h_dir = h_parent->d_inode;
24860 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
24861 +
24862 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
24863 +               struct au_do_plink_lkup_args args = {
24864 +                       .errp           = &h_dentry,
24865 +                       .tgtname        = &tgtname,
24866 +                       .h_parent       = h_parent,
24867 +                       .br             = br
24868 +               };
24869 +
24870 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
24871 +               if (unlikely(wkq_err))
24872 +                       h_dentry = ERR_PTR(wkq_err);
24873 +       } else
24874 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
24875 +
24876 +       return h_dentry;
24877 +}
24878 +
24879 +/* create a pseudo-link */
24880 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
24881 +                     struct dentry *h_dentry, struct au_branch *br)
24882 +{
24883 +       int err;
24884 +       struct path h_path = {
24885 +               .mnt = au_br_mnt(br)
24886 +       };
24887 +       struct inode *h_dir, *delegated;
24888 +
24889 +       h_dir = h_parent->d_inode;
24890 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
24891 +again:
24892 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
24893 +       err = PTR_ERR(h_path.dentry);
24894 +       if (IS_ERR(h_path.dentry))
24895 +               goto out;
24896 +
24897 +       err = 0;
24898 +       /* wh.plink dir is not monitored */
24899 +       /* todo: is it really safe? */
24900 +       if (h_path.dentry->d_inode
24901 +           && h_path.dentry->d_inode != h_dentry->d_inode) {
24902 +               delegated = NULL;
24903 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
24904 +               if (unlikely(err == -EWOULDBLOCK)) {
24905 +                       pr_warn("cannot retry for NFSv4 delegation"
24906 +                               " for an internal unlink\n");
24907 +                       iput(delegated);
24908 +               }
24909 +               dput(h_path.dentry);
24910 +               h_path.dentry = NULL;
24911 +               if (!err)
24912 +                       goto again;
24913 +       }
24914 +       if (!err && !h_path.dentry->d_inode) {
24915 +               delegated = NULL;
24916 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
24917 +               if (unlikely(err == -EWOULDBLOCK)) {
24918 +                       pr_warn("cannot retry for NFSv4 delegation"
24919 +                               " for an internal link\n");
24920 +                       iput(delegated);
24921 +               }
24922 +       }
24923 +       dput(h_path.dentry);
24924 +
24925 +out:
24926 +       mutex_unlock(&h_dir->i_mutex);
24927 +       return err;
24928 +}
24929 +
24930 +struct do_whplink_args {
24931 +       int *errp;
24932 +       struct qstr *tgt;
24933 +       struct dentry *h_parent;
24934 +       struct dentry *h_dentry;
24935 +       struct au_branch *br;
24936 +};
24937 +
24938 +static void call_do_whplink(void *args)
24939 +{
24940 +       struct do_whplink_args *a = args;
24941 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
24942 +}
24943 +
24944 +static int whplink(struct dentry *h_dentry, struct inode *inode,
24945 +                  aufs_bindex_t bindex, struct au_branch *br)
24946 +{
24947 +       int err, wkq_err;
24948 +       struct au_wbr *wbr;
24949 +       struct dentry *h_parent;
24950 +       struct inode *h_dir;
24951 +       char a[PLINK_NAME_LEN];
24952 +       struct qstr tgtname = QSTR_INIT(a, 0);
24953 +
24954 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
24955 +       h_parent = wbr->wbr_plink;
24956 +       h_dir = h_parent->d_inode;
24957 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
24958 +
24959 +       /* always superio. */
24960 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
24961 +               struct do_whplink_args args = {
24962 +                       .errp           = &err,
24963 +                       .tgt            = &tgtname,
24964 +                       .h_parent       = h_parent,
24965 +                       .h_dentry       = h_dentry,
24966 +                       .br             = br
24967 +               };
24968 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
24969 +               if (unlikely(wkq_err))
24970 +                       err = wkq_err;
24971 +       } else
24972 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
24973 +
24974 +       return err;
24975 +}
24976 +
24977 +/* free a single plink */
24978 +static void do_put_plink(struct pseudo_link *plink, int do_del)
24979 +{
24980 +       if (do_del)
24981 +               hlist_del(&plink->hlist);
24982 +       iput(plink->inode);
24983 +       kfree(plink);
24984 +}
24985 +
24986 +static void do_put_plink_rcu(struct rcu_head *rcu)
24987 +{
24988 +       struct pseudo_link *plink;
24989 +
24990 +       plink = container_of(rcu, struct pseudo_link, rcu);
24991 +       iput(plink->inode);
24992 +       kfree(plink);
24993 +}
24994 +
24995 +/*
24996 + * create a new pseudo-link for @h_dentry on @bindex.
24997 + * the linked inode is held in aufs @inode.
24998 + */
24999 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
25000 +                    struct dentry *h_dentry)
25001 +{
25002 +       struct super_block *sb;
25003 +       struct au_sbinfo *sbinfo;
25004 +       struct hlist_head *plink_hlist;
25005 +       struct pseudo_link *plink, *tmp;
25006 +       struct au_sphlhead *sphl;
25007 +       int found, err, cnt, i;
25008 +
25009 +       sb = inode->i_sb;
25010 +       sbinfo = au_sbi(sb);
25011 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25012 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25013 +
25014 +       found = au_plink_test(inode);
25015 +       if (found)
25016 +               return;
25017 +
25018 +       i = au_plink_hash(inode->i_ino);
25019 +       sphl = sbinfo->si_plink + i;
25020 +       plink_hlist = &sphl->head;
25021 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
25022 +       if (tmp)
25023 +               tmp->inode = au_igrab(inode);
25024 +       else {
25025 +               err = -ENOMEM;
25026 +               goto out;
25027 +       }
25028 +
25029 +       spin_lock(&sphl->spin);
25030 +       hlist_for_each_entry(plink, plink_hlist, hlist) {
25031 +               if (plink->inode == inode) {
25032 +                       found = 1;
25033 +                       break;
25034 +               }
25035 +       }
25036 +       if (!found)
25037 +               hlist_add_head_rcu(&tmp->hlist, plink_hlist);
25038 +       spin_unlock(&sphl->spin);
25039 +       if (!found) {
25040 +               cnt = au_sphl_count(sphl);
25041 +#define msg "unexpectedly unblanced or too many pseudo-links"
25042 +               if (cnt > AUFS_PLINK_WARN)
25043 +                       AuWarn1(msg ", %d\n", cnt);
25044 +#undef msg
25045 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
25046 +       } else {
25047 +               do_put_plink(tmp, 0);
25048 +               return;
25049 +       }
25050 +
25051 +out:
25052 +       if (unlikely(err)) {
25053 +               pr_warn("err %d, damaged pseudo link.\n", err);
25054 +               if (tmp) {
25055 +                       au_sphl_del_rcu(&tmp->hlist, sphl);
25056 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
25057 +               }
25058 +       }
25059 +}
25060 +
25061 +/* free all plinks */
25062 +void au_plink_put(struct super_block *sb, int verbose)
25063 +{
25064 +       int i, warned;
25065 +       struct au_sbinfo *sbinfo;
25066 +       struct hlist_head *plink_hlist;
25067 +       struct hlist_node *tmp;
25068 +       struct pseudo_link *plink;
25069 +
25070 +       SiMustWriteLock(sb);
25071 +
25072 +       sbinfo = au_sbi(sb);
25073 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25074 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25075 +
25076 +       /* no spin_lock since sbinfo is write-locked */
25077 +       warned = 0;
25078 +       for (i = 0; i < AuPlink_NHASH; i++) {
25079 +               plink_hlist = &sbinfo->si_plink[i].head;
25080 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
25081 +                       pr_warn("pseudo-link is not flushed");
25082 +                       warned = 1;
25083 +               }
25084 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
25085 +                       do_put_plink(plink, 0);
25086 +               INIT_HLIST_HEAD(plink_hlist);
25087 +       }
25088 +}
25089 +
25090 +void au_plink_clean(struct super_block *sb, int verbose)
25091 +{
25092 +       struct dentry *root;
25093 +
25094 +       root = sb->s_root;
25095 +       aufs_write_lock(root);
25096 +       if (au_opt_test(au_mntflags(sb), PLINK))
25097 +               au_plink_put(sb, verbose);
25098 +       aufs_write_unlock(root);
25099 +}
25100 +
25101 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
25102 +{
25103 +       int do_put;
25104 +       aufs_bindex_t bstart, bend, bindex;
25105 +
25106 +       do_put = 0;
25107 +       bstart = au_ibstart(inode);
25108 +       bend = au_ibend(inode);
25109 +       if (bstart >= 0) {
25110 +               for (bindex = bstart; bindex <= bend; bindex++) {
25111 +                       if (!au_h_iptr(inode, bindex)
25112 +                           || au_ii_br_id(inode, bindex) != br_id)
25113 +                               continue;
25114 +                       au_set_h_iptr(inode, bindex, NULL, 0);
25115 +                       do_put = 1;
25116 +                       break;
25117 +               }
25118 +               if (do_put)
25119 +                       for (bindex = bstart; bindex <= bend; bindex++)
25120 +                               if (au_h_iptr(inode, bindex)) {
25121 +                                       do_put = 0;
25122 +                                       break;
25123 +                               }
25124 +       } else
25125 +               do_put = 1;
25126 +
25127 +       return do_put;
25128 +}
25129 +
25130 +/* free the plinks on a branch specified by @br_id */
25131 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
25132 +{
25133 +       struct au_sbinfo *sbinfo;
25134 +       struct hlist_head *plink_hlist;
25135 +       struct hlist_node *tmp;
25136 +       struct pseudo_link *plink;
25137 +       struct inode *inode;
25138 +       int i, do_put;
25139 +
25140 +       SiMustWriteLock(sb);
25141 +
25142 +       sbinfo = au_sbi(sb);
25143 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25144 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25145 +
25146 +       /* no spin_lock since sbinfo is write-locked */
25147 +       for (i = 0; i < AuPlink_NHASH; i++) {
25148 +               plink_hlist = &sbinfo->si_plink[i].head;
25149 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
25150 +                       inode = au_igrab(plink->inode);
25151 +                       ii_write_lock_child(inode);
25152 +                       do_put = au_plink_do_half_refresh(inode, br_id);
25153 +                       if (do_put)
25154 +                               do_put_plink(plink, 1);
25155 +                       ii_write_unlock(inode);
25156 +                       iput(inode);
25157 +               }
25158 +       }
25159 +}
25160 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
25161 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
25162 +++ linux/fs/aufs/poll.c        2015-06-22 08:29:23.710091096 +0200
25163 @@ -0,0 +1,52 @@
25164 +/*
25165 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25166 + *
25167 + * This program, aufs is free software; you can redistribute it and/or modify
25168 + * it under the terms of the GNU General Public License as published by
25169 + * the Free Software Foundation; either version 2 of the License, or
25170 + * (at your option) any later version.
25171 + *
25172 + * This program is distributed in the hope that it will be useful,
25173 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25174 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25175 + * GNU General Public License for more details.
25176 + *
25177 + * You should have received a copy of the GNU General Public License
25178 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25179 + */
25180 +
25181 +/*
25182 + * poll operation
25183 + * There is only one filesystem which implements ->poll operation, currently.
25184 + */
25185 +
25186 +#include "aufs.h"
25187 +
25188 +unsigned int aufs_poll(struct file *file, poll_table *wait)
25189 +{
25190 +       unsigned int mask;
25191 +       int err;
25192 +       struct file *h_file;
25193 +       struct super_block *sb;
25194 +
25195 +       /* We should pretend an error happened. */
25196 +       mask = POLLERR /* | POLLIN | POLLOUT */;
25197 +       sb = file->f_path.dentry->d_sb;
25198 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
25199 +
25200 +       h_file = au_read_pre(file, /*keep_fi*/0);
25201 +       err = PTR_ERR(h_file);
25202 +       if (IS_ERR(h_file))
25203 +               goto out;
25204 +
25205 +       /* it is not an error if h_file has no operation */
25206 +       mask = DEFAULT_POLLMASK;
25207 +       if (h_file->f_op->poll)
25208 +               mask = h_file->f_op->poll(h_file, wait);
25209 +       fput(h_file); /* instead of au_read_post() */
25210 +
25211 +out:
25212 +       si_read_unlock(sb);
25213 +       AuTraceErr((int)mask);
25214 +       return mask;
25215 +}
25216 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
25217 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
25218 +++ linux/fs/aufs/posix_acl.c   2015-06-22 08:27:37.887501948 +0200
25219 @@ -0,0 +1,99 @@
25220 +/*
25221 + * Copyright (C) 2014-2015 Junjiro R. Okajima
25222 + *
25223 + * This program, aufs is free software; you can redistribute it and/or modify
25224 + * it under the terms of the GNU General Public License as published by
25225 + * the Free Software Foundation; either version 2 of the License, or
25226 + * (at your option) any later version.
25227 + *
25228 + * This program is distributed in the hope that it will be useful,
25229 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25230 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25231 + * GNU General Public License for more details.
25232 + *
25233 + * You should have received a copy of the GNU General Public License
25234 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25235 + */
25236 +
25237 +/*
25238 + * posix acl operations
25239 + */
25240 +
25241 +#include <linux/fs.h>
25242 +#include <linux/posix_acl.h>
25243 +#include "aufs.h"
25244 +
25245 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
25246 +{
25247 +       struct posix_acl *acl;
25248 +       int err;
25249 +       aufs_bindex_t bindex;
25250 +       struct inode *h_inode;
25251 +       struct super_block *sb;
25252 +
25253 +       acl = NULL;
25254 +       sb = inode->i_sb;
25255 +       si_read_lock(sb, AuLock_FLUSH);
25256 +       ii_read_lock_child(inode);
25257 +       if (!(sb->s_flags & MS_POSIXACL))
25258 +               goto out;
25259 +
25260 +       bindex = au_ibstart(inode);
25261 +       h_inode = au_h_iptr(inode, bindex);
25262 +       if (unlikely(!h_inode
25263 +                    || ((h_inode->i_mode & S_IFMT)
25264 +                        != (inode->i_mode & S_IFMT)))) {
25265 +               err = au_busy_or_stale();
25266 +               acl = ERR_PTR(err);
25267 +               goto out;
25268 +       }
25269 +
25270 +       /* always topmost only */
25271 +       acl = get_acl(h_inode, type);
25272 +
25273 +out:
25274 +       ii_read_unlock(inode);
25275 +       si_read_unlock(sb);
25276 +
25277 +       AuTraceErrPtr(acl);
25278 +       return acl;
25279 +}
25280 +
25281 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
25282 +{
25283 +       int err;
25284 +       ssize_t ssz;
25285 +       struct dentry *dentry;
25286 +       struct au_srxattr arg = {
25287 +               .type = AU_ACL_SET,
25288 +               .u.acl_set = {
25289 +                       .acl    = acl,
25290 +                       .type   = type
25291 +               },
25292 +       };
25293 +
25294 +       mutex_lock(&inode->i_mutex);
25295 +       if (inode->i_ino == AUFS_ROOT_INO)
25296 +               dentry = dget(inode->i_sb->s_root);
25297 +       else {
25298 +               dentry = d_find_alias(inode);
25299 +               if (!dentry)
25300 +                       dentry = d_find_any_alias(inode);
25301 +               if (!dentry) {
25302 +                       pr_warn("cannot handle this inode, "
25303 +                               "please report to aufs-users ML\n");
25304 +                       err = -ENOENT;
25305 +                       goto out;
25306 +               }
25307 +       }
25308 +
25309 +       ssz = au_srxattr(dentry, &arg);
25310 +       dput(dentry);
25311 +       err = ssz;
25312 +       if (ssz >= 0)
25313 +               err = 0;
25314 +
25315 +out:
25316 +       mutex_unlock(&inode->i_mutex);
25317 +       return err;
25318 +}
25319 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
25320 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
25321 +++ linux/fs/aufs/procfs.c      2015-06-22 08:27:37.887501948 +0200
25322 @@ -0,0 +1,169 @@
25323 +/*
25324 + * Copyright (C) 2010-2015 Junjiro R. Okajima
25325 + *
25326 + * This program, aufs is free software; you can redistribute it and/or modify
25327 + * it under the terms of the GNU General Public License as published by
25328 + * the Free Software Foundation; either version 2 of the License, or
25329 + * (at your option) any later version.
25330 + *
25331 + * This program is distributed in the hope that it will be useful,
25332 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25333 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25334 + * GNU General Public License for more details.
25335 + *
25336 + * You should have received a copy of the GNU General Public License
25337 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25338 + */
25339 +
25340 +/*
25341 + * procfs interfaces
25342 + */
25343 +
25344 +#include <linux/proc_fs.h>
25345 +#include "aufs.h"
25346 +
25347 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
25348 +{
25349 +       struct au_sbinfo *sbinfo;
25350 +
25351 +       sbinfo = file->private_data;
25352 +       if (sbinfo) {
25353 +               au_plink_maint_leave(sbinfo);
25354 +               kobject_put(&sbinfo->si_kobj);
25355 +       }
25356 +
25357 +       return 0;
25358 +}
25359 +
25360 +static void au_procfs_plm_write_clean(struct file *file)
25361 +{
25362 +       struct au_sbinfo *sbinfo;
25363 +
25364 +       sbinfo = file->private_data;
25365 +       if (sbinfo)
25366 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
25367 +}
25368 +
25369 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
25370 +{
25371 +       int err;
25372 +       struct super_block *sb;
25373 +       struct au_sbinfo *sbinfo;
25374 +
25375 +       err = -EBUSY;
25376 +       if (unlikely(file->private_data))
25377 +               goto out;
25378 +
25379 +       sb = NULL;
25380 +       /* don't use au_sbilist_lock() here */
25381 +       spin_lock(&au_sbilist.spin);
25382 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
25383 +               if (id == sysaufs_si_id(sbinfo)) {
25384 +                       kobject_get(&sbinfo->si_kobj);
25385 +                       sb = sbinfo->si_sb;
25386 +                       break;
25387 +               }
25388 +       spin_unlock(&au_sbilist.spin);
25389 +
25390 +       err = -EINVAL;
25391 +       if (unlikely(!sb))
25392 +               goto out;
25393 +
25394 +       err = au_plink_maint_enter(sb);
25395 +       if (!err)
25396 +               /* keep kobject_get() */
25397 +               file->private_data = sbinfo;
25398 +       else
25399 +               kobject_put(&sbinfo->si_kobj);
25400 +out:
25401 +       return err;
25402 +}
25403 +
25404 +/*
25405 + * Accept a valid "si=xxxx" only.
25406 + * Once it is accepted successfully, accept "clean" too.
25407 + */
25408 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
25409 +                                  size_t count, loff_t *ppos)
25410 +{
25411 +       ssize_t err;
25412 +       unsigned long id;
25413 +       /* last newline is allowed */
25414 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
25415 +
25416 +       err = -EACCES;
25417 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25418 +               goto out;
25419 +
25420 +       err = -EINVAL;
25421 +       if (unlikely(count > sizeof(buf)))
25422 +               goto out;
25423 +
25424 +       err = copy_from_user(buf, ubuf, count);
25425 +       if (unlikely(err)) {
25426 +               err = -EFAULT;
25427 +               goto out;
25428 +       }
25429 +       buf[count] = 0;
25430 +
25431 +       err = -EINVAL;
25432 +       if (!strcmp("clean", buf)) {
25433 +               au_procfs_plm_write_clean(file);
25434 +               goto out_success;
25435 +       } else if (unlikely(strncmp("si=", buf, 3)))
25436 +               goto out;
25437 +
25438 +       err = kstrtoul(buf + 3, 16, &id);
25439 +       if (unlikely(err))
25440 +               goto out;
25441 +
25442 +       err = au_procfs_plm_write_si(file, id);
25443 +       if (unlikely(err))
25444 +               goto out;
25445 +
25446 +out_success:
25447 +       err = count; /* success */
25448 +out:
25449 +       return err;
25450 +}
25451 +
25452 +static const struct file_operations au_procfs_plm_fop = {
25453 +       .write          = au_procfs_plm_write,
25454 +       .release        = au_procfs_plm_release,
25455 +       .owner          = THIS_MODULE
25456 +};
25457 +
25458 +/* ---------------------------------------------------------------------- */
25459 +
25460 +static struct proc_dir_entry *au_procfs_dir;
25461 +
25462 +void au_procfs_fin(void)
25463 +{
25464 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
25465 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25466 +}
25467 +
25468 +int __init au_procfs_init(void)
25469 +{
25470 +       int err;
25471 +       struct proc_dir_entry *entry;
25472 +
25473 +       err = -ENOMEM;
25474 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
25475 +       if (unlikely(!au_procfs_dir))
25476 +               goto out;
25477 +
25478 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
25479 +                           au_procfs_dir, &au_procfs_plm_fop);
25480 +       if (unlikely(!entry))
25481 +               goto out_dir;
25482 +
25483 +       err = 0;
25484 +       goto out; /* success */
25485 +
25486 +
25487 +out_dir:
25488 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25489 +out:
25490 +       return err;
25491 +}
25492 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
25493 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
25494 +++ linux/fs/aufs/rdu.c 2015-06-22 08:29:23.710091096 +0200
25495 @@ -0,0 +1,388 @@
25496 +/*
25497 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25498 + *
25499 + * This program, aufs is free software; you can redistribute it and/or modify
25500 + * it under the terms of the GNU General Public License as published by
25501 + * the Free Software Foundation; either version 2 of the License, or
25502 + * (at your option) any later version.
25503 + *
25504 + * This program is distributed in the hope that it will be useful,
25505 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25506 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25507 + * GNU General Public License for more details.
25508 + *
25509 + * You should have received a copy of the GNU General Public License
25510 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25511 + */
25512 +
25513 +/*
25514 + * readdir in userspace.
25515 + */
25516 +
25517 +#include <linux/compat.h>
25518 +#include <linux/fs_stack.h>
25519 +#include <linux/security.h>
25520 +#include "aufs.h"
25521 +
25522 +/* bits for struct aufs_rdu.flags */
25523 +#define        AuRdu_CALLED    1
25524 +#define        AuRdu_CONT      (1 << 1)
25525 +#define        AuRdu_FULL      (1 << 2)
25526 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
25527 +#define au_fset_rdu(flags, name) \
25528 +       do { (flags) |= AuRdu_##name; } while (0)
25529 +#define au_fclr_rdu(flags, name) \
25530 +       do { (flags) &= ~AuRdu_##name; } while (0)
25531 +
25532 +struct au_rdu_arg {
25533 +       struct dir_context              ctx;
25534 +       struct aufs_rdu                 *rdu;
25535 +       union au_rdu_ent_ul             ent;
25536 +       unsigned long                   end;
25537 +
25538 +       struct super_block              *sb;
25539 +       int                             err;
25540 +};
25541 +
25542 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
25543 +                      loff_t offset, u64 h_ino, unsigned int d_type)
25544 +{
25545 +       int err, len;
25546 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
25547 +       struct aufs_rdu *rdu = arg->rdu;
25548 +       struct au_rdu_ent ent;
25549 +
25550 +       err = 0;
25551 +       arg->err = 0;
25552 +       au_fset_rdu(rdu->cookie.flags, CALLED);
25553 +       len = au_rdu_len(nlen);
25554 +       if (arg->ent.ul + len  < arg->end) {
25555 +               ent.ino = h_ino;
25556 +               ent.bindex = rdu->cookie.bindex;
25557 +               ent.type = d_type;
25558 +               ent.nlen = nlen;
25559 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
25560 +                       ent.type = DT_UNKNOWN;
25561 +
25562 +               /* unnecessary to support mmap_sem since this is a dir */
25563 +               err = -EFAULT;
25564 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
25565 +                       goto out;
25566 +               if (copy_to_user(arg->ent.e->name, name, nlen))
25567 +                       goto out;
25568 +               /* the terminating NULL */
25569 +               if (__put_user(0, arg->ent.e->name + nlen))
25570 +                       goto out;
25571 +               err = 0;
25572 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
25573 +               arg->ent.ul += len;
25574 +               rdu->rent++;
25575 +       } else {
25576 +               err = -EFAULT;
25577 +               au_fset_rdu(rdu->cookie.flags, FULL);
25578 +               rdu->full = 1;
25579 +               rdu->tail = arg->ent;
25580 +       }
25581 +
25582 +out:
25583 +       /* AuTraceErr(err); */
25584 +       return err;
25585 +}
25586 +
25587 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
25588 +{
25589 +       int err;
25590 +       loff_t offset;
25591 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
25592 +
25593 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
25594 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
25595 +       err = offset;
25596 +       if (unlikely(offset != cookie->h_pos))
25597 +               goto out;
25598 +
25599 +       err = 0;
25600 +       do {
25601 +               arg->err = 0;
25602 +               au_fclr_rdu(cookie->flags, CALLED);
25603 +               /* smp_mb(); */
25604 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
25605 +               if (err >= 0)
25606 +                       err = arg->err;
25607 +       } while (!err
25608 +                && au_ftest_rdu(cookie->flags, CALLED)
25609 +                && !au_ftest_rdu(cookie->flags, FULL));
25610 +       cookie->h_pos = h_file->f_pos;
25611 +
25612 +out:
25613 +       AuTraceErr(err);
25614 +       return err;
25615 +}
25616 +
25617 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
25618 +{
25619 +       int err;
25620 +       aufs_bindex_t bend;
25621 +       struct au_rdu_arg arg = {
25622 +               .ctx = {
25623 +                       .actor = au_rdu_fill
25624 +               }
25625 +       };
25626 +       struct dentry *dentry;
25627 +       struct inode *inode;
25628 +       struct file *h_file;
25629 +       struct au_rdu_cookie *cookie = &rdu->cookie;
25630 +
25631 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
25632 +       if (unlikely(err)) {
25633 +               err = -EFAULT;
25634 +               AuTraceErr(err);
25635 +               goto out;
25636 +       }
25637 +       rdu->rent = 0;
25638 +       rdu->tail = rdu->ent;
25639 +       rdu->full = 0;
25640 +       arg.rdu = rdu;
25641 +       arg.ent = rdu->ent;
25642 +       arg.end = arg.ent.ul;
25643 +       arg.end += rdu->sz;
25644 +
25645 +       err = -ENOTDIR;
25646 +       if (unlikely(!file->f_op->iterate))
25647 +               goto out;
25648 +
25649 +       err = security_file_permission(file, MAY_READ);
25650 +       AuTraceErr(err);
25651 +       if (unlikely(err))
25652 +               goto out;
25653 +
25654 +       dentry = file->f_path.dentry;
25655 +       inode = dentry->d_inode;
25656 +#if 1
25657 +       mutex_lock(&inode->i_mutex);
25658 +#else
25659 +       err = mutex_lock_killable(&inode->i_mutex);
25660 +       AuTraceErr(err);
25661 +       if (unlikely(err))
25662 +               goto out;
25663 +#endif
25664 +
25665 +       arg.sb = inode->i_sb;
25666 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
25667 +       if (unlikely(err))
25668 +               goto out_mtx;
25669 +       err = au_alive_dir(dentry);
25670 +       if (unlikely(err))
25671 +               goto out_si;
25672 +       /* todo: reval? */
25673 +       fi_read_lock(file);
25674 +
25675 +       err = -EAGAIN;
25676 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
25677 +                    && cookie->generation != au_figen(file)))
25678 +               goto out_unlock;
25679 +
25680 +       err = 0;
25681 +       if (!rdu->blk) {
25682 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
25683 +               if (!rdu->blk)
25684 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
25685 +       }
25686 +       bend = au_fbstart(file);
25687 +       if (cookie->bindex < bend)
25688 +               cookie->bindex = bend;
25689 +       bend = au_fbend_dir(file);
25690 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
25691 +       for (; !err && cookie->bindex <= bend;
25692 +            cookie->bindex++, cookie->h_pos = 0) {
25693 +               h_file = au_hf_dir(file, cookie->bindex);
25694 +               if (!h_file)
25695 +                       continue;
25696 +
25697 +               au_fclr_rdu(cookie->flags, FULL);
25698 +               err = au_rdu_do(h_file, &arg);
25699 +               AuTraceErr(err);
25700 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
25701 +                       break;
25702 +       }
25703 +       AuDbg("rent %llu\n", rdu->rent);
25704 +
25705 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
25706 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
25707 +               au_fset_rdu(cookie->flags, CONT);
25708 +               cookie->generation = au_figen(file);
25709 +       }
25710 +
25711 +       ii_read_lock_child(inode);
25712 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
25713 +       ii_read_unlock(inode);
25714 +
25715 +out_unlock:
25716 +       fi_read_unlock(file);
25717 +out_si:
25718 +       si_read_unlock(arg.sb);
25719 +out_mtx:
25720 +       mutex_unlock(&inode->i_mutex);
25721 +out:
25722 +       AuTraceErr(err);
25723 +       return err;
25724 +}
25725 +
25726 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
25727 +{
25728 +       int err;
25729 +       ino_t ino;
25730 +       unsigned long long nent;
25731 +       union au_rdu_ent_ul *u;
25732 +       struct au_rdu_ent ent;
25733 +       struct super_block *sb;
25734 +
25735 +       err = 0;
25736 +       nent = rdu->nent;
25737 +       u = &rdu->ent;
25738 +       sb = file->f_path.dentry->d_sb;
25739 +       si_read_lock(sb, AuLock_FLUSH);
25740 +       while (nent-- > 0) {
25741 +               /* unnecessary to support mmap_sem since this is a dir */
25742 +               err = copy_from_user(&ent, u->e, sizeof(ent));
25743 +               if (!err)
25744 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
25745 +               if (unlikely(err)) {
25746 +                       err = -EFAULT;
25747 +                       AuTraceErr(err);
25748 +                       break;
25749 +               }
25750 +
25751 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
25752 +               if (!ent.wh)
25753 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
25754 +               else
25755 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
25756 +                                       &ino);
25757 +               if (unlikely(err)) {
25758 +                       AuTraceErr(err);
25759 +                       break;
25760 +               }
25761 +
25762 +               err = __put_user(ino, &u->e->ino);
25763 +               if (unlikely(err)) {
25764 +                       err = -EFAULT;
25765 +                       AuTraceErr(err);
25766 +                       break;
25767 +               }
25768 +               u->ul += au_rdu_len(ent.nlen);
25769 +       }
25770 +       si_read_unlock(sb);
25771 +
25772 +       return err;
25773 +}
25774 +
25775 +/* ---------------------------------------------------------------------- */
25776 +
25777 +static int au_rdu_verify(struct aufs_rdu *rdu)
25778 +{
25779 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
25780 +             "%llu, b%d, 0x%x, g%u}\n",
25781 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
25782 +             rdu->blk,
25783 +             rdu->rent, rdu->shwh, rdu->full,
25784 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
25785 +             rdu->cookie.generation);
25786 +
25787 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
25788 +               return 0;
25789 +
25790 +       AuDbg("%u:%u\n",
25791 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
25792 +       return -EINVAL;
25793 +}
25794 +
25795 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
25796 +{
25797 +       long err, e;
25798 +       struct aufs_rdu rdu;
25799 +       void __user *p = (void __user *)arg;
25800 +
25801 +       err = copy_from_user(&rdu, p, sizeof(rdu));
25802 +       if (unlikely(err)) {
25803 +               err = -EFAULT;
25804 +               AuTraceErr(err);
25805 +               goto out;
25806 +       }
25807 +       err = au_rdu_verify(&rdu);
25808 +       if (unlikely(err))
25809 +               goto out;
25810 +
25811 +       switch (cmd) {
25812 +       case AUFS_CTL_RDU:
25813 +               err = au_rdu(file, &rdu);
25814 +               if (unlikely(err))
25815 +                       break;
25816 +
25817 +               e = copy_to_user(p, &rdu, sizeof(rdu));
25818 +               if (unlikely(e)) {
25819 +                       err = -EFAULT;
25820 +                       AuTraceErr(err);
25821 +               }
25822 +               break;
25823 +       case AUFS_CTL_RDU_INO:
25824 +               err = au_rdu_ino(file, &rdu);
25825 +               break;
25826 +
25827 +       default:
25828 +               /* err = -ENOTTY; */
25829 +               err = -EINVAL;
25830 +       }
25831 +
25832 +out:
25833 +       AuTraceErr(err);
25834 +       return err;
25835 +}
25836 +
25837 +#ifdef CONFIG_COMPAT
25838 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
25839 +{
25840 +       long err, e;
25841 +       struct aufs_rdu rdu;
25842 +       void __user *p = compat_ptr(arg);
25843 +
25844 +       /* todo: get_user()? */
25845 +       err = copy_from_user(&rdu, p, sizeof(rdu));
25846 +       if (unlikely(err)) {
25847 +               err = -EFAULT;
25848 +               AuTraceErr(err);
25849 +               goto out;
25850 +       }
25851 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
25852 +       err = au_rdu_verify(&rdu);
25853 +       if (unlikely(err))
25854 +               goto out;
25855 +
25856 +       switch (cmd) {
25857 +       case AUFS_CTL_RDU:
25858 +               err = au_rdu(file, &rdu);
25859 +               if (unlikely(err))
25860 +                       break;
25861 +
25862 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
25863 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
25864 +               e = copy_to_user(p, &rdu, sizeof(rdu));
25865 +               if (unlikely(e)) {
25866 +                       err = -EFAULT;
25867 +                       AuTraceErr(err);
25868 +               }
25869 +               break;
25870 +       case AUFS_CTL_RDU_INO:
25871 +               err = au_rdu_ino(file, &rdu);
25872 +               break;
25873 +
25874 +       default:
25875 +               /* err = -ENOTTY; */
25876 +               err = -EINVAL;
25877 +       }
25878 +
25879 +out:
25880 +       AuTraceErr(err);
25881 +       return err;
25882 +}
25883 +#endif
25884 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
25885 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
25886 +++ linux/fs/aufs/rwsem.h       2015-06-22 08:27:37.887501948 +0200
25887 @@ -0,0 +1,191 @@
25888 +/*
25889 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25890 + *
25891 + * This program, aufs is free software; you can redistribute it and/or modify
25892 + * it under the terms of the GNU General Public License as published by
25893 + * the Free Software Foundation; either version 2 of the License, or
25894 + * (at your option) any later version.
25895 + *
25896 + * This program is distributed in the hope that it will be useful,
25897 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25898 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25899 + * GNU General Public License for more details.
25900 + *
25901 + * You should have received a copy of the GNU General Public License
25902 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25903 + */
25904 +
25905 +/*
25906 + * simple read-write semaphore wrappers
25907 + */
25908 +
25909 +#ifndef __AUFS_RWSEM_H__
25910 +#define __AUFS_RWSEM_H__
25911 +
25912 +#ifdef __KERNEL__
25913 +
25914 +#include "debug.h"
25915 +
25916 +struct au_rwsem {
25917 +       struct rw_semaphore     rwsem;
25918 +#ifdef CONFIG_AUFS_DEBUG
25919 +       /* just for debugging, not almighty counter */
25920 +       atomic_t                rcnt, wcnt;
25921 +#endif
25922 +};
25923 +
25924 +#ifdef CONFIG_AUFS_DEBUG
25925 +#define AuDbgCntInit(rw) do { \
25926 +       atomic_set(&(rw)->rcnt, 0); \
25927 +       atomic_set(&(rw)->wcnt, 0); \
25928 +       smp_mb(); /* atomic set */ \
25929 +} while (0)
25930 +
25931 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
25932 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
25933 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
25934 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
25935 +#else
25936 +#define AuDbgCntInit(rw)       do {} while (0)
25937 +#define AuDbgRcntInc(rw)       do {} while (0)
25938 +#define AuDbgRcntDec(rw)       do {} while (0)
25939 +#define AuDbgWcntInc(rw)       do {} while (0)
25940 +#define AuDbgWcntDec(rw)       do {} while (0)
25941 +#endif /* CONFIG_AUFS_DEBUG */
25942 +
25943 +/* to debug easier, do not make them inlined functions */
25944 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
25945 +/* rwsem_is_locked() is unusable */
25946 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
25947 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
25948 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
25949 +                                       && atomic_read(&(rw)->wcnt) <= 0)
25950 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
25951 +                                       || atomic_read(&(rw)->wcnt))
25952 +
25953 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
25954 +
25955 +static inline void au_rw_init(struct au_rwsem *rw)
25956 +{
25957 +       AuDbgCntInit(rw);
25958 +       init_rwsem(&rw->rwsem);
25959 +}
25960 +
25961 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
25962 +{
25963 +       au_rw_init(rw);
25964 +       down_write(&rw->rwsem);
25965 +       AuDbgWcntInc(rw);
25966 +}
25967 +
25968 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
25969 +                                          unsigned int lsc)
25970 +{
25971 +       au_rw_init(rw);
25972 +       down_write_nested(&rw->rwsem, lsc);
25973 +       AuDbgWcntInc(rw);
25974 +}
25975 +
25976 +static inline void au_rw_read_lock(struct au_rwsem *rw)
25977 +{
25978 +       down_read(&rw->rwsem);
25979 +       AuDbgRcntInc(rw);
25980 +}
25981 +
25982 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
25983 +{
25984 +       down_read_nested(&rw->rwsem, lsc);
25985 +       AuDbgRcntInc(rw);
25986 +}
25987 +
25988 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
25989 +{
25990 +       AuRwMustReadLock(rw);
25991 +       AuDbgRcntDec(rw);
25992 +       up_read(&rw->rwsem);
25993 +}
25994 +
25995 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
25996 +{
25997 +       AuRwMustWriteLock(rw);
25998 +       AuDbgRcntInc(rw);
25999 +       AuDbgWcntDec(rw);
26000 +       downgrade_write(&rw->rwsem);
26001 +}
26002 +
26003 +static inline void au_rw_write_lock(struct au_rwsem *rw)
26004 +{
26005 +       down_write(&rw->rwsem);
26006 +       AuDbgWcntInc(rw);
26007 +}
26008 +
26009 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
26010 +                                          unsigned int lsc)
26011 +{
26012 +       down_write_nested(&rw->rwsem, lsc);
26013 +       AuDbgWcntInc(rw);
26014 +}
26015 +
26016 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
26017 +{
26018 +       AuRwMustWriteLock(rw);
26019 +       AuDbgWcntDec(rw);
26020 +       up_write(&rw->rwsem);
26021 +}
26022 +
26023 +/* why is not _nested version defined */
26024 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
26025 +{
26026 +       int ret;
26027 +
26028 +       ret = down_read_trylock(&rw->rwsem);
26029 +       if (ret)
26030 +               AuDbgRcntInc(rw);
26031 +       return ret;
26032 +}
26033 +
26034 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
26035 +{
26036 +       int ret;
26037 +
26038 +       ret = down_write_trylock(&rw->rwsem);
26039 +       if (ret)
26040 +               AuDbgWcntInc(rw);
26041 +       return ret;
26042 +}
26043 +
26044 +#undef AuDbgCntInit
26045 +#undef AuDbgRcntInc
26046 +#undef AuDbgRcntDec
26047 +#undef AuDbgWcntInc
26048 +#undef AuDbgWcntDec
26049 +
26050 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26051 +static inline void prefix##_read_lock(param) \
26052 +{ au_rw_read_lock(rwsem); } \
26053 +static inline void prefix##_write_lock(param) \
26054 +{ au_rw_write_lock(rwsem); } \
26055 +static inline int prefix##_read_trylock(param) \
26056 +{ return au_rw_read_trylock(rwsem); } \
26057 +static inline int prefix##_write_trylock(param) \
26058 +{ return au_rw_write_trylock(rwsem); }
26059 +/* why is not _nested version defined */
26060 +/* static inline void prefix##_read_trylock_nested(param, lsc)
26061 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
26062 +static inline void prefix##_write_trylock_nestd(param, lsc)
26063 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
26064 +
26065 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
26066 +static inline void prefix##_read_unlock(param) \
26067 +{ au_rw_read_unlock(rwsem); } \
26068 +static inline void prefix##_write_unlock(param) \
26069 +{ au_rw_write_unlock(rwsem); } \
26070 +static inline void prefix##_downgrade_lock(param) \
26071 +{ au_rw_dgrade_lock(rwsem); }
26072 +
26073 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
26074 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26075 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
26076 +
26077 +#endif /* __KERNEL__ */
26078 +#endif /* __AUFS_RWSEM_H__ */
26079 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
26080 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
26081 +++ linux/fs/aufs/sbinfo.c      2015-06-22 08:27:37.887501948 +0200
26082 @@ -0,0 +1,354 @@
26083 +/*
26084 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26085 + *
26086 + * This program, aufs is free software; you can redistribute it and/or modify
26087 + * it under the terms of the GNU General Public License as published by
26088 + * the Free Software Foundation; either version 2 of the License, or
26089 + * (at your option) any later version.
26090 + *
26091 + * This program is distributed in the hope that it will be useful,
26092 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26093 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26094 + * GNU General Public License for more details.
26095 + *
26096 + * You should have received a copy of the GNU General Public License
26097 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26098 + */
26099 +
26100 +/*
26101 + * superblock private data
26102 + */
26103 +
26104 +#include "aufs.h"
26105 +
26106 +/*
26107 + * they are necessary regardless sysfs is disabled.
26108 + */
26109 +void au_si_free(struct kobject *kobj)
26110 +{
26111 +       int i;
26112 +       struct au_sbinfo *sbinfo;
26113 +       char *locked __maybe_unused; /* debug only */
26114 +
26115 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
26116 +       for (i = 0; i < AuPlink_NHASH; i++)
26117 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
26118 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
26119 +
26120 +       au_rw_write_lock(&sbinfo->si_rwsem);
26121 +       au_br_free(sbinfo);
26122 +       au_rw_write_unlock(&sbinfo->si_rwsem);
26123 +
26124 +       AuDebugOn(radix_tree_gang_lookup
26125 +                 (&sbinfo->au_si_pid.tree, (void **)&locked,
26126 +                  /*first_index*/PID_MAX_DEFAULT - 1,
26127 +                  /*max_items*/sizeof(locked)/sizeof(*locked)));
26128 +
26129 +       kfree(sbinfo->si_branch);
26130 +       kfree(sbinfo->au_si_pid.bitmap);
26131 +       mutex_destroy(&sbinfo->si_xib_mtx);
26132 +       AuRwDestroy(&sbinfo->si_rwsem);
26133 +
26134 +       kfree(sbinfo);
26135 +}
26136 +
26137 +int au_si_alloc(struct super_block *sb)
26138 +{
26139 +       int err, i;
26140 +       struct au_sbinfo *sbinfo;
26141 +       static struct lock_class_key aufs_si;
26142 +
26143 +       err = -ENOMEM;
26144 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
26145 +       if (unlikely(!sbinfo))
26146 +               goto out;
26147 +
26148 +       BUILD_BUG_ON(sizeof(unsigned long) !=
26149 +                    sizeof(*sbinfo->au_si_pid.bitmap));
26150 +       sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
26151 +                                       sizeof(*sbinfo->au_si_pid.bitmap),
26152 +                                       GFP_NOFS);
26153 +       if (unlikely(!sbinfo->au_si_pid.bitmap))
26154 +               goto out_sbinfo;
26155 +
26156 +       /* will be reallocated separately */
26157 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
26158 +       if (unlikely(!sbinfo->si_branch))
26159 +               goto out_pidmap;
26160 +
26161 +       err = sysaufs_si_init(sbinfo);
26162 +       if (unlikely(err))
26163 +               goto out_br;
26164 +
26165 +       au_nwt_init(&sbinfo->si_nowait);
26166 +       au_rw_init_wlock(&sbinfo->si_rwsem);
26167 +       au_rw_class(&sbinfo->si_rwsem, &aufs_si);
26168 +       spin_lock_init(&sbinfo->au_si_pid.tree_lock);
26169 +       INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
26170 +
26171 +       atomic_long_set(&sbinfo->si_ninodes, 0);
26172 +       atomic_long_set(&sbinfo->si_nfiles, 0);
26173 +
26174 +       sbinfo->si_bend = -1;
26175 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
26176 +
26177 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
26178 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
26179 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
26180 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
26181 +
26182 +       au_fhsm_init(sbinfo);
26183 +
26184 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
26185 +
26186 +       sbinfo->si_xino_jiffy = jiffies;
26187 +       sbinfo->si_xino_expire
26188 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
26189 +       mutex_init(&sbinfo->si_xib_mtx);
26190 +       sbinfo->si_xino_brid = -1;
26191 +       /* leave si_xib_last_pindex and si_xib_next_bit */
26192 +
26193 +       au_sphl_init(&sbinfo->si_aopen);
26194 +
26195 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
26196 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26197 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26198 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
26199 +
26200 +       for (i = 0; i < AuPlink_NHASH; i++)
26201 +               au_sphl_init(sbinfo->si_plink + i);
26202 +       init_waitqueue_head(&sbinfo->si_plink_wq);
26203 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
26204 +
26205 +       au_sphl_init(&sbinfo->si_files);
26206 +
26207 +       /* leave other members for sysaufs and si_mnt. */
26208 +       sbinfo->si_sb = sb;
26209 +       sb->s_fs_info = sbinfo;
26210 +       si_pid_set(sb);
26211 +       return 0; /* success */
26212 +
26213 +out_br:
26214 +       kfree(sbinfo->si_branch);
26215 +out_pidmap:
26216 +       kfree(sbinfo->au_si_pid.bitmap);
26217 +out_sbinfo:
26218 +       kfree(sbinfo);
26219 +out:
26220 +       return err;
26221 +}
26222 +
26223 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
26224 +{
26225 +       int err, sz;
26226 +       struct au_branch **brp;
26227 +
26228 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
26229 +
26230 +       err = -ENOMEM;
26231 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
26232 +       if (unlikely(!sz))
26233 +               sz = sizeof(*brp);
26234 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
26235 +       if (brp) {
26236 +               sbinfo->si_branch = brp;
26237 +               err = 0;
26238 +       }
26239 +
26240 +       return err;
26241 +}
26242 +
26243 +/* ---------------------------------------------------------------------- */
26244 +
26245 +unsigned int au_sigen_inc(struct super_block *sb)
26246 +{
26247 +       unsigned int gen;
26248 +
26249 +       SiMustWriteLock(sb);
26250 +
26251 +       gen = ++au_sbi(sb)->si_generation;
26252 +       au_update_digen(sb->s_root);
26253 +       au_update_iigen(sb->s_root->d_inode, /*half*/0);
26254 +       sb->s_root->d_inode->i_version++;
26255 +       return gen;
26256 +}
26257 +
26258 +aufs_bindex_t au_new_br_id(struct super_block *sb)
26259 +{
26260 +       aufs_bindex_t br_id;
26261 +       int i;
26262 +       struct au_sbinfo *sbinfo;
26263 +
26264 +       SiMustWriteLock(sb);
26265 +
26266 +       sbinfo = au_sbi(sb);
26267 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
26268 +               br_id = ++sbinfo->si_last_br_id;
26269 +               AuDebugOn(br_id < 0);
26270 +               if (br_id && au_br_index(sb, br_id) < 0)
26271 +                       return br_id;
26272 +       }
26273 +
26274 +       return -1;
26275 +}
26276 +
26277 +/* ---------------------------------------------------------------------- */
26278 +
26279 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
26280 +int si_read_lock(struct super_block *sb, int flags)
26281 +{
26282 +       int err;
26283 +
26284 +       err = 0;
26285 +       if (au_ftest_lock(flags, FLUSH))
26286 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26287 +
26288 +       si_noflush_read_lock(sb);
26289 +       err = au_plink_maint(sb, flags);
26290 +       if (unlikely(err))
26291 +               si_read_unlock(sb);
26292 +
26293 +       return err;
26294 +}
26295 +
26296 +int si_write_lock(struct super_block *sb, int flags)
26297 +{
26298 +       int err;
26299 +
26300 +       if (au_ftest_lock(flags, FLUSH))
26301 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26302 +
26303 +       si_noflush_write_lock(sb);
26304 +       err = au_plink_maint(sb, flags);
26305 +       if (unlikely(err))
26306 +               si_write_unlock(sb);
26307 +
26308 +       return err;
26309 +}
26310 +
26311 +/* dentry and super_block lock. call at entry point */
26312 +int aufs_read_lock(struct dentry *dentry, int flags)
26313 +{
26314 +       int err;
26315 +       struct super_block *sb;
26316 +
26317 +       sb = dentry->d_sb;
26318 +       err = si_read_lock(sb, flags);
26319 +       if (unlikely(err))
26320 +               goto out;
26321 +
26322 +       if (au_ftest_lock(flags, DW))
26323 +               di_write_lock_child(dentry);
26324 +       else
26325 +               di_read_lock_child(dentry, flags);
26326 +
26327 +       if (au_ftest_lock(flags, GEN)) {
26328 +               err = au_digen_test(dentry, au_sigen(sb));
26329 +               AuDebugOn(!err && au_dbrange_test(dentry));
26330 +               if (unlikely(err))
26331 +                       aufs_read_unlock(dentry, flags);
26332 +       }
26333 +
26334 +out:
26335 +       return err;
26336 +}
26337 +
26338 +void aufs_read_unlock(struct dentry *dentry, int flags)
26339 +{
26340 +       if (au_ftest_lock(flags, DW))
26341 +               di_write_unlock(dentry);
26342 +       else
26343 +               di_read_unlock(dentry, flags);
26344 +       si_read_unlock(dentry->d_sb);
26345 +}
26346 +
26347 +void aufs_write_lock(struct dentry *dentry)
26348 +{
26349 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
26350 +       di_write_lock_child(dentry);
26351 +}
26352 +
26353 +void aufs_write_unlock(struct dentry *dentry)
26354 +{
26355 +       di_write_unlock(dentry);
26356 +       si_write_unlock(dentry->d_sb);
26357 +}
26358 +
26359 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
26360 +{
26361 +       int err;
26362 +       unsigned int sigen;
26363 +       struct super_block *sb;
26364 +
26365 +       sb = d1->d_sb;
26366 +       err = si_read_lock(sb, flags);
26367 +       if (unlikely(err))
26368 +               goto out;
26369 +
26370 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
26371 +
26372 +       if (au_ftest_lock(flags, GEN)) {
26373 +               sigen = au_sigen(sb);
26374 +               err = au_digen_test(d1, sigen);
26375 +               AuDebugOn(!err && au_dbrange_test(d1));
26376 +               if (!err) {
26377 +                       err = au_digen_test(d2, sigen);
26378 +                       AuDebugOn(!err && au_dbrange_test(d2));
26379 +               }
26380 +               if (unlikely(err))
26381 +                       aufs_read_and_write_unlock2(d1, d2);
26382 +       }
26383 +
26384 +out:
26385 +       return err;
26386 +}
26387 +
26388 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
26389 +{
26390 +       di_write_unlock2(d1, d2);
26391 +       si_read_unlock(d1->d_sb);
26392 +}
26393 +
26394 +/* ---------------------------------------------------------------------- */
26395 +
26396 +int si_pid_test_slow(struct super_block *sb)
26397 +{
26398 +       void *p;
26399 +
26400 +       rcu_read_lock();
26401 +       p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
26402 +       rcu_read_unlock();
26403 +
26404 +       return (long)!!p;
26405 +}
26406 +
26407 +void si_pid_set_slow(struct super_block *sb)
26408 +{
26409 +       int err;
26410 +       struct au_sbinfo *sbinfo;
26411 +
26412 +       AuDebugOn(si_pid_test_slow(sb));
26413 +
26414 +       sbinfo = au_sbi(sb);
26415 +       err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
26416 +       AuDebugOn(err);
26417 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
26418 +       err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
26419 +                               /*any valid ptr*/sb);
26420 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
26421 +       AuDebugOn(err);
26422 +       radix_tree_preload_end();
26423 +}
26424 +
26425 +void si_pid_clr_slow(struct super_block *sb)
26426 +{
26427 +       void *p;
26428 +       struct au_sbinfo *sbinfo;
26429 +
26430 +       AuDebugOn(!si_pid_test_slow(sb));
26431 +
26432 +       sbinfo = au_sbi(sb);
26433 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
26434 +       p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
26435 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
26436 +}
26437 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
26438 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
26439 +++ linux/fs/aufs/spl.h 2015-06-22 08:27:37.887501948 +0200
26440 @@ -0,0 +1,111 @@
26441 +/*
26442 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26443 + *
26444 + * This program, aufs is free software; you can redistribute it and/or modify
26445 + * it under the terms of the GNU General Public License as published by
26446 + * the Free Software Foundation; either version 2 of the License, or
26447 + * (at your option) any later version.
26448 + *
26449 + * This program is distributed in the hope that it will be useful,
26450 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26451 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26452 + * GNU General Public License for more details.
26453 + *
26454 + * You should have received a copy of the GNU General Public License
26455 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26456 + */
26457 +
26458 +/*
26459 + * simple list protected by a spinlock
26460 + */
26461 +
26462 +#ifndef __AUFS_SPL_H__
26463 +#define __AUFS_SPL_H__
26464 +
26465 +#ifdef __KERNEL__
26466 +
26467 +struct au_splhead {
26468 +       spinlock_t              spin;
26469 +       struct list_head        head;
26470 +};
26471 +
26472 +static inline void au_spl_init(struct au_splhead *spl)
26473 +{
26474 +       spin_lock_init(&spl->spin);
26475 +       INIT_LIST_HEAD(&spl->head);
26476 +}
26477 +
26478 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
26479 +{
26480 +       spin_lock(&spl->spin);
26481 +       list_add(list, &spl->head);
26482 +       spin_unlock(&spl->spin);
26483 +}
26484 +
26485 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
26486 +{
26487 +       spin_lock(&spl->spin);
26488 +       list_del(list);
26489 +       spin_unlock(&spl->spin);
26490 +}
26491 +
26492 +static inline void au_spl_del_rcu(struct list_head *list,
26493 +                                 struct au_splhead *spl)
26494 +{
26495 +       spin_lock(&spl->spin);
26496 +       list_del_rcu(list);
26497 +       spin_unlock(&spl->spin);
26498 +}
26499 +
26500 +/* ---------------------------------------------------------------------- */
26501 +
26502 +struct au_sphlhead {
26503 +       spinlock_t              spin;
26504 +       struct hlist_head       head;
26505 +};
26506 +
26507 +static inline void au_sphl_init(struct au_sphlhead *sphl)
26508 +{
26509 +       spin_lock_init(&sphl->spin);
26510 +       INIT_HLIST_HEAD(&sphl->head);
26511 +}
26512 +
26513 +static inline void au_sphl_add(struct hlist_node *hlist,
26514 +                              struct au_sphlhead *sphl)
26515 +{
26516 +       spin_lock(&sphl->spin);
26517 +       hlist_add_head(hlist, &sphl->head);
26518 +       spin_unlock(&sphl->spin);
26519 +}
26520 +
26521 +static inline void au_sphl_del(struct hlist_node *hlist,
26522 +                              struct au_sphlhead *sphl)
26523 +{
26524 +       spin_lock(&sphl->spin);
26525 +       hlist_del(hlist);
26526 +       spin_unlock(&sphl->spin);
26527 +}
26528 +
26529 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
26530 +                                  struct au_sphlhead *sphl)
26531 +{
26532 +       spin_lock(&sphl->spin);
26533 +       hlist_del_rcu(hlist);
26534 +       spin_unlock(&sphl->spin);
26535 +}
26536 +
26537 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
26538 +{
26539 +       unsigned long cnt;
26540 +       struct hlist_node *pos;
26541 +
26542 +       cnt = 0;
26543 +       spin_lock(&sphl->spin);
26544 +       hlist_for_each(pos, &sphl->head)
26545 +               cnt++;
26546 +       spin_unlock(&sphl->spin);
26547 +       return cnt;
26548 +}
26549 +
26550 +#endif /* __KERNEL__ */
26551 +#endif /* __AUFS_SPL_H__ */
26552 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
26553 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
26554 +++ linux/fs/aufs/super.c       2015-06-22 08:29:23.710091096 +0200
26555 @@ -0,0 +1,1007 @@
26556 +/*
26557 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26558 + *
26559 + * This program, aufs is free software; you can redistribute it and/or modify
26560 + * it under the terms of the GNU General Public License as published by
26561 + * the Free Software Foundation; either version 2 of the License, or
26562 + * (at your option) any later version.
26563 + *
26564 + * This program is distributed in the hope that it will be useful,
26565 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26566 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26567 + * GNU General Public License for more details.
26568 + *
26569 + * You should have received a copy of the GNU General Public License
26570 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26571 + */
26572 +
26573 +/*
26574 + * mount and super_block operations
26575 + */
26576 +
26577 +#include <linux/mm.h>
26578 +#include <linux/seq_file.h>
26579 +#include <linux/statfs.h>
26580 +#include <linux/vmalloc.h>
26581 +#include "aufs.h"
26582 +
26583 +/*
26584 + * super_operations
26585 + */
26586 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
26587 +{
26588 +       struct au_icntnr *c;
26589 +
26590 +       c = au_cache_alloc_icntnr();
26591 +       if (c) {
26592 +               au_icntnr_init(c);
26593 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
26594 +               c->iinfo.ii_hinode = NULL;
26595 +               return &c->vfs_inode;
26596 +       }
26597 +       return NULL;
26598 +}
26599 +
26600 +static void aufs_destroy_inode_cb(struct rcu_head *head)
26601 +{
26602 +       struct inode *inode = container_of(head, struct inode, i_rcu);
26603 +
26604 +       INIT_HLIST_HEAD(&inode->i_dentry);
26605 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
26606 +}
26607 +
26608 +static void aufs_destroy_inode(struct inode *inode)
26609 +{
26610 +       au_iinfo_fin(inode);
26611 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
26612 +}
26613 +
26614 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
26615 +{
26616 +       struct inode *inode;
26617 +       int err;
26618 +
26619 +       inode = iget_locked(sb, ino);
26620 +       if (unlikely(!inode)) {
26621 +               inode = ERR_PTR(-ENOMEM);
26622 +               goto out;
26623 +       }
26624 +       if (!(inode->i_state & I_NEW))
26625 +               goto out;
26626 +
26627 +       err = au_xigen_new(inode);
26628 +       if (!err)
26629 +               err = au_iinfo_init(inode);
26630 +       if (!err)
26631 +               inode->i_version++;
26632 +       else {
26633 +               iget_failed(inode);
26634 +               inode = ERR_PTR(err);
26635 +       }
26636 +
26637 +out:
26638 +       /* never return NULL */
26639 +       AuDebugOn(!inode);
26640 +       AuTraceErrPtr(inode);
26641 +       return inode;
26642 +}
26643 +
26644 +/* lock free root dinfo */
26645 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
26646 +{
26647 +       int err;
26648 +       aufs_bindex_t bindex, bend;
26649 +       struct path path;
26650 +       struct au_hdentry *hdp;
26651 +       struct au_branch *br;
26652 +       au_br_perm_str_t perm;
26653 +
26654 +       err = 0;
26655 +       bend = au_sbend(sb);
26656 +       hdp = au_di(sb->s_root)->di_hdentry;
26657 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
26658 +               br = au_sbr(sb, bindex);
26659 +               path.mnt = au_br_mnt(br);
26660 +               path.dentry = hdp[bindex].hd_dentry;
26661 +               err = au_seq_path(seq, &path);
26662 +               if (err > 0) {
26663 +                       au_optstr_br_perm(&perm, br->br_perm);
26664 +                       err = seq_printf(seq, "=%s", perm.a);
26665 +                       if (err == -1)
26666 +                               err = -E2BIG;
26667 +               }
26668 +               if (!err && bindex != bend)
26669 +                       err = seq_putc(seq, ':');
26670 +       }
26671 +
26672 +       return err;
26673 +}
26674 +
26675 +static void au_show_wbr_create(struct seq_file *m, int v,
26676 +                              struct au_sbinfo *sbinfo)
26677 +{
26678 +       const char *pat;
26679 +
26680 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26681 +
26682 +       seq_puts(m, ",create=");
26683 +       pat = au_optstr_wbr_create(v);
26684 +       switch (v) {
26685 +       case AuWbrCreate_TDP:
26686 +       case AuWbrCreate_RR:
26687 +       case AuWbrCreate_MFS:
26688 +       case AuWbrCreate_PMFS:
26689 +               seq_puts(m, pat);
26690 +               break;
26691 +       case AuWbrCreate_MFSV:
26692 +               seq_printf(m, /*pat*/"mfs:%lu",
26693 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26694 +                          / MSEC_PER_SEC);
26695 +               break;
26696 +       case AuWbrCreate_PMFSV:
26697 +               seq_printf(m, /*pat*/"pmfs:%lu",
26698 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26699 +                          / MSEC_PER_SEC);
26700 +               break;
26701 +       case AuWbrCreate_MFSRR:
26702 +               seq_printf(m, /*pat*/"mfsrr:%llu",
26703 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26704 +               break;
26705 +       case AuWbrCreate_MFSRRV:
26706 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
26707 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26708 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26709 +                          / MSEC_PER_SEC);
26710 +               break;
26711 +       case AuWbrCreate_PMFSRR:
26712 +               seq_printf(m, /*pat*/"pmfsrr:%llu",
26713 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26714 +               break;
26715 +       case AuWbrCreate_PMFSRRV:
26716 +               seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
26717 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26718 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26719 +                          / MSEC_PER_SEC);
26720 +               break;
26721 +       }
26722 +}
26723 +
26724 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
26725 +{
26726 +#ifdef CONFIG_SYSFS
26727 +       return 0;
26728 +#else
26729 +       int err;
26730 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
26731 +       aufs_bindex_t bindex, brid;
26732 +       struct qstr *name;
26733 +       struct file *f;
26734 +       struct dentry *d, *h_root;
26735 +       struct au_hdentry *hdp;
26736 +
26737 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26738 +
26739 +       err = 0;
26740 +       f = au_sbi(sb)->si_xib;
26741 +       if (!f)
26742 +               goto out;
26743 +
26744 +       /* stop printing the default xino path on the first writable branch */
26745 +       h_root = NULL;
26746 +       brid = au_xino_brid(sb);
26747 +       if (brid >= 0) {
26748 +               bindex = au_br_index(sb, brid);
26749 +               hdp = au_di(sb->s_root)->di_hdentry;
26750 +               h_root = hdp[0 + bindex].hd_dentry;
26751 +       }
26752 +       d = f->f_path.dentry;
26753 +       name = &d->d_name;
26754 +       /* safe ->d_parent because the file is unlinked */
26755 +       if (d->d_parent == h_root
26756 +           && name->len == len
26757 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
26758 +               goto out;
26759 +
26760 +       seq_puts(seq, ",xino=");
26761 +       err = au_xino_path(seq, f);
26762 +
26763 +out:
26764 +       return err;
26765 +#endif
26766 +}
26767 +
26768 +/* seq_file will re-call me in case of too long string */
26769 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
26770 +{
26771 +       int err;
26772 +       unsigned int mnt_flags, v;
26773 +       struct super_block *sb;
26774 +       struct au_sbinfo *sbinfo;
26775 +
26776 +#define AuBool(name, str) do { \
26777 +       v = au_opt_test(mnt_flags, name); \
26778 +       if (v != au_opt_test(AuOpt_Def, name)) \
26779 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
26780 +} while (0)
26781 +
26782 +#define AuStr(name, str) do { \
26783 +       v = mnt_flags & AuOptMask_##name; \
26784 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
26785 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
26786 +} while (0)
26787 +
26788 +#define AuUInt(name, str, val) do { \
26789 +       if (val != AUFS_##name##_DEF) \
26790 +               seq_printf(m, "," #str "=%u", val); \
26791 +} while (0)
26792 +
26793 +       sb = dentry->d_sb;
26794 +       if (sb->s_flags & MS_POSIXACL)
26795 +               seq_puts(m, ",acl");
26796 +
26797 +       /* lock free root dinfo */
26798 +       si_noflush_read_lock(sb);
26799 +       sbinfo = au_sbi(sb);
26800 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
26801 +
26802 +       mnt_flags = au_mntflags(sb);
26803 +       if (au_opt_test(mnt_flags, XINO)) {
26804 +               err = au_show_xino(m, sb);
26805 +               if (unlikely(err))
26806 +                       goto out;
26807 +       } else
26808 +               seq_puts(m, ",noxino");
26809 +
26810 +       AuBool(TRUNC_XINO, trunc_xino);
26811 +       AuStr(UDBA, udba);
26812 +       AuBool(SHWH, shwh);
26813 +       AuBool(PLINK, plink);
26814 +       AuBool(DIO, dio);
26815 +       AuBool(DIRPERM1, dirperm1);
26816 +       /* AuBool(REFROF, refrof); */
26817 +
26818 +       v = sbinfo->si_wbr_create;
26819 +       if (v != AuWbrCreate_Def)
26820 +               au_show_wbr_create(m, v, sbinfo);
26821 +
26822 +       v = sbinfo->si_wbr_copyup;
26823 +       if (v != AuWbrCopyup_Def)
26824 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
26825 +
26826 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
26827 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
26828 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
26829 +
26830 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
26831 +
26832 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
26833 +       AuUInt(RDCACHE, rdcache, v);
26834 +
26835 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
26836 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
26837 +
26838 +       au_fhsm_show(m, sbinfo);
26839 +
26840 +       AuBool(SUM, sum);
26841 +       /* AuBool(SUM_W, wsum); */
26842 +       AuBool(WARN_PERM, warn_perm);
26843 +       AuBool(VERBOSE, verbose);
26844 +
26845 +out:
26846 +       /* be sure to print "br:" last */
26847 +       if (!sysaufs_brs) {
26848 +               seq_puts(m, ",br:");
26849 +               au_show_brs(m, sb);
26850 +       }
26851 +       si_read_unlock(sb);
26852 +       return 0;
26853 +
26854 +#undef AuBool
26855 +#undef AuStr
26856 +#undef AuUInt
26857 +}
26858 +
26859 +/* ---------------------------------------------------------------------- */
26860 +
26861 +/* sum mode which returns the summation for statfs(2) */
26862 +
26863 +static u64 au_add_till_max(u64 a, u64 b)
26864 +{
26865 +       u64 old;
26866 +
26867 +       old = a;
26868 +       a += b;
26869 +       if (old <= a)
26870 +               return a;
26871 +       return ULLONG_MAX;
26872 +}
26873 +
26874 +static u64 au_mul_till_max(u64 a, long mul)
26875 +{
26876 +       u64 old;
26877 +
26878 +       old = a;
26879 +       a *= mul;
26880 +       if (old <= a)
26881 +               return a;
26882 +       return ULLONG_MAX;
26883 +}
26884 +
26885 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
26886 +{
26887 +       int err;
26888 +       long bsize, factor;
26889 +       u64 blocks, bfree, bavail, files, ffree;
26890 +       aufs_bindex_t bend, bindex, i;
26891 +       unsigned char shared;
26892 +       struct path h_path;
26893 +       struct super_block *h_sb;
26894 +
26895 +       err = 0;
26896 +       bsize = LONG_MAX;
26897 +       files = 0;
26898 +       ffree = 0;
26899 +       blocks = 0;
26900 +       bfree = 0;
26901 +       bavail = 0;
26902 +       bend = au_sbend(sb);
26903 +       for (bindex = 0; bindex <= bend; bindex++) {
26904 +               h_path.mnt = au_sbr_mnt(sb, bindex);
26905 +               h_sb = h_path.mnt->mnt_sb;
26906 +               shared = 0;
26907 +               for (i = 0; !shared && i < bindex; i++)
26908 +                       shared = (au_sbr_sb(sb, i) == h_sb);
26909 +               if (shared)
26910 +                       continue;
26911 +
26912 +               /* sb->s_root for NFS is unreliable */
26913 +               h_path.dentry = h_path.mnt->mnt_root;
26914 +               err = vfs_statfs(&h_path, buf);
26915 +               if (unlikely(err))
26916 +                       goto out;
26917 +
26918 +               if (bsize > buf->f_bsize) {
26919 +                       /*
26920 +                        * we will reduce bsize, so we have to expand blocks
26921 +                        * etc. to match them again
26922 +                        */
26923 +                       factor = (bsize / buf->f_bsize);
26924 +                       blocks = au_mul_till_max(blocks, factor);
26925 +                       bfree = au_mul_till_max(bfree, factor);
26926 +                       bavail = au_mul_till_max(bavail, factor);
26927 +                       bsize = buf->f_bsize;
26928 +               }
26929 +
26930 +               factor = (buf->f_bsize / bsize);
26931 +               blocks = au_add_till_max(blocks,
26932 +                               au_mul_till_max(buf->f_blocks, factor));
26933 +               bfree = au_add_till_max(bfree,
26934 +                               au_mul_till_max(buf->f_bfree, factor));
26935 +               bavail = au_add_till_max(bavail,
26936 +                               au_mul_till_max(buf->f_bavail, factor));
26937 +               files = au_add_till_max(files, buf->f_files);
26938 +               ffree = au_add_till_max(ffree, buf->f_ffree);
26939 +       }
26940 +
26941 +       buf->f_bsize = bsize;
26942 +       buf->f_blocks = blocks;
26943 +       buf->f_bfree = bfree;
26944 +       buf->f_bavail = bavail;
26945 +       buf->f_files = files;
26946 +       buf->f_ffree = ffree;
26947 +       buf->f_frsize = 0;
26948 +
26949 +out:
26950 +       return err;
26951 +}
26952 +
26953 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
26954 +{
26955 +       int err;
26956 +       struct path h_path;
26957 +       struct super_block *sb;
26958 +
26959 +       /* lock free root dinfo */
26960 +       sb = dentry->d_sb;
26961 +       si_noflush_read_lock(sb);
26962 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
26963 +               /* sb->s_root for NFS is unreliable */
26964 +               h_path.mnt = au_sbr_mnt(sb, 0);
26965 +               h_path.dentry = h_path.mnt->mnt_root;
26966 +               err = vfs_statfs(&h_path, buf);
26967 +       } else
26968 +               err = au_statfs_sum(sb, buf);
26969 +       si_read_unlock(sb);
26970 +
26971 +       if (!err) {
26972 +               buf->f_type = AUFS_SUPER_MAGIC;
26973 +               buf->f_namelen = AUFS_MAX_NAMELEN;
26974 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
26975 +       }
26976 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
26977 +
26978 +       return err;
26979 +}
26980 +
26981 +/* ---------------------------------------------------------------------- */
26982 +
26983 +static int aufs_sync_fs(struct super_block *sb, int wait)
26984 +{
26985 +       int err, e;
26986 +       aufs_bindex_t bend, bindex;
26987 +       struct au_branch *br;
26988 +       struct super_block *h_sb;
26989 +
26990 +       err = 0;
26991 +       si_noflush_read_lock(sb);
26992 +       bend = au_sbend(sb);
26993 +       for (bindex = 0; bindex <= bend; bindex++) {
26994 +               br = au_sbr(sb, bindex);
26995 +               if (!au_br_writable(br->br_perm))
26996 +                       continue;
26997 +
26998 +               h_sb = au_sbr_sb(sb, bindex);
26999 +               if (h_sb->s_op->sync_fs) {
27000 +                       e = h_sb->s_op->sync_fs(h_sb, wait);
27001 +                       if (unlikely(e && !err))
27002 +                               err = e;
27003 +                       /* go on even if an error happens */
27004 +               }
27005 +       }
27006 +       si_read_unlock(sb);
27007 +
27008 +       return err;
27009 +}
27010 +
27011 +/* ---------------------------------------------------------------------- */
27012 +
27013 +/* final actions when unmounting a file system */
27014 +static void aufs_put_super(struct super_block *sb)
27015 +{
27016 +       struct au_sbinfo *sbinfo;
27017 +
27018 +       sbinfo = au_sbi(sb);
27019 +       if (!sbinfo)
27020 +               return;
27021 +
27022 +       dbgaufs_si_fin(sbinfo);
27023 +       kobject_put(&sbinfo->si_kobj);
27024 +}
27025 +
27026 +/* ---------------------------------------------------------------------- */
27027 +
27028 +void au_array_free(void *array)
27029 +{
27030 +       if (array) {
27031 +               if (!is_vmalloc_addr(array))
27032 +                       kfree(array);
27033 +               else
27034 +                       vfree(array);
27035 +       }
27036 +}
27037 +
27038 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
27039 +{
27040 +       void *array;
27041 +       unsigned long long n, sz;
27042 +
27043 +       array = NULL;
27044 +       n = 0;
27045 +       if (!*hint)
27046 +               goto out;
27047 +
27048 +       if (*hint > ULLONG_MAX / sizeof(array)) {
27049 +               array = ERR_PTR(-EMFILE);
27050 +               pr_err("hint %llu\n", *hint);
27051 +               goto out;
27052 +       }
27053 +
27054 +       sz = sizeof(array) * *hint;
27055 +       array = kzalloc(sz, GFP_NOFS);
27056 +       if (unlikely(!array))
27057 +               array = vzalloc(sz);
27058 +       if (unlikely(!array)) {
27059 +               array = ERR_PTR(-ENOMEM);
27060 +               goto out;
27061 +       }
27062 +
27063 +       n = cb(array, *hint, arg);
27064 +       AuDebugOn(n > *hint);
27065 +
27066 +out:
27067 +       *hint = n;
27068 +       return array;
27069 +}
27070 +
27071 +static unsigned long long au_iarray_cb(void *a,
27072 +                                      unsigned long long max __maybe_unused,
27073 +                                      void *arg)
27074 +{
27075 +       unsigned long long n;
27076 +       struct inode **p, *inode;
27077 +       struct list_head *head;
27078 +
27079 +       n = 0;
27080 +       p = a;
27081 +       head = arg;
27082 +       spin_lock(&inode_sb_list_lock);
27083 +       list_for_each_entry(inode, head, i_sb_list) {
27084 +               if (!is_bad_inode(inode)
27085 +                   && au_ii(inode)->ii_bstart >= 0) {
27086 +                       spin_lock(&inode->i_lock);
27087 +                       if (atomic_read(&inode->i_count)) {
27088 +                               au_igrab(inode);
27089 +                               *p++ = inode;
27090 +                               n++;
27091 +                               AuDebugOn(n > max);
27092 +                       }
27093 +                       spin_unlock(&inode->i_lock);
27094 +               }
27095 +       }
27096 +       spin_unlock(&inode_sb_list_lock);
27097 +
27098 +       return n;
27099 +}
27100 +
27101 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
27102 +{
27103 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
27104 +       return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
27105 +}
27106 +
27107 +void au_iarray_free(struct inode **a, unsigned long long max)
27108 +{
27109 +       unsigned long long ull;
27110 +
27111 +       for (ull = 0; ull < max; ull++)
27112 +               iput(a[ull]);
27113 +       au_array_free(a);
27114 +}
27115 +
27116 +/* ---------------------------------------------------------------------- */
27117 +
27118 +/*
27119 + * refresh dentry and inode at remount time.
27120 + */
27121 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
27122 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
27123 +                     struct dentry *parent)
27124 +{
27125 +       int err;
27126 +
27127 +       di_write_lock_child(dentry);
27128 +       di_read_lock_parent(parent, AuLock_IR);
27129 +       err = au_refresh_dentry(dentry, parent);
27130 +       if (!err && dir_flags)
27131 +               au_hn_reset(dentry->d_inode, dir_flags);
27132 +       di_read_unlock(parent, AuLock_IR);
27133 +       di_write_unlock(dentry);
27134 +
27135 +       return err;
27136 +}
27137 +
27138 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
27139 +                          struct au_sbinfo *sbinfo,
27140 +                          const unsigned int dir_flags)
27141 +{
27142 +       int err;
27143 +       struct dentry *parent;
27144 +       struct inode *inode;
27145 +
27146 +       err = 0;
27147 +       parent = dget_parent(dentry);
27148 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
27149 +               inode = dentry->d_inode;
27150 +               if (inode) {
27151 +                       if (!S_ISDIR(inode->i_mode))
27152 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
27153 +                                                parent);
27154 +                       else {
27155 +                               err = au_do_refresh(dentry, dir_flags, parent);
27156 +                               if (unlikely(err))
27157 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
27158 +                       }
27159 +               } else
27160 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
27161 +               AuDbgDentry(dentry);
27162 +       }
27163 +       dput(parent);
27164 +
27165 +       AuTraceErr(err);
27166 +       return err;
27167 +}
27168 +
27169 +static int au_refresh_d(struct super_block *sb)
27170 +{
27171 +       int err, i, j, ndentry, e;
27172 +       unsigned int sigen;
27173 +       struct au_dcsub_pages dpages;
27174 +       struct au_dpage *dpage;
27175 +       struct dentry **dentries, *d;
27176 +       struct au_sbinfo *sbinfo;
27177 +       struct dentry *root = sb->s_root;
27178 +       const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
27179 +
27180 +       err = au_dpages_init(&dpages, GFP_NOFS);
27181 +       if (unlikely(err))
27182 +               goto out;
27183 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
27184 +       if (unlikely(err))
27185 +               goto out_dpages;
27186 +
27187 +       sigen = au_sigen(sb);
27188 +       sbinfo = au_sbi(sb);
27189 +       for (i = 0; i < dpages.ndpage; i++) {
27190 +               dpage = dpages.dpages + i;
27191 +               dentries = dpage->dentries;
27192 +               ndentry = dpage->ndentry;
27193 +               for (j = 0; j < ndentry; j++) {
27194 +                       d = dentries[j];
27195 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
27196 +                       if (unlikely(e && !err))
27197 +                               err = e;
27198 +                       /* go on even err */
27199 +               }
27200 +       }
27201 +
27202 +out_dpages:
27203 +       au_dpages_free(&dpages);
27204 +out:
27205 +       return err;
27206 +}
27207 +
27208 +static int au_refresh_i(struct super_block *sb)
27209 +{
27210 +       int err, e;
27211 +       unsigned int sigen;
27212 +       unsigned long long max, ull;
27213 +       struct inode *inode, **array;
27214 +
27215 +       array = au_iarray_alloc(sb, &max);
27216 +       err = PTR_ERR(array);
27217 +       if (IS_ERR(array))
27218 +               goto out;
27219 +
27220 +       err = 0;
27221 +       sigen = au_sigen(sb);
27222 +       for (ull = 0; ull < max; ull++) {
27223 +               inode = array[ull];
27224 +               if (unlikely(!inode))
27225 +                       break;
27226 +               if (au_iigen(inode, NULL) != sigen) {
27227 +                       ii_write_lock_child(inode);
27228 +                       e = au_refresh_hinode_self(inode);
27229 +                       ii_write_unlock(inode);
27230 +                       if (unlikely(e)) {
27231 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
27232 +                               if (!err)
27233 +                                       err = e;
27234 +                               /* go on even if err */
27235 +                       }
27236 +               }
27237 +       }
27238 +
27239 +       au_iarray_free(array, max);
27240 +
27241 +out:
27242 +       return err;
27243 +}
27244 +
27245 +static void au_remount_refresh(struct super_block *sb)
27246 +{
27247 +       int err, e;
27248 +       unsigned int udba;
27249 +       aufs_bindex_t bindex, bend;
27250 +       struct dentry *root;
27251 +       struct inode *inode;
27252 +       struct au_branch *br;
27253 +
27254 +       au_sigen_inc(sb);
27255 +       au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
27256 +
27257 +       root = sb->s_root;
27258 +       DiMustNoWaiters(root);
27259 +       inode = root->d_inode;
27260 +       IiMustNoWaiters(inode);
27261 +
27262 +       udba = au_opt_udba(sb);
27263 +       bend = au_sbend(sb);
27264 +       for (bindex = 0; bindex <= bend; bindex++) {
27265 +               br = au_sbr(sb, bindex);
27266 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
27267 +               if (unlikely(err))
27268 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27269 +                               bindex, err);
27270 +               /* go on even if err */
27271 +       }
27272 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
27273 +
27274 +       di_write_unlock(root);
27275 +       err = au_refresh_d(sb);
27276 +       e = au_refresh_i(sb);
27277 +       if (unlikely(e && !err))
27278 +               err = e;
27279 +       /* aufs_write_lock() calls ..._child() */
27280 +       di_write_lock_child(root);
27281 +
27282 +       au_cpup_attr_all(inode, /*force*/1);
27283 +
27284 +       if (unlikely(err))
27285 +               AuIOErr("refresh failed, ignored, %d\n", err);
27286 +}
27287 +
27288 +/* stop extra interpretation of errno in mount(8), and strange error messages */
27289 +static int cvt_err(int err)
27290 +{
27291 +       AuTraceErr(err);
27292 +
27293 +       switch (err) {
27294 +       case -ENOENT:
27295 +       case -ENOTDIR:
27296 +       case -EEXIST:
27297 +       case -EIO:
27298 +               err = -EINVAL;
27299 +       }
27300 +       return err;
27301 +}
27302 +
27303 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
27304 +{
27305 +       int err, do_dx;
27306 +       unsigned int mntflags;
27307 +       struct au_opts opts;
27308 +       struct dentry *root;
27309 +       struct inode *inode;
27310 +       struct au_sbinfo *sbinfo;
27311 +
27312 +       err = 0;
27313 +       root = sb->s_root;
27314 +       if (!data || !*data) {
27315 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27316 +               if (!err) {
27317 +                       di_write_lock_child(root);
27318 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
27319 +                       aufs_write_unlock(root);
27320 +               }
27321 +               goto out;
27322 +       }
27323 +
27324 +       err = -ENOMEM;
27325 +       memset(&opts, 0, sizeof(opts));
27326 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
27327 +       if (unlikely(!opts.opt))
27328 +               goto out;
27329 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27330 +       opts.flags = AuOpts_REMOUNT;
27331 +       opts.sb_flags = *flags;
27332 +
27333 +       /* parse it before aufs lock */
27334 +       err = au_opts_parse(sb, data, &opts);
27335 +       if (unlikely(err))
27336 +               goto out_opts;
27337 +
27338 +       sbinfo = au_sbi(sb);
27339 +       inode = root->d_inode;
27340 +       mutex_lock(&inode->i_mutex);
27341 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27342 +       if (unlikely(err))
27343 +               goto out_mtx;
27344 +       di_write_lock_child(root);
27345 +
27346 +       /* au_opts_remount() may return an error */
27347 +       err = au_opts_remount(sb, &opts);
27348 +       au_opts_free(&opts);
27349 +
27350 +       if (au_ftest_opts(opts.flags, REFRESH))
27351 +               au_remount_refresh(sb);
27352 +
27353 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
27354 +               mntflags = au_mntflags(sb);
27355 +               do_dx = !!au_opt_test(mntflags, DIO);
27356 +               au_dy_arefresh(do_dx);
27357 +       }
27358 +
27359 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
27360 +       aufs_write_unlock(root);
27361 +
27362 +out_mtx:
27363 +       mutex_unlock(&inode->i_mutex);
27364 +out_opts:
27365 +       free_page((unsigned long)opts.opt);
27366 +out:
27367 +       err = cvt_err(err);
27368 +       AuTraceErr(err);
27369 +       return err;
27370 +}
27371 +
27372 +static const struct super_operations aufs_sop = {
27373 +       .alloc_inode    = aufs_alloc_inode,
27374 +       .destroy_inode  = aufs_destroy_inode,
27375 +       /* always deleting, no clearing */
27376 +       .drop_inode     = generic_delete_inode,
27377 +       .show_options   = aufs_show_options,
27378 +       .statfs         = aufs_statfs,
27379 +       .put_super      = aufs_put_super,
27380 +       .sync_fs        = aufs_sync_fs,
27381 +       .remount_fs     = aufs_remount_fs
27382 +};
27383 +
27384 +/* ---------------------------------------------------------------------- */
27385 +
27386 +static int alloc_root(struct super_block *sb)
27387 +{
27388 +       int err;
27389 +       struct inode *inode;
27390 +       struct dentry *root;
27391 +
27392 +       err = -ENOMEM;
27393 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
27394 +       err = PTR_ERR(inode);
27395 +       if (IS_ERR(inode))
27396 +               goto out;
27397 +
27398 +       inode->i_op = &aufs_dir_iop;
27399 +       inode->i_fop = &aufs_dir_fop;
27400 +       inode->i_mode = S_IFDIR;
27401 +       set_nlink(inode, 2);
27402 +       unlock_new_inode(inode);
27403 +
27404 +       root = d_make_root(inode);
27405 +       if (unlikely(!root))
27406 +               goto out;
27407 +       err = PTR_ERR(root);
27408 +       if (IS_ERR(root))
27409 +               goto out;
27410 +
27411 +       err = au_di_init(root);
27412 +       if (!err) {
27413 +               sb->s_root = root;
27414 +               return 0; /* success */
27415 +       }
27416 +       dput(root);
27417 +
27418 +out:
27419 +       return err;
27420 +}
27421 +
27422 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
27423 +                          int silent __maybe_unused)
27424 +{
27425 +       int err;
27426 +       struct au_opts opts;
27427 +       struct dentry *root;
27428 +       struct inode *inode;
27429 +       char *arg = raw_data;
27430 +
27431 +       if (unlikely(!arg || !*arg)) {
27432 +               err = -EINVAL;
27433 +               pr_err("no arg\n");
27434 +               goto out;
27435 +       }
27436 +
27437 +       err = -ENOMEM;
27438 +       memset(&opts, 0, sizeof(opts));
27439 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
27440 +       if (unlikely(!opts.opt))
27441 +               goto out;
27442 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27443 +       opts.sb_flags = sb->s_flags;
27444 +
27445 +       err = au_si_alloc(sb);
27446 +       if (unlikely(err))
27447 +               goto out_opts;
27448 +
27449 +       /* all timestamps always follow the ones on the branch */
27450 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
27451 +       sb->s_op = &aufs_sop;
27452 +       sb->s_d_op = &aufs_dop;
27453 +       sb->s_magic = AUFS_SUPER_MAGIC;
27454 +       sb->s_maxbytes = 0;
27455 +       sb->s_stack_depth = 1;
27456 +       au_export_init(sb);
27457 +       /* au_xattr_init(sb); */
27458 +
27459 +       err = alloc_root(sb);
27460 +       if (unlikely(err)) {
27461 +               si_write_unlock(sb);
27462 +               goto out_info;
27463 +       }
27464 +       root = sb->s_root;
27465 +       inode = root->d_inode;
27466 +
27467 +       /*
27468 +        * actually we can parse options regardless aufs lock here.
27469 +        * but at remount time, parsing must be done before aufs lock.
27470 +        * so we follow the same rule.
27471 +        */
27472 +       ii_write_lock_parent(inode);
27473 +       aufs_write_unlock(root);
27474 +       err = au_opts_parse(sb, arg, &opts);
27475 +       if (unlikely(err))
27476 +               goto out_root;
27477 +
27478 +       /* lock vfs_inode first, then aufs. */
27479 +       mutex_lock(&inode->i_mutex);
27480 +       aufs_write_lock(root);
27481 +       err = au_opts_mount(sb, &opts);
27482 +       au_opts_free(&opts);
27483 +       aufs_write_unlock(root);
27484 +       mutex_unlock(&inode->i_mutex);
27485 +       if (!err)
27486 +               goto out_opts; /* success */
27487 +
27488 +out_root:
27489 +       dput(root);
27490 +       sb->s_root = NULL;
27491 +out_info:
27492 +       dbgaufs_si_fin(au_sbi(sb));
27493 +       kobject_put(&au_sbi(sb)->si_kobj);
27494 +       sb->s_fs_info = NULL;
27495 +out_opts:
27496 +       free_page((unsigned long)opts.opt);
27497 +out:
27498 +       AuTraceErr(err);
27499 +       err = cvt_err(err);
27500 +       AuTraceErr(err);
27501 +       return err;
27502 +}
27503 +
27504 +/* ---------------------------------------------------------------------- */
27505 +
27506 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
27507 +                                const char *dev_name __maybe_unused,
27508 +                                void *raw_data)
27509 +{
27510 +       struct dentry *root;
27511 +       struct super_block *sb;
27512 +
27513 +       /* all timestamps always follow the ones on the branch */
27514 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
27515 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
27516 +       if (IS_ERR(root))
27517 +               goto out;
27518 +
27519 +       sb = root->d_sb;
27520 +       si_write_lock(sb, !AuLock_FLUSH);
27521 +       sysaufs_brs_add(sb, 0);
27522 +       si_write_unlock(sb);
27523 +       au_sbilist_add(sb);
27524 +
27525 +out:
27526 +       return root;
27527 +}
27528 +
27529 +static void aufs_kill_sb(struct super_block *sb)
27530 +{
27531 +       struct au_sbinfo *sbinfo;
27532 +
27533 +       sbinfo = au_sbi(sb);
27534 +       if (sbinfo) {
27535 +               au_sbilist_del(sb);
27536 +               aufs_write_lock(sb->s_root);
27537 +               au_fhsm_fin(sb);
27538 +               if (sbinfo->si_wbr_create_ops->fin)
27539 +                       sbinfo->si_wbr_create_ops->fin(sb);
27540 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
27541 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
27542 +                       au_remount_refresh(sb);
27543 +               }
27544 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27545 +                       au_plink_put(sb, /*verbose*/1);
27546 +               au_xino_clr(sb);
27547 +               sbinfo->si_sb = NULL;
27548 +               aufs_write_unlock(sb->s_root);
27549 +               au_nwt_flush(&sbinfo->si_nowait);
27550 +       }
27551 +       kill_anon_super(sb);
27552 +}
27553 +
27554 +struct file_system_type aufs_fs_type = {
27555 +       .name           = AUFS_FSTYPE,
27556 +       /* a race between rename and others */
27557 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
27558 +       .mount          = aufs_mount,
27559 +       .kill_sb        = aufs_kill_sb,
27560 +       /* no need to __module_get() and module_put(). */
27561 +       .owner          = THIS_MODULE,
27562 +};
27563 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
27564 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
27565 +++ linux/fs/aufs/super.h       2015-06-22 08:27:37.887501948 +0200
27566 @@ -0,0 +1,638 @@
27567 +/*
27568 + * Copyright (C) 2005-2015 Junjiro R. Okajima
27569 + *
27570 + * This program, aufs is free software; you can redistribute it and/or modify
27571 + * it under the terms of the GNU General Public License as published by
27572 + * the Free Software Foundation; either version 2 of the License, or
27573 + * (at your option) any later version.
27574 + *
27575 + * This program is distributed in the hope that it will be useful,
27576 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27577 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27578 + * GNU General Public License for more details.
27579 + *
27580 + * You should have received a copy of the GNU General Public License
27581 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27582 + */
27583 +
27584 +/*
27585 + * super_block operations
27586 + */
27587 +
27588 +#ifndef __AUFS_SUPER_H__
27589 +#define __AUFS_SUPER_H__
27590 +
27591 +#ifdef __KERNEL__
27592 +
27593 +#include <linux/fs.h>
27594 +#include "rwsem.h"
27595 +#include "spl.h"
27596 +#include "wkq.h"
27597 +
27598 +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
27599 +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
27600 +                              loff_t *);
27601 +
27602 +/* policies to select one among multiple writable branches */
27603 +struct au_wbr_copyup_operations {
27604 +       int (*copyup)(struct dentry *dentry);
27605 +};
27606 +
27607 +#define AuWbr_DIR      1               /* target is a dir */
27608 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
27609 +
27610 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
27611 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
27612 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
27613 +
27614 +struct au_wbr_create_operations {
27615 +       int (*create)(struct dentry *dentry, unsigned int flags);
27616 +       int (*init)(struct super_block *sb);
27617 +       int (*fin)(struct super_block *sb);
27618 +};
27619 +
27620 +struct au_wbr_mfs {
27621 +       struct mutex    mfs_lock; /* protect this structure */
27622 +       unsigned long   mfs_jiffy;
27623 +       unsigned long   mfs_expire;
27624 +       aufs_bindex_t   mfs_bindex;
27625 +
27626 +       unsigned long long      mfsrr_bytes;
27627 +       unsigned long long      mfsrr_watermark;
27628 +};
27629 +
27630 +struct pseudo_link {
27631 +       union {
27632 +               struct hlist_node hlist;
27633 +               struct rcu_head rcu;
27634 +       };
27635 +       struct inode *inode;
27636 +};
27637 +
27638 +#define AuPlink_NHASH 100
27639 +static inline int au_plink_hash(ino_t ino)
27640 +{
27641 +       return ino % AuPlink_NHASH;
27642 +}
27643 +
27644 +/* File-based Hierarchical Storage Management */
27645 +struct au_fhsm {
27646 +#ifdef CONFIG_AUFS_FHSM
27647 +       /* allow only one process who can receive the notification */
27648 +       spinlock_t              fhsm_spin;
27649 +       pid_t                   fhsm_pid;
27650 +       wait_queue_head_t       fhsm_wqh;
27651 +       atomic_t                fhsm_readable;
27652 +
27653 +       /* these are protected by si_rwsem */
27654 +       unsigned long           fhsm_expire;
27655 +       aufs_bindex_t           fhsm_bottom;
27656 +#endif
27657 +};
27658 +
27659 +struct au_branch;
27660 +struct au_sbinfo {
27661 +       /* nowait tasks in the system-wide workqueue */
27662 +       struct au_nowait_tasks  si_nowait;
27663 +
27664 +       /*
27665 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
27666 +        * rwsem for au_sbinfo is necessary.
27667 +        */
27668 +       struct au_rwsem         si_rwsem;
27669 +
27670 +       /* prevent recursive locking in deleting inode */
27671 +       struct {
27672 +               unsigned long           *bitmap;
27673 +               spinlock_t              tree_lock;
27674 +               struct radix_tree_root  tree;
27675 +       } au_si_pid;
27676 +
27677 +       /*
27678 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
27679 +        * remount.
27680 +        */
27681 +       atomic_long_t           si_ninodes, si_nfiles;
27682 +
27683 +       /* branch management */
27684 +       unsigned int            si_generation;
27685 +
27686 +       /* see AuSi_ flags */
27687 +       unsigned char           au_si_status;
27688 +
27689 +       aufs_bindex_t           si_bend;
27690 +
27691 +       /* dirty trick to keep br_id plus */
27692 +       unsigned int            si_last_br_id :
27693 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
27694 +       struct au_branch        **si_branch;
27695 +
27696 +       /* policy to select a writable branch */
27697 +       unsigned char           si_wbr_copyup;
27698 +       unsigned char           si_wbr_create;
27699 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
27700 +       struct au_wbr_create_operations *si_wbr_create_ops;
27701 +
27702 +       /* round robin */
27703 +       atomic_t                si_wbr_rr_next;
27704 +
27705 +       /* most free space */
27706 +       struct au_wbr_mfs       si_wbr_mfs;
27707 +
27708 +       /* File-based Hierarchical Storage Management */
27709 +       struct au_fhsm          si_fhsm;
27710 +
27711 +       /* mount flags */
27712 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
27713 +       unsigned int            si_mntflags;
27714 +
27715 +       /* external inode number (bitmap and translation table) */
27716 +       au_readf_t              si_xread;
27717 +       au_writef_t             si_xwrite;
27718 +       struct file             *si_xib;
27719 +       struct mutex            si_xib_mtx; /* protect xib members */
27720 +       unsigned long           *si_xib_buf;
27721 +       unsigned long           si_xib_last_pindex;
27722 +       int                     si_xib_next_bit;
27723 +       aufs_bindex_t           si_xino_brid;
27724 +       unsigned long           si_xino_jiffy;
27725 +       unsigned long           si_xino_expire;
27726 +       /* reserved for future use */
27727 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
27728 +
27729 +#ifdef CONFIG_AUFS_EXPORT
27730 +       /* i_generation */
27731 +       struct file             *si_xigen;
27732 +       atomic_t                si_xigen_next;
27733 +#endif
27734 +
27735 +       /* dirty trick to suppoer atomic_open */
27736 +       struct au_sphlhead      si_aopen;
27737 +
27738 +       /* vdir parameters */
27739 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
27740 +       unsigned int            si_rdblk;       /* deblk size */
27741 +       unsigned int            si_rdhash;      /* hash size */
27742 +
27743 +       /*
27744 +        * If the number of whiteouts are larger than si_dirwh, leave all of
27745 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
27746 +        * future fsck.aufs or kernel thread will remove them later.
27747 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
27748 +        */
27749 +       unsigned int            si_dirwh;
27750 +
27751 +       /* pseudo_link list */
27752 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
27753 +       wait_queue_head_t       si_plink_wq;
27754 +       spinlock_t              si_plink_maint_lock;
27755 +       pid_t                   si_plink_maint_pid;
27756 +
27757 +       /* file list */
27758 +       struct au_sphlhead      si_files;
27759 +
27760 +       /*
27761 +        * sysfs and lifetime management.
27762 +        * this is not a small structure and it may be a waste of memory in case
27763 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
27764 +        * but using sysfs is majority.
27765 +        */
27766 +       struct kobject          si_kobj;
27767 +#ifdef CONFIG_DEBUG_FS
27768 +       struct dentry            *si_dbgaufs;
27769 +       struct dentry            *si_dbgaufs_plink;
27770 +       struct dentry            *si_dbgaufs_xib;
27771 +#ifdef CONFIG_AUFS_EXPORT
27772 +       struct dentry            *si_dbgaufs_xigen;
27773 +#endif
27774 +#endif
27775 +
27776 +#ifdef CONFIG_AUFS_SBILIST
27777 +       struct list_head        si_list;
27778 +#endif
27779 +
27780 +       /* dirty, necessary for unmounting, sysfs and sysrq */
27781 +       struct super_block      *si_sb;
27782 +};
27783 +
27784 +/* sbinfo status flags */
27785 +/*
27786 + * set true when refresh_dirs() failed at remount time.
27787 + * then try refreshing dirs at access time again.
27788 + * if it is false, refreshing dirs at access time is unnecesary
27789 + */
27790 +#define AuSi_FAILED_REFRESH_DIR        1
27791 +
27792 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
27793 +
27794 +#ifndef CONFIG_AUFS_FHSM
27795 +#undef AuSi_FHSM
27796 +#define AuSi_FHSM              0
27797 +#endif
27798 +
27799 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
27800 +                                          unsigned int flag)
27801 +{
27802 +       AuRwMustAnyLock(&sbi->si_rwsem);
27803 +       return sbi->au_si_status & flag;
27804 +}
27805 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
27806 +#define au_fset_si(sbinfo, name) do { \
27807 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
27808 +       (sbinfo)->au_si_status |= AuSi_##name; \
27809 +} while (0)
27810 +#define au_fclr_si(sbinfo, name) do { \
27811 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
27812 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
27813 +} while (0)
27814 +
27815 +/* ---------------------------------------------------------------------- */
27816 +
27817 +/* policy to select one among writable branches */
27818 +#define AuWbrCopyup(sbinfo, ...) \
27819 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
27820 +#define AuWbrCreate(sbinfo, ...) \
27821 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
27822 +
27823 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
27824 +#define AuLock_DW              1               /* write-lock dentry */
27825 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
27826 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
27827 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
27828 +#define AuLock_DIR             (1 << 4)        /* target is a dir */
27829 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
27830 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
27831 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
27832 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
27833 +#define au_fset_lock(flags, name) \
27834 +       do { (flags) |= AuLock_##name; } while (0)
27835 +#define au_fclr_lock(flags, name) \
27836 +       do { (flags) &= ~AuLock_##name; } while (0)
27837 +
27838 +/* ---------------------------------------------------------------------- */
27839 +
27840 +/* super.c */
27841 +extern struct file_system_type aufs_fs_type;
27842 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
27843 +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
27844 +                                          void *arg);
27845 +void au_array_free(void *array);
27846 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
27847 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
27848 +void au_iarray_free(struct inode **a, unsigned long long max);
27849 +
27850 +/* sbinfo.c */
27851 +void au_si_free(struct kobject *kobj);
27852 +int au_si_alloc(struct super_block *sb);
27853 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
27854 +
27855 +unsigned int au_sigen_inc(struct super_block *sb);
27856 +aufs_bindex_t au_new_br_id(struct super_block *sb);
27857 +
27858 +int si_read_lock(struct super_block *sb, int flags);
27859 +int si_write_lock(struct super_block *sb, int flags);
27860 +int aufs_read_lock(struct dentry *dentry, int flags);
27861 +void aufs_read_unlock(struct dentry *dentry, int flags);
27862 +void aufs_write_lock(struct dentry *dentry);
27863 +void aufs_write_unlock(struct dentry *dentry);
27864 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
27865 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
27866 +
27867 +int si_pid_test_slow(struct super_block *sb);
27868 +void si_pid_set_slow(struct super_block *sb);
27869 +void si_pid_clr_slow(struct super_block *sb);
27870 +
27871 +/* wbr_policy.c */
27872 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
27873 +extern struct au_wbr_create_operations au_wbr_create_ops[];
27874 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
27875 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
27876 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart);
27877 +
27878 +/* mvdown.c */
27879 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
27880 +
27881 +#ifdef CONFIG_AUFS_FHSM
27882 +/* fhsm.c */
27883 +
27884 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
27885 +{
27886 +       pid_t pid;
27887 +
27888 +       spin_lock(&fhsm->fhsm_spin);
27889 +       pid = fhsm->fhsm_pid;
27890 +       spin_unlock(&fhsm->fhsm_spin);
27891 +
27892 +       return pid;
27893 +}
27894 +
27895 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
27896 +void au_fhsm_wrote_all(struct super_block *sb, int force);
27897 +int au_fhsm_fd(struct super_block *sb, int oflags);
27898 +int au_fhsm_br_alloc(struct au_branch *br);
27899 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
27900 +void au_fhsm_fin(struct super_block *sb);
27901 +void au_fhsm_init(struct au_sbinfo *sbinfo);
27902 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
27903 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
27904 +#else
27905 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
27906 +          int force)
27907 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
27908 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
27909 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
27910 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
27911 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
27912 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
27913 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
27914 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
27915 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
27916 +#endif
27917 +
27918 +/* ---------------------------------------------------------------------- */
27919 +
27920 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
27921 +{
27922 +       return sb->s_fs_info;
27923 +}
27924 +
27925 +/* ---------------------------------------------------------------------- */
27926 +
27927 +#ifdef CONFIG_AUFS_EXPORT
27928 +int au_test_nfsd(void);
27929 +void au_export_init(struct super_block *sb);
27930 +void au_xigen_inc(struct inode *inode);
27931 +int au_xigen_new(struct inode *inode);
27932 +int au_xigen_set(struct super_block *sb, struct file *base);
27933 +void au_xigen_clr(struct super_block *sb);
27934 +
27935 +static inline int au_busy_or_stale(void)
27936 +{
27937 +       if (!au_test_nfsd())
27938 +               return -EBUSY;
27939 +       return -ESTALE;
27940 +}
27941 +#else
27942 +AuStubInt0(au_test_nfsd, void)
27943 +AuStubVoid(au_export_init, struct super_block *sb)
27944 +AuStubVoid(au_xigen_inc, struct inode *inode)
27945 +AuStubInt0(au_xigen_new, struct inode *inode)
27946 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
27947 +AuStubVoid(au_xigen_clr, struct super_block *sb)
27948 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
27949 +#endif /* CONFIG_AUFS_EXPORT */
27950 +
27951 +/* ---------------------------------------------------------------------- */
27952 +
27953 +#ifdef CONFIG_AUFS_SBILIST
27954 +/* module.c */
27955 +extern struct au_splhead au_sbilist;
27956 +
27957 +static inline void au_sbilist_init(void)
27958 +{
27959 +       au_spl_init(&au_sbilist);
27960 +}
27961 +
27962 +static inline void au_sbilist_add(struct super_block *sb)
27963 +{
27964 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
27965 +}
27966 +
27967 +static inline void au_sbilist_del(struct super_block *sb)
27968 +{
27969 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
27970 +}
27971 +
27972 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
27973 +static inline void au_sbilist_lock(void)
27974 +{
27975 +       spin_lock(&au_sbilist.spin);
27976 +}
27977 +
27978 +static inline void au_sbilist_unlock(void)
27979 +{
27980 +       spin_unlock(&au_sbilist.spin);
27981 +}
27982 +#define AuGFP_SBILIST  GFP_ATOMIC
27983 +#else
27984 +AuStubVoid(au_sbilist_lock, void)
27985 +AuStubVoid(au_sbilist_unlock, void)
27986 +#define AuGFP_SBILIST  GFP_NOFS
27987 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
27988 +#else
27989 +AuStubVoid(au_sbilist_init, void)
27990 +AuStubVoid(au_sbilist_add, struct super_block *sb)
27991 +AuStubVoid(au_sbilist_del, struct super_block *sb)
27992 +AuStubVoid(au_sbilist_lock, void)
27993 +AuStubVoid(au_sbilist_unlock, void)
27994 +#define AuGFP_SBILIST  GFP_NOFS
27995 +#endif
27996 +
27997 +/* ---------------------------------------------------------------------- */
27998 +
27999 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
28000 +{
28001 +       /*
28002 +        * This function is a dynamic '__init' function actually,
28003 +        * so the tiny check for si_rwsem is unnecessary.
28004 +        */
28005 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
28006 +#ifdef CONFIG_DEBUG_FS
28007 +       sbinfo->si_dbgaufs = NULL;
28008 +       sbinfo->si_dbgaufs_plink = NULL;
28009 +       sbinfo->si_dbgaufs_xib = NULL;
28010 +#ifdef CONFIG_AUFS_EXPORT
28011 +       sbinfo->si_dbgaufs_xigen = NULL;
28012 +#endif
28013 +#endif
28014 +}
28015 +
28016 +/* ---------------------------------------------------------------------- */
28017 +
28018 +static inline pid_t si_pid_bit(void)
28019 +{
28020 +       /* the origin of pid is 1, but the bitmap's is 0 */
28021 +       return current->pid - 1;
28022 +}
28023 +
28024 +static inline int si_pid_test(struct super_block *sb)
28025 +{
28026 +       pid_t bit;
28027 +
28028 +       bit = si_pid_bit();
28029 +       if (bit < PID_MAX_DEFAULT)
28030 +               return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
28031 +       return si_pid_test_slow(sb);
28032 +}
28033 +
28034 +static inline void si_pid_set(struct super_block *sb)
28035 +{
28036 +       pid_t bit;
28037 +
28038 +       bit = si_pid_bit();
28039 +       if (bit < PID_MAX_DEFAULT) {
28040 +               AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
28041 +               set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
28042 +               /* smp_mb(); */
28043 +       } else
28044 +               si_pid_set_slow(sb);
28045 +}
28046 +
28047 +static inline void si_pid_clr(struct super_block *sb)
28048 +{
28049 +       pid_t bit;
28050 +
28051 +       bit = si_pid_bit();
28052 +       if (bit < PID_MAX_DEFAULT) {
28053 +               AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
28054 +               clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
28055 +               /* smp_mb(); */
28056 +       } else
28057 +               si_pid_clr_slow(sb);
28058 +}
28059 +
28060 +/* ---------------------------------------------------------------------- */
28061 +
28062 +/* lock superblock. mainly for entry point functions */
28063 +/*
28064 + * __si_read_lock, __si_write_lock,
28065 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
28066 + */
28067 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
28068 +
28069 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
28070 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
28071 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
28072 +
28073 +static inline void si_noflush_read_lock(struct super_block *sb)
28074 +{
28075 +       __si_read_lock(sb);
28076 +       si_pid_set(sb);
28077 +}
28078 +
28079 +static inline int si_noflush_read_trylock(struct super_block *sb)
28080 +{
28081 +       int locked;
28082 +
28083 +       locked = __si_read_trylock(sb);
28084 +       if (locked)
28085 +               si_pid_set(sb);
28086 +       return locked;
28087 +}
28088 +
28089 +static inline void si_noflush_write_lock(struct super_block *sb)
28090 +{
28091 +       __si_write_lock(sb);
28092 +       si_pid_set(sb);
28093 +}
28094 +
28095 +static inline int si_noflush_write_trylock(struct super_block *sb)
28096 +{
28097 +       int locked;
28098 +
28099 +       locked = __si_write_trylock(sb);
28100 +       if (locked)
28101 +               si_pid_set(sb);
28102 +       return locked;
28103 +}
28104 +
28105 +#if 0 /* reserved */
28106 +static inline int si_read_trylock(struct super_block *sb, int flags)
28107 +{
28108 +       if (au_ftest_lock(flags, FLUSH))
28109 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28110 +       return si_noflush_read_trylock(sb);
28111 +}
28112 +#endif
28113 +
28114 +static inline void si_read_unlock(struct super_block *sb)
28115 +{
28116 +       si_pid_clr(sb);
28117 +       __si_read_unlock(sb);
28118 +}
28119 +
28120 +#if 0 /* reserved */
28121 +static inline int si_write_trylock(struct super_block *sb, int flags)
28122 +{
28123 +       if (au_ftest_lock(flags, FLUSH))
28124 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28125 +       return si_noflush_write_trylock(sb);
28126 +}
28127 +#endif
28128 +
28129 +static inline void si_write_unlock(struct super_block *sb)
28130 +{
28131 +       si_pid_clr(sb);
28132 +       __si_write_unlock(sb);
28133 +}
28134 +
28135 +#if 0 /* reserved */
28136 +static inline void si_downgrade_lock(struct super_block *sb)
28137 +{
28138 +       __si_downgrade_lock(sb);
28139 +}
28140 +#endif
28141 +
28142 +/* ---------------------------------------------------------------------- */
28143 +
28144 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
28145 +{
28146 +       SiMustAnyLock(sb);
28147 +       return au_sbi(sb)->si_bend;
28148 +}
28149 +
28150 +static inline unsigned int au_mntflags(struct super_block *sb)
28151 +{
28152 +       SiMustAnyLock(sb);
28153 +       return au_sbi(sb)->si_mntflags;
28154 +}
28155 +
28156 +static inline unsigned int au_sigen(struct super_block *sb)
28157 +{
28158 +       SiMustAnyLock(sb);
28159 +       return au_sbi(sb)->si_generation;
28160 +}
28161 +
28162 +static inline void au_ninodes_inc(struct super_block *sb)
28163 +{
28164 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
28165 +}
28166 +
28167 +static inline void au_ninodes_dec(struct super_block *sb)
28168 +{
28169 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
28170 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
28171 +}
28172 +
28173 +static inline void au_nfiles_inc(struct super_block *sb)
28174 +{
28175 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
28176 +}
28177 +
28178 +static inline void au_nfiles_dec(struct super_block *sb)
28179 +{
28180 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
28181 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
28182 +}
28183 +
28184 +static inline struct au_branch *au_sbr(struct super_block *sb,
28185 +                                      aufs_bindex_t bindex)
28186 +{
28187 +       SiMustAnyLock(sb);
28188 +       return au_sbi(sb)->si_branch[0 + bindex];
28189 +}
28190 +
28191 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
28192 +{
28193 +       SiMustWriteLock(sb);
28194 +       au_sbi(sb)->si_xino_brid = brid;
28195 +}
28196 +
28197 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
28198 +{
28199 +       SiMustAnyLock(sb);
28200 +       return au_sbi(sb)->si_xino_brid;
28201 +}
28202 +
28203 +#endif /* __KERNEL__ */
28204 +#endif /* __AUFS_SUPER_H__ */
28205 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
28206 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
28207 +++ linux/fs/aufs/sysaufs.c     2015-06-22 08:27:37.887501948 +0200
28208 @@ -0,0 +1,104 @@
28209 +/*
28210 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28211 + *
28212 + * This program, aufs is free software; you can redistribute it and/or modify
28213 + * it under the terms of the GNU General Public License as published by
28214 + * the Free Software Foundation; either version 2 of the License, or
28215 + * (at your option) any later version.
28216 + *
28217 + * This program is distributed in the hope that it will be useful,
28218 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28219 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28220 + * GNU General Public License for more details.
28221 + *
28222 + * You should have received a copy of the GNU General Public License
28223 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28224 + */
28225 +
28226 +/*
28227 + * sysfs interface and lifetime management
28228 + * they are necessary regardless sysfs is disabled.
28229 + */
28230 +
28231 +#include <linux/random.h>
28232 +#include "aufs.h"
28233 +
28234 +unsigned long sysaufs_si_mask;
28235 +struct kset *sysaufs_kset;
28236 +
28237 +#define AuSiAttr(_name) { \
28238 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
28239 +       .show   = sysaufs_si_##_name,                           \
28240 +}
28241 +
28242 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
28243 +struct attribute *sysaufs_si_attrs[] = {
28244 +       &sysaufs_si_attr_xi_path.attr,
28245 +       NULL,
28246 +};
28247 +
28248 +static const struct sysfs_ops au_sbi_ops = {
28249 +       .show   = sysaufs_si_show
28250 +};
28251 +
28252 +static struct kobj_type au_sbi_ktype = {
28253 +       .release        = au_si_free,
28254 +       .sysfs_ops      = &au_sbi_ops,
28255 +       .default_attrs  = sysaufs_si_attrs
28256 +};
28257 +
28258 +/* ---------------------------------------------------------------------- */
28259 +
28260 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
28261 +{
28262 +       int err;
28263 +
28264 +       sbinfo->si_kobj.kset = sysaufs_kset;
28265 +       /* cf. sysaufs_name() */
28266 +       err = kobject_init_and_add
28267 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
28268 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
28269 +
28270 +       dbgaufs_si_null(sbinfo);
28271 +       if (!err) {
28272 +               err = dbgaufs_si_init(sbinfo);
28273 +               if (unlikely(err))
28274 +                       kobject_put(&sbinfo->si_kobj);
28275 +       }
28276 +       return err;
28277 +}
28278 +
28279 +void sysaufs_fin(void)
28280 +{
28281 +       dbgaufs_fin();
28282 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28283 +       kset_unregister(sysaufs_kset);
28284 +}
28285 +
28286 +int __init sysaufs_init(void)
28287 +{
28288 +       int err;
28289 +
28290 +       do {
28291 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
28292 +       } while (!sysaufs_si_mask);
28293 +
28294 +       err = -EINVAL;
28295 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
28296 +       if (unlikely(!sysaufs_kset))
28297 +               goto out;
28298 +       err = PTR_ERR(sysaufs_kset);
28299 +       if (IS_ERR(sysaufs_kset))
28300 +               goto out;
28301 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28302 +       if (unlikely(err)) {
28303 +               kset_unregister(sysaufs_kset);
28304 +               goto out;
28305 +       }
28306 +
28307 +       err = dbgaufs_init();
28308 +       if (unlikely(err))
28309 +               sysaufs_fin();
28310 +out:
28311 +       return err;
28312 +}
28313 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
28314 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
28315 +++ linux/fs/aufs/sysaufs.h     2015-06-22 08:27:37.887501948 +0200
28316 @@ -0,0 +1,101 @@
28317 +/*
28318 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28319 + *
28320 + * This program, aufs is free software; you can redistribute it and/or modify
28321 + * it under the terms of the GNU General Public License as published by
28322 + * the Free Software Foundation; either version 2 of the License, or
28323 + * (at your option) any later version.
28324 + *
28325 + * This program is distributed in the hope that it will be useful,
28326 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28327 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28328 + * GNU General Public License for more details.
28329 + *
28330 + * You should have received a copy of the GNU General Public License
28331 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28332 + */
28333 +
28334 +/*
28335 + * sysfs interface and mount lifetime management
28336 + */
28337 +
28338 +#ifndef __SYSAUFS_H__
28339 +#define __SYSAUFS_H__
28340 +
28341 +#ifdef __KERNEL__
28342 +
28343 +#include <linux/sysfs.h>
28344 +#include "module.h"
28345 +
28346 +struct super_block;
28347 +struct au_sbinfo;
28348 +
28349 +struct sysaufs_si_attr {
28350 +       struct attribute attr;
28351 +       int (*show)(struct seq_file *seq, struct super_block *sb);
28352 +};
28353 +
28354 +/* ---------------------------------------------------------------------- */
28355 +
28356 +/* sysaufs.c */
28357 +extern unsigned long sysaufs_si_mask;
28358 +extern struct kset *sysaufs_kset;
28359 +extern struct attribute *sysaufs_si_attrs[];
28360 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
28361 +int __init sysaufs_init(void);
28362 +void sysaufs_fin(void);
28363 +
28364 +/* ---------------------------------------------------------------------- */
28365 +
28366 +/* some people doesn't like to show a pointer in kernel */
28367 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
28368 +{
28369 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
28370 +}
28371 +
28372 +#define SysaufsSiNamePrefix    "si_"
28373 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
28374 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
28375 +{
28376 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
28377 +                sysaufs_si_id(sbinfo));
28378 +}
28379 +
28380 +struct au_branch;
28381 +#ifdef CONFIG_SYSFS
28382 +/* sysfs.c */
28383 +extern struct attribute_group *sysaufs_attr_group;
28384 +
28385 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
28386 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28387 +                        char *buf);
28388 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
28389 +#ifdef CONFIG_COMPAT
28390 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
28391 +#endif
28392 +
28393 +void sysaufs_br_init(struct au_branch *br);
28394 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
28395 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
28396 +
28397 +#define sysaufs_brs_init()     do {} while (0)
28398 +
28399 +#else
28400 +#define sysaufs_attr_group     NULL
28401 +
28402 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
28403 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
28404 +       struct attribute *attr, char *buf)
28405 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
28406 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
28407 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
28408 +
28409 +static inline void sysaufs_brs_init(void)
28410 +{
28411 +       sysaufs_brs = 0;
28412 +}
28413 +
28414 +#endif /* CONFIG_SYSFS */
28415 +
28416 +#endif /* __KERNEL__ */
28417 +#endif /* __SYSAUFS_H__ */
28418 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
28419 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
28420 +++ linux/fs/aufs/sysfs.c       2015-06-22 08:29:23.710091096 +0200
28421 @@ -0,0 +1,372 @@
28422 +/*
28423 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28424 + *
28425 + * This program, aufs is free software; you can redistribute it and/or modify
28426 + * it under the terms of the GNU General Public License as published by
28427 + * the Free Software Foundation; either version 2 of the License, or
28428 + * (at your option) any later version.
28429 + *
28430 + * This program is distributed in the hope that it will be useful,
28431 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28432 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28433 + * GNU General Public License for more details.
28434 + *
28435 + * You should have received a copy of the GNU General Public License
28436 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28437 + */
28438 +
28439 +/*
28440 + * sysfs interface
28441 + */
28442 +
28443 +#include <linux/compat.h>
28444 +#include <linux/seq_file.h>
28445 +#include "aufs.h"
28446 +
28447 +#ifdef CONFIG_AUFS_FS_MODULE
28448 +/* this entry violates the "one line per file" policy of sysfs */
28449 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
28450 +                          char *buf)
28451 +{
28452 +       ssize_t err;
28453 +       static char *conf =
28454 +/* this file is generated at compiling */
28455 +#include "conf.str"
28456 +               ;
28457 +
28458 +       err = snprintf(buf, PAGE_SIZE, conf);
28459 +       if (unlikely(err >= PAGE_SIZE))
28460 +               err = -EFBIG;
28461 +       return err;
28462 +}
28463 +
28464 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
28465 +#endif
28466 +
28467 +static struct attribute *au_attr[] = {
28468 +#ifdef CONFIG_AUFS_FS_MODULE
28469 +       &au_config_attr.attr,
28470 +#endif
28471 +       NULL,   /* need to NULL terminate the list of attributes */
28472 +};
28473 +
28474 +static struct attribute_group sysaufs_attr_group_body = {
28475 +       .attrs = au_attr
28476 +};
28477 +
28478 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
28479 +
28480 +/* ---------------------------------------------------------------------- */
28481 +
28482 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
28483 +{
28484 +       int err;
28485 +
28486 +       SiMustAnyLock(sb);
28487 +
28488 +       err = 0;
28489 +       if (au_opt_test(au_mntflags(sb), XINO)) {
28490 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
28491 +               seq_putc(seq, '\n');
28492 +       }
28493 +       return err;
28494 +}
28495 +
28496 +/*
28497 + * the lifetime of branch is independent from the entry under sysfs.
28498 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
28499 + * unlinked.
28500 + */
28501 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
28502 +                        aufs_bindex_t bindex, int idx)
28503 +{
28504 +       int err;
28505 +       struct path path;
28506 +       struct dentry *root;
28507 +       struct au_branch *br;
28508 +       au_br_perm_str_t perm;
28509 +
28510 +       AuDbg("b%d\n", bindex);
28511 +
28512 +       err = 0;
28513 +       root = sb->s_root;
28514 +       di_read_lock_parent(root, !AuLock_IR);
28515 +       br = au_sbr(sb, bindex);
28516 +
28517 +       switch (idx) {
28518 +       case AuBrSysfs_BR:
28519 +               path.mnt = au_br_mnt(br);
28520 +               path.dentry = au_h_dptr(root, bindex);
28521 +               au_seq_path(seq, &path);
28522 +               au_optstr_br_perm(&perm, br->br_perm);
28523 +               err = seq_printf(seq, "=%s\n", perm.a);
28524 +               break;
28525 +       case AuBrSysfs_BRID:
28526 +               err = seq_printf(seq, "%d\n", br->br_id);
28527 +               break;
28528 +       }
28529 +       di_read_unlock(root, !AuLock_IR);
28530 +       if (err == -1)
28531 +               err = -E2BIG;
28532 +
28533 +       return err;
28534 +}
28535 +
28536 +/* ---------------------------------------------------------------------- */
28537 +
28538 +static struct seq_file *au_seq(char *p, ssize_t len)
28539 +{
28540 +       struct seq_file *seq;
28541 +
28542 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
28543 +       if (seq) {
28544 +               /* mutex_init(&seq.lock); */
28545 +               seq->buf = p;
28546 +               seq->size = len;
28547 +               return seq; /* success */
28548 +       }
28549 +
28550 +       seq = ERR_PTR(-ENOMEM);
28551 +       return seq;
28552 +}
28553 +
28554 +#define SysaufsBr_PREFIX       "br"
28555 +#define SysaufsBrid_PREFIX     "brid"
28556 +
28557 +/* todo: file size may exceed PAGE_SIZE */
28558 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28559 +                       char *buf)
28560 +{
28561 +       ssize_t err;
28562 +       int idx;
28563 +       long l;
28564 +       aufs_bindex_t bend;
28565 +       struct au_sbinfo *sbinfo;
28566 +       struct super_block *sb;
28567 +       struct seq_file *seq;
28568 +       char *name;
28569 +       struct attribute **cattr;
28570 +
28571 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
28572 +       sb = sbinfo->si_sb;
28573 +
28574 +       /*
28575 +        * prevent a race condition between sysfs and aufs.
28576 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
28577 +        * prohibits maintaining the sysfs entries.
28578 +        * hew we acquire read lock after sysfs_get_active_two().
28579 +        * on the other hand, the remount process may maintain the sysfs/aufs
28580 +        * entries after acquiring write lock.
28581 +        * it can cause a deadlock.
28582 +        * simply we gave up processing read here.
28583 +        */
28584 +       err = -EBUSY;
28585 +       if (unlikely(!si_noflush_read_trylock(sb)))
28586 +               goto out;
28587 +
28588 +       seq = au_seq(buf, PAGE_SIZE);
28589 +       err = PTR_ERR(seq);
28590 +       if (IS_ERR(seq))
28591 +               goto out_unlock;
28592 +
28593 +       name = (void *)attr->name;
28594 +       cattr = sysaufs_si_attrs;
28595 +       while (*cattr) {
28596 +               if (!strcmp(name, (*cattr)->name)) {
28597 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
28598 +                               ->show(seq, sb);
28599 +                       goto out_seq;
28600 +               }
28601 +               cattr++;
28602 +       }
28603 +
28604 +       if (!strncmp(name, SysaufsBrid_PREFIX,
28605 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
28606 +               idx = AuBrSysfs_BRID;
28607 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
28608 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
28609 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
28610 +               idx = AuBrSysfs_BR;
28611 +               name += sizeof(SysaufsBr_PREFIX) - 1;
28612 +       } else
28613 +                 BUG();
28614 +
28615 +       err = kstrtol(name, 10, &l);
28616 +       if (!err) {
28617 +               bend = au_sbend(sb);
28618 +               if (l <= bend)
28619 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
28620 +               else
28621 +                       err = -ENOENT;
28622 +       }
28623 +
28624 +out_seq:
28625 +       if (!err) {
28626 +               err = seq->count;
28627 +               /* sysfs limit */
28628 +               if (unlikely(err == PAGE_SIZE))
28629 +                       err = -EFBIG;
28630 +       }
28631 +       kfree(seq);
28632 +out_unlock:
28633 +       si_read_unlock(sb);
28634 +out:
28635 +       return err;
28636 +}
28637 +
28638 +/* ---------------------------------------------------------------------- */
28639 +
28640 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
28641 +{
28642 +       int err;
28643 +       int16_t brid;
28644 +       aufs_bindex_t bindex, bend;
28645 +       size_t sz;
28646 +       char *buf;
28647 +       struct seq_file *seq;
28648 +       struct au_branch *br;
28649 +
28650 +       si_read_lock(sb, AuLock_FLUSH);
28651 +       bend = au_sbend(sb);
28652 +       err = bend + 1;
28653 +       if (!arg)
28654 +               goto out;
28655 +
28656 +       err = -ENOMEM;
28657 +       buf = (void *)__get_free_page(GFP_NOFS);
28658 +       if (unlikely(!buf))
28659 +               goto out;
28660 +
28661 +       seq = au_seq(buf, PAGE_SIZE);
28662 +       err = PTR_ERR(seq);
28663 +       if (IS_ERR(seq))
28664 +               goto out_buf;
28665 +
28666 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
28667 +       for (bindex = 0; bindex <= bend; bindex++, arg++) {
28668 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
28669 +               if (unlikely(err))
28670 +                       break;
28671 +
28672 +               br = au_sbr(sb, bindex);
28673 +               brid = br->br_id;
28674 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
28675 +               err = __put_user(brid, &arg->id);
28676 +               if (unlikely(err))
28677 +                       break;
28678 +
28679 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
28680 +               err = __put_user(br->br_perm, &arg->perm);
28681 +               if (unlikely(err))
28682 +                       break;
28683 +
28684 +               au_seq_path(seq, &br->br_path);
28685 +               err = seq_putc(seq, '\0');
28686 +               if (!err && seq->count <= sz) {
28687 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
28688 +                       seq->count = 0;
28689 +                       if (unlikely(err))
28690 +                               break;
28691 +               } else {
28692 +                       err = -E2BIG;
28693 +                       goto out_seq;
28694 +               }
28695 +       }
28696 +       if (unlikely(err))
28697 +               err = -EFAULT;
28698 +
28699 +out_seq:
28700 +       kfree(seq);
28701 +out_buf:
28702 +       free_page((unsigned long)buf);
28703 +out:
28704 +       si_read_unlock(sb);
28705 +       return err;
28706 +}
28707 +
28708 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
28709 +{
28710 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
28711 +}
28712 +
28713 +#ifdef CONFIG_COMPAT
28714 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
28715 +{
28716 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
28717 +}
28718 +#endif
28719 +
28720 +/* ---------------------------------------------------------------------- */
28721 +
28722 +void sysaufs_br_init(struct au_branch *br)
28723 +{
28724 +       int i;
28725 +       struct au_brsysfs *br_sysfs;
28726 +       struct attribute *attr;
28727 +
28728 +       br_sysfs = br->br_sysfs;
28729 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28730 +               attr = &br_sysfs->attr;
28731 +               sysfs_attr_init(attr);
28732 +               attr->name = br_sysfs->name;
28733 +               attr->mode = S_IRUGO;
28734 +               br_sysfs++;
28735 +       }
28736 +}
28737 +
28738 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
28739 +{
28740 +       struct au_branch *br;
28741 +       struct kobject *kobj;
28742 +       struct au_brsysfs *br_sysfs;
28743 +       int i;
28744 +       aufs_bindex_t bend;
28745 +
28746 +       dbgaufs_brs_del(sb, bindex);
28747 +
28748 +       if (!sysaufs_brs)
28749 +               return;
28750 +
28751 +       kobj = &au_sbi(sb)->si_kobj;
28752 +       bend = au_sbend(sb);
28753 +       for (; bindex <= bend; bindex++) {
28754 +               br = au_sbr(sb, bindex);
28755 +               br_sysfs = br->br_sysfs;
28756 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28757 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
28758 +                       br_sysfs++;
28759 +               }
28760 +       }
28761 +}
28762 +
28763 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
28764 +{
28765 +       int err, i;
28766 +       aufs_bindex_t bend;
28767 +       struct kobject *kobj;
28768 +       struct au_branch *br;
28769 +       struct au_brsysfs *br_sysfs;
28770 +
28771 +       dbgaufs_brs_add(sb, bindex);
28772 +
28773 +       if (!sysaufs_brs)
28774 +               return;
28775 +
28776 +       kobj = &au_sbi(sb)->si_kobj;
28777 +       bend = au_sbend(sb);
28778 +       for (; bindex <= bend; bindex++) {
28779 +               br = au_sbr(sb, bindex);
28780 +               br_sysfs = br->br_sysfs;
28781 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
28782 +                        SysaufsBr_PREFIX "%d", bindex);
28783 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
28784 +                        SysaufsBrid_PREFIX "%d", bindex);
28785 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28786 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
28787 +                       if (unlikely(err))
28788 +                               pr_warn("failed %s under sysfs(%d)\n",
28789 +                                       br_sysfs->name, err);
28790 +                       br_sysfs++;
28791 +               }
28792 +       }
28793 +}
28794 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
28795 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
28796 +++ linux/fs/aufs/sysrq.c       2015-06-22 08:27:37.887501948 +0200
28797 @@ -0,0 +1,157 @@
28798 +/*
28799 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28800 + *
28801 + * This program, aufs is free software; you can redistribute it and/or modify
28802 + * it under the terms of the GNU General Public License as published by
28803 + * the Free Software Foundation; either version 2 of the License, or
28804 + * (at your option) any later version.
28805 + *
28806 + * This program is distributed in the hope that it will be useful,
28807 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28808 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28809 + * GNU General Public License for more details.
28810 + *
28811 + * You should have received a copy of the GNU General Public License
28812 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28813 + */
28814 +
28815 +/*
28816 + * magic sysrq hanlder
28817 + */
28818 +
28819 +/* #include <linux/sysrq.h> */
28820 +#include <linux/writeback.h>
28821 +#include "aufs.h"
28822 +
28823 +/* ---------------------------------------------------------------------- */
28824 +
28825 +static void sysrq_sb(struct super_block *sb)
28826 +{
28827 +       char *plevel;
28828 +       struct au_sbinfo *sbinfo;
28829 +       struct file *file;
28830 +       struct au_sphlhead *files;
28831 +       struct au_finfo *finfo;
28832 +
28833 +       plevel = au_plevel;
28834 +       au_plevel = KERN_WARNING;
28835 +
28836 +       /* since we define pr_fmt, call printk directly */
28837 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
28838 +
28839 +       sbinfo = au_sbi(sb);
28840 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
28841 +       pr("superblock\n");
28842 +       au_dpri_sb(sb);
28843 +
28844 +#if 0
28845 +       pr("root dentry\n");
28846 +       au_dpri_dentry(sb->s_root);
28847 +       pr("root inode\n");
28848 +       au_dpri_inode(sb->s_root->d_inode);
28849 +#endif
28850 +
28851 +#if 0
28852 +       do {
28853 +               int err, i, j, ndentry;
28854 +               struct au_dcsub_pages dpages;
28855 +               struct au_dpage *dpage;
28856 +
28857 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
28858 +               if (unlikely(err))
28859 +                       break;
28860 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
28861 +               if (!err)
28862 +                       for (i = 0; i < dpages.ndpage; i++) {
28863 +                               dpage = dpages.dpages + i;
28864 +                               ndentry = dpage->ndentry;
28865 +                               for (j = 0; j < ndentry; j++)
28866 +                                       au_dpri_dentry(dpage->dentries[j]);
28867 +                       }
28868 +               au_dpages_free(&dpages);
28869 +       } while (0);
28870 +#endif
28871 +
28872 +#if 1
28873 +       {
28874 +               struct inode *i;
28875 +
28876 +               pr("isolated inode\n");
28877 +               spin_lock(&inode_sb_list_lock);
28878 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
28879 +                       spin_lock(&i->i_lock);
28880 +                       if (1 || hlist_empty(&i->i_dentry))
28881 +                               au_dpri_inode(i);
28882 +                       spin_unlock(&i->i_lock);
28883 +               }
28884 +               spin_unlock(&inode_sb_list_lock);
28885 +       }
28886 +#endif
28887 +       pr("files\n");
28888 +       files = &au_sbi(sb)->si_files;
28889 +       spin_lock(&files->spin);
28890 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
28891 +               umode_t mode;
28892 +
28893 +               file = finfo->fi_file;
28894 +               mode = file_inode(file)->i_mode;
28895 +               if (!special_file(mode))
28896 +                       au_dpri_file(file);
28897 +       }
28898 +       spin_unlock(&files->spin);
28899 +       pr("done\n");
28900 +
28901 +#undef pr
28902 +       au_plevel = plevel;
28903 +}
28904 +
28905 +/* ---------------------------------------------------------------------- */
28906 +
28907 +/* module parameter */
28908 +static char *aufs_sysrq_key = "a";
28909 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
28910 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
28911 +
28912 +static void au_sysrq(int key __maybe_unused)
28913 +{
28914 +       struct au_sbinfo *sbinfo;
28915 +
28916 +       lockdep_off();
28917 +       au_sbilist_lock();
28918 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
28919 +               sysrq_sb(sbinfo->si_sb);
28920 +       au_sbilist_unlock();
28921 +       lockdep_on();
28922 +}
28923 +
28924 +static struct sysrq_key_op au_sysrq_op = {
28925 +       .handler        = au_sysrq,
28926 +       .help_msg       = "Aufs",
28927 +       .action_msg     = "Aufs",
28928 +       .enable_mask    = SYSRQ_ENABLE_DUMP
28929 +};
28930 +
28931 +/* ---------------------------------------------------------------------- */
28932 +
28933 +int __init au_sysrq_init(void)
28934 +{
28935 +       int err;
28936 +       char key;
28937 +
28938 +       err = -1;
28939 +       key = *aufs_sysrq_key;
28940 +       if ('a' <= key && key <= 'z')
28941 +               err = register_sysrq_key(key, &au_sysrq_op);
28942 +       if (unlikely(err))
28943 +               pr_err("err %d, sysrq=%c\n", err, key);
28944 +       return err;
28945 +}
28946 +
28947 +void au_sysrq_fin(void)
28948 +{
28949 +       int err;
28950 +
28951 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
28952 +       if (unlikely(err))
28953 +               pr_err("err %d (ignored)\n", err);
28954 +}
28955 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
28956 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
28957 +++ linux/fs/aufs/vdir.c        2015-06-22 08:29:23.710091096 +0200
28958 @@ -0,0 +1,888 @@
28959 +/*
28960 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28961 + *
28962 + * This program, aufs is free software; you can redistribute it and/or modify
28963 + * it under the terms of the GNU General Public License as published by
28964 + * the Free Software Foundation; either version 2 of the License, or
28965 + * (at your option) any later version.
28966 + *
28967 + * This program is distributed in the hope that it will be useful,
28968 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28969 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28970 + * GNU General Public License for more details.
28971 + *
28972 + * You should have received a copy of the GNU General Public License
28973 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28974 + */
28975 +
28976 +/*
28977 + * virtual or vertical directory
28978 + */
28979 +
28980 +#include "aufs.h"
28981 +
28982 +static unsigned int calc_size(int nlen)
28983 +{
28984 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
28985 +}
28986 +
28987 +static int set_deblk_end(union au_vdir_deblk_p *p,
28988 +                        union au_vdir_deblk_p *deblk_end)
28989 +{
28990 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
28991 +               p->de->de_str.len = 0;
28992 +               /* smp_mb(); */
28993 +               return 0;
28994 +       }
28995 +       return -1; /* error */
28996 +}
28997 +
28998 +/* returns true or false */
28999 +static int is_deblk_end(union au_vdir_deblk_p *p,
29000 +                       union au_vdir_deblk_p *deblk_end)
29001 +{
29002 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
29003 +               return !p->de->de_str.len;
29004 +       return 1;
29005 +}
29006 +
29007 +static unsigned char *last_deblk(struct au_vdir *vdir)
29008 +{
29009 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
29010 +}
29011 +
29012 +/* ---------------------------------------------------------------------- */
29013 +
29014 +/* estimate the apropriate size for name hash table */
29015 +unsigned int au_rdhash_est(loff_t sz)
29016 +{
29017 +       unsigned int n;
29018 +
29019 +       n = UINT_MAX;
29020 +       sz >>= 10;
29021 +       if (sz < n)
29022 +               n = sz;
29023 +       if (sz < AUFS_RDHASH_DEF)
29024 +               n = AUFS_RDHASH_DEF;
29025 +       /* pr_info("n %u\n", n); */
29026 +       return n;
29027 +}
29028 +
29029 +/*
29030 + * the allocated memory has to be freed by
29031 + * au_nhash_wh_free() or au_nhash_de_free().
29032 + */
29033 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
29034 +{
29035 +       struct hlist_head *head;
29036 +       unsigned int u;
29037 +       size_t sz;
29038 +
29039 +       sz = sizeof(*nhash->nh_head) * num_hash;
29040 +       head = kmalloc(sz, gfp);
29041 +       if (head) {
29042 +               nhash->nh_num = num_hash;
29043 +               nhash->nh_head = head;
29044 +               for (u = 0; u < num_hash; u++)
29045 +                       INIT_HLIST_HEAD(head++);
29046 +               return 0; /* success */
29047 +       }
29048 +
29049 +       return -ENOMEM;
29050 +}
29051 +
29052 +static void nhash_count(struct hlist_head *head)
29053 +{
29054 +#if 0
29055 +       unsigned long n;
29056 +       struct hlist_node *pos;
29057 +
29058 +       n = 0;
29059 +       hlist_for_each(pos, head)
29060 +               n++;
29061 +       pr_info("%lu\n", n);
29062 +#endif
29063 +}
29064 +
29065 +static void au_nhash_wh_do_free(struct hlist_head *head)
29066 +{
29067 +       struct au_vdir_wh *pos;
29068 +       struct hlist_node *node;
29069 +
29070 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
29071 +               kfree(pos);
29072 +}
29073 +
29074 +static void au_nhash_de_do_free(struct hlist_head *head)
29075 +{
29076 +       struct au_vdir_dehstr *pos;
29077 +       struct hlist_node *node;
29078 +
29079 +       hlist_for_each_entry_safe(pos, node, head, hash)
29080 +               au_cache_free_vdir_dehstr(pos);
29081 +}
29082 +
29083 +static void au_nhash_do_free(struct au_nhash *nhash,
29084 +                            void (*free)(struct hlist_head *head))
29085 +{
29086 +       unsigned int n;
29087 +       struct hlist_head *head;
29088 +
29089 +       n = nhash->nh_num;
29090 +       if (!n)
29091 +               return;
29092 +
29093 +       head = nhash->nh_head;
29094 +       while (n-- > 0) {
29095 +               nhash_count(head);
29096 +               free(head++);
29097 +       }
29098 +       kfree(nhash->nh_head);
29099 +}
29100 +
29101 +void au_nhash_wh_free(struct au_nhash *whlist)
29102 +{
29103 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
29104 +}
29105 +
29106 +static void au_nhash_de_free(struct au_nhash *delist)
29107 +{
29108 +       au_nhash_do_free(delist, au_nhash_de_do_free);
29109 +}
29110 +
29111 +/* ---------------------------------------------------------------------- */
29112 +
29113 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
29114 +                           int limit)
29115 +{
29116 +       int num;
29117 +       unsigned int u, n;
29118 +       struct hlist_head *head;
29119 +       struct au_vdir_wh *pos;
29120 +
29121 +       num = 0;
29122 +       n = whlist->nh_num;
29123 +       head = whlist->nh_head;
29124 +       for (u = 0; u < n; u++, head++)
29125 +               hlist_for_each_entry(pos, head, wh_hash)
29126 +                       if (pos->wh_bindex == btgt && ++num > limit)
29127 +                               return 1;
29128 +       return 0;
29129 +}
29130 +
29131 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
29132 +                                      unsigned char *name,
29133 +                                      unsigned int len)
29134 +{
29135 +       unsigned int v;
29136 +       /* const unsigned int magic_bit = 12; */
29137 +
29138 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
29139 +
29140 +       v = 0;
29141 +       while (len--)
29142 +               v += *name++;
29143 +       /* v = hash_long(v, magic_bit); */
29144 +       v %= nhash->nh_num;
29145 +       return nhash->nh_head + v;
29146 +}
29147 +
29148 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
29149 +                             int nlen)
29150 +{
29151 +       return str->len == nlen && !memcmp(str->name, name, nlen);
29152 +}
29153 +
29154 +/* returns found or not */
29155 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
29156 +{
29157 +       struct hlist_head *head;
29158 +       struct au_vdir_wh *pos;
29159 +       struct au_vdir_destr *str;
29160 +
29161 +       head = au_name_hash(whlist, name, nlen);
29162 +       hlist_for_each_entry(pos, head, wh_hash) {
29163 +               str = &pos->wh_str;
29164 +               AuDbg("%.*s\n", str->len, str->name);
29165 +               if (au_nhash_test_name(str, name, nlen))
29166 +                       return 1;
29167 +       }
29168 +       return 0;
29169 +}
29170 +
29171 +/* returns found(true) or not */
29172 +static int test_known(struct au_nhash *delist, char *name, int nlen)
29173 +{
29174 +       struct hlist_head *head;
29175 +       struct au_vdir_dehstr *pos;
29176 +       struct au_vdir_destr *str;
29177 +
29178 +       head = au_name_hash(delist, name, nlen);
29179 +       hlist_for_each_entry(pos, head, hash) {
29180 +               str = pos->str;
29181 +               AuDbg("%.*s\n", str->len, str->name);
29182 +               if (au_nhash_test_name(str, name, nlen))
29183 +                       return 1;
29184 +       }
29185 +       return 0;
29186 +}
29187 +
29188 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
29189 +                           unsigned char d_type)
29190 +{
29191 +#ifdef CONFIG_AUFS_SHWH
29192 +       wh->wh_ino = ino;
29193 +       wh->wh_type = d_type;
29194 +#endif
29195 +}
29196 +
29197 +/* ---------------------------------------------------------------------- */
29198 +
29199 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
29200 +                      unsigned int d_type, aufs_bindex_t bindex,
29201 +                      unsigned char shwh)
29202 +{
29203 +       int err;
29204 +       struct au_vdir_destr *str;
29205 +       struct au_vdir_wh *wh;
29206 +
29207 +       AuDbg("%.*s\n", nlen, name);
29208 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
29209 +
29210 +       err = -ENOMEM;
29211 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
29212 +       if (unlikely(!wh))
29213 +               goto out;
29214 +
29215 +       err = 0;
29216 +       wh->wh_bindex = bindex;
29217 +       if (shwh)
29218 +               au_shwh_init_wh(wh, ino, d_type);
29219 +       str = &wh->wh_str;
29220 +       str->len = nlen;
29221 +       memcpy(str->name, name, nlen);
29222 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
29223 +       /* smp_mb(); */
29224 +
29225 +out:
29226 +       return err;
29227 +}
29228 +
29229 +static int append_deblk(struct au_vdir *vdir)
29230 +{
29231 +       int err;
29232 +       unsigned long ul;
29233 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29234 +       union au_vdir_deblk_p p, deblk_end;
29235 +       unsigned char **o;
29236 +
29237 +       err = -ENOMEM;
29238 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
29239 +                    GFP_NOFS);
29240 +       if (unlikely(!o))
29241 +               goto out;
29242 +
29243 +       vdir->vd_deblk = o;
29244 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
29245 +       if (p.deblk) {
29246 +               ul = vdir->vd_nblk++;
29247 +               vdir->vd_deblk[ul] = p.deblk;
29248 +               vdir->vd_last.ul = ul;
29249 +               vdir->vd_last.p.deblk = p.deblk;
29250 +               deblk_end.deblk = p.deblk + deblk_sz;
29251 +               err = set_deblk_end(&p, &deblk_end);
29252 +       }
29253 +
29254 +out:
29255 +       return err;
29256 +}
29257 +
29258 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
29259 +                    unsigned int d_type, struct au_nhash *delist)
29260 +{
29261 +       int err;
29262 +       unsigned int sz;
29263 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29264 +       union au_vdir_deblk_p p, *room, deblk_end;
29265 +       struct au_vdir_dehstr *dehstr;
29266 +
29267 +       p.deblk = last_deblk(vdir);
29268 +       deblk_end.deblk = p.deblk + deblk_sz;
29269 +       room = &vdir->vd_last.p;
29270 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
29271 +                 || !is_deblk_end(room, &deblk_end));
29272 +
29273 +       sz = calc_size(nlen);
29274 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
29275 +               err = append_deblk(vdir);
29276 +               if (unlikely(err))
29277 +                       goto out;
29278 +
29279 +               p.deblk = last_deblk(vdir);
29280 +               deblk_end.deblk = p.deblk + deblk_sz;
29281 +               /* smp_mb(); */
29282 +               AuDebugOn(room->deblk != p.deblk);
29283 +       }
29284 +
29285 +       err = -ENOMEM;
29286 +       dehstr = au_cache_alloc_vdir_dehstr();
29287 +       if (unlikely(!dehstr))
29288 +               goto out;
29289 +
29290 +       dehstr->str = &room->de->de_str;
29291 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
29292 +       room->de->de_ino = ino;
29293 +       room->de->de_type = d_type;
29294 +       room->de->de_str.len = nlen;
29295 +       memcpy(room->de->de_str.name, name, nlen);
29296 +
29297 +       err = 0;
29298 +       room->deblk += sz;
29299 +       if (unlikely(set_deblk_end(room, &deblk_end)))
29300 +               err = append_deblk(vdir);
29301 +       /* smp_mb(); */
29302 +
29303 +out:
29304 +       return err;
29305 +}
29306 +
29307 +/* ---------------------------------------------------------------------- */
29308 +
29309 +void au_vdir_free(struct au_vdir *vdir)
29310 +{
29311 +       unsigned char **deblk;
29312 +
29313 +       deblk = vdir->vd_deblk;
29314 +       while (vdir->vd_nblk--)
29315 +               kfree(*deblk++);
29316 +       kfree(vdir->vd_deblk);
29317 +       au_cache_free_vdir(vdir);
29318 +}
29319 +
29320 +static struct au_vdir *alloc_vdir(struct file *file)
29321 +{
29322 +       struct au_vdir *vdir;
29323 +       struct super_block *sb;
29324 +       int err;
29325 +
29326 +       sb = file->f_path.dentry->d_sb;
29327 +       SiMustAnyLock(sb);
29328 +
29329 +       err = -ENOMEM;
29330 +       vdir = au_cache_alloc_vdir();
29331 +       if (unlikely(!vdir))
29332 +               goto out;
29333 +
29334 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
29335 +       if (unlikely(!vdir->vd_deblk))
29336 +               goto out_free;
29337 +
29338 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
29339 +       if (!vdir->vd_deblk_sz) {
29340 +               /* estimate the apropriate size for deblk */
29341 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
29342 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
29343 +       }
29344 +       vdir->vd_nblk = 0;
29345 +       vdir->vd_version = 0;
29346 +       vdir->vd_jiffy = 0;
29347 +       err = append_deblk(vdir);
29348 +       if (!err)
29349 +               return vdir; /* success */
29350 +
29351 +       kfree(vdir->vd_deblk);
29352 +
29353 +out_free:
29354 +       au_cache_free_vdir(vdir);
29355 +out:
29356 +       vdir = ERR_PTR(err);
29357 +       return vdir;
29358 +}
29359 +
29360 +static int reinit_vdir(struct au_vdir *vdir)
29361 +{
29362 +       int err;
29363 +       union au_vdir_deblk_p p, deblk_end;
29364 +
29365 +       while (vdir->vd_nblk > 1) {
29366 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
29367 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
29368 +               vdir->vd_nblk--;
29369 +       }
29370 +       p.deblk = vdir->vd_deblk[0];
29371 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
29372 +       err = set_deblk_end(&p, &deblk_end);
29373 +       /* keep vd_dblk_sz */
29374 +       vdir->vd_last.ul = 0;
29375 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29376 +       vdir->vd_version = 0;
29377 +       vdir->vd_jiffy = 0;
29378 +       /* smp_mb(); */
29379 +       return err;
29380 +}
29381 +
29382 +/* ---------------------------------------------------------------------- */
29383 +
29384 +#define AuFillVdir_CALLED      1
29385 +#define AuFillVdir_WHABLE      (1 << 1)
29386 +#define AuFillVdir_SHWH                (1 << 2)
29387 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
29388 +#define au_fset_fillvdir(flags, name) \
29389 +       do { (flags) |= AuFillVdir_##name; } while (0)
29390 +#define au_fclr_fillvdir(flags, name) \
29391 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
29392 +
29393 +#ifndef CONFIG_AUFS_SHWH
29394 +#undef AuFillVdir_SHWH
29395 +#define AuFillVdir_SHWH                0
29396 +#endif
29397 +
29398 +struct fillvdir_arg {
29399 +       struct dir_context      ctx;
29400 +       struct file             *file;
29401 +       struct au_vdir          *vdir;
29402 +       struct au_nhash         delist;
29403 +       struct au_nhash         whlist;
29404 +       aufs_bindex_t           bindex;
29405 +       unsigned int            flags;
29406 +       int                     err;
29407 +};
29408 +
29409 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
29410 +                   loff_t offset __maybe_unused, u64 h_ino,
29411 +                   unsigned int d_type)
29412 +{
29413 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
29414 +       char *name = (void *)__name;
29415 +       struct super_block *sb;
29416 +       ino_t ino;
29417 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
29418 +
29419 +       arg->err = 0;
29420 +       sb = arg->file->f_path.dentry->d_sb;
29421 +       au_fset_fillvdir(arg->flags, CALLED);
29422 +       /* smp_mb(); */
29423 +       if (nlen <= AUFS_WH_PFX_LEN
29424 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
29425 +               if (test_known(&arg->delist, name, nlen)
29426 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
29427 +                       goto out; /* already exists or whiteouted */
29428 +
29429 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
29430 +               if (!arg->err) {
29431 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
29432 +                               d_type = DT_UNKNOWN;
29433 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
29434 +                                            d_type, &arg->delist);
29435 +               }
29436 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
29437 +               name += AUFS_WH_PFX_LEN;
29438 +               nlen -= AUFS_WH_PFX_LEN;
29439 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
29440 +                       goto out; /* already whiteouted */
29441 +
29442 +               if (shwh)
29443 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
29444 +                                            &ino);
29445 +               if (!arg->err) {
29446 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
29447 +                               d_type = DT_UNKNOWN;
29448 +                       arg->err = au_nhash_append_wh
29449 +                               (&arg->whlist, name, nlen, ino, d_type,
29450 +                                arg->bindex, shwh);
29451 +               }
29452 +       }
29453 +
29454 +out:
29455 +       if (!arg->err)
29456 +               arg->vdir->vd_jiffy = jiffies;
29457 +       /* smp_mb(); */
29458 +       AuTraceErr(arg->err);
29459 +       return arg->err;
29460 +}
29461 +
29462 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
29463 +                         struct au_nhash *whlist, struct au_nhash *delist)
29464 +{
29465 +#ifdef CONFIG_AUFS_SHWH
29466 +       int err;
29467 +       unsigned int nh, u;
29468 +       struct hlist_head *head;
29469 +       struct au_vdir_wh *pos;
29470 +       struct hlist_node *n;
29471 +       char *p, *o;
29472 +       struct au_vdir_destr *destr;
29473 +
29474 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
29475 +
29476 +       err = -ENOMEM;
29477 +       o = p = (void *)__get_free_page(GFP_NOFS);
29478 +       if (unlikely(!p))
29479 +               goto out;
29480 +
29481 +       err = 0;
29482 +       nh = whlist->nh_num;
29483 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29484 +       p += AUFS_WH_PFX_LEN;
29485 +       for (u = 0; u < nh; u++) {
29486 +               head = whlist->nh_head + u;
29487 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
29488 +                       destr = &pos->wh_str;
29489 +                       memcpy(p, destr->name, destr->len);
29490 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
29491 +                                       pos->wh_ino, pos->wh_type, delist);
29492 +                       if (unlikely(err))
29493 +                               break;
29494 +               }
29495 +       }
29496 +
29497 +       free_page((unsigned long)o);
29498 +
29499 +out:
29500 +       AuTraceErr(err);
29501 +       return err;
29502 +#else
29503 +       return 0;
29504 +#endif
29505 +}
29506 +
29507 +static int au_do_read_vdir(struct fillvdir_arg *arg)
29508 +{
29509 +       int err;
29510 +       unsigned int rdhash;
29511 +       loff_t offset;
29512 +       aufs_bindex_t bend, bindex, bstart;
29513 +       unsigned char shwh;
29514 +       struct file *hf, *file;
29515 +       struct super_block *sb;
29516 +
29517 +       file = arg->file;
29518 +       sb = file->f_path.dentry->d_sb;
29519 +       SiMustAnyLock(sb);
29520 +
29521 +       rdhash = au_sbi(sb)->si_rdhash;
29522 +       if (!rdhash)
29523 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
29524 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
29525 +       if (unlikely(err))
29526 +               goto out;
29527 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
29528 +       if (unlikely(err))
29529 +               goto out_delist;
29530 +
29531 +       err = 0;
29532 +       arg->flags = 0;
29533 +       shwh = 0;
29534 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
29535 +               shwh = 1;
29536 +               au_fset_fillvdir(arg->flags, SHWH);
29537 +       }
29538 +       bstart = au_fbstart(file);
29539 +       bend = au_fbend_dir(file);
29540 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
29541 +               hf = au_hf_dir(file, bindex);
29542 +               if (!hf)
29543 +                       continue;
29544 +
29545 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
29546 +               err = offset;
29547 +               if (unlikely(offset))
29548 +                       break;
29549 +
29550 +               arg->bindex = bindex;
29551 +               au_fclr_fillvdir(arg->flags, WHABLE);
29552 +               if (shwh
29553 +                   || (bindex != bend
29554 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
29555 +                       au_fset_fillvdir(arg->flags, WHABLE);
29556 +               do {
29557 +                       arg->err = 0;
29558 +                       au_fclr_fillvdir(arg->flags, CALLED);
29559 +                       /* smp_mb(); */
29560 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
29561 +                       if (err >= 0)
29562 +                               err = arg->err;
29563 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
29564 +
29565 +               /*
29566 +                * dir_relax() may be good for concurrency, but aufs should not
29567 +                * use it since it will cause a lockdep problem.
29568 +                */
29569 +       }
29570 +
29571 +       if (!err && shwh)
29572 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
29573 +
29574 +       au_nhash_wh_free(&arg->whlist);
29575 +
29576 +out_delist:
29577 +       au_nhash_de_free(&arg->delist);
29578 +out:
29579 +       return err;
29580 +}
29581 +
29582 +static int read_vdir(struct file *file, int may_read)
29583 +{
29584 +       int err;
29585 +       unsigned long expire;
29586 +       unsigned char do_read;
29587 +       struct fillvdir_arg arg = {
29588 +               .ctx = {
29589 +                       .actor = fillvdir
29590 +               }
29591 +       };
29592 +       struct inode *inode;
29593 +       struct au_vdir *vdir, *allocated;
29594 +
29595 +       err = 0;
29596 +       inode = file_inode(file);
29597 +       IMustLock(inode);
29598 +       SiMustAnyLock(inode->i_sb);
29599 +
29600 +       allocated = NULL;
29601 +       do_read = 0;
29602 +       expire = au_sbi(inode->i_sb)->si_rdcache;
29603 +       vdir = au_ivdir(inode);
29604 +       if (!vdir) {
29605 +               do_read = 1;
29606 +               vdir = alloc_vdir(file);
29607 +               err = PTR_ERR(vdir);
29608 +               if (IS_ERR(vdir))
29609 +                       goto out;
29610 +               err = 0;
29611 +               allocated = vdir;
29612 +       } else if (may_read
29613 +                  && (inode->i_version != vdir->vd_version
29614 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
29615 +               do_read = 1;
29616 +               err = reinit_vdir(vdir);
29617 +               if (unlikely(err))
29618 +                       goto out;
29619 +       }
29620 +
29621 +       if (!do_read)
29622 +               return 0; /* success */
29623 +
29624 +       arg.file = file;
29625 +       arg.vdir = vdir;
29626 +       err = au_do_read_vdir(&arg);
29627 +       if (!err) {
29628 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
29629 +               vdir->vd_version = inode->i_version;
29630 +               vdir->vd_last.ul = 0;
29631 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29632 +               if (allocated)
29633 +                       au_set_ivdir(inode, allocated);
29634 +       } else if (allocated)
29635 +               au_vdir_free(allocated);
29636 +
29637 +out:
29638 +       return err;
29639 +}
29640 +
29641 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
29642 +{
29643 +       int err, rerr;
29644 +       unsigned long ul, n;
29645 +       const unsigned int deblk_sz = src->vd_deblk_sz;
29646 +
29647 +       AuDebugOn(tgt->vd_nblk != 1);
29648 +
29649 +       err = -ENOMEM;
29650 +       if (tgt->vd_nblk < src->vd_nblk) {
29651 +               unsigned char **p;
29652 +
29653 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
29654 +                            GFP_NOFS);
29655 +               if (unlikely(!p))
29656 +                       goto out;
29657 +               tgt->vd_deblk = p;
29658 +       }
29659 +
29660 +       if (tgt->vd_deblk_sz != deblk_sz) {
29661 +               unsigned char *p;
29662 +
29663 +               tgt->vd_deblk_sz = deblk_sz;
29664 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
29665 +               if (unlikely(!p))
29666 +                       goto out;
29667 +               tgt->vd_deblk[0] = p;
29668 +       }
29669 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
29670 +       tgt->vd_version = src->vd_version;
29671 +       tgt->vd_jiffy = src->vd_jiffy;
29672 +
29673 +       n = src->vd_nblk;
29674 +       for (ul = 1; ul < n; ul++) {
29675 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
29676 +                                           GFP_NOFS);
29677 +               if (unlikely(!tgt->vd_deblk[ul]))
29678 +                       goto out;
29679 +               tgt->vd_nblk++;
29680 +       }
29681 +       tgt->vd_nblk = n;
29682 +       tgt->vd_last.ul = tgt->vd_last.ul;
29683 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
29684 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
29685 +               - src->vd_deblk[src->vd_last.ul];
29686 +       /* smp_mb(); */
29687 +       return 0; /* success */
29688 +
29689 +out:
29690 +       rerr = reinit_vdir(tgt);
29691 +       BUG_ON(rerr);
29692 +       return err;
29693 +}
29694 +
29695 +int au_vdir_init(struct file *file)
29696 +{
29697 +       int err;
29698 +       struct inode *inode;
29699 +       struct au_vdir *vdir_cache, *allocated;
29700 +
29701 +       /* test file->f_pos here instead of ctx->pos */
29702 +       err = read_vdir(file, !file->f_pos);
29703 +       if (unlikely(err))
29704 +               goto out;
29705 +
29706 +       allocated = NULL;
29707 +       vdir_cache = au_fvdir_cache(file);
29708 +       if (!vdir_cache) {
29709 +               vdir_cache = alloc_vdir(file);
29710 +               err = PTR_ERR(vdir_cache);
29711 +               if (IS_ERR(vdir_cache))
29712 +                       goto out;
29713 +               allocated = vdir_cache;
29714 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
29715 +               /* test file->f_pos here instead of ctx->pos */
29716 +               err = reinit_vdir(vdir_cache);
29717 +               if (unlikely(err))
29718 +                       goto out;
29719 +       } else
29720 +               return 0; /* success */
29721 +
29722 +       inode = file_inode(file);
29723 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
29724 +       if (!err) {
29725 +               file->f_version = inode->i_version;
29726 +               if (allocated)
29727 +                       au_set_fvdir_cache(file, allocated);
29728 +       } else if (allocated)
29729 +               au_vdir_free(allocated);
29730 +
29731 +out:
29732 +       return err;
29733 +}
29734 +
29735 +static loff_t calc_offset(struct au_vdir *vdir)
29736 +{
29737 +       loff_t offset;
29738 +       union au_vdir_deblk_p p;
29739 +
29740 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
29741 +       offset = vdir->vd_last.p.deblk - p.deblk;
29742 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
29743 +       return offset;
29744 +}
29745 +
29746 +/* returns true or false */
29747 +static int seek_vdir(struct file *file, struct dir_context *ctx)
29748 +{
29749 +       int valid;
29750 +       unsigned int deblk_sz;
29751 +       unsigned long ul, n;
29752 +       loff_t offset;
29753 +       union au_vdir_deblk_p p, deblk_end;
29754 +       struct au_vdir *vdir_cache;
29755 +
29756 +       valid = 1;
29757 +       vdir_cache = au_fvdir_cache(file);
29758 +       offset = calc_offset(vdir_cache);
29759 +       AuDbg("offset %lld\n", offset);
29760 +       if (ctx->pos == offset)
29761 +               goto out;
29762 +
29763 +       vdir_cache->vd_last.ul = 0;
29764 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
29765 +       if (!ctx->pos)
29766 +               goto out;
29767 +
29768 +       valid = 0;
29769 +       deblk_sz = vdir_cache->vd_deblk_sz;
29770 +       ul = div64_u64(ctx->pos, deblk_sz);
29771 +       AuDbg("ul %lu\n", ul);
29772 +       if (ul >= vdir_cache->vd_nblk)
29773 +               goto out;
29774 +
29775 +       n = vdir_cache->vd_nblk;
29776 +       for (; ul < n; ul++) {
29777 +               p.deblk = vdir_cache->vd_deblk[ul];
29778 +               deblk_end.deblk = p.deblk + deblk_sz;
29779 +               offset = ul;
29780 +               offset *= deblk_sz;
29781 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
29782 +                       unsigned int l;
29783 +
29784 +                       l = calc_size(p.de->de_str.len);
29785 +                       offset += l;
29786 +                       p.deblk += l;
29787 +               }
29788 +               if (!is_deblk_end(&p, &deblk_end)) {
29789 +                       valid = 1;
29790 +                       vdir_cache->vd_last.ul = ul;
29791 +                       vdir_cache->vd_last.p = p;
29792 +                       break;
29793 +               }
29794 +       }
29795 +
29796 +out:
29797 +       /* smp_mb(); */
29798 +       AuTraceErr(!valid);
29799 +       return valid;
29800 +}
29801 +
29802 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
29803 +{
29804 +       unsigned int l, deblk_sz;
29805 +       union au_vdir_deblk_p deblk_end;
29806 +       struct au_vdir *vdir_cache;
29807 +       struct au_vdir_de *de;
29808 +
29809 +       vdir_cache = au_fvdir_cache(file);
29810 +       if (!seek_vdir(file, ctx))
29811 +               return 0;
29812 +
29813 +       deblk_sz = vdir_cache->vd_deblk_sz;
29814 +       while (1) {
29815 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
29816 +               deblk_end.deblk += deblk_sz;
29817 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
29818 +                       de = vdir_cache->vd_last.p.de;
29819 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
29820 +                             de->de_str.len, de->de_str.name, ctx->pos,
29821 +                             (unsigned long)de->de_ino, de->de_type);
29822 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
29823 +                                              de->de_str.len, de->de_ino,
29824 +                                              de->de_type))) {
29825 +                               /* todo: ignore the error caused by udba? */
29826 +                               /* return err; */
29827 +                               return 0;
29828 +                       }
29829 +
29830 +                       l = calc_size(de->de_str.len);
29831 +                       vdir_cache->vd_last.p.deblk += l;
29832 +                       ctx->pos += l;
29833 +               }
29834 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
29835 +                       vdir_cache->vd_last.ul++;
29836 +                       vdir_cache->vd_last.p.deblk
29837 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
29838 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
29839 +                       continue;
29840 +               }
29841 +               break;
29842 +       }
29843 +
29844 +       /* smp_mb(); */
29845 +       return 0;
29846 +}
29847 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
29848 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
29849 +++ linux/fs/aufs/vfsub.c       2015-06-22 08:29:23.710091096 +0200
29850 @@ -0,0 +1,846 @@
29851 +/*
29852 + * Copyright (C) 2005-2015 Junjiro R. Okajima
29853 + *
29854 + * This program, aufs is free software; you can redistribute it and/or modify
29855 + * it under the terms of the GNU General Public License as published by
29856 + * the Free Software Foundation; either version 2 of the License, or
29857 + * (at your option) any later version.
29858 + *
29859 + * This program is distributed in the hope that it will be useful,
29860 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29861 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29862 + * GNU General Public License for more details.
29863 + *
29864 + * You should have received a copy of the GNU General Public License
29865 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29866 + */
29867 +
29868 +/*
29869 + * sub-routines for VFS
29870 + */
29871 +
29872 +#include <linux/namei.h>
29873 +#include <linux/security.h>
29874 +#include <linux/splice.h>
29875 +#include "aufs.h"
29876 +
29877 +int vfsub_update_h_iattr(struct path *h_path, int *did)
29878 +{
29879 +       int err;
29880 +       struct kstat st;
29881 +       struct super_block *h_sb;
29882 +
29883 +       /* for remote fs, leave work for its getattr or d_revalidate */
29884 +       /* for bad i_attr fs, handle them in aufs_getattr() */
29885 +       /* still some fs may acquire i_mutex. we need to skip them */
29886 +       err = 0;
29887 +       if (!did)
29888 +               did = &err;
29889 +       h_sb = h_path->dentry->d_sb;
29890 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
29891 +       if (*did)
29892 +               err = vfs_getattr(h_path, &st);
29893 +
29894 +       return err;
29895 +}
29896 +
29897 +/* ---------------------------------------------------------------------- */
29898 +
29899 +struct file *vfsub_dentry_open(struct path *path, int flags)
29900 +{
29901 +       struct file *file;
29902 +
29903 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
29904 +                          current_cred());
29905 +       if (!IS_ERR_OR_NULL(file)
29906 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
29907 +               i_readcount_inc(path->dentry->d_inode);
29908 +
29909 +       return file;
29910 +}
29911 +
29912 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
29913 +{
29914 +       struct file *file;
29915 +
29916 +       lockdep_off();
29917 +       file = filp_open(path,
29918 +                        oflags /* | __FMODE_NONOTIFY */,
29919 +                        mode);
29920 +       lockdep_on();
29921 +       if (IS_ERR(file))
29922 +               goto out;
29923 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
29924 +
29925 +out:
29926 +       return file;
29927 +}
29928 +
29929 +/*
29930 + * Ideally this function should call VFS:do_last() in order to keep all its
29931 + * checkings. But it is very hard for aufs to regenerate several VFS internal
29932 + * structure such as nameidata. This is a second (or third) best approach.
29933 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
29934 + */
29935 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
29936 +                     struct vfsub_aopen_args *args, struct au_branch *br)
29937 +{
29938 +       int err;
29939 +       struct file *file = args->file;
29940 +       /* copied from linux/fs/namei.c:atomic_open() */
29941 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
29942 +
29943 +       IMustLock(dir);
29944 +       AuDebugOn(!dir->i_op->atomic_open);
29945 +
29946 +       err = au_br_test_oflag(args->open_flag, br);
29947 +       if (unlikely(err))
29948 +               goto out;
29949 +
29950 +       args->file->f_path.dentry = DENTRY_NOT_SET;
29951 +       args->file->f_path.mnt = au_br_mnt(br);
29952 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
29953 +                                    args->create_mode, args->opened);
29954 +       if (err >= 0) {
29955 +               /* some filesystems don't set FILE_CREATED while succeeded? */
29956 +               if (*args->opened & FILE_CREATED)
29957 +                       fsnotify_create(dir, dentry);
29958 +       } else
29959 +               goto out;
29960 +
29961 +
29962 +       if (!err) {
29963 +               /* todo: call VFS:may_open() here */
29964 +               err = open_check_o_direct(file);
29965 +               /* todo: ima_file_check() too? */
29966 +               if (!err && (args->open_flag & __FMODE_EXEC))
29967 +                       err = deny_write_access(file);
29968 +               if (unlikely(err))
29969 +                       /* note that the file is created and still opened */
29970 +                       goto out;
29971 +       }
29972 +
29973 +       atomic_inc(&br->br_count);
29974 +       fsnotify_open(file);
29975 +
29976 +out:
29977 +       return err;
29978 +}
29979 +
29980 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
29981 +{
29982 +       int err;
29983 +
29984 +       err = kern_path(name, flags, path);
29985 +       if (!err && path->dentry->d_inode)
29986 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
29987 +       return err;
29988 +}
29989 +
29990 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
29991 +                                   int len)
29992 +{
29993 +       struct path path = {
29994 +               .mnt = NULL
29995 +       };
29996 +
29997 +       /* VFS checks it too, but by WARN_ON_ONCE() */
29998 +       IMustLock(parent->d_inode);
29999 +
30000 +       path.dentry = lookup_one_len(name, parent, len);
30001 +       if (IS_ERR(path.dentry))
30002 +               goto out;
30003 +       if (path.dentry->d_inode)
30004 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30005 +
30006 +out:
30007 +       AuTraceErrPtr(path.dentry);
30008 +       return path.dentry;
30009 +}
30010 +
30011 +void vfsub_call_lkup_one(void *args)
30012 +{
30013 +       struct vfsub_lkup_one_args *a = args;
30014 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
30015 +}
30016 +
30017 +/* ---------------------------------------------------------------------- */
30018 +
30019 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30020 +                                struct dentry *d2, struct au_hinode *hdir2)
30021 +{
30022 +       struct dentry *d;
30023 +
30024 +       lockdep_off();
30025 +       d = lock_rename(d1, d2);
30026 +       lockdep_on();
30027 +       au_hn_suspend(hdir1);
30028 +       if (hdir1 != hdir2)
30029 +               au_hn_suspend(hdir2);
30030 +
30031 +       return d;
30032 +}
30033 +
30034 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30035 +                        struct dentry *d2, struct au_hinode *hdir2)
30036 +{
30037 +       au_hn_resume(hdir1);
30038 +       if (hdir1 != hdir2)
30039 +               au_hn_resume(hdir2);
30040 +       lockdep_off();
30041 +       unlock_rename(d1, d2);
30042 +       lockdep_on();
30043 +}
30044 +
30045 +/* ---------------------------------------------------------------------- */
30046 +
30047 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
30048 +{
30049 +       int err;
30050 +       struct dentry *d;
30051 +
30052 +       IMustLock(dir);
30053 +
30054 +       d = path->dentry;
30055 +       path->dentry = d->d_parent;
30056 +       err = security_path_mknod(path, d, mode, 0);
30057 +       path->dentry = d;
30058 +       if (unlikely(err))
30059 +               goto out;
30060 +
30061 +       lockdep_off();
30062 +       err = vfs_create(dir, path->dentry, mode, want_excl);
30063 +       lockdep_on();
30064 +       if (!err) {
30065 +               struct path tmp = *path;
30066 +               int did;
30067 +
30068 +               vfsub_update_h_iattr(&tmp, &did);
30069 +               if (did) {
30070 +                       tmp.dentry = path->dentry->d_parent;
30071 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30072 +               }
30073 +               /*ignore*/
30074 +       }
30075 +
30076 +out:
30077 +       return err;
30078 +}
30079 +
30080 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
30081 +{
30082 +       int err;
30083 +       struct dentry *d;
30084 +
30085 +       IMustLock(dir);
30086 +
30087 +       d = path->dentry;
30088 +       path->dentry = d->d_parent;
30089 +       err = security_path_symlink(path, d, symname);
30090 +       path->dentry = d;
30091 +       if (unlikely(err))
30092 +               goto out;
30093 +
30094 +       lockdep_off();
30095 +       err = vfs_symlink(dir, path->dentry, symname);
30096 +       lockdep_on();
30097 +       if (!err) {
30098 +               struct path tmp = *path;
30099 +               int did;
30100 +
30101 +               vfsub_update_h_iattr(&tmp, &did);
30102 +               if (did) {
30103 +                       tmp.dentry = path->dentry->d_parent;
30104 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30105 +               }
30106 +               /*ignore*/
30107 +       }
30108 +
30109 +out:
30110 +       return err;
30111 +}
30112 +
30113 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
30114 +{
30115 +       int err;
30116 +       struct dentry *d;
30117 +
30118 +       IMustLock(dir);
30119 +
30120 +       d = path->dentry;
30121 +       path->dentry = d->d_parent;
30122 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
30123 +       path->dentry = d;
30124 +       if (unlikely(err))
30125 +               goto out;
30126 +
30127 +       lockdep_off();
30128 +       err = vfs_mknod(dir, path->dentry, mode, dev);
30129 +       lockdep_on();
30130 +       if (!err) {
30131 +               struct path tmp = *path;
30132 +               int did;
30133 +
30134 +               vfsub_update_h_iattr(&tmp, &did);
30135 +               if (did) {
30136 +                       tmp.dentry = path->dentry->d_parent;
30137 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30138 +               }
30139 +               /*ignore*/
30140 +       }
30141 +
30142 +out:
30143 +       return err;
30144 +}
30145 +
30146 +static int au_test_nlink(struct inode *inode)
30147 +{
30148 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
30149 +
30150 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
30151 +           || inode->i_nlink < link_max)
30152 +               return 0;
30153 +       return -EMLINK;
30154 +}
30155 +
30156 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
30157 +              struct inode **delegated_inode)
30158 +{
30159 +       int err;
30160 +       struct dentry *d;
30161 +
30162 +       IMustLock(dir);
30163 +
30164 +       err = au_test_nlink(src_dentry->d_inode);
30165 +       if (unlikely(err))
30166 +               return err;
30167 +
30168 +       /* we don't call may_linkat() */
30169 +       d = path->dentry;
30170 +       path->dentry = d->d_parent;
30171 +       err = security_path_link(src_dentry, path, d);
30172 +       path->dentry = d;
30173 +       if (unlikely(err))
30174 +               goto out;
30175 +
30176 +       lockdep_off();
30177 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
30178 +       lockdep_on();
30179 +       if (!err) {
30180 +               struct path tmp = *path;
30181 +               int did;
30182 +
30183 +               /* fuse has different memory inode for the same inumber */
30184 +               vfsub_update_h_iattr(&tmp, &did);
30185 +               if (did) {
30186 +                       tmp.dentry = path->dentry->d_parent;
30187 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30188 +                       tmp.dentry = src_dentry;
30189 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30190 +               }
30191 +               /*ignore*/
30192 +       }
30193 +
30194 +out:
30195 +       return err;
30196 +}
30197 +
30198 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
30199 +                struct inode *dir, struct path *path,
30200 +                struct inode **delegated_inode)
30201 +{
30202 +       int err;
30203 +       struct path tmp = {
30204 +               .mnt    = path->mnt
30205 +       };
30206 +       struct dentry *d;
30207 +
30208 +       IMustLock(dir);
30209 +       IMustLock(src_dir);
30210 +
30211 +       d = path->dentry;
30212 +       path->dentry = d->d_parent;
30213 +       tmp.dentry = src_dentry->d_parent;
30214 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
30215 +       path->dentry = d;
30216 +       if (unlikely(err))
30217 +               goto out;
30218 +
30219 +       lockdep_off();
30220 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
30221 +                        delegated_inode, /*flags*/0);
30222 +       lockdep_on();
30223 +       if (!err) {
30224 +               int did;
30225 +
30226 +               tmp.dentry = d->d_parent;
30227 +               vfsub_update_h_iattr(&tmp, &did);
30228 +               if (did) {
30229 +                       tmp.dentry = src_dentry;
30230 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30231 +                       tmp.dentry = src_dentry->d_parent;
30232 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30233 +               }
30234 +               /*ignore*/
30235 +       }
30236 +
30237 +out:
30238 +       return err;
30239 +}
30240 +
30241 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
30242 +{
30243 +       int err;
30244 +       struct dentry *d;
30245 +
30246 +       IMustLock(dir);
30247 +
30248 +       d = path->dentry;
30249 +       path->dentry = d->d_parent;
30250 +       err = security_path_mkdir(path, d, mode);
30251 +       path->dentry = d;
30252 +       if (unlikely(err))
30253 +               goto out;
30254 +
30255 +       lockdep_off();
30256 +       err = vfs_mkdir(dir, path->dentry, mode);
30257 +       lockdep_on();
30258 +       if (!err) {
30259 +               struct path tmp = *path;
30260 +               int did;
30261 +
30262 +               vfsub_update_h_iattr(&tmp, &did);
30263 +               if (did) {
30264 +                       tmp.dentry = path->dentry->d_parent;
30265 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30266 +               }
30267 +               /*ignore*/
30268 +       }
30269 +
30270 +out:
30271 +       return err;
30272 +}
30273 +
30274 +int vfsub_rmdir(struct inode *dir, struct path *path)
30275 +{
30276 +       int err;
30277 +       struct dentry *d;
30278 +
30279 +       IMustLock(dir);
30280 +
30281 +       d = path->dentry;
30282 +       path->dentry = d->d_parent;
30283 +       err = security_path_rmdir(path, d);
30284 +       path->dentry = d;
30285 +       if (unlikely(err))
30286 +               goto out;
30287 +
30288 +       lockdep_off();
30289 +       err = vfs_rmdir(dir, path->dentry);
30290 +       lockdep_on();
30291 +       if (!err) {
30292 +               struct path tmp = {
30293 +                       .dentry = path->dentry->d_parent,
30294 +                       .mnt    = path->mnt
30295 +               };
30296 +
30297 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30298 +       }
30299 +
30300 +out:
30301 +       return err;
30302 +}
30303 +
30304 +/* ---------------------------------------------------------------------- */
30305 +
30306 +/* todo: support mmap_sem? */
30307 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
30308 +                    loff_t *ppos)
30309 +{
30310 +       ssize_t err;
30311 +
30312 +       lockdep_off();
30313 +       err = vfs_read(file, ubuf, count, ppos);
30314 +       lockdep_on();
30315 +       if (err >= 0)
30316 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30317 +       return err;
30318 +}
30319 +
30320 +/* todo: kernel_read()? */
30321 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
30322 +                    loff_t *ppos)
30323 +{
30324 +       ssize_t err;
30325 +       mm_segment_t oldfs;
30326 +       union {
30327 +               void *k;
30328 +               char __user *u;
30329 +       } buf;
30330 +
30331 +       buf.k = kbuf;
30332 +       oldfs = get_fs();
30333 +       set_fs(KERNEL_DS);
30334 +       err = vfsub_read_u(file, buf.u, count, ppos);
30335 +       set_fs(oldfs);
30336 +       return err;
30337 +}
30338 +
30339 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
30340 +                     loff_t *ppos)
30341 +{
30342 +       ssize_t err;
30343 +
30344 +       lockdep_off();
30345 +       err = vfs_write(file, ubuf, count, ppos);
30346 +       lockdep_on();
30347 +       if (err >= 0)
30348 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30349 +       return err;
30350 +}
30351 +
30352 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
30353 +{
30354 +       ssize_t err;
30355 +       mm_segment_t oldfs;
30356 +       union {
30357 +               void *k;
30358 +               const char __user *u;
30359 +       } buf;
30360 +
30361 +       buf.k = kbuf;
30362 +       oldfs = get_fs();
30363 +       set_fs(KERNEL_DS);
30364 +       err = vfsub_write_u(file, buf.u, count, ppos);
30365 +       set_fs(oldfs);
30366 +       return err;
30367 +}
30368 +
30369 +int vfsub_flush(struct file *file, fl_owner_t id)
30370 +{
30371 +       int err;
30372 +
30373 +       err = 0;
30374 +       if (file->f_op->flush) {
30375 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
30376 +                       err = file->f_op->flush(file, id);
30377 +               else {
30378 +                       lockdep_off();
30379 +                       err = file->f_op->flush(file, id);
30380 +                       lockdep_on();
30381 +               }
30382 +               if (!err)
30383 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
30384 +               /*ignore*/
30385 +       }
30386 +       return err;
30387 +}
30388 +
30389 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
30390 +{
30391 +       int err;
30392 +
30393 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
30394 +
30395 +       lockdep_off();
30396 +       err = iterate_dir(file, ctx);
30397 +       lockdep_on();
30398 +       if (err >= 0)
30399 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30400 +       return err;
30401 +}
30402 +
30403 +long vfsub_splice_to(struct file *in, loff_t *ppos,
30404 +                    struct pipe_inode_info *pipe, size_t len,
30405 +                    unsigned int flags)
30406 +{
30407 +       long err;
30408 +
30409 +       lockdep_off();
30410 +       err = do_splice_to(in, ppos, pipe, len, flags);
30411 +       lockdep_on();
30412 +       file_accessed(in);
30413 +       if (err >= 0)
30414 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
30415 +       return err;
30416 +}
30417 +
30418 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
30419 +                      loff_t *ppos, size_t len, unsigned int flags)
30420 +{
30421 +       long err;
30422 +
30423 +       lockdep_off();
30424 +       err = do_splice_from(pipe, out, ppos, len, flags);
30425 +       lockdep_on();
30426 +       if (err >= 0)
30427 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
30428 +       return err;
30429 +}
30430 +
30431 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
30432 +{
30433 +       int err;
30434 +
30435 +       /* file can be NULL */
30436 +       lockdep_off();
30437 +       err = vfs_fsync(file, datasync);
30438 +       lockdep_on();
30439 +       if (!err) {
30440 +               if (!path) {
30441 +                       AuDebugOn(!file);
30442 +                       path = &file->f_path;
30443 +               }
30444 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30445 +       }
30446 +       return err;
30447 +}
30448 +
30449 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
30450 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
30451 +               struct file *h_file)
30452 +{
30453 +       int err;
30454 +       struct inode *h_inode;
30455 +       struct super_block *h_sb;
30456 +
30457 +       if (!h_file) {
30458 +               err = vfsub_truncate(h_path, length);
30459 +               goto out;
30460 +       }
30461 +
30462 +       h_inode = h_path->dentry->d_inode;
30463 +       h_sb = h_inode->i_sb;
30464 +       lockdep_off();
30465 +       sb_start_write(h_sb);
30466 +       lockdep_on();
30467 +       err = locks_verify_truncate(h_inode, h_file, length);
30468 +       if (!err)
30469 +               err = security_path_truncate(h_path);
30470 +       if (!err) {
30471 +               lockdep_off();
30472 +               err = do_truncate(h_path->dentry, length, attr, h_file);
30473 +               lockdep_on();
30474 +       }
30475 +       lockdep_off();
30476 +       sb_end_write(h_sb);
30477 +       lockdep_on();
30478 +
30479 +out:
30480 +       return err;
30481 +}
30482 +
30483 +/* ---------------------------------------------------------------------- */
30484 +
30485 +struct au_vfsub_mkdir_args {
30486 +       int *errp;
30487 +       struct inode *dir;
30488 +       struct path *path;
30489 +       int mode;
30490 +};
30491 +
30492 +static void au_call_vfsub_mkdir(void *args)
30493 +{
30494 +       struct au_vfsub_mkdir_args *a = args;
30495 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
30496 +}
30497 +
30498 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
30499 +{
30500 +       int err, do_sio, wkq_err;
30501 +
30502 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30503 +       if (!do_sio) {
30504 +               lockdep_off();
30505 +               err = vfsub_mkdir(dir, path, mode);
30506 +               lockdep_on();
30507 +       } else {
30508 +               struct au_vfsub_mkdir_args args = {
30509 +                       .errp   = &err,
30510 +                       .dir    = dir,
30511 +                       .path   = path,
30512 +                       .mode   = mode
30513 +               };
30514 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
30515 +               if (unlikely(wkq_err))
30516 +                       err = wkq_err;
30517 +       }
30518 +
30519 +       return err;
30520 +}
30521 +
30522 +struct au_vfsub_rmdir_args {
30523 +       int *errp;
30524 +       struct inode *dir;
30525 +       struct path *path;
30526 +};
30527 +
30528 +static void au_call_vfsub_rmdir(void *args)
30529 +{
30530 +       struct au_vfsub_rmdir_args *a = args;
30531 +       *a->errp = vfsub_rmdir(a->dir, a->path);
30532 +}
30533 +
30534 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
30535 +{
30536 +       int err, do_sio, wkq_err;
30537 +
30538 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30539 +       if (!do_sio) {
30540 +               lockdep_off();
30541 +               err = vfsub_rmdir(dir, path);
30542 +               lockdep_on();
30543 +       } else {
30544 +               struct au_vfsub_rmdir_args args = {
30545 +                       .errp   = &err,
30546 +                       .dir    = dir,
30547 +                       .path   = path
30548 +               };
30549 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
30550 +               if (unlikely(wkq_err))
30551 +                       err = wkq_err;
30552 +       }
30553 +
30554 +       return err;
30555 +}
30556 +
30557 +/* ---------------------------------------------------------------------- */
30558 +
30559 +struct notify_change_args {
30560 +       int *errp;
30561 +       struct path *path;
30562 +       struct iattr *ia;
30563 +       struct inode **delegated_inode;
30564 +};
30565 +
30566 +static void call_notify_change(void *args)
30567 +{
30568 +       struct notify_change_args *a = args;
30569 +       struct inode *h_inode;
30570 +
30571 +       h_inode = a->path->dentry->d_inode;
30572 +       IMustLock(h_inode);
30573 +
30574 +       *a->errp = -EPERM;
30575 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
30576 +               lockdep_off();
30577 +               *a->errp = notify_change(a->path->dentry, a->ia,
30578 +                                        a->delegated_inode);
30579 +               lockdep_on();
30580 +               if (!*a->errp)
30581 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
30582 +       }
30583 +       AuTraceErr(*a->errp);
30584 +}
30585 +
30586 +int vfsub_notify_change(struct path *path, struct iattr *ia,
30587 +                       struct inode **delegated_inode)
30588 +{
30589 +       int err;
30590 +       struct notify_change_args args = {
30591 +               .errp                   = &err,
30592 +               .path                   = path,
30593 +               .ia                     = ia,
30594 +               .delegated_inode        = delegated_inode
30595 +       };
30596 +
30597 +       call_notify_change(&args);
30598 +
30599 +       return err;
30600 +}
30601 +
30602 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
30603 +                           struct inode **delegated_inode)
30604 +{
30605 +       int err, wkq_err;
30606 +       struct notify_change_args args = {
30607 +               .errp                   = &err,
30608 +               .path                   = path,
30609 +               .ia                     = ia,
30610 +               .delegated_inode        = delegated_inode
30611 +       };
30612 +
30613 +       wkq_err = au_wkq_wait(call_notify_change, &args);
30614 +       if (unlikely(wkq_err))
30615 +               err = wkq_err;
30616 +
30617 +       return err;
30618 +}
30619 +
30620 +/* ---------------------------------------------------------------------- */
30621 +
30622 +struct unlink_args {
30623 +       int *errp;
30624 +       struct inode *dir;
30625 +       struct path *path;
30626 +       struct inode **delegated_inode;
30627 +};
30628 +
30629 +static void call_unlink(void *args)
30630 +{
30631 +       struct unlink_args *a = args;
30632 +       struct dentry *d = a->path->dentry;
30633 +       struct inode *h_inode;
30634 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
30635 +                                     && au_dcount(d) == 1);
30636 +
30637 +       IMustLock(a->dir);
30638 +
30639 +       a->path->dentry = d->d_parent;
30640 +       *a->errp = security_path_unlink(a->path, d);
30641 +       a->path->dentry = d;
30642 +       if (unlikely(*a->errp))
30643 +               return;
30644 +
30645 +       if (!stop_sillyrename)
30646 +               dget(d);
30647 +       h_inode = d->d_inode;
30648 +       if (h_inode)
30649 +               ihold(h_inode);
30650 +
30651 +       lockdep_off();
30652 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
30653 +       lockdep_on();
30654 +       if (!*a->errp) {
30655 +               struct path tmp = {
30656 +                       .dentry = d->d_parent,
30657 +                       .mnt    = a->path->mnt
30658 +               };
30659 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30660 +       }
30661 +
30662 +       if (!stop_sillyrename)
30663 +               dput(d);
30664 +       if (h_inode)
30665 +               iput(h_inode);
30666 +
30667 +       AuTraceErr(*a->errp);
30668 +}
30669 +
30670 +/*
30671 + * @dir: must be locked.
30672 + * @dentry: target dentry.
30673 + */
30674 +int vfsub_unlink(struct inode *dir, struct path *path,
30675 +                struct inode **delegated_inode, int force)
30676 +{
30677 +       int err;
30678 +       struct unlink_args args = {
30679 +               .errp                   = &err,
30680 +               .dir                    = dir,
30681 +               .path                   = path,
30682 +               .delegated_inode        = delegated_inode
30683 +       };
30684 +
30685 +       if (!force)
30686 +               call_unlink(&args);
30687 +       else {
30688 +               int wkq_err;
30689 +
30690 +               wkq_err = au_wkq_wait(call_unlink, &args);
30691 +               if (unlikely(wkq_err))
30692 +                       err = wkq_err;
30693 +       }
30694 +
30695 +       return err;
30696 +}
30697 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
30698 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
30699 +++ linux/fs/aufs/vfsub.h       2015-06-22 08:29:23.710091096 +0200
30700 @@ -0,0 +1,286 @@
30701 +/*
30702 + * Copyright (C) 2005-2015 Junjiro R. Okajima
30703 + *
30704 + * This program, aufs is free software; you can redistribute it and/or modify
30705 + * it under the terms of the GNU General Public License as published by
30706 + * the Free Software Foundation; either version 2 of the License, or
30707 + * (at your option) any later version.
30708 + *
30709 + * This program is distributed in the hope that it will be useful,
30710 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30711 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30712 + * GNU General Public License for more details.
30713 + *
30714 + * You should have received a copy of the GNU General Public License
30715 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30716 + */
30717 +
30718 +/*
30719 + * sub-routines for VFS
30720 + */
30721 +
30722 +#ifndef __AUFS_VFSUB_H__
30723 +#define __AUFS_VFSUB_H__
30724 +
30725 +#ifdef __KERNEL__
30726 +
30727 +#include <linux/fs.h>
30728 +#include <linux/mount.h>
30729 +#include <linux/xattr.h>
30730 +#include "debug.h"
30731 +
30732 +/* copied from linux/fs/internal.h */
30733 +/* todo: BAD approach!! */
30734 +extern void __mnt_drop_write(struct vfsmount *);
30735 +extern spinlock_t inode_sb_list_lock;
30736 +extern int open_check_o_direct(struct file *f);
30737 +
30738 +/* ---------------------------------------------------------------------- */
30739 +
30740 +/* lock subclass for lower inode */
30741 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
30742 +/* reduce? gave up. */
30743 +enum {
30744 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
30745 +       AuLsc_I_PARENT,         /* lower inode, parent first */
30746 +       AuLsc_I_PARENT2,        /* copyup dirs */
30747 +       AuLsc_I_PARENT3,        /* copyup wh */
30748 +       AuLsc_I_CHILD,
30749 +       AuLsc_I_CHILD2,
30750 +       AuLsc_I_End
30751 +};
30752 +
30753 +/* to debug easier, do not make them inlined functions */
30754 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
30755 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
30756 +
30757 +/* ---------------------------------------------------------------------- */
30758 +
30759 +static inline void vfsub_drop_nlink(struct inode *inode)
30760 +{
30761 +       AuDebugOn(!inode->i_nlink);
30762 +       drop_nlink(inode);
30763 +}
30764 +
30765 +static inline void vfsub_dead_dir(struct inode *inode)
30766 +{
30767 +       AuDebugOn(!S_ISDIR(inode->i_mode));
30768 +       inode->i_flags |= S_DEAD;
30769 +       clear_nlink(inode);
30770 +}
30771 +
30772 +static inline int vfsub_native_ro(struct inode *inode)
30773 +{
30774 +       return (inode->i_sb->s_flags & MS_RDONLY)
30775 +               || IS_RDONLY(inode)
30776 +               /* || IS_APPEND(inode) */
30777 +               || IS_IMMUTABLE(inode);
30778 +}
30779 +
30780 +/* ---------------------------------------------------------------------- */
30781 +
30782 +int vfsub_update_h_iattr(struct path *h_path, int *did);
30783 +struct file *vfsub_dentry_open(struct path *path, int flags);
30784 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
30785 +struct vfsub_aopen_args {
30786 +       struct file     *file;
30787 +       unsigned int    open_flag;
30788 +       umode_t         create_mode;
30789 +       int             *opened;
30790 +};
30791 +struct au_branch;
30792 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
30793 +                     struct vfsub_aopen_args *args, struct au_branch *br);
30794 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
30795 +
30796 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30797 +                                   int len);
30798 +
30799 +struct vfsub_lkup_one_args {
30800 +       struct dentry **errp;
30801 +       struct qstr *name;
30802 +       struct dentry *parent;
30803 +};
30804 +
30805 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
30806 +                                           struct dentry *parent)
30807 +{
30808 +       return vfsub_lookup_one_len(name->name, parent, name->len);
30809 +}
30810 +
30811 +void vfsub_call_lkup_one(void *args);
30812 +
30813 +/* ---------------------------------------------------------------------- */
30814 +
30815 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
30816 +{
30817 +       int err;
30818 +
30819 +       lockdep_off();
30820 +       err = mnt_want_write(mnt);
30821 +       lockdep_on();
30822 +       return err;
30823 +}
30824 +
30825 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
30826 +{
30827 +       lockdep_off();
30828 +       mnt_drop_write(mnt);
30829 +       lockdep_on();
30830 +}
30831 +
30832 +#if 0 /* reserved */
30833 +static inline void vfsub_mnt_drop_write_file(struct file *file)
30834 +{
30835 +       lockdep_off();
30836 +       mnt_drop_write_file(file);
30837 +       lockdep_on();
30838 +}
30839 +#endif
30840 +
30841 +/* ---------------------------------------------------------------------- */
30842 +
30843 +struct au_hinode;
30844 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30845 +                                struct dentry *d2, struct au_hinode *hdir2);
30846 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30847 +                        struct dentry *d2, struct au_hinode *hdir2);
30848 +
30849 +int vfsub_create(struct inode *dir, struct path *path, int mode,
30850 +                bool want_excl);
30851 +int vfsub_symlink(struct inode *dir, struct path *path,
30852 +                 const char *symname);
30853 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
30854 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
30855 +              struct path *path, struct inode **delegated_inode);
30856 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
30857 +                struct inode *hdir, struct path *path,
30858 +                struct inode **delegated_inode);
30859 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
30860 +int vfsub_rmdir(struct inode *dir, struct path *path);
30861 +
30862 +/* ---------------------------------------------------------------------- */
30863 +
30864 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
30865 +                    loff_t *ppos);
30866 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
30867 +                       loff_t *ppos);
30868 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
30869 +                     loff_t *ppos);
30870 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
30871 +                     loff_t *ppos);
30872 +int vfsub_flush(struct file *file, fl_owner_t id);
30873 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
30874 +
30875 +static inline loff_t vfsub_f_size_read(struct file *file)
30876 +{
30877 +       return i_size_read(file_inode(file));
30878 +}
30879 +
30880 +static inline unsigned int vfsub_file_flags(struct file *file)
30881 +{
30882 +       unsigned int flags;
30883 +
30884 +       spin_lock(&file->f_lock);
30885 +       flags = file->f_flags;
30886 +       spin_unlock(&file->f_lock);
30887 +
30888 +       return flags;
30889 +}
30890 +
30891 +#if 0 /* reserved */
30892 +static inline void vfsub_file_accessed(struct file *h_file)
30893 +{
30894 +       file_accessed(h_file);
30895 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
30896 +}
30897 +#endif
30898 +
30899 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
30900 +                                    struct dentry *h_dentry)
30901 +{
30902 +       struct path h_path = {
30903 +               .dentry = h_dentry,
30904 +               .mnt    = h_mnt
30905 +       };
30906 +       touch_atime(&h_path);
30907 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
30908 +}
30909 +
30910 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
30911 +                                   int flags)
30912 +{
30913 +       return generic_update_time(h_inode, ts, flags);
30914 +       /* no vfsub_update_h_iattr() since we don't have struct path */
30915 +}
30916 +
30917 +long vfsub_splice_to(struct file *in, loff_t *ppos,
30918 +                    struct pipe_inode_info *pipe, size_t len,
30919 +                    unsigned int flags);
30920 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
30921 +                      loff_t *ppos, size_t len, unsigned int flags);
30922 +
30923 +static inline long vfsub_truncate(struct path *path, loff_t length)
30924 +{
30925 +       long err;
30926 +
30927 +       lockdep_off();
30928 +       err = vfs_truncate(path, length);
30929 +       lockdep_on();
30930 +       return err;
30931 +}
30932 +
30933 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
30934 +               struct file *h_file);
30935 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
30936 +
30937 +/* ---------------------------------------------------------------------- */
30938 +
30939 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
30940 +{
30941 +       loff_t err;
30942 +
30943 +       lockdep_off();
30944 +       err = vfs_llseek(file, offset, origin);
30945 +       lockdep_on();
30946 +       return err;
30947 +}
30948 +
30949 +/* ---------------------------------------------------------------------- */
30950 +
30951 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
30952 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
30953 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
30954 +                           struct inode **delegated_inode);
30955 +int vfsub_notify_change(struct path *path, struct iattr *ia,
30956 +                       struct inode **delegated_inode);
30957 +int vfsub_unlink(struct inode *dir, struct path *path,
30958 +                struct inode **delegated_inode, int force);
30959 +
30960 +/* ---------------------------------------------------------------------- */
30961 +
30962 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
30963 +                                const void *value, size_t size, int flags)
30964 +{
30965 +       int err;
30966 +
30967 +       lockdep_off();
30968 +       err = vfs_setxattr(dentry, name, value, size, flags);
30969 +       lockdep_on();
30970 +
30971 +       return err;
30972 +}
30973 +
30974 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
30975 +{
30976 +       int err;
30977 +
30978 +       lockdep_off();
30979 +       err = vfs_removexattr(dentry, name);
30980 +       lockdep_on();
30981 +
30982 +       return err;
30983 +}
30984 +
30985 +#endif /* __KERNEL__ */
30986 +#endif /* __AUFS_VFSUB_H__ */
30987 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
30988 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
30989 +++ linux/fs/aufs/wbr_policy.c  2015-06-22 08:27:37.887501948 +0200
30990 @@ -0,0 +1,765 @@
30991 +/*
30992 + * Copyright (C) 2005-2015 Junjiro R. Okajima
30993 + *
30994 + * This program, aufs is free software; you can redistribute it and/or modify
30995 + * it under the terms of the GNU General Public License as published by
30996 + * the Free Software Foundation; either version 2 of the License, or
30997 + * (at your option) any later version.
30998 + *
30999 + * This program is distributed in the hope that it will be useful,
31000 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31001 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31002 + * GNU General Public License for more details.
31003 + *
31004 + * You should have received a copy of the GNU General Public License
31005 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31006 + */
31007 +
31008 +/*
31009 + * policies for selecting one among multiple writable branches
31010 + */
31011 +
31012 +#include <linux/statfs.h>
31013 +#include "aufs.h"
31014 +
31015 +/* subset of cpup_attr() */
31016 +static noinline_for_stack
31017 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
31018 +{
31019 +       int err, sbits;
31020 +       struct iattr ia;
31021 +       struct inode *h_isrc;
31022 +
31023 +       h_isrc = h_src->d_inode;
31024 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
31025 +       ia.ia_mode = h_isrc->i_mode;
31026 +       ia.ia_uid = h_isrc->i_uid;
31027 +       ia.ia_gid = h_isrc->i_gid;
31028 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
31029 +       au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc->i_flags);
31030 +       /* no delegation since it is just created */
31031 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31032 +
31033 +       /* is this nfs only? */
31034 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
31035 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
31036 +               ia.ia_mode = h_isrc->i_mode;
31037 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31038 +       }
31039 +
31040 +       return err;
31041 +}
31042 +
31043 +#define AuCpdown_PARENT_OPQ    1
31044 +#define AuCpdown_WHED          (1 << 1)
31045 +#define AuCpdown_MADE_DIR      (1 << 2)
31046 +#define AuCpdown_DIROPQ                (1 << 3)
31047 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
31048 +#define au_fset_cpdown(flags, name) \
31049 +       do { (flags) |= AuCpdown_##name; } while (0)
31050 +#define au_fclr_cpdown(flags, name) \
31051 +       do { (flags) &= ~AuCpdown_##name; } while (0)
31052 +
31053 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
31054 +                            unsigned int *flags)
31055 +{
31056 +       int err;
31057 +       struct dentry *opq_dentry;
31058 +
31059 +       opq_dentry = au_diropq_create(dentry, bdst);
31060 +       err = PTR_ERR(opq_dentry);
31061 +       if (IS_ERR(opq_dentry))
31062 +               goto out;
31063 +       dput(opq_dentry);
31064 +       au_fset_cpdown(*flags, DIROPQ);
31065 +
31066 +out:
31067 +       return err;
31068 +}
31069 +
31070 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
31071 +                           struct inode *dir, aufs_bindex_t bdst)
31072 +{
31073 +       int err;
31074 +       struct path h_path;
31075 +       struct au_branch *br;
31076 +
31077 +       br = au_sbr(dentry->d_sb, bdst);
31078 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
31079 +       err = PTR_ERR(h_path.dentry);
31080 +       if (IS_ERR(h_path.dentry))
31081 +               goto out;
31082 +
31083 +       err = 0;
31084 +       if (h_path.dentry->d_inode) {
31085 +               h_path.mnt = au_br_mnt(br);
31086 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
31087 +                                         dentry);
31088 +       }
31089 +       dput(h_path.dentry);
31090 +
31091 +out:
31092 +       return err;
31093 +}
31094 +
31095 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
31096 +                        struct au_pin *pin,
31097 +                        struct dentry *h_parent, void *arg)
31098 +{
31099 +       int err, rerr;
31100 +       aufs_bindex_t bopq, bstart;
31101 +       struct path h_path;
31102 +       struct dentry *parent;
31103 +       struct inode *h_dir, *h_inode, *inode, *dir;
31104 +       unsigned int *flags = arg;
31105 +
31106 +       bstart = au_dbstart(dentry);
31107 +       /* dentry is di-locked */
31108 +       parent = dget_parent(dentry);
31109 +       dir = parent->d_inode;
31110 +       h_dir = h_parent->d_inode;
31111 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
31112 +       IMustLock(h_dir);
31113 +
31114 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
31115 +       if (unlikely(err < 0))
31116 +               goto out;
31117 +       h_path.dentry = au_h_dptr(dentry, bdst);
31118 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
31119 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
31120 +                             S_IRWXU | S_IRUGO | S_IXUGO);
31121 +       if (unlikely(err))
31122 +               goto out_put;
31123 +       au_fset_cpdown(*flags, MADE_DIR);
31124 +
31125 +       bopq = au_dbdiropq(dentry);
31126 +       au_fclr_cpdown(*flags, WHED);
31127 +       au_fclr_cpdown(*flags, DIROPQ);
31128 +       if (au_dbwh(dentry) == bdst)
31129 +               au_fset_cpdown(*flags, WHED);
31130 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
31131 +               au_fset_cpdown(*flags, PARENT_OPQ);
31132 +       h_inode = h_path.dentry->d_inode;
31133 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
31134 +       if (au_ftest_cpdown(*flags, WHED)) {
31135 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
31136 +               if (unlikely(err)) {
31137 +                       mutex_unlock(&h_inode->i_mutex);
31138 +                       goto out_dir;
31139 +               }
31140 +       }
31141 +
31142 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
31143 +       mutex_unlock(&h_inode->i_mutex);
31144 +       if (unlikely(err))
31145 +               goto out_opq;
31146 +
31147 +       if (au_ftest_cpdown(*flags, WHED)) {
31148 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
31149 +               if (unlikely(err))
31150 +                       goto out_opq;
31151 +       }
31152 +
31153 +       inode = dentry->d_inode;
31154 +       if (au_ibend(inode) < bdst)
31155 +               au_set_ibend(inode, bdst);
31156 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
31157 +                     au_hi_flags(inode, /*isdir*/1));
31158 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
31159 +       goto out; /* success */
31160 +
31161 +       /* revert */
31162 +out_opq:
31163 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
31164 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
31165 +               rerr = au_diropq_remove(dentry, bdst);
31166 +               mutex_unlock(&h_inode->i_mutex);
31167 +               if (unlikely(rerr)) {
31168 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
31169 +                               dentry, bdst, rerr);
31170 +                       err = -EIO;
31171 +                       goto out;
31172 +               }
31173 +       }
31174 +out_dir:
31175 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
31176 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
31177 +               if (unlikely(rerr)) {
31178 +                       AuIOErr("failed removing %pd b%d (%d)\n",
31179 +                               dentry, bdst, rerr);
31180 +                       err = -EIO;
31181 +               }
31182 +       }
31183 +out_put:
31184 +       au_set_h_dptr(dentry, bdst, NULL);
31185 +       if (au_dbend(dentry) == bdst)
31186 +               au_update_dbend(dentry);
31187 +out:
31188 +       dput(parent);
31189 +       return err;
31190 +}
31191 +
31192 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
31193 +{
31194 +       int err;
31195 +       unsigned int flags;
31196 +
31197 +       flags = 0;
31198 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
31199 +
31200 +       return err;
31201 +}
31202 +
31203 +/* ---------------------------------------------------------------------- */
31204 +
31205 +/* policies for create */
31206 +
31207 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
31208 +{
31209 +       int err, i, j, ndentry;
31210 +       aufs_bindex_t bopq;
31211 +       struct au_dcsub_pages dpages;
31212 +       struct au_dpage *dpage;
31213 +       struct dentry **dentries, *parent, *d;
31214 +
31215 +       err = au_dpages_init(&dpages, GFP_NOFS);
31216 +       if (unlikely(err))
31217 +               goto out;
31218 +       parent = dget_parent(dentry);
31219 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
31220 +       if (unlikely(err))
31221 +               goto out_free;
31222 +
31223 +       err = bindex;
31224 +       for (i = 0; i < dpages.ndpage; i++) {
31225 +               dpage = dpages.dpages + i;
31226 +               dentries = dpage->dentries;
31227 +               ndentry = dpage->ndentry;
31228 +               for (j = 0; j < ndentry; j++) {
31229 +                       d = dentries[j];
31230 +                       di_read_lock_parent2(d, !AuLock_IR);
31231 +                       bopq = au_dbdiropq(d);
31232 +                       di_read_unlock(d, !AuLock_IR);
31233 +                       if (bopq >= 0 && bopq < err)
31234 +                               err = bopq;
31235 +               }
31236 +       }
31237 +
31238 +out_free:
31239 +       dput(parent);
31240 +       au_dpages_free(&dpages);
31241 +out:
31242 +       return err;
31243 +}
31244 +
31245 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
31246 +{
31247 +       for (; bindex >= 0; bindex--)
31248 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
31249 +                       return bindex;
31250 +       return -EROFS;
31251 +}
31252 +
31253 +/* top down parent */
31254 +static int au_wbr_create_tdp(struct dentry *dentry,
31255 +                            unsigned int flags __maybe_unused)
31256 +{
31257 +       int err;
31258 +       aufs_bindex_t bstart, bindex;
31259 +       struct super_block *sb;
31260 +       struct dentry *parent, *h_parent;
31261 +
31262 +       sb = dentry->d_sb;
31263 +       bstart = au_dbstart(dentry);
31264 +       err = bstart;
31265 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
31266 +               goto out;
31267 +
31268 +       err = -EROFS;
31269 +       parent = dget_parent(dentry);
31270 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
31271 +               h_parent = au_h_dptr(parent, bindex);
31272 +               if (!h_parent || !h_parent->d_inode)
31273 +                       continue;
31274 +
31275 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
31276 +                       err = bindex;
31277 +                       break;
31278 +               }
31279 +       }
31280 +       dput(parent);
31281 +
31282 +       /* bottom up here */
31283 +       if (unlikely(err < 0)) {
31284 +               err = au_wbr_bu(sb, bstart - 1);
31285 +               if (err >= 0)
31286 +                       err = au_wbr_nonopq(dentry, err);
31287 +       }
31288 +
31289 +out:
31290 +       AuDbg("b%d\n", err);
31291 +       return err;
31292 +}
31293 +
31294 +/* ---------------------------------------------------------------------- */
31295 +
31296 +/* an exception for the policy other than tdp */
31297 +static int au_wbr_create_exp(struct dentry *dentry)
31298 +{
31299 +       int err;
31300 +       aufs_bindex_t bwh, bdiropq;
31301 +       struct dentry *parent;
31302 +
31303 +       err = -1;
31304 +       bwh = au_dbwh(dentry);
31305 +       parent = dget_parent(dentry);
31306 +       bdiropq = au_dbdiropq(parent);
31307 +       if (bwh >= 0) {
31308 +               if (bdiropq >= 0)
31309 +                       err = min(bdiropq, bwh);
31310 +               else
31311 +                       err = bwh;
31312 +               AuDbg("%d\n", err);
31313 +       } else if (bdiropq >= 0) {
31314 +               err = bdiropq;
31315 +               AuDbg("%d\n", err);
31316 +       }
31317 +       dput(parent);
31318 +
31319 +       if (err >= 0)
31320 +               err = au_wbr_nonopq(dentry, err);
31321 +
31322 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
31323 +               err = -1;
31324 +
31325 +       AuDbg("%d\n", err);
31326 +       return err;
31327 +}
31328 +
31329 +/* ---------------------------------------------------------------------- */
31330 +
31331 +/* round robin */
31332 +static int au_wbr_create_init_rr(struct super_block *sb)
31333 +{
31334 +       int err;
31335 +
31336 +       err = au_wbr_bu(sb, au_sbend(sb));
31337 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
31338 +       /* smp_mb(); */
31339 +
31340 +       AuDbg("b%d\n", err);
31341 +       return err;
31342 +}
31343 +
31344 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
31345 +{
31346 +       int err, nbr;
31347 +       unsigned int u;
31348 +       aufs_bindex_t bindex, bend;
31349 +       struct super_block *sb;
31350 +       atomic_t *next;
31351 +
31352 +       err = au_wbr_create_exp(dentry);
31353 +       if (err >= 0)
31354 +               goto out;
31355 +
31356 +       sb = dentry->d_sb;
31357 +       next = &au_sbi(sb)->si_wbr_rr_next;
31358 +       bend = au_sbend(sb);
31359 +       nbr = bend + 1;
31360 +       for (bindex = 0; bindex <= bend; bindex++) {
31361 +               if (!au_ftest_wbr(flags, DIR)) {
31362 +                       err = atomic_dec_return(next) + 1;
31363 +                       /* modulo for 0 is meaningless */
31364 +                       if (unlikely(!err))
31365 +                               err = atomic_dec_return(next) + 1;
31366 +               } else
31367 +                       err = atomic_read(next);
31368 +               AuDbg("%d\n", err);
31369 +               u = err;
31370 +               err = u % nbr;
31371 +               AuDbg("%d\n", err);
31372 +               if (!au_br_rdonly(au_sbr(sb, err)))
31373 +                       break;
31374 +               err = -EROFS;
31375 +       }
31376 +
31377 +       if (err >= 0)
31378 +               err = au_wbr_nonopq(dentry, err);
31379 +
31380 +out:
31381 +       AuDbg("%d\n", err);
31382 +       return err;
31383 +}
31384 +
31385 +/* ---------------------------------------------------------------------- */
31386 +
31387 +/* most free space */
31388 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
31389 +{
31390 +       struct super_block *sb;
31391 +       struct au_branch *br;
31392 +       struct au_wbr_mfs *mfs;
31393 +       struct dentry *h_parent;
31394 +       aufs_bindex_t bindex, bend;
31395 +       int err;
31396 +       unsigned long long b, bavail;
31397 +       struct path h_path;
31398 +       /* reduce the stack usage */
31399 +       struct kstatfs *st;
31400 +
31401 +       st = kmalloc(sizeof(*st), GFP_NOFS);
31402 +       if (unlikely(!st)) {
31403 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
31404 +               return;
31405 +       }
31406 +
31407 +       bavail = 0;
31408 +       sb = dentry->d_sb;
31409 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31410 +       MtxMustLock(&mfs->mfs_lock);
31411 +       mfs->mfs_bindex = -EROFS;
31412 +       mfs->mfsrr_bytes = 0;
31413 +       if (!parent) {
31414 +               bindex = 0;
31415 +               bend = au_sbend(sb);
31416 +       } else {
31417 +               bindex = au_dbstart(parent);
31418 +               bend = au_dbtaildir(parent);
31419 +       }
31420 +
31421 +       for (; bindex <= bend; bindex++) {
31422 +               if (parent) {
31423 +                       h_parent = au_h_dptr(parent, bindex);
31424 +                       if (!h_parent || !h_parent->d_inode)
31425 +                               continue;
31426 +               }
31427 +               br = au_sbr(sb, bindex);
31428 +               if (au_br_rdonly(br))
31429 +                       continue;
31430 +
31431 +               /* sb->s_root for NFS is unreliable */
31432 +               h_path.mnt = au_br_mnt(br);
31433 +               h_path.dentry = h_path.mnt->mnt_root;
31434 +               err = vfs_statfs(&h_path, st);
31435 +               if (unlikely(err)) {
31436 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
31437 +                       continue;
31438 +               }
31439 +
31440 +               /* when the available size is equal, select the lower one */
31441 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
31442 +                            || sizeof(b) < sizeof(st->f_bsize));
31443 +               b = st->f_bavail * st->f_bsize;
31444 +               br->br_wbr->wbr_bytes = b;
31445 +               if (b >= bavail) {
31446 +                       bavail = b;
31447 +                       mfs->mfs_bindex = bindex;
31448 +                       mfs->mfs_jiffy = jiffies;
31449 +               }
31450 +       }
31451 +
31452 +       mfs->mfsrr_bytes = bavail;
31453 +       AuDbg("b%d\n", mfs->mfs_bindex);
31454 +       kfree(st);
31455 +}
31456 +
31457 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
31458 +{
31459 +       int err;
31460 +       struct dentry *parent;
31461 +       struct super_block *sb;
31462 +       struct au_wbr_mfs *mfs;
31463 +
31464 +       err = au_wbr_create_exp(dentry);
31465 +       if (err >= 0)
31466 +               goto out;
31467 +
31468 +       sb = dentry->d_sb;
31469 +       parent = NULL;
31470 +       if (au_ftest_wbr(flags, PARENT))
31471 +               parent = dget_parent(dentry);
31472 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31473 +       mutex_lock(&mfs->mfs_lock);
31474 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
31475 +           || mfs->mfs_bindex < 0
31476 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
31477 +               au_mfs(dentry, parent);
31478 +       mutex_unlock(&mfs->mfs_lock);
31479 +       err = mfs->mfs_bindex;
31480 +       dput(parent);
31481 +
31482 +       if (err >= 0)
31483 +               err = au_wbr_nonopq(dentry, err);
31484 +
31485 +out:
31486 +       AuDbg("b%d\n", err);
31487 +       return err;
31488 +}
31489 +
31490 +static int au_wbr_create_init_mfs(struct super_block *sb)
31491 +{
31492 +       struct au_wbr_mfs *mfs;
31493 +
31494 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31495 +       mutex_init(&mfs->mfs_lock);
31496 +       mfs->mfs_jiffy = 0;
31497 +       mfs->mfs_bindex = -EROFS;
31498 +
31499 +       return 0;
31500 +}
31501 +
31502 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
31503 +{
31504 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
31505 +       return 0;
31506 +}
31507 +
31508 +/* ---------------------------------------------------------------------- */
31509 +
31510 +/* most free space and then round robin */
31511 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
31512 +{
31513 +       int err;
31514 +       struct au_wbr_mfs *mfs;
31515 +
31516 +       err = au_wbr_create_mfs(dentry, flags);
31517 +       if (err >= 0) {
31518 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
31519 +               mutex_lock(&mfs->mfs_lock);
31520 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
31521 +                       err = au_wbr_create_rr(dentry, flags);
31522 +               mutex_unlock(&mfs->mfs_lock);
31523 +       }
31524 +
31525 +       AuDbg("b%d\n", err);
31526 +       return err;
31527 +}
31528 +
31529 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
31530 +{
31531 +       int err;
31532 +
31533 +       au_wbr_create_init_mfs(sb); /* ignore */
31534 +       err = au_wbr_create_init_rr(sb);
31535 +
31536 +       return err;
31537 +}
31538 +
31539 +/* ---------------------------------------------------------------------- */
31540 +
31541 +/* top down parent and most free space */
31542 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
31543 +{
31544 +       int err, e2;
31545 +       unsigned long long b;
31546 +       aufs_bindex_t bindex, bstart, bend;
31547 +       struct super_block *sb;
31548 +       struct dentry *parent, *h_parent;
31549 +       struct au_branch *br;
31550 +
31551 +       err = au_wbr_create_tdp(dentry, flags);
31552 +       if (unlikely(err < 0))
31553 +               goto out;
31554 +       parent = dget_parent(dentry);
31555 +       bstart = au_dbstart(parent);
31556 +       bend = au_dbtaildir(parent);
31557 +       if (bstart == bend)
31558 +               goto out_parent; /* success */
31559 +
31560 +       e2 = au_wbr_create_mfs(dentry, flags);
31561 +       if (e2 < 0)
31562 +               goto out_parent; /* success */
31563 +
31564 +       /* when the available size is equal, select upper one */
31565 +       sb = dentry->d_sb;
31566 +       br = au_sbr(sb, err);
31567 +       b = br->br_wbr->wbr_bytes;
31568 +       AuDbg("b%d, %llu\n", err, b);
31569 +
31570 +       for (bindex = bstart; bindex <= bend; bindex++) {
31571 +               h_parent = au_h_dptr(parent, bindex);
31572 +               if (!h_parent || !h_parent->d_inode)
31573 +                       continue;
31574 +
31575 +               br = au_sbr(sb, bindex);
31576 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
31577 +                       b = br->br_wbr->wbr_bytes;
31578 +                       err = bindex;
31579 +                       AuDbg("b%d, %llu\n", err, b);
31580 +               }
31581 +       }
31582 +
31583 +       if (err >= 0)
31584 +               err = au_wbr_nonopq(dentry, err);
31585 +
31586 +out_parent:
31587 +       dput(parent);
31588 +out:
31589 +       AuDbg("b%d\n", err);
31590 +       return err;
31591 +}
31592 +
31593 +/* ---------------------------------------------------------------------- */
31594 +
31595 +/*
31596 + * - top down parent
31597 + * - most free space with parent
31598 + * - most free space round-robin regardless parent
31599 + */
31600 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
31601 +{
31602 +       int err;
31603 +       unsigned long long watermark;
31604 +       struct super_block *sb;
31605 +       struct au_branch *br;
31606 +       struct au_wbr_mfs *mfs;
31607 +
31608 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
31609 +       if (unlikely(err < 0))
31610 +               goto out;
31611 +
31612 +       sb = dentry->d_sb;
31613 +       br = au_sbr(sb, err);
31614 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31615 +       mutex_lock(&mfs->mfs_lock);
31616 +       watermark = mfs->mfsrr_watermark;
31617 +       mutex_unlock(&mfs->mfs_lock);
31618 +       if (br->br_wbr->wbr_bytes < watermark)
31619 +               /* regardless the parent dir */
31620 +               err = au_wbr_create_mfsrr(dentry, flags);
31621 +
31622 +out:
31623 +       AuDbg("b%d\n", err);
31624 +       return err;
31625 +}
31626 +
31627 +/* ---------------------------------------------------------------------- */
31628 +
31629 +/* policies for copyup */
31630 +
31631 +/* top down parent */
31632 +static int au_wbr_copyup_tdp(struct dentry *dentry)
31633 +{
31634 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
31635 +}
31636 +
31637 +/* bottom up parent */
31638 +static int au_wbr_copyup_bup(struct dentry *dentry)
31639 +{
31640 +       int err;
31641 +       aufs_bindex_t bindex, bstart;
31642 +       struct dentry *parent, *h_parent;
31643 +       struct super_block *sb;
31644 +
31645 +       err = -EROFS;
31646 +       sb = dentry->d_sb;
31647 +       parent = dget_parent(dentry);
31648 +       bstart = au_dbstart(parent);
31649 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
31650 +               h_parent = au_h_dptr(parent, bindex);
31651 +               if (!h_parent || !h_parent->d_inode)
31652 +                       continue;
31653 +
31654 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
31655 +                       err = bindex;
31656 +                       break;
31657 +               }
31658 +       }
31659 +       dput(parent);
31660 +
31661 +       /* bottom up here */
31662 +       if (unlikely(err < 0))
31663 +               err = au_wbr_bu(sb, bstart - 1);
31664 +
31665 +       AuDbg("b%d\n", err);
31666 +       return err;
31667 +}
31668 +
31669 +/* bottom up */
31670 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart)
31671 +{
31672 +       int err;
31673 +
31674 +       err = au_wbr_bu(dentry->d_sb, bstart);
31675 +       AuDbg("b%d\n", err);
31676 +       if (err > bstart)
31677 +               err = au_wbr_nonopq(dentry, err);
31678 +
31679 +       AuDbg("b%d\n", err);
31680 +       return err;
31681 +}
31682 +
31683 +static int au_wbr_copyup_bu(struct dentry *dentry)
31684 +{
31685 +       int err;
31686 +       aufs_bindex_t bstart;
31687 +
31688 +       bstart = au_dbstart(dentry);
31689 +       err = au_wbr_do_copyup_bu(dentry, bstart);
31690 +       return err;
31691 +}
31692 +
31693 +/* ---------------------------------------------------------------------- */
31694 +
31695 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
31696 +       [AuWbrCopyup_TDP] = {
31697 +               .copyup = au_wbr_copyup_tdp
31698 +       },
31699 +       [AuWbrCopyup_BUP] = {
31700 +               .copyup = au_wbr_copyup_bup
31701 +       },
31702 +       [AuWbrCopyup_BU] = {
31703 +               .copyup = au_wbr_copyup_bu
31704 +       }
31705 +};
31706 +
31707 +struct au_wbr_create_operations au_wbr_create_ops[] = {
31708 +       [AuWbrCreate_TDP] = {
31709 +               .create = au_wbr_create_tdp
31710 +       },
31711 +       [AuWbrCreate_RR] = {
31712 +               .create = au_wbr_create_rr,
31713 +               .init   = au_wbr_create_init_rr
31714 +       },
31715 +       [AuWbrCreate_MFS] = {
31716 +               .create = au_wbr_create_mfs,
31717 +               .init   = au_wbr_create_init_mfs,
31718 +               .fin    = au_wbr_create_fin_mfs
31719 +       },
31720 +       [AuWbrCreate_MFSV] = {
31721 +               .create = au_wbr_create_mfs,
31722 +               .init   = au_wbr_create_init_mfs,
31723 +               .fin    = au_wbr_create_fin_mfs
31724 +       },
31725 +       [AuWbrCreate_MFSRR] = {
31726 +               .create = au_wbr_create_mfsrr,
31727 +               .init   = au_wbr_create_init_mfsrr,
31728 +               .fin    = au_wbr_create_fin_mfs
31729 +       },
31730 +       [AuWbrCreate_MFSRRV] = {
31731 +               .create = au_wbr_create_mfsrr,
31732 +               .init   = au_wbr_create_init_mfsrr,
31733 +               .fin    = au_wbr_create_fin_mfs
31734 +       },
31735 +       [AuWbrCreate_PMFS] = {
31736 +               .create = au_wbr_create_pmfs,
31737 +               .init   = au_wbr_create_init_mfs,
31738 +               .fin    = au_wbr_create_fin_mfs
31739 +       },
31740 +       [AuWbrCreate_PMFSV] = {
31741 +               .create = au_wbr_create_pmfs,
31742 +               .init   = au_wbr_create_init_mfs,
31743 +               .fin    = au_wbr_create_fin_mfs
31744 +       },
31745 +       [AuWbrCreate_PMFSRR] = {
31746 +               .create = au_wbr_create_pmfsrr,
31747 +               .init   = au_wbr_create_init_mfsrr,
31748 +               .fin    = au_wbr_create_fin_mfs
31749 +       },
31750 +       [AuWbrCreate_PMFSRRV] = {
31751 +               .create = au_wbr_create_pmfsrr,
31752 +               .init   = au_wbr_create_init_mfsrr,
31753 +               .fin    = au_wbr_create_fin_mfs
31754 +       }
31755 +};
31756 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
31757 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
31758 +++ linux/fs/aufs/whout.c       2015-06-22 08:29:23.710091096 +0200
31759 @@ -0,0 +1,1064 @@
31760 +/*
31761 + * Copyright (C) 2005-2015 Junjiro R. Okajima
31762 + *
31763 + * This program, aufs is free software; you can redistribute it and/or modify
31764 + * it under the terms of the GNU General Public License as published by
31765 + * the Free Software Foundation; either version 2 of the License, or
31766 + * (at your option) any later version.
31767 + *
31768 + * This program is distributed in the hope that it will be useful,
31769 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31770 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31771 + * GNU General Public License for more details.
31772 + *
31773 + * You should have received a copy of the GNU General Public License
31774 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31775 + */
31776 +
31777 +/*
31778 + * whiteout for logical deletion and opaque directory
31779 + */
31780 +
31781 +#include "aufs.h"
31782 +
31783 +#define WH_MASK                        S_IRUGO
31784 +
31785 +/*
31786 + * If a directory contains this file, then it is opaque.  We start with the
31787 + * .wh. flag so that it is blocked by lookup.
31788 + */
31789 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
31790 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
31791 +
31792 +/*
31793 + * generate whiteout name, which is NOT terminated by NULL.
31794 + * @name: original d_name.name
31795 + * @len: original d_name.len
31796 + * @wh: whiteout qstr
31797 + * returns zero when succeeds, otherwise error.
31798 + * succeeded value as wh->name should be freed by kfree().
31799 + */
31800 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
31801 +{
31802 +       char *p;
31803 +
31804 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
31805 +               return -ENAMETOOLONG;
31806 +
31807 +       wh->len = name->len + AUFS_WH_PFX_LEN;
31808 +       p = kmalloc(wh->len, GFP_NOFS);
31809 +       wh->name = p;
31810 +       if (p) {
31811 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
31812 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
31813 +               /* smp_mb(); */
31814 +               return 0;
31815 +       }
31816 +       return -ENOMEM;
31817 +}
31818 +
31819 +/* ---------------------------------------------------------------------- */
31820 +
31821 +/*
31822 + * test if the @wh_name exists under @h_parent.
31823 + * @try_sio specifies the necessary of super-io.
31824 + */
31825 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
31826 +{
31827 +       int err;
31828 +       struct dentry *wh_dentry;
31829 +
31830 +       if (!try_sio)
31831 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
31832 +       else
31833 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
31834 +       err = PTR_ERR(wh_dentry);
31835 +       if (IS_ERR(wh_dentry)) {
31836 +               if (err == -ENAMETOOLONG)
31837 +                       err = 0;
31838 +               goto out;
31839 +       }
31840 +
31841 +       err = 0;
31842 +       if (!wh_dentry->d_inode)
31843 +               goto out_wh; /* success */
31844 +
31845 +       err = 1;
31846 +       if (d_is_reg(wh_dentry))
31847 +               goto out_wh; /* success */
31848 +
31849 +       err = -EIO;
31850 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
31851 +               wh_dentry, wh_dentry->d_inode->i_mode);
31852 +
31853 +out_wh:
31854 +       dput(wh_dentry);
31855 +out:
31856 +       return err;
31857 +}
31858 +
31859 +/*
31860 + * test if the @h_dentry sets opaque or not.
31861 + */
31862 +int au_diropq_test(struct dentry *h_dentry)
31863 +{
31864 +       int err;
31865 +       struct inode *h_dir;
31866 +
31867 +       h_dir = h_dentry->d_inode;
31868 +       err = au_wh_test(h_dentry, &diropq_name,
31869 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
31870 +       return err;
31871 +}
31872 +
31873 +/*
31874 + * returns a negative dentry whose name is unique and temporary.
31875 + */
31876 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
31877 +                            struct qstr *prefix)
31878 +{
31879 +       struct dentry *dentry;
31880 +       int i;
31881 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
31882 +               *name, *p;
31883 +       /* strict atomic_t is unnecessary here */
31884 +       static unsigned short cnt;
31885 +       struct qstr qs;
31886 +
31887 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
31888 +
31889 +       name = defname;
31890 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
31891 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
31892 +               dentry = ERR_PTR(-ENAMETOOLONG);
31893 +               if (unlikely(qs.len > NAME_MAX))
31894 +                       goto out;
31895 +               dentry = ERR_PTR(-ENOMEM);
31896 +               name = kmalloc(qs.len + 1, GFP_NOFS);
31897 +               if (unlikely(!name))
31898 +                       goto out;
31899 +       }
31900 +
31901 +       /* doubly whiteout-ed */
31902 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
31903 +       p = name + AUFS_WH_PFX_LEN * 2;
31904 +       memcpy(p, prefix->name, prefix->len);
31905 +       p += prefix->len;
31906 +       *p++ = '.';
31907 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
31908 +
31909 +       qs.name = name;
31910 +       for (i = 0; i < 3; i++) {
31911 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
31912 +               dentry = au_sio_lkup_one(&qs, h_parent);
31913 +               if (IS_ERR(dentry) || !dentry->d_inode)
31914 +                       goto out_name;
31915 +               dput(dentry);
31916 +       }
31917 +       /* pr_warn("could not get random name\n"); */
31918 +       dentry = ERR_PTR(-EEXIST);
31919 +       AuDbg("%.*s\n", AuLNPair(&qs));
31920 +       BUG();
31921 +
31922 +out_name:
31923 +       if (name != defname)
31924 +               kfree(name);
31925 +out:
31926 +       AuTraceErrPtr(dentry);
31927 +       return dentry;
31928 +}
31929 +
31930 +/*
31931 + * rename the @h_dentry on @br to the whiteouted temporary name.
31932 + */
31933 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
31934 +{
31935 +       int err;
31936 +       struct path h_path = {
31937 +               .mnt = au_br_mnt(br)
31938 +       };
31939 +       struct inode *h_dir, *delegated;
31940 +       struct dentry *h_parent;
31941 +
31942 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
31943 +       h_dir = h_parent->d_inode;
31944 +       IMustLock(h_dir);
31945 +
31946 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
31947 +       err = PTR_ERR(h_path.dentry);
31948 +       if (IS_ERR(h_path.dentry))
31949 +               goto out;
31950 +
31951 +       /* under the same dir, no need to lock_rename() */
31952 +       delegated = NULL;
31953 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
31954 +       AuTraceErr(err);
31955 +       if (unlikely(err == -EWOULDBLOCK)) {
31956 +               pr_warn("cannot retry for NFSv4 delegation"
31957 +                       " for an internal rename\n");
31958 +               iput(delegated);
31959 +       }
31960 +       dput(h_path.dentry);
31961 +
31962 +out:
31963 +       AuTraceErr(err);
31964 +       return err;
31965 +}
31966 +
31967 +/* ---------------------------------------------------------------------- */
31968 +/*
31969 + * functions for removing a whiteout
31970 + */
31971 +
31972 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
31973 +{
31974 +       int err, force;
31975 +       struct inode *delegated;
31976 +
31977 +       /*
31978 +        * forces superio when the dir has a sticky bit.
31979 +        * this may be a violation of unix fs semantics.
31980 +        */
31981 +       force = (h_dir->i_mode & S_ISVTX)
31982 +               && !uid_eq(current_fsuid(), h_path->dentry->d_inode->i_uid);
31983 +       delegated = NULL;
31984 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
31985 +       if (unlikely(err == -EWOULDBLOCK)) {
31986 +               pr_warn("cannot retry for NFSv4 delegation"
31987 +                       " for an internal unlink\n");
31988 +               iput(delegated);
31989 +       }
31990 +       return err;
31991 +}
31992 +
31993 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
31994 +                       struct dentry *dentry)
31995 +{
31996 +       int err;
31997 +
31998 +       err = do_unlink_wh(h_dir, h_path);
31999 +       if (!err && dentry)
32000 +               au_set_dbwh(dentry, -1);
32001 +
32002 +       return err;
32003 +}
32004 +
32005 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
32006 +                         struct au_branch *br)
32007 +{
32008 +       int err;
32009 +       struct path h_path = {
32010 +               .mnt = au_br_mnt(br)
32011 +       };
32012 +
32013 +       err = 0;
32014 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
32015 +       if (IS_ERR(h_path.dentry))
32016 +               err = PTR_ERR(h_path.dentry);
32017 +       else {
32018 +               if (h_path.dentry->d_inode
32019 +                   && d_is_reg(h_path.dentry))
32020 +                       err = do_unlink_wh(h_parent->d_inode, &h_path);
32021 +               dput(h_path.dentry);
32022 +       }
32023 +
32024 +       return err;
32025 +}
32026 +
32027 +/* ---------------------------------------------------------------------- */
32028 +/*
32029 + * initialize/clean whiteout for a branch
32030 + */
32031 +
32032 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
32033 +                       const int isdir)
32034 +{
32035 +       int err;
32036 +       struct inode *delegated;
32037 +
32038 +       if (!whpath->dentry->d_inode)
32039 +               return;
32040 +
32041 +       if (isdir)
32042 +               err = vfsub_rmdir(h_dir, whpath);
32043 +       else {
32044 +               delegated = NULL;
32045 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
32046 +               if (unlikely(err == -EWOULDBLOCK)) {
32047 +                       pr_warn("cannot retry for NFSv4 delegation"
32048 +                               " for an internal unlink\n");
32049 +                       iput(delegated);
32050 +               }
32051 +       }
32052 +       if (unlikely(err))
32053 +               pr_warn("failed removing %pd (%d), ignored.\n",
32054 +                       whpath->dentry, err);
32055 +}
32056 +
32057 +static int test_linkable(struct dentry *h_root)
32058 +{
32059 +       struct inode *h_dir = h_root->d_inode;
32060 +
32061 +       if (h_dir->i_op->link)
32062 +               return 0;
32063 +
32064 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
32065 +              h_root, au_sbtype(h_root->d_sb));
32066 +       return -ENOSYS;
32067 +}
32068 +
32069 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
32070 +static int au_whdir(struct inode *h_dir, struct path *path)
32071 +{
32072 +       int err;
32073 +
32074 +       err = -EEXIST;
32075 +       if (!path->dentry->d_inode) {
32076 +               int mode = S_IRWXU;
32077 +
32078 +               if (au_test_nfs(path->dentry->d_sb))
32079 +                       mode |= S_IXUGO;
32080 +               err = vfsub_mkdir(h_dir, path, mode);
32081 +       } else if (d_is_dir(path->dentry))
32082 +               err = 0;
32083 +       else
32084 +               pr_err("unknown %pd exists\n", path->dentry);
32085 +
32086 +       return err;
32087 +}
32088 +
32089 +struct au_wh_base {
32090 +       const struct qstr *name;
32091 +       struct dentry *dentry;
32092 +};
32093 +
32094 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
32095 +                         struct path *h_path)
32096 +{
32097 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32098 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32099 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32100 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32101 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32102 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32103 +}
32104 +
32105 +/*
32106 + * returns tri-state,
32107 + * minus: error, caller should print the message
32108 + * zero: succuess
32109 + * plus: error, caller should NOT print the message
32110 + */
32111 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
32112 +                               int do_plink, struct au_wh_base base[],
32113 +                               struct path *h_path)
32114 +{
32115 +       int err;
32116 +       struct inode *h_dir;
32117 +
32118 +       h_dir = h_root->d_inode;
32119 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32120 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32121 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32122 +       if (do_plink) {
32123 +               err = test_linkable(h_root);
32124 +               if (unlikely(err)) {
32125 +                       err = 1;
32126 +                       goto out;
32127 +               }
32128 +
32129 +               err = au_whdir(h_dir, h_path);
32130 +               if (unlikely(err))
32131 +                       goto out;
32132 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32133 +       } else
32134 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
32135 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32136 +       err = au_whdir(h_dir, h_path);
32137 +       if (unlikely(err))
32138 +               goto out;
32139 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32140 +
32141 +out:
32142 +       return err;
32143 +}
32144 +
32145 +/*
32146 + * for the moment, aufs supports the branch filesystem which does not support
32147 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
32148 + * copyup failed. finally, such filesystem will not be used as the writable
32149 + * branch.
32150 + *
32151 + * returns tri-state, see above.
32152 + */
32153 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
32154 +                        int do_plink, struct au_wh_base base[],
32155 +                        struct path *h_path)
32156 +{
32157 +       int err;
32158 +       struct inode *h_dir;
32159 +
32160 +       WbrWhMustWriteLock(wbr);
32161 +
32162 +       err = test_linkable(h_root);
32163 +       if (unlikely(err)) {
32164 +               err = 1;
32165 +               goto out;
32166 +       }
32167 +
32168 +       /*
32169 +        * todo: should this create be done in /sbin/mount.aufs helper?
32170 +        */
32171 +       err = -EEXIST;
32172 +       h_dir = h_root->d_inode;
32173 +       if (!base[AuBrWh_BASE].dentry->d_inode) {
32174 +               h_path->dentry = base[AuBrWh_BASE].dentry;
32175 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
32176 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
32177 +               err = 0;
32178 +       else
32179 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
32180 +       if (unlikely(err))
32181 +               goto out;
32182 +
32183 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32184 +       if (do_plink) {
32185 +               err = au_whdir(h_dir, h_path);
32186 +               if (unlikely(err))
32187 +                       goto out;
32188 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32189 +       } else
32190 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
32191 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
32192 +
32193 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32194 +       err = au_whdir(h_dir, h_path);
32195 +       if (unlikely(err))
32196 +               goto out;
32197 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32198 +
32199 +out:
32200 +       return err;
32201 +}
32202 +
32203 +/*
32204 + * initialize the whiteout base file/dir for @br.
32205 + */
32206 +int au_wh_init(struct au_branch *br, struct super_block *sb)
32207 +{
32208 +       int err, i;
32209 +       const unsigned char do_plink
32210 +               = !!au_opt_test(au_mntflags(sb), PLINK);
32211 +       struct inode *h_dir;
32212 +       struct path path = br->br_path;
32213 +       struct dentry *h_root = path.dentry;
32214 +       struct au_wbr *wbr = br->br_wbr;
32215 +       static const struct qstr base_name[] = {
32216 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
32217 +                                         sizeof(AUFS_BASE_NAME) - 1),
32218 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
32219 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
32220 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
32221 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
32222 +       };
32223 +       struct au_wh_base base[] = {
32224 +               [AuBrWh_BASE] = {
32225 +                       .name   = base_name + AuBrWh_BASE,
32226 +                       .dentry = NULL
32227 +               },
32228 +               [AuBrWh_PLINK] = {
32229 +                       .name   = base_name + AuBrWh_PLINK,
32230 +                       .dentry = NULL
32231 +               },
32232 +               [AuBrWh_ORPH] = {
32233 +                       .name   = base_name + AuBrWh_ORPH,
32234 +                       .dentry = NULL
32235 +               }
32236 +       };
32237 +
32238 +       if (wbr)
32239 +               WbrWhMustWriteLock(wbr);
32240 +
32241 +       for (i = 0; i < AuBrWh_Last; i++) {
32242 +               /* doubly whiteouted */
32243 +               struct dentry *d;
32244 +
32245 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
32246 +               err = PTR_ERR(d);
32247 +               if (IS_ERR(d))
32248 +                       goto out;
32249 +
32250 +               base[i].dentry = d;
32251 +               AuDebugOn(wbr
32252 +                         && wbr->wbr_wh[i]
32253 +                         && wbr->wbr_wh[i] != base[i].dentry);
32254 +       }
32255 +
32256 +       if (wbr)
32257 +               for (i = 0; i < AuBrWh_Last; i++) {
32258 +                       dput(wbr->wbr_wh[i]);
32259 +                       wbr->wbr_wh[i] = NULL;
32260 +               }
32261 +
32262 +       err = 0;
32263 +       if (!au_br_writable(br->br_perm)) {
32264 +               h_dir = h_root->d_inode;
32265 +               au_wh_init_ro(h_dir, base, &path);
32266 +       } else if (!au_br_wh_linkable(br->br_perm)) {
32267 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
32268 +               if (err > 0)
32269 +                       goto out;
32270 +               else if (err)
32271 +                       goto out_err;
32272 +       } else {
32273 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
32274 +               if (err > 0)
32275 +                       goto out;
32276 +               else if (err)
32277 +                       goto out_err;
32278 +       }
32279 +       goto out; /* success */
32280 +
32281 +out_err:
32282 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
32283 +              err, h_root, au_sbtype(h_root->d_sb));
32284 +out:
32285 +       for (i = 0; i < AuBrWh_Last; i++)
32286 +               dput(base[i].dentry);
32287 +       return err;
32288 +}
32289 +
32290 +/* ---------------------------------------------------------------------- */
32291 +/*
32292 + * whiteouts are all hard-linked usually.
32293 + * when its link count reaches a ceiling, we create a new whiteout base
32294 + * asynchronously.
32295 + */
32296 +
32297 +struct reinit_br_wh {
32298 +       struct super_block *sb;
32299 +       struct au_branch *br;
32300 +};
32301 +
32302 +static void reinit_br_wh(void *arg)
32303 +{
32304 +       int err;
32305 +       aufs_bindex_t bindex;
32306 +       struct path h_path;
32307 +       struct reinit_br_wh *a = arg;
32308 +       struct au_wbr *wbr;
32309 +       struct inode *dir, *delegated;
32310 +       struct dentry *h_root;
32311 +       struct au_hinode *hdir;
32312 +
32313 +       err = 0;
32314 +       wbr = a->br->br_wbr;
32315 +       /* big aufs lock */
32316 +       si_noflush_write_lock(a->sb);
32317 +       if (!au_br_writable(a->br->br_perm))
32318 +               goto out;
32319 +       bindex = au_br_index(a->sb, a->br->br_id);
32320 +       if (unlikely(bindex < 0))
32321 +               goto out;
32322 +
32323 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
32324 +       dir = a->sb->s_root->d_inode;
32325 +       hdir = au_hi(dir, bindex);
32326 +       h_root = au_h_dptr(a->sb->s_root, bindex);
32327 +       AuDebugOn(h_root != au_br_dentry(a->br));
32328 +
32329 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
32330 +       wbr_wh_write_lock(wbr);
32331 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
32332 +                         h_root, a->br);
32333 +       if (!err) {
32334 +               h_path.dentry = wbr->wbr_whbase;
32335 +               h_path.mnt = au_br_mnt(a->br);
32336 +               delegated = NULL;
32337 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
32338 +                                  /*force*/0);
32339 +               if (unlikely(err == -EWOULDBLOCK)) {
32340 +                       pr_warn("cannot retry for NFSv4 delegation"
32341 +                               " for an internal unlink\n");
32342 +                       iput(delegated);
32343 +               }
32344 +       } else {
32345 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
32346 +               err = 0;
32347 +       }
32348 +       dput(wbr->wbr_whbase);
32349 +       wbr->wbr_whbase = NULL;
32350 +       if (!err)
32351 +               err = au_wh_init(a->br, a->sb);
32352 +       wbr_wh_write_unlock(wbr);
32353 +       au_hn_imtx_unlock(hdir);
32354 +       di_read_unlock(a->sb->s_root, AuLock_IR);
32355 +       if (!err)
32356 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
32357 +
32358 +out:
32359 +       if (wbr)
32360 +               atomic_dec(&wbr->wbr_wh_running);
32361 +       atomic_dec(&a->br->br_count);
32362 +       si_write_unlock(a->sb);
32363 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
32364 +       kfree(arg);
32365 +       if (unlikely(err))
32366 +               AuIOErr("err %d\n", err);
32367 +}
32368 +
32369 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
32370 +{
32371 +       int do_dec, wkq_err;
32372 +       struct reinit_br_wh *arg;
32373 +
32374 +       do_dec = 1;
32375 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
32376 +               goto out;
32377 +
32378 +       /* ignore ENOMEM */
32379 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
32380 +       if (arg) {
32381 +               /*
32382 +                * dec(wh_running), kfree(arg) and dec(br_count)
32383 +                * in reinit function
32384 +                */
32385 +               arg->sb = sb;
32386 +               arg->br = br;
32387 +               atomic_inc(&br->br_count);
32388 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
32389 +               if (unlikely(wkq_err)) {
32390 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
32391 +                       atomic_dec(&br->br_count);
32392 +                       kfree(arg);
32393 +               }
32394 +               do_dec = 0;
32395 +       }
32396 +
32397 +out:
32398 +       if (do_dec)
32399 +               atomic_dec(&br->br_wbr->wbr_wh_running);
32400 +}
32401 +
32402 +/* ---------------------------------------------------------------------- */
32403 +
32404 +/*
32405 + * create the whiteout @wh.
32406 + */
32407 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
32408 +                            struct dentry *wh)
32409 +{
32410 +       int err;
32411 +       struct path h_path = {
32412 +               .dentry = wh
32413 +       };
32414 +       struct au_branch *br;
32415 +       struct au_wbr *wbr;
32416 +       struct dentry *h_parent;
32417 +       struct inode *h_dir, *delegated;
32418 +
32419 +       h_parent = wh->d_parent; /* dir inode is locked */
32420 +       h_dir = h_parent->d_inode;
32421 +       IMustLock(h_dir);
32422 +
32423 +       br = au_sbr(sb, bindex);
32424 +       h_path.mnt = au_br_mnt(br);
32425 +       wbr = br->br_wbr;
32426 +       wbr_wh_read_lock(wbr);
32427 +       if (wbr->wbr_whbase) {
32428 +               delegated = NULL;
32429 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
32430 +               if (unlikely(err == -EWOULDBLOCK)) {
32431 +                       pr_warn("cannot retry for NFSv4 delegation"
32432 +                               " for an internal link\n");
32433 +                       iput(delegated);
32434 +               }
32435 +               if (!err || err != -EMLINK)
32436 +                       goto out;
32437 +
32438 +               /* link count full. re-initialize br_whbase. */
32439 +               kick_reinit_br_wh(sb, br);
32440 +       }
32441 +
32442 +       /* return this error in this context */
32443 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
32444 +       if (!err)
32445 +               au_fhsm_wrote(sb, bindex, /*force*/0);
32446 +
32447 +out:
32448 +       wbr_wh_read_unlock(wbr);
32449 +       return err;
32450 +}
32451 +
32452 +/* ---------------------------------------------------------------------- */
32453 +
32454 +/*
32455 + * create or remove the diropq.
32456 + */
32457 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
32458 +                               unsigned int flags)
32459 +{
32460 +       struct dentry *opq_dentry, *h_dentry;
32461 +       struct super_block *sb;
32462 +       struct au_branch *br;
32463 +       int err;
32464 +
32465 +       sb = dentry->d_sb;
32466 +       br = au_sbr(sb, bindex);
32467 +       h_dentry = au_h_dptr(dentry, bindex);
32468 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
32469 +       if (IS_ERR(opq_dentry))
32470 +               goto out;
32471 +
32472 +       if (au_ftest_diropq(flags, CREATE)) {
32473 +               err = link_or_create_wh(sb, bindex, opq_dentry);
32474 +               if (!err) {
32475 +                       au_set_dbdiropq(dentry, bindex);
32476 +                       goto out; /* success */
32477 +               }
32478 +       } else {
32479 +               struct path tmp = {
32480 +                       .dentry = opq_dentry,
32481 +                       .mnt    = au_br_mnt(br)
32482 +               };
32483 +               err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
32484 +               if (!err)
32485 +                       au_set_dbdiropq(dentry, -1);
32486 +       }
32487 +       dput(opq_dentry);
32488 +       opq_dentry = ERR_PTR(err);
32489 +
32490 +out:
32491 +       return opq_dentry;
32492 +}
32493 +
32494 +struct do_diropq_args {
32495 +       struct dentry **errp;
32496 +       struct dentry *dentry;
32497 +       aufs_bindex_t bindex;
32498 +       unsigned int flags;
32499 +};
32500 +
32501 +static void call_do_diropq(void *args)
32502 +{
32503 +       struct do_diropq_args *a = args;
32504 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
32505 +}
32506 +
32507 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
32508 +                            unsigned int flags)
32509 +{
32510 +       struct dentry *diropq, *h_dentry;
32511 +
32512 +       h_dentry = au_h_dptr(dentry, bindex);
32513 +       if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
32514 +               diropq = do_diropq(dentry, bindex, flags);
32515 +       else {
32516 +               int wkq_err;
32517 +               struct do_diropq_args args = {
32518 +                       .errp           = &diropq,
32519 +                       .dentry         = dentry,
32520 +                       .bindex         = bindex,
32521 +                       .flags          = flags
32522 +               };
32523 +
32524 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
32525 +               if (unlikely(wkq_err))
32526 +                       diropq = ERR_PTR(wkq_err);
32527 +       }
32528 +
32529 +       return diropq;
32530 +}
32531 +
32532 +/* ---------------------------------------------------------------------- */
32533 +
32534 +/*
32535 + * lookup whiteout dentry.
32536 + * @h_parent: lower parent dentry which must exist and be locked
32537 + * @base_name: name of dentry which will be whiteouted
32538 + * returns dentry for whiteout.
32539 + */
32540 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
32541 +                         struct au_branch *br)
32542 +{
32543 +       int err;
32544 +       struct qstr wh_name;
32545 +       struct dentry *wh_dentry;
32546 +
32547 +       err = au_wh_name_alloc(&wh_name, base_name);
32548 +       wh_dentry = ERR_PTR(err);
32549 +       if (!err) {
32550 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
32551 +               kfree(wh_name.name);
32552 +       }
32553 +       return wh_dentry;
32554 +}
32555 +
32556 +/*
32557 + * link/create a whiteout for @dentry on @bindex.
32558 + */
32559 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
32560 +                           struct dentry *h_parent)
32561 +{
32562 +       struct dentry *wh_dentry;
32563 +       struct super_block *sb;
32564 +       int err;
32565 +
32566 +       sb = dentry->d_sb;
32567 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
32568 +       if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
32569 +               err = link_or_create_wh(sb, bindex, wh_dentry);
32570 +               if (!err) {
32571 +                       au_set_dbwh(dentry, bindex);
32572 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
32573 +               } else {
32574 +                       dput(wh_dentry);
32575 +                       wh_dentry = ERR_PTR(err);
32576 +               }
32577 +       }
32578 +
32579 +       return wh_dentry;
32580 +}
32581 +
32582 +/* ---------------------------------------------------------------------- */
32583 +
32584 +/* Delete all whiteouts in this directory on branch bindex. */
32585 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
32586 +                          aufs_bindex_t bindex, struct au_branch *br)
32587 +{
32588 +       int err;
32589 +       unsigned long ul, n;
32590 +       struct qstr wh_name;
32591 +       char *p;
32592 +       struct hlist_head *head;
32593 +       struct au_vdir_wh *pos;
32594 +       struct au_vdir_destr *str;
32595 +
32596 +       err = -ENOMEM;
32597 +       p = (void *)__get_free_page(GFP_NOFS);
32598 +       wh_name.name = p;
32599 +       if (unlikely(!wh_name.name))
32600 +               goto out;
32601 +
32602 +       err = 0;
32603 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32604 +       p += AUFS_WH_PFX_LEN;
32605 +       n = whlist->nh_num;
32606 +       head = whlist->nh_head;
32607 +       for (ul = 0; !err && ul < n; ul++, head++) {
32608 +               hlist_for_each_entry(pos, head, wh_hash) {
32609 +                       if (pos->wh_bindex != bindex)
32610 +                               continue;
32611 +
32612 +                       str = &pos->wh_str;
32613 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
32614 +                               memcpy(p, str->name, str->len);
32615 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
32616 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
32617 +                               if (!err)
32618 +                                       continue;
32619 +                               break;
32620 +                       }
32621 +                       AuIOErr("whiteout name too long %.*s\n",
32622 +                               str->len, str->name);
32623 +                       err = -EIO;
32624 +                       break;
32625 +               }
32626 +       }
32627 +       free_page((unsigned long)wh_name.name);
32628 +
32629 +out:
32630 +       return err;
32631 +}
32632 +
32633 +struct del_wh_children_args {
32634 +       int *errp;
32635 +       struct dentry *h_dentry;
32636 +       struct au_nhash *whlist;
32637 +       aufs_bindex_t bindex;
32638 +       struct au_branch *br;
32639 +};
32640 +
32641 +static void call_del_wh_children(void *args)
32642 +{
32643 +       struct del_wh_children_args *a = args;
32644 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
32645 +}
32646 +
32647 +/* ---------------------------------------------------------------------- */
32648 +
32649 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
32650 +{
32651 +       struct au_whtmp_rmdir *whtmp;
32652 +       int err;
32653 +       unsigned int rdhash;
32654 +
32655 +       SiMustAnyLock(sb);
32656 +
32657 +       whtmp = kmalloc(sizeof(*whtmp), gfp);
32658 +       if (unlikely(!whtmp)) {
32659 +               whtmp = ERR_PTR(-ENOMEM);
32660 +               goto out;
32661 +       }
32662 +
32663 +       whtmp->dir = NULL;
32664 +       whtmp->br = NULL;
32665 +       whtmp->wh_dentry = NULL;
32666 +       /* no estimation for dir size */
32667 +       rdhash = au_sbi(sb)->si_rdhash;
32668 +       if (!rdhash)
32669 +               rdhash = AUFS_RDHASH_DEF;
32670 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
32671 +       if (unlikely(err)) {
32672 +               kfree(whtmp);
32673 +               whtmp = ERR_PTR(err);
32674 +       }
32675 +
32676 +out:
32677 +       return whtmp;
32678 +}
32679 +
32680 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
32681 +{
32682 +       if (whtmp->br)
32683 +               atomic_dec(&whtmp->br->br_count);
32684 +       dput(whtmp->wh_dentry);
32685 +       iput(whtmp->dir);
32686 +       au_nhash_wh_free(&whtmp->whlist);
32687 +       kfree(whtmp);
32688 +}
32689 +
32690 +/*
32691 + * rmdir the whiteouted temporary named dir @h_dentry.
32692 + * @whlist: whiteouted children.
32693 + */
32694 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
32695 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
32696 +{
32697 +       int err;
32698 +       unsigned int h_nlink;
32699 +       struct path h_tmp;
32700 +       struct inode *wh_inode, *h_dir;
32701 +       struct au_branch *br;
32702 +
32703 +       h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
32704 +       IMustLock(h_dir);
32705 +
32706 +       br = au_sbr(dir->i_sb, bindex);
32707 +       wh_inode = wh_dentry->d_inode;
32708 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
32709 +
32710 +       /*
32711 +        * someone else might change some whiteouts while we were sleeping.
32712 +        * it means this whlist may have an obsoleted entry.
32713 +        */
32714 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
32715 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
32716 +       else {
32717 +               int wkq_err;
32718 +               struct del_wh_children_args args = {
32719 +                       .errp           = &err,
32720 +                       .h_dentry       = wh_dentry,
32721 +                       .whlist         = whlist,
32722 +                       .bindex         = bindex,
32723 +                       .br             = br
32724 +               };
32725 +
32726 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
32727 +               if (unlikely(wkq_err))
32728 +                       err = wkq_err;
32729 +       }
32730 +       mutex_unlock(&wh_inode->i_mutex);
32731 +
32732 +       if (!err) {
32733 +               h_tmp.dentry = wh_dentry;
32734 +               h_tmp.mnt = au_br_mnt(br);
32735 +               h_nlink = h_dir->i_nlink;
32736 +               err = vfsub_rmdir(h_dir, &h_tmp);
32737 +               /* some fs doesn't change the parent nlink in some cases */
32738 +               h_nlink -= h_dir->i_nlink;
32739 +       }
32740 +
32741 +       if (!err) {
32742 +               if (au_ibstart(dir) == bindex) {
32743 +                       /* todo: dir->i_mutex is necessary */
32744 +                       au_cpup_attr_timesizes(dir);
32745 +                       if (h_nlink)
32746 +                               vfsub_drop_nlink(dir);
32747 +               }
32748 +               return 0; /* success */
32749 +       }
32750 +
32751 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
32752 +       return err;
32753 +}
32754 +
32755 +static void call_rmdir_whtmp(void *args)
32756 +{
32757 +       int err;
32758 +       aufs_bindex_t bindex;
32759 +       struct au_whtmp_rmdir *a = args;
32760 +       struct super_block *sb;
32761 +       struct dentry *h_parent;
32762 +       struct inode *h_dir;
32763 +       struct au_hinode *hdir;
32764 +
32765 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
32766 +       /* mutex_lock(&a->dir->i_mutex); */
32767 +       err = -EROFS;
32768 +       sb = a->dir->i_sb;
32769 +       si_read_lock(sb, !AuLock_FLUSH);
32770 +       if (!au_br_writable(a->br->br_perm))
32771 +               goto out;
32772 +       bindex = au_br_index(sb, a->br->br_id);
32773 +       if (unlikely(bindex < 0))
32774 +               goto out;
32775 +
32776 +       err = -EIO;
32777 +       ii_write_lock_parent(a->dir);
32778 +       h_parent = dget_parent(a->wh_dentry);
32779 +       h_dir = h_parent->d_inode;
32780 +       hdir = au_hi(a->dir, bindex);
32781 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
32782 +       if (unlikely(err))
32783 +               goto out_mnt;
32784 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
32785 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
32786 +                         a->br);
32787 +       if (!err)
32788 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
32789 +       au_hn_imtx_unlock(hdir);
32790 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
32791 +
32792 +out_mnt:
32793 +       dput(h_parent);
32794 +       ii_write_unlock(a->dir);
32795 +out:
32796 +       /* mutex_unlock(&a->dir->i_mutex); */
32797 +       au_whtmp_rmdir_free(a);
32798 +       si_read_unlock(sb);
32799 +       au_nwt_done(&au_sbi(sb)->si_nowait);
32800 +       if (unlikely(err))
32801 +               AuIOErr("err %d\n", err);
32802 +}
32803 +
32804 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
32805 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
32806 +{
32807 +       int wkq_err;
32808 +       struct super_block *sb;
32809 +
32810 +       IMustLock(dir);
32811 +
32812 +       /* all post-process will be done in do_rmdir_whtmp(). */
32813 +       sb = dir->i_sb;
32814 +       args->dir = au_igrab(dir);
32815 +       args->br = au_sbr(sb, bindex);
32816 +       atomic_inc(&args->br->br_count);
32817 +       args->wh_dentry = dget(wh_dentry);
32818 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
32819 +       if (unlikely(wkq_err)) {
32820 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
32821 +               au_whtmp_rmdir_free(args);
32822 +       }
32823 +}
32824 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
32825 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
32826 +++ linux/fs/aufs/whout.h       2015-06-22 08:27:37.890835363 +0200
32827 @@ -0,0 +1,85 @@
32828 +/*
32829 + * Copyright (C) 2005-2015 Junjiro R. Okajima
32830 + *
32831 + * This program, aufs is free software; you can redistribute it and/or modify
32832 + * it under the terms of the GNU General Public License as published by
32833 + * the Free Software Foundation; either version 2 of the License, or
32834 + * (at your option) any later version.
32835 + *
32836 + * This program is distributed in the hope that it will be useful,
32837 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32838 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32839 + * GNU General Public License for more details.
32840 + *
32841 + * You should have received a copy of the GNU General Public License
32842 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32843 + */
32844 +
32845 +/*
32846 + * whiteout for logical deletion and opaque directory
32847 + */
32848 +
32849 +#ifndef __AUFS_WHOUT_H__
32850 +#define __AUFS_WHOUT_H__
32851 +
32852 +#ifdef __KERNEL__
32853 +
32854 +#include "dir.h"
32855 +
32856 +/* whout.c */
32857 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
32858 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
32859 +int au_diropq_test(struct dentry *h_dentry);
32860 +struct au_branch;
32861 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
32862 +                            struct qstr *prefix);
32863 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
32864 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32865 +                       struct dentry *dentry);
32866 +int au_wh_init(struct au_branch *br, struct super_block *sb);
32867 +
32868 +/* diropq flags */
32869 +#define AuDiropq_CREATE        1
32870 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
32871 +#define au_fset_diropq(flags, name) \
32872 +       do { (flags) |= AuDiropq_##name; } while (0)
32873 +#define au_fclr_diropq(flags, name) \
32874 +       do { (flags) &= ~AuDiropq_##name; } while (0)
32875 +
32876 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
32877 +                            unsigned int flags);
32878 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
32879 +                         struct au_branch *br);
32880 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
32881 +                           struct dentry *h_parent);
32882 +
32883 +/* real rmdir for the whiteout-ed dir */
32884 +struct au_whtmp_rmdir {
32885 +       struct inode *dir;
32886 +       struct au_branch *br;
32887 +       struct dentry *wh_dentry;
32888 +       struct au_nhash whlist;
32889 +};
32890 +
32891 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
32892 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
32893 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
32894 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
32895 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
32896 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
32897 +
32898 +/* ---------------------------------------------------------------------- */
32899 +
32900 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
32901 +                                             aufs_bindex_t bindex)
32902 +{
32903 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
32904 +}
32905 +
32906 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
32907 +{
32908 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
32909 +}
32910 +
32911 +#endif /* __KERNEL__ */
32912 +#endif /* __AUFS_WHOUT_H__ */
32913 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
32914 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
32915 +++ linux/fs/aufs/wkq.c 2015-06-22 08:27:37.890835363 +0200
32916 @@ -0,0 +1,213 @@
32917 +/*
32918 + * Copyright (C) 2005-2015 Junjiro R. Okajima
32919 + *
32920 + * This program, aufs is free software; you can redistribute it and/or modify
32921 + * it under the terms of the GNU General Public License as published by
32922 + * the Free Software Foundation; either version 2 of the License, or
32923 + * (at your option) any later version.
32924 + *
32925 + * This program is distributed in the hope that it will be useful,
32926 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32927 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32928 + * GNU General Public License for more details.
32929 + *
32930 + * You should have received a copy of the GNU General Public License
32931 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32932 + */
32933 +
32934 +/*
32935 + * workqueue for asynchronous/super-io operations
32936 + * todo: try new dredential scheme
32937 + */
32938 +
32939 +#include <linux/module.h>
32940 +#include "aufs.h"
32941 +
32942 +/* internal workqueue named AUFS_WKQ_NAME */
32943 +
32944 +static struct workqueue_struct *au_wkq;
32945 +
32946 +struct au_wkinfo {
32947 +       struct work_struct wk;
32948 +       struct kobject *kobj;
32949 +
32950 +       unsigned int flags; /* see wkq.h */
32951 +
32952 +       au_wkq_func_t func;
32953 +       void *args;
32954 +
32955 +       struct completion *comp;
32956 +};
32957 +
32958 +/* ---------------------------------------------------------------------- */
32959 +
32960 +static void wkq_func(struct work_struct *wk)
32961 +{
32962 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
32963 +
32964 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
32965 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
32966 +
32967 +       wkinfo->func(wkinfo->args);
32968 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
32969 +               complete(wkinfo->comp);
32970 +       else {
32971 +               kobject_put(wkinfo->kobj);
32972 +               module_put(THIS_MODULE); /* todo: ?? */
32973 +               kfree(wkinfo);
32974 +       }
32975 +}
32976 +
32977 +/*
32978 + * Since struct completion is large, try allocating it dynamically.
32979 + */
32980 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
32981 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
32982 +
32983 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
32984 +{
32985 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
32986 +       if (*comp) {
32987 +               init_completion(*comp);
32988 +               wkinfo->comp = *comp;
32989 +               return 0;
32990 +       }
32991 +       return -ENOMEM;
32992 +}
32993 +
32994 +static void au_wkq_comp_free(struct completion *comp)
32995 +{
32996 +       kfree(comp);
32997 +}
32998 +
32999 +#else
33000 +
33001 +/* no braces */
33002 +#define AuWkqCompDeclare(name) \
33003 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
33004 +       struct completion *comp = &_ ## name
33005 +
33006 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33007 +{
33008 +       wkinfo->comp = *comp;
33009 +       return 0;
33010 +}
33011 +
33012 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
33013 +{
33014 +       /* empty */
33015 +}
33016 +#endif /* 4KSTACKS */
33017 +
33018 +static void au_wkq_run(struct au_wkinfo *wkinfo)
33019 +{
33020 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
33021 +               if (au_wkq_test()) {
33022 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
33023 +                               " due to a dead dir by UDBA?\n");
33024 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
33025 +               }
33026 +       } else
33027 +               au_dbg_verify_kthread();
33028 +
33029 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
33030 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
33031 +               queue_work(au_wkq, &wkinfo->wk);
33032 +       } else {
33033 +               INIT_WORK(&wkinfo->wk, wkq_func);
33034 +               schedule_work(&wkinfo->wk);
33035 +       }
33036 +}
33037 +
33038 +/*
33039 + * Be careful. It is easy to make deadlock happen.
33040 + * processA: lock, wkq and wait
33041 + * processB: wkq and wait, lock in wkq
33042 + * --> deadlock
33043 + */
33044 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
33045 +{
33046 +       int err;
33047 +       AuWkqCompDeclare(comp);
33048 +       struct au_wkinfo wkinfo = {
33049 +               .flags  = flags,
33050 +               .func   = func,
33051 +               .args   = args
33052 +       };
33053 +
33054 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
33055 +       if (!err) {
33056 +               au_wkq_run(&wkinfo);
33057 +               /* no timeout, no interrupt */
33058 +               wait_for_completion(wkinfo.comp);
33059 +               au_wkq_comp_free(comp);
33060 +               destroy_work_on_stack(&wkinfo.wk);
33061 +       }
33062 +
33063 +       return err;
33064 +
33065 +}
33066 +
33067 +/*
33068 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
33069 + * problem in a concurrent umounting.
33070 + */
33071 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33072 +                 unsigned int flags)
33073 +{
33074 +       int err;
33075 +       struct au_wkinfo *wkinfo;
33076 +
33077 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
33078 +
33079 +       /*
33080 +        * wkq_func() must free this wkinfo.
33081 +        * it highly depends upon the implementation of workqueue.
33082 +        */
33083 +       err = 0;
33084 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
33085 +       if (wkinfo) {
33086 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
33087 +               wkinfo->flags = flags & ~AuWkq_WAIT;
33088 +               wkinfo->func = func;
33089 +               wkinfo->args = args;
33090 +               wkinfo->comp = NULL;
33091 +               kobject_get(wkinfo->kobj);
33092 +               __module_get(THIS_MODULE); /* todo: ?? */
33093 +
33094 +               au_wkq_run(wkinfo);
33095 +       } else {
33096 +               err = -ENOMEM;
33097 +               au_nwt_done(&au_sbi(sb)->si_nowait);
33098 +       }
33099 +
33100 +       return err;
33101 +}
33102 +
33103 +/* ---------------------------------------------------------------------- */
33104 +
33105 +void au_nwt_init(struct au_nowait_tasks *nwt)
33106 +{
33107 +       atomic_set(&nwt->nw_len, 0);
33108 +       /* smp_mb(); */ /* atomic_set */
33109 +       init_waitqueue_head(&nwt->nw_wq);
33110 +}
33111 +
33112 +void au_wkq_fin(void)
33113 +{
33114 +       destroy_workqueue(au_wkq);
33115 +}
33116 +
33117 +int __init au_wkq_init(void)
33118 +{
33119 +       int err;
33120 +
33121 +       err = 0;
33122 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
33123 +       if (IS_ERR(au_wkq))
33124 +               err = PTR_ERR(au_wkq);
33125 +       else if (!au_wkq)
33126 +               err = -ENOMEM;
33127 +
33128 +       return err;
33129 +}
33130 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
33131 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
33132 +++ linux/fs/aufs/wkq.h 2015-06-22 08:27:37.890835363 +0200
33133 @@ -0,0 +1,91 @@
33134 +/*
33135 + * Copyright (C) 2005-2015 Junjiro R. Okajima
33136 + *
33137 + * This program, aufs is free software; you can redistribute it and/or modify
33138 + * it under the terms of the GNU General Public License as published by
33139 + * the Free Software Foundation; either version 2 of the License, or
33140 + * (at your option) any later version.
33141 + *
33142 + * This program is distributed in the hope that it will be useful,
33143 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33144 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33145 + * GNU General Public License for more details.
33146 + *
33147 + * You should have received a copy of the GNU General Public License
33148 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33149 + */
33150 +
33151 +/*
33152 + * workqueue for asynchronous/super-io operations
33153 + * todo: try new credentials management scheme
33154 + */
33155 +
33156 +#ifndef __AUFS_WKQ_H__
33157 +#define __AUFS_WKQ_H__
33158 +
33159 +#ifdef __KERNEL__
33160 +
33161 +struct super_block;
33162 +
33163 +/* ---------------------------------------------------------------------- */
33164 +
33165 +/*
33166 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
33167 + */
33168 +struct au_nowait_tasks {
33169 +       atomic_t                nw_len;
33170 +       wait_queue_head_t       nw_wq;
33171 +};
33172 +
33173 +/* ---------------------------------------------------------------------- */
33174 +
33175 +typedef void (*au_wkq_func_t)(void *args);
33176 +
33177 +/* wkq flags */
33178 +#define AuWkq_WAIT     1
33179 +#define AuWkq_NEST     (1 << 1)
33180 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
33181 +#define au_fset_wkq(flags, name) \
33182 +       do { (flags) |= AuWkq_##name; } while (0)
33183 +#define au_fclr_wkq(flags, name) \
33184 +       do { (flags) &= ~AuWkq_##name; } while (0)
33185 +
33186 +#ifndef CONFIG_AUFS_HNOTIFY
33187 +#undef AuWkq_NEST
33188 +#define AuWkq_NEST     0
33189 +#endif
33190 +
33191 +/* wkq.c */
33192 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
33193 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33194 +                 unsigned int flags);
33195 +void au_nwt_init(struct au_nowait_tasks *nwt);
33196 +int __init au_wkq_init(void);
33197 +void au_wkq_fin(void);
33198 +
33199 +/* ---------------------------------------------------------------------- */
33200 +
33201 +static inline int au_wkq_test(void)
33202 +{
33203 +       return current->flags & PF_WQ_WORKER;
33204 +}
33205 +
33206 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
33207 +{
33208 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
33209 +}
33210 +
33211 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
33212 +{
33213 +       if (atomic_dec_and_test(&nwt->nw_len))
33214 +               wake_up_all(&nwt->nw_wq);
33215 +}
33216 +
33217 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
33218 +{
33219 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
33220 +       return 0;
33221 +}
33222 +
33223 +#endif /* __KERNEL__ */
33224 +#endif /* __AUFS_WKQ_H__ */
33225 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
33226 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
33227 +++ linux/fs/aufs/xattr.c       2015-06-22 08:27:37.890835363 +0200
33228 @@ -0,0 +1,344 @@
33229 +/*
33230 + * Copyright (C) 2014-2015 Junjiro R. Okajima
33231 + *
33232 + * This program, aufs is free software; you can redistribute it and/or modify
33233 + * it under the terms of the GNU General Public License as published by
33234 + * the Free Software Foundation; either version 2 of the License, or
33235 + * (at your option) any later version.
33236 + *
33237 + * This program is distributed in the hope that it will be useful,
33238 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33239 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33240 + * GNU General Public License for more details.
33241 + *
33242 + * You should have received a copy of the GNU General Public License
33243 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33244 + */
33245 +
33246 +/*
33247 + * handling xattr functions
33248 + */
33249 +
33250 +#include <linux/xattr.h>
33251 +#include "aufs.h"
33252 +
33253 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
33254 +{
33255 +       if (!ignore_flags)
33256 +               goto out;
33257 +       switch (err) {
33258 +       case -ENOMEM:
33259 +       case -EDQUOT:
33260 +               goto out;
33261 +       }
33262 +
33263 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
33264 +               err = 0;
33265 +               goto out;
33266 +       }
33267 +
33268 +#define cmp(brattr, prefix) do {                                       \
33269 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
33270 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
33271 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
33272 +                               err = 0;                                \
33273 +                       goto out;                                       \
33274 +               }                                                       \
33275 +       } while (0)
33276 +
33277 +       cmp(SEC, SECURITY);
33278 +       cmp(SYS, SYSTEM);
33279 +       cmp(TR, TRUSTED);
33280 +       cmp(USR, USER);
33281 +#undef cmp
33282 +
33283 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
33284 +               err = 0;
33285 +
33286 +out:
33287 +       return err;
33288 +}
33289 +
33290 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
33291 +
33292 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
33293 +                           char *name, char **buf, unsigned int ignore_flags,
33294 +                           unsigned int verbose)
33295 +{
33296 +       int err;
33297 +       ssize_t ssz;
33298 +       struct inode *h_idst;
33299 +
33300 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
33301 +       err = ssz;
33302 +       if (unlikely(err <= 0)) {
33303 +               if (err == -ENODATA
33304 +                   || (err == -EOPNOTSUPP
33305 +                       && ((ignore_flags & au_xattr_out_of_list)
33306 +                           || (au_test_nfs_noacl(h_src->d_inode)
33307 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
33308 +                                   || !strcmp(name,
33309 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
33310 +                           ))
33311 +                       err = 0;
33312 +               if (err && (verbose || au_debug_test()))
33313 +                       pr_err("%s, err %d\n", name, err);
33314 +               goto out;
33315 +       }
33316 +
33317 +       /* unlock it temporary */
33318 +       h_idst = h_dst->d_inode;
33319 +       mutex_unlock(&h_idst->i_mutex);
33320 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
33321 +       mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
33322 +       if (unlikely(err)) {
33323 +               if (verbose || au_debug_test())
33324 +                       pr_err("%s, err %d\n", name, err);
33325 +               err = au_xattr_ignore(err, name, ignore_flags);
33326 +       }
33327 +
33328 +out:
33329 +       return err;
33330 +}
33331 +
33332 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
33333 +                 unsigned int verbose)
33334 +{
33335 +       int err, unlocked, acl_access, acl_default;
33336 +       ssize_t ssz;
33337 +       struct inode *h_isrc, *h_idst;
33338 +       char *value, *p, *o, *e;
33339 +
33340 +       /* try stopping to update the source inode while we are referencing */
33341 +       /* there should not be the parent-child relationship between them */
33342 +       h_isrc = h_src->d_inode;
33343 +       h_idst = h_dst->d_inode;
33344 +       mutex_unlock(&h_idst->i_mutex);
33345 +       mutex_lock_nested(&h_isrc->i_mutex, AuLsc_I_CHILD);
33346 +       mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
33347 +       unlocked = 0;
33348 +
33349 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
33350 +       ssz = vfs_listxattr(h_src, NULL, 0);
33351 +       err = ssz;
33352 +       if (unlikely(err < 0)) {
33353 +               AuTraceErr(err);
33354 +               if (err == -ENODATA
33355 +                   || err == -EOPNOTSUPP)
33356 +                       err = 0;        /* ignore */
33357 +               goto out;
33358 +       }
33359 +
33360 +       err = 0;
33361 +       p = NULL;
33362 +       o = NULL;
33363 +       if (ssz) {
33364 +               err = -ENOMEM;
33365 +               p = kmalloc(ssz, GFP_NOFS);
33366 +               o = p;
33367 +               if (unlikely(!p))
33368 +                       goto out;
33369 +               err = vfs_listxattr(h_src, p, ssz);
33370 +       }
33371 +       mutex_unlock(&h_isrc->i_mutex);
33372 +       unlocked = 1;
33373 +       AuDbg("err %d, ssz %zd\n", err, ssz);
33374 +       if (unlikely(err < 0))
33375 +               goto out_free;
33376 +
33377 +       err = 0;
33378 +       e = p + ssz;
33379 +       value = NULL;
33380 +       acl_access = 0;
33381 +       acl_default = 0;
33382 +       while (!err && p < e) {
33383 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
33384 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
33385 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
33386 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
33387 +                                       - 1);
33388 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
33389 +                                      verbose);
33390 +               p += strlen(p) + 1;
33391 +       }
33392 +       AuTraceErr(err);
33393 +       ignore_flags |= au_xattr_out_of_list;
33394 +       if (!err && !acl_access) {
33395 +               err = au_do_cpup_xattr(h_dst, h_src,
33396 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
33397 +                                      ignore_flags, verbose);
33398 +               AuTraceErr(err);
33399 +       }
33400 +       if (!err && !acl_default) {
33401 +               err = au_do_cpup_xattr(h_dst, h_src,
33402 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
33403 +                                      ignore_flags, verbose);
33404 +               AuTraceErr(err);
33405 +       }
33406 +
33407 +       kfree(value);
33408 +
33409 +out_free:
33410 +       kfree(o);
33411 +out:
33412 +       if (!unlocked)
33413 +               mutex_unlock(&h_isrc->i_mutex);
33414 +       AuTraceErr(err);
33415 +       return err;
33416 +}
33417 +
33418 +/* ---------------------------------------------------------------------- */
33419 +
33420 +enum {
33421 +       AU_XATTR_LIST,
33422 +       AU_XATTR_GET
33423 +};
33424 +
33425 +struct au_lgxattr {
33426 +       int type;
33427 +       union {
33428 +               struct {
33429 +                       char    *list;
33430 +                       size_t  size;
33431 +               } list;
33432 +               struct {
33433 +                       const char      *name;
33434 +                       void            *value;
33435 +                       size_t          size;
33436 +               } get;
33437 +       } u;
33438 +};
33439 +
33440 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
33441 +{
33442 +       ssize_t err;
33443 +       struct path h_path;
33444 +       struct super_block *sb;
33445 +
33446 +       sb = dentry->d_sb;
33447 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
33448 +       if (unlikely(err))
33449 +               goto out;
33450 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path);
33451 +       if (unlikely(err))
33452 +               goto out_si;
33453 +       if (unlikely(!h_path.dentry))
33454 +               /* illegally overlapped or something */
33455 +               goto out_di; /* pretending success */
33456 +
33457 +       /* always topmost entry only */
33458 +       switch (arg->type) {
33459 +       case AU_XATTR_LIST:
33460 +               err = vfs_listxattr(h_path.dentry,
33461 +                                   arg->u.list.list, arg->u.list.size);
33462 +               break;
33463 +       case AU_XATTR_GET:
33464 +               err = vfs_getxattr(h_path.dentry,
33465 +                                  arg->u.get.name, arg->u.get.value,
33466 +                                  arg->u.get.size);
33467 +               break;
33468 +       }
33469 +
33470 +out_di:
33471 +       di_read_unlock(dentry, AuLock_IR);
33472 +out_si:
33473 +       si_read_unlock(sb);
33474 +out:
33475 +       AuTraceErr(err);
33476 +       return err;
33477 +}
33478 +
33479 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
33480 +{
33481 +       struct au_lgxattr arg = {
33482 +               .type = AU_XATTR_LIST,
33483 +               .u.list = {
33484 +                       .list   = list,
33485 +                       .size   = size
33486 +               },
33487 +       };
33488 +
33489 +       return au_lgxattr(dentry, &arg);
33490 +}
33491 +
33492 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
33493 +                     size_t size)
33494 +{
33495 +       struct au_lgxattr arg = {
33496 +               .type = AU_XATTR_GET,
33497 +               .u.get = {
33498 +                       .name   = name,
33499 +                       .value  = value,
33500 +                       .size   = size
33501 +               },
33502 +       };
33503 +
33504 +       return au_lgxattr(dentry, &arg);
33505 +}
33506 +
33507 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
33508 +                 size_t size, int flags)
33509 +{
33510 +       struct au_srxattr arg = {
33511 +               .type = AU_XATTR_SET,
33512 +               .u.set = {
33513 +                       .name   = name,
33514 +                       .value  = value,
33515 +                       .size   = size,
33516 +                       .flags  = flags
33517 +               },
33518 +       };
33519 +
33520 +       return au_srxattr(dentry, &arg);
33521 +}
33522 +
33523 +int aufs_removexattr(struct dentry *dentry, const char *name)
33524 +{
33525 +       struct au_srxattr arg = {
33526 +               .type = AU_XATTR_REMOVE,
33527 +               .u.remove = {
33528 +                       .name   = name
33529 +               },
33530 +       };
33531 +
33532 +       return au_srxattr(dentry, &arg);
33533 +}
33534 +
33535 +/* ---------------------------------------------------------------------- */
33536 +
33537 +#if 0
33538 +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
33539 +                           const char *name, size_t name_len, int type)
33540 +{
33541 +       return aufs_listxattr(dentry, list, list_size);
33542 +}
33543 +
33544 +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
33545 +                       size_t size, int type)
33546 +{
33547 +       return aufs_getxattr(dentry, name, buffer, size);
33548 +}
33549 +
33550 +static int au_xattr_set(struct dentry *dentry, const char *name,
33551 +                       const void *value, size_t size, int flags, int type)
33552 +{
33553 +       return aufs_setxattr(dentry, name, value, size, flags);
33554 +}
33555 +
33556 +static const struct xattr_handler au_xattr_handler = {
33557 +       /* no prefix, no flags */
33558 +       .list   = au_xattr_list,
33559 +       .get    = au_xattr_get,
33560 +       .set    = au_xattr_set
33561 +       /* why no remove? */
33562 +};
33563 +
33564 +static const struct xattr_handler *au_xattr_handlers[] = {
33565 +       &au_xattr_handler
33566 +};
33567 +
33568 +void au_xattr_init(struct super_block *sb)
33569 +{
33570 +       /* sb->s_xattr = au_xattr_handlers; */
33571 +}
33572 +#endif
33573 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
33574 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
33575 +++ linux/fs/aufs/xino.c        2015-06-22 08:29:23.710091096 +0200
33576 @@ -0,0 +1,1322 @@
33577 +/*
33578 + * Copyright (C) 2005-2015 Junjiro R. Okajima
33579 + *
33580 + * This program, aufs is free software; you can redistribute it and/or modify
33581 + * it under the terms of the GNU General Public License as published by
33582 + * the Free Software Foundation; either version 2 of the License, or
33583 + * (at your option) any later version.
33584 + *
33585 + * This program is distributed in the hope that it will be useful,
33586 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33587 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33588 + * GNU General Public License for more details.
33589 + *
33590 + * You should have received a copy of the GNU General Public License
33591 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33592 + */
33593 +
33594 +/*
33595 + * external inode number translation table and bitmap
33596 + */
33597 +
33598 +#include <linux/seq_file.h>
33599 +#include <linux/statfs.h>
33600 +#include "aufs.h"
33601 +
33602 +/* todo: unnecessary to support mmap_sem since kernel-space? */
33603 +ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
33604 +                  loff_t *pos)
33605 +{
33606 +       ssize_t err;
33607 +       mm_segment_t oldfs;
33608 +       union {
33609 +               void *k;
33610 +               char __user *u;
33611 +       } buf;
33612 +
33613 +       buf.k = kbuf;
33614 +       oldfs = get_fs();
33615 +       set_fs(KERNEL_DS);
33616 +       do {
33617 +               /* todo: signal_pending? */
33618 +               err = func(file, buf.u, size, pos);
33619 +       } while (err == -EAGAIN || err == -EINTR);
33620 +       set_fs(oldfs);
33621 +
33622 +#if 0 /* reserved for future use */
33623 +       if (err > 0)
33624 +               fsnotify_access(file->f_path.dentry);
33625 +#endif
33626 +
33627 +       return err;
33628 +}
33629 +
33630 +/* ---------------------------------------------------------------------- */
33631 +
33632 +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
33633 +                             size_t size, loff_t *pos)
33634 +{
33635 +       ssize_t err;
33636 +       mm_segment_t oldfs;
33637 +       union {
33638 +               void *k;
33639 +               const char __user *u;
33640 +       } buf;
33641 +
33642 +       buf.k = kbuf;
33643 +       oldfs = get_fs();
33644 +       set_fs(KERNEL_DS);
33645 +       do {
33646 +               /* todo: signal_pending? */
33647 +               err = func(file, buf.u, size, pos);
33648 +       } while (err == -EAGAIN || err == -EINTR);
33649 +       set_fs(oldfs);
33650 +
33651 +#if 0 /* reserved for future use */
33652 +       if (err > 0)
33653 +               fsnotify_modify(file->f_path.dentry);
33654 +#endif
33655 +
33656 +       return err;
33657 +}
33658 +
33659 +struct do_xino_fwrite_args {
33660 +       ssize_t *errp;
33661 +       au_writef_t func;
33662 +       struct file *file;
33663 +       void *buf;
33664 +       size_t size;
33665 +       loff_t *pos;
33666 +};
33667 +
33668 +static void call_do_xino_fwrite(void *args)
33669 +{
33670 +       struct do_xino_fwrite_args *a = args;
33671 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
33672 +}
33673 +
33674 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
33675 +                   loff_t *pos)
33676 +{
33677 +       ssize_t err;
33678 +
33679 +       /* todo: signal block and no wkq? */
33680 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
33681 +               lockdep_off();
33682 +               err = do_xino_fwrite(func, file, buf, size, pos);
33683 +               lockdep_on();
33684 +       } else {
33685 +               /*
33686 +                * it breaks RLIMIT_FSIZE and normal user's limit,
33687 +                * users should care about quota and real 'filesystem full.'
33688 +                */
33689 +               int wkq_err;
33690 +               struct do_xino_fwrite_args args = {
33691 +                       .errp   = &err,
33692 +                       .func   = func,
33693 +                       .file   = file,
33694 +                       .buf    = buf,
33695 +                       .size   = size,
33696 +                       .pos    = pos
33697 +               };
33698 +
33699 +               wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
33700 +               if (unlikely(wkq_err))
33701 +                       err = wkq_err;
33702 +       }
33703 +
33704 +       return err;
33705 +}
33706 +
33707 +/* ---------------------------------------------------------------------- */
33708 +
33709 +/*
33710 + * create a new xinofile at the same place/path as @base_file.
33711 + */
33712 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
33713 +{
33714 +       struct file *file;
33715 +       struct dentry *base, *parent;
33716 +       struct inode *dir, *delegated;
33717 +       struct qstr *name;
33718 +       struct path path;
33719 +       int err;
33720 +
33721 +       base = base_file->f_path.dentry;
33722 +       parent = base->d_parent; /* dir inode is locked */
33723 +       dir = parent->d_inode;
33724 +       IMustLock(dir);
33725 +
33726 +       file = ERR_PTR(-EINVAL);
33727 +       name = &base->d_name;
33728 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
33729 +       if (IS_ERR(path.dentry)) {
33730 +               file = (void *)path.dentry;
33731 +               pr_err("%pd lookup err %ld\n",
33732 +                      base, PTR_ERR(path.dentry));
33733 +               goto out;
33734 +       }
33735 +
33736 +       /* no need to mnt_want_write() since we call dentry_open() later */
33737 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
33738 +       if (unlikely(err)) {
33739 +               file = ERR_PTR(err);
33740 +               pr_err("%pd create err %d\n", base, err);
33741 +               goto out_dput;
33742 +       }
33743 +
33744 +       path.mnt = base_file->f_path.mnt;
33745 +       file = vfsub_dentry_open(&path,
33746 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
33747 +                                /* | __FMODE_NONOTIFY */);
33748 +       if (IS_ERR(file)) {
33749 +               pr_err("%pd open err %ld\n", base, PTR_ERR(file));
33750 +               goto out_dput;
33751 +       }
33752 +
33753 +       delegated = NULL;
33754 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
33755 +       if (unlikely(err == -EWOULDBLOCK)) {
33756 +               pr_warn("cannot retry for NFSv4 delegation"
33757 +                       " for an internal unlink\n");
33758 +               iput(delegated);
33759 +       }
33760 +       if (unlikely(err)) {
33761 +               pr_err("%pd unlink err %d\n", base, err);
33762 +               goto out_fput;
33763 +       }
33764 +
33765 +       if (copy_src) {
33766 +               /* no one can touch copy_src xino */
33767 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
33768 +               if (unlikely(err)) {
33769 +                       pr_err("%pd copy err %d\n", base, err);
33770 +                       goto out_fput;
33771 +               }
33772 +       }
33773 +       goto out_dput; /* success */
33774 +
33775 +out_fput:
33776 +       fput(file);
33777 +       file = ERR_PTR(err);
33778 +out_dput:
33779 +       dput(path.dentry);
33780 +out:
33781 +       return file;
33782 +}
33783 +
33784 +struct au_xino_lock_dir {
33785 +       struct au_hinode *hdir;
33786 +       struct dentry *parent;
33787 +       struct mutex *mtx;
33788 +};
33789 +
33790 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
33791 +                            struct au_xino_lock_dir *ldir)
33792 +{
33793 +       aufs_bindex_t brid, bindex;
33794 +
33795 +       ldir->hdir = NULL;
33796 +       bindex = -1;
33797 +       brid = au_xino_brid(sb);
33798 +       if (brid >= 0)
33799 +               bindex = au_br_index(sb, brid);
33800 +       if (bindex >= 0) {
33801 +               ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
33802 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
33803 +       } else {
33804 +               ldir->parent = dget_parent(xino->f_path.dentry);
33805 +               ldir->mtx = &ldir->parent->d_inode->i_mutex;
33806 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
33807 +       }
33808 +}
33809 +
33810 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
33811 +{
33812 +       if (ldir->hdir)
33813 +               au_hn_imtx_unlock(ldir->hdir);
33814 +       else {
33815 +               mutex_unlock(ldir->mtx);
33816 +               dput(ldir->parent);
33817 +       }
33818 +}
33819 +
33820 +/* ---------------------------------------------------------------------- */
33821 +
33822 +/* trucate xino files asynchronously */
33823 +
33824 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
33825 +{
33826 +       int err;
33827 +       unsigned long jiffy;
33828 +       blkcnt_t blocks;
33829 +       aufs_bindex_t bi, bend;
33830 +       struct kstatfs *st;
33831 +       struct au_branch *br;
33832 +       struct file *new_xino, *file;
33833 +       struct super_block *h_sb;
33834 +       struct au_xino_lock_dir ldir;
33835 +
33836 +       err = -ENOMEM;
33837 +       st = kzalloc(sizeof(*st), GFP_NOFS);
33838 +       if (unlikely(!st))
33839 +               goto out;
33840 +
33841 +       err = -EINVAL;
33842 +       bend = au_sbend(sb);
33843 +       if (unlikely(bindex < 0 || bend < bindex))
33844 +               goto out_st;
33845 +       br = au_sbr(sb, bindex);
33846 +       file = br->br_xino.xi_file;
33847 +       if (!file)
33848 +               goto out_st;
33849 +
33850 +       err = vfs_statfs(&file->f_path, st);
33851 +       if (unlikely(err))
33852 +               AuErr1("statfs err %d, ignored\n", err);
33853 +       jiffy = jiffies;
33854 +       blocks = file_inode(file)->i_blocks;
33855 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
33856 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
33857 +
33858 +       au_xino_lock_dir(sb, file, &ldir);
33859 +       /* mnt_want_write() is unnecessary here */
33860 +       new_xino = au_xino_create2(file, file);
33861 +       au_xino_unlock_dir(&ldir);
33862 +       err = PTR_ERR(new_xino);
33863 +       if (IS_ERR(new_xino)) {
33864 +               pr_err("err %d, ignored\n", err);
33865 +               goto out_st;
33866 +       }
33867 +       err = 0;
33868 +       fput(file);
33869 +       br->br_xino.xi_file = new_xino;
33870 +
33871 +       h_sb = au_br_sb(br);
33872 +       for (bi = 0; bi <= bend; bi++) {
33873 +               if (unlikely(bi == bindex))
33874 +                       continue;
33875 +               br = au_sbr(sb, bi);
33876 +               if (au_br_sb(br) != h_sb)
33877 +                       continue;
33878 +
33879 +               fput(br->br_xino.xi_file);
33880 +               br->br_xino.xi_file = new_xino;
33881 +               get_file(new_xino);
33882 +       }
33883 +
33884 +       err = vfs_statfs(&new_xino->f_path, st);
33885 +       if (!err) {
33886 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
33887 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
33888 +                       st->f_bfree, st->f_blocks);
33889 +               if (file_inode(new_xino)->i_blocks < blocks)
33890 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
33891 +       } else
33892 +               AuErr1("statfs err %d, ignored\n", err);
33893 +
33894 +out_st:
33895 +       kfree(st);
33896 +out:
33897 +       return err;
33898 +}
33899 +
33900 +struct xino_do_trunc_args {
33901 +       struct super_block *sb;
33902 +       struct au_branch *br;
33903 +};
33904 +
33905 +static void xino_do_trunc(void *_args)
33906 +{
33907 +       struct xino_do_trunc_args *args = _args;
33908 +       struct super_block *sb;
33909 +       struct au_branch *br;
33910 +       struct inode *dir;
33911 +       int err;
33912 +       aufs_bindex_t bindex;
33913 +
33914 +       err = 0;
33915 +       sb = args->sb;
33916 +       dir = sb->s_root->d_inode;
33917 +       br = args->br;
33918 +
33919 +       si_noflush_write_lock(sb);
33920 +       ii_read_lock_parent(dir);
33921 +       bindex = au_br_index(sb, br->br_id);
33922 +       err = au_xino_trunc(sb, bindex);
33923 +       ii_read_unlock(dir);
33924 +       if (unlikely(err))
33925 +               pr_warn("err b%d, (%d)\n", bindex, err);
33926 +       atomic_dec(&br->br_xino_running);
33927 +       atomic_dec(&br->br_count);
33928 +       si_write_unlock(sb);
33929 +       au_nwt_done(&au_sbi(sb)->si_nowait);
33930 +       kfree(args);
33931 +}
33932 +
33933 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
33934 +{
33935 +       int err;
33936 +       struct kstatfs st;
33937 +       struct au_sbinfo *sbinfo;
33938 +
33939 +       /* todo: si_xino_expire and the ratio should be customizable */
33940 +       sbinfo = au_sbi(sb);
33941 +       if (time_before(jiffies,
33942 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
33943 +               return 0;
33944 +
33945 +       /* truncation border */
33946 +       err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
33947 +       if (unlikely(err)) {
33948 +               AuErr1("statfs err %d, ignored\n", err);
33949 +               return 0;
33950 +       }
33951 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
33952 +               return 0;
33953 +
33954 +       return 1;
33955 +}
33956 +
33957 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
33958 +{
33959 +       struct xino_do_trunc_args *args;
33960 +       int wkq_err;
33961 +
33962 +       if (!xino_trunc_test(sb, br))
33963 +               return;
33964 +
33965 +       if (atomic_inc_return(&br->br_xino_running) > 1)
33966 +               goto out;
33967 +
33968 +       /* lock and kfree() will be called in trunc_xino() */
33969 +       args = kmalloc(sizeof(*args), GFP_NOFS);
33970 +       if (unlikely(!args)) {
33971 +               AuErr1("no memory\n");
33972 +               goto out_args;
33973 +       }
33974 +
33975 +       atomic_inc(&br->br_count);
33976 +       args->sb = sb;
33977 +       args->br = br;
33978 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
33979 +       if (!wkq_err)
33980 +               return; /* success */
33981 +
33982 +       pr_err("wkq %d\n", wkq_err);
33983 +       atomic_dec(&br->br_count);
33984 +
33985 +out_args:
33986 +       kfree(args);
33987 +out:
33988 +       atomic_dec(&br->br_xino_running);
33989 +}
33990 +
33991 +/* ---------------------------------------------------------------------- */
33992 +
33993 +static int au_xino_do_write(au_writef_t write, struct file *file,
33994 +                           ino_t h_ino, ino_t ino)
33995 +{
33996 +       loff_t pos;
33997 +       ssize_t sz;
33998 +
33999 +       pos = h_ino;
34000 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
34001 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34002 +               return -EFBIG;
34003 +       }
34004 +       pos *= sizeof(ino);
34005 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
34006 +       if (sz == sizeof(ino))
34007 +               return 0; /* success */
34008 +
34009 +       AuIOErr("write failed (%zd)\n", sz);
34010 +       return -EIO;
34011 +}
34012 +
34013 +/*
34014 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
34015 + * at the position of @h_ino.
34016 + * even if @ino is zero, it is written to the xinofile and means no entry.
34017 + * if the size of the xino file on a specific filesystem exceeds the watermark,
34018 + * try truncating it.
34019 + */
34020 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34021 +                 ino_t ino)
34022 +{
34023 +       int err;
34024 +       unsigned int mnt_flags;
34025 +       struct au_branch *br;
34026 +
34027 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
34028 +                    || ((loff_t)-1) > 0);
34029 +       SiMustAnyLock(sb);
34030 +
34031 +       mnt_flags = au_mntflags(sb);
34032 +       if (!au_opt_test(mnt_flags, XINO))
34033 +               return 0;
34034 +
34035 +       br = au_sbr(sb, bindex);
34036 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34037 +                              h_ino, ino);
34038 +       if (!err) {
34039 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
34040 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34041 +                       xino_try_trunc(sb, br);
34042 +               return 0; /* success */
34043 +       }
34044 +
34045 +       AuIOErr("write failed (%d)\n", err);
34046 +       return -EIO;
34047 +}
34048 +
34049 +/* ---------------------------------------------------------------------- */
34050 +
34051 +/* aufs inode number bitmap */
34052 +
34053 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
34054 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
34055 +{
34056 +       ino_t ino;
34057 +
34058 +       AuDebugOn(bit < 0 || page_bits <= bit);
34059 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
34060 +       return ino;
34061 +}
34062 +
34063 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
34064 +{
34065 +       AuDebugOn(ino < AUFS_FIRST_INO);
34066 +       ino -= AUFS_FIRST_INO;
34067 +       *pindex = ino / page_bits;
34068 +       *bit = ino % page_bits;
34069 +}
34070 +
34071 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
34072 +{
34073 +       int err;
34074 +       loff_t pos;
34075 +       ssize_t sz;
34076 +       struct au_sbinfo *sbinfo;
34077 +       struct file *xib;
34078 +       unsigned long *p;
34079 +
34080 +       sbinfo = au_sbi(sb);
34081 +       MtxMustLock(&sbinfo->si_xib_mtx);
34082 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
34083 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
34084 +
34085 +       if (pindex == sbinfo->si_xib_last_pindex)
34086 +               return 0;
34087 +
34088 +       xib = sbinfo->si_xib;
34089 +       p = sbinfo->si_xib_buf;
34090 +       pos = sbinfo->si_xib_last_pindex;
34091 +       pos *= PAGE_SIZE;
34092 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34093 +       if (unlikely(sz != PAGE_SIZE))
34094 +               goto out;
34095 +
34096 +       pos = pindex;
34097 +       pos *= PAGE_SIZE;
34098 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
34099 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
34100 +       else {
34101 +               memset(p, 0, PAGE_SIZE);
34102 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34103 +       }
34104 +       if (sz == PAGE_SIZE) {
34105 +               sbinfo->si_xib_last_pindex = pindex;
34106 +               return 0; /* success */
34107 +       }
34108 +
34109 +out:
34110 +       AuIOErr1("write failed (%zd)\n", sz);
34111 +       err = sz;
34112 +       if (sz >= 0)
34113 +               err = -EIO;
34114 +       return err;
34115 +}
34116 +
34117 +/* ---------------------------------------------------------------------- */
34118 +
34119 +static void au_xib_clear_bit(struct inode *inode)
34120 +{
34121 +       int err, bit;
34122 +       unsigned long pindex;
34123 +       struct super_block *sb;
34124 +       struct au_sbinfo *sbinfo;
34125 +
34126 +       AuDebugOn(inode->i_nlink);
34127 +
34128 +       sb = inode->i_sb;
34129 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
34130 +       AuDebugOn(page_bits <= bit);
34131 +       sbinfo = au_sbi(sb);
34132 +       mutex_lock(&sbinfo->si_xib_mtx);
34133 +       err = xib_pindex(sb, pindex);
34134 +       if (!err) {
34135 +               clear_bit(bit, sbinfo->si_xib_buf);
34136 +               sbinfo->si_xib_next_bit = bit;
34137 +       }
34138 +       mutex_unlock(&sbinfo->si_xib_mtx);
34139 +}
34140 +
34141 +/* for s_op->delete_inode() */
34142 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
34143 +{
34144 +       int err;
34145 +       unsigned int mnt_flags;
34146 +       aufs_bindex_t bindex, bend, bi;
34147 +       unsigned char try_trunc;
34148 +       struct au_iinfo *iinfo;
34149 +       struct super_block *sb;
34150 +       struct au_hinode *hi;
34151 +       struct inode *h_inode;
34152 +       struct au_branch *br;
34153 +       au_writef_t xwrite;
34154 +
34155 +       sb = inode->i_sb;
34156 +       mnt_flags = au_mntflags(sb);
34157 +       if (!au_opt_test(mnt_flags, XINO)
34158 +           || inode->i_ino == AUFS_ROOT_INO)
34159 +               return;
34160 +
34161 +       if (unlinked) {
34162 +               au_xigen_inc(inode);
34163 +               au_xib_clear_bit(inode);
34164 +       }
34165 +
34166 +       iinfo = au_ii(inode);
34167 +       if (!iinfo)
34168 +               return;
34169 +
34170 +       bindex = iinfo->ii_bstart;
34171 +       if (bindex < 0)
34172 +               return;
34173 +
34174 +       xwrite = au_sbi(sb)->si_xwrite;
34175 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
34176 +       hi = iinfo->ii_hinode + bindex;
34177 +       bend = iinfo->ii_bend;
34178 +       for (; bindex <= bend; bindex++, hi++) {
34179 +               h_inode = hi->hi_inode;
34180 +               if (!h_inode
34181 +                   || (!unlinked && h_inode->i_nlink))
34182 +                       continue;
34183 +
34184 +               /* inode may not be revalidated */
34185 +               bi = au_br_index(sb, hi->hi_id);
34186 +               if (bi < 0)
34187 +                       continue;
34188 +
34189 +               br = au_sbr(sb, bi);
34190 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
34191 +                                      h_inode->i_ino, /*ino*/0);
34192 +               if (!err && try_trunc
34193 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34194 +                       xino_try_trunc(sb, br);
34195 +       }
34196 +}
34197 +
34198 +/* get an unused inode number from bitmap */
34199 +ino_t au_xino_new_ino(struct super_block *sb)
34200 +{
34201 +       ino_t ino;
34202 +       unsigned long *p, pindex, ul, pend;
34203 +       struct au_sbinfo *sbinfo;
34204 +       struct file *file;
34205 +       int free_bit, err;
34206 +
34207 +       if (!au_opt_test(au_mntflags(sb), XINO))
34208 +               return iunique(sb, AUFS_FIRST_INO);
34209 +
34210 +       sbinfo = au_sbi(sb);
34211 +       mutex_lock(&sbinfo->si_xib_mtx);
34212 +       p = sbinfo->si_xib_buf;
34213 +       free_bit = sbinfo->si_xib_next_bit;
34214 +       if (free_bit < page_bits && !test_bit(free_bit, p))
34215 +               goto out; /* success */
34216 +       free_bit = find_first_zero_bit(p, page_bits);
34217 +       if (free_bit < page_bits)
34218 +               goto out; /* success */
34219 +
34220 +       pindex = sbinfo->si_xib_last_pindex;
34221 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
34222 +               err = xib_pindex(sb, ul);
34223 +               if (unlikely(err))
34224 +                       goto out_err;
34225 +               free_bit = find_first_zero_bit(p, page_bits);
34226 +               if (free_bit < page_bits)
34227 +                       goto out; /* success */
34228 +       }
34229 +
34230 +       file = sbinfo->si_xib;
34231 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
34232 +       for (ul = pindex + 1; ul <= pend; ul++) {
34233 +               err = xib_pindex(sb, ul);
34234 +               if (unlikely(err))
34235 +                       goto out_err;
34236 +               free_bit = find_first_zero_bit(p, page_bits);
34237 +               if (free_bit < page_bits)
34238 +                       goto out; /* success */
34239 +       }
34240 +       BUG();
34241 +
34242 +out:
34243 +       set_bit(free_bit, p);
34244 +       sbinfo->si_xib_next_bit = free_bit + 1;
34245 +       pindex = sbinfo->si_xib_last_pindex;
34246 +       mutex_unlock(&sbinfo->si_xib_mtx);
34247 +       ino = xib_calc_ino(pindex, free_bit);
34248 +       AuDbg("i%lu\n", (unsigned long)ino);
34249 +       return ino;
34250 +out_err:
34251 +       mutex_unlock(&sbinfo->si_xib_mtx);
34252 +       AuDbg("i0\n");
34253 +       return 0;
34254 +}
34255 +
34256 +/*
34257 + * read @ino from xinofile for the specified branch{@sb, @bindex}
34258 + * at the position of @h_ino.
34259 + * if @ino does not exist and @do_new is true, get new one.
34260 + */
34261 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34262 +                ino_t *ino)
34263 +{
34264 +       int err;
34265 +       ssize_t sz;
34266 +       loff_t pos;
34267 +       struct file *file;
34268 +       struct au_sbinfo *sbinfo;
34269 +
34270 +       *ino = 0;
34271 +       if (!au_opt_test(au_mntflags(sb), XINO))
34272 +               return 0; /* no xino */
34273 +
34274 +       err = 0;
34275 +       sbinfo = au_sbi(sb);
34276 +       pos = h_ino;
34277 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
34278 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34279 +               return -EFBIG;
34280 +       }
34281 +       pos *= sizeof(*ino);
34282 +
34283 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
34284 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
34285 +               return 0; /* no ino */
34286 +
34287 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
34288 +       if (sz == sizeof(*ino))
34289 +               return 0; /* success */
34290 +
34291 +       err = sz;
34292 +       if (unlikely(sz >= 0)) {
34293 +               err = -EIO;
34294 +               AuIOErr("xino read error (%zd)\n", sz);
34295 +       }
34296 +
34297 +       return err;
34298 +}
34299 +
34300 +/* ---------------------------------------------------------------------- */
34301 +
34302 +/* create and set a new xino file */
34303 +
34304 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
34305 +{
34306 +       struct file *file;
34307 +       struct dentry *h_parent, *d;
34308 +       struct inode *h_dir, *inode;
34309 +       int err;
34310 +
34311 +       /*
34312 +        * at mount-time, and the xino file is the default path,
34313 +        * hnotify is disabled so we have no notify events to ignore.
34314 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
34315 +        */
34316 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
34317 +                              /* | __FMODE_NONOTIFY */,
34318 +                              S_IRUGO | S_IWUGO);
34319 +       if (IS_ERR(file)) {
34320 +               if (!silent)
34321 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
34322 +               return file;
34323 +       }
34324 +
34325 +       /* keep file count */
34326 +       err = 0;
34327 +       inode = file_inode(file);
34328 +       h_parent = dget_parent(file->f_path.dentry);
34329 +       h_dir = h_parent->d_inode;
34330 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
34331 +       /* mnt_want_write() is unnecessary here */
34332 +       /* no delegation since it is just created */
34333 +       if (inode->i_nlink)
34334 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
34335 +                                  /*force*/0);
34336 +       mutex_unlock(&h_dir->i_mutex);
34337 +       dput(h_parent);
34338 +       if (unlikely(err)) {
34339 +               if (!silent)
34340 +                       pr_err("unlink %s(%d)\n", fname, err);
34341 +               goto out;
34342 +       }
34343 +
34344 +       err = -EINVAL;
34345 +       d = file->f_path.dentry;
34346 +       if (unlikely(sb == d->d_sb)) {
34347 +               if (!silent)
34348 +                       pr_err("%s must be outside\n", fname);
34349 +               goto out;
34350 +       }
34351 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
34352 +               if (!silent)
34353 +                       pr_err("xino doesn't support %s(%s)\n",
34354 +                              fname, au_sbtype(d->d_sb));
34355 +               goto out;
34356 +       }
34357 +       return file; /* success */
34358 +
34359 +out:
34360 +       fput(file);
34361 +       file = ERR_PTR(err);
34362 +       return file;
34363 +}
34364 +
34365 +/*
34366 + * find another branch who is on the same filesystem of the specified
34367 + * branch{@btgt}. search until @bend.
34368 + */
34369 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
34370 +                       aufs_bindex_t bend)
34371 +{
34372 +       aufs_bindex_t bindex;
34373 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
34374 +
34375 +       for (bindex = 0; bindex < btgt; bindex++)
34376 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34377 +                       return bindex;
34378 +       for (bindex++; bindex <= bend; bindex++)
34379 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34380 +                       return bindex;
34381 +       return -1;
34382 +}
34383 +
34384 +/* ---------------------------------------------------------------------- */
34385 +
34386 +/*
34387 + * initialize the xinofile for the specified branch @br
34388 + * at the place/path where @base_file indicates.
34389 + * test whether another branch is on the same filesystem or not,
34390 + * if @do_test is true.
34391 + */
34392 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
34393 +              struct file *base_file, int do_test)
34394 +{
34395 +       int err;
34396 +       ino_t ino;
34397 +       aufs_bindex_t bend, bindex;
34398 +       struct au_branch *shared_br, *b;
34399 +       struct file *file;
34400 +       struct super_block *tgt_sb;
34401 +
34402 +       shared_br = NULL;
34403 +       bend = au_sbend(sb);
34404 +       if (do_test) {
34405 +               tgt_sb = au_br_sb(br);
34406 +               for (bindex = 0; bindex <= bend; bindex++) {
34407 +                       b = au_sbr(sb, bindex);
34408 +                       if (tgt_sb == au_br_sb(b)) {
34409 +                               shared_br = b;
34410 +                               break;
34411 +                       }
34412 +               }
34413 +       }
34414 +
34415 +       if (!shared_br || !shared_br->br_xino.xi_file) {
34416 +               struct au_xino_lock_dir ldir;
34417 +
34418 +               au_xino_lock_dir(sb, base_file, &ldir);
34419 +               /* mnt_want_write() is unnecessary here */
34420 +               file = au_xino_create2(base_file, NULL);
34421 +               au_xino_unlock_dir(&ldir);
34422 +               err = PTR_ERR(file);
34423 +               if (IS_ERR(file))
34424 +                       goto out;
34425 +               br->br_xino.xi_file = file;
34426 +       } else {
34427 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
34428 +               get_file(br->br_xino.xi_file);
34429 +       }
34430 +
34431 +       ino = AUFS_ROOT_INO;
34432 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34433 +                              h_ino, ino);
34434 +       if (unlikely(err)) {
34435 +               fput(br->br_xino.xi_file);
34436 +               br->br_xino.xi_file = NULL;
34437 +       }
34438 +
34439 +out:
34440 +       return err;
34441 +}
34442 +
34443 +/* ---------------------------------------------------------------------- */
34444 +
34445 +/* trucate a xino bitmap file */
34446 +
34447 +/* todo: slow */
34448 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
34449 +{
34450 +       int err, bit;
34451 +       ssize_t sz;
34452 +       unsigned long pindex;
34453 +       loff_t pos, pend;
34454 +       struct au_sbinfo *sbinfo;
34455 +       au_readf_t func;
34456 +       ino_t *ino;
34457 +       unsigned long *p;
34458 +
34459 +       err = 0;
34460 +       sbinfo = au_sbi(sb);
34461 +       MtxMustLock(&sbinfo->si_xib_mtx);
34462 +       p = sbinfo->si_xib_buf;
34463 +       func = sbinfo->si_xread;
34464 +       pend = vfsub_f_size_read(file);
34465 +       pos = 0;
34466 +       while (pos < pend) {
34467 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
34468 +               err = sz;
34469 +               if (unlikely(sz <= 0))
34470 +                       goto out;
34471 +
34472 +               err = 0;
34473 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
34474 +                       if (unlikely(*ino < AUFS_FIRST_INO))
34475 +                               continue;
34476 +
34477 +                       xib_calc_bit(*ino, &pindex, &bit);
34478 +                       AuDebugOn(page_bits <= bit);
34479 +                       err = xib_pindex(sb, pindex);
34480 +                       if (!err)
34481 +                               set_bit(bit, p);
34482 +                       else
34483 +                               goto out;
34484 +               }
34485 +       }
34486 +
34487 +out:
34488 +       return err;
34489 +}
34490 +
34491 +static int xib_restore(struct super_block *sb)
34492 +{
34493 +       int err;
34494 +       aufs_bindex_t bindex, bend;
34495 +       void *page;
34496 +
34497 +       err = -ENOMEM;
34498 +       page = (void *)__get_free_page(GFP_NOFS);
34499 +       if (unlikely(!page))
34500 +               goto out;
34501 +
34502 +       err = 0;
34503 +       bend = au_sbend(sb);
34504 +       for (bindex = 0; !err && bindex <= bend; bindex++)
34505 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
34506 +                       err = do_xib_restore
34507 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
34508 +               else
34509 +                       AuDbg("b%d\n", bindex);
34510 +       free_page((unsigned long)page);
34511 +
34512 +out:
34513 +       return err;
34514 +}
34515 +
34516 +int au_xib_trunc(struct super_block *sb)
34517 +{
34518 +       int err;
34519 +       ssize_t sz;
34520 +       loff_t pos;
34521 +       struct au_xino_lock_dir ldir;
34522 +       struct au_sbinfo *sbinfo;
34523 +       unsigned long *p;
34524 +       struct file *file;
34525 +
34526 +       SiMustWriteLock(sb);
34527 +
34528 +       err = 0;
34529 +       sbinfo = au_sbi(sb);
34530 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
34531 +               goto out;
34532 +
34533 +       file = sbinfo->si_xib;
34534 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
34535 +               goto out;
34536 +
34537 +       au_xino_lock_dir(sb, file, &ldir);
34538 +       /* mnt_want_write() is unnecessary here */
34539 +       file = au_xino_create2(sbinfo->si_xib, NULL);
34540 +       au_xino_unlock_dir(&ldir);
34541 +       err = PTR_ERR(file);
34542 +       if (IS_ERR(file))
34543 +               goto out;
34544 +       fput(sbinfo->si_xib);
34545 +       sbinfo->si_xib = file;
34546 +
34547 +       p = sbinfo->si_xib_buf;
34548 +       memset(p, 0, PAGE_SIZE);
34549 +       pos = 0;
34550 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
34551 +       if (unlikely(sz != PAGE_SIZE)) {
34552 +               err = sz;
34553 +               AuIOErr("err %d\n", err);
34554 +               if (sz >= 0)
34555 +                       err = -EIO;
34556 +               goto out;
34557 +       }
34558 +
34559 +       mutex_lock(&sbinfo->si_xib_mtx);
34560 +       /* mnt_want_write() is unnecessary here */
34561 +       err = xib_restore(sb);
34562 +       mutex_unlock(&sbinfo->si_xib_mtx);
34563 +
34564 +out:
34565 +       return err;
34566 +}
34567 +
34568 +/* ---------------------------------------------------------------------- */
34569 +
34570 +/*
34571 + * xino mount option handlers
34572 + */
34573 +static au_readf_t find_readf(struct file *h_file)
34574 +{
34575 +       const struct file_operations *fop = h_file->f_op;
34576 +
34577 +       if (fop->read)
34578 +               return fop->read;
34579 +       if (fop->aio_read)
34580 +               return do_sync_read;
34581 +       if (fop->read_iter)
34582 +               return new_sync_read;
34583 +       return ERR_PTR(-ENOSYS);
34584 +}
34585 +
34586 +static au_writef_t find_writef(struct file *h_file)
34587 +{
34588 +       const struct file_operations *fop = h_file->f_op;
34589 +
34590 +       if (fop->write)
34591 +               return fop->write;
34592 +       if (fop->aio_write)
34593 +               return do_sync_write;
34594 +       if (fop->write_iter)
34595 +               return new_sync_write;
34596 +       return ERR_PTR(-ENOSYS);
34597 +}
34598 +
34599 +/* xino bitmap */
34600 +static void xino_clear_xib(struct super_block *sb)
34601 +{
34602 +       struct au_sbinfo *sbinfo;
34603 +
34604 +       SiMustWriteLock(sb);
34605 +
34606 +       sbinfo = au_sbi(sb);
34607 +       sbinfo->si_xread = NULL;
34608 +       sbinfo->si_xwrite = NULL;
34609 +       if (sbinfo->si_xib)
34610 +               fput(sbinfo->si_xib);
34611 +       sbinfo->si_xib = NULL;
34612 +       free_page((unsigned long)sbinfo->si_xib_buf);
34613 +       sbinfo->si_xib_buf = NULL;
34614 +}
34615 +
34616 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
34617 +{
34618 +       int err;
34619 +       loff_t pos;
34620 +       struct au_sbinfo *sbinfo;
34621 +       struct file *file;
34622 +
34623 +       SiMustWriteLock(sb);
34624 +
34625 +       sbinfo = au_sbi(sb);
34626 +       file = au_xino_create2(base, sbinfo->si_xib);
34627 +       err = PTR_ERR(file);
34628 +       if (IS_ERR(file))
34629 +               goto out;
34630 +       if (sbinfo->si_xib)
34631 +               fput(sbinfo->si_xib);
34632 +       sbinfo->si_xib = file;
34633 +       sbinfo->si_xread = find_readf(file);
34634 +       sbinfo->si_xwrite = find_writef(file);
34635 +
34636 +       err = -ENOMEM;
34637 +       if (!sbinfo->si_xib_buf)
34638 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
34639 +       if (unlikely(!sbinfo->si_xib_buf))
34640 +               goto out_unset;
34641 +
34642 +       sbinfo->si_xib_last_pindex = 0;
34643 +       sbinfo->si_xib_next_bit = 0;
34644 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
34645 +               pos = 0;
34646 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
34647 +                                 PAGE_SIZE, &pos);
34648 +               if (unlikely(err != PAGE_SIZE))
34649 +                       goto out_free;
34650 +       }
34651 +       err = 0;
34652 +       goto out; /* success */
34653 +
34654 +out_free:
34655 +       free_page((unsigned long)sbinfo->si_xib_buf);
34656 +       sbinfo->si_xib_buf = NULL;
34657 +       if (err >= 0)
34658 +               err = -EIO;
34659 +out_unset:
34660 +       fput(sbinfo->si_xib);
34661 +       sbinfo->si_xib = NULL;
34662 +       sbinfo->si_xread = NULL;
34663 +       sbinfo->si_xwrite = NULL;
34664 +out:
34665 +       return err;
34666 +}
34667 +
34668 +/* xino for each branch */
34669 +static void xino_clear_br(struct super_block *sb)
34670 +{
34671 +       aufs_bindex_t bindex, bend;
34672 +       struct au_branch *br;
34673 +
34674 +       bend = au_sbend(sb);
34675 +       for (bindex = 0; bindex <= bend; bindex++) {
34676 +               br = au_sbr(sb, bindex);
34677 +               if (!br || !br->br_xino.xi_file)
34678 +                       continue;
34679 +
34680 +               fput(br->br_xino.xi_file);
34681 +               br->br_xino.xi_file = NULL;
34682 +       }
34683 +}
34684 +
34685 +static int au_xino_set_br(struct super_block *sb, struct file *base)
34686 +{
34687 +       int err;
34688 +       ino_t ino;
34689 +       aufs_bindex_t bindex, bend, bshared;
34690 +       struct {
34691 +               struct file *old, *new;
34692 +       } *fpair, *p;
34693 +       struct au_branch *br;
34694 +       struct inode *inode;
34695 +       au_writef_t writef;
34696 +
34697 +       SiMustWriteLock(sb);
34698 +
34699 +       err = -ENOMEM;
34700 +       bend = au_sbend(sb);
34701 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
34702 +       if (unlikely(!fpair))
34703 +               goto out;
34704 +
34705 +       inode = sb->s_root->d_inode;
34706 +       ino = AUFS_ROOT_INO;
34707 +       writef = au_sbi(sb)->si_xwrite;
34708 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
34709 +               br = au_sbr(sb, bindex);
34710 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
34711 +               if (bshared >= 0) {
34712 +                       /* shared xino */
34713 +                       *p = fpair[bshared];
34714 +                       get_file(p->new);
34715 +               }
34716 +
34717 +               if (!p->new) {
34718 +                       /* new xino */
34719 +                       p->old = br->br_xino.xi_file;
34720 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
34721 +                       err = PTR_ERR(p->new);
34722 +                       if (IS_ERR(p->new)) {
34723 +                               p->new = NULL;
34724 +                               goto out_pair;
34725 +                       }
34726 +               }
34727 +
34728 +               err = au_xino_do_write(writef, p->new,
34729 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
34730 +               if (unlikely(err))
34731 +                       goto out_pair;
34732 +       }
34733 +
34734 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
34735 +               br = au_sbr(sb, bindex);
34736 +               if (br->br_xino.xi_file)
34737 +                       fput(br->br_xino.xi_file);
34738 +               get_file(p->new);
34739 +               br->br_xino.xi_file = p->new;
34740 +       }
34741 +
34742 +out_pair:
34743 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
34744 +               if (p->new)
34745 +                       fput(p->new);
34746 +               else
34747 +                       break;
34748 +       kfree(fpair);
34749 +out:
34750 +       return err;
34751 +}
34752 +
34753 +void au_xino_clr(struct super_block *sb)
34754 +{
34755 +       struct au_sbinfo *sbinfo;
34756 +
34757 +       au_xigen_clr(sb);
34758 +       xino_clear_xib(sb);
34759 +       xino_clear_br(sb);
34760 +       sbinfo = au_sbi(sb);
34761 +       /* lvalue, do not call au_mntflags() */
34762 +       au_opt_clr(sbinfo->si_mntflags, XINO);
34763 +}
34764 +
34765 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
34766 +{
34767 +       int err, skip;
34768 +       struct dentry *parent, *cur_parent;
34769 +       struct qstr *dname, *cur_name;
34770 +       struct file *cur_xino;
34771 +       struct inode *dir;
34772 +       struct au_sbinfo *sbinfo;
34773 +
34774 +       SiMustWriteLock(sb);
34775 +
34776 +       err = 0;
34777 +       sbinfo = au_sbi(sb);
34778 +       parent = dget_parent(xino->file->f_path.dentry);
34779 +       if (remount) {
34780 +               skip = 0;
34781 +               dname = &xino->file->f_path.dentry->d_name;
34782 +               cur_xino = sbinfo->si_xib;
34783 +               if (cur_xino) {
34784 +                       cur_parent = dget_parent(cur_xino->f_path.dentry);
34785 +                       cur_name = &cur_xino->f_path.dentry->d_name;
34786 +                       skip = (cur_parent == parent
34787 +                               && au_qstreq(dname, cur_name));
34788 +                       dput(cur_parent);
34789 +               }
34790 +               if (skip)
34791 +                       goto out;
34792 +       }
34793 +
34794 +       au_opt_set(sbinfo->si_mntflags, XINO);
34795 +       dir = parent->d_inode;
34796 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
34797 +       /* mnt_want_write() is unnecessary here */
34798 +       err = au_xino_set_xib(sb, xino->file);
34799 +       if (!err)
34800 +               err = au_xigen_set(sb, xino->file);
34801 +       if (!err)
34802 +               err = au_xino_set_br(sb, xino->file);
34803 +       mutex_unlock(&dir->i_mutex);
34804 +       if (!err)
34805 +               goto out; /* success */
34806 +
34807 +       /* reset all */
34808 +       AuIOErr("failed creating xino(%d).\n", err);
34809 +       au_xigen_clr(sb);
34810 +       xino_clear_xib(sb);
34811 +
34812 +out:
34813 +       dput(parent);
34814 +       return err;
34815 +}
34816 +
34817 +/* ---------------------------------------------------------------------- */
34818 +
34819 +/*
34820 + * create a xinofile at the default place/path.
34821 + */
34822 +struct file *au_xino_def(struct super_block *sb)
34823 +{
34824 +       struct file *file;
34825 +       char *page, *p;
34826 +       struct au_branch *br;
34827 +       struct super_block *h_sb;
34828 +       struct path path;
34829 +       aufs_bindex_t bend, bindex, bwr;
34830 +
34831 +       br = NULL;
34832 +       bend = au_sbend(sb);
34833 +       bwr = -1;
34834 +       for (bindex = 0; bindex <= bend; bindex++) {
34835 +               br = au_sbr(sb, bindex);
34836 +               if (au_br_writable(br->br_perm)
34837 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
34838 +                       bwr = bindex;
34839 +                       break;
34840 +               }
34841 +       }
34842 +
34843 +       if (bwr >= 0) {
34844 +               file = ERR_PTR(-ENOMEM);
34845 +               page = (void *)__get_free_page(GFP_NOFS);
34846 +               if (unlikely(!page))
34847 +                       goto out;
34848 +               path.mnt = au_br_mnt(br);
34849 +               path.dentry = au_h_dptr(sb->s_root, bwr);
34850 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
34851 +               file = (void *)p;
34852 +               if (!IS_ERR(p)) {
34853 +                       strcat(p, "/" AUFS_XINO_FNAME);
34854 +                       AuDbg("%s\n", p);
34855 +                       file = au_xino_create(sb, p, /*silent*/0);
34856 +                       if (!IS_ERR(file))
34857 +                               au_xino_brid_set(sb, br->br_id);
34858 +               }
34859 +               free_page((unsigned long)page);
34860 +       } else {
34861 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
34862 +               if (IS_ERR(file))
34863 +                       goto out;
34864 +               h_sb = file->f_path.dentry->d_sb;
34865 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
34866 +                       pr_err("xino doesn't support %s(%s)\n",
34867 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
34868 +                       fput(file);
34869 +                       file = ERR_PTR(-EINVAL);
34870 +               }
34871 +               if (!IS_ERR(file))
34872 +                       au_xino_brid_set(sb, -1);
34873 +       }
34874 +
34875 +out:
34876 +       return file;
34877 +}
34878 +
34879 +/* ---------------------------------------------------------------------- */
34880 +
34881 +int au_xino_path(struct seq_file *seq, struct file *file)
34882 +{
34883 +       int err;
34884 +
34885 +       err = au_seq_path(seq, &file->f_path);
34886 +       if (unlikely(err < 0))
34887 +               goto out;
34888 +
34889 +       err = 0;
34890 +#define Deleted "\\040(deleted)"
34891 +       seq->count -= sizeof(Deleted) - 1;
34892 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
34893 +                        sizeof(Deleted) - 1));
34894 +#undef Deleted
34895 +
34896 +out:
34897 +       return err;
34898 +}
34899 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
34900 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
34901 +++ linux/include/uapi/linux/aufs_type.h        2015-06-22 08:29:23.710091096 +0200
34902 @@ -0,0 +1,419 @@
34903 +/*
34904 + * Copyright (C) 2005-2015 Junjiro R. Okajima
34905 + *
34906 + * This program, aufs is free software; you can redistribute it and/or modify
34907 + * it under the terms of the GNU General Public License as published by
34908 + * the Free Software Foundation; either version 2 of the License, or
34909 + * (at your option) any later version.
34910 + *
34911 + * This program is distributed in the hope that it will be useful,
34912 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34913 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34914 + * GNU General Public License for more details.
34915 + *
34916 + * You should have received a copy of the GNU General Public License
34917 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34918 + */
34919 +
34920 +#ifndef __AUFS_TYPE_H__
34921 +#define __AUFS_TYPE_H__
34922 +
34923 +#define AUFS_NAME      "aufs"
34924 +
34925 +#ifdef __KERNEL__
34926 +/*
34927 + * define it before including all other headers.
34928 + * sched.h may use pr_* macros before defining "current", so define the
34929 + * no-current version first, and re-define later.
34930 + */
34931 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
34932 +#include <linux/sched.h>
34933 +#undef pr_fmt
34934 +#define pr_fmt(fmt) \
34935 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
34936 +               (int)sizeof(current->comm), current->comm, current->pid
34937 +#else
34938 +#include <stdint.h>
34939 +#include <sys/types.h>
34940 +#endif /* __KERNEL__ */
34941 +
34942 +#include <linux/limits.h>
34943 +
34944 +#define AUFS_VERSION   "3.x-rcN-20150622"
34945 +
34946 +/* todo? move this to linux-2.6.19/include/magic.h */
34947 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
34948 +
34949 +/* ---------------------------------------------------------------------- */
34950 +
34951 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
34952 +typedef int8_t aufs_bindex_t;
34953 +#define AUFS_BRANCH_MAX 127
34954 +#else
34955 +typedef int16_t aufs_bindex_t;
34956 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
34957 +#define AUFS_BRANCH_MAX 511
34958 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
34959 +#define AUFS_BRANCH_MAX 1023
34960 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
34961 +#define AUFS_BRANCH_MAX 32767
34962 +#endif
34963 +#endif
34964 +
34965 +#ifdef __KERNEL__
34966 +#ifndef AUFS_BRANCH_MAX
34967 +#error unknown CONFIG_AUFS_BRANCH_MAX value
34968 +#endif
34969 +#endif /* __KERNEL__ */
34970 +
34971 +/* ---------------------------------------------------------------------- */
34972 +
34973 +#define AUFS_FSTYPE            AUFS_NAME
34974 +
34975 +#define AUFS_ROOT_INO          2
34976 +#define AUFS_FIRST_INO         11
34977 +
34978 +#define AUFS_WH_PFX            ".wh."
34979 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
34980 +#define AUFS_WH_TMP_LEN                4
34981 +/* a limit for rmdir/rename a dir and copyup */
34982 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
34983 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
34984 +                               - 1                     /* dot */\
34985 +                               - AUFS_WH_TMP_LEN)      /* hex */
34986 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
34987 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
34988 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
34989 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
34990 +#define AUFS_DIRWH_DEF         3
34991 +#define AUFS_RDCACHE_DEF       10 /* seconds */
34992 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
34993 +#define AUFS_RDBLK_DEF         512 /* bytes */
34994 +#define AUFS_RDHASH_DEF                32
34995 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
34996 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
34997 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
34998 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
34999 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
35000 +
35001 +/* pseudo-link maintenace under /proc */
35002 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
35003 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
35004 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
35005 +
35006 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
35007 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
35008 +
35009 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
35010 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
35011 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
35012 +
35013 +/* doubly whiteouted */
35014 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
35015 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
35016 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
35017 +
35018 +/* branch permissions and attributes */
35019 +#define AUFS_BRPERM_RW         "rw"
35020 +#define AUFS_BRPERM_RO         "ro"
35021 +#define AUFS_BRPERM_RR         "rr"
35022 +#define AUFS_BRATTR_COO_REG    "coo_reg"
35023 +#define AUFS_BRATTR_COO_ALL    "coo_all"
35024 +#define AUFS_BRATTR_FHSM       "fhsm"
35025 +#define AUFS_BRATTR_UNPIN      "unpin"
35026 +#define AUFS_BRATTR_ICEX       "icex"
35027 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
35028 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
35029 +#define AUFS_BRATTR_ICEX_TR    "icextr"
35030 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
35031 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
35032 +#define AUFS_BRRATTR_WH                "wh"
35033 +#define AUFS_BRWATTR_NLWH      "nolwh"
35034 +#define AUFS_BRWATTR_MOO       "moo"
35035 +
35036 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
35037 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
35038 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
35039 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
35040 +
35041 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
35042 +#define AuBrAttr_COO_ALL       (1 << 4)
35043 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
35044 +
35045 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
35046 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
35047 +                                                  branch. meaningless since
35048 +                                                  linux-3.18-rc1 */
35049 +
35050 +/* ignore error in copying XATTR */
35051 +#define AuBrAttr_ICEX_SEC      (1 << 7)
35052 +#define AuBrAttr_ICEX_SYS      (1 << 8)
35053 +#define AuBrAttr_ICEX_TR       (1 << 9)
35054 +#define AuBrAttr_ICEX_USR      (1 << 10)
35055 +#define AuBrAttr_ICEX_OTH      (1 << 11)
35056 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
35057 +                                | AuBrAttr_ICEX_SYS    \
35058 +                                | AuBrAttr_ICEX_TR     \
35059 +                                | AuBrAttr_ICEX_USR    \
35060 +                                | AuBrAttr_ICEX_OTH)
35061 +
35062 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
35063 +#define AuBrRAttr_Mask         AuBrRAttr_WH
35064 +
35065 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
35066 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
35067 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
35068 +
35069 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
35070 +
35071 +/* #warning test userspace */
35072 +#ifdef __KERNEL__
35073 +#ifndef CONFIG_AUFS_FHSM
35074 +#undef AuBrAttr_FHSM
35075 +#define AuBrAttr_FHSM          0
35076 +#endif
35077 +#ifndef CONFIG_AUFS_XATTR
35078 +#undef AuBrAttr_ICEX
35079 +#define AuBrAttr_ICEX          0
35080 +#undef AuBrAttr_ICEX_SEC
35081 +#define AuBrAttr_ICEX_SEC      0
35082 +#undef AuBrAttr_ICEX_SYS
35083 +#define AuBrAttr_ICEX_SYS      0
35084 +#undef AuBrAttr_ICEX_TR
35085 +#define AuBrAttr_ICEX_TR       0
35086 +#undef AuBrAttr_ICEX_USR
35087 +#define AuBrAttr_ICEX_USR      0
35088 +#undef AuBrAttr_ICEX_OTH
35089 +#define AuBrAttr_ICEX_OTH      0
35090 +#endif
35091 +#endif
35092 +
35093 +/* the longest combination */
35094 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
35095 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
35096 +                              "+" AUFS_BRATTR_COO_REG          \
35097 +                              "+" AUFS_BRATTR_FHSM             \
35098 +                              "+" AUFS_BRATTR_UNPIN            \
35099 +                              "+" AUFS_BRATTR_ICEX_SEC         \
35100 +                              "+" AUFS_BRATTR_ICEX_SYS         \
35101 +                              "+" AUFS_BRATTR_ICEX_USR         \
35102 +                              "+" AUFS_BRATTR_ICEX_OTH         \
35103 +                              "+" AUFS_BRWATTR_NLWH)
35104 +
35105 +typedef struct {
35106 +       char a[AuBrPermStrSz];
35107 +} au_br_perm_str_t;
35108 +
35109 +static inline int au_br_writable(int brperm)
35110 +{
35111 +       return brperm & AuBrPerm_RW;
35112 +}
35113 +
35114 +static inline int au_br_whable(int brperm)
35115 +{
35116 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
35117 +}
35118 +
35119 +static inline int au_br_wh_linkable(int brperm)
35120 +{
35121 +       return !(brperm & AuBrWAttr_NoLinkWH);
35122 +}
35123 +
35124 +static inline int au_br_cmoo(int brperm)
35125 +{
35126 +       return brperm & AuBrAttr_CMOO_Mask;
35127 +}
35128 +
35129 +static inline int au_br_fhsm(int brperm)
35130 +{
35131 +       return brperm & AuBrAttr_FHSM;
35132 +}
35133 +
35134 +/* ---------------------------------------------------------------------- */
35135 +
35136 +/* ioctl */
35137 +enum {
35138 +       /* readdir in userspace */
35139 +       AuCtl_RDU,
35140 +       AuCtl_RDU_INO,
35141 +
35142 +       AuCtl_WBR_FD,   /* pathconf wrapper */
35143 +       AuCtl_IBUSY,    /* busy inode */
35144 +       AuCtl_MVDOWN,   /* move-down */
35145 +       AuCtl_BR,       /* info about branches */
35146 +       AuCtl_FHSM_FD   /* connection for fhsm */
35147 +};
35148 +
35149 +/* borrowed from linux/include/linux/kernel.h */
35150 +#ifndef ALIGN
35151 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
35152 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
35153 +#endif
35154 +
35155 +/* borrowed from linux/include/linux/compiler-gcc3.h */
35156 +#ifndef __aligned
35157 +#define __aligned(x)                   __attribute__((aligned(x)))
35158 +#endif
35159 +
35160 +#ifdef __KERNEL__
35161 +#ifndef __packed
35162 +#define __packed                       __attribute__((packed))
35163 +#endif
35164 +#endif
35165 +
35166 +struct au_rdu_cookie {
35167 +       uint64_t        h_pos;
35168 +       int16_t         bindex;
35169 +       uint8_t         flags;
35170 +       uint8_t         pad;
35171 +       uint32_t        generation;
35172 +} __aligned(8);
35173 +
35174 +struct au_rdu_ent {
35175 +       uint64_t        ino;
35176 +       int16_t         bindex;
35177 +       uint8_t         type;
35178 +       uint8_t         nlen;
35179 +       uint8_t         wh;
35180 +       char            name[0];
35181 +} __aligned(8);
35182 +
35183 +static inline int au_rdu_len(int nlen)
35184 +{
35185 +       /* include the terminating NULL */
35186 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
35187 +                    sizeof(uint64_t));
35188 +}
35189 +
35190 +union au_rdu_ent_ul {
35191 +       struct au_rdu_ent __user        *e;
35192 +       uint64_t                        ul;
35193 +};
35194 +
35195 +enum {
35196 +       AufsCtlRduV_SZ,
35197 +       AufsCtlRduV_End
35198 +};
35199 +
35200 +struct aufs_rdu {
35201 +       /* input */
35202 +       union {
35203 +               uint64_t        sz;     /* AuCtl_RDU */
35204 +               uint64_t        nent;   /* AuCtl_RDU_INO */
35205 +       };
35206 +       union au_rdu_ent_ul     ent;
35207 +       uint16_t                verify[AufsCtlRduV_End];
35208 +
35209 +       /* input/output */
35210 +       uint32_t                blk;
35211 +
35212 +       /* output */
35213 +       union au_rdu_ent_ul     tail;
35214 +       /* number of entries which were added in a single call */
35215 +       uint64_t                rent;
35216 +       uint8_t                 full;
35217 +       uint8_t                 shwh;
35218 +
35219 +       struct au_rdu_cookie    cookie;
35220 +} __aligned(8);
35221 +
35222 +/* ---------------------------------------------------------------------- */
35223 +
35224 +struct aufs_wbr_fd {
35225 +       uint32_t        oflags;
35226 +       int16_t         brid;
35227 +} __aligned(8);
35228 +
35229 +/* ---------------------------------------------------------------------- */
35230 +
35231 +struct aufs_ibusy {
35232 +       uint64_t        ino, h_ino;
35233 +       int16_t         bindex;
35234 +} __aligned(8);
35235 +
35236 +/* ---------------------------------------------------------------------- */
35237 +
35238 +/* error code for move-down */
35239 +/* the actual message strings are implemented in aufs-util.git */
35240 +enum {
35241 +       EAU_MVDOWN_OPAQUE = 1,
35242 +       EAU_MVDOWN_WHITEOUT,
35243 +       EAU_MVDOWN_UPPER,
35244 +       EAU_MVDOWN_BOTTOM,
35245 +       EAU_MVDOWN_NOUPPER,
35246 +       EAU_MVDOWN_NOLOWERBR,
35247 +       EAU_Last
35248 +};
35249 +
35250 +/* flags for move-down */
35251 +#define AUFS_MVDOWN_DMSG       1
35252 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
35253 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
35254 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
35255 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
35256 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
35257 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
35258 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
35259 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
35260 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
35261 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
35262 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
35263 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
35264 +
35265 +/* index for move-down */
35266 +enum {
35267 +       AUFS_MVDOWN_UPPER,
35268 +       AUFS_MVDOWN_LOWER,
35269 +       AUFS_MVDOWN_NARRAY
35270 +};
35271 +
35272 +/*
35273 + * additional info of move-down
35274 + * number of free blocks and inodes.
35275 + * subset of struct kstatfs, but smaller and always 64bit.
35276 + */
35277 +struct aufs_stfs {
35278 +       uint64_t        f_blocks;
35279 +       uint64_t        f_bavail;
35280 +       uint64_t        f_files;
35281 +       uint64_t        f_ffree;
35282 +};
35283 +
35284 +struct aufs_stbr {
35285 +       int16_t                 brid;   /* optional input */
35286 +       int16_t                 bindex; /* output */
35287 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
35288 +} __aligned(8);
35289 +
35290 +struct aufs_mvdown {
35291 +       uint32_t                flags;                  /* input/output */
35292 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
35293 +       int8_t                  au_errno;               /* output */
35294 +} __aligned(8);
35295 +
35296 +/* ---------------------------------------------------------------------- */
35297 +
35298 +union aufs_brinfo {
35299 +       /* PATH_MAX may differ between kernel-space and user-space */
35300 +       char    _spacer[4096];
35301 +       struct {
35302 +               int16_t id;
35303 +               int     perm;
35304 +               char    path[0];
35305 +       };
35306 +} __aligned(8);
35307 +
35308 +/* ---------------------------------------------------------------------- */
35309 +
35310 +#define AuCtlType              'A'
35311 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
35312 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
35313 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
35314 +                                    struct aufs_wbr_fd)
35315 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
35316 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
35317 +                                     struct aufs_mvdown)
35318 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
35319 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
35320 +
35321 +#endif /* __AUFS_TYPE_H__ */
This page took 2.747508 seconds and 3 git commands to generate.