]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs3.patch
- up to 4.0.0, builds, untested
[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 1de6afa..082a271 100644
40 --- a/MAINTAINERS
41 +++ b/MAINTAINERS
42 @@ -1865,6 +1865,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/splice.h b/include/linux/splice.h
134 index da2751d..2e0fca6 100644
135 --- a/include/linux/splice.h
136 +++ b/include/linux/splice.h
137 @@ -83,4 +83,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
138  extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
139  
140  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
141 +
142 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
143 +                          loff_t *ppos, size_t len, unsigned int flags);
144 +extern long do_splice_to(struct file *in, loff_t *ppos,
145 +                        struct pipe_inode_info *pipe, size_t len,
146 +                        unsigned int flags);
147  #endif
148 aufs3.x-rcN mmap patch
149
150 diff --git a/fs/buffer.c b/fs/buffer.c
151 index 20805db..363569f 100644
152 --- a/fs/buffer.c
153 +++ b/fs/buffer.c
154 @@ -2450,7 +2450,7 @@ int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
155          * Update file times before taking page lock. We may end up failing the
156          * fault so this update may be superfluous but who really cares...
157          */
158 -       file_update_time(vma->vm_file);
159 +       vma_file_update_time(vma);
160  
161         ret = __block_page_mkwrite(vma, vmf, get_block);
162         sb_end_pagefault(sb);
163 diff --git a/fs/proc/base.c b/fs/proc/base.c
164 index 3f3d7ae..426bcc7 100644
165 --- a/fs/proc/base.c
166 +++ b/fs/proc/base.c
167 @@ -1735,7 +1735,7 @@ static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
168         down_read(&mm->mmap_sem);
169         vma = find_exact_vma(mm, vm_start, vm_end);
170         if (vma && vma->vm_file) {
171 -               *path = vma->vm_file->f_path;
172 +               *path = vma_pr_or_file(vma)->f_path;
173                 path_get(path);
174                 rc = 0;
175         }
176 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
177 index d4a3574..1397181 100644
178 --- a/fs/proc/nommu.c
179 +++ b/fs/proc/nommu.c
180 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
181         file = region->vm_file;
182  
183         if (file) {
184 -               struct inode *inode = file_inode(region->vm_file);
185 +               struct inode *inode;
186 +
187 +               file = vmr_pr_or_file(region);
188 +               inode = file_inode(file);
189                 dev = inode->i_sb->s_dev;
190                 ino = inode->i_ino;
191         }
192 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
193 index 6dee68d..9afa35d 100644
194 --- a/fs/proc/task_mmu.c
195 +++ b/fs/proc/task_mmu.c
196 @@ -279,7 +279,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
197         const char *name = NULL;
198  
199         if (file) {
200 -               struct inode *inode = file_inode(vma->vm_file);
201 +               struct inode *inode;
202 +
203 +               file = vma_pr_or_file(vma);
204 +               inode = file_inode(file);
205                 dev = inode->i_sb->s_dev;
206                 ino = inode->i_ino;
207                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
208 @@ -1479,7 +1482,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
209         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
210         struct vm_area_struct *vma = v;
211         struct numa_maps *md = &numa_priv->md;
212 -       struct file *file = vma->vm_file;
213 +       struct file *file = vma_pr_or_file(vma);
214         struct mm_struct *mm = vma->vm_mm;
215         struct mm_walk walk = {
216                 .hugetlb_entry = gather_hugetlb_stats,
217 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
218 index 599ec2e..de6cd6e 100644
219 --- a/fs/proc/task_nommu.c
220 +++ b/fs/proc/task_nommu.c
221 @@ -160,7 +160,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
222         file = vma->vm_file;
223  
224         if (file) {
225 -               struct inode *inode = file_inode(vma->vm_file);
226 +               struct inode *inode;
227 +
228 +               file = vma_pr_or_file(file);
229 +               inode = file_inode(file);
230                 dev = inode->i_sb->s_dev;
231                 ino = inode->i_ino;
232                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
233 diff --git a/include/linux/mm.h b/include/linux/mm.h
234 index 47a9392..60bc29c 100644
235 --- a/include/linux/mm.h
236 +++ b/include/linux/mm.h
237 @@ -1254,6 +1254,28 @@ static inline int fixup_user_fault(struct task_struct *tsk,
238  }
239  #endif
240  
241 +#ifdef CONFIG_MMU
242 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
243 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
244 +                                     int);
245 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
246 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
247 +
248 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
249 +                                                               __LINE__)
250 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
251 +                                                         __LINE__)
252 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
253 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
254 +#else
255 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
256 +extern void vmr_do_fput(struct vm_region *, const char[], int);
257 +
258 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
259 +                                                         __LINE__)
260 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
261 +#endif /* CONFIG_MMU */
262 +
263  extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
264  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
265                 void *buf, int len, int write);
266 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
267 index 199a03a..ed38ea3 100644
268 --- a/include/linux/mm_types.h
269 +++ b/include/linux/mm_types.h
270 @@ -241,6 +241,7 @@ struct vm_region {
271         unsigned long   vm_top;         /* region allocated to here */
272         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
273         struct file     *vm_file;       /* the backing file or NULL */
274 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
275  
276         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
277         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
278 @@ -305,6 +306,7 @@ struct vm_area_struct {
279         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
280                                            units, *not* PAGE_CACHE_SIZE */
281         struct file * vm_file;          /* File we map to (can be NULL). */
282 +       struct file *vm_prfile;         /* shadow of vm_file */
283         void * vm_private_data;         /* was vm_pte (shared mem) */
284  
285  #ifndef CONFIG_MMU
286 diff --git a/kernel/fork.c b/kernel/fork.c
287 index cf65139..f28a048 100644
288 --- a/kernel/fork.c
289 +++ b/kernel/fork.c
290 @@ -430,7 +430,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
291                         struct inode *inode = file_inode(file);
292                         struct address_space *mapping = file->f_mapping;
293  
294 -                       get_file(file);
295 +                       vma_get_file(tmp);
296                         if (tmp->vm_flags & VM_DENYWRITE)
297                                 atomic_dec(&inode->i_writecount);
298                         i_mmap_lock_write(mapping);
299 diff --git a/mm/Makefile b/mm/Makefile
300 index 15dbe99..88e25b5 100644
301 --- a/mm/Makefile
302 +++ b/mm/Makefile
303 @@ -21,7 +21,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o \
304                            mm_init.o mmu_context.o percpu.o slab_common.o \
305                            compaction.o vmacache.o \
306                            interval_tree.o list_lru.o workingset.o \
307 -                          debug.o $(mmu-y)
308 +                          prfile.o debug.o $(mmu-y)
309  
310  obj-y += init-mm.o
311  
312 diff --git a/mm/filemap.c b/mm/filemap.c
313 index ad72420..8885258 100644
314 --- a/mm/filemap.c
315 +++ b/mm/filemap.c
316 @@ -2064,7 +2064,7 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
317         int ret = VM_FAULT_LOCKED;
318  
319         sb_start_pagefault(inode->i_sb);
320 -       file_update_time(vma->vm_file);
321 +       vma_file_update_time(vma);
322         lock_page(page);
323         if (page->mapping != inode->i_mapping) {
324                 unlock_page(page);
325 diff --git a/mm/madvise.c b/mm/madvise.c
326 index d551475..1ebf71b 100644
327 --- a/mm/madvise.c
328 +++ b/mm/madvise.c
329 @@ -320,12 +320,12 @@ static long madvise_remove(struct vm_area_struct *vma,
330          * vma's reference to the file) can go away as soon as we drop
331          * mmap_sem.
332          */
333 -       get_file(f);
334 +       vma_get_file(vma);
335         up_read(&current->mm->mmap_sem);
336         error = vfs_fallocate(f,
337                                 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
338                                 offset, end - start);
339 -       fput(f);
340 +       vma_fput(vma);
341         down_read(&current->mm->mmap_sem);
342         return error;
343  }
344 diff --git a/mm/memory.c b/mm/memory.c
345 index 97839f5..5e5d491 100644
346 --- a/mm/memory.c
347 +++ b/mm/memory.c
348 @@ -2132,7 +2132,7 @@ reuse:
349                         }
350  
351                         if (!page_mkwrite)
352 -                               file_update_time(vma->vm_file);
353 +                               vma_file_update_time(vma);
354                 }
355  
356                 return ret;
357 diff --git a/mm/mmap.c b/mm/mmap.c
358 index 9ec50a3..4a6a641 100644
359 --- a/mm/mmap.c
360 +++ b/mm/mmap.c
361 @@ -274,7 +274,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
362         if (vma->vm_ops && vma->vm_ops->close)
363                 vma->vm_ops->close(vma);
364         if (vma->vm_file)
365 -               fput(vma->vm_file);
366 +               vma_fput(vma);
367         mpol_put(vma_policy(vma));
368         kmem_cache_free(vm_area_cachep, vma);
369         return next;
370 @@ -886,7 +886,7 @@ again:                      remove_next = 1 + (end > next->vm_end);
371         if (remove_next) {
372                 if (file) {
373                         uprobe_munmap(next, next->vm_start, next->vm_end);
374 -                       fput(file);
375 +                       vma_fput(vma);
376                 }
377                 if (next->anon_vma)
378                         anon_vma_merge(vma, next);
379 @@ -1671,8 +1671,8 @@ out:
380         return addr;
381  
382  unmap_and_free_vma:
383 +       vma_fput(vma);
384         vma->vm_file = NULL;
385 -       fput(file);
386  
387         /* Undo any partial mapping done by a device driver. */
388         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
389 @@ -2473,7 +2473,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
390                 goto out_free_mpol;
391  
392         if (new->vm_file)
393 -               get_file(new->vm_file);
394 +               vma_get_file(new);
395  
396         if (new->vm_ops && new->vm_ops->open)
397                 new->vm_ops->open(new);
398 @@ -2492,7 +2492,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
399         if (new->vm_ops && new->vm_ops->close)
400                 new->vm_ops->close(new);
401         if (new->vm_file)
402 -               fput(new->vm_file);
403 +               vma_fput(new);
404         unlink_anon_vmas(new);
405   out_free_mpol:
406         mpol_put(vma_policy(new));
407 @@ -2635,7 +2635,6 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
408         struct vm_area_struct *vma;
409         unsigned long populate = 0;
410         unsigned long ret = -EINVAL;
411 -       struct file *file;
412  
413         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. "
414                         "See Documentation/vm/remap_file_pages.txt.\n",
415 @@ -2679,10 +2678,10 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
416                 munlock_vma_pages_range(vma, start, start + size);
417         }
418  
419 -       file = get_file(vma->vm_file);
420 +       vma_get_file(vma);
421         ret = do_mmap_pgoff(vma->vm_file, start, size,
422                         prot, flags, pgoff, &populate);
423 -       fput(file);
424 +       vma_fput(vma);
425  out:
426         up_write(&mm->mmap_sem);
427         if (populate)
428 @@ -2950,7 +2949,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
429                         if (anon_vma_clone(new_vma, vma))
430                                 goto out_free_mempol;
431                         if (new_vma->vm_file)
432 -                               get_file(new_vma->vm_file);
433 +                               vma_get_file(new_vma);
434                         if (new_vma->vm_ops && new_vma->vm_ops->open)
435                                 new_vma->vm_ops->open(new_vma);
436                         vma_link(mm, new_vma, prev, rb_link, rb_parent);
437 diff --git a/mm/msync.c b/mm/msync.c
438 index bb04d53..5c24c54 100644
439 --- a/mm/msync.c
440 +++ b/mm/msync.c
441 @@ -84,10 +84,10 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
442                 start = vma->vm_end;
443                 if ((flags & MS_SYNC) && file &&
444                                 (vma->vm_flags & VM_SHARED)) {
445 -                       get_file(file);
446 +                       vma_get_file(vma);
447                         up_read(&mm->mmap_sem);
448                         error = vfs_fsync_range(file, fstart, fend, 1);
449 -                       fput(file);
450 +                       vma_fput(vma);
451                         if (error || start >= end)
452                                 goto out;
453                         down_read(&mm->mmap_sem);
454 diff --git a/mm/nommu.c b/mm/nommu.c
455 index 3fba2dc..870ce96 100644
456 --- a/mm/nommu.c
457 +++ b/mm/nommu.c
458 @@ -693,7 +693,7 @@ static void __put_nommu_region(struct vm_region *region)
459                 up_write(&nommu_region_sem);
460  
461                 if (region->vm_file)
462 -                       fput(region->vm_file);
463 +                       vmr_fput(region);
464  
465                 /* IO memory and memory shared directly out of the pagecache
466                  * from ramfs/tmpfs mustn't be released here */
467 @@ -858,7 +858,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
468         if (vma->vm_ops && vma->vm_ops->close)
469                 vma->vm_ops->close(vma);
470         if (vma->vm_file)
471 -               fput(vma->vm_file);
472 +               vma_fput(vma);
473         put_nommu_region(vma->vm_region);
474         kmem_cache_free(vm_area_cachep, vma);
475  }
476 @@ -1398,7 +1398,7 @@ unsigned long do_mmap_pgoff(struct file *file,
477                                         goto error_just_free;
478                                 }
479                         }
480 -                       fput(region->vm_file);
481 +                       vmr_fput(region);
482                         kmem_cache_free(vm_region_jar, region);
483                         region = pregion;
484                         result = start;
485 @@ -1474,10 +1474,10 @@ error_just_free:
486         up_write(&nommu_region_sem);
487  error:
488         if (region->vm_file)
489 -               fput(region->vm_file);
490 +               vmr_fput(region);
491         kmem_cache_free(vm_region_jar, region);
492         if (vma->vm_file)
493 -               fput(vma->vm_file);
494 +               vma_fput(vma);
495         kmem_cache_free(vm_area_cachep, vma);
496         kleave(" = %d", ret);
497         return ret;
498 diff --git a/mm/prfile.c b/mm/prfile.c
499 new file mode 100644
500 index 0000000..6c145eb
501 --- /dev/null
502 +++ b/mm/prfile.c
503 @@ -0,0 +1,86 @@
504 +/*
505 + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
506 + * in /proc/PID/maps.
507 + * Call these functions via macros defined in linux/mm.h.
508 + *
509 + * See Documentation/filesystems/aufs/design/06mmap.txt
510 + *
511 + * Copyright (c) 2014 Junjro R. Okajima
512 + * Copyright (c) 2014 Ian Campbell
513 + */
514 +
515 +#include <linux/mm.h>
516 +#include <linux/file.h>
517 +#include <linux/fs.h>
518 +
519 +/* #define PRFILE_TRACE */
520 +static inline void prfile_trace(struct file *f, struct file *pr,
521 +                             const char func[], int line, const char func2[])
522 +{
523 +#ifdef PRFILE_TRACE
524 +       if (pr)
525 +               pr_info("%s:%d: %s, %p\n", func, line, func2,
526 +                       f ? (char *)f->f_path.dentry->d_name.name : "(null)");
527 +#endif
528 +}
529 +
530 +#ifdef CONFIG_MMU
531 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
532 +                            int line)
533 +{
534 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
535 +
536 +       prfile_trace(f, pr, func, line, __func__);
537 +       file_update_time(f);
538 +       if (f && pr)
539 +               file_update_time(pr);
540 +}
541 +
542 +struct file *vma_do_pr_or_file(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 +       return (f && pr) ? pr : f;
549 +}
550 +
551 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
552 +{
553 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
554 +
555 +       prfile_trace(f, pr, func, line, __func__);
556 +       get_file(f);
557 +       if (f && pr)
558 +               get_file(pr);
559 +}
560 +
561 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
562 +{
563 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
564 +
565 +       prfile_trace(f, pr, func, line, __func__);
566 +       fput(f);
567 +       if (f && pr)
568 +               fput(pr);
569 +}
570 +#else
571 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
572 +                              int line)
573 +{
574 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
575 +
576 +       prfile_trace(f, pr, func, line, __func__);
577 +       return (f && pr) ? pr : f;
578 +}
579 +
580 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
581 +{
582 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
583 +
584 +       prfile_trace(f, pr, func, line, __func__);
585 +       fput(f);
586 +       if (f && pr)
587 +               fput(pr);
588 +}
589 +#endif /* CONFIG_MMU */
590 aufs3.x-rcN standalone patch
591
592 diff --git a/fs/dcache.c b/fs/dcache.c
593 index b93a9f2..5a010ee 100644
594 --- a/fs/dcache.c
595 +++ b/fs/dcache.c
596 @@ -1235,6 +1235,7 @@ rename_retry:
597         seq = 1;
598         goto again;
599  }
600 +EXPORT_SYMBOL(d_walk);
601  
602  /*
603   * Search for at least 1 mount point in the dentry's subdirs.
604 diff --git a/fs/inode.c b/fs/inode.c
605 index f00b16f..eb7b8b5 100644
606 --- a/fs/inode.c
607 +++ b/fs/inode.c
608 @@ -58,6 +58,7 @@ static struct hlist_head *inode_hashtable __read_mostly;
609  static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
610  
611  __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
612 +EXPORT_SYMBOL(inode_sb_list_lock);
613  
614  /*
615   * Empty aops. Can be used for the cases where the user does not
616 diff --git a/fs/namespace.c b/fs/namespace.c
617 index 82ef140..a3aa4b9 100644
618 --- a/fs/namespace.c
619 +++ b/fs/namespace.c
620 @@ -463,6 +463,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
621         mnt_dec_writers(real_mount(mnt));
622         preempt_enable();
623  }
624 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
625  
626  /**
627   * mnt_drop_write - give up write access to a mount
628 @@ -1718,6 +1719,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
629         }
630         return 0;
631  }
632 +EXPORT_SYMBOL(iterate_mounts);
633  
634  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
635  {
636 diff --git a/fs/notify/group.c b/fs/notify/group.c
637 index d16b62c..06ca6bc 100644
638 --- a/fs/notify/group.c
639 +++ b/fs/notify/group.c
640 @@ -22,6 +22,7 @@
641  #include <linux/srcu.h>
642  #include <linux/rculist.h>
643  #include <linux/wait.h>
644 +#include <linux/module.h>
645  
646  #include <linux/fsnotify_backend.h>
647  #include "fsnotify.h"
648 @@ -72,6 +73,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
649  {
650         atomic_inc(&group->refcnt);
651  }
652 +EXPORT_SYMBOL(fsnotify_get_group);
653  
654  /*
655   * Drop a reference to a group.  Free it if it's through.
656 @@ -81,6 +83,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
657         if (atomic_dec_and_test(&group->refcnt))
658                 fsnotify_final_destroy_group(group);
659  }
660 +EXPORT_SYMBOL(fsnotify_put_group);
661  
662  /*
663   * Create a new fsnotify_group and hold a reference for the group returned.
664 @@ -109,6 +112,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
665  
666         return group;
667  }
668 +EXPORT_SYMBOL(fsnotify_alloc_group);
669  
670  int fsnotify_fasync(int fd, struct file *file, int on)
671  {
672 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
673 index 92e48c7..d2c4b68 100644
674 --- a/fs/notify/mark.c
675 +++ b/fs/notify/mark.c
676 @@ -109,6 +109,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
677                 mark->free_mark(mark);
678         }
679  }
680 +EXPORT_SYMBOL(fsnotify_put_mark);
681  
682  /* Calculate mask of events for a list of marks */
683  u32 fsnotify_recalc_mask(struct hlist_head *head)
684 @@ -202,6 +203,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
685         fsnotify_destroy_mark_locked(mark, group);
686         mutex_unlock(&group->mark_mutex);
687  }
688 +EXPORT_SYMBOL(fsnotify_destroy_mark);
689  
690  /*
691   * Destroy all marks in the given list. The marks must be already detached from
692 @@ -376,6 +378,7 @@ err:
693  
694         return ret;
695  }
696 +EXPORT_SYMBOL(fsnotify_add_mark);
697  
698  int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
699                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
700 @@ -455,6 +458,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
701         atomic_set(&mark->refcnt, 1);
702         mark->free_mark = free_mark;
703  }
704 +EXPORT_SYMBOL(fsnotify_init_mark);
705  
706  static int fsnotify_mark_destroy(void *ignored)
707  {
708 diff --git a/fs/open.c b/fs/open.c
709 index 33f9cbf..2c8bd72 100644
710 --- a/fs/open.c
711 +++ b/fs/open.c
712 @@ -62,6 +62,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
713         mutex_unlock(&dentry->d_inode->i_mutex);
714         return ret;
715  }
716 +EXPORT_SYMBOL(do_truncate);
717  
718  long vfs_truncate(struct path *path, loff_t length)
719  {
720 diff --git a/fs/splice.c b/fs/splice.c
721 index beb9993..813275a 100644
722 --- a/fs/splice.c
723 +++ b/fs/splice.c
724 @@ -1112,6 +1112,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
725  
726         return splice_write(pipe, out, ppos, len, flags);
727  }
728 +EXPORT_SYMBOL(do_splice_from);
729  
730  /*
731   * Attempt to initiate a splice from a file to a pipe.
732 @@ -1138,6 +1139,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
733  
734         return splice_read(in, ppos, pipe, len, flags);
735  }
736 +EXPORT_SYMBOL(do_splice_to);
737  
738  /**
739   * splice_direct_to_actor - splices data directly between two non-pipes
740 diff --git a/fs/xattr.c b/fs/xattr.c
741 index 4ef6985..6bb6303 100644
742 --- a/fs/xattr.c
743 +++ b/fs/xattr.c
744 @@ -207,6 +207,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
745         *xattr_value = value;
746         return error;
747  }
748 +EXPORT_SYMBOL(vfs_getxattr_alloc);
749  
750  /* Compare an extended attribute value with the given value */
751  int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
752 diff --git a/security/commoncap.c b/security/commoncap.c
753 index f66713b..fd4c4b8 100644
754 --- a/security/commoncap.c
755 +++ b/security/commoncap.c
756 @@ -975,9 +975,11 @@ int cap_mmap_addr(unsigned long addr)
757         }
758         return ret;
759  }
760 +EXPORT_SYMBOL(cap_mmap_addr);
761  
762  int cap_mmap_file(struct file *file, unsigned long reqprot,
763                   unsigned long prot, unsigned long flags)
764  {
765         return 0;
766  }
767 +EXPORT_SYMBOL(cap_mmap_file);
768 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
769 index 188c1d2..426d9af 100644
770 --- a/security/device_cgroup.c
771 +++ b/security/device_cgroup.c
772 @@ -7,6 +7,7 @@
773  #include <linux/device_cgroup.h>
774  #include <linux/cgroup.h>
775  #include <linux/ctype.h>
776 +#include <linux/export.h>
777  #include <linux/list.h>
778  #include <linux/uaccess.h>
779  #include <linux/seq_file.h>
780 @@ -849,6 +850,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
781         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
782                         access);
783  }
784 +EXPORT_SYMBOL(__devcgroup_inode_permission);
785  
786  int devcgroup_inode_mknod(int mode, dev_t dev)
787  {
788 diff --git a/security/security.c b/security/security.c
789 index e81d5bb..0b7b840 100644
790 --- a/security/security.c
791 +++ b/security/security.c
792 @@ -430,6 +430,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
793                 return 0;
794         return security_ops->path_rmdir(dir, dentry);
795  }
796 +EXPORT_SYMBOL(security_path_rmdir);
797  
798  int security_path_unlink(struct path *dir, struct dentry *dentry)
799  {
800 @@ -446,6 +447,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
801                 return 0;
802         return security_ops->path_symlink(dir, dentry, old_name);
803  }
804 +EXPORT_SYMBOL(security_path_symlink);
805  
806  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
807                        struct dentry *new_dentry)
808 @@ -454,6 +456,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
809                 return 0;
810         return security_ops->path_link(old_dentry, new_dir, new_dentry);
811  }
812 +EXPORT_SYMBOL(security_path_link);
813  
814  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
815                          struct path *new_dir, struct dentry *new_dentry,
816 @@ -481,6 +484,7 @@ int security_path_truncate(struct path *path)
817                 return 0;
818         return security_ops->path_truncate(path);
819  }
820 +EXPORT_SYMBOL(security_path_truncate);
821  
822  int security_path_chmod(struct path *path, umode_t mode)
823  {
824 @@ -488,6 +492,7 @@ int security_path_chmod(struct path *path, umode_t mode)
825                 return 0;
826         return security_ops->path_chmod(path, mode);
827  }
828 +EXPORT_SYMBOL(security_path_chmod);
829  
830  int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
831  {
832 @@ -495,6 +500,7 @@ int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
833                 return 0;
834         return security_ops->path_chown(path, uid, gid);
835  }
836 +EXPORT_SYMBOL(security_path_chown);
837  
838  int security_path_chroot(struct path *path)
839  {
840 @@ -580,6 +586,7 @@ int security_inode_readlink(struct dentry *dentry)
841                 return 0;
842         return security_ops->inode_readlink(dentry);
843  }
844 +EXPORT_SYMBOL(security_inode_readlink);
845  
846  int security_inode_follow_link(struct dentry *dentry, struct nameidata *nd)
847  {
848 @@ -594,6 +601,7 @@ int security_inode_permission(struct inode *inode, int mask)
849                 return 0;
850         return security_ops->inode_permission(inode, mask);
851  }
852 +EXPORT_SYMBOL(security_inode_permission);
853  
854  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
855  {
856 @@ -716,6 +724,7 @@ int security_file_permission(struct file *file, int mask)
857  
858         return fsnotify_perm(file, mask);
859  }
860 +EXPORT_SYMBOL(security_file_permission);
861  
862  int security_file_alloc(struct file *file)
863  {
864 @@ -775,6 +784,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
865                 return ret;
866         return ima_file_mmap(file, prot);
867  }
868 +EXPORT_SYMBOL(security_mmap_file);
869  
870  int security_mmap_addr(unsigned long addr)
871  {
872 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
873 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
874 +++ linux/Documentation/ABI/testing/debugfs-aufs        2015-04-13 15:10:20.773489965 +0200
875 @@ -0,0 +1,50 @@
876 +What:          /debug/aufs/si_<id>/
877 +Date:          March 2009
878 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
879 +Description:
880 +               Under /debug/aufs, a directory named si_<id> is created
881 +               per aufs mount, where <id> is a unique id generated
882 +               internally.
883 +
884 +What:          /debug/aufs/si_<id>/plink
885 +Date:          Apr 2013
886 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
887 +Description:
888 +               It has three lines and shows the information about the
889 +               pseudo-link. The first line is a single number
890 +               representing a number of buckets. The second line is a
891 +               number of pseudo-links per buckets (separated by a
892 +               blank). The last line is a single number representing a
893 +               total number of psedo-links.
894 +               When the aufs mount option 'noplink' is specified, it
895 +               will show "1\n0\n0\n".
896 +
897 +What:          /debug/aufs/si_<id>/xib
898 +Date:          March 2009
899 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
900 +Description:
901 +               It shows the consumed blocks by xib (External Inode Number
902 +               Bitmap), its block size and file size.
903 +               When the aufs mount option 'noxino' is specified, it
904 +               will be empty. About XINO files, see the aufs manual.
905 +
906 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
907 +Date:          March 2009
908 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
909 +Description:
910 +               It shows the consumed blocks by xino (External Inode Number
911 +               Translation Table), its link count, block size and file
912 +               size.
913 +               When the aufs mount option 'noxino' is specified, it
914 +               will be empty. About XINO files, see the aufs manual.
915 +
916 +What:          /debug/aufs/si_<id>/xigen
917 +Date:          March 2009
918 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
919 +Description:
920 +               It shows the consumed blocks by xigen (External Inode
921 +               Generation Table), its block size and file size.
922 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
923 +               be created.
924 +               When the aufs mount option 'noxino' is specified, it
925 +               will be empty. About XINO files, see the aufs manual.
926 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
927 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
928 +++ linux/Documentation/ABI/testing/sysfs-aufs  2015-04-13 15:10:20.773489965 +0200
929 @@ -0,0 +1,31 @@
930 +What:          /sys/fs/aufs/si_<id>/
931 +Date:          March 2009
932 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
933 +Description:
934 +               Under /sys/fs/aufs, a directory named si_<id> is created
935 +               per aufs mount, where <id> is a unique id generated
936 +               internally.
937 +
938 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
939 +Date:          March 2009
940 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
941 +Description:
942 +               It shows the abolute path of a member directory (which
943 +               is called branch) in aufs, and its permission.
944 +
945 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
946 +Date:          July 2013
947 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
948 +Description:
949 +               It shows the id of a member directory (which is called
950 +               branch) in aufs.
951 +
952 +What:          /sys/fs/aufs/si_<id>/xi_path
953 +Date:          March 2009
954 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
955 +Description:
956 +               It shows the abolute path of XINO (External Inode Number
957 +               Bitmap, Translation Table and Generation Table) file
958 +               even if it is the default path.
959 +               When the aufs mount option 'noxino' is specified, it
960 +               will be empty. About XINO files, see the aufs manual.
961 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
962 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
963 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2015-04-13 15:10:20.773489965 +0200
964 @@ -0,0 +1,170 @@
965 +
966 +# Copyright (C) 2005-2015 Junjiro R. Okajima
967 +# 
968 +# This program is free software; you can redistribute it and/or modify
969 +# it under the terms of the GNU General Public License as published by
970 +# the Free Software Foundation; either version 2 of the License, or
971 +# (at your option) any later version.
972 +# 
973 +# This program is distributed in the hope that it will be useful,
974 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
975 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
976 +# GNU General Public License for more details.
977 +# 
978 +# You should have received a copy of the GNU General Public License
979 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
980 +
981 +Introduction
982 +----------------------------------------
983 +
984 +aufs [ei ju: ef es] | [a u f s]
985 +1. abbrev. for "advanced multi-layered unification filesystem".
986 +2. abbrev. for "another unionfs".
987 +3. abbrev. for "auf das" in German which means "on the" in English.
988 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
989 +   But "Filesystem aufs Filesystem" is hard to understand.
990 +
991 +AUFS is a filesystem with features:
992 +- multi layered stackable unification filesystem, the member directory
993 +  is called as a branch.
994 +- branch permission and attribute, 'readonly', 'real-readonly',
995 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
996 +  combination.
997 +- internal "file copy-on-write".
998 +- logical deletion, whiteout.
999 +- dynamic branch manipulation, adding, deleting and changing permission.
1000 +- allow bypassing aufs, user's direct branch access.
1001 +- external inode number translation table and bitmap which maintains the
1002 +  persistent aufs inode number.
1003 +- seekable directory, including NFS readdir.
1004 +- file mapping, mmap and sharing pages.
1005 +- pseudo-link, hardlink over branches.
1006 +- loopback mounted filesystem as a branch.
1007 +- several policies to select one among multiple writable branches.
1008 +- revert a single systemcall when an error occurs in aufs.
1009 +- and more...
1010 +
1011 +
1012 +Multi Layered Stackable Unification Filesystem
1013 +----------------------------------------------------------------------
1014 +Most people already knows what it is.
1015 +It is a filesystem which unifies several directories and provides a
1016 +merged single directory. When users access a file, the access will be
1017 +passed/re-directed/converted (sorry, I am not sure which English word is
1018 +correct) to the real file on the member filesystem. The member
1019 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1020 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1021 +readonly branch is handled by creating 'whiteout' on the upper writable
1022 +branch.
1023 +
1024 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1025 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1026 +different approaches to implement the merged-view.
1027 +The former tries putting it into VFS, and the latter implements as a
1028 +separate filesystem.
1029 +(If I misunderstand about these implementations, please let me know and
1030 +I shall correct it. Because it is a long time ago when I read their
1031 +source files last time).
1032 +
1033 +UnionMount's approach will be able to small, but may be hard to share
1034 +branches between several UnionMount since the whiteout in it is
1035 +implemented in the inode on branch filesystem and always
1036 +shared. According to Bharata's post, readdir does not seems to be
1037 +finished yet.
1038 +There are several missing features known in this implementations such as
1039 +- for users, the inode number may change silently. eg. copy-up.
1040 +- link(2) may break by copy-up.
1041 +- read(2) may get an obsoleted filedata (fstat(2) too).
1042 +- fcntl(F_SETLK) may be broken by copy-up.
1043 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1044 +  open(O_RDWR).
1045 +
1046 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1047 +merged into mainline. This is another implementation of UnionMount as a
1048 +separated filesystem. All the limitations and known problems which
1049 +UnionMount are equally inherited to "overlay" filesystem.
1050 +
1051 +Unionfs has a longer history. When I started implementing a stackable
1052 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1053 +inode, dentry and file objects and they have an array pointing lower
1054 +same kind objects. After contributing many patches for Unionfs, I
1055 +re-started my project AUFS (Jun 2006).
1056 +
1057 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1058 +implemented my own ideas, approaches and enhancements and it became
1059 +totally different one.
1060 +
1061 +Comparing DM snapshot and fs based implementation
1062 +- the number of bytes to be copied between devices is much smaller.
1063 +- the type of filesystem must be one and only.
1064 +- the fs must be writable, no readonly fs, even for the lower original
1065 +  device. so the compression fs will not be usable. but if we use
1066 +  loopback mount, we may address this issue.
1067 +  for instance,
1068 +       mount /cdrom/squashfs.img /sq
1069 +       losetup /sq/ext2.img
1070 +       losetup /somewhere/cow
1071 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1072 +- it will be difficult (or needs more operations) to extract the
1073 +  difference between the original device and COW.
1074 +- DM snapshot-merge may help a lot when users try merging. in the
1075 +  fs-layer union, users will use rsync(1).
1076 +
1077 +You may want to read my old paper "Filesystems in LiveCD"
1078 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1079 +
1080 +
1081 +Several characters/aspects/persona of aufs
1082 +----------------------------------------------------------------------
1083 +
1084 +Aufs has several characters, aspects or persona.
1085 +1. a filesystem, callee of VFS helper
1086 +2. sub-VFS, caller of VFS helper for branches
1087 +3. a virtual filesystem which maintains persistent inode number
1088 +4. reader/writer of files on branches such like an application
1089 +
1090 +1. Callee of VFS Helper
1091 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1092 +unlink(2) from an application reaches sys_unlink() kernel function and
1093 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1094 +calls filesystem specific unlink operation. Actually aufs implements the
1095 +unlink operation but it behaves like a redirector.
1096 +
1097 +2. Caller of VFS Helper for Branches
1098 +aufs_unlink() passes the unlink request to the branch filesystem as if
1099 +it were called from VFS. So the called unlink operation of the branch
1100 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1101 +every necessary pre/post operation for the branch filesystem.
1102 +- acquire the lock for the parent dir on a branch
1103 +- lookup in a branch
1104 +- revalidate dentry on a branch
1105 +- mnt_want_write() for a branch
1106 +- vfs_unlink() for a branch
1107 +- mnt_drop_write() for a branch
1108 +- release the lock on a branch
1109 +
1110 +3. Persistent Inode Number
1111 +One of the most important issue for a filesystem is to maintain inode
1112 +numbers. This is particularly important to support exporting a
1113 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1114 +backend block device for its own. But some storage is necessary to
1115 +keep and maintain the inode numbers. It may be a large space and may not
1116 +suit to keep in memory. Aufs rents some space from its first writable
1117 +branch filesystem (by default) and creates file(s) on it. These files
1118 +are created by aufs internally and removed soon (currently) keeping
1119 +opened.
1120 +Note: Because these files are removed, they are totally gone after
1121 +      unmounting aufs. It means the inode numbers are not persistent
1122 +      across unmount or reboot. I have a plan to make them really
1123 +      persistent which will be important for aufs on NFS server.
1124 +
1125 +4. Read/Write Files Internally (copy-on-write)
1126 +Because a branch can be readonly, when you write a file on it, aufs will
1127 +"copy-up" it to the upper writable branch internally. And then write the
1128 +originally requested thing to the file. Generally kernel doesn't
1129 +open/read/write file actively. In aufs, even a single write may cause a
1130 +internal "file copy". This behaviour is very similar to cp(1) command.
1131 +
1132 +Some people may think it is better to pass such work to user space
1133 +helper, instead of doing in kernel space. Actually I am still thinking
1134 +about it. But currently I have implemented it in kernel space.
1135 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1136 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1137 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2015-04-13 15:10:20.773489965 +0200
1138 @@ -0,0 +1,258 @@
1139 +
1140 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1141 +# 
1142 +# This program is free software; you can redistribute it and/or modify
1143 +# it under the terms of the GNU General Public License as published by
1144 +# the Free Software Foundation; either version 2 of the License, or
1145 +# (at your option) any later version.
1146 +# 
1147 +# This program is distributed in the hope that it will be useful,
1148 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1149 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1150 +# GNU General Public License for more details.
1151 +# 
1152 +# You should have received a copy of the GNU General Public License
1153 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1154 +
1155 +Basic Aufs Internal Structure
1156 +
1157 +Superblock/Inode/Dentry/File Objects
1158 +----------------------------------------------------------------------
1159 +As like an ordinary filesystem, aufs has its own
1160 +superblock/inode/dentry/file objects. All these objects have a
1161 +dynamically allocated array and store the same kind of pointers to the
1162 +lower filesystem, branch.
1163 +For example, when you build a union with one readwrite branch and one
1164 +readonly, mounted /au, /rw and /ro respectively.
1165 +- /au = /rw + /ro
1166 +- /ro/fileA exists but /rw/fileA
1167 +
1168 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1169 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1170 +- [0] = NULL (because /rw/fileA doesn't exist)
1171 +- [1] = /ro/fileA
1172 +
1173 +This style of an array is essentially same to the aufs
1174 +superblock/inode/dentry/file objects.
1175 +
1176 +Because aufs supports manipulating branches, ie. add/delete/change
1177 +branches dynamically, these objects has its own generation. When
1178 +branches are changed, the generation in aufs superblock is
1179 +incremented. And a generation in other object are compared when it is
1180 +accessed. When a generation in other objects are obsoleted, aufs
1181 +refreshes the internal array.
1182 +
1183 +
1184 +Superblock
1185 +----------------------------------------------------------------------
1186 +Additionally aufs superblock has some data for policies to select one
1187 +among multiple writable branches, XIB files, pseudo-links and kobject.
1188 +See below in detail.
1189 +About the policies which supports copy-down a directory, see
1190 +wbr_policy.txt too.
1191 +
1192 +
1193 +Branch and XINO(External Inode Number Translation Table)
1194 +----------------------------------------------------------------------
1195 +Every branch has its own xino (external inode number translation table)
1196 +file. The xino file is created and unlinked by aufs internally. When two
1197 +members of a union exist on the same filesystem, they share the single
1198 +xino file.
1199 +The struct of a xino file is simple, just a sequence of aufs inode
1200 +numbers which is indexed by the lower inode number.
1201 +In the above sample, assume the inode number of /ro/fileA is i111 and
1202 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1203 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1204 +
1205 +When the inode numbers are not contiguous, the xino file will be sparse
1206 +which has a hole in it and doesn't consume as much disk space as it
1207 +might appear. If your branch filesystem consumes disk space for such
1208 +holes, then you should specify 'xino=' option at mounting aufs.
1209 +
1210 +Aufs has a mount option to free the disk blocks for such holes in XINO
1211 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1212 +meet a problem of disk shortage due to XINO files, then you should try
1213 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1214 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1215 +the holes in XINO files.
1216 +
1217 +Also a writable branch has three kinds of "whiteout bases". All these
1218 +are existed when the branch is joined to aufs, and their names are
1219 +whiteout-ed doubly, so that users will never see their names in aufs
1220 +hierarchy.
1221 +1. a regular file which will be hardlinked to all whiteouts.
1222 +2. a directory to store a pseudo-link.
1223 +3. a directory to store an "orphan"-ed file temporary.
1224 +
1225 +1. Whiteout Base
1226 +   When you remove a file on a readonly branch, aufs handles it as a
1227 +   logical deletion and creates a whiteout on the upper writable branch
1228 +   as a hardlink of this file in order not to consume inode on the
1229 +   writable branch.
1230 +2. Pseudo-link Dir
1231 +   See below, Pseudo-link.
1232 +3. Step-Parent Dir
1233 +   When "fileC" exists on the lower readonly branch only and it is
1234 +   opened and removed with its parent dir, and then user writes
1235 +   something into it, then aufs copies-up fileC to this
1236 +   directory. Because there is no other dir to store fileC. After
1237 +   creating a file under this dir, the file is unlinked.
1238 +
1239 +Because aufs supports manipulating branches, ie. add/delete/change
1240 +dynamically, a branch has its own id. When the branch order changes,
1241 +aufs finds the new index by searching the branch id.
1242 +
1243 +
1244 +Pseudo-link
1245 +----------------------------------------------------------------------
1246 +Assume "fileA" exists on the lower readonly branch only and it is
1247 +hardlinked to "fileB" on the branch. When you write something to fileA,
1248 +aufs copies-up it to the upper writable branch. Additionally aufs
1249 +creates a hardlink under the Pseudo-link Directory of the writable
1250 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1251 +simple list. If fileB is read after unlinking fileA, aufs returns
1252 +filedata from the pseudo-link instead of the lower readonly
1253 +branch. Because the pseudo-link is based upon the inode, to keep the
1254 +inode number by xino (see above) is essentially necessary.
1255 +
1256 +All the hardlinks under the Pseudo-link Directory of the writable branch
1257 +should be restored in a proper location later. Aufs provides a utility
1258 +to do this. The userspace helpers executed at remounting and unmounting
1259 +aufs by default.
1260 +During this utility is running, it puts aufs into the pseudo-link
1261 +maintenance mode. In this mode, only the process which began the
1262 +maintenance mode (and its child processes) is allowed to operate in
1263 +aufs. Some other processes which are not related to the pseudo-link will
1264 +be allowed to run too, but the rest have to return an error or wait
1265 +until the maintenance mode ends. If a process already acquires an inode
1266 +mutex (in VFS), it has to return an error.
1267 +
1268 +
1269 +XIB(external inode number bitmap)
1270 +----------------------------------------------------------------------
1271 +Addition to the xino file per a branch, aufs has an external inode number
1272 +bitmap in a superblock object. It is also an internal file such like a
1273 +xino file.
1274 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1275 +not.
1276 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1277 +
1278 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1279 +reduce the number of consumed disk blocks for these files.
1280 +
1281 +
1282 +Virtual or Vertical Dir, and Readdir in Userspace
1283 +----------------------------------------------------------------------
1284 +In order to support multiple layers (branches), aufs readdir operation
1285 +constructs a virtual dir block on memory. For readdir, aufs calls
1286 +vfs_readdir() internally for each dir on branches, merges their entries
1287 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1288 +object. So the file object has its entry list until it is closed. The
1289 +entry list will be updated when the file position is zero and becomes
1290 +obsoleted. This decision is made in aufs automatically.
1291 +
1292 +The dynamically allocated memory block for the name of entries has a
1293 +unit of 512 bytes (by default) and stores the names contiguously (no
1294 +padding). Another block for each entry is handled by kmem_cache too.
1295 +During building dir blocks, aufs creates hash list and judging whether
1296 +the entry is whiteouted by its upper branch or already listed.
1297 +The merged result is cached in the corresponding inode object and
1298 +maintained by a customizable life-time option.
1299 +
1300 +Some people may call it can be a security hole or invite DoS attack
1301 +since the opened and once readdir-ed dir (file object) holds its entry
1302 +list and becomes a pressure for system memory. But I'd say it is similar
1303 +to files under /proc or /sys. The virtual files in them also holds a
1304 +memory page (generally) while they are opened. When an idea to reduce
1305 +memory for them is introduced, it will be applied to aufs too.
1306 +For those who really hate this situation, I've developed readdir(3)
1307 +library which operates this merging in userspace. You just need to set
1308 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1309 +kernel space for readdir(3).
1310 +
1311 +
1312 +Workqueue
1313 +----------------------------------------------------------------------
1314 +Aufs sometimes requires privilege access to a branch. For instance,
1315 +in copy-up/down operation. When a user process is going to make changes
1316 +to a file which exists in the lower readonly branch only, and the mode
1317 +of one of ancestor directories may not be writable by a user
1318 +process. Here aufs copy-up the file with its ancestors and they may
1319 +require privilege to set its owner/group/mode/etc.
1320 +This is a typical case of a application character of aufs (see
1321 +Introduction).
1322 +
1323 +Aufs uses workqueue synchronously for this case. It creates its own
1324 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1325 +passes the request to call mkdir or write (for example), and wait for
1326 +its completion. This approach solves a problem of a signal handler
1327 +simply.
1328 +If aufs didn't adopt the workqueue and changed the privilege of the
1329 +process, then the process may receive the unexpected SIGXFSZ or other
1330 +signals.
1331 +
1332 +Also aufs uses the system global workqueue ("events" kernel thread) too
1333 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1334 +whiteout base and etc. This is unrelated to a privilege.
1335 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1336 +superblock at the beginning, at the same time waits for the completion
1337 +of all queued asynchronous tasks.
1338 +
1339 +
1340 +Whiteout
1341 +----------------------------------------------------------------------
1342 +The whiteout in aufs is very similar to Unionfs's. That is represented
1343 +by its filename. UnionMount takes an approach of a file mode, but I am
1344 +afraid several utilities (find(1) or something) will have to support it.
1345 +
1346 +Basically the whiteout represents "logical deletion" which stops aufs to
1347 +lookup further, but also it represents "dir is opaque" which also stop
1348 +further lookup.
1349 +
1350 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1351 +In order to make several functions in a single systemcall to be
1352 +revertible, aufs adopts an approach to rename a directory to a temporary
1353 +unique whiteouted name.
1354 +For example, in rename(2) dir where the target dir already existed, aufs
1355 +renames the target dir to a temporary unique whiteouted name before the
1356 +actual rename on a branch, and then handles other actions (make it opaque,
1357 +update the attributes, etc). If an error happens in these actions, aufs
1358 +simply renames the whiteouted name back and returns an error. If all are
1359 +succeeded, aufs registers a function to remove the whiteouted unique
1360 +temporary name completely and asynchronously to the system global
1361 +workqueue.
1362 +
1363 +
1364 +Copy-up
1365 +----------------------------------------------------------------------
1366 +It is a well-known feature or concept.
1367 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1368 +internally and makes change to the new file on the upper writable branch.
1369 +When the trigger systemcall does not update the timestamps of the parent
1370 +dir, aufs reverts it after copy-up.
1371 +
1372 +
1373 +Move-down (aufs3.9 and later)
1374 +----------------------------------------------------------------------
1375 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1376 +the lower readonly branch to the upper writable branch when a user
1377 +changes something about the file.
1378 +"Move-down" is an opposite action of copy-up. Basically this action is
1379 +ran manually instead of automatically and internally.
1380 +For desgin and implementation, aufs has to consider these issues.
1381 +- whiteout for the file may exist on the lower branch.
1382 +- ancestor directories may not exist on the lower branch.
1383 +- diropq for the ancestor directories may exist on the upper branch.
1384 +- free space on the lower branch will reduce.
1385 +- another access to the file may happen during moving-down, including
1386 +  UDBA (see "Revalidate Dentry and UDBA").
1387 +- the file should not be hard-linked nor pseudo-linked. they should be
1388 +  handled by auplink utility later.
1389 +
1390 +Sometimes users want to move-down a file from the upper writable branch
1391 +to the lower readonly or writable branch. For instance,
1392 +- the free space of the upper writable branch is going to run out.
1393 +- create a new intermediate branch between the upper and lower branch.
1394 +- etc.
1395 +
1396 +For this purpose, use "aumvdown" command in aufs-util.git.
1397 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1398 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1399 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2015-04-13 15:10:20.776823376 +0200
1400 @@ -0,0 +1,113 @@
1401 +
1402 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1403 +# 
1404 +# This program is free software; you can redistribute it and/or modify
1405 +# it under the terms of the GNU General Public License as published by
1406 +# the Free Software Foundation; either version 2 of the License, or
1407 +# (at your option) any later version.
1408 +# 
1409 +# This program is distributed in the hope that it will be useful,
1410 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1411 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1412 +# GNU General Public License for more details.
1413 +# 
1414 +# You should have received a copy of the GNU General Public License
1415 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1416 +
1417 +Lookup in a Branch
1418 +----------------------------------------------------------------------
1419 +Since aufs has a character of sub-VFS (see Introduction), it operates
1420 +lookup for branches as VFS does. It may be a heavy work. But almost all
1421 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1422 +directly connected to its parent. Digging down the directory hierarchy
1423 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1424 +aufs calls it.
1425 +
1426 +When a branch is a remote filesystem, aufs basically relies upon its
1427 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1428 +them.
1429 +For d_revalidate, aufs implements three levels of revalidate tests. See
1430 +"Revalidate Dentry and UDBA" in detail.
1431 +
1432 +
1433 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1434 +----------------------------------------------------------------------
1435 +Let's try case study.
1436 +- aufs has two branches, upper readwrite and lower readonly.
1437 +  /au = /rw + /ro
1438 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1439 +- user invoked "chmod a+rx /au/dirA"
1440 +- the internal copy-up is activated and "/rw/dirA" is created and its
1441 +  permission bits are set to world readable.
1442 +- then "/au/dirA" becomes world readable?
1443 +
1444 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1445 +or it may be a natively readonly filesystem. If aufs respects the lower
1446 +branch, it should not respond readdir request from other users. But user
1447 +allowed it by chmod. Should really aufs rejects showing the entries
1448 +under /ro/dirA?
1449 +
1450 +To be honest, I don't have a good solution for this case. So aufs
1451 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1452 +users.
1453 +When dirperm1 is specified, aufs checks only the highest one for the
1454 +directory permission, and shows the entries. Otherwise, as usual, checks
1455 +every dir existing on all branches and rejects the request.
1456 +
1457 +As a side effect, dirperm1 option improves the performance of aufs
1458 +because the number of permission check is reduced when the number of
1459 +branch is many.
1460 +
1461 +
1462 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1463 +----------------------------------------------------------------------
1464 +Generally VFS helpers re-validate a dentry as a part of lookup.
1465 +0. digging down the directory hierarchy.
1466 +1. lock the parent dir by its i_mutex.
1467 +2. lookup the final (child) entry.
1468 +3. revalidate it.
1469 +4. call the actual operation (create, unlink, etc.)
1470 +5. unlock the parent dir
1471 +
1472 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1473 +called. Actually aufs implements it and checks the dentry on a branch is
1474 +still valid.
1475 +But it is not enough. Because aufs has to release the lock for the
1476 +parent dir on a branch at the end of ->lookup() (step 2) and
1477 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1478 +held by VFS.
1479 +If the file on a branch is changed directly, eg. bypassing aufs, after
1480 +aufs released the lock, then the subsequent operation may cause
1481 +something unpleasant result.
1482 +
1483 +This situation is a result of VFS architecture, ->lookup() and
1484 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1485 +design from VFS's point of view. It is just not suitable for sub-VFS
1486 +character in aufs.
1487 +
1488 +Aufs supports such case by three level of revalidation which is
1489 +selectable by user.
1490 +1. Simple Revalidate
1491 +   Addition to the native flow in VFS's, confirm the child-parent
1492 +   relationship on the branch just after locking the parent dir on the
1493 +   branch in the "actual operation" (step 4). When this validation
1494 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1495 +   checks the validation of the dentry on branches.
1496 +2. Monitor Changes Internally by Inotify/Fsnotify
1497 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1498 +   the dentry on the branch, and returns EBUSY if it finds different
1499 +   dentry.
1500 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1501 +   during it is in cache. When the event is notified, aufs registers a
1502 +   function to kernel 'events' thread by schedule_work(). And the
1503 +   function sets some special status to the cached aufs dentry and inode
1504 +   private data. If they are not cached, then aufs has nothing to
1505 +   do. When the same file is accessed through aufs (step 0-3) later,
1506 +   aufs will detect the status and refresh all necessary data.
1507 +   In this mode, aufs has to ignore the event which is fired by aufs
1508 +   itself.
1509 +3. No Extra Validation
1510 +   This is the simplest test and doesn't add any additional revalidation
1511 +   test, and skip the revalidation in step 4. It is useful and improves
1512 +   aufs performance when system surely hide the aufs branches from user,
1513 +   by over-mounting something (or another method).
1514 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1515 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1516 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2015-04-13 15:10:20.776823376 +0200
1517 @@ -0,0 +1,74 @@
1518 +
1519 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1520 +# 
1521 +# This program is free software; you can redistribute it and/or modify
1522 +# it under the terms of the GNU General Public License as published by
1523 +# the Free Software Foundation; either version 2 of the License, or
1524 +# (at your option) any later version.
1525 +# 
1526 +# This program is distributed in the hope that it will be useful,
1527 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1528 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1529 +# GNU General Public License for more details.
1530 +# 
1531 +# You should have received a copy of the GNU General Public License
1532 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1533 +
1534 +Branch Manipulation
1535 +
1536 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1537 +and changing its permission/attribute, there are a lot of works to do.
1538 +
1539 +
1540 +Add a Branch
1541 +----------------------------------------------------------------------
1542 +o Confirm the adding dir exists outside of aufs, including loopback
1543 +  mount, and its various attributes.
1544 +o Initialize the xino file and whiteout bases if necessary.
1545 +  See struct.txt.
1546 +
1547 +o Check the owner/group/mode of the directory
1548 +  When the owner/group/mode of the adding directory differs from the
1549 +  existing branch, aufs issues a warning because it may impose a
1550 +  security risk.
1551 +  For example, when a upper writable branch has a world writable empty
1552 +  top directory, a malicious user can create any files on the writable
1553 +  branch directly, like copy-up and modify manually. If something like
1554 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1555 +  writable branch, and the writable branch is world-writable, then a
1556 +  malicious guy may create /etc/passwd on the writable branch directly
1557 +  and the infected file will be valid in aufs.
1558 +  I am afraid it can be a security issue, but aufs can do nothing except
1559 +  producing a warning.
1560 +
1561 +
1562 +Delete a Branch
1563 +----------------------------------------------------------------------
1564 +o Confirm the deleting branch is not busy
1565 +  To be general, there is one merit to adopt "remount" interface to
1566 +  manipulate branches. It is to discard caches. At deleting a branch,
1567 +  aufs checks the still cached (and connected) dentries and inodes. If
1568 +  there are any, then they are all in-use. An inode without its
1569 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1570 +
1571 +  For the cached one, aufs checks whether the same named entry exists on
1572 +  other branches.
1573 +  If the cached one is a directory, because aufs provides a merged view
1574 +  to users, as long as one dir is left on any branch aufs can show the
1575 +  dir to users. In this case, the branch can be removed from aufs.
1576 +  Otherwise aufs rejects deleting the branch.
1577 +
1578 +  If any file on the deleting branch is opened by aufs, then aufs
1579 +  rejects deleting.
1580 +
1581 +
1582 +Modify the Permission of a Branch
1583 +----------------------------------------------------------------------
1584 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1585 +  See struct.txt.
1586 +
1587 +o rw --> ro: Confirm the modifying branch is not busy
1588 +  Aufs rejects the request if any of these conditions are true.
1589 +  - a file on the branch is mmap-ed.
1590 +  - a regular file on the branch is opened for write and there is no
1591 +    same named entry on the upper branch.
1592 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1593 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1594 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2015-04-13 15:10:20.776823376 +0200
1595 @@ -0,0 +1,64 @@
1596 +
1597 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1598 +# 
1599 +# This program is free software; you can redistribute it and/or modify
1600 +# it under the terms of the GNU General Public License as published by
1601 +# the Free Software Foundation; either version 2 of the License, or
1602 +# (at your option) any later version.
1603 +# 
1604 +# This program is distributed in the hope that it will be useful,
1605 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1606 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1607 +# GNU General Public License for more details.
1608 +# 
1609 +# You should have received a copy of the GNU General Public License
1610 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1611 +
1612 +Policies to Select One among Multiple Writable Branches
1613 +----------------------------------------------------------------------
1614 +When the number of writable branch is more than one, aufs has to decide
1615 +the target branch for file creation or copy-up. By default, the highest
1616 +writable branch which has the parent (or ancestor) dir of the target
1617 +file is chosen (top-down-parent policy).
1618 +By user's request, aufs implements some other policies to select the
1619 +writable branch, for file creation several policies, round-robin,
1620 +most-free-space, and other policies. For copy-up, top-down-parent,
1621 +bottom-up-parent, bottom-up and others.
1622 +
1623 +As expected, the round-robin policy selects the branch in circular. When
1624 +you have two writable branches and creates 10 new files, 5 files will be
1625 +created for each branch. mkdir(2) systemcall is an exception. When you
1626 +create 10 new directories, all will be created on the same branch.
1627 +And the most-free-space policy selects the one which has most free
1628 +space among the writable branches. The amount of free space will be
1629 +checked by aufs internally, and users can specify its time interval.
1630 +
1631 +The policies for copy-up is more simple,
1632 +top-down-parent is equivalent to the same named on in create policy,
1633 +bottom-up-parent selects the writable branch where the parent dir
1634 +exists and the nearest upper one from the copyup-source,
1635 +bottom-up selects the nearest upper writable branch from the
1636 +copyup-source, regardless the existence of the parent dir.
1637 +
1638 +There are some rules or exceptions to apply these policies.
1639 +- If there is a readonly branch above the policy-selected branch and
1640 +  the parent dir is marked as opaque (a variation of whiteout), or the
1641 +  target (creating) file is whiteout-ed on the upper readonly branch,
1642 +  then the result of the policy is ignored and the target file will be
1643 +  created on the nearest upper writable branch than the readonly branch.
1644 +- If there is a writable branch above the policy-selected branch and
1645 +  the parent dir is marked as opaque or the target file is whiteouted
1646 +  on the branch, then the result of the policy is ignored and the target
1647 +  file will be created on the highest one among the upper writable
1648 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1649 +  it as usual.
1650 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1651 +  They try selecting the branch where the source exists as possible
1652 +  since copyup a large file will take long time. If it can't be,
1653 +  ie. the branch where the source exists is readonly, then they will
1654 +  follow the copyup policy.
1655 +- There is an exception for rename(2) when the target exists.
1656 +  If the rename target exists, aufs compares the index of the branches
1657 +  where the source and the target exists and selects the higher
1658 +  one. If the selected branch is readonly, then aufs follows the
1659 +  copyup policy.
1660 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1661 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1662 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2015-04-13 15:10:20.776823376 +0200
1663 @@ -0,0 +1,120 @@
1664 +
1665 +# Copyright (C) 2011-2015 Junjiro R. Okajima
1666 +# 
1667 +# This program is free software; you can redistribute it and/or modify
1668 +# it under the terms of the GNU General Public License as published by
1669 +# the Free Software Foundation; either version 2 of the License, or
1670 +# (at your option) any later version.
1671 +# 
1672 +# This program is distributed in the hope that it will be useful,
1673 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1674 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1675 +# GNU General Public License for more details.
1676 +# 
1677 +# You should have received a copy of the GNU General Public License
1678 +# along with this program; if not, write to the Free Software
1679 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1680 +
1681 +
1682 +File-based Hierarchical Storage Management (FHSM)
1683 +----------------------------------------------------------------------
1684 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1685 +storage world. Aufs provides this feature as file-based with multiple
1686 +writable branches, based upon the principle of "Colder, the Lower".
1687 +Here the word "colder" means that the less used files, and "lower" means
1688 +that the position in the order of the stacked branches vertically.
1689 +These multiple writable branches are prioritized, ie. the topmost one
1690 +should be the fastest drive and be used heavily.
1691 +
1692 +o Characters in aufs FHSM story
1693 +- aufs itself and a new branch attribute.
1694 +- a new ioctl interface to move-down and to establish a connection with
1695 +  the daemon ("move-down" is a converse of "copy-up").
1696 +- userspace tool and daemon.
1697 +
1698 +The userspace daemon establishes a connection with aufs and waits for
1699 +the notification. The notified information is very similar to struct
1700 +statfs containing the number of consumed blocks and inodes.
1701 +When the consumed blocks/inodes of a branch exceeds the user-specified
1702 +upper watermark, the daemon activates its move-down process until the
1703 +consumed blocks/inodes reaches the user-specified lower watermark.
1704 +
1705 +The actual move-down is done by aufs based upon the request from
1706 +user-space since we need to maintain the inode number and the internal
1707 +pointer arrays in aufs.
1708 +
1709 +Currently aufs FHSM handles the regular files only. Additionally they
1710 +must not be hard-linked nor pseudo-linked.
1711 +
1712 +
1713 +o Cowork of aufs and the user-space daemon
1714 +  During the userspace daemon established the connection, aufs sends a
1715 +  small notification to it whenever aufs writes something into the
1716 +  writable branch. But it may cost high since aufs issues statfs(2)
1717 +  internally. So user can specify a new option to cache the
1718 +  info. Actually the notification is controlled by these factors.
1719 +  + the specified cache time.
1720 +  + classified as "force" by aufs internally.
1721 +  Until the specified time expires, aufs doesn't send the info
1722 +  except the forced cases. When aufs decide forcing, the info is always
1723 +  notified to userspace.
1724 +  For example, the number of free inodes is generally large enough and
1725 +  the shortage of it happens rarely. So aufs doesn't force the
1726 +  notification when creating a new file, directory and others. This is
1727 +  the typical case which aufs doesn't force.
1728 +  When aufs writes the actual filedata and the files consumes any of new
1729 +  blocks, the aufs forces notifying.
1730 +
1731 +
1732 +o Interfaces in aufs
1733 +- New branch attribute.
1734 +  + fhsm
1735 +    Specifies that the branch is managed by FHSM feature. In other word,
1736 +    participant in the FHSM.
1737 +    When nofhsm is set to the branch, it will not be the source/target
1738 +    branch of the move-down operation. This attribute is set
1739 +    independently from coo and moo attributes, and if you want full
1740 +    FHSM, you should specify them as well.
1741 +- New mount option.
1742 +  + fhsm_sec
1743 +    Specifies a second to suppress many less important info to be
1744 +    notified.
1745 +- New ioctl.
1746 +  + AUFS_CTL_FHSM_FD
1747 +    create a new file descriptor which userspace can read the notification
1748 +    (a subset of struct statfs) from aufs.
1749 +- Module parameter 'brs'
1750 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
1751 +  be set.
1752 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
1753 +  When there are two or more branches with fhsm attributes,
1754 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
1755 +  terminates it. As a result of remounting and branch-manipulation, the
1756 +  number of branches with fhsm attribute can be one. In this case,
1757 +  /sbin/mount.aufs will terminate the user-space daemon.
1758 +
1759 +
1760 +Finally the operation is done as these steps in kernel-space.
1761 +- make sure that,
1762 +  + no one else is using the file.
1763 +  + the file is not hard-linked.
1764 +  + the file is not pseudo-linked.
1765 +  + the file is a regular file.
1766 +  + the parent dir is not opaqued.
1767 +- find the target writable branch.
1768 +- make sure the file is not whiteout-ed by the upper (than the target)
1769 +  branch.
1770 +- make the parent dir on the target branch.
1771 +- mutex lock the inode on the branch.
1772 +- unlink the whiteout on the target branch (if exists).
1773 +- lookup and create the whiteout-ed temporary name on the target branch.
1774 +- copy the file as the whiteout-ed temporary name on the target branch.
1775 +- rename the whiteout-ed temporary name to the original name.
1776 +- unlink the file on the source branch.
1777 +- maintain the internal pointer array and the external inode number
1778 +  table (XINO).
1779 +- maintain the timestamps and other attributes of the parent dir and the
1780 +  file.
1781 +
1782 +And of course, in every step, an error may happen. So the operation
1783 +should restore the original file state after an error happens.
1784 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
1785 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
1786 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2015-04-13 15:10:20.776823376 +0200
1787 @@ -0,0 +1,46 @@
1788 +
1789 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1790 +# 
1791 +# This program is free software; you can redistribute it and/or modify
1792 +# it under the terms of the GNU General Public License as published by
1793 +# the Free Software Foundation; either version 2 of the License, or
1794 +# (at your option) any later version.
1795 +# 
1796 +# This program is distributed in the hope that it will be useful,
1797 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1798 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1799 +# GNU General Public License for more details.
1800 +# 
1801 +# You should have received a copy of the GNU General Public License
1802 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1803 +
1804 +mmap(2) -- File Memory Mapping
1805 +----------------------------------------------------------------------
1806 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1807 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1808 +->mmap().
1809 +This approach is simple and good, but there is one problem.
1810 +Under /proc, several entries show the mmapped files by its path (with
1811 +device and inode number), and the printed path will be the path on the
1812 +branch fs's instead of virtual aufs's.
1813 +This is not a problem in most cases, but some utilities lsof(1) (and its
1814 +user) may expect the path on aufs.
1815 +
1816 +To address this issue, aufs adds a new member called vm_prfile in struct
1817 +vm_area_struct (and struct vm_region). The original vm_file points to
1818 +the file on the branch fs in order to handle everything correctly as
1819 +usual. The new vm_prfile points to a virtual file in aufs, and the
1820 +show-functions in procfs refers to vm_prfile if it is set.
1821 +Also we need to maintain several other places where touching vm_file
1822 +such like
1823 +- fork()/clone() copies vma and the reference count of vm_file is
1824 +  incremented.
1825 +- merging vma maintains the ref count too.
1826 +
1827 +This is not a good approach. It just fakes the printed path. But it
1828 +leaves all behaviour around f_mapping unchanged. This is surely an
1829 +advantage.
1830 +Actually aufs had adopted another complicated approach which calls
1831 +generic_file_mmap() and handles struct vm_operations_struct. In this
1832 +approach, aufs met a hard problem and I could not solve it without
1833 +switching the approach.
1834 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
1835 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
1836 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2015-04-13 15:10:20.776823376 +0200
1837 @@ -0,0 +1,96 @@
1838 +
1839 +# Copyright (C) 2014-2015 Junjiro R. Okajima
1840 +#
1841 +# This program is free software; you can redistribute it and/or modify
1842 +# it under the terms of the GNU General Public License as published by
1843 +# the Free Software Foundation; either version 2 of the License, or
1844 +# (at your option) any later version.
1845 +#
1846 +# This program is distributed in the hope that it will be useful,
1847 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1848 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1849 +# GNU General Public License for more details.
1850 +#
1851 +# You should have received a copy of the GNU General Public License
1852 +# along with this program; if not, write to the Free Software
1853 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1854 +
1855 +
1856 +Listing XATTR/EA and getting the value
1857 +----------------------------------------------------------------------
1858 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
1859 +shows the values from the topmost existing file. This behaviour is good
1860 +for the non-dir entries since the bahaviour exactly matches the shown
1861 +information. But for the directories, aufs considers all the same named
1862 +entries on the lower branches. Which means, if one of the lower entry
1863 +rejects readdir call, then aufs returns an error even if the topmost
1864 +entry allows it. This behaviour is necessary to respect the branch fs's
1865 +security, but can make users confused since the user-visible standard
1866 +attributes don't match the behaviour.
1867 +To address this issue, aufs has a mount option called dirperm1 which
1868 +checks the permission for the topmost entry only, and ignores the lower
1869 +entry's permission.
1870 +
1871 +A similar issue can happen around XATTR.
1872 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
1873 +always set. Otherwise these very unpleasant situation would happen.
1874 +- listxattr(2) may return the duplicated entries.
1875 +- users may not be able to remove or reset the XATTR forever,
1876 +
1877 +
1878 +XATTR/EA support in the internal (copy,move)-(up,down)
1879 +----------------------------------------------------------------------
1880 +Generally the extended attributes of inode are categorized as these.
1881 +- "security" for LSM and capability.
1882 +- "system" for posix ACL, 'acl' mount option is required for the branch
1883 +  fs generally.
1884 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
1885 +- "user" for userspace, 'user_xattr' mount option is required for the
1886 +  branch fs generally.
1887 +
1888 +Moreover there are some other categories. Aufs handles these rather
1889 +unpopular categories as the ordinary ones, ie. there is no special
1890 +condition nor exception.
1891 +
1892 +In copy-up, the support for XATTR on the dst branch may differ from the
1893 +src branch. In this case, the copy-up operation will get an error and
1894 +the original user operation which triggered the copy-up will fail. It
1895 +can happen that even all copy-up will fail.
1896 +When both of src and dst branches support XATTR and if an error occurs
1897 +during copying XATTR, then the copy-up should fail obviously. That is a
1898 +good reason and aufs should return an error to userspace. But when only
1899 +the src branch support that XATTR, aufs should not return an error.
1900 +For example, the src branch supports ACL but the dst branch doesn't
1901 +because the dst branch may natively un-support it or temporary
1902 +un-support it due to "noacl" mount option. Of course, the dst branch fs
1903 +may NOT return an error even if the XATTR is not supported. It is
1904 +totally up to the branch fs.
1905 +
1906 +Anyway when the aufs internal copy-up gets an error from the dst branch
1907 +fs, then aufs tries removing the just copied entry and returns the error
1908 +to the userspace. The worst case of this situation will be all copy-up
1909 +will fail.
1910 +
1911 +For the copy-up operation, there two basic approaches.
1912 +- copy the specified XATTR only (by category above), and return the
1913 +  error unconditionally if it happens.
1914 +- copy all XATTR, and ignore the error on the specified category only.
1915 +
1916 +In order to support XATTR and to implement the correct behaviour, aufs
1917 +chooses the latter approach and introduces some new branch attributes,
1918 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
1919 +They correspond to the XATTR namespaces (see above). Additionally, to be
1920 +convenient, "icex" is also provided which means all "icex*" attributes
1921 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
1922 +
1923 +The meaning of these attributes is to ignore the error from setting
1924 +XATTR on that branch.
1925 +Note that aufs tries copying all XATTR unconditionally, and ignores the
1926 +error from the dst branch according to the specified attributes.
1927 +
1928 +Some XATTR may have its default value. The default value may come from
1929 +the parent dir or the environment. If the default value is set at the
1930 +file creating-time, it will be overwritten by copy-up.
1931 +Some contradiction may happen I am afraid.
1932 +Do we need another attribute to stop copying XATTR? I am unsure. For
1933 +now, aufs implements the branch attributes to ignore the error.
1934 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
1935 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
1936 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2015-04-13 15:10:20.776823376 +0200
1937 @@ -0,0 +1,58 @@
1938 +
1939 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1940 +# 
1941 +# This program is free software; you can redistribute it and/or modify
1942 +# it under the terms of the GNU General Public License as published by
1943 +# the Free Software Foundation; either version 2 of the License, or
1944 +# (at your option) any later version.
1945 +# 
1946 +# This program is distributed in the hope that it will be useful,
1947 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1948 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1949 +# GNU General Public License for more details.
1950 +# 
1951 +# You should have received a copy of the GNU General Public License
1952 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1953 +
1954 +Export Aufs via NFS
1955 +----------------------------------------------------------------------
1956 +Here is an approach.
1957 +- like xino/xib, add a new file 'xigen' which stores aufs inode
1958 +  generation.
1959 +- iget_locked(): initialize aufs inode generation for a new inode, and
1960 +  store it in xigen file.
1961 +- destroy_inode(): increment aufs inode generation and store it in xigen
1962 +  file. it is necessary even if it is not unlinked, because any data of
1963 +  inode may be changed by UDBA.
1964 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
1965 +  build file handle by
1966 +  + branch id (4 bytes)
1967 +  + superblock generation (4 bytes)
1968 +  + inode number (4 or 8 bytes)
1969 +  + parent dir inode number (4 or 8 bytes)
1970 +  + inode generation (4 bytes))
1971 +  + return value of exportfs_encode_fh() for the parent on a branch (4
1972 +    bytes)
1973 +  + file handle for a branch (by exportfs_encode_fh())
1974 +- fh_to_dentry():
1975 +  + find the index of a branch from its id in handle, and check it is
1976 +    still exist in aufs.
1977 +  + 1st level: get the inode number from handle and search it in cache.
1978 +  + 2nd level: if not found in cache, get the parent inode number from
1979 +    the handle and search it in cache. and then open the found parent
1980 +    dir, find the matching inode number by vfs_readdir() and get its
1981 +    name, and call lookup_one_len() for the target dentry.
1982 +  + 3rd level: if the parent dir is not cached, call
1983 +    exportfs_decode_fh() for a branch and get the parent on a branch,
1984 +    build a pathname of it, convert it a pathname in aufs, call
1985 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
1986 +    the 2nd level.
1987 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
1988 +    for every branch, but not itself. to get this, (currently) aufs
1989 +    searches in current->nsproxy->mnt_ns list. it may not be a good
1990 +    idea, but I didn't get other approach.
1991 +  + test the generation of the gotten inode.
1992 +- every inode operation: they may get EBUSY due to UDBA. in this case,
1993 +  convert it into ESTALE for NFSD.
1994 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
1995 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
1996 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
1997 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
1998 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2015-04-13 15:10:20.776823376 +0200
1999 @@ -0,0 +1,52 @@
2000 +
2001 +# Copyright (C) 2005-2015 Junjiro R. Okajima
2002 +# 
2003 +# This program is free software; you can redistribute it and/or modify
2004 +# it under the terms of the GNU General Public License as published by
2005 +# the Free Software Foundation; either version 2 of the License, or
2006 +# (at your option) any later version.
2007 +# 
2008 +# This program is distributed in the hope that it will be useful,
2009 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2010 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2011 +# GNU General Public License for more details.
2012 +# 
2013 +# You should have received a copy of the GNU General Public License
2014 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2015 +
2016 +Show Whiteout Mode (shwh)
2017 +----------------------------------------------------------------------
2018 +Generally aufs hides the name of whiteouts. But in some cases, to show
2019 +them is very useful for users. For instance, creating a new middle layer
2020 +(branch) by merging existing layers.
2021 +
2022 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2023 +When you have three branches,
2024 +- Bottom: 'system', squashfs (underlying base system), read-only
2025 +- Middle: 'mods', squashfs, read-only
2026 +- Top: 'overlay', ram (tmpfs), read-write
2027 +
2028 +The top layer is loaded at boot time and saved at shutdown, to preserve
2029 +the changes made to the system during the session.
2030 +When larger changes have been made, or smaller changes have accumulated,
2031 +the size of the saved top layer data grows. At this point, it would be
2032 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2033 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2034 +restoring save and load speed.
2035 +
2036 +This merging is simplified by the use of another aufs mount, of just the
2037 +two overlay branches using the 'shwh' option.
2038 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2039 +       aufs /livesys/merge_union
2040 +
2041 +A merged view of these two branches is then available at
2042 +/livesys/merge_union, and the new feature is that the whiteouts are
2043 +visible!
2044 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2045 +writing to all branches. Also the default mode for all branches is 'ro'.
2046 +It is now possible to save the combined contents of the two overlay
2047 +branches to a new squashfs, e.g.:
2048 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2049 +
2050 +This new squashfs archive can be stored on the boot device and the
2051 +initramfs will use it to replace the old one at the next boot.
2052 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2053 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2054 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2015-04-13 15:10:20.776823376 +0200
2055 @@ -0,0 +1,47 @@
2056 +
2057 +# Copyright (C) 2010-2015 Junjiro R. Okajima
2058 +#
2059 +# This program is free software; you can redistribute it and/or modify
2060 +# it under the terms of the GNU General Public License as published by
2061 +# the Free Software Foundation; either version 2 of the License, or
2062 +# (at your option) any later version.
2063 +#
2064 +# This program is distributed in the hope that it will be useful,
2065 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2066 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2067 +# GNU General Public License for more details.
2068 +#
2069 +# You should have received a copy of the GNU General Public License
2070 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2071 +
2072 +Dynamically customizable FS operations
2073 +----------------------------------------------------------------------
2074 +Generally FS operations (struct inode_operations, struct
2075 +address_space_operations, struct file_operations, etc.) are defined as
2076 +"static const", but it never means that FS have only one set of
2077 +operation. Some FS have multiple sets of them. For instance, ext2 has
2078 +three sets, one for XIP, for NOBH, and for normal.
2079 +Since aufs overrides and redirects these operations, sometimes aufs has
2080 +to change its behaviour according to the branch FS type. More importantly
2081 +VFS acts differently if a function (member in the struct) is set or
2082 +not. It means aufs should have several sets of operations and select one
2083 +among them according to the branch FS definition.
2084 +
2085 +In order to solve this problem and not to affect the behaviour of VFS,
2086 +aufs defines these operations dynamically. For instance, aufs defines
2087 +dummy direct_IO function for struct address_space_operations, but it may
2088 +not be set to the address_space_operations actually. When the branch FS
2089 +doesn't have it, aufs doesn't set it to its address_space_operations
2090 +while the function definition itself is still alive. So the behaviour
2091 +itself will not change, and it will return an error when direct_IO is
2092 +not set.
2093 +
2094 +The lifetime of these dynamically generated operation object is
2095 +maintained by aufs branch object. When the branch is removed from aufs,
2096 +the reference counter of the object is decremented. When it reaches
2097 +zero, the dynamically generated operation object will be freed.
2098 +
2099 +This approach is designed to support AIO (io_submit), Direct I/O and
2100 +XIP (DAX) mainly.
2101 +Currently this approach is applied to address_space_operations for
2102 +regular files only.
2103 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt linux/Documentation/filesystems/aufs/design/99plan.txt
2104 --- /usr/share/empty/Documentation/filesystems/aufs/design/99plan.txt   1970-01-01 01:00:00.000000000 +0100
2105 +++ linux/Documentation/filesystems/aufs/design/99plan.txt      2015-04-13 15:10:20.776823376 +0200
2106 @@ -0,0 +1,57 @@
2107 +
2108 +# Copyright (C) 2005-2015 Junjiro R. Okajima
2109 +# 
2110 +# This program is free software; you can redistribute it and/or modify
2111 +# it under the terms of the GNU General Public License as published by
2112 +# the Free Software Foundation; either version 2 of the License, or
2113 +# (at your option) any later version.
2114 +# 
2115 +# This program is distributed in the hope that it will be useful,
2116 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2117 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2118 +# GNU General Public License for more details.
2119 +# 
2120 +# You should have received a copy of the GNU General Public License
2121 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2122 +
2123 +Plan
2124 +
2125 +Restoring some features which was implemented in aufs1.
2126 +They were dropped in aufs2 in order to make source files simpler and
2127 +easier to be reviewed.
2128 +
2129 +
2130 +Being Another Aufs's Readonly Branch (robr)
2131 +----------------------------------------------------------------------
2132 +Aufs1 allows aufs to be another aufs's readonly branch.
2133 +This feature was developed by a user's request. But it may not be used
2134 +currently.
2135 +
2136 +
2137 +Refresh the Opened File (refrof)
2138 +----------------------------------------------------------------------
2139 +This option is implemented in aufs1 but incomplete.
2140 +
2141 +When user reads from a file, he expects to get its latest filedata
2142 +generally. If the file is removed and a new same named file is created,
2143 +the content he gets is unchanged, ie. the unlinked filedata.
2144 +
2145 +Let's try case study again.
2146 +- aufs has two branches.
2147 +  /au = /rw + /ro
2148 +- "fileA" exists under /ro, but /rw.
2149 +- user opened "/au/fileA".
2150 +- he or someone else inserts a branch (/new) between /rw and /ro.
2151 +  /au = /rw + /new + /ro
2152 +- the new branch contains "fileA".
2153 +- user reads from the opened "fileA"
2154 +- which filedata should aufs return, from /ro or /new?
2155 +
2156 +Some people says it has to be "from /ro" and it is a semantics of Unix.
2157 +The others say it should be "from /new" because the file is not removed
2158 +and it is equivalent to the case of someone else modifies the file.
2159 +
2160 +Here again I don't have a best and final answer. I got an idea to
2161 +implement 'refrof' and 'norefrof' option. When 'refrof' (REFResh the
2162 +Opened File) is specified (by default), aufs returns the filedata from
2163 +/new. Otherwise from /ro.
2164 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2165 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2166 +++ linux/Documentation/filesystems/aufs/README 2015-04-13 15:10:20.773489965 +0200
2167 @@ -0,0 +1,384 @@
2168 +
2169 +Aufs3 -- advanced multi layered unification filesystem version 3.x
2170 +http://aufs.sf.net
2171 +Junjiro R. Okajima
2172 +
2173 +
2174 +0. Introduction
2175 +----------------------------------------
2176 +In the early days, aufs was entirely re-designed and re-implemented
2177 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2178 +improvements and implementations, it becomes totally different from
2179 +Unionfs while keeping the basic features.
2180 +Recently, Unionfs Version 2.x series begin taking some of the same
2181 +approaches to aufs1's.
2182 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2183 +University and his team.
2184 +
2185 +Aufs3 supports linux-3.0 and later.
2186 +If you want older kernel version support, try aufs2-2.6.git or
2187 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2188 +
2189 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2190 +      According to Christoph Hellwig, linux rejects all union-type
2191 +      filesystems but UnionMount.
2192 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2193 +
2194 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2195 +    UnionMount, and he pointed out an issue around a directory mutex
2196 +    lock and aufs addressed it. But it is still unsure whether aufs will
2197 +    be merged (or any other union solution).
2198 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2199 +
2200 +
2201 +1. Features
2202 +----------------------------------------
2203 +- unite several directories into a single virtual filesystem. The member
2204 +  directory is called as a branch.
2205 +- you can specify the permission flags to the branch, which are 'readonly',
2206 +  'readwrite' and 'whiteout-able.'
2207 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2208 +  readonly branch are modifiable logically.
2209 +- dynamic branch manipulation, add, del.
2210 +- etc...
2211 +
2212 +Also there are many enhancements in aufs, such as:
2213 +- test only the highest one for the directory permission (dirperm1)
2214 +- copyup on open (coo=)
2215 +- 'move' policy for copy-up between two writable branches, after
2216 +  checking free space.
2217 +- xattr, acl
2218 +- readdir(3) in userspace.
2219 +- keep inode number by external inode number table
2220 +- keep the timestamps of file/dir in internal copyup operation
2221 +- seekable directory, supporting NFS readdir.
2222 +- whiteout is hardlinked in order to reduce the consumption of inodes
2223 +  on branch
2224 +- do not copyup, nor create a whiteout when it is unnecessary
2225 +- revert a single systemcall when an error occurs in aufs
2226 +- remount interface instead of ioctl
2227 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2228 +- loopback mounted filesystem as a branch
2229 +- kernel thread for removing the dir who has a plenty of whiteouts
2230 +- support copyup sparse file (a file which has a 'hole' in it)
2231 +- default permission flags for branches
2232 +- selectable permission flags for ro branch, whether whiteout can
2233 +  exist or not
2234 +- export via NFS.
2235 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2236 +- support multiple writable branches, some policies to select one
2237 +  among multiple writable branches.
2238 +- a new semantics for link(2) and rename(2) to support multiple
2239 +  writable branches.
2240 +- no glibc changes are required.
2241 +- pseudo hardlink (hardlink over branches)
2242 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2243 +  including NFS or remote filesystem branch.
2244 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2245 +- and more...
2246 +
2247 +Currently these features are dropped temporary from aufs3.
2248 +See design/08plan.txt in detail.
2249 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2250 +  (robr)
2251 +- statistics of aufs thread (/sys/fs/aufs/stat)
2252 +
2253 +Features or just an idea in the future (see also design/*.txt),
2254 +- reorder the branch index without del/re-add.
2255 +- permanent xino files for NFSD
2256 +- an option for refreshing the opened files after add/del branches
2257 +- light version, without branch manipulation. (unnecessary?)
2258 +- copyup in userspace
2259 +- inotify in userspace
2260 +- readv/writev
2261 +
2262 +
2263 +2. Download
2264 +----------------------------------------
2265 +There were three GIT trees for aufs3, aufs3-linux.git,
2266 +aufs3-standalone.git, and aufs-util.git. Note that there is no "3" in
2267 +"aufs-util.git."
2268 +While the aufs-util is always necessary, you need either of aufs3-linux
2269 +or aufs3-standalone.
2270 +
2271 +The aufs3-linux tree includes the whole linux mainline GIT tree,
2272 +git://git.kernel.org/.../torvalds/linux.git.
2273 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2274 +build aufs3 as an external kernel module.
2275 +Several extra patches are not included in this tree. Only
2276 +aufs3-standalong tree contains them. They are describe in the later
2277 +section "Configuration and Compilation."
2278 +
2279 +On the other hand, the aufs3-standalone tree has only aufs source files
2280 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2281 +But you need to apply all aufs patches manually.
2282 +
2283 +You will find GIT branches whose name is in form of "aufs3.x" where "x"
2284 +represents the linux kernel version, "linux-3.x". For instance,
2285 +"aufs3.0" is for linux-3.0. For latest "linux-3.x-rcN", use
2286 +"aufs3.x-rcN" branch.
2287 +
2288 +o aufs3-linux tree
2289 +$ git clone --reference /your/linux/git/tree \
2290 +       git://git.code.sf.net/p/aufs/aufs3-linux aufs3-linux.git
2291 +- if you don't have linux GIT tree, then remove "--reference ..."
2292 +$ cd aufs3-linux.git
2293 +$ git checkout origin/aufs3.0
2294 +
2295 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2296 +leave the patch-work to GIT.
2297 +$ cd /your/linux/git/tree
2298 +$ git remote add aufs3 https://github.com/sfjro/aufs3-linux.git
2299 +- aufs3-linux.git tree also exists on github.
2300 +$ git fetch aufs3
2301 +$ git checkout -b my3.14 v3.14
2302 +$ (add your change...)
2303 +$ git pull aufs3 aufs3.14
2304 +- now you have v3.14 + your_changes + aufs3.14 in you my3.14 branch.
2305 +- you may need to solve some conflicts between your_changes and
2306 +  aufs3.14. in this case, git-rerere is recommended so that you can
2307 +  solve the similar confilicts automatically when you upgrade to 3.15 or
2308 +  later in the future.
2309 +
2310 +o aufs3-standalone tree
2311 +$ git clone git://git.code.sf.net/p/aufs/aufs3-standalone \
2312 +       aufs3-standalone.git
2313 +$ cd aufs3-standalone.git
2314 +$ git checkout origin/aufs3.0
2315 +
2316 +o aufs-util tree
2317 +$ git clone git://git.code.sf.net/p/aufs/aufs-util \
2318 +       aufs-util.git
2319 +$ cd aufs-util.git
2320 +$ git checkout origin/aufs3.0
2321 +
2322 +Note: The 3.x-rcN branch is to be used with `rc' kernel versions ONLY.
2323 +The minor version number, 'x' in '3.x', of aufs may not always
2324 +follow the minor version number of the kernel.
2325 +Because changes in the kernel that cause the use of a new
2326 +minor version number do not always require changes to aufs-util.
2327 +
2328 +Since aufs-util has its own minor version number, you may not be
2329 +able to find a GIT branch in aufs-util for your kernel's
2330 +exact minor version number.
2331 +In this case, you should git-checkout the branch for the
2332 +nearest lower number.
2333 +
2334 +For (an unreleased) example:
2335 +If you are using "linux-3.10" and the "aufs3.10" branch
2336 +does not exist in aufs-util repository, then "aufs3.9", "aufs3.8"
2337 +or something numerically smaller is the branch for your kernel.
2338 +
2339 +Also you can view all branches by
2340 +       $ git branch -a
2341 +
2342 +
2343 +3. Configuration and Compilation
2344 +----------------------------------------
2345 +Make sure you have git-checkout'ed the correct branch.
2346 +
2347 +For aufs3-linux tree,
2348 +- enable CONFIG_AUFS_FS.
2349 +- set other aufs configurations if necessary.
2350 +
2351 +For aufs3-standalone tree,
2352 +There are several ways to build.
2353 +
2354 +1.
2355 +- apply ./aufs3-kbuild.patch to your kernel source files.
2356 +- apply ./aufs3-base.patch too.
2357 +- apply ./aufs3-mmap.patch too.
2358 +- apply ./aufs3-standalone.patch too, if you have a plan to set
2359 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs3-standalone.patch.
2360 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2361 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2362 +- enable CONFIG_AUFS_FS, you can select either
2363 +  =m or =y.
2364 +- and build your kernel as usual.
2365 +- install the built kernel.
2366 +  Note: Since linux-3.9, every filesystem module requires an alias
2367 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2368 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2369 +- install the header files too by "make headers_install" to the
2370 +  directory where you specify. By default, it is $PWD/usr.
2371 +  "make help" shows a brief note for headers_install.
2372 +- and reboot your system.
2373 +
2374 +2.
2375 +- module only (CONFIG_AUFS_FS=m).
2376 +- apply ./aufs3-base.patch to your kernel source files.
2377 +- apply ./aufs3-mmap.patch too.
2378 +- apply ./aufs3-standalone.patch too.
2379 +- build your kernel, don't forget "make headers_install", and reboot.
2380 +- edit ./config.mk and set other aufs configurations if necessary.
2381 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2382 +  every aufs configurations.
2383 +- build the module by simple "make".
2384 +  Note: Since linux-3.9, every filesystem module requires an alias
2385 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2386 +  modules.aliases file.
2387 +- you can specify ${KDIR} make variable which points to your kernel
2388 +  source tree.
2389 +- install the files
2390 +  + run "make install" to install the aufs module, or copy the built
2391 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2392 +  + run "make install_headers" (instead of headers_install) to install
2393 +    the modified aufs header file (you can specify DESTDIR which is
2394 +    available in aufs standalone version's Makefile only), or copy
2395 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2396 +    you like manually. By default, the target directory is $PWD/usr.
2397 +- no need to apply aufs3-kbuild.patch, nor copying source files to your
2398 +  kernel source tree.
2399 +
2400 +Note: The header file aufs_type.h is necessary to build aufs-util
2401 +      as well as "make headers_install" in the kernel source tree.
2402 +      headers_install is subject to be forgotten, but it is essentially
2403 +      necessary, not only for building aufs-util.
2404 +      You may not meet problems without headers_install in some older
2405 +      version though.
2406 +
2407 +And then,
2408 +- read README in aufs-util, build and install it
2409 +- note that your distribution may contain an obsoleted version of
2410 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2411 +  utilities, make sure that your compiler refers the correct aufs header
2412 +  file which is built by "make headers_install."
2413 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2414 +  then run "make install_ulib" too. And refer to the aufs manual in
2415 +  detail.
2416 +
2417 +There several other patches in aufs3-standalone.git. They are all
2418 +optional. When you meet some problems, they will help you.
2419 +- aufs3-loopback.patch
2420 +  Supports a nested loopback mount in a branch-fs. This patch is
2421 +  unnecessary until aufs produces a message like "you may want to try
2422 +  another patch for loopback file".
2423 +- vfs-ino.patch
2424 +  Modifies a system global kernel internal function get_next_ino() in
2425 +  order to stop assigning 0 for an inode-number. Not directly related to
2426 +  aufs, but recommended generally.
2427 +- tmpfs-idr.patch
2428 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2429 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2430 +  duplication of inode number, which is important for backup tools and
2431 +  other utilities. When you find aufs XINO files for tmpfs branch
2432 +  growing too much, try this patch.
2433 +
2434 +
2435 +4. Usage
2436 +----------------------------------------
2437 +At first, make sure aufs-util are installed, and please read the aufs
2438 +manual, aufs.5 in aufs-util.git tree.
2439 +$ man -l aufs.5
2440 +
2441 +And then,
2442 +$ mkdir /tmp/rw /tmp/aufs
2443 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2444 +
2445 +Here is another example. The result is equivalent.
2446 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2447 +  Or
2448 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2449 +# mount -o remount,append:${HOME} /tmp/aufs
2450 +
2451 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2452 +you modify a file under /tmp/aufs, the one on your home directory is
2453 +not affected, instead the same named file will be newly created under
2454 +/tmp/rw. And all of your modification to a file will be applied to
2455 +the one under /tmp/rw. This is called the file based Copy on Write
2456 +(COW) method.
2457 +Aufs mount options are described in aufs.5.
2458 +If you run chroot or something and make your aufs as a root directory,
2459 +then you need to customize the shutdown script. See the aufs manual in
2460 +detail.
2461 +
2462 +Additionally, there are some sample usages of aufs which are a
2463 +diskless system with network booting, and LiveCD over NFS.
2464 +See sample dir in CVS tree on SourceForge.
2465 +
2466 +
2467 +5. Contact
2468 +----------------------------------------
2469 +When you have any problems or strange behaviour in aufs, please let me
2470 +know with:
2471 +- /proc/mounts (instead of the output of mount(8))
2472 +- /sys/module/aufs/*
2473 +- /sys/fs/aufs/* (if you have them)
2474 +- /debug/aufs/* (if you have them)
2475 +- linux kernel version
2476 +  if your kernel is not plain, for example modified by distributor,
2477 +  the url where i can download its source is necessary too.
2478 +- aufs version which was printed at loading the module or booting the
2479 +  system, instead of the date you downloaded.
2480 +- configuration (define/undefine CONFIG_AUFS_xxx)
2481 +- kernel configuration or /proc/config.gz (if you have it)
2482 +- behaviour which you think to be incorrect
2483 +- actual operation, reproducible one is better
2484 +- mailto: aufs-users at lists.sourceforge.net
2485 +
2486 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2487 +and Feature Requests) on SourceForge. Please join and write to
2488 +aufs-users ML.
2489 +
2490 +
2491 +6. Acknowledgements
2492 +----------------------------------------
2493 +Thanks to everyone who have tried and are using aufs, whoever
2494 +have reported a bug or any feedback.
2495 +
2496 +Especially donators:
2497 +Tomas Matejicek(slax.org) made a donation (much more than once).
2498 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2499 +       scripts) is making "doubling" donations.
2500 +       Unfortunately I cannot list all of the donators, but I really
2501 +       appreciate.
2502 +       It ends Aug 2010, but the ordinary donation URL is still available.
2503 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2504 +Dai Itasaka made a donation (2007/8).
2505 +Chuck Smith made a donation (2008/4, 10 and 12).
2506 +Henk Schoneveld made a donation (2008/9).
2507 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2508 +Francois Dupoux made a donation (2008/11).
2509 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2510 +       aufs2 GIT tree (2009/2).
2511 +William Grant made a donation (2009/3).
2512 +Patrick Lane made a donation (2009/4).
2513 +The Mail Archive (mail-archive.com) made donations (2009/5).
2514 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2515 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2516 +Pavel Pronskiy made a donation (2011/2).
2517 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2518 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2519 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2520 +11).
2521 +Sam Liddicott made a donation (2011/9).
2522 +Era Scarecrow made a donation (2013/4).
2523 +Bor Ratajc made a donation (2013/4).
2524 +Alessandro Gorreta made a donation (2013/4).
2525 +POIRETTE Marc made a donation (2013/4).
2526 +Alessandro Gorreta made a donation (2013/4).
2527 +lauri kasvandik made a donation (2013/5).
2528 +"pemasu from Finland" made a donation (2013/7).
2529 +The Parted Magic Project made a donation (2013/9 and 11).
2530 +Pavel Barta made a donation (2013/10).
2531 +Nikolay Pertsev made a donation (2014/5).
2532 +James B made a donation (2014/7).
2533 +Stefano Di Biase made a donation (2014/8).
2534 +Daniel Epellei made a donation (2015/1).
2535 +
2536 +Thank you very much.
2537 +Donations are always, including future donations, very important and
2538 +helpful for me to keep on developing aufs.
2539 +
2540 +
2541 +7.
2542 +----------------------------------------
2543 +If you are an experienced user, no explanation is needed. Aufs is
2544 +just a linux filesystem.
2545 +
2546 +
2547 +Enjoy!
2548 +
2549 +# Local variables: ;
2550 +# mode: text;
2551 +# End: ;
2552 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2553 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2554 +++ linux/fs/aufs/aufs.h        2015-04-13 15:10:20.776823376 +0200
2555 @@ -0,0 +1,59 @@
2556 +/*
2557 + * Copyright (C) 2005-2015 Junjiro R. Okajima
2558 + *
2559 + * This program, aufs is free software; you can redistribute it and/or modify
2560 + * it under the terms of the GNU General Public License as published by
2561 + * the Free Software Foundation; either version 2 of the License, or
2562 + * (at your option) any later version.
2563 + *
2564 + * This program is distributed in the hope that it will be useful,
2565 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2566 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2567 + * GNU General Public License for more details.
2568 + *
2569 + * You should have received a copy of the GNU General Public License
2570 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2571 + */
2572 +
2573 +/*
2574 + * all header files
2575 + */
2576 +
2577 +#ifndef __AUFS_H__
2578 +#define __AUFS_H__
2579 +
2580 +#ifdef __KERNEL__
2581 +
2582 +#define AuStub(type, name, body, ...) \
2583 +       static inline type name(__VA_ARGS__) { body; }
2584 +
2585 +#define AuStubVoid(name, ...) \
2586 +       AuStub(void, name, , __VA_ARGS__)
2587 +#define AuStubInt0(name, ...) \
2588 +       AuStub(int, name, return 0, __VA_ARGS__)
2589 +
2590 +#include "debug.h"
2591 +
2592 +#include "branch.h"
2593 +#include "cpup.h"
2594 +#include "dcsub.h"
2595 +#include "dbgaufs.h"
2596 +#include "dentry.h"
2597 +#include "dir.h"
2598 +#include "dynop.h"
2599 +#include "file.h"
2600 +#include "fstype.h"
2601 +#include "inode.h"
2602 +#include "loop.h"
2603 +#include "module.h"
2604 +#include "opts.h"
2605 +#include "rwsem.h"
2606 +#include "spl.h"
2607 +#include "super.h"
2608 +#include "sysaufs.h"
2609 +#include "vfsub.h"
2610 +#include "whout.h"
2611 +#include "wkq.h"
2612 +
2613 +#endif /* __KERNEL__ */
2614 +#endif /* __AUFS_H__ */
2615 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2616 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2617 +++ linux/fs/aufs/branch.c      2015-04-13 15:10:20.780156788 +0200
2618 @@ -0,0 +1,1406 @@
2619 +/*
2620 + * Copyright (C) 2005-2015 Junjiro R. Okajima
2621 + *
2622 + * This program, aufs is free software; you can redistribute it and/or modify
2623 + * it under the terms of the GNU General Public License as published by
2624 + * the Free Software Foundation; either version 2 of the License, or
2625 + * (at your option) any later version.
2626 + *
2627 + * This program is distributed in the hope that it will be useful,
2628 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2629 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2630 + * GNU General Public License for more details.
2631 + *
2632 + * You should have received a copy of the GNU General Public License
2633 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2634 + */
2635 +
2636 +/*
2637 + * branch management
2638 + */
2639 +
2640 +#include <linux/compat.h>
2641 +#include <linux/statfs.h>
2642 +#include "aufs.h"
2643 +
2644 +/*
2645 + * free a single branch
2646 + */
2647 +static void au_br_do_free(struct au_branch *br)
2648 +{
2649 +       int i;
2650 +       struct au_wbr *wbr;
2651 +       struct au_dykey **key;
2652 +
2653 +       au_hnotify_fin_br(br);
2654 +
2655 +       if (br->br_xino.xi_file)
2656 +               fput(br->br_xino.xi_file);
2657 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
2658 +
2659 +       AuDebugOn(atomic_read(&br->br_count));
2660 +
2661 +       wbr = br->br_wbr;
2662 +       if (wbr) {
2663 +               for (i = 0; i < AuBrWh_Last; i++)
2664 +                       dput(wbr->wbr_wh[i]);
2665 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2666 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2667 +       }
2668 +
2669 +       if (br->br_fhsm) {
2670 +               au_br_fhsm_fin(br->br_fhsm);
2671 +               kfree(br->br_fhsm);
2672 +       }
2673 +
2674 +       key = br->br_dykey;
2675 +       for (i = 0; i < AuBrDynOp; i++, key++)
2676 +               if (*key)
2677 +                       au_dy_put(*key);
2678 +               else
2679 +                       break;
2680 +
2681 +       /* recursive lock, s_umount of branch's */
2682 +       lockdep_off();
2683 +       path_put(&br->br_path);
2684 +       lockdep_on();
2685 +       kfree(wbr);
2686 +       kfree(br);
2687 +}
2688 +
2689 +/*
2690 + * frees all branches
2691 + */
2692 +void au_br_free(struct au_sbinfo *sbinfo)
2693 +{
2694 +       aufs_bindex_t bmax;
2695 +       struct au_branch **br;
2696 +
2697 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2698 +
2699 +       bmax = sbinfo->si_bend + 1;
2700 +       br = sbinfo->si_branch;
2701 +       while (bmax--)
2702 +               au_br_do_free(*br++);
2703 +}
2704 +
2705 +/*
2706 + * find the index of a branch which is specified by @br_id.
2707 + */
2708 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2709 +{
2710 +       aufs_bindex_t bindex, bend;
2711 +
2712 +       bend = au_sbend(sb);
2713 +       for (bindex = 0; bindex <= bend; bindex++)
2714 +               if (au_sbr_id(sb, bindex) == br_id)
2715 +                       return bindex;
2716 +       return -1;
2717 +}
2718 +
2719 +/* ---------------------------------------------------------------------- */
2720 +
2721 +/*
2722 + * add a branch
2723 + */
2724 +
2725 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2726 +                       struct dentry *h_root)
2727 +{
2728 +       if (unlikely(h_adding == h_root
2729 +                    || au_test_loopback_overlap(sb, h_adding)))
2730 +               return 1;
2731 +       if (h_adding->d_sb != h_root->d_sb)
2732 +               return 0;
2733 +       return au_test_subdir(h_adding, h_root)
2734 +               || au_test_subdir(h_root, h_adding);
2735 +}
2736 +
2737 +/*
2738 + * returns a newly allocated branch. @new_nbranch is a number of branches
2739 + * after adding a branch.
2740 + */
2741 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2742 +                                    int perm)
2743 +{
2744 +       struct au_branch *add_branch;
2745 +       struct dentry *root;
2746 +       int err;
2747 +
2748 +       err = -ENOMEM;
2749 +       root = sb->s_root;
2750 +       add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
2751 +       if (unlikely(!add_branch))
2752 +               goto out;
2753 +
2754 +       err = au_hnotify_init_br(add_branch, perm);
2755 +       if (unlikely(err))
2756 +               goto out_br;
2757 +
2758 +       add_branch->br_wbr = NULL;
2759 +       if (au_br_writable(perm)) {
2760 +               /* may be freed separately at changing the branch permission */
2761 +               add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
2762 +                                            GFP_NOFS);
2763 +               if (unlikely(!add_branch->br_wbr))
2764 +                       goto out_hnotify;
2765 +       }
2766 +
2767 +       add_branch->br_fhsm = NULL;
2768 +       if (au_br_fhsm(perm)) {
2769 +               err = au_fhsm_br_alloc(add_branch);
2770 +               if (unlikely(err))
2771 +                       goto out_wbr;
2772 +       }
2773 +
2774 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
2775 +       if (!err)
2776 +               err = au_di_realloc(au_di(root), new_nbranch);
2777 +       if (!err)
2778 +               err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
2779 +       if (!err)
2780 +               return add_branch; /* success */
2781 +
2782 +out_wbr:
2783 +       kfree(add_branch->br_wbr);
2784 +out_hnotify:
2785 +       au_hnotify_fin_br(add_branch);
2786 +out_br:
2787 +       kfree(add_branch);
2788 +out:
2789 +       return ERR_PTR(err);
2790 +}
2791 +
2792 +/*
2793 + * test if the branch permission is legal or not.
2794 + */
2795 +static int test_br(struct inode *inode, int brperm, char *path)
2796 +{
2797 +       int err;
2798 +
2799 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
2800 +       if (!err)
2801 +               goto out;
2802 +
2803 +       err = -EINVAL;
2804 +       pr_err("write permission for readonly mount or inode, %s\n", path);
2805 +
2806 +out:
2807 +       return err;
2808 +}
2809 +
2810 +/*
2811 + * returns:
2812 + * 0: success, the caller will add it
2813 + * plus: success, it is already unified, the caller should ignore it
2814 + * minus: error
2815 + */
2816 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
2817 +{
2818 +       int err;
2819 +       aufs_bindex_t bend, bindex;
2820 +       struct dentry *root;
2821 +       struct inode *inode, *h_inode;
2822 +
2823 +       root = sb->s_root;
2824 +       bend = au_sbend(sb);
2825 +       if (unlikely(bend >= 0
2826 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
2827 +               err = 1;
2828 +               if (!remount) {
2829 +                       err = -EINVAL;
2830 +                       pr_err("%s duplicated\n", add->pathname);
2831 +               }
2832 +               goto out;
2833 +       }
2834 +
2835 +       err = -ENOSPC; /* -E2BIG; */
2836 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2837 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
2838 +               pr_err("number of branches exceeded %s\n", add->pathname);
2839 +               goto out;
2840 +       }
2841 +
2842 +       err = -EDOM;
2843 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
2844 +               pr_err("bad index %d\n", add->bindex);
2845 +               goto out;
2846 +       }
2847 +
2848 +       inode = add->path.dentry->d_inode;
2849 +       err = -ENOENT;
2850 +       if (unlikely(!inode->i_nlink)) {
2851 +               pr_err("no existence %s\n", add->pathname);
2852 +               goto out;
2853 +       }
2854 +
2855 +       err = -EINVAL;
2856 +       if (unlikely(inode->i_sb == sb)) {
2857 +               pr_err("%s must be outside\n", add->pathname);
2858 +               goto out;
2859 +       }
2860 +
2861 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
2862 +               pr_err("unsupported filesystem, %s (%s)\n",
2863 +                      add->pathname, au_sbtype(inode->i_sb));
2864 +               goto out;
2865 +       }
2866 +
2867 +       if (unlikely(inode->i_sb->s_stack_depth)) {
2868 +               pr_err("already stacked, %s (%s)\n",
2869 +                      add->pathname, au_sbtype(inode->i_sb));
2870 +               goto out;
2871 +       }
2872 +
2873 +       err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
2874 +       if (unlikely(err))
2875 +               goto out;
2876 +
2877 +       if (bend < 0)
2878 +               return 0; /* success */
2879 +
2880 +       err = -EINVAL;
2881 +       for (bindex = 0; bindex <= bend; bindex++)
2882 +               if (unlikely(test_overlap(sb, add->path.dentry,
2883 +                                         au_h_dptr(root, bindex)))) {
2884 +                       pr_err("%s is overlapped\n", add->pathname);
2885 +                       goto out;
2886 +               }
2887 +
2888 +       err = 0;
2889 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
2890 +               h_inode = au_h_dptr(root, 0)->d_inode;
2891 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
2892 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
2893 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
2894 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
2895 +                               add->pathname,
2896 +                               i_uid_read(inode), i_gid_read(inode),
2897 +                               (inode->i_mode & S_IALLUGO),
2898 +                               i_uid_read(h_inode), i_gid_read(h_inode),
2899 +                               (h_inode->i_mode & S_IALLUGO));
2900 +       }
2901 +
2902 +out:
2903 +       return err;
2904 +}
2905 +
2906 +/*
2907 + * initialize or clean the whiteouts for an adding branch
2908 + */
2909 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
2910 +                        int new_perm)
2911 +{
2912 +       int err, old_perm;
2913 +       aufs_bindex_t bindex;
2914 +       struct mutex *h_mtx;
2915 +       struct au_wbr *wbr;
2916 +       struct au_hinode *hdir;
2917 +
2918 +       err = vfsub_mnt_want_write(au_br_mnt(br));
2919 +       if (unlikely(err))
2920 +               goto out;
2921 +
2922 +       wbr = br->br_wbr;
2923 +       old_perm = br->br_perm;
2924 +       br->br_perm = new_perm;
2925 +       hdir = NULL;
2926 +       h_mtx = NULL;
2927 +       bindex = au_br_index(sb, br->br_id);
2928 +       if (0 <= bindex) {
2929 +               hdir = au_hi(sb->s_root->d_inode, bindex);
2930 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
2931 +       } else {
2932 +               h_mtx = &au_br_dentry(br)->d_inode->i_mutex;
2933 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
2934 +       }
2935 +       if (!wbr)
2936 +               err = au_wh_init(br, sb);
2937 +       else {
2938 +               wbr_wh_write_lock(wbr);
2939 +               err = au_wh_init(br, sb);
2940 +               wbr_wh_write_unlock(wbr);
2941 +       }
2942 +       if (hdir)
2943 +               au_hn_imtx_unlock(hdir);
2944 +       else
2945 +               mutex_unlock(h_mtx);
2946 +       vfsub_mnt_drop_write(au_br_mnt(br));
2947 +       br->br_perm = old_perm;
2948 +
2949 +       if (!err && wbr && !au_br_writable(new_perm)) {
2950 +               kfree(wbr);
2951 +               br->br_wbr = NULL;
2952 +       }
2953 +
2954 +out:
2955 +       return err;
2956 +}
2957 +
2958 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
2959 +                      int perm)
2960 +{
2961 +       int err;
2962 +       struct kstatfs kst;
2963 +       struct au_wbr *wbr;
2964 +
2965 +       wbr = br->br_wbr;
2966 +       au_rw_init(&wbr->wbr_wh_rwsem);
2967 +       memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
2968 +       atomic_set(&wbr->wbr_wh_running, 0);
2969 +       wbr->wbr_bytes = 0;
2970 +
2971 +       /*
2972 +        * a limit for rmdir/rename a dir
2973 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
2974 +        */
2975 +       err = vfs_statfs(&br->br_path, &kst);
2976 +       if (unlikely(err))
2977 +               goto out;
2978 +       err = -EINVAL;
2979 +       if (kst.f_namelen >= NAME_MAX)
2980 +               err = au_br_init_wh(sb, br, perm);
2981 +       else
2982 +               pr_err("%pd(%s), unsupported namelen %ld\n",
2983 +                      au_br_dentry(br),
2984 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
2985 +
2986 +out:
2987 +       return err;
2988 +}
2989 +
2990 +/* initialize a new branch */
2991 +static int au_br_init(struct au_branch *br, struct super_block *sb,
2992 +                     struct au_opt_add *add)
2993 +{
2994 +       int err;
2995 +
2996 +       err = 0;
2997 +       memset(&br->br_xino, 0, sizeof(br->br_xino));
2998 +       mutex_init(&br->br_xino.xi_nondir_mtx);
2999 +       br->br_perm = add->perm;
3000 +       br->br_path = add->path; /* set first, path_get() later */
3001 +       spin_lock_init(&br->br_dykey_lock);
3002 +       memset(br->br_dykey, 0, sizeof(br->br_dykey));
3003 +       atomic_set(&br->br_count, 0);
3004 +       atomic_set(&br->br_xino_running, 0);
3005 +       br->br_id = au_new_br_id(sb);
3006 +       AuDebugOn(br->br_id < 0);
3007 +
3008 +       if (au_br_writable(add->perm)) {
3009 +               err = au_wbr_init(br, sb, add->perm);
3010 +               if (unlikely(err))
3011 +                       goto out_err;
3012 +       }
3013 +
3014 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3015 +               err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
3016 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3017 +               if (unlikely(err)) {
3018 +                       AuDebugOn(br->br_xino.xi_file);
3019 +                       goto out_err;
3020 +               }
3021 +       }
3022 +
3023 +       sysaufs_br_init(br);
3024 +       path_get(&br->br_path);
3025 +       goto out; /* success */
3026 +
3027 +out_err:
3028 +       memset(&br->br_path, 0, sizeof(br->br_path));
3029 +out:
3030 +       return err;
3031 +}
3032 +
3033 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3034 +                            struct au_branch *br, aufs_bindex_t bend,
3035 +                            aufs_bindex_t amount)
3036 +{
3037 +       struct au_branch **brp;
3038 +
3039 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3040 +
3041 +       brp = sbinfo->si_branch + bindex;
3042 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3043 +       *brp = br;
3044 +       sbinfo->si_bend++;
3045 +       if (unlikely(bend < 0))
3046 +               sbinfo->si_bend = 0;
3047 +}
3048 +
3049 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3050 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3051 +{
3052 +       struct au_hdentry *hdp;
3053 +
3054 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3055 +
3056 +       hdp = dinfo->di_hdentry + bindex;
3057 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3058 +       au_h_dentry_init(hdp);
3059 +       dinfo->di_bend++;
3060 +       if (unlikely(bend < 0))
3061 +               dinfo->di_bstart = 0;
3062 +}
3063 +
3064 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3065 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3066 +{
3067 +       struct au_hinode *hip;
3068 +
3069 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3070 +
3071 +       hip = iinfo->ii_hinode + bindex;
3072 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3073 +       hip->hi_inode = NULL;
3074 +       au_hn_init(hip);
3075 +       iinfo->ii_bend++;
3076 +       if (unlikely(bend < 0))
3077 +               iinfo->ii_bstart = 0;
3078 +}
3079 +
3080 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3081 +                        aufs_bindex_t bindex)
3082 +{
3083 +       struct dentry *root, *h_dentry;
3084 +       struct inode *root_inode;
3085 +       aufs_bindex_t bend, amount;
3086 +
3087 +       root = sb->s_root;
3088 +       root_inode = root->d_inode;
3089 +       bend = au_sbend(sb);
3090 +       amount = bend + 1 - bindex;
3091 +       h_dentry = au_br_dentry(br);
3092 +       au_sbilist_lock();
3093 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
3094 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
3095 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
3096 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3097 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
3098 +                     /*flags*/0);
3099 +       au_sbilist_unlock();
3100 +}
3101 +
3102 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3103 +{
3104 +       int err;
3105 +       aufs_bindex_t bend, add_bindex;
3106 +       struct dentry *root, *h_dentry;
3107 +       struct inode *root_inode;
3108 +       struct au_branch *add_branch;
3109 +
3110 +       root = sb->s_root;
3111 +       root_inode = root->d_inode;
3112 +       IMustLock(root_inode);
3113 +       err = test_add(sb, add, remount);
3114 +       if (unlikely(err < 0))
3115 +               goto out;
3116 +       if (err) {
3117 +               err = 0;
3118 +               goto out; /* success */
3119 +       }
3120 +
3121 +       bend = au_sbend(sb);
3122 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
3123 +       err = PTR_ERR(add_branch);
3124 +       if (IS_ERR(add_branch))
3125 +               goto out;
3126 +
3127 +       err = au_br_init(add_branch, sb, add);
3128 +       if (unlikely(err)) {
3129 +               au_br_do_free(add_branch);
3130 +               goto out;
3131 +       }
3132 +
3133 +       add_bindex = add->bindex;
3134 +       if (!remount)
3135 +               au_br_do_add(sb, add_branch, add_bindex);
3136 +       else {
3137 +               sysaufs_brs_del(sb, add_bindex);
3138 +               au_br_do_add(sb, add_branch, add_bindex);
3139 +               sysaufs_brs_add(sb, add_bindex);
3140 +       }
3141 +
3142 +       h_dentry = add->path.dentry;
3143 +       if (!add_bindex) {
3144 +               au_cpup_attr_all(root_inode, /*force*/1);
3145 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3146 +       } else
3147 +               au_add_nlink(root_inode, h_dentry->d_inode);
3148 +
3149 +       /*
3150 +        * this test/set prevents aufs from handling unnecesary notify events
3151 +        * of xino files, in case of re-adding a writable branch which was
3152 +        * once detached from aufs.
3153 +        */
3154 +       if (au_xino_brid(sb) < 0
3155 +           && au_br_writable(add_branch->br_perm)
3156 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
3157 +           && add_branch->br_xino.xi_file
3158 +           && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
3159 +               au_xino_brid_set(sb, add_branch->br_id);
3160 +
3161 +out:
3162 +       return err;
3163 +}
3164 +
3165 +/* ---------------------------------------------------------------------- */
3166 +
3167 +static unsigned long long au_farray_cb(void *a,
3168 +                                      unsigned long long max __maybe_unused,
3169 +                                      void *arg)
3170 +{
3171 +       unsigned long long n;
3172 +       struct file **p, *f;
3173 +       struct au_sphlhead *files;
3174 +       struct au_finfo *finfo;
3175 +       struct super_block *sb = arg;
3176 +
3177 +       n = 0;
3178 +       p = a;
3179 +       files = &au_sbi(sb)->si_files;
3180 +       spin_lock(&files->spin);
3181 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
3182 +               f = finfo->fi_file;
3183 +               if (file_count(f)
3184 +                   && !special_file(file_inode(f)->i_mode)) {
3185 +                       get_file(f);
3186 +                       *p++ = f;
3187 +                       n++;
3188 +                       AuDebugOn(n > max);
3189 +               }
3190 +       }
3191 +       spin_unlock(&files->spin);
3192 +
3193 +       return n;
3194 +}
3195 +
3196 +static struct file **au_farray_alloc(struct super_block *sb,
3197 +                                    unsigned long long *max)
3198 +{
3199 +       *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
3200 +       return au_array_alloc(max, au_farray_cb, sb);
3201 +}
3202 +
3203 +static void au_farray_free(struct file **a, unsigned long long max)
3204 +{
3205 +       unsigned long long ull;
3206 +
3207 +       for (ull = 0; ull < max; ull++)
3208 +               if (a[ull])
3209 +                       fput(a[ull]);
3210 +       au_array_free(a);
3211 +}
3212 +
3213 +/* ---------------------------------------------------------------------- */
3214 +
3215 +/*
3216 + * delete a branch
3217 + */
3218 +
3219 +/* to show the line number, do not make it inlined function */
3220 +#define AuVerbose(do_info, fmt, ...) do { \
3221 +       if (do_info) \
3222 +               pr_info(fmt, ##__VA_ARGS__); \
3223 +} while (0)
3224 +
3225 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
3226 +                        aufs_bindex_t bend)
3227 +{
3228 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
3229 +}
3230 +
3231 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
3232 +                        aufs_bindex_t bend)
3233 +{
3234 +       return au_test_ibusy(dentry->d_inode, bstart, bend);
3235 +}
3236 +
3237 +/*
3238 + * test if the branch is deletable or not.
3239 + */
3240 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3241 +                           unsigned int sigen, const unsigned int verbose)
3242 +{
3243 +       int err, i, j, ndentry;
3244 +       aufs_bindex_t bstart, bend;
3245 +       struct au_dcsub_pages dpages;
3246 +       struct au_dpage *dpage;
3247 +       struct dentry *d;
3248 +
3249 +       err = au_dpages_init(&dpages, GFP_NOFS);
3250 +       if (unlikely(err))
3251 +               goto out;
3252 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3253 +       if (unlikely(err))
3254 +               goto out_dpages;
3255 +
3256 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3257 +               dpage = dpages.dpages + i;
3258 +               ndentry = dpage->ndentry;
3259 +               for (j = 0; !err && j < ndentry; j++) {
3260 +                       d = dpage->dentries[j];
3261 +                       AuDebugOn(au_dcount(d) <= 0);
3262 +                       if (!au_digen_test(d, sigen)) {
3263 +                               di_read_lock_child(d, AuLock_IR);
3264 +                               if (unlikely(au_dbrange_test(d))) {
3265 +                                       di_read_unlock(d, AuLock_IR);
3266 +                                       continue;
3267 +                               }
3268 +                       } else {
3269 +                               di_write_lock_child(d);
3270 +                               if (unlikely(au_dbrange_test(d))) {
3271 +                                       di_write_unlock(d);
3272 +                                       continue;
3273 +                               }
3274 +                               err = au_reval_dpath(d, sigen);
3275 +                               if (!err)
3276 +                                       di_downgrade_lock(d, AuLock_IR);
3277 +                               else {
3278 +                                       di_write_unlock(d);
3279 +                                       break;
3280 +                               }
3281 +                       }
3282 +
3283 +                       /* AuDbgDentry(d); */
3284 +                       bstart = au_dbstart(d);
3285 +                       bend = au_dbend(d);
3286 +                       if (bstart <= bindex
3287 +                           && bindex <= bend
3288 +                           && au_h_dptr(d, bindex)
3289 +                           && au_test_dbusy(d, bstart, bend)) {
3290 +                               err = -EBUSY;
3291 +                               AuVerbose(verbose, "busy %pd\n", d);
3292 +                               AuDbgDentry(d);
3293 +                       }
3294 +                       di_read_unlock(d, AuLock_IR);
3295 +               }
3296 +       }
3297 +
3298 +out_dpages:
3299 +       au_dpages_free(&dpages);
3300 +out:
3301 +       return err;
3302 +}
3303 +
3304 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3305 +                          unsigned int sigen, const unsigned int verbose)
3306 +{
3307 +       int err;
3308 +       unsigned long long max, ull;
3309 +       struct inode *i, **array;
3310 +       aufs_bindex_t bstart, bend;
3311 +
3312 +       array = au_iarray_alloc(sb, &max);
3313 +       err = PTR_ERR(array);
3314 +       if (IS_ERR(array))
3315 +               goto out;
3316 +
3317 +       err = 0;
3318 +       AuDbg("b%d\n", bindex);
3319 +       for (ull = 0; !err && ull < max; ull++) {
3320 +               i = array[ull];
3321 +               if (unlikely(!i))
3322 +                       break;
3323 +               if (i->i_ino == AUFS_ROOT_INO)
3324 +                       continue;
3325 +
3326 +               /* AuDbgInode(i); */
3327 +               if (au_iigen(i, NULL) == sigen)
3328 +                       ii_read_lock_child(i);
3329 +               else {
3330 +                       ii_write_lock_child(i);
3331 +                       err = au_refresh_hinode_self(i);
3332 +                       au_iigen_dec(i);
3333 +                       if (!err)
3334 +                               ii_downgrade_lock(i);
3335 +                       else {
3336 +                               ii_write_unlock(i);
3337 +                               break;
3338 +                       }
3339 +               }
3340 +
3341 +               bstart = au_ibstart(i);
3342 +               bend = au_ibend(i);
3343 +               if (bstart <= bindex
3344 +                   && bindex <= bend
3345 +                   && au_h_iptr(i, bindex)
3346 +                   && au_test_ibusy(i, bstart, bend)) {
3347 +                       err = -EBUSY;
3348 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3349 +                       AuDbgInode(i);
3350 +               }
3351 +               ii_read_unlock(i);
3352 +       }
3353 +       au_iarray_free(array, max);
3354 +
3355 +out:
3356 +       return err;
3357 +}
3358 +
3359 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3360 +                             const unsigned int verbose)
3361 +{
3362 +       int err;
3363 +       unsigned int sigen;
3364 +
3365 +       sigen = au_sigen(root->d_sb);
3366 +       DiMustNoWaiters(root);
3367 +       IiMustNoWaiters(root->d_inode);
3368 +       di_write_unlock(root);
3369 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3370 +       if (!err)
3371 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3372 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3373 +
3374 +       return err;
3375 +}
3376 +
3377 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3378 +                        struct file **to_free, int *idx)
3379 +{
3380 +       int err;
3381 +       unsigned char matched, root;
3382 +       aufs_bindex_t bindex, bend;
3383 +       struct au_fidir *fidir;
3384 +       struct au_hfile *hfile;
3385 +
3386 +       err = 0;
3387 +       root = IS_ROOT(file->f_path.dentry);
3388 +       if (root) {
3389 +               get_file(file);
3390 +               to_free[*idx] = file;
3391 +               (*idx)++;
3392 +               goto out;
3393 +       }
3394 +
3395 +       matched = 0;
3396 +       fidir = au_fi(file)->fi_hdir;
3397 +       AuDebugOn(!fidir);
3398 +       bend = au_fbend_dir(file);
3399 +       for (bindex = au_fbstart(file); bindex <= bend; bindex++) {
3400 +               hfile = fidir->fd_hfile + bindex;
3401 +               if (!hfile->hf_file)
3402 +                       continue;
3403 +
3404 +               if (hfile->hf_br->br_id == br_id) {
3405 +                       matched = 1;
3406 +                       break;
3407 +               }
3408 +       }
3409 +       if (matched)
3410 +               err = -EBUSY;
3411 +
3412 +out:
3413 +       return err;
3414 +}
3415 +
3416 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3417 +                         struct file **to_free, int opened)
3418 +{
3419 +       int err, idx;
3420 +       unsigned long long ull, max;
3421 +       aufs_bindex_t bstart;
3422 +       struct file *file, **array;
3423 +       struct dentry *root;
3424 +       struct au_hfile *hfile;
3425 +
3426 +       array = au_farray_alloc(sb, &max);
3427 +       err = PTR_ERR(array);
3428 +       if (IS_ERR(array))
3429 +               goto out;
3430 +
3431 +       err = 0;
3432 +       idx = 0;
3433 +       root = sb->s_root;
3434 +       di_write_unlock(root);
3435 +       for (ull = 0; ull < max; ull++) {
3436 +               file = array[ull];
3437 +               if (unlikely(!file))
3438 +                       break;
3439 +
3440 +               /* AuDbg("%pD\n", file); */
3441 +               fi_read_lock(file);
3442 +               bstart = au_fbstart(file);
3443 +               if (!d_is_dir(file->f_path.dentry)) {
3444 +                       hfile = &au_fi(file)->fi_htop;
3445 +                       if (hfile->hf_br->br_id == br_id)
3446 +                               err = -EBUSY;
3447 +               } else
3448 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3449 +               fi_read_unlock(file);
3450 +               if (unlikely(err))
3451 +                       break;
3452 +       }
3453 +       di_write_lock_child(root);
3454 +       au_farray_free(array, max);
3455 +       AuDebugOn(idx > opened);
3456 +
3457 +out:
3458 +       return err;
3459 +}
3460 +
3461 +static void br_del_file(struct file **to_free, unsigned long long opened,
3462 +                         aufs_bindex_t br_id)
3463 +{
3464 +       unsigned long long ull;
3465 +       aufs_bindex_t bindex, bstart, bend, bfound;
3466 +       struct file *file;
3467 +       struct au_fidir *fidir;
3468 +       struct au_hfile *hfile;
3469 +
3470 +       for (ull = 0; ull < opened; ull++) {
3471 +               file = to_free[ull];
3472 +               if (unlikely(!file))
3473 +                       break;
3474 +
3475 +               /* AuDbg("%pD\n", file); */
3476 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3477 +               bfound = -1;
3478 +               fidir = au_fi(file)->fi_hdir;
3479 +               AuDebugOn(!fidir);
3480 +               fi_write_lock(file);
3481 +               bstart = au_fbstart(file);
3482 +               bend = au_fbend_dir(file);
3483 +               for (bindex = bstart; bindex <= bend; bindex++) {
3484 +                       hfile = fidir->fd_hfile + bindex;
3485 +                       if (!hfile->hf_file)
3486 +                               continue;
3487 +
3488 +                       if (hfile->hf_br->br_id == br_id) {
3489 +                               bfound = bindex;
3490 +                               break;
3491 +                       }
3492 +               }
3493 +               AuDebugOn(bfound < 0);
3494 +               au_set_h_fptr(file, bfound, NULL);
3495 +               if (bfound == bstart) {
3496 +                       for (bstart++; bstart <= bend; bstart++)
3497 +                               if (au_hf_dir(file, bstart)) {
3498 +                                       au_set_fbstart(file, bstart);
3499 +                                       break;
3500 +                               }
3501 +               }
3502 +               fi_write_unlock(file);
3503 +       }
3504 +}
3505 +
3506 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3507 +                            const aufs_bindex_t bindex,
3508 +                            const aufs_bindex_t bend)
3509 +{
3510 +       struct au_branch **brp, **p;
3511 +
3512 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3513 +
3514 +       brp = sbinfo->si_branch + bindex;
3515 +       if (bindex < bend)
3516 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
3517 +       sbinfo->si_branch[0 + bend] = NULL;
3518 +       sbinfo->si_bend--;
3519 +
3520 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
3521 +       if (p)
3522 +               sbinfo->si_branch = p;
3523 +       /* harmless error */
3524 +}
3525 +
3526 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3527 +                            const aufs_bindex_t bend)
3528 +{
3529 +       struct au_hdentry *hdp, *p;
3530 +
3531 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3532 +
3533 +       hdp = dinfo->di_hdentry;
3534 +       if (bindex < bend)
3535 +               memmove(hdp + bindex, hdp + bindex + 1,
3536 +                       sizeof(*hdp) * (bend - bindex));
3537 +       hdp[0 + bend].hd_dentry = NULL;
3538 +       dinfo->di_bend--;
3539 +
3540 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
3541 +       if (p)
3542 +               dinfo->di_hdentry = p;
3543 +       /* harmless error */
3544 +}
3545 +
3546 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3547 +                            const aufs_bindex_t bend)
3548 +{
3549 +       struct au_hinode *hip, *p;
3550 +
3551 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3552 +
3553 +       hip = iinfo->ii_hinode + bindex;
3554 +       if (bindex < bend)
3555 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
3556 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
3557 +       au_hn_init(iinfo->ii_hinode + bend);
3558 +       iinfo->ii_bend--;
3559 +
3560 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
3561 +       if (p)
3562 +               iinfo->ii_hinode = p;
3563 +       /* harmless error */
3564 +}
3565 +
3566 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3567 +                        struct au_branch *br)
3568 +{
3569 +       aufs_bindex_t bend;
3570 +       struct au_sbinfo *sbinfo;
3571 +       struct dentry *root, *h_root;
3572 +       struct inode *inode, *h_inode;
3573 +       struct au_hinode *hinode;
3574 +
3575 +       SiMustWriteLock(sb);
3576 +
3577 +       root = sb->s_root;
3578 +       inode = root->d_inode;
3579 +       sbinfo = au_sbi(sb);
3580 +       bend = sbinfo->si_bend;
3581 +
3582 +       h_root = au_h_dptr(root, bindex);
3583 +       hinode = au_hi(inode, bindex);
3584 +       h_inode = au_igrab(hinode->hi_inode);
3585 +       au_hiput(hinode);
3586 +
3587 +       au_sbilist_lock();
3588 +       au_br_do_del_brp(sbinfo, bindex, bend);
3589 +       au_br_do_del_hdp(au_di(root), bindex, bend);
3590 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
3591 +       au_sbilist_unlock();
3592 +
3593 +       dput(h_root);
3594 +       iput(h_inode);
3595 +       au_br_do_free(br);
3596 +}
3597 +
3598 +static unsigned long long empty_cb(void *array, unsigned long long max,
3599 +                                  void *arg)
3600 +{
3601 +       return max;
3602 +}
3603 +
3604 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3605 +{
3606 +       int err, rerr, i;
3607 +       unsigned long long opened;
3608 +       unsigned int mnt_flags;
3609 +       aufs_bindex_t bindex, bend, br_id;
3610 +       unsigned char do_wh, verbose;
3611 +       struct au_branch *br;
3612 +       struct au_wbr *wbr;
3613 +       struct dentry *root;
3614 +       struct file **to_free;
3615 +
3616 +       err = 0;
3617 +       opened = 0;
3618 +       to_free = NULL;
3619 +       root = sb->s_root;
3620 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3621 +       if (bindex < 0) {
3622 +               if (remount)
3623 +                       goto out; /* success */
3624 +               err = -ENOENT;
3625 +               pr_err("%s no such branch\n", del->pathname);
3626 +               goto out;
3627 +       }
3628 +       AuDbg("bindex b%d\n", bindex);
3629 +
3630 +       err = -EBUSY;
3631 +       mnt_flags = au_mntflags(sb);
3632 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3633 +       bend = au_sbend(sb);
3634 +       if (unlikely(!bend)) {
3635 +               AuVerbose(verbose, "no more branches left\n");
3636 +               goto out;
3637 +       }
3638 +       br = au_sbr(sb, bindex);
3639 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3640 +
3641 +       br_id = br->br_id;
3642 +       opened = atomic_read(&br->br_count);
3643 +       if (unlikely(opened)) {
3644 +               to_free = au_array_alloc(&opened, empty_cb, NULL);
3645 +               err = PTR_ERR(to_free);
3646 +               if (IS_ERR(to_free))
3647 +                       goto out;
3648 +
3649 +               err = test_file_busy(sb, br_id, to_free, opened);
3650 +               if (unlikely(err)) {
3651 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3652 +                       goto out;
3653 +               }
3654 +       }
3655 +
3656 +       wbr = br->br_wbr;
3657 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3658 +       if (do_wh) {
3659 +               /* instead of WbrWhMustWriteLock(wbr) */
3660 +               SiMustWriteLock(sb);
3661 +               for (i = 0; i < AuBrWh_Last; i++) {
3662 +                       dput(wbr->wbr_wh[i]);
3663 +                       wbr->wbr_wh[i] = NULL;
3664 +               }
3665 +       }
3666 +
3667 +       err = test_children_busy(root, bindex, verbose);
3668 +       if (unlikely(err)) {
3669 +               if (do_wh)
3670 +                       goto out_wh;
3671 +               goto out;
3672 +       }
3673 +
3674 +       err = 0;
3675 +       if (to_free) {
3676 +               /*
3677 +                * now we confirmed the branch is deletable.
3678 +                * let's free the remaining opened dirs on the branch.
3679 +                */
3680 +               di_write_unlock(root);
3681 +               br_del_file(to_free, opened, br_id);
3682 +               di_write_lock_child(root);
3683 +       }
3684 +
3685 +       if (!remount)
3686 +               au_br_do_del(sb, bindex, br);
3687 +       else {
3688 +               sysaufs_brs_del(sb, bindex);
3689 +               au_br_do_del(sb, bindex, br);
3690 +               sysaufs_brs_add(sb, bindex);
3691 +       }
3692 +
3693 +       if (!bindex) {
3694 +               au_cpup_attr_all(root->d_inode, /*force*/1);
3695 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3696 +       } else
3697 +               au_sub_nlink(root->d_inode, del->h_path.dentry->d_inode);
3698 +       if (au_opt_test(mnt_flags, PLINK))
3699 +               au_plink_half_refresh(sb, br_id);
3700 +
3701 +       if (au_xino_brid(sb) == br_id)
3702 +               au_xino_brid_set(sb, -1);
3703 +       goto out; /* success */
3704 +
3705 +out_wh:
3706 +       /* revert */
3707 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3708 +       if (rerr)
3709 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3710 +                       del->pathname, rerr);
3711 +out:
3712 +       if (to_free)
3713 +               au_farray_free(to_free, opened);
3714 +       return err;
3715 +}
3716 +
3717 +/* ---------------------------------------------------------------------- */
3718 +
3719 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3720 +{
3721 +       int err;
3722 +       aufs_bindex_t bstart, bend;
3723 +       struct aufs_ibusy ibusy;
3724 +       struct inode *inode, *h_inode;
3725 +
3726 +       err = -EPERM;
3727 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
3728 +               goto out;
3729 +
3730 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
3731 +       if (!err)
3732 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
3733 +       if (unlikely(err)) {
3734 +               err = -EFAULT;
3735 +               AuTraceErr(err);
3736 +               goto out;
3737 +       }
3738 +
3739 +       err = -EINVAL;
3740 +       si_read_lock(sb, AuLock_FLUSH);
3741 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
3742 +               goto out_unlock;
3743 +
3744 +       err = 0;
3745 +       ibusy.h_ino = 0; /* invalid */
3746 +       inode = ilookup(sb, ibusy.ino);
3747 +       if (!inode
3748 +           || inode->i_ino == AUFS_ROOT_INO
3749 +           || is_bad_inode(inode))
3750 +               goto out_unlock;
3751 +
3752 +       ii_read_lock_child(inode);
3753 +       bstart = au_ibstart(inode);
3754 +       bend = au_ibend(inode);
3755 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
3756 +               h_inode = au_h_iptr(inode, ibusy.bindex);
3757 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
3758 +                       ibusy.h_ino = h_inode->i_ino;
3759 +       }
3760 +       ii_read_unlock(inode);
3761 +       iput(inode);
3762 +
3763 +out_unlock:
3764 +       si_read_unlock(sb);
3765 +       if (!err) {
3766 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
3767 +               if (unlikely(err)) {
3768 +                       err = -EFAULT;
3769 +                       AuTraceErr(err);
3770 +               }
3771 +       }
3772 +out:
3773 +       return err;
3774 +}
3775 +
3776 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
3777 +{
3778 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
3779 +}
3780 +
3781 +#ifdef CONFIG_COMPAT
3782 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
3783 +{
3784 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
3785 +}
3786 +#endif
3787 +
3788 +/* ---------------------------------------------------------------------- */
3789 +
3790 +/*
3791 + * change a branch permission
3792 + */
3793 +
3794 +static void au_warn_ima(void)
3795 +{
3796 +#ifdef CONFIG_IMA
3797 +       /* since it doesn't support mark_files_ro() */
3798 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
3799 +#endif
3800 +}
3801 +
3802 +static int do_need_sigen_inc(int a, int b)
3803 +{
3804 +       return au_br_whable(a) && !au_br_whable(b);
3805 +}
3806 +
3807 +static int need_sigen_inc(int old, int new)
3808 +{
3809 +       return do_need_sigen_inc(old, new)
3810 +               || do_need_sigen_inc(new, old);
3811 +}
3812 +
3813 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
3814 +{
3815 +       int err, do_warn;
3816 +       unsigned int mnt_flags;
3817 +       unsigned long long ull, max;
3818 +       aufs_bindex_t br_id;
3819 +       unsigned char verbose, writer;
3820 +       struct file *file, *hf, **array;
3821 +       struct au_hfile *hfile;
3822 +
3823 +       mnt_flags = au_mntflags(sb);
3824 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3825 +
3826 +       array = au_farray_alloc(sb, &max);
3827 +       err = PTR_ERR(array);
3828 +       if (IS_ERR(array))
3829 +               goto out;
3830 +
3831 +       do_warn = 0;
3832 +       br_id = au_sbr_id(sb, bindex);
3833 +       for (ull = 0; ull < max; ull++) {
3834 +               file = array[ull];
3835 +               if (unlikely(!file))
3836 +                       break;
3837 +
3838 +               /* AuDbg("%pD\n", file); */
3839 +               fi_read_lock(file);
3840 +               if (unlikely(au_test_mmapped(file))) {
3841 +                       err = -EBUSY;
3842 +                       AuVerbose(verbose, "mmapped %pD\n", file);
3843 +                       AuDbgFile(file);
3844 +                       FiMustNoWaiters(file);
3845 +                       fi_read_unlock(file);
3846 +                       goto out_array;
3847 +               }
3848 +
3849 +               hfile = &au_fi(file)->fi_htop;
3850 +               hf = hfile->hf_file;
3851 +               if (!d_is_reg(file->f_path.dentry)
3852 +                   || !(file->f_mode & FMODE_WRITE)
3853 +                   || hfile->hf_br->br_id != br_id
3854 +                   || !(hf->f_mode & FMODE_WRITE))
3855 +                       array[ull] = NULL;
3856 +               else {
3857 +                       do_warn = 1;
3858 +                       get_file(file);
3859 +               }
3860 +
3861 +               FiMustNoWaiters(file);
3862 +               fi_read_unlock(file);
3863 +               fput(file);
3864 +       }
3865 +
3866 +       err = 0;
3867 +       if (do_warn)
3868 +               au_warn_ima();
3869 +
3870 +       for (ull = 0; ull < max; ull++) {
3871 +               file = array[ull];
3872 +               if (!file)
3873 +                       continue;
3874 +
3875 +               /* todo: already flushed? */
3876 +               /*
3877 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
3878 +                * approach which resets f_mode and calls mnt_drop_write() and
3879 +                * file_release_write() for each file, because the branch
3880 +                * attribute in aufs world is totally different from the native
3881 +                * fs rw/ro mode.
3882 +               */
3883 +               /* fi_read_lock(file); */
3884 +               hfile = &au_fi(file)->fi_htop;
3885 +               hf = hfile->hf_file;
3886 +               /* fi_read_unlock(file); */
3887 +               spin_lock(&hf->f_lock);
3888 +               writer = !!(hf->f_mode & FMODE_WRITER);
3889 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
3890 +               spin_unlock(&hf->f_lock);
3891 +               if (writer) {
3892 +                       put_write_access(file_inode(hf));
3893 +                       __mnt_drop_write(hf->f_path.mnt);
3894 +               }
3895 +       }
3896 +
3897 +out_array:
3898 +       au_farray_free(array, max);
3899 +out:
3900 +       AuTraceErr(err);
3901 +       return err;
3902 +}
3903 +
3904 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
3905 +             int *do_refresh)
3906 +{
3907 +       int err, rerr;
3908 +       aufs_bindex_t bindex;
3909 +       struct dentry *root;
3910 +       struct au_branch *br;
3911 +       struct au_br_fhsm *bf;
3912 +
3913 +       root = sb->s_root;
3914 +       bindex = au_find_dbindex(root, mod->h_root);
3915 +       if (bindex < 0) {
3916 +               if (remount)
3917 +                       return 0; /* success */
3918 +               err = -ENOENT;
3919 +               pr_err("%s no such branch\n", mod->path);
3920 +               goto out;
3921 +       }
3922 +       AuDbg("bindex b%d\n", bindex);
3923 +
3924 +       err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
3925 +       if (unlikely(err))
3926 +               goto out;
3927 +
3928 +       br = au_sbr(sb, bindex);
3929 +       AuDebugOn(mod->h_root != au_br_dentry(br));
3930 +       if (br->br_perm == mod->perm)
3931 +               return 0; /* success */
3932 +
3933 +       /* pre-allocate for non-fhsm --> fhsm */
3934 +       bf = NULL;
3935 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
3936 +               err = au_fhsm_br_alloc(br);
3937 +               if (unlikely(err))
3938 +                       goto out;
3939 +               bf = br->br_fhsm;
3940 +               br->br_fhsm = NULL;
3941 +       }
3942 +
3943 +       if (au_br_writable(br->br_perm)) {
3944 +               /* remove whiteout base */
3945 +               err = au_br_init_wh(sb, br, mod->perm);
3946 +               if (unlikely(err))
3947 +                       goto out_bf;
3948 +
3949 +               if (!au_br_writable(mod->perm)) {
3950 +                       /* rw --> ro, file might be mmapped */
3951 +                       DiMustNoWaiters(root);
3952 +                       IiMustNoWaiters(root->d_inode);
3953 +                       di_write_unlock(root);
3954 +                       err = au_br_mod_files_ro(sb, bindex);
3955 +                       /* aufs_write_lock() calls ..._child() */
3956 +                       di_write_lock_child(root);
3957 +
3958 +                       if (unlikely(err)) {
3959 +                               rerr = -ENOMEM;
3960 +                               br->br_wbr = kmalloc(sizeof(*br->br_wbr),
3961 +                                                    GFP_NOFS);
3962 +                               if (br->br_wbr)
3963 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
3964 +                               if (unlikely(rerr)) {
3965 +                                       AuIOErr("nested error %d (%d)\n",
3966 +                                               rerr, err);
3967 +                                       br->br_perm = mod->perm;
3968 +                               }
3969 +                       }
3970 +               }
3971 +       } else if (au_br_writable(mod->perm)) {
3972 +               /* ro --> rw */
3973 +               err = -ENOMEM;
3974 +               br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
3975 +               if (br->br_wbr) {
3976 +                       err = au_wbr_init(br, sb, mod->perm);
3977 +                       if (unlikely(err)) {
3978 +                               kfree(br->br_wbr);
3979 +                               br->br_wbr = NULL;
3980 +                       }
3981 +               }
3982 +       }
3983 +       if (unlikely(err))
3984 +               goto out_bf;
3985 +
3986 +       if (au_br_fhsm(br->br_perm)) {
3987 +               if (!au_br_fhsm(mod->perm)) {
3988 +                       /* fhsm --> non-fhsm */
3989 +                       au_br_fhsm_fin(br->br_fhsm);
3990 +                       kfree(br->br_fhsm);
3991 +                       br->br_fhsm = NULL;
3992 +               }
3993 +       } else if (au_br_fhsm(mod->perm))
3994 +               /* non-fhsm --> fhsm */
3995 +               br->br_fhsm = bf;
3996 +
3997 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
3998 +       br->br_perm = mod->perm;
3999 +       goto out; /* success */
4000 +
4001 +out_bf:
4002 +       kfree(bf);
4003 +out:
4004 +       AuTraceErr(err);
4005 +       return err;
4006 +}
4007 +
4008 +/* ---------------------------------------------------------------------- */
4009 +
4010 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4011 +{
4012 +       int err;
4013 +       struct kstatfs kstfs;
4014 +
4015 +       err = vfs_statfs(&br->br_path, &kstfs);
4016 +       if (!err) {
4017 +               stfs->f_blocks = kstfs.f_blocks;
4018 +               stfs->f_bavail = kstfs.f_bavail;
4019 +               stfs->f_files = kstfs.f_files;
4020 +               stfs->f_ffree = kstfs.f_ffree;
4021 +       }
4022 +
4023 +       return err;
4024 +}
4025 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4026 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4027 +++ linux/fs/aufs/branch.h      2015-04-13 15:10:20.780156788 +0200
4028 @@ -0,0 +1,267 @@
4029 +/*
4030 + * Copyright (C) 2005-2015 Junjiro R. Okajima
4031 + *
4032 + * This program, aufs is free software; you can redistribute it and/or modify
4033 + * it under the terms of the GNU General Public License as published by
4034 + * the Free Software Foundation; either version 2 of the License, or
4035 + * (at your option) any later version.
4036 + *
4037 + * This program is distributed in the hope that it will be useful,
4038 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4039 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4040 + * GNU General Public License for more details.
4041 + *
4042 + * You should have received a copy of the GNU General Public License
4043 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4044 + */
4045 +
4046 +/*
4047 + * branch filesystems and xino for them
4048 + */
4049 +
4050 +#ifndef __AUFS_BRANCH_H__
4051 +#define __AUFS_BRANCH_H__
4052 +
4053 +#ifdef __KERNEL__
4054 +
4055 +#include <linux/mount.h>
4056 +#include "dynop.h"
4057 +#include "rwsem.h"
4058 +#include "super.h"
4059 +
4060 +/* ---------------------------------------------------------------------- */
4061 +
4062 +/* a xino file */
4063 +struct au_xino_file {
4064 +       struct file             *xi_file;
4065 +       struct mutex            xi_nondir_mtx;
4066 +
4067 +       /* todo: make xino files an array to support huge inode number */
4068 +
4069 +#ifdef CONFIG_DEBUG_FS
4070 +       struct dentry            *xi_dbgaufs;
4071 +#endif
4072 +};
4073 +
4074 +/* File-based Hierarchical Storage Management */
4075 +struct au_br_fhsm {
4076 +#ifdef CONFIG_AUFS_FHSM
4077 +       struct mutex            bf_lock;
4078 +       unsigned long           bf_jiffy;
4079 +       struct aufs_stfs        bf_stfs;
4080 +       int                     bf_readable;
4081 +#endif
4082 +};
4083 +
4084 +/* members for writable branch only */
4085 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4086 +struct au_wbr {
4087 +       struct au_rwsem         wbr_wh_rwsem;
4088 +       struct dentry           *wbr_wh[AuBrWh_Last];
4089 +       atomic_t                wbr_wh_running;
4090 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4091 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4092 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4093 +
4094 +       /* mfs mode */
4095 +       unsigned long long      wbr_bytes;
4096 +};
4097 +
4098 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4099 +#define AuBrDynOp (AuDyLast * 4)
4100 +
4101 +#ifdef CONFIG_AUFS_HFSNOTIFY
4102 +/* support for asynchronous destruction */
4103 +struct au_br_hfsnotify {
4104 +       struct fsnotify_group   *hfsn_group;
4105 +};
4106 +#endif
4107 +
4108 +/* sysfs entries */
4109 +struct au_brsysfs {
4110 +       char                    name[16];
4111 +       struct attribute        attr;
4112 +};
4113 +
4114 +enum {
4115 +       AuBrSysfs_BR,
4116 +       AuBrSysfs_BRID,
4117 +       AuBrSysfs_Last
4118 +};
4119 +
4120 +/* protected by superblock rwsem */
4121 +struct au_branch {
4122 +       struct au_xino_file     br_xino;
4123 +
4124 +       aufs_bindex_t           br_id;
4125 +
4126 +       int                     br_perm;
4127 +       struct path             br_path;
4128 +       spinlock_t              br_dykey_lock;
4129 +       struct au_dykey         *br_dykey[AuBrDynOp];
4130 +       atomic_t                br_count;
4131 +
4132 +       struct au_wbr           *br_wbr;
4133 +       struct au_br_fhsm       *br_fhsm;
4134 +
4135 +       /* xino truncation */
4136 +       atomic_t                br_xino_running;
4137 +
4138 +#ifdef CONFIG_AUFS_HFSNOTIFY
4139 +       struct au_br_hfsnotify  *br_hfsn;
4140 +#endif
4141 +
4142 +#ifdef CONFIG_SYSFS
4143 +       /* entries under sysfs per mount-point */
4144 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4145 +#endif
4146 +};
4147 +
4148 +/* ---------------------------------------------------------------------- */
4149 +
4150 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4151 +{
4152 +       return br->br_path.mnt;
4153 +}
4154 +
4155 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4156 +{
4157 +       return br->br_path.dentry;
4158 +}
4159 +
4160 +static inline struct super_block *au_br_sb(struct au_branch *br)
4161 +{
4162 +       return au_br_mnt(br)->mnt_sb;
4163 +}
4164 +
4165 +static inline int au_br_rdonly(struct au_branch *br)
4166 +{
4167 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
4168 +               || !au_br_writable(br->br_perm))
4169 +               ? -EROFS : 0;
4170 +}
4171 +
4172 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4173 +{
4174 +#ifdef CONFIG_AUFS_HNOTIFY
4175 +       return !(brperm & AuBrPerm_RR);
4176 +#else
4177 +       return 0;
4178 +#endif
4179 +}
4180 +
4181 +/* ---------------------------------------------------------------------- */
4182 +
4183 +/* branch.c */
4184 +struct au_sbinfo;
4185 +void au_br_free(struct au_sbinfo *sinfo);
4186 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4187 +struct au_opt_add;
4188 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4189 +struct au_opt_del;
4190 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4191 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4192 +#ifdef CONFIG_COMPAT
4193 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4194 +#endif
4195 +struct au_opt_mod;
4196 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4197 +             int *do_refresh);
4198 +struct aufs_stfs;
4199 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4200 +
4201 +/* xino.c */
4202 +static const loff_t au_loff_max = LLONG_MAX;
4203 +
4204 +int au_xib_trunc(struct super_block *sb);
4205 +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
4206 +                  loff_t *pos);
4207 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
4208 +                   loff_t *pos);
4209 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4210 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4211 +ino_t au_xino_new_ino(struct super_block *sb);
4212 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4213 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4214 +                 ino_t ino);
4215 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4216 +                ino_t *ino);
4217 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4218 +              struct file *base_file, int do_test);
4219 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4220 +
4221 +struct au_opt_xino;
4222 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4223 +void au_xino_clr(struct super_block *sb);
4224 +struct file *au_xino_def(struct super_block *sb);
4225 +int au_xino_path(struct seq_file *seq, struct file *file);
4226 +
4227 +/* ---------------------------------------------------------------------- */
4228 +
4229 +/* Superblock to branch */
4230 +static inline
4231 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4232 +{
4233 +       return au_sbr(sb, bindex)->br_id;
4234 +}
4235 +
4236 +static inline
4237 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4238 +{
4239 +       return au_br_mnt(au_sbr(sb, bindex));
4240 +}
4241 +
4242 +static inline
4243 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4244 +{
4245 +       return au_br_sb(au_sbr(sb, bindex));
4246 +}
4247 +
4248 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4249 +{
4250 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
4251 +}
4252 +
4253 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4254 +{
4255 +       return au_sbr(sb, bindex)->br_perm;
4256 +}
4257 +
4258 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4259 +{
4260 +       return au_br_whable(au_sbr_perm(sb, bindex));
4261 +}
4262 +
4263 +/* ---------------------------------------------------------------------- */
4264 +
4265 +/*
4266 + * wbr_wh_read_lock, wbr_wh_write_lock
4267 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4268 + */
4269 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4270 +
4271 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
4272 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
4273 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
4274 +
4275 +/* ---------------------------------------------------------------------- */
4276 +
4277 +#ifdef CONFIG_AUFS_FHSM
4278 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4279 +{
4280 +       mutex_init(&brfhsm->bf_lock);
4281 +       brfhsm->bf_jiffy = 0;
4282 +       brfhsm->bf_readable = 0;
4283 +}
4284 +
4285 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4286 +{
4287 +       mutex_destroy(&brfhsm->bf_lock);
4288 +}
4289 +#else
4290 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4291 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4292 +#endif
4293 +
4294 +#endif /* __KERNEL__ */
4295 +#endif /* __AUFS_BRANCH_H__ */
4296 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4297 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4298 +++ linux/fs/aufs/conf.mk       2015-04-13 15:10:20.780156788 +0200
4299 @@ -0,0 +1,38 @@
4300 +
4301 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4302 +
4303 +define AuConf
4304 +ifdef ${1}
4305 +AuConfStr += ${1}=${${1}}
4306 +endif
4307 +endef
4308 +
4309 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4310 +       SBILIST \
4311 +       HNOTIFY HFSNOTIFY \
4312 +       EXPORT INO_T_64 \
4313 +       XATTR \
4314 +       FHSM \
4315 +       RDU \
4316 +       SHWH \
4317 +       BR_RAMFS \
4318 +       BR_FUSE POLL \
4319 +       BR_HFSPLUS \
4320 +       BDEV_LOOP \
4321 +       DEBUG MAGIC_SYSRQ
4322 +$(foreach i, ${AuConfAll}, \
4323 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4324 +
4325 +AuConfName = ${obj}/conf.str
4326 +${AuConfName}.tmp: FORCE
4327 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4328 +${AuConfName}: ${AuConfName}.tmp
4329 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4330 +       echo '  GEN    ' $@; \
4331 +       cp -p $< $@; \
4332 +       }
4333 +FORCE:
4334 +clean-files += ${AuConfName} ${AuConfName}.tmp
4335 +${obj}/sysfs.o: ${AuConfName}
4336 +
4337 +-include ${srctree}/${src}/conf_priv.mk
4338 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4339 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4340 +++ linux/fs/aufs/cpup.c        2015-04-13 15:10:20.780156788 +0200
4341 @@ -0,0 +1,1308 @@
4342 +/*
4343 + * Copyright (C) 2005-2015 Junjiro R. Okajima
4344 + *
4345 + * This program, aufs is free software; you can redistribute it and/or modify
4346 + * it under the terms of the GNU General Public License as published by
4347 + * the Free Software Foundation; either version 2 of the License, or
4348 + * (at your option) any later version.
4349 + *
4350 + * This program is distributed in the hope that it will be useful,
4351 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4352 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4353 + * GNU General Public License for more details.
4354 + *
4355 + * You should have received a copy of the GNU General Public License
4356 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4357 + */
4358 +
4359 +/*
4360 + * copy-up functions, see wbr_policy.c for copy-down
4361 + */
4362 +
4363 +#include <linux/fs_stack.h>
4364 +#include <linux/mm.h>
4365 +#include "aufs.h"
4366 +
4367 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4368 +{
4369 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4370 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4371 +
4372 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4373 +
4374 +       dst->i_flags |= iflags & ~mask;
4375 +       if (au_test_fs_notime(dst->i_sb))
4376 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4377 +}
4378 +
4379 +void au_cpup_attr_timesizes(struct inode *inode)
4380 +{
4381 +       struct inode *h_inode;
4382 +
4383 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4384 +       fsstack_copy_attr_times(inode, h_inode);
4385 +       fsstack_copy_inode_size(inode, h_inode);
4386 +}
4387 +
4388 +void au_cpup_attr_nlink(struct inode *inode, int force)
4389 +{
4390 +       struct inode *h_inode;
4391 +       struct super_block *sb;
4392 +       aufs_bindex_t bindex, bend;
4393 +
4394 +       sb = inode->i_sb;
4395 +       bindex = au_ibstart(inode);
4396 +       h_inode = au_h_iptr(inode, bindex);
4397 +       if (!force
4398 +           && !S_ISDIR(h_inode->i_mode)
4399 +           && au_opt_test(au_mntflags(sb), PLINK)
4400 +           && au_plink_test(inode))
4401 +               return;
4402 +
4403 +       /*
4404 +        * 0 can happen in revalidating.
4405 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4406 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4407 +        * case.
4408 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4409 +        *       the incorrect link count.
4410 +        */
4411 +       set_nlink(inode, h_inode->i_nlink);
4412 +
4413 +       /*
4414 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4415 +        * it may includes whplink directory.
4416 +        */
4417 +       if (S_ISDIR(h_inode->i_mode)) {
4418 +               bend = au_ibend(inode);
4419 +               for (bindex++; bindex <= bend; bindex++) {
4420 +                       h_inode = au_h_iptr(inode, bindex);
4421 +                       if (h_inode)
4422 +                               au_add_nlink(inode, h_inode);
4423 +               }
4424 +       }
4425 +}
4426 +
4427 +void au_cpup_attr_changeable(struct inode *inode)
4428 +{
4429 +       struct inode *h_inode;
4430 +
4431 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4432 +       inode->i_mode = h_inode->i_mode;
4433 +       inode->i_uid = h_inode->i_uid;
4434 +       inode->i_gid = h_inode->i_gid;
4435 +       au_cpup_attr_timesizes(inode);
4436 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4437 +}
4438 +
4439 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4440 +{
4441 +       struct au_iinfo *iinfo = au_ii(inode);
4442 +
4443 +       IiMustWriteLock(inode);
4444 +
4445 +       iinfo->ii_higen = h_inode->i_generation;
4446 +       iinfo->ii_hsb1 = h_inode->i_sb;
4447 +}
4448 +
4449 +void au_cpup_attr_all(struct inode *inode, int force)
4450 +{
4451 +       struct inode *h_inode;
4452 +
4453 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4454 +       au_cpup_attr_changeable(inode);
4455 +       if (inode->i_nlink > 0)
4456 +               au_cpup_attr_nlink(inode, force);
4457 +       inode->i_rdev = h_inode->i_rdev;
4458 +       inode->i_blkbits = h_inode->i_blkbits;
4459 +       au_cpup_igen(inode, h_inode);
4460 +}
4461 +
4462 +/* ---------------------------------------------------------------------- */
4463 +
4464 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4465 +
4466 +/* keep the timestamps of the parent dir when cpup */
4467 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4468 +                   struct path *h_path)
4469 +{
4470 +       struct inode *h_inode;
4471 +
4472 +       dt->dt_dentry = dentry;
4473 +       dt->dt_h_path = *h_path;
4474 +       h_inode = h_path->dentry->d_inode;
4475 +       dt->dt_atime = h_inode->i_atime;
4476 +       dt->dt_mtime = h_inode->i_mtime;
4477 +       /* smp_mb(); */
4478 +}
4479 +
4480 +void au_dtime_revert(struct au_dtime *dt)
4481 +{
4482 +       struct iattr attr;
4483 +       int err;
4484 +
4485 +       attr.ia_atime = dt->dt_atime;
4486 +       attr.ia_mtime = dt->dt_mtime;
4487 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4488 +               | ATTR_ATIME | ATTR_ATIME_SET;
4489 +
4490 +       /* no delegation since this is a directory */
4491 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4492 +       if (unlikely(err))
4493 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4494 +}
4495 +
4496 +/* ---------------------------------------------------------------------- */
4497 +
4498 +/* internal use only */
4499 +struct au_cpup_reg_attr {
4500 +       int             valid;
4501 +       struct kstat    st;
4502 +       unsigned int    iflags; /* inode->i_flags */
4503 +};
4504 +
4505 +static noinline_for_stack
4506 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4507 +              struct au_cpup_reg_attr *h_src_attr)
4508 +{
4509 +       int err, sbits, icex;
4510 +       unsigned int mnt_flags;
4511 +       unsigned char verbose;
4512 +       struct iattr ia;
4513 +       struct path h_path;
4514 +       struct inode *h_isrc, *h_idst;
4515 +       struct kstat *h_st;
4516 +       struct au_branch *br;
4517 +
4518 +       h_path.dentry = au_h_dptr(dst, bindex);
4519 +       h_idst = h_path.dentry->d_inode;
4520 +       br = au_sbr(dst->d_sb, bindex);
4521 +       h_path.mnt = au_br_mnt(br);
4522 +       h_isrc = h_src->d_inode;
4523 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4524 +               | ATTR_ATIME | ATTR_MTIME
4525 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4526 +       if (h_src_attr && h_src_attr->valid) {
4527 +               h_st = &h_src_attr->st;
4528 +               ia.ia_uid = h_st->uid;
4529 +               ia.ia_gid = h_st->gid;
4530 +               ia.ia_atime = h_st->atime;
4531 +               ia.ia_mtime = h_st->mtime;
4532 +               if (h_idst->i_mode != h_st->mode
4533 +                   && !S_ISLNK(h_idst->i_mode)) {
4534 +                       ia.ia_valid |= ATTR_MODE;
4535 +                       ia.ia_mode = h_st->mode;
4536 +               }
4537 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4538 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4539 +       } else {
4540 +               ia.ia_uid = h_isrc->i_uid;
4541 +               ia.ia_gid = h_isrc->i_gid;
4542 +               ia.ia_atime = h_isrc->i_atime;
4543 +               ia.ia_mtime = h_isrc->i_mtime;
4544 +               if (h_idst->i_mode != h_isrc->i_mode
4545 +                   && !S_ISLNK(h_idst->i_mode)) {
4546 +                       ia.ia_valid |= ATTR_MODE;
4547 +                       ia.ia_mode = h_isrc->i_mode;
4548 +               }
4549 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4550 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4551 +       }
4552 +       /* no delegation since it is just created */
4553 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4554 +
4555 +       /* is this nfs only? */
4556 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4557 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4558 +               ia.ia_mode = h_isrc->i_mode;
4559 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4560 +       }
4561 +
4562 +       icex = br->br_perm & AuBrAttr_ICEX;
4563 +       if (!err) {
4564 +               mnt_flags = au_mntflags(dst->d_sb);
4565 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4566 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
4567 +       }
4568 +
4569 +       return err;
4570 +}
4571 +
4572 +/* ---------------------------------------------------------------------- */
4573 +
4574 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4575 +                          char *buf, unsigned long blksize)
4576 +{
4577 +       int err;
4578 +       size_t sz, rbytes, wbytes;
4579 +       unsigned char all_zero;
4580 +       char *p, *zp;
4581 +       struct mutex *h_mtx;
4582 +       /* reduce stack usage */
4583 +       struct iattr *ia;
4584 +
4585 +       zp = page_address(ZERO_PAGE(0));
4586 +       if (unlikely(!zp))
4587 +               return -ENOMEM; /* possible? */
4588 +
4589 +       err = 0;
4590 +       all_zero = 0;
4591 +       while (len) {
4592 +               AuDbg("len %lld\n", len);
4593 +               sz = blksize;
4594 +               if (len < blksize)
4595 +                       sz = len;
4596 +
4597 +               rbytes = 0;
4598 +               /* todo: signal_pending? */
4599 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4600 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4601 +                       err = rbytes;
4602 +               }
4603 +               if (unlikely(err < 0))
4604 +                       break;
4605 +
4606 +               all_zero = 0;
4607 +               if (len >= rbytes && rbytes == blksize)
4608 +                       all_zero = !memcmp(buf, zp, rbytes);
4609 +               if (!all_zero) {
4610 +                       wbytes = rbytes;
4611 +                       p = buf;
4612 +                       while (wbytes) {
4613 +                               size_t b;
4614 +
4615 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4616 +                               err = b;
4617 +                               /* todo: signal_pending? */
4618 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
4619 +                                       continue;
4620 +                               if (unlikely(err < 0))
4621 +                                       break;
4622 +                               wbytes -= b;
4623 +                               p += b;
4624 +                       }
4625 +                       if (unlikely(err < 0))
4626 +                               break;
4627 +               } else {
4628 +                       loff_t res;
4629 +
4630 +                       AuLabel(hole);
4631 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4632 +                       err = res;
4633 +                       if (unlikely(res < 0))
4634 +                               break;
4635 +               }
4636 +               len -= rbytes;
4637 +               err = 0;
4638 +       }
4639 +
4640 +       /* the last block may be a hole */
4641 +       if (!err && all_zero) {
4642 +               AuLabel(last hole);
4643 +
4644 +               err = 1;
4645 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
4646 +                       /* nfs requires this step to make last hole */
4647 +                       /* is this only nfs? */
4648 +                       do {
4649 +                               /* todo: signal_pending? */
4650 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4651 +                       } while (err == -EAGAIN || err == -EINTR);
4652 +                       if (err == 1)
4653 +                               dst->f_pos--;
4654 +               }
4655 +
4656 +               if (err == 1) {
4657 +                       ia = (void *)buf;
4658 +                       ia->ia_size = dst->f_pos;
4659 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4660 +                       ia->ia_file = dst;
4661 +                       h_mtx = &file_inode(dst)->i_mutex;
4662 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
4663 +                       /* no delegation since it is just created */
4664 +                       err = vfsub_notify_change(&dst->f_path, ia,
4665 +                                                 /*delegated*/NULL);
4666 +                       mutex_unlock(h_mtx);
4667 +               }
4668 +       }
4669 +
4670 +       return err;
4671 +}
4672 +
4673 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
4674 +{
4675 +       int err;
4676 +       unsigned long blksize;
4677 +       unsigned char do_kfree;
4678 +       char *buf;
4679 +
4680 +       err = -ENOMEM;
4681 +       blksize = dst->f_path.dentry->d_sb->s_blocksize;
4682 +       if (!blksize || PAGE_SIZE < blksize)
4683 +               blksize = PAGE_SIZE;
4684 +       AuDbg("blksize %lu\n", blksize);
4685 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4686 +       if (do_kfree)
4687 +               buf = kmalloc(blksize, GFP_NOFS);
4688 +       else
4689 +               buf = (void *)__get_free_page(GFP_NOFS);
4690 +       if (unlikely(!buf))
4691 +               goto out;
4692 +
4693 +       if (len > (1 << 22))
4694 +               AuDbg("copying a large file %lld\n", (long long)len);
4695 +
4696 +       src->f_pos = 0;
4697 +       dst->f_pos = 0;
4698 +       err = au_do_copy_file(dst, src, len, buf, blksize);
4699 +       if (do_kfree)
4700 +               kfree(buf);
4701 +       else
4702 +               free_page((unsigned long)buf);
4703 +
4704 +out:
4705 +       return err;
4706 +}
4707 +
4708 +/*
4709 + * to support a sparse file which is opened with O_APPEND,
4710 + * we need to close the file.
4711 + */
4712 +static int au_cp_regular(struct au_cp_generic *cpg)
4713 +{
4714 +       int err, i;
4715 +       enum { SRC, DST };
4716 +       struct {
4717 +               aufs_bindex_t bindex;
4718 +               unsigned int flags;
4719 +               struct dentry *dentry;
4720 +               int force_wr;
4721 +               struct file *file;
4722 +               void *label;
4723 +       } *f, file[] = {
4724 +               {
4725 +                       .bindex = cpg->bsrc,
4726 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
4727 +                       .label = &&out
4728 +               },
4729 +               {
4730 +                       .bindex = cpg->bdst,
4731 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
4732 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
4733 +                       .label = &&out_src
4734 +               }
4735 +       };
4736 +       struct super_block *sb;
4737 +
4738 +       /* bsrc branch can be ro/rw. */
4739 +       sb = cpg->dentry->d_sb;
4740 +       f = file;
4741 +       for (i = 0; i < 2; i++, f++) {
4742 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
4743 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
4744 +                                   /*file*/NULL, f->force_wr);
4745 +               err = PTR_ERR(f->file);
4746 +               if (IS_ERR(f->file))
4747 +                       goto *f->label;
4748 +       }
4749 +
4750 +       /* try stopping to update while we copyup */
4751 +       IMustLock(file[SRC].dentry->d_inode);
4752 +       err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
4753 +
4754 +       fput(file[DST].file);
4755 +       au_sbr_put(sb, file[DST].bindex);
4756 +
4757 +out_src:
4758 +       fput(file[SRC].file);
4759 +       au_sbr_put(sb, file[SRC].bindex);
4760 +out:
4761 +       return err;
4762 +}
4763 +
4764 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
4765 +                             struct au_cpup_reg_attr *h_src_attr)
4766 +{
4767 +       int err, rerr;
4768 +       loff_t l;
4769 +       struct path h_path;
4770 +       struct inode *h_src_inode, *h_dst_inode;
4771 +
4772 +       err = 0;
4773 +       h_src_inode = au_h_iptr(cpg->dentry->d_inode, cpg->bsrc);
4774 +       l = i_size_read(h_src_inode);
4775 +       if (cpg->len == -1 || l < cpg->len)
4776 +               cpg->len = l;
4777 +       if (cpg->len) {
4778 +               /* try stopping to update while we are referencing */
4779 +               mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
4780 +               au_pin_hdir_unlock(cpg->pin);
4781 +
4782 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
4783 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
4784 +               h_src_attr->iflags = h_src_inode->i_flags;
4785 +               err = vfs_getattr(&h_path, &h_src_attr->st);
4786 +               if (unlikely(err)) {
4787 +                       mutex_unlock(&h_src_inode->i_mutex);
4788 +                       goto out;
4789 +               }
4790 +               h_src_attr->valid = 1;
4791 +               err = au_cp_regular(cpg);
4792 +               mutex_unlock(&h_src_inode->i_mutex);
4793 +               rerr = au_pin_hdir_relock(cpg->pin);
4794 +               if (!err && rerr)
4795 +                       err = rerr;
4796 +       }
4797 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
4798 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
4799 +               h_dst_inode = h_path.dentry->d_inode;
4800 +               spin_lock(&h_dst_inode->i_lock);
4801 +               h_dst_inode->i_state |= I_LINKABLE;
4802 +               spin_unlock(&h_dst_inode->i_lock);
4803 +       }
4804 +
4805 +out:
4806 +       return err;
4807 +}
4808 +
4809 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
4810 +                             struct inode *h_dir)
4811 +{
4812 +       int err, symlen;
4813 +       mm_segment_t old_fs;
4814 +       union {
4815 +               char *k;
4816 +               char __user *u;
4817 +       } sym;
4818 +
4819 +       err = -ENOSYS;
4820 +       if (unlikely(!h_src->d_inode->i_op->readlink))
4821 +               goto out;
4822 +
4823 +       err = -ENOMEM;
4824 +       sym.k = (void *)__get_free_page(GFP_NOFS);
4825 +       if (unlikely(!sym.k))
4826 +               goto out;
4827 +
4828 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
4829 +       old_fs = get_fs();
4830 +       set_fs(KERNEL_DS);
4831 +       symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
4832 +       err = symlen;
4833 +       set_fs(old_fs);
4834 +
4835 +       if (symlen > 0) {
4836 +               sym.k[symlen] = 0;
4837 +               err = vfsub_symlink(h_dir, h_path, sym.k);
4838 +       }
4839 +       free_page((unsigned long)sym.k);
4840 +
4841 +out:
4842 +       return err;
4843 +}
4844 +
4845 +static noinline_for_stack
4846 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
4847 +              struct au_cpup_reg_attr *h_src_attr)
4848 +{
4849 +       int err;
4850 +       umode_t mode;
4851 +       unsigned int mnt_flags;
4852 +       unsigned char isdir, isreg, force;
4853 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
4854 +       struct au_dtime dt;
4855 +       struct path h_path;
4856 +       struct dentry *h_src, *h_dst, *h_parent;
4857 +       struct inode *h_inode, *h_dir;
4858 +       struct super_block *sb;
4859 +
4860 +       /* bsrc branch can be ro/rw. */
4861 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
4862 +       h_inode = h_src->d_inode;
4863 +       AuDebugOn(h_inode != au_h_iptr(cpg->dentry->d_inode, cpg->bsrc));
4864 +
4865 +       /* try stopping to be referenced while we are creating */
4866 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
4867 +       if (au_ftest_cpup(cpg->flags, RENAME))
4868 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
4869 +                                 AUFS_WH_PFX_LEN));
4870 +       h_parent = h_dst->d_parent; /* dir inode is locked */
4871 +       h_dir = h_parent->d_inode;
4872 +       IMustLock(h_dir);
4873 +       AuDebugOn(h_parent != h_dst->d_parent);
4874 +
4875 +       sb = cpg->dentry->d_sb;
4876 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
4877 +       if (do_dt) {
4878 +               h_path.dentry = h_parent;
4879 +               au_dtime_store(&dt, dst_parent, &h_path);
4880 +       }
4881 +       h_path.dentry = h_dst;
4882 +
4883 +       isreg = 0;
4884 +       isdir = 0;
4885 +       mode = h_inode->i_mode;
4886 +       switch (mode & S_IFMT) {
4887 +       case S_IFREG:
4888 +               isreg = 1;
4889 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
4890 +                                  /*want_excl*/true);
4891 +               if (!err)
4892 +                       err = au_do_cpup_regular(cpg, h_src_attr);
4893 +               break;
4894 +       case S_IFDIR:
4895 +               isdir = 1;
4896 +               err = vfsub_mkdir(h_dir, &h_path, mode);
4897 +               if (!err) {
4898 +                       /*
4899 +                        * strange behaviour from the users view,
4900 +                        * particularry setattr case
4901 +                        */
4902 +                       if (au_ibstart(dst_parent->d_inode) == cpg->bdst)
4903 +                               au_cpup_attr_nlink(dst_parent->d_inode,
4904 +                                                  /*force*/1);
4905 +                       au_cpup_attr_nlink(cpg->dentry->d_inode, /*force*/1);
4906 +               }
4907 +               break;
4908 +       case S_IFLNK:
4909 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
4910 +               break;
4911 +       case S_IFCHR:
4912 +       case S_IFBLK:
4913 +               AuDebugOn(!capable(CAP_MKNOD));
4914 +               /*FALLTHROUGH*/
4915 +       case S_IFIFO:
4916 +       case S_IFSOCK:
4917 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
4918 +               break;
4919 +       default:
4920 +               AuIOErr("Unknown inode type 0%o\n", mode);
4921 +               err = -EIO;
4922 +       }
4923 +
4924 +       mnt_flags = au_mntflags(sb);
4925 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
4926 +           && !isdir
4927 +           && au_opt_test(mnt_flags, XINO)
4928 +           && (h_inode->i_nlink == 1
4929 +               || (h_inode->i_state & I_LINKABLE))
4930 +           /* todo: unnecessary? */
4931 +           /* && cpg->dentry->d_inode->i_nlink == 1 */
4932 +           && cpg->bdst < cpg->bsrc
4933 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
4934 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
4935 +               /* ignore this error */
4936 +
4937 +       if (!err) {
4938 +               force = 0;
4939 +               if (isreg) {
4940 +                       force = !!cpg->len;
4941 +                       if (cpg->len == -1)
4942 +                               force = !!i_size_read(h_inode);
4943 +               }
4944 +               au_fhsm_wrote(sb, cpg->bdst, force);
4945 +       }
4946 +
4947 +       if (do_dt)
4948 +               au_dtime_revert(&dt);
4949 +       return err;
4950 +}
4951 +
4952 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
4953 +{
4954 +       int err;
4955 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
4956 +       struct inode *h_dir;
4957 +       aufs_bindex_t bdst;
4958 +
4959 +       dentry = cpg->dentry;
4960 +       bdst = cpg->bdst;
4961 +       h_dentry = au_h_dptr(dentry, bdst);
4962 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
4963 +               dget(h_dentry);
4964 +               au_set_h_dptr(dentry, bdst, NULL);
4965 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
4966 +               if (!err)
4967 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
4968 +               au_set_h_dptr(dentry, bdst, h_dentry);
4969 +       } else {
4970 +               err = 0;
4971 +               parent = dget_parent(dentry);
4972 +               h_parent = au_h_dptr(parent, bdst);
4973 +               dput(parent);
4974 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
4975 +               if (IS_ERR(h_path->dentry))
4976 +                       err = PTR_ERR(h_path->dentry);
4977 +       }
4978 +       if (unlikely(err))
4979 +               goto out;
4980 +
4981 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
4982 +       h_dir = h_parent->d_inode;
4983 +       IMustLock(h_dir);
4984 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
4985 +       /* no delegation since it is just created */
4986 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
4987 +       dput(h_path->dentry);
4988 +
4989 +out:
4990 +       return err;
4991 +}
4992 +
4993 +/*
4994 + * copyup the @dentry from @bsrc to @bdst.
4995 + * the caller must set the both of lower dentries.
4996 + * @len is for truncating when it is -1 copyup the entire file.
4997 + * in link/rename cases, @dst_parent may be different from the real one.
4998 + * basic->bsrc can be larger than basic->bdst.
4999 + */
5000 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5001 +{
5002 +       int err, rerr;
5003 +       aufs_bindex_t old_ibstart;
5004 +       unsigned char isdir, plink;
5005 +       struct dentry *h_src, *h_dst, *h_parent;
5006 +       struct inode *dst_inode, *h_dir, *inode, *delegated;
5007 +       struct super_block *sb;
5008 +       struct au_branch *br;
5009 +       /* to reuduce stack size */
5010 +       struct {
5011 +               struct au_dtime dt;
5012 +               struct path h_path;
5013 +               struct au_cpup_reg_attr h_src_attr;
5014 +       } *a;
5015 +
5016 +       err = -ENOMEM;
5017 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5018 +       if (unlikely(!a))
5019 +               goto out;
5020 +       a->h_src_attr.valid = 0;
5021 +
5022 +       sb = cpg->dentry->d_sb;
5023 +       br = au_sbr(sb, cpg->bdst);
5024 +       a->h_path.mnt = au_br_mnt(br);
5025 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5026 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5027 +       h_dir = h_parent->d_inode;
5028 +       IMustLock(h_dir);
5029 +
5030 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5031 +       inode = cpg->dentry->d_inode;
5032 +
5033 +       if (!dst_parent)
5034 +               dst_parent = dget_parent(cpg->dentry);
5035 +       else
5036 +               dget(dst_parent);
5037 +
5038 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5039 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5040 +       if (dst_inode) {
5041 +               if (unlikely(!plink)) {
5042 +                       err = -EIO;
5043 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5044 +                               "but plink is disabled\n",
5045 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5046 +                       goto out_parent;
5047 +               }
5048 +
5049 +               if (dst_inode->i_nlink) {
5050 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5051 +
5052 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5053 +                       err = PTR_ERR(h_src);
5054 +                       if (IS_ERR(h_src))
5055 +                               goto out_parent;
5056 +                       if (unlikely(!h_src->d_inode)) {
5057 +                               err = -EIO;
5058 +                               AuIOErr("i%lu exists on a upper branch "
5059 +                                       "but not pseudo-linked\n",
5060 +                                       inode->i_ino);
5061 +                               dput(h_src);
5062 +                               goto out_parent;
5063 +                       }
5064 +
5065 +                       if (do_dt) {
5066 +                               a->h_path.dentry = h_parent;
5067 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5068 +                       }
5069 +
5070 +                       a->h_path.dentry = h_dst;
5071 +                       delegated = NULL;
5072 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5073 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5074 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5075 +                       if (do_dt)
5076 +                               au_dtime_revert(&a->dt);
5077 +                       if (unlikely(err == -EWOULDBLOCK)) {
5078 +                               pr_warn("cannot retry for NFSv4 delegation"
5079 +                                       " for an internal link\n");
5080 +                               iput(delegated);
5081 +                       }
5082 +                       dput(h_src);
5083 +                       goto out_parent;
5084 +               } else
5085 +                       /* todo: cpup_wh_file? */
5086 +                       /* udba work */
5087 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5088 +       }
5089 +
5090 +       isdir = S_ISDIR(inode->i_mode);
5091 +       old_ibstart = au_ibstart(inode);
5092 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5093 +       if (unlikely(err))
5094 +               goto out_rev;
5095 +       dst_inode = h_dst->d_inode;
5096 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
5097 +       /* todo: necessary? */
5098 +       /* au_pin_hdir_unlock(cpg->pin); */
5099 +
5100 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5101 +       if (unlikely(err)) {
5102 +               /* todo: necessary? */
5103 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5104 +               mutex_unlock(&dst_inode->i_mutex);
5105 +               goto out_rev;
5106 +       }
5107 +
5108 +       if (cpg->bdst < old_ibstart) {
5109 +               if (S_ISREG(inode->i_mode)) {
5110 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5111 +                       if (unlikely(err)) {
5112 +                               /* ignore an error */
5113 +                               /* au_pin_hdir_relock(cpg->pin); */
5114 +                               mutex_unlock(&dst_inode->i_mutex);
5115 +                               goto out_rev;
5116 +                       }
5117 +               }
5118 +               au_set_ibstart(inode, cpg->bdst);
5119 +       } else
5120 +               au_set_ibend(inode, cpg->bdst);
5121 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5122 +                     au_hi_flags(inode, isdir));
5123 +
5124 +       /* todo: necessary? */
5125 +       /* err = au_pin_hdir_relock(cpg->pin); */
5126 +       mutex_unlock(&dst_inode->i_mutex);
5127 +       if (unlikely(err))
5128 +               goto out_rev;
5129 +
5130 +       if (!isdir
5131 +           && (h_src->d_inode->i_nlink > 1
5132 +               || h_src->d_inode->i_state & I_LINKABLE)
5133 +           && plink)
5134 +               au_plink_append(inode, cpg->bdst, h_dst);
5135 +
5136 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5137 +               a->h_path.dentry = h_dst;
5138 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5139 +       }
5140 +       if (!err)
5141 +               goto out_parent; /* success */
5142 +
5143 +       /* revert */
5144 +out_rev:
5145 +       a->h_path.dentry = h_parent;
5146 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5147 +       a->h_path.dentry = h_dst;
5148 +       rerr = 0;
5149 +       if (h_dst->d_inode) {
5150 +               if (!isdir) {
5151 +                       /* no delegation since it is just created */
5152 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5153 +                                           /*delegated*/NULL, /*force*/0);
5154 +               } else
5155 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5156 +       }
5157 +       au_dtime_revert(&a->dt);
5158 +       if (rerr) {
5159 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5160 +               err = -EIO;
5161 +       }
5162 +out_parent:
5163 +       dput(dst_parent);
5164 +       kfree(a);
5165 +out:
5166 +       return err;
5167 +}
5168 +
5169 +#if 0 /* reserved */
5170 +struct au_cpup_single_args {
5171 +       int *errp;
5172 +       struct au_cp_generic *cpg;
5173 +       struct dentry *dst_parent;
5174 +};
5175 +
5176 +static void au_call_cpup_single(void *args)
5177 +{
5178 +       struct au_cpup_single_args *a = args;
5179 +
5180 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5181 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5182 +       au_pin_hdir_release(a->cpg->pin);
5183 +}
5184 +#endif
5185 +
5186 +/*
5187 + * prevent SIGXFSZ in copy-up.
5188 + * testing CAP_MKNOD is for generic fs,
5189 + * but CAP_FSETID is for xfs only, currently.
5190 + */
5191 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5192 +{
5193 +       int do_sio;
5194 +       struct super_block *sb;
5195 +       struct inode *h_dir;
5196 +
5197 +       do_sio = 0;
5198 +       sb = au_pinned_parent(pin)->d_sb;
5199 +       if (!au_wkq_test()
5200 +           && (!au_sbi(sb)->si_plink_maint_pid
5201 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5202 +               switch (mode & S_IFMT) {
5203 +               case S_IFREG:
5204 +                       /* no condition about RLIMIT_FSIZE and the file size */
5205 +                       do_sio = 1;
5206 +                       break;
5207 +               case S_IFCHR:
5208 +               case S_IFBLK:
5209 +                       do_sio = !capable(CAP_MKNOD);
5210 +                       break;
5211 +               }
5212 +               if (!do_sio)
5213 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5214 +                                 && !capable(CAP_FSETID));
5215 +               /* this workaround may be removed in the future */
5216 +               if (!do_sio) {
5217 +                       h_dir = au_pinned_h_dir(pin);
5218 +                       do_sio = h_dir->i_mode & S_ISVTX;
5219 +               }
5220 +       }
5221 +
5222 +       return do_sio;
5223 +}
5224 +
5225 +#if 0 /* reserved */
5226 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5227 +{
5228 +       int err, wkq_err;
5229 +       struct dentry *h_dentry;
5230 +
5231 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5232 +       if (!au_cpup_sio_test(pin, h_dentry->d_inode->i_mode))
5233 +               err = au_cpup_single(cpg, dst_parent);
5234 +       else {
5235 +               struct au_cpup_single_args args = {
5236 +                       .errp           = &err,
5237 +                       .cpg            = cpg,
5238 +                       .dst_parent     = dst_parent
5239 +               };
5240 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5241 +               if (unlikely(wkq_err))
5242 +                       err = wkq_err;
5243 +       }
5244 +
5245 +       return err;
5246 +}
5247 +#endif
5248 +
5249 +/*
5250 + * copyup the @dentry from the first active lower branch to @bdst,
5251 + * using au_cpup_single().
5252 + */
5253 +static int au_cpup_simple(struct au_cp_generic *cpg)
5254 +{
5255 +       int err;
5256 +       unsigned int flags_orig;
5257 +       struct dentry *dentry;
5258 +
5259 +       AuDebugOn(cpg->bsrc < 0);
5260 +
5261 +       dentry = cpg->dentry;
5262 +       DiMustWriteLock(dentry);
5263 +
5264 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5265 +       if (!err) {
5266 +               flags_orig = cpg->flags;
5267 +               au_fset_cpup(cpg->flags, RENAME);
5268 +               err = au_cpup_single(cpg, NULL);
5269 +               cpg->flags = flags_orig;
5270 +               if (!err)
5271 +                       return 0; /* success */
5272 +
5273 +               /* revert */
5274 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5275 +               au_set_dbstart(dentry, cpg->bsrc);
5276 +       }
5277 +
5278 +       return err;
5279 +}
5280 +
5281 +struct au_cpup_simple_args {
5282 +       int *errp;
5283 +       struct au_cp_generic *cpg;
5284 +};
5285 +
5286 +static void au_call_cpup_simple(void *args)
5287 +{
5288 +       struct au_cpup_simple_args *a = args;
5289 +
5290 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5291 +       *a->errp = au_cpup_simple(a->cpg);
5292 +       au_pin_hdir_release(a->cpg->pin);
5293 +}
5294 +
5295 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5296 +{
5297 +       int err, wkq_err;
5298 +       struct dentry *dentry, *parent;
5299 +       struct file *h_file;
5300 +       struct inode *h_dir;
5301 +
5302 +       dentry = cpg->dentry;
5303 +       h_file = NULL;
5304 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5305 +               AuDebugOn(cpg->bsrc < 0);
5306 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5307 +               err = PTR_ERR(h_file);
5308 +               if (IS_ERR(h_file))
5309 +                       goto out;
5310 +       }
5311 +
5312 +       parent = dget_parent(dentry);
5313 +       h_dir = au_h_iptr(parent->d_inode, cpg->bdst);
5314 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5315 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
5316 +               err = au_cpup_simple(cpg);
5317 +       else {
5318 +               struct au_cpup_simple_args args = {
5319 +                       .errp           = &err,
5320 +                       .cpg            = cpg
5321 +               };
5322 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5323 +               if (unlikely(wkq_err))
5324 +                       err = wkq_err;
5325 +       }
5326 +
5327 +       dput(parent);
5328 +       if (h_file)
5329 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5330 +
5331 +out:
5332 +       return err;
5333 +}
5334 +
5335 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5336 +{
5337 +       aufs_bindex_t bsrc, bend;
5338 +       struct dentry *dentry, *h_dentry;
5339 +
5340 +       if (cpg->bsrc < 0) {
5341 +               dentry = cpg->dentry;
5342 +               bend = au_dbend(dentry);
5343 +               for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
5344 +                       h_dentry = au_h_dptr(dentry, bsrc);
5345 +                       if (h_dentry) {
5346 +                               AuDebugOn(!h_dentry->d_inode);
5347 +                               break;
5348 +                       }
5349 +               }
5350 +               AuDebugOn(bsrc > bend);
5351 +               cpg->bsrc = bsrc;
5352 +       }
5353 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5354 +       return au_do_sio_cpup_simple(cpg);
5355 +}
5356 +
5357 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5358 +{
5359 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5360 +       return au_do_sio_cpup_simple(cpg);
5361 +}
5362 +
5363 +/* ---------------------------------------------------------------------- */
5364 +
5365 +/*
5366 + * copyup the deleted file for writing.
5367 + */
5368 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5369 +                        struct file *file)
5370 +{
5371 +       int err;
5372 +       unsigned int flags_orig;
5373 +       aufs_bindex_t bsrc_orig;
5374 +       struct dentry *h_d_dst, *h_d_start;
5375 +       struct au_dinfo *dinfo;
5376 +       struct au_hdentry *hdp;
5377 +
5378 +       dinfo = au_di(cpg->dentry);
5379 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5380 +
5381 +       bsrc_orig = cpg->bsrc;
5382 +       cpg->bsrc = dinfo->di_bstart;
5383 +       hdp = dinfo->di_hdentry;
5384 +       h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
5385 +       dinfo->di_bstart = cpg->bdst;
5386 +       hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
5387 +       h_d_start = NULL;
5388 +       if (file) {
5389 +               h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
5390 +               hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_path.dentry;
5391 +       }
5392 +       flags_orig = cpg->flags;
5393 +       cpg->flags = !AuCpup_DTIME;
5394 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5395 +       cpg->flags = flags_orig;
5396 +       if (file) {
5397 +               if (!err)
5398 +                       err = au_reopen_nondir(file);
5399 +               hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
5400 +       }
5401 +       hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
5402 +       dinfo->di_bstart = cpg->bsrc;
5403 +       cpg->bsrc = bsrc_orig;
5404 +
5405 +       return err;
5406 +}
5407 +
5408 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5409 +{
5410 +       int err;
5411 +       aufs_bindex_t bdst;
5412 +       struct au_dtime dt;
5413 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5414 +       struct au_branch *br;
5415 +       struct path h_path;
5416 +
5417 +       dentry = cpg->dentry;
5418 +       bdst = cpg->bdst;
5419 +       br = au_sbr(dentry->d_sb, bdst);
5420 +       parent = dget_parent(dentry);
5421 +       h_parent = au_h_dptr(parent, bdst);
5422 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5423 +       err = PTR_ERR(wh_dentry);
5424 +       if (IS_ERR(wh_dentry))
5425 +               goto out;
5426 +
5427 +       h_path.dentry = h_parent;
5428 +       h_path.mnt = au_br_mnt(br);
5429 +       au_dtime_store(&dt, parent, &h_path);
5430 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5431 +       if (unlikely(err))
5432 +               goto out_wh;
5433 +
5434 +       dget(wh_dentry);
5435 +       h_path.dentry = wh_dentry;
5436 +       if (!d_is_dir(wh_dentry)) {
5437 +               /* no delegation since it is just created */
5438 +               err = vfsub_unlink(h_parent->d_inode, &h_path,
5439 +                                  /*delegated*/NULL, /*force*/0);
5440 +       } else
5441 +               err = vfsub_rmdir(h_parent->d_inode, &h_path);
5442 +       if (unlikely(err)) {
5443 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5444 +                       wh_dentry, err);
5445 +               err = -EIO;
5446 +       }
5447 +       au_dtime_revert(&dt);
5448 +       au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
5449 +
5450 +out_wh:
5451 +       dput(wh_dentry);
5452 +out:
5453 +       dput(parent);
5454 +       return err;
5455 +}
5456 +
5457 +struct au_cpup_wh_args {
5458 +       int *errp;
5459 +       struct au_cp_generic *cpg;
5460 +       struct file *file;
5461 +};
5462 +
5463 +static void au_call_cpup_wh(void *args)
5464 +{
5465 +       struct au_cpup_wh_args *a = args;
5466 +
5467 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5468 +       *a->errp = au_cpup_wh(a->cpg, a->file);
5469 +       au_pin_hdir_release(a->cpg->pin);
5470 +}
5471 +
5472 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5473 +{
5474 +       int err, wkq_err;
5475 +       aufs_bindex_t bdst;
5476 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
5477 +       struct inode *dir, *h_dir, *h_tmpdir;
5478 +       struct au_wbr *wbr;
5479 +       struct au_pin wh_pin, *pin_orig;
5480 +
5481 +       dentry = cpg->dentry;
5482 +       bdst = cpg->bdst;
5483 +       parent = dget_parent(dentry);
5484 +       dir = parent->d_inode;
5485 +       h_orph = NULL;
5486 +       h_parent = NULL;
5487 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
5488 +       h_tmpdir = h_dir;
5489 +       pin_orig = NULL;
5490 +       if (!h_dir->i_nlink) {
5491 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5492 +               h_orph = wbr->wbr_orph;
5493 +
5494 +               h_parent = dget(au_h_dptr(parent, bdst));
5495 +               au_set_h_dptr(parent, bdst, dget(h_orph));
5496 +               h_tmpdir = h_orph->d_inode;
5497 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5498 +
5499 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
5500 +               /* todo: au_h_open_pre()? */
5501 +
5502 +               pin_orig = cpg->pin;
5503 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
5504 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5505 +               cpg->pin = &wh_pin;
5506 +       }
5507 +
5508 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5509 +           && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
5510 +               err = au_cpup_wh(cpg, file);
5511 +       else {
5512 +               struct au_cpup_wh_args args = {
5513 +                       .errp   = &err,
5514 +                       .cpg    = cpg,
5515 +                       .file   = file
5516 +               };
5517 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5518 +               if (unlikely(wkq_err))
5519 +                       err = wkq_err;
5520 +       }
5521 +
5522 +       if (h_orph) {
5523 +               mutex_unlock(&h_tmpdir->i_mutex);
5524 +               /* todo: au_h_open_post()? */
5525 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5526 +               au_set_h_dptr(parent, bdst, h_parent);
5527 +               AuDebugOn(!pin_orig);
5528 +               cpg->pin = pin_orig;
5529 +       }
5530 +       iput(h_dir);
5531 +       dput(parent);
5532 +
5533 +       return err;
5534 +}
5535 +
5536 +/* ---------------------------------------------------------------------- */
5537 +
5538 +/*
5539 + * generic routine for both of copy-up and copy-down.
5540 + */
5541 +/* cf. revalidate function in file.c */
5542 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5543 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5544 +                        struct au_pin *pin,
5545 +                        struct dentry *h_parent, void *arg),
5546 +              void *arg)
5547 +{
5548 +       int err;
5549 +       struct au_pin pin;
5550 +       struct dentry *d, *parent, *h_parent, *real_parent;
5551 +
5552 +       err = 0;
5553 +       parent = dget_parent(dentry);
5554 +       if (IS_ROOT(parent))
5555 +               goto out;
5556 +
5557 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5558 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5559 +
5560 +       /* do not use au_dpage */
5561 +       real_parent = parent;
5562 +       while (1) {
5563 +               dput(parent);
5564 +               parent = dget_parent(dentry);
5565 +               h_parent = au_h_dptr(parent, bdst);
5566 +               if (h_parent)
5567 +                       goto out; /* success */
5568 +
5569 +               /* find top dir which is necessary to cpup */
5570 +               do {
5571 +                       d = parent;
5572 +                       dput(parent);
5573 +                       parent = dget_parent(d);
5574 +                       di_read_lock_parent3(parent, !AuLock_IR);
5575 +                       h_parent = au_h_dptr(parent, bdst);
5576 +                       di_read_unlock(parent, !AuLock_IR);
5577 +               } while (!h_parent);
5578 +
5579 +               if (d != real_parent)
5580 +                       di_write_lock_child3(d);
5581 +
5582 +               /* somebody else might create while we were sleeping */
5583 +               if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
5584 +                       if (au_h_dptr(d, bdst))
5585 +                               au_update_dbstart(d);
5586 +
5587 +                       au_pin_set_dentry(&pin, d);
5588 +                       err = au_do_pin(&pin);
5589 +                       if (!err) {
5590 +                               err = cp(d, bdst, &pin, h_parent, arg);
5591 +                               au_unpin(&pin);
5592 +                       }
5593 +               }
5594 +
5595 +               if (d != real_parent)
5596 +                       di_write_unlock(d);
5597 +               if (unlikely(err))
5598 +                       break;
5599 +       }
5600 +
5601 +out:
5602 +       dput(parent);
5603 +       return err;
5604 +}
5605 +
5606 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
5607 +                      struct au_pin *pin,
5608 +                      struct dentry *h_parent __maybe_unused,
5609 +                      void *arg __maybe_unused)
5610 +{
5611 +       struct au_cp_generic cpg = {
5612 +               .dentry = dentry,
5613 +               .bdst   = bdst,
5614 +               .bsrc   = -1,
5615 +               .len    = 0,
5616 +               .pin    = pin,
5617 +               .flags  = AuCpup_DTIME
5618 +       };
5619 +       return au_sio_cpup_simple(&cpg);
5620 +}
5621 +
5622 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5623 +{
5624 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5625 +}
5626 +
5627 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5628 +{
5629 +       int err;
5630 +       struct dentry *parent;
5631 +       struct inode *dir;
5632 +
5633 +       parent = dget_parent(dentry);
5634 +       dir = parent->d_inode;
5635 +       err = 0;
5636 +       if (au_h_iptr(dir, bdst))
5637 +               goto out;
5638 +
5639 +       di_read_unlock(parent, AuLock_IR);
5640 +       di_write_lock_parent(parent);
5641 +       /* someone else might change our inode while we were sleeping */
5642 +       if (!au_h_iptr(dir, bdst))
5643 +               err = au_cpup_dirs(dentry, bdst);
5644 +       di_downgrade_lock(parent, AuLock_IR);
5645 +
5646 +out:
5647 +       dput(parent);
5648 +       return err;
5649 +}
5650 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
5651 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
5652 +++ linux/fs/aufs/cpup.h        2015-04-13 15:10:20.780156788 +0200
5653 @@ -0,0 +1,94 @@
5654 +/*
5655 + * Copyright (C) 2005-2015 Junjiro R. Okajima
5656 + *
5657 + * This program, aufs is free software; you can redistribute it and/or modify
5658 + * it under the terms of the GNU General Public License as published by
5659 + * the Free Software Foundation; either version 2 of the License, or
5660 + * (at your option) any later version.
5661 + *
5662 + * This program is distributed in the hope that it will be useful,
5663 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5664 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5665 + * GNU General Public License for more details.
5666 + *
5667 + * You should have received a copy of the GNU General Public License
5668 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5669 + */
5670 +
5671 +/*
5672 + * copy-up/down functions
5673 + */
5674 +
5675 +#ifndef __AUFS_CPUP_H__
5676 +#define __AUFS_CPUP_H__
5677 +
5678 +#ifdef __KERNEL__
5679 +
5680 +#include <linux/path.h>
5681 +
5682 +struct inode;
5683 +struct file;
5684 +struct au_pin;
5685 +
5686 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
5687 +void au_cpup_attr_timesizes(struct inode *inode);
5688 +void au_cpup_attr_nlink(struct inode *inode, int force);
5689 +void au_cpup_attr_changeable(struct inode *inode);
5690 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
5691 +void au_cpup_attr_all(struct inode *inode, int force);
5692 +
5693 +/* ---------------------------------------------------------------------- */
5694 +
5695 +struct au_cp_generic {
5696 +       struct dentry   *dentry;
5697 +       aufs_bindex_t   bdst, bsrc;
5698 +       loff_t          len;
5699 +       struct au_pin   *pin;
5700 +       unsigned int    flags;
5701 +};
5702 +
5703 +/* cpup flags */
5704 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
5705 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
5706 +                                                  for link(2) */
5707 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
5708 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
5709 +                                                  cpup */
5710 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
5711 +                                                  existing entry */
5712 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
5713 +                                                  the branch is marked as RO */
5714 +
5715 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
5716 +#define au_fset_cpup(flags, name) \
5717 +       do { (flags) |= AuCpup_##name; } while (0)
5718 +#define au_fclr_cpup(flags, name) \
5719 +       do { (flags) &= ~AuCpup_##name; } while (0)
5720 +
5721 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
5722 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
5723 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
5724 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
5725 +
5726 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5727 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5728 +                        struct au_pin *pin,
5729 +                        struct dentry *h_parent, void *arg),
5730 +              void *arg);
5731 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5732 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5733 +
5734 +/* ---------------------------------------------------------------------- */
5735 +
5736 +/* keep timestamps when copyup */
5737 +struct au_dtime {
5738 +       struct dentry *dt_dentry;
5739 +       struct path dt_h_path;
5740 +       struct timespec dt_atime, dt_mtime;
5741 +};
5742 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5743 +                   struct path *h_path);
5744 +void au_dtime_revert(struct au_dtime *dt);
5745 +
5746 +#endif /* __KERNEL__ */
5747 +#endif /* __AUFS_CPUP_H__ */
5748 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
5749 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
5750 +++ linux/fs/aufs/dbgaufs.c     2015-04-13 15:10:20.780156788 +0200
5751 @@ -0,0 +1,432 @@
5752 +/*
5753 + * Copyright (C) 2005-2015 Junjiro R. Okajima
5754 + *
5755 + * This program, aufs is free software; you can redistribute it and/or modify
5756 + * it under the terms of the GNU General Public License as published by
5757 + * the Free Software Foundation; either version 2 of the License, or
5758 + * (at your option) any later version.
5759 + *
5760 + * This program is distributed in the hope that it will be useful,
5761 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5762 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5763 + * GNU General Public License for more details.
5764 + *
5765 + * You should have received a copy of the GNU General Public License
5766 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5767 + */
5768 +
5769 +/*
5770 + * debugfs interface
5771 + */
5772 +
5773 +#include <linux/debugfs.h>
5774 +#include "aufs.h"
5775 +
5776 +#ifndef CONFIG_SYSFS
5777 +#error DEBUG_FS depends upon SYSFS
5778 +#endif
5779 +
5780 +static struct dentry *dbgaufs;
5781 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
5782 +
5783 +/* 20 is max digits length of ulong 64 */
5784 +struct dbgaufs_arg {
5785 +       int n;
5786 +       char a[20 * 4];
5787 +};
5788 +
5789 +/*
5790 + * common function for all XINO files
5791 + */
5792 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
5793 +                             struct file *file)
5794 +{
5795 +       kfree(file->private_data);
5796 +       return 0;
5797 +}
5798 +
5799 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
5800 +{
5801 +       int err;
5802 +       struct kstat st;
5803 +       struct dbgaufs_arg *p;
5804 +
5805 +       err = -ENOMEM;
5806 +       p = kmalloc(sizeof(*p), GFP_NOFS);
5807 +       if (unlikely(!p))
5808 +               goto out;
5809 +
5810 +       err = 0;
5811 +       p->n = 0;
5812 +       file->private_data = p;
5813 +       if (!xf)
5814 +               goto out;
5815 +
5816 +       err = vfs_getattr(&xf->f_path, &st);
5817 +       if (!err) {
5818 +               if (do_fcnt)
5819 +                       p->n = snprintf
5820 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
5821 +                                (long)file_count(xf), st.blocks, st.blksize,
5822 +                                (long long)st.size);
5823 +               else
5824 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
5825 +                                       st.blocks, st.blksize,
5826 +                                       (long long)st.size);
5827 +               AuDebugOn(p->n >= sizeof(p->a));
5828 +       } else {
5829 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
5830 +               err = 0;
5831 +       }
5832 +
5833 +out:
5834 +       return err;
5835 +
5836 +}
5837 +
5838 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
5839 +                              size_t count, loff_t *ppos)
5840 +{
5841 +       struct dbgaufs_arg *p;
5842 +
5843 +       p = file->private_data;
5844 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
5845 +}
5846 +
5847 +/* ---------------------------------------------------------------------- */
5848 +
5849 +struct dbgaufs_plink_arg {
5850 +       int n;
5851 +       char a[];
5852 +};
5853 +
5854 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
5855 +                                struct file *file)
5856 +{
5857 +       free_page((unsigned long)file->private_data);
5858 +       return 0;
5859 +}
5860 +
5861 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
5862 +{
5863 +       int err, i, limit;
5864 +       unsigned long n, sum;
5865 +       struct dbgaufs_plink_arg *p;
5866 +       struct au_sbinfo *sbinfo;
5867 +       struct super_block *sb;
5868 +       struct au_sphlhead *sphl;
5869 +
5870 +       err = -ENOMEM;
5871 +       p = (void *)get_zeroed_page(GFP_NOFS);
5872 +       if (unlikely(!p))
5873 +               goto out;
5874 +
5875 +       err = -EFBIG;
5876 +       sbinfo = inode->i_private;
5877 +       sb = sbinfo->si_sb;
5878 +       si_noflush_read_lock(sb);
5879 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
5880 +               limit = PAGE_SIZE - sizeof(p->n);
5881 +
5882 +               /* the number of buckets */
5883 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
5884 +               p->n += n;
5885 +               limit -= n;
5886 +
5887 +               sum = 0;
5888 +               for (i = 0, sphl = sbinfo->si_plink;
5889 +                    i < AuPlink_NHASH;
5890 +                    i++, sphl++) {
5891 +                       n = au_sphl_count(sphl);
5892 +                       sum += n;
5893 +
5894 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
5895 +                       p->n += n;
5896 +                       limit -= n;
5897 +                       if (unlikely(limit <= 0))
5898 +                               goto out_free;
5899 +               }
5900 +               p->a[p->n - 1] = '\n';
5901 +
5902 +               /* the sum of plinks */
5903 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
5904 +               p->n += n;
5905 +               limit -= n;
5906 +               if (unlikely(limit <= 0))
5907 +                       goto out_free;
5908 +       } else {
5909 +#define str "1\n0\n0\n"
5910 +               p->n = sizeof(str) - 1;
5911 +               strcpy(p->a, str);
5912 +#undef str
5913 +       }
5914 +       si_read_unlock(sb);
5915 +
5916 +       err = 0;
5917 +       file->private_data = p;
5918 +       goto out; /* success */
5919 +
5920 +out_free:
5921 +       free_page((unsigned long)p);
5922 +out:
5923 +       return err;
5924 +}
5925 +
5926 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
5927 +                                 size_t count, loff_t *ppos)
5928 +{
5929 +       struct dbgaufs_plink_arg *p;
5930 +
5931 +       p = file->private_data;
5932 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
5933 +}
5934 +
5935 +static const struct file_operations dbgaufs_plink_fop = {
5936 +       .owner          = THIS_MODULE,
5937 +       .open           = dbgaufs_plink_open,
5938 +       .release        = dbgaufs_plink_release,
5939 +       .read           = dbgaufs_plink_read
5940 +};
5941 +
5942 +/* ---------------------------------------------------------------------- */
5943 +
5944 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
5945 +{
5946 +       int err;
5947 +       struct au_sbinfo *sbinfo;
5948 +       struct super_block *sb;
5949 +
5950 +       sbinfo = inode->i_private;
5951 +       sb = sbinfo->si_sb;
5952 +       si_noflush_read_lock(sb);
5953 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
5954 +       si_read_unlock(sb);
5955 +       return err;
5956 +}
5957 +
5958 +static const struct file_operations dbgaufs_xib_fop = {
5959 +       .owner          = THIS_MODULE,
5960 +       .open           = dbgaufs_xib_open,
5961 +       .release        = dbgaufs_xi_release,
5962 +       .read           = dbgaufs_xi_read
5963 +};
5964 +
5965 +/* ---------------------------------------------------------------------- */
5966 +
5967 +#define DbgaufsXi_PREFIX "xi"
5968 +
5969 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
5970 +{
5971 +       int err;
5972 +       long l;
5973 +       struct au_sbinfo *sbinfo;
5974 +       struct super_block *sb;
5975 +       struct file *xf;
5976 +       struct qstr *name;
5977 +
5978 +       err = -ENOENT;
5979 +       xf = NULL;
5980 +       name = &file->f_path.dentry->d_name;
5981 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
5982 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
5983 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
5984 +               goto out;
5985 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
5986 +       if (unlikely(err))
5987 +               goto out;
5988 +
5989 +       sbinfo = inode->i_private;
5990 +       sb = sbinfo->si_sb;
5991 +       si_noflush_read_lock(sb);
5992 +       if (l <= au_sbend(sb)) {
5993 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
5994 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
5995 +       } else
5996 +               err = -ENOENT;
5997 +       si_read_unlock(sb);
5998 +
5999 +out:
6000 +       return err;
6001 +}
6002 +
6003 +static const struct file_operations dbgaufs_xino_fop = {
6004 +       .owner          = THIS_MODULE,
6005 +       .open           = dbgaufs_xino_open,
6006 +       .release        = dbgaufs_xi_release,
6007 +       .read           = dbgaufs_xi_read
6008 +};
6009 +
6010 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6011 +{
6012 +       aufs_bindex_t bend;
6013 +       struct au_branch *br;
6014 +       struct au_xino_file *xi;
6015 +
6016 +       if (!au_sbi(sb)->si_dbgaufs)
6017 +               return;
6018 +
6019 +       bend = au_sbend(sb);
6020 +       for (; bindex <= bend; bindex++) {
6021 +               br = au_sbr(sb, bindex);
6022 +               xi = &br->br_xino;
6023 +               debugfs_remove(xi->xi_dbgaufs);
6024 +               xi->xi_dbgaufs = NULL;
6025 +       }
6026 +}
6027 +
6028 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6029 +{
6030 +       struct au_sbinfo *sbinfo;
6031 +       struct dentry *parent;
6032 +       struct au_branch *br;
6033 +       struct au_xino_file *xi;
6034 +       aufs_bindex_t bend;
6035 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6036 +
6037 +       sbinfo = au_sbi(sb);
6038 +       parent = sbinfo->si_dbgaufs;
6039 +       if (!parent)
6040 +               return;
6041 +
6042 +       bend = au_sbend(sb);
6043 +       for (; bindex <= bend; bindex++) {
6044 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6045 +               br = au_sbr(sb, bindex);
6046 +               xi = &br->br_xino;
6047 +               AuDebugOn(xi->xi_dbgaufs);
6048 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6049 +                                                    sbinfo, &dbgaufs_xino_fop);
6050 +               /* ignore an error */
6051 +               if (unlikely(!xi->xi_dbgaufs))
6052 +                       AuWarn1("failed %s under debugfs\n", name);
6053 +       }
6054 +}
6055 +
6056 +/* ---------------------------------------------------------------------- */
6057 +
6058 +#ifdef CONFIG_AUFS_EXPORT
6059 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6060 +{
6061 +       int err;
6062 +       struct au_sbinfo *sbinfo;
6063 +       struct super_block *sb;
6064 +
6065 +       sbinfo = inode->i_private;
6066 +       sb = sbinfo->si_sb;
6067 +       si_noflush_read_lock(sb);
6068 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6069 +       si_read_unlock(sb);
6070 +       return err;
6071 +}
6072 +
6073 +static const struct file_operations dbgaufs_xigen_fop = {
6074 +       .owner          = THIS_MODULE,
6075 +       .open           = dbgaufs_xigen_open,
6076 +       .release        = dbgaufs_xi_release,
6077 +       .read           = dbgaufs_xi_read
6078 +};
6079 +
6080 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6081 +{
6082 +       int err;
6083 +
6084 +       /*
6085 +        * This function is a dynamic '__init' function actually,
6086 +        * so the tiny check for si_rwsem is unnecessary.
6087 +        */
6088 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6089 +
6090 +       err = -EIO;
6091 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6092 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6093 +                &dbgaufs_xigen_fop);
6094 +       if (sbinfo->si_dbgaufs_xigen)
6095 +               err = 0;
6096 +
6097 +       return err;
6098 +}
6099 +#else
6100 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6101 +{
6102 +       return 0;
6103 +}
6104 +#endif /* CONFIG_AUFS_EXPORT */
6105 +
6106 +/* ---------------------------------------------------------------------- */
6107 +
6108 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6109 +{
6110 +       /*
6111 +        * This function is a dynamic '__fin' function actually,
6112 +        * so the tiny check for si_rwsem is unnecessary.
6113 +        */
6114 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6115 +
6116 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6117 +       sbinfo->si_dbgaufs = NULL;
6118 +       kobject_put(&sbinfo->si_kobj);
6119 +}
6120 +
6121 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6122 +{
6123 +       int err;
6124 +       char name[SysaufsSiNameLen];
6125 +
6126 +       /*
6127 +        * This function is a dynamic '__init' function actually,
6128 +        * so the tiny check for si_rwsem is unnecessary.
6129 +        */
6130 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6131 +
6132 +       err = -ENOENT;
6133 +       if (!dbgaufs) {
6134 +               AuErr1("/debug/aufs is uninitialized\n");
6135 +               goto out;
6136 +       }
6137 +
6138 +       err = -EIO;
6139 +       sysaufs_name(sbinfo, name);
6140 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6141 +       if (unlikely(!sbinfo->si_dbgaufs))
6142 +               goto out;
6143 +       kobject_get(&sbinfo->si_kobj);
6144 +
6145 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6146 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6147 +                &dbgaufs_xib_fop);
6148 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6149 +               goto out_dir;
6150 +
6151 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6152 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6153 +                &dbgaufs_plink_fop);
6154 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6155 +               goto out_dir;
6156 +
6157 +       err = dbgaufs_xigen_init(sbinfo);
6158 +       if (!err)
6159 +               goto out; /* success */
6160 +
6161 +out_dir:
6162 +       dbgaufs_si_fin(sbinfo);
6163 +out:
6164 +       return err;
6165 +}
6166 +
6167 +/* ---------------------------------------------------------------------- */
6168 +
6169 +void dbgaufs_fin(void)
6170 +{
6171 +       debugfs_remove(dbgaufs);
6172 +}
6173 +
6174 +int __init dbgaufs_init(void)
6175 +{
6176 +       int err;
6177 +
6178 +       err = -EIO;
6179 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6180 +       if (dbgaufs)
6181 +               err = 0;
6182 +       return err;
6183 +}
6184 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6185 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6186 +++ linux/fs/aufs/dbgaufs.h     2015-04-13 15:10:20.780156788 +0200
6187 @@ -0,0 +1,48 @@
6188 +/*
6189 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6190 + *
6191 + * This program, aufs is free software; you can redistribute it and/or modify
6192 + * it under the terms of the GNU General Public License as published by
6193 + * the Free Software Foundation; either version 2 of the License, or
6194 + * (at your option) any later version.
6195 + *
6196 + * This program is distributed in the hope that it will be useful,
6197 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6198 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6199 + * GNU General Public License for more details.
6200 + *
6201 + * You should have received a copy of the GNU General Public License
6202 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6203 + */
6204 +
6205 +/*
6206 + * debugfs interface
6207 + */
6208 +
6209 +#ifndef __DBGAUFS_H__
6210 +#define __DBGAUFS_H__
6211 +
6212 +#ifdef __KERNEL__
6213 +
6214 +struct super_block;
6215 +struct au_sbinfo;
6216 +
6217 +#ifdef CONFIG_DEBUG_FS
6218 +/* dbgaufs.c */
6219 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6220 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6221 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6222 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6223 +void dbgaufs_fin(void);
6224 +int __init dbgaufs_init(void);
6225 +#else
6226 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6227 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6228 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6229 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6230 +AuStubVoid(dbgaufs_fin, void)
6231 +AuStubInt0(__init dbgaufs_init, void)
6232 +#endif /* CONFIG_DEBUG_FS */
6233 +
6234 +#endif /* __KERNEL__ */
6235 +#endif /* __DBGAUFS_H__ */
6236 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6237 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6238 +++ linux/fs/aufs/dcsub.c       2015-04-13 15:10:20.780156788 +0200
6239 @@ -0,0 +1,224 @@
6240 +/*
6241 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6242 + *
6243 + * This program, aufs is free software; you can redistribute it and/or modify
6244 + * it under the terms of the GNU General Public License as published by
6245 + * the Free Software Foundation; either version 2 of the License, or
6246 + * (at your option) any later version.
6247 + *
6248 + * This program is distributed in the hope that it will be useful,
6249 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6250 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6251 + * GNU General Public License for more details.
6252 + *
6253 + * You should have received a copy of the GNU General Public License
6254 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6255 + */
6256 +
6257 +/*
6258 + * sub-routines for dentry cache
6259 + */
6260 +
6261 +#include "aufs.h"
6262 +
6263 +static void au_dpage_free(struct au_dpage *dpage)
6264 +{
6265 +       int i;
6266 +       struct dentry **p;
6267 +
6268 +       p = dpage->dentries;
6269 +       for (i = 0; i < dpage->ndentry; i++)
6270 +               dput(*p++);
6271 +       free_page((unsigned long)dpage->dentries);
6272 +}
6273 +
6274 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6275 +{
6276 +       int err;
6277 +       void *p;
6278 +
6279 +       err = -ENOMEM;
6280 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6281 +       if (unlikely(!dpages->dpages))
6282 +               goto out;
6283 +
6284 +       p = (void *)__get_free_page(gfp);
6285 +       if (unlikely(!p))
6286 +               goto out_dpages;
6287 +
6288 +       dpages->dpages[0].ndentry = 0;
6289 +       dpages->dpages[0].dentries = p;
6290 +       dpages->ndpage = 1;
6291 +       return 0; /* success */
6292 +
6293 +out_dpages:
6294 +       kfree(dpages->dpages);
6295 +out:
6296 +       return err;
6297 +}
6298 +
6299 +void au_dpages_free(struct au_dcsub_pages *dpages)
6300 +{
6301 +       int i;
6302 +       struct au_dpage *p;
6303 +
6304 +       p = dpages->dpages;
6305 +       for (i = 0; i < dpages->ndpage; i++)
6306 +               au_dpage_free(p++);
6307 +       kfree(dpages->dpages);
6308 +}
6309 +
6310 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6311 +                           struct dentry *dentry, gfp_t gfp)
6312 +{
6313 +       int err, sz;
6314 +       struct au_dpage *dpage;
6315 +       void *p;
6316 +
6317 +       dpage = dpages->dpages + dpages->ndpage - 1;
6318 +       sz = PAGE_SIZE / sizeof(dentry);
6319 +       if (unlikely(dpage->ndentry >= sz)) {
6320 +               AuLabel(new dpage);
6321 +               err = -ENOMEM;
6322 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6323 +               p = au_kzrealloc(dpages->dpages, sz,
6324 +                                sz + sizeof(*dpages->dpages), gfp);
6325 +               if (unlikely(!p))
6326 +                       goto out;
6327 +
6328 +               dpages->dpages = p;
6329 +               dpage = dpages->dpages + dpages->ndpage;
6330 +               p = (void *)__get_free_page(gfp);
6331 +               if (unlikely(!p))
6332 +                       goto out;
6333 +
6334 +               dpage->ndentry = 0;
6335 +               dpage->dentries = p;
6336 +               dpages->ndpage++;
6337 +       }
6338 +
6339 +       AuDebugOn(au_dcount(dentry) <= 0);
6340 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6341 +       return 0; /* success */
6342 +
6343 +out:
6344 +       return err;
6345 +}
6346 +
6347 +/* todo: BAD approach */
6348 +/* copied from linux/fs/dcache.c */
6349 +enum d_walk_ret {
6350 +       D_WALK_CONTINUE,
6351 +       D_WALK_QUIT,
6352 +       D_WALK_NORETRY,
6353 +       D_WALK_SKIP,
6354 +};
6355 +
6356 +extern void d_walk(struct dentry *parent, void *data,
6357 +                  enum d_walk_ret (*enter)(void *, struct dentry *),
6358 +                  void (*finish)(void *));
6359 +
6360 +struct ac_dpages_arg {
6361 +       int err;
6362 +       struct au_dcsub_pages *dpages;
6363 +       struct super_block *sb;
6364 +       au_dpages_test test;
6365 +       void *arg;
6366 +};
6367 +
6368 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6369 +{
6370 +       enum d_walk_ret ret;
6371 +       struct ac_dpages_arg *arg = _arg;
6372 +
6373 +       ret = D_WALK_CONTINUE;
6374 +       if (dentry->d_sb == arg->sb
6375 +           && !IS_ROOT(dentry)
6376 +           && au_dcount(dentry) > 0
6377 +           && au_di(dentry)
6378 +           && (!arg->test || arg->test(dentry, arg->arg))) {
6379 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6380 +               if (unlikely(arg->err))
6381 +                       ret = D_WALK_QUIT;
6382 +       }
6383 +
6384 +       return ret;
6385 +}
6386 +
6387 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6388 +                  au_dpages_test test, void *arg)
6389 +{
6390 +       struct ac_dpages_arg args = {
6391 +               .err    = 0,
6392 +               .dpages = dpages,
6393 +               .sb     = root->d_sb,
6394 +               .test   = test,
6395 +               .arg    = arg
6396 +       };
6397 +
6398 +       d_walk(root, &args, au_call_dpages_append, NULL);
6399 +
6400 +       return args.err;
6401 +}
6402 +
6403 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6404 +                      int do_include, au_dpages_test test, void *arg)
6405 +{
6406 +       int err;
6407 +
6408 +       err = 0;
6409 +       write_seqlock(&rename_lock);
6410 +       spin_lock(&dentry->d_lock);
6411 +       if (do_include
6412 +           && au_dcount(dentry) > 0
6413 +           && (!test || test(dentry, arg)))
6414 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6415 +       spin_unlock(&dentry->d_lock);
6416 +       if (unlikely(err))
6417 +               goto out;
6418 +
6419 +       /*
6420 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
6421 +        * mount
6422 +        */
6423 +       while (!IS_ROOT(dentry)) {
6424 +               dentry = dentry->d_parent; /* rename_lock is locked */
6425 +               spin_lock(&dentry->d_lock);
6426 +               if (au_dcount(dentry) > 0
6427 +                   && (!test || test(dentry, arg)))
6428 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6429 +               spin_unlock(&dentry->d_lock);
6430 +               if (unlikely(err))
6431 +                       break;
6432 +       }
6433 +
6434 +out:
6435 +       write_sequnlock(&rename_lock);
6436 +       return err;
6437 +}
6438 +
6439 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6440 +{
6441 +       return au_di(dentry) && dentry->d_sb == arg;
6442 +}
6443 +
6444 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6445 +                           struct dentry *dentry, int do_include)
6446 +{
6447 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
6448 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
6449 +}
6450 +
6451 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
6452 +{
6453 +       struct path path[2] = {
6454 +               {
6455 +                       .dentry = d1
6456 +               },
6457 +               {
6458 +                       .dentry = d2
6459 +               }
6460 +       };
6461 +
6462 +       return path_is_under(path + 0, path + 1);
6463 +}
6464 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
6465 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
6466 +++ linux/fs/aufs/dcsub.h       2015-04-13 15:10:20.780156788 +0200
6467 @@ -0,0 +1,132 @@
6468 +/*
6469 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6470 + *
6471 + * This program, aufs is free software; you can redistribute it and/or modify
6472 + * it under the terms of the GNU General Public License as published by
6473 + * the Free Software Foundation; either version 2 of the License, or
6474 + * (at your option) any later version.
6475 + *
6476 + * This program is distributed in the hope that it will be useful,
6477 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6478 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6479 + * GNU General Public License for more details.
6480 + *
6481 + * You should have received a copy of the GNU General Public License
6482 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6483 + */
6484 +
6485 +/*
6486 + * sub-routines for dentry cache
6487 + */
6488 +
6489 +#ifndef __AUFS_DCSUB_H__
6490 +#define __AUFS_DCSUB_H__
6491 +
6492 +#ifdef __KERNEL__
6493 +
6494 +#include <linux/dcache.h>
6495 +#include <linux/fs.h>
6496 +
6497 +struct au_dpage {
6498 +       int ndentry;
6499 +       struct dentry **dentries;
6500 +};
6501 +
6502 +struct au_dcsub_pages {
6503 +       int ndpage;
6504 +       struct au_dpage *dpages;
6505 +};
6506 +
6507 +/* ---------------------------------------------------------------------- */
6508 +
6509 +/* dcsub.c */
6510 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6511 +void au_dpages_free(struct au_dcsub_pages *dpages);
6512 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6513 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6514 +                  au_dpages_test test, void *arg);
6515 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6516 +                      int do_include, au_dpages_test test, void *arg);
6517 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6518 +                           struct dentry *dentry, int do_include);
6519 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
6520 +
6521 +/* ---------------------------------------------------------------------- */
6522 +
6523 +/*
6524 + * todo: in linux-3.13, several similar (but faster) helpers are added to
6525 + * include/linux/dcache.h. Try them (in the future).
6526 + */
6527 +
6528 +static inline int au_d_hashed_positive(struct dentry *d)
6529 +{
6530 +       int err;
6531 +       struct inode *inode = d->d_inode;
6532 +
6533 +       err = 0;
6534 +       if (unlikely(d_unhashed(d) || !inode || !inode->i_nlink))
6535 +               err = -ENOENT;
6536 +       return err;
6537 +}
6538 +
6539 +static inline int au_d_linkable(struct dentry *d)
6540 +{
6541 +       int err;
6542 +       struct inode *inode = d->d_inode;
6543 +
6544 +       err = au_d_hashed_positive(d);
6545 +       if (err
6546 +           && inode
6547 +           && (inode->i_state & I_LINKABLE))
6548 +               err = 0;
6549 +       return err;
6550 +}
6551 +
6552 +static inline int au_d_alive(struct dentry *d)
6553 +{
6554 +       int err;
6555 +       struct inode *inode;
6556 +
6557 +       err = 0;
6558 +       if (!IS_ROOT(d))
6559 +               err = au_d_hashed_positive(d);
6560 +       else {
6561 +               inode = d->d_inode;
6562 +               if (unlikely(d_unlinked(d) || !inode || !inode->i_nlink))
6563 +                       err = -ENOENT;
6564 +       }
6565 +       return err;
6566 +}
6567 +
6568 +static inline int au_alive_dir(struct dentry *d)
6569 +{
6570 +       int err;
6571 +
6572 +       err = au_d_alive(d);
6573 +       if (unlikely(err || IS_DEADDIR(d->d_inode)))
6574 +               err = -ENOENT;
6575 +       return err;
6576 +}
6577 +
6578 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
6579 +{
6580 +       return a->len == b->len
6581 +               && !memcmp(a->name, b->name, a->len);
6582 +}
6583 +
6584 +/*
6585 + * by the commit
6586 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
6587 + *                     taking d_lock
6588 + * the type of d_lockref.count became int, but the inlined function d_count()
6589 + * still returns unsigned int.
6590 + * I don't know why. Maybe it is for every d_count() users?
6591 + * Anyway au_dcount() lives on.
6592 + */
6593 +static inline int au_dcount(struct dentry *d)
6594 +{
6595 +       return (int)d_count(d);
6596 +}
6597 +
6598 +#endif /* __KERNEL__ */
6599 +#endif /* __AUFS_DCSUB_H__ */
6600 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
6601 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
6602 +++ linux/fs/aufs/debug.c       2015-04-13 15:10:20.780156788 +0200
6603 @@ -0,0 +1,438 @@
6604 +/*
6605 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6606 + *
6607 + * This program, aufs is free software; you can redistribute it and/or modify
6608 + * it under the terms of the GNU General Public License as published by
6609 + * the Free Software Foundation; either version 2 of the License, or
6610 + * (at your option) any later version.
6611 + *
6612 + * This program is distributed in the hope that it will be useful,
6613 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6614 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6615 + * GNU General Public License for more details.
6616 + *
6617 + * You should have received a copy of the GNU General Public License
6618 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6619 + */
6620 +
6621 +/*
6622 + * debug print functions
6623 + */
6624 +
6625 +#include "aufs.h"
6626 +
6627 +/* Returns 0, or -errno.  arg is in kp->arg. */
6628 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
6629 +{
6630 +       int err, n;
6631 +
6632 +       err = kstrtoint(val, 0, &n);
6633 +       if (!err) {
6634 +               if (n > 0)
6635 +                       au_debug_on();
6636 +               else
6637 +                       au_debug_off();
6638 +       }
6639 +       return err;
6640 +}
6641 +
6642 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
6643 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
6644 +{
6645 +       atomic_t *a;
6646 +
6647 +       a = kp->arg;
6648 +       return sprintf(buffer, "%d", atomic_read(a));
6649 +}
6650 +
6651 +static struct kernel_param_ops param_ops_atomic_t = {
6652 +       .set = param_atomic_t_set,
6653 +       .get = param_atomic_t_get
6654 +       /* void (*free)(void *arg) */
6655 +};
6656 +
6657 +atomic_t aufs_debug = ATOMIC_INIT(0);
6658 +MODULE_PARM_DESC(debug, "debug print");
6659 +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
6660 +
6661 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
6662 +char *au_plevel = KERN_DEBUG;
6663 +#define dpri(fmt, ...) do {                                    \
6664 +       if ((au_plevel                                          \
6665 +            && strcmp(au_plevel, KERN_DEBUG))                  \
6666 +           || au_debug_test())                                 \
6667 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
6668 +} while (0)
6669 +
6670 +/* ---------------------------------------------------------------------- */
6671 +
6672 +void au_dpri_whlist(struct au_nhash *whlist)
6673 +{
6674 +       unsigned long ul, n;
6675 +       struct hlist_head *head;
6676 +       struct au_vdir_wh *pos;
6677 +
6678 +       n = whlist->nh_num;
6679 +       head = whlist->nh_head;
6680 +       for (ul = 0; ul < n; ul++) {
6681 +               hlist_for_each_entry(pos, head, wh_hash)
6682 +                       dpri("b%d, %.*s, %d\n",
6683 +                            pos->wh_bindex,
6684 +                            pos->wh_str.len, pos->wh_str.name,
6685 +                            pos->wh_str.len);
6686 +               head++;
6687 +       }
6688 +}
6689 +
6690 +void au_dpri_vdir(struct au_vdir *vdir)
6691 +{
6692 +       unsigned long ul;
6693 +       union au_vdir_deblk_p p;
6694 +       unsigned char *o;
6695 +
6696 +       if (!vdir || IS_ERR(vdir)) {
6697 +               dpri("err %ld\n", PTR_ERR(vdir));
6698 +               return;
6699 +       }
6700 +
6701 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
6702 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
6703 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
6704 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
6705 +               p.deblk = vdir->vd_deblk[ul];
6706 +               o = p.deblk;
6707 +               dpri("[%lu]: %p\n", ul, o);
6708 +       }
6709 +}
6710 +
6711 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
6712 +                       struct dentry *wh)
6713 +{
6714 +       char *n = NULL;
6715 +       int l = 0;
6716 +
6717 +       if (!inode || IS_ERR(inode)) {
6718 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
6719 +               return -1;
6720 +       }
6721 +
6722 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
6723 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
6724 +                    && sizeof(inode->i_blocks) != sizeof(u64));
6725 +       if (wh) {
6726 +               n = (void *)wh->d_name.name;
6727 +               l = wh->d_name.len;
6728 +       }
6729 +
6730 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
6731 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
6732 +            bindex, inode,
6733 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
6734 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
6735 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
6736 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
6737 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
6738 +            inode->i_state, inode->i_flags, inode->i_version,
6739 +            inode->i_generation,
6740 +            l ? ", wh " : "", l, n);
6741 +       return 0;
6742 +}
6743 +
6744 +void au_dpri_inode(struct inode *inode)
6745 +{
6746 +       struct au_iinfo *iinfo;
6747 +       aufs_bindex_t bindex;
6748 +       int err, hn;
6749 +
6750 +       err = do_pri_inode(-1, inode, -1, NULL);
6751 +       if (err || !au_test_aufs(inode->i_sb))
6752 +               return;
6753 +
6754 +       iinfo = au_ii(inode);
6755 +       if (!iinfo)
6756 +               return;
6757 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
6758 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
6759 +       if (iinfo->ii_bstart < 0)
6760 +               return;
6761 +       hn = 0;
6762 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
6763 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
6764 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
6765 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
6766 +       }
6767 +}
6768 +
6769 +void au_dpri_dalias(struct inode *inode)
6770 +{
6771 +       struct dentry *d;
6772 +
6773 +       spin_lock(&inode->i_lock);
6774 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
6775 +               au_dpri_dentry(d);
6776 +       spin_unlock(&inode->i_lock);
6777 +}
6778 +
6779 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
6780 +{
6781 +       struct dentry *wh = NULL;
6782 +       int hn;
6783 +       struct au_iinfo *iinfo;
6784 +
6785 +       if (!dentry || IS_ERR(dentry)) {
6786 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
6787 +               return -1;
6788 +       }
6789 +       /* do not call dget_parent() here */
6790 +       /* note: access d_xxx without d_lock */
6791 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
6792 +            bindex, dentry, dentry,
6793 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
6794 +            au_dcount(dentry), dentry->d_flags,
6795 +            d_unhashed(dentry) ? "un" : "");
6796 +       hn = -1;
6797 +       if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
6798 +               iinfo = au_ii(dentry->d_inode);
6799 +               if (iinfo) {
6800 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
6801 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
6802 +               }
6803 +       }
6804 +       do_pri_inode(bindex, dentry->d_inode, hn, wh);
6805 +       return 0;
6806 +}
6807 +
6808 +void au_dpri_dentry(struct dentry *dentry)
6809 +{
6810 +       struct au_dinfo *dinfo;
6811 +       aufs_bindex_t bindex;
6812 +       int err;
6813 +       struct au_hdentry *hdp;
6814 +
6815 +       err = do_pri_dentry(-1, dentry);
6816 +       if (err || !au_test_aufs(dentry->d_sb))
6817 +               return;
6818 +
6819 +       dinfo = au_di(dentry);
6820 +       if (!dinfo)
6821 +               return;
6822 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
6823 +            dinfo->di_bstart, dinfo->di_bend,
6824 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
6825 +            dinfo->di_tmpfile);
6826 +       if (dinfo->di_bstart < 0)
6827 +               return;
6828 +       hdp = dinfo->di_hdentry;
6829 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
6830 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
6831 +}
6832 +
6833 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
6834 +{
6835 +       char a[32];
6836 +
6837 +       if (!file || IS_ERR(file)) {
6838 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
6839 +               return -1;
6840 +       }
6841 +       a[0] = 0;
6842 +       if (bindex < 0
6843 +           && file->f_path.dentry
6844 +           && au_test_aufs(file->f_path.dentry->d_sb)
6845 +           && au_fi(file))
6846 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
6847 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
6848 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
6849 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
6850 +            file->f_version, file->f_pos, a);
6851 +       if (file->f_path.dentry)
6852 +               do_pri_dentry(bindex, file->f_path.dentry);
6853 +       return 0;
6854 +}
6855 +
6856 +void au_dpri_file(struct file *file)
6857 +{
6858 +       struct au_finfo *finfo;
6859 +       struct au_fidir *fidir;
6860 +       struct au_hfile *hfile;
6861 +       aufs_bindex_t bindex;
6862 +       int err;
6863 +
6864 +       err = do_pri_file(-1, file);
6865 +       if (err
6866 +           || !file->f_path.dentry
6867 +           || !au_test_aufs(file->f_path.dentry->d_sb))
6868 +               return;
6869 +
6870 +       finfo = au_fi(file);
6871 +       if (!finfo)
6872 +               return;
6873 +       if (finfo->fi_btop < 0)
6874 +               return;
6875 +       fidir = finfo->fi_hdir;
6876 +       if (!fidir)
6877 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
6878 +       else
6879 +               for (bindex = finfo->fi_btop;
6880 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
6881 +                    bindex++) {
6882 +                       hfile = fidir->fd_hfile + bindex;
6883 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
6884 +               }
6885 +}
6886 +
6887 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
6888 +{
6889 +       struct vfsmount *mnt;
6890 +       struct super_block *sb;
6891 +
6892 +       if (!br || IS_ERR(br))
6893 +               goto out;
6894 +       mnt = au_br_mnt(br);
6895 +       if (!mnt || IS_ERR(mnt))
6896 +               goto out;
6897 +       sb = mnt->mnt_sb;
6898 +       if (!sb || IS_ERR(sb))
6899 +               goto out;
6900 +
6901 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
6902 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
6903 +            "xino %d\n",
6904 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
6905 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
6906 +            sb->s_flags, sb->s_count,
6907 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
6908 +       return 0;
6909 +
6910 +out:
6911 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
6912 +       return -1;
6913 +}
6914 +
6915 +void au_dpri_sb(struct super_block *sb)
6916 +{
6917 +       struct au_sbinfo *sbinfo;
6918 +       aufs_bindex_t bindex;
6919 +       int err;
6920 +       /* to reuduce stack size */
6921 +       struct {
6922 +               struct vfsmount mnt;
6923 +               struct au_branch fake;
6924 +       } *a;
6925 +
6926 +       /* this function can be called from magic sysrq */
6927 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
6928 +       if (unlikely(!a)) {
6929 +               dpri("no memory\n");
6930 +               return;
6931 +       }
6932 +
6933 +       a->mnt.mnt_sb = sb;
6934 +       a->fake.br_perm = 0;
6935 +       a->fake.br_path.mnt = &a->mnt;
6936 +       a->fake.br_xino.xi_file = NULL;
6937 +       atomic_set(&a->fake.br_count, 0);
6938 +       smp_mb(); /* atomic_set */
6939 +       err = do_pri_br(-1, &a->fake);
6940 +       kfree(a);
6941 +       dpri("dev 0x%x\n", sb->s_dev);
6942 +       if (err || !au_test_aufs(sb))
6943 +               return;
6944 +
6945 +       sbinfo = au_sbi(sb);
6946 +       if (!sbinfo)
6947 +               return;
6948 +       dpri("nw %d, gen %u, kobj %d\n",
6949 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
6950 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
6951 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
6952 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
6953 +}
6954 +
6955 +/* ---------------------------------------------------------------------- */
6956 +
6957 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
6958 +{
6959 +       struct inode *h_inode, *inode = dentry->d_inode;
6960 +       struct dentry *h_dentry;
6961 +       aufs_bindex_t bindex, bend, bi;
6962 +
6963 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
6964 +               return;
6965 +
6966 +       bend = au_dbend(dentry);
6967 +       bi = au_ibend(inode);
6968 +       if (bi < bend)
6969 +               bend = bi;
6970 +       bindex = au_dbstart(dentry);
6971 +       bi = au_ibstart(inode);
6972 +       if (bi > bindex)
6973 +               bindex = bi;
6974 +
6975 +       for (; bindex <= bend; bindex++) {
6976 +               h_dentry = au_h_dptr(dentry, bindex);
6977 +               if (!h_dentry)
6978 +                       continue;
6979 +               h_inode = au_h_iptr(inode, bindex);
6980 +               if (unlikely(h_inode != h_dentry->d_inode)) {
6981 +                       au_debug_on();
6982 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
6983 +                       AuDbgDentry(dentry);
6984 +                       AuDbgInode(inode);
6985 +                       au_debug_off();
6986 +                       BUG();
6987 +               }
6988 +       }
6989 +}
6990 +
6991 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
6992 +{
6993 +       int err, i, j;
6994 +       struct au_dcsub_pages dpages;
6995 +       struct au_dpage *dpage;
6996 +       struct dentry **dentries;
6997 +
6998 +       err = au_dpages_init(&dpages, GFP_NOFS);
6999 +       AuDebugOn(err);
7000 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7001 +       AuDebugOn(err);
7002 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7003 +               dpage = dpages.dpages + i;
7004 +               dentries = dpage->dentries;
7005 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7006 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7007 +       }
7008 +       au_dpages_free(&dpages);
7009 +}
7010 +
7011 +void au_dbg_verify_kthread(void)
7012 +{
7013 +       if (au_wkq_test()) {
7014 +               au_dbg_blocked();
7015 +               /*
7016 +                * It may be recursive, but udba=notify between two aufs mounts,
7017 +                * where a single ro branch is shared, is not a problem.
7018 +                */
7019 +               /* WARN_ON(1); */
7020 +       }
7021 +}
7022 +
7023 +/* ---------------------------------------------------------------------- */
7024 +
7025 +int __init au_debug_init(void)
7026 +{
7027 +       aufs_bindex_t bindex;
7028 +       struct au_vdir_destr destr;
7029 +
7030 +       bindex = -1;
7031 +       AuDebugOn(bindex >= 0);
7032 +
7033 +       destr.len = -1;
7034 +       AuDebugOn(destr.len < NAME_MAX);
7035 +
7036 +#ifdef CONFIG_4KSTACKS
7037 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7038 +#endif
7039 +
7040 +       return 0;
7041 +}
7042 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7043 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7044 +++ linux/fs/aufs/debug.h       2015-04-13 15:10:20.780156788 +0200
7045 @@ -0,0 +1,228 @@
7046 +/*
7047 + * Copyright (C) 2005-2015 Junjiro R. Okajima
7048 + *
7049 + * This program, aufs is free software; you can redistribute it and/or modify
7050 + * it under the terms of the GNU General Public License as published by
7051 + * the Free Software Foundation; either version 2 of the License, or
7052 + * (at your option) any later version.
7053 + *
7054 + * This program is distributed in the hope that it will be useful,
7055 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7056 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7057 + * GNU General Public License for more details.
7058 + *
7059 + * You should have received a copy of the GNU General Public License
7060 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7061 + */
7062 +
7063 +/*
7064 + * debug print functions
7065 + */
7066 +
7067 +#ifndef __AUFS_DEBUG_H__
7068 +#define __AUFS_DEBUG_H__
7069 +
7070 +#ifdef __KERNEL__
7071 +
7072 +#include <linux/atomic.h>
7073 +#include <linux/module.h>
7074 +#include <linux/kallsyms.h>
7075 +#include <linux/sysrq.h>
7076 +
7077 +#ifdef CONFIG_AUFS_DEBUG
7078 +#define AuDebugOn(a)           BUG_ON(a)
7079 +
7080 +/* module parameter */
7081 +extern atomic_t aufs_debug;
7082 +static inline void au_debug_on(void)
7083 +{
7084 +       atomic_inc(&aufs_debug);
7085 +}
7086 +static inline void au_debug_off(void)
7087 +{
7088 +       atomic_dec_if_positive(&aufs_debug);
7089 +}
7090 +
7091 +static inline int au_debug_test(void)
7092 +{
7093 +       return atomic_read(&aufs_debug) > 0;
7094 +}
7095 +#else
7096 +#define AuDebugOn(a)           do {} while (0)
7097 +AuStubVoid(au_debug_on, void)
7098 +AuStubVoid(au_debug_off, void)
7099 +AuStubInt0(au_debug_test, void)
7100 +#endif /* CONFIG_AUFS_DEBUG */
7101 +
7102 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7103 +
7104 +/* ---------------------------------------------------------------------- */
7105 +
7106 +/* debug print */
7107 +
7108 +#define AuDbg(fmt, ...) do { \
7109 +       if (au_debug_test()) \
7110 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7111 +} while (0)
7112 +#define AuLabel(l)             AuDbg(#l "\n")
7113 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7114 +#define AuWarn1(fmt, ...) do { \
7115 +       static unsigned char _c; \
7116 +       if (!_c++) \
7117 +               pr_warn(fmt, ##__VA_ARGS__); \
7118 +} while (0)
7119 +
7120 +#define AuErr1(fmt, ...) do { \
7121 +       static unsigned char _c; \
7122 +       if (!_c++) \
7123 +               pr_err(fmt, ##__VA_ARGS__); \
7124 +} while (0)
7125 +
7126 +#define AuIOErr1(fmt, ...) do { \
7127 +       static unsigned char _c; \
7128 +       if (!_c++) \
7129 +               AuIOErr(fmt, ##__VA_ARGS__); \
7130 +} while (0)
7131 +
7132 +#define AuUnsupportMsg "This operation is not supported." \
7133 +                       " Please report this application to aufs-users ML."
7134 +#define AuUnsupport(fmt, ...) do { \
7135 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7136 +       dump_stack(); \
7137 +} while (0)
7138 +
7139 +#define AuTraceErr(e) do { \
7140 +       if (unlikely((e) < 0)) \
7141 +               AuDbg("err %d\n", (int)(e)); \
7142 +} while (0)
7143 +
7144 +#define AuTraceErrPtr(p) do { \
7145 +       if (IS_ERR(p)) \
7146 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7147 +} while (0)
7148 +
7149 +/* dirty macros for debug print, use with "%.*s" and caution */
7150 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7151 +
7152 +/* ---------------------------------------------------------------------- */
7153 +
7154 +struct dentry;
7155 +#ifdef CONFIG_AUFS_DEBUG
7156 +extern struct mutex au_dbg_mtx;
7157 +extern char *au_plevel;
7158 +struct au_nhash;
7159 +void au_dpri_whlist(struct au_nhash *whlist);
7160 +struct au_vdir;
7161 +void au_dpri_vdir(struct au_vdir *vdir);
7162 +struct inode;
7163 +void au_dpri_inode(struct inode *inode);
7164 +void au_dpri_dalias(struct inode *inode);
7165 +void au_dpri_dentry(struct dentry *dentry);
7166 +struct file;
7167 +void au_dpri_file(struct file *filp);
7168 +struct super_block;
7169 +void au_dpri_sb(struct super_block *sb);
7170 +
7171 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7172 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7173 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7174 +void au_dbg_verify_kthread(void);
7175 +
7176 +int __init au_debug_init(void);
7177 +
7178 +#define AuDbgWhlist(w) do { \
7179 +       mutex_lock(&au_dbg_mtx); \
7180 +       AuDbg(#w "\n"); \
7181 +       au_dpri_whlist(w); \
7182 +       mutex_unlock(&au_dbg_mtx); \
7183 +} while (0)
7184 +
7185 +#define AuDbgVdir(v) do { \
7186 +       mutex_lock(&au_dbg_mtx); \
7187 +       AuDbg(#v "\n"); \
7188 +       au_dpri_vdir(v); \
7189 +       mutex_unlock(&au_dbg_mtx); \
7190 +} while (0)
7191 +
7192 +#define AuDbgInode(i) do { \
7193 +       mutex_lock(&au_dbg_mtx); \
7194 +       AuDbg(#i "\n"); \
7195 +       au_dpri_inode(i); \
7196 +       mutex_unlock(&au_dbg_mtx); \
7197 +} while (0)
7198 +
7199 +#define AuDbgDAlias(i) do { \
7200 +       mutex_lock(&au_dbg_mtx); \
7201 +       AuDbg(#i "\n"); \
7202 +       au_dpri_dalias(i); \
7203 +       mutex_unlock(&au_dbg_mtx); \
7204 +} while (0)
7205 +
7206 +#define AuDbgDentry(d) do { \
7207 +       mutex_lock(&au_dbg_mtx); \
7208 +       AuDbg(#d "\n"); \
7209 +       au_dpri_dentry(d); \
7210 +       mutex_unlock(&au_dbg_mtx); \
7211 +} while (0)
7212 +
7213 +#define AuDbgFile(f) do { \
7214 +       mutex_lock(&au_dbg_mtx); \
7215 +       AuDbg(#f "\n"); \
7216 +       au_dpri_file(f); \
7217 +       mutex_unlock(&au_dbg_mtx); \
7218 +} while (0)
7219 +
7220 +#define AuDbgSb(sb) do { \
7221 +       mutex_lock(&au_dbg_mtx); \
7222 +       AuDbg(#sb "\n"); \
7223 +       au_dpri_sb(sb); \
7224 +       mutex_unlock(&au_dbg_mtx); \
7225 +} while (0)
7226 +
7227 +#define AuDbgSym(addr) do {                            \
7228 +       char sym[KSYM_SYMBOL_LEN];                      \
7229 +       sprint_symbol(sym, (unsigned long)addr);        \
7230 +       AuDbg("%s\n", sym);                             \
7231 +} while (0)
7232 +#else
7233 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7234 +AuStubVoid(au_dbg_verify_dir_parent, struct dentry *dentry, unsigned int sigen)
7235 +AuStubVoid(au_dbg_verify_nondir_parent, struct dentry *dentry,
7236 +          unsigned int sigen)
7237 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7238 +AuStubVoid(au_dbg_verify_kthread, void)
7239 +AuStubInt0(__init au_debug_init, void)
7240 +
7241 +#define AuDbgWhlist(w)         do {} while (0)
7242 +#define AuDbgVdir(v)           do {} while (0)
7243 +#define AuDbgInode(i)          do {} while (0)
7244 +#define AuDbgDAlias(i)         do {} while (0)
7245 +#define AuDbgDentry(d)         do {} while (0)
7246 +#define AuDbgFile(f)           do {} while (0)
7247 +#define AuDbgSb(sb)            do {} while (0)
7248 +#define AuDbgSym(addr)         do {} while (0)
7249 +#endif /* CONFIG_AUFS_DEBUG */
7250 +
7251 +/* ---------------------------------------------------------------------- */
7252 +
7253 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7254 +int __init au_sysrq_init(void);
7255 +void au_sysrq_fin(void);
7256 +
7257 +#ifdef CONFIG_HW_CONSOLE
7258 +#define au_dbg_blocked() do { \
7259 +       WARN_ON(1); \
7260 +       handle_sysrq('w'); \
7261 +} while (0)
7262 +#else
7263 +AuStubVoid(au_dbg_blocked, void)
7264 +#endif
7265 +
7266 +#else
7267 +AuStubInt0(__init au_sysrq_init, void)
7268 +AuStubVoid(au_sysrq_fin, void)
7269 +AuStubVoid(au_dbg_blocked, void)
7270 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7271 +
7272 +#endif /* __KERNEL__ */
7273 +#endif /* __AUFS_DEBUG_H__ */
7274 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7275 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7276 +++ linux/fs/aufs/dentry.c      2015-04-13 15:10:20.780156788 +0200
7277 @@ -0,0 +1,1097 @@
7278 +/*
7279 + * Copyright (C) 2005-2015 Junjiro R. Okajima
7280 + *
7281 + * This program, aufs is free software; you can redistribute it and/or modify
7282 + * it under the terms of the GNU General Public License as published by
7283 + * the Free Software Foundation; either version 2 of the License, or
7284 + * (at your option) any later version.
7285 + *
7286 + * This program is distributed in the hope that it will be useful,
7287 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7288 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7289 + * GNU General Public License for more details.
7290 + *
7291 + * You should have received a copy of the GNU General Public License
7292 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7293 + */
7294 +
7295 +/*
7296 + * lookup and dentry operations
7297 + */
7298 +
7299 +#include <linux/namei.h>
7300 +#include "aufs.h"
7301 +
7302 +#define AuLkup_ALLOW_NEG       1
7303 +#define AuLkup_IGNORE_PERM     (1 << 1)
7304 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
7305 +#define au_fset_lkup(flags, name) \
7306 +       do { (flags) |= AuLkup_##name; } while (0)
7307 +#define au_fclr_lkup(flags, name) \
7308 +       do { (flags) &= ~AuLkup_##name; } while (0)
7309 +
7310 +struct au_do_lookup_args {
7311 +       unsigned int            flags;
7312 +       mode_t                  type;
7313 +};
7314 +
7315 +/*
7316 + * returns positive/negative dentry, NULL or an error.
7317 + * NULL means whiteout-ed or not-found.
7318 + */
7319 +static struct dentry*
7320 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7321 +            aufs_bindex_t bindex, struct qstr *wh_name,
7322 +            struct au_do_lookup_args *args)
7323 +{
7324 +       struct dentry *h_dentry;
7325 +       struct inode *h_inode;
7326 +       struct au_branch *br;
7327 +       int wh_found, opq;
7328 +       unsigned char wh_able;
7329 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7330 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7331 +                                                         IGNORE_PERM);
7332 +
7333 +       wh_found = 0;
7334 +       br = au_sbr(dentry->d_sb, bindex);
7335 +       wh_able = !!au_br_whable(br->br_perm);
7336 +       if (wh_able)
7337 +               wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
7338 +       h_dentry = ERR_PTR(wh_found);
7339 +       if (!wh_found)
7340 +               goto real_lookup;
7341 +       if (unlikely(wh_found < 0))
7342 +               goto out;
7343 +
7344 +       /* We found a whiteout */
7345 +       /* au_set_dbend(dentry, bindex); */
7346 +       au_set_dbwh(dentry, bindex);
7347 +       if (!allow_neg)
7348 +               return NULL; /* success */
7349 +
7350 +real_lookup:
7351 +       if (!ignore_perm)
7352 +               h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
7353 +       else
7354 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7355 +       if (IS_ERR(h_dentry)) {
7356 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7357 +                   && !allow_neg)
7358 +                       h_dentry = NULL;
7359 +               goto out;
7360 +       }
7361 +
7362 +       h_inode = h_dentry->d_inode;
7363 +       if (!h_inode) {
7364 +               if (!allow_neg)
7365 +                       goto out_neg;
7366 +       } else if (wh_found
7367 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7368 +               goto out_neg;
7369 +
7370 +       if (au_dbend(dentry) <= bindex)
7371 +               au_set_dbend(dentry, bindex);
7372 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7373 +               au_set_dbstart(dentry, bindex);
7374 +       au_set_h_dptr(dentry, bindex, h_dentry);
7375 +
7376 +       if (!d_is_dir(h_dentry)
7377 +           || !wh_able
7378 +           || (d_is_positive(dentry) && !d_is_dir(dentry)))
7379 +               goto out; /* success */
7380 +
7381 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
7382 +       opq = au_diropq_test(h_dentry);
7383 +       mutex_unlock(&h_inode->i_mutex);
7384 +       if (opq > 0)
7385 +               au_set_dbdiropq(dentry, bindex);
7386 +       else if (unlikely(opq < 0)) {
7387 +               au_set_h_dptr(dentry, bindex, NULL);
7388 +               h_dentry = ERR_PTR(opq);
7389 +       }
7390 +       goto out;
7391 +
7392 +out_neg:
7393 +       dput(h_dentry);
7394 +       h_dentry = NULL;
7395 +out:
7396 +       return h_dentry;
7397 +}
7398 +
7399 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7400 +{
7401 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7402 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7403 +               return -EPERM;
7404 +       return 0;
7405 +}
7406 +
7407 +/*
7408 + * returns the number of lower positive dentries,
7409 + * otherwise an error.
7410 + * can be called at unlinking with @type is zero.
7411 + */
7412 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
7413 +{
7414 +       int npositive, err;
7415 +       aufs_bindex_t bindex, btail, bdiropq;
7416 +       unsigned char isdir, dirperm1;
7417 +       struct qstr whname;
7418 +       struct au_do_lookup_args args = {
7419 +               .flags          = 0,
7420 +               .type           = type
7421 +       };
7422 +       const struct qstr *name = &dentry->d_name;
7423 +       struct dentry *parent;
7424 +       struct inode *inode;
7425 +       struct super_block *sb;
7426 +
7427 +       sb = dentry->d_sb;
7428 +       err = au_test_shwh(sb, name);
7429 +       if (unlikely(err))
7430 +               goto out;
7431 +
7432 +       err = au_wh_name_alloc(&whname, name);
7433 +       if (unlikely(err))
7434 +               goto out;
7435 +
7436 +       inode = dentry->d_inode;
7437 +       isdir = !!d_is_dir(dentry);
7438 +       if (!type)
7439 +               au_fset_lkup(args.flags, ALLOW_NEG);
7440 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
7441 +
7442 +       npositive = 0;
7443 +       parent = dget_parent(dentry);
7444 +       btail = au_dbtaildir(parent);
7445 +       for (bindex = bstart; bindex <= btail; bindex++) {
7446 +               struct dentry *h_parent, *h_dentry;
7447 +               struct inode *h_inode, *h_dir;
7448 +
7449 +               h_dentry = au_h_dptr(dentry, bindex);
7450 +               if (h_dentry) {
7451 +                       if (h_dentry->d_inode)
7452 +                               npositive++;
7453 +                       if (type != S_IFDIR)
7454 +                               break;
7455 +                       continue;
7456 +               }
7457 +               h_parent = au_h_dptr(parent, bindex);
7458 +               if (!h_parent || !d_is_dir(h_parent))
7459 +                       continue;
7460 +
7461 +               h_dir = h_parent->d_inode;
7462 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
7463 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7464 +                                       &args);
7465 +               mutex_unlock(&h_dir->i_mutex);
7466 +               err = PTR_ERR(h_dentry);
7467 +               if (IS_ERR(h_dentry))
7468 +                       goto out_parent;
7469 +               if (h_dentry)
7470 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
7471 +               if (dirperm1)
7472 +                       au_fset_lkup(args.flags, IGNORE_PERM);
7473 +
7474 +               if (au_dbwh(dentry) >= 0)
7475 +                       break;
7476 +               if (!h_dentry)
7477 +                       continue;
7478 +               h_inode = h_dentry->d_inode;
7479 +               if (!h_inode)
7480 +                       continue;
7481 +               npositive++;
7482 +               if (!args.type)
7483 +                       args.type = h_inode->i_mode & S_IFMT;
7484 +               if (args.type != S_IFDIR)
7485 +                       break;
7486 +               else if (isdir) {
7487 +                       /* the type of lower may be different */
7488 +                       bdiropq = au_dbdiropq(dentry);
7489 +                       if (bdiropq >= 0 && bdiropq <= bindex)
7490 +                               break;
7491 +               }
7492 +       }
7493 +
7494 +       if (npositive) {
7495 +               AuLabel(positive);
7496 +               au_update_dbstart(dentry);
7497 +       }
7498 +       err = npositive;
7499 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
7500 +                    && au_dbstart(dentry) < 0)) {
7501 +               err = -EIO;
7502 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7503 +                       dentry, err);
7504 +       }
7505 +
7506 +out_parent:
7507 +       dput(parent);
7508 +       kfree(whname.name);
7509 +out:
7510 +       return err;
7511 +}
7512 +
7513 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
7514 +{
7515 +       struct dentry *dentry;
7516 +       int wkq_err;
7517 +
7518 +       if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
7519 +               dentry = vfsub_lkup_one(name, parent);
7520 +       else {
7521 +               struct vfsub_lkup_one_args args = {
7522 +                       .errp   = &dentry,
7523 +                       .name   = name,
7524 +                       .parent = parent
7525 +               };
7526 +
7527 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
7528 +               if (unlikely(wkq_err))
7529 +                       dentry = ERR_PTR(wkq_err);
7530 +       }
7531 +
7532 +       return dentry;
7533 +}
7534 +
7535 +/*
7536 + * lookup @dentry on @bindex which should be negative.
7537 + */
7538 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
7539 +{
7540 +       int err;
7541 +       struct dentry *parent, *h_parent, *h_dentry;
7542 +       struct au_branch *br;
7543 +
7544 +       parent = dget_parent(dentry);
7545 +       h_parent = au_h_dptr(parent, bindex);
7546 +       br = au_sbr(dentry->d_sb, bindex);
7547 +       if (wh)
7548 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
7549 +       else
7550 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7551 +       err = PTR_ERR(h_dentry);
7552 +       if (IS_ERR(h_dentry))
7553 +               goto out;
7554 +       if (unlikely(h_dentry->d_inode)) {
7555 +               err = -EIO;
7556 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
7557 +               dput(h_dentry);
7558 +               goto out;
7559 +       }
7560 +
7561 +       err = 0;
7562 +       if (bindex < au_dbstart(dentry))
7563 +               au_set_dbstart(dentry, bindex);
7564 +       if (au_dbend(dentry) < bindex)
7565 +               au_set_dbend(dentry, bindex);
7566 +       au_set_h_dptr(dentry, bindex, h_dentry);
7567 +
7568 +out:
7569 +       dput(parent);
7570 +       return err;
7571 +}
7572 +
7573 +/* ---------------------------------------------------------------------- */
7574 +
7575 +/* subset of struct inode */
7576 +struct au_iattr {
7577 +       unsigned long           i_ino;
7578 +       /* unsigned int         i_nlink; */
7579 +       kuid_t                  i_uid;
7580 +       kgid_t                  i_gid;
7581 +       u64                     i_version;
7582 +/*
7583 +       loff_t                  i_size;
7584 +       blkcnt_t                i_blocks;
7585 +*/
7586 +       umode_t                 i_mode;
7587 +};
7588 +
7589 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7590 +{
7591 +       ia->i_ino = h_inode->i_ino;
7592 +       /* ia->i_nlink = h_inode->i_nlink; */
7593 +       ia->i_uid = h_inode->i_uid;
7594 +       ia->i_gid = h_inode->i_gid;
7595 +       ia->i_version = h_inode->i_version;
7596 +/*
7597 +       ia->i_size = h_inode->i_size;
7598 +       ia->i_blocks = h_inode->i_blocks;
7599 +*/
7600 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
7601 +}
7602 +
7603 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
7604 +{
7605 +       return ia->i_ino != h_inode->i_ino
7606 +               /* || ia->i_nlink != h_inode->i_nlink */
7607 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
7608 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
7609 +               || ia->i_version != h_inode->i_version
7610 +/*
7611 +               || ia->i_size != h_inode->i_size
7612 +               || ia->i_blocks != h_inode->i_blocks
7613 +*/
7614 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
7615 +}
7616 +
7617 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7618 +                             struct au_branch *br)
7619 +{
7620 +       int err;
7621 +       struct au_iattr ia;
7622 +       struct inode *h_inode;
7623 +       struct dentry *h_d;
7624 +       struct super_block *h_sb;
7625 +
7626 +       err = 0;
7627 +       memset(&ia, -1, sizeof(ia));
7628 +       h_sb = h_dentry->d_sb;
7629 +       h_inode = h_dentry->d_inode;
7630 +       if (h_inode)
7631 +               au_iattr_save(&ia, h_inode);
7632 +       else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
7633 +               /* nfs d_revalidate may return 0 for negative dentry */
7634 +               /* fuse d_revalidate always return 0 for negative dentry */
7635 +               goto out;
7636 +
7637 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
7638 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
7639 +       err = PTR_ERR(h_d);
7640 +       if (IS_ERR(h_d))
7641 +               goto out;
7642 +
7643 +       err = 0;
7644 +       if (unlikely(h_d != h_dentry
7645 +                    || h_d->d_inode != h_inode
7646 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
7647 +               err = au_busy_or_stale();
7648 +       dput(h_d);
7649 +
7650 +out:
7651 +       AuTraceErr(err);
7652 +       return err;
7653 +}
7654 +
7655 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7656 +               struct dentry *h_parent, struct au_branch *br)
7657 +{
7658 +       int err;
7659 +
7660 +       err = 0;
7661 +       if (udba == AuOpt_UDBA_REVAL
7662 +           && !au_test_fs_remote(h_dentry->d_sb)) {
7663 +               IMustLock(h_dir);
7664 +               err = (h_dentry->d_parent->d_inode != h_dir);
7665 +       } else if (udba != AuOpt_UDBA_NONE)
7666 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
7667 +
7668 +       return err;
7669 +}
7670 +
7671 +/* ---------------------------------------------------------------------- */
7672 +
7673 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
7674 +{
7675 +       int err;
7676 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
7677 +       struct au_hdentry tmp, *p, *q;
7678 +       struct au_dinfo *dinfo;
7679 +       struct super_block *sb;
7680 +
7681 +       DiMustWriteLock(dentry);
7682 +
7683 +       sb = dentry->d_sb;
7684 +       dinfo = au_di(dentry);
7685 +       bend = dinfo->di_bend;
7686 +       bwh = dinfo->di_bwh;
7687 +       bdiropq = dinfo->di_bdiropq;
7688 +       p = dinfo->di_hdentry + dinfo->di_bstart;
7689 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
7690 +               if (!p->hd_dentry)
7691 +                       continue;
7692 +
7693 +               new_bindex = au_br_index(sb, p->hd_id);
7694 +               if (new_bindex == bindex)
7695 +                       continue;
7696 +
7697 +               if (dinfo->di_bwh == bindex)
7698 +                       bwh = new_bindex;
7699 +               if (dinfo->di_bdiropq == bindex)
7700 +                       bdiropq = new_bindex;
7701 +               if (new_bindex < 0) {
7702 +                       au_hdput(p);
7703 +                       p->hd_dentry = NULL;
7704 +                       continue;
7705 +               }
7706 +
7707 +               /* swap two lower dentries, and loop again */
7708 +               q = dinfo->di_hdentry + new_bindex;
7709 +               tmp = *q;
7710 +               *q = *p;
7711 +               *p = tmp;
7712 +               if (tmp.hd_dentry) {
7713 +                       bindex--;
7714 +                       p--;
7715 +               }
7716 +       }
7717 +
7718 +       dinfo->di_bwh = -1;
7719 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
7720 +               dinfo->di_bwh = bwh;
7721 +
7722 +       dinfo->di_bdiropq = -1;
7723 +       if (bdiropq >= 0
7724 +           && bdiropq <= au_sbend(sb)
7725 +           && au_sbr_whable(sb, bdiropq))
7726 +               dinfo->di_bdiropq = bdiropq;
7727 +
7728 +       err = -EIO;
7729 +       dinfo->di_bstart = -1;
7730 +       dinfo->di_bend = -1;
7731 +       bend = au_dbend(parent);
7732 +       p = dinfo->di_hdentry;
7733 +       for (bindex = 0; bindex <= bend; bindex++, p++)
7734 +               if (p->hd_dentry) {
7735 +                       dinfo->di_bstart = bindex;
7736 +                       break;
7737 +               }
7738 +
7739 +       if (dinfo->di_bstart >= 0) {
7740 +               p = dinfo->di_hdentry + bend;
7741 +               for (bindex = bend; bindex >= 0; bindex--, p--)
7742 +                       if (p->hd_dentry) {
7743 +                               dinfo->di_bend = bindex;
7744 +                               err = 0;
7745 +                               break;
7746 +                       }
7747 +       }
7748 +
7749 +       return err;
7750 +}
7751 +
7752 +static void au_do_hide(struct dentry *dentry)
7753 +{
7754 +       struct inode *inode;
7755 +
7756 +       inode = dentry->d_inode;
7757 +       if (inode) {
7758 +               if (!S_ISDIR(inode->i_mode)) {
7759 +                       if (inode->i_nlink && !d_unhashed(dentry))
7760 +                               drop_nlink(inode);
7761 +               } else {
7762 +                       clear_nlink(inode);
7763 +                       /* stop next lookup */
7764 +                       inode->i_flags |= S_DEAD;
7765 +               }
7766 +               smp_mb(); /* necessary? */
7767 +       }
7768 +       d_drop(dentry);
7769 +}
7770 +
7771 +static int au_hide_children(struct dentry *parent)
7772 +{
7773 +       int err, i, j, ndentry;
7774 +       struct au_dcsub_pages dpages;
7775 +       struct au_dpage *dpage;
7776 +       struct dentry *dentry;
7777 +
7778 +       err = au_dpages_init(&dpages, GFP_NOFS);
7779 +       if (unlikely(err))
7780 +               goto out;
7781 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
7782 +       if (unlikely(err))
7783 +               goto out_dpages;
7784 +
7785 +       /* in reverse order */
7786 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
7787 +               dpage = dpages.dpages + i;
7788 +               ndentry = dpage->ndentry;
7789 +               for (j = ndentry - 1; j >= 0; j--) {
7790 +                       dentry = dpage->dentries[j];
7791 +                       if (dentry != parent)
7792 +                               au_do_hide(dentry);
7793 +               }
7794 +       }
7795 +
7796 +out_dpages:
7797 +       au_dpages_free(&dpages);
7798 +out:
7799 +       return err;
7800 +}
7801 +
7802 +static void au_hide(struct dentry *dentry)
7803 +{
7804 +       int err;
7805 +
7806 +       AuDbgDentry(dentry);
7807 +       if (d_is_dir(dentry)) {
7808 +               /* shrink_dcache_parent(dentry); */
7809 +               err = au_hide_children(dentry);
7810 +               if (unlikely(err))
7811 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
7812 +                               dentry, err);
7813 +       }
7814 +       au_do_hide(dentry);
7815 +}
7816 +
7817 +/*
7818 + * By adding a dirty branch, a cached dentry may be affected in various ways.
7819 + *
7820 + * a dirty branch is added
7821 + * - on the top of layers
7822 + * - in the middle of layers
7823 + * - to the bottom of layers
7824 + *
7825 + * on the added branch there exists
7826 + * - a whiteout
7827 + * - a diropq
7828 + * - a same named entry
7829 + *   + exist
7830 + *     * negative --> positive
7831 + *     * positive --> positive
7832 + *      - type is unchanged
7833 + *      - type is changed
7834 + *   + doesn't exist
7835 + *     * negative --> negative
7836 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
7837 + * - none
7838 + */
7839 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
7840 +                              struct au_dinfo *tmp)
7841 +{
7842 +       int err;
7843 +       aufs_bindex_t bindex, bend;
7844 +       struct {
7845 +               struct dentry *dentry;
7846 +               struct inode *inode;
7847 +               mode_t mode;
7848 +       } orig_h, tmp_h;
7849 +       struct au_hdentry *hd;
7850 +       struct inode *inode, *h_inode;
7851 +       struct dentry *h_dentry;
7852 +
7853 +       err = 0;
7854 +       AuDebugOn(dinfo->di_bstart < 0);
7855 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
7856 +       orig_h.inode = orig_h.dentry->d_inode;
7857 +       orig_h.mode = 0;
7858 +       if (orig_h.inode)
7859 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
7860 +       memset(&tmp_h, 0, sizeof(tmp_h));
7861 +       if (tmp->di_bstart >= 0) {
7862 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
7863 +               tmp_h.inode = tmp_h.dentry->d_inode;
7864 +               if (tmp_h.inode)
7865 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
7866 +       }
7867 +
7868 +       inode = dentry->d_inode;
7869 +       if (!orig_h.inode) {
7870 +               AuDbg("nagative originally\n");
7871 +               if (inode) {
7872 +                       au_hide(dentry);
7873 +                       goto out;
7874 +               }
7875 +               AuDebugOn(inode);
7876 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
7877 +               AuDebugOn(dinfo->di_bdiropq != -1);
7878 +
7879 +               if (!tmp_h.inode) {
7880 +                       AuDbg("negative --> negative\n");
7881 +                       /* should have only one negative lower */
7882 +                       if (tmp->di_bstart >= 0
7883 +                           && tmp->di_bstart < dinfo->di_bstart) {
7884 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
7885 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
7886 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
7887 +                               au_di_cp(dinfo, tmp);
7888 +                               hd = tmp->di_hdentry + tmp->di_bstart;
7889 +                               au_set_h_dptr(dentry, tmp->di_bstart,
7890 +                                             dget(hd->hd_dentry));
7891 +                       }
7892 +                       au_dbg_verify_dinode(dentry);
7893 +               } else {
7894 +                       AuDbg("negative --> positive\n");
7895 +                       /*
7896 +                        * similar to the behaviour of creating with bypassing
7897 +                        * aufs.
7898 +                        * unhash it in order to force an error in the
7899 +                        * succeeding create operation.
7900 +                        * we should not set S_DEAD here.
7901 +                        */
7902 +                       d_drop(dentry);
7903 +                       /* au_di_swap(tmp, dinfo); */
7904 +                       au_dbg_verify_dinode(dentry);
7905 +               }
7906 +       } else {
7907 +               AuDbg("positive originally\n");
7908 +               /* inode may be NULL */
7909 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
7910 +               if (!tmp_h.inode) {
7911 +                       AuDbg("positive --> negative\n");
7912 +                       /* or bypassing aufs */
7913 +                       au_hide(dentry);
7914 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
7915 +                               dinfo->di_bwh = tmp->di_bwh;
7916 +                       if (inode)
7917 +                               err = au_refresh_hinode_self(inode);
7918 +                       au_dbg_verify_dinode(dentry);
7919 +               } else if (orig_h.mode == tmp_h.mode) {
7920 +                       AuDbg("positive --> positive, same type\n");
7921 +                       if (!S_ISDIR(orig_h.mode)
7922 +                           && dinfo->di_bstart > tmp->di_bstart) {
7923 +                               /*
7924 +                                * similar to the behaviour of removing and
7925 +                                * creating.
7926 +                                */
7927 +                               au_hide(dentry);
7928 +                               if (inode)
7929 +                                       err = au_refresh_hinode_self(inode);
7930 +                               au_dbg_verify_dinode(dentry);
7931 +                       } else {
7932 +                               /* fill empty slots */
7933 +                               if (dinfo->di_bstart > tmp->di_bstart)
7934 +                                       dinfo->di_bstart = tmp->di_bstart;
7935 +                               if (dinfo->di_bend < tmp->di_bend)
7936 +                                       dinfo->di_bend = tmp->di_bend;
7937 +                               dinfo->di_bwh = tmp->di_bwh;
7938 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
7939 +                               hd = tmp->di_hdentry;
7940 +                               bend = dinfo->di_bend;
7941 +                               for (bindex = tmp->di_bstart; bindex <= bend;
7942 +                                    bindex++) {
7943 +                                       if (au_h_dptr(dentry, bindex))
7944 +                                               continue;
7945 +                                       h_dentry = hd[bindex].hd_dentry;
7946 +                                       if (!h_dentry)
7947 +                                               continue;
7948 +                                       h_inode = h_dentry->d_inode;
7949 +                                       AuDebugOn(!h_inode);
7950 +                                       AuDebugOn(orig_h.mode
7951 +                                                 != (h_inode->i_mode
7952 +                                                     & S_IFMT));
7953 +                                       au_set_h_dptr(dentry, bindex,
7954 +                                                     dget(h_dentry));
7955 +                               }
7956 +                               err = au_refresh_hinode(inode, dentry);
7957 +                               au_dbg_verify_dinode(dentry);
7958 +                       }
7959 +               } else {
7960 +                       AuDbg("positive --> positive, different type\n");
7961 +                       /* similar to the behaviour of removing and creating */
7962 +                       au_hide(dentry);
7963 +                       if (inode)
7964 +                               err = au_refresh_hinode_self(inode);
7965 +                       au_dbg_verify_dinode(dentry);
7966 +               }
7967 +       }
7968 +
7969 +out:
7970 +       return err;
7971 +}
7972 +
7973 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
7974 +{
7975 +       int err, ebrange;
7976 +       unsigned int sigen;
7977 +       struct au_dinfo *dinfo, *tmp;
7978 +       struct super_block *sb;
7979 +       struct inode *inode;
7980 +
7981 +       DiMustWriteLock(dentry);
7982 +       AuDebugOn(IS_ROOT(dentry));
7983 +       AuDebugOn(!parent->d_inode);
7984 +
7985 +       sb = dentry->d_sb;
7986 +       inode = dentry->d_inode;
7987 +       sigen = au_sigen(sb);
7988 +       err = au_digen_test(parent, sigen);
7989 +       if (unlikely(err))
7990 +               goto out;
7991 +
7992 +       dinfo = au_di(dentry);
7993 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
7994 +       if (unlikely(err))
7995 +               goto out;
7996 +       ebrange = au_dbrange_test(dentry);
7997 +       if (!ebrange)
7998 +               ebrange = au_do_refresh_hdentry(dentry, parent);
7999 +
8000 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8001 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
8002 +               if (inode)
8003 +                       err = au_refresh_hinode_self(inode);
8004 +               au_dbg_verify_dinode(dentry);
8005 +               if (!err)
8006 +                       goto out_dgen; /* success */
8007 +               goto out;
8008 +       }
8009 +
8010 +       /* temporary dinfo */
8011 +       AuDbgDentry(dentry);
8012 +       err = -ENOMEM;
8013 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8014 +       if (unlikely(!tmp))
8015 +               goto out;
8016 +       au_di_swap(tmp, dinfo);
8017 +       /* returns the number of positive dentries */
8018 +       /*
8019 +        * if current working dir is removed, it returns an error.
8020 +        * but the dentry is legal.
8021 +        */
8022 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
8023 +       AuDbgDentry(dentry);
8024 +       au_di_swap(tmp, dinfo);
8025 +       if (err == -ENOENT)
8026 +               err = 0;
8027 +       if (err >= 0) {
8028 +               /* compare/refresh by dinfo */
8029 +               AuDbgDentry(dentry);
8030 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8031 +               au_dbg_verify_dinode(dentry);
8032 +               AuTraceErr(err);
8033 +       }
8034 +       au_rw_write_unlock(&tmp->di_rwsem);
8035 +       au_di_free(tmp);
8036 +       if (unlikely(err))
8037 +               goto out;
8038 +
8039 +out_dgen:
8040 +       au_update_digen(dentry);
8041 +out:
8042 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8043 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8044 +               AuDbgDentry(dentry);
8045 +       }
8046 +       AuTraceErr(err);
8047 +       return err;
8048 +}
8049 +
8050 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8051 +                          struct dentry *dentry, aufs_bindex_t bindex)
8052 +{
8053 +       int err, valid;
8054 +
8055 +       err = 0;
8056 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8057 +               goto out;
8058 +
8059 +       AuDbg("b%d\n", bindex);
8060 +       /*
8061 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8062 +        * due to whiteout and branch permission.
8063 +        */
8064 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8065 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8066 +       /* it may return tri-state */
8067 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8068 +
8069 +       if (unlikely(valid < 0))
8070 +               err = valid;
8071 +       else if (!valid)
8072 +               err = -EINVAL;
8073 +
8074 +out:
8075 +       AuTraceErr(err);
8076 +       return err;
8077 +}
8078 +
8079 +/* todo: remove this */
8080 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8081 +                         unsigned int flags, int do_udba)
8082 +{
8083 +       int err;
8084 +       umode_t mode, h_mode;
8085 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
8086 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8087 +       struct inode *h_inode, *h_cached_inode;
8088 +       struct dentry *h_dentry;
8089 +       struct qstr *name, *h_name;
8090 +
8091 +       err = 0;
8092 +       plus = 0;
8093 +       mode = 0;
8094 +       ibs = -1;
8095 +       ibe = -1;
8096 +       unhashed = !!d_unhashed(dentry);
8097 +       is_root = !!IS_ROOT(dentry);
8098 +       name = &dentry->d_name;
8099 +       tmpfile = au_di(dentry)->di_tmpfile;
8100 +
8101 +       /*
8102 +        * Theoretically, REVAL test should be unnecessary in case of
8103 +        * {FS,I}NOTIFY.
8104 +        * But {fs,i}notify doesn't fire some necessary events,
8105 +        *      IN_ATTRIB for atime/nlink/pageio
8106 +        * Let's do REVAL test too.
8107 +        */
8108 +       if (do_udba && inode) {
8109 +               mode = (inode->i_mode & S_IFMT);
8110 +               plus = (inode->i_nlink > 0);
8111 +               ibs = au_ibstart(inode);
8112 +               ibe = au_ibend(inode);
8113 +       }
8114 +
8115 +       bstart = au_dbstart(dentry);
8116 +       btail = bstart;
8117 +       if (inode && S_ISDIR(inode->i_mode))
8118 +               btail = au_dbtaildir(dentry);
8119 +       for (bindex = bstart; bindex <= btail; bindex++) {
8120 +               h_dentry = au_h_dptr(dentry, bindex);
8121 +               if (!h_dentry)
8122 +                       continue;
8123 +
8124 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8125 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8126 +               spin_lock(&h_dentry->d_lock);
8127 +               h_name = &h_dentry->d_name;
8128 +               if (unlikely(do_udba
8129 +                            && !is_root
8130 +                            && ((!h_nfs
8131 +                                 && (unhashed != !!d_unhashed(h_dentry)
8132 +                                     || (!tmpfile
8133 +                                         && !au_qstreq(name, h_name))
8134 +                                         ))
8135 +                                || (h_nfs
8136 +                                    && !(flags & LOOKUP_OPEN)
8137 +                                    && (h_dentry->d_flags
8138 +                                        & DCACHE_NFSFS_RENAMED)))
8139 +                           )) {
8140 +                       int h_unhashed;
8141 +
8142 +                       h_unhashed = d_unhashed(h_dentry);
8143 +                       spin_unlock(&h_dentry->d_lock);
8144 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8145 +                             unhashed, h_unhashed, dentry, h_dentry);
8146 +                       goto err;
8147 +               }
8148 +               spin_unlock(&h_dentry->d_lock);
8149 +
8150 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8151 +               if (unlikely(err))
8152 +                       /* do not goto err, to keep the errno */
8153 +                       break;
8154 +
8155 +               /* todo: plink too? */
8156 +               if (!do_udba)
8157 +                       continue;
8158 +
8159 +               /* UDBA tests */
8160 +               h_inode = h_dentry->d_inode;
8161 +               if (unlikely(!!inode != !!h_inode))
8162 +                       goto err;
8163 +
8164 +               h_plus = plus;
8165 +               h_mode = mode;
8166 +               h_cached_inode = h_inode;
8167 +               if (h_inode) {
8168 +                       h_mode = (h_inode->i_mode & S_IFMT);
8169 +                       h_plus = (h_inode->i_nlink > 0);
8170 +               }
8171 +               if (inode && ibs <= bindex && bindex <= ibe)
8172 +                       h_cached_inode = au_h_iptr(inode, bindex);
8173 +
8174 +               if (!h_nfs) {
8175 +                       if (unlikely(plus != h_plus && !tmpfile))
8176 +                               goto err;
8177 +               } else {
8178 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8179 +                                    && !is_root
8180 +                                    && !IS_ROOT(h_dentry)
8181 +                                    && unhashed != d_unhashed(h_dentry)))
8182 +                               goto err;
8183 +               }
8184 +               if (unlikely(mode != h_mode
8185 +                            || h_cached_inode != h_inode))
8186 +                       goto err;
8187 +               continue;
8188 +
8189 +err:
8190 +               err = -EINVAL;
8191 +               break;
8192 +       }
8193 +
8194 +       AuTraceErr(err);
8195 +       return err;
8196 +}
8197 +
8198 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8199 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8200 +{
8201 +       int err;
8202 +       struct dentry *parent;
8203 +
8204 +       if (!au_digen_test(dentry, sigen))
8205 +               return 0;
8206 +
8207 +       parent = dget_parent(dentry);
8208 +       di_read_lock_parent(parent, AuLock_IR);
8209 +       AuDebugOn(au_digen_test(parent, sigen));
8210 +       au_dbg_verify_gen(parent, sigen);
8211 +       err = au_refresh_dentry(dentry, parent);
8212 +       di_read_unlock(parent, AuLock_IR);
8213 +       dput(parent);
8214 +       AuTraceErr(err);
8215 +       return err;
8216 +}
8217 +
8218 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8219 +{
8220 +       int err;
8221 +       struct dentry *d, *parent;
8222 +       struct inode *inode;
8223 +
8224 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8225 +               return simple_reval_dpath(dentry, sigen);
8226 +
8227 +       /* slow loop, keep it simple and stupid */
8228 +       /* cf: au_cpup_dirs() */
8229 +       err = 0;
8230 +       parent = NULL;
8231 +       while (au_digen_test(dentry, sigen)) {
8232 +               d = dentry;
8233 +               while (1) {
8234 +                       dput(parent);
8235 +                       parent = dget_parent(d);
8236 +                       if (!au_digen_test(parent, sigen))
8237 +                               break;
8238 +                       d = parent;
8239 +               }
8240 +
8241 +               inode = d->d_inode;
8242 +               if (d != dentry)
8243 +                       di_write_lock_child2(d);
8244 +
8245 +               /* someone might update our dentry while we were sleeping */
8246 +               if (au_digen_test(d, sigen)) {
8247 +                       /*
8248 +                        * todo: consolidate with simple_reval_dpath(),
8249 +                        * do_refresh() and au_reval_for_attr().
8250 +                        */
8251 +                       di_read_lock_parent(parent, AuLock_IR);
8252 +                       err = au_refresh_dentry(d, parent);
8253 +                       di_read_unlock(parent, AuLock_IR);
8254 +               }
8255 +
8256 +               if (d != dentry)
8257 +                       di_write_unlock(d);
8258 +               dput(parent);
8259 +               if (unlikely(err))
8260 +                       break;
8261 +       }
8262 +
8263 +       return err;
8264 +}
8265 +
8266 +/*
8267 + * if valid returns 1, otherwise 0.
8268 + */
8269 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8270 +{
8271 +       int valid, err;
8272 +       unsigned int sigen;
8273 +       unsigned char do_udba;
8274 +       struct super_block *sb;
8275 +       struct inode *inode;
8276 +
8277 +       /* todo: support rcu-walk? */
8278 +       if (flags & LOOKUP_RCU)
8279 +               return -ECHILD;
8280 +
8281 +       valid = 0;
8282 +       if (unlikely(!au_di(dentry)))
8283 +               goto out;
8284 +
8285 +       valid = 1;
8286 +       sb = dentry->d_sb;
8287 +       /*
8288 +        * todo: very ugly
8289 +        * i_mutex of parent dir may be held,
8290 +        * but we should not return 'invalid' due to busy.
8291 +        */
8292 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8293 +       if (unlikely(err)) {
8294 +               valid = err;
8295 +               AuTraceErr(err);
8296 +               goto out;
8297 +       }
8298 +       inode = dentry->d_inode;
8299 +       if (unlikely(inode && is_bad_inode(inode))) {
8300 +               err = -EINVAL;
8301 +               AuTraceErr(err);
8302 +               goto out_dgrade;
8303 +       }
8304 +       if (unlikely(au_dbrange_test(dentry))) {
8305 +               err = -EINVAL;
8306 +               AuTraceErr(err);
8307 +               goto out_dgrade;
8308 +       }
8309 +
8310 +       sigen = au_sigen(sb);
8311 +       if (au_digen_test(dentry, sigen)) {
8312 +               AuDebugOn(IS_ROOT(dentry));
8313 +               err = au_reval_dpath(dentry, sigen);
8314 +               if (unlikely(err)) {
8315 +                       AuTraceErr(err);
8316 +                       goto out_dgrade;
8317 +               }
8318 +       }
8319 +       di_downgrade_lock(dentry, AuLock_IR);
8320 +
8321 +       err = -EINVAL;
8322 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
8323 +           && inode
8324 +           && !(inode->i_state && I_LINKABLE)
8325 +           && (IS_DEADDIR(inode) || !inode->i_nlink))
8326 +               goto out_inval;
8327 +
8328 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8329 +       if (do_udba && inode) {
8330 +               aufs_bindex_t bstart = au_ibstart(inode);
8331 +               struct inode *h_inode;
8332 +
8333 +               if (bstart >= 0) {
8334 +                       h_inode = au_h_iptr(inode, bstart);
8335 +                       if (h_inode && au_test_higen(inode, h_inode))
8336 +                               goto out_inval;
8337 +               }
8338 +       }
8339 +
8340 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
8341 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
8342 +               err = -EIO;
8343 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
8344 +                     dentry, err);
8345 +       }
8346 +       goto out_inval;
8347 +
8348 +out_dgrade:
8349 +       di_downgrade_lock(dentry, AuLock_IR);
8350 +out_inval:
8351 +       aufs_read_unlock(dentry, AuLock_IR);
8352 +       AuTraceErr(err);
8353 +       valid = !err;
8354 +out:
8355 +       if (!valid) {
8356 +               AuDbg("%pd invalid, %d\n", dentry, valid);
8357 +               d_drop(dentry);
8358 +       }
8359 +       return valid;
8360 +}
8361 +
8362 +static void aufs_d_release(struct dentry *dentry)
8363 +{
8364 +       if (au_di(dentry)) {
8365 +               au_di_fin(dentry);
8366 +               au_hn_di_reinit(dentry);
8367 +       }
8368 +}
8369 +
8370 +const struct dentry_operations aufs_dop = {
8371 +       .d_revalidate           = aufs_d_revalidate,
8372 +       .d_weak_revalidate      = aufs_d_revalidate,
8373 +       .d_release              = aufs_d_release
8374 +};
8375 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
8376 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
8377 +++ linux/fs/aufs/dentry.h      2015-04-13 15:10:20.780156788 +0200
8378 @@ -0,0 +1,233 @@
8379 +/*
8380 + * Copyright (C) 2005-2015 Junjiro R. Okajima
8381 + *
8382 + * This program, aufs is free software; you can redistribute it and/or modify
8383 + * it under the terms of the GNU General Public License as published by
8384 + * the Free Software Foundation; either version 2 of the License, or
8385 + * (at your option) any later version.
8386 + *
8387 + * This program is distributed in the hope that it will be useful,
8388 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8389 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8390 + * GNU General Public License for more details.
8391 + *
8392 + * You should have received a copy of the GNU General Public License
8393 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8394 + */
8395 +
8396 +/*
8397 + * lookup and dentry operations
8398 + */
8399 +
8400 +#ifndef __AUFS_DENTRY_H__
8401 +#define __AUFS_DENTRY_H__
8402 +
8403 +#ifdef __KERNEL__
8404 +
8405 +#include <linux/dcache.h>
8406 +#include "rwsem.h"
8407 +
8408 +struct au_hdentry {
8409 +       struct dentry           *hd_dentry;
8410 +       aufs_bindex_t           hd_id;
8411 +};
8412 +
8413 +struct au_dinfo {
8414 +       atomic_t                di_generation;
8415 +
8416 +       struct au_rwsem         di_rwsem;
8417 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
8418 +       unsigned char           di_tmpfile; /* to allow the different name */
8419 +       struct au_hdentry       *di_hdentry;
8420 +} ____cacheline_aligned_in_smp;
8421 +
8422 +/* ---------------------------------------------------------------------- */
8423 +
8424 +/* dentry.c */
8425 +extern const struct dentry_operations aufs_dop;
8426 +struct au_branch;
8427 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
8428 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8429 +               struct dentry *h_parent, struct au_branch *br);
8430 +
8431 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
8432 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
8433 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
8434 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
8435 +
8436 +/* dinfo.c */
8437 +void au_di_init_once(void *_di);
8438 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8439 +void au_di_free(struct au_dinfo *dinfo);
8440 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8441 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
8442 +int au_di_init(struct dentry *dentry);
8443 +void au_di_fin(struct dentry *dentry);
8444 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
8445 +
8446 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8447 +void di_read_unlock(struct dentry *d, int flags);
8448 +void di_downgrade_lock(struct dentry *d, int flags);
8449 +void di_write_lock(struct dentry *d, unsigned int lsc);
8450 +void di_write_unlock(struct dentry *d);
8451 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8452 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8453 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8454 +
8455 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
8456 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
8457 +aufs_bindex_t au_dbtail(struct dentry *dentry);
8458 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8459 +
8460 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8461 +                  struct dentry *h_dentry);
8462 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
8463 +int au_dbrange_test(struct dentry *dentry);
8464 +void au_update_digen(struct dentry *dentry);
8465 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
8466 +void au_update_dbstart(struct dentry *dentry);
8467 +void au_update_dbend(struct dentry *dentry);
8468 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8469 +
8470 +/* ---------------------------------------------------------------------- */
8471 +
8472 +static inline struct au_dinfo *au_di(struct dentry *dentry)
8473 +{
8474 +       return dentry->d_fsdata;
8475 +}
8476 +
8477 +/* ---------------------------------------------------------------------- */
8478 +
8479 +/* lock subclass for dinfo */
8480 +enum {
8481 +       AuLsc_DI_CHILD,         /* child first */
8482 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
8483 +       AuLsc_DI_CHILD3,        /* copyup dirs */
8484 +       AuLsc_DI_PARENT,
8485 +       AuLsc_DI_PARENT2,
8486 +       AuLsc_DI_PARENT3,
8487 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
8488 +};
8489 +
8490 +/*
8491 + * di_read_lock_child, di_write_lock_child,
8492 + * di_read_lock_child2, di_write_lock_child2,
8493 + * di_read_lock_child3, di_write_lock_child3,
8494 + * di_read_lock_parent, di_write_lock_parent,
8495 + * di_read_lock_parent2, di_write_lock_parent2,
8496 + * di_read_lock_parent3, di_write_lock_parent3,
8497 + */
8498 +#define AuReadLockFunc(name, lsc) \
8499 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
8500 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8501 +
8502 +#define AuWriteLockFunc(name, lsc) \
8503 +static inline void di_write_lock_##name(struct dentry *d) \
8504 +{ di_write_lock(d, AuLsc_DI_##lsc); }
8505 +
8506 +#define AuRWLockFuncs(name, lsc) \
8507 +       AuReadLockFunc(name, lsc) \
8508 +       AuWriteLockFunc(name, lsc)
8509 +
8510 +AuRWLockFuncs(child, CHILD);
8511 +AuRWLockFuncs(child2, CHILD2);
8512 +AuRWLockFuncs(child3, CHILD3);
8513 +AuRWLockFuncs(parent, PARENT);
8514 +AuRWLockFuncs(parent2, PARENT2);
8515 +AuRWLockFuncs(parent3, PARENT3);
8516 +
8517 +#undef AuReadLockFunc
8518 +#undef AuWriteLockFunc
8519 +#undef AuRWLockFuncs
8520 +
8521 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
8522 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
8523 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
8524 +
8525 +/* ---------------------------------------------------------------------- */
8526 +
8527 +/* todo: memory barrier? */
8528 +static inline unsigned int au_digen(struct dentry *d)
8529 +{
8530 +       return atomic_read(&au_di(d)->di_generation);
8531 +}
8532 +
8533 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
8534 +{
8535 +       hdentry->hd_dentry = NULL;
8536 +}
8537 +
8538 +static inline void au_hdput(struct au_hdentry *hd)
8539 +{
8540 +       if (hd)
8541 +               dput(hd->hd_dentry);
8542 +}
8543 +
8544 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
8545 +{
8546 +       DiMustAnyLock(dentry);
8547 +       return au_di(dentry)->di_bstart;
8548 +}
8549 +
8550 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
8551 +{
8552 +       DiMustAnyLock(dentry);
8553 +       return au_di(dentry)->di_bend;
8554 +}
8555 +
8556 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
8557 +{
8558 +       DiMustAnyLock(dentry);
8559 +       return au_di(dentry)->di_bwh;
8560 +}
8561 +
8562 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
8563 +{
8564 +       DiMustAnyLock(dentry);
8565 +       return au_di(dentry)->di_bdiropq;
8566 +}
8567 +
8568 +/* todo: hard/soft set? */
8569 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
8570 +{
8571 +       DiMustWriteLock(dentry);
8572 +       au_di(dentry)->di_bstart = bindex;
8573 +}
8574 +
8575 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
8576 +{
8577 +       DiMustWriteLock(dentry);
8578 +       au_di(dentry)->di_bend = bindex;
8579 +}
8580 +
8581 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
8582 +{
8583 +       DiMustWriteLock(dentry);
8584 +       /* dbwh can be outside of bstart - bend range */
8585 +       au_di(dentry)->di_bwh = bindex;
8586 +}
8587 +
8588 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
8589 +{
8590 +       DiMustWriteLock(dentry);
8591 +       au_di(dentry)->di_bdiropq = bindex;
8592 +}
8593 +
8594 +/* ---------------------------------------------------------------------- */
8595 +
8596 +#ifdef CONFIG_AUFS_HNOTIFY
8597 +static inline void au_digen_dec(struct dentry *d)
8598 +{
8599 +       atomic_dec(&au_di(d)->di_generation);
8600 +}
8601 +
8602 +static inline void au_hn_di_reinit(struct dentry *dentry)
8603 +{
8604 +       dentry->d_fsdata = NULL;
8605 +}
8606 +#else
8607 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
8608 +#endif /* CONFIG_AUFS_HNOTIFY */
8609 +
8610 +#endif /* __KERNEL__ */
8611 +#endif /* __AUFS_DENTRY_H__ */
8612 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
8613 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
8614 +++ linux/fs/aufs/dinfo.c       2015-04-13 15:10:20.783490201 +0200
8615 @@ -0,0 +1,544 @@
8616 +/*
8617 + * Copyright (C) 2005-2015 Junjiro R. Okajima
8618 + *
8619 + * This program, aufs is free software; you can redistribute it and/or modify
8620 + * it under the terms of the GNU General Public License as published by
8621 + * the Free Software Foundation; either version 2 of the License, or
8622 + * (at your option) any later version.
8623 + *
8624 + * This program is distributed in the hope that it will be useful,
8625 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8626 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8627 + * GNU General Public License for more details.
8628 + *
8629 + * You should have received a copy of the GNU General Public License
8630 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8631 + */
8632 +
8633 +/*
8634 + * dentry private data
8635 + */
8636 +
8637 +#include "aufs.h"
8638 +
8639 +void au_di_init_once(void *_dinfo)
8640 +{
8641 +       struct au_dinfo *dinfo = _dinfo;
8642 +       static struct lock_class_key aufs_di;
8643 +
8644 +       au_rw_init(&dinfo->di_rwsem);
8645 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
8646 +}
8647 +
8648 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
8649 +{
8650 +       struct au_dinfo *dinfo;
8651 +       int nbr, i;
8652 +
8653 +       dinfo = au_cache_alloc_dinfo();
8654 +       if (unlikely(!dinfo))
8655 +               goto out;
8656 +
8657 +       nbr = au_sbend(sb) + 1;
8658 +       if (nbr <= 0)
8659 +               nbr = 1;
8660 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
8661 +       if (dinfo->di_hdentry) {
8662 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
8663 +               dinfo->di_bstart = -1;
8664 +               dinfo->di_bend = -1;
8665 +               dinfo->di_bwh = -1;
8666 +               dinfo->di_bdiropq = -1;
8667 +               dinfo->di_tmpfile = 0;
8668 +               for (i = 0; i < nbr; i++)
8669 +                       dinfo->di_hdentry[i].hd_id = -1;
8670 +               goto out;
8671 +       }
8672 +
8673 +       au_cache_free_dinfo(dinfo);
8674 +       dinfo = NULL;
8675 +
8676 +out:
8677 +       return dinfo;
8678 +}
8679 +
8680 +void au_di_free(struct au_dinfo *dinfo)
8681 +{
8682 +       struct au_hdentry *p;
8683 +       aufs_bindex_t bend, bindex;
8684 +
8685 +       /* dentry may not be revalidated */
8686 +       bindex = dinfo->di_bstart;
8687 +       if (bindex >= 0) {
8688 +               bend = dinfo->di_bend;
8689 +               p = dinfo->di_hdentry + bindex;
8690 +               while (bindex++ <= bend)
8691 +                       au_hdput(p++);
8692 +       }
8693 +       kfree(dinfo->di_hdentry);
8694 +       au_cache_free_dinfo(dinfo);
8695 +}
8696 +
8697 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
8698 +{
8699 +       struct au_hdentry *p;
8700 +       aufs_bindex_t bi;
8701 +
8702 +       AuRwMustWriteLock(&a->di_rwsem);
8703 +       AuRwMustWriteLock(&b->di_rwsem);
8704 +
8705 +#define DiSwap(v, name)                                \
8706 +       do {                                    \
8707 +               v = a->di_##name;               \
8708 +               a->di_##name = b->di_##name;    \
8709 +               b->di_##name = v;               \
8710 +       } while (0)
8711 +
8712 +       DiSwap(p, hdentry);
8713 +       DiSwap(bi, bstart);
8714 +       DiSwap(bi, bend);
8715 +       DiSwap(bi, bwh);
8716 +       DiSwap(bi, bdiropq);
8717 +       /* smp_mb(); */
8718 +
8719 +#undef DiSwap
8720 +}
8721 +
8722 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
8723 +{
8724 +       AuRwMustWriteLock(&dst->di_rwsem);
8725 +       AuRwMustWriteLock(&src->di_rwsem);
8726 +
8727 +       dst->di_bstart = src->di_bstart;
8728 +       dst->di_bend = src->di_bend;
8729 +       dst->di_bwh = src->di_bwh;
8730 +       dst->di_bdiropq = src->di_bdiropq;
8731 +       /* smp_mb(); */
8732 +}
8733 +
8734 +int au_di_init(struct dentry *dentry)
8735 +{
8736 +       int err;
8737 +       struct super_block *sb;
8738 +       struct au_dinfo *dinfo;
8739 +
8740 +       err = 0;
8741 +       sb = dentry->d_sb;
8742 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
8743 +       if (dinfo) {
8744 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
8745 +               /* smp_mb(); */ /* atomic_set */
8746 +               dentry->d_fsdata = dinfo;
8747 +       } else
8748 +               err = -ENOMEM;
8749 +
8750 +       return err;
8751 +}
8752 +
8753 +void au_di_fin(struct dentry *dentry)
8754 +{
8755 +       struct au_dinfo *dinfo;
8756 +
8757 +       dinfo = au_di(dentry);
8758 +       AuRwDestroy(&dinfo->di_rwsem);
8759 +       au_di_free(dinfo);
8760 +}
8761 +
8762 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
8763 +{
8764 +       int err, sz;
8765 +       struct au_hdentry *hdp;
8766 +
8767 +       AuRwMustWriteLock(&dinfo->di_rwsem);
8768 +
8769 +       err = -ENOMEM;
8770 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
8771 +       if (!sz)
8772 +               sz = sizeof(*hdp);
8773 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
8774 +       if (hdp) {
8775 +               dinfo->di_hdentry = hdp;
8776 +               err = 0;
8777 +       }
8778 +
8779 +       return err;
8780 +}
8781 +
8782 +/* ---------------------------------------------------------------------- */
8783 +
8784 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
8785 +{
8786 +       switch (lsc) {
8787 +       case AuLsc_DI_CHILD:
8788 +               ii_write_lock_child(inode);
8789 +               break;
8790 +       case AuLsc_DI_CHILD2:
8791 +               ii_write_lock_child2(inode);
8792 +               break;
8793 +       case AuLsc_DI_CHILD3:
8794 +               ii_write_lock_child3(inode);
8795 +               break;
8796 +       case AuLsc_DI_PARENT:
8797 +               ii_write_lock_parent(inode);
8798 +               break;
8799 +       case AuLsc_DI_PARENT2:
8800 +               ii_write_lock_parent2(inode);
8801 +               break;
8802 +       case AuLsc_DI_PARENT3:
8803 +               ii_write_lock_parent3(inode);
8804 +               break;
8805 +       default:
8806 +               BUG();
8807 +       }
8808 +}
8809 +
8810 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
8811 +{
8812 +       switch (lsc) {
8813 +       case AuLsc_DI_CHILD:
8814 +               ii_read_lock_child(inode);
8815 +               break;
8816 +       case AuLsc_DI_CHILD2:
8817 +               ii_read_lock_child2(inode);
8818 +               break;
8819 +       case AuLsc_DI_CHILD3:
8820 +               ii_read_lock_child3(inode);
8821 +               break;
8822 +       case AuLsc_DI_PARENT:
8823 +               ii_read_lock_parent(inode);
8824 +               break;
8825 +       case AuLsc_DI_PARENT2:
8826 +               ii_read_lock_parent2(inode);
8827 +               break;
8828 +       case AuLsc_DI_PARENT3:
8829 +               ii_read_lock_parent3(inode);
8830 +               break;
8831 +       default:
8832 +               BUG();
8833 +       }
8834 +}
8835 +
8836 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
8837 +{
8838 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
8839 +       if (d->d_inode) {
8840 +               if (au_ftest_lock(flags, IW))
8841 +                       do_ii_write_lock(d->d_inode, lsc);
8842 +               else if (au_ftest_lock(flags, IR))
8843 +                       do_ii_read_lock(d->d_inode, lsc);
8844 +       }
8845 +}
8846 +
8847 +void di_read_unlock(struct dentry *d, int flags)
8848 +{
8849 +       if (d->d_inode) {
8850 +               if (au_ftest_lock(flags, IW)) {
8851 +                       au_dbg_verify_dinode(d);
8852 +                       ii_write_unlock(d->d_inode);
8853 +               } else if (au_ftest_lock(flags, IR)) {
8854 +                       au_dbg_verify_dinode(d);
8855 +                       ii_read_unlock(d->d_inode);
8856 +               }
8857 +       }
8858 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
8859 +}
8860 +
8861 +void di_downgrade_lock(struct dentry *d, int flags)
8862 +{
8863 +       if (d->d_inode && au_ftest_lock(flags, IR))
8864 +               ii_downgrade_lock(d->d_inode);
8865 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
8866 +}
8867 +
8868 +void di_write_lock(struct dentry *d, unsigned int lsc)
8869 +{
8870 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
8871 +       if (d->d_inode)
8872 +               do_ii_write_lock(d->d_inode, lsc);
8873 +}
8874 +
8875 +void di_write_unlock(struct dentry *d)
8876 +{
8877 +       au_dbg_verify_dinode(d);
8878 +       if (d->d_inode)
8879 +               ii_write_unlock(d->d_inode);
8880 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
8881 +}
8882 +
8883 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
8884 +{
8885 +       AuDebugOn(d1 == d2
8886 +                 || d1->d_inode == d2->d_inode
8887 +                 || d1->d_sb != d2->d_sb);
8888 +
8889 +       if (isdir && au_test_subdir(d1, d2)) {
8890 +               di_write_lock_child(d1);
8891 +               di_write_lock_child2(d2);
8892 +       } else {
8893 +               /* there should be no races */
8894 +               di_write_lock_child(d2);
8895 +               di_write_lock_child2(d1);
8896 +       }
8897 +}
8898 +
8899 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
8900 +{
8901 +       AuDebugOn(d1 == d2
8902 +                 || d1->d_inode == d2->d_inode
8903 +                 || d1->d_sb != d2->d_sb);
8904 +
8905 +       if (isdir && au_test_subdir(d1, d2)) {
8906 +               di_write_lock_parent(d1);
8907 +               di_write_lock_parent2(d2);
8908 +       } else {
8909 +               /* there should be no races */
8910 +               di_write_lock_parent(d2);
8911 +               di_write_lock_parent2(d1);
8912 +       }
8913 +}
8914 +
8915 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
8916 +{
8917 +       di_write_unlock(d1);
8918 +       if (d1->d_inode == d2->d_inode)
8919 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
8920 +       else
8921 +               di_write_unlock(d2);
8922 +}
8923 +
8924 +/* ---------------------------------------------------------------------- */
8925 +
8926 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
8927 +{
8928 +       struct dentry *d;
8929 +
8930 +       DiMustAnyLock(dentry);
8931 +
8932 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
8933 +               return NULL;
8934 +       AuDebugOn(bindex < 0);
8935 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
8936 +       AuDebugOn(d && au_dcount(d) <= 0);
8937 +       return d;
8938 +}
8939 +
8940 +/*
8941 + * extended version of au_h_dptr().
8942 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
8943 + * error.
8944 + */
8945 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
8946 +{
8947 +       struct dentry *h_dentry;
8948 +       struct inode *inode, *h_inode;
8949 +
8950 +       inode = dentry->d_inode;
8951 +       AuDebugOn(!inode);
8952 +
8953 +       h_dentry = NULL;
8954 +       if (au_dbstart(dentry) <= bindex
8955 +           && bindex <= au_dbend(dentry))
8956 +               h_dentry = au_h_dptr(dentry, bindex);
8957 +       if (h_dentry && !au_d_linkable(h_dentry)) {
8958 +               dget(h_dentry);
8959 +               goto out; /* success */
8960 +       }
8961 +
8962 +       AuDebugOn(bindex < au_ibstart(inode));
8963 +       AuDebugOn(au_ibend(inode) < bindex);
8964 +       h_inode = au_h_iptr(inode, bindex);
8965 +       h_dentry = d_find_alias(h_inode);
8966 +       if (h_dentry) {
8967 +               if (!IS_ERR(h_dentry)) {
8968 +                       if (!au_d_linkable(h_dentry))
8969 +                               goto out; /* success */
8970 +                       dput(h_dentry);
8971 +               } else
8972 +                       goto out;
8973 +       }
8974 +
8975 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
8976 +               h_dentry = au_plink_lkup(inode, bindex);
8977 +               AuDebugOn(!h_dentry);
8978 +               if (!IS_ERR(h_dentry)) {
8979 +                       if (!au_d_hashed_positive(h_dentry))
8980 +                               goto out; /* success */
8981 +                       dput(h_dentry);
8982 +                       h_dentry = NULL;
8983 +               }
8984 +       }
8985 +
8986 +out:
8987 +       AuDbgDentry(h_dentry);
8988 +       return h_dentry;
8989 +}
8990 +
8991 +aufs_bindex_t au_dbtail(struct dentry *dentry)
8992 +{
8993 +       aufs_bindex_t bend, bwh;
8994 +
8995 +       bend = au_dbend(dentry);
8996 +       if (0 <= bend) {
8997 +               bwh = au_dbwh(dentry);
8998 +               if (!bwh)
8999 +                       return bwh;
9000 +               if (0 < bwh && bwh < bend)
9001 +                       return bwh - 1;
9002 +       }
9003 +       return bend;
9004 +}
9005 +
9006 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9007 +{
9008 +       aufs_bindex_t bend, bopq;
9009 +
9010 +       bend = au_dbtail(dentry);
9011 +       if (0 <= bend) {
9012 +               bopq = au_dbdiropq(dentry);
9013 +               if (0 <= bopq && bopq < bend)
9014 +                       bend = bopq;
9015 +       }
9016 +       return bend;
9017 +}
9018 +
9019 +/* ---------------------------------------------------------------------- */
9020 +
9021 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9022 +                  struct dentry *h_dentry)
9023 +{
9024 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
9025 +       struct au_branch *br;
9026 +
9027 +       DiMustWriteLock(dentry);
9028 +
9029 +       au_hdput(hd);
9030 +       hd->hd_dentry = h_dentry;
9031 +       if (h_dentry) {
9032 +               br = au_sbr(dentry->d_sb, bindex);
9033 +               hd->hd_id = br->br_id;
9034 +       }
9035 +}
9036 +
9037 +int au_dbrange_test(struct dentry *dentry)
9038 +{
9039 +       int err;
9040 +       aufs_bindex_t bstart, bend;
9041 +
9042 +       err = 0;
9043 +       bstart = au_dbstart(dentry);
9044 +       bend = au_dbend(dentry);
9045 +       if (bstart >= 0)
9046 +               AuDebugOn(bend < 0 && bstart > bend);
9047 +       else {
9048 +               err = -EIO;
9049 +               AuDebugOn(bend >= 0);
9050 +       }
9051 +
9052 +       return err;
9053 +}
9054 +
9055 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9056 +{
9057 +       int err;
9058 +
9059 +       err = 0;
9060 +       if (unlikely(au_digen(dentry) != sigen
9061 +                    || au_iigen_test(dentry->d_inode, sigen)))
9062 +               err = -EIO;
9063 +
9064 +       return err;
9065 +}
9066 +
9067 +void au_update_digen(struct dentry *dentry)
9068 +{
9069 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9070 +       /* smp_mb(); */ /* atomic_set */
9071 +}
9072 +
9073 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9074 +{
9075 +       struct au_dinfo *dinfo;
9076 +       struct dentry *h_d;
9077 +       struct au_hdentry *hdp;
9078 +
9079 +       DiMustWriteLock(dentry);
9080 +
9081 +       dinfo = au_di(dentry);
9082 +       if (!dinfo || dinfo->di_bstart < 0)
9083 +               return;
9084 +
9085 +       hdp = dinfo->di_hdentry;
9086 +       if (do_put_zero) {
9087 +               aufs_bindex_t bindex, bend;
9088 +
9089 +               bend = dinfo->di_bend;
9090 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
9091 +                       h_d = hdp[0 + bindex].hd_dentry;
9092 +                       if (h_d && !h_d->d_inode)
9093 +                               au_set_h_dptr(dentry, bindex, NULL);
9094 +               }
9095 +       }
9096 +
9097 +       dinfo->di_bstart = -1;
9098 +       while (++dinfo->di_bstart <= dinfo->di_bend)
9099 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
9100 +                       break;
9101 +       if (dinfo->di_bstart > dinfo->di_bend) {
9102 +               dinfo->di_bstart = -1;
9103 +               dinfo->di_bend = -1;
9104 +               return;
9105 +       }
9106 +
9107 +       dinfo->di_bend++;
9108 +       while (0 <= --dinfo->di_bend)
9109 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
9110 +                       break;
9111 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
9112 +}
9113 +
9114 +void au_update_dbstart(struct dentry *dentry)
9115 +{
9116 +       aufs_bindex_t bindex, bend;
9117 +       struct dentry *h_dentry;
9118 +
9119 +       bend = au_dbend(dentry);
9120 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
9121 +               h_dentry = au_h_dptr(dentry, bindex);
9122 +               if (!h_dentry)
9123 +                       continue;
9124 +               if (h_dentry->d_inode) {
9125 +                       au_set_dbstart(dentry, bindex);
9126 +                       return;
9127 +               }
9128 +               au_set_h_dptr(dentry, bindex, NULL);
9129 +       }
9130 +}
9131 +
9132 +void au_update_dbend(struct dentry *dentry)
9133 +{
9134 +       aufs_bindex_t bindex, bstart;
9135 +       struct dentry *h_dentry;
9136 +
9137 +       bstart = au_dbstart(dentry);
9138 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
9139 +               h_dentry = au_h_dptr(dentry, bindex);
9140 +               if (!h_dentry)
9141 +                       continue;
9142 +               if (h_dentry->d_inode) {
9143 +                       au_set_dbend(dentry, bindex);
9144 +                       return;
9145 +               }
9146 +               au_set_h_dptr(dentry, bindex, NULL);
9147 +       }
9148 +}
9149 +
9150 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9151 +{
9152 +       aufs_bindex_t bindex, bend;
9153 +
9154 +       bend = au_dbend(dentry);
9155 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
9156 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9157 +                       return bindex;
9158 +       return -1;
9159 +}
9160 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9161 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9162 +++ linux/fs/aufs/dir.c 2015-04-13 15:10:20.783490201 +0200
9163 @@ -0,0 +1,643 @@
9164 +/*
9165 + * Copyright (C) 2005-2015 Junjiro R. Okajima
9166 + *
9167 + * This program, aufs is free software; you can redistribute it and/or modify
9168 + * it under the terms of the GNU General Public License as published by
9169 + * the Free Software Foundation; either version 2 of the License, or
9170 + * (at your option) any later version.
9171 + *
9172 + * This program is distributed in the hope that it will be useful,
9173 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9174 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9175 + * GNU General Public License for more details.
9176 + *
9177 + * You should have received a copy of the GNU General Public License
9178 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9179 + */
9180 +
9181 +/*
9182 + * directory operations
9183 + */
9184 +
9185 +#include <linux/fs_stack.h>
9186 +#include "aufs.h"
9187 +
9188 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9189 +{
9190 +       unsigned int nlink;
9191 +
9192 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9193 +
9194 +       nlink = dir->i_nlink;
9195 +       nlink += h_dir->i_nlink - 2;
9196 +       if (h_dir->i_nlink < 2)
9197 +               nlink += 2;
9198 +       smp_mb(); /* for i_nlink */
9199 +       /* 0 can happen in revaliding */
9200 +       set_nlink(dir, nlink);
9201 +}
9202 +
9203 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9204 +{
9205 +       unsigned int nlink;
9206 +
9207 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9208 +
9209 +       nlink = dir->i_nlink;
9210 +       nlink -= h_dir->i_nlink - 2;
9211 +       if (h_dir->i_nlink < 2)
9212 +               nlink -= 2;
9213 +       smp_mb(); /* for i_nlink */
9214 +       /* nlink == 0 means the branch-fs is broken */
9215 +       set_nlink(dir, nlink);
9216 +}
9217 +
9218 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
9219 +{
9220 +       loff_t sz;
9221 +       aufs_bindex_t bindex, bend;
9222 +       struct file *h_file;
9223 +       struct dentry *h_dentry;
9224 +
9225 +       sz = 0;
9226 +       if (file) {
9227 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
9228 +
9229 +               bend = au_fbend_dir(file);
9230 +               for (bindex = au_fbstart(file);
9231 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9232 +                    bindex++) {
9233 +                       h_file = au_hf_dir(file, bindex);
9234 +                       if (h_file && file_inode(h_file))
9235 +                               sz += vfsub_f_size_read(h_file);
9236 +               }
9237 +       } else {
9238 +               AuDebugOn(!dentry);
9239 +               AuDebugOn(!d_is_dir(dentry));
9240 +
9241 +               bend = au_dbtaildir(dentry);
9242 +               for (bindex = au_dbstart(dentry);
9243 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9244 +                    bindex++) {
9245 +                       h_dentry = au_h_dptr(dentry, bindex);
9246 +                       if (h_dentry && h_dentry->d_inode)
9247 +                               sz += i_size_read(h_dentry->d_inode);
9248 +               }
9249 +       }
9250 +       if (sz < KMALLOC_MAX_SIZE)
9251 +               sz = roundup_pow_of_two(sz);
9252 +       if (sz > KMALLOC_MAX_SIZE)
9253 +               sz = KMALLOC_MAX_SIZE;
9254 +       else if (sz < NAME_MAX) {
9255 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9256 +               sz = AUFS_RDBLK_DEF;
9257 +       }
9258 +       return sz;
9259 +}
9260 +
9261 +/* ---------------------------------------------------------------------- */
9262 +
9263 +static int reopen_dir(struct file *file)
9264 +{
9265 +       int err;
9266 +       unsigned int flags;
9267 +       aufs_bindex_t bindex, btail, bstart;
9268 +       struct dentry *dentry, *h_dentry;
9269 +       struct file *h_file;
9270 +
9271 +       /* open all lower dirs */
9272 +       dentry = file->f_path.dentry;
9273 +       bstart = au_dbstart(dentry);
9274 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
9275 +               au_set_h_fptr(file, bindex, NULL);
9276 +       au_set_fbstart(file, bstart);
9277 +
9278 +       btail = au_dbtaildir(dentry);
9279 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
9280 +               au_set_h_fptr(file, bindex, NULL);
9281 +       au_set_fbend_dir(file, btail);
9282 +
9283 +       flags = vfsub_file_flags(file);
9284 +       for (bindex = bstart; bindex <= btail; bindex++) {
9285 +               h_dentry = au_h_dptr(dentry, bindex);
9286 +               if (!h_dentry)
9287 +                       continue;
9288 +               h_file = au_hf_dir(file, bindex);
9289 +               if (h_file)
9290 +                       continue;
9291 +
9292 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9293 +               err = PTR_ERR(h_file);
9294 +               if (IS_ERR(h_file))
9295 +                       goto out; /* close all? */
9296 +               au_set_h_fptr(file, bindex, h_file);
9297 +       }
9298 +       au_update_figen(file);
9299 +       /* todo: necessary? */
9300 +       /* file->f_ra = h_file->f_ra; */
9301 +       err = 0;
9302 +
9303 +out:
9304 +       return err;
9305 +}
9306 +
9307 +static int do_open_dir(struct file *file, int flags)
9308 +{
9309 +       int err;
9310 +       aufs_bindex_t bindex, btail;
9311 +       struct dentry *dentry, *h_dentry;
9312 +       struct file *h_file;
9313 +
9314 +       FiMustWriteLock(file);
9315 +
9316 +       err = 0;
9317 +       dentry = file->f_path.dentry;
9318 +       file->f_version = dentry->d_inode->i_version;
9319 +       bindex = au_dbstart(dentry);
9320 +       au_set_fbstart(file, bindex);
9321 +       btail = au_dbtaildir(dentry);
9322 +       au_set_fbend_dir(file, btail);
9323 +       for (; !err && bindex <= btail; bindex++) {
9324 +               h_dentry = au_h_dptr(dentry, bindex);
9325 +               if (!h_dentry)
9326 +                       continue;
9327 +
9328 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9329 +               if (IS_ERR(h_file)) {
9330 +                       err = PTR_ERR(h_file);
9331 +                       break;
9332 +               }
9333 +               au_set_h_fptr(file, bindex, h_file);
9334 +       }
9335 +       au_update_figen(file);
9336 +       /* todo: necessary? */
9337 +       /* file->f_ra = h_file->f_ra; */
9338 +       if (!err)
9339 +               return 0; /* success */
9340 +
9341 +       /* close all */
9342 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
9343 +               au_set_h_fptr(file, bindex, NULL);
9344 +       au_set_fbstart(file, -1);
9345 +       au_set_fbend_dir(file, -1);
9346 +
9347 +       return err;
9348 +}
9349 +
9350 +static int aufs_open_dir(struct inode *inode __maybe_unused,
9351 +                        struct file *file)
9352 +{
9353 +       int err;
9354 +       struct super_block *sb;
9355 +       struct au_fidir *fidir;
9356 +
9357 +       err = -ENOMEM;
9358 +       sb = file->f_path.dentry->d_sb;
9359 +       si_read_lock(sb, AuLock_FLUSH);
9360 +       fidir = au_fidir_alloc(sb);
9361 +       if (fidir) {
9362 +               err = au_do_open(file, do_open_dir, fidir);
9363 +               if (unlikely(err))
9364 +                       kfree(fidir);
9365 +       }
9366 +       si_read_unlock(sb);
9367 +       return err;
9368 +}
9369 +
9370 +static int aufs_release_dir(struct inode *inode __maybe_unused,
9371 +                           struct file *file)
9372 +{
9373 +       struct au_vdir *vdir_cache;
9374 +       struct au_finfo *finfo;
9375 +       struct au_fidir *fidir;
9376 +       aufs_bindex_t bindex, bend;
9377 +
9378 +       finfo = au_fi(file);
9379 +       fidir = finfo->fi_hdir;
9380 +       if (fidir) {
9381 +               au_sphl_del(&finfo->fi_hlist,
9382 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
9383 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
9384 +               if (vdir_cache)
9385 +                       au_vdir_free(vdir_cache);
9386 +
9387 +               bindex = finfo->fi_btop;
9388 +               if (bindex >= 0) {
9389 +                       /*
9390 +                        * calls fput() instead of filp_close(),
9391 +                        * since no dnotify or lock for the lower file.
9392 +                        */
9393 +                       bend = fidir->fd_bbot;
9394 +                       for (; bindex <= bend; bindex++)
9395 +                               au_set_h_fptr(file, bindex, NULL);
9396 +               }
9397 +               kfree(fidir);
9398 +               finfo->fi_hdir = NULL;
9399 +       }
9400 +       au_finfo_fin(file);
9401 +       return 0;
9402 +}
9403 +
9404 +/* ---------------------------------------------------------------------- */
9405 +
9406 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
9407 +{
9408 +       int err;
9409 +       aufs_bindex_t bindex, bend;
9410 +       struct file *h_file;
9411 +
9412 +       err = 0;
9413 +       bend = au_fbend_dir(file);
9414 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9415 +               h_file = au_hf_dir(file, bindex);
9416 +               if (h_file)
9417 +                       err = vfsub_flush(h_file, id);
9418 +       }
9419 +       return err;
9420 +}
9421 +
9422 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
9423 +{
9424 +       return au_do_flush(file, id, au_do_flush_dir);
9425 +}
9426 +
9427 +/* ---------------------------------------------------------------------- */
9428 +
9429 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
9430 +{
9431 +       int err;
9432 +       aufs_bindex_t bend, bindex;
9433 +       struct inode *inode;
9434 +       struct super_block *sb;
9435 +
9436 +       err = 0;
9437 +       sb = dentry->d_sb;
9438 +       inode = dentry->d_inode;
9439 +       IMustLock(inode);
9440 +       bend = au_dbend(dentry);
9441 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
9442 +               struct path h_path;
9443 +
9444 +               if (au_test_ro(sb, bindex, inode))
9445 +                       continue;
9446 +               h_path.dentry = au_h_dptr(dentry, bindex);
9447 +               if (!h_path.dentry)
9448 +                       continue;
9449 +
9450 +               h_path.mnt = au_sbr_mnt(sb, bindex);
9451 +               err = vfsub_fsync(NULL, &h_path, datasync);
9452 +       }
9453 +
9454 +       return err;
9455 +}
9456 +
9457 +static int au_do_fsync_dir(struct file *file, int datasync)
9458 +{
9459 +       int err;
9460 +       aufs_bindex_t bend, bindex;
9461 +       struct file *h_file;
9462 +       struct super_block *sb;
9463 +       struct inode *inode;
9464 +
9465 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9466 +       if (unlikely(err))
9467 +               goto out;
9468 +
9469 +       sb = file->f_path.dentry->d_sb;
9470 +       inode = file_inode(file);
9471 +       bend = au_fbend_dir(file);
9472 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9473 +               h_file = au_hf_dir(file, bindex);
9474 +               if (!h_file || au_test_ro(sb, bindex, inode))
9475 +                       continue;
9476 +
9477 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
9478 +       }
9479 +
9480 +out:
9481 +       return err;
9482 +}
9483 +
9484 +/*
9485 + * @file may be NULL
9486 + */
9487 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
9488 +                         int datasync)
9489 +{
9490 +       int err;
9491 +       struct dentry *dentry;
9492 +       struct super_block *sb;
9493 +       struct mutex *mtx;
9494 +
9495 +       err = 0;
9496 +       dentry = file->f_path.dentry;
9497 +       mtx = &dentry->d_inode->i_mutex;
9498 +       mutex_lock(mtx);
9499 +       sb = dentry->d_sb;
9500 +       si_noflush_read_lock(sb);
9501 +       if (file)
9502 +               err = au_do_fsync_dir(file, datasync);
9503 +       else {
9504 +               di_write_lock_child(dentry);
9505 +               err = au_do_fsync_dir_no_file(dentry, datasync);
9506 +       }
9507 +       au_cpup_attr_timesizes(dentry->d_inode);
9508 +       di_write_unlock(dentry);
9509 +       if (file)
9510 +               fi_write_unlock(file);
9511 +
9512 +       si_read_unlock(sb);
9513 +       mutex_unlock(mtx);
9514 +       return err;
9515 +}
9516 +
9517 +/* ---------------------------------------------------------------------- */
9518 +
9519 +static int aufs_iterate(struct file *file, struct dir_context *ctx)
9520 +{
9521 +       int err;
9522 +       struct dentry *dentry;
9523 +       struct inode *inode, *h_inode;
9524 +       struct super_block *sb;
9525 +
9526 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
9527 +
9528 +       dentry = file->f_path.dentry;
9529 +       inode = dentry->d_inode;
9530 +       IMustLock(inode);
9531 +
9532 +       sb = dentry->d_sb;
9533 +       si_read_lock(sb, AuLock_FLUSH);
9534 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9535 +       if (unlikely(err))
9536 +               goto out;
9537 +       err = au_alive_dir(dentry);
9538 +       if (!err)
9539 +               err = au_vdir_init(file);
9540 +       di_downgrade_lock(dentry, AuLock_IR);
9541 +       if (unlikely(err))
9542 +               goto out_unlock;
9543 +
9544 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
9545 +       if (!au_test_nfsd()) {
9546 +               err = au_vdir_fill_de(file, ctx);
9547 +               fsstack_copy_attr_atime(inode, h_inode);
9548 +       } else {
9549 +               /*
9550 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
9551 +                * encode_fh() and others.
9552 +                */
9553 +               atomic_inc(&h_inode->i_count);
9554 +               di_read_unlock(dentry, AuLock_IR);
9555 +               si_read_unlock(sb);
9556 +               err = au_vdir_fill_de(file, ctx);
9557 +               fsstack_copy_attr_atime(inode, h_inode);
9558 +               fi_write_unlock(file);
9559 +               iput(h_inode);
9560 +
9561 +               AuTraceErr(err);
9562 +               return err;
9563 +       }
9564 +
9565 +out_unlock:
9566 +       di_read_unlock(dentry, AuLock_IR);
9567 +       fi_write_unlock(file);
9568 +out:
9569 +       si_read_unlock(sb);
9570 +       return err;
9571 +}
9572 +
9573 +/* ---------------------------------------------------------------------- */
9574 +
9575 +#define AuTestEmpty_WHONLY     1
9576 +#define AuTestEmpty_CALLED     (1 << 1)
9577 +#define AuTestEmpty_SHWH       (1 << 2)
9578 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
9579 +#define au_fset_testempty(flags, name) \
9580 +       do { (flags) |= AuTestEmpty_##name; } while (0)
9581 +#define au_fclr_testempty(flags, name) \
9582 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
9583 +
9584 +#ifndef CONFIG_AUFS_SHWH
9585 +#undef AuTestEmpty_SHWH
9586 +#define AuTestEmpty_SHWH       0
9587 +#endif
9588 +
9589 +struct test_empty_arg {
9590 +       struct dir_context ctx;
9591 +       struct au_nhash *whlist;
9592 +       unsigned int flags;
9593 +       int err;
9594 +       aufs_bindex_t bindex;
9595 +};
9596 +
9597 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
9598 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
9599 +                        unsigned int d_type)
9600 +{
9601 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
9602 +                                                 ctx);
9603 +       char *name = (void *)__name;
9604 +
9605 +       arg->err = 0;
9606 +       au_fset_testempty(arg->flags, CALLED);
9607 +       /* smp_mb(); */
9608 +       if (name[0] == '.'
9609 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
9610 +               goto out; /* success */
9611 +
9612 +       if (namelen <= AUFS_WH_PFX_LEN
9613 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
9614 +               if (au_ftest_testempty(arg->flags, WHONLY)
9615 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
9616 +                       arg->err = -ENOTEMPTY;
9617 +               goto out;
9618 +       }
9619 +
9620 +       name += AUFS_WH_PFX_LEN;
9621 +       namelen -= AUFS_WH_PFX_LEN;
9622 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
9623 +               arg->err = au_nhash_append_wh
9624 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
9625 +                        au_ftest_testempty(arg->flags, SHWH));
9626 +
9627 +out:
9628 +       /* smp_mb(); */
9629 +       AuTraceErr(arg->err);
9630 +       return arg->err;
9631 +}
9632 +
9633 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9634 +{
9635 +       int err;
9636 +       struct file *h_file;
9637 +
9638 +       h_file = au_h_open(dentry, arg->bindex,
9639 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
9640 +                          /*file*/NULL, /*force_wr*/0);
9641 +       err = PTR_ERR(h_file);
9642 +       if (IS_ERR(h_file))
9643 +               goto out;
9644 +
9645 +       err = 0;
9646 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
9647 +           && !file_inode(h_file)->i_nlink)
9648 +               goto out_put;
9649 +
9650 +       do {
9651 +               arg->err = 0;
9652 +               au_fclr_testempty(arg->flags, CALLED);
9653 +               /* smp_mb(); */
9654 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
9655 +               if (err >= 0)
9656 +                       err = arg->err;
9657 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
9658 +
9659 +out_put:
9660 +       fput(h_file);
9661 +       au_sbr_put(dentry->d_sb, arg->bindex);
9662 +out:
9663 +       return err;
9664 +}
9665 +
9666 +struct do_test_empty_args {
9667 +       int *errp;
9668 +       struct dentry *dentry;
9669 +       struct test_empty_arg *arg;
9670 +};
9671 +
9672 +static void call_do_test_empty(void *args)
9673 +{
9674 +       struct do_test_empty_args *a = args;
9675 +       *a->errp = do_test_empty(a->dentry, a->arg);
9676 +}
9677 +
9678 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9679 +{
9680 +       int err, wkq_err;
9681 +       struct dentry *h_dentry;
9682 +       struct inode *h_inode;
9683 +
9684 +       h_dentry = au_h_dptr(dentry, arg->bindex);
9685 +       h_inode = h_dentry->d_inode;
9686 +       /* todo: i_mode changes anytime? */
9687 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
9688 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
9689 +       mutex_unlock(&h_inode->i_mutex);
9690 +       if (!err)
9691 +               err = do_test_empty(dentry, arg);
9692 +       else {
9693 +               struct do_test_empty_args args = {
9694 +                       .errp   = &err,
9695 +                       .dentry = dentry,
9696 +                       .arg    = arg
9697 +               };
9698 +               unsigned int flags = arg->flags;
9699 +
9700 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
9701 +               if (unlikely(wkq_err))
9702 +                       err = wkq_err;
9703 +               arg->flags = flags;
9704 +       }
9705 +
9706 +       return err;
9707 +}
9708 +
9709 +int au_test_empty_lower(struct dentry *dentry)
9710 +{
9711 +       int err;
9712 +       unsigned int rdhash;
9713 +       aufs_bindex_t bindex, bstart, btail;
9714 +       struct au_nhash whlist;
9715 +       struct test_empty_arg arg = {
9716 +               .ctx = {
9717 +                       .actor = test_empty_cb
9718 +               }
9719 +       };
9720 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
9721 +
9722 +       SiMustAnyLock(dentry->d_sb);
9723 +
9724 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
9725 +       if (!rdhash)
9726 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
9727 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
9728 +       if (unlikely(err))
9729 +               goto out;
9730 +
9731 +       arg.flags = 0;
9732 +       arg.whlist = &whlist;
9733 +       bstart = au_dbstart(dentry);
9734 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
9735 +               au_fset_testempty(arg.flags, SHWH);
9736 +       test_empty = do_test_empty;
9737 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
9738 +               test_empty = sio_test_empty;
9739 +       arg.bindex = bstart;
9740 +       err = test_empty(dentry, &arg);
9741 +       if (unlikely(err))
9742 +               goto out_whlist;
9743 +
9744 +       au_fset_testempty(arg.flags, WHONLY);
9745 +       btail = au_dbtaildir(dentry);
9746 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
9747 +               struct dentry *h_dentry;
9748 +
9749 +               h_dentry = au_h_dptr(dentry, bindex);
9750 +               if (h_dentry && h_dentry->d_inode) {
9751 +                       arg.bindex = bindex;
9752 +                       err = test_empty(dentry, &arg);
9753 +               }
9754 +       }
9755 +
9756 +out_whlist:
9757 +       au_nhash_wh_free(&whlist);
9758 +out:
9759 +       return err;
9760 +}
9761 +
9762 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
9763 +{
9764 +       int err;
9765 +       struct test_empty_arg arg = {
9766 +               .ctx = {
9767 +                       .actor = test_empty_cb
9768 +               }
9769 +       };
9770 +       aufs_bindex_t bindex, btail;
9771 +
9772 +       err = 0;
9773 +       arg.whlist = whlist;
9774 +       arg.flags = AuTestEmpty_WHONLY;
9775 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
9776 +               au_fset_testempty(arg.flags, SHWH);
9777 +       btail = au_dbtaildir(dentry);
9778 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
9779 +               struct dentry *h_dentry;
9780 +
9781 +               h_dentry = au_h_dptr(dentry, bindex);
9782 +               if (h_dentry && h_dentry->d_inode) {
9783 +                       arg.bindex = bindex;
9784 +                       err = sio_test_empty(dentry, &arg);
9785 +               }
9786 +       }
9787 +
9788 +       return err;
9789 +}
9790 +
9791 +/* ---------------------------------------------------------------------- */
9792 +
9793 +const struct file_operations aufs_dir_fop = {
9794 +       .owner          = THIS_MODULE,
9795 +       .llseek         = default_llseek,
9796 +       .read           = generic_read_dir,
9797 +       .iterate        = aufs_iterate,
9798 +       .unlocked_ioctl = aufs_ioctl_dir,
9799 +#ifdef CONFIG_COMPAT
9800 +       .compat_ioctl   = aufs_compat_ioctl_dir,
9801 +#endif
9802 +       .open           = aufs_open_dir,
9803 +       .release        = aufs_release_dir,
9804 +       .flush          = aufs_flush_dir,
9805 +       .fsync          = aufs_fsync_dir
9806 +};
9807 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
9808 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
9809 +++ linux/fs/aufs/dir.h 2015-04-13 15:10:20.783490201 +0200
9810 @@ -0,0 +1,130 @@
9811 +/*
9812 + * Copyright (C) 2005-2015 Junjiro R. Okajima
9813 + *
9814 + * This program, aufs is free software; you can redistribute it and/or modify
9815 + * it under the terms of the GNU General Public License as published by
9816 + * the Free Software Foundation; either version 2 of the License, or
9817 + * (at your option) any later version.
9818 + *
9819 + * This program is distributed in the hope that it will be useful,
9820 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9821 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9822 + * GNU General Public License for more details.
9823 + *
9824 + * You should have received a copy of the GNU General Public License
9825 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9826 + */
9827 +
9828 +/*
9829 + * directory operations
9830 + */
9831 +
9832 +#ifndef __AUFS_DIR_H__
9833 +#define __AUFS_DIR_H__
9834 +
9835 +#ifdef __KERNEL__
9836 +
9837 +#include <linux/fs.h>
9838 +
9839 +/* ---------------------------------------------------------------------- */
9840 +
9841 +/* need to be faster and smaller */
9842 +
9843 +struct au_nhash {
9844 +       unsigned int            nh_num;
9845 +       struct hlist_head       *nh_head;
9846 +};
9847 +
9848 +struct au_vdir_destr {
9849 +       unsigned char   len;
9850 +       unsigned char   name[0];
9851 +} __packed;
9852 +
9853 +struct au_vdir_dehstr {
9854 +       struct hlist_node       hash;
9855 +       struct au_vdir_destr    *str;
9856 +} ____cacheline_aligned_in_smp;
9857 +
9858 +struct au_vdir_de {
9859 +       ino_t                   de_ino;
9860 +       unsigned char           de_type;
9861 +       /* caution: packed */
9862 +       struct au_vdir_destr    de_str;
9863 +} __packed;
9864 +
9865 +struct au_vdir_wh {
9866 +       struct hlist_node       wh_hash;
9867 +#ifdef CONFIG_AUFS_SHWH
9868 +       ino_t                   wh_ino;
9869 +       aufs_bindex_t           wh_bindex;
9870 +       unsigned char           wh_type;
9871 +#else
9872 +       aufs_bindex_t           wh_bindex;
9873 +#endif
9874 +       /* caution: packed */
9875 +       struct au_vdir_destr    wh_str;
9876 +} __packed;
9877 +
9878 +union au_vdir_deblk_p {
9879 +       unsigned char           *deblk;
9880 +       struct au_vdir_de       *de;
9881 +};
9882 +
9883 +struct au_vdir {
9884 +       unsigned char   **vd_deblk;
9885 +       unsigned long   vd_nblk;
9886 +       struct {
9887 +               unsigned long           ul;
9888 +               union au_vdir_deblk_p   p;
9889 +       } vd_last;
9890 +
9891 +       unsigned long   vd_version;
9892 +       unsigned int    vd_deblk_sz;
9893 +       unsigned long   vd_jiffy;
9894 +} ____cacheline_aligned_in_smp;
9895 +
9896 +/* ---------------------------------------------------------------------- */
9897 +
9898 +/* dir.c */
9899 +extern const struct file_operations aufs_dir_fop;
9900 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
9901 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
9902 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
9903 +int au_test_empty_lower(struct dentry *dentry);
9904 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
9905 +
9906 +/* vdir.c */
9907 +unsigned int au_rdhash_est(loff_t sz);
9908 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
9909 +void au_nhash_wh_free(struct au_nhash *whlist);
9910 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
9911 +                           int limit);
9912 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
9913 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
9914 +                      unsigned int d_type, aufs_bindex_t bindex,
9915 +                      unsigned char shwh);
9916 +void au_vdir_free(struct au_vdir *vdir);
9917 +int au_vdir_init(struct file *file);
9918 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
9919 +
9920 +/* ioctl.c */
9921 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
9922 +
9923 +#ifdef CONFIG_AUFS_RDU
9924 +/* rdu.c */
9925 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
9926 +#ifdef CONFIG_COMPAT
9927 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
9928 +                        unsigned long arg);
9929 +#endif
9930 +#else
9931 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
9932 +       unsigned int cmd, unsigned long arg)
9933 +#ifdef CONFIG_COMPAT
9934 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
9935 +       unsigned int cmd, unsigned long arg)
9936 +#endif
9937 +#endif
9938 +
9939 +#endif /* __KERNEL__ */
9940 +#endif /* __AUFS_DIR_H__ */
9941 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
9942 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
9943 +++ linux/fs/aufs/dynop.c       2015-04-13 15:10:20.783490201 +0200
9944 @@ -0,0 +1,369 @@
9945 +/*
9946 + * Copyright (C) 2010-2015 Junjiro R. Okajima
9947 + *
9948 + * This program, aufs is free software; you can redistribute it and/or modify
9949 + * it under the terms of the GNU General Public License as published by
9950 + * the Free Software Foundation; either version 2 of the License, or
9951 + * (at your option) any later version.
9952 + *
9953 + * This program is distributed in the hope that it will be useful,
9954 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9955 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9956 + * GNU General Public License for more details.
9957 + *
9958 + * You should have received a copy of the GNU General Public License
9959 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9960 + */
9961 +
9962 +/*
9963 + * dynamically customizable operations for regular files
9964 + */
9965 +
9966 +#include "aufs.h"
9967 +
9968 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
9969 +
9970 +/*
9971 + * How large will these lists be?
9972 + * Usually just a few elements, 20-30 at most for each, I guess.
9973 + */
9974 +static struct au_splhead dynop[AuDyLast];
9975 +
9976 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
9977 +{
9978 +       struct au_dykey *key, *tmp;
9979 +       struct list_head *head;
9980 +
9981 +       key = NULL;
9982 +       head = &spl->head;
9983 +       rcu_read_lock();
9984 +       list_for_each_entry_rcu(tmp, head, dk_list)
9985 +               if (tmp->dk_op.dy_hop == h_op) {
9986 +                       key = tmp;
9987 +                       kref_get(&key->dk_kref);
9988 +                       break;
9989 +               }
9990 +       rcu_read_unlock();
9991 +
9992 +       return key;
9993 +}
9994 +
9995 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
9996 +{
9997 +       struct au_dykey **k, *found;
9998 +       const void *h_op = key->dk_op.dy_hop;
9999 +       int i;
10000 +
10001 +       found = NULL;
10002 +       k = br->br_dykey;
10003 +       for (i = 0; i < AuBrDynOp; i++)
10004 +               if (k[i]) {
10005 +                       if (k[i]->dk_op.dy_hop == h_op) {
10006 +                               found = k[i];
10007 +                               break;
10008 +                       }
10009 +               } else
10010 +                       break;
10011 +       if (!found) {
10012 +               spin_lock(&br->br_dykey_lock);
10013 +               for (; i < AuBrDynOp; i++)
10014 +                       if (k[i]) {
10015 +                               if (k[i]->dk_op.dy_hop == h_op) {
10016 +                                       found = k[i];
10017 +                                       break;
10018 +                               }
10019 +                       } else {
10020 +                               k[i] = key;
10021 +                               break;
10022 +                       }
10023 +               spin_unlock(&br->br_dykey_lock);
10024 +               BUG_ON(i == AuBrDynOp); /* expand the array */
10025 +       }
10026 +
10027 +       return found;
10028 +}
10029 +
10030 +/* kref_get() if @key is already added */
10031 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
10032 +{
10033 +       struct au_dykey *tmp, *found;
10034 +       struct list_head *head;
10035 +       const void *h_op = key->dk_op.dy_hop;
10036 +
10037 +       found = NULL;
10038 +       head = &spl->head;
10039 +       spin_lock(&spl->spin);
10040 +       list_for_each_entry(tmp, head, dk_list)
10041 +               if (tmp->dk_op.dy_hop == h_op) {
10042 +                       kref_get(&tmp->dk_kref);
10043 +                       found = tmp;
10044 +                       break;
10045 +               }
10046 +       if (!found)
10047 +               list_add_rcu(&key->dk_list, head);
10048 +       spin_unlock(&spl->spin);
10049 +
10050 +       if (!found)
10051 +               DyPrSym(key);
10052 +       return found;
10053 +}
10054 +
10055 +static void dy_free_rcu(struct rcu_head *rcu)
10056 +{
10057 +       struct au_dykey *key;
10058 +
10059 +       key = container_of(rcu, struct au_dykey, dk_rcu);
10060 +       DyPrSym(key);
10061 +       kfree(key);
10062 +}
10063 +
10064 +static void dy_free(struct kref *kref)
10065 +{
10066 +       struct au_dykey *key;
10067 +       struct au_splhead *spl;
10068 +
10069 +       key = container_of(kref, struct au_dykey, dk_kref);
10070 +       spl = dynop + key->dk_op.dy_type;
10071 +       au_spl_del_rcu(&key->dk_list, spl);
10072 +       call_rcu(&key->dk_rcu, dy_free_rcu);
10073 +}
10074 +
10075 +void au_dy_put(struct au_dykey *key)
10076 +{
10077 +       kref_put(&key->dk_kref, dy_free);
10078 +}
10079 +
10080 +/* ---------------------------------------------------------------------- */
10081 +
10082 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
10083 +
10084 +#ifdef CONFIG_AUFS_DEBUG
10085 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
10086 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
10087 +#else
10088 +#define DyDbgDeclare(cnt)      do {} while (0)
10089 +#define DyDbgInc(cnt)          do {} while (0)
10090 +#endif
10091 +
10092 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
10093 +       DyDbgInc(cnt);                                                  \
10094 +       if (h_op->func) {                                               \
10095 +               if (src.func)                                           \
10096 +                       dst.func = src.func;                            \
10097 +               else                                                    \
10098 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
10099 +       }                                                               \
10100 +} while (0)
10101 +
10102 +#define DySetForce(func, dst, src) do {                \
10103 +       AuDebugOn(!src.func);                   \
10104 +       DyDbgInc(cnt);                          \
10105 +       dst.func = src.func;                    \
10106 +} while (0)
10107 +
10108 +#define DySetAop(func) \
10109 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
10110 +#define DySetAopForce(func) \
10111 +       DySetForce(func, dyaop->da_op, aufs_aop)
10112 +
10113 +static void dy_aop(struct au_dykey *key, const void *h_op,
10114 +                  struct super_block *h_sb __maybe_unused)
10115 +{
10116 +       struct au_dyaop *dyaop = (void *)key;
10117 +       const struct address_space_operations *h_aop = h_op;
10118 +       DyDbgDeclare(cnt);
10119 +
10120 +       AuDbg("%s\n", au_sbtype(h_sb));
10121 +
10122 +       DySetAop(writepage);
10123 +       DySetAopForce(readpage);        /* force */
10124 +       DySetAop(writepages);
10125 +       DySetAop(set_page_dirty);
10126 +       DySetAop(readpages);
10127 +       DySetAop(write_begin);
10128 +       DySetAop(write_end);
10129 +       DySetAop(bmap);
10130 +       DySetAop(invalidatepage);
10131 +       DySetAop(releasepage);
10132 +       DySetAop(freepage);
10133 +       /* this one will be changed according to an aufs mount option */
10134 +       DySetAop(direct_IO);
10135 +       DySetAop(migratepage);
10136 +       DySetAop(launder_page);
10137 +       DySetAop(is_partially_uptodate);
10138 +       DySetAop(is_dirty_writeback);
10139 +       DySetAop(error_remove_page);
10140 +       DySetAop(swap_activate);
10141 +       DySetAop(swap_deactivate);
10142 +
10143 +       DyDbgSize(cnt, *h_aop);
10144 +}
10145 +
10146 +/* ---------------------------------------------------------------------- */
10147 +
10148 +static void dy_bug(struct kref *kref)
10149 +{
10150 +       BUG();
10151 +}
10152 +
10153 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
10154 +{
10155 +       struct au_dykey *key, *old;
10156 +       struct au_splhead *spl;
10157 +       struct op {
10158 +               unsigned int sz;
10159 +               void (*set)(struct au_dykey *key, const void *h_op,
10160 +                           struct super_block *h_sb __maybe_unused);
10161 +       };
10162 +       static const struct op a[] = {
10163 +               [AuDy_AOP] = {
10164 +                       .sz     = sizeof(struct au_dyaop),
10165 +                       .set    = dy_aop
10166 +               }
10167 +       };
10168 +       const struct op *p;
10169 +
10170 +       spl = dynop + op->dy_type;
10171 +       key = dy_gfind_get(spl, op->dy_hop);
10172 +       if (key)
10173 +               goto out_add; /* success */
10174 +
10175 +       p = a + op->dy_type;
10176 +       key = kzalloc(p->sz, GFP_NOFS);
10177 +       if (unlikely(!key)) {
10178 +               key = ERR_PTR(-ENOMEM);
10179 +               goto out;
10180 +       }
10181 +
10182 +       key->dk_op.dy_hop = op->dy_hop;
10183 +       kref_init(&key->dk_kref);
10184 +       p->set(key, op->dy_hop, au_br_sb(br));
10185 +       old = dy_gadd(spl, key);
10186 +       if (old) {
10187 +               kfree(key);
10188 +               key = old;
10189 +       }
10190 +
10191 +out_add:
10192 +       old = dy_bradd(br, key);
10193 +       if (old)
10194 +               /* its ref-count should never be zero here */
10195 +               kref_put(&key->dk_kref, dy_bug);
10196 +out:
10197 +       return key;
10198 +}
10199 +
10200 +/* ---------------------------------------------------------------------- */
10201 +/*
10202 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
10203 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
10204 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
10205 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
10206 + * See the aufs manual in detail.
10207 + */
10208 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
10209 +{
10210 +       if (!do_dx)
10211 +               dyaop->da_op.direct_IO = NULL;
10212 +       else
10213 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
10214 +}
10215 +
10216 +static struct au_dyaop *dy_aget(struct au_branch *br,
10217 +                               const struct address_space_operations *h_aop,
10218 +                               int do_dx)
10219 +{
10220 +       struct au_dyaop *dyaop;
10221 +       struct au_dynop op;
10222 +
10223 +       op.dy_type = AuDy_AOP;
10224 +       op.dy_haop = h_aop;
10225 +       dyaop = (void *)dy_get(&op, br);
10226 +       if (IS_ERR(dyaop))
10227 +               goto out;
10228 +       dy_adx(dyaop, do_dx);
10229 +
10230 +out:
10231 +       return dyaop;
10232 +}
10233 +
10234 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10235 +               struct inode *h_inode)
10236 +{
10237 +       int err, do_dx;
10238 +       struct super_block *sb;
10239 +       struct au_branch *br;
10240 +       struct au_dyaop *dyaop;
10241 +
10242 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
10243 +       IiMustWriteLock(inode);
10244 +
10245 +       sb = inode->i_sb;
10246 +       br = au_sbr(sb, bindex);
10247 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
10248 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
10249 +       err = PTR_ERR(dyaop);
10250 +       if (IS_ERR(dyaop))
10251 +               /* unnecessary to call dy_fput() */
10252 +               goto out;
10253 +
10254 +       err = 0;
10255 +       inode->i_mapping->a_ops = &dyaop->da_op;
10256 +
10257 +out:
10258 +       return err;
10259 +}
10260 +
10261 +/*
10262 + * Is it safe to replace a_ops during the inode/file is in operation?
10263 + * Yes, I hope so.
10264 + */
10265 +int au_dy_irefresh(struct inode *inode)
10266 +{
10267 +       int err;
10268 +       aufs_bindex_t bstart;
10269 +       struct inode *h_inode;
10270 +
10271 +       err = 0;
10272 +       if (S_ISREG(inode->i_mode)) {
10273 +               bstart = au_ibstart(inode);
10274 +               h_inode = au_h_iptr(inode, bstart);
10275 +               err = au_dy_iaop(inode, bstart, h_inode);
10276 +       }
10277 +       return err;
10278 +}
10279 +
10280 +void au_dy_arefresh(int do_dx)
10281 +{
10282 +       struct au_splhead *spl;
10283 +       struct list_head *head;
10284 +       struct au_dykey *key;
10285 +
10286 +       spl = dynop + AuDy_AOP;
10287 +       head = &spl->head;
10288 +       spin_lock(&spl->spin);
10289 +       list_for_each_entry(key, head, dk_list)
10290 +               dy_adx((void *)key, do_dx);
10291 +       spin_unlock(&spl->spin);
10292 +}
10293 +
10294 +/* ---------------------------------------------------------------------- */
10295 +
10296 +void __init au_dy_init(void)
10297 +{
10298 +       int i;
10299 +
10300 +       /* make sure that 'struct au_dykey *' can be any type */
10301 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
10302 +
10303 +       for (i = 0; i < AuDyLast; i++)
10304 +               au_spl_init(dynop + i);
10305 +}
10306 +
10307 +void au_dy_fin(void)
10308 +{
10309 +       int i;
10310 +
10311 +       for (i = 0; i < AuDyLast; i++)
10312 +               WARN_ON(!list_empty(&dynop[i].head));
10313 +}
10314 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
10315 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
10316 +++ linux/fs/aufs/dynop.h       2015-04-13 15:10:20.783490201 +0200
10317 @@ -0,0 +1,74 @@
10318 +/*
10319 + * Copyright (C) 2010-2015 Junjiro R. Okajima
10320 + *
10321 + * This program, aufs is free software; you can redistribute it and/or modify
10322 + * it under the terms of the GNU General Public License as published by
10323 + * the Free Software Foundation; either version 2 of the License, or
10324 + * (at your option) any later version.
10325 + *
10326 + * This program is distributed in the hope that it will be useful,
10327 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10328 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10329 + * GNU General Public License for more details.
10330 + *
10331 + * You should have received a copy of the GNU General Public License
10332 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10333 + */
10334 +
10335 +/*
10336 + * dynamically customizable operations (for regular files only)
10337 + */
10338 +
10339 +#ifndef __AUFS_DYNOP_H__
10340 +#define __AUFS_DYNOP_H__
10341 +
10342 +#ifdef __KERNEL__
10343 +
10344 +#include <linux/fs.h>
10345 +#include <linux/kref.h>
10346 +
10347 +enum {AuDy_AOP, AuDyLast};
10348 +
10349 +struct au_dynop {
10350 +       int                                             dy_type;
10351 +       union {
10352 +               const void                              *dy_hop;
10353 +               const struct address_space_operations   *dy_haop;
10354 +       };
10355 +};
10356 +
10357 +struct au_dykey {
10358 +       union {
10359 +               struct list_head        dk_list;
10360 +               struct rcu_head         dk_rcu;
10361 +       };
10362 +       struct au_dynop         dk_op;
10363 +
10364 +       /*
10365 +        * during I am in the branch local array, kref is gotten. when the
10366 +        * branch is removed, kref is put.
10367 +        */
10368 +       struct kref             dk_kref;
10369 +};
10370 +
10371 +/* stop unioning since their sizes are very different from each other */
10372 +struct au_dyaop {
10373 +       struct au_dykey                 da_key;
10374 +       struct address_space_operations da_op; /* not const */
10375 +};
10376 +
10377 +/* ---------------------------------------------------------------------- */
10378 +
10379 +/* dynop.c */
10380 +struct au_branch;
10381 +void au_dy_put(struct au_dykey *key);
10382 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10383 +               struct inode *h_inode);
10384 +int au_dy_irefresh(struct inode *inode);
10385 +void au_dy_arefresh(int do_dio);
10386 +
10387 +void __init au_dy_init(void);
10388 +void au_dy_fin(void);
10389 +
10390 +#endif /* __KERNEL__ */
10391 +#endif /* __AUFS_DYNOP_H__ */
10392 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
10393 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
10394 +++ linux/fs/aufs/export.c      2015-04-13 15:10:20.783490201 +0200
10395 @@ -0,0 +1,831 @@
10396 +/*
10397 + * Copyright (C) 2005-2015 Junjiro R. Okajima
10398 + *
10399 + * This program, aufs is free software; you can redistribute it and/or modify
10400 + * it under the terms of the GNU General Public License as published by
10401 + * the Free Software Foundation; either version 2 of the License, or
10402 + * (at your option) any later version.
10403 + *
10404 + * This program is distributed in the hope that it will be useful,
10405 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10406 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10407 + * GNU General Public License for more details.
10408 + *
10409 + * You should have received a copy of the GNU General Public License
10410 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10411 + */
10412 +
10413 +/*
10414 + * export via nfs
10415 + */
10416 +
10417 +#include <linux/exportfs.h>
10418 +#include <linux/fs_struct.h>
10419 +#include <linux/namei.h>
10420 +#include <linux/nsproxy.h>
10421 +#include <linux/random.h>
10422 +#include <linux/writeback.h>
10423 +#include "../fs/mount.h"
10424 +#include "aufs.h"
10425 +
10426 +union conv {
10427 +#ifdef CONFIG_AUFS_INO_T_64
10428 +       __u32 a[2];
10429 +#else
10430 +       __u32 a[1];
10431 +#endif
10432 +       ino_t ino;
10433 +};
10434 +
10435 +static ino_t decode_ino(__u32 *a)
10436 +{
10437 +       union conv u;
10438 +
10439 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
10440 +       u.a[0] = a[0];
10441 +#ifdef CONFIG_AUFS_INO_T_64
10442 +       u.a[1] = a[1];
10443 +#endif
10444 +       return u.ino;
10445 +}
10446 +
10447 +static void encode_ino(__u32 *a, ino_t ino)
10448 +{
10449 +       union conv u;
10450 +
10451 +       u.ino = ino;
10452 +       a[0] = u.a[0];
10453 +#ifdef CONFIG_AUFS_INO_T_64
10454 +       a[1] = u.a[1];
10455 +#endif
10456 +}
10457 +
10458 +/* NFS file handle */
10459 +enum {
10460 +       Fh_br_id,
10461 +       Fh_sigen,
10462 +#ifdef CONFIG_AUFS_INO_T_64
10463 +       /* support 64bit inode number */
10464 +       Fh_ino1,
10465 +       Fh_ino2,
10466 +       Fh_dir_ino1,
10467 +       Fh_dir_ino2,
10468 +#else
10469 +       Fh_ino1,
10470 +       Fh_dir_ino1,
10471 +#endif
10472 +       Fh_igen,
10473 +       Fh_h_type,
10474 +       Fh_tail,
10475 +
10476 +       Fh_ino = Fh_ino1,
10477 +       Fh_dir_ino = Fh_dir_ino1
10478 +};
10479 +
10480 +static int au_test_anon(struct dentry *dentry)
10481 +{
10482 +       /* note: read d_flags without d_lock */
10483 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
10484 +}
10485 +
10486 +int au_test_nfsd(void)
10487 +{
10488 +       int ret;
10489 +       struct task_struct *tsk = current;
10490 +       char comm[sizeof(tsk->comm)];
10491 +
10492 +       ret = 0;
10493 +       if (tsk->flags & PF_KTHREAD) {
10494 +               get_task_comm(comm, tsk);
10495 +               ret = !strcmp(comm, "nfsd");
10496 +       }
10497 +
10498 +       return ret;
10499 +}
10500 +
10501 +/* ---------------------------------------------------------------------- */
10502 +/* inode generation external table */
10503 +
10504 +void au_xigen_inc(struct inode *inode)
10505 +{
10506 +       loff_t pos;
10507 +       ssize_t sz;
10508 +       __u32 igen;
10509 +       struct super_block *sb;
10510 +       struct au_sbinfo *sbinfo;
10511 +
10512 +       sb = inode->i_sb;
10513 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
10514 +
10515 +       sbinfo = au_sbi(sb);
10516 +       pos = inode->i_ino;
10517 +       pos *= sizeof(igen);
10518 +       igen = inode->i_generation + 1;
10519 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
10520 +                        sizeof(igen), &pos);
10521 +       if (sz == sizeof(igen))
10522 +               return; /* success */
10523 +
10524 +       if (unlikely(sz >= 0))
10525 +               AuIOErr("xigen error (%zd)\n", sz);
10526 +}
10527 +
10528 +int au_xigen_new(struct inode *inode)
10529 +{
10530 +       int err;
10531 +       loff_t pos;
10532 +       ssize_t sz;
10533 +       struct super_block *sb;
10534 +       struct au_sbinfo *sbinfo;
10535 +       struct file *file;
10536 +
10537 +       err = 0;
10538 +       /* todo: dirty, at mount time */
10539 +       if (inode->i_ino == AUFS_ROOT_INO)
10540 +               goto out;
10541 +       sb = inode->i_sb;
10542 +       SiMustAnyLock(sb);
10543 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
10544 +               goto out;
10545 +
10546 +       err = -EFBIG;
10547 +       pos = inode->i_ino;
10548 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
10549 +               AuIOErr1("too large i%lld\n", pos);
10550 +               goto out;
10551 +       }
10552 +       pos *= sizeof(inode->i_generation);
10553 +
10554 +       err = 0;
10555 +       sbinfo = au_sbi(sb);
10556 +       file = sbinfo->si_xigen;
10557 +       BUG_ON(!file);
10558 +
10559 +       if (vfsub_f_size_read(file)
10560 +           < pos + sizeof(inode->i_generation)) {
10561 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
10562 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
10563 +                                sizeof(inode->i_generation), &pos);
10564 +       } else
10565 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
10566 +                               sizeof(inode->i_generation), &pos);
10567 +       if (sz == sizeof(inode->i_generation))
10568 +               goto out; /* success */
10569 +
10570 +       err = sz;
10571 +       if (unlikely(sz >= 0)) {
10572 +               err = -EIO;
10573 +               AuIOErr("xigen error (%zd)\n", sz);
10574 +       }
10575 +
10576 +out:
10577 +       return err;
10578 +}
10579 +
10580 +int au_xigen_set(struct super_block *sb, struct file *base)
10581 +{
10582 +       int err;
10583 +       struct au_sbinfo *sbinfo;
10584 +       struct file *file;
10585 +
10586 +       SiMustWriteLock(sb);
10587 +
10588 +       sbinfo = au_sbi(sb);
10589 +       file = au_xino_create2(base, sbinfo->si_xigen);
10590 +       err = PTR_ERR(file);
10591 +       if (IS_ERR(file))
10592 +               goto out;
10593 +       err = 0;
10594 +       if (sbinfo->si_xigen)
10595 +               fput(sbinfo->si_xigen);
10596 +       sbinfo->si_xigen = file;
10597 +
10598 +out:
10599 +       return err;
10600 +}
10601 +
10602 +void au_xigen_clr(struct super_block *sb)
10603 +{
10604 +       struct au_sbinfo *sbinfo;
10605 +
10606 +       SiMustWriteLock(sb);
10607 +
10608 +       sbinfo = au_sbi(sb);
10609 +       if (sbinfo->si_xigen) {
10610 +               fput(sbinfo->si_xigen);
10611 +               sbinfo->si_xigen = NULL;
10612 +       }
10613 +}
10614 +
10615 +/* ---------------------------------------------------------------------- */
10616 +
10617 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
10618 +                                   ino_t dir_ino)
10619 +{
10620 +       struct dentry *dentry, *d;
10621 +       struct inode *inode;
10622 +       unsigned int sigen;
10623 +
10624 +       dentry = NULL;
10625 +       inode = ilookup(sb, ino);
10626 +       if (!inode)
10627 +               goto out;
10628 +
10629 +       dentry = ERR_PTR(-ESTALE);
10630 +       sigen = au_sigen(sb);
10631 +       if (unlikely(is_bad_inode(inode)
10632 +                    || IS_DEADDIR(inode)
10633 +                    || sigen != au_iigen(inode, NULL)))
10634 +               goto out_iput;
10635 +
10636 +       dentry = NULL;
10637 +       if (!dir_ino || S_ISDIR(inode->i_mode))
10638 +               dentry = d_find_alias(inode);
10639 +       else {
10640 +               spin_lock(&inode->i_lock);
10641 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
10642 +                       spin_lock(&d->d_lock);
10643 +                       if (!au_test_anon(d)
10644 +                           && d->d_parent->d_inode->i_ino == dir_ino) {
10645 +                               dentry = dget_dlock(d);
10646 +                               spin_unlock(&d->d_lock);
10647 +                               break;
10648 +                       }
10649 +                       spin_unlock(&d->d_lock);
10650 +               }
10651 +               spin_unlock(&inode->i_lock);
10652 +       }
10653 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
10654 +               /* need to refresh */
10655 +               dput(dentry);
10656 +               dentry = NULL;
10657 +       }
10658 +
10659 +out_iput:
10660 +       iput(inode);
10661 +out:
10662 +       AuTraceErrPtr(dentry);
10663 +       return dentry;
10664 +}
10665 +
10666 +/* ---------------------------------------------------------------------- */
10667 +
10668 +/* todo: dirty? */
10669 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
10670 +
10671 +struct au_compare_mnt_args {
10672 +       /* input */
10673 +       struct super_block *sb;
10674 +
10675 +       /* output */
10676 +       struct vfsmount *mnt;
10677 +};
10678 +
10679 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
10680 +{
10681 +       struct au_compare_mnt_args *a = arg;
10682 +
10683 +       if (mnt->mnt_sb != a->sb)
10684 +               return 0;
10685 +       a->mnt = mntget(mnt);
10686 +       return 1;
10687 +}
10688 +
10689 +static struct vfsmount *au_mnt_get(struct super_block *sb)
10690 +{
10691 +       int err;
10692 +       struct path root;
10693 +       struct au_compare_mnt_args args = {
10694 +               .sb = sb
10695 +       };
10696 +
10697 +       get_fs_root(current->fs, &root);
10698 +       rcu_read_lock();
10699 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
10700 +       rcu_read_unlock();
10701 +       path_put(&root);
10702 +       AuDebugOn(!err);
10703 +       AuDebugOn(!args.mnt);
10704 +       return args.mnt;
10705 +}
10706 +
10707 +struct au_nfsd_si_lock {
10708 +       unsigned int sigen;
10709 +       aufs_bindex_t bindex, br_id;
10710 +       unsigned char force_lock;
10711 +};
10712 +
10713 +static int si_nfsd_read_lock(struct super_block *sb,
10714 +                            struct au_nfsd_si_lock *nsi_lock)
10715 +{
10716 +       int err;
10717 +       aufs_bindex_t bindex;
10718 +
10719 +       si_read_lock(sb, AuLock_FLUSH);
10720 +
10721 +       /* branch id may be wrapped around */
10722 +       err = 0;
10723 +       bindex = au_br_index(sb, nsi_lock->br_id);
10724 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
10725 +               goto out; /* success */
10726 +
10727 +       err = -ESTALE;
10728 +       bindex = -1;
10729 +       if (!nsi_lock->force_lock)
10730 +               si_read_unlock(sb);
10731 +
10732 +out:
10733 +       nsi_lock->bindex = bindex;
10734 +       return err;
10735 +}
10736 +
10737 +struct find_name_by_ino {
10738 +       struct dir_context ctx;
10739 +       int called, found;
10740 +       ino_t ino;
10741 +       char *name;
10742 +       int namelen;
10743 +};
10744 +
10745 +static int
10746 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
10747 +                loff_t offset, u64 ino, unsigned int d_type)
10748 +{
10749 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
10750 +                                                 ctx);
10751 +
10752 +       a->called++;
10753 +       if (a->ino != ino)
10754 +               return 0;
10755 +
10756 +       memcpy(a->name, name, namelen);
10757 +       a->namelen = namelen;
10758 +       a->found = 1;
10759 +       return 1;
10760 +}
10761 +
10762 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
10763 +                                    struct au_nfsd_si_lock *nsi_lock)
10764 +{
10765 +       struct dentry *dentry, *parent;
10766 +       struct file *file;
10767 +       struct inode *dir;
10768 +       struct find_name_by_ino arg = {
10769 +               .ctx = {
10770 +                       .actor = find_name_by_ino
10771 +               }
10772 +       };
10773 +       int err;
10774 +
10775 +       parent = path->dentry;
10776 +       if (nsi_lock)
10777 +               si_read_unlock(parent->d_sb);
10778 +       file = vfsub_dentry_open(path, au_dir_roflags);
10779 +       dentry = (void *)file;
10780 +       if (IS_ERR(file))
10781 +               goto out;
10782 +
10783 +       dentry = ERR_PTR(-ENOMEM);
10784 +       arg.name = (void *)__get_free_page(GFP_NOFS);
10785 +       if (unlikely(!arg.name))
10786 +               goto out_file;
10787 +       arg.ino = ino;
10788 +       arg.found = 0;
10789 +       do {
10790 +               arg.called = 0;
10791 +               /* smp_mb(); */
10792 +               err = vfsub_iterate_dir(file, &arg.ctx);
10793 +       } while (!err && !arg.found && arg.called);
10794 +       dentry = ERR_PTR(err);
10795 +       if (unlikely(err))
10796 +               goto out_name;
10797 +       /* instead of ENOENT */
10798 +       dentry = ERR_PTR(-ESTALE);
10799 +       if (!arg.found)
10800 +               goto out_name;
10801 +
10802 +       /* do not call vfsub_lkup_one() */
10803 +       dir = parent->d_inode;
10804 +       mutex_lock(&dir->i_mutex);
10805 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
10806 +       mutex_unlock(&dir->i_mutex);
10807 +       AuTraceErrPtr(dentry);
10808 +       if (IS_ERR(dentry))
10809 +               goto out_name;
10810 +       AuDebugOn(au_test_anon(dentry));
10811 +       if (unlikely(!dentry->d_inode)) {
10812 +               dput(dentry);
10813 +               dentry = ERR_PTR(-ENOENT);
10814 +       }
10815 +
10816 +out_name:
10817 +       free_page((unsigned long)arg.name);
10818 +out_file:
10819 +       fput(file);
10820 +out:
10821 +       if (unlikely(nsi_lock
10822 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
10823 +               if (!IS_ERR(dentry)) {
10824 +                       dput(dentry);
10825 +                       dentry = ERR_PTR(-ESTALE);
10826 +               }
10827 +       AuTraceErrPtr(dentry);
10828 +       return dentry;
10829 +}
10830 +
10831 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
10832 +                                       ino_t dir_ino,
10833 +                                       struct au_nfsd_si_lock *nsi_lock)
10834 +{
10835 +       struct dentry *dentry;
10836 +       struct path path;
10837 +
10838 +       if (dir_ino != AUFS_ROOT_INO) {
10839 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
10840 +               dentry = path.dentry;
10841 +               if (!path.dentry || IS_ERR(path.dentry))
10842 +                       goto out;
10843 +               AuDebugOn(au_test_anon(path.dentry));
10844 +       } else
10845 +               path.dentry = dget(sb->s_root);
10846 +
10847 +       path.mnt = au_mnt_get(sb);
10848 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
10849 +       path_put(&path);
10850 +
10851 +out:
10852 +       AuTraceErrPtr(dentry);
10853 +       return dentry;
10854 +}
10855 +
10856 +/* ---------------------------------------------------------------------- */
10857 +
10858 +static int h_acceptable(void *expv, struct dentry *dentry)
10859 +{
10860 +       return 1;
10861 +}
10862 +
10863 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
10864 +                          char *buf, int len, struct super_block *sb)
10865 +{
10866 +       char *p;
10867 +       int n;
10868 +       struct path path;
10869 +
10870 +       p = d_path(h_rootpath, buf, len);
10871 +       if (IS_ERR(p))
10872 +               goto out;
10873 +       n = strlen(p);
10874 +
10875 +       path.mnt = h_rootpath->mnt;
10876 +       path.dentry = h_parent;
10877 +       p = d_path(&path, buf, len);
10878 +       if (IS_ERR(p))
10879 +               goto out;
10880 +       if (n != 1)
10881 +               p += n;
10882 +
10883 +       path.mnt = au_mnt_get(sb);
10884 +       path.dentry = sb->s_root;
10885 +       p = d_path(&path, buf, len - strlen(p));
10886 +       mntput(path.mnt);
10887 +       if (IS_ERR(p))
10888 +               goto out;
10889 +       if (n != 1)
10890 +               p[strlen(p)] = '/';
10891 +
10892 +out:
10893 +       AuTraceErrPtr(p);
10894 +       return p;
10895 +}
10896 +
10897 +static
10898 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
10899 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
10900 +{
10901 +       struct dentry *dentry, *h_parent, *root;
10902 +       struct super_block *h_sb;
10903 +       char *pathname, *p;
10904 +       struct vfsmount *h_mnt;
10905 +       struct au_branch *br;
10906 +       int err;
10907 +       struct path path;
10908 +
10909 +       br = au_sbr(sb, nsi_lock->bindex);
10910 +       h_mnt = au_br_mnt(br);
10911 +       h_sb = h_mnt->mnt_sb;
10912 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
10913 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
10914 +                                     fh_len - Fh_tail, fh[Fh_h_type],
10915 +                                     h_acceptable, /*context*/NULL);
10916 +       dentry = h_parent;
10917 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
10918 +               AuWarn1("%s decode_fh failed, %ld\n",
10919 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
10920 +               goto out;
10921 +       }
10922 +       dentry = NULL;
10923 +       if (unlikely(au_test_anon(h_parent))) {
10924 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
10925 +                       au_sbtype(h_sb));
10926 +               goto out_h_parent;
10927 +       }
10928 +
10929 +       dentry = ERR_PTR(-ENOMEM);
10930 +       pathname = (void *)__get_free_page(GFP_NOFS);
10931 +       if (unlikely(!pathname))
10932 +               goto out_h_parent;
10933 +
10934 +       root = sb->s_root;
10935 +       path.mnt = h_mnt;
10936 +       di_read_lock_parent(root, !AuLock_IR);
10937 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
10938 +       di_read_unlock(root, !AuLock_IR);
10939 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
10940 +       dentry = (void *)p;
10941 +       if (IS_ERR(p))
10942 +               goto out_pathname;
10943 +
10944 +       si_read_unlock(sb);
10945 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
10946 +       dentry = ERR_PTR(err);
10947 +       if (unlikely(err))
10948 +               goto out_relock;
10949 +
10950 +       dentry = ERR_PTR(-ENOENT);
10951 +       AuDebugOn(au_test_anon(path.dentry));
10952 +       if (unlikely(!path.dentry->d_inode))
10953 +               goto out_path;
10954 +
10955 +       if (ino != path.dentry->d_inode->i_ino)
10956 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
10957 +       else
10958 +               dentry = dget(path.dentry);
10959 +
10960 +out_path:
10961 +       path_put(&path);
10962 +out_relock:
10963 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
10964 +               if (!IS_ERR(dentry)) {
10965 +                       dput(dentry);
10966 +                       dentry = ERR_PTR(-ESTALE);
10967 +               }
10968 +out_pathname:
10969 +       free_page((unsigned long)pathname);
10970 +out_h_parent:
10971 +       dput(h_parent);
10972 +out:
10973 +       AuTraceErrPtr(dentry);
10974 +       return dentry;
10975 +}
10976 +
10977 +/* ---------------------------------------------------------------------- */
10978 +
10979 +static struct dentry *
10980 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
10981 +                 int fh_type)
10982 +{
10983 +       struct dentry *dentry;
10984 +       __u32 *fh = fid->raw;
10985 +       struct au_branch *br;
10986 +       ino_t ino, dir_ino;
10987 +       struct au_nfsd_si_lock nsi_lock = {
10988 +               .force_lock     = 0
10989 +       };
10990 +
10991 +       dentry = ERR_PTR(-ESTALE);
10992 +       /* it should never happen, but the file handle is unreliable */
10993 +       if (unlikely(fh_len < Fh_tail))
10994 +               goto out;
10995 +       nsi_lock.sigen = fh[Fh_sigen];
10996 +       nsi_lock.br_id = fh[Fh_br_id];
10997 +
10998 +       /* branch id may be wrapped around */
10999 +       br = NULL;
11000 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
11001 +               goto out;
11002 +       nsi_lock.force_lock = 1;
11003 +
11004 +       /* is this inode still cached? */
11005 +       ino = decode_ino(fh + Fh_ino);
11006 +       /* it should never happen */
11007 +       if (unlikely(ino == AUFS_ROOT_INO))
11008 +               goto out;
11009 +
11010 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11011 +       dentry = decode_by_ino(sb, ino, dir_ino);
11012 +       if (IS_ERR(dentry))
11013 +               goto out_unlock;
11014 +       if (dentry)
11015 +               goto accept;
11016 +
11017 +       /* is the parent dir cached? */
11018 +       br = au_sbr(sb, nsi_lock.bindex);
11019 +       atomic_inc(&br->br_count);
11020 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
11021 +       if (IS_ERR(dentry))
11022 +               goto out_unlock;
11023 +       if (dentry)
11024 +               goto accept;
11025 +
11026 +       /* lookup path */
11027 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
11028 +       if (IS_ERR(dentry))
11029 +               goto out_unlock;
11030 +       if (unlikely(!dentry))
11031 +               /* todo?: make it ESTALE */
11032 +               goto out_unlock;
11033 +
11034 +accept:
11035 +       if (!au_digen_test(dentry, au_sigen(sb))
11036 +           && dentry->d_inode->i_generation == fh[Fh_igen])
11037 +               goto out_unlock; /* success */
11038 +
11039 +       dput(dentry);
11040 +       dentry = ERR_PTR(-ESTALE);
11041 +out_unlock:
11042 +       if (br)
11043 +               atomic_dec(&br->br_count);
11044 +       si_read_unlock(sb);
11045 +out:
11046 +       AuTraceErrPtr(dentry);
11047 +       return dentry;
11048 +}
11049 +
11050 +#if 0 /* reserved for future use */
11051 +/* support subtreecheck option */
11052 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
11053 +                                       int fh_len, int fh_type)
11054 +{
11055 +       struct dentry *parent;
11056 +       __u32 *fh = fid->raw;
11057 +       ino_t dir_ino;
11058 +
11059 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11060 +       parent = decode_by_ino(sb, dir_ino, 0);
11061 +       if (IS_ERR(parent))
11062 +               goto out;
11063 +       if (!parent)
11064 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
11065 +                                       dir_ino, fh, fh_len);
11066 +
11067 +out:
11068 +       AuTraceErrPtr(parent);
11069 +       return parent;
11070 +}
11071 +#endif
11072 +
11073 +/* ---------------------------------------------------------------------- */
11074 +
11075 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
11076 +                         struct inode *dir)
11077 +{
11078 +       int err;
11079 +       aufs_bindex_t bindex;
11080 +       struct super_block *sb, *h_sb;
11081 +       struct dentry *dentry, *parent, *h_parent;
11082 +       struct inode *h_dir;
11083 +       struct au_branch *br;
11084 +
11085 +       err = -ENOSPC;
11086 +       if (unlikely(*max_len <= Fh_tail)) {
11087 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
11088 +               goto out;
11089 +       }
11090 +
11091 +       err = FILEID_ROOT;
11092 +       if (inode->i_ino == AUFS_ROOT_INO) {
11093 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
11094 +               goto out;
11095 +       }
11096 +
11097 +       h_parent = NULL;
11098 +       sb = inode->i_sb;
11099 +       err = si_read_lock(sb, AuLock_FLUSH);
11100 +       if (unlikely(err))
11101 +               goto out;
11102 +
11103 +#ifdef CONFIG_AUFS_DEBUG
11104 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11105 +               AuWarn1("NFS-exporting requires xino\n");
11106 +#endif
11107 +       err = -EIO;
11108 +       parent = NULL;
11109 +       ii_read_lock_child(inode);
11110 +       bindex = au_ibstart(inode);
11111 +       if (!dir) {
11112 +               dentry = d_find_any_alias(inode);
11113 +               if (unlikely(!dentry))
11114 +                       goto out_unlock;
11115 +               AuDebugOn(au_test_anon(dentry));
11116 +               parent = dget_parent(dentry);
11117 +               dput(dentry);
11118 +               if (unlikely(!parent))
11119 +                       goto out_unlock;
11120 +               dir = parent->d_inode;
11121 +       }
11122 +
11123 +       ii_read_lock_parent(dir);
11124 +       h_dir = au_h_iptr(dir, bindex);
11125 +       ii_read_unlock(dir);
11126 +       if (unlikely(!h_dir))
11127 +               goto out_parent;
11128 +       h_parent = d_find_any_alias(h_dir);
11129 +       if (unlikely(!h_parent))
11130 +               goto out_hparent;
11131 +
11132 +       err = -EPERM;
11133 +       br = au_sbr(sb, bindex);
11134 +       h_sb = au_br_sb(br);
11135 +       if (unlikely(!h_sb->s_export_op)) {
11136 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
11137 +               goto out_hparent;
11138 +       }
11139 +
11140 +       fh[Fh_br_id] = br->br_id;
11141 +       fh[Fh_sigen] = au_sigen(sb);
11142 +       encode_ino(fh + Fh_ino, inode->i_ino);
11143 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
11144 +       fh[Fh_igen] = inode->i_generation;
11145 +
11146 +       *max_len -= Fh_tail;
11147 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
11148 +                                          max_len,
11149 +                                          /*connectable or subtreecheck*/0);
11150 +       err = fh[Fh_h_type];
11151 +       *max_len += Fh_tail;
11152 +       /* todo: macros? */
11153 +       if (err != FILEID_INVALID)
11154 +               err = 99;
11155 +       else
11156 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
11157 +
11158 +out_hparent:
11159 +       dput(h_parent);
11160 +out_parent:
11161 +       dput(parent);
11162 +out_unlock:
11163 +       ii_read_unlock(inode);
11164 +       si_read_unlock(sb);
11165 +out:
11166 +       if (unlikely(err < 0))
11167 +               err = FILEID_INVALID;
11168 +       return err;
11169 +}
11170 +
11171 +/* ---------------------------------------------------------------------- */
11172 +
11173 +static int aufs_commit_metadata(struct inode *inode)
11174 +{
11175 +       int err;
11176 +       aufs_bindex_t bindex;
11177 +       struct super_block *sb;
11178 +       struct inode *h_inode;
11179 +       int (*f)(struct inode *inode);
11180 +
11181 +       sb = inode->i_sb;
11182 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11183 +       ii_write_lock_child(inode);
11184 +       bindex = au_ibstart(inode);
11185 +       AuDebugOn(bindex < 0);
11186 +       h_inode = au_h_iptr(inode, bindex);
11187 +
11188 +       f = h_inode->i_sb->s_export_op->commit_metadata;
11189 +       if (f)
11190 +               err = f(h_inode);
11191 +       else {
11192 +               struct writeback_control wbc = {
11193 +                       .sync_mode      = WB_SYNC_ALL,
11194 +                       .nr_to_write    = 0 /* metadata only */
11195 +               };
11196 +
11197 +               err = sync_inode(h_inode, &wbc);
11198 +       }
11199 +
11200 +       au_cpup_attr_timesizes(inode);
11201 +       ii_write_unlock(inode);
11202 +       si_read_unlock(sb);
11203 +       return err;
11204 +}
11205 +
11206 +/* ---------------------------------------------------------------------- */
11207 +
11208 +static struct export_operations aufs_export_op = {
11209 +       .fh_to_dentry           = aufs_fh_to_dentry,
11210 +       /* .fh_to_parent        = aufs_fh_to_parent, */
11211 +       .encode_fh              = aufs_encode_fh,
11212 +       .commit_metadata        = aufs_commit_metadata
11213 +};
11214 +
11215 +void au_export_init(struct super_block *sb)
11216 +{
11217 +       struct au_sbinfo *sbinfo;
11218 +       __u32 u;
11219 +
11220 +       sb->s_export_op = &aufs_export_op;
11221 +       sbinfo = au_sbi(sb);
11222 +       sbinfo->si_xigen = NULL;
11223 +       get_random_bytes(&u, sizeof(u));
11224 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
11225 +       atomic_set(&sbinfo->si_xigen_next, u);
11226 +}
11227 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
11228 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
11229 +++ linux/fs/aufs/fhsm.c        2015-04-13 15:10:20.783490201 +0200
11230 @@ -0,0 +1,426 @@
11231 +/*
11232 + * Copyright (C) 2011-2015 Junjiro R. Okajima
11233 + *
11234 + * This program, aufs is free software; you can redistribute it and/or modify
11235 + * it under the terms of the GNU General Public License as published by
11236 + * the Free Software Foundation; either version 2 of the License, or
11237 + * (at your option) any later version.
11238 + *
11239 + * This program is distributed in the hope that it will be useful,
11240 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11241 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11242 + * GNU General Public License for more details.
11243 + *
11244 + * You should have received a copy of the GNU General Public License
11245 + * along with this program; if not, write to the Free Software
11246 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11247 + */
11248 +
11249 +/*
11250 + * File-based Hierarchy Storage Management
11251 + */
11252 +
11253 +#include <linux/anon_inodes.h>
11254 +#include <linux/poll.h>
11255 +#include <linux/seq_file.h>
11256 +#include <linux/statfs.h>
11257 +#include "aufs.h"
11258 +
11259 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
11260 +{
11261 +       struct au_sbinfo *sbinfo;
11262 +       struct au_fhsm *fhsm;
11263 +
11264 +       SiMustAnyLock(sb);
11265 +
11266 +       sbinfo = au_sbi(sb);
11267 +       fhsm = &sbinfo->si_fhsm;
11268 +       AuDebugOn(!fhsm);
11269 +       return fhsm->fhsm_bottom;
11270 +}
11271 +
11272 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
11273 +{
11274 +       struct au_sbinfo *sbinfo;
11275 +       struct au_fhsm *fhsm;
11276 +
11277 +       SiMustWriteLock(sb);
11278 +
11279 +       sbinfo = au_sbi(sb);
11280 +       fhsm = &sbinfo->si_fhsm;
11281 +       AuDebugOn(!fhsm);
11282 +       fhsm->fhsm_bottom = bindex;
11283 +}
11284 +
11285 +/* ---------------------------------------------------------------------- */
11286 +
11287 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
11288 +{
11289 +       struct au_br_fhsm *bf;
11290 +
11291 +       bf = br->br_fhsm;
11292 +       MtxMustLock(&bf->bf_lock);
11293 +
11294 +       return !bf->bf_readable
11295 +               || time_after(jiffies,
11296 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
11297 +}
11298 +
11299 +/* ---------------------------------------------------------------------- */
11300 +
11301 +static void au_fhsm_notify(struct super_block *sb, int val)
11302 +{
11303 +       struct au_sbinfo *sbinfo;
11304 +       struct au_fhsm *fhsm;
11305 +
11306 +       SiMustAnyLock(sb);
11307 +
11308 +       sbinfo = au_sbi(sb);
11309 +       fhsm = &sbinfo->si_fhsm;
11310 +       if (au_fhsm_pid(fhsm)
11311 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
11312 +               atomic_set(&fhsm->fhsm_readable, val);
11313 +               if (val)
11314 +                       wake_up(&fhsm->fhsm_wqh);
11315 +       }
11316 +}
11317 +
11318 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
11319 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
11320 +{
11321 +       int err;
11322 +       struct au_branch *br;
11323 +       struct au_br_fhsm *bf;
11324 +
11325 +       br = au_sbr(sb, bindex);
11326 +       AuDebugOn(au_br_rdonly(br));
11327 +       bf = br->br_fhsm;
11328 +       AuDebugOn(!bf);
11329 +
11330 +       if (do_lock)
11331 +               mutex_lock(&bf->bf_lock);
11332 +       else
11333 +               MtxMustLock(&bf->bf_lock);
11334 +
11335 +       /* sb->s_root for NFS is unreliable */
11336 +       err = au_br_stfs(br, &bf->bf_stfs);
11337 +       if (unlikely(err)) {
11338 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
11339 +               goto out;
11340 +       }
11341 +
11342 +       bf->bf_jiffy = jiffies;
11343 +       bf->bf_readable = 1;
11344 +       if (do_notify)
11345 +               au_fhsm_notify(sb, /*val*/1);
11346 +       if (rstfs)
11347 +               *rstfs = bf->bf_stfs;
11348 +
11349 +out:
11350 +       if (do_lock)
11351 +               mutex_unlock(&bf->bf_lock);
11352 +       au_fhsm_notify(sb, /*val*/1);
11353 +
11354 +       return err;
11355 +}
11356 +
11357 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
11358 +{
11359 +       int err;
11360 +       struct au_sbinfo *sbinfo;
11361 +       struct au_fhsm *fhsm;
11362 +       struct au_branch *br;
11363 +       struct au_br_fhsm *bf;
11364 +
11365 +       AuDbg("b%d, force %d\n", bindex, force);
11366 +       SiMustAnyLock(sb);
11367 +
11368 +       sbinfo = au_sbi(sb);
11369 +       fhsm = &sbinfo->si_fhsm;
11370 +       if (!au_ftest_si(sbinfo, FHSM)
11371 +           || fhsm->fhsm_bottom == bindex)
11372 +               return;
11373 +
11374 +       br = au_sbr(sb, bindex);
11375 +       bf = br->br_fhsm;
11376 +       AuDebugOn(!bf);
11377 +       mutex_lock(&bf->bf_lock);
11378 +       if (force
11379 +           || au_fhsm_pid(fhsm)
11380 +           || au_fhsm_test_jiffy(sbinfo, br))
11381 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
11382 +                                 /*do_notify*/1);
11383 +       mutex_unlock(&bf->bf_lock);
11384 +}
11385 +
11386 +void au_fhsm_wrote_all(struct super_block *sb, int force)
11387 +{
11388 +       aufs_bindex_t bindex, bend;
11389 +       struct au_branch *br;
11390 +
11391 +       /* exclude the bottom */
11392 +       bend = au_fhsm_bottom(sb);
11393 +       for (bindex = 0; bindex < bend; bindex++) {
11394 +               br = au_sbr(sb, bindex);
11395 +               if (au_br_fhsm(br->br_perm))
11396 +                       au_fhsm_wrote(sb, bindex, force);
11397 +       }
11398 +}
11399 +
11400 +/* ---------------------------------------------------------------------- */
11401 +
11402 +static unsigned int au_fhsm_poll(struct file *file,
11403 +                                struct poll_table_struct *wait)
11404 +{
11405 +       unsigned int mask;
11406 +       struct au_sbinfo *sbinfo;
11407 +       struct au_fhsm *fhsm;
11408 +
11409 +       mask = 0;
11410 +       sbinfo = file->private_data;
11411 +       fhsm = &sbinfo->si_fhsm;
11412 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
11413 +       if (atomic_read(&fhsm->fhsm_readable))
11414 +               mask = POLLIN /* | POLLRDNORM */;
11415 +
11416 +       AuTraceErr((int)mask);
11417 +       return mask;
11418 +}
11419 +
11420 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
11421 +                             struct aufs_stfs *stfs, __s16 brid)
11422 +{
11423 +       int err;
11424 +
11425 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
11426 +       if (!err)
11427 +               err = __put_user(brid, &stbr->brid);
11428 +       if (unlikely(err))
11429 +               err = -EFAULT;
11430 +
11431 +       return err;
11432 +}
11433 +
11434 +static ssize_t au_fhsm_do_read(struct super_block *sb,
11435 +                              struct aufs_stbr __user *stbr, size_t count)
11436 +{
11437 +       ssize_t err;
11438 +       int nstbr;
11439 +       aufs_bindex_t bindex, bend;
11440 +       struct au_branch *br;
11441 +       struct au_br_fhsm *bf;
11442 +
11443 +       /* except the bottom branch */
11444 +       err = 0;
11445 +       nstbr = 0;
11446 +       bend = au_fhsm_bottom(sb);
11447 +       for (bindex = 0; !err && bindex < bend; bindex++) {
11448 +               br = au_sbr(sb, bindex);
11449 +               if (!au_br_fhsm(br->br_perm))
11450 +                       continue;
11451 +
11452 +               bf = br->br_fhsm;
11453 +               mutex_lock(&bf->bf_lock);
11454 +               if (bf->bf_readable) {
11455 +                       err = -EFAULT;
11456 +                       if (count >= sizeof(*stbr))
11457 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
11458 +                                                         br->br_id);
11459 +                       if (!err) {
11460 +                               bf->bf_readable = 0;
11461 +                               count -= sizeof(*stbr);
11462 +                               nstbr++;
11463 +                       }
11464 +               }
11465 +               mutex_unlock(&bf->bf_lock);
11466 +       }
11467 +       if (!err)
11468 +               err = sizeof(*stbr) * nstbr;
11469 +
11470 +       return err;
11471 +}
11472 +
11473 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
11474 +                          loff_t *pos)
11475 +{
11476 +       ssize_t err;
11477 +       int readable;
11478 +       aufs_bindex_t nfhsm, bindex, bend;
11479 +       struct au_sbinfo *sbinfo;
11480 +       struct au_fhsm *fhsm;
11481 +       struct au_branch *br;
11482 +       struct super_block *sb;
11483 +
11484 +       err = 0;
11485 +       sbinfo = file->private_data;
11486 +       fhsm = &sbinfo->si_fhsm;
11487 +need_data:
11488 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
11489 +       if (!atomic_read(&fhsm->fhsm_readable)) {
11490 +               if (vfsub_file_flags(file) & O_NONBLOCK)
11491 +                       err = -EAGAIN;
11492 +               else
11493 +                       err = wait_event_interruptible_locked_irq
11494 +                               (fhsm->fhsm_wqh,
11495 +                                atomic_read(&fhsm->fhsm_readable));
11496 +       }
11497 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
11498 +       if (unlikely(err))
11499 +               goto out;
11500 +
11501 +       /* sb may already be dead */
11502 +       au_rw_read_lock(&sbinfo->si_rwsem);
11503 +       readable = atomic_read(&fhsm->fhsm_readable);
11504 +       if (readable > 0) {
11505 +               sb = sbinfo->si_sb;
11506 +               AuDebugOn(!sb);
11507 +               /* exclude the bottom branch */
11508 +               nfhsm = 0;
11509 +               bend = au_fhsm_bottom(sb);
11510 +               for (bindex = 0; bindex < bend; bindex++) {
11511 +                       br = au_sbr(sb, bindex);
11512 +                       if (au_br_fhsm(br->br_perm))
11513 +                               nfhsm++;
11514 +               }
11515 +               err = -EMSGSIZE;
11516 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
11517 +                       atomic_set(&fhsm->fhsm_readable, 0);
11518 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
11519 +                                            count);
11520 +               }
11521 +       }
11522 +       au_rw_read_unlock(&sbinfo->si_rwsem);
11523 +       if (!readable)
11524 +               goto need_data;
11525 +
11526 +out:
11527 +       return err;
11528 +}
11529 +
11530 +static int au_fhsm_release(struct inode *inode, struct file *file)
11531 +{
11532 +       struct au_sbinfo *sbinfo;
11533 +       struct au_fhsm *fhsm;
11534 +
11535 +       /* sb may already be dead */
11536 +       sbinfo = file->private_data;
11537 +       fhsm = &sbinfo->si_fhsm;
11538 +       spin_lock(&fhsm->fhsm_spin);
11539 +       fhsm->fhsm_pid = 0;
11540 +       spin_unlock(&fhsm->fhsm_spin);
11541 +       kobject_put(&sbinfo->si_kobj);
11542 +
11543 +       return 0;
11544 +}
11545 +
11546 +static const struct file_operations au_fhsm_fops = {
11547 +       .owner          = THIS_MODULE,
11548 +       .llseek         = noop_llseek,
11549 +       .read           = au_fhsm_read,
11550 +       .poll           = au_fhsm_poll,
11551 +       .release        = au_fhsm_release
11552 +};
11553 +
11554 +int au_fhsm_fd(struct super_block *sb, int oflags)
11555 +{
11556 +       int err, fd;
11557 +       struct au_sbinfo *sbinfo;
11558 +       struct au_fhsm *fhsm;
11559 +
11560 +       err = -EPERM;
11561 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
11562 +               goto out;
11563 +
11564 +       err = -EINVAL;
11565 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
11566 +               goto out;
11567 +
11568 +       err = 0;
11569 +       sbinfo = au_sbi(sb);
11570 +       fhsm = &sbinfo->si_fhsm;
11571 +       spin_lock(&fhsm->fhsm_spin);
11572 +       if (!fhsm->fhsm_pid)
11573 +               fhsm->fhsm_pid = current->pid;
11574 +       else
11575 +               err = -EBUSY;
11576 +       spin_unlock(&fhsm->fhsm_spin);
11577 +       if (unlikely(err))
11578 +               goto out;
11579 +
11580 +       oflags |= O_RDONLY;
11581 +       /* oflags |= FMODE_NONOTIFY; */
11582 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
11583 +       err = fd;
11584 +       if (unlikely(fd < 0))
11585 +               goto out_pid;
11586 +
11587 +       /* succeed reglardless 'fhsm' status */
11588 +       kobject_get(&sbinfo->si_kobj);
11589 +       si_noflush_read_lock(sb);
11590 +       if (au_ftest_si(sbinfo, FHSM))
11591 +               au_fhsm_wrote_all(sb, /*force*/0);
11592 +       si_read_unlock(sb);
11593 +       goto out; /* success */
11594 +
11595 +out_pid:
11596 +       spin_lock(&fhsm->fhsm_spin);
11597 +       fhsm->fhsm_pid = 0;
11598 +       spin_unlock(&fhsm->fhsm_spin);
11599 +out:
11600 +       AuTraceErr(err);
11601 +       return err;
11602 +}
11603 +
11604 +/* ---------------------------------------------------------------------- */
11605 +
11606 +int au_fhsm_br_alloc(struct au_branch *br)
11607 +{
11608 +       int err;
11609 +
11610 +       err = 0;
11611 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
11612 +       if (br->br_fhsm)
11613 +               au_br_fhsm_init(br->br_fhsm);
11614 +       else
11615 +               err = -ENOMEM;
11616 +
11617 +       return err;
11618 +}
11619 +
11620 +/* ---------------------------------------------------------------------- */
11621 +
11622 +void au_fhsm_fin(struct super_block *sb)
11623 +{
11624 +       au_fhsm_notify(sb, /*val*/-1);
11625 +}
11626 +
11627 +void au_fhsm_init(struct au_sbinfo *sbinfo)
11628 +{
11629 +       struct au_fhsm *fhsm;
11630 +
11631 +       fhsm = &sbinfo->si_fhsm;
11632 +       spin_lock_init(&fhsm->fhsm_spin);
11633 +       init_waitqueue_head(&fhsm->fhsm_wqh);
11634 +       atomic_set(&fhsm->fhsm_readable, 0);
11635 +       fhsm->fhsm_expire
11636 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
11637 +       fhsm->fhsm_bottom = -1;
11638 +}
11639 +
11640 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
11641 +{
11642 +       sbinfo->si_fhsm.fhsm_expire
11643 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
11644 +}
11645 +
11646 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
11647 +{
11648 +       unsigned int u;
11649 +
11650 +       if (!au_ftest_si(sbinfo, FHSM))
11651 +               return;
11652 +
11653 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
11654 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
11655 +               seq_printf(seq, ",fhsm_sec=%u", u);
11656 +}
11657 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
11658 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
11659 +++ linux/fs/aufs/file.c        2015-04-13 15:10:20.783490201 +0200
11660 @@ -0,0 +1,819 @@
11661 +/*
11662 + * Copyright (C) 2005-2015 Junjiro R. Okajima
11663 + *
11664 + * This program, aufs is free software; you can redistribute it and/or modify
11665 + * it under the terms of the GNU General Public License as published by
11666 + * the Free Software Foundation; either version 2 of the License, or
11667 + * (at your option) any later version.
11668 + *
11669 + * This program is distributed in the hope that it will be useful,
11670 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11671 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11672 + * GNU General Public License for more details.
11673 + *
11674 + * You should have received a copy of the GNU General Public License
11675 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11676 + */
11677 +
11678 +/*
11679 + * handling file/dir, and address_space operation
11680 + */
11681 +
11682 +#ifdef CONFIG_AUFS_DEBUG
11683 +#include <linux/migrate.h>
11684 +#endif
11685 +#include <linux/pagemap.h>
11686 +#include "aufs.h"
11687 +
11688 +/* drop flags for writing */
11689 +unsigned int au_file_roflags(unsigned int flags)
11690 +{
11691 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
11692 +       flags |= O_RDONLY | O_NOATIME;
11693 +       return flags;
11694 +}
11695 +
11696 +/* common functions to regular file and dir */
11697 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
11698 +                      struct file *file, int force_wr)
11699 +{
11700 +       struct file *h_file;
11701 +       struct dentry *h_dentry;
11702 +       struct inode *h_inode;
11703 +       struct super_block *sb;
11704 +       struct au_branch *br;
11705 +       struct path h_path;
11706 +       int err, exec_flag;
11707 +
11708 +       /* a race condition can happen between open and unlink/rmdir */
11709 +       h_file = ERR_PTR(-ENOENT);
11710 +       h_dentry = au_h_dptr(dentry, bindex);
11711 +       if (au_test_nfsd() && !h_dentry)
11712 +               goto out;
11713 +       h_inode = h_dentry->d_inode;
11714 +       if (au_test_nfsd() && !h_inode)
11715 +               goto out;
11716 +       spin_lock(&h_dentry->d_lock);
11717 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
11718 +               || !h_inode
11719 +               /* || !dentry->d_inode->i_nlink */
11720 +               ;
11721 +       spin_unlock(&h_dentry->d_lock);
11722 +       if (unlikely(err))
11723 +               goto out;
11724 +
11725 +       sb = dentry->d_sb;
11726 +       br = au_sbr(sb, bindex);
11727 +       h_file = ERR_PTR(-EACCES);
11728 +       exec_flag = flags & __FMODE_EXEC;
11729 +       if (exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC))
11730 +               goto out;
11731 +
11732 +       /* drop flags for writing */
11733 +       if (au_test_ro(sb, bindex, dentry->d_inode)) {
11734 +               if (force_wr && !(flags & O_WRONLY))
11735 +                       force_wr = 0;
11736 +               flags = au_file_roflags(flags);
11737 +               if (force_wr) {
11738 +                       h_file = ERR_PTR(-EROFS);
11739 +                       flags = au_file_roflags(flags);
11740 +                       if (unlikely(vfsub_native_ro(h_inode)
11741 +                                    || IS_APPEND(h_inode)))
11742 +                               goto out;
11743 +                       flags &= ~O_ACCMODE;
11744 +                       flags |= O_WRONLY;
11745 +               }
11746 +       }
11747 +       flags &= ~O_CREAT;
11748 +       atomic_inc(&br->br_count);
11749 +       h_path.dentry = h_dentry;
11750 +       h_path.mnt = au_br_mnt(br);
11751 +       h_file = vfsub_dentry_open(&h_path, flags);
11752 +       if (IS_ERR(h_file))
11753 +               goto out_br;
11754 +
11755 +       if (exec_flag) {
11756 +               err = deny_write_access(h_file);
11757 +               if (unlikely(err)) {
11758 +                       fput(h_file);
11759 +                       h_file = ERR_PTR(err);
11760 +                       goto out_br;
11761 +               }
11762 +       }
11763 +       fsnotify_open(h_file);
11764 +       goto out; /* success */
11765 +
11766 +out_br:
11767 +       atomic_dec(&br->br_count);
11768 +out:
11769 +       return h_file;
11770 +}
11771 +
11772 +static int au_cmoo(struct dentry *dentry)
11773 +{
11774 +       int err, cmoo;
11775 +       unsigned int udba;
11776 +       struct path h_path;
11777 +       struct au_pin pin;
11778 +       struct au_cp_generic cpg = {
11779 +               .dentry = dentry,
11780 +               .bdst   = -1,
11781 +               .bsrc   = -1,
11782 +               .len    = -1,
11783 +               .pin    = &pin,
11784 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
11785 +       };
11786 +       struct inode *delegated;
11787 +       struct super_block *sb;
11788 +       struct au_sbinfo *sbinfo;
11789 +       struct au_fhsm *fhsm;
11790 +       pid_t pid;
11791 +       struct au_branch *br;
11792 +       struct dentry *parent;
11793 +       struct au_hinode *hdir;
11794 +
11795 +       DiMustWriteLock(dentry);
11796 +       IiMustWriteLock(dentry->d_inode);
11797 +
11798 +       err = 0;
11799 +       if (IS_ROOT(dentry))
11800 +               goto out;
11801 +       cpg.bsrc = au_dbstart(dentry);
11802 +       if (!cpg.bsrc)
11803 +               goto out;
11804 +
11805 +       sb = dentry->d_sb;
11806 +       sbinfo = au_sbi(sb);
11807 +       fhsm = &sbinfo->si_fhsm;
11808 +       pid = au_fhsm_pid(fhsm);
11809 +       if (pid
11810 +           && (current->pid == pid
11811 +               || current->real_parent->pid == pid))
11812 +               goto out;
11813 +
11814 +       br = au_sbr(sb, cpg.bsrc);
11815 +       cmoo = au_br_cmoo(br->br_perm);
11816 +       if (!cmoo)
11817 +               goto out;
11818 +       if (!d_is_reg(dentry))
11819 +               cmoo &= AuBrAttr_COO_ALL;
11820 +       if (!cmoo)
11821 +               goto out;
11822 +
11823 +       parent = dget_parent(dentry);
11824 +       di_write_lock_parent(parent);
11825 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
11826 +       cpg.bdst = err;
11827 +       if (unlikely(err < 0)) {
11828 +               err = 0;        /* there is no upper writable branch */
11829 +               goto out_dgrade;
11830 +       }
11831 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
11832 +
11833 +       /* do not respect the coo attrib for the target branch */
11834 +       err = au_cpup_dirs(dentry, cpg.bdst);
11835 +       if (unlikely(err))
11836 +               goto out_dgrade;
11837 +
11838 +       di_downgrade_lock(parent, AuLock_IR);
11839 +       udba = au_opt_udba(sb);
11840 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
11841 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
11842 +       if (unlikely(err))
11843 +               goto out_parent;
11844 +
11845 +       err = au_sio_cpup_simple(&cpg);
11846 +       au_unpin(&pin);
11847 +       if (unlikely(err))
11848 +               goto out_parent;
11849 +       if (!(cmoo & AuBrWAttr_MOO))
11850 +               goto out_parent; /* success */
11851 +
11852 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
11853 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
11854 +       if (unlikely(err))
11855 +               goto out_parent;
11856 +
11857 +       h_path.mnt = au_br_mnt(br);
11858 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
11859 +       hdir = au_hi(parent->d_inode, cpg.bsrc);
11860 +       delegated = NULL;
11861 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
11862 +       au_unpin(&pin);
11863 +       /* todo: keep h_dentry or not? */
11864 +       if (unlikely(err == -EWOULDBLOCK)) {
11865 +               pr_warn("cannot retry for NFSv4 delegation"
11866 +                       " for an internal unlink\n");
11867 +               iput(delegated);
11868 +       }
11869 +       if (unlikely(err)) {
11870 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
11871 +                      dentry, err);
11872 +               err = 0;
11873 +       }
11874 +       goto out_parent; /* success */
11875 +
11876 +out_dgrade:
11877 +       di_downgrade_lock(parent, AuLock_IR);
11878 +out_parent:
11879 +       di_read_unlock(parent, AuLock_IR);
11880 +       dput(parent);
11881 +out:
11882 +       AuTraceErr(err);
11883 +       return err;
11884 +}
11885 +
11886 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
11887 +              struct au_fidir *fidir)
11888 +{
11889 +       int err;
11890 +       struct dentry *dentry;
11891 +       struct au_finfo *finfo;
11892 +
11893 +       err = au_finfo_init(file, fidir);
11894 +       if (unlikely(err))
11895 +               goto out;
11896 +
11897 +       dentry = file->f_path.dentry;
11898 +       di_write_lock_child(dentry);
11899 +       err = au_cmoo(dentry);
11900 +       di_downgrade_lock(dentry, AuLock_IR);
11901 +       if (!err)
11902 +               err = open(file, vfsub_file_flags(file));
11903 +       di_read_unlock(dentry, AuLock_IR);
11904 +
11905 +       finfo = au_fi(file);
11906 +       if (!err) {
11907 +               finfo->fi_file = file;
11908 +               au_sphl_add(&finfo->fi_hlist,
11909 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
11910 +       }
11911 +       fi_write_unlock(file);
11912 +       if (unlikely(err)) {
11913 +               finfo->fi_hdir = NULL;
11914 +               au_finfo_fin(file);
11915 +       }
11916 +
11917 +out:
11918 +       return err;
11919 +}
11920 +
11921 +int au_reopen_nondir(struct file *file)
11922 +{
11923 +       int err;
11924 +       aufs_bindex_t bstart;
11925 +       struct dentry *dentry;
11926 +       struct file *h_file, *h_file_tmp;
11927 +
11928 +       dentry = file->f_path.dentry;
11929 +       bstart = au_dbstart(dentry);
11930 +       h_file_tmp = NULL;
11931 +       if (au_fbstart(file) == bstart) {
11932 +               h_file = au_hf_top(file);
11933 +               if (file->f_mode == h_file->f_mode)
11934 +                       return 0; /* success */
11935 +               h_file_tmp = h_file;
11936 +               get_file(h_file_tmp);
11937 +               au_set_h_fptr(file, bstart, NULL);
11938 +       }
11939 +       AuDebugOn(au_fi(file)->fi_hdir);
11940 +       /*
11941 +        * it can happen
11942 +        * file exists on both of rw and ro
11943 +        * open --> dbstart and fbstart are both 0
11944 +        * prepend a branch as rw, "rw" become ro
11945 +        * remove rw/file
11946 +        * delete the top branch, "rw" becomes rw again
11947 +        *      --> dbstart is 1, fbstart is still 0
11948 +        * write --> fbstart is 0 but dbstart is 1
11949 +        */
11950 +       /* AuDebugOn(au_fbstart(file) < bstart); */
11951 +
11952 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
11953 +                          file, /*force_wr*/0);
11954 +       err = PTR_ERR(h_file);
11955 +       if (IS_ERR(h_file)) {
11956 +               if (h_file_tmp) {
11957 +                       atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
11958 +                       au_set_h_fptr(file, bstart, h_file_tmp);
11959 +                       h_file_tmp = NULL;
11960 +               }
11961 +               goto out; /* todo: close all? */
11962 +       }
11963 +
11964 +       err = 0;
11965 +       au_set_fbstart(file, bstart);
11966 +       au_set_h_fptr(file, bstart, h_file);
11967 +       au_update_figen(file);
11968 +       /* todo: necessary? */
11969 +       /* file->f_ra = h_file->f_ra; */
11970 +
11971 +out:
11972 +       if (h_file_tmp)
11973 +               fput(h_file_tmp);
11974 +       return err;
11975 +}
11976 +
11977 +/* ---------------------------------------------------------------------- */
11978 +
11979 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
11980 +                       struct dentry *hi_wh)
11981 +{
11982 +       int err;
11983 +       aufs_bindex_t bstart;
11984 +       struct au_dinfo *dinfo;
11985 +       struct dentry *h_dentry;
11986 +       struct au_hdentry *hdp;
11987 +
11988 +       dinfo = au_di(file->f_path.dentry);
11989 +       AuRwMustWriteLock(&dinfo->di_rwsem);
11990 +
11991 +       bstart = dinfo->di_bstart;
11992 +       dinfo->di_bstart = btgt;
11993 +       hdp = dinfo->di_hdentry;
11994 +       h_dentry = hdp[0 + btgt].hd_dentry;
11995 +       hdp[0 + btgt].hd_dentry = hi_wh;
11996 +       err = au_reopen_nondir(file);
11997 +       hdp[0 + btgt].hd_dentry = h_dentry;
11998 +       dinfo->di_bstart = bstart;
11999 +
12000 +       return err;
12001 +}
12002 +
12003 +static int au_ready_to_write_wh(struct file *file, loff_t len,
12004 +                               aufs_bindex_t bcpup, struct au_pin *pin)
12005 +{
12006 +       int err;
12007 +       struct inode *inode, *h_inode;
12008 +       struct dentry *h_dentry, *hi_wh;
12009 +       struct au_cp_generic cpg = {
12010 +               .dentry = file->f_path.dentry,
12011 +               .bdst   = bcpup,
12012 +               .bsrc   = -1,
12013 +               .len    = len,
12014 +               .pin    = pin
12015 +       };
12016 +
12017 +       au_update_dbstart(cpg.dentry);
12018 +       inode = cpg.dentry->d_inode;
12019 +       h_inode = NULL;
12020 +       if (au_dbstart(cpg.dentry) <= bcpup
12021 +           && au_dbend(cpg.dentry) >= bcpup) {
12022 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
12023 +               if (h_dentry)
12024 +                       h_inode = h_dentry->d_inode;
12025 +       }
12026 +       hi_wh = au_hi_wh(inode, bcpup);
12027 +       if (!hi_wh && !h_inode)
12028 +               err = au_sio_cpup_wh(&cpg, file);
12029 +       else
12030 +               /* already copied-up after unlink */
12031 +               err = au_reopen_wh(file, bcpup, hi_wh);
12032 +
12033 +       if (!err
12034 +           && (inode->i_nlink > 1
12035 +               || (inode->i_state & I_LINKABLE))
12036 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
12037 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
12038 +
12039 +       return err;
12040 +}
12041 +
12042 +/*
12043 + * prepare the @file for writing.
12044 + */
12045 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
12046 +{
12047 +       int err;
12048 +       aufs_bindex_t dbstart;
12049 +       struct dentry *parent;
12050 +       struct inode *inode;
12051 +       struct super_block *sb;
12052 +       struct file *h_file;
12053 +       struct au_cp_generic cpg = {
12054 +               .dentry = file->f_path.dentry,
12055 +               .bdst   = -1,
12056 +               .bsrc   = -1,
12057 +               .len    = len,
12058 +               .pin    = pin,
12059 +               .flags  = AuCpup_DTIME
12060 +       };
12061 +
12062 +       sb = cpg.dentry->d_sb;
12063 +       inode = cpg.dentry->d_inode;
12064 +       cpg.bsrc = au_fbstart(file);
12065 +       err = au_test_ro(sb, cpg.bsrc, inode);
12066 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
12067 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
12068 +                            /*flags*/0);
12069 +               goto out;
12070 +       }
12071 +
12072 +       /* need to cpup or reopen */
12073 +       parent = dget_parent(cpg.dentry);
12074 +       di_write_lock_parent(parent);
12075 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12076 +       cpg.bdst = err;
12077 +       if (unlikely(err < 0))
12078 +               goto out_dgrade;
12079 +       err = 0;
12080 +
12081 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
12082 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
12083 +               if (unlikely(err))
12084 +                       goto out_dgrade;
12085 +       }
12086 +
12087 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12088 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12089 +       if (unlikely(err))
12090 +               goto out_dgrade;
12091 +
12092 +       dbstart = au_dbstart(cpg.dentry);
12093 +       if (dbstart <= cpg.bdst)
12094 +               cpg.bsrc = cpg.bdst;
12095 +
12096 +       if (dbstart <= cpg.bdst         /* just reopen */
12097 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
12098 +               ) {
12099 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
12100 +               if (IS_ERR(h_file))
12101 +                       err = PTR_ERR(h_file);
12102 +               else {
12103 +                       di_downgrade_lock(parent, AuLock_IR);
12104 +                       if (dbstart > cpg.bdst)
12105 +                               err = au_sio_cpup_simple(&cpg);
12106 +                       if (!err)
12107 +                               err = au_reopen_nondir(file);
12108 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
12109 +               }
12110 +       } else {                        /* copyup as wh and reopen */
12111 +               /*
12112 +                * since writable hfsplus branch is not supported,
12113 +                * h_open_pre/post() are unnecessary.
12114 +                */
12115 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
12116 +               di_downgrade_lock(parent, AuLock_IR);
12117 +       }
12118 +
12119 +       if (!err) {
12120 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
12121 +               goto out_dput; /* success */
12122 +       }
12123 +       au_unpin(pin);
12124 +       goto out_unlock;
12125 +
12126 +out_dgrade:
12127 +       di_downgrade_lock(parent, AuLock_IR);
12128 +out_unlock:
12129 +       di_read_unlock(parent, AuLock_IR);
12130 +out_dput:
12131 +       dput(parent);
12132 +out:
12133 +       return err;
12134 +}
12135 +
12136 +/* ---------------------------------------------------------------------- */
12137 +
12138 +int au_do_flush(struct file *file, fl_owner_t id,
12139 +               int (*flush)(struct file *file, fl_owner_t id))
12140 +{
12141 +       int err;
12142 +       struct super_block *sb;
12143 +       struct inode *inode;
12144 +
12145 +       inode = file_inode(file);
12146 +       sb = inode->i_sb;
12147 +       si_noflush_read_lock(sb);
12148 +       fi_read_lock(file);
12149 +       ii_read_lock_child(inode);
12150 +
12151 +       err = flush(file, id);
12152 +       au_cpup_attr_timesizes(inode);
12153 +
12154 +       ii_read_unlock(inode);
12155 +       fi_read_unlock(file);
12156 +       si_read_unlock(sb);
12157 +       return err;
12158 +}
12159 +
12160 +/* ---------------------------------------------------------------------- */
12161 +
12162 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
12163 +{
12164 +       int err;
12165 +       struct au_pin pin;
12166 +       struct au_finfo *finfo;
12167 +       struct dentry *parent, *hi_wh;
12168 +       struct inode *inode;
12169 +       struct super_block *sb;
12170 +       struct au_cp_generic cpg = {
12171 +               .dentry = file->f_path.dentry,
12172 +               .bdst   = -1,
12173 +               .bsrc   = -1,
12174 +               .len    = -1,
12175 +               .pin    = &pin,
12176 +               .flags  = AuCpup_DTIME
12177 +       };
12178 +
12179 +       FiMustWriteLock(file);
12180 +
12181 +       err = 0;
12182 +       finfo = au_fi(file);
12183 +       sb = cpg.dentry->d_sb;
12184 +       inode = cpg.dentry->d_inode;
12185 +       cpg.bdst = au_ibstart(inode);
12186 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
12187 +               goto out;
12188 +
12189 +       parent = dget_parent(cpg.dentry);
12190 +       if (au_test_ro(sb, cpg.bdst, inode)) {
12191 +               di_read_lock_parent(parent, !AuLock_IR);
12192 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12193 +               cpg.bdst = err;
12194 +               di_read_unlock(parent, !AuLock_IR);
12195 +               if (unlikely(err < 0))
12196 +                       goto out_parent;
12197 +               err = 0;
12198 +       }
12199 +
12200 +       di_read_lock_parent(parent, AuLock_IR);
12201 +       hi_wh = au_hi_wh(inode, cpg.bdst);
12202 +       if (!S_ISDIR(inode->i_mode)
12203 +           && au_opt_test(au_mntflags(sb), PLINK)
12204 +           && au_plink_test(inode)
12205 +           && !d_unhashed(cpg.dentry)
12206 +           && cpg.bdst < au_dbstart(cpg.dentry)) {
12207 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
12208 +               if (unlikely(err))
12209 +                       goto out_unlock;
12210 +
12211 +               /* always superio. */
12212 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12213 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12214 +               if (!err) {
12215 +                       err = au_sio_cpup_simple(&cpg);
12216 +                       au_unpin(&pin);
12217 +               }
12218 +       } else if (hi_wh) {
12219 +               /* already copied-up after unlink */
12220 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
12221 +               *need_reopen = 0;
12222 +       }
12223 +
12224 +out_unlock:
12225 +       di_read_unlock(parent, AuLock_IR);
12226 +out_parent:
12227 +       dput(parent);
12228 +out:
12229 +       return err;
12230 +}
12231 +
12232 +static void au_do_refresh_dir(struct file *file)
12233 +{
12234 +       aufs_bindex_t bindex, bend, new_bindex, brid;
12235 +       struct au_hfile *p, tmp, *q;
12236 +       struct au_finfo *finfo;
12237 +       struct super_block *sb;
12238 +       struct au_fidir *fidir;
12239 +
12240 +       FiMustWriteLock(file);
12241 +
12242 +       sb = file->f_path.dentry->d_sb;
12243 +       finfo = au_fi(file);
12244 +       fidir = finfo->fi_hdir;
12245 +       AuDebugOn(!fidir);
12246 +       p = fidir->fd_hfile + finfo->fi_btop;
12247 +       brid = p->hf_br->br_id;
12248 +       bend = fidir->fd_bbot;
12249 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
12250 +               if (!p->hf_file)
12251 +                       continue;
12252 +
12253 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
12254 +               if (new_bindex == bindex)
12255 +                       continue;
12256 +               if (new_bindex < 0) {
12257 +                       au_set_h_fptr(file, bindex, NULL);
12258 +                       continue;
12259 +               }
12260 +
12261 +               /* swap two lower inode, and loop again */
12262 +               q = fidir->fd_hfile + new_bindex;
12263 +               tmp = *q;
12264 +               *q = *p;
12265 +               *p = tmp;
12266 +               if (tmp.hf_file) {
12267 +                       bindex--;
12268 +                       p--;
12269 +               }
12270 +       }
12271 +
12272 +       p = fidir->fd_hfile;
12273 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
12274 +               bend = au_sbend(sb);
12275 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
12276 +                    finfo->fi_btop++, p++)
12277 +                       if (p->hf_file) {
12278 +                               if (file_inode(p->hf_file))
12279 +                                       break;
12280 +                               au_hfput(p, file);
12281 +                       }
12282 +       } else {
12283 +               bend = au_br_index(sb, brid);
12284 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
12285 +                    finfo->fi_btop++, p++)
12286 +                       if (p->hf_file)
12287 +                               au_hfput(p, file);
12288 +               bend = au_sbend(sb);
12289 +       }
12290 +
12291 +       p = fidir->fd_hfile + bend;
12292 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
12293 +            fidir->fd_bbot--, p--)
12294 +               if (p->hf_file) {
12295 +                       if (file_inode(p->hf_file))
12296 +                               break;
12297 +                       au_hfput(p, file);
12298 +               }
12299 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
12300 +}
12301 +
12302 +/*
12303 + * after branch manipulating, refresh the file.
12304 + */
12305 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
12306 +{
12307 +       int err, need_reopen;
12308 +       aufs_bindex_t bend, bindex;
12309 +       struct dentry *dentry;
12310 +       struct au_finfo *finfo;
12311 +       struct au_hfile *hfile;
12312 +
12313 +       dentry = file->f_path.dentry;
12314 +       finfo = au_fi(file);
12315 +       if (!finfo->fi_hdir) {
12316 +               hfile = &finfo->fi_htop;
12317 +               AuDebugOn(!hfile->hf_file);
12318 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
12319 +               AuDebugOn(bindex < 0);
12320 +               if (bindex != finfo->fi_btop)
12321 +                       au_set_fbstart(file, bindex);
12322 +       } else {
12323 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
12324 +               if (unlikely(err))
12325 +                       goto out;
12326 +               au_do_refresh_dir(file);
12327 +       }
12328 +
12329 +       err = 0;
12330 +       need_reopen = 1;
12331 +       if (!au_test_mmapped(file))
12332 +               err = au_file_refresh_by_inode(file, &need_reopen);
12333 +       if (!err && need_reopen && !d_unlinked(dentry))
12334 +               err = reopen(file);
12335 +       if (!err) {
12336 +               au_update_figen(file);
12337 +               goto out; /* success */
12338 +       }
12339 +
12340 +       /* error, close all lower files */
12341 +       if (finfo->fi_hdir) {
12342 +               bend = au_fbend_dir(file);
12343 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
12344 +                       au_set_h_fptr(file, bindex, NULL);
12345 +       }
12346 +
12347 +out:
12348 +       return err;
12349 +}
12350 +
12351 +/* common function to regular file and dir */
12352 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12353 +                         int wlock)
12354 +{
12355 +       int err;
12356 +       unsigned int sigen, figen;
12357 +       aufs_bindex_t bstart;
12358 +       unsigned char pseudo_link;
12359 +       struct dentry *dentry;
12360 +       struct inode *inode;
12361 +
12362 +       err = 0;
12363 +       dentry = file->f_path.dentry;
12364 +       inode = dentry->d_inode;
12365 +       sigen = au_sigen(dentry->d_sb);
12366 +       fi_write_lock(file);
12367 +       figen = au_figen(file);
12368 +       di_write_lock_child(dentry);
12369 +       bstart = au_dbstart(dentry);
12370 +       pseudo_link = (bstart != au_ibstart(inode));
12371 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
12372 +               if (!wlock) {
12373 +                       di_downgrade_lock(dentry, AuLock_IR);
12374 +                       fi_downgrade_lock(file);
12375 +               }
12376 +               goto out; /* success */
12377 +       }
12378 +
12379 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
12380 +       if (au_digen_test(dentry, sigen)) {
12381 +               err = au_reval_dpath(dentry, sigen);
12382 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
12383 +       }
12384 +
12385 +       if (!err)
12386 +               err = refresh_file(file, reopen);
12387 +       if (!err) {
12388 +               if (!wlock) {
12389 +                       di_downgrade_lock(dentry, AuLock_IR);
12390 +                       fi_downgrade_lock(file);
12391 +               }
12392 +       } else {
12393 +               di_write_unlock(dentry);
12394 +               fi_write_unlock(file);
12395 +       }
12396 +
12397 +out:
12398 +       return err;
12399 +}
12400 +
12401 +/* ---------------------------------------------------------------------- */
12402 +
12403 +/* cf. aufs_nopage() */
12404 +/* for madvise(2) */
12405 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
12406 +{
12407 +       unlock_page(page);
12408 +       return 0;
12409 +}
12410 +
12411 +/* it will never be called, but necessary to support O_DIRECT */
12412 +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
12413 +                             struct iov_iter *iter, loff_t offset)
12414 +{ BUG(); return 0; }
12415 +
12416 +/* they will never be called. */
12417 +#ifdef CONFIG_AUFS_DEBUG
12418 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
12419 +                           loff_t pos, unsigned len, unsigned flags,
12420 +                           struct page **pagep, void **fsdata)
12421 +{ AuUnsupport(); return 0; }
12422 +static int aufs_write_end(struct file *file, struct address_space *mapping,
12423 +                         loff_t pos, unsigned len, unsigned copied,
12424 +                         struct page *page, void *fsdata)
12425 +{ AuUnsupport(); return 0; }
12426 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
12427 +{ AuUnsupport(); return 0; }
12428 +
12429 +static int aufs_set_page_dirty(struct page *page)
12430 +{ AuUnsupport(); return 0; }
12431 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
12432 +                               unsigned int length)
12433 +{ AuUnsupport(); }
12434 +static int aufs_releasepage(struct page *page, gfp_t gfp)
12435 +{ AuUnsupport(); return 0; }
12436 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
12437 +                           struct page *page, enum migrate_mode mode)
12438 +{ AuUnsupport(); return 0; }
12439 +static int aufs_launder_page(struct page *page)
12440 +{ AuUnsupport(); return 0; }
12441 +static int aufs_is_partially_uptodate(struct page *page,
12442 +                                     unsigned long from,
12443 +                                     unsigned long count)
12444 +{ AuUnsupport(); return 0; }
12445 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
12446 +                                   bool *writeback)
12447 +{ AuUnsupport(); }
12448 +static int aufs_error_remove_page(struct address_space *mapping,
12449 +                                 struct page *page)
12450 +{ AuUnsupport(); return 0; }
12451 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
12452 +                             sector_t *span)
12453 +{ AuUnsupport(); return 0; }
12454 +static void aufs_swap_deactivate(struct file *file)
12455 +{ AuUnsupport(); }
12456 +#endif /* CONFIG_AUFS_DEBUG */
12457 +
12458 +const struct address_space_operations aufs_aop = {
12459 +       .readpage               = aufs_readpage,
12460 +       .direct_IO              = aufs_direct_IO,
12461 +#ifdef CONFIG_AUFS_DEBUG
12462 +       .writepage              = aufs_writepage,
12463 +       /* no writepages, because of writepage */
12464 +       .set_page_dirty         = aufs_set_page_dirty,
12465 +       /* no readpages, because of readpage */
12466 +       .write_begin            = aufs_write_begin,
12467 +       .write_end              = aufs_write_end,
12468 +       /* no bmap, no block device */
12469 +       .invalidatepage         = aufs_invalidatepage,
12470 +       .releasepage            = aufs_releasepage,
12471 +       .migratepage            = aufs_migratepage,
12472 +       .launder_page           = aufs_launder_page,
12473 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
12474 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
12475 +       .error_remove_page      = aufs_error_remove_page,
12476 +       .swap_activate          = aufs_swap_activate,
12477 +       .swap_deactivate        = aufs_swap_deactivate
12478 +#endif /* CONFIG_AUFS_DEBUG */
12479 +};
12480 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
12481 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
12482 +++ linux/fs/aufs/file.h        2015-04-13 15:10:20.783490201 +0200
12483 @@ -0,0 +1,284 @@
12484 +/*
12485 + * Copyright (C) 2005-2015 Junjiro R. Okajima
12486 + *
12487 + * This program, aufs is free software; you can redistribute it and/or modify
12488 + * it under the terms of the GNU General Public License as published by
12489 + * the Free Software Foundation; either version 2 of the License, or
12490 + * (at your option) any later version.
12491 + *
12492 + * This program is distributed in the hope that it will be useful,
12493 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12494 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12495 + * GNU General Public License for more details.
12496 + *
12497 + * You should have received a copy of the GNU General Public License
12498 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12499 + */
12500 +
12501 +/*
12502 + * file operations
12503 + */
12504 +
12505 +#ifndef __AUFS_FILE_H__
12506 +#define __AUFS_FILE_H__
12507 +
12508 +#ifdef __KERNEL__
12509 +
12510 +#include <linux/file.h>
12511 +#include <linux/fs.h>
12512 +#include <linux/poll.h>
12513 +#include "rwsem.h"
12514 +
12515 +struct au_branch;
12516 +struct au_hfile {
12517 +       struct file             *hf_file;
12518 +       struct au_branch        *hf_br;
12519 +};
12520 +
12521 +struct au_vdir;
12522 +struct au_fidir {
12523 +       aufs_bindex_t           fd_bbot;
12524 +       aufs_bindex_t           fd_nent;
12525 +       struct au_vdir          *fd_vdir_cache;
12526 +       struct au_hfile         fd_hfile[];
12527 +};
12528 +
12529 +static inline int au_fidir_sz(int nent)
12530 +{
12531 +       AuDebugOn(nent < 0);
12532 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
12533 +}
12534 +
12535 +struct au_finfo {
12536 +       atomic_t                fi_generation;
12537 +
12538 +       struct au_rwsem         fi_rwsem;
12539 +       aufs_bindex_t           fi_btop;
12540 +
12541 +       /* do not union them */
12542 +       struct {                                /* for non-dir */
12543 +               struct au_hfile                 fi_htop;
12544 +               atomic_t                        fi_mmapped;
12545 +       };
12546 +       struct au_fidir         *fi_hdir;       /* for dir only */
12547 +
12548 +       struct hlist_node       fi_hlist;
12549 +       struct file             *fi_file;       /* very ugly */
12550 +} ____cacheline_aligned_in_smp;
12551 +
12552 +/* ---------------------------------------------------------------------- */
12553 +
12554 +/* file.c */
12555 +extern const struct address_space_operations aufs_aop;
12556 +unsigned int au_file_roflags(unsigned int flags);
12557 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
12558 +                      struct file *file, int force_wr);
12559 +int au_do_open(struct file *file, int (*open)(struct file *file, int flags),
12560 +              struct au_fidir *fidir);
12561 +int au_reopen_nondir(struct file *file);
12562 +struct au_pin;
12563 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
12564 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12565 +                         int wlock);
12566 +int au_do_flush(struct file *file, fl_owner_t id,
12567 +               int (*flush)(struct file *file, fl_owner_t id));
12568 +
12569 +/* poll.c */
12570 +#ifdef CONFIG_AUFS_POLL
12571 +unsigned int aufs_poll(struct file *file, poll_table *wait);
12572 +#endif
12573 +
12574 +#ifdef CONFIG_AUFS_BR_HFSPLUS
12575 +/* hfsplus.c */
12576 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
12577 +                          int force_wr);
12578 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
12579 +                   struct file *h_file);
12580 +#else
12581 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
12582 +       aufs_bindex_t bindex, int force_wr)
12583 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
12584 +          struct file *h_file);
12585 +#endif
12586 +
12587 +/* f_op.c */
12588 +extern const struct file_operations aufs_file_fop;
12589 +int au_do_open_nondir(struct file *file, int flags);
12590 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
12591 +
12592 +/* finfo.c */
12593 +void au_hfput(struct au_hfile *hf, struct file *file);
12594 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
12595 +                  struct file *h_file);
12596 +
12597 +void au_update_figen(struct file *file);
12598 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
12599 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
12600 +
12601 +void au_fi_init_once(void *_fi);
12602 +void au_finfo_fin(struct file *file);
12603 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
12604 +
12605 +/* ioctl.c */
12606 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
12607 +#ifdef CONFIG_COMPAT
12608 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
12609 +                          unsigned long arg);
12610 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
12611 +                             unsigned long arg);
12612 +#endif
12613 +
12614 +/* ---------------------------------------------------------------------- */
12615 +
12616 +static inline struct au_finfo *au_fi(struct file *file)
12617 +{
12618 +       return file->private_data;
12619 +}
12620 +
12621 +/* ---------------------------------------------------------------------- */
12622 +
12623 +/*
12624 + * fi_read_lock, fi_write_lock,
12625 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
12626 + */
12627 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
12628 +
12629 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
12630 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
12631 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
12632 +
12633 +/* ---------------------------------------------------------------------- */
12634 +
12635 +/* todo: hard/soft set? */
12636 +static inline aufs_bindex_t au_fbstart(struct file *file)
12637 +{
12638 +       FiMustAnyLock(file);
12639 +       return au_fi(file)->fi_btop;
12640 +}
12641 +
12642 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
12643 +{
12644 +       FiMustAnyLock(file);
12645 +       AuDebugOn(!au_fi(file)->fi_hdir);
12646 +       return au_fi(file)->fi_hdir->fd_bbot;
12647 +}
12648 +
12649 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
12650 +{
12651 +       FiMustAnyLock(file);
12652 +       AuDebugOn(!au_fi(file)->fi_hdir);
12653 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
12654 +}
12655 +
12656 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
12657 +{
12658 +       FiMustWriteLock(file);
12659 +       au_fi(file)->fi_btop = bindex;
12660 +}
12661 +
12662 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
12663 +{
12664 +       FiMustWriteLock(file);
12665 +       AuDebugOn(!au_fi(file)->fi_hdir);
12666 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
12667 +}
12668 +
12669 +static inline void au_set_fvdir_cache(struct file *file,
12670 +                                     struct au_vdir *vdir_cache)
12671 +{
12672 +       FiMustWriteLock(file);
12673 +       AuDebugOn(!au_fi(file)->fi_hdir);
12674 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
12675 +}
12676 +
12677 +static inline struct file *au_hf_top(struct file *file)
12678 +{
12679 +       FiMustAnyLock(file);
12680 +       AuDebugOn(au_fi(file)->fi_hdir);
12681 +       return au_fi(file)->fi_htop.hf_file;
12682 +}
12683 +
12684 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
12685 +{
12686 +       FiMustAnyLock(file);
12687 +       AuDebugOn(!au_fi(file)->fi_hdir);
12688 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
12689 +}
12690 +
12691 +/* todo: memory barrier? */
12692 +static inline unsigned int au_figen(struct file *f)
12693 +{
12694 +       return atomic_read(&au_fi(f)->fi_generation);
12695 +}
12696 +
12697 +static inline void au_set_mmapped(struct file *f)
12698 +{
12699 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
12700 +               return;
12701 +       pr_warn("fi_mmapped wrapped around\n");
12702 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
12703 +               ;
12704 +}
12705 +
12706 +static inline void au_unset_mmapped(struct file *f)
12707 +{
12708 +       atomic_dec(&au_fi(f)->fi_mmapped);
12709 +}
12710 +
12711 +static inline int au_test_mmapped(struct file *f)
12712 +{
12713 +       return atomic_read(&au_fi(f)->fi_mmapped);
12714 +}
12715 +
12716 +/* customize vma->vm_file */
12717 +
12718 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
12719 +                                      struct file *file)
12720 +{
12721 +       struct file *f;
12722 +
12723 +       f = vma->vm_file;
12724 +       get_file(file);
12725 +       vma->vm_file = file;
12726 +       fput(f);
12727 +}
12728 +
12729 +#ifdef CONFIG_MMU
12730 +#define AuDbgVmRegion(file, vma) do {} while (0)
12731 +
12732 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
12733 +                                   struct file *file)
12734 +{
12735 +       au_do_vm_file_reset(vma, file);
12736 +}
12737 +#else
12738 +#define AuDbgVmRegion(file, vma) \
12739 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
12740 +
12741 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
12742 +                                   struct file *file)
12743 +{
12744 +       struct file *f;
12745 +
12746 +       au_do_vm_file_reset(vma, file);
12747 +       f = vma->vm_region->vm_file;
12748 +       get_file(file);
12749 +       vma->vm_region->vm_file = file;
12750 +       fput(f);
12751 +}
12752 +#endif /* CONFIG_MMU */
12753 +
12754 +/* handle vma->vm_prfile */
12755 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
12756 +                                   struct file *file)
12757 +{
12758 +       get_file(file);
12759 +       vma->vm_prfile = file;
12760 +#ifndef CONFIG_MMU
12761 +       get_file(file);
12762 +       vma->vm_region->vm_prfile = file;
12763 +#endif
12764 +}
12765 +
12766 +#endif /* __KERNEL__ */
12767 +#endif /* __AUFS_FILE_H__ */
12768 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
12769 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
12770 +++ linux/fs/aufs/finfo.c       2015-04-13 15:10:20.783490201 +0200
12771 @@ -0,0 +1,156 @@
12772 +/*
12773 + * Copyright (C) 2005-2015 Junjiro R. Okajima
12774 + *
12775 + * This program, aufs is free software; you can redistribute it and/or modify
12776 + * it under the terms of the GNU General Public License as published by
12777 + * the Free Software Foundation; either version 2 of the License, or
12778 + * (at your option) any later version.
12779 + *
12780 + * This program is distributed in the hope that it will be useful,
12781 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12782 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12783 + * GNU General Public License for more details.
12784 + *
12785 + * You should have received a copy of the GNU General Public License
12786 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12787 + */
12788 +
12789 +/*
12790 + * file private data
12791 + */
12792 +
12793 +#include "aufs.h"
12794 +
12795 +void au_hfput(struct au_hfile *hf, struct file *file)
12796 +{
12797 +       /* todo: direct access f_flags */
12798 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
12799 +               allow_write_access(hf->hf_file);
12800 +       fput(hf->hf_file);
12801 +       hf->hf_file = NULL;
12802 +       atomic_dec(&hf->hf_br->br_count);
12803 +       hf->hf_br = NULL;
12804 +}
12805 +
12806 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
12807 +{
12808 +       struct au_finfo *finfo = au_fi(file);
12809 +       struct au_hfile *hf;
12810 +       struct au_fidir *fidir;
12811 +
12812 +       fidir = finfo->fi_hdir;
12813 +       if (!fidir) {
12814 +               AuDebugOn(finfo->fi_btop != bindex);
12815 +               hf = &finfo->fi_htop;
12816 +       } else
12817 +               hf = fidir->fd_hfile + bindex;
12818 +
12819 +       if (hf && hf->hf_file)
12820 +               au_hfput(hf, file);
12821 +       if (val) {
12822 +               FiMustWriteLock(file);
12823 +               hf->hf_file = val;
12824 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
12825 +       }
12826 +}
12827 +
12828 +void au_update_figen(struct file *file)
12829 +{
12830 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
12831 +       /* smp_mb(); */ /* atomic_set */
12832 +}
12833 +
12834 +/* ---------------------------------------------------------------------- */
12835 +
12836 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
12837 +{
12838 +       struct au_fidir *fidir;
12839 +       int nbr;
12840 +
12841 +       nbr = au_sbend(sb) + 1;
12842 +       if (nbr < 2)
12843 +               nbr = 2; /* initial allocate for 2 branches */
12844 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
12845 +       if (fidir) {
12846 +               fidir->fd_bbot = -1;
12847 +               fidir->fd_nent = nbr;
12848 +               fidir->fd_vdir_cache = NULL;
12849 +       }
12850 +
12851 +       return fidir;
12852 +}
12853 +
12854 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
12855 +{
12856 +       int err;
12857 +       struct au_fidir *fidir, *p;
12858 +
12859 +       AuRwMustWriteLock(&finfo->fi_rwsem);
12860 +       fidir = finfo->fi_hdir;
12861 +       AuDebugOn(!fidir);
12862 +
12863 +       err = -ENOMEM;
12864 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
12865 +                        GFP_NOFS);
12866 +       if (p) {
12867 +               p->fd_nent = nbr;
12868 +               finfo->fi_hdir = p;
12869 +               err = 0;
12870 +       }
12871 +
12872 +       return err;
12873 +}
12874 +
12875 +/* ---------------------------------------------------------------------- */
12876 +
12877 +void au_finfo_fin(struct file *file)
12878 +{
12879 +       struct au_finfo *finfo;
12880 +
12881 +       au_nfiles_dec(file->f_path.dentry->d_sb);
12882 +
12883 +       finfo = au_fi(file);
12884 +       AuDebugOn(finfo->fi_hdir);
12885 +       AuRwDestroy(&finfo->fi_rwsem);
12886 +       au_cache_free_finfo(finfo);
12887 +}
12888 +
12889 +void au_fi_init_once(void *_finfo)
12890 +{
12891 +       struct au_finfo *finfo = _finfo;
12892 +       static struct lock_class_key aufs_fi;
12893 +
12894 +       au_rw_init(&finfo->fi_rwsem);
12895 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
12896 +}
12897 +
12898 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
12899 +{
12900 +       int err;
12901 +       struct au_finfo *finfo;
12902 +       struct dentry *dentry;
12903 +
12904 +       err = -ENOMEM;
12905 +       dentry = file->f_path.dentry;
12906 +       finfo = au_cache_alloc_finfo();
12907 +       if (unlikely(!finfo))
12908 +               goto out;
12909 +
12910 +       err = 0;
12911 +       au_nfiles_inc(dentry->d_sb);
12912 +       /* verbose coding for lock class name */
12913 +       if (!fidir)
12914 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
12915 +       else
12916 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
12917 +       au_rw_write_lock(&finfo->fi_rwsem);
12918 +       finfo->fi_btop = -1;
12919 +       finfo->fi_hdir = fidir;
12920 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
12921 +       /* smp_mb(); */ /* atomic_set */
12922 +
12923 +       file->private_data = finfo;
12924 +
12925 +out:
12926 +       return err;
12927 +}
12928 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
12929 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
12930 +++ linux/fs/aufs/f_op.c        2015-04-13 15:10:20.783490201 +0200
12931 @@ -0,0 +1,814 @@
12932 +/*
12933 + * Copyright (C) 2005-2015 Junjiro R. Okajima
12934 + *
12935 + * This program, aufs is free software; you can redistribute it and/or modify
12936 + * it under the terms of the GNU General Public License as published by
12937 + * the Free Software Foundation; either version 2 of the License, or
12938 + * (at your option) any later version.
12939 + *
12940 + * This program is distributed in the hope that it will be useful,
12941 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12942 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12943 + * GNU General Public License for more details.
12944 + *
12945 + * You should have received a copy of the GNU General Public License
12946 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12947 + */
12948 +
12949 +/*
12950 + * file and vm operations
12951 + */
12952 +
12953 +#include <linux/aio.h>
12954 +#include <linux/fs_stack.h>
12955 +#include <linux/mman.h>
12956 +#include <linux/security.h>
12957 +#include "aufs.h"
12958 +
12959 +int au_do_open_nondir(struct file *file, int flags)
12960 +{
12961 +       int err;
12962 +       aufs_bindex_t bindex;
12963 +       struct file *h_file;
12964 +       struct dentry *dentry;
12965 +       struct au_finfo *finfo;
12966 +       struct inode *h_inode;
12967 +
12968 +       FiMustWriteLock(file);
12969 +
12970 +       err = 0;
12971 +       dentry = file->f_path.dentry;
12972 +       finfo = au_fi(file);
12973 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
12974 +       atomic_set(&finfo->fi_mmapped, 0);
12975 +       bindex = au_dbstart(dentry);
12976 +       h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
12977 +       if (IS_ERR(h_file))
12978 +               err = PTR_ERR(h_file);
12979 +       else {
12980 +               if ((flags & __O_TMPFILE)
12981 +                   && !(flags & O_EXCL)) {
12982 +                       h_inode = file_inode(h_file);
12983 +                       spin_lock(&h_inode->i_lock);
12984 +                       h_inode->i_state |= I_LINKABLE;
12985 +                       spin_unlock(&h_inode->i_lock);
12986 +               }
12987 +               au_set_fbstart(file, bindex);
12988 +               au_set_h_fptr(file, bindex, h_file);
12989 +               au_update_figen(file);
12990 +               /* todo: necessary? */
12991 +               /* file->f_ra = h_file->f_ra; */
12992 +       }
12993 +
12994 +       return err;
12995 +}
12996 +
12997 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
12998 +                           struct file *file)
12999 +{
13000 +       int err;
13001 +       struct super_block *sb;
13002 +
13003 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13004 +             file, vfsub_file_flags(file), file->f_mode);
13005 +
13006 +       sb = file->f_path.dentry->d_sb;
13007 +       si_read_lock(sb, AuLock_FLUSH);
13008 +       err = au_do_open(file, au_do_open_nondir, /*fidir*/NULL);
13009 +       si_read_unlock(sb);
13010 +       return err;
13011 +}
13012 +
13013 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13014 +{
13015 +       struct au_finfo *finfo;
13016 +       aufs_bindex_t bindex;
13017 +
13018 +       finfo = au_fi(file);
13019 +       au_sphl_del(&finfo->fi_hlist,
13020 +                   &au_sbi(file->f_path.dentry->d_sb)->si_files);
13021 +       bindex = finfo->fi_btop;
13022 +       if (bindex >= 0)
13023 +               au_set_h_fptr(file, bindex, NULL);
13024 +
13025 +       au_finfo_fin(file);
13026 +       return 0;
13027 +}
13028 +
13029 +/* ---------------------------------------------------------------------- */
13030 +
13031 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
13032 +{
13033 +       int err;
13034 +       struct file *h_file;
13035 +
13036 +       err = 0;
13037 +       h_file = au_hf_top(file);
13038 +       if (h_file)
13039 +               err = vfsub_flush(h_file, id);
13040 +       return err;
13041 +}
13042 +
13043 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13044 +{
13045 +       return au_do_flush(file, id, au_do_flush_nondir);
13046 +}
13047 +
13048 +/* ---------------------------------------------------------------------- */
13049 +/*
13050 + * read and write functions acquire [fdi]_rwsem once, but release before
13051 + * mmap_sem. This is because to stop a race condition between mmap(2).
13052 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
13053 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13054 + * read functions after [fdi]_rwsem are released, but it should be harmless.
13055 + */
13056 +
13057 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
13058 +                        loff_t *ppos)
13059 +{
13060 +       ssize_t err;
13061 +       struct dentry *dentry;
13062 +       struct file *h_file;
13063 +       struct super_block *sb;
13064 +
13065 +       dentry = file->f_path.dentry;
13066 +       sb = dentry->d_sb;
13067 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13068 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
13069 +       if (unlikely(err))
13070 +               goto out;
13071 +
13072 +       h_file = au_hf_top(file);
13073 +       get_file(h_file);
13074 +       di_read_unlock(dentry, AuLock_IR);
13075 +       fi_read_unlock(file);
13076 +
13077 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
13078 +       err = vfsub_read_u(h_file, buf, count, ppos);
13079 +       /* todo: necessary? */
13080 +       /* file->f_ra = h_file->f_ra; */
13081 +       /* update without lock, I don't think it a problem */
13082 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
13083 +       fput(h_file);
13084 +
13085 +out:
13086 +       si_read_unlock(sb);
13087 +       return err;
13088 +}
13089 +
13090 +/*
13091 + * todo: very ugly
13092 + * it locks both of i_mutex and si_rwsem for read in safe.
13093 + * if the plink maintenance mode continues forever (that is the problem),
13094 + * may loop forever.
13095 + */
13096 +static void au_mtx_and_read_lock(struct inode *inode)
13097 +{
13098 +       int err;
13099 +       struct super_block *sb = inode->i_sb;
13100 +
13101 +       while (1) {
13102 +               mutex_lock(&inode->i_mutex);
13103 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13104 +               if (!err)
13105 +                       break;
13106 +               mutex_unlock(&inode->i_mutex);
13107 +               si_read_lock(sb, AuLock_NOPLMW);
13108 +               si_read_unlock(sb);
13109 +       }
13110 +}
13111 +
13112 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
13113 +                         size_t count, loff_t *ppos)
13114 +{
13115 +       ssize_t err;
13116 +       blkcnt_t blks;
13117 +       aufs_bindex_t bstart;
13118 +       struct au_pin pin;
13119 +       struct dentry *dentry;
13120 +       struct inode *inode, *h_inode;
13121 +       struct super_block *sb;
13122 +       struct file *h_file;
13123 +       char __user *buf = (char __user *)ubuf;
13124 +
13125 +       dentry = file->f_path.dentry;
13126 +       sb = dentry->d_sb;
13127 +       inode = dentry->d_inode;
13128 +       au_mtx_and_read_lock(inode);
13129 +
13130 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13131 +       if (unlikely(err))
13132 +               goto out;
13133 +
13134 +       err = au_ready_to_write(file, -1, &pin);
13135 +       di_downgrade_lock(dentry, AuLock_IR);
13136 +       if (unlikely(err)) {
13137 +               di_read_unlock(dentry, AuLock_IR);
13138 +               fi_write_unlock(file);
13139 +               goto out;
13140 +       }
13141 +
13142 +       bstart = au_fbstart(file);
13143 +       h_file = au_hf_top(file);
13144 +       get_file(h_file);
13145 +       h_inode = file_inode(h_file);
13146 +       blks = h_inode->i_blocks;
13147 +       au_unpin(&pin);
13148 +       di_read_unlock(dentry, AuLock_IR);
13149 +       fi_write_unlock(file);
13150 +
13151 +       err = vfsub_write_u(h_file, buf, count, ppos);
13152 +       ii_write_lock_child(inode);
13153 +       au_cpup_attr_timesizes(inode);
13154 +       inode->i_mode = file_inode(h_file)->i_mode;
13155 +       AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks);
13156 +       if (err > 0)
13157 +               au_fhsm_wrote(sb, bstart, /*force*/h_inode->i_blocks > blks);
13158 +       ii_write_unlock(inode);
13159 +       fput(h_file);
13160 +
13161 +out:
13162 +       si_read_unlock(sb);
13163 +       mutex_unlock(&inode->i_mutex);
13164 +       return err;
13165 +}
13166 +
13167 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13168 +                         struct iov_iter *iov_iter)
13169 +{
13170 +       ssize_t err;
13171 +       struct file *file;
13172 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
13173 +       ssize_t (*aio)(struct kiocb *, const struct iovec *, unsigned long,
13174 +                      loff_t);
13175 +
13176 +       err = security_file_permission(h_file, rw);
13177 +       if (unlikely(err))
13178 +               goto out;
13179 +
13180 +       err = -ENOSYS;
13181 +       iter = NULL;
13182 +       aio = NULL;
13183 +       if (rw == MAY_READ) {
13184 +               iter = h_file->f_op->read_iter;
13185 +               aio = h_file->f_op->aio_read;
13186 +       } else if (rw == MAY_WRITE) {
13187 +               iter = h_file->f_op->write_iter;
13188 +               aio = h_file->f_op->aio_write;
13189 +       }
13190 +
13191 +       file = kio->ki_filp;
13192 +       kio->ki_filp = h_file;
13193 +       if (iter) {
13194 +               lockdep_off();
13195 +               err = iter(kio, iov_iter);
13196 +               lockdep_on();
13197 +       } else if (aio) {
13198 +               lockdep_off();
13199 +               err = aio(kio, iov_iter->iov, iov_iter->nr_segs, kio->ki_pos);
13200 +               lockdep_on();
13201 +       } else
13202 +               /* currently there is no such fs */
13203 +               WARN_ON_ONCE(1);
13204 +       kio->ki_filp = file;
13205 +
13206 +out:
13207 +       return err;
13208 +}
13209 +
13210 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13211 +{
13212 +       ssize_t err;
13213 +       struct file *file, *h_file;
13214 +       struct dentry *dentry;
13215 +       struct super_block *sb;
13216 +
13217 +       file = kio->ki_filp;
13218 +       dentry = file->f_path.dentry;
13219 +       sb = dentry->d_sb;
13220 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13221 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
13222 +       if (unlikely(err))
13223 +               goto out;
13224 +
13225 +       h_file = au_hf_top(file);
13226 +       get_file(h_file);
13227 +       di_read_unlock(dentry, AuLock_IR);
13228 +       fi_read_unlock(file);
13229 +
13230 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
13231 +       /* todo: necessary? */
13232 +       /* file->f_ra = h_file->f_ra; */
13233 +       /* update without lock, I don't think it a problem */
13234 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
13235 +       fput(h_file);
13236 +
13237 +out:
13238 +       si_read_unlock(sb);
13239 +       return err;
13240 +}
13241 +
13242 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13243 +{
13244 +       ssize_t err;
13245 +       blkcnt_t blks;
13246 +       aufs_bindex_t bstart;
13247 +       struct au_pin pin;
13248 +       struct dentry *dentry;
13249 +       struct inode *inode, *h_inode;
13250 +       struct file *file, *h_file;
13251 +       struct super_block *sb;
13252 +
13253 +       file = kio->ki_filp;
13254 +       dentry = file->f_path.dentry;
13255 +       sb = dentry->d_sb;
13256 +       inode = dentry->d_inode;
13257 +       au_mtx_and_read_lock(inode);
13258 +
13259 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13260 +       if (unlikely(err))
13261 +               goto out;
13262 +
13263 +       err = au_ready_to_write(file, -1, &pin);
13264 +       di_downgrade_lock(dentry, AuLock_IR);
13265 +       if (unlikely(err)) {
13266 +               di_read_unlock(dentry, AuLock_IR);
13267 +               fi_write_unlock(file);
13268 +               goto out;
13269 +       }
13270 +
13271 +       bstart = au_fbstart(file);
13272 +       h_file = au_hf_top(file);
13273 +       get_file(h_file);
13274 +       h_inode = file_inode(h_file);
13275 +       blks = h_inode->i_blocks;
13276 +       au_unpin(&pin);
13277 +       di_read_unlock(dentry, AuLock_IR);
13278 +       fi_write_unlock(file);
13279 +
13280 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
13281 +       ii_write_lock_child(inode);
13282 +       au_cpup_attr_timesizes(inode);
13283 +       inode->i_mode = file_inode(h_file)->i_mode;
13284 +       AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks);
13285 +       if (err > 0)
13286 +               au_fhsm_wrote(sb, bstart, /*force*/h_inode->i_blocks > blks);
13287 +       ii_write_unlock(inode);
13288 +       fput(h_file);
13289 +
13290 +out:
13291 +       si_read_unlock(sb);
13292 +       mutex_unlock(&inode->i_mutex);
13293 +       return err;
13294 +}
13295 +
13296 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
13297 +                               struct pipe_inode_info *pipe, size_t len,
13298 +                               unsigned int flags)
13299 +{
13300 +       ssize_t err;
13301 +       struct file *h_file;
13302 +       struct dentry *dentry;
13303 +       struct super_block *sb;
13304 +
13305 +       dentry = file->f_path.dentry;
13306 +       sb = dentry->d_sb;
13307 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13308 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
13309 +       if (unlikely(err))
13310 +               goto out;
13311 +
13312 +       err = -EINVAL;
13313 +       h_file = au_hf_top(file);
13314 +       get_file(h_file);
13315 +       if (au_test_loopback_kthread()) {
13316 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
13317 +               if (file->f_mapping != h_file->f_mapping) {
13318 +                       file->f_mapping = h_file->f_mapping;
13319 +                       smp_mb(); /* unnecessary? */
13320 +               }
13321 +       }
13322 +       di_read_unlock(dentry, AuLock_IR);
13323 +       fi_read_unlock(file);
13324 +
13325 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
13326 +       /* todo: necessasry? */
13327 +       /* file->f_ra = h_file->f_ra; */
13328 +       /* update without lock, I don't think it a problem */
13329 +       fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
13330 +       fput(h_file);
13331 +
13332 +out:
13333 +       si_read_unlock(sb);
13334 +       return err;
13335 +}
13336 +
13337 +static ssize_t
13338 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
13339 +                 size_t len, unsigned int flags)
13340 +{
13341 +       ssize_t err;
13342 +       blkcnt_t blks;
13343 +       aufs_bindex_t bstart;
13344 +       struct au_pin pin;
13345 +       struct dentry *dentry;
13346 +       struct inode *inode, *h_inode;
13347 +       struct super_block *sb;
13348 +       struct file *h_file;
13349 +
13350 +       dentry = file->f_path.dentry;
13351 +       sb = dentry->d_sb;
13352 +       inode = dentry->d_inode;
13353 +       au_mtx_and_read_lock(inode);
13354 +
13355 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13356 +       if (unlikely(err))
13357 +               goto out;
13358 +
13359 +       err = au_ready_to_write(file, -1, &pin);
13360 +       di_downgrade_lock(dentry, AuLock_IR);
13361 +       if (unlikely(err)) {
13362 +               di_read_unlock(dentry, AuLock_IR);
13363 +               fi_write_unlock(file);
13364 +               goto out;
13365 +       }
13366 +
13367 +       bstart = au_fbstart(file);
13368 +       h_file = au_hf_top(file);
13369 +       get_file(h_file);
13370 +       h_inode = file_inode(h_file);
13371 +       blks = h_inode->i_blocks;
13372 +       au_unpin(&pin);
13373 +       di_read_unlock(dentry, AuLock_IR);
13374 +       fi_write_unlock(file);
13375 +
13376 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
13377 +       ii_write_lock_child(inode);
13378 +       au_cpup_attr_timesizes(inode);
13379 +       inode->i_mode = file_inode(h_file)->i_mode;
13380 +       AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks);
13381 +       if (err > 0)
13382 +               au_fhsm_wrote(sb, bstart, /*force*/h_inode->i_blocks > blks);
13383 +       ii_write_unlock(inode);
13384 +       fput(h_file);
13385 +
13386 +out:
13387 +       si_read_unlock(sb);
13388 +       mutex_unlock(&inode->i_mutex);
13389 +       return err;
13390 +}
13391 +
13392 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
13393 +                          loff_t len)
13394 +{
13395 +       long err;
13396 +       struct au_pin pin;
13397 +       struct dentry *dentry;
13398 +       struct super_block *sb;
13399 +       struct inode *inode;
13400 +       struct file *h_file;
13401 +
13402 +       dentry = file->f_path.dentry;
13403 +       sb = dentry->d_sb;
13404 +       inode = dentry->d_inode;
13405 +       au_mtx_and_read_lock(inode);
13406 +
13407 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13408 +       if (unlikely(err))
13409 +               goto out;
13410 +
13411 +       err = au_ready_to_write(file, -1, &pin);
13412 +       di_downgrade_lock(dentry, AuLock_IR);
13413 +       if (unlikely(err)) {
13414 +               di_read_unlock(dentry, AuLock_IR);
13415 +               fi_write_unlock(file);
13416 +               goto out;
13417 +       }
13418 +
13419 +       h_file = au_hf_top(file);
13420 +       get_file(h_file);
13421 +       au_unpin(&pin);
13422 +       di_read_unlock(dentry, AuLock_IR);
13423 +       fi_write_unlock(file);
13424 +
13425 +       lockdep_off();
13426 +       err = vfs_fallocate(h_file, mode, offset, len);
13427 +       lockdep_on();
13428 +       ii_write_lock_child(inode);
13429 +       au_cpup_attr_timesizes(inode);
13430 +       inode->i_mode = file_inode(h_file)->i_mode;
13431 +       ii_write_unlock(inode);
13432 +       fput(h_file);
13433 +
13434 +out:
13435 +       si_read_unlock(sb);
13436 +       mutex_unlock(&inode->i_mutex);
13437 +       return err;
13438 +}
13439 +
13440 +/* ---------------------------------------------------------------------- */
13441 +
13442 +/*
13443 + * The locking order around current->mmap_sem.
13444 + * - in most and regular cases
13445 + *   file I/O syscall -- aufs_read() or something
13446 + *     -- si_rwsem for read -- mmap_sem
13447 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
13448 + * - in mmap case
13449 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
13450 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
13451 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
13452 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
13453 + * It means that when aufs acquires si_rwsem for write, the process should never
13454 + * acquire mmap_sem.
13455 + *
13456 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
13457 + * problem either since any directory is not able to be mmap-ed.
13458 + * The similar scenario is applied to aufs_readlink() too.
13459 + */
13460 +
13461 +#if 0 /* stop calling security_file_mmap() */
13462 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
13463 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
13464 +
13465 +static unsigned long au_arch_prot_conv(unsigned long flags)
13466 +{
13467 +       /* currently ppc64 only */
13468 +#ifdef CONFIG_PPC64
13469 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
13470 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
13471 +       return AuConv_VM_PROT(flags, SAO);
13472 +#else
13473 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
13474 +       return 0;
13475 +#endif
13476 +}
13477 +
13478 +static unsigned long au_prot_conv(unsigned long flags)
13479 +{
13480 +       return AuConv_VM_PROT(flags, READ)
13481 +               | AuConv_VM_PROT(flags, WRITE)
13482 +               | AuConv_VM_PROT(flags, EXEC)
13483 +               | au_arch_prot_conv(flags);
13484 +}
13485 +
13486 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
13487 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
13488 +
13489 +static unsigned long au_flag_conv(unsigned long flags)
13490 +{
13491 +       return AuConv_VM_MAP(flags, GROWSDOWN)
13492 +               | AuConv_VM_MAP(flags, DENYWRITE)
13493 +               | AuConv_VM_MAP(flags, LOCKED);
13494 +}
13495 +#endif
13496 +
13497 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
13498 +{
13499 +       int err;
13500 +       aufs_bindex_t bstart;
13501 +       const unsigned char wlock
13502 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
13503 +       struct dentry *dentry;
13504 +       struct super_block *sb;
13505 +       struct file *h_file;
13506 +       struct au_branch *br;
13507 +       struct au_pin pin;
13508 +
13509 +       AuDbgVmRegion(file, vma);
13510 +
13511 +       dentry = file->f_path.dentry;
13512 +       sb = dentry->d_sb;
13513 +       lockdep_off();
13514 +       si_read_lock(sb, AuLock_NOPLMW);
13515 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13516 +       if (unlikely(err))
13517 +               goto out;
13518 +
13519 +       if (wlock) {
13520 +               err = au_ready_to_write(file, -1, &pin);
13521 +               di_write_unlock(dentry);
13522 +               if (unlikely(err)) {
13523 +                       fi_write_unlock(file);
13524 +                       goto out;
13525 +               }
13526 +               au_unpin(&pin);
13527 +       } else
13528 +               di_write_unlock(dentry);
13529 +
13530 +       bstart = au_fbstart(file);
13531 +       br = au_sbr(sb, bstart);
13532 +       h_file = au_hf_top(file);
13533 +       get_file(h_file);
13534 +       au_set_mmapped(file);
13535 +       fi_write_unlock(file);
13536 +       lockdep_on();
13537 +
13538 +       au_vm_file_reset(vma, h_file);
13539 +       /*
13540 +        * we cannot call security_mmap_file() here since it may acquire
13541 +        * mmap_sem or i_mutex.
13542 +        *
13543 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
13544 +        *                       au_flag_conv(vma->vm_flags));
13545 +        */
13546 +       if (!err)
13547 +               err = h_file->f_op->mmap(h_file, vma);
13548 +       if (unlikely(err))
13549 +               goto out_reset;
13550 +
13551 +       au_vm_prfile_set(vma, file);
13552 +       /* update without lock, I don't think it a problem */
13553 +       fsstack_copy_attr_atime(file_inode(file), file_inode(h_file));
13554 +       goto out_fput; /* success */
13555 +
13556 +out_reset:
13557 +       au_unset_mmapped(file);
13558 +       au_vm_file_reset(vma, file);
13559 +out_fput:
13560 +       fput(h_file);
13561 +       lockdep_off();
13562 +out:
13563 +       si_read_unlock(sb);
13564 +       lockdep_on();
13565 +       AuTraceErr(err);
13566 +       return err;
13567 +}
13568 +
13569 +/* ---------------------------------------------------------------------- */
13570 +
13571 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
13572 +                            int datasync)
13573 +{
13574 +       int err;
13575 +       struct au_pin pin;
13576 +       struct dentry *dentry;
13577 +       struct inode *inode;
13578 +       struct file *h_file;
13579 +       struct super_block *sb;
13580 +
13581 +       dentry = file->f_path.dentry;
13582 +       inode = dentry->d_inode;
13583 +       sb = dentry->d_sb;
13584 +       mutex_lock(&inode->i_mutex);
13585 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13586 +       if (unlikely(err))
13587 +               goto out;
13588 +
13589 +       err = 0; /* -EBADF; */ /* posix? */
13590 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
13591 +               goto out_si;
13592 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13593 +       if (unlikely(err))
13594 +               goto out_si;
13595 +
13596 +       err = au_ready_to_write(file, -1, &pin);
13597 +       di_downgrade_lock(dentry, AuLock_IR);
13598 +       if (unlikely(err))
13599 +               goto out_unlock;
13600 +       au_unpin(&pin);
13601 +
13602 +       err = -EINVAL;
13603 +       h_file = au_hf_top(file);
13604 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
13605 +       au_cpup_attr_timesizes(inode);
13606 +
13607 +out_unlock:
13608 +       di_read_unlock(dentry, AuLock_IR);
13609 +       fi_write_unlock(file);
13610 +out_si:
13611 +       si_read_unlock(sb);
13612 +out:
13613 +       mutex_unlock(&inode->i_mutex);
13614 +       return err;
13615 +}
13616 +
13617 +/* no one supports this operation, currently */
13618 +#if 0
13619 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
13620 +{
13621 +       int err;
13622 +       struct au_pin pin;
13623 +       struct dentry *dentry;
13624 +       struct inode *inode;
13625 +       struct file *file, *h_file;
13626 +
13627 +       file = kio->ki_filp;
13628 +       dentry = file->f_path.dentry;
13629 +       inode = dentry->d_inode;
13630 +       au_mtx_and_read_lock(inode);
13631 +
13632 +       err = 0; /* -EBADF; */ /* posix? */
13633 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
13634 +               goto out;
13635 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13636 +       if (unlikely(err))
13637 +               goto out;
13638 +
13639 +       err = au_ready_to_write(file, -1, &pin);
13640 +       di_downgrade_lock(dentry, AuLock_IR);
13641 +       if (unlikely(err))
13642 +               goto out_unlock;
13643 +       au_unpin(&pin);
13644 +
13645 +       err = -ENOSYS;
13646 +       h_file = au_hf_top(file);
13647 +       if (h_file->f_op->aio_fsync) {
13648 +               struct mutex *h_mtx;
13649 +
13650 +               h_mtx = &file_inode(h_file)->i_mutex;
13651 +               if (!is_sync_kiocb(kio)) {
13652 +                       get_file(h_file);
13653 +                       fput(file);
13654 +               }
13655 +               kio->ki_filp = h_file;
13656 +               err = h_file->f_op->aio_fsync(kio, datasync);
13657 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
13658 +               if (!err)
13659 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
13660 +               /*ignore*/
13661 +               au_cpup_attr_timesizes(inode);
13662 +               mutex_unlock(h_mtx);
13663 +       }
13664 +
13665 +out_unlock:
13666 +       di_read_unlock(dentry, AuLock_IR);
13667 +       fi_write_unlock(file);
13668 +out:
13669 +       si_read_unlock(inode->sb);
13670 +       mutex_unlock(&inode->i_mutex);
13671 +       return err;
13672 +}
13673 +#endif
13674 +
13675 +static int aufs_fasync(int fd, struct file *file, int flag)
13676 +{
13677 +       int err;
13678 +       struct file *h_file;
13679 +       struct dentry *dentry;
13680 +       struct super_block *sb;
13681 +
13682 +       dentry = file->f_path.dentry;
13683 +       sb = dentry->d_sb;
13684 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13685 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
13686 +       if (unlikely(err))
13687 +               goto out;
13688 +
13689 +       h_file = au_hf_top(file);
13690 +       if (h_file->f_op->fasync)
13691 +               err = h_file->f_op->fasync(fd, h_file, flag);
13692 +
13693 +       di_read_unlock(dentry, AuLock_IR);
13694 +       fi_read_unlock(file);
13695 +
13696 +out:
13697 +       si_read_unlock(sb);
13698 +       return err;
13699 +}
13700 +
13701 +/* ---------------------------------------------------------------------- */
13702 +
13703 +/* no one supports this operation, currently */
13704 +#if 0
13705 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
13706 +                            size_t len, loff_t *pos, int more)
13707 +{
13708 +}
13709 +#endif
13710 +
13711 +/* ---------------------------------------------------------------------- */
13712 +
13713 +const struct file_operations aufs_file_fop = {
13714 +       .owner          = THIS_MODULE,
13715 +
13716 +       .llseek         = default_llseek,
13717 +
13718 +       .read           = aufs_read,
13719 +       .write          = aufs_write,
13720 +       .read_iter      = aufs_read_iter,
13721 +       .write_iter     = aufs_write_iter,
13722 +
13723 +#ifdef CONFIG_AUFS_POLL
13724 +       .poll           = aufs_poll,
13725 +#endif
13726 +       .unlocked_ioctl = aufs_ioctl_nondir,
13727 +#ifdef CONFIG_COMPAT
13728 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
13729 +#endif
13730 +       .mmap           = aufs_mmap,
13731 +       .open           = aufs_open_nondir,
13732 +       .flush          = aufs_flush_nondir,
13733 +       .release        = aufs_release_nondir,
13734 +       .fsync          = aufs_fsync_nondir,
13735 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
13736 +       .fasync         = aufs_fasync,
13737 +       /* .sendpage    = aufs_sendpage, */
13738 +       .splice_write   = aufs_splice_write,
13739 +       .splice_read    = aufs_splice_read,
13740 +#if 0
13741 +       .aio_splice_write = aufs_aio_splice_write,
13742 +       .aio_splice_read  = aufs_aio_splice_read,
13743 +#endif
13744 +       .fallocate      = aufs_fallocate
13745 +};
13746 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
13747 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
13748 +++ linux/fs/aufs/fstype.h      2015-04-13 15:10:20.783490201 +0200
13749 @@ -0,0 +1,388 @@
13750 +/*
13751 + * Copyright (C) 2005-2015 Junjiro R. Okajima
13752 + *
13753 + * This program, aufs is free software; you can redistribute it and/or modify
13754 + * it under the terms of the GNU General Public License as published by
13755 + * the Free Software Foundation; either version 2 of the License, or
13756 + * (at your option) any later version.
13757 + *
13758 + * This program is distributed in the hope that it will be useful,
13759 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13760 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13761 + * GNU General Public License for more details.
13762 + *
13763 + * You should have received a copy of the GNU General Public License
13764 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13765 + */
13766 +
13767 +/*
13768 + * judging filesystem type
13769 + */
13770 +
13771 +#ifndef __AUFS_FSTYPE_H__
13772 +#define __AUFS_FSTYPE_H__
13773 +
13774 +#ifdef __KERNEL__
13775 +
13776 +#include <linux/fs.h>
13777 +#include <linux/magic.h>
13778 +#include <linux/romfs_fs.h>
13779 +
13780 +static inline int au_test_aufs(struct super_block *sb)
13781 +{
13782 +       return sb->s_magic == AUFS_SUPER_MAGIC;
13783 +}
13784 +
13785 +static inline const char *au_sbtype(struct super_block *sb)
13786 +{
13787 +       return sb->s_type->name;
13788 +}
13789 +
13790 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
13791 +{
13792 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
13793 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
13794 +#else
13795 +       return 0;
13796 +#endif
13797 +}
13798 +
13799 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
13800 +{
13801 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
13802 +       return sb->s_magic == ROMFS_MAGIC;
13803 +#else
13804 +       return 0;
13805 +#endif
13806 +}
13807 +
13808 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
13809 +{
13810 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
13811 +       return sb->s_magic == CRAMFS_MAGIC;
13812 +#endif
13813 +       return 0;
13814 +}
13815 +
13816 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
13817 +{
13818 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
13819 +       return sb->s_magic == NFS_SUPER_MAGIC;
13820 +#else
13821 +       return 0;
13822 +#endif
13823 +}
13824 +
13825 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
13826 +{
13827 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
13828 +       return sb->s_magic == FUSE_SUPER_MAGIC;
13829 +#else
13830 +       return 0;
13831 +#endif
13832 +}
13833 +
13834 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
13835 +{
13836 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
13837 +       return sb->s_magic == XFS_SB_MAGIC;
13838 +#else
13839 +       return 0;
13840 +#endif
13841 +}
13842 +
13843 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
13844 +{
13845 +#ifdef CONFIG_TMPFS
13846 +       return sb->s_magic == TMPFS_MAGIC;
13847 +#else
13848 +       return 0;
13849 +#endif
13850 +}
13851 +
13852 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
13853 +{
13854 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
13855 +       return !strcmp(au_sbtype(sb), "ecryptfs");
13856 +#else
13857 +       return 0;
13858 +#endif
13859 +}
13860 +
13861 +static inline int au_test_ramfs(struct super_block *sb)
13862 +{
13863 +       return sb->s_magic == RAMFS_MAGIC;
13864 +}
13865 +
13866 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
13867 +{
13868 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
13869 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
13870 +#else
13871 +       return 0;
13872 +#endif
13873 +}
13874 +
13875 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
13876 +{
13877 +#ifdef CONFIG_PROC_FS
13878 +       return sb->s_magic == PROC_SUPER_MAGIC;
13879 +#else
13880 +       return 0;
13881 +#endif
13882 +}
13883 +
13884 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
13885 +{
13886 +#ifdef CONFIG_SYSFS
13887 +       return sb->s_magic == SYSFS_MAGIC;
13888 +#else
13889 +       return 0;
13890 +#endif
13891 +}
13892 +
13893 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
13894 +{
13895 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
13896 +       return sb->s_magic == CONFIGFS_MAGIC;
13897 +#else
13898 +       return 0;
13899 +#endif
13900 +}
13901 +
13902 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
13903 +{
13904 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
13905 +       return sb->s_magic == MINIX3_SUPER_MAGIC
13906 +               || sb->s_magic == MINIX2_SUPER_MAGIC
13907 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
13908 +               || sb->s_magic == MINIX_SUPER_MAGIC
13909 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
13910 +#else
13911 +       return 0;
13912 +#endif
13913 +}
13914 +
13915 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
13916 +{
13917 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
13918 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
13919 +#else
13920 +       return 0;
13921 +#endif
13922 +}
13923 +
13924 +static inline int au_test_msdos(struct super_block *sb)
13925 +{
13926 +       return au_test_fat(sb);
13927 +}
13928 +
13929 +static inline int au_test_vfat(struct super_block *sb)
13930 +{
13931 +       return au_test_fat(sb);
13932 +}
13933 +
13934 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
13935 +{
13936 +#ifdef CONFIG_SECURITYFS
13937 +       return sb->s_magic == SECURITYFS_MAGIC;
13938 +#else
13939 +       return 0;
13940 +#endif
13941 +}
13942 +
13943 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
13944 +{
13945 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
13946 +       return sb->s_magic == SQUASHFS_MAGIC;
13947 +#else
13948 +       return 0;
13949 +#endif
13950 +}
13951 +
13952 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
13953 +{
13954 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
13955 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
13956 +#else
13957 +       return 0;
13958 +#endif
13959 +}
13960 +
13961 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
13962 +{
13963 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
13964 +       return sb->s_magic == XENFS_SUPER_MAGIC;
13965 +#else
13966 +       return 0;
13967 +#endif
13968 +}
13969 +
13970 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
13971 +{
13972 +#ifdef CONFIG_DEBUG_FS
13973 +       return sb->s_magic == DEBUGFS_MAGIC;
13974 +#else
13975 +       return 0;
13976 +#endif
13977 +}
13978 +
13979 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
13980 +{
13981 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
13982 +       return sb->s_magic == NILFS_SUPER_MAGIC;
13983 +#else
13984 +       return 0;
13985 +#endif
13986 +}
13987 +
13988 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
13989 +{
13990 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
13991 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
13992 +#else
13993 +       return 0;
13994 +#endif
13995 +}
13996 +
13997 +/* ---------------------------------------------------------------------- */
13998 +/*
13999 + * they can't be an aufs branch.
14000 + */
14001 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
14002 +{
14003 +       return
14004 +#ifndef CONFIG_AUFS_BR_RAMFS
14005 +               au_test_ramfs(sb) ||
14006 +#endif
14007 +               au_test_procfs(sb)
14008 +               || au_test_sysfs(sb)
14009 +               || au_test_configfs(sb)
14010 +               || au_test_debugfs(sb)
14011 +               || au_test_securityfs(sb)
14012 +               || au_test_xenfs(sb)
14013 +               || au_test_ecryptfs(sb)
14014 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
14015 +               || au_test_aufs(sb); /* will be supported in next version */
14016 +}
14017 +
14018 +static inline int au_test_fs_remote(struct super_block *sb)
14019 +{
14020 +       return !au_test_tmpfs(sb)
14021 +#ifdef CONFIG_AUFS_BR_RAMFS
14022 +               && !au_test_ramfs(sb)
14023 +#endif
14024 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
14025 +}
14026 +
14027 +/* ---------------------------------------------------------------------- */
14028 +
14029 +/*
14030 + * Note: these functions (below) are created after reading ->getattr() in all
14031 + * filesystems under linux/fs. it means we have to do so in every update...
14032 + */
14033 +
14034 +/*
14035 + * some filesystems require getattr to refresh the inode attributes before
14036 + * referencing.
14037 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
14038 + * and leave the work for d_revalidate()
14039 + */
14040 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
14041 +{
14042 +       return au_test_nfs(sb)
14043 +               || au_test_fuse(sb)
14044 +               /* || au_test_btrfs(sb) */      /* untested */
14045 +               ;
14046 +}
14047 +
14048 +/*
14049 + * filesystems which don't maintain i_size or i_blocks.
14050 + */
14051 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
14052 +{
14053 +       return au_test_xfs(sb)
14054 +               || au_test_btrfs(sb)
14055 +               || au_test_ubifs(sb)
14056 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
14057 +               /* || au_test_minix(sb) */      /* untested */
14058 +               ;
14059 +}
14060 +
14061 +/*
14062 + * filesystems which don't store the correct value in some of their inode
14063 + * attributes.
14064 + */
14065 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
14066 +{
14067 +       return au_test_fs_bad_iattr_size(sb)
14068 +               || au_test_fat(sb)
14069 +               || au_test_msdos(sb)
14070 +               || au_test_vfat(sb);
14071 +}
14072 +
14073 +/* they don't check i_nlink in link(2) */
14074 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
14075 +{
14076 +       return au_test_tmpfs(sb)
14077 +#ifdef CONFIG_AUFS_BR_RAMFS
14078 +               || au_test_ramfs(sb)
14079 +#endif
14080 +               || au_test_ubifs(sb)
14081 +               || au_test_hfsplus(sb);
14082 +}
14083 +
14084 +/*
14085 + * filesystems which sets S_NOATIME and S_NOCMTIME.
14086 + */
14087 +static inline int au_test_fs_notime(struct super_block *sb)
14088 +{
14089 +       return au_test_nfs(sb)
14090 +               || au_test_fuse(sb)
14091 +               || au_test_ubifs(sb)
14092 +               ;
14093 +}
14094 +
14095 +/* temporary support for i#1 in cramfs */
14096 +static inline int au_test_fs_unique_ino(struct inode *inode)
14097 +{
14098 +       if (au_test_cramfs(inode->i_sb))
14099 +               return inode->i_ino != 1;
14100 +       return 1;
14101 +}
14102 +
14103 +/* ---------------------------------------------------------------------- */
14104 +
14105 +/*
14106 + * the filesystem where the xino files placed must support i/o after unlink and
14107 + * maintain i_size and i_blocks.
14108 + */
14109 +static inline int au_test_fs_bad_xino(struct super_block *sb)
14110 +{
14111 +       return au_test_fs_remote(sb)
14112 +               || au_test_fs_bad_iattr_size(sb)
14113 +               /* don't want unnecessary work for xino */
14114 +               || au_test_aufs(sb)
14115 +               || au_test_ecryptfs(sb)
14116 +               || au_test_nilfs(sb);
14117 +}
14118 +
14119 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
14120 +{
14121 +       return au_test_tmpfs(sb)
14122 +               || au_test_ramfs(sb);
14123 +}
14124 +
14125 +/*
14126 + * test if the @sb is real-readonly.
14127 + */
14128 +static inline int au_test_fs_rr(struct super_block *sb)
14129 +{
14130 +       return au_test_squashfs(sb)
14131 +               || au_test_iso9660(sb)
14132 +               || au_test_cramfs(sb)
14133 +               || au_test_romfs(sb);
14134 +}
14135 +
14136 +#endif /* __KERNEL__ */
14137 +#endif /* __AUFS_FSTYPE_H__ */
14138 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
14139 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
14140 +++ linux/fs/aufs/hfsnotify.c   2015-04-13 15:10:20.783490201 +0200
14141 @@ -0,0 +1,288 @@
14142 +/*
14143 + * Copyright (C) 2005-2015 Junjiro R. Okajima
14144 + *
14145 + * This program, aufs is free software; you can redistribute it and/or modify
14146 + * it under the terms of the GNU General Public License as published by
14147 + * the Free Software Foundation; either version 2 of the License, or
14148 + * (at your option) any later version.
14149 + *
14150 + * This program is distributed in the hope that it will be useful,
14151 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14152 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14153 + * GNU General Public License for more details.
14154 + *
14155 + * You should have received a copy of the GNU General Public License
14156 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14157 + */
14158 +
14159 +/*
14160 + * fsnotify for the lower directories
14161 + */
14162 +
14163 +#include "aufs.h"
14164 +
14165 +/* FS_IN_IGNORED is unnecessary */
14166 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
14167 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
14168 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
14169 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
14170 +
14171 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
14172 +{
14173 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
14174 +                                            hn_mark);
14175 +       AuDbg("here\n");
14176 +       au_cache_free_hnotify(hn);
14177 +       smp_mb__before_atomic();
14178 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
14179 +               wake_up(&au_hfsn_wq);
14180 +}
14181 +
14182 +static int au_hfsn_alloc(struct au_hinode *hinode)
14183 +{
14184 +       int err;
14185 +       struct au_hnotify *hn;
14186 +       struct super_block *sb;
14187 +       struct au_branch *br;
14188 +       struct fsnotify_mark *mark;
14189 +       aufs_bindex_t bindex;
14190 +
14191 +       hn = hinode->hi_notify;
14192 +       sb = hn->hn_aufs_inode->i_sb;
14193 +       bindex = au_br_index(sb, hinode->hi_id);
14194 +       br = au_sbr(sb, bindex);
14195 +       AuDebugOn(!br->br_hfsn);
14196 +
14197 +       mark = &hn->hn_mark;
14198 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
14199 +       mark->mask = AuHfsnMask;
14200 +       /*
14201 +        * by udba rename or rmdir, aufs assign a new inode to the known
14202 +        * h_inode, so specify 1 to allow dups.
14203 +        */
14204 +       lockdep_off();
14205 +       err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
14206 +                                /*mnt*/NULL, /*allow_dups*/1);
14207 +       /* even if err */
14208 +       fsnotify_put_mark(mark);
14209 +       lockdep_on();
14210 +
14211 +       return err;
14212 +}
14213 +
14214 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
14215 +{
14216 +       struct fsnotify_mark *mark;
14217 +       unsigned long long ull;
14218 +       struct fsnotify_group *group;
14219 +
14220 +       ull = atomic64_inc_return(&au_hfsn_ifree);
14221 +       BUG_ON(!ull);
14222 +
14223 +       mark = &hn->hn_mark;
14224 +       spin_lock(&mark->lock);
14225 +       group = mark->group;
14226 +       fsnotify_get_group(group);
14227 +       spin_unlock(&mark->lock);
14228 +       lockdep_off();
14229 +       fsnotify_destroy_mark(mark, group);
14230 +       fsnotify_put_group(group);
14231 +       lockdep_on();
14232 +
14233 +       /* free hn by myself */
14234 +       return 0;
14235 +}
14236 +
14237 +/* ---------------------------------------------------------------------- */
14238 +
14239 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
14240 +{
14241 +       struct fsnotify_mark *mark;
14242 +
14243 +       mark = &hinode->hi_notify->hn_mark;
14244 +       spin_lock(&mark->lock);
14245 +       if (do_set) {
14246 +               AuDebugOn(mark->mask & AuHfsnMask);
14247 +               mark->mask |= AuHfsnMask;
14248 +       } else {
14249 +               AuDebugOn(!(mark->mask & AuHfsnMask));
14250 +               mark->mask &= ~AuHfsnMask;
14251 +       }
14252 +       spin_unlock(&mark->lock);
14253 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
14254 +}
14255 +
14256 +/* ---------------------------------------------------------------------- */
14257 +
14258 +/* #define AuDbgHnotify */
14259 +#ifdef AuDbgHnotify
14260 +static char *au_hfsn_name(u32 mask)
14261 +{
14262 +#ifdef CONFIG_AUFS_DEBUG
14263 +#define test_ret(flag)                         \
14264 +       do {                                    \
14265 +               if (mask & flag)                \
14266 +                       return #flag;           \
14267 +       } while (0)
14268 +       test_ret(FS_ACCESS);
14269 +       test_ret(FS_MODIFY);
14270 +       test_ret(FS_ATTRIB);
14271 +       test_ret(FS_CLOSE_WRITE);
14272 +       test_ret(FS_CLOSE_NOWRITE);
14273 +       test_ret(FS_OPEN);
14274 +       test_ret(FS_MOVED_FROM);
14275 +       test_ret(FS_MOVED_TO);
14276 +       test_ret(FS_CREATE);
14277 +       test_ret(FS_DELETE);
14278 +       test_ret(FS_DELETE_SELF);
14279 +       test_ret(FS_MOVE_SELF);
14280 +       test_ret(FS_UNMOUNT);
14281 +       test_ret(FS_Q_OVERFLOW);
14282 +       test_ret(FS_IN_IGNORED);
14283 +       test_ret(FS_IN_ISDIR);
14284 +       test_ret(FS_IN_ONESHOT);
14285 +       test_ret(FS_EVENT_ON_CHILD);
14286 +       return "";
14287 +#undef test_ret
14288 +#else
14289 +       return "??";
14290 +#endif
14291 +}
14292 +#endif
14293 +
14294 +/* ---------------------------------------------------------------------- */
14295 +
14296 +static void au_hfsn_free_group(struct fsnotify_group *group)
14297 +{
14298 +       struct au_br_hfsnotify *hfsn = group->private;
14299 +
14300 +       AuDbg("here\n");
14301 +       kfree(hfsn);
14302 +}
14303 +
14304 +static int au_hfsn_handle_event(struct fsnotify_group *group,
14305 +                               struct inode *inode,
14306 +                               struct fsnotify_mark *inode_mark,
14307 +                               struct fsnotify_mark *vfsmount_mark,
14308 +                               u32 mask, void *data, int data_type,
14309 +                               const unsigned char *file_name, u32 cookie)
14310 +{
14311 +       int err;
14312 +       struct au_hnotify *hnotify;
14313 +       struct inode *h_dir, *h_inode;
14314 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
14315 +
14316 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
14317 +
14318 +       err = 0;
14319 +       /* if FS_UNMOUNT happens, there must be another bug */
14320 +       AuDebugOn(mask & FS_UNMOUNT);
14321 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
14322 +               goto out;
14323 +
14324 +       h_dir = inode;
14325 +       h_inode = NULL;
14326 +#ifdef AuDbgHnotify
14327 +       au_debug_on();
14328 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
14329 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
14330 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
14331 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
14332 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
14333 +               /* WARN_ON(1); */
14334 +       }
14335 +       au_debug_off();
14336 +#endif
14337 +
14338 +       AuDebugOn(!inode_mark);
14339 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
14340 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
14341 +
14342 +out:
14343 +       return err;
14344 +}
14345 +
14346 +static struct fsnotify_ops au_hfsn_ops = {
14347 +       .handle_event           = au_hfsn_handle_event,
14348 +       .free_group_priv        = au_hfsn_free_group
14349 +};
14350 +
14351 +/* ---------------------------------------------------------------------- */
14352 +
14353 +static void au_hfsn_fin_br(struct au_branch *br)
14354 +{
14355 +       struct au_br_hfsnotify *hfsn;
14356 +
14357 +       hfsn = br->br_hfsn;
14358 +       if (hfsn) {
14359 +               lockdep_off();
14360 +               fsnotify_put_group(hfsn->hfsn_group);
14361 +               lockdep_on();
14362 +       }
14363 +}
14364 +
14365 +static int au_hfsn_init_br(struct au_branch *br, int perm)
14366 +{
14367 +       int err;
14368 +       struct fsnotify_group *group;
14369 +       struct au_br_hfsnotify *hfsn;
14370 +
14371 +       err = 0;
14372 +       br->br_hfsn = NULL;
14373 +       if (!au_br_hnotifyable(perm))
14374 +               goto out;
14375 +
14376 +       err = -ENOMEM;
14377 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
14378 +       if (unlikely(!hfsn))
14379 +               goto out;
14380 +
14381 +       err = 0;
14382 +       group = fsnotify_alloc_group(&au_hfsn_ops);
14383 +       if (IS_ERR(group)) {
14384 +               err = PTR_ERR(group);
14385 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
14386 +               goto out_hfsn;
14387 +       }
14388 +
14389 +       group->private = hfsn;
14390 +       hfsn->hfsn_group = group;
14391 +       br->br_hfsn = hfsn;
14392 +       goto out; /* success */
14393 +
14394 +out_hfsn:
14395 +       kfree(hfsn);
14396 +out:
14397 +       return err;
14398 +}
14399 +
14400 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
14401 +{
14402 +       int err;
14403 +
14404 +       err = 0;
14405 +       if (!br->br_hfsn)
14406 +               err = au_hfsn_init_br(br, perm);
14407 +
14408 +       return err;
14409 +}
14410 +
14411 +/* ---------------------------------------------------------------------- */
14412 +
14413 +static void au_hfsn_fin(void)
14414 +{
14415 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
14416 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
14417 +}
14418 +
14419 +const struct au_hnotify_op au_hnotify_op = {
14420 +       .ctl            = au_hfsn_ctl,
14421 +       .alloc          = au_hfsn_alloc,
14422 +       .free           = au_hfsn_free,
14423 +
14424 +       .fin            = au_hfsn_fin,
14425 +
14426 +       .reset_br       = au_hfsn_reset_br,
14427 +       .fin_br         = au_hfsn_fin_br,
14428 +       .init_br        = au_hfsn_init_br
14429 +};
14430 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
14431 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
14432 +++ linux/fs/aufs/hfsplus.c     2015-04-13 15:10:20.783490201 +0200
14433 @@ -0,0 +1,56 @@
14434 +/*
14435 + * Copyright (C) 2010-2015 Junjiro R. Okajima
14436 + *
14437 + * This program, aufs is free software; you can redistribute it and/or modify
14438 + * it under the terms of the GNU General Public License as published by
14439 + * the Free Software Foundation; either version 2 of the License, or
14440 + * (at your option) any later version.
14441 + *
14442 + * This program is distributed in the hope that it will be useful,
14443 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14444 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14445 + * GNU General Public License for more details.
14446 + *
14447 + * You should have received a copy of the GNU General Public License
14448 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14449 + */
14450 +
14451 +/*
14452 + * special support for filesystems which aqucires an inode mutex
14453 + * at final closing a file, eg, hfsplus.
14454 + *
14455 + * This trick is very simple and stupid, just to open the file before really
14456 + * neceeary open to tell hfsplus that this is not the final closing.
14457 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
14458 + * and au_h_open_post() after releasing it.
14459 + */
14460 +
14461 +#include "aufs.h"
14462 +
14463 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
14464 +                          int force_wr)
14465 +{
14466 +       struct file *h_file;
14467 +       struct dentry *h_dentry;
14468 +
14469 +       h_dentry = au_h_dptr(dentry, bindex);
14470 +       AuDebugOn(!h_dentry);
14471 +       AuDebugOn(!h_dentry->d_inode);
14472 +
14473 +       h_file = NULL;
14474 +       if (au_test_hfsplus(h_dentry->d_sb)
14475 +           && d_is_reg(h_dentry))
14476 +               h_file = au_h_open(dentry, bindex,
14477 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
14478 +                                  /*file*/NULL, force_wr);
14479 +       return h_file;
14480 +}
14481 +
14482 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
14483 +                   struct file *h_file)
14484 +{
14485 +       if (h_file) {
14486 +               fput(h_file);
14487 +               au_sbr_put(dentry->d_sb, bindex);
14488 +       }
14489 +}
14490 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
14491 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
14492 +++ linux/fs/aufs/hnotify.c     2015-04-13 15:10:20.783490201 +0200
14493 @@ -0,0 +1,714 @@
14494 +/*
14495 + * Copyright (C) 2005-2015 Junjiro R. Okajima
14496 + *
14497 + * This program, aufs is free software; you can redistribute it and/or modify
14498 + * it under the terms of the GNU General Public License as published by
14499 + * the Free Software Foundation; either version 2 of the License, or
14500 + * (at your option) any later version.
14501 + *
14502 + * This program is distributed in the hope that it will be useful,
14503 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14504 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14505 + * GNU General Public License for more details.
14506 + *
14507 + * You should have received a copy of the GNU General Public License
14508 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14509 + */
14510 +
14511 +/*
14512 + * abstraction to notify the direct changes on lower directories
14513 + */
14514 +
14515 +#include "aufs.h"
14516 +
14517 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
14518 +{
14519 +       int err;
14520 +       struct au_hnotify *hn;
14521 +
14522 +       err = -ENOMEM;
14523 +       hn = au_cache_alloc_hnotify();
14524 +       if (hn) {
14525 +               hn->hn_aufs_inode = inode;
14526 +               hinode->hi_notify = hn;
14527 +               err = au_hnotify_op.alloc(hinode);
14528 +               AuTraceErr(err);
14529 +               if (unlikely(err)) {
14530 +                       hinode->hi_notify = NULL;
14531 +                       au_cache_free_hnotify(hn);
14532 +                       /*
14533 +                        * The upper dir was removed by udba, but the same named
14534 +                        * dir left. In this case, aufs assignes a new inode
14535 +                        * number and set the monitor again.
14536 +                        * For the lower dir, the old monitnor is still left.
14537 +                        */
14538 +                       if (err == -EEXIST)
14539 +                               err = 0;
14540 +               }
14541 +       }
14542 +
14543 +       AuTraceErr(err);
14544 +       return err;
14545 +}
14546 +
14547 +void au_hn_free(struct au_hinode *hinode)
14548 +{
14549 +       struct au_hnotify *hn;
14550 +
14551 +       hn = hinode->hi_notify;
14552 +       if (hn) {
14553 +               hinode->hi_notify = NULL;
14554 +               if (au_hnotify_op.free(hinode, hn))
14555 +                       au_cache_free_hnotify(hn);
14556 +       }
14557 +}
14558 +
14559 +/* ---------------------------------------------------------------------- */
14560 +
14561 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
14562 +{
14563 +       if (hinode->hi_notify)
14564 +               au_hnotify_op.ctl(hinode, do_set);
14565 +}
14566 +
14567 +void au_hn_reset(struct inode *inode, unsigned int flags)
14568 +{
14569 +       aufs_bindex_t bindex, bend;
14570 +       struct inode *hi;
14571 +       struct dentry *iwhdentry;
14572 +
14573 +       bend = au_ibend(inode);
14574 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
14575 +               hi = au_h_iptr(inode, bindex);
14576 +               if (!hi)
14577 +                       continue;
14578 +
14579 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
14580 +               iwhdentry = au_hi_wh(inode, bindex);
14581 +               if (iwhdentry)
14582 +                       dget(iwhdentry);
14583 +               au_igrab(hi);
14584 +               au_set_h_iptr(inode, bindex, NULL, 0);
14585 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
14586 +                             flags & ~AuHi_XINO);
14587 +               iput(hi);
14588 +               dput(iwhdentry);
14589 +               /* mutex_unlock(&hi->i_mutex); */
14590 +       }
14591 +}
14592 +
14593 +/* ---------------------------------------------------------------------- */
14594 +
14595 +static int hn_xino(struct inode *inode, struct inode *h_inode)
14596 +{
14597 +       int err;
14598 +       aufs_bindex_t bindex, bend, bfound, bstart;
14599 +       struct inode *h_i;
14600 +
14601 +       err = 0;
14602 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14603 +               pr_warn("branch root dir was changed\n");
14604 +               goto out;
14605 +       }
14606 +
14607 +       bfound = -1;
14608 +       bend = au_ibend(inode);
14609 +       bstart = au_ibstart(inode);
14610 +#if 0 /* reserved for future use */
14611 +       if (bindex == bend) {
14612 +               /* keep this ino in rename case */
14613 +               goto out;
14614 +       }
14615 +#endif
14616 +       for (bindex = bstart; bindex <= bend; bindex++)
14617 +               if (au_h_iptr(inode, bindex) == h_inode) {
14618 +                       bfound = bindex;
14619 +                       break;
14620 +               }
14621 +       if (bfound < 0)
14622 +               goto out;
14623 +
14624 +       for (bindex = bstart; bindex <= bend; bindex++) {
14625 +               h_i = au_h_iptr(inode, bindex);
14626 +               if (!h_i)
14627 +                       continue;
14628 +
14629 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
14630 +               /* ignore this error */
14631 +               /* bad action? */
14632 +       }
14633 +
14634 +       /* children inode number will be broken */
14635 +
14636 +out:
14637 +       AuTraceErr(err);
14638 +       return err;
14639 +}
14640 +
14641 +static int hn_gen_tree(struct dentry *dentry)
14642 +{
14643 +       int err, i, j, ndentry;
14644 +       struct au_dcsub_pages dpages;
14645 +       struct au_dpage *dpage;
14646 +       struct dentry **dentries;
14647 +
14648 +       err = au_dpages_init(&dpages, GFP_NOFS);
14649 +       if (unlikely(err))
14650 +               goto out;
14651 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
14652 +       if (unlikely(err))
14653 +               goto out_dpages;
14654 +
14655 +       for (i = 0; i < dpages.ndpage; i++) {
14656 +               dpage = dpages.dpages + i;
14657 +               dentries = dpage->dentries;
14658 +               ndentry = dpage->ndentry;
14659 +               for (j = 0; j < ndentry; j++) {
14660 +                       struct dentry *d;
14661 +
14662 +                       d = dentries[j];
14663 +                       if (IS_ROOT(d))
14664 +                               continue;
14665 +
14666 +                       au_digen_dec(d);
14667 +                       if (d->d_inode)
14668 +                               /* todo: reset children xino?
14669 +                                  cached children only? */
14670 +                               au_iigen_dec(d->d_inode);
14671 +               }
14672 +       }
14673 +
14674 +out_dpages:
14675 +       au_dpages_free(&dpages);
14676 +
14677 +#if 0
14678 +       /* discard children */
14679 +       dentry_unhash(dentry);
14680 +       dput(dentry);
14681 +#endif
14682 +out:
14683 +       return err;
14684 +}
14685 +
14686 +/*
14687 + * return 0 if processed.
14688 + */
14689 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
14690 +                          const unsigned int isdir)
14691 +{
14692 +       int err;
14693 +       struct dentry *d;
14694 +       struct qstr *dname;
14695 +
14696 +       err = 1;
14697 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14698 +               pr_warn("branch root dir was changed\n");
14699 +               err = 0;
14700 +               goto out;
14701 +       }
14702 +
14703 +       if (!isdir) {
14704 +               AuDebugOn(!name);
14705 +               au_iigen_dec(inode);
14706 +               spin_lock(&inode->i_lock);
14707 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
14708 +                       spin_lock(&d->d_lock);
14709 +                       dname = &d->d_name;
14710 +                       if (dname->len != nlen
14711 +                           && memcmp(dname->name, name, nlen)) {
14712 +                               spin_unlock(&d->d_lock);
14713 +                               continue;
14714 +                       }
14715 +                       err = 0;
14716 +                       au_digen_dec(d);
14717 +                       spin_unlock(&d->d_lock);
14718 +                       break;
14719 +               }
14720 +               spin_unlock(&inode->i_lock);
14721 +       } else {
14722 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
14723 +               d = d_find_any_alias(inode);
14724 +               if (!d) {
14725 +                       au_iigen_dec(inode);
14726 +                       goto out;
14727 +               }
14728 +
14729 +               spin_lock(&d->d_lock);
14730 +               dname = &d->d_name;
14731 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
14732 +                       spin_unlock(&d->d_lock);
14733 +                       err = hn_gen_tree(d);
14734 +                       spin_lock(&d->d_lock);
14735 +               }
14736 +               spin_unlock(&d->d_lock);
14737 +               dput(d);
14738 +       }
14739 +
14740 +out:
14741 +       AuTraceErr(err);
14742 +       return err;
14743 +}
14744 +
14745 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
14746 +{
14747 +       int err;
14748 +       struct inode *inode;
14749 +
14750 +       inode = dentry->d_inode;
14751 +       if (IS_ROOT(dentry)
14752 +           /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
14753 +               ) {
14754 +               pr_warn("branch root dir was changed\n");
14755 +               return 0;
14756 +       }
14757 +
14758 +       err = 0;
14759 +       if (!isdir) {
14760 +               au_digen_dec(dentry);
14761 +               if (inode)
14762 +                       au_iigen_dec(inode);
14763 +       } else {
14764 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
14765 +               if (inode)
14766 +                       err = hn_gen_tree(dentry);
14767 +       }
14768 +
14769 +       AuTraceErr(err);
14770 +       return err;
14771 +}
14772 +
14773 +/* ---------------------------------------------------------------------- */
14774 +
14775 +/* hnotify job flags */
14776 +#define AuHnJob_XINO0          1
14777 +#define AuHnJob_GEN            (1 << 1)
14778 +#define AuHnJob_DIRENT         (1 << 2)
14779 +#define AuHnJob_ISDIR          (1 << 3)
14780 +#define AuHnJob_TRYXINO0       (1 << 4)
14781 +#define AuHnJob_MNTPNT         (1 << 5)
14782 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
14783 +#define au_fset_hnjob(flags, name) \
14784 +       do { (flags) |= AuHnJob_##name; } while (0)
14785 +#define au_fclr_hnjob(flags, name) \
14786 +       do { (flags) &= ~AuHnJob_##name; } while (0)
14787 +
14788 +enum {
14789 +       AuHn_CHILD,
14790 +       AuHn_PARENT,
14791 +       AuHnLast
14792 +};
14793 +
14794 +struct au_hnotify_args {
14795 +       struct inode *h_dir, *dir, *h_child_inode;
14796 +       u32 mask;
14797 +       unsigned int flags[AuHnLast];
14798 +       unsigned int h_child_nlen;
14799 +       char h_child_name[];
14800 +};
14801 +
14802 +struct hn_job_args {
14803 +       unsigned int flags;
14804 +       struct inode *inode, *h_inode, *dir, *h_dir;
14805 +       struct dentry *dentry;
14806 +       char *h_name;
14807 +       int h_nlen;
14808 +};
14809 +
14810 +static int hn_job(struct hn_job_args *a)
14811 +{
14812 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
14813 +       int e;
14814 +
14815 +       /* reset xino */
14816 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
14817 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
14818 +
14819 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
14820 +           && a->inode
14821 +           && a->h_inode) {
14822 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
14823 +               if (!a->h_inode->i_nlink
14824 +                   && !(a->h_inode->i_state & I_LINKABLE))
14825 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
14826 +               mutex_unlock(&a->h_inode->i_mutex);
14827 +       }
14828 +
14829 +       /* make the generation obsolete */
14830 +       if (au_ftest_hnjob(a->flags, GEN)) {
14831 +               e = -1;
14832 +               if (a->inode)
14833 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
14834 +                                             isdir);
14835 +               if (e && a->dentry)
14836 +                       hn_gen_by_name(a->dentry, isdir);
14837 +               /* ignore this error */
14838 +       }
14839 +
14840 +       /* make dir entries obsolete */
14841 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
14842 +               struct au_vdir *vdir;
14843 +
14844 +               vdir = au_ivdir(a->inode);
14845 +               if (vdir)
14846 +                       vdir->vd_jiffy = 0;
14847 +               /* IMustLock(a->inode); */
14848 +               /* a->inode->i_version++; */
14849 +       }
14850 +
14851 +       /* can do nothing but warn */
14852 +       if (au_ftest_hnjob(a->flags, MNTPNT)
14853 +           && a->dentry
14854 +           && d_mountpoint(a->dentry))
14855 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
14856 +
14857 +       return 0;
14858 +}
14859 +
14860 +/* ---------------------------------------------------------------------- */
14861 +
14862 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
14863 +                                          struct inode *dir)
14864 +{
14865 +       struct dentry *dentry, *d, *parent;
14866 +       struct qstr *dname;
14867 +
14868 +       parent = d_find_any_alias(dir);
14869 +       if (!parent)
14870 +               return NULL;
14871 +
14872 +       dentry = NULL;
14873 +       spin_lock(&parent->d_lock);
14874 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
14875 +               /* AuDbg("%pd\n", d); */
14876 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
14877 +               dname = &d->d_name;
14878 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
14879 +                       goto cont_unlock;
14880 +               if (au_di(d))
14881 +                       au_digen_dec(d);
14882 +               else
14883 +                       goto cont_unlock;
14884 +               if (au_dcount(d) > 0) {
14885 +                       dentry = dget_dlock(d);
14886 +                       spin_unlock(&d->d_lock);
14887 +                       break;
14888 +               }
14889 +
14890 +cont_unlock:
14891 +               spin_unlock(&d->d_lock);
14892 +       }
14893 +       spin_unlock(&parent->d_lock);
14894 +       dput(parent);
14895 +
14896 +       if (dentry)
14897 +               di_write_lock_child(dentry);
14898 +
14899 +       return dentry;
14900 +}
14901 +
14902 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
14903 +                                        aufs_bindex_t bindex, ino_t h_ino)
14904 +{
14905 +       struct inode *inode;
14906 +       ino_t ino;
14907 +       int err;
14908 +
14909 +       inode = NULL;
14910 +       err = au_xino_read(sb, bindex, h_ino, &ino);
14911 +       if (!err && ino)
14912 +               inode = ilookup(sb, ino);
14913 +       if (!inode)
14914 +               goto out;
14915 +
14916 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14917 +               pr_warn("wrong root branch\n");
14918 +               iput(inode);
14919 +               inode = NULL;
14920 +               goto out;
14921 +       }
14922 +
14923 +       ii_write_lock_child(inode);
14924 +
14925 +out:
14926 +       return inode;
14927 +}
14928 +
14929 +static void au_hn_bh(void *_args)
14930 +{
14931 +       struct au_hnotify_args *a = _args;
14932 +       struct super_block *sb;
14933 +       aufs_bindex_t bindex, bend, bfound;
14934 +       unsigned char xino, try_iput;
14935 +       int err;
14936 +       struct inode *inode;
14937 +       ino_t h_ino;
14938 +       struct hn_job_args args;
14939 +       struct dentry *dentry;
14940 +       struct au_sbinfo *sbinfo;
14941 +
14942 +       AuDebugOn(!_args);
14943 +       AuDebugOn(!a->h_dir);
14944 +       AuDebugOn(!a->dir);
14945 +       AuDebugOn(!a->mask);
14946 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
14947 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
14948 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
14949 +
14950 +       inode = NULL;
14951 +       dentry = NULL;
14952 +       /*
14953 +        * do not lock a->dir->i_mutex here
14954 +        * because of d_revalidate() may cause a deadlock.
14955 +        */
14956 +       sb = a->dir->i_sb;
14957 +       AuDebugOn(!sb);
14958 +       sbinfo = au_sbi(sb);
14959 +       AuDebugOn(!sbinfo);
14960 +       si_write_lock(sb, AuLock_NOPLMW);
14961 +
14962 +       ii_read_lock_parent(a->dir);
14963 +       bfound = -1;
14964 +       bend = au_ibend(a->dir);
14965 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
14966 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
14967 +                       bfound = bindex;
14968 +                       break;
14969 +               }
14970 +       ii_read_unlock(a->dir);
14971 +       if (unlikely(bfound < 0))
14972 +               goto out;
14973 +
14974 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
14975 +       h_ino = 0;
14976 +       if (a->h_child_inode)
14977 +               h_ino = a->h_child_inode->i_ino;
14978 +
14979 +       if (a->h_child_nlen
14980 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
14981 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
14982 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
14983 +                                             a->dir);
14984 +       try_iput = 0;
14985 +       if (dentry)
14986 +               inode = dentry->d_inode;
14987 +       if (xino && !inode && h_ino
14988 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
14989 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
14990 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
14991 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
14992 +               try_iput = 1;
14993 +           }
14994 +
14995 +       args.flags = a->flags[AuHn_CHILD];
14996 +       args.dentry = dentry;
14997 +       args.inode = inode;
14998 +       args.h_inode = a->h_child_inode;
14999 +       args.dir = a->dir;
15000 +       args.h_dir = a->h_dir;
15001 +       args.h_name = a->h_child_name;
15002 +       args.h_nlen = a->h_child_nlen;
15003 +       err = hn_job(&args);
15004 +       if (dentry) {
15005 +               if (au_di(dentry))
15006 +                       di_write_unlock(dentry);
15007 +               dput(dentry);
15008 +       }
15009 +       if (inode && try_iput) {
15010 +               ii_write_unlock(inode);
15011 +               iput(inode);
15012 +       }
15013 +
15014 +       ii_write_lock_parent(a->dir);
15015 +       args.flags = a->flags[AuHn_PARENT];
15016 +       args.dentry = NULL;
15017 +       args.inode = a->dir;
15018 +       args.h_inode = a->h_dir;
15019 +       args.dir = NULL;
15020 +       args.h_dir = NULL;
15021 +       args.h_name = NULL;
15022 +       args.h_nlen = 0;
15023 +       err = hn_job(&args);
15024 +       ii_write_unlock(a->dir);
15025 +
15026 +out:
15027 +       iput(a->h_child_inode);
15028 +       iput(a->h_dir);
15029 +       iput(a->dir);
15030 +       si_write_unlock(sb);
15031 +       au_nwt_done(&sbinfo->si_nowait);
15032 +       kfree(a);
15033 +}
15034 +
15035 +/* ---------------------------------------------------------------------- */
15036 +
15037 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15038 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
15039 +{
15040 +       int err, len;
15041 +       unsigned int flags[AuHnLast], f;
15042 +       unsigned char isdir, isroot, wh;
15043 +       struct inode *dir;
15044 +       struct au_hnotify_args *args;
15045 +       char *p, *h_child_name;
15046 +
15047 +       err = 0;
15048 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
15049 +       dir = igrab(hnotify->hn_aufs_inode);
15050 +       if (!dir)
15051 +               goto out;
15052 +
15053 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
15054 +       wh = 0;
15055 +       h_child_name = (void *)h_child_qstr->name;
15056 +       len = h_child_qstr->len;
15057 +       if (h_child_name) {
15058 +               if (len > AUFS_WH_PFX_LEN
15059 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
15060 +                       h_child_name += AUFS_WH_PFX_LEN;
15061 +                       len -= AUFS_WH_PFX_LEN;
15062 +                       wh = 1;
15063 +               }
15064 +       }
15065 +
15066 +       isdir = 0;
15067 +       if (h_child_inode)
15068 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
15069 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
15070 +       flags[AuHn_CHILD] = 0;
15071 +       if (isdir)
15072 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
15073 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
15074 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
15075 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
15076 +       case FS_MOVED_FROM:
15077 +       case FS_MOVED_TO:
15078 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
15079 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15080 +               /*FALLTHROUGH*/
15081 +       case FS_CREATE:
15082 +               AuDebugOn(!h_child_name);
15083 +               break;
15084 +
15085 +       case FS_DELETE:
15086 +               /*
15087 +                * aufs never be able to get this child inode.
15088 +                * revalidation should be in d_revalidate()
15089 +                * by checking i_nlink, i_generation or d_unhashed().
15090 +                */
15091 +               AuDebugOn(!h_child_name);
15092 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
15093 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15094 +               break;
15095 +
15096 +       default:
15097 +               AuDebugOn(1);
15098 +       }
15099 +
15100 +       if (wh)
15101 +               h_child_inode = NULL;
15102 +
15103 +       err = -ENOMEM;
15104 +       /* iput() and kfree() will be called in au_hnotify() */
15105 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
15106 +       if (unlikely(!args)) {
15107 +               AuErr1("no memory\n");
15108 +               iput(dir);
15109 +               goto out;
15110 +       }
15111 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
15112 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
15113 +       args->mask = mask;
15114 +       args->dir = dir;
15115 +       args->h_dir = igrab(h_dir);
15116 +       if (h_child_inode)
15117 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
15118 +       args->h_child_inode = h_child_inode;
15119 +       args->h_child_nlen = len;
15120 +       if (len) {
15121 +               p = (void *)args;
15122 +               p += sizeof(*args);
15123 +               memcpy(p, h_child_name, len);
15124 +               p[len] = 0;
15125 +       }
15126 +
15127 +       /* NFS fires the event for silly-renamed one from kworker */
15128 +       f = 0;
15129 +       if (!dir->i_nlink
15130 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
15131 +               f = AuWkq_NEST;
15132 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
15133 +       if (unlikely(err)) {
15134 +               pr_err("wkq %d\n", err);
15135 +               iput(args->h_child_inode);
15136 +               iput(args->h_dir);
15137 +               iput(args->dir);
15138 +               kfree(args);
15139 +       }
15140 +
15141 +out:
15142 +       return err;
15143 +}
15144 +
15145 +/* ---------------------------------------------------------------------- */
15146 +
15147 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
15148 +{
15149 +       int err;
15150 +
15151 +       AuDebugOn(!(udba & AuOptMask_UDBA));
15152 +
15153 +       err = 0;
15154 +       if (au_hnotify_op.reset_br)
15155 +               err = au_hnotify_op.reset_br(udba, br, perm);
15156 +
15157 +       return err;
15158 +}
15159 +
15160 +int au_hnotify_init_br(struct au_branch *br, int perm)
15161 +{
15162 +       int err;
15163 +
15164 +       err = 0;
15165 +       if (au_hnotify_op.init_br)
15166 +               err = au_hnotify_op.init_br(br, perm);
15167 +
15168 +       return err;
15169 +}
15170 +
15171 +void au_hnotify_fin_br(struct au_branch *br)
15172 +{
15173 +       if (au_hnotify_op.fin_br)
15174 +               au_hnotify_op.fin_br(br);
15175 +}
15176 +
15177 +static void au_hn_destroy_cache(void)
15178 +{
15179 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
15180 +       au_cachep[AuCache_HNOTIFY] = NULL;
15181 +}
15182 +
15183 +int __init au_hnotify_init(void)
15184 +{
15185 +       int err;
15186 +
15187 +       err = -ENOMEM;
15188 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
15189 +       if (au_cachep[AuCache_HNOTIFY]) {
15190 +               err = 0;
15191 +               if (au_hnotify_op.init)
15192 +                       err = au_hnotify_op.init();
15193 +               if (unlikely(err))
15194 +                       au_hn_destroy_cache();
15195 +       }
15196 +       AuTraceErr(err);
15197 +       return err;
15198 +}
15199 +
15200 +void au_hnotify_fin(void)
15201 +{
15202 +       if (au_hnotify_op.fin)
15203 +               au_hnotify_op.fin();
15204 +       /* cf. au_cache_fin() */
15205 +       if (au_cachep[AuCache_HNOTIFY])
15206 +               au_hn_destroy_cache();
15207 +}
15208 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
15209 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
15210 +++ linux/fs/aufs/iinfo.c       2015-04-13 15:10:20.786823613 +0200
15211 @@ -0,0 +1,277 @@
15212 +/*
15213 + * Copyright (C) 2005-2015 Junjiro R. Okajima
15214 + *
15215 + * This program, aufs is free software; you can redistribute it and/or modify
15216 + * it under the terms of the GNU General Public License as published by
15217 + * the Free Software Foundation; either version 2 of the License, or
15218 + * (at your option) any later version.
15219 + *
15220 + * This program is distributed in the hope that it will be useful,
15221 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15222 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15223 + * GNU General Public License for more details.
15224 + *
15225 + * You should have received a copy of the GNU General Public License
15226 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15227 + */
15228 +
15229 +/*
15230 + * inode private data
15231 + */
15232 +
15233 +#include "aufs.h"
15234 +
15235 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
15236 +{
15237 +       struct inode *h_inode;
15238 +
15239 +       IiMustAnyLock(inode);
15240 +
15241 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
15242 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15243 +       return h_inode;
15244 +}
15245 +
15246 +/* todo: hard/soft set? */
15247 +void au_hiput(struct au_hinode *hinode)
15248 +{
15249 +       au_hn_free(hinode);
15250 +       dput(hinode->hi_whdentry);
15251 +       iput(hinode->hi_inode);
15252 +}
15253 +
15254 +unsigned int au_hi_flags(struct inode *inode, int isdir)
15255 +{
15256 +       unsigned int flags;
15257 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
15258 +
15259 +       flags = 0;
15260 +       if (au_opt_test(mnt_flags, XINO))
15261 +               au_fset_hi(flags, XINO);
15262 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
15263 +               au_fset_hi(flags, HNOTIFY);
15264 +       return flags;
15265 +}
15266 +
15267 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15268 +                  struct inode *h_inode, unsigned int flags)
15269 +{
15270 +       struct au_hinode *hinode;
15271 +       struct inode *hi;
15272 +       struct au_iinfo *iinfo = au_ii(inode);
15273 +
15274 +       IiMustWriteLock(inode);
15275 +
15276 +       hinode = iinfo->ii_hinode + bindex;
15277 +       hi = hinode->hi_inode;
15278 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15279 +
15280 +       if (hi)
15281 +               au_hiput(hinode);
15282 +       hinode->hi_inode = h_inode;
15283 +       if (h_inode) {
15284 +               int err;
15285 +               struct super_block *sb = inode->i_sb;
15286 +               struct au_branch *br;
15287 +
15288 +               AuDebugOn(inode->i_mode
15289 +                         && (h_inode->i_mode & S_IFMT)
15290 +                         != (inode->i_mode & S_IFMT));
15291 +               if (bindex == iinfo->ii_bstart)
15292 +                       au_cpup_igen(inode, h_inode);
15293 +               br = au_sbr(sb, bindex);
15294 +               hinode->hi_id = br->br_id;
15295 +               if (au_ftest_hi(flags, XINO)) {
15296 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
15297 +                                           inode->i_ino);
15298 +                       if (unlikely(err))
15299 +                               AuIOErr1("failed au_xino_write() %d\n", err);
15300 +               }
15301 +
15302 +               if (au_ftest_hi(flags, HNOTIFY)
15303 +                   && au_br_hnotifyable(br->br_perm)) {
15304 +                       err = au_hn_alloc(hinode, inode);
15305 +                       if (unlikely(err))
15306 +                               AuIOErr1("au_hn_alloc() %d\n", err);
15307 +               }
15308 +       }
15309 +}
15310 +
15311 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15312 +                 struct dentry *h_wh)
15313 +{
15314 +       struct au_hinode *hinode;
15315 +
15316 +       IiMustWriteLock(inode);
15317 +
15318 +       hinode = au_ii(inode)->ii_hinode + bindex;
15319 +       AuDebugOn(hinode->hi_whdentry);
15320 +       hinode->hi_whdentry = h_wh;
15321 +}
15322 +
15323 +void au_update_iigen(struct inode *inode, int half)
15324 +{
15325 +       struct au_iinfo *iinfo;
15326 +       struct au_iigen *iigen;
15327 +       unsigned int sigen;
15328 +
15329 +       sigen = au_sigen(inode->i_sb);
15330 +       iinfo = au_ii(inode);
15331 +       iigen = &iinfo->ii_generation;
15332 +       spin_lock(&iinfo->ii_genspin);
15333 +       iigen->ig_generation = sigen;
15334 +       if (half)
15335 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
15336 +       else
15337 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
15338 +       spin_unlock(&iinfo->ii_genspin);
15339 +}
15340 +
15341 +/* it may be called at remount time, too */
15342 +void au_update_ibrange(struct inode *inode, int do_put_zero)
15343 +{
15344 +       struct au_iinfo *iinfo;
15345 +       aufs_bindex_t bindex, bend;
15346 +
15347 +       iinfo = au_ii(inode);
15348 +       if (!iinfo)
15349 +               return;
15350 +
15351 +       IiMustWriteLock(inode);
15352 +
15353 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
15354 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15355 +                    bindex++) {
15356 +                       struct inode *h_i;
15357 +
15358 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
15359 +                       if (h_i
15360 +                           && !h_i->i_nlink
15361 +                           && !(h_i->i_state & I_LINKABLE))
15362 +                               au_set_h_iptr(inode, bindex, NULL, 0);
15363 +               }
15364 +       }
15365 +
15366 +       iinfo->ii_bstart = -1;
15367 +       iinfo->ii_bend = -1;
15368 +       bend = au_sbend(inode->i_sb);
15369 +       for (bindex = 0; bindex <= bend; bindex++)
15370 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15371 +                       iinfo->ii_bstart = bindex;
15372 +                       break;
15373 +               }
15374 +       if (iinfo->ii_bstart >= 0)
15375 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
15376 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15377 +                               iinfo->ii_bend = bindex;
15378 +                               break;
15379 +                       }
15380 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
15381 +}
15382 +
15383 +/* ---------------------------------------------------------------------- */
15384 +
15385 +void au_icntnr_init_once(void *_c)
15386 +{
15387 +       struct au_icntnr *c = _c;
15388 +       struct au_iinfo *iinfo = &c->iinfo;
15389 +       static struct lock_class_key aufs_ii;
15390 +
15391 +       spin_lock_init(&iinfo->ii_genspin);
15392 +       au_rw_init(&iinfo->ii_rwsem);
15393 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
15394 +       inode_init_once(&c->vfs_inode);
15395 +}
15396 +
15397 +int au_iinfo_init(struct inode *inode)
15398 +{
15399 +       struct au_iinfo *iinfo;
15400 +       struct super_block *sb;
15401 +       int nbr, i;
15402 +
15403 +       sb = inode->i_sb;
15404 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15405 +       nbr = au_sbend(sb) + 1;
15406 +       if (unlikely(nbr <= 0))
15407 +               nbr = 1;
15408 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
15409 +       if (iinfo->ii_hinode) {
15410 +               au_ninodes_inc(sb);
15411 +               for (i = 0; i < nbr; i++)
15412 +                       iinfo->ii_hinode[i].hi_id = -1;
15413 +
15414 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
15415 +               iinfo->ii_bstart = -1;
15416 +               iinfo->ii_bend = -1;
15417 +               iinfo->ii_vdir = NULL;
15418 +               return 0;
15419 +       }
15420 +       return -ENOMEM;
15421 +}
15422 +
15423 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
15424 +{
15425 +       int err, sz;
15426 +       struct au_hinode *hip;
15427 +
15428 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
15429 +
15430 +       err = -ENOMEM;
15431 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
15432 +       if (!sz)
15433 +               sz = sizeof(*hip);
15434 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
15435 +       if (hip) {
15436 +               iinfo->ii_hinode = hip;
15437 +               err = 0;
15438 +       }
15439 +
15440 +       return err;
15441 +}
15442 +
15443 +void au_iinfo_fin(struct inode *inode)
15444 +{
15445 +       struct au_iinfo *iinfo;
15446 +       struct au_hinode *hi;
15447 +       struct super_block *sb;
15448 +       aufs_bindex_t bindex, bend;
15449 +       const unsigned char unlinked = !inode->i_nlink;
15450 +
15451 +       iinfo = au_ii(inode);
15452 +       /* bad_inode case */
15453 +       if (!iinfo)
15454 +               return;
15455 +
15456 +       sb = inode->i_sb;
15457 +       au_ninodes_dec(sb);
15458 +       if (si_pid_test(sb))
15459 +               au_xino_delete_inode(inode, unlinked);
15460 +       else {
15461 +               /*
15462 +                * it is safe to hide the dependency between sbinfo and
15463 +                * sb->s_umount.
15464 +                */
15465 +               lockdep_off();
15466 +               si_noflush_read_lock(sb);
15467 +               au_xino_delete_inode(inode, unlinked);
15468 +               si_read_unlock(sb);
15469 +               lockdep_on();
15470 +       }
15471 +
15472 +       if (iinfo->ii_vdir)
15473 +               au_vdir_free(iinfo->ii_vdir);
15474 +
15475 +       bindex = iinfo->ii_bstart;
15476 +       if (bindex >= 0) {
15477 +               hi = iinfo->ii_hinode + bindex;
15478 +               bend = iinfo->ii_bend;
15479 +               while (bindex++ <= bend) {
15480 +                       if (hi->hi_inode)
15481 +                               au_hiput(hi);
15482 +                       hi++;
15483 +               }
15484 +       }
15485 +       kfree(iinfo->ii_hinode);
15486 +       iinfo->ii_hinode = NULL;
15487 +       AuRwDestroy(&iinfo->ii_rwsem);
15488 +}
15489 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
15490 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
15491 +++ linux/fs/aufs/inode.c       2015-04-13 15:10:20.786823613 +0200
15492 @@ -0,0 +1,495 @@
15493 +/*
15494 + * Copyright (C) 2005-2015 Junjiro R. Okajima
15495 + *
15496 + * This program, aufs is free software; you can redistribute it and/or modify
15497 + * it under the terms of the GNU General Public License as published by
15498 + * the Free Software Foundation; either version 2 of the License, or
15499 + * (at your option) any later version.
15500 + *
15501 + * This program is distributed in the hope that it will be useful,
15502 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15503 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15504 + * GNU General Public License for more details.
15505 + *
15506 + * You should have received a copy of the GNU General Public License
15507 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15508 + */
15509 +
15510 +/*
15511 + * inode functions
15512 + */
15513 +
15514 +#include "aufs.h"
15515 +
15516 +struct inode *au_igrab(struct inode *inode)
15517 +{
15518 +       if (inode) {
15519 +               AuDebugOn(!atomic_read(&inode->i_count));
15520 +               ihold(inode);
15521 +       }
15522 +       return inode;
15523 +}
15524 +
15525 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
15526 +{
15527 +       au_cpup_attr_all(inode, /*force*/0);
15528 +       au_update_iigen(inode, /*half*/1);
15529 +       if (do_version)
15530 +               inode->i_version++;
15531 +}
15532 +
15533 +static int au_ii_refresh(struct inode *inode, int *update)
15534 +{
15535 +       int err, e;
15536 +       umode_t type;
15537 +       aufs_bindex_t bindex, new_bindex;
15538 +       struct super_block *sb;
15539 +       struct au_iinfo *iinfo;
15540 +       struct au_hinode *p, *q, tmp;
15541 +
15542 +       IiMustWriteLock(inode);
15543 +
15544 +       *update = 0;
15545 +       sb = inode->i_sb;
15546 +       type = inode->i_mode & S_IFMT;
15547 +       iinfo = au_ii(inode);
15548 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
15549 +       if (unlikely(err))
15550 +               goto out;
15551 +
15552 +       AuDebugOn(iinfo->ii_bstart < 0);
15553 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15554 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15555 +            bindex++, p++) {
15556 +               if (!p->hi_inode)
15557 +                       continue;
15558 +
15559 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
15560 +               new_bindex = au_br_index(sb, p->hi_id);
15561 +               if (new_bindex == bindex)
15562 +                       continue;
15563 +
15564 +               if (new_bindex < 0) {
15565 +                       *update = 1;
15566 +                       au_hiput(p);
15567 +                       p->hi_inode = NULL;
15568 +                       continue;
15569 +               }
15570 +
15571 +               if (new_bindex < iinfo->ii_bstart)
15572 +                       iinfo->ii_bstart = new_bindex;
15573 +               if (iinfo->ii_bend < new_bindex)
15574 +                       iinfo->ii_bend = new_bindex;
15575 +               /* swap two lower inode, and loop again */
15576 +               q = iinfo->ii_hinode + new_bindex;
15577 +               tmp = *q;
15578 +               *q = *p;
15579 +               *p = tmp;
15580 +               if (tmp.hi_inode) {
15581 +                       bindex--;
15582 +                       p--;
15583 +               }
15584 +       }
15585 +       au_update_ibrange(inode, /*do_put_zero*/0);
15586 +       e = au_dy_irefresh(inode);
15587 +       if (unlikely(e && !err))
15588 +               err = e;
15589 +
15590 +out:
15591 +       AuTraceErr(err);
15592 +       return err;
15593 +}
15594 +
15595 +int au_refresh_hinode_self(struct inode *inode)
15596 +{
15597 +       int err, update;
15598 +
15599 +       err = au_ii_refresh(inode, &update);
15600 +       if (!err)
15601 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
15602 +
15603 +       AuTraceErr(err);
15604 +       return err;
15605 +}
15606 +
15607 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
15608 +{
15609 +       int err, e, update;
15610 +       unsigned int flags;
15611 +       umode_t mode;
15612 +       aufs_bindex_t bindex, bend;
15613 +       unsigned char isdir;
15614 +       struct au_hinode *p;
15615 +       struct au_iinfo *iinfo;
15616 +
15617 +       err = au_ii_refresh(inode, &update);
15618 +       if (unlikely(err))
15619 +               goto out;
15620 +
15621 +       update = 0;
15622 +       iinfo = au_ii(inode);
15623 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15624 +       mode = (inode->i_mode & S_IFMT);
15625 +       isdir = S_ISDIR(mode);
15626 +       flags = au_hi_flags(inode, isdir);
15627 +       bend = au_dbend(dentry);
15628 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
15629 +               struct inode *h_i;
15630 +               struct dentry *h_d;
15631 +
15632 +               h_d = au_h_dptr(dentry, bindex);
15633 +               if (!h_d || !h_d->d_inode)
15634 +                       continue;
15635 +
15636 +               AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
15637 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
15638 +                       h_i = au_h_iptr(inode, bindex);
15639 +                       if (h_i) {
15640 +                               if (h_i == h_d->d_inode)
15641 +                                       continue;
15642 +                               err = -EIO;
15643 +                               break;
15644 +                       }
15645 +               }
15646 +               if (bindex < iinfo->ii_bstart)
15647 +                       iinfo->ii_bstart = bindex;
15648 +               if (iinfo->ii_bend < bindex)
15649 +                       iinfo->ii_bend = bindex;
15650 +               au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
15651 +               update = 1;
15652 +       }
15653 +       au_update_ibrange(inode, /*do_put_zero*/0);
15654 +       e = au_dy_irefresh(inode);
15655 +       if (unlikely(e && !err))
15656 +               err = e;
15657 +       if (!err)
15658 +               au_refresh_hinode_attr(inode, update && isdir);
15659 +
15660 +out:
15661 +       AuTraceErr(err);
15662 +       return err;
15663 +}
15664 +
15665 +static int set_inode(struct inode *inode, struct dentry *dentry)
15666 +{
15667 +       int err;
15668 +       unsigned int flags;
15669 +       umode_t mode;
15670 +       aufs_bindex_t bindex, bstart, btail;
15671 +       unsigned char isdir;
15672 +       struct dentry *h_dentry;
15673 +       struct inode *h_inode;
15674 +       struct au_iinfo *iinfo;
15675 +
15676 +       IiMustWriteLock(inode);
15677 +
15678 +       err = 0;
15679 +       isdir = 0;
15680 +       bstart = au_dbstart(dentry);
15681 +       h_inode = au_h_dptr(dentry, bstart)->d_inode;
15682 +       mode = h_inode->i_mode;
15683 +       switch (mode & S_IFMT) {
15684 +       case S_IFREG:
15685 +               btail = au_dbtail(dentry);
15686 +               inode->i_op = &aufs_iop;
15687 +               inode->i_fop = &aufs_file_fop;
15688 +               err = au_dy_iaop(inode, bstart, h_inode);
15689 +               if (unlikely(err))
15690 +                       goto out;
15691 +               break;
15692 +       case S_IFDIR:
15693 +               isdir = 1;
15694 +               btail = au_dbtaildir(dentry);
15695 +               inode->i_op = &aufs_dir_iop;
15696 +               inode->i_fop = &aufs_dir_fop;
15697 +               break;
15698 +       case S_IFLNK:
15699 +               btail = au_dbtail(dentry);
15700 +               inode->i_op = &aufs_symlink_iop;
15701 +               break;
15702 +       case S_IFBLK:
15703 +       case S_IFCHR:
15704 +       case S_IFIFO:
15705 +       case S_IFSOCK:
15706 +               btail = au_dbtail(dentry);
15707 +               inode->i_op = &aufs_iop;
15708 +               init_special_inode(inode, mode, h_inode->i_rdev);
15709 +               break;
15710 +       default:
15711 +               AuIOErr("Unknown file type 0%o\n", mode);
15712 +               err = -EIO;
15713 +               goto out;
15714 +       }
15715 +
15716 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
15717 +       flags = au_hi_flags(inode, isdir);
15718 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
15719 +           && au_ftest_hi(flags, HNOTIFY)
15720 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
15721 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
15722 +               au_fclr_hi(flags, HNOTIFY);
15723 +       iinfo = au_ii(inode);
15724 +       iinfo->ii_bstart = bstart;
15725 +       iinfo->ii_bend = btail;
15726 +       for (bindex = bstart; bindex <= btail; bindex++) {
15727 +               h_dentry = au_h_dptr(dentry, bindex);
15728 +               if (h_dentry)
15729 +                       au_set_h_iptr(inode, bindex,
15730 +                                     au_igrab(h_dentry->d_inode), flags);
15731 +       }
15732 +       au_cpup_attr_all(inode, /*force*/1);
15733 +       /*
15734 +        * to force calling aufs_get_acl() every time,
15735 +        * do not call cache_no_acl() for aufs inode.
15736 +        */
15737 +
15738 +out:
15739 +       return err;
15740 +}
15741 +
15742 +/*
15743 + * successful returns with iinfo write_locked
15744 + * minus: errno
15745 + * zero: success, matched
15746 + * plus: no error, but unmatched
15747 + */
15748 +static int reval_inode(struct inode *inode, struct dentry *dentry)
15749 +{
15750 +       int err;
15751 +       unsigned int gen;
15752 +       struct au_iigen iigen;
15753 +       aufs_bindex_t bindex, bend;
15754 +       struct inode *h_inode, *h_dinode;
15755 +
15756 +       /*
15757 +        * before this function, if aufs got any iinfo lock, it must be only
15758 +        * one, the parent dir.
15759 +        * it can happen by UDBA and the obsoleted inode number.
15760 +        */
15761 +       err = -EIO;
15762 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
15763 +               goto out;
15764 +
15765 +       err = 1;
15766 +       ii_write_lock_new_child(inode);
15767 +       h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
15768 +       bend = au_ibend(inode);
15769 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
15770 +               h_inode = au_h_iptr(inode, bindex);
15771 +               if (!h_inode || h_inode != h_dinode)
15772 +                       continue;
15773 +
15774 +               err = 0;
15775 +               gen = au_iigen(inode, &iigen);
15776 +               if (gen == au_digen(dentry)
15777 +                   && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
15778 +                       break;
15779 +
15780 +               /* fully refresh inode using dentry */
15781 +               err = au_refresh_hinode(inode, dentry);
15782 +               if (!err)
15783 +                       au_update_iigen(inode, /*half*/0);
15784 +               break;
15785 +       }
15786 +
15787 +       if (unlikely(err))
15788 +               ii_write_unlock(inode);
15789 +out:
15790 +       return err;
15791 +}
15792 +
15793 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
15794 +          unsigned int d_type, ino_t *ino)
15795 +{
15796 +       int err;
15797 +       struct mutex *mtx;
15798 +
15799 +       /* prevent hardlinked inode number from race condition */
15800 +       mtx = NULL;
15801 +       if (d_type != DT_DIR) {
15802 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
15803 +               mutex_lock(mtx);
15804 +       }
15805 +       err = au_xino_read(sb, bindex, h_ino, ino);
15806 +       if (unlikely(err))
15807 +               goto out;
15808 +
15809 +       if (!*ino) {
15810 +               err = -EIO;
15811 +               *ino = au_xino_new_ino(sb);
15812 +               if (unlikely(!*ino))
15813 +                       goto out;
15814 +               err = au_xino_write(sb, bindex, h_ino, *ino);
15815 +               if (unlikely(err))
15816 +                       goto out;
15817 +       }
15818 +
15819 +out:
15820 +       if (mtx)
15821 +               mutex_unlock(mtx);
15822 +       return err;
15823 +}
15824 +
15825 +/* successful returns with iinfo write_locked */
15826 +/* todo: return with unlocked? */
15827 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
15828 +{
15829 +       struct inode *inode;
15830 +       struct dentry *h_dentry;
15831 +       struct super_block *sb;
15832 +       struct mutex *mtx;
15833 +       ino_t h_ino, ino;
15834 +       int err;
15835 +       aufs_bindex_t bstart;
15836 +
15837 +       sb = dentry->d_sb;
15838 +       bstart = au_dbstart(dentry);
15839 +       h_dentry = au_h_dptr(dentry, bstart);
15840 +       h_ino = h_dentry->d_inode->i_ino;
15841 +
15842 +       /*
15843 +        * stop 'race'-ing between hardlinks under different
15844 +        * parents.
15845 +        */
15846 +       mtx = NULL;
15847 +       if (!d_is_dir(h_dentry))
15848 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
15849 +
15850 +new_ino:
15851 +       if (mtx)
15852 +               mutex_lock(mtx);
15853 +       err = au_xino_read(sb, bstart, h_ino, &ino);
15854 +       inode = ERR_PTR(err);
15855 +       if (unlikely(err))
15856 +               goto out;
15857 +
15858 +       if (!ino) {
15859 +               ino = au_xino_new_ino(sb);
15860 +               if (unlikely(!ino)) {
15861 +                       inode = ERR_PTR(-EIO);
15862 +                       goto out;
15863 +               }
15864 +       }
15865 +
15866 +       AuDbg("i%lu\n", (unsigned long)ino);
15867 +       inode = au_iget_locked(sb, ino);
15868 +       err = PTR_ERR(inode);
15869 +       if (IS_ERR(inode))
15870 +               goto out;
15871 +
15872 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
15873 +       if (inode->i_state & I_NEW) {
15874 +               /* verbose coding for lock class name */
15875 +               if (unlikely(d_is_symlink(h_dentry)))
15876 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
15877 +                                   au_lc_key + AuLcSymlink_IIINFO);
15878 +               else if (unlikely(d_is_dir(h_dentry)))
15879 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
15880 +                                   au_lc_key + AuLcDir_IIINFO);
15881 +               else /* likely */
15882 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
15883 +                                   au_lc_key + AuLcNonDir_IIINFO);
15884 +
15885 +               ii_write_lock_new_child(inode);
15886 +               err = set_inode(inode, dentry);
15887 +               if (!err) {
15888 +                       unlock_new_inode(inode);
15889 +                       goto out; /* success */
15890 +               }
15891 +
15892 +               /*
15893 +                * iget_failed() calls iput(), but we need to call
15894 +                * ii_write_unlock() after iget_failed(). so dirty hack for
15895 +                * i_count.
15896 +                */
15897 +               atomic_inc(&inode->i_count);
15898 +               iget_failed(inode);
15899 +               ii_write_unlock(inode);
15900 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
15901 +               /* ignore this error */
15902 +               goto out_iput;
15903 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
15904 +               /*
15905 +                * horrible race condition between lookup, readdir and copyup
15906 +                * (or something).
15907 +                */
15908 +               if (mtx)
15909 +                       mutex_unlock(mtx);
15910 +               err = reval_inode(inode, dentry);
15911 +               if (unlikely(err < 0)) {
15912 +                       mtx = NULL;
15913 +                       goto out_iput;
15914 +               }
15915 +
15916 +               if (!err) {
15917 +                       mtx = NULL;
15918 +                       goto out; /* success */
15919 +               } else if (mtx)
15920 +                       mutex_lock(mtx);
15921 +       }
15922 +
15923 +       if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
15924 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
15925 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
15926 +                       bstart, au_sbtype(h_dentry->d_sb), dentry,
15927 +                       (unsigned long)h_ino, (unsigned long)ino);
15928 +       ino = 0;
15929 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
15930 +       if (!err) {
15931 +               iput(inode);
15932 +               if (mtx)
15933 +                       mutex_unlock(mtx);
15934 +               goto new_ino;
15935 +       }
15936 +
15937 +out_iput:
15938 +       iput(inode);
15939 +       inode = ERR_PTR(err);
15940 +out:
15941 +       if (mtx)
15942 +               mutex_unlock(mtx);
15943 +       return inode;
15944 +}
15945 +
15946 +/* ---------------------------------------------------------------------- */
15947 +
15948 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
15949 +              struct inode *inode)
15950 +{
15951 +       int err;
15952 +       struct inode *hi;
15953 +
15954 +       err = au_br_rdonly(au_sbr(sb, bindex));
15955 +
15956 +       /* pseudo-link after flushed may happen out of bounds */
15957 +       if (!err
15958 +           && inode
15959 +           && au_ibstart(inode) <= bindex
15960 +           && bindex <= au_ibend(inode)) {
15961 +               /*
15962 +                * permission check is unnecessary since vfsub routine
15963 +                * will be called later
15964 +                */
15965 +               hi = au_h_iptr(inode, bindex);
15966 +               if (hi)
15967 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
15968 +       }
15969 +
15970 +       return err;
15971 +}
15972 +
15973 +int au_test_h_perm(struct inode *h_inode, int mask)
15974 +{
15975 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
15976 +               return 0;
15977 +       return inode_permission(h_inode, mask);
15978 +}
15979 +
15980 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
15981 +{
15982 +       if (au_test_nfs(h_inode->i_sb)
15983 +           && (mask & MAY_WRITE)
15984 +           && S_ISDIR(h_inode->i_mode))
15985 +               mask |= MAY_READ; /* force permission check */
15986 +       return au_test_h_perm(h_inode, mask);
15987 +}
15988 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
15989 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
15990 +++ linux/fs/aufs/inode.h       2015-04-13 15:10:20.786823613 +0200
15991 @@ -0,0 +1,670 @@
15992 +/*
15993 + * Copyright (C) 2005-2015 Junjiro R. Okajima
15994 + *
15995 + * This program, aufs is free software; you can redistribute it and/or modify
15996 + * it under the terms of the GNU General Public License as published by
15997 + * the Free Software Foundation; either version 2 of the License, or
15998 + * (at your option) any later version.
15999 + *
16000 + * This program is distributed in the hope that it will be useful,
16001 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16002 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16003 + * GNU General Public License for more details.
16004 + *
16005 + * You should have received a copy of the GNU General Public License
16006 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16007 + */
16008 +
16009 +/*
16010 + * inode operations
16011 + */
16012 +
16013 +#ifndef __AUFS_INODE_H__
16014 +#define __AUFS_INODE_H__
16015 +
16016 +#ifdef __KERNEL__
16017 +
16018 +#include <linux/fsnotify.h>
16019 +#include "rwsem.h"
16020 +
16021 +struct vfsmount;
16022 +
16023 +struct au_hnotify {
16024 +#ifdef CONFIG_AUFS_HNOTIFY
16025 +#ifdef CONFIG_AUFS_HFSNOTIFY
16026 +       /* never use fsnotify_add_vfsmount_mark() */
16027 +       struct fsnotify_mark            hn_mark;
16028 +#endif
16029 +       struct inode                    *hn_aufs_inode; /* no get/put */
16030 +#endif
16031 +} ____cacheline_aligned_in_smp;
16032 +
16033 +struct au_hinode {
16034 +       struct inode            *hi_inode;
16035 +       aufs_bindex_t           hi_id;
16036 +#ifdef CONFIG_AUFS_HNOTIFY
16037 +       struct au_hnotify       *hi_notify;
16038 +#endif
16039 +
16040 +       /* reference to the copied-up whiteout with get/put */
16041 +       struct dentry           *hi_whdentry;
16042 +};
16043 +
16044 +/* ig_flags */
16045 +#define AuIG_HALF_REFRESHED            1
16046 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
16047 +#define au_ig_fset(flags, name) \
16048 +       do { (flags) |= AuIG_##name; } while (0)
16049 +#define au_ig_fclr(flags, name) \
16050 +       do { (flags) &= ~AuIG_##name; } while (0)
16051 +
16052 +struct au_iigen {
16053 +       __u32           ig_generation, ig_flags;
16054 +};
16055 +
16056 +struct au_vdir;
16057 +struct au_iinfo {
16058 +       spinlock_t              ii_genspin;
16059 +       struct au_iigen         ii_generation;
16060 +       struct super_block      *ii_hsb1;       /* no get/put */
16061 +
16062 +       struct au_rwsem         ii_rwsem;
16063 +       aufs_bindex_t           ii_bstart, ii_bend;
16064 +       __u32                   ii_higen;
16065 +       struct au_hinode        *ii_hinode;
16066 +       struct au_vdir          *ii_vdir;
16067 +};
16068 +
16069 +struct au_icntnr {
16070 +       struct au_iinfo iinfo;
16071 +       struct inode vfs_inode;
16072 +} ____cacheline_aligned_in_smp;
16073 +
16074 +/* au_pin flags */
16075 +#define AuPin_DI_LOCKED                1
16076 +#define AuPin_MNT_WRITE                (1 << 1)
16077 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
16078 +#define au_fset_pin(flags, name) \
16079 +       do { (flags) |= AuPin_##name; } while (0)
16080 +#define au_fclr_pin(flags, name) \
16081 +       do { (flags) &= ~AuPin_##name; } while (0)
16082 +
16083 +struct au_pin {
16084 +       /* input */
16085 +       struct dentry *dentry;
16086 +       unsigned int udba;
16087 +       unsigned char lsc_di, lsc_hi, flags;
16088 +       aufs_bindex_t bindex;
16089 +
16090 +       /* output */
16091 +       struct dentry *parent;
16092 +       struct au_hinode *hdir;
16093 +       struct vfsmount *h_mnt;
16094 +
16095 +       /* temporary unlock/relock for copyup */
16096 +       struct dentry *h_dentry, *h_parent;
16097 +       struct au_branch *br;
16098 +       struct task_struct *task;
16099 +};
16100 +
16101 +void au_pin_hdir_unlock(struct au_pin *p);
16102 +int au_pin_hdir_lock(struct au_pin *p);
16103 +int au_pin_hdir_relock(struct au_pin *p);
16104 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
16105 +void au_pin_hdir_acquire_nest(struct au_pin *p);
16106 +void au_pin_hdir_release(struct au_pin *p);
16107 +
16108 +/* ---------------------------------------------------------------------- */
16109 +
16110 +static inline struct au_iinfo *au_ii(struct inode *inode)
16111 +{
16112 +       struct au_iinfo *iinfo;
16113 +
16114 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
16115 +       if (iinfo->ii_hinode)
16116 +               return iinfo;
16117 +       return NULL; /* debugging bad_inode case */
16118 +}
16119 +
16120 +/* ---------------------------------------------------------------------- */
16121 +
16122 +/* inode.c */
16123 +struct inode *au_igrab(struct inode *inode);
16124 +int au_refresh_hinode_self(struct inode *inode);
16125 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
16126 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16127 +          unsigned int d_type, ino_t *ino);
16128 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
16129 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16130 +              struct inode *inode);
16131 +int au_test_h_perm(struct inode *h_inode, int mask);
16132 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
16133 +
16134 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
16135 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
16136 +{
16137 +#ifdef CONFIG_AUFS_SHWH
16138 +       return au_ino(sb, bindex, h_ino, d_type, ino);
16139 +#else
16140 +       return 0;
16141 +#endif
16142 +}
16143 +
16144 +/* i_op.c */
16145 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
16146 +
16147 +/* au_wr_dir flags */
16148 +#define AuWrDir_ADD_ENTRY      1
16149 +#define AuWrDir_ISDIR          (1 << 1)
16150 +#define AuWrDir_TMPFILE                (1 << 2)
16151 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
16152 +#define au_fset_wrdir(flags, name) \
16153 +       do { (flags) |= AuWrDir_##name; } while (0)
16154 +#define au_fclr_wrdir(flags, name) \
16155 +       do { (flags) &= ~AuWrDir_##name; } while (0)
16156 +
16157 +struct au_wr_dir_args {
16158 +       aufs_bindex_t force_btgt;
16159 +       unsigned char flags;
16160 +};
16161 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16162 +             struct au_wr_dir_args *args);
16163 +
16164 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
16165 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
16166 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16167 +                unsigned int udba, unsigned char flags);
16168 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16169 +          unsigned int udba, unsigned char flags) __must_check;
16170 +int au_do_pin(struct au_pin *pin) __must_check;
16171 +void au_unpin(struct au_pin *pin);
16172 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
16173 +
16174 +#define AuIcpup_DID_CPUP       1
16175 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
16176 +#define au_fset_icpup(flags, name) \
16177 +       do { (flags) |= AuIcpup_##name; } while (0)
16178 +#define au_fclr_icpup(flags, name) \
16179 +       do { (flags) &= ~AuIcpup_##name; } while (0)
16180 +
16181 +struct au_icpup_args {
16182 +       unsigned char flags;
16183 +       unsigned char pin_flags;
16184 +       aufs_bindex_t btgt;
16185 +       unsigned int udba;
16186 +       struct au_pin pin;
16187 +       struct path h_path;
16188 +       struct inode *h_inode;
16189 +};
16190 +
16191 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16192 +                    struct au_icpup_args *a);
16193 +
16194 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
16195 +
16196 +/* i_op_add.c */
16197 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16198 +              struct dentry *h_parent, int isdir);
16199 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16200 +              dev_t dev);
16201 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
16202 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
16203 +               bool want_excl);
16204 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
16205 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
16206 +             struct dentry *dentry);
16207 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
16208 +
16209 +/* i_op_del.c */
16210 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
16211 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16212 +              struct dentry *h_parent, int isdir);
16213 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
16214 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
16215 +
16216 +/* i_op_ren.c */
16217 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
16218 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
16219 +               struct inode *dir, struct dentry *dentry);
16220 +
16221 +/* iinfo.c */
16222 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
16223 +void au_hiput(struct au_hinode *hinode);
16224 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
16225 +                 struct dentry *h_wh);
16226 +unsigned int au_hi_flags(struct inode *inode, int isdir);
16227 +
16228 +/* hinode flags */
16229 +#define AuHi_XINO      1
16230 +#define AuHi_HNOTIFY   (1 << 1)
16231 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
16232 +#define au_fset_hi(flags, name) \
16233 +       do { (flags) |= AuHi_##name; } while (0)
16234 +#define au_fclr_hi(flags, name) \
16235 +       do { (flags) &= ~AuHi_##name; } while (0)
16236 +
16237 +#ifndef CONFIG_AUFS_HNOTIFY
16238 +#undef AuHi_HNOTIFY
16239 +#define AuHi_HNOTIFY   0
16240 +#endif
16241 +
16242 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
16243 +                  struct inode *h_inode, unsigned int flags);
16244 +
16245 +void au_update_iigen(struct inode *inode, int half);
16246 +void au_update_ibrange(struct inode *inode, int do_put_zero);
16247 +
16248 +void au_icntnr_init_once(void *_c);
16249 +int au_iinfo_init(struct inode *inode);
16250 +void au_iinfo_fin(struct inode *inode);
16251 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
16252 +
16253 +#ifdef CONFIG_PROC_FS
16254 +/* plink.c */
16255 +int au_plink_maint(struct super_block *sb, int flags);
16256 +struct au_sbinfo;
16257 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
16258 +int au_plink_maint_enter(struct super_block *sb);
16259 +#ifdef CONFIG_AUFS_DEBUG
16260 +void au_plink_list(struct super_block *sb);
16261 +#else
16262 +AuStubVoid(au_plink_list, struct super_block *sb)
16263 +#endif
16264 +int au_plink_test(struct inode *inode);
16265 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
16266 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
16267 +                    struct dentry *h_dentry);
16268 +void au_plink_put(struct super_block *sb, int verbose);
16269 +void au_plink_clean(struct super_block *sb, int verbose);
16270 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
16271 +#else
16272 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
16273 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
16274 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
16275 +AuStubVoid(au_plink_list, struct super_block *sb);
16276 +AuStubInt0(au_plink_test, struct inode *inode);
16277 +AuStub(struct dentry *, au_plink_lkup, return NULL,
16278 +       struct inode *inode, aufs_bindex_t bindex);
16279 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
16280 +          struct dentry *h_dentry);
16281 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
16282 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
16283 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
16284 +#endif /* CONFIG_PROC_FS */
16285 +
16286 +#ifdef CONFIG_AUFS_XATTR
16287 +/* xattr.c */
16288 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
16289 +                 unsigned int verbose);
16290 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
16291 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
16292 +                     size_t size);
16293 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
16294 +                 size_t size, int flags);
16295 +int aufs_removexattr(struct dentry *dentry, const char *name);
16296 +
16297 +/* void au_xattr_init(struct super_block *sb); */
16298 +#else
16299 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
16300 +          int ignore_flags, unsigned int verbose);
16301 +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
16302 +#endif
16303 +
16304 +#ifdef CONFIG_FS_POSIX_ACL
16305 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
16306 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
16307 +#endif
16308 +
16309 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
16310 +enum {
16311 +       AU_XATTR_SET,
16312 +       AU_XATTR_REMOVE,
16313 +       AU_ACL_SET
16314 +};
16315 +
16316 +struct au_srxattr {
16317 +       int type;
16318 +       union {
16319 +               struct {
16320 +                       const char      *name;
16321 +                       const void      *value;
16322 +                       size_t          size;
16323 +                       int             flags;
16324 +               } set;
16325 +               struct {
16326 +                       const char      *name;
16327 +               } remove;
16328 +               struct {
16329 +                       struct posix_acl *acl;
16330 +                       int             type;
16331 +               } acl_set;
16332 +       } u;
16333 +};
16334 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg);
16335 +#endif
16336 +
16337 +/* ---------------------------------------------------------------------- */
16338 +
16339 +/* lock subclass for iinfo */
16340 +enum {
16341 +       AuLsc_II_CHILD,         /* child first */
16342 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
16343 +       AuLsc_II_CHILD3,        /* copyup dirs */
16344 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
16345 +       AuLsc_II_PARENT2,
16346 +       AuLsc_II_PARENT3,       /* copyup dirs */
16347 +       AuLsc_II_NEW_CHILD
16348 +};
16349 +
16350 +/*
16351 + * ii_read_lock_child, ii_write_lock_child,
16352 + * ii_read_lock_child2, ii_write_lock_child2,
16353 + * ii_read_lock_child3, ii_write_lock_child3,
16354 + * ii_read_lock_parent, ii_write_lock_parent,
16355 + * ii_read_lock_parent2, ii_write_lock_parent2,
16356 + * ii_read_lock_parent3, ii_write_lock_parent3,
16357 + * ii_read_lock_new_child, ii_write_lock_new_child,
16358 + */
16359 +#define AuReadLockFunc(name, lsc) \
16360 +static inline void ii_read_lock_##name(struct inode *i) \
16361 +{ \
16362 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16363 +}
16364 +
16365 +#define AuWriteLockFunc(name, lsc) \
16366 +static inline void ii_write_lock_##name(struct inode *i) \
16367 +{ \
16368 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16369 +}
16370 +
16371 +#define AuRWLockFuncs(name, lsc) \
16372 +       AuReadLockFunc(name, lsc) \
16373 +       AuWriteLockFunc(name, lsc)
16374 +
16375 +AuRWLockFuncs(child, CHILD);
16376 +AuRWLockFuncs(child2, CHILD2);
16377 +AuRWLockFuncs(child3, CHILD3);
16378 +AuRWLockFuncs(parent, PARENT);
16379 +AuRWLockFuncs(parent2, PARENT2);
16380 +AuRWLockFuncs(parent3, PARENT3);
16381 +AuRWLockFuncs(new_child, NEW_CHILD);
16382 +
16383 +#undef AuReadLockFunc
16384 +#undef AuWriteLockFunc
16385 +#undef AuRWLockFuncs
16386 +
16387 +/*
16388 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
16389 + */
16390 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
16391 +
16392 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
16393 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
16394 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
16395 +
16396 +/* ---------------------------------------------------------------------- */
16397 +
16398 +static inline void au_icntnr_init(struct au_icntnr *c)
16399 +{
16400 +#ifdef CONFIG_AUFS_DEBUG
16401 +       c->vfs_inode.i_mode = 0;
16402 +#endif
16403 +}
16404 +
16405 +static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
16406 +{
16407 +       unsigned int gen;
16408 +       struct au_iinfo *iinfo;
16409 +
16410 +       iinfo = au_ii(inode);
16411 +       spin_lock(&iinfo->ii_genspin);
16412 +       if (iigen)
16413 +               *iigen = iinfo->ii_generation;
16414 +       gen = iinfo->ii_generation.ig_generation;
16415 +       spin_unlock(&iinfo->ii_genspin);
16416 +
16417 +       return gen;
16418 +}
16419 +
16420 +/* tiny test for inode number */
16421 +/* tmpfs generation is too rough */
16422 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
16423 +{
16424 +       struct au_iinfo *iinfo;
16425 +
16426 +       iinfo = au_ii(inode);
16427 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
16428 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
16429 +                && iinfo->ii_higen == h_inode->i_generation);
16430 +}
16431 +
16432 +static inline void au_iigen_dec(struct inode *inode)
16433 +{
16434 +       struct au_iinfo *iinfo;
16435 +
16436 +       iinfo = au_ii(inode);
16437 +       spin_lock(&iinfo->ii_genspin);
16438 +       iinfo->ii_generation.ig_generation--;
16439 +       spin_unlock(&iinfo->ii_genspin);
16440 +}
16441 +
16442 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
16443 +{
16444 +       int err;
16445 +
16446 +       err = 0;
16447 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
16448 +               err = -EIO;
16449 +
16450 +       return err;
16451 +}
16452 +
16453 +/* ---------------------------------------------------------------------- */
16454 +
16455 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
16456 +                                       aufs_bindex_t bindex)
16457 +{
16458 +       IiMustAnyLock(inode);
16459 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
16460 +}
16461 +
16462 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
16463 +{
16464 +       IiMustAnyLock(inode);
16465 +       return au_ii(inode)->ii_bstart;
16466 +}
16467 +
16468 +static inline aufs_bindex_t au_ibend(struct inode *inode)
16469 +{
16470 +       IiMustAnyLock(inode);
16471 +       return au_ii(inode)->ii_bend;
16472 +}
16473 +
16474 +static inline struct au_vdir *au_ivdir(struct inode *inode)
16475 +{
16476 +       IiMustAnyLock(inode);
16477 +       return au_ii(inode)->ii_vdir;
16478 +}
16479 +
16480 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
16481 +{
16482 +       IiMustAnyLock(inode);
16483 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
16484 +}
16485 +
16486 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
16487 +{
16488 +       IiMustWriteLock(inode);
16489 +       au_ii(inode)->ii_bstart = bindex;
16490 +}
16491 +
16492 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
16493 +{
16494 +       IiMustWriteLock(inode);
16495 +       au_ii(inode)->ii_bend = bindex;
16496 +}
16497 +
16498 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
16499 +{
16500 +       IiMustWriteLock(inode);
16501 +       au_ii(inode)->ii_vdir = vdir;
16502 +}
16503 +
16504 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
16505 +{
16506 +       IiMustAnyLock(inode);
16507 +       return au_ii(inode)->ii_hinode + bindex;
16508 +}
16509 +
16510 +/* ---------------------------------------------------------------------- */
16511 +
16512 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
16513 +{
16514 +       if (pin)
16515 +               return pin->parent;
16516 +       return NULL;
16517 +}
16518 +
16519 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
16520 +{
16521 +       if (pin && pin->hdir)
16522 +               return pin->hdir->hi_inode;
16523 +       return NULL;
16524 +}
16525 +
16526 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
16527 +{
16528 +       if (pin)
16529 +               return pin->hdir;
16530 +       return NULL;
16531 +}
16532 +
16533 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
16534 +{
16535 +       if (pin)
16536 +               pin->dentry = dentry;
16537 +}
16538 +
16539 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
16540 +                                          unsigned char lflag)
16541 +{
16542 +       if (pin) {
16543 +               if (lflag)
16544 +                       au_fset_pin(pin->flags, DI_LOCKED);
16545 +               else
16546 +                       au_fclr_pin(pin->flags, DI_LOCKED);
16547 +       }
16548 +}
16549 +
16550 +#if 0 /* reserved */
16551 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
16552 +{
16553 +       if (pin) {
16554 +               dput(pin->parent);
16555 +               pin->parent = dget(parent);
16556 +       }
16557 +}
16558 +#endif
16559 +
16560 +/* ---------------------------------------------------------------------- */
16561 +
16562 +struct au_branch;
16563 +#ifdef CONFIG_AUFS_HNOTIFY
16564 +struct au_hnotify_op {
16565 +       void (*ctl)(struct au_hinode *hinode, int do_set);
16566 +       int (*alloc)(struct au_hinode *hinode);
16567 +
16568 +       /*
16569 +        * if it returns true, the the caller should free hinode->hi_notify,
16570 +        * otherwise ->free() frees it.
16571 +        */
16572 +       int (*free)(struct au_hinode *hinode,
16573 +                   struct au_hnotify *hn) __must_check;
16574 +
16575 +       void (*fin)(void);
16576 +       int (*init)(void);
16577 +
16578 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
16579 +       void (*fin_br)(struct au_branch *br);
16580 +       int (*init_br)(struct au_branch *br, int perm);
16581 +};
16582 +
16583 +/* hnotify.c */
16584 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
16585 +void au_hn_free(struct au_hinode *hinode);
16586 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
16587 +void au_hn_reset(struct inode *inode, unsigned int flags);
16588 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
16589 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
16590 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
16591 +int au_hnotify_init_br(struct au_branch *br, int perm);
16592 +void au_hnotify_fin_br(struct au_branch *br);
16593 +int __init au_hnotify_init(void);
16594 +void au_hnotify_fin(void);
16595 +
16596 +/* hfsnotify.c */
16597 +extern const struct au_hnotify_op au_hnotify_op;
16598 +
16599 +static inline
16600 +void au_hn_init(struct au_hinode *hinode)
16601 +{
16602 +       hinode->hi_notify = NULL;
16603 +}
16604 +
16605 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
16606 +{
16607 +       return hinode->hi_notify;
16608 +}
16609 +
16610 +#else
16611 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
16612 +       struct au_hinode *hinode __maybe_unused,
16613 +       struct inode *inode __maybe_unused)
16614 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
16615 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
16616 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
16617 +          int do_set __maybe_unused)
16618 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
16619 +          unsigned int flags __maybe_unused)
16620 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
16621 +          struct au_branch *br __maybe_unused,
16622 +          int perm __maybe_unused)
16623 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
16624 +          int perm __maybe_unused)
16625 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
16626 +AuStubInt0(__init au_hnotify_init, void)
16627 +AuStubVoid(au_hnotify_fin, void)
16628 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
16629 +#endif /* CONFIG_AUFS_HNOTIFY */
16630 +
16631 +static inline void au_hn_suspend(struct au_hinode *hdir)
16632 +{
16633 +       au_hn_ctl(hdir, /*do_set*/0);
16634 +}
16635 +
16636 +static inline void au_hn_resume(struct au_hinode *hdir)
16637 +{
16638 +       au_hn_ctl(hdir, /*do_set*/1);
16639 +}
16640 +
16641 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
16642 +{
16643 +       mutex_lock(&hdir->hi_inode->i_mutex);
16644 +       au_hn_suspend(hdir);
16645 +}
16646 +
16647 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
16648 +                                         unsigned int sc __maybe_unused)
16649 +{
16650 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
16651 +       au_hn_suspend(hdir);
16652 +}
16653 +
16654 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
16655 +{
16656 +       au_hn_resume(hdir);
16657 +       mutex_unlock(&hdir->hi_inode->i_mutex);
16658 +}
16659 +
16660 +#endif /* __KERNEL__ */
16661 +#endif /* __AUFS_INODE_H__ */
16662 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
16663 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
16664 +++ linux/fs/aufs/ioctl.c       2015-04-13 15:10:20.786823613 +0200
16665 @@ -0,0 +1,219 @@
16666 +/*
16667 + * Copyright (C) 2005-2015 Junjiro R. Okajima
16668 + *
16669 + * This program, aufs is free software; you can redistribute it and/or modify
16670 + * it under the terms of the GNU General Public License as published by
16671 + * the Free Software Foundation; either version 2 of the License, or
16672 + * (at your option) any later version.
16673 + *
16674 + * This program is distributed in the hope that it will be useful,
16675 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16676 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16677 + * GNU General Public License for more details.
16678 + *
16679 + * You should have received a copy of the GNU General Public License
16680 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16681 + */
16682 +
16683 +/*
16684 + * ioctl
16685 + * plink-management and readdir in userspace.
16686 + * assist the pathconf(3) wrapper library.
16687 + * move-down
16688 + * File-based Hierarchical Storage Management.
16689 + */
16690 +
16691 +#include <linux/compat.h>
16692 +#include <linux/file.h>
16693 +#include "aufs.h"
16694 +
16695 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
16696 +{
16697 +       int err, fd;
16698 +       aufs_bindex_t wbi, bindex, bend;
16699 +       struct file *h_file;
16700 +       struct super_block *sb;
16701 +       struct dentry *root;
16702 +       struct au_branch *br;
16703 +       struct aufs_wbr_fd wbrfd = {
16704 +               .oflags = au_dir_roflags,
16705 +               .brid   = -1
16706 +       };
16707 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
16708 +               | O_NOATIME | O_CLOEXEC;
16709 +
16710 +       AuDebugOn(wbrfd.oflags & ~valid);
16711 +
16712 +       if (arg) {
16713 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
16714 +               if (unlikely(err)) {
16715 +                       err = -EFAULT;
16716 +                       goto out;
16717 +               }
16718 +
16719 +               err = -EINVAL;
16720 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
16721 +               wbrfd.oflags |= au_dir_roflags;
16722 +               AuDbg("0%o\n", wbrfd.oflags);
16723 +               if (unlikely(wbrfd.oflags & ~valid))
16724 +                       goto out;
16725 +       }
16726 +
16727 +       fd = get_unused_fd_flags(0);
16728 +       err = fd;
16729 +       if (unlikely(fd < 0))
16730 +               goto out;
16731 +
16732 +       h_file = ERR_PTR(-EINVAL);
16733 +       wbi = 0;
16734 +       br = NULL;
16735 +       sb = path->dentry->d_sb;
16736 +       root = sb->s_root;
16737 +       aufs_read_lock(root, AuLock_IR);
16738 +       bend = au_sbend(sb);
16739 +       if (wbrfd.brid >= 0) {
16740 +               wbi = au_br_index(sb, wbrfd.brid);
16741 +               if (unlikely(wbi < 0 || wbi > bend))
16742 +                       goto out_unlock;
16743 +       }
16744 +
16745 +       h_file = ERR_PTR(-ENOENT);
16746 +       br = au_sbr(sb, wbi);
16747 +       if (!au_br_writable(br->br_perm)) {
16748 +               if (arg)
16749 +                       goto out_unlock;
16750 +
16751 +               bindex = wbi + 1;
16752 +               wbi = -1;
16753 +               for (; bindex <= bend; bindex++) {
16754 +                       br = au_sbr(sb, bindex);
16755 +                       if (au_br_writable(br->br_perm)) {
16756 +                               wbi = bindex;
16757 +                               br = au_sbr(sb, wbi);
16758 +                               break;
16759 +                       }
16760 +               }
16761 +       }
16762 +       AuDbg("wbi %d\n", wbi);
16763 +       if (wbi >= 0)
16764 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
16765 +                                  /*force_wr*/0);
16766 +
16767 +out_unlock:
16768 +       aufs_read_unlock(root, AuLock_IR);
16769 +       err = PTR_ERR(h_file);
16770 +       if (IS_ERR(h_file))
16771 +               goto out_fd;
16772 +
16773 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
16774 +       fd_install(fd, h_file);
16775 +       err = fd;
16776 +       goto out; /* success */
16777 +
16778 +out_fd:
16779 +       put_unused_fd(fd);
16780 +out:
16781 +       AuTraceErr(err);
16782 +       return err;
16783 +}
16784 +
16785 +/* ---------------------------------------------------------------------- */
16786 +
16787 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
16788 +{
16789 +       long err;
16790 +       struct dentry *dentry;
16791 +
16792 +       switch (cmd) {
16793 +       case AUFS_CTL_RDU:
16794 +       case AUFS_CTL_RDU_INO:
16795 +               err = au_rdu_ioctl(file, cmd, arg);
16796 +               break;
16797 +
16798 +       case AUFS_CTL_WBR_FD:
16799 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
16800 +               break;
16801 +
16802 +       case AUFS_CTL_IBUSY:
16803 +               err = au_ibusy_ioctl(file, arg);
16804 +               break;
16805 +
16806 +       case AUFS_CTL_BRINFO:
16807 +               err = au_brinfo_ioctl(file, arg);
16808 +               break;
16809 +
16810 +       case AUFS_CTL_FHSM_FD:
16811 +               dentry = file->f_path.dentry;
16812 +               if (IS_ROOT(dentry))
16813 +                       err = au_fhsm_fd(dentry->d_sb, arg);
16814 +               else
16815 +                       err = -ENOTTY;
16816 +               break;
16817 +
16818 +       default:
16819 +               /* do not call the lower */
16820 +               AuDbg("0x%x\n", cmd);
16821 +               err = -ENOTTY;
16822 +       }
16823 +
16824 +       AuTraceErr(err);
16825 +       return err;
16826 +}
16827 +
16828 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
16829 +{
16830 +       long err;
16831 +
16832 +       switch (cmd) {
16833 +       case AUFS_CTL_MVDOWN:
16834 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
16835 +               break;
16836 +
16837 +       case AUFS_CTL_WBR_FD:
16838 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
16839 +               break;
16840 +
16841 +       default:
16842 +               /* do not call the lower */
16843 +               AuDbg("0x%x\n", cmd);
16844 +               err = -ENOTTY;
16845 +       }
16846 +
16847 +       AuTraceErr(err);
16848 +       return err;
16849 +}
16850 +
16851 +#ifdef CONFIG_COMPAT
16852 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
16853 +                          unsigned long arg)
16854 +{
16855 +       long err;
16856 +
16857 +       switch (cmd) {
16858 +       case AUFS_CTL_RDU:
16859 +       case AUFS_CTL_RDU_INO:
16860 +               err = au_rdu_compat_ioctl(file, cmd, arg);
16861 +               break;
16862 +
16863 +       case AUFS_CTL_IBUSY:
16864 +               err = au_ibusy_compat_ioctl(file, arg);
16865 +               break;
16866 +
16867 +       case AUFS_CTL_BRINFO:
16868 +               err = au_brinfo_compat_ioctl(file, arg);
16869 +               break;
16870 +
16871 +       default:
16872 +               err = aufs_ioctl_dir(file, cmd, arg);
16873 +       }
16874 +
16875 +       AuTraceErr(err);
16876 +       return err;
16877 +}
16878 +
16879 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
16880 +                             unsigned long arg)
16881 +{
16882 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
16883 +}
16884 +#endif
16885 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
16886 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
16887 +++ linux/fs/aufs/i_op_add.c    2015-04-13 15:10:20.786823613 +0200
16888 @@ -0,0 +1,896 @@
16889 +/*
16890 + * Copyright (C) 2005-2015 Junjiro R. Okajima
16891 + *
16892 + * This program, aufs is free software; you can redistribute it and/or modify
16893 + * it under the terms of the GNU General Public License as published by
16894 + * the Free Software Foundation; either version 2 of the License, or
16895 + * (at your option) any later version.
16896 + *
16897 + * This program is distributed in the hope that it will be useful,
16898 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16899 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16900 + * GNU General Public License for more details.
16901 + *
16902 + * You should have received a copy of the GNU General Public License
16903 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16904 + */
16905 +
16906 +/*
16907 + * inode operations (add entry)
16908 + */
16909 +
16910 +#include "aufs.h"
16911 +
16912 +/*
16913 + * final procedure of adding a new entry, except link(2).
16914 + * remove whiteout, instantiate, copyup the parent dir's times and size
16915 + * and update version.
16916 + * if it failed, re-create the removed whiteout.
16917 + */
16918 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
16919 +                 struct dentry *wh_dentry, struct dentry *dentry)
16920 +{
16921 +       int err, rerr;
16922 +       aufs_bindex_t bwh;
16923 +       struct path h_path;
16924 +       struct super_block *sb;
16925 +       struct inode *inode, *h_dir;
16926 +       struct dentry *wh;
16927 +
16928 +       bwh = -1;
16929 +       sb = dir->i_sb;
16930 +       if (wh_dentry) {
16931 +               h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
16932 +               IMustLock(h_dir);
16933 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
16934 +               bwh = au_dbwh(dentry);
16935 +               h_path.dentry = wh_dentry;
16936 +               h_path.mnt = au_sbr_mnt(sb, bindex);
16937 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
16938 +                                         dentry);
16939 +               if (unlikely(err))
16940 +                       goto out;
16941 +       }
16942 +
16943 +       inode = au_new_inode(dentry, /*must_new*/1);
16944 +       if (!IS_ERR(inode)) {
16945 +               d_instantiate(dentry, inode);
16946 +               dir = dentry->d_parent->d_inode; /* dir inode is locked */
16947 +               IMustLock(dir);
16948 +               if (au_ibstart(dir) == au_dbstart(dentry))
16949 +                       au_cpup_attr_timesizes(dir);
16950 +               dir->i_version++;
16951 +               au_fhsm_wrote(sb, bindex, /*force*/0);
16952 +               return 0; /* success */
16953 +       }
16954 +
16955 +       err = PTR_ERR(inode);
16956 +       if (!wh_dentry)
16957 +               goto out;
16958 +
16959 +       /* revert */
16960 +       /* dir inode is locked */
16961 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
16962 +       rerr = PTR_ERR(wh);
16963 +       if (IS_ERR(wh)) {
16964 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
16965 +                       dentry, err, rerr);
16966 +               err = -EIO;
16967 +       } else
16968 +               dput(wh);
16969 +
16970 +out:
16971 +       return err;
16972 +}
16973 +
16974 +static int au_d_may_add(struct dentry *dentry)
16975 +{
16976 +       int err;
16977 +
16978 +       err = 0;
16979 +       if (unlikely(d_unhashed(dentry)))
16980 +               err = -ENOENT;
16981 +       if (unlikely(dentry->d_inode))
16982 +               err = -EEXIST;
16983 +       return err;
16984 +}
16985 +
16986 +/*
16987 + * simple tests for the adding inode operations.
16988 + * following the checks in vfs, plus the parent-child relationship.
16989 + */
16990 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16991 +              struct dentry *h_parent, int isdir)
16992 +{
16993 +       int err;
16994 +       umode_t h_mode;
16995 +       struct dentry *h_dentry;
16996 +       struct inode *h_inode;
16997 +
16998 +       err = -ENAMETOOLONG;
16999 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17000 +               goto out;
17001 +
17002 +       h_dentry = au_h_dptr(dentry, bindex);
17003 +       h_inode = h_dentry->d_inode;
17004 +       if (!dentry->d_inode) {
17005 +               err = -EEXIST;
17006 +               if (unlikely(h_inode))
17007 +                       goto out;
17008 +       } else {
17009 +               /* rename(2) case */
17010 +               err = -EIO;
17011 +               if (unlikely(!h_inode || !h_inode->i_nlink))
17012 +                       goto out;
17013 +
17014 +               h_mode = h_inode->i_mode;
17015 +               if (!isdir) {
17016 +                       err = -EISDIR;
17017 +                       if (unlikely(S_ISDIR(h_mode)))
17018 +                               goto out;
17019 +               } else if (unlikely(!S_ISDIR(h_mode))) {
17020 +                       err = -ENOTDIR;
17021 +                       goto out;
17022 +               }
17023 +       }
17024 +
17025 +       err = 0;
17026 +       /* expected parent dir is locked */
17027 +       if (unlikely(h_parent != h_dentry->d_parent))
17028 +               err = -EIO;
17029 +
17030 +out:
17031 +       AuTraceErr(err);
17032 +       return err;
17033 +}
17034 +
17035 +/*
17036 + * initial procedure of adding a new entry.
17037 + * prepare writable branch and the parent dir, lock it,
17038 + * and lookup whiteout for the new entry.
17039 + */
17040 +static struct dentry*
17041 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
17042 +                 struct dentry *src_dentry, struct au_pin *pin,
17043 +                 struct au_wr_dir_args *wr_dir_args)
17044 +{
17045 +       struct dentry *wh_dentry, *h_parent;
17046 +       struct super_block *sb;
17047 +       struct au_branch *br;
17048 +       int err;
17049 +       unsigned int udba;
17050 +       aufs_bindex_t bcpup;
17051 +
17052 +       AuDbg("%pd\n", dentry);
17053 +
17054 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
17055 +       bcpup = err;
17056 +       wh_dentry = ERR_PTR(err);
17057 +       if (unlikely(err < 0))
17058 +               goto out;
17059 +
17060 +       sb = dentry->d_sb;
17061 +       udba = au_opt_udba(sb);
17062 +       err = au_pin(pin, dentry, bcpup, udba,
17063 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17064 +       wh_dentry = ERR_PTR(err);
17065 +       if (unlikely(err))
17066 +               goto out;
17067 +
17068 +       h_parent = au_pinned_h_parent(pin);
17069 +       if (udba != AuOpt_UDBA_NONE
17070 +           && au_dbstart(dentry) == bcpup)
17071 +               err = au_may_add(dentry, bcpup, h_parent,
17072 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
17073 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17074 +               err = -ENAMETOOLONG;
17075 +       wh_dentry = ERR_PTR(err);
17076 +       if (unlikely(err))
17077 +               goto out_unpin;
17078 +
17079 +       br = au_sbr(sb, bcpup);
17080 +       if (dt) {
17081 +               struct path tmp = {
17082 +                       .dentry = h_parent,
17083 +                       .mnt    = au_br_mnt(br)
17084 +               };
17085 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
17086 +       }
17087 +
17088 +       wh_dentry = NULL;
17089 +       if (bcpup != au_dbwh(dentry))
17090 +               goto out; /* success */
17091 +
17092 +       /*
17093 +        * ENAMETOOLONG here means that if we allowed create such name, then it
17094 +        * would not be able to removed in the future. So we don't allow such
17095 +        * name here and we don't handle ENAMETOOLONG differently here.
17096 +        */
17097 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
17098 +
17099 +out_unpin:
17100 +       if (IS_ERR(wh_dentry))
17101 +               au_unpin(pin);
17102 +out:
17103 +       return wh_dentry;
17104 +}
17105 +
17106 +/* ---------------------------------------------------------------------- */
17107 +
17108 +enum { Mknod, Symlink, Creat };
17109 +struct simple_arg {
17110 +       int type;
17111 +       union {
17112 +               struct {
17113 +                       umode_t mode;
17114 +                       bool want_excl;
17115 +               } c;
17116 +               struct {
17117 +                       const char *symname;
17118 +               } s;
17119 +               struct {
17120 +                       umode_t mode;
17121 +                       dev_t dev;
17122 +               } m;
17123 +       } u;
17124 +};
17125 +
17126 +static int add_simple(struct inode *dir, struct dentry *dentry,
17127 +                     struct simple_arg *arg)
17128 +{
17129 +       int err, rerr;
17130 +       aufs_bindex_t bstart;
17131 +       unsigned char created;
17132 +       struct dentry *wh_dentry, *parent;
17133 +       struct inode *h_dir;
17134 +       /* to reuduce stack size */
17135 +       struct {
17136 +               struct au_dtime dt;
17137 +               struct au_pin pin;
17138 +               struct path h_path;
17139 +               struct au_wr_dir_args wr_dir_args;
17140 +       } *a;
17141 +
17142 +       AuDbg("%pd\n", dentry);
17143 +       IMustLock(dir);
17144 +
17145 +       err = -ENOMEM;
17146 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17147 +       if (unlikely(!a))
17148 +               goto out;
17149 +       a->wr_dir_args.force_btgt = -1;
17150 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
17151 +
17152 +       parent = dentry->d_parent; /* dir inode is locked */
17153 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17154 +       if (unlikely(err))
17155 +               goto out_free;
17156 +       err = au_d_may_add(dentry);
17157 +       if (unlikely(err))
17158 +               goto out_unlock;
17159 +       di_write_lock_parent(parent);
17160 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17161 +                                     &a->pin, &a->wr_dir_args);
17162 +       err = PTR_ERR(wh_dentry);
17163 +       if (IS_ERR(wh_dentry))
17164 +               goto out_parent;
17165 +
17166 +       bstart = au_dbstart(dentry);
17167 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17168 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
17169 +       h_dir = au_pinned_h_dir(&a->pin);
17170 +       switch (arg->type) {
17171 +       case Creat:
17172 +               err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
17173 +                                  arg->u.c.want_excl);
17174 +               break;
17175 +       case Symlink:
17176 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
17177 +               break;
17178 +       case Mknod:
17179 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
17180 +                                 arg->u.m.dev);
17181 +               break;
17182 +       default:
17183 +               BUG();
17184 +       }
17185 +       created = !err;
17186 +       if (!err)
17187 +               err = epilog(dir, bstart, wh_dentry, dentry);
17188 +
17189 +       /* revert */
17190 +       if (unlikely(created && err && a->h_path.dentry->d_inode)) {
17191 +               /* no delegation since it is just created */
17192 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
17193 +                                   /*force*/0);
17194 +               if (rerr) {
17195 +                       AuIOErr("%pd revert failure(%d, %d)\n",
17196 +                               dentry, err, rerr);
17197 +                       err = -EIO;
17198 +               }
17199 +               au_dtime_revert(&a->dt);
17200 +       }
17201 +
17202 +       au_unpin(&a->pin);
17203 +       dput(wh_dentry);
17204 +
17205 +out_parent:
17206 +       di_write_unlock(parent);
17207 +out_unlock:
17208 +       if (unlikely(err)) {
17209 +               au_update_dbstart(dentry);
17210 +               d_drop(dentry);
17211 +       }
17212 +       aufs_read_unlock(dentry, AuLock_DW);
17213 +out_free:
17214 +       kfree(a);
17215 +out:
17216 +       return err;
17217 +}
17218 +
17219 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
17220 +              dev_t dev)
17221 +{
17222 +       struct simple_arg arg = {
17223 +               .type = Mknod,
17224 +               .u.m = {
17225 +                       .mode   = mode,
17226 +                       .dev    = dev
17227 +               }
17228 +       };
17229 +       return add_simple(dir, dentry, &arg);
17230 +}
17231 +
17232 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17233 +{
17234 +       struct simple_arg arg = {
17235 +               .type = Symlink,
17236 +               .u.s.symname = symname
17237 +       };
17238 +       return add_simple(dir, dentry, &arg);
17239 +}
17240 +
17241 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
17242 +               bool want_excl)
17243 +{
17244 +       struct simple_arg arg = {
17245 +               .type = Creat,
17246 +               .u.c = {
17247 +                       .mode           = mode,
17248 +                       .want_excl      = want_excl
17249 +               }
17250 +       };
17251 +       return add_simple(dir, dentry, &arg);
17252 +}
17253 +
17254 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
17255 +{
17256 +       int err;
17257 +       aufs_bindex_t bindex;
17258 +       struct super_block *sb;
17259 +       struct dentry *parent, *h_parent, *h_dentry;
17260 +       struct inode *h_dir, *inode;
17261 +       struct vfsmount *h_mnt;
17262 +       struct au_wr_dir_args wr_dir_args = {
17263 +               .force_btgt     = -1,
17264 +               .flags          = AuWrDir_TMPFILE
17265 +       };
17266 +
17267 +       /* copy-up may happen */
17268 +       mutex_lock(&dir->i_mutex);
17269 +
17270 +       sb = dir->i_sb;
17271 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17272 +       if (unlikely(err))
17273 +               goto out;
17274 +
17275 +       err = au_di_init(dentry);
17276 +       if (unlikely(err))
17277 +               goto out_si;
17278 +
17279 +       err = -EBUSY;
17280 +       parent = d_find_any_alias(dir);
17281 +       AuDebugOn(!parent);
17282 +       di_write_lock_parent(parent);
17283 +       if (unlikely(parent->d_inode != dir))
17284 +               goto out_parent;
17285 +
17286 +       err = au_digen_test(parent, au_sigen(sb));
17287 +       if (unlikely(err))
17288 +               goto out_parent;
17289 +
17290 +       bindex = au_dbstart(parent);
17291 +       au_set_dbstart(dentry, bindex);
17292 +       au_set_dbend(dentry, bindex);
17293 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17294 +       bindex = err;
17295 +       if (unlikely(err < 0))
17296 +               goto out_parent;
17297 +
17298 +       err = -EOPNOTSUPP;
17299 +       h_dir = au_h_iptr(dir, bindex);
17300 +       if (unlikely(!h_dir->i_op->tmpfile))
17301 +               goto out_parent;
17302 +
17303 +       h_mnt = au_sbr_mnt(sb, bindex);
17304 +       err = vfsub_mnt_want_write(h_mnt);
17305 +       if (unlikely(err))
17306 +               goto out_parent;
17307 +
17308 +       h_parent = au_h_dptr(parent, bindex);
17309 +       err = inode_permission(h_parent->d_inode, MAY_WRITE | MAY_EXEC);
17310 +       if (unlikely(err))
17311 +               goto out_mnt;
17312 +
17313 +       err = -ENOMEM;
17314 +       h_dentry = d_alloc(h_parent, &dentry->d_name);
17315 +       if (unlikely(!h_dentry))
17316 +               goto out_mnt;
17317 +
17318 +       err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
17319 +       if (unlikely(err))
17320 +               goto out_dentry;
17321 +
17322 +       au_set_dbstart(dentry, bindex);
17323 +       au_set_dbend(dentry, bindex);
17324 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
17325 +       inode = au_new_inode(dentry, /*must_new*/1);
17326 +       if (IS_ERR(inode)) {
17327 +               err = PTR_ERR(inode);
17328 +               au_set_h_dptr(dentry, bindex, NULL);
17329 +               au_set_dbstart(dentry, -1);
17330 +               au_set_dbend(dentry, -1);
17331 +       } else {
17332 +               if (!inode->i_nlink)
17333 +                       set_nlink(inode, 1);
17334 +               d_tmpfile(dentry, inode);
17335 +               au_di(dentry)->di_tmpfile = 1;
17336 +
17337 +               /* update without i_mutex */
17338 +               if (au_ibstart(dir) == au_dbstart(dentry))
17339 +                       au_cpup_attr_timesizes(dir);
17340 +       }
17341 +
17342 +out_dentry:
17343 +       dput(h_dentry);
17344 +out_mnt:
17345 +       vfsub_mnt_drop_write(h_mnt);
17346 +out_parent:
17347 +       di_write_unlock(parent);
17348 +       dput(parent);
17349 +       di_write_unlock(dentry);
17350 +       if (!err)
17351 +#if 0
17352 +               /* verbose coding for lock class name */
17353 +               au_rw_class(&au_di(dentry)->di_rwsem,
17354 +                           au_lc_key + AuLcNonDir_DIINFO);
17355 +#else
17356 +               ;
17357 +#endif
17358 +       else {
17359 +               au_di_fin(dentry);
17360 +               dentry->d_fsdata = NULL;
17361 +       }
17362 +out_si:
17363 +       si_read_unlock(sb);
17364 +out:
17365 +       mutex_unlock(&dir->i_mutex);
17366 +       return err;
17367 +}
17368 +
17369 +/* ---------------------------------------------------------------------- */
17370 +
17371 +struct au_link_args {
17372 +       aufs_bindex_t bdst, bsrc;
17373 +       struct au_pin pin;
17374 +       struct path h_path;
17375 +       struct dentry *src_parent, *parent;
17376 +};
17377 +
17378 +static int au_cpup_before_link(struct dentry *src_dentry,
17379 +                              struct au_link_args *a)
17380 +{
17381 +       int err;
17382 +       struct dentry *h_src_dentry;
17383 +       struct au_cp_generic cpg = {
17384 +               .dentry = src_dentry,
17385 +               .bdst   = a->bdst,
17386 +               .bsrc   = a->bsrc,
17387 +               .len    = -1,
17388 +               .pin    = &a->pin,
17389 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
17390 +       };
17391 +
17392 +       di_read_lock_parent(a->src_parent, AuLock_IR);
17393 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
17394 +       if (unlikely(err))
17395 +               goto out;
17396 +
17397 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
17398 +       err = au_pin(&a->pin, src_dentry, a->bdst,
17399 +                    au_opt_udba(src_dentry->d_sb),
17400 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17401 +       if (unlikely(err))
17402 +               goto out;
17403 +
17404 +       err = au_sio_cpup_simple(&cpg);
17405 +       au_unpin(&a->pin);
17406 +
17407 +out:
17408 +       di_read_unlock(a->src_parent, AuLock_IR);
17409 +       return err;
17410 +}
17411 +
17412 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
17413 +                          struct au_link_args *a)
17414 +{
17415 +       int err;
17416 +       unsigned char plink;
17417 +       aufs_bindex_t bend;
17418 +       struct dentry *h_src_dentry;
17419 +       struct inode *h_inode, *inode, *delegated;
17420 +       struct super_block *sb;
17421 +       struct file *h_file;
17422 +
17423 +       plink = 0;
17424 +       h_inode = NULL;
17425 +       sb = src_dentry->d_sb;
17426 +       inode = src_dentry->d_inode;
17427 +       if (au_ibstart(inode) <= a->bdst)
17428 +               h_inode = au_h_iptr(inode, a->bdst);
17429 +       if (!h_inode || !h_inode->i_nlink) {
17430 +               /* copyup src_dentry as the name of dentry. */
17431 +               bend = au_dbend(dentry);
17432 +               if (bend < a->bsrc)
17433 +                       au_set_dbend(dentry, a->bsrc);
17434 +               au_set_h_dptr(dentry, a->bsrc,
17435 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
17436 +               dget(a->h_path.dentry);
17437 +               au_set_h_dptr(dentry, a->bdst, NULL);
17438 +               AuDbg("temporary d_inode...\n");
17439 +               spin_lock(&dentry->d_lock);
17440 +               dentry->d_inode = src_dentry->d_inode; /* tmp */
17441 +               spin_unlock(&dentry->d_lock);
17442 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
17443 +               if (IS_ERR(h_file))
17444 +                       err = PTR_ERR(h_file);
17445 +               else {
17446 +                       struct au_cp_generic cpg = {
17447 +                               .dentry = dentry,
17448 +                               .bdst   = a->bdst,
17449 +                               .bsrc   = -1,
17450 +                               .len    = -1,
17451 +                               .pin    = &a->pin,
17452 +                               .flags  = AuCpup_KEEPLINO
17453 +                       };
17454 +                       err = au_sio_cpup_simple(&cpg);
17455 +                       au_h_open_post(dentry, a->bsrc, h_file);
17456 +                       if (!err) {
17457 +                               dput(a->h_path.dentry);
17458 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
17459 +                       } else
17460 +                               au_set_h_dptr(dentry, a->bdst,
17461 +                                             a->h_path.dentry);
17462 +               }
17463 +               spin_lock(&dentry->d_lock);
17464 +               dentry->d_inode = NULL; /* restore */
17465 +               spin_unlock(&dentry->d_lock);
17466 +               AuDbg("temporary d_inode...done\n");
17467 +               au_set_h_dptr(dentry, a->bsrc, NULL);
17468 +               au_set_dbend(dentry, bend);
17469 +       } else {
17470 +               /* the inode of src_dentry already exists on a.bdst branch */
17471 +               h_src_dentry = d_find_alias(h_inode);
17472 +               if (!h_src_dentry && au_plink_test(inode)) {
17473 +                       plink = 1;
17474 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
17475 +                       err = PTR_ERR(h_src_dentry);
17476 +                       if (IS_ERR(h_src_dentry))
17477 +                               goto out;
17478 +
17479 +                       if (unlikely(!h_src_dentry->d_inode)) {
17480 +                               dput(h_src_dentry);
17481 +                               h_src_dentry = NULL;
17482 +                       }
17483 +
17484 +               }
17485 +               if (h_src_dentry) {
17486 +                       delegated = NULL;
17487 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
17488 +                                        &a->h_path, &delegated);
17489 +                       if (unlikely(err == -EWOULDBLOCK)) {
17490 +                               pr_warn("cannot retry for NFSv4 delegation"
17491 +                                       " for an internal link\n");
17492 +                               iput(delegated);
17493 +                       }
17494 +                       dput(h_src_dentry);
17495 +               } else {
17496 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
17497 +                               h_inode->i_ino, a->bdst);
17498 +                       err = -EIO;
17499 +               }
17500 +       }
17501 +
17502 +       if (!err && !plink)
17503 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
17504 +
17505 +out:
17506 +       AuTraceErr(err);
17507 +       return err;
17508 +}
17509 +
17510 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
17511 +             struct dentry *dentry)
17512 +{
17513 +       int err, rerr;
17514 +       struct au_dtime dt;
17515 +       struct au_link_args *a;
17516 +       struct dentry *wh_dentry, *h_src_dentry;
17517 +       struct inode *inode, *delegated;
17518 +       struct super_block *sb;
17519 +       struct au_wr_dir_args wr_dir_args = {
17520 +               /* .force_btgt  = -1, */
17521 +               .flags          = AuWrDir_ADD_ENTRY
17522 +       };
17523 +
17524 +       IMustLock(dir);
17525 +       inode = src_dentry->d_inode;
17526 +       IMustLock(inode);
17527 +
17528 +       err = -ENOMEM;
17529 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17530 +       if (unlikely(!a))
17531 +               goto out;
17532 +
17533 +       a->parent = dentry->d_parent; /* dir inode is locked */
17534 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
17535 +                                       AuLock_NOPLM | AuLock_GEN);
17536 +       if (unlikely(err))
17537 +               goto out_kfree;
17538 +       err = au_d_linkable(src_dentry);
17539 +       if (unlikely(err))
17540 +               goto out_unlock;
17541 +       err = au_d_may_add(dentry);
17542 +       if (unlikely(err))
17543 +               goto out_unlock;
17544 +
17545 +       a->src_parent = dget_parent(src_dentry);
17546 +       wr_dir_args.force_btgt = au_ibstart(inode);
17547 +
17548 +       di_write_lock_parent(a->parent);
17549 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
17550 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
17551 +                                     &wr_dir_args);
17552 +       err = PTR_ERR(wh_dentry);
17553 +       if (IS_ERR(wh_dentry))
17554 +               goto out_parent;
17555 +
17556 +       err = 0;
17557 +       sb = dentry->d_sb;
17558 +       a->bdst = au_dbstart(dentry);
17559 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
17560 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
17561 +       a->bsrc = au_ibstart(inode);
17562 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
17563 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
17564 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
17565 +       if (!h_src_dentry) {
17566 +               a->bsrc = au_dbstart(src_dentry);
17567 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
17568 +               AuDebugOn(!h_src_dentry);
17569 +       } else if (IS_ERR(h_src_dentry)) {
17570 +               err = PTR_ERR(h_src_dentry);
17571 +               goto out_parent;
17572 +       }
17573 +
17574 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
17575 +               if (a->bdst < a->bsrc
17576 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
17577 +                       err = au_cpup_or_link(src_dentry, dentry, a);
17578 +               else {
17579 +                       delegated = NULL;
17580 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
17581 +                                        &a->h_path, &delegated);
17582 +                       if (unlikely(err == -EWOULDBLOCK)) {
17583 +                               pr_warn("cannot retry for NFSv4 delegation"
17584 +                                       " for an internal link\n");
17585 +                               iput(delegated);
17586 +                       }
17587 +               }
17588 +               dput(h_src_dentry);
17589 +       } else {
17590 +               /*
17591 +                * copyup src_dentry to the branch we process,
17592 +                * and then link(2) to it.
17593 +                */
17594 +               dput(h_src_dentry);
17595 +               if (a->bdst < a->bsrc
17596 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
17597 +                       au_unpin(&a->pin);
17598 +                       di_write_unlock(a->parent);
17599 +                       err = au_cpup_before_link(src_dentry, a);
17600 +                       di_write_lock_parent(a->parent);
17601 +                       if (!err)
17602 +                               err = au_pin(&a->pin, dentry, a->bdst,
17603 +                                            au_opt_udba(sb),
17604 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17605 +                       if (unlikely(err))
17606 +                               goto out_wh;
17607 +               }
17608 +               if (!err) {
17609 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
17610 +                       err = -ENOENT;
17611 +                       if (h_src_dentry && h_src_dentry->d_inode) {
17612 +                               delegated = NULL;
17613 +                               err = vfsub_link(h_src_dentry,
17614 +                                                au_pinned_h_dir(&a->pin),
17615 +                                                &a->h_path, &delegated);
17616 +                               if (unlikely(err == -EWOULDBLOCK)) {
17617 +                                       pr_warn("cannot retry"
17618 +                                               " for NFSv4 delegation"
17619 +                                               " for an internal link\n");
17620 +                                       iput(delegated);
17621 +                               }
17622 +                       }
17623 +               }
17624 +       }
17625 +       if (unlikely(err))
17626 +               goto out_unpin;
17627 +
17628 +       if (wh_dentry) {
17629 +               a->h_path.dentry = wh_dentry;
17630 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
17631 +                                         dentry);
17632 +               if (unlikely(err))
17633 +                       goto out_revert;
17634 +       }
17635 +
17636 +       dir->i_version++;
17637 +       if (au_ibstart(dir) == au_dbstart(dentry))
17638 +               au_cpup_attr_timesizes(dir);
17639 +       inc_nlink(inode);
17640 +       inode->i_ctime = dir->i_ctime;
17641 +       d_instantiate(dentry, au_igrab(inode));
17642 +       if (d_unhashed(a->h_path.dentry))
17643 +               /* some filesystem calls d_drop() */
17644 +               d_drop(dentry);
17645 +       /* some filesystems consume an inode even hardlink */
17646 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
17647 +       goto out_unpin; /* success */
17648 +
17649 +out_revert:
17650 +       /* no delegation since it is just created */
17651 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
17652 +                           /*delegated*/NULL, /*force*/0);
17653 +       if (unlikely(rerr)) {
17654 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
17655 +               err = -EIO;
17656 +       }
17657 +       au_dtime_revert(&dt);
17658 +out_unpin:
17659 +       au_unpin(&a->pin);
17660 +out_wh:
17661 +       dput(wh_dentry);
17662 +out_parent:
17663 +       di_write_unlock(a->parent);
17664 +       dput(a->src_parent);
17665 +out_unlock:
17666 +       if (unlikely(err)) {
17667 +               au_update_dbstart(dentry);
17668 +               d_drop(dentry);
17669 +       }
17670 +       aufs_read_and_write_unlock2(dentry, src_dentry);
17671 +out_kfree:
17672 +       kfree(a);
17673 +out:
17674 +       AuTraceErr(err);
17675 +       return err;
17676 +}
17677 +
17678 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
17679 +{
17680 +       int err, rerr;
17681 +       aufs_bindex_t bindex;
17682 +       unsigned char diropq;
17683 +       struct path h_path;
17684 +       struct dentry *wh_dentry, *parent, *opq_dentry;
17685 +       struct mutex *h_mtx;
17686 +       struct super_block *sb;
17687 +       struct {
17688 +               struct au_pin pin;
17689 +               struct au_dtime dt;
17690 +       } *a; /* reduce the stack usage */
17691 +       struct au_wr_dir_args wr_dir_args = {
17692 +               .force_btgt     = -1,
17693 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
17694 +       };
17695 +
17696 +       IMustLock(dir);
17697 +
17698 +       err = -ENOMEM;
17699 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17700 +       if (unlikely(!a))
17701 +               goto out;
17702 +
17703 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17704 +       if (unlikely(err))
17705 +               goto out_free;
17706 +       err = au_d_may_add(dentry);
17707 +       if (unlikely(err))
17708 +               goto out_unlock;
17709 +
17710 +       parent = dentry->d_parent; /* dir inode is locked */
17711 +       di_write_lock_parent(parent);
17712 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17713 +                                     &a->pin, &wr_dir_args);
17714 +       err = PTR_ERR(wh_dentry);
17715 +       if (IS_ERR(wh_dentry))
17716 +               goto out_parent;
17717 +
17718 +       sb = dentry->d_sb;
17719 +       bindex = au_dbstart(dentry);
17720 +       h_path.dentry = au_h_dptr(dentry, bindex);
17721 +       h_path.mnt = au_sbr_mnt(sb, bindex);
17722 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
17723 +       if (unlikely(err))
17724 +               goto out_unpin;
17725 +
17726 +       /* make the dir opaque */
17727 +       diropq = 0;
17728 +       h_mtx = &h_path.dentry->d_inode->i_mutex;
17729 +       if (wh_dentry
17730 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
17731 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
17732 +               opq_dentry = au_diropq_create(dentry, bindex);
17733 +               mutex_unlock(h_mtx);
17734 +               err = PTR_ERR(opq_dentry);
17735 +               if (IS_ERR(opq_dentry))
17736 +                       goto out_dir;
17737 +               dput(opq_dentry);
17738 +               diropq = 1;
17739 +       }
17740 +
17741 +       err = epilog(dir, bindex, wh_dentry, dentry);
17742 +       if (!err) {
17743 +               inc_nlink(dir);
17744 +               goto out_unpin; /* success */
17745 +       }
17746 +
17747 +       /* revert */
17748 +       if (diropq) {
17749 +               AuLabel(revert opq);
17750 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
17751 +               rerr = au_diropq_remove(dentry, bindex);
17752 +               mutex_unlock(h_mtx);
17753 +               if (rerr) {
17754 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
17755 +                               dentry, err, rerr);
17756 +                       err = -EIO;
17757 +               }
17758 +       }
17759 +
17760 +out_dir:
17761 +       AuLabel(revert dir);
17762 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
17763 +       if (rerr) {
17764 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
17765 +                       dentry, err, rerr);
17766 +               err = -EIO;
17767 +       }
17768 +       au_dtime_revert(&a->dt);
17769 +out_unpin:
17770 +       au_unpin(&a->pin);
17771 +       dput(wh_dentry);
17772 +out_parent:
17773 +       di_write_unlock(parent);
17774 +out_unlock:
17775 +       if (unlikely(err)) {
17776 +               au_update_dbstart(dentry);
17777 +               d_drop(dentry);
17778 +       }
17779 +       aufs_read_unlock(dentry, AuLock_DW);
17780 +out_free:
17781 +       kfree(a);
17782 +out:
17783 +       return err;
17784 +}
17785 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
17786 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
17787 +++ linux/fs/aufs/i_op.c        2015-04-13 15:10:20.786823613 +0200
17788 @@ -0,0 +1,1297 @@
17789 +/*
17790 + * Copyright (C) 2005-2015 Junjiro R. Okajima
17791 + *
17792 + * This program, aufs is free software; you can redistribute it and/or modify
17793 + * it under the terms of the GNU General Public License as published by
17794 + * the Free Software Foundation; either version 2 of the License, or
17795 + * (at your option) any later version.
17796 + *
17797 + * This program is distributed in the hope that it will be useful,
17798 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17799 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17800 + * GNU General Public License for more details.
17801 + *
17802 + * You should have received a copy of the GNU General Public License
17803 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17804 + */
17805 +
17806 +/*
17807 + * inode operations (except add/del/rename)
17808 + */
17809 +
17810 +#include <linux/device_cgroup.h>
17811 +#include <linux/fs_stack.h>
17812 +#include <linux/mm.h>
17813 +#include <linux/namei.h>
17814 +#include <linux/security.h>
17815 +#include "aufs.h"
17816 +
17817 +static int h_permission(struct inode *h_inode, int mask,
17818 +                       struct vfsmount *h_mnt, int brperm)
17819 +{
17820 +       int err;
17821 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
17822 +
17823 +       err = -EACCES;
17824 +       if ((write_mask && IS_IMMUTABLE(h_inode))
17825 +           || ((mask & MAY_EXEC)
17826 +               && S_ISREG(h_inode->i_mode)
17827 +               && ((h_mnt->mnt_flags & MNT_NOEXEC)
17828 +                   || !(h_inode->i_mode & S_IXUGO))))
17829 +               goto out;
17830 +
17831 +       /*
17832 +        * - skip the lower fs test in the case of write to ro branch.
17833 +        * - nfs dir permission write check is optimized, but a policy for
17834 +        *   link/rename requires a real check.
17835 +        */
17836 +       if ((write_mask && !au_br_writable(brperm))
17837 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
17838 +               && write_mask && !(mask & MAY_READ))
17839 +           || !h_inode->i_op->permission) {
17840 +               /* AuLabel(generic_permission); */
17841 +               err = generic_permission(h_inode, mask);
17842 +       } else {
17843 +               /* AuLabel(h_inode->permission); */
17844 +               err = h_inode->i_op->permission(h_inode, mask);
17845 +               AuTraceErr(err);
17846 +       }
17847 +
17848 +       if (!err)
17849 +               err = devcgroup_inode_permission(h_inode, mask);
17850 +       if (!err)
17851 +               err = security_inode_permission(h_inode, mask);
17852 +
17853 +#if 0
17854 +       if (!err) {
17855 +               /* todo: do we need to call ima_path_check()? */
17856 +               struct path h_path = {
17857 +                       .dentry =
17858 +                       .mnt    = h_mnt
17859 +               };
17860 +               err = ima_path_check(&h_path,
17861 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
17862 +                                    IMA_COUNT_LEAVE);
17863 +       }
17864 +#endif
17865 +
17866 +out:
17867 +       return err;
17868 +}
17869 +
17870 +static int aufs_permission(struct inode *inode, int mask)
17871 +{
17872 +       int err;
17873 +       aufs_bindex_t bindex, bend;
17874 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
17875 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
17876 +       struct inode *h_inode;
17877 +       struct super_block *sb;
17878 +       struct au_branch *br;
17879 +
17880 +       /* todo: support rcu-walk? */
17881 +       if (mask & MAY_NOT_BLOCK)
17882 +               return -ECHILD;
17883 +
17884 +       sb = inode->i_sb;
17885 +       si_read_lock(sb, AuLock_FLUSH);
17886 +       ii_read_lock_child(inode);
17887 +#if 0
17888 +       err = au_iigen_test(inode, au_sigen(sb));
17889 +       if (unlikely(err))
17890 +               goto out;
17891 +#endif
17892 +
17893 +       if (!isdir
17894 +           || write_mask
17895 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
17896 +               err = au_busy_or_stale();
17897 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
17898 +               if (unlikely(!h_inode
17899 +                            || (h_inode->i_mode & S_IFMT)
17900 +                            != (inode->i_mode & S_IFMT)))
17901 +                       goto out;
17902 +
17903 +               err = 0;
17904 +               bindex = au_ibstart(inode);
17905 +               br = au_sbr(sb, bindex);
17906 +               err = h_permission(h_inode, mask, au_br_mnt(br), br->br_perm);
17907 +               if (write_mask
17908 +                   && !err
17909 +                   && !special_file(h_inode->i_mode)) {
17910 +                       /* test whether the upper writable branch exists */
17911 +                       err = -EROFS;
17912 +                       for (; bindex >= 0; bindex--)
17913 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
17914 +                                       err = 0;
17915 +                                       break;
17916 +                               }
17917 +               }
17918 +               goto out;
17919 +       }
17920 +
17921 +       /* non-write to dir */
17922 +       err = 0;
17923 +       bend = au_ibend(inode);
17924 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
17925 +               h_inode = au_h_iptr(inode, bindex);
17926 +               if (h_inode) {
17927 +                       err = au_busy_or_stale();
17928 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
17929 +                               break;
17930 +
17931 +                       br = au_sbr(sb, bindex);
17932 +                       err = h_permission(h_inode, mask, au_br_mnt(br),
17933 +                                          br->br_perm);
17934 +               }
17935 +       }
17936 +
17937 +out:
17938 +       ii_read_unlock(inode);
17939 +       si_read_unlock(sb);
17940 +       return err;
17941 +}
17942 +
17943 +/* ---------------------------------------------------------------------- */
17944 +
17945 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
17946 +                                 unsigned int flags)
17947 +{
17948 +       struct dentry *ret, *parent;
17949 +       struct inode *inode;
17950 +       struct super_block *sb;
17951 +       int err, npositive;
17952 +
17953 +       IMustLock(dir);
17954 +
17955 +       /* todo: support rcu-walk? */
17956 +       ret = ERR_PTR(-ECHILD);
17957 +       if (flags & LOOKUP_RCU)
17958 +               goto out;
17959 +
17960 +       ret = ERR_PTR(-ENAMETOOLONG);
17961 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17962 +               goto out;
17963 +
17964 +       sb = dir->i_sb;
17965 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17966 +       ret = ERR_PTR(err);
17967 +       if (unlikely(err))
17968 +               goto out;
17969 +
17970 +       err = au_di_init(dentry);
17971 +       ret = ERR_PTR(err);
17972 +       if (unlikely(err))
17973 +               goto out_si;
17974 +
17975 +       inode = NULL;
17976 +       npositive = 0; /* suppress a warning */
17977 +       parent = dentry->d_parent; /* dir inode is locked */
17978 +       di_read_lock_parent(parent, AuLock_IR);
17979 +       err = au_alive_dir(parent);
17980 +       if (!err)
17981 +               err = au_digen_test(parent, au_sigen(sb));
17982 +       if (!err) {
17983 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
17984 +                                          /*type*/0);
17985 +               err = npositive;
17986 +       }
17987 +       di_read_unlock(parent, AuLock_IR);
17988 +       ret = ERR_PTR(err);
17989 +       if (unlikely(err < 0))
17990 +               goto out_unlock;
17991 +
17992 +       if (npositive) {
17993 +               inode = au_new_inode(dentry, /*must_new*/0);
17994 +               if (IS_ERR(inode)) {
17995 +                       ret = (void *)inode;
17996 +                       inode = NULL;
17997 +                       goto out_unlock;
17998 +               }
17999 +       }
18000 +
18001 +       if (inode)
18002 +               atomic_inc(&inode->i_count);
18003 +       ret = d_splice_alias(inode, dentry);
18004 +#if 0
18005 +       if (unlikely(d_need_lookup(dentry))) {
18006 +               spin_lock(&dentry->d_lock);
18007 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18008 +               spin_unlock(&dentry->d_lock);
18009 +       } else
18010 +#endif
18011 +       if (inode) {
18012 +               if (!IS_ERR(ret)) {
18013 +                       iput(inode);
18014 +                       if (ret && ret != dentry)
18015 +                               ii_write_unlock(inode);
18016 +               } else {
18017 +                       ii_write_unlock(inode);
18018 +                       iput(inode);
18019 +                       inode = NULL;
18020 +               }
18021 +       }
18022 +
18023 +out_unlock:
18024 +       di_write_unlock(dentry);
18025 +       if (inode) {
18026 +               /* verbose coding for lock class name */
18027 +               if (unlikely(S_ISLNK(inode->i_mode)))
18028 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18029 +                                   au_lc_key + AuLcSymlink_DIINFO);
18030 +               else if (unlikely(S_ISDIR(inode->i_mode)))
18031 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18032 +                                   au_lc_key + AuLcDir_DIINFO);
18033 +               else /* likely */
18034 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18035 +                                   au_lc_key + AuLcNonDir_DIINFO);
18036 +       }
18037 +out_si:
18038 +       si_read_unlock(sb);
18039 +out:
18040 +       return ret;
18041 +}
18042 +
18043 +/* ---------------------------------------------------------------------- */
18044 +
18045 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
18046 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
18047 +                         aufs_bindex_t bstart)
18048 +{
18049 +       int err;
18050 +       struct dentry *h_parent;
18051 +       struct inode *h_dir;
18052 +
18053 +       if (add_entry)
18054 +               IMustLock(parent->d_inode);
18055 +       else
18056 +               di_write_lock_parent(parent);
18057 +
18058 +       err = 0;
18059 +       if (!au_h_dptr(parent, bcpup)) {
18060 +               if (bstart > bcpup)
18061 +                       err = au_cpup_dirs(dentry, bcpup);
18062 +               else if (bstart < bcpup)
18063 +                       err = au_cpdown_dirs(dentry, bcpup);
18064 +               else
18065 +                       BUG();
18066 +       }
18067 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
18068 +               h_parent = au_h_dptr(parent, bcpup);
18069 +               h_dir = h_parent->d_inode;
18070 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
18071 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
18072 +               /* todo: no unlock here */
18073 +               mutex_unlock(&h_dir->i_mutex);
18074 +
18075 +               AuDbg("bcpup %d\n", bcpup);
18076 +               if (!err) {
18077 +                       if (!dentry->d_inode)
18078 +                               au_set_h_dptr(dentry, bstart, NULL);
18079 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
18080 +               }
18081 +       }
18082 +
18083 +       if (!add_entry)
18084 +               di_write_unlock(parent);
18085 +       if (!err)
18086 +               err = bcpup; /* success */
18087 +
18088 +       AuTraceErr(err);
18089 +       return err;
18090 +}
18091 +
18092 +/*
18093 + * decide the branch and the parent dir where we will create a new entry.
18094 + * returns new bindex or an error.
18095 + * copyup the parent dir if needed.
18096 + */
18097 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18098 +             struct au_wr_dir_args *args)
18099 +{
18100 +       int err;
18101 +       unsigned int flags;
18102 +       aufs_bindex_t bcpup, bstart, src_bstart;
18103 +       const unsigned char add_entry
18104 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
18105 +               | au_ftest_wrdir(args->flags, TMPFILE);
18106 +       struct super_block *sb;
18107 +       struct dentry *parent;
18108 +       struct au_sbinfo *sbinfo;
18109 +
18110 +       sb = dentry->d_sb;
18111 +       sbinfo = au_sbi(sb);
18112 +       parent = dget_parent(dentry);
18113 +       bstart = au_dbstart(dentry);
18114 +       bcpup = bstart;
18115 +       if (args->force_btgt < 0) {
18116 +               if (src_dentry) {
18117 +                       src_bstart = au_dbstart(src_dentry);
18118 +                       if (src_bstart < bstart)
18119 +                               bcpup = src_bstart;
18120 +               } else if (add_entry) {
18121 +                       flags = 0;
18122 +                       if (au_ftest_wrdir(args->flags, ISDIR))
18123 +                               au_fset_wbr(flags, DIR);
18124 +                       err = AuWbrCreate(sbinfo, dentry, flags);
18125 +                       bcpup = err;
18126 +               }
18127 +
18128 +               if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
18129 +                       if (add_entry)
18130 +                               err = AuWbrCopyup(sbinfo, dentry);
18131 +                       else {
18132 +                               if (!IS_ROOT(dentry)) {
18133 +                                       di_read_lock_parent(parent, !AuLock_IR);
18134 +                                       err = AuWbrCopyup(sbinfo, dentry);
18135 +                                       di_read_unlock(parent, !AuLock_IR);
18136 +                               } else
18137 +                                       err = AuWbrCopyup(sbinfo, dentry);
18138 +                       }
18139 +                       bcpup = err;
18140 +                       if (unlikely(err < 0))
18141 +                               goto out;
18142 +               }
18143 +       } else {
18144 +               bcpup = args->force_btgt;
18145 +               AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
18146 +       }
18147 +
18148 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
18149 +       err = bcpup;
18150 +       if (bcpup == bstart)
18151 +               goto out; /* success */
18152 +
18153 +       /* copyup the new parent into the branch we process */
18154 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
18155 +       if (err >= 0) {
18156 +               if (!dentry->d_inode) {
18157 +                       au_set_h_dptr(dentry, bstart, NULL);
18158 +                       au_set_dbstart(dentry, bcpup);
18159 +                       au_set_dbend(dentry, bcpup);
18160 +               }
18161 +               AuDebugOn(add_entry
18162 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
18163 +                         && !au_h_dptr(dentry, bcpup));
18164 +       }
18165 +
18166 +out:
18167 +       dput(parent);
18168 +       return err;
18169 +}
18170 +
18171 +/* ---------------------------------------------------------------------- */
18172 +
18173 +void au_pin_hdir_unlock(struct au_pin *p)
18174 +{
18175 +       if (p->hdir)
18176 +               au_hn_imtx_unlock(p->hdir);
18177 +}
18178 +
18179 +int au_pin_hdir_lock(struct au_pin *p)
18180 +{
18181 +       int err;
18182 +
18183 +       err = 0;
18184 +       if (!p->hdir)
18185 +               goto out;
18186 +
18187 +       /* even if an error happens later, keep this lock */
18188 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
18189 +
18190 +       err = -EBUSY;
18191 +       if (unlikely(p->hdir->hi_inode != p->h_parent->d_inode))
18192 +               goto out;
18193 +
18194 +       err = 0;
18195 +       if (p->h_dentry)
18196 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
18197 +                                 p->h_parent, p->br);
18198 +
18199 +out:
18200 +       return err;
18201 +}
18202 +
18203 +int au_pin_hdir_relock(struct au_pin *p)
18204 +{
18205 +       int err, i;
18206 +       struct inode *h_i;
18207 +       struct dentry *h_d[] = {
18208 +               p->h_dentry,
18209 +               p->h_parent
18210 +       };
18211 +
18212 +       err = au_pin_hdir_lock(p);
18213 +       if (unlikely(err))
18214 +               goto out;
18215 +
18216 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
18217 +               if (!h_d[i])
18218 +                       continue;
18219 +               h_i = h_d[i]->d_inode;
18220 +               if (h_i)
18221 +                       err = !h_i->i_nlink;
18222 +       }
18223 +
18224 +out:
18225 +       return err;
18226 +}
18227 +
18228 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
18229 +{
18230 +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
18231 +       p->hdir->hi_inode->i_mutex.owner = task;
18232 +#endif
18233 +}
18234 +
18235 +void au_pin_hdir_acquire_nest(struct au_pin *p)
18236 +{
18237 +       if (p->hdir) {
18238 +               mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
18239 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
18240 +               au_pin_hdir_set_owner(p, current);
18241 +       }
18242 +}
18243 +
18244 +void au_pin_hdir_release(struct au_pin *p)
18245 +{
18246 +       if (p->hdir) {
18247 +               au_pin_hdir_set_owner(p, p->task);
18248 +               mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
18249 +       }
18250 +}
18251 +
18252 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
18253 +{
18254 +       if (pin && pin->parent)
18255 +               return au_h_dptr(pin->parent, pin->bindex);
18256 +       return NULL;
18257 +}
18258 +
18259 +void au_unpin(struct au_pin *p)
18260 +{
18261 +       if (p->hdir)
18262 +               au_pin_hdir_unlock(p);
18263 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
18264 +               vfsub_mnt_drop_write(p->h_mnt);
18265 +       if (!p->hdir)
18266 +               return;
18267 +
18268 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18269 +               di_read_unlock(p->parent, AuLock_IR);
18270 +       iput(p->hdir->hi_inode);
18271 +       dput(p->parent);
18272 +       p->parent = NULL;
18273 +       p->hdir = NULL;
18274 +       p->h_mnt = NULL;
18275 +       /* do not clear p->task */
18276 +}
18277 +
18278 +int au_do_pin(struct au_pin *p)
18279 +{
18280 +       int err;
18281 +       struct super_block *sb;
18282 +       struct inode *h_dir;
18283 +
18284 +       err = 0;
18285 +       sb = p->dentry->d_sb;
18286 +       p->br = au_sbr(sb, p->bindex);
18287 +       if (IS_ROOT(p->dentry)) {
18288 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
18289 +                       p->h_mnt = au_br_mnt(p->br);
18290 +                       err = vfsub_mnt_want_write(p->h_mnt);
18291 +                       if (unlikely(err)) {
18292 +                               au_fclr_pin(p->flags, MNT_WRITE);
18293 +                               goto out_err;
18294 +                       }
18295 +               }
18296 +               goto out;
18297 +       }
18298 +
18299 +       p->h_dentry = NULL;
18300 +       if (p->bindex <= au_dbend(p->dentry))
18301 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
18302 +
18303 +       p->parent = dget_parent(p->dentry);
18304 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18305 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
18306 +
18307 +       h_dir = NULL;
18308 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
18309 +       p->hdir = au_hi(p->parent->d_inode, p->bindex);
18310 +       if (p->hdir)
18311 +               h_dir = p->hdir->hi_inode;
18312 +
18313 +       /*
18314 +        * udba case, or
18315 +        * if DI_LOCKED is not set, then p->parent may be different
18316 +        * and h_parent can be NULL.
18317 +        */
18318 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
18319 +               err = -EBUSY;
18320 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
18321 +                       di_read_unlock(p->parent, AuLock_IR);
18322 +               dput(p->parent);
18323 +               p->parent = NULL;
18324 +               goto out_err;
18325 +       }
18326 +
18327 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
18328 +               p->h_mnt = au_br_mnt(p->br);
18329 +               err = vfsub_mnt_want_write(p->h_mnt);
18330 +               if (unlikely(err)) {
18331 +                       au_fclr_pin(p->flags, MNT_WRITE);
18332 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
18333 +                               di_read_unlock(p->parent, AuLock_IR);
18334 +                       dput(p->parent);
18335 +                       p->parent = NULL;
18336 +                       goto out_err;
18337 +               }
18338 +       }
18339 +
18340 +       au_igrab(h_dir);
18341 +       err = au_pin_hdir_lock(p);
18342 +       if (!err)
18343 +               goto out; /* success */
18344 +
18345 +       au_unpin(p);
18346 +
18347 +out_err:
18348 +       pr_err("err %d\n", err);
18349 +       err = au_busy_or_stale();
18350 +out:
18351 +       return err;
18352 +}
18353 +
18354 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
18355 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18356 +                unsigned int udba, unsigned char flags)
18357 +{
18358 +       p->dentry = dentry;
18359 +       p->udba = udba;
18360 +       p->lsc_di = lsc_di;
18361 +       p->lsc_hi = lsc_hi;
18362 +       p->flags = flags;
18363 +       p->bindex = bindex;
18364 +
18365 +       p->parent = NULL;
18366 +       p->hdir = NULL;
18367 +       p->h_mnt = NULL;
18368 +
18369 +       p->h_dentry = NULL;
18370 +       p->h_parent = NULL;
18371 +       p->br = NULL;
18372 +       p->task = current;
18373 +}
18374 +
18375 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18376 +          unsigned int udba, unsigned char flags)
18377 +{
18378 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
18379 +                   udba, flags);
18380 +       return au_do_pin(pin);
18381 +}
18382 +
18383 +/* ---------------------------------------------------------------------- */
18384 +
18385 +/*
18386 + * ->setattr() and ->getattr() are called in various cases.
18387 + * chmod, stat: dentry is revalidated.
18388 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
18389 + *               unhashed.
18390 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
18391 + */
18392 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
18393 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
18394 +{
18395 +       int err;
18396 +       struct inode *inode;
18397 +       struct dentry *parent;
18398 +
18399 +       err = 0;
18400 +       inode = dentry->d_inode;
18401 +       if (au_digen_test(dentry, sigen)) {
18402 +               parent = dget_parent(dentry);
18403 +               di_read_lock_parent(parent, AuLock_IR);
18404 +               err = au_refresh_dentry(dentry, parent);
18405 +               di_read_unlock(parent, AuLock_IR);
18406 +               dput(parent);
18407 +       }
18408 +
18409 +       AuTraceErr(err);
18410 +       return err;
18411 +}
18412 +
18413 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18414 +                    struct au_icpup_args *a)
18415 +{
18416 +       int err;
18417 +       loff_t sz;
18418 +       aufs_bindex_t bstart, ibstart;
18419 +       struct dentry *hi_wh, *parent;
18420 +       struct inode *inode;
18421 +       struct au_wr_dir_args wr_dir_args = {
18422 +               .force_btgt     = -1,
18423 +               .flags          = 0
18424 +       };
18425 +
18426 +       if (d_is_dir(dentry))
18427 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
18428 +       /* plink or hi_wh() case */
18429 +       bstart = au_dbstart(dentry);
18430 +       inode = dentry->d_inode;
18431 +       ibstart = au_ibstart(inode);
18432 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
18433 +               wr_dir_args.force_btgt = ibstart;
18434 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
18435 +       if (unlikely(err < 0))
18436 +               goto out;
18437 +       a->btgt = err;
18438 +       if (err != bstart)
18439 +               au_fset_icpup(a->flags, DID_CPUP);
18440 +
18441 +       err = 0;
18442 +       a->pin_flags = AuPin_MNT_WRITE;
18443 +       parent = NULL;
18444 +       if (!IS_ROOT(dentry)) {
18445 +               au_fset_pin(a->pin_flags, DI_LOCKED);
18446 +               parent = dget_parent(dentry);
18447 +               di_write_lock_parent(parent);
18448 +       }
18449 +
18450 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
18451 +       if (unlikely(err))
18452 +               goto out_parent;
18453 +
18454 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
18455 +       a->h_inode = a->h_path.dentry->d_inode;
18456 +       sz = -1;
18457 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
18458 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
18459 +               if (ia->ia_size < i_size_read(a->h_inode))
18460 +                       sz = ia->ia_size;
18461 +               mutex_unlock(&a->h_inode->i_mutex);
18462 +       }
18463 +
18464 +       hi_wh = NULL;
18465 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
18466 +               hi_wh = au_hi_wh(inode, a->btgt);
18467 +               if (!hi_wh) {
18468 +                       struct au_cp_generic cpg = {
18469 +                               .dentry = dentry,
18470 +                               .bdst   = a->btgt,
18471 +                               .bsrc   = -1,
18472 +                               .len    = sz,
18473 +                               .pin    = &a->pin
18474 +                       };
18475 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
18476 +                       if (unlikely(err))
18477 +                               goto out_unlock;
18478 +                       hi_wh = au_hi_wh(inode, a->btgt);
18479 +                       /* todo: revalidate hi_wh? */
18480 +               }
18481 +       }
18482 +
18483 +       if (parent) {
18484 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
18485 +               di_downgrade_lock(parent, AuLock_IR);
18486 +               dput(parent);
18487 +               parent = NULL;
18488 +       }
18489 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
18490 +               goto out; /* success */
18491 +
18492 +       if (!d_unhashed(dentry)) {
18493 +               struct au_cp_generic cpg = {
18494 +                       .dentry = dentry,
18495 +                       .bdst   = a->btgt,
18496 +                       .bsrc   = bstart,
18497 +                       .len    = sz,
18498 +                       .pin    = &a->pin,
18499 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
18500 +               };
18501 +               err = au_sio_cpup_simple(&cpg);
18502 +               if (!err)
18503 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
18504 +       } else if (!hi_wh)
18505 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
18506 +       else
18507 +               a->h_path.dentry = hi_wh; /* do not dget here */
18508 +
18509 +out_unlock:
18510 +       a->h_inode = a->h_path.dentry->d_inode;
18511 +       if (!err)
18512 +               goto out; /* success */
18513 +       au_unpin(&a->pin);
18514 +out_parent:
18515 +       if (parent) {
18516 +               di_write_unlock(parent);
18517 +               dput(parent);
18518 +       }
18519 +out:
18520 +       if (!err)
18521 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
18522 +       return err;
18523 +}
18524 +
18525 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
18526 +{
18527 +       int err;
18528 +       struct inode *inode, *delegated;
18529 +       struct super_block *sb;
18530 +       struct file *file;
18531 +       struct au_icpup_args *a;
18532 +
18533 +       inode = dentry->d_inode;
18534 +       IMustLock(inode);
18535 +
18536 +       err = -ENOMEM;
18537 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18538 +       if (unlikely(!a))
18539 +               goto out;
18540 +
18541 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
18542 +               ia->ia_valid &= ~ATTR_MODE;
18543 +
18544 +       file = NULL;
18545 +       sb = dentry->d_sb;
18546 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18547 +       if (unlikely(err))
18548 +               goto out_kfree;
18549 +
18550 +       if (ia->ia_valid & ATTR_FILE) {
18551 +               /* currently ftruncate(2) only */
18552 +               AuDebugOn(!d_is_reg(dentry));
18553 +               file = ia->ia_file;
18554 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
18555 +               if (unlikely(err))
18556 +                       goto out_si;
18557 +               ia->ia_file = au_hf_top(file);
18558 +               a->udba = AuOpt_UDBA_NONE;
18559 +       } else {
18560 +               /* fchmod() doesn't pass ia_file */
18561 +               a->udba = au_opt_udba(sb);
18562 +               di_write_lock_child(dentry);
18563 +               /* no d_unlinked(), to set UDBA_NONE for root */
18564 +               if (d_unhashed(dentry))
18565 +                       a->udba = AuOpt_UDBA_NONE;
18566 +               if (a->udba != AuOpt_UDBA_NONE) {
18567 +                       AuDebugOn(IS_ROOT(dentry));
18568 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
18569 +                       if (unlikely(err))
18570 +                               goto out_dentry;
18571 +               }
18572 +       }
18573 +
18574 +       err = au_pin_and_icpup(dentry, ia, a);
18575 +       if (unlikely(err < 0))
18576 +               goto out_dentry;
18577 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
18578 +               ia->ia_file = NULL;
18579 +               ia->ia_valid &= ~ATTR_FILE;
18580 +       }
18581 +
18582 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
18583 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
18584 +           == (ATTR_MODE | ATTR_CTIME)) {
18585 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
18586 +               if (unlikely(err))
18587 +                       goto out_unlock;
18588 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
18589 +                  && (ia->ia_valid & ATTR_CTIME)) {
18590 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
18591 +               if (unlikely(err))
18592 +                       goto out_unlock;
18593 +       }
18594 +
18595 +       if (ia->ia_valid & ATTR_SIZE) {
18596 +               struct file *f;
18597 +
18598 +               if (ia->ia_size < i_size_read(inode))
18599 +                       /* unmap only */
18600 +                       truncate_setsize(inode, ia->ia_size);
18601 +
18602 +               f = NULL;
18603 +               if (ia->ia_valid & ATTR_FILE)
18604 +                       f = ia->ia_file;
18605 +               mutex_unlock(&a->h_inode->i_mutex);
18606 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
18607 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
18608 +       } else {
18609 +               delegated = NULL;
18610 +               while (1) {
18611 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
18612 +                       if (delegated) {
18613 +                               err = break_deleg_wait(&delegated);
18614 +                               if (!err)
18615 +                                       continue;
18616 +                       }
18617 +                       break;
18618 +               }
18619 +       }
18620 +       if (!err)
18621 +               au_cpup_attr_changeable(inode);
18622 +
18623 +out_unlock:
18624 +       mutex_unlock(&a->h_inode->i_mutex);
18625 +       au_unpin(&a->pin);
18626 +       if (unlikely(err))
18627 +               au_update_dbstart(dentry);
18628 +out_dentry:
18629 +       di_write_unlock(dentry);
18630 +       if (file) {
18631 +               fi_write_unlock(file);
18632 +               ia->ia_file = file;
18633 +               ia->ia_valid |= ATTR_FILE;
18634 +       }
18635 +out_si:
18636 +       si_read_unlock(sb);
18637 +out_kfree:
18638 +       kfree(a);
18639 +out:
18640 +       AuTraceErr(err);
18641 +       return err;
18642 +}
18643 +
18644 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18645 +static int au_h_path_to_set_attr(struct dentry *dentry,
18646 +                                struct au_icpup_args *a, struct path *h_path)
18647 +{
18648 +       int err;
18649 +       struct super_block *sb;
18650 +
18651 +       sb = dentry->d_sb;
18652 +       a->udba = au_opt_udba(sb);
18653 +       /* no d_unlinked(), to set UDBA_NONE for root */
18654 +       if (d_unhashed(dentry))
18655 +               a->udba = AuOpt_UDBA_NONE;
18656 +       if (a->udba != AuOpt_UDBA_NONE) {
18657 +               AuDebugOn(IS_ROOT(dentry));
18658 +               err = au_reval_for_attr(dentry, au_sigen(sb));
18659 +               if (unlikely(err))
18660 +                       goto out;
18661 +       }
18662 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
18663 +       if (unlikely(err < 0))
18664 +               goto out;
18665 +
18666 +       h_path->dentry = a->h_path.dentry;
18667 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
18668 +
18669 +out:
18670 +       return err;
18671 +}
18672 +
18673 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg)
18674 +{
18675 +       int err;
18676 +       struct path h_path;
18677 +       struct super_block *sb;
18678 +       struct au_icpup_args *a;
18679 +       struct inode *inode, *h_inode;
18680 +
18681 +       inode = dentry->d_inode;
18682 +       IMustLock(inode);
18683 +
18684 +       err = -ENOMEM;
18685 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18686 +       if (unlikely(!a))
18687 +               goto out;
18688 +
18689 +       sb = dentry->d_sb;
18690 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18691 +       if (unlikely(err))
18692 +               goto out_kfree;
18693 +
18694 +       h_path.dentry = NULL;   /* silence gcc */
18695 +       di_write_lock_child(dentry);
18696 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
18697 +       if (unlikely(err))
18698 +               goto out_di;
18699 +
18700 +       mutex_unlock(&a->h_inode->i_mutex);
18701 +       switch (arg->type) {
18702 +       case AU_XATTR_SET:
18703 +               err = vfsub_setxattr(h_path.dentry,
18704 +                                    arg->u.set.name, arg->u.set.value,
18705 +                                    arg->u.set.size, arg->u.set.flags);
18706 +               break;
18707 +       case AU_XATTR_REMOVE:
18708 +               err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
18709 +               break;
18710 +       case AU_ACL_SET:
18711 +               err = -EOPNOTSUPP;
18712 +               h_inode = h_path.dentry->d_inode;
18713 +               if (h_inode->i_op->set_acl)
18714 +                       err = h_inode->i_op->set_acl(h_inode,
18715 +                                                    arg->u.acl_set.acl,
18716 +                                                    arg->u.acl_set.type);
18717 +               break;
18718 +       }
18719 +       if (!err)
18720 +               au_cpup_attr_timesizes(inode);
18721 +
18722 +       au_unpin(&a->pin);
18723 +       if (unlikely(err))
18724 +               au_update_dbstart(dentry);
18725 +
18726 +out_di:
18727 +       di_write_unlock(dentry);
18728 +       si_read_unlock(sb);
18729 +out_kfree:
18730 +       kfree(a);
18731 +out:
18732 +       AuTraceErr(err);
18733 +       return err;
18734 +}
18735 +#endif
18736 +
18737 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
18738 +                            unsigned int nlink)
18739 +{
18740 +       unsigned int n;
18741 +
18742 +       inode->i_mode = st->mode;
18743 +       /* don't i_[ug]id_write() here */
18744 +       inode->i_uid = st->uid;
18745 +       inode->i_gid = st->gid;
18746 +       inode->i_atime = st->atime;
18747 +       inode->i_mtime = st->mtime;
18748 +       inode->i_ctime = st->ctime;
18749 +
18750 +       au_cpup_attr_nlink(inode, /*force*/0);
18751 +       if (S_ISDIR(inode->i_mode)) {
18752 +               n = inode->i_nlink;
18753 +               n -= nlink;
18754 +               n += st->nlink;
18755 +               smp_mb(); /* for i_nlink */
18756 +               /* 0 can happen */
18757 +               set_nlink(inode, n);
18758 +       }
18759 +
18760 +       spin_lock(&inode->i_lock);
18761 +       inode->i_blocks = st->blocks;
18762 +       i_size_write(inode, st->size);
18763 +       spin_unlock(&inode->i_lock);
18764 +}
18765 +
18766 +/*
18767 + * common routine for aufs_getattr() and aufs_getxattr().
18768 + * returns zero or negative (an error).
18769 + * @dentry will be read-locked in success.
18770 + */
18771 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
18772 +{
18773 +       int err;
18774 +       unsigned int mnt_flags, sigen;
18775 +       unsigned char udba_none;
18776 +       aufs_bindex_t bindex;
18777 +       struct super_block *sb, *h_sb;
18778 +       struct inode *inode;
18779 +
18780 +       h_path->mnt = NULL;
18781 +       h_path->dentry = NULL;
18782 +
18783 +       err = 0;
18784 +       sb = dentry->d_sb;
18785 +       mnt_flags = au_mntflags(sb);
18786 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
18787 +
18788 +       /* support fstat(2) */
18789 +       if (!d_unlinked(dentry) && !udba_none) {
18790 +               sigen = au_sigen(sb);
18791 +               err = au_digen_test(dentry, sigen);
18792 +               if (!err) {
18793 +                       di_read_lock_child(dentry, AuLock_IR);
18794 +                       err = au_dbrange_test(dentry);
18795 +                       if (unlikely(err)) {
18796 +                               di_read_unlock(dentry, AuLock_IR);
18797 +                               goto out;
18798 +                       }
18799 +               } else {
18800 +                       AuDebugOn(IS_ROOT(dentry));
18801 +                       di_write_lock_child(dentry);
18802 +                       err = au_dbrange_test(dentry);
18803 +                       if (!err)
18804 +                               err = au_reval_for_attr(dentry, sigen);
18805 +                       if (!err)
18806 +                               di_downgrade_lock(dentry, AuLock_IR);
18807 +                       else {
18808 +                               di_write_unlock(dentry);
18809 +                               goto out;
18810 +                       }
18811 +               }
18812 +       } else
18813 +               di_read_lock_child(dentry, AuLock_IR);
18814 +
18815 +       inode = dentry->d_inode;
18816 +       bindex = au_ibstart(inode);
18817 +       h_path->mnt = au_sbr_mnt(sb, bindex);
18818 +       h_sb = h_path->mnt->mnt_sb;
18819 +       if (!force
18820 +           && !au_test_fs_bad_iattr(h_sb)
18821 +           && udba_none)
18822 +               goto out; /* success */
18823 +
18824 +       if (au_dbstart(dentry) == bindex)
18825 +               h_path->dentry = au_h_dptr(dentry, bindex);
18826 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
18827 +               h_path->dentry = au_plink_lkup(inode, bindex);
18828 +               if (IS_ERR(h_path->dentry))
18829 +                       /* pretending success */
18830 +                       h_path->dentry = NULL;
18831 +               else
18832 +                       dput(h_path->dentry);
18833 +       }
18834 +
18835 +out:
18836 +       return err;
18837 +}
18838 +
18839 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
18840 +                       struct dentry *dentry, struct kstat *st)
18841 +{
18842 +       int err;
18843 +       unsigned char positive;
18844 +       struct path h_path;
18845 +       struct inode *inode;
18846 +       struct super_block *sb;
18847 +
18848 +       inode = dentry->d_inode;
18849 +       sb = dentry->d_sb;
18850 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18851 +       if (unlikely(err))
18852 +               goto out;
18853 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path);
18854 +       if (unlikely(err))
18855 +               goto out_si;
18856 +       if (unlikely(!h_path.dentry))
18857 +               /* illegally overlapped or something */
18858 +               goto out_fill; /* pretending success */
18859 +
18860 +       positive = !!h_path.dentry->d_inode;
18861 +       if (positive)
18862 +               err = vfs_getattr(&h_path, st);
18863 +       if (!err) {
18864 +               if (positive)
18865 +                       au_refresh_iattr(inode, st,
18866 +                                        h_path.dentry->d_inode->i_nlink);
18867 +               goto out_fill; /* success */
18868 +       }
18869 +       AuTraceErr(err);
18870 +       goto out_di;
18871 +
18872 +out_fill:
18873 +       generic_fillattr(inode, st);
18874 +out_di:
18875 +       di_read_unlock(dentry, AuLock_IR);
18876 +out_si:
18877 +       si_read_unlock(sb);
18878 +out:
18879 +       AuTraceErr(err);
18880 +       return err;
18881 +}
18882 +
18883 +/* ---------------------------------------------------------------------- */
18884 +
18885 +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
18886 +                     int bufsiz)
18887 +{
18888 +       int err;
18889 +       struct super_block *sb;
18890 +       struct dentry *h_dentry;
18891 +
18892 +       err = -EINVAL;
18893 +       h_dentry = au_h_dptr(dentry, bindex);
18894 +       if (unlikely(!h_dentry->d_inode->i_op->readlink))
18895 +               goto out;
18896 +
18897 +       err = security_inode_readlink(h_dentry);
18898 +       if (unlikely(err))
18899 +               goto out;
18900 +
18901 +       sb = dentry->d_sb;
18902 +       if (!au_test_ro(sb, bindex, dentry->d_inode)) {
18903 +               vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
18904 +               fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
18905 +       }
18906 +       err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
18907 +
18908 +out:
18909 +       return err;
18910 +}
18911 +
18912 +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
18913 +{
18914 +       int err;
18915 +
18916 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
18917 +       if (unlikely(err))
18918 +               goto out;
18919 +       err = au_d_hashed_positive(dentry);
18920 +       if (!err)
18921 +               err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
18922 +       aufs_read_unlock(dentry, AuLock_IR);
18923 +
18924 +out:
18925 +       return err;
18926 +}
18927 +
18928 +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
18929 +{
18930 +       int err;
18931 +       mm_segment_t old_fs;
18932 +       union {
18933 +               char *k;
18934 +               char __user *u;
18935 +       } buf;
18936 +
18937 +       err = -ENOMEM;
18938 +       buf.k = (void *)__get_free_page(GFP_NOFS);
18939 +       if (unlikely(!buf.k))
18940 +               goto out;
18941 +
18942 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
18943 +       if (unlikely(err))
18944 +               goto out_name;
18945 +
18946 +       err = au_d_hashed_positive(dentry);
18947 +       if (!err) {
18948 +               old_fs = get_fs();
18949 +               set_fs(KERNEL_DS);
18950 +               err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
18951 +               set_fs(old_fs);
18952 +       }
18953 +       aufs_read_unlock(dentry, AuLock_IR);
18954 +
18955 +       if (err >= 0) {
18956 +               buf.k[err] = 0;
18957 +               /* will be freed by put_link */
18958 +               nd_set_link(nd, buf.k);
18959 +               return NULL; /* success */
18960 +       }
18961 +
18962 +out_name:
18963 +       free_page((unsigned long)buf.k);
18964 +out:
18965 +       AuTraceErr(err);
18966 +       return ERR_PTR(err);
18967 +}
18968 +
18969 +static void aufs_put_link(struct dentry *dentry __maybe_unused,
18970 +                         struct nameidata *nd, void *cookie __maybe_unused)
18971 +{
18972 +       char *p;
18973 +
18974 +       p = nd_get_link(nd);
18975 +       if (!IS_ERR_OR_NULL(p))
18976 +               free_page((unsigned long)p);
18977 +}
18978 +
18979 +/* ---------------------------------------------------------------------- */
18980 +
18981 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
18982 +{
18983 +       int err;
18984 +       struct super_block *sb;
18985 +       struct inode *h_inode;
18986 +
18987 +       sb = inode->i_sb;
18988 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
18989 +       lockdep_off();
18990 +       si_read_lock(sb, AuLock_FLUSH);
18991 +       ii_write_lock_child(inode);
18992 +       lockdep_on();
18993 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
18994 +       err = vfsub_update_time(h_inode, ts, flags);
18995 +       lockdep_off();
18996 +       if (!err)
18997 +               au_cpup_attr_timesizes(inode);
18998 +       ii_write_unlock(inode);
18999 +       si_read_unlock(sb);
19000 +       lockdep_on();
19001 +
19002 +       if (!err && (flags & S_VERSION))
19003 +               inode_inc_iversion(inode);
19004 +
19005 +       return err;
19006 +}
19007 +
19008 +/* ---------------------------------------------------------------------- */
19009 +
19010 +struct inode_operations aufs_symlink_iop = {
19011 +       .permission     = aufs_permission,
19012 +#ifdef CONFIG_FS_POSIX_ACL
19013 +       .get_acl        = aufs_get_acl,
19014 +       .set_acl        = aufs_set_acl, /* unsupport for symlink? */
19015 +#endif
19016 +
19017 +       .setattr        = aufs_setattr,
19018 +       .getattr        = aufs_getattr,
19019 +
19020 +#ifdef CONFIG_AUFS_XATTR
19021 +       .setxattr       = aufs_setxattr,
19022 +       .getxattr       = aufs_getxattr,
19023 +       .listxattr      = aufs_listxattr,
19024 +       .removexattr    = aufs_removexattr,
19025 +#endif
19026 +
19027 +       .readlink       = aufs_readlink,
19028 +       .follow_link    = aufs_follow_link,
19029 +       .put_link       = aufs_put_link,
19030 +
19031 +       /* .update_time = aufs_update_time */
19032 +};
19033 +
19034 +struct inode_operations aufs_dir_iop = {
19035 +       .create         = aufs_create,
19036 +       .lookup         = aufs_lookup,
19037 +       .link           = aufs_link,
19038 +       .unlink         = aufs_unlink,
19039 +       .symlink        = aufs_symlink,
19040 +       .mkdir          = aufs_mkdir,
19041 +       .rmdir          = aufs_rmdir,
19042 +       .mknod          = aufs_mknod,
19043 +       .rename         = aufs_rename,
19044 +
19045 +       .permission     = aufs_permission,
19046 +#ifdef CONFIG_FS_POSIX_ACL
19047 +       .get_acl        = aufs_get_acl,
19048 +       .set_acl        = aufs_set_acl,
19049 +#endif
19050 +
19051 +       .setattr        = aufs_setattr,
19052 +       .getattr        = aufs_getattr,
19053 +
19054 +#ifdef CONFIG_AUFS_XATTR
19055 +       .setxattr       = aufs_setxattr,
19056 +       .getxattr       = aufs_getxattr,
19057 +       .listxattr      = aufs_listxattr,
19058 +       .removexattr    = aufs_removexattr,
19059 +#endif
19060 +
19061 +       .update_time    = aufs_update_time,
19062 +       /* no support for atomic_open() */
19063 +
19064 +       .tmpfile        = aufs_tmpfile
19065 +};
19066 +
19067 +struct inode_operations aufs_iop = {
19068 +       .permission     = aufs_permission,
19069 +#ifdef CONFIG_FS_POSIX_ACL
19070 +       .get_acl        = aufs_get_acl,
19071 +       .set_acl        = aufs_set_acl,
19072 +#endif
19073 +
19074 +       .setattr        = aufs_setattr,
19075 +       .getattr        = aufs_getattr,
19076 +
19077 +#ifdef CONFIG_AUFS_XATTR
19078 +       .setxattr       = aufs_setxattr,
19079 +       .getxattr       = aufs_getxattr,
19080 +       .listxattr      = aufs_listxattr,
19081 +       .removexattr    = aufs_removexattr,
19082 +#endif
19083 +
19084 +       .update_time    = aufs_update_time
19085 +};
19086 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
19087 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
19088 +++ linux/fs/aufs/i_op_del.c    2015-04-13 15:10:20.786823613 +0200
19089 @@ -0,0 +1,507 @@
19090 +/*
19091 + * Copyright (C) 2005-2015 Junjiro R. Okajima
19092 + *
19093 + * This program, aufs is free software; you can redistribute it and/or modify
19094 + * it under the terms of the GNU General Public License as published by
19095 + * the Free Software Foundation; either version 2 of the License, or
19096 + * (at your option) any later version.
19097 + *
19098 + * This program is distributed in the hope that it will be useful,
19099 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19100 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19101 + * GNU General Public License for more details.
19102 + *
19103 + * You should have received a copy of the GNU General Public License
19104 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19105 + */
19106 +
19107 +/*
19108 + * inode operations (del entry)
19109 + */
19110 +
19111 +#include "aufs.h"
19112 +
19113 +/*
19114 + * decide if a new whiteout for @dentry is necessary or not.
19115 + * when it is necessary, prepare the parent dir for the upper branch whose
19116 + * branch index is @bcpup for creation. the actual creation of the whiteout will
19117 + * be done by caller.
19118 + * return value:
19119 + * 0: wh is unnecessary
19120 + * plus: wh is necessary
19121 + * minus: error
19122 + */
19123 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
19124 +{
19125 +       int need_wh, err;
19126 +       aufs_bindex_t bstart;
19127 +       struct super_block *sb;
19128 +
19129 +       sb = dentry->d_sb;
19130 +       bstart = au_dbstart(dentry);
19131 +       if (*bcpup < 0) {
19132 +               *bcpup = bstart;
19133 +               if (au_test_ro(sb, bstart, dentry->d_inode)) {
19134 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
19135 +                       *bcpup = err;
19136 +                       if (unlikely(err < 0))
19137 +                               goto out;
19138 +               }
19139 +       } else
19140 +               AuDebugOn(bstart < *bcpup
19141 +                         || au_test_ro(sb, *bcpup, dentry->d_inode));
19142 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
19143 +
19144 +       if (*bcpup != bstart) {
19145 +               err = au_cpup_dirs(dentry, *bcpup);
19146 +               if (unlikely(err))
19147 +                       goto out;
19148 +               need_wh = 1;
19149 +       } else {
19150 +               struct au_dinfo *dinfo, *tmp;
19151 +
19152 +               need_wh = -ENOMEM;
19153 +               dinfo = au_di(dentry);
19154 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
19155 +               if (tmp) {
19156 +                       au_di_cp(tmp, dinfo);
19157 +                       au_di_swap(tmp, dinfo);
19158 +                       /* returns the number of positive dentries */
19159 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
19160 +                       au_di_swap(tmp, dinfo);
19161 +                       au_rw_write_unlock(&tmp->di_rwsem);
19162 +                       au_di_free(tmp);
19163 +               }
19164 +       }
19165 +       AuDbg("need_wh %d\n", need_wh);
19166 +       err = need_wh;
19167 +
19168 +out:
19169 +       return err;
19170 +}
19171 +
19172 +/*
19173 + * simple tests for the del-entry operations.
19174 + * following the checks in vfs, plus the parent-child relationship.
19175 + */
19176 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19177 +              struct dentry *h_parent, int isdir)
19178 +{
19179 +       int err;
19180 +       umode_t h_mode;
19181 +       struct dentry *h_dentry, *h_latest;
19182 +       struct inode *h_inode;
19183 +
19184 +       h_dentry = au_h_dptr(dentry, bindex);
19185 +       h_inode = h_dentry->d_inode;
19186 +       if (dentry->d_inode) {
19187 +               err = -ENOENT;
19188 +               if (unlikely(!h_inode || !h_inode->i_nlink))
19189 +                       goto out;
19190 +
19191 +               h_mode = h_inode->i_mode;
19192 +               if (!isdir) {
19193 +                       err = -EISDIR;
19194 +                       if (unlikely(S_ISDIR(h_mode)))
19195 +                               goto out;
19196 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19197 +                       err = -ENOTDIR;
19198 +                       goto out;
19199 +               }
19200 +       } else {
19201 +               /* rename(2) case */
19202 +               err = -EIO;
19203 +               if (unlikely(h_inode))
19204 +                       goto out;
19205 +       }
19206 +
19207 +       err = -ENOENT;
19208 +       /* expected parent dir is locked */
19209 +       if (unlikely(h_parent != h_dentry->d_parent))
19210 +               goto out;
19211 +       err = 0;
19212 +
19213 +       /*
19214 +        * rmdir a dir may break the consistency on some filesystem.
19215 +        * let's try heavy test.
19216 +        */
19217 +       err = -EACCES;
19218 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
19219 +                    && au_test_h_perm(h_parent->d_inode,
19220 +                                      MAY_EXEC | MAY_WRITE)))
19221 +               goto out;
19222 +
19223 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
19224 +       err = -EIO;
19225 +       if (IS_ERR(h_latest))
19226 +               goto out;
19227 +       if (h_latest == h_dentry)
19228 +               err = 0;
19229 +       dput(h_latest);
19230 +
19231 +out:
19232 +       return err;
19233 +}
19234 +
19235 +/*
19236 + * decide the branch where we operate for @dentry. the branch index will be set
19237 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
19238 + * dir for reverting.
19239 + * when a new whiteout is necessary, create it.
19240 + */
19241 +static struct dentry*
19242 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
19243 +                   struct au_dtime *dt, struct au_pin *pin)
19244 +{
19245 +       struct dentry *wh_dentry;
19246 +       struct super_block *sb;
19247 +       struct path h_path;
19248 +       int err, need_wh;
19249 +       unsigned int udba;
19250 +       aufs_bindex_t bcpup;
19251 +
19252 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
19253 +       wh_dentry = ERR_PTR(need_wh);
19254 +       if (unlikely(need_wh < 0))
19255 +               goto out;
19256 +
19257 +       sb = dentry->d_sb;
19258 +       udba = au_opt_udba(sb);
19259 +       bcpup = *rbcpup;
19260 +       err = au_pin(pin, dentry, bcpup, udba,
19261 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19262 +       wh_dentry = ERR_PTR(err);
19263 +       if (unlikely(err))
19264 +               goto out;
19265 +
19266 +       h_path.dentry = au_pinned_h_parent(pin);
19267 +       if (udba != AuOpt_UDBA_NONE
19268 +           && au_dbstart(dentry) == bcpup) {
19269 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
19270 +               wh_dentry = ERR_PTR(err);
19271 +               if (unlikely(err))
19272 +                       goto out_unpin;
19273 +       }
19274 +
19275 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
19276 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
19277 +       wh_dentry = NULL;
19278 +       if (!need_wh)
19279 +               goto out; /* success, no need to create whiteout */
19280 +
19281 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
19282 +       if (IS_ERR(wh_dentry))
19283 +               goto out_unpin;
19284 +
19285 +       /* returns with the parent is locked and wh_dentry is dget-ed */
19286 +       goto out; /* success */
19287 +
19288 +out_unpin:
19289 +       au_unpin(pin);
19290 +out:
19291 +       return wh_dentry;
19292 +}
19293 +
19294 +/*
19295 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
19296 + * in order to be revertible and save time for removing many child whiteouts
19297 + * under the dir.
19298 + * returns 1 when there are too many child whiteout and caller should remove
19299 + * them asynchronously. returns 0 when the number of children is enough small to
19300 + * remove now or the branch fs is a remote fs.
19301 + * otherwise return an error.
19302 + */
19303 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
19304 +                          struct au_nhash *whlist, struct inode *dir)
19305 +{
19306 +       int rmdir_later, err, dirwh;
19307 +       struct dentry *h_dentry;
19308 +       struct super_block *sb;
19309 +
19310 +       sb = dentry->d_sb;
19311 +       SiMustAnyLock(sb);
19312 +       h_dentry = au_h_dptr(dentry, bindex);
19313 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
19314 +       if (unlikely(err))
19315 +               goto out;
19316 +
19317 +       /* stop monitoring */
19318 +       au_hn_free(au_hi(dentry->d_inode, bindex));
19319 +
19320 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
19321 +               dirwh = au_sbi(sb)->si_dirwh;
19322 +               rmdir_later = (dirwh <= 1);
19323 +               if (!rmdir_later)
19324 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
19325 +                                                             dirwh);
19326 +               if (rmdir_later)
19327 +                       return rmdir_later;
19328 +       }
19329 +
19330 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
19331 +       if (unlikely(err)) {
19332 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
19333 +                       h_dentry, bindex, err);
19334 +               err = 0;
19335 +       }
19336 +
19337 +out:
19338 +       AuTraceErr(err);
19339 +       return err;
19340 +}
19341 +
19342 +/*
19343 + * final procedure for deleting a entry.
19344 + * maintain dentry and iattr.
19345 + */
19346 +static void epilog(struct inode *dir, struct dentry *dentry,
19347 +                  aufs_bindex_t bindex)
19348 +{
19349 +       struct inode *inode;
19350 +
19351 +       inode = dentry->d_inode;
19352 +       d_drop(dentry);
19353 +       inode->i_ctime = dir->i_ctime;
19354 +
19355 +       if (au_ibstart(dir) == bindex)
19356 +               au_cpup_attr_timesizes(dir);
19357 +       dir->i_version++;
19358 +}
19359 +
19360 +/*
19361 + * when an error happened, remove the created whiteout and revert everything.
19362 + */
19363 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
19364 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
19365 +                    struct dentry *dentry, struct au_dtime *dt)
19366 +{
19367 +       int rerr;
19368 +       struct path h_path = {
19369 +               .dentry = wh_dentry,
19370 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
19371 +       };
19372 +
19373 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
19374 +       if (!rerr) {
19375 +               au_set_dbwh(dentry, bwh);
19376 +               au_dtime_revert(dt);
19377 +               return 0;
19378 +       }
19379 +
19380 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
19381 +       return -EIO;
19382 +}
19383 +
19384 +/* ---------------------------------------------------------------------- */
19385 +
19386 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
19387 +{
19388 +       int err;
19389 +       aufs_bindex_t bwh, bindex, bstart;
19390 +       struct inode *inode, *h_dir, *delegated;
19391 +       struct dentry *parent, *wh_dentry;
19392 +       /* to reuduce stack size */
19393 +       struct {
19394 +               struct au_dtime dt;
19395 +               struct au_pin pin;
19396 +               struct path h_path;
19397 +       } *a;
19398 +
19399 +       IMustLock(dir);
19400 +
19401 +       err = -ENOMEM;
19402 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19403 +       if (unlikely(!a))
19404 +               goto out;
19405 +
19406 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19407 +       if (unlikely(err))
19408 +               goto out_free;
19409 +       err = au_d_hashed_positive(dentry);
19410 +       if (unlikely(err))
19411 +               goto out_unlock;
19412 +       inode = dentry->d_inode;
19413 +       IMustLock(inode);
19414 +       err = -EISDIR;
19415 +       if (unlikely(d_is_dir(dentry)))
19416 +               goto out_unlock; /* possible? */
19417 +
19418 +       bstart = au_dbstart(dentry);
19419 +       bwh = au_dbwh(dentry);
19420 +       bindex = -1;
19421 +       parent = dentry->d_parent; /* dir inode is locked */
19422 +       di_write_lock_parent(parent);
19423 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
19424 +                                       &a->pin);
19425 +       err = PTR_ERR(wh_dentry);
19426 +       if (IS_ERR(wh_dentry))
19427 +               goto out_parent;
19428 +
19429 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
19430 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
19431 +       dget(a->h_path.dentry);
19432 +       if (bindex == bstart) {
19433 +               h_dir = au_pinned_h_dir(&a->pin);
19434 +               delegated = NULL;
19435 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
19436 +               if (unlikely(err == -EWOULDBLOCK)) {
19437 +                       pr_warn("cannot retry for NFSv4 delegation"
19438 +                               " for an internal unlink\n");
19439 +                       iput(delegated);
19440 +               }
19441 +       } else {
19442 +               /* dir inode is locked */
19443 +               h_dir = wh_dentry->d_parent->d_inode;
19444 +               IMustLock(h_dir);
19445 +               err = 0;
19446 +       }
19447 +
19448 +       if (!err) {
19449 +               vfsub_drop_nlink(inode);
19450 +               epilog(dir, dentry, bindex);
19451 +
19452 +               /* update target timestamps */
19453 +               if (bindex == bstart) {
19454 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
19455 +                       /*ignore*/
19456 +                       inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
19457 +               } else
19458 +                       /* todo: this timestamp may be reverted later */
19459 +                       inode->i_ctime = h_dir->i_ctime;
19460 +               goto out_unpin; /* success */
19461 +       }
19462 +
19463 +       /* revert */
19464 +       if (wh_dentry) {
19465 +               int rerr;
19466 +
19467 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
19468 +                                &a->dt);
19469 +               if (rerr)
19470 +                       err = rerr;
19471 +       }
19472 +
19473 +out_unpin:
19474 +       au_unpin(&a->pin);
19475 +       dput(wh_dentry);
19476 +       dput(a->h_path.dentry);
19477 +out_parent:
19478 +       di_write_unlock(parent);
19479 +out_unlock:
19480 +       aufs_read_unlock(dentry, AuLock_DW);
19481 +out_free:
19482 +       kfree(a);
19483 +out:
19484 +       return err;
19485 +}
19486 +
19487 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
19488 +{
19489 +       int err, rmdir_later;
19490 +       aufs_bindex_t bwh, bindex, bstart;
19491 +       struct inode *inode;
19492 +       struct dentry *parent, *wh_dentry, *h_dentry;
19493 +       struct au_whtmp_rmdir *args;
19494 +       /* to reuduce stack size */
19495 +       struct {
19496 +               struct au_dtime dt;
19497 +               struct au_pin pin;
19498 +       } *a;
19499 +
19500 +       IMustLock(dir);
19501 +
19502 +       err = -ENOMEM;
19503 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19504 +       if (unlikely(!a))
19505 +               goto out;
19506 +
19507 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
19508 +       if (unlikely(err))
19509 +               goto out_free;
19510 +       err = au_alive_dir(dentry);
19511 +       if (unlikely(err))
19512 +               goto out_unlock;
19513 +       inode = dentry->d_inode;
19514 +       IMustLock(inode);
19515 +       err = -ENOTDIR;
19516 +       if (unlikely(!d_is_dir(dentry)))
19517 +               goto out_unlock; /* possible? */
19518 +
19519 +       err = -ENOMEM;
19520 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
19521 +       if (unlikely(!args))
19522 +               goto out_unlock;
19523 +
19524 +       parent = dentry->d_parent; /* dir inode is locked */
19525 +       di_write_lock_parent(parent);
19526 +       err = au_test_empty(dentry, &args->whlist);
19527 +       if (unlikely(err))
19528 +               goto out_parent;
19529 +
19530 +       bstart = au_dbstart(dentry);
19531 +       bwh = au_dbwh(dentry);
19532 +       bindex = -1;
19533 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
19534 +                                       &a->pin);
19535 +       err = PTR_ERR(wh_dentry);
19536 +       if (IS_ERR(wh_dentry))
19537 +               goto out_parent;
19538 +
19539 +       h_dentry = au_h_dptr(dentry, bstart);
19540 +       dget(h_dentry);
19541 +       rmdir_later = 0;
19542 +       if (bindex == bstart) {
19543 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
19544 +               if (err > 0) {
19545 +                       rmdir_later = err;
19546 +                       err = 0;
19547 +               }
19548 +       } else {
19549 +               /* stop monitoring */
19550 +               au_hn_free(au_hi(inode, bstart));
19551 +
19552 +               /* dir inode is locked */
19553 +               IMustLock(wh_dentry->d_parent->d_inode);
19554 +               err = 0;
19555 +       }
19556 +
19557 +       if (!err) {
19558 +               vfsub_dead_dir(inode);
19559 +               au_set_dbdiropq(dentry, -1);
19560 +               epilog(dir, dentry, bindex);
19561 +
19562 +               if (rmdir_later) {
19563 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
19564 +                       args = NULL;
19565 +               }
19566 +
19567 +               goto out_unpin; /* success */
19568 +       }
19569 +
19570 +       /* revert */
19571 +       AuLabel(revert);
19572 +       if (wh_dentry) {
19573 +               int rerr;
19574 +
19575 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
19576 +                                &a->dt);
19577 +               if (rerr)
19578 +                       err = rerr;
19579 +       }
19580 +
19581 +out_unpin:
19582 +       au_unpin(&a->pin);
19583 +       dput(wh_dentry);
19584 +       dput(h_dentry);
19585 +out_parent:
19586 +       di_write_unlock(parent);
19587 +       if (args)
19588 +               au_whtmp_rmdir_free(args);
19589 +out_unlock:
19590 +       aufs_read_unlock(dentry, AuLock_DW);
19591 +out_free:
19592 +       kfree(a);
19593 +out:
19594 +       AuTraceErr(err);
19595 +       return err;
19596 +}
19597 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
19598 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
19599 +++ linux/fs/aufs/i_op_ren.c    2015-04-13 15:10:20.786823613 +0200
19600 @@ -0,0 +1,1017 @@
19601 +/*
19602 + * Copyright (C) 2005-2015 Junjiro R. Okajima
19603 + *
19604 + * This program, aufs is free software; you can redistribute it and/or modify
19605 + * it under the terms of the GNU General Public License as published by
19606 + * the Free Software Foundation; either version 2 of the License, or
19607 + * (at your option) any later version.
19608 + *
19609 + * This program is distributed in the hope that it will be useful,
19610 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19611 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19612 + * GNU General Public License for more details.
19613 + *
19614 + * You should have received a copy of the GNU General Public License
19615 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19616 + */
19617 +
19618 +/*
19619 + * inode operation (rename entry)
19620 + * todo: this is crazy monster
19621 + */
19622 +
19623 +#include "aufs.h"
19624 +
19625 +enum { AuSRC, AuDST, AuSrcDst };
19626 +enum { AuPARENT, AuCHILD, AuParentChild };
19627 +
19628 +#define AuRen_ISDIR    1
19629 +#define AuRen_ISSAMEDIR        (1 << 1)
19630 +#define AuRen_WHSRC    (1 << 2)
19631 +#define AuRen_WHDST    (1 << 3)
19632 +#define AuRen_MNT_WRITE        (1 << 4)
19633 +#define AuRen_DT_DSTDIR        (1 << 5)
19634 +#define AuRen_DIROPQ   (1 << 6)
19635 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
19636 +#define au_fset_ren(flags, name) \
19637 +       do { (flags) |= AuRen_##name; } while (0)
19638 +#define au_fclr_ren(flags, name) \
19639 +       do { (flags) &= ~AuRen_##name; } while (0)
19640 +
19641 +struct au_ren_args {
19642 +       struct {
19643 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
19644 +                       *wh_dentry;
19645 +               struct inode *dir, *inode;
19646 +               struct au_hinode *hdir;
19647 +               struct au_dtime dt[AuParentChild];
19648 +               aufs_bindex_t bstart;
19649 +       } sd[AuSrcDst];
19650 +
19651 +#define src_dentry     sd[AuSRC].dentry
19652 +#define src_dir                sd[AuSRC].dir
19653 +#define src_inode      sd[AuSRC].inode
19654 +#define src_h_dentry   sd[AuSRC].h_dentry
19655 +#define src_parent     sd[AuSRC].parent
19656 +#define src_h_parent   sd[AuSRC].h_parent
19657 +#define src_wh_dentry  sd[AuSRC].wh_dentry
19658 +#define src_hdir       sd[AuSRC].hdir
19659 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
19660 +#define src_dt         sd[AuSRC].dt
19661 +#define src_bstart     sd[AuSRC].bstart
19662 +
19663 +#define dst_dentry     sd[AuDST].dentry
19664 +#define dst_dir                sd[AuDST].dir
19665 +#define dst_inode      sd[AuDST].inode
19666 +#define dst_h_dentry   sd[AuDST].h_dentry
19667 +#define dst_parent     sd[AuDST].parent
19668 +#define dst_h_parent   sd[AuDST].h_parent
19669 +#define dst_wh_dentry  sd[AuDST].wh_dentry
19670 +#define dst_hdir       sd[AuDST].hdir
19671 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
19672 +#define dst_dt         sd[AuDST].dt
19673 +#define dst_bstart     sd[AuDST].bstart
19674 +
19675 +       struct dentry *h_trap;
19676 +       struct au_branch *br;
19677 +       struct au_hinode *src_hinode;
19678 +       struct path h_path;
19679 +       struct au_nhash whlist;
19680 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
19681 +
19682 +       unsigned int flags;
19683 +
19684 +       struct au_whtmp_rmdir *thargs;
19685 +       struct dentry *h_dst;
19686 +};
19687 +
19688 +/* ---------------------------------------------------------------------- */
19689 +
19690 +/*
19691 + * functions for reverting.
19692 + * when an error happened in a single rename systemcall, we should revert
19693 + * everything as if nothing happend.
19694 + * we don't need to revert the copied-up/down the parent dir since they are
19695 + * harmless.
19696 + */
19697 +
19698 +#define RevertFailure(fmt, ...) do { \
19699 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
19700 +               ##__VA_ARGS__, err, rerr); \
19701 +       err = -EIO; \
19702 +} while (0)
19703 +
19704 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
19705 +{
19706 +       int rerr;
19707 +
19708 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
19709 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
19710 +       au_hn_imtx_unlock(a->src_hinode);
19711 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
19712 +       if (rerr)
19713 +               RevertFailure("remove diropq %pd", a->src_dentry);
19714 +}
19715 +
19716 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
19717 +{
19718 +       int rerr;
19719 +       struct inode *delegated;
19720 +
19721 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
19722 +                                         a->src_h_parent);
19723 +       rerr = PTR_ERR(a->h_path.dentry);
19724 +       if (IS_ERR(a->h_path.dentry)) {
19725 +               RevertFailure("lkup one %pd", a->src_dentry);
19726 +               return;
19727 +       }
19728 +
19729 +       delegated = NULL;
19730 +       rerr = vfsub_rename(a->dst_h_dir,
19731 +                           au_h_dptr(a->src_dentry, a->btgt),
19732 +                           a->src_h_dir, &a->h_path, &delegated);
19733 +       if (unlikely(rerr == -EWOULDBLOCK)) {
19734 +               pr_warn("cannot retry for NFSv4 delegation"
19735 +                       " for an internal rename\n");
19736 +               iput(delegated);
19737 +       }
19738 +       d_drop(a->h_path.dentry);
19739 +       dput(a->h_path.dentry);
19740 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
19741 +       if (rerr)
19742 +               RevertFailure("rename %pd", a->src_dentry);
19743 +}
19744 +
19745 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
19746 +{
19747 +       int rerr;
19748 +       struct inode *delegated;
19749 +
19750 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
19751 +                                         a->dst_h_parent);
19752 +       rerr = PTR_ERR(a->h_path.dentry);
19753 +       if (IS_ERR(a->h_path.dentry)) {
19754 +               RevertFailure("lkup one %pd", a->dst_dentry);
19755 +               return;
19756 +       }
19757 +       if (a->h_path.dentry->d_inode) {
19758 +               d_drop(a->h_path.dentry);
19759 +               dput(a->h_path.dentry);
19760 +               return;
19761 +       }
19762 +
19763 +       delegated = NULL;
19764 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
19765 +                           &delegated);
19766 +       if (unlikely(rerr == -EWOULDBLOCK)) {
19767 +               pr_warn("cannot retry for NFSv4 delegation"
19768 +                       " for an internal rename\n");
19769 +               iput(delegated);
19770 +       }
19771 +       d_drop(a->h_path.dentry);
19772 +       dput(a->h_path.dentry);
19773 +       if (!rerr)
19774 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
19775 +       else
19776 +               RevertFailure("rename %pd", a->h_dst);
19777 +}
19778 +
19779 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
19780 +{
19781 +       int rerr;
19782 +
19783 +       a->h_path.dentry = a->src_wh_dentry;
19784 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
19785 +       au_set_dbwh(a->src_dentry, a->src_bwh);
19786 +       if (rerr)
19787 +               RevertFailure("unlink %pd", a->src_wh_dentry);
19788 +}
19789 +#undef RevertFailure
19790 +
19791 +/* ---------------------------------------------------------------------- */
19792 +
19793 +/*
19794 + * when we have to copyup the renaming entry, do it with the rename-target name
19795 + * in order to minimize the cost (the later actual rename is unnecessary).
19796 + * otherwise rename it on the target branch.
19797 + */
19798 +static int au_ren_or_cpup(struct au_ren_args *a)
19799 +{
19800 +       int err;
19801 +       struct dentry *d;
19802 +       struct inode *delegated;
19803 +
19804 +       d = a->src_dentry;
19805 +       if (au_dbstart(d) == a->btgt) {
19806 +               a->h_path.dentry = a->dst_h_dentry;
19807 +               if (au_ftest_ren(a->flags, DIROPQ)
19808 +                   && au_dbdiropq(d) == a->btgt)
19809 +                       au_fclr_ren(a->flags, DIROPQ);
19810 +               AuDebugOn(au_dbstart(d) != a->btgt);
19811 +               delegated = NULL;
19812 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
19813 +                                  a->dst_h_dir, &a->h_path, &delegated);
19814 +               if (unlikely(err == -EWOULDBLOCK)) {
19815 +                       pr_warn("cannot retry for NFSv4 delegation"
19816 +                               " for an internal rename\n");
19817 +                       iput(delegated);
19818 +               }
19819 +       } else
19820 +               BUG();
19821 +
19822 +       if (!err && a->h_dst)
19823 +               /* it will be set to dinfo later */
19824 +               dget(a->h_dst);
19825 +
19826 +       return err;
19827 +}
19828 +
19829 +/* cf. aufs_rmdir() */
19830 +static int au_ren_del_whtmp(struct au_ren_args *a)
19831 +{
19832 +       int err;
19833 +       struct inode *dir;
19834 +
19835 +       dir = a->dst_dir;
19836 +       SiMustAnyLock(dir->i_sb);
19837 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
19838 +                                    au_sbi(dir->i_sb)->si_dirwh)
19839 +           || au_test_fs_remote(a->h_dst->d_sb)) {
19840 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
19841 +               if (unlikely(err))
19842 +                       pr_warn("failed removing whtmp dir %pd (%d), "
19843 +                               "ignored.\n", a->h_dst, err);
19844 +       } else {
19845 +               au_nhash_wh_free(&a->thargs->whlist);
19846 +               a->thargs->whlist = a->whlist;
19847 +               a->whlist.nh_num = 0;
19848 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
19849 +               dput(a->h_dst);
19850 +               a->thargs = NULL;
19851 +       }
19852 +
19853 +       return 0;
19854 +}
19855 +
19856 +/* make it 'opaque' dir. */
19857 +static int au_ren_diropq(struct au_ren_args *a)
19858 +{
19859 +       int err;
19860 +       struct dentry *diropq;
19861 +
19862 +       err = 0;
19863 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
19864 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
19865 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
19866 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
19867 +       au_hn_imtx_unlock(a->src_hinode);
19868 +       if (IS_ERR(diropq))
19869 +               err = PTR_ERR(diropq);
19870 +       else
19871 +               dput(diropq);
19872 +
19873 +       return err;
19874 +}
19875 +
19876 +static int do_rename(struct au_ren_args *a)
19877 +{
19878 +       int err;
19879 +       struct dentry *d, *h_d;
19880 +
19881 +       /* prepare workqueue args for asynchronous rmdir */
19882 +       h_d = a->dst_h_dentry;
19883 +       if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
19884 +               err = -ENOMEM;
19885 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
19886 +               if (unlikely(!a->thargs))
19887 +                       goto out;
19888 +               a->h_dst = dget(h_d);
19889 +       }
19890 +
19891 +       /* create whiteout for src_dentry */
19892 +       if (au_ftest_ren(a->flags, WHSRC)) {
19893 +               a->src_bwh = au_dbwh(a->src_dentry);
19894 +               AuDebugOn(a->src_bwh >= 0);
19895 +               a->src_wh_dentry
19896 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
19897 +               err = PTR_ERR(a->src_wh_dentry);
19898 +               if (IS_ERR(a->src_wh_dentry))
19899 +                       goto out_thargs;
19900 +       }
19901 +
19902 +       /* lookup whiteout for dentry */
19903 +       if (au_ftest_ren(a->flags, WHDST)) {
19904 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
19905 +                                a->br);
19906 +               err = PTR_ERR(h_d);
19907 +               if (IS_ERR(h_d))
19908 +                       goto out_whsrc;
19909 +               if (!h_d->d_inode)
19910 +                       dput(h_d);
19911 +               else
19912 +                       a->dst_wh_dentry = h_d;
19913 +       }
19914 +
19915 +       /* rename dentry to tmpwh */
19916 +       if (a->thargs) {
19917 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
19918 +               if (unlikely(err))
19919 +                       goto out_whdst;
19920 +
19921 +               d = a->dst_dentry;
19922 +               au_set_h_dptr(d, a->btgt, NULL);
19923 +               err = au_lkup_neg(d, a->btgt, /*wh*/0);
19924 +               if (unlikely(err))
19925 +                       goto out_whtmp;
19926 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
19927 +       }
19928 +
19929 +       BUG_ON(a->dst_h_dentry->d_inode && a->src_bstart != a->btgt);
19930 +
19931 +       /* rename by vfs_rename or cpup */
19932 +       d = a->dst_dentry;
19933 +       if (au_ftest_ren(a->flags, ISDIR)
19934 +           && (a->dst_wh_dentry
19935 +               || au_dbdiropq(d) == a->btgt
19936 +               /* hide the lower to keep xino */
19937 +               || a->btgt < au_dbend(d)
19938 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
19939 +               au_fset_ren(a->flags, DIROPQ);
19940 +       err = au_ren_or_cpup(a);
19941 +       if (unlikely(err))
19942 +               /* leave the copied-up one */
19943 +               goto out_whtmp;
19944 +
19945 +       /* make dir opaque */
19946 +       if (au_ftest_ren(a->flags, DIROPQ)) {
19947 +               err = au_ren_diropq(a);
19948 +               if (unlikely(err))
19949 +                       goto out_rename;
19950 +       }
19951 +
19952 +       /* update target timestamps */
19953 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
19954 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
19955 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
19956 +       a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
19957 +
19958 +       /* remove whiteout for dentry */
19959 +       if (a->dst_wh_dentry) {
19960 +               a->h_path.dentry = a->dst_wh_dentry;
19961 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
19962 +                                         a->dst_dentry);
19963 +               if (unlikely(err))
19964 +                       goto out_diropq;
19965 +       }
19966 +
19967 +       /* remove whtmp */
19968 +       if (a->thargs)
19969 +               au_ren_del_whtmp(a); /* ignore this error */
19970 +
19971 +       au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
19972 +       err = 0;
19973 +       goto out_success;
19974 +
19975 +out_diropq:
19976 +       if (au_ftest_ren(a->flags, DIROPQ))
19977 +               au_ren_rev_diropq(err, a);
19978 +out_rename:
19979 +       au_ren_rev_rename(err, a);
19980 +       dput(a->h_dst);
19981 +out_whtmp:
19982 +       if (a->thargs)
19983 +               au_ren_rev_whtmp(err, a);
19984 +out_whdst:
19985 +       dput(a->dst_wh_dentry);
19986 +       a->dst_wh_dentry = NULL;
19987 +out_whsrc:
19988 +       if (a->src_wh_dentry)
19989 +               au_ren_rev_whsrc(err, a);
19990 +out_success:
19991 +       dput(a->src_wh_dentry);
19992 +       dput(a->dst_wh_dentry);
19993 +out_thargs:
19994 +       if (a->thargs) {
19995 +               dput(a->h_dst);
19996 +               au_whtmp_rmdir_free(a->thargs);
19997 +               a->thargs = NULL;
19998 +       }
19999 +out:
20000 +       return err;
20001 +}
20002 +
20003 +/* ---------------------------------------------------------------------- */
20004 +
20005 +/*
20006 + * test if @dentry dir can be rename destination or not.
20007 + * success means, it is a logically empty dir.
20008 + */
20009 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
20010 +{
20011 +       return au_test_empty(dentry, whlist);
20012 +}
20013 +
20014 +/*
20015 + * test if @dentry dir can be rename source or not.
20016 + * if it can, return 0 and @children is filled.
20017 + * success means,
20018 + * - it is a logically empty dir.
20019 + * - or, it exists on writable branch and has no children including whiteouts
20020 + *       on the lower branch.
20021 + */
20022 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
20023 +{
20024 +       int err;
20025 +       unsigned int rdhash;
20026 +       aufs_bindex_t bstart;
20027 +
20028 +       bstart = au_dbstart(dentry);
20029 +       if (bstart != btgt) {
20030 +               struct au_nhash whlist;
20031 +
20032 +               SiMustAnyLock(dentry->d_sb);
20033 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
20034 +               if (!rdhash)
20035 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
20036 +                                                          dentry));
20037 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
20038 +               if (unlikely(err))
20039 +                       goto out;
20040 +               err = au_test_empty(dentry, &whlist);
20041 +               au_nhash_wh_free(&whlist);
20042 +               goto out;
20043 +       }
20044 +
20045 +       if (bstart == au_dbtaildir(dentry))
20046 +               return 0; /* success */
20047 +
20048 +       err = au_test_empty_lower(dentry);
20049 +
20050 +out:
20051 +       if (err == -ENOTEMPTY) {
20052 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
20053 +                       " is not supported\n");
20054 +               err = -EXDEV;
20055 +       }
20056 +       return err;
20057 +}
20058 +
20059 +/* side effect: sets whlist and h_dentry */
20060 +static int au_ren_may_dir(struct au_ren_args *a)
20061 +{
20062 +       int err;
20063 +       unsigned int rdhash;
20064 +       struct dentry *d;
20065 +
20066 +       d = a->dst_dentry;
20067 +       SiMustAnyLock(d->d_sb);
20068 +
20069 +       err = 0;
20070 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
20071 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
20072 +               if (!rdhash)
20073 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
20074 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
20075 +               if (unlikely(err))
20076 +                       goto out;
20077 +
20078 +               au_set_dbstart(d, a->dst_bstart);
20079 +               err = may_rename_dstdir(d, &a->whlist);
20080 +               au_set_dbstart(d, a->btgt);
20081 +       }
20082 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
20083 +       if (unlikely(err))
20084 +               goto out;
20085 +
20086 +       d = a->src_dentry;
20087 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
20088 +       if (au_ftest_ren(a->flags, ISDIR)) {
20089 +               err = may_rename_srcdir(d, a->btgt);
20090 +               if (unlikely(err)) {
20091 +                       au_nhash_wh_free(&a->whlist);
20092 +                       a->whlist.nh_num = 0;
20093 +               }
20094 +       }
20095 +out:
20096 +       return err;
20097 +}
20098 +
20099 +/* ---------------------------------------------------------------------- */
20100 +
20101 +/*
20102 + * simple tests for rename.
20103 + * following the checks in vfs, plus the parent-child relationship.
20104 + */
20105 +static int au_may_ren(struct au_ren_args *a)
20106 +{
20107 +       int err, isdir;
20108 +       struct inode *h_inode;
20109 +
20110 +       if (a->src_bstart == a->btgt) {
20111 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
20112 +                                au_ftest_ren(a->flags, ISDIR));
20113 +               if (unlikely(err))
20114 +                       goto out;
20115 +               err = -EINVAL;
20116 +               if (unlikely(a->src_h_dentry == a->h_trap))
20117 +                       goto out;
20118 +       }
20119 +
20120 +       err = 0;
20121 +       if (a->dst_bstart != a->btgt)
20122 +               goto out;
20123 +
20124 +       err = -ENOTEMPTY;
20125 +       if (unlikely(a->dst_h_dentry == a->h_trap))
20126 +               goto out;
20127 +
20128 +       err = -EIO;
20129 +       h_inode = a->dst_h_dentry->d_inode;
20130 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
20131 +       if (!a->dst_dentry->d_inode) {
20132 +               if (unlikely(h_inode))
20133 +                       goto out;
20134 +               err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
20135 +                                isdir);
20136 +       } else {
20137 +               if (unlikely(!h_inode || !h_inode->i_nlink))
20138 +                       goto out;
20139 +               err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
20140 +                                isdir);
20141 +               if (unlikely(err))
20142 +                       goto out;
20143 +       }
20144 +
20145 +out:
20146 +       if (unlikely(err == -ENOENT || err == -EEXIST))
20147 +               err = -EIO;
20148 +       AuTraceErr(err);
20149 +       return err;
20150 +}
20151 +
20152 +/* ---------------------------------------------------------------------- */
20153 +
20154 +/*
20155 + * locking order
20156 + * (VFS)
20157 + * - src_dir and dir by lock_rename()
20158 + * - inode if exitsts
20159 + * (aufs)
20160 + * - lock all
20161 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
20162 + *     + si_read_lock
20163 + *     + di_write_lock2_child()
20164 + *       + di_write_lock_child()
20165 + *        + ii_write_lock_child()
20166 + *       + di_write_lock_child2()
20167 + *        + ii_write_lock_child2()
20168 + *     + src_parent and parent
20169 + *       + di_write_lock_parent()
20170 + *        + ii_write_lock_parent()
20171 + *       + di_write_lock_parent2()
20172 + *        + ii_write_lock_parent2()
20173 + *   + lower src_dir and dir by vfsub_lock_rename()
20174 + *   + verify the every relationships between child and parent. if any
20175 + *     of them failed, unlock all and return -EBUSY.
20176 + */
20177 +static void au_ren_unlock(struct au_ren_args *a)
20178 +{
20179 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
20180 +                           a->dst_h_parent, a->dst_hdir);
20181 +       if (au_ftest_ren(a->flags, MNT_WRITE))
20182 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
20183 +}
20184 +
20185 +static int au_ren_lock(struct au_ren_args *a)
20186 +{
20187 +       int err;
20188 +       unsigned int udba;
20189 +
20190 +       err = 0;
20191 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
20192 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
20193 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
20194 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
20195 +
20196 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
20197 +       if (unlikely(err))
20198 +               goto out;
20199 +       au_fset_ren(a->flags, MNT_WRITE);
20200 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
20201 +                                     a->dst_h_parent, a->dst_hdir);
20202 +       udba = au_opt_udba(a->src_dentry->d_sb);
20203 +       if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
20204 +                    || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
20205 +               err = au_busy_or_stale();
20206 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
20207 +               err = au_h_verify(a->src_h_dentry, udba,
20208 +                                 a->src_h_parent->d_inode, a->src_h_parent,
20209 +                                 a->br);
20210 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
20211 +               err = au_h_verify(a->dst_h_dentry, udba,
20212 +                                 a->dst_h_parent->d_inode, a->dst_h_parent,
20213 +                                 a->br);
20214 +       if (!err)
20215 +               goto out; /* success */
20216 +
20217 +       err = au_busy_or_stale();
20218 +       au_ren_unlock(a);
20219 +
20220 +out:
20221 +       return err;
20222 +}
20223 +
20224 +/* ---------------------------------------------------------------------- */
20225 +
20226 +static void au_ren_refresh_dir(struct au_ren_args *a)
20227 +{
20228 +       struct inode *dir;
20229 +
20230 +       dir = a->dst_dir;
20231 +       dir->i_version++;
20232 +       if (au_ftest_ren(a->flags, ISDIR)) {
20233 +               /* is this updating defined in POSIX? */
20234 +               au_cpup_attr_timesizes(a->src_inode);
20235 +               au_cpup_attr_nlink(dir, /*force*/1);
20236 +       }
20237 +
20238 +       if (au_ibstart(dir) == a->btgt)
20239 +               au_cpup_attr_timesizes(dir);
20240 +
20241 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
20242 +               return;
20243 +
20244 +       dir = a->src_dir;
20245 +       dir->i_version++;
20246 +       if (au_ftest_ren(a->flags, ISDIR))
20247 +               au_cpup_attr_nlink(dir, /*force*/1);
20248 +       if (au_ibstart(dir) == a->btgt)
20249 +               au_cpup_attr_timesizes(dir);
20250 +}
20251 +
20252 +static void au_ren_refresh(struct au_ren_args *a)
20253 +{
20254 +       aufs_bindex_t bend, bindex;
20255 +       struct dentry *d, *h_d;
20256 +       struct inode *i, *h_i;
20257 +       struct super_block *sb;
20258 +
20259 +       d = a->dst_dentry;
20260 +       d_drop(d);
20261 +       if (a->h_dst)
20262 +               /* already dget-ed by au_ren_or_cpup() */
20263 +               au_set_h_dptr(d, a->btgt, a->h_dst);
20264 +
20265 +       i = a->dst_inode;
20266 +       if (i) {
20267 +               if (!au_ftest_ren(a->flags, ISDIR))
20268 +                       vfsub_drop_nlink(i);
20269 +               else {
20270 +                       vfsub_dead_dir(i);
20271 +                       au_cpup_attr_timesizes(i);
20272 +               }
20273 +               au_update_dbrange(d, /*do_put_zero*/1);
20274 +       } else {
20275 +               bend = a->btgt;
20276 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
20277 +                       au_set_h_dptr(d, bindex, NULL);
20278 +               bend = au_dbend(d);
20279 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
20280 +                       au_set_h_dptr(d, bindex, NULL);
20281 +               au_update_dbrange(d, /*do_put_zero*/0);
20282 +       }
20283 +
20284 +       d = a->src_dentry;
20285 +       au_set_dbwh(d, -1);
20286 +       bend = au_dbend(d);
20287 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20288 +               h_d = au_h_dptr(d, bindex);
20289 +               if (h_d)
20290 +                       au_set_h_dptr(d, bindex, NULL);
20291 +       }
20292 +       au_set_dbend(d, a->btgt);
20293 +
20294 +       sb = d->d_sb;
20295 +       i = a->src_inode;
20296 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
20297 +               return; /* success */
20298 +
20299 +       bend = au_ibend(i);
20300 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20301 +               h_i = au_h_iptr(i, bindex);
20302 +               if (h_i) {
20303 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
20304 +                       /* ignore this error */
20305 +                       au_set_h_iptr(i, bindex, NULL, 0);
20306 +               }
20307 +       }
20308 +       au_set_ibend(i, a->btgt);
20309 +}
20310 +
20311 +/* ---------------------------------------------------------------------- */
20312 +
20313 +/* mainly for link(2) and rename(2) */
20314 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
20315 +{
20316 +       aufs_bindex_t bdiropq, bwh;
20317 +       struct dentry *parent;
20318 +       struct au_branch *br;
20319 +
20320 +       parent = dentry->d_parent;
20321 +       IMustLock(parent->d_inode); /* dir is locked */
20322 +
20323 +       bdiropq = au_dbdiropq(parent);
20324 +       bwh = au_dbwh(dentry);
20325 +       br = au_sbr(dentry->d_sb, btgt);
20326 +       if (au_br_rdonly(br)
20327 +           || (0 <= bdiropq && bdiropq < btgt)
20328 +           || (0 <= bwh && bwh < btgt))
20329 +               btgt = -1;
20330 +
20331 +       AuDbg("btgt %d\n", btgt);
20332 +       return btgt;
20333 +}
20334 +
20335 +/* sets src_bstart, dst_bstart and btgt */
20336 +static int au_ren_wbr(struct au_ren_args *a)
20337 +{
20338 +       int err;
20339 +       struct au_wr_dir_args wr_dir_args = {
20340 +               /* .force_btgt  = -1, */
20341 +               .flags          = AuWrDir_ADD_ENTRY
20342 +       };
20343 +
20344 +       a->src_bstart = au_dbstart(a->src_dentry);
20345 +       a->dst_bstart = au_dbstart(a->dst_dentry);
20346 +       if (au_ftest_ren(a->flags, ISDIR))
20347 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
20348 +       wr_dir_args.force_btgt = a->src_bstart;
20349 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
20350 +               wr_dir_args.force_btgt = a->dst_bstart;
20351 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
20352 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
20353 +       a->btgt = err;
20354 +
20355 +       return err;
20356 +}
20357 +
20358 +static void au_ren_dt(struct au_ren_args *a)
20359 +{
20360 +       a->h_path.dentry = a->src_h_parent;
20361 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
20362 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
20363 +               a->h_path.dentry = a->dst_h_parent;
20364 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
20365 +       }
20366 +
20367 +       au_fclr_ren(a->flags, DT_DSTDIR);
20368 +       if (!au_ftest_ren(a->flags, ISDIR))
20369 +               return;
20370 +
20371 +       a->h_path.dentry = a->src_h_dentry;
20372 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
20373 +       if (a->dst_h_dentry->d_inode) {
20374 +               au_fset_ren(a->flags, DT_DSTDIR);
20375 +               a->h_path.dentry = a->dst_h_dentry;
20376 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
20377 +       }
20378 +}
20379 +
20380 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
20381 +{
20382 +       struct dentry *h_d;
20383 +       struct mutex *h_mtx;
20384 +
20385 +       au_dtime_revert(a->src_dt + AuPARENT);
20386 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
20387 +               au_dtime_revert(a->dst_dt + AuPARENT);
20388 +
20389 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
20390 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
20391 +               h_mtx = &h_d->d_inode->i_mutex;
20392 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
20393 +               au_dtime_revert(a->src_dt + AuCHILD);
20394 +               mutex_unlock(h_mtx);
20395 +
20396 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
20397 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
20398 +                       h_mtx = &h_d->d_inode->i_mutex;
20399 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
20400 +                       au_dtime_revert(a->dst_dt + AuCHILD);
20401 +                       mutex_unlock(h_mtx);
20402 +               }
20403 +       }
20404 +}
20405 +
20406 +/* ---------------------------------------------------------------------- */
20407 +
20408 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
20409 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
20410 +{
20411 +       int err, flags;
20412 +       /* reduce stack space */
20413 +       struct au_ren_args *a;
20414 +
20415 +       AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
20416 +       IMustLock(_src_dir);
20417 +       IMustLock(_dst_dir);
20418 +
20419 +       err = -ENOMEM;
20420 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
20421 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20422 +       if (unlikely(!a))
20423 +               goto out;
20424 +
20425 +       a->src_dir = _src_dir;
20426 +       a->src_dentry = _src_dentry;
20427 +       a->src_inode = a->src_dentry->d_inode;
20428 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
20429 +       a->dst_dir = _dst_dir;
20430 +       a->dst_dentry = _dst_dentry;
20431 +       a->dst_inode = a->dst_dentry->d_inode;
20432 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
20433 +       if (a->dst_inode) {
20434 +               IMustLock(a->dst_inode);
20435 +               au_igrab(a->dst_inode);
20436 +       }
20437 +
20438 +       err = -ENOTDIR;
20439 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
20440 +       if (d_is_dir(a->src_dentry)) {
20441 +               au_fset_ren(a->flags, ISDIR);
20442 +               if (unlikely(d_is_positive(a->dst_dentry)
20443 +                            && !d_is_dir(a->dst_dentry)))
20444 +                       goto out_free;
20445 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
20446 +                                               AuLock_DIR | flags);
20447 +       } else
20448 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
20449 +                                               flags);
20450 +       if (unlikely(err))
20451 +               goto out_free;
20452 +
20453 +       err = au_d_hashed_positive(a->src_dentry);
20454 +       if (unlikely(err))
20455 +               goto out_unlock;
20456 +       err = -ENOENT;
20457 +       if (a->dst_inode) {
20458 +               /*
20459 +                * If it is a dir, VFS unhash dst_dentry before this
20460 +                * function. It means we cannot rely upon d_unhashed().
20461 +                */
20462 +               if (unlikely(!a->dst_inode->i_nlink))
20463 +                       goto out_unlock;
20464 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
20465 +                       err = au_d_hashed_positive(a->dst_dentry);
20466 +                       if (unlikely(err))
20467 +                               goto out_unlock;
20468 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
20469 +                       goto out_unlock;
20470 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
20471 +               goto out_unlock;
20472 +
20473 +       /*
20474 +        * is it possible?
20475 +        * yes, it happend (in linux-3.3-rcN) but I don't know why.
20476 +        * there may exist a problem somewhere else.
20477 +        */
20478 +       err = -EINVAL;
20479 +       if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
20480 +               goto out_unlock;
20481 +
20482 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
20483 +       di_write_lock_parent(a->dst_parent);
20484 +
20485 +       /* which branch we process */
20486 +       err = au_ren_wbr(a);
20487 +       if (unlikely(err < 0))
20488 +               goto out_parent;
20489 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
20490 +       a->h_path.mnt = au_br_mnt(a->br);
20491 +
20492 +       /* are they available to be renamed */
20493 +       err = au_ren_may_dir(a);
20494 +       if (unlikely(err))
20495 +               goto out_children;
20496 +
20497 +       /* prepare the writable parent dir on the same branch */
20498 +       if (a->dst_bstart == a->btgt) {
20499 +               au_fset_ren(a->flags, WHDST);
20500 +       } else {
20501 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
20502 +               if (unlikely(err))
20503 +                       goto out_children;
20504 +       }
20505 +
20506 +       if (a->src_dir != a->dst_dir) {
20507 +               /*
20508 +                * this temporary unlock is safe,
20509 +                * because both dir->i_mutex are locked.
20510 +                */
20511 +               di_write_unlock(a->dst_parent);
20512 +               di_write_lock_parent(a->src_parent);
20513 +               err = au_wr_dir_need_wh(a->src_dentry,
20514 +                                       au_ftest_ren(a->flags, ISDIR),
20515 +                                       &a->btgt);
20516 +               di_write_unlock(a->src_parent);
20517 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
20518 +               au_fclr_ren(a->flags, ISSAMEDIR);
20519 +       } else
20520 +               err = au_wr_dir_need_wh(a->src_dentry,
20521 +                                       au_ftest_ren(a->flags, ISDIR),
20522 +                                       &a->btgt);
20523 +       if (unlikely(err < 0))
20524 +               goto out_children;
20525 +       if (err)
20526 +               au_fset_ren(a->flags, WHSRC);
20527 +
20528 +       /* cpup src */
20529 +       if (a->src_bstart != a->btgt) {
20530 +               struct au_pin pin;
20531 +
20532 +               err = au_pin(&pin, a->src_dentry, a->btgt,
20533 +                            au_opt_udba(a->src_dentry->d_sb),
20534 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20535 +               if (!err) {
20536 +                       struct au_cp_generic cpg = {
20537 +                               .dentry = a->src_dentry,
20538 +                               .bdst   = a->btgt,
20539 +                               .bsrc   = a->src_bstart,
20540 +                               .len    = -1,
20541 +                               .pin    = &pin,
20542 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
20543 +                       };
20544 +                       AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
20545 +                       err = au_sio_cpup_simple(&cpg);
20546 +                       au_unpin(&pin);
20547 +               }
20548 +               if (unlikely(err))
20549 +                       goto out_children;
20550 +               a->src_bstart = a->btgt;
20551 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
20552 +               au_fset_ren(a->flags, WHSRC);
20553 +       }
20554 +
20555 +       /* lock them all */
20556 +       err = au_ren_lock(a);
20557 +       if (unlikely(err))
20558 +               /* leave the copied-up one */
20559 +               goto out_children;
20560 +
20561 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
20562 +               err = au_may_ren(a);
20563 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
20564 +               err = -ENAMETOOLONG;
20565 +       if (unlikely(err))
20566 +               goto out_hdir;
20567 +
20568 +       /* store timestamps to be revertible */
20569 +       au_ren_dt(a);
20570 +
20571 +       /* here we go */
20572 +       err = do_rename(a);
20573 +       if (unlikely(err))
20574 +               goto out_dt;
20575 +
20576 +       /* update dir attributes */
20577 +       au_ren_refresh_dir(a);
20578 +
20579 +       /* dput/iput all lower dentries */
20580 +       au_ren_refresh(a);
20581 +
20582 +       goto out_hdir; /* success */
20583 +
20584 +out_dt:
20585 +       au_ren_rev_dt(err, a);
20586 +out_hdir:
20587 +       au_ren_unlock(a);
20588 +out_children:
20589 +       au_nhash_wh_free(&a->whlist);
20590 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
20591 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
20592 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
20593 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
20594 +       }
20595 +out_parent:
20596 +       if (!err)
20597 +               d_move(a->src_dentry, a->dst_dentry);
20598 +       else {
20599 +               au_update_dbstart(a->dst_dentry);
20600 +               if (!a->dst_inode)
20601 +                       d_drop(a->dst_dentry);
20602 +       }
20603 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
20604 +               di_write_unlock(a->dst_parent);
20605 +       else
20606 +               di_write_unlock2(a->src_parent, a->dst_parent);
20607 +out_unlock:
20608 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
20609 +out_free:
20610 +       iput(a->dst_inode);
20611 +       if (a->thargs)
20612 +               au_whtmp_rmdir_free(a->thargs);
20613 +       kfree(a);
20614 +out:
20615 +       AuTraceErr(err);
20616 +       return err;
20617 +}
20618 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
20619 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
20620 +++ linux/fs/aufs/Kconfig       2015-04-13 15:10:20.776823376 +0200
20621 @@ -0,0 +1,185 @@
20622 +config AUFS_FS
20623 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
20624 +       help
20625 +       Aufs is a stackable unification filesystem such as Unionfs,
20626 +       which unifies several directories and provides a merged single
20627 +       directory.
20628 +       In the early days, aufs was entirely re-designed and
20629 +       re-implemented Unionfs Version 1.x series. Introducing many
20630 +       original ideas, approaches and improvements, it becomes totally
20631 +       different from Unionfs while keeping the basic features.
20632 +
20633 +if AUFS_FS
20634 +choice
20635 +       prompt "Maximum number of branches"
20636 +       default AUFS_BRANCH_MAX_127
20637 +       help
20638 +       Specifies the maximum number of branches (or member directories)
20639 +       in a single aufs. The larger value consumes more system
20640 +       resources and has a minor impact to performance.
20641 +config AUFS_BRANCH_MAX_127
20642 +       bool "127"
20643 +       help
20644 +       Specifies the maximum number of branches (or member directories)
20645 +       in a single aufs. The larger value consumes more system
20646 +       resources and has a minor impact to performance.
20647 +config AUFS_BRANCH_MAX_511
20648 +       bool "511"
20649 +       help
20650 +       Specifies the maximum number of branches (or member directories)
20651 +       in a single aufs. The larger value consumes more system
20652 +       resources and has a minor impact to performance.
20653 +config AUFS_BRANCH_MAX_1023
20654 +       bool "1023"
20655 +       help
20656 +       Specifies the maximum number of branches (or member directories)
20657 +       in a single aufs. The larger value consumes more system
20658 +       resources and has a minor impact to performance.
20659 +config AUFS_BRANCH_MAX_32767
20660 +       bool "32767"
20661 +       help
20662 +       Specifies the maximum number of branches (or member directories)
20663 +       in a single aufs. The larger value consumes more system
20664 +       resources and has a minor impact to performance.
20665 +endchoice
20666 +
20667 +config AUFS_SBILIST
20668 +       bool
20669 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
20670 +       default y
20671 +       help
20672 +       Automatic configuration for internal use.
20673 +       When aufs supports Magic SysRq or /proc, enabled automatically.
20674 +
20675 +config AUFS_HNOTIFY
20676 +       bool "Detect direct branch access (bypassing aufs)"
20677 +       help
20678 +       If you want to modify files on branches directly, eg. bypassing aufs,
20679 +       and want aufs to detect the changes of them fully, then enable this
20680 +       option and use 'udba=notify' mount option.
20681 +       Currently there is only one available configuration, "fsnotify".
20682 +       It will have a negative impact to the performance.
20683 +       See detail in aufs.5.
20684 +
20685 +choice
20686 +       prompt "method" if AUFS_HNOTIFY
20687 +       default AUFS_HFSNOTIFY
20688 +config AUFS_HFSNOTIFY
20689 +       bool "fsnotify"
20690 +       select FSNOTIFY
20691 +endchoice
20692 +
20693 +config AUFS_EXPORT
20694 +       bool "NFS-exportable aufs"
20695 +       depends on EXPORTFS
20696 +       help
20697 +       If you want to export your mounted aufs via NFS, then enable this
20698 +       option. There are several requirements for this configuration.
20699 +       See detail in aufs.5.
20700 +
20701 +config AUFS_INO_T_64
20702 +       bool
20703 +       depends on AUFS_EXPORT
20704 +       depends on 64BIT && !(ALPHA || S390)
20705 +       default y
20706 +       help
20707 +       Automatic configuration for internal use.
20708 +       /* typedef unsigned long/int __kernel_ino_t */
20709 +       /* alpha and s390x are int */
20710 +
20711 +config AUFS_XATTR
20712 +       bool "support for XATTR/EA (including Security Labels)"
20713 +       help
20714 +       If your branch fs supports XATTR/EA and you want to make them
20715 +       available in aufs too, then enable this opsion and specify the
20716 +       branch attributes for EA.
20717 +       See detail in aufs.5.
20718 +
20719 +config AUFS_FHSM
20720 +       bool "File-based Hierarchical Storage Management"
20721 +       help
20722 +       Hierarchical Storage Management (or HSM) is a well-known feature
20723 +       in the storage world. Aufs provides this feature as file-based.
20724 +       with multiple branches.
20725 +       These multiple branches are prioritized, ie. the topmost one
20726 +       should be the fastest drive and be used heavily.
20727 +
20728 +config AUFS_RDU
20729 +       bool "Readdir in userspace"
20730 +       help
20731 +       Aufs has two methods to provide a merged view for a directory,
20732 +       by a user-space library and by kernel-space natively. The latter
20733 +       is always enabled but sometimes large and slow.
20734 +       If you enable this option, install the library in aufs2-util
20735 +       package, and set some environment variables for your readdir(3),
20736 +       then the work will be handled in user-space which generally
20737 +       shows better performance in most cases.
20738 +       See detail in aufs.5.
20739 +
20740 +config AUFS_SHWH
20741 +       bool "Show whiteouts"
20742 +       help
20743 +       If you want to make the whiteouts in aufs visible, then enable
20744 +       this option and specify 'shwh' mount option. Although it may
20745 +       sounds like philosophy or something, but in technically it
20746 +       simply shows the name of whiteout with keeping its behaviour.
20747 +
20748 +config AUFS_BR_RAMFS
20749 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
20750 +       help
20751 +       If you want to use ramfs as an aufs branch fs, then enable this
20752 +       option. Generally tmpfs is recommended.
20753 +       Aufs prohibited them to be a branch fs by default, because
20754 +       initramfs becomes unusable after switch_root or something
20755 +       generally. If you sets initramfs as an aufs branch and boot your
20756 +       system by switch_root, you will meet a problem easily since the
20757 +       files in initramfs may be inaccessible.
20758 +       Unless you are going to use ramfs as an aufs branch fs without
20759 +       switch_root or something, leave it N.
20760 +
20761 +config AUFS_BR_FUSE
20762 +       bool "Fuse fs as an aufs branch"
20763 +       depends on FUSE_FS
20764 +       select AUFS_POLL
20765 +       help
20766 +       If you want to use fuse-based userspace filesystem as an aufs
20767 +       branch fs, then enable this option.
20768 +       It implements the internal poll(2) operation which is
20769 +       implemented by fuse only (curretnly).
20770 +
20771 +config AUFS_POLL
20772 +       bool
20773 +       help
20774 +       Automatic configuration for internal use.
20775 +
20776 +config AUFS_BR_HFSPLUS
20777 +       bool "Hfsplus as an aufs branch"
20778 +       depends on HFSPLUS_FS
20779 +       default y
20780 +       help
20781 +       If you want to use hfsplus fs as an aufs branch fs, then enable
20782 +       this option. This option introduces a small overhead at
20783 +       copying-up a file on hfsplus.
20784 +
20785 +config AUFS_BDEV_LOOP
20786 +       bool
20787 +       depends on BLK_DEV_LOOP
20788 +       default y
20789 +       help
20790 +       Automatic configuration for internal use.
20791 +       Convert =[ym] into =y.
20792 +
20793 +config AUFS_DEBUG
20794 +       bool "Debug aufs"
20795 +       help
20796 +       Enable this to compile aufs internal debug code.
20797 +       It will have a negative impact to the performance.
20798 +
20799 +config AUFS_MAGIC_SYSRQ
20800 +       bool
20801 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
20802 +       default y
20803 +       help
20804 +       Automatic configuration for internal use.
20805 +       When aufs supports Magic SysRq, enabled automatically.
20806 +endif
20807 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
20808 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
20809 +++ linux/fs/aufs/loop.c        2015-04-13 15:10:20.786823613 +0200
20810 @@ -0,0 +1,145 @@
20811 +/*
20812 + * Copyright (C) 2005-2015 Junjiro R. Okajima
20813 + *
20814 + * This program, aufs is free software; you can redistribute it and/or modify
20815 + * it under the terms of the GNU General Public License as published by
20816 + * the Free Software Foundation; either version 2 of the License, or
20817 + * (at your option) any later version.
20818 + *
20819 + * This program is distributed in the hope that it will be useful,
20820 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20821 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20822 + * GNU General Public License for more details.
20823 + *
20824 + * You should have received a copy of the GNU General Public License
20825 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20826 + */
20827 +
20828 +/*
20829 + * support for loopback block device as a branch
20830 + */
20831 +
20832 +#include "aufs.h"
20833 +
20834 +/* added into drivers/block/loop.c */
20835 +static struct file *(*backing_file_func)(struct super_block *sb);
20836 +
20837 +/*
20838 + * test if two lower dentries have overlapping branches.
20839 + */
20840 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
20841 +{
20842 +       struct super_block *h_sb;
20843 +       struct file *backing_file;
20844 +
20845 +       if (unlikely(!backing_file_func)) {
20846 +               /* don't load "loop" module here */
20847 +               backing_file_func = symbol_get(loop_backing_file);
20848 +               if (unlikely(!backing_file_func))
20849 +                       /* "loop" module is not loaded */
20850 +                       return 0;
20851 +       }
20852 +
20853 +       h_sb = h_adding->d_sb;
20854 +       backing_file = backing_file_func(h_sb);
20855 +       if (!backing_file)
20856 +               return 0;
20857 +
20858 +       h_adding = backing_file->f_path.dentry;
20859 +       /*
20860 +        * h_adding can be local NFS.
20861 +        * in this case aufs cannot detect the loop.
20862 +        */
20863 +       if (unlikely(h_adding->d_sb == sb))
20864 +               return 1;
20865 +       return !!au_test_subdir(h_adding, sb->s_root);
20866 +}
20867 +
20868 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
20869 +int au_test_loopback_kthread(void)
20870 +{
20871 +       int ret;
20872 +       struct task_struct *tsk = current;
20873 +       char c, comm[sizeof(tsk->comm)];
20874 +
20875 +       ret = 0;
20876 +       if (tsk->flags & PF_KTHREAD) {
20877 +               get_task_comm(comm, tsk);
20878 +               c = comm[4];
20879 +               ret = ('0' <= c && c <= '9'
20880 +                      && !strncmp(comm, "loop", 4));
20881 +       }
20882 +
20883 +       return ret;
20884 +}
20885 +
20886 +/* ---------------------------------------------------------------------- */
20887 +
20888 +#define au_warn_loopback_step  16
20889 +static int au_warn_loopback_nelem = au_warn_loopback_step;
20890 +static unsigned long *au_warn_loopback_array;
20891 +
20892 +void au_warn_loopback(struct super_block *h_sb)
20893 +{
20894 +       int i, new_nelem;
20895 +       unsigned long *a, magic;
20896 +       static DEFINE_SPINLOCK(spin);
20897 +
20898 +       magic = h_sb->s_magic;
20899 +       spin_lock(&spin);
20900 +       a = au_warn_loopback_array;
20901 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
20902 +               if (a[i] == magic) {
20903 +                       spin_unlock(&spin);
20904 +                       return;
20905 +               }
20906 +
20907 +       /* h_sb is new to us, print it */
20908 +       if (i < au_warn_loopback_nelem) {
20909 +               a[i] = magic;
20910 +               goto pr;
20911 +       }
20912 +
20913 +       /* expand the array */
20914 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
20915 +       a = au_kzrealloc(au_warn_loopback_array,
20916 +                        au_warn_loopback_nelem * sizeof(unsigned long),
20917 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
20918 +       if (a) {
20919 +               au_warn_loopback_nelem = new_nelem;
20920 +               au_warn_loopback_array = a;
20921 +               a[i] = magic;
20922 +               goto pr;
20923 +       }
20924 +
20925 +       spin_unlock(&spin);
20926 +       AuWarn1("realloc failed, ignored\n");
20927 +       return;
20928 +
20929 +pr:
20930 +       spin_unlock(&spin);
20931 +       pr_warn("you may want to try another patch for loopback file "
20932 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
20933 +}
20934 +
20935 +int au_loopback_init(void)
20936 +{
20937 +       int err;
20938 +       struct super_block *sb __maybe_unused;
20939 +
20940 +       AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
20941 +
20942 +       err = 0;
20943 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
20944 +                                        sizeof(unsigned long), GFP_NOFS);
20945 +       if (unlikely(!au_warn_loopback_array))
20946 +               err = -ENOMEM;
20947 +
20948 +       return err;
20949 +}
20950 +
20951 +void au_loopback_fin(void)
20952 +{
20953 +       symbol_put(loop_backing_file);
20954 +       kfree(au_warn_loopback_array);
20955 +}
20956 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
20957 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
20958 +++ linux/fs/aufs/loop.h        2015-04-13 15:10:20.786823613 +0200
20959 @@ -0,0 +1,52 @@
20960 +/*
20961 + * Copyright (C) 2005-2015 Junjiro R. Okajima
20962 + *
20963 + * This program, aufs is free software; you can redistribute it and/or modify
20964 + * it under the terms of the GNU General Public License as published by
20965 + * the Free Software Foundation; either version 2 of the License, or
20966 + * (at your option) any later version.
20967 + *
20968 + * This program is distributed in the hope that it will be useful,
20969 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20970 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20971 + * GNU General Public License for more details.
20972 + *
20973 + * You should have received a copy of the GNU General Public License
20974 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20975 + */
20976 +
20977 +/*
20978 + * support for loopback mount as a branch
20979 + */
20980 +
20981 +#ifndef __AUFS_LOOP_H__
20982 +#define __AUFS_LOOP_H__
20983 +
20984 +#ifdef __KERNEL__
20985 +
20986 +struct dentry;
20987 +struct super_block;
20988 +
20989 +#ifdef CONFIG_AUFS_BDEV_LOOP
20990 +/* drivers/block/loop.c */
20991 +struct file *loop_backing_file(struct super_block *sb);
20992 +
20993 +/* loop.c */
20994 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
20995 +int au_test_loopback_kthread(void);
20996 +void au_warn_loopback(struct super_block *h_sb);
20997 +
20998 +int au_loopback_init(void);
20999 +void au_loopback_fin(void);
21000 +#else
21001 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
21002 +          struct dentry *h_adding)
21003 +AuStubInt0(au_test_loopback_kthread, void)
21004 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
21005 +
21006 +AuStubInt0(au_loopback_init, void)
21007 +AuStubVoid(au_loopback_fin, void)
21008 +#endif /* BLK_DEV_LOOP */
21009 +
21010 +#endif /* __KERNEL__ */
21011 +#endif /* __AUFS_LOOP_H__ */
21012 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
21013 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
21014 +++ linux/fs/aufs/magic.mk      2015-04-13 15:10:20.786823613 +0200
21015 @@ -0,0 +1,30 @@
21016 +
21017 +# defined in ${srctree}/fs/fuse/inode.c
21018 +# tristate
21019 +ifdef CONFIG_FUSE_FS
21020 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
21021 +endif
21022 +
21023 +# defined in ${srctree}/fs/xfs/xfs_sb.h
21024 +# tristate
21025 +ifdef CONFIG_XFS_FS
21026 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
21027 +endif
21028 +
21029 +# defined in ${srctree}/fs/configfs/mount.c
21030 +# tristate
21031 +ifdef CONFIG_CONFIGFS_FS
21032 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
21033 +endif
21034 +
21035 +# defined in ${srctree}/fs/ubifs/ubifs.h
21036 +# tristate
21037 +ifdef CONFIG_UBIFS_FS
21038 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
21039 +endif
21040 +
21041 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
21042 +# tristate
21043 +ifdef CONFIG_HFSPLUS_FS
21044 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
21045 +endif
21046 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
21047 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
21048 +++ linux/fs/aufs/Makefile      2015-04-13 15:10:20.776823376 +0200
21049 @@ -0,0 +1,44 @@
21050 +
21051 +include ${src}/magic.mk
21052 +ifeq (${CONFIG_AUFS_FS},m)
21053 +include ${src}/conf.mk
21054 +endif
21055 +-include ${src}/priv_def.mk
21056 +
21057 +# cf. include/linux/kernel.h
21058 +# enable pr_debug
21059 +ccflags-y += -DDEBUG
21060 +# sparse requires the full pathname
21061 +ifdef M
21062 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
21063 +else
21064 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
21065 +endif
21066 +
21067 +obj-$(CONFIG_AUFS_FS) += aufs.o
21068 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
21069 +       wkq.o vfsub.o dcsub.o \
21070 +       cpup.o whout.o wbr_policy.o \
21071 +       dinfo.o dentry.o \
21072 +       dynop.o \
21073 +       finfo.o file.o f_op.o \
21074 +       dir.o vdir.o \
21075 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
21076 +       mvdown.o ioctl.o
21077 +
21078 +# all are boolean
21079 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
21080 +aufs-$(CONFIG_SYSFS) += sysfs.o
21081 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
21082 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
21083 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
21084 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
21085 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
21086 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
21087 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
21088 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
21089 +aufs-$(CONFIG_AUFS_POLL) += poll.o
21090 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
21091 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
21092 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
21093 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
21094 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
21095 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
21096 +++ linux/fs/aufs/module.c      2015-04-13 15:10:20.786823613 +0200
21097 @@ -0,0 +1,210 @@
21098 +/*
21099 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21100 + *
21101 + * This program, aufs is free software; you can redistribute it and/or modify
21102 + * it under the terms of the GNU General Public License as published by
21103 + * the Free Software Foundation; either version 2 of the License, or
21104 + * (at your option) any later version.
21105 + *
21106 + * This program is distributed in the hope that it will be useful,
21107 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21108 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21109 + * GNU General Public License for more details.
21110 + *
21111 + * You should have received a copy of the GNU General Public License
21112 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21113 + */
21114 +
21115 +/*
21116 + * module global variables and operations
21117 + */
21118 +
21119 +#include <linux/module.h>
21120 +#include <linux/seq_file.h>
21121 +#include "aufs.h"
21122 +
21123 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
21124 +{
21125 +       if (new_sz <= nused)
21126 +               return p;
21127 +
21128 +       p = krealloc(p, new_sz, gfp);
21129 +       if (p)
21130 +               memset(p + nused, 0, new_sz - nused);
21131 +       return p;
21132 +}
21133 +
21134 +/* ---------------------------------------------------------------------- */
21135 +
21136 +/*
21137 + * aufs caches
21138 + */
21139 +struct kmem_cache *au_cachep[AuCache_Last];
21140 +static int __init au_cache_init(void)
21141 +{
21142 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
21143 +       if (au_cachep[AuCache_DINFO])
21144 +               /* SLAB_DESTROY_BY_RCU */
21145 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
21146 +                                                       au_icntnr_init_once);
21147 +       if (au_cachep[AuCache_ICNTNR])
21148 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
21149 +                                                      au_fi_init_once);
21150 +       if (au_cachep[AuCache_FINFO])
21151 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
21152 +       if (au_cachep[AuCache_VDIR])
21153 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
21154 +       if (au_cachep[AuCache_DEHSTR])
21155 +               return 0;
21156 +
21157 +       return -ENOMEM;
21158 +}
21159 +
21160 +static void au_cache_fin(void)
21161 +{
21162 +       int i;
21163 +
21164 +       /*
21165 +        * Make sure all delayed rcu free inodes are flushed before we
21166 +        * destroy cache.
21167 +        */
21168 +       rcu_barrier();
21169 +
21170 +       /* excluding AuCache_HNOTIFY */
21171 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
21172 +       for (i = 0; i < AuCache_HNOTIFY; i++)
21173 +               if (au_cachep[i]) {
21174 +                       kmem_cache_destroy(au_cachep[i]);
21175 +                       au_cachep[i] = NULL;
21176 +               }
21177 +}
21178 +
21179 +/* ---------------------------------------------------------------------- */
21180 +
21181 +int au_dir_roflags;
21182 +
21183 +#ifdef CONFIG_AUFS_SBILIST
21184 +/*
21185 + * iterate_supers_type() doesn't protect us from
21186 + * remounting (branch management)
21187 + */
21188 +struct au_splhead au_sbilist;
21189 +#endif
21190 +
21191 +struct lock_class_key au_lc_key[AuLcKey_Last];
21192 +
21193 +/*
21194 + * functions for module interface.
21195 + */
21196 +MODULE_LICENSE("GPL");
21197 +/* MODULE_LICENSE("GPL v2"); */
21198 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
21199 +MODULE_DESCRIPTION(AUFS_NAME
21200 +       " -- Advanced multi layered unification filesystem");
21201 +MODULE_VERSION(AUFS_VERSION);
21202 +MODULE_ALIAS_FS(AUFS_NAME);
21203 +
21204 +/* this module parameter has no meaning when SYSFS is disabled */
21205 +int sysaufs_brs = 1;
21206 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
21207 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
21208 +
21209 +/* this module parameter has no meaning when USER_NS is disabled */
21210 +static bool au_userns;
21211 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
21212 +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
21213 +
21214 +/* ---------------------------------------------------------------------- */
21215 +
21216 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
21217 +
21218 +int au_seq_path(struct seq_file *seq, struct path *path)
21219 +{
21220 +       return seq_path(seq, path, au_esc_chars);
21221 +}
21222 +
21223 +/* ---------------------------------------------------------------------- */
21224 +
21225 +static int __init aufs_init(void)
21226 +{
21227 +       int err, i;
21228 +       char *p;
21229 +
21230 +       p = au_esc_chars;
21231 +       for (i = 1; i <= ' '; i++)
21232 +               *p++ = i;
21233 +       *p++ = '\\';
21234 +       *p++ = '\x7f';
21235 +       *p = 0;
21236 +
21237 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
21238 +
21239 +       au_sbilist_init();
21240 +       sysaufs_brs_init();
21241 +       au_debug_init();
21242 +       au_dy_init();
21243 +       err = sysaufs_init();
21244 +       if (unlikely(err))
21245 +               goto out;
21246 +       err = au_procfs_init();
21247 +       if (unlikely(err))
21248 +               goto out_sysaufs;
21249 +       err = au_wkq_init();
21250 +       if (unlikely(err))
21251 +               goto out_procfs;
21252 +       err = au_loopback_init();
21253 +       if (unlikely(err))
21254 +               goto out_wkq;
21255 +       err = au_hnotify_init();
21256 +       if (unlikely(err))
21257 +               goto out_loopback;
21258 +       err = au_sysrq_init();
21259 +       if (unlikely(err))
21260 +               goto out_hin;
21261 +       err = au_cache_init();
21262 +       if (unlikely(err))
21263 +               goto out_sysrq;
21264 +
21265 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
21266 +       err = register_filesystem(&aufs_fs_type);
21267 +       if (unlikely(err))
21268 +               goto out_cache;
21269 +
21270 +       /* since we define pr_fmt, call printk directly */
21271 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
21272 +       goto out; /* success */
21273 +
21274 +out_cache:
21275 +       au_cache_fin();
21276 +out_sysrq:
21277 +       au_sysrq_fin();
21278 +out_hin:
21279 +       au_hnotify_fin();
21280 +out_loopback:
21281 +       au_loopback_fin();
21282 +out_wkq:
21283 +       au_wkq_fin();
21284 +out_procfs:
21285 +       au_procfs_fin();
21286 +out_sysaufs:
21287 +       sysaufs_fin();
21288 +       au_dy_fin();
21289 +out:
21290 +       return err;
21291 +}
21292 +
21293 +static void __exit aufs_exit(void)
21294 +{
21295 +       unregister_filesystem(&aufs_fs_type);
21296 +       au_cache_fin();
21297 +       au_sysrq_fin();
21298 +       au_hnotify_fin();
21299 +       au_loopback_fin();
21300 +       au_wkq_fin();
21301 +       au_procfs_fin();
21302 +       sysaufs_fin();
21303 +       au_dy_fin();
21304 +}
21305 +
21306 +module_init(aufs_init);
21307 +module_exit(aufs_exit);
21308 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
21309 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
21310 +++ linux/fs/aufs/module.h      2015-04-13 15:10:20.786823613 +0200
21311 @@ -0,0 +1,104 @@
21312 +/*
21313 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21314 + *
21315 + * This program, aufs is free software; you can redistribute it and/or modify
21316 + * it under the terms of the GNU General Public License as published by
21317 + * the Free Software Foundation; either version 2 of the License, or
21318 + * (at your option) any later version.
21319 + *
21320 + * This program is distributed in the hope that it will be useful,
21321 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21322 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21323 + * GNU General Public License for more details.
21324 + *
21325 + * You should have received a copy of the GNU General Public License
21326 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21327 + */
21328 +
21329 +/*
21330 + * module initialization and module-global
21331 + */
21332 +
21333 +#ifndef __AUFS_MODULE_H__
21334 +#define __AUFS_MODULE_H__
21335 +
21336 +#ifdef __KERNEL__
21337 +
21338 +#include <linux/slab.h>
21339 +
21340 +struct path;
21341 +struct seq_file;
21342 +
21343 +/* module parameters */
21344 +extern int sysaufs_brs;
21345 +
21346 +/* ---------------------------------------------------------------------- */
21347 +
21348 +extern int au_dir_roflags;
21349 +
21350 +enum {
21351 +       AuLcNonDir_FIINFO,
21352 +       AuLcNonDir_DIINFO,
21353 +       AuLcNonDir_IIINFO,
21354 +
21355 +       AuLcDir_FIINFO,
21356 +       AuLcDir_DIINFO,
21357 +       AuLcDir_IIINFO,
21358 +
21359 +       AuLcSymlink_DIINFO,
21360 +       AuLcSymlink_IIINFO,
21361 +
21362 +       AuLcKey_Last
21363 +};
21364 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
21365 +
21366 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
21367 +int au_seq_path(struct seq_file *seq, struct path *path);
21368 +
21369 +#ifdef CONFIG_PROC_FS
21370 +/* procfs.c */
21371 +int __init au_procfs_init(void);
21372 +void au_procfs_fin(void);
21373 +#else
21374 +AuStubInt0(au_procfs_init, void);
21375 +AuStubVoid(au_procfs_fin, void);
21376 +#endif
21377 +
21378 +/* ---------------------------------------------------------------------- */
21379 +
21380 +/* kmem cache */
21381 +enum {
21382 +       AuCache_DINFO,
21383 +       AuCache_ICNTNR,
21384 +       AuCache_FINFO,
21385 +       AuCache_VDIR,
21386 +       AuCache_DEHSTR,
21387 +       AuCache_HNOTIFY, /* must be last */
21388 +       AuCache_Last
21389 +};
21390 +
21391 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
21392 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
21393 +#define AuCacheCtor(type, ctor)        \
21394 +       kmem_cache_create(#type, sizeof(struct type), \
21395 +                         __alignof__(struct type), AuCacheFlags, ctor)
21396 +
21397 +extern struct kmem_cache *au_cachep[];
21398 +
21399 +#define AuCacheFuncs(name, index) \
21400 +static inline struct au_##name *au_cache_alloc_##name(void) \
21401 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
21402 +static inline void au_cache_free_##name(struct au_##name *p) \
21403 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
21404 +
21405 +AuCacheFuncs(dinfo, DINFO);
21406 +AuCacheFuncs(icntnr, ICNTNR);
21407 +AuCacheFuncs(finfo, FINFO);
21408 +AuCacheFuncs(vdir, VDIR);
21409 +AuCacheFuncs(vdir_dehstr, DEHSTR);
21410 +#ifdef CONFIG_AUFS_HNOTIFY
21411 +AuCacheFuncs(hnotify, HNOTIFY);
21412 +#endif
21413 +
21414 +#endif /* __KERNEL__ */
21415 +#endif /* __AUFS_MODULE_H__ */
21416 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
21417 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
21418 +++ linux/fs/aufs/mvdown.c      2015-04-13 15:10:20.786823613 +0200
21419 @@ -0,0 +1,694 @@
21420 +/*
21421 + * Copyright (C) 2011-2015 Junjiro R. Okajima
21422 + *
21423 + * This program, aufs is free software; you can redistribute it and/or modify
21424 + * it under the terms of the GNU General Public License as published by
21425 + * the Free Software Foundation; either version 2 of the License, or
21426 + * (at your option) any later version.
21427 + *
21428 + * This program is distributed in the hope that it will be useful,
21429 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21430 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21431 + * GNU General Public License for more details.
21432 + *
21433 + * You should have received a copy of the GNU General Public License
21434 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21435 + */
21436 +
21437 +/*
21438 + * move-down, opposite of copy-up
21439 + */
21440 +
21441 +#include "aufs.h"
21442 +
21443 +struct au_mvd_args {
21444 +       struct {
21445 +               struct super_block *h_sb;
21446 +               struct dentry *h_parent;
21447 +               struct au_hinode *hdir;
21448 +               struct inode *h_dir, *h_inode;
21449 +               struct au_pin pin;
21450 +       } info[AUFS_MVDOWN_NARRAY];
21451 +
21452 +       struct aufs_mvdown mvdown;
21453 +       struct dentry *dentry, *parent;
21454 +       struct inode *inode, *dir;
21455 +       struct super_block *sb;
21456 +       aufs_bindex_t bopq, bwh, bfound;
21457 +       unsigned char rename_lock;
21458 +};
21459 +
21460 +#define mvd_errno              mvdown.au_errno
21461 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
21462 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
21463 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
21464 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
21465 +
21466 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
21467 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
21468 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
21469 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
21470 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
21471 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
21472 +
21473 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
21474 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
21475 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
21476 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
21477 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
21478 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
21479 +
21480 +#define AU_MVD_PR(flag, ...) do {                      \
21481 +               if (flag)                               \
21482 +                       pr_err(__VA_ARGS__);            \
21483 +       } while (0)
21484 +
21485 +static int find_lower_writable(struct au_mvd_args *a)
21486 +{
21487 +       struct super_block *sb;
21488 +       aufs_bindex_t bindex, bend;
21489 +       struct au_branch *br;
21490 +
21491 +       sb = a->sb;
21492 +       bindex = a->mvd_bsrc;
21493 +       bend = au_sbend(sb);
21494 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
21495 +               for (bindex++; bindex <= bend; bindex++) {
21496 +                       br = au_sbr(sb, bindex);
21497 +                       if (au_br_fhsm(br->br_perm)
21498 +                           && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
21499 +                               return bindex;
21500 +               }
21501 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
21502 +               for (bindex++; bindex <= bend; bindex++) {
21503 +                       br = au_sbr(sb, bindex);
21504 +                       if (!au_br_rdonly(br))
21505 +                               return bindex;
21506 +               }
21507 +       else
21508 +               for (bindex++; bindex <= bend; bindex++) {
21509 +                       br = au_sbr(sb, bindex);
21510 +                       if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
21511 +                               if (au_br_rdonly(br))
21512 +                                       a->mvdown.flags
21513 +                                               |= AUFS_MVDOWN_ROLOWER_R;
21514 +                               return bindex;
21515 +                       }
21516 +               }
21517 +
21518 +       return -1;
21519 +}
21520 +
21521 +/* make the parent dir on bdst */
21522 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
21523 +{
21524 +       int err;
21525 +
21526 +       err = 0;
21527 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
21528 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
21529 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
21530 +       a->mvd_h_dst_parent = NULL;
21531 +       if (au_dbend(a->parent) >= a->mvd_bdst)
21532 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
21533 +       if (!a->mvd_h_dst_parent) {
21534 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
21535 +               if (unlikely(err)) {
21536 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
21537 +                       goto out;
21538 +               }
21539 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
21540 +       }
21541 +
21542 +out:
21543 +       AuTraceErr(err);
21544 +       return err;
21545 +}
21546 +
21547 +/* lock them all */
21548 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
21549 +{
21550 +       int err;
21551 +       struct dentry *h_trap;
21552 +
21553 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
21554 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
21555 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
21556 +                    au_opt_udba(a->sb),
21557 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
21558 +       AuTraceErr(err);
21559 +       if (unlikely(err)) {
21560 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
21561 +               goto out;
21562 +       }
21563 +
21564 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
21565 +               a->rename_lock = 0;
21566 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
21567 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
21568 +                           au_opt_udba(a->sb),
21569 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
21570 +               err = au_do_pin(&a->mvd_pin_src);
21571 +               AuTraceErr(err);
21572 +               a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
21573 +               if (unlikely(err)) {
21574 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
21575 +                       goto out_dst;
21576 +               }
21577 +               goto out; /* success */
21578 +       }
21579 +
21580 +       a->rename_lock = 1;
21581 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
21582 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
21583 +                    au_opt_udba(a->sb),
21584 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
21585 +       AuTraceErr(err);
21586 +       a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
21587 +       if (unlikely(err)) {
21588 +               AU_MVD_PR(dmsg, "pin_src failed\n");
21589 +               au_pin_hdir_lock(&a->mvd_pin_dst);
21590 +               goto out_dst;
21591 +       }
21592 +       au_pin_hdir_unlock(&a->mvd_pin_src);
21593 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
21594 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
21595 +       if (h_trap) {
21596 +               err = (h_trap != a->mvd_h_src_parent);
21597 +               if (err)
21598 +                       err = (h_trap != a->mvd_h_dst_parent);
21599 +       }
21600 +       BUG_ON(err); /* it should never happen */
21601 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
21602 +               err = -EBUSY;
21603 +               AuTraceErr(err);
21604 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
21605 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
21606 +               au_pin_hdir_lock(&a->mvd_pin_src);
21607 +               au_unpin(&a->mvd_pin_src);
21608 +               au_pin_hdir_lock(&a->mvd_pin_dst);
21609 +               goto out_dst;
21610 +       }
21611 +       goto out; /* success */
21612 +
21613 +out_dst:
21614 +       au_unpin(&a->mvd_pin_dst);
21615 +out:
21616 +       AuTraceErr(err);
21617 +       return err;
21618 +}
21619 +
21620 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
21621 +{
21622 +       if (!a->rename_lock)
21623 +               au_unpin(&a->mvd_pin_src);
21624 +       else {
21625 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
21626 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
21627 +               au_pin_hdir_lock(&a->mvd_pin_src);
21628 +               au_unpin(&a->mvd_pin_src);
21629 +               au_pin_hdir_lock(&a->mvd_pin_dst);
21630 +       }
21631 +       au_unpin(&a->mvd_pin_dst);
21632 +}
21633 +
21634 +/* copy-down the file */
21635 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
21636 +{
21637 +       int err;
21638 +       struct au_cp_generic cpg = {
21639 +               .dentry = a->dentry,
21640 +               .bdst   = a->mvd_bdst,
21641 +               .bsrc   = a->mvd_bsrc,
21642 +               .len    = -1,
21643 +               .pin    = &a->mvd_pin_dst,
21644 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
21645 +       };
21646 +
21647 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
21648 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
21649 +               au_fset_cpup(cpg.flags, OVERWRITE);
21650 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
21651 +               au_fset_cpup(cpg.flags, RWDST);
21652 +       err = au_sio_cpdown_simple(&cpg);
21653 +       if (unlikely(err))
21654 +               AU_MVD_PR(dmsg, "cpdown failed\n");
21655 +
21656 +       AuTraceErr(err);
21657 +       return err;
21658 +}
21659 +
21660 +/*
21661 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
21662 + * were sleeping
21663 + */
21664 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
21665 +{
21666 +       int err;
21667 +       struct path h_path;
21668 +       struct au_branch *br;
21669 +       struct inode *delegated;
21670 +
21671 +       br = au_sbr(a->sb, a->mvd_bdst);
21672 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
21673 +       err = PTR_ERR(h_path.dentry);
21674 +       if (IS_ERR(h_path.dentry)) {
21675 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
21676 +               goto out;
21677 +       }
21678 +
21679 +       err = 0;
21680 +       if (h_path.dentry->d_inode) {
21681 +               h_path.mnt = au_br_mnt(br);
21682 +               delegated = NULL;
21683 +               err = vfsub_unlink(a->mvd_h_dst_parent->d_inode, &h_path,
21684 +                                  &delegated, /*force*/0);
21685 +               if (unlikely(err == -EWOULDBLOCK)) {
21686 +                       pr_warn("cannot retry for NFSv4 delegation"
21687 +                               " for an internal unlink\n");
21688 +                       iput(delegated);
21689 +               }
21690 +               if (unlikely(err))
21691 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
21692 +       }
21693 +       dput(h_path.dentry);
21694 +
21695 +out:
21696 +       AuTraceErr(err);
21697 +       return err;
21698 +}
21699 +
21700 +/*
21701 + * unlink the topmost h_dentry
21702 + */
21703 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
21704 +{
21705 +       int err;
21706 +       struct path h_path;
21707 +       struct inode *delegated;
21708 +
21709 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
21710 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
21711 +       delegated = NULL;
21712 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
21713 +       if (unlikely(err == -EWOULDBLOCK)) {
21714 +               pr_warn("cannot retry for NFSv4 delegation"
21715 +                       " for an internal unlink\n");
21716 +               iput(delegated);
21717 +       }
21718 +       if (unlikely(err))
21719 +               AU_MVD_PR(dmsg, "unlink failed\n");
21720 +
21721 +       AuTraceErr(err);
21722 +       return err;
21723 +}
21724 +
21725 +/* Since mvdown succeeded, we ignore an error of this function */
21726 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
21727 +{
21728 +       int err;
21729 +       struct au_branch *br;
21730 +
21731 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
21732 +       br = au_sbr(a->sb, a->mvd_bsrc);
21733 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
21734 +       if (!err) {
21735 +               br = au_sbr(a->sb, a->mvd_bdst);
21736 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
21737 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
21738 +       }
21739 +       if (!err)
21740 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
21741 +       else
21742 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
21743 +}
21744 +
21745 +/*
21746 + * copy-down the file and unlink the bsrc file.
21747 + * - unlink the bdst whout if exist
21748 + * - copy-down the file (with whtmp name and rename)
21749 + * - unlink the bsrc file
21750 + */
21751 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
21752 +{
21753 +       int err;
21754 +
21755 +       err = au_do_mkdir(dmsg, a);
21756 +       if (!err)
21757 +               err = au_do_lock(dmsg, a);
21758 +       if (unlikely(err))
21759 +               goto out;
21760 +
21761 +       /*
21762 +        * do not revert the activities we made on bdst since they should be
21763 +        * harmless in aufs.
21764 +        */
21765 +
21766 +       err = au_do_cpdown(dmsg, a);
21767 +       if (!err)
21768 +               err = au_do_unlink_wh(dmsg, a);
21769 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
21770 +               err = au_do_unlink(dmsg, a);
21771 +       if (unlikely(err))
21772 +               goto out_unlock;
21773 +
21774 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
21775 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
21776 +       if (find_lower_writable(a) < 0)
21777 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
21778 +
21779 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
21780 +               au_do_stfs(dmsg, a);
21781 +
21782 +       /* maintain internal array */
21783 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
21784 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
21785 +               au_set_dbstart(a->dentry, a->mvd_bdst);
21786 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
21787 +               au_set_ibstart(a->inode, a->mvd_bdst);
21788 +       }
21789 +       if (au_dbend(a->dentry) < a->mvd_bdst)
21790 +               au_set_dbend(a->dentry, a->mvd_bdst);
21791 +       if (au_ibend(a->inode) < a->mvd_bdst)
21792 +               au_set_ibend(a->inode, a->mvd_bdst);
21793 +
21794 +out_unlock:
21795 +       au_do_unlock(dmsg, a);
21796 +out:
21797 +       AuTraceErr(err);
21798 +       return err;
21799 +}
21800 +
21801 +/* ---------------------------------------------------------------------- */
21802 +
21803 +/* make sure the file is idle */
21804 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
21805 +{
21806 +       int err, plinked;
21807 +
21808 +       err = 0;
21809 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
21810 +       if (au_dbstart(a->dentry) == a->mvd_bsrc
21811 +           && au_dcount(a->dentry) == 1
21812 +           && atomic_read(&a->inode->i_count) == 1
21813 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
21814 +           && (!plinked || !au_plink_test(a->inode))
21815 +           && a->inode->i_nlink == 1)
21816 +               goto out;
21817 +
21818 +       err = -EBUSY;
21819 +       AU_MVD_PR(dmsg,
21820 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
21821 +                 a->mvd_bsrc, au_dbstart(a->dentry), au_dcount(a->dentry),
21822 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
21823 +                 a->mvd_h_src_inode->i_nlink,
21824 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
21825 +
21826 +out:
21827 +       AuTraceErr(err);
21828 +       return err;
21829 +}
21830 +
21831 +/* make sure the parent dir is fine */
21832 +static int au_mvd_args_parent(const unsigned char dmsg,
21833 +                             struct au_mvd_args *a)
21834 +{
21835 +       int err;
21836 +       aufs_bindex_t bindex;
21837 +
21838 +       err = 0;
21839 +       if (unlikely(au_alive_dir(a->parent))) {
21840 +               err = -ENOENT;
21841 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
21842 +               goto out;
21843 +       }
21844 +
21845 +       a->bopq = au_dbdiropq(a->parent);
21846 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
21847 +       AuDbg("b%d\n", bindex);
21848 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
21849 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
21850 +               err = -EINVAL;
21851 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
21852 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
21853 +                         a->bopq, a->mvd_bdst);
21854 +       }
21855 +
21856 +out:
21857 +       AuTraceErr(err);
21858 +       return err;
21859 +}
21860 +
21861 +static int au_mvd_args_intermediate(const unsigned char dmsg,
21862 +                                   struct au_mvd_args *a)
21863 +{
21864 +       int err;
21865 +       struct au_dinfo *dinfo, *tmp;
21866 +
21867 +       /* lookup the next lower positive entry */
21868 +       err = -ENOMEM;
21869 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
21870 +       if (unlikely(!tmp))
21871 +               goto out;
21872 +
21873 +       a->bfound = -1;
21874 +       a->bwh = -1;
21875 +       dinfo = au_di(a->dentry);
21876 +       au_di_cp(tmp, dinfo);
21877 +       au_di_swap(tmp, dinfo);
21878 +
21879 +       /* returns the number of positive dentries */
21880 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
21881 +       if (!err)
21882 +               a->bwh = au_dbwh(a->dentry);
21883 +       else if (err > 0)
21884 +               a->bfound = au_dbstart(a->dentry);
21885 +
21886 +       au_di_swap(tmp, dinfo);
21887 +       au_rw_write_unlock(&tmp->di_rwsem);
21888 +       au_di_free(tmp);
21889 +       if (unlikely(err < 0))
21890 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
21891 +
21892 +       /*
21893 +        * here, we have these cases.
21894 +        * bfound == -1
21895 +        *      no positive dentry under bsrc. there are more sub-cases.
21896 +        *      bwh < 0
21897 +        *              there no whiteout, we can safely move-down.
21898 +        *      bwh <= bsrc
21899 +        *              impossible
21900 +        *      bsrc < bwh && bwh < bdst
21901 +        *              there is a whiteout on RO branch. cannot proceed.
21902 +        *      bwh == bdst
21903 +        *              there is a whiteout on the RW target branch. it should
21904 +        *              be removed.
21905 +        *      bdst < bwh
21906 +        *              there is a whiteout somewhere unrelated branch.
21907 +        * -1 < bfound && bfound <= bsrc
21908 +        *      impossible.
21909 +        * bfound < bdst
21910 +        *      found, but it is on RO branch between bsrc and bdst. cannot
21911 +        *      proceed.
21912 +        * bfound == bdst
21913 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
21914 +        *      error.
21915 +        * bdst < bfound
21916 +        *      found, after we create the file on bdst, it will be hidden.
21917 +        */
21918 +
21919 +       AuDebugOn(a->bfound == -1
21920 +                 && a->bwh != -1
21921 +                 && a->bwh <= a->mvd_bsrc);
21922 +       AuDebugOn(-1 < a->bfound
21923 +                 && a->bfound <= a->mvd_bsrc);
21924 +
21925 +       err = -EINVAL;
21926 +       if (a->bfound == -1
21927 +           && a->mvd_bsrc < a->bwh
21928 +           && a->bwh != -1
21929 +           && a->bwh < a->mvd_bdst) {
21930 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
21931 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
21932 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
21933 +               goto out;
21934 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
21935 +               a->mvd_errno = EAU_MVDOWN_UPPER;
21936 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
21937 +                         a->mvd_bdst, a->bfound);
21938 +               goto out;
21939 +       }
21940 +
21941 +       err = 0; /* success */
21942 +
21943 +out:
21944 +       AuTraceErr(err);
21945 +       return err;
21946 +}
21947 +
21948 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
21949 +{
21950 +       int err;
21951 +
21952 +       err = 0;
21953 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
21954 +           && a->bfound == a->mvd_bdst)
21955 +               err = -EEXIST;
21956 +       AuTraceErr(err);
21957 +       return err;
21958 +}
21959 +
21960 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
21961 +{
21962 +       int err;
21963 +       struct au_branch *br;
21964 +
21965 +       err = -EISDIR;
21966 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
21967 +               goto out;
21968 +
21969 +       err = -EINVAL;
21970 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
21971 +               a->mvd_bsrc = au_ibstart(a->inode);
21972 +       else {
21973 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
21974 +               if (unlikely(a->mvd_bsrc < 0
21975 +                            || (a->mvd_bsrc < au_dbstart(a->dentry)
21976 +                                || au_dbend(a->dentry) < a->mvd_bsrc
21977 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
21978 +                            || (a->mvd_bsrc < au_ibstart(a->inode)
21979 +                                || au_ibend(a->inode) < a->mvd_bsrc
21980 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
21981 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
21982 +                       AU_MVD_PR(dmsg, "no upper\n");
21983 +                       goto out;
21984 +               }
21985 +       }
21986 +       if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
21987 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
21988 +               AU_MVD_PR(dmsg, "on the bottom\n");
21989 +               goto out;
21990 +       }
21991 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
21992 +       br = au_sbr(a->sb, a->mvd_bsrc);
21993 +       err = au_br_rdonly(br);
21994 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
21995 +               if (unlikely(err))
21996 +                       goto out;
21997 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
21998 +                    || IS_APPEND(a->mvd_h_src_inode))) {
21999 +               if (err)
22000 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
22001 +               /* go on */
22002 +       } else
22003 +               goto out;
22004 +
22005 +       err = -EINVAL;
22006 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
22007 +               a->mvd_bdst = find_lower_writable(a);
22008 +               if (unlikely(a->mvd_bdst < 0)) {
22009 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
22010 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
22011 +                       goto out;
22012 +               }
22013 +       } else {
22014 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
22015 +               if (unlikely(a->mvd_bdst < 0
22016 +                            || au_sbend(a->sb) < a->mvd_bdst)) {
22017 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
22018 +                       AU_MVD_PR(dmsg, "no lower brid\n");
22019 +                       goto out;
22020 +               }
22021 +       }
22022 +
22023 +       err = au_mvd_args_busy(dmsg, a);
22024 +       if (!err)
22025 +               err = au_mvd_args_parent(dmsg, a);
22026 +       if (!err)
22027 +               err = au_mvd_args_intermediate(dmsg, a);
22028 +       if (!err)
22029 +               err = au_mvd_args_exist(dmsg, a);
22030 +       if (!err)
22031 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
22032 +
22033 +out:
22034 +       AuTraceErr(err);
22035 +       return err;
22036 +}
22037 +
22038 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
22039 +{
22040 +       int err, e;
22041 +       unsigned char dmsg;
22042 +       struct au_mvd_args *args;
22043 +
22044 +       err = -EPERM;
22045 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
22046 +               goto out;
22047 +
22048 +       err = -ENOMEM;
22049 +       args = kmalloc(sizeof(*args), GFP_NOFS);
22050 +       if (unlikely(!args))
22051 +               goto out;
22052 +
22053 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
22054 +       if (!err)
22055 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
22056 +       if (unlikely(err)) {
22057 +               err = -EFAULT;
22058 +               AuTraceErr(err);
22059 +               goto out_free;
22060 +       }
22061 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
22062 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
22063 +       args->mvdown.au_errno = 0;
22064 +       args->dentry = dentry;
22065 +       args->inode = dentry->d_inode;
22066 +       args->sb = dentry->d_sb;
22067 +
22068 +       err = -ENOENT;
22069 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
22070 +       args->parent = dget_parent(dentry);
22071 +       args->dir = args->parent->d_inode;
22072 +       mutex_lock_nested(&args->dir->i_mutex, I_MUTEX_PARENT);
22073 +       dput(args->parent);
22074 +       if (unlikely(args->parent != dentry->d_parent)) {
22075 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
22076 +               goto out_dir;
22077 +       }
22078 +
22079 +       mutex_lock_nested(&args->inode->i_mutex, I_MUTEX_CHILD);
22080 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
22081 +       if (unlikely(err))
22082 +               goto out_inode;
22083 +
22084 +       di_write_lock_parent(args->parent);
22085 +       err = au_mvd_args(dmsg, args);
22086 +       if (unlikely(err))
22087 +               goto out_parent;
22088 +
22089 +       err = au_do_mvdown(dmsg, args);
22090 +       if (unlikely(err))
22091 +               goto out_parent;
22092 +
22093 +       au_cpup_attr_timesizes(args->dir);
22094 +       au_cpup_attr_timesizes(args->inode);
22095 +       au_cpup_igen(args->inode, au_h_iptr(args->inode, args->mvd_bdst));
22096 +       /* au_digen_dec(dentry); */
22097 +
22098 +out_parent:
22099 +       di_write_unlock(args->parent);
22100 +       aufs_read_unlock(dentry, AuLock_DW);
22101 +out_inode:
22102 +       mutex_unlock(&args->inode->i_mutex);
22103 +out_dir:
22104 +       mutex_unlock(&args->dir->i_mutex);
22105 +out_free:
22106 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
22107 +       if (unlikely(e))
22108 +               err = -EFAULT;
22109 +       kfree(args);
22110 +out:
22111 +       AuTraceErr(err);
22112 +       return err;
22113 +}
22114 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
22115 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
22116 +++ linux/fs/aufs/opts.c        2015-04-13 15:10:20.786823613 +0200
22117 @@ -0,0 +1,1854 @@
22118 +/*
22119 + * Copyright (C) 2005-2015 Junjiro R. Okajima
22120 + *
22121 + * This program, aufs is free software; you can redistribute it and/or modify
22122 + * it under the terms of the GNU General Public License as published by
22123 + * the Free Software Foundation; either version 2 of the License, or
22124 + * (at your option) any later version.
22125 + *
22126 + * This program is distributed in the hope that it will be useful,
22127 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22128 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22129 + * GNU General Public License for more details.
22130 + *
22131 + * You should have received a copy of the GNU General Public License
22132 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22133 + */
22134 +
22135 +/*
22136 + * mount options/flags
22137 + */
22138 +
22139 +#include <linux/namei.h>
22140 +#include <linux/types.h> /* a distribution requires */
22141 +#include <linux/parser.h>
22142 +#include "aufs.h"
22143 +
22144 +/* ---------------------------------------------------------------------- */
22145 +
22146 +enum {
22147 +       Opt_br,
22148 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
22149 +       Opt_idel, Opt_imod,
22150 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
22151 +       Opt_rdblk_def, Opt_rdhash_def,
22152 +       Opt_xino, Opt_noxino,
22153 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
22154 +       Opt_trunc_xino_path, Opt_itrunc_xino,
22155 +       Opt_trunc_xib, Opt_notrunc_xib,
22156 +       Opt_shwh, Opt_noshwh,
22157 +       Opt_plink, Opt_noplink, Opt_list_plink,
22158 +       Opt_udba,
22159 +       Opt_dio, Opt_nodio,
22160 +       Opt_diropq_a, Opt_diropq_w,
22161 +       Opt_warn_perm, Opt_nowarn_perm,
22162 +       Opt_wbr_copyup, Opt_wbr_create,
22163 +       Opt_fhsm_sec,
22164 +       Opt_refrof, Opt_norefrof,
22165 +       Opt_verbose, Opt_noverbose,
22166 +       Opt_sum, Opt_nosum, Opt_wsum,
22167 +       Opt_dirperm1, Opt_nodirperm1,
22168 +       Opt_acl, Opt_noacl,
22169 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
22170 +};
22171 +
22172 +static match_table_t options = {
22173 +       {Opt_br, "br=%s"},
22174 +       {Opt_br, "br:%s"},
22175 +
22176 +       {Opt_add, "add=%d:%s"},
22177 +       {Opt_add, "add:%d:%s"},
22178 +       {Opt_add, "ins=%d:%s"},
22179 +       {Opt_add, "ins:%d:%s"},
22180 +       {Opt_append, "append=%s"},
22181 +       {Opt_append, "append:%s"},
22182 +       {Opt_prepend, "prepend=%s"},
22183 +       {Opt_prepend, "prepend:%s"},
22184 +
22185 +       {Opt_del, "del=%s"},
22186 +       {Opt_del, "del:%s"},
22187 +       /* {Opt_idel, "idel:%d"}, */
22188 +       {Opt_mod, "mod=%s"},
22189 +       {Opt_mod, "mod:%s"},
22190 +       /* {Opt_imod, "imod:%d:%s"}, */
22191 +
22192 +       {Opt_dirwh, "dirwh=%d"},
22193 +
22194 +       {Opt_xino, "xino=%s"},
22195 +       {Opt_noxino, "noxino"},
22196 +       {Opt_trunc_xino, "trunc_xino"},
22197 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
22198 +       {Opt_notrunc_xino, "notrunc_xino"},
22199 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
22200 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
22201 +       /* {Opt_zxino, "zxino=%s"}, */
22202 +       {Opt_trunc_xib, "trunc_xib"},
22203 +       {Opt_notrunc_xib, "notrunc_xib"},
22204 +
22205 +#ifdef CONFIG_PROC_FS
22206 +       {Opt_plink, "plink"},
22207 +#else
22208 +       {Opt_ignore_silent, "plink"},
22209 +#endif
22210 +
22211 +       {Opt_noplink, "noplink"},
22212 +
22213 +#ifdef CONFIG_AUFS_DEBUG
22214 +       {Opt_list_plink, "list_plink"},
22215 +#endif
22216 +
22217 +       {Opt_udba, "udba=%s"},
22218 +
22219 +       {Opt_dio, "dio"},
22220 +       {Opt_nodio, "nodio"},
22221 +
22222 +#ifdef CONFIG_AUFS_FHSM
22223 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
22224 +#else
22225 +       {Opt_ignore_silent, "fhsm_sec=%d"},
22226 +#endif
22227 +
22228 +       {Opt_diropq_a, "diropq=always"},
22229 +       {Opt_diropq_a, "diropq=a"},
22230 +       {Opt_diropq_w, "diropq=whiteouted"},
22231 +       {Opt_diropq_w, "diropq=w"},
22232 +
22233 +       {Opt_warn_perm, "warn_perm"},
22234 +       {Opt_nowarn_perm, "nowarn_perm"},
22235 +
22236 +       /* keep them temporary */
22237 +       {Opt_ignore_silent, "nodlgt"},
22238 +       {Opt_ignore_silent, "clean_plink"},
22239 +
22240 +#ifdef CONFIG_AUFS_SHWH
22241 +       {Opt_shwh, "shwh"},
22242 +#endif
22243 +       {Opt_noshwh, "noshwh"},
22244 +
22245 +       {Opt_dirperm1, "dirperm1"},
22246 +       {Opt_nodirperm1, "nodirperm1"},
22247 +
22248 +       {Opt_refrof, "refrof"},
22249 +       {Opt_norefrof, "norefrof"},
22250 +
22251 +       {Opt_verbose, "verbose"},
22252 +       {Opt_verbose, "v"},
22253 +       {Opt_noverbose, "noverbose"},
22254 +       {Opt_noverbose, "quiet"},
22255 +       {Opt_noverbose, "q"},
22256 +       {Opt_noverbose, "silent"},
22257 +
22258 +       {Opt_sum, "sum"},
22259 +       {Opt_nosum, "nosum"},
22260 +       {Opt_wsum, "wsum"},
22261 +
22262 +       {Opt_rdcache, "rdcache=%d"},
22263 +       {Opt_rdblk, "rdblk=%d"},
22264 +       {Opt_rdblk_def, "rdblk=def"},
22265 +       {Opt_rdhash, "rdhash=%d"},
22266 +       {Opt_rdhash_def, "rdhash=def"},
22267 +
22268 +       {Opt_wbr_create, "create=%s"},
22269 +       {Opt_wbr_create, "create_policy=%s"},
22270 +       {Opt_wbr_copyup, "cpup=%s"},
22271 +       {Opt_wbr_copyup, "copyup=%s"},
22272 +       {Opt_wbr_copyup, "copyup_policy=%s"},
22273 +
22274 +       /* generic VFS flag */
22275 +#ifdef CONFIG_FS_POSIX_ACL
22276 +       {Opt_acl, "acl"},
22277 +       {Opt_noacl, "noacl"},
22278 +#else
22279 +       {Opt_ignore_silent, "acl"},
22280 +       {Opt_ignore_silent, "noacl"},
22281 +#endif
22282 +
22283 +       /* internal use for the scripts */
22284 +       {Opt_ignore_silent, "si=%s"},
22285 +
22286 +       {Opt_br, "dirs=%s"},
22287 +       {Opt_ignore, "debug=%d"},
22288 +       {Opt_ignore, "delete=whiteout"},
22289 +       {Opt_ignore, "delete=all"},
22290 +       {Opt_ignore, "imap=%s"},
22291 +
22292 +       /* temporary workaround, due to old mount(8)? */
22293 +       {Opt_ignore_silent, "relatime"},
22294 +
22295 +       {Opt_err, NULL}
22296 +};
22297 +
22298 +/* ---------------------------------------------------------------------- */
22299 +
22300 +static const char *au_parser_pattern(int val, match_table_t tbl)
22301 +{
22302 +       struct match_token *p;
22303 +
22304 +       p = tbl;
22305 +       while (p->pattern) {
22306 +               if (p->token == val)
22307 +                       return p->pattern;
22308 +               p++;
22309 +       }
22310 +       BUG();
22311 +       return "??";
22312 +}
22313 +
22314 +static const char *au_optstr(int *val, match_table_t tbl)
22315 +{
22316 +       struct match_token *p;
22317 +       int v;
22318 +
22319 +       v = *val;
22320 +       if (!v)
22321 +               goto out;
22322 +       p = tbl;
22323 +       while (p->pattern) {
22324 +               if (p->token
22325 +                   && (v & p->token) == p->token) {
22326 +                       *val &= ~p->token;
22327 +                       return p->pattern;
22328 +               }
22329 +               p++;
22330 +       }
22331 +
22332 +out:
22333 +       return NULL;
22334 +}
22335 +
22336 +/* ---------------------------------------------------------------------- */
22337 +
22338 +static match_table_t brperm = {
22339 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
22340 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
22341 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
22342 +       {0, NULL}
22343 +};
22344 +
22345 +static match_table_t brattr = {
22346 +       /* general */
22347 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
22348 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
22349 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
22350 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
22351 +#ifdef CONFIG_AUFS_FHSM
22352 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
22353 +#endif
22354 +#ifdef CONFIG_AUFS_XATTR
22355 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
22356 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
22357 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
22358 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
22359 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
22360 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
22361 +#endif
22362 +
22363 +       /* ro/rr branch */
22364 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
22365 +
22366 +       /* rw branch */
22367 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
22368 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
22369 +
22370 +       {0, NULL}
22371 +};
22372 +
22373 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
22374 +{
22375 +       int attr, v;
22376 +       char *p;
22377 +
22378 +       attr = 0;
22379 +       do {
22380 +               p = strchr(str, '+');
22381 +               if (p)
22382 +                       *p = 0;
22383 +               v = match_token(str, table, args);
22384 +               if (v) {
22385 +                       if (v & AuBrAttr_CMOO_Mask)
22386 +                               attr &= ~AuBrAttr_CMOO_Mask;
22387 +                       attr |= v;
22388 +               } else {
22389 +                       if (p)
22390 +                               *p = '+';
22391 +                       pr_warn("ignored branch attribute %s\n", str);
22392 +                       break;
22393 +               }
22394 +               if (p)
22395 +                       str = p + 1;
22396 +       } while (p);
22397 +
22398 +       return attr;
22399 +}
22400 +
22401 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
22402 +{
22403 +       int sz;
22404 +       const char *p;
22405 +       char *q;
22406 +
22407 +       q = str->a;
22408 +       *q = 0;
22409 +       p = au_optstr(&perm, brattr);
22410 +       if (p) {
22411 +               sz = strlen(p);
22412 +               memcpy(q, p, sz + 1);
22413 +               q += sz;
22414 +       } else
22415 +               goto out;
22416 +
22417 +       do {
22418 +               p = au_optstr(&perm, brattr);
22419 +               if (p) {
22420 +                       *q++ = '+';
22421 +                       sz = strlen(p);
22422 +                       memcpy(q, p, sz + 1);
22423 +                       q += sz;
22424 +               }
22425 +       } while (p);
22426 +
22427 +out:
22428 +       return q - str->a;
22429 +}
22430 +
22431 +static int noinline_for_stack br_perm_val(char *perm)
22432 +{
22433 +       int val, bad, sz;
22434 +       char *p;
22435 +       substring_t args[MAX_OPT_ARGS];
22436 +       au_br_perm_str_t attr;
22437 +
22438 +       p = strchr(perm, '+');
22439 +       if (p)
22440 +               *p = 0;
22441 +       val = match_token(perm, brperm, args);
22442 +       if (!val) {
22443 +               if (p)
22444 +                       *p = '+';
22445 +               pr_warn("ignored branch permission %s\n", perm);
22446 +               val = AuBrPerm_RO;
22447 +               goto out;
22448 +       }
22449 +       if (!p)
22450 +               goto out;
22451 +
22452 +       val |= br_attr_val(p + 1, brattr, args);
22453 +
22454 +       bad = 0;
22455 +       switch (val & AuBrPerm_Mask) {
22456 +       case AuBrPerm_RO:
22457 +       case AuBrPerm_RR:
22458 +               bad = val & AuBrWAttr_Mask;
22459 +               val &= ~AuBrWAttr_Mask;
22460 +               break;
22461 +       case AuBrPerm_RW:
22462 +               bad = val & AuBrRAttr_Mask;
22463 +               val &= ~AuBrRAttr_Mask;
22464 +               break;
22465 +       }
22466 +
22467 +       /*
22468 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
22469 +        * does not treat it as an error, just warning.
22470 +        * this is a tiny guard for the user operation.
22471 +        */
22472 +       if (val & AuBrAttr_UNPIN) {
22473 +               bad |= AuBrAttr_UNPIN;
22474 +               val &= ~AuBrAttr_UNPIN;
22475 +       }
22476 +
22477 +       if (unlikely(bad)) {
22478 +               sz = au_do_optstr_br_attr(&attr, bad);
22479 +               AuDebugOn(!sz);
22480 +               pr_warn("ignored branch attribute %s\n", attr.a);
22481 +       }
22482 +
22483 +out:
22484 +       return val;
22485 +}
22486 +
22487 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
22488 +{
22489 +       au_br_perm_str_t attr;
22490 +       const char *p;
22491 +       char *q;
22492 +       int sz;
22493 +
22494 +       q = str->a;
22495 +       p = au_optstr(&perm, brperm);
22496 +       AuDebugOn(!p || !*p);
22497 +       sz = strlen(p);
22498 +       memcpy(q, p, sz + 1);
22499 +       q += sz;
22500 +
22501 +       sz = au_do_optstr_br_attr(&attr, perm);
22502 +       if (sz) {
22503 +               *q++ = '+';
22504 +               memcpy(q, attr.a, sz + 1);
22505 +       }
22506 +
22507 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
22508 +}
22509 +
22510 +/* ---------------------------------------------------------------------- */
22511 +
22512 +static match_table_t udbalevel = {
22513 +       {AuOpt_UDBA_REVAL, "reval"},
22514 +       {AuOpt_UDBA_NONE, "none"},
22515 +#ifdef CONFIG_AUFS_HNOTIFY
22516 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
22517 +#ifdef CONFIG_AUFS_HFSNOTIFY
22518 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
22519 +#endif
22520 +#endif
22521 +       {-1, NULL}
22522 +};
22523 +
22524 +static int noinline_for_stack udba_val(char *str)
22525 +{
22526 +       substring_t args[MAX_OPT_ARGS];
22527 +
22528 +       return match_token(str, udbalevel, args);
22529 +}
22530 +
22531 +const char *au_optstr_udba(int udba)
22532 +{
22533 +       return au_parser_pattern(udba, udbalevel);
22534 +}
22535 +
22536 +/* ---------------------------------------------------------------------- */
22537 +
22538 +static match_table_t au_wbr_create_policy = {
22539 +       {AuWbrCreate_TDP, "tdp"},
22540 +       {AuWbrCreate_TDP, "top-down-parent"},
22541 +       {AuWbrCreate_RR, "rr"},
22542 +       {AuWbrCreate_RR, "round-robin"},
22543 +       {AuWbrCreate_MFS, "mfs"},
22544 +       {AuWbrCreate_MFS, "most-free-space"},
22545 +       {AuWbrCreate_MFSV, "mfs:%d"},
22546 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
22547 +
22548 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
22549 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
22550 +       {AuWbrCreate_PMFS, "pmfs"},
22551 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
22552 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
22553 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
22554 +
22555 +       {-1, NULL}
22556 +};
22557 +
22558 +/*
22559 + * cf. linux/lib/parser.c and cmdline.c
22560 + * gave up calling memparse() since it uses simple_strtoull() instead of
22561 + * kstrto...().
22562 + */
22563 +static int noinline_for_stack
22564 +au_match_ull(substring_t *s, unsigned long long *result)
22565 +{
22566 +       int err;
22567 +       unsigned int len;
22568 +       char a[32];
22569 +
22570 +       err = -ERANGE;
22571 +       len = s->to - s->from;
22572 +       if (len + 1 <= sizeof(a)) {
22573 +               memcpy(a, s->from, len);
22574 +               a[len] = '\0';
22575 +               err = kstrtoull(a, 0, result);
22576 +       }
22577 +       return err;
22578 +}
22579 +
22580 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
22581 +                           struct au_opt_wbr_create *create)
22582 +{
22583 +       int err;
22584 +       unsigned long long ull;
22585 +
22586 +       err = 0;
22587 +       if (!au_match_ull(arg, &ull))
22588 +               create->mfsrr_watermark = ull;
22589 +       else {
22590 +               pr_err("bad integer in %s\n", str);
22591 +               err = -EINVAL;
22592 +       }
22593 +
22594 +       return err;
22595 +}
22596 +
22597 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
22598 +                         struct au_opt_wbr_create *create)
22599 +{
22600 +       int n, err;
22601 +
22602 +       err = 0;
22603 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
22604 +               create->mfs_second = n;
22605 +       else {
22606 +               pr_err("bad integer in %s\n", str);
22607 +               err = -EINVAL;
22608 +       }
22609 +
22610 +       return err;
22611 +}
22612 +
22613 +static int noinline_for_stack
22614 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
22615 +{
22616 +       int err, e;
22617 +       substring_t args[MAX_OPT_ARGS];
22618 +
22619 +       err = match_token(str, au_wbr_create_policy, args);
22620 +       create->wbr_create = err;
22621 +       switch (err) {
22622 +       case AuWbrCreate_MFSRRV:
22623 +       case AuWbrCreate_PMFSRRV:
22624 +               e = au_wbr_mfs_wmark(&args[0], str, create);
22625 +               if (!e)
22626 +                       e = au_wbr_mfs_sec(&args[1], str, create);
22627 +               if (unlikely(e))
22628 +                       err = e;
22629 +               break;
22630 +       case AuWbrCreate_MFSRR:
22631 +       case AuWbrCreate_PMFSRR:
22632 +               e = au_wbr_mfs_wmark(&args[0], str, create);
22633 +               if (unlikely(e)) {
22634 +                       err = e;
22635 +                       break;
22636 +               }
22637 +               /*FALLTHROUGH*/
22638 +       case AuWbrCreate_MFS:
22639 +       case AuWbrCreate_PMFS:
22640 +               create->mfs_second = AUFS_MFS_DEF_SEC;
22641 +               break;
22642 +       case AuWbrCreate_MFSV:
22643 +       case AuWbrCreate_PMFSV:
22644 +               e = au_wbr_mfs_sec(&args[0], str, create);
22645 +               if (unlikely(e))
22646 +                       err = e;
22647 +               break;
22648 +       }
22649 +
22650 +       return err;
22651 +}
22652 +
22653 +const char *au_optstr_wbr_create(int wbr_create)
22654 +{
22655 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
22656 +}
22657 +
22658 +static match_table_t au_wbr_copyup_policy = {
22659 +       {AuWbrCopyup_TDP, "tdp"},
22660 +       {AuWbrCopyup_TDP, "top-down-parent"},
22661 +       {AuWbrCopyup_BUP, "bup"},
22662 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
22663 +       {AuWbrCopyup_BU, "bu"},
22664 +       {AuWbrCopyup_BU, "bottom-up"},
22665 +       {-1, NULL}
22666 +};
22667 +
22668 +static int noinline_for_stack au_wbr_copyup_val(char *str)
22669 +{
22670 +       substring_t args[MAX_OPT_ARGS];
22671 +
22672 +       return match_token(str, au_wbr_copyup_policy, args);
22673 +}
22674 +
22675 +const char *au_optstr_wbr_copyup(int wbr_copyup)
22676 +{
22677 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
22678 +}
22679 +
22680 +/* ---------------------------------------------------------------------- */
22681 +
22682 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
22683 +
22684 +static void dump_opts(struct au_opts *opts)
22685 +{
22686 +#ifdef CONFIG_AUFS_DEBUG
22687 +       /* reduce stack space */
22688 +       union {
22689 +               struct au_opt_add *add;
22690 +               struct au_opt_del *del;
22691 +               struct au_opt_mod *mod;
22692 +               struct au_opt_xino *xino;
22693 +               struct au_opt_xino_itrunc *xino_itrunc;
22694 +               struct au_opt_wbr_create *create;
22695 +       } u;
22696 +       struct au_opt *opt;
22697 +
22698 +       opt = opts->opt;
22699 +       while (opt->type != Opt_tail) {
22700 +               switch (opt->type) {
22701 +               case Opt_add:
22702 +                       u.add = &opt->add;
22703 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
22704 +                                 u.add->bindex, u.add->pathname, u.add->perm,
22705 +                                 u.add->path.dentry);
22706 +                       break;
22707 +               case Opt_del:
22708 +               case Opt_idel:
22709 +                       u.del = &opt->del;
22710 +                       AuDbg("del {%s, %p}\n",
22711 +                             u.del->pathname, u.del->h_path.dentry);
22712 +                       break;
22713 +               case Opt_mod:
22714 +               case Opt_imod:
22715 +                       u.mod = &opt->mod;
22716 +                       AuDbg("mod {%s, 0x%x, %p}\n",
22717 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
22718 +                       break;
22719 +               case Opt_append:
22720 +                       u.add = &opt->add;
22721 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
22722 +                                 u.add->bindex, u.add->pathname, u.add->perm,
22723 +                                 u.add->path.dentry);
22724 +                       break;
22725 +               case Opt_prepend:
22726 +                       u.add = &opt->add;
22727 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
22728 +                                 u.add->bindex, u.add->pathname, u.add->perm,
22729 +                                 u.add->path.dentry);
22730 +                       break;
22731 +               case Opt_dirwh:
22732 +                       AuDbg("dirwh %d\n", opt->dirwh);
22733 +                       break;
22734 +               case Opt_rdcache:
22735 +                       AuDbg("rdcache %d\n", opt->rdcache);
22736 +                       break;
22737 +               case Opt_rdblk:
22738 +                       AuDbg("rdblk %u\n", opt->rdblk);
22739 +                       break;
22740 +               case Opt_rdblk_def:
22741 +                       AuDbg("rdblk_def\n");
22742 +                       break;
22743 +               case Opt_rdhash:
22744 +                       AuDbg("rdhash %u\n", opt->rdhash);
22745 +                       break;
22746 +               case Opt_rdhash_def:
22747 +                       AuDbg("rdhash_def\n");
22748 +                       break;
22749 +               case Opt_xino:
22750 +                       u.xino = &opt->xino;
22751 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
22752 +                       break;
22753 +               case Opt_trunc_xino:
22754 +                       AuLabel(trunc_xino);
22755 +                       break;
22756 +               case Opt_notrunc_xino:
22757 +                       AuLabel(notrunc_xino);
22758 +                       break;
22759 +               case Opt_trunc_xino_path:
22760 +               case Opt_itrunc_xino:
22761 +                       u.xino_itrunc = &opt->xino_itrunc;
22762 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
22763 +                       break;
22764 +               case Opt_noxino:
22765 +                       AuLabel(noxino);
22766 +                       break;
22767 +               case Opt_trunc_xib:
22768 +                       AuLabel(trunc_xib);
22769 +                       break;
22770 +               case Opt_notrunc_xib:
22771 +                       AuLabel(notrunc_xib);
22772 +                       break;
22773 +               case Opt_shwh:
22774 +                       AuLabel(shwh);
22775 +                       break;
22776 +               case Opt_noshwh:
22777 +                       AuLabel(noshwh);
22778 +                       break;
22779 +               case Opt_dirperm1:
22780 +                       AuLabel(dirperm1);
22781 +                       break;
22782 +               case Opt_nodirperm1:
22783 +                       AuLabel(nodirperm1);
22784 +                       break;
22785 +               case Opt_plink:
22786 +                       AuLabel(plink);
22787 +                       break;
22788 +               case Opt_noplink:
22789 +                       AuLabel(noplink);
22790 +                       break;
22791 +               case Opt_list_plink:
22792 +                       AuLabel(list_plink);
22793 +                       break;
22794 +               case Opt_udba:
22795 +                       AuDbg("udba %d, %s\n",
22796 +                                 opt->udba, au_optstr_udba(opt->udba));
22797 +                       break;
22798 +               case Opt_dio:
22799 +                       AuLabel(dio);
22800 +                       break;
22801 +               case Opt_nodio:
22802 +                       AuLabel(nodio);
22803 +                       break;
22804 +               case Opt_diropq_a:
22805 +                       AuLabel(diropq_a);
22806 +                       break;
22807 +               case Opt_diropq_w:
22808 +                       AuLabel(diropq_w);
22809 +                       break;
22810 +               case Opt_warn_perm:
22811 +                       AuLabel(warn_perm);
22812 +                       break;
22813 +               case Opt_nowarn_perm:
22814 +                       AuLabel(nowarn_perm);
22815 +                       break;
22816 +               case Opt_refrof:
22817 +                       AuLabel(refrof);
22818 +                       break;
22819 +               case Opt_norefrof:
22820 +                       AuLabel(norefrof);
22821 +                       break;
22822 +               case Opt_verbose:
22823 +                       AuLabel(verbose);
22824 +                       break;
22825 +               case Opt_noverbose:
22826 +                       AuLabel(noverbose);
22827 +                       break;
22828 +               case Opt_sum:
22829 +                       AuLabel(sum);
22830 +                       break;
22831 +               case Opt_nosum:
22832 +                       AuLabel(nosum);
22833 +                       break;
22834 +               case Opt_wsum:
22835 +                       AuLabel(wsum);
22836 +                       break;
22837 +               case Opt_wbr_create:
22838 +                       u.create = &opt->wbr_create;
22839 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
22840 +                                 au_optstr_wbr_create(u.create->wbr_create));
22841 +                       switch (u.create->wbr_create) {
22842 +                       case AuWbrCreate_MFSV:
22843 +                       case AuWbrCreate_PMFSV:
22844 +                               AuDbg("%d sec\n", u.create->mfs_second);
22845 +                               break;
22846 +                       case AuWbrCreate_MFSRR:
22847 +                               AuDbg("%llu watermark\n",
22848 +                                         u.create->mfsrr_watermark);
22849 +                               break;
22850 +                       case AuWbrCreate_MFSRRV:
22851 +                       case AuWbrCreate_PMFSRRV:
22852 +                               AuDbg("%llu watermark, %d sec\n",
22853 +                                         u.create->mfsrr_watermark,
22854 +                                         u.create->mfs_second);
22855 +                               break;
22856 +                       }
22857 +                       break;
22858 +               case Opt_wbr_copyup:
22859 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
22860 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
22861 +                       break;
22862 +               case Opt_fhsm_sec:
22863 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
22864 +                       break;
22865 +               case Opt_acl:
22866 +                       AuLabel(acl);
22867 +                       break;
22868 +               case Opt_noacl:
22869 +                       AuLabel(noacl);
22870 +                       break;
22871 +               default:
22872 +                       BUG();
22873 +               }
22874 +               opt++;
22875 +       }
22876 +#endif
22877 +}
22878 +
22879 +void au_opts_free(struct au_opts *opts)
22880 +{
22881 +       struct au_opt *opt;
22882 +
22883 +       opt = opts->opt;
22884 +       while (opt->type != Opt_tail) {
22885 +               switch (opt->type) {
22886 +               case Opt_add:
22887 +               case Opt_append:
22888 +               case Opt_prepend:
22889 +                       path_put(&opt->add.path);
22890 +                       break;
22891 +               case Opt_del:
22892 +               case Opt_idel:
22893 +                       path_put(&opt->del.h_path);
22894 +                       break;
22895 +               case Opt_mod:
22896 +               case Opt_imod:
22897 +                       dput(opt->mod.h_root);
22898 +                       break;
22899 +               case Opt_xino:
22900 +                       fput(opt->xino.file);
22901 +                       break;
22902 +               }
22903 +               opt++;
22904 +       }
22905 +}
22906 +
22907 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
22908 +                  aufs_bindex_t bindex)
22909 +{
22910 +       int err;
22911 +       struct au_opt_add *add = &opt->add;
22912 +       char *p;
22913 +
22914 +       add->bindex = bindex;
22915 +       add->perm = AuBrPerm_RO;
22916 +       add->pathname = opt_str;
22917 +       p = strchr(opt_str, '=');
22918 +       if (p) {
22919 +               *p++ = 0;
22920 +               if (*p)
22921 +                       add->perm = br_perm_val(p);
22922 +       }
22923 +
22924 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
22925 +       if (!err) {
22926 +               if (!p) {
22927 +                       add->perm = AuBrPerm_RO;
22928 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
22929 +                               add->perm = AuBrPerm_RR;
22930 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
22931 +                               add->perm = AuBrPerm_RW;
22932 +               }
22933 +               opt->type = Opt_add;
22934 +               goto out;
22935 +       }
22936 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
22937 +       err = -EINVAL;
22938 +
22939 +out:
22940 +       return err;
22941 +}
22942 +
22943 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
22944 +{
22945 +       int err;
22946 +
22947 +       del->pathname = args[0].from;
22948 +       AuDbg("del path %s\n", del->pathname);
22949 +
22950 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
22951 +       if (unlikely(err))
22952 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
22953 +
22954 +       return err;
22955 +}
22956 +
22957 +#if 0 /* reserved for future use */
22958 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
22959 +                             struct au_opt_del *del, substring_t args[])
22960 +{
22961 +       int err;
22962 +       struct dentry *root;
22963 +
22964 +       err = -EINVAL;
22965 +       root = sb->s_root;
22966 +       aufs_read_lock(root, AuLock_FLUSH);
22967 +       if (bindex < 0 || au_sbend(sb) < bindex) {
22968 +               pr_err("out of bounds, %d\n", bindex);
22969 +               goto out;
22970 +       }
22971 +
22972 +       err = 0;
22973 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
22974 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
22975 +
22976 +out:
22977 +       aufs_read_unlock(root, !AuLock_IR);
22978 +       return err;
22979 +}
22980 +#endif
22981 +
22982 +static int noinline_for_stack
22983 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
22984 +{
22985 +       int err;
22986 +       struct path path;
22987 +       char *p;
22988 +
22989 +       err = -EINVAL;
22990 +       mod->path = args[0].from;
22991 +       p = strchr(mod->path, '=');
22992 +       if (unlikely(!p)) {
22993 +               pr_err("no permssion %s\n", args[0].from);
22994 +               goto out;
22995 +       }
22996 +
22997 +       *p++ = 0;
22998 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
22999 +       if (unlikely(err)) {
23000 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
23001 +               goto out;
23002 +       }
23003 +
23004 +       mod->perm = br_perm_val(p);
23005 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
23006 +       mod->h_root = dget(path.dentry);
23007 +       path_put(&path);
23008 +
23009 +out:
23010 +       return err;
23011 +}
23012 +
23013 +#if 0 /* reserved for future use */
23014 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
23015 +                             struct au_opt_mod *mod, substring_t args[])
23016 +{
23017 +       int err;
23018 +       struct dentry *root;
23019 +
23020 +       err = -EINVAL;
23021 +       root = sb->s_root;
23022 +       aufs_read_lock(root, AuLock_FLUSH);
23023 +       if (bindex < 0 || au_sbend(sb) < bindex) {
23024 +               pr_err("out of bounds, %d\n", bindex);
23025 +               goto out;
23026 +       }
23027 +
23028 +       err = 0;
23029 +       mod->perm = br_perm_val(args[1].from);
23030 +       AuDbg("mod path %s, perm 0x%x, %s\n",
23031 +             mod->path, mod->perm, args[1].from);
23032 +       mod->h_root = dget(au_h_dptr(root, bindex));
23033 +
23034 +out:
23035 +       aufs_read_unlock(root, !AuLock_IR);
23036 +       return err;
23037 +}
23038 +#endif
23039 +
23040 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
23041 +                             substring_t args[])
23042 +{
23043 +       int err;
23044 +       struct file *file;
23045 +
23046 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
23047 +       err = PTR_ERR(file);
23048 +       if (IS_ERR(file))
23049 +               goto out;
23050 +
23051 +       err = -EINVAL;
23052 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
23053 +               fput(file);
23054 +               pr_err("%s must be outside\n", args[0].from);
23055 +               goto out;
23056 +       }
23057 +
23058 +       err = 0;
23059 +       xino->file = file;
23060 +       xino->path = args[0].from;
23061 +
23062 +out:
23063 +       return err;
23064 +}
23065 +
23066 +static int noinline_for_stack
23067 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
23068 +                              struct au_opt_xino_itrunc *xino_itrunc,
23069 +                              substring_t args[])
23070 +{
23071 +       int err;
23072 +       aufs_bindex_t bend, bindex;
23073 +       struct path path;
23074 +       struct dentry *root;
23075 +
23076 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
23077 +       if (unlikely(err)) {
23078 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
23079 +               goto out;
23080 +       }
23081 +
23082 +       xino_itrunc->bindex = -1;
23083 +       root = sb->s_root;
23084 +       aufs_read_lock(root, AuLock_FLUSH);
23085 +       bend = au_sbend(sb);
23086 +       for (bindex = 0; bindex <= bend; bindex++) {
23087 +               if (au_h_dptr(root, bindex) == path.dentry) {
23088 +                       xino_itrunc->bindex = bindex;
23089 +                       break;
23090 +               }
23091 +       }
23092 +       aufs_read_unlock(root, !AuLock_IR);
23093 +       path_put(&path);
23094 +
23095 +       if (unlikely(xino_itrunc->bindex < 0)) {
23096 +               pr_err("no such branch %s\n", args[0].from);
23097 +               err = -EINVAL;
23098 +       }
23099 +
23100 +out:
23101 +       return err;
23102 +}
23103 +
23104 +/* called without aufs lock */
23105 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
23106 +{
23107 +       int err, n, token;
23108 +       aufs_bindex_t bindex;
23109 +       unsigned char skipped;
23110 +       struct dentry *root;
23111 +       struct au_opt *opt, *opt_tail;
23112 +       char *opt_str;
23113 +       /* reduce the stack space */
23114 +       union {
23115 +               struct au_opt_xino_itrunc *xino_itrunc;
23116 +               struct au_opt_wbr_create *create;
23117 +       } u;
23118 +       struct {
23119 +               substring_t args[MAX_OPT_ARGS];
23120 +       } *a;
23121 +
23122 +       err = -ENOMEM;
23123 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23124 +       if (unlikely(!a))
23125 +               goto out;
23126 +
23127 +       root = sb->s_root;
23128 +       err = 0;
23129 +       bindex = 0;
23130 +       opt = opts->opt;
23131 +       opt_tail = opt + opts->max_opt - 1;
23132 +       opt->type = Opt_tail;
23133 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
23134 +               err = -EINVAL;
23135 +               skipped = 0;
23136 +               token = match_token(opt_str, options, a->args);
23137 +               switch (token) {
23138 +               case Opt_br:
23139 +                       err = 0;
23140 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
23141 +                              && *opt_str) {
23142 +                               err = opt_add(opt, opt_str, opts->sb_flags,
23143 +                                             bindex++);
23144 +                               if (unlikely(!err && ++opt > opt_tail)) {
23145 +                                       err = -E2BIG;
23146 +                                       break;
23147 +                               }
23148 +                               opt->type = Opt_tail;
23149 +                               skipped = 1;
23150 +                       }
23151 +                       break;
23152 +               case Opt_add:
23153 +                       if (unlikely(match_int(&a->args[0], &n))) {
23154 +                               pr_err("bad integer in %s\n", opt_str);
23155 +                               break;
23156 +                       }
23157 +                       bindex = n;
23158 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
23159 +                                     bindex);
23160 +                       if (!err)
23161 +                               opt->type = token;
23162 +                       break;
23163 +               case Opt_append:
23164 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23165 +                                     /*dummy bindex*/1);
23166 +                       if (!err)
23167 +                               opt->type = token;
23168 +                       break;
23169 +               case Opt_prepend:
23170 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23171 +                                     /*bindex*/0);
23172 +                       if (!err)
23173 +                               opt->type = token;
23174 +                       break;
23175 +               case Opt_del:
23176 +                       err = au_opts_parse_del(&opt->del, a->args);
23177 +                       if (!err)
23178 +                               opt->type = token;
23179 +                       break;
23180 +#if 0 /* reserved for future use */
23181 +               case Opt_idel:
23182 +                       del->pathname = "(indexed)";
23183 +                       if (unlikely(match_int(&args[0], &n))) {
23184 +                               pr_err("bad integer in %s\n", opt_str);
23185 +                               break;
23186 +                       }
23187 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
23188 +                       if (!err)
23189 +                               opt->type = token;
23190 +                       break;
23191 +#endif
23192 +               case Opt_mod:
23193 +                       err = au_opts_parse_mod(&opt->mod, a->args);
23194 +                       if (!err)
23195 +                               opt->type = token;
23196 +                       break;
23197 +#ifdef IMOD /* reserved for future use */
23198 +               case Opt_imod:
23199 +                       u.mod->path = "(indexed)";
23200 +                       if (unlikely(match_int(&a->args[0], &n))) {
23201 +                               pr_err("bad integer in %s\n", opt_str);
23202 +                               break;
23203 +                       }
23204 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
23205 +                       if (!err)
23206 +                               opt->type = token;
23207 +                       break;
23208 +#endif
23209 +               case Opt_xino:
23210 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
23211 +                       if (!err)
23212 +                               opt->type = token;
23213 +                       break;
23214 +
23215 +               case Opt_trunc_xino_path:
23216 +                       err = au_opts_parse_xino_itrunc_path
23217 +                               (sb, &opt->xino_itrunc, a->args);
23218 +                       if (!err)
23219 +                               opt->type = token;
23220 +                       break;
23221 +
23222 +               case Opt_itrunc_xino:
23223 +                       u.xino_itrunc = &opt->xino_itrunc;
23224 +                       if (unlikely(match_int(&a->args[0], &n))) {
23225 +                               pr_err("bad integer in %s\n", opt_str);
23226 +                               break;
23227 +                       }
23228 +                       u.xino_itrunc->bindex = n;
23229 +                       aufs_read_lock(root, AuLock_FLUSH);
23230 +                       if (n < 0 || au_sbend(sb) < n) {
23231 +                               pr_err("out of bounds, %d\n", n);
23232 +                               aufs_read_unlock(root, !AuLock_IR);
23233 +                               break;
23234 +                       }
23235 +                       aufs_read_unlock(root, !AuLock_IR);
23236 +                       err = 0;
23237 +                       opt->type = token;
23238 +                       break;
23239 +
23240 +               case Opt_dirwh:
23241 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
23242 +                               break;
23243 +                       err = 0;
23244 +                       opt->type = token;
23245 +                       break;
23246 +
23247 +               case Opt_rdcache:
23248 +                       if (unlikely(match_int(&a->args[0], &n))) {
23249 +                               pr_err("bad integer in %s\n", opt_str);
23250 +                               break;
23251 +                       }
23252 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
23253 +                               pr_err("rdcache must be smaller than %d\n",
23254 +                                      AUFS_RDCACHE_MAX);
23255 +                               break;
23256 +                       }
23257 +                       opt->rdcache = n;
23258 +                       err = 0;
23259 +                       opt->type = token;
23260 +                       break;
23261 +               case Opt_rdblk:
23262 +                       if (unlikely(match_int(&a->args[0], &n)
23263 +                                    || n < 0
23264 +                                    || n > KMALLOC_MAX_SIZE)) {
23265 +                               pr_err("bad integer in %s\n", opt_str);
23266 +                               break;
23267 +                       }
23268 +                       if (unlikely(n && n < NAME_MAX)) {
23269 +                               pr_err("rdblk must be larger than %d\n",
23270 +                                      NAME_MAX);
23271 +                               break;
23272 +                       }
23273 +                       opt->rdblk = n;
23274 +                       err = 0;
23275 +                       opt->type = token;
23276 +                       break;
23277 +               case Opt_rdhash:
23278 +                       if (unlikely(match_int(&a->args[0], &n)
23279 +                                    || n < 0
23280 +                                    || n * sizeof(struct hlist_head)
23281 +                                    > KMALLOC_MAX_SIZE)) {
23282 +                               pr_err("bad integer in %s\n", opt_str);
23283 +                               break;
23284 +                       }
23285 +                       opt->rdhash = n;
23286 +                       err = 0;
23287 +                       opt->type = token;
23288 +                       break;
23289 +
23290 +               case Opt_trunc_xino:
23291 +               case Opt_notrunc_xino:
23292 +               case Opt_noxino:
23293 +               case Opt_trunc_xib:
23294 +               case Opt_notrunc_xib:
23295 +               case Opt_shwh:
23296 +               case Opt_noshwh:
23297 +               case Opt_dirperm1:
23298 +               case Opt_nodirperm1:
23299 +               case Opt_plink:
23300 +               case Opt_noplink:
23301 +               case Opt_list_plink:
23302 +               case Opt_dio:
23303 +               case Opt_nodio:
23304 +               case Opt_diropq_a:
23305 +               case Opt_diropq_w:
23306 +               case Opt_warn_perm:
23307 +               case Opt_nowarn_perm:
23308 +               case Opt_refrof:
23309 +               case Opt_norefrof:
23310 +               case Opt_verbose:
23311 +               case Opt_noverbose:
23312 +               case Opt_sum:
23313 +               case Opt_nosum:
23314 +               case Opt_wsum:
23315 +               case Opt_rdblk_def:
23316 +               case Opt_rdhash_def:
23317 +               case Opt_acl:
23318 +               case Opt_noacl:
23319 +                       err = 0;
23320 +                       opt->type = token;
23321 +                       break;
23322 +
23323 +               case Opt_udba:
23324 +                       opt->udba = udba_val(a->args[0].from);
23325 +                       if (opt->udba >= 0) {
23326 +                               err = 0;
23327 +                               opt->type = token;
23328 +                       } else
23329 +                               pr_err("wrong value, %s\n", opt_str);
23330 +                       break;
23331 +
23332 +               case Opt_wbr_create:
23333 +                       u.create = &opt->wbr_create;
23334 +                       u.create->wbr_create
23335 +                               = au_wbr_create_val(a->args[0].from, u.create);
23336 +                       if (u.create->wbr_create >= 0) {
23337 +                               err = 0;
23338 +                               opt->type = token;
23339 +                       } else
23340 +                               pr_err("wrong value, %s\n", opt_str);
23341 +                       break;
23342 +               case Opt_wbr_copyup:
23343 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
23344 +                       if (opt->wbr_copyup >= 0) {
23345 +                               err = 0;
23346 +                               opt->type = token;
23347 +                       } else
23348 +                               pr_err("wrong value, %s\n", opt_str);
23349 +                       break;
23350 +
23351 +               case Opt_fhsm_sec:
23352 +                       if (unlikely(match_int(&a->args[0], &n)
23353 +                                    || n < 0)) {
23354 +                               pr_err("bad integer in %s\n", opt_str);
23355 +                               break;
23356 +                       }
23357 +                       if (sysaufs_brs) {
23358 +                               opt->fhsm_second = n;
23359 +                               opt->type = token;
23360 +                       } else
23361 +                               pr_warn("ignored %s\n", opt_str);
23362 +                       err = 0;
23363 +                       break;
23364 +
23365 +               case Opt_ignore:
23366 +                       pr_warn("ignored %s\n", opt_str);
23367 +                       /*FALLTHROUGH*/
23368 +               case Opt_ignore_silent:
23369 +                       skipped = 1;
23370 +                       err = 0;
23371 +                       break;
23372 +               case Opt_err:
23373 +                       pr_err("unknown option %s\n", opt_str);
23374 +                       break;
23375 +               }
23376 +
23377 +               if (!err && !skipped) {
23378 +                       if (unlikely(++opt > opt_tail)) {
23379 +                               err = -E2BIG;
23380 +                               opt--;
23381 +                               opt->type = Opt_tail;
23382 +                               break;
23383 +                       }
23384 +                       opt->type = Opt_tail;
23385 +               }
23386 +       }
23387 +
23388 +       kfree(a);
23389 +       dump_opts(opts);
23390 +       if (unlikely(err))
23391 +               au_opts_free(opts);
23392 +
23393 +out:
23394 +       return err;
23395 +}
23396 +
23397 +static int au_opt_wbr_create(struct super_block *sb,
23398 +                            struct au_opt_wbr_create *create)
23399 +{
23400 +       int err;
23401 +       struct au_sbinfo *sbinfo;
23402 +
23403 +       SiMustWriteLock(sb);
23404 +
23405 +       err = 1; /* handled */
23406 +       sbinfo = au_sbi(sb);
23407 +       if (sbinfo->si_wbr_create_ops->fin) {
23408 +               err = sbinfo->si_wbr_create_ops->fin(sb);
23409 +               if (!err)
23410 +                       err = 1;
23411 +       }
23412 +
23413 +       sbinfo->si_wbr_create = create->wbr_create;
23414 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
23415 +       switch (create->wbr_create) {
23416 +       case AuWbrCreate_MFSRRV:
23417 +       case AuWbrCreate_MFSRR:
23418 +       case AuWbrCreate_PMFSRR:
23419 +       case AuWbrCreate_PMFSRRV:
23420 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
23421 +               /*FALLTHROUGH*/
23422 +       case AuWbrCreate_MFS:
23423 +       case AuWbrCreate_MFSV:
23424 +       case AuWbrCreate_PMFS:
23425 +       case AuWbrCreate_PMFSV:
23426 +               sbinfo->si_wbr_mfs.mfs_expire
23427 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
23428 +               break;
23429 +       }
23430 +
23431 +       if (sbinfo->si_wbr_create_ops->init)
23432 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
23433 +
23434 +       return err;
23435 +}
23436 +
23437 +/*
23438 + * returns,
23439 + * plus: processed without an error
23440 + * zero: unprocessed
23441 + */
23442 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
23443 +                        struct au_opts *opts)
23444 +{
23445 +       int err;
23446 +       struct au_sbinfo *sbinfo;
23447 +
23448 +       SiMustWriteLock(sb);
23449 +
23450 +       err = 1; /* handled */
23451 +       sbinfo = au_sbi(sb);
23452 +       switch (opt->type) {
23453 +       case Opt_udba:
23454 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
23455 +               sbinfo->si_mntflags |= opt->udba;
23456 +               opts->given_udba |= opt->udba;
23457 +               break;
23458 +
23459 +       case Opt_plink:
23460 +               au_opt_set(sbinfo->si_mntflags, PLINK);
23461 +               break;
23462 +       case Opt_noplink:
23463 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
23464 +                       au_plink_put(sb, /*verbose*/1);
23465 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
23466 +               break;
23467 +       case Opt_list_plink:
23468 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
23469 +                       au_plink_list(sb);
23470 +               break;
23471 +
23472 +       case Opt_dio:
23473 +               au_opt_set(sbinfo->si_mntflags, DIO);
23474 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
23475 +               break;
23476 +       case Opt_nodio:
23477 +               au_opt_clr(sbinfo->si_mntflags, DIO);
23478 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
23479 +               break;
23480 +
23481 +       case Opt_fhsm_sec:
23482 +               au_fhsm_set(sbinfo, opt->fhsm_second);
23483 +               break;
23484 +
23485 +       case Opt_diropq_a:
23486 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
23487 +               break;
23488 +       case Opt_diropq_w:
23489 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
23490 +               break;
23491 +
23492 +       case Opt_warn_perm:
23493 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
23494 +               break;
23495 +       case Opt_nowarn_perm:
23496 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
23497 +               break;
23498 +
23499 +       case Opt_refrof:
23500 +               au_opt_set(sbinfo->si_mntflags, REFROF);
23501 +               break;
23502 +       case Opt_norefrof:
23503 +               au_opt_clr(sbinfo->si_mntflags, REFROF);
23504 +               break;
23505 +
23506 +       case Opt_verbose:
23507 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
23508 +               break;
23509 +       case Opt_noverbose:
23510 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
23511 +               break;
23512 +
23513 +       case Opt_sum:
23514 +               au_opt_set(sbinfo->si_mntflags, SUM);
23515 +               break;
23516 +       case Opt_wsum:
23517 +               au_opt_clr(sbinfo->si_mntflags, SUM);
23518 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
23519 +       case Opt_nosum:
23520 +               au_opt_clr(sbinfo->si_mntflags, SUM);
23521 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
23522 +               break;
23523 +
23524 +       case Opt_wbr_create:
23525 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
23526 +               break;
23527 +       case Opt_wbr_copyup:
23528 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
23529 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
23530 +               break;
23531 +
23532 +       case Opt_dirwh:
23533 +               sbinfo->si_dirwh = opt->dirwh;
23534 +               break;
23535 +
23536 +       case Opt_rdcache:
23537 +               sbinfo->si_rdcache
23538 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
23539 +               break;
23540 +       case Opt_rdblk:
23541 +               sbinfo->si_rdblk = opt->rdblk;
23542 +               break;
23543 +       case Opt_rdblk_def:
23544 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
23545 +               break;
23546 +       case Opt_rdhash:
23547 +               sbinfo->si_rdhash = opt->rdhash;
23548 +               break;
23549 +       case Opt_rdhash_def:
23550 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
23551 +               break;
23552 +
23553 +       case Opt_shwh:
23554 +               au_opt_set(sbinfo->si_mntflags, SHWH);
23555 +               break;
23556 +       case Opt_noshwh:
23557 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
23558 +               break;
23559 +
23560 +       case Opt_dirperm1:
23561 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
23562 +               break;
23563 +       case Opt_nodirperm1:
23564 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
23565 +               break;
23566 +
23567 +       case Opt_trunc_xino:
23568 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
23569 +               break;
23570 +       case Opt_notrunc_xino:
23571 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
23572 +               break;
23573 +
23574 +       case Opt_trunc_xino_path:
23575 +       case Opt_itrunc_xino:
23576 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
23577 +               if (!err)
23578 +                       err = 1;
23579 +               break;
23580 +
23581 +       case Opt_trunc_xib:
23582 +               au_fset_opts(opts->flags, TRUNC_XIB);
23583 +               break;
23584 +       case Opt_notrunc_xib:
23585 +               au_fclr_opts(opts->flags, TRUNC_XIB);
23586 +               break;
23587 +
23588 +       case Opt_acl:
23589 +               sb->s_flags |= MS_POSIXACL;
23590 +               break;
23591 +       case Opt_noacl:
23592 +               sb->s_flags &= ~MS_POSIXACL;
23593 +               break;
23594 +
23595 +       default:
23596 +               err = 0;
23597 +               break;
23598 +       }
23599 +
23600 +       return err;
23601 +}
23602 +
23603 +/*
23604 + * returns tri-state.
23605 + * plus: processed without an error
23606 + * zero: unprocessed
23607 + * minus: error
23608 + */
23609 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
23610 +                    struct au_opts *opts)
23611 +{
23612 +       int err, do_refresh;
23613 +
23614 +       err = 0;
23615 +       switch (opt->type) {
23616 +       case Opt_append:
23617 +               opt->add.bindex = au_sbend(sb) + 1;
23618 +               if (opt->add.bindex < 0)
23619 +                       opt->add.bindex = 0;
23620 +               goto add;
23621 +       case Opt_prepend:
23622 +               opt->add.bindex = 0;
23623 +       add: /* indented label */
23624 +       case Opt_add:
23625 +               err = au_br_add(sb, &opt->add,
23626 +                               au_ftest_opts(opts->flags, REMOUNT));
23627 +               if (!err) {
23628 +                       err = 1;
23629 +                       au_fset_opts(opts->flags, REFRESH);
23630 +               }
23631 +               break;
23632 +
23633 +       case Opt_del:
23634 +       case Opt_idel:
23635 +               err = au_br_del(sb, &opt->del,
23636 +                               au_ftest_opts(opts->flags, REMOUNT));
23637 +               if (!err) {
23638 +                       err = 1;
23639 +                       au_fset_opts(opts->flags, TRUNC_XIB);
23640 +                       au_fset_opts(opts->flags, REFRESH);
23641 +               }
23642 +               break;
23643 +
23644 +       case Opt_mod:
23645 +       case Opt_imod:
23646 +               err = au_br_mod(sb, &opt->mod,
23647 +                               au_ftest_opts(opts->flags, REMOUNT),
23648 +                               &do_refresh);
23649 +               if (!err) {
23650 +                       err = 1;
23651 +                       if (do_refresh)
23652 +                               au_fset_opts(opts->flags, REFRESH);
23653 +               }
23654 +               break;
23655 +       }
23656 +
23657 +       return err;
23658 +}
23659 +
23660 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
23661 +                      struct au_opt_xino **opt_xino,
23662 +                      struct au_opts *opts)
23663 +{
23664 +       int err;
23665 +       aufs_bindex_t bend, bindex;
23666 +       struct dentry *root, *parent, *h_root;
23667 +
23668 +       err = 0;
23669 +       switch (opt->type) {
23670 +       case Opt_xino:
23671 +               err = au_xino_set(sb, &opt->xino,
23672 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
23673 +               if (unlikely(err))
23674 +                       break;
23675 +
23676 +               *opt_xino = &opt->xino;
23677 +               au_xino_brid_set(sb, -1);
23678 +
23679 +               /* safe d_parent access */
23680 +               parent = opt->xino.file->f_path.dentry->d_parent;
23681 +               root = sb->s_root;
23682 +               bend = au_sbend(sb);
23683 +               for (bindex = 0; bindex <= bend; bindex++) {
23684 +                       h_root = au_h_dptr(root, bindex);
23685 +                       if (h_root == parent) {
23686 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
23687 +                               break;
23688 +                       }
23689 +               }
23690 +               break;
23691 +
23692 +       case Opt_noxino:
23693 +               au_xino_clr(sb);
23694 +               au_xino_brid_set(sb, -1);
23695 +               *opt_xino = (void *)-1;
23696 +               break;
23697 +       }
23698 +
23699 +       return err;
23700 +}
23701 +
23702 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
23703 +                  unsigned int pending)
23704 +{
23705 +       int err, fhsm;
23706 +       aufs_bindex_t bindex, bend;
23707 +       unsigned char do_plink, skip, do_free;
23708 +       struct au_branch *br;
23709 +       struct au_wbr *wbr;
23710 +       struct dentry *root;
23711 +       struct inode *dir, *h_dir;
23712 +       struct au_sbinfo *sbinfo;
23713 +       struct au_hinode *hdir;
23714 +
23715 +       SiMustAnyLock(sb);
23716 +
23717 +       sbinfo = au_sbi(sb);
23718 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
23719 +
23720 +       if (!(sb_flags & MS_RDONLY)) {
23721 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
23722 +                       pr_warn("first branch should be rw\n");
23723 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
23724 +                       pr_warn("shwh should be used with ro\n");
23725 +       }
23726 +
23727 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
23728 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
23729 +               pr_warn("udba=*notify requires xino\n");
23730 +
23731 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
23732 +               pr_warn("dirperm1 breaks the protection"
23733 +                       " by the permission bits on the lower branch\n");
23734 +
23735 +       err = 0;
23736 +       fhsm = 0;
23737 +       root = sb->s_root;
23738 +       dir = root->d_inode;
23739 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
23740 +       bend = au_sbend(sb);
23741 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
23742 +               skip = 0;
23743 +               h_dir = au_h_iptr(dir, bindex);
23744 +               br = au_sbr(sb, bindex);
23745 +
23746 +               if ((br->br_perm & AuBrAttr_ICEX)
23747 +                   && !h_dir->i_op->listxattr)
23748 +                       br->br_perm &= ~AuBrAttr_ICEX;
23749 +#if 0
23750 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
23751 +                   && (au_br_sb(br)->s_flags & MS_NOSEC))
23752 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
23753 +#endif
23754 +
23755 +               do_free = 0;
23756 +               wbr = br->br_wbr;
23757 +               if (wbr)
23758 +                       wbr_wh_read_lock(wbr);
23759 +
23760 +               if (!au_br_writable(br->br_perm)) {
23761 +                       do_free = !!wbr;
23762 +                       skip = (!wbr
23763 +                               || (!wbr->wbr_whbase
23764 +                                   && !wbr->wbr_plink
23765 +                                   && !wbr->wbr_orph));
23766 +               } else if (!au_br_wh_linkable(br->br_perm)) {
23767 +                       /* skip = (!br->br_whbase && !br->br_orph); */
23768 +                       skip = (!wbr || !wbr->wbr_whbase);
23769 +                       if (skip && wbr) {
23770 +                               if (do_plink)
23771 +                                       skip = !!wbr->wbr_plink;
23772 +                               else
23773 +                                       skip = !wbr->wbr_plink;
23774 +                       }
23775 +               } else {
23776 +                       /* skip = (br->br_whbase && br->br_ohph); */
23777 +                       skip = (wbr && wbr->wbr_whbase);
23778 +                       if (skip) {
23779 +                               if (do_plink)
23780 +                                       skip = !!wbr->wbr_plink;
23781 +                               else
23782 +                                       skip = !wbr->wbr_plink;
23783 +                       }
23784 +               }
23785 +               if (wbr)
23786 +                       wbr_wh_read_unlock(wbr);
23787 +
23788 +               if (au_br_fhsm(br->br_perm)) {
23789 +                       fhsm++;
23790 +                       AuDebugOn(!br->br_fhsm);
23791 +               }
23792 +
23793 +               if (skip)
23794 +                       continue;
23795 +
23796 +               hdir = au_hi(dir, bindex);
23797 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
23798 +               if (wbr)
23799 +                       wbr_wh_write_lock(wbr);
23800 +               err = au_wh_init(br, sb);
23801 +               if (wbr)
23802 +                       wbr_wh_write_unlock(wbr);
23803 +               au_hn_imtx_unlock(hdir);
23804 +
23805 +               if (!err && do_free) {
23806 +                       kfree(wbr);
23807 +                       br->br_wbr = NULL;
23808 +               }
23809 +       }
23810 +
23811 +       if (fhsm >= 2) {
23812 +               au_fset_si(sbinfo, FHSM);
23813 +               for (bindex = bend; bindex >= 0; bindex--) {
23814 +                       br = au_sbr(sb, bindex);
23815 +                       if (au_br_fhsm(br->br_perm)) {
23816 +                               au_fhsm_set_bottom(sb, bindex);
23817 +                               break;
23818 +                       }
23819 +               }
23820 +       } else {
23821 +               au_fclr_si(sbinfo, FHSM);
23822 +               au_fhsm_set_bottom(sb, -1);
23823 +       }
23824 +
23825 +       return err;
23826 +}
23827 +
23828 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
23829 +{
23830 +       int err;
23831 +       unsigned int tmp;
23832 +       aufs_bindex_t bindex, bend;
23833 +       struct au_opt *opt;
23834 +       struct au_opt_xino *opt_xino, xino;
23835 +       struct au_sbinfo *sbinfo;
23836 +       struct au_branch *br;
23837 +       struct inode *dir;
23838 +
23839 +       SiMustWriteLock(sb);
23840 +
23841 +       err = 0;
23842 +       opt_xino = NULL;
23843 +       opt = opts->opt;
23844 +       while (err >= 0 && opt->type != Opt_tail)
23845 +               err = au_opt_simple(sb, opt++, opts);
23846 +       if (err > 0)
23847 +               err = 0;
23848 +       else if (unlikely(err < 0))
23849 +               goto out;
23850 +
23851 +       /* disable xino and udba temporary */
23852 +       sbinfo = au_sbi(sb);
23853 +       tmp = sbinfo->si_mntflags;
23854 +       au_opt_clr(sbinfo->si_mntflags, XINO);
23855 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
23856 +
23857 +       opt = opts->opt;
23858 +       while (err >= 0 && opt->type != Opt_tail)
23859 +               err = au_opt_br(sb, opt++, opts);
23860 +       if (err > 0)
23861 +               err = 0;
23862 +       else if (unlikely(err < 0))
23863 +               goto out;
23864 +
23865 +       bend = au_sbend(sb);
23866 +       if (unlikely(bend < 0)) {
23867 +               err = -EINVAL;
23868 +               pr_err("no branches\n");
23869 +               goto out;
23870 +       }
23871 +
23872 +       if (au_opt_test(tmp, XINO))
23873 +               au_opt_set(sbinfo->si_mntflags, XINO);
23874 +       opt = opts->opt;
23875 +       while (!err && opt->type != Opt_tail)
23876 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
23877 +       if (unlikely(err))
23878 +               goto out;
23879 +
23880 +       err = au_opts_verify(sb, sb->s_flags, tmp);
23881 +       if (unlikely(err))
23882 +               goto out;
23883 +
23884 +       /* restore xino */
23885 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
23886 +               xino.file = au_xino_def(sb);
23887 +               err = PTR_ERR(xino.file);
23888 +               if (IS_ERR(xino.file))
23889 +                       goto out;
23890 +
23891 +               err = au_xino_set(sb, &xino, /*remount*/0);
23892 +               fput(xino.file);
23893 +               if (unlikely(err))
23894 +                       goto out;
23895 +       }
23896 +
23897 +       /* restore udba */
23898 +       tmp &= AuOptMask_UDBA;
23899 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
23900 +       sbinfo->si_mntflags |= tmp;
23901 +       bend = au_sbend(sb);
23902 +       for (bindex = 0; bindex <= bend; bindex++) {
23903 +               br = au_sbr(sb, bindex);
23904 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
23905 +               if (unlikely(err))
23906 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
23907 +                               bindex, err);
23908 +               /* go on even if err */
23909 +       }
23910 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
23911 +               dir = sb->s_root->d_inode;
23912 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
23913 +       }
23914 +
23915 +out:
23916 +       return err;
23917 +}
23918 +
23919 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
23920 +{
23921 +       int err, rerr;
23922 +       struct inode *dir;
23923 +       struct au_opt_xino *opt_xino;
23924 +       struct au_opt *opt;
23925 +       struct au_sbinfo *sbinfo;
23926 +
23927 +       SiMustWriteLock(sb);
23928 +
23929 +       dir = sb->s_root->d_inode;
23930 +       sbinfo = au_sbi(sb);
23931 +       err = 0;
23932 +       opt_xino = NULL;
23933 +       opt = opts->opt;
23934 +       while (err >= 0 && opt->type != Opt_tail) {
23935 +               err = au_opt_simple(sb, opt, opts);
23936 +               if (!err)
23937 +                       err = au_opt_br(sb, opt, opts);
23938 +               if (!err)
23939 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
23940 +               opt++;
23941 +       }
23942 +       if (err > 0)
23943 +               err = 0;
23944 +       AuTraceErr(err);
23945 +       /* go on even err */
23946 +
23947 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
23948 +       if (unlikely(rerr && !err))
23949 +               err = rerr;
23950 +
23951 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
23952 +               rerr = au_xib_trunc(sb);
23953 +               if (unlikely(rerr && !err))
23954 +                       err = rerr;
23955 +       }
23956 +
23957 +       /* will be handled by the caller */
23958 +       if (!au_ftest_opts(opts->flags, REFRESH)
23959 +           && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
23960 +               au_fset_opts(opts->flags, REFRESH);
23961 +
23962 +       AuDbg("status 0x%x\n", opts->flags);
23963 +       return err;
23964 +}
23965 +
23966 +/* ---------------------------------------------------------------------- */
23967 +
23968 +unsigned int au_opt_udba(struct super_block *sb)
23969 +{
23970 +       return au_mntflags(sb) & AuOptMask_UDBA;
23971 +}
23972 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
23973 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
23974 +++ linux/fs/aufs/opts.h        2015-04-13 15:10:20.786823613 +0200
23975 @@ -0,0 +1,211 @@
23976 +/*
23977 + * Copyright (C) 2005-2015 Junjiro R. Okajima
23978 + *
23979 + * This program, aufs is free software; you can redistribute it and/or modify
23980 + * it under the terms of the GNU General Public License as published by
23981 + * the Free Software Foundation; either version 2 of the License, or
23982 + * (at your option) any later version.
23983 + *
23984 + * This program is distributed in the hope that it will be useful,
23985 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23986 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23987 + * GNU General Public License for more details.
23988 + *
23989 + * You should have received a copy of the GNU General Public License
23990 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23991 + */
23992 +
23993 +/*
23994 + * mount options/flags
23995 + */
23996 +
23997 +#ifndef __AUFS_OPTS_H__
23998 +#define __AUFS_OPTS_H__
23999 +
24000 +#ifdef __KERNEL__
24001 +
24002 +#include <linux/path.h>
24003 +
24004 +struct file;
24005 +struct super_block;
24006 +
24007 +/* ---------------------------------------------------------------------- */
24008 +
24009 +/* mount flags */
24010 +#define AuOpt_XINO             1               /* external inode number bitmap
24011 +                                                  and translation table */
24012 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
24013 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
24014 +#define AuOpt_UDBA_REVAL       (1 << 3)
24015 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
24016 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
24017 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
24018 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
24019 +                                                  bits */
24020 +#define AuOpt_REFROF           (1 << 8)        /* unimplemented */
24021 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
24022 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
24023 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
24024 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
24025 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
24026 +#define AuOpt_DIO              (1 << 14)       /* direct io */
24027 +
24028 +#ifndef CONFIG_AUFS_HNOTIFY
24029 +#undef AuOpt_UDBA_HNOTIFY
24030 +#define AuOpt_UDBA_HNOTIFY     0
24031 +#endif
24032 +#ifndef CONFIG_AUFS_SHWH
24033 +#undef AuOpt_SHWH
24034 +#define AuOpt_SHWH             0
24035 +#endif
24036 +
24037 +#define AuOpt_Def      (AuOpt_XINO \
24038 +                        | AuOpt_UDBA_REVAL \
24039 +                        | AuOpt_PLINK \
24040 +                        /* | AuOpt_DIRPERM1 */ \
24041 +                        | AuOpt_WARN_PERM)
24042 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
24043 +                        | AuOpt_UDBA_REVAL \
24044 +                        | AuOpt_UDBA_HNOTIFY)
24045 +
24046 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
24047 +#define au_opt_set(flags, name) do { \
24048 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
24049 +       ((flags) |= AuOpt_##name); \
24050 +} while (0)
24051 +#define au_opt_set_udba(flags, name) do { \
24052 +       (flags) &= ~AuOptMask_UDBA; \
24053 +       ((flags) |= AuOpt_##name); \
24054 +} while (0)
24055 +#define au_opt_clr(flags, name) do { \
24056 +       ((flags) &= ~AuOpt_##name); \
24057 +} while (0)
24058 +
24059 +static inline unsigned int au_opts_plink(unsigned int mntflags)
24060 +{
24061 +#ifdef CONFIG_PROC_FS
24062 +       return mntflags;
24063 +#else
24064 +       return mntflags & ~AuOpt_PLINK;
24065 +#endif
24066 +}
24067 +
24068 +/* ---------------------------------------------------------------------- */
24069 +
24070 +/* policies to select one among multiple writable branches */
24071 +enum {
24072 +       AuWbrCreate_TDP,        /* top down parent */
24073 +       AuWbrCreate_RR,         /* round robin */
24074 +       AuWbrCreate_MFS,        /* most free space */
24075 +       AuWbrCreate_MFSV,       /* mfs with seconds */
24076 +       AuWbrCreate_MFSRR,      /* mfs then rr */
24077 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
24078 +       AuWbrCreate_PMFS,       /* parent and mfs */
24079 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
24080 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
24081 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
24082 +
24083 +       AuWbrCreate_Def = AuWbrCreate_TDP
24084 +};
24085 +
24086 +enum {
24087 +       AuWbrCopyup_TDP,        /* top down parent */
24088 +       AuWbrCopyup_BUP,        /* bottom up parent */
24089 +       AuWbrCopyup_BU,         /* bottom up */
24090 +
24091 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
24092 +};
24093 +
24094 +/* ---------------------------------------------------------------------- */
24095 +
24096 +struct au_opt_add {
24097 +       aufs_bindex_t   bindex;
24098 +       char            *pathname;
24099 +       int             perm;
24100 +       struct path     path;
24101 +};
24102 +
24103 +struct au_opt_del {
24104 +       char            *pathname;
24105 +       struct path     h_path;
24106 +};
24107 +
24108 +struct au_opt_mod {
24109 +       char            *path;
24110 +       int             perm;
24111 +       struct dentry   *h_root;
24112 +};
24113 +
24114 +struct au_opt_xino {
24115 +       char            *path;
24116 +       struct file     *file;
24117 +};
24118 +
24119 +struct au_opt_xino_itrunc {
24120 +       aufs_bindex_t   bindex;
24121 +};
24122 +
24123 +struct au_opt_wbr_create {
24124 +       int                     wbr_create;
24125 +       int                     mfs_second;
24126 +       unsigned long long      mfsrr_watermark;
24127 +};
24128 +
24129 +struct au_opt {
24130 +       int type;
24131 +       union {
24132 +               struct au_opt_xino      xino;
24133 +               struct au_opt_xino_itrunc xino_itrunc;
24134 +               struct au_opt_add       add;
24135 +               struct au_opt_del       del;
24136 +               struct au_opt_mod       mod;
24137 +               int                     dirwh;
24138 +               int                     rdcache;
24139 +               unsigned int            rdblk;
24140 +               unsigned int            rdhash;
24141 +               int                     udba;
24142 +               struct au_opt_wbr_create wbr_create;
24143 +               int                     wbr_copyup;
24144 +               unsigned int            fhsm_second;
24145 +       };
24146 +};
24147 +
24148 +/* opts flags */
24149 +#define AuOpts_REMOUNT         1
24150 +#define AuOpts_REFRESH         (1 << 1)
24151 +#define AuOpts_TRUNC_XIB       (1 << 2)
24152 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
24153 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
24154 +#define au_fset_opts(flags, name) \
24155 +       do { (flags) |= AuOpts_##name; } while (0)
24156 +#define au_fclr_opts(flags, name) \
24157 +       do { (flags) &= ~AuOpts_##name; } while (0)
24158 +
24159 +struct au_opts {
24160 +       struct au_opt   *opt;
24161 +       int             max_opt;
24162 +
24163 +       unsigned int    given_udba;
24164 +       unsigned int    flags;
24165 +       unsigned long   sb_flags;
24166 +};
24167 +
24168 +/* ---------------------------------------------------------------------- */
24169 +
24170 +/* opts.c */
24171 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
24172 +const char *au_optstr_udba(int udba);
24173 +const char *au_optstr_wbr_copyup(int wbr_copyup);
24174 +const char *au_optstr_wbr_create(int wbr_create);
24175 +
24176 +void au_opts_free(struct au_opts *opts);
24177 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
24178 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24179 +                  unsigned int pending);
24180 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
24181 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
24182 +
24183 +unsigned int au_opt_udba(struct super_block *sb);
24184 +
24185 +#endif /* __KERNEL__ */
24186 +#endif /* __AUFS_OPTS_H__ */
24187 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
24188 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
24189 +++ linux/fs/aufs/plink.c       2015-04-13 15:10:20.790157026 +0200
24190 @@ -0,0 +1,532 @@
24191 +/*
24192 + * Copyright (C) 2005-2015 Junjiro R. Okajima
24193 + *
24194 + * This program, aufs is free software; you can redistribute it and/or modify
24195 + * it under the terms of the GNU General Public License as published by
24196 + * the Free Software Foundation; either version 2 of the License, or
24197 + * (at your option) any later version.
24198 + *
24199 + * This program is distributed in the hope that it will be useful,
24200 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24201 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24202 + * GNU General Public License for more details.
24203 + *
24204 + * You should have received a copy of the GNU General Public License
24205 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24206 + */
24207 +
24208 +/*
24209 + * pseudo-link
24210 + */
24211 +
24212 +#include "aufs.h"
24213 +
24214 +/*
24215 + * the pseudo-link maintenance mode.
24216 + * during a user process maintains the pseudo-links,
24217 + * prohibit adding a new plink and branch manipulation.
24218 + *
24219 + * Flags
24220 + * NOPLM:
24221 + *     For entry functions which will handle plink, and i_mutex is already held
24222 + *     in VFS.
24223 + *     They cannot wait and should return an error at once.
24224 + *     Callers has to check the error.
24225 + * NOPLMW:
24226 + *     For entry functions which will handle plink, but i_mutex is not held
24227 + *     in VFS.
24228 + *     They can wait the plink maintenance mode to finish.
24229 + *
24230 + * They behave like F_SETLK and F_SETLKW.
24231 + * If the caller never handle plink, then both flags are unnecessary.
24232 + */
24233 +
24234 +int au_plink_maint(struct super_block *sb, int flags)
24235 +{
24236 +       int err;
24237 +       pid_t pid, ppid;
24238 +       struct au_sbinfo *sbi;
24239 +
24240 +       SiMustAnyLock(sb);
24241 +
24242 +       err = 0;
24243 +       if (!au_opt_test(au_mntflags(sb), PLINK))
24244 +               goto out;
24245 +
24246 +       sbi = au_sbi(sb);
24247 +       pid = sbi->si_plink_maint_pid;
24248 +       if (!pid || pid == current->pid)
24249 +               goto out;
24250 +
24251 +       /* todo: it highly depends upon /sbin/mount.aufs */
24252 +       rcu_read_lock();
24253 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
24254 +       rcu_read_unlock();
24255 +       if (pid == ppid)
24256 +               goto out;
24257 +
24258 +       if (au_ftest_lock(flags, NOPLMW)) {
24259 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
24260 +               /* AuDebugOn(!lockdep_depth(current)); */
24261 +               while (sbi->si_plink_maint_pid) {
24262 +                       si_read_unlock(sb);
24263 +                       /* gave up wake_up_bit() */
24264 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
24265 +
24266 +                       if (au_ftest_lock(flags, FLUSH))
24267 +                               au_nwt_flush(&sbi->si_nowait);
24268 +                       si_noflush_read_lock(sb);
24269 +               }
24270 +       } else if (au_ftest_lock(flags, NOPLM)) {
24271 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
24272 +               err = -EAGAIN;
24273 +       }
24274 +
24275 +out:
24276 +       return err;
24277 +}
24278 +
24279 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
24280 +{
24281 +       spin_lock(&sbinfo->si_plink_maint_lock);
24282 +       sbinfo->si_plink_maint_pid = 0;
24283 +       spin_unlock(&sbinfo->si_plink_maint_lock);
24284 +       wake_up_all(&sbinfo->si_plink_wq);
24285 +}
24286 +
24287 +int au_plink_maint_enter(struct super_block *sb)
24288 +{
24289 +       int err;
24290 +       struct au_sbinfo *sbinfo;
24291 +
24292 +       err = 0;
24293 +       sbinfo = au_sbi(sb);
24294 +       /* make sure i am the only one in this fs */
24295 +       si_write_lock(sb, AuLock_FLUSH);
24296 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
24297 +               spin_lock(&sbinfo->si_plink_maint_lock);
24298 +               if (!sbinfo->si_plink_maint_pid)
24299 +                       sbinfo->si_plink_maint_pid = current->pid;
24300 +               else
24301 +                       err = -EBUSY;
24302 +               spin_unlock(&sbinfo->si_plink_maint_lock);
24303 +       }
24304 +       si_write_unlock(sb);
24305 +
24306 +       return err;
24307 +}
24308 +
24309 +/* ---------------------------------------------------------------------- */
24310 +
24311 +#ifdef CONFIG_AUFS_DEBUG
24312 +void au_plink_list(struct super_block *sb)
24313 +{
24314 +       int i;
24315 +       struct au_sbinfo *sbinfo;
24316 +       struct hlist_head *plink_hlist;
24317 +       struct pseudo_link *plink;
24318 +
24319 +       SiMustAnyLock(sb);
24320 +
24321 +       sbinfo = au_sbi(sb);
24322 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
24323 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
24324 +
24325 +       for (i = 0; i < AuPlink_NHASH; i++) {
24326 +               plink_hlist = &sbinfo->si_plink[i].head;
24327 +               rcu_read_lock();
24328 +               hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
24329 +                       AuDbg("%lu\n", plink->inode->i_ino);
24330 +               rcu_read_unlock();
24331 +       }
24332 +}
24333 +#endif
24334 +
24335 +/* is the inode pseudo-linked? */
24336 +int au_plink_test(struct inode *inode)
24337 +{
24338 +       int found, i;
24339 +       struct au_sbinfo *sbinfo;
24340 +       struct hlist_head *plink_hlist;
24341 +       struct pseudo_link *plink;
24342 +
24343 +       sbinfo = au_sbi(inode->i_sb);
24344 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24345 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
24346 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
24347 +
24348 +       found = 0;
24349 +       i = au_plink_hash(inode->i_ino);
24350 +       plink_hlist = &sbinfo->si_plink[i].head;
24351 +       rcu_read_lock();
24352 +       hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
24353 +               if (plink->inode == inode) {
24354 +                       found = 1;
24355 +                       break;
24356 +               }
24357 +       rcu_read_unlock();
24358 +       return found;
24359 +}
24360 +
24361 +/* ---------------------------------------------------------------------- */
24362 +
24363 +/*
24364 + * generate a name for plink.
24365 + * the file will be stored under AUFS_WH_PLINKDIR.
24366 + */
24367 +/* 20 is max digits length of ulong 64 */
24368 +#define PLINK_NAME_LEN ((20 + 1) * 2)
24369 +
24370 +static int plink_name(char *name, int len, struct inode *inode,
24371 +                     aufs_bindex_t bindex)
24372 +{
24373 +       int rlen;
24374 +       struct inode *h_inode;
24375 +
24376 +       h_inode = au_h_iptr(inode, bindex);
24377 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
24378 +       return rlen;
24379 +}
24380 +
24381 +struct au_do_plink_lkup_args {
24382 +       struct dentry **errp;
24383 +       struct qstr *tgtname;
24384 +       struct dentry *h_parent;
24385 +       struct au_branch *br;
24386 +};
24387 +
24388 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
24389 +                                      struct dentry *h_parent,
24390 +                                      struct au_branch *br)
24391 +{
24392 +       struct dentry *h_dentry;
24393 +       struct mutex *h_mtx;
24394 +
24395 +       h_mtx = &h_parent->d_inode->i_mutex;
24396 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
24397 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
24398 +       mutex_unlock(h_mtx);
24399 +       return h_dentry;
24400 +}
24401 +
24402 +static void au_call_do_plink_lkup(void *args)
24403 +{
24404 +       struct au_do_plink_lkup_args *a = args;
24405 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
24406 +}
24407 +
24408 +/* lookup the plink-ed @inode under the branch at @bindex */
24409 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
24410 +{
24411 +       struct dentry *h_dentry, *h_parent;
24412 +       struct au_branch *br;
24413 +       struct inode *h_dir;
24414 +       int wkq_err;
24415 +       char a[PLINK_NAME_LEN];
24416 +       struct qstr tgtname = QSTR_INIT(a, 0);
24417 +
24418 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
24419 +
24420 +       br = au_sbr(inode->i_sb, bindex);
24421 +       h_parent = br->br_wbr->wbr_plink;
24422 +       h_dir = h_parent->d_inode;
24423 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
24424 +
24425 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
24426 +               struct au_do_plink_lkup_args args = {
24427 +                       .errp           = &h_dentry,
24428 +                       .tgtname        = &tgtname,
24429 +                       .h_parent       = h_parent,
24430 +                       .br             = br
24431 +               };
24432 +
24433 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
24434 +               if (unlikely(wkq_err))
24435 +                       h_dentry = ERR_PTR(wkq_err);
24436 +       } else
24437 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
24438 +
24439 +       return h_dentry;
24440 +}
24441 +
24442 +/* create a pseudo-link */
24443 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
24444 +                     struct dentry *h_dentry, struct au_branch *br)
24445 +{
24446 +       int err;
24447 +       struct path h_path = {
24448 +               .mnt = au_br_mnt(br)
24449 +       };
24450 +       struct inode *h_dir, *delegated;
24451 +
24452 +       h_dir = h_parent->d_inode;
24453 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
24454 +again:
24455 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
24456 +       err = PTR_ERR(h_path.dentry);
24457 +       if (IS_ERR(h_path.dentry))
24458 +               goto out;
24459 +
24460 +       err = 0;
24461 +       /* wh.plink dir is not monitored */
24462 +       /* todo: is it really safe? */
24463 +       if (h_path.dentry->d_inode
24464 +           && h_path.dentry->d_inode != h_dentry->d_inode) {
24465 +               delegated = NULL;
24466 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
24467 +               if (unlikely(err == -EWOULDBLOCK)) {
24468 +                       pr_warn("cannot retry for NFSv4 delegation"
24469 +                               " for an internal unlink\n");
24470 +                       iput(delegated);
24471 +               }
24472 +               dput(h_path.dentry);
24473 +               h_path.dentry = NULL;
24474 +               if (!err)
24475 +                       goto again;
24476 +       }
24477 +       if (!err && !h_path.dentry->d_inode) {
24478 +               delegated = NULL;
24479 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
24480 +               if (unlikely(err == -EWOULDBLOCK)) {
24481 +                       pr_warn("cannot retry for NFSv4 delegation"
24482 +                               " for an internal link\n");
24483 +                       iput(delegated);
24484 +               }
24485 +       }
24486 +       dput(h_path.dentry);
24487 +
24488 +out:
24489 +       mutex_unlock(&h_dir->i_mutex);
24490 +       return err;
24491 +}
24492 +
24493 +struct do_whplink_args {
24494 +       int *errp;
24495 +       struct qstr *tgt;
24496 +       struct dentry *h_parent;
24497 +       struct dentry *h_dentry;
24498 +       struct au_branch *br;
24499 +};
24500 +
24501 +static void call_do_whplink(void *args)
24502 +{
24503 +       struct do_whplink_args *a = args;
24504 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
24505 +}
24506 +
24507 +static int whplink(struct dentry *h_dentry, struct inode *inode,
24508 +                  aufs_bindex_t bindex, struct au_branch *br)
24509 +{
24510 +       int err, wkq_err;
24511 +       struct au_wbr *wbr;
24512 +       struct dentry *h_parent;
24513 +       struct inode *h_dir;
24514 +       char a[PLINK_NAME_LEN];
24515 +       struct qstr tgtname = QSTR_INIT(a, 0);
24516 +
24517 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
24518 +       h_parent = wbr->wbr_plink;
24519 +       h_dir = h_parent->d_inode;
24520 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
24521 +
24522 +       /* always superio. */
24523 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
24524 +               struct do_whplink_args args = {
24525 +                       .errp           = &err,
24526 +                       .tgt            = &tgtname,
24527 +                       .h_parent       = h_parent,
24528 +                       .h_dentry       = h_dentry,
24529 +                       .br             = br
24530 +               };
24531 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
24532 +               if (unlikely(wkq_err))
24533 +                       err = wkq_err;
24534 +       } else
24535 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
24536 +
24537 +       return err;
24538 +}
24539 +
24540 +/* free a single plink */
24541 +static void do_put_plink(struct pseudo_link *plink, int do_del)
24542 +{
24543 +       if (do_del)
24544 +               hlist_del(&plink->hlist);
24545 +       iput(plink->inode);
24546 +       kfree(plink);
24547 +}
24548 +
24549 +static void do_put_plink_rcu(struct rcu_head *rcu)
24550 +{
24551 +       struct pseudo_link *plink;
24552 +
24553 +       plink = container_of(rcu, struct pseudo_link, rcu);
24554 +       iput(plink->inode);
24555 +       kfree(plink);
24556 +}
24557 +
24558 +/*
24559 + * create a new pseudo-link for @h_dentry on @bindex.
24560 + * the linked inode is held in aufs @inode.
24561 + */
24562 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
24563 +                    struct dentry *h_dentry)
24564 +{
24565 +       struct super_block *sb;
24566 +       struct au_sbinfo *sbinfo;
24567 +       struct hlist_head *plink_hlist;
24568 +       struct pseudo_link *plink, *tmp;
24569 +       struct au_sphlhead *sphl;
24570 +       int found, err, cnt, i;
24571 +
24572 +       sb = inode->i_sb;
24573 +       sbinfo = au_sbi(sb);
24574 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
24575 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
24576 +
24577 +       found = au_plink_test(inode);
24578 +       if (found)
24579 +               return;
24580 +
24581 +       i = au_plink_hash(inode->i_ino);
24582 +       sphl = sbinfo->si_plink + i;
24583 +       plink_hlist = &sphl->head;
24584 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
24585 +       if (tmp)
24586 +               tmp->inode = au_igrab(inode);
24587 +       else {
24588 +               err = -ENOMEM;
24589 +               goto out;
24590 +       }
24591 +
24592 +       spin_lock(&sphl->spin);
24593 +       hlist_for_each_entry(plink, plink_hlist, hlist) {
24594 +               if (plink->inode == inode) {
24595 +                       found = 1;
24596 +                       break;
24597 +               }
24598 +       }
24599 +       if (!found)
24600 +               hlist_add_head_rcu(&tmp->hlist, plink_hlist);
24601 +       spin_unlock(&sphl->spin);
24602 +       if (!found) {
24603 +               cnt = au_sphl_count(sphl);
24604 +#define msg "unexpectedly unblanced or too many pseudo-links"
24605 +               if (cnt > AUFS_PLINK_WARN)
24606 +                       AuWarn1(msg ", %d\n", cnt);
24607 +#undef msg
24608 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
24609 +       } else {
24610 +               do_put_plink(tmp, 0);
24611 +               return;
24612 +       }
24613 +
24614 +out:
24615 +       if (unlikely(err)) {
24616 +               pr_warn("err %d, damaged pseudo link.\n", err);
24617 +               if (tmp) {
24618 +                       au_sphl_del_rcu(&tmp->hlist, sphl);
24619 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
24620 +               }
24621 +       }
24622 +}
24623 +
24624 +/* free all plinks */
24625 +void au_plink_put(struct super_block *sb, int verbose)
24626 +{
24627 +       int i, warned;
24628 +       struct au_sbinfo *sbinfo;
24629 +       struct hlist_head *plink_hlist;
24630 +       struct hlist_node *tmp;
24631 +       struct pseudo_link *plink;
24632 +
24633 +       SiMustWriteLock(sb);
24634 +
24635 +       sbinfo = au_sbi(sb);
24636 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
24637 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
24638 +
24639 +       /* no spin_lock since sbinfo is write-locked */
24640 +       warned = 0;
24641 +       for (i = 0; i < AuPlink_NHASH; i++) {
24642 +               plink_hlist = &sbinfo->si_plink[i].head;
24643 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
24644 +                       pr_warn("pseudo-link is not flushed");
24645 +                       warned = 1;
24646 +               }
24647 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
24648 +                       do_put_plink(plink, 0);
24649 +               INIT_HLIST_HEAD(plink_hlist);
24650 +       }
24651 +}
24652 +
24653 +void au_plink_clean(struct super_block *sb, int verbose)
24654 +{
24655 +       struct dentry *root;
24656 +
24657 +       root = sb->s_root;
24658 +       aufs_write_lock(root);
24659 +       if (au_opt_test(au_mntflags(sb), PLINK))
24660 +               au_plink_put(sb, verbose);
24661 +       aufs_write_unlock(root);
24662 +}
24663 +
24664 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
24665 +{
24666 +       int do_put;
24667 +       aufs_bindex_t bstart, bend, bindex;
24668 +
24669 +       do_put = 0;
24670 +       bstart = au_ibstart(inode);
24671 +       bend = au_ibend(inode);
24672 +       if (bstart >= 0) {
24673 +               for (bindex = bstart; bindex <= bend; bindex++) {
24674 +                       if (!au_h_iptr(inode, bindex)
24675 +                           || au_ii_br_id(inode, bindex) != br_id)
24676 +                               continue;
24677 +                       au_set_h_iptr(inode, bindex, NULL, 0);
24678 +                       do_put = 1;
24679 +                       break;
24680 +               }
24681 +               if (do_put)
24682 +                       for (bindex = bstart; bindex <= bend; bindex++)
24683 +                               if (au_h_iptr(inode, bindex)) {
24684 +                                       do_put = 0;
24685 +                                       break;
24686 +                               }
24687 +       } else
24688 +               do_put = 1;
24689 +
24690 +       return do_put;
24691 +}
24692 +
24693 +/* free the plinks on a branch specified by @br_id */
24694 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
24695 +{
24696 +       struct au_sbinfo *sbinfo;
24697 +       struct hlist_head *plink_hlist;
24698 +       struct hlist_node *tmp;
24699 +       struct pseudo_link *plink;
24700 +       struct inode *inode;
24701 +       int i, do_put;
24702 +
24703 +       SiMustWriteLock(sb);
24704 +
24705 +       sbinfo = au_sbi(sb);
24706 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
24707 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
24708 +
24709 +       /* no spin_lock since sbinfo is write-locked */
24710 +       for (i = 0; i < AuPlink_NHASH; i++) {
24711 +               plink_hlist = &sbinfo->si_plink[i].head;
24712 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
24713 +                       inode = au_igrab(plink->inode);
24714 +                       ii_write_lock_child(inode);
24715 +                       do_put = au_plink_do_half_refresh(inode, br_id);
24716 +                       if (do_put)
24717 +                               do_put_plink(plink, 1);
24718 +                       ii_write_unlock(inode);
24719 +                       iput(inode);
24720 +               }
24721 +       }
24722 +}
24723 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
24724 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
24725 +++ linux/fs/aufs/poll.c        2015-04-13 15:10:20.790157026 +0200
24726 @@ -0,0 +1,55 @@
24727 +/*
24728 + * Copyright (C) 2005-2015 Junjiro R. Okajima
24729 + *
24730 + * This program, aufs is free software; you can redistribute it and/or modify
24731 + * it under the terms of the GNU General Public License as published by
24732 + * the Free Software Foundation; either version 2 of the License, or
24733 + * (at your option) any later version.
24734 + *
24735 + * This program is distributed in the hope that it will be useful,
24736 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24737 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24738 + * GNU General Public License for more details.
24739 + *
24740 + * You should have received a copy of the GNU General Public License
24741 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24742 + */
24743 +
24744 +/*
24745 + * poll operation
24746 + * There is only one filesystem which implements ->poll operation, currently.
24747 + */
24748 +
24749 +#include "aufs.h"
24750 +
24751 +unsigned int aufs_poll(struct file *file, poll_table *wait)
24752 +{
24753 +       unsigned int mask;
24754 +       int err;
24755 +       struct file *h_file;
24756 +       struct dentry *dentry;
24757 +       struct super_block *sb;
24758 +
24759 +       /* We should pretend an error happened. */
24760 +       mask = POLLERR /* | POLLIN | POLLOUT */;
24761 +       dentry = file->f_path.dentry;
24762 +       sb = dentry->d_sb;
24763 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
24764 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
24765 +       if (unlikely(err))
24766 +               goto out;
24767 +
24768 +       /* it is not an error if h_file has no operation */
24769 +       mask = DEFAULT_POLLMASK;
24770 +       h_file = au_hf_top(file);
24771 +       if (h_file->f_op->poll)
24772 +               mask = h_file->f_op->poll(h_file, wait);
24773 +
24774 +       di_read_unlock(dentry, AuLock_IR);
24775 +       fi_read_unlock(file);
24776 +
24777 +out:
24778 +       si_read_unlock(sb);
24779 +       AuTraceErr((int)mask);
24780 +       return mask;
24781 +}
24782 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
24783 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
24784 +++ linux/fs/aufs/posix_acl.c   2015-04-13 15:10:20.790157026 +0200
24785 @@ -0,0 +1,99 @@
24786 +/*
24787 + * Copyright (C) 2014-2015 Junjiro R. Okajima
24788 + *
24789 + * This program, aufs is free software; you can redistribute it and/or modify
24790 + * it under the terms of the GNU General Public License as published by
24791 + * the Free Software Foundation; either version 2 of the License, or
24792 + * (at your option) any later version.
24793 + *
24794 + * This program is distributed in the hope that it will be useful,
24795 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24796 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24797 + * GNU General Public License for more details.
24798 + *
24799 + * You should have received a copy of the GNU General Public License
24800 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24801 + */
24802 +
24803 +/*
24804 + * posix acl operations
24805 + */
24806 +
24807 +#include <linux/fs.h>
24808 +#include <linux/posix_acl.h>
24809 +#include "aufs.h"
24810 +
24811 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
24812 +{
24813 +       struct posix_acl *acl;
24814 +       int err;
24815 +       aufs_bindex_t bindex;
24816 +       struct inode *h_inode;
24817 +       struct super_block *sb;
24818 +
24819 +       acl = NULL;
24820 +       sb = inode->i_sb;
24821 +       si_read_lock(sb, AuLock_FLUSH);
24822 +       ii_read_lock_child(inode);
24823 +       if (!(sb->s_flags & MS_POSIXACL))
24824 +               goto out;
24825 +
24826 +       bindex = au_ibstart(inode);
24827 +       h_inode = au_h_iptr(inode, bindex);
24828 +       if (unlikely(!h_inode
24829 +                    || ((h_inode->i_mode & S_IFMT)
24830 +                        != (inode->i_mode & S_IFMT)))) {
24831 +               err = au_busy_or_stale();
24832 +               acl = ERR_PTR(err);
24833 +               goto out;
24834 +       }
24835 +
24836 +       /* always topmost only */
24837 +       acl = get_acl(h_inode, type);
24838 +
24839 +out:
24840 +       ii_read_unlock(inode);
24841 +       si_read_unlock(sb);
24842 +
24843 +       AuTraceErrPtr(acl);
24844 +       return acl;
24845 +}
24846 +
24847 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
24848 +{
24849 +       int err;
24850 +       ssize_t ssz;
24851 +       struct dentry *dentry;
24852 +       struct au_srxattr arg = {
24853 +               .type = AU_ACL_SET,
24854 +               .u.acl_set = {
24855 +                       .acl    = acl,
24856 +                       .type   = type
24857 +               },
24858 +       };
24859 +
24860 +       mutex_lock(&inode->i_mutex);
24861 +       if (inode->i_ino == AUFS_ROOT_INO)
24862 +               dentry = dget(inode->i_sb->s_root);
24863 +       else {
24864 +               dentry = d_find_alias(inode);
24865 +               if (!dentry)
24866 +                       dentry = d_find_any_alias(inode);
24867 +               if (!dentry) {
24868 +                       pr_warn("cannot handle this inode, "
24869 +                               "please report to aufs-users ML\n");
24870 +                       err = -ENOENT;
24871 +                       goto out;
24872 +               }
24873 +       }
24874 +
24875 +       ssz = au_srxattr(dentry, &arg);
24876 +       dput(dentry);
24877 +       err = ssz;
24878 +       if (ssz >= 0)
24879 +               err = 0;
24880 +
24881 +out:
24882 +       mutex_unlock(&inode->i_mutex);
24883 +       return err;
24884 +}
24885 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
24886 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
24887 +++ linux/fs/aufs/procfs.c      2015-04-13 15:10:20.790157026 +0200
24888 @@ -0,0 +1,169 @@
24889 +/*
24890 + * Copyright (C) 2010-2015 Junjiro R. Okajima
24891 + *
24892 + * This program, aufs is free software; you can redistribute it and/or modify
24893 + * it under the terms of the GNU General Public License as published by
24894 + * the Free Software Foundation; either version 2 of the License, or
24895 + * (at your option) any later version.
24896 + *
24897 + * This program is distributed in the hope that it will be useful,
24898 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24899 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24900 + * GNU General Public License for more details.
24901 + *
24902 + * You should have received a copy of the GNU General Public License
24903 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24904 + */
24905 +
24906 +/*
24907 + * procfs interfaces
24908 + */
24909 +
24910 +#include <linux/proc_fs.h>
24911 +#include "aufs.h"
24912 +
24913 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
24914 +{
24915 +       struct au_sbinfo *sbinfo;
24916 +
24917 +       sbinfo = file->private_data;
24918 +       if (sbinfo) {
24919 +               au_plink_maint_leave(sbinfo);
24920 +               kobject_put(&sbinfo->si_kobj);
24921 +       }
24922 +
24923 +       return 0;
24924 +}
24925 +
24926 +static void au_procfs_plm_write_clean(struct file *file)
24927 +{
24928 +       struct au_sbinfo *sbinfo;
24929 +
24930 +       sbinfo = file->private_data;
24931 +       if (sbinfo)
24932 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
24933 +}
24934 +
24935 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
24936 +{
24937 +       int err;
24938 +       struct super_block *sb;
24939 +       struct au_sbinfo *sbinfo;
24940 +
24941 +       err = -EBUSY;
24942 +       if (unlikely(file->private_data))
24943 +               goto out;
24944 +
24945 +       sb = NULL;
24946 +       /* don't use au_sbilist_lock() here */
24947 +       spin_lock(&au_sbilist.spin);
24948 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
24949 +               if (id == sysaufs_si_id(sbinfo)) {
24950 +                       kobject_get(&sbinfo->si_kobj);
24951 +                       sb = sbinfo->si_sb;
24952 +                       break;
24953 +               }
24954 +       spin_unlock(&au_sbilist.spin);
24955 +
24956 +       err = -EINVAL;
24957 +       if (unlikely(!sb))
24958 +               goto out;
24959 +
24960 +       err = au_plink_maint_enter(sb);
24961 +       if (!err)
24962 +               /* keep kobject_get() */
24963 +               file->private_data = sbinfo;
24964 +       else
24965 +               kobject_put(&sbinfo->si_kobj);
24966 +out:
24967 +       return err;
24968 +}
24969 +
24970 +/*
24971 + * Accept a valid "si=xxxx" only.
24972 + * Once it is accepted successfully, accept "clean" too.
24973 + */
24974 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
24975 +                                  size_t count, loff_t *ppos)
24976 +{
24977 +       ssize_t err;
24978 +       unsigned long id;
24979 +       /* last newline is allowed */
24980 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
24981 +
24982 +       err = -EACCES;
24983 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
24984 +               goto out;
24985 +
24986 +       err = -EINVAL;
24987 +       if (unlikely(count > sizeof(buf)))
24988 +               goto out;
24989 +
24990 +       err = copy_from_user(buf, ubuf, count);
24991 +       if (unlikely(err)) {
24992 +               err = -EFAULT;
24993 +               goto out;
24994 +       }
24995 +       buf[count] = 0;
24996 +
24997 +       err = -EINVAL;
24998 +       if (!strcmp("clean", buf)) {
24999 +               au_procfs_plm_write_clean(file);
25000 +               goto out_success;
25001 +       } else if (unlikely(strncmp("si=", buf, 3)))
25002 +               goto out;
25003 +
25004 +       err = kstrtoul(buf + 3, 16, &id);
25005 +       if (unlikely(err))
25006 +               goto out;
25007 +
25008 +       err = au_procfs_plm_write_si(file, id);
25009 +       if (unlikely(err))
25010 +               goto out;
25011 +
25012 +out_success:
25013 +       err = count; /* success */
25014 +out:
25015 +       return err;
25016 +}
25017 +
25018 +static const struct file_operations au_procfs_plm_fop = {
25019 +       .write          = au_procfs_plm_write,
25020 +       .release        = au_procfs_plm_release,
25021 +       .owner          = THIS_MODULE
25022 +};
25023 +
25024 +/* ---------------------------------------------------------------------- */
25025 +
25026 +static struct proc_dir_entry *au_procfs_dir;
25027 +
25028 +void au_procfs_fin(void)
25029 +{
25030 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
25031 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25032 +}
25033 +
25034 +int __init au_procfs_init(void)
25035 +{
25036 +       int err;
25037 +       struct proc_dir_entry *entry;
25038 +
25039 +       err = -ENOMEM;
25040 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
25041 +       if (unlikely(!au_procfs_dir))
25042 +               goto out;
25043 +
25044 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
25045 +                           au_procfs_dir, &au_procfs_plm_fop);
25046 +       if (unlikely(!entry))
25047 +               goto out_dir;
25048 +
25049 +       err = 0;
25050 +       goto out; /* success */
25051 +
25052 +
25053 +out_dir:
25054 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25055 +out:
25056 +       return err;
25057 +}
25058 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
25059 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
25060 +++ linux/fs/aufs/rdu.c 2015-04-13 15:10:20.790157026 +0200
25061 @@ -0,0 +1,388 @@
25062 +/*
25063 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25064 + *
25065 + * This program, aufs is free software; you can redistribute it and/or modify
25066 + * it under the terms of the GNU General Public License as published by
25067 + * the Free Software Foundation; either version 2 of the License, or
25068 + * (at your option) any later version.
25069 + *
25070 + * This program is distributed in the hope that it will be useful,
25071 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25072 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25073 + * GNU General Public License for more details.
25074 + *
25075 + * You should have received a copy of the GNU General Public License
25076 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25077 + */
25078 +
25079 +/*
25080 + * readdir in userspace.
25081 + */
25082 +
25083 +#include <linux/compat.h>
25084 +#include <linux/fs_stack.h>
25085 +#include <linux/security.h>
25086 +#include "aufs.h"
25087 +
25088 +/* bits for struct aufs_rdu.flags */
25089 +#define        AuRdu_CALLED    1
25090 +#define        AuRdu_CONT      (1 << 1)
25091 +#define        AuRdu_FULL      (1 << 2)
25092 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
25093 +#define au_fset_rdu(flags, name) \
25094 +       do { (flags) |= AuRdu_##name; } while (0)
25095 +#define au_fclr_rdu(flags, name) \
25096 +       do { (flags) &= ~AuRdu_##name; } while (0)
25097 +
25098 +struct au_rdu_arg {
25099 +       struct dir_context              ctx;
25100 +       struct aufs_rdu                 *rdu;
25101 +       union au_rdu_ent_ul             ent;
25102 +       unsigned long                   end;
25103 +
25104 +       struct super_block              *sb;
25105 +       int                             err;
25106 +};
25107 +
25108 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
25109 +                      loff_t offset, u64 h_ino, unsigned int d_type)
25110 +{
25111 +       int err, len;
25112 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
25113 +       struct aufs_rdu *rdu = arg->rdu;
25114 +       struct au_rdu_ent ent;
25115 +
25116 +       err = 0;
25117 +       arg->err = 0;
25118 +       au_fset_rdu(rdu->cookie.flags, CALLED);
25119 +       len = au_rdu_len(nlen);
25120 +       if (arg->ent.ul + len  < arg->end) {
25121 +               ent.ino = h_ino;
25122 +               ent.bindex = rdu->cookie.bindex;
25123 +               ent.type = d_type;
25124 +               ent.nlen = nlen;
25125 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
25126 +                       ent.type = DT_UNKNOWN;
25127 +
25128 +               /* unnecessary to support mmap_sem since this is a dir */
25129 +               err = -EFAULT;
25130 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
25131 +                       goto out;
25132 +               if (copy_to_user(arg->ent.e->name, name, nlen))
25133 +                       goto out;
25134 +               /* the terminating NULL */
25135 +               if (__put_user(0, arg->ent.e->name + nlen))
25136 +                       goto out;
25137 +               err = 0;
25138 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
25139 +               arg->ent.ul += len;
25140 +               rdu->rent++;
25141 +       } else {
25142 +               err = -EFAULT;
25143 +               au_fset_rdu(rdu->cookie.flags, FULL);
25144 +               rdu->full = 1;
25145 +               rdu->tail = arg->ent;
25146 +       }
25147 +
25148 +out:
25149 +       /* AuTraceErr(err); */
25150 +       return err;
25151 +}
25152 +
25153 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
25154 +{
25155 +       int err;
25156 +       loff_t offset;
25157 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
25158 +
25159 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
25160 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
25161 +       err = offset;
25162 +       if (unlikely(offset != cookie->h_pos))
25163 +               goto out;
25164 +
25165 +       err = 0;
25166 +       do {
25167 +               arg->err = 0;
25168 +               au_fclr_rdu(cookie->flags, CALLED);
25169 +               /* smp_mb(); */
25170 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
25171 +               if (err >= 0)
25172 +                       err = arg->err;
25173 +       } while (!err
25174 +                && au_ftest_rdu(cookie->flags, CALLED)
25175 +                && !au_ftest_rdu(cookie->flags, FULL));
25176 +       cookie->h_pos = h_file->f_pos;
25177 +
25178 +out:
25179 +       AuTraceErr(err);
25180 +       return err;
25181 +}
25182 +
25183 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
25184 +{
25185 +       int err;
25186 +       aufs_bindex_t bend;
25187 +       struct au_rdu_arg arg = {
25188 +               .ctx = {
25189 +                       .actor = au_rdu_fill
25190 +               }
25191 +       };
25192 +       struct dentry *dentry;
25193 +       struct inode *inode;
25194 +       struct file *h_file;
25195 +       struct au_rdu_cookie *cookie = &rdu->cookie;
25196 +
25197 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
25198 +       if (unlikely(err)) {
25199 +               err = -EFAULT;
25200 +               AuTraceErr(err);
25201 +               goto out;
25202 +       }
25203 +       rdu->rent = 0;
25204 +       rdu->tail = rdu->ent;
25205 +       rdu->full = 0;
25206 +       arg.rdu = rdu;
25207 +       arg.ent = rdu->ent;
25208 +       arg.end = arg.ent.ul;
25209 +       arg.end += rdu->sz;
25210 +
25211 +       err = -ENOTDIR;
25212 +       if (unlikely(!file->f_op->iterate))
25213 +               goto out;
25214 +
25215 +       err = security_file_permission(file, MAY_READ);
25216 +       AuTraceErr(err);
25217 +       if (unlikely(err))
25218 +               goto out;
25219 +
25220 +       dentry = file->f_path.dentry;
25221 +       inode = dentry->d_inode;
25222 +#if 1
25223 +       mutex_lock(&inode->i_mutex);
25224 +#else
25225 +       err = mutex_lock_killable(&inode->i_mutex);
25226 +       AuTraceErr(err);
25227 +       if (unlikely(err))
25228 +               goto out;
25229 +#endif
25230 +
25231 +       arg.sb = inode->i_sb;
25232 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
25233 +       if (unlikely(err))
25234 +               goto out_mtx;
25235 +       err = au_alive_dir(dentry);
25236 +       if (unlikely(err))
25237 +               goto out_si;
25238 +       /* todo: reval? */
25239 +       fi_read_lock(file);
25240 +
25241 +       err = -EAGAIN;
25242 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
25243 +                    && cookie->generation != au_figen(file)))
25244 +               goto out_unlock;
25245 +
25246 +       err = 0;
25247 +       if (!rdu->blk) {
25248 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
25249 +               if (!rdu->blk)
25250 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
25251 +       }
25252 +       bend = au_fbstart(file);
25253 +       if (cookie->bindex < bend)
25254 +               cookie->bindex = bend;
25255 +       bend = au_fbend_dir(file);
25256 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
25257 +       for (; !err && cookie->bindex <= bend;
25258 +            cookie->bindex++, cookie->h_pos = 0) {
25259 +               h_file = au_hf_dir(file, cookie->bindex);
25260 +               if (!h_file)
25261 +                       continue;
25262 +
25263 +               au_fclr_rdu(cookie->flags, FULL);
25264 +               err = au_rdu_do(h_file, &arg);
25265 +               AuTraceErr(err);
25266 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
25267 +                       break;
25268 +       }
25269 +       AuDbg("rent %llu\n", rdu->rent);
25270 +
25271 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
25272 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
25273 +               au_fset_rdu(cookie->flags, CONT);
25274 +               cookie->generation = au_figen(file);
25275 +       }
25276 +
25277 +       ii_read_lock_child(inode);
25278 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
25279 +       ii_read_unlock(inode);
25280 +
25281 +out_unlock:
25282 +       fi_read_unlock(file);
25283 +out_si:
25284 +       si_read_unlock(arg.sb);
25285 +out_mtx:
25286 +       mutex_unlock(&inode->i_mutex);
25287 +out:
25288 +       AuTraceErr(err);
25289 +       return err;
25290 +}
25291 +
25292 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
25293 +{
25294 +       int err;
25295 +       ino_t ino;
25296 +       unsigned long long nent;
25297 +       union au_rdu_ent_ul *u;
25298 +       struct au_rdu_ent ent;
25299 +       struct super_block *sb;
25300 +
25301 +       err = 0;
25302 +       nent = rdu->nent;
25303 +       u = &rdu->ent;
25304 +       sb = file->f_path.dentry->d_sb;
25305 +       si_read_lock(sb, AuLock_FLUSH);
25306 +       while (nent-- > 0) {
25307 +               /* unnecessary to support mmap_sem since this is a dir */
25308 +               err = copy_from_user(&ent, u->e, sizeof(ent));
25309 +               if (!err)
25310 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
25311 +               if (unlikely(err)) {
25312 +                       err = -EFAULT;
25313 +                       AuTraceErr(err);
25314 +                       break;
25315 +               }
25316 +
25317 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
25318 +               if (!ent.wh)
25319 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
25320 +               else
25321 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
25322 +                                       &ino);
25323 +               if (unlikely(err)) {
25324 +                       AuTraceErr(err);
25325 +                       break;
25326 +               }
25327 +
25328 +               err = __put_user(ino, &u->e->ino);
25329 +               if (unlikely(err)) {
25330 +                       err = -EFAULT;
25331 +                       AuTraceErr(err);
25332 +                       break;
25333 +               }
25334 +               u->ul += au_rdu_len(ent.nlen);
25335 +       }
25336 +       si_read_unlock(sb);
25337 +
25338 +       return err;
25339 +}
25340 +
25341 +/* ---------------------------------------------------------------------- */
25342 +
25343 +static int au_rdu_verify(struct aufs_rdu *rdu)
25344 +{
25345 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
25346 +             "%llu, b%d, 0x%x, g%u}\n",
25347 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
25348 +             rdu->blk,
25349 +             rdu->rent, rdu->shwh, rdu->full,
25350 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
25351 +             rdu->cookie.generation);
25352 +
25353 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
25354 +               return 0;
25355 +
25356 +       AuDbg("%u:%u\n",
25357 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
25358 +       return -EINVAL;
25359 +}
25360 +
25361 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
25362 +{
25363 +       long err, e;
25364 +       struct aufs_rdu rdu;
25365 +       void __user *p = (void __user *)arg;
25366 +
25367 +       err = copy_from_user(&rdu, p, sizeof(rdu));
25368 +       if (unlikely(err)) {
25369 +               err = -EFAULT;
25370 +               AuTraceErr(err);
25371 +               goto out;
25372 +       }
25373 +       err = au_rdu_verify(&rdu);
25374 +       if (unlikely(err))
25375 +               goto out;
25376 +
25377 +       switch (cmd) {
25378 +       case AUFS_CTL_RDU:
25379 +               err = au_rdu(file, &rdu);
25380 +               if (unlikely(err))
25381 +                       break;
25382 +
25383 +               e = copy_to_user(p, &rdu, sizeof(rdu));
25384 +               if (unlikely(e)) {
25385 +                       err = -EFAULT;
25386 +                       AuTraceErr(err);
25387 +               }
25388 +               break;
25389 +       case AUFS_CTL_RDU_INO:
25390 +               err = au_rdu_ino(file, &rdu);
25391 +               break;
25392 +
25393 +       default:
25394 +               /* err = -ENOTTY; */
25395 +               err = -EINVAL;
25396 +       }
25397 +
25398 +out:
25399 +       AuTraceErr(err);
25400 +       return err;
25401 +}
25402 +
25403 +#ifdef CONFIG_COMPAT
25404 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
25405 +{
25406 +       long err, e;
25407 +       struct aufs_rdu rdu;
25408 +       void __user *p = compat_ptr(arg);
25409 +
25410 +       /* todo: get_user()? */
25411 +       err = copy_from_user(&rdu, p, sizeof(rdu));
25412 +       if (unlikely(err)) {
25413 +               err = -EFAULT;
25414 +               AuTraceErr(err);
25415 +               goto out;
25416 +       }
25417 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
25418 +       err = au_rdu_verify(&rdu);
25419 +       if (unlikely(err))
25420 +               goto out;
25421 +
25422 +       switch (cmd) {
25423 +       case AUFS_CTL_RDU:
25424 +               err = au_rdu(file, &rdu);
25425 +               if (unlikely(err))
25426 +                       break;
25427 +
25428 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
25429 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
25430 +               e = copy_to_user(p, &rdu, sizeof(rdu));
25431 +               if (unlikely(e)) {
25432 +                       err = -EFAULT;
25433 +                       AuTraceErr(err);
25434 +               }
25435 +               break;
25436 +       case AUFS_CTL_RDU_INO:
25437 +               err = au_rdu_ino(file, &rdu);
25438 +               break;
25439 +
25440 +       default:
25441 +               /* err = -ENOTTY; */
25442 +               err = -EINVAL;
25443 +       }
25444 +
25445 +out:
25446 +       AuTraceErr(err);
25447 +       return err;
25448 +}
25449 +#endif
25450 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
25451 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
25452 +++ linux/fs/aufs/rwsem.h       2015-04-13 15:10:20.790157026 +0200
25453 @@ -0,0 +1,191 @@
25454 +/*
25455 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25456 + *
25457 + * This program, aufs is free software; you can redistribute it and/or modify
25458 + * it under the terms of the GNU General Public License as published by
25459 + * the Free Software Foundation; either version 2 of the License, or
25460 + * (at your option) any later version.
25461 + *
25462 + * This program is distributed in the hope that it will be useful,
25463 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25464 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25465 + * GNU General Public License for more details.
25466 + *
25467 + * You should have received a copy of the GNU General Public License
25468 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25469 + */
25470 +
25471 +/*
25472 + * simple read-write semaphore wrappers
25473 + */
25474 +
25475 +#ifndef __AUFS_RWSEM_H__
25476 +#define __AUFS_RWSEM_H__
25477 +
25478 +#ifdef __KERNEL__
25479 +
25480 +#include "debug.h"
25481 +
25482 +struct au_rwsem {
25483 +       struct rw_semaphore     rwsem;
25484 +#ifdef CONFIG_AUFS_DEBUG
25485 +       /* just for debugging, not almighty counter */
25486 +       atomic_t                rcnt, wcnt;
25487 +#endif
25488 +};
25489 +
25490 +#ifdef CONFIG_AUFS_DEBUG
25491 +#define AuDbgCntInit(rw) do { \
25492 +       atomic_set(&(rw)->rcnt, 0); \
25493 +       atomic_set(&(rw)->wcnt, 0); \
25494 +       smp_mb(); /* atomic set */ \
25495 +} while (0)
25496 +
25497 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
25498 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
25499 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
25500 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
25501 +#else
25502 +#define AuDbgCntInit(rw)       do {} while (0)
25503 +#define AuDbgRcntInc(rw)       do {} while (0)
25504 +#define AuDbgRcntDec(rw)       do {} while (0)
25505 +#define AuDbgWcntInc(rw)       do {} while (0)
25506 +#define AuDbgWcntDec(rw)       do {} while (0)
25507 +#endif /* CONFIG_AUFS_DEBUG */
25508 +
25509 +/* to debug easier, do not make them inlined functions */
25510 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
25511 +/* rwsem_is_locked() is unusable */
25512 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
25513 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
25514 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
25515 +                                       && atomic_read(&(rw)->wcnt) <= 0)
25516 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
25517 +                                       || atomic_read(&(rw)->wcnt))
25518 +
25519 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
25520 +
25521 +static inline void au_rw_init(struct au_rwsem *rw)
25522 +{
25523 +       AuDbgCntInit(rw);
25524 +       init_rwsem(&rw->rwsem);
25525 +}
25526 +
25527 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
25528 +{
25529 +       au_rw_init(rw);
25530 +       down_write(&rw->rwsem);
25531 +       AuDbgWcntInc(rw);
25532 +}
25533 +
25534 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
25535 +                                          unsigned int lsc)
25536 +{
25537 +       au_rw_init(rw);
25538 +       down_write_nested(&rw->rwsem, lsc);
25539 +       AuDbgWcntInc(rw);
25540 +}
25541 +
25542 +static inline void au_rw_read_lock(struct au_rwsem *rw)
25543 +{
25544 +       down_read(&rw->rwsem);
25545 +       AuDbgRcntInc(rw);
25546 +}
25547 +
25548 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
25549 +{
25550 +       down_read_nested(&rw->rwsem, lsc);
25551 +       AuDbgRcntInc(rw);
25552 +}
25553 +
25554 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
25555 +{
25556 +       AuRwMustReadLock(rw);
25557 +       AuDbgRcntDec(rw);
25558 +       up_read(&rw->rwsem);
25559 +}
25560 +
25561 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
25562 +{
25563 +       AuRwMustWriteLock(rw);
25564 +       AuDbgRcntInc(rw);
25565 +       AuDbgWcntDec(rw);
25566 +       downgrade_write(&rw->rwsem);
25567 +}
25568 +
25569 +static inline void au_rw_write_lock(struct au_rwsem *rw)
25570 +{
25571 +       down_write(&rw->rwsem);
25572 +       AuDbgWcntInc(rw);
25573 +}
25574 +
25575 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
25576 +                                          unsigned int lsc)
25577 +{
25578 +       down_write_nested(&rw->rwsem, lsc);
25579 +       AuDbgWcntInc(rw);
25580 +}
25581 +
25582 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
25583 +{
25584 +       AuRwMustWriteLock(rw);
25585 +       AuDbgWcntDec(rw);
25586 +       up_write(&rw->rwsem);
25587 +}
25588 +
25589 +/* why is not _nested version defined */
25590 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
25591 +{
25592 +       int ret;
25593 +
25594 +       ret = down_read_trylock(&rw->rwsem);
25595 +       if (ret)
25596 +               AuDbgRcntInc(rw);
25597 +       return ret;
25598 +}
25599 +
25600 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
25601 +{
25602 +       int ret;
25603 +
25604 +       ret = down_write_trylock(&rw->rwsem);
25605 +       if (ret)
25606 +               AuDbgWcntInc(rw);
25607 +       return ret;
25608 +}
25609 +
25610 +#undef AuDbgCntInit
25611 +#undef AuDbgRcntInc
25612 +#undef AuDbgRcntDec
25613 +#undef AuDbgWcntInc
25614 +#undef AuDbgWcntDec
25615 +
25616 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
25617 +static inline void prefix##_read_lock(param) \
25618 +{ au_rw_read_lock(rwsem); } \
25619 +static inline void prefix##_write_lock(param) \
25620 +{ au_rw_write_lock(rwsem); } \
25621 +static inline int prefix##_read_trylock(param) \
25622 +{ return au_rw_read_trylock(rwsem); } \
25623 +static inline int prefix##_write_trylock(param) \
25624 +{ return au_rw_write_trylock(rwsem); }
25625 +/* why is not _nested version defined */
25626 +/* static inline void prefix##_read_trylock_nested(param, lsc)
25627 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
25628 +static inline void prefix##_write_trylock_nestd(param, lsc)
25629 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
25630 +
25631 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
25632 +static inline void prefix##_read_unlock(param) \
25633 +{ au_rw_read_unlock(rwsem); } \
25634 +static inline void prefix##_write_unlock(param) \
25635 +{ au_rw_write_unlock(rwsem); } \
25636 +static inline void prefix##_downgrade_lock(param) \
25637 +{ au_rw_dgrade_lock(rwsem); }
25638 +
25639 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
25640 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
25641 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
25642 +
25643 +#endif /* __KERNEL__ */
25644 +#endif /* __AUFS_RWSEM_H__ */
25645 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
25646 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
25647 +++ linux/fs/aufs/sbinfo.c      2015-04-13 15:10:20.790157026 +0200
25648 @@ -0,0 +1,352 @@
25649 +/*
25650 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25651 + *
25652 + * This program, aufs is free software; you can redistribute it and/or modify
25653 + * it under the terms of the GNU General Public License as published by
25654 + * the Free Software Foundation; either version 2 of the License, or
25655 + * (at your option) any later version.
25656 + *
25657 + * This program is distributed in the hope that it will be useful,
25658 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25659 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25660 + * GNU General Public License for more details.
25661 + *
25662 + * You should have received a copy of the GNU General Public License
25663 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25664 + */
25665 +
25666 +/*
25667 + * superblock private data
25668 + */
25669 +
25670 +#include "aufs.h"
25671 +
25672 +/*
25673 + * they are necessary regardless sysfs is disabled.
25674 + */
25675 +void au_si_free(struct kobject *kobj)
25676 +{
25677 +       int i;
25678 +       struct au_sbinfo *sbinfo;
25679 +       char *locked __maybe_unused; /* debug only */
25680 +
25681 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
25682 +       for (i = 0; i < AuPlink_NHASH; i++)
25683 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
25684 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
25685 +
25686 +       au_rw_write_lock(&sbinfo->si_rwsem);
25687 +       au_br_free(sbinfo);
25688 +       au_rw_write_unlock(&sbinfo->si_rwsem);
25689 +
25690 +       AuDebugOn(radix_tree_gang_lookup
25691 +                 (&sbinfo->au_si_pid.tree, (void **)&locked,
25692 +                  /*first_index*/PID_MAX_DEFAULT - 1,
25693 +                  /*max_items*/sizeof(locked)/sizeof(*locked)));
25694 +
25695 +       kfree(sbinfo->si_branch);
25696 +       kfree(sbinfo->au_si_pid.bitmap);
25697 +       mutex_destroy(&sbinfo->si_xib_mtx);
25698 +       AuRwDestroy(&sbinfo->si_rwsem);
25699 +
25700 +       kfree(sbinfo);
25701 +}
25702 +
25703 +int au_si_alloc(struct super_block *sb)
25704 +{
25705 +       int err, i;
25706 +       struct au_sbinfo *sbinfo;
25707 +       static struct lock_class_key aufs_si;
25708 +
25709 +       err = -ENOMEM;
25710 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
25711 +       if (unlikely(!sbinfo))
25712 +               goto out;
25713 +
25714 +       BUILD_BUG_ON(sizeof(unsigned long) !=
25715 +                    sizeof(*sbinfo->au_si_pid.bitmap));
25716 +       sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
25717 +                                       sizeof(*sbinfo->au_si_pid.bitmap),
25718 +                                       GFP_NOFS);
25719 +       if (unlikely(!sbinfo->au_si_pid.bitmap))
25720 +               goto out_sbinfo;
25721 +
25722 +       /* will be reallocated separately */
25723 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
25724 +       if (unlikely(!sbinfo->si_branch))
25725 +               goto out_pidmap;
25726 +
25727 +       err = sysaufs_si_init(sbinfo);
25728 +       if (unlikely(err))
25729 +               goto out_br;
25730 +
25731 +       au_nwt_init(&sbinfo->si_nowait);
25732 +       au_rw_init_wlock(&sbinfo->si_rwsem);
25733 +       au_rw_class(&sbinfo->si_rwsem, &aufs_si);
25734 +       spin_lock_init(&sbinfo->au_si_pid.tree_lock);
25735 +       INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
25736 +
25737 +       atomic_long_set(&sbinfo->si_ninodes, 0);
25738 +       atomic_long_set(&sbinfo->si_nfiles, 0);
25739 +
25740 +       sbinfo->si_bend = -1;
25741 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
25742 +
25743 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
25744 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
25745 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
25746 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
25747 +
25748 +       au_fhsm_init(sbinfo);
25749 +
25750 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
25751 +
25752 +       sbinfo->si_xino_jiffy = jiffies;
25753 +       sbinfo->si_xino_expire
25754 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
25755 +       mutex_init(&sbinfo->si_xib_mtx);
25756 +       sbinfo->si_xino_brid = -1;
25757 +       /* leave si_xib_last_pindex and si_xib_next_bit */
25758 +
25759 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
25760 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
25761 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
25762 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
25763 +
25764 +       for (i = 0; i < AuPlink_NHASH; i++)
25765 +               au_sphl_init(sbinfo->si_plink + i);
25766 +       init_waitqueue_head(&sbinfo->si_plink_wq);
25767 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
25768 +
25769 +       au_sphl_init(&sbinfo->si_files);
25770 +
25771 +       /* leave other members for sysaufs and si_mnt. */
25772 +       sbinfo->si_sb = sb;
25773 +       sb->s_fs_info = sbinfo;
25774 +       si_pid_set(sb);
25775 +       return 0; /* success */
25776 +
25777 +out_br:
25778 +       kfree(sbinfo->si_branch);
25779 +out_pidmap:
25780 +       kfree(sbinfo->au_si_pid.bitmap);
25781 +out_sbinfo:
25782 +       kfree(sbinfo);
25783 +out:
25784 +       return err;
25785 +}
25786 +
25787 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
25788 +{
25789 +       int err, sz;
25790 +       struct au_branch **brp;
25791 +
25792 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
25793 +
25794 +       err = -ENOMEM;
25795 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
25796 +       if (unlikely(!sz))
25797 +               sz = sizeof(*brp);
25798 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
25799 +       if (brp) {
25800 +               sbinfo->si_branch = brp;
25801 +               err = 0;
25802 +       }
25803 +
25804 +       return err;
25805 +}
25806 +
25807 +/* ---------------------------------------------------------------------- */
25808 +
25809 +unsigned int au_sigen_inc(struct super_block *sb)
25810 +{
25811 +       unsigned int gen;
25812 +
25813 +       SiMustWriteLock(sb);
25814 +
25815 +       gen = ++au_sbi(sb)->si_generation;
25816 +       au_update_digen(sb->s_root);
25817 +       au_update_iigen(sb->s_root->d_inode, /*half*/0);
25818 +       sb->s_root->d_inode->i_version++;
25819 +       return gen;
25820 +}
25821 +
25822 +aufs_bindex_t au_new_br_id(struct super_block *sb)
25823 +{
25824 +       aufs_bindex_t br_id;
25825 +       int i;
25826 +       struct au_sbinfo *sbinfo;
25827 +
25828 +       SiMustWriteLock(sb);
25829 +
25830 +       sbinfo = au_sbi(sb);
25831 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
25832 +               br_id = ++sbinfo->si_last_br_id;
25833 +               AuDebugOn(br_id < 0);
25834 +               if (br_id && au_br_index(sb, br_id) < 0)
25835 +                       return br_id;
25836 +       }
25837 +
25838 +       return -1;
25839 +}
25840 +
25841 +/* ---------------------------------------------------------------------- */
25842 +
25843 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
25844 +int si_read_lock(struct super_block *sb, int flags)
25845 +{
25846 +       int err;
25847 +
25848 +       err = 0;
25849 +       if (au_ftest_lock(flags, FLUSH))
25850 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
25851 +
25852 +       si_noflush_read_lock(sb);
25853 +       err = au_plink_maint(sb, flags);
25854 +       if (unlikely(err))
25855 +               si_read_unlock(sb);
25856 +
25857 +       return err;
25858 +}
25859 +
25860 +int si_write_lock(struct super_block *sb, int flags)
25861 +{
25862 +       int err;
25863 +
25864 +       if (au_ftest_lock(flags, FLUSH))
25865 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
25866 +
25867 +       si_noflush_write_lock(sb);
25868 +       err = au_plink_maint(sb, flags);
25869 +       if (unlikely(err))
25870 +               si_write_unlock(sb);
25871 +
25872 +       return err;
25873 +}
25874 +
25875 +/* dentry and super_block lock. call at entry point */
25876 +int aufs_read_lock(struct dentry *dentry, int flags)
25877 +{
25878 +       int err;
25879 +       struct super_block *sb;
25880 +
25881 +       sb = dentry->d_sb;
25882 +       err = si_read_lock(sb, flags);
25883 +       if (unlikely(err))
25884 +               goto out;
25885 +
25886 +       if (au_ftest_lock(flags, DW))
25887 +               di_write_lock_child(dentry);
25888 +       else
25889 +               di_read_lock_child(dentry, flags);
25890 +
25891 +       if (au_ftest_lock(flags, GEN)) {
25892 +               err = au_digen_test(dentry, au_sigen(sb));
25893 +               AuDebugOn(!err && au_dbrange_test(dentry));
25894 +               if (unlikely(err))
25895 +                       aufs_read_unlock(dentry, flags);
25896 +       }
25897 +
25898 +out:
25899 +       return err;
25900 +}
25901 +
25902 +void aufs_read_unlock(struct dentry *dentry, int flags)
25903 +{
25904 +       if (au_ftest_lock(flags, DW))
25905 +               di_write_unlock(dentry);
25906 +       else
25907 +               di_read_unlock(dentry, flags);
25908 +       si_read_unlock(dentry->d_sb);
25909 +}
25910 +
25911 +void aufs_write_lock(struct dentry *dentry)
25912 +{
25913 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
25914 +       di_write_lock_child(dentry);
25915 +}
25916 +
25917 +void aufs_write_unlock(struct dentry *dentry)
25918 +{
25919 +       di_write_unlock(dentry);
25920 +       si_write_unlock(dentry->d_sb);
25921 +}
25922 +
25923 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
25924 +{
25925 +       int err;
25926 +       unsigned int sigen;
25927 +       struct super_block *sb;
25928 +
25929 +       sb = d1->d_sb;
25930 +       err = si_read_lock(sb, flags);
25931 +       if (unlikely(err))
25932 +               goto out;
25933 +
25934 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
25935 +
25936 +       if (au_ftest_lock(flags, GEN)) {
25937 +               sigen = au_sigen(sb);
25938 +               err = au_digen_test(d1, sigen);
25939 +               AuDebugOn(!err && au_dbrange_test(d1));
25940 +               if (!err) {
25941 +                       err = au_digen_test(d2, sigen);
25942 +                       AuDebugOn(!err && au_dbrange_test(d2));
25943 +               }
25944 +               if (unlikely(err))
25945 +                       aufs_read_and_write_unlock2(d1, d2);
25946 +       }
25947 +
25948 +out:
25949 +       return err;
25950 +}
25951 +
25952 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
25953 +{
25954 +       di_write_unlock2(d1, d2);
25955 +       si_read_unlock(d1->d_sb);
25956 +}
25957 +
25958 +/* ---------------------------------------------------------------------- */
25959 +
25960 +int si_pid_test_slow(struct super_block *sb)
25961 +{
25962 +       void *p;
25963 +
25964 +       rcu_read_lock();
25965 +       p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
25966 +       rcu_read_unlock();
25967 +
25968 +       return (long)!!p;
25969 +}
25970 +
25971 +void si_pid_set_slow(struct super_block *sb)
25972 +{
25973 +       int err;
25974 +       struct au_sbinfo *sbinfo;
25975 +
25976 +       AuDebugOn(si_pid_test_slow(sb));
25977 +
25978 +       sbinfo = au_sbi(sb);
25979 +       err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
25980 +       AuDebugOn(err);
25981 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
25982 +       err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
25983 +                               /*any valid ptr*/sb);
25984 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
25985 +       AuDebugOn(err);
25986 +       radix_tree_preload_end();
25987 +}
25988 +
25989 +void si_pid_clr_slow(struct super_block *sb)
25990 +{
25991 +       void *p;
25992 +       struct au_sbinfo *sbinfo;
25993 +
25994 +       AuDebugOn(!si_pid_test_slow(sb));
25995 +
25996 +       sbinfo = au_sbi(sb);
25997 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
25998 +       p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
25999 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
26000 +}
26001 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
26002 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
26003 +++ linux/fs/aufs/spl.h 2015-04-13 15:10:20.790157026 +0200
26004 @@ -0,0 +1,111 @@
26005 +/*
26006 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26007 + *
26008 + * This program, aufs is free software; you can redistribute it and/or modify
26009 + * it under the terms of the GNU General Public License as published by
26010 + * the Free Software Foundation; either version 2 of the License, or
26011 + * (at your option) any later version.
26012 + *
26013 + * This program is distributed in the hope that it will be useful,
26014 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26015 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26016 + * GNU General Public License for more details.
26017 + *
26018 + * You should have received a copy of the GNU General Public License
26019 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26020 + */
26021 +
26022 +/*
26023 + * simple list protected by a spinlock
26024 + */
26025 +
26026 +#ifndef __AUFS_SPL_H__
26027 +#define __AUFS_SPL_H__
26028 +
26029 +#ifdef __KERNEL__
26030 +
26031 +struct au_splhead {
26032 +       spinlock_t              spin;
26033 +       struct list_head        head;
26034 +};
26035 +
26036 +static inline void au_spl_init(struct au_splhead *spl)
26037 +{
26038 +       spin_lock_init(&spl->spin);
26039 +       INIT_LIST_HEAD(&spl->head);
26040 +}
26041 +
26042 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
26043 +{
26044 +       spin_lock(&spl->spin);
26045 +       list_add(list, &spl->head);
26046 +       spin_unlock(&spl->spin);
26047 +}
26048 +
26049 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
26050 +{
26051 +       spin_lock(&spl->spin);
26052 +       list_del(list);
26053 +       spin_unlock(&spl->spin);
26054 +}
26055 +
26056 +static inline void au_spl_del_rcu(struct list_head *list,
26057 +                                 struct au_splhead *spl)
26058 +{
26059 +       spin_lock(&spl->spin);
26060 +       list_del_rcu(list);
26061 +       spin_unlock(&spl->spin);
26062 +}
26063 +
26064 +/* ---------------------------------------------------------------------- */
26065 +
26066 +struct au_sphlhead {
26067 +       spinlock_t              spin;
26068 +       struct hlist_head       head;
26069 +};
26070 +
26071 +static inline void au_sphl_init(struct au_sphlhead *sphl)
26072 +{
26073 +       spin_lock_init(&sphl->spin);
26074 +       INIT_HLIST_HEAD(&sphl->head);
26075 +}
26076 +
26077 +static inline void au_sphl_add(struct hlist_node *hlist,
26078 +                              struct au_sphlhead *sphl)
26079 +{
26080 +       spin_lock(&sphl->spin);
26081 +       hlist_add_head(hlist, &sphl->head);
26082 +       spin_unlock(&sphl->spin);
26083 +}
26084 +
26085 +static inline void au_sphl_del(struct hlist_node *hlist,
26086 +                              struct au_sphlhead *sphl)
26087 +{
26088 +       spin_lock(&sphl->spin);
26089 +       hlist_del(hlist);
26090 +       spin_unlock(&sphl->spin);
26091 +}
26092 +
26093 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
26094 +                                  struct au_sphlhead *sphl)
26095 +{
26096 +       spin_lock(&sphl->spin);
26097 +       hlist_del_rcu(hlist);
26098 +       spin_unlock(&sphl->spin);
26099 +}
26100 +
26101 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
26102 +{
26103 +       unsigned long cnt;
26104 +       struct hlist_node *pos;
26105 +
26106 +       cnt = 0;
26107 +       spin_lock(&sphl->spin);
26108 +       hlist_for_each(pos, &sphl->head)
26109 +               cnt++;
26110 +       spin_unlock(&sphl->spin);
26111 +       return cnt;
26112 +}
26113 +
26114 +#endif /* __KERNEL__ */
26115 +#endif /* __AUFS_SPL_H__ */
26116 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
26117 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
26118 +++ linux/fs/aufs/super.c       2015-04-13 15:10:20.790157026 +0200
26119 @@ -0,0 +1,1007 @@
26120 +/*
26121 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26122 + *
26123 + * This program, aufs is free software; you can redistribute it and/or modify
26124 + * it under the terms of the GNU General Public License as published by
26125 + * the Free Software Foundation; either version 2 of the License, or
26126 + * (at your option) any later version.
26127 + *
26128 + * This program is distributed in the hope that it will be useful,
26129 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26130 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26131 + * GNU General Public License for more details.
26132 + *
26133 + * You should have received a copy of the GNU General Public License
26134 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26135 + */
26136 +
26137 +/*
26138 + * mount and super_block operations
26139 + */
26140 +
26141 +#include <linux/mm.h>
26142 +#include <linux/seq_file.h>
26143 +#include <linux/statfs.h>
26144 +#include <linux/vmalloc.h>
26145 +#include "aufs.h"
26146 +
26147 +/*
26148 + * super_operations
26149 + */
26150 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
26151 +{
26152 +       struct au_icntnr *c;
26153 +
26154 +       c = au_cache_alloc_icntnr();
26155 +       if (c) {
26156 +               au_icntnr_init(c);
26157 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
26158 +               c->iinfo.ii_hinode = NULL;
26159 +               return &c->vfs_inode;
26160 +       }
26161 +       return NULL;
26162 +}
26163 +
26164 +static void aufs_destroy_inode_cb(struct rcu_head *head)
26165 +{
26166 +       struct inode *inode = container_of(head, struct inode, i_rcu);
26167 +
26168 +       INIT_HLIST_HEAD(&inode->i_dentry);
26169 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
26170 +}
26171 +
26172 +static void aufs_destroy_inode(struct inode *inode)
26173 +{
26174 +       au_iinfo_fin(inode);
26175 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
26176 +}
26177 +
26178 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
26179 +{
26180 +       struct inode *inode;
26181 +       int err;
26182 +
26183 +       inode = iget_locked(sb, ino);
26184 +       if (unlikely(!inode)) {
26185 +               inode = ERR_PTR(-ENOMEM);
26186 +               goto out;
26187 +       }
26188 +       if (!(inode->i_state & I_NEW))
26189 +               goto out;
26190 +
26191 +       err = au_xigen_new(inode);
26192 +       if (!err)
26193 +               err = au_iinfo_init(inode);
26194 +       if (!err)
26195 +               inode->i_version++;
26196 +       else {
26197 +               iget_failed(inode);
26198 +               inode = ERR_PTR(err);
26199 +       }
26200 +
26201 +out:
26202 +       /* never return NULL */
26203 +       AuDebugOn(!inode);
26204 +       AuTraceErrPtr(inode);
26205 +       return inode;
26206 +}
26207 +
26208 +/* lock free root dinfo */
26209 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
26210 +{
26211 +       int err;
26212 +       aufs_bindex_t bindex, bend;
26213 +       struct path path;
26214 +       struct au_hdentry *hdp;
26215 +       struct au_branch *br;
26216 +       au_br_perm_str_t perm;
26217 +
26218 +       err = 0;
26219 +       bend = au_sbend(sb);
26220 +       hdp = au_di(sb->s_root)->di_hdentry;
26221 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
26222 +               br = au_sbr(sb, bindex);
26223 +               path.mnt = au_br_mnt(br);
26224 +               path.dentry = hdp[bindex].hd_dentry;
26225 +               err = au_seq_path(seq, &path);
26226 +               if (err > 0) {
26227 +                       au_optstr_br_perm(&perm, br->br_perm);
26228 +                       err = seq_printf(seq, "=%s", perm.a);
26229 +                       if (err == -1)
26230 +                               err = -E2BIG;
26231 +               }
26232 +               if (!err && bindex != bend)
26233 +                       err = seq_putc(seq, ':');
26234 +       }
26235 +
26236 +       return err;
26237 +}
26238 +
26239 +static void au_show_wbr_create(struct seq_file *m, int v,
26240 +                              struct au_sbinfo *sbinfo)
26241 +{
26242 +       const char *pat;
26243 +
26244 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26245 +
26246 +       seq_puts(m, ",create=");
26247 +       pat = au_optstr_wbr_create(v);
26248 +       switch (v) {
26249 +       case AuWbrCreate_TDP:
26250 +       case AuWbrCreate_RR:
26251 +       case AuWbrCreate_MFS:
26252 +       case AuWbrCreate_PMFS:
26253 +               seq_puts(m, pat);
26254 +               break;
26255 +       case AuWbrCreate_MFSV:
26256 +               seq_printf(m, /*pat*/"mfs:%lu",
26257 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26258 +                          / MSEC_PER_SEC);
26259 +               break;
26260 +       case AuWbrCreate_PMFSV:
26261 +               seq_printf(m, /*pat*/"pmfs:%lu",
26262 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26263 +                          / MSEC_PER_SEC);
26264 +               break;
26265 +       case AuWbrCreate_MFSRR:
26266 +               seq_printf(m, /*pat*/"mfsrr:%llu",
26267 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26268 +               break;
26269 +       case AuWbrCreate_MFSRRV:
26270 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
26271 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26272 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26273 +                          / MSEC_PER_SEC);
26274 +               break;
26275 +       case AuWbrCreate_PMFSRR:
26276 +               seq_printf(m, /*pat*/"pmfsrr:%llu",
26277 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26278 +               break;
26279 +       case AuWbrCreate_PMFSRRV:
26280 +               seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
26281 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26282 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26283 +                          / MSEC_PER_SEC);
26284 +               break;
26285 +       }
26286 +}
26287 +
26288 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
26289 +{
26290 +#ifdef CONFIG_SYSFS
26291 +       return 0;
26292 +#else
26293 +       int err;
26294 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
26295 +       aufs_bindex_t bindex, brid;
26296 +       struct qstr *name;
26297 +       struct file *f;
26298 +       struct dentry *d, *h_root;
26299 +       struct au_hdentry *hdp;
26300 +
26301 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26302 +
26303 +       err = 0;
26304 +       f = au_sbi(sb)->si_xib;
26305 +       if (!f)
26306 +               goto out;
26307 +
26308 +       /* stop printing the default xino path on the first writable branch */
26309 +       h_root = NULL;
26310 +       brid = au_xino_brid(sb);
26311 +       if (brid >= 0) {
26312 +               bindex = au_br_index(sb, brid);
26313 +               hdp = au_di(sb->s_root)->di_hdentry;
26314 +               h_root = hdp[0 + bindex].hd_dentry;
26315 +       }
26316 +       d = f->f_path.dentry;
26317 +       name = &d->d_name;
26318 +       /* safe ->d_parent because the file is unlinked */
26319 +       if (d->d_parent == h_root
26320 +           && name->len == len
26321 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
26322 +               goto out;
26323 +
26324 +       seq_puts(seq, ",xino=");
26325 +       err = au_xino_path(seq, f);
26326 +
26327 +out:
26328 +       return err;
26329 +#endif
26330 +}
26331 +
26332 +/* seq_file will re-call me in case of too long string */
26333 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
26334 +{
26335 +       int err;
26336 +       unsigned int mnt_flags, v;
26337 +       struct super_block *sb;
26338 +       struct au_sbinfo *sbinfo;
26339 +
26340 +#define AuBool(name, str) do { \
26341 +       v = au_opt_test(mnt_flags, name); \
26342 +       if (v != au_opt_test(AuOpt_Def, name)) \
26343 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
26344 +} while (0)
26345 +
26346 +#define AuStr(name, str) do { \
26347 +       v = mnt_flags & AuOptMask_##name; \
26348 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
26349 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
26350 +} while (0)
26351 +
26352 +#define AuUInt(name, str, val) do { \
26353 +       if (val != AUFS_##name##_DEF) \
26354 +               seq_printf(m, "," #str "=%u", val); \
26355 +} while (0)
26356 +
26357 +       sb = dentry->d_sb;
26358 +       if (sb->s_flags & MS_POSIXACL)
26359 +               seq_puts(m, ",acl");
26360 +
26361 +       /* lock free root dinfo */
26362 +       si_noflush_read_lock(sb);
26363 +       sbinfo = au_sbi(sb);
26364 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
26365 +
26366 +       mnt_flags = au_mntflags(sb);
26367 +       if (au_opt_test(mnt_flags, XINO)) {
26368 +               err = au_show_xino(m, sb);
26369 +               if (unlikely(err))
26370 +                       goto out;
26371 +       } else
26372 +               seq_puts(m, ",noxino");
26373 +
26374 +       AuBool(TRUNC_XINO, trunc_xino);
26375 +       AuStr(UDBA, udba);
26376 +       AuBool(SHWH, shwh);
26377 +       AuBool(PLINK, plink);
26378 +       AuBool(DIO, dio);
26379 +       AuBool(DIRPERM1, dirperm1);
26380 +       /* AuBool(REFROF, refrof); */
26381 +
26382 +       v = sbinfo->si_wbr_create;
26383 +       if (v != AuWbrCreate_Def)
26384 +               au_show_wbr_create(m, v, sbinfo);
26385 +
26386 +       v = sbinfo->si_wbr_copyup;
26387 +       if (v != AuWbrCopyup_Def)
26388 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
26389 +
26390 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
26391 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
26392 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
26393 +
26394 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
26395 +
26396 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
26397 +       AuUInt(RDCACHE, rdcache, v);
26398 +
26399 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
26400 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
26401 +
26402 +       au_fhsm_show(m, sbinfo);
26403 +
26404 +       AuBool(SUM, sum);
26405 +       /* AuBool(SUM_W, wsum); */
26406 +       AuBool(WARN_PERM, warn_perm);
26407 +       AuBool(VERBOSE, verbose);
26408 +
26409 +out:
26410 +       /* be sure to print "br:" last */
26411 +       if (!sysaufs_brs) {
26412 +               seq_puts(m, ",br:");
26413 +               au_show_brs(m, sb);
26414 +       }
26415 +       si_read_unlock(sb);
26416 +       return 0;
26417 +
26418 +#undef AuBool
26419 +#undef AuStr
26420 +#undef AuUInt
26421 +}
26422 +
26423 +/* ---------------------------------------------------------------------- */
26424 +
26425 +/* sum mode which returns the summation for statfs(2) */
26426 +
26427 +static u64 au_add_till_max(u64 a, u64 b)
26428 +{
26429 +       u64 old;
26430 +
26431 +       old = a;
26432 +       a += b;
26433 +       if (old <= a)
26434 +               return a;
26435 +       return ULLONG_MAX;
26436 +}
26437 +
26438 +static u64 au_mul_till_max(u64 a, long mul)
26439 +{
26440 +       u64 old;
26441 +
26442 +       old = a;
26443 +       a *= mul;
26444 +       if (old <= a)
26445 +               return a;
26446 +       return ULLONG_MAX;
26447 +}
26448 +
26449 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
26450 +{
26451 +       int err;
26452 +       long bsize, factor;
26453 +       u64 blocks, bfree, bavail, files, ffree;
26454 +       aufs_bindex_t bend, bindex, i;
26455 +       unsigned char shared;
26456 +       struct path h_path;
26457 +       struct super_block *h_sb;
26458 +
26459 +       err = 0;
26460 +       bsize = LONG_MAX;
26461 +       files = 0;
26462 +       ffree = 0;
26463 +       blocks = 0;
26464 +       bfree = 0;
26465 +       bavail = 0;
26466 +       bend = au_sbend(sb);
26467 +       for (bindex = 0; bindex <= bend; bindex++) {
26468 +               h_path.mnt = au_sbr_mnt(sb, bindex);
26469 +               h_sb = h_path.mnt->mnt_sb;
26470 +               shared = 0;
26471 +               for (i = 0; !shared && i < bindex; i++)
26472 +                       shared = (au_sbr_sb(sb, i) == h_sb);
26473 +               if (shared)
26474 +                       continue;
26475 +
26476 +               /* sb->s_root for NFS is unreliable */
26477 +               h_path.dentry = h_path.mnt->mnt_root;
26478 +               err = vfs_statfs(&h_path, buf);
26479 +               if (unlikely(err))
26480 +                       goto out;
26481 +
26482 +               if (bsize > buf->f_bsize) {
26483 +                       /*
26484 +                        * we will reduce bsize, so we have to expand blocks
26485 +                        * etc. to match them again
26486 +                        */
26487 +                       factor = (bsize / buf->f_bsize);
26488 +                       blocks = au_mul_till_max(blocks, factor);
26489 +                       bfree = au_mul_till_max(bfree, factor);
26490 +                       bavail = au_mul_till_max(bavail, factor);
26491 +                       bsize = buf->f_bsize;
26492 +               }
26493 +
26494 +               factor = (buf->f_bsize / bsize);
26495 +               blocks = au_add_till_max(blocks,
26496 +                               au_mul_till_max(buf->f_blocks, factor));
26497 +               bfree = au_add_till_max(bfree,
26498 +                               au_mul_till_max(buf->f_bfree, factor));
26499 +               bavail = au_add_till_max(bavail,
26500 +                               au_mul_till_max(buf->f_bavail, factor));
26501 +               files = au_add_till_max(files, buf->f_files);
26502 +               ffree = au_add_till_max(ffree, buf->f_ffree);
26503 +       }
26504 +
26505 +       buf->f_bsize = bsize;
26506 +       buf->f_blocks = blocks;
26507 +       buf->f_bfree = bfree;
26508 +       buf->f_bavail = bavail;
26509 +       buf->f_files = files;
26510 +       buf->f_ffree = ffree;
26511 +       buf->f_frsize = 0;
26512 +
26513 +out:
26514 +       return err;
26515 +}
26516 +
26517 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
26518 +{
26519 +       int err;
26520 +       struct path h_path;
26521 +       struct super_block *sb;
26522 +
26523 +       /* lock free root dinfo */
26524 +       sb = dentry->d_sb;
26525 +       si_noflush_read_lock(sb);
26526 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
26527 +               /* sb->s_root for NFS is unreliable */
26528 +               h_path.mnt = au_sbr_mnt(sb, 0);
26529 +               h_path.dentry = h_path.mnt->mnt_root;
26530 +               err = vfs_statfs(&h_path, buf);
26531 +       } else
26532 +               err = au_statfs_sum(sb, buf);
26533 +       si_read_unlock(sb);
26534 +
26535 +       if (!err) {
26536 +               buf->f_type = AUFS_SUPER_MAGIC;
26537 +               buf->f_namelen = AUFS_MAX_NAMELEN;
26538 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
26539 +       }
26540 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
26541 +
26542 +       return err;
26543 +}
26544 +
26545 +/* ---------------------------------------------------------------------- */
26546 +
26547 +static int aufs_sync_fs(struct super_block *sb, int wait)
26548 +{
26549 +       int err, e;
26550 +       aufs_bindex_t bend, bindex;
26551 +       struct au_branch *br;
26552 +       struct super_block *h_sb;
26553 +
26554 +       err = 0;
26555 +       si_noflush_read_lock(sb);
26556 +       bend = au_sbend(sb);
26557 +       for (bindex = 0; bindex <= bend; bindex++) {
26558 +               br = au_sbr(sb, bindex);
26559 +               if (!au_br_writable(br->br_perm))
26560 +                       continue;
26561 +
26562 +               h_sb = au_sbr_sb(sb, bindex);
26563 +               if (h_sb->s_op->sync_fs) {
26564 +                       e = h_sb->s_op->sync_fs(h_sb, wait);
26565 +                       if (unlikely(e && !err))
26566 +                               err = e;
26567 +                       /* go on even if an error happens */
26568 +               }
26569 +       }
26570 +       si_read_unlock(sb);
26571 +
26572 +       return err;
26573 +}
26574 +
26575 +/* ---------------------------------------------------------------------- */
26576 +
26577 +/* final actions when unmounting a file system */
26578 +static void aufs_put_super(struct super_block *sb)
26579 +{
26580 +       struct au_sbinfo *sbinfo;
26581 +
26582 +       sbinfo = au_sbi(sb);
26583 +       if (!sbinfo)
26584 +               return;
26585 +
26586 +       dbgaufs_si_fin(sbinfo);
26587 +       kobject_put(&sbinfo->si_kobj);
26588 +}
26589 +
26590 +/* ---------------------------------------------------------------------- */
26591 +
26592 +void au_array_free(void *array)
26593 +{
26594 +       if (array) {
26595 +               if (!is_vmalloc_addr(array))
26596 +                       kfree(array);
26597 +               else
26598 +                       vfree(array);
26599 +       }
26600 +}
26601 +
26602 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
26603 +{
26604 +       void *array;
26605 +       unsigned long long n, sz;
26606 +
26607 +       array = NULL;
26608 +       n = 0;
26609 +       if (!*hint)
26610 +               goto out;
26611 +
26612 +       if (*hint > ULLONG_MAX / sizeof(array)) {
26613 +               array = ERR_PTR(-EMFILE);
26614 +               pr_err("hint %llu\n", *hint);
26615 +               goto out;
26616 +       }
26617 +
26618 +       sz = sizeof(array) * *hint;
26619 +       array = kzalloc(sz, GFP_NOFS);
26620 +       if (unlikely(!array))
26621 +               array = vzalloc(sz);
26622 +       if (unlikely(!array)) {
26623 +               array = ERR_PTR(-ENOMEM);
26624 +               goto out;
26625 +       }
26626 +
26627 +       n = cb(array, *hint, arg);
26628 +       AuDebugOn(n > *hint);
26629 +
26630 +out:
26631 +       *hint = n;
26632 +       return array;
26633 +}
26634 +
26635 +static unsigned long long au_iarray_cb(void *a,
26636 +                                      unsigned long long max __maybe_unused,
26637 +                                      void *arg)
26638 +{
26639 +       unsigned long long n;
26640 +       struct inode **p, *inode;
26641 +       struct list_head *head;
26642 +
26643 +       n = 0;
26644 +       p = a;
26645 +       head = arg;
26646 +       spin_lock(&inode_sb_list_lock);
26647 +       list_for_each_entry(inode, head, i_sb_list) {
26648 +               if (!is_bad_inode(inode)
26649 +                   && au_ii(inode)->ii_bstart >= 0) {
26650 +                       spin_lock(&inode->i_lock);
26651 +                       if (atomic_read(&inode->i_count)) {
26652 +                               au_igrab(inode);
26653 +                               *p++ = inode;
26654 +                               n++;
26655 +                               AuDebugOn(n > max);
26656 +                       }
26657 +                       spin_unlock(&inode->i_lock);
26658 +               }
26659 +       }
26660 +       spin_unlock(&inode_sb_list_lock);
26661 +
26662 +       return n;
26663 +}
26664 +
26665 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
26666 +{
26667 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
26668 +       return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
26669 +}
26670 +
26671 +void au_iarray_free(struct inode **a, unsigned long long max)
26672 +{
26673 +       unsigned long long ull;
26674 +
26675 +       for (ull = 0; ull < max; ull++)
26676 +               iput(a[ull]);
26677 +       au_array_free(a);
26678 +}
26679 +
26680 +/* ---------------------------------------------------------------------- */
26681 +
26682 +/*
26683 + * refresh dentry and inode at remount time.
26684 + */
26685 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
26686 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
26687 +                     struct dentry *parent)
26688 +{
26689 +       int err;
26690 +
26691 +       di_write_lock_child(dentry);
26692 +       di_read_lock_parent(parent, AuLock_IR);
26693 +       err = au_refresh_dentry(dentry, parent);
26694 +       if (!err && dir_flags)
26695 +               au_hn_reset(dentry->d_inode, dir_flags);
26696 +       di_read_unlock(parent, AuLock_IR);
26697 +       di_write_unlock(dentry);
26698 +
26699 +       return err;
26700 +}
26701 +
26702 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
26703 +                          struct au_sbinfo *sbinfo,
26704 +                          const unsigned int dir_flags)
26705 +{
26706 +       int err;
26707 +       struct dentry *parent;
26708 +       struct inode *inode;
26709 +
26710 +       err = 0;
26711 +       parent = dget_parent(dentry);
26712 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
26713 +               inode = dentry->d_inode;
26714 +               if (inode) {
26715 +                       if (!S_ISDIR(inode->i_mode))
26716 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
26717 +                                                parent);
26718 +                       else {
26719 +                               err = au_do_refresh(dentry, dir_flags, parent);
26720 +                               if (unlikely(err))
26721 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
26722 +                       }
26723 +               } else
26724 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
26725 +               AuDbgDentry(dentry);
26726 +       }
26727 +       dput(parent);
26728 +
26729 +       AuTraceErr(err);
26730 +       return err;
26731 +}
26732 +
26733 +static int au_refresh_d(struct super_block *sb)
26734 +{
26735 +       int err, i, j, ndentry, e;
26736 +       unsigned int sigen;
26737 +       struct au_dcsub_pages dpages;
26738 +       struct au_dpage *dpage;
26739 +       struct dentry **dentries, *d;
26740 +       struct au_sbinfo *sbinfo;
26741 +       struct dentry *root = sb->s_root;
26742 +       const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
26743 +
26744 +       err = au_dpages_init(&dpages, GFP_NOFS);
26745 +       if (unlikely(err))
26746 +               goto out;
26747 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
26748 +       if (unlikely(err))
26749 +               goto out_dpages;
26750 +
26751 +       sigen = au_sigen(sb);
26752 +       sbinfo = au_sbi(sb);
26753 +       for (i = 0; i < dpages.ndpage; i++) {
26754 +               dpage = dpages.dpages + i;
26755 +               dentries = dpage->dentries;
26756 +               ndentry = dpage->ndentry;
26757 +               for (j = 0; j < ndentry; j++) {
26758 +                       d = dentries[j];
26759 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
26760 +                       if (unlikely(e && !err))
26761 +                               err = e;
26762 +                       /* go on even err */
26763 +               }
26764 +       }
26765 +
26766 +out_dpages:
26767 +       au_dpages_free(&dpages);
26768 +out:
26769 +       return err;
26770 +}
26771 +
26772 +static int au_refresh_i(struct super_block *sb)
26773 +{
26774 +       int err, e;
26775 +       unsigned int sigen;
26776 +       unsigned long long max, ull;
26777 +       struct inode *inode, **array;
26778 +
26779 +       array = au_iarray_alloc(sb, &max);
26780 +       err = PTR_ERR(array);
26781 +       if (IS_ERR(array))
26782 +               goto out;
26783 +
26784 +       err = 0;
26785 +       sigen = au_sigen(sb);
26786 +       for (ull = 0; ull < max; ull++) {
26787 +               inode = array[ull];
26788 +               if (unlikely(!inode))
26789 +                       break;
26790 +               if (au_iigen(inode, NULL) != sigen) {
26791 +                       ii_write_lock_child(inode);
26792 +                       e = au_refresh_hinode_self(inode);
26793 +                       ii_write_unlock(inode);
26794 +                       if (unlikely(e)) {
26795 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
26796 +                               if (!err)
26797 +                                       err = e;
26798 +                               /* go on even if err */
26799 +                       }
26800 +               }
26801 +       }
26802 +
26803 +       au_iarray_free(array, max);
26804 +
26805 +out:
26806 +       return err;
26807 +}
26808 +
26809 +static void au_remount_refresh(struct super_block *sb)
26810 +{
26811 +       int err, e;
26812 +       unsigned int udba;
26813 +       aufs_bindex_t bindex, bend;
26814 +       struct dentry *root;
26815 +       struct inode *inode;
26816 +       struct au_branch *br;
26817 +
26818 +       au_sigen_inc(sb);
26819 +       au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
26820 +
26821 +       root = sb->s_root;
26822 +       DiMustNoWaiters(root);
26823 +       inode = root->d_inode;
26824 +       IiMustNoWaiters(inode);
26825 +
26826 +       udba = au_opt_udba(sb);
26827 +       bend = au_sbend(sb);
26828 +       for (bindex = 0; bindex <= bend; bindex++) {
26829 +               br = au_sbr(sb, bindex);
26830 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
26831 +               if (unlikely(err))
26832 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
26833 +                               bindex, err);
26834 +               /* go on even if err */
26835 +       }
26836 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
26837 +
26838 +       di_write_unlock(root);
26839 +       err = au_refresh_d(sb);
26840 +       e = au_refresh_i(sb);
26841 +       if (unlikely(e && !err))
26842 +               err = e;
26843 +       /* aufs_write_lock() calls ..._child() */
26844 +       di_write_lock_child(root);
26845 +
26846 +       au_cpup_attr_all(inode, /*force*/1);
26847 +
26848 +       if (unlikely(err))
26849 +               AuIOErr("refresh failed, ignored, %d\n", err);
26850 +}
26851 +
26852 +/* stop extra interpretation of errno in mount(8), and strange error messages */
26853 +static int cvt_err(int err)
26854 +{
26855 +       AuTraceErr(err);
26856 +
26857 +       switch (err) {
26858 +       case -ENOENT:
26859 +       case -ENOTDIR:
26860 +       case -EEXIST:
26861 +       case -EIO:
26862 +               err = -EINVAL;
26863 +       }
26864 +       return err;
26865 +}
26866 +
26867 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
26868 +{
26869 +       int err, do_dx;
26870 +       unsigned int mntflags;
26871 +       struct au_opts opts;
26872 +       struct dentry *root;
26873 +       struct inode *inode;
26874 +       struct au_sbinfo *sbinfo;
26875 +
26876 +       err = 0;
26877 +       root = sb->s_root;
26878 +       if (!data || !*data) {
26879 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
26880 +               if (!err) {
26881 +                       di_write_lock_child(root);
26882 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
26883 +                       aufs_write_unlock(root);
26884 +               }
26885 +               goto out;
26886 +       }
26887 +
26888 +       err = -ENOMEM;
26889 +       memset(&opts, 0, sizeof(opts));
26890 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
26891 +       if (unlikely(!opts.opt))
26892 +               goto out;
26893 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
26894 +       opts.flags = AuOpts_REMOUNT;
26895 +       opts.sb_flags = *flags;
26896 +
26897 +       /* parse it before aufs lock */
26898 +       err = au_opts_parse(sb, data, &opts);
26899 +       if (unlikely(err))
26900 +               goto out_opts;
26901 +
26902 +       sbinfo = au_sbi(sb);
26903 +       inode = root->d_inode;
26904 +       mutex_lock(&inode->i_mutex);
26905 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
26906 +       if (unlikely(err))
26907 +               goto out_mtx;
26908 +       di_write_lock_child(root);
26909 +
26910 +       /* au_opts_remount() may return an error */
26911 +       err = au_opts_remount(sb, &opts);
26912 +       au_opts_free(&opts);
26913 +
26914 +       if (au_ftest_opts(opts.flags, REFRESH))
26915 +               au_remount_refresh(sb);
26916 +
26917 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
26918 +               mntflags = au_mntflags(sb);
26919 +               do_dx = !!au_opt_test(mntflags, DIO);
26920 +               au_dy_arefresh(do_dx);
26921 +       }
26922 +
26923 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
26924 +       aufs_write_unlock(root);
26925 +
26926 +out_mtx:
26927 +       mutex_unlock(&inode->i_mutex);
26928 +out_opts:
26929 +       free_page((unsigned long)opts.opt);
26930 +out:
26931 +       err = cvt_err(err);
26932 +       AuTraceErr(err);
26933 +       return err;
26934 +}
26935 +
26936 +static const struct super_operations aufs_sop = {
26937 +       .alloc_inode    = aufs_alloc_inode,
26938 +       .destroy_inode  = aufs_destroy_inode,
26939 +       /* always deleting, no clearing */
26940 +       .drop_inode     = generic_delete_inode,
26941 +       .show_options   = aufs_show_options,
26942 +       .statfs         = aufs_statfs,
26943 +       .put_super      = aufs_put_super,
26944 +       .sync_fs        = aufs_sync_fs,
26945 +       .remount_fs     = aufs_remount_fs
26946 +};
26947 +
26948 +/* ---------------------------------------------------------------------- */
26949 +
26950 +static int alloc_root(struct super_block *sb)
26951 +{
26952 +       int err;
26953 +       struct inode *inode;
26954 +       struct dentry *root;
26955 +
26956 +       err = -ENOMEM;
26957 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
26958 +       err = PTR_ERR(inode);
26959 +       if (IS_ERR(inode))
26960 +               goto out;
26961 +
26962 +       inode->i_op = &aufs_dir_iop;
26963 +       inode->i_fop = &aufs_dir_fop;
26964 +       inode->i_mode = S_IFDIR;
26965 +       set_nlink(inode, 2);
26966 +       unlock_new_inode(inode);
26967 +
26968 +       root = d_make_root(inode);
26969 +       if (unlikely(!root))
26970 +               goto out;
26971 +       err = PTR_ERR(root);
26972 +       if (IS_ERR(root))
26973 +               goto out;
26974 +
26975 +       err = au_di_init(root);
26976 +       if (!err) {
26977 +               sb->s_root = root;
26978 +               return 0; /* success */
26979 +       }
26980 +       dput(root);
26981 +
26982 +out:
26983 +       return err;
26984 +}
26985 +
26986 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
26987 +                          int silent __maybe_unused)
26988 +{
26989 +       int err;
26990 +       struct au_opts opts;
26991 +       struct dentry *root;
26992 +       struct inode *inode;
26993 +       char *arg = raw_data;
26994 +
26995 +       if (unlikely(!arg || !*arg)) {
26996 +               err = -EINVAL;
26997 +               pr_err("no arg\n");
26998 +               goto out;
26999 +       }
27000 +
27001 +       err = -ENOMEM;
27002 +       memset(&opts, 0, sizeof(opts));
27003 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
27004 +       if (unlikely(!opts.opt))
27005 +               goto out;
27006 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27007 +       opts.sb_flags = sb->s_flags;
27008 +
27009 +       err = au_si_alloc(sb);
27010 +       if (unlikely(err))
27011 +               goto out_opts;
27012 +
27013 +       /* all timestamps always follow the ones on the branch */
27014 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
27015 +       sb->s_op = &aufs_sop;
27016 +       sb->s_d_op = &aufs_dop;
27017 +       sb->s_magic = AUFS_SUPER_MAGIC;
27018 +       sb->s_maxbytes = 0;
27019 +       sb->s_stack_depth = 1;
27020 +       au_export_init(sb);
27021 +       /* au_xattr_init(sb); */
27022 +
27023 +       err = alloc_root(sb);
27024 +       if (unlikely(err)) {
27025 +               si_write_unlock(sb);
27026 +               goto out_info;
27027 +       }
27028 +       root = sb->s_root;
27029 +       inode = root->d_inode;
27030 +
27031 +       /*
27032 +        * actually we can parse options regardless aufs lock here.
27033 +        * but at remount time, parsing must be done before aufs lock.
27034 +        * so we follow the same rule.
27035 +        */
27036 +       ii_write_lock_parent(inode);
27037 +       aufs_write_unlock(root);
27038 +       err = au_opts_parse(sb, arg, &opts);
27039 +       if (unlikely(err))
27040 +               goto out_root;
27041 +
27042 +       /* lock vfs_inode first, then aufs. */
27043 +       mutex_lock(&inode->i_mutex);
27044 +       aufs_write_lock(root);
27045 +       err = au_opts_mount(sb, &opts);
27046 +       au_opts_free(&opts);
27047 +       aufs_write_unlock(root);
27048 +       mutex_unlock(&inode->i_mutex);
27049 +       if (!err)
27050 +               goto out_opts; /* success */
27051 +
27052 +out_root:
27053 +       dput(root);
27054 +       sb->s_root = NULL;
27055 +out_info:
27056 +       dbgaufs_si_fin(au_sbi(sb));
27057 +       kobject_put(&au_sbi(sb)->si_kobj);
27058 +       sb->s_fs_info = NULL;
27059 +out_opts:
27060 +       free_page((unsigned long)opts.opt);
27061 +out:
27062 +       AuTraceErr(err);
27063 +       err = cvt_err(err);
27064 +       AuTraceErr(err);
27065 +       return err;
27066 +}
27067 +
27068 +/* ---------------------------------------------------------------------- */
27069 +
27070 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
27071 +                                const char *dev_name __maybe_unused,
27072 +                                void *raw_data)
27073 +{
27074 +       struct dentry *root;
27075 +       struct super_block *sb;
27076 +
27077 +       /* all timestamps always follow the ones on the branch */
27078 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
27079 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
27080 +       if (IS_ERR(root))
27081 +               goto out;
27082 +
27083 +       sb = root->d_sb;
27084 +       si_write_lock(sb, !AuLock_FLUSH);
27085 +       sysaufs_brs_add(sb, 0);
27086 +       si_write_unlock(sb);
27087 +       au_sbilist_add(sb);
27088 +
27089 +out:
27090 +       return root;
27091 +}
27092 +
27093 +static void aufs_kill_sb(struct super_block *sb)
27094 +{
27095 +       struct au_sbinfo *sbinfo;
27096 +
27097 +       sbinfo = au_sbi(sb);
27098 +       if (sbinfo) {
27099 +               au_sbilist_del(sb);
27100 +               aufs_write_lock(sb->s_root);
27101 +               au_fhsm_fin(sb);
27102 +               if (sbinfo->si_wbr_create_ops->fin)
27103 +                       sbinfo->si_wbr_create_ops->fin(sb);
27104 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
27105 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
27106 +                       au_remount_refresh(sb);
27107 +               }
27108 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27109 +                       au_plink_put(sb, /*verbose*/1);
27110 +               au_xino_clr(sb);
27111 +               sbinfo->si_sb = NULL;
27112 +               aufs_write_unlock(sb->s_root);
27113 +               au_nwt_flush(&sbinfo->si_nowait);
27114 +       }
27115 +       kill_anon_super(sb);
27116 +}
27117 +
27118 +struct file_system_type aufs_fs_type = {
27119 +       .name           = AUFS_FSTYPE,
27120 +       /* a race between rename and others */
27121 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
27122 +       .mount          = aufs_mount,
27123 +       .kill_sb        = aufs_kill_sb,
27124 +       /* no need to __module_get() and module_put(). */
27125 +       .owner          = THIS_MODULE,
27126 +};
27127 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
27128 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
27129 +++ linux/fs/aufs/super.h       2015-04-13 15:10:20.790157026 +0200
27130 @@ -0,0 +1,635 @@
27131 +/*
27132 + * Copyright (C) 2005-2015 Junjiro R. Okajima
27133 + *
27134 + * This program, aufs is free software; you can redistribute it and/or modify
27135 + * it under the terms of the GNU General Public License as published by
27136 + * the Free Software Foundation; either version 2 of the License, or
27137 + * (at your option) any later version.
27138 + *
27139 + * This program is distributed in the hope that it will be useful,
27140 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27141 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27142 + * GNU General Public License for more details.
27143 + *
27144 + * You should have received a copy of the GNU General Public License
27145 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27146 + */
27147 +
27148 +/*
27149 + * super_block operations
27150 + */
27151 +
27152 +#ifndef __AUFS_SUPER_H__
27153 +#define __AUFS_SUPER_H__
27154 +
27155 +#ifdef __KERNEL__
27156 +
27157 +#include <linux/fs.h>
27158 +#include "rwsem.h"
27159 +#include "spl.h"
27160 +#include "wkq.h"
27161 +
27162 +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
27163 +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
27164 +                              loff_t *);
27165 +
27166 +/* policies to select one among multiple writable branches */
27167 +struct au_wbr_copyup_operations {
27168 +       int (*copyup)(struct dentry *dentry);
27169 +};
27170 +
27171 +#define AuWbr_DIR      1               /* target is a dir */
27172 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
27173 +
27174 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
27175 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
27176 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
27177 +
27178 +struct au_wbr_create_operations {
27179 +       int (*create)(struct dentry *dentry, unsigned int flags);
27180 +       int (*init)(struct super_block *sb);
27181 +       int (*fin)(struct super_block *sb);
27182 +};
27183 +
27184 +struct au_wbr_mfs {
27185 +       struct mutex    mfs_lock; /* protect this structure */
27186 +       unsigned long   mfs_jiffy;
27187 +       unsigned long   mfs_expire;
27188 +       aufs_bindex_t   mfs_bindex;
27189 +
27190 +       unsigned long long      mfsrr_bytes;
27191 +       unsigned long long      mfsrr_watermark;
27192 +};
27193 +
27194 +struct pseudo_link {
27195 +       union {
27196 +               struct hlist_node hlist;
27197 +               struct rcu_head rcu;
27198 +       };
27199 +       struct inode *inode;
27200 +};
27201 +
27202 +#define AuPlink_NHASH 100
27203 +static inline int au_plink_hash(ino_t ino)
27204 +{
27205 +       return ino % AuPlink_NHASH;
27206 +}
27207 +
27208 +/* File-based Hierarchical Storage Management */
27209 +struct au_fhsm {
27210 +#ifdef CONFIG_AUFS_FHSM
27211 +       /* allow only one process who can receive the notification */
27212 +       spinlock_t              fhsm_spin;
27213 +       pid_t                   fhsm_pid;
27214 +       wait_queue_head_t       fhsm_wqh;
27215 +       atomic_t                fhsm_readable;
27216 +
27217 +       /* these are protected by si_rwsem */
27218 +       unsigned long           fhsm_expire;
27219 +       aufs_bindex_t           fhsm_bottom;
27220 +#endif
27221 +};
27222 +
27223 +struct au_branch;
27224 +struct au_sbinfo {
27225 +       /* nowait tasks in the system-wide workqueue */
27226 +       struct au_nowait_tasks  si_nowait;
27227 +
27228 +       /*
27229 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
27230 +        * rwsem for au_sbinfo is necessary.
27231 +        */
27232 +       struct au_rwsem         si_rwsem;
27233 +
27234 +       /* prevent recursive locking in deleting inode */
27235 +       struct {
27236 +               unsigned long           *bitmap;
27237 +               spinlock_t              tree_lock;
27238 +               struct radix_tree_root  tree;
27239 +       } au_si_pid;
27240 +
27241 +       /*
27242 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
27243 +        * remount.
27244 +        */
27245 +       atomic_long_t           si_ninodes, si_nfiles;
27246 +
27247 +       /* branch management */
27248 +       unsigned int            si_generation;
27249 +
27250 +       /* see AuSi_ flags */
27251 +       unsigned char           au_si_status;
27252 +
27253 +       aufs_bindex_t           si_bend;
27254 +
27255 +       /* dirty trick to keep br_id plus */
27256 +       unsigned int            si_last_br_id :
27257 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
27258 +       struct au_branch        **si_branch;
27259 +
27260 +       /* policy to select a writable branch */
27261 +       unsigned char           si_wbr_copyup;
27262 +       unsigned char           si_wbr_create;
27263 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
27264 +       struct au_wbr_create_operations *si_wbr_create_ops;
27265 +
27266 +       /* round robin */
27267 +       atomic_t                si_wbr_rr_next;
27268 +
27269 +       /* most free space */
27270 +       struct au_wbr_mfs       si_wbr_mfs;
27271 +
27272 +       /* File-based Hierarchical Storage Management */
27273 +       struct au_fhsm          si_fhsm;
27274 +
27275 +       /* mount flags */
27276 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
27277 +       unsigned int            si_mntflags;
27278 +
27279 +       /* external inode number (bitmap and translation table) */
27280 +       au_readf_t              si_xread;
27281 +       au_writef_t             si_xwrite;
27282 +       struct file             *si_xib;
27283 +       struct mutex            si_xib_mtx; /* protect xib members */
27284 +       unsigned long           *si_xib_buf;
27285 +       unsigned long           si_xib_last_pindex;
27286 +       int                     si_xib_next_bit;
27287 +       aufs_bindex_t           si_xino_brid;
27288 +       unsigned long           si_xino_jiffy;
27289 +       unsigned long           si_xino_expire;
27290 +       /* reserved for future use */
27291 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
27292 +
27293 +#ifdef CONFIG_AUFS_EXPORT
27294 +       /* i_generation */
27295 +       struct file             *si_xigen;
27296 +       atomic_t                si_xigen_next;
27297 +#endif
27298 +
27299 +       /* vdir parameters */
27300 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
27301 +       unsigned int            si_rdblk;       /* deblk size */
27302 +       unsigned int            si_rdhash;      /* hash size */
27303 +
27304 +       /*
27305 +        * If the number of whiteouts are larger than si_dirwh, leave all of
27306 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
27307 +        * future fsck.aufs or kernel thread will remove them later.
27308 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
27309 +        */
27310 +       unsigned int            si_dirwh;
27311 +
27312 +       /* pseudo_link list */
27313 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
27314 +       wait_queue_head_t       si_plink_wq;
27315 +       spinlock_t              si_plink_maint_lock;
27316 +       pid_t                   si_plink_maint_pid;
27317 +
27318 +       /* file list */
27319 +       struct au_sphlhead      si_files;
27320 +
27321 +       /*
27322 +        * sysfs and lifetime management.
27323 +        * this is not a small structure and it may be a waste of memory in case
27324 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
27325 +        * but using sysfs is majority.
27326 +        */
27327 +       struct kobject          si_kobj;
27328 +#ifdef CONFIG_DEBUG_FS
27329 +       struct dentry            *si_dbgaufs;
27330 +       struct dentry            *si_dbgaufs_plink;
27331 +       struct dentry            *si_dbgaufs_xib;
27332 +#ifdef CONFIG_AUFS_EXPORT
27333 +       struct dentry            *si_dbgaufs_xigen;
27334 +#endif
27335 +#endif
27336 +
27337 +#ifdef CONFIG_AUFS_SBILIST
27338 +       struct list_head        si_list;
27339 +#endif
27340 +
27341 +       /* dirty, necessary for unmounting, sysfs and sysrq */
27342 +       struct super_block      *si_sb;
27343 +};
27344 +
27345 +/* sbinfo status flags */
27346 +/*
27347 + * set true when refresh_dirs() failed at remount time.
27348 + * then try refreshing dirs at access time again.
27349 + * if it is false, refreshing dirs at access time is unnecesary
27350 + */
27351 +#define AuSi_FAILED_REFRESH_DIR        1
27352 +
27353 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
27354 +
27355 +#ifndef CONFIG_AUFS_FHSM
27356 +#undef AuSi_FHSM
27357 +#define AuSi_FHSM              0
27358 +#endif
27359 +
27360 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
27361 +                                          unsigned int flag)
27362 +{
27363 +       AuRwMustAnyLock(&sbi->si_rwsem);
27364 +       return sbi->au_si_status & flag;
27365 +}
27366 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
27367 +#define au_fset_si(sbinfo, name) do { \
27368 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
27369 +       (sbinfo)->au_si_status |= AuSi_##name; \
27370 +} while (0)
27371 +#define au_fclr_si(sbinfo, name) do { \
27372 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
27373 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
27374 +} while (0)
27375 +
27376 +/* ---------------------------------------------------------------------- */
27377 +
27378 +/* policy to select one among writable branches */
27379 +#define AuWbrCopyup(sbinfo, ...) \
27380 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
27381 +#define AuWbrCreate(sbinfo, ...) \
27382 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
27383 +
27384 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
27385 +#define AuLock_DW              1               /* write-lock dentry */
27386 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
27387 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
27388 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
27389 +#define AuLock_DIR             (1 << 4)        /* target is a dir */
27390 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
27391 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
27392 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
27393 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
27394 +#define au_fset_lock(flags, name) \
27395 +       do { (flags) |= AuLock_##name; } while (0)
27396 +#define au_fclr_lock(flags, name) \
27397 +       do { (flags) &= ~AuLock_##name; } while (0)
27398 +
27399 +/* ---------------------------------------------------------------------- */
27400 +
27401 +/* super.c */
27402 +extern struct file_system_type aufs_fs_type;
27403 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
27404 +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
27405 +                                          void *arg);
27406 +void au_array_free(void *array);
27407 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
27408 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
27409 +void au_iarray_free(struct inode **a, unsigned long long max);
27410 +
27411 +/* sbinfo.c */
27412 +void au_si_free(struct kobject *kobj);
27413 +int au_si_alloc(struct super_block *sb);
27414 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
27415 +
27416 +unsigned int au_sigen_inc(struct super_block *sb);
27417 +aufs_bindex_t au_new_br_id(struct super_block *sb);
27418 +
27419 +int si_read_lock(struct super_block *sb, int flags);
27420 +int si_write_lock(struct super_block *sb, int flags);
27421 +int aufs_read_lock(struct dentry *dentry, int flags);
27422 +void aufs_read_unlock(struct dentry *dentry, int flags);
27423 +void aufs_write_lock(struct dentry *dentry);
27424 +void aufs_write_unlock(struct dentry *dentry);
27425 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
27426 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
27427 +
27428 +int si_pid_test_slow(struct super_block *sb);
27429 +void si_pid_set_slow(struct super_block *sb);
27430 +void si_pid_clr_slow(struct super_block *sb);
27431 +
27432 +/* wbr_policy.c */
27433 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
27434 +extern struct au_wbr_create_operations au_wbr_create_ops[];
27435 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
27436 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
27437 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart);
27438 +
27439 +/* mvdown.c */
27440 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
27441 +
27442 +#ifdef CONFIG_AUFS_FHSM
27443 +/* fhsm.c */
27444 +
27445 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
27446 +{
27447 +       pid_t pid;
27448 +
27449 +       spin_lock(&fhsm->fhsm_spin);
27450 +       pid = fhsm->fhsm_pid;
27451 +       spin_unlock(&fhsm->fhsm_spin);
27452 +
27453 +       return pid;
27454 +}
27455 +
27456 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
27457 +void au_fhsm_wrote_all(struct super_block *sb, int force);
27458 +int au_fhsm_fd(struct super_block *sb, int oflags);
27459 +int au_fhsm_br_alloc(struct au_branch *br);
27460 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
27461 +void au_fhsm_fin(struct super_block *sb);
27462 +void au_fhsm_init(struct au_sbinfo *sbinfo);
27463 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
27464 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
27465 +#else
27466 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
27467 +          int force)
27468 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
27469 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
27470 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
27471 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
27472 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
27473 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
27474 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
27475 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
27476 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
27477 +#endif
27478 +
27479 +/* ---------------------------------------------------------------------- */
27480 +
27481 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
27482 +{
27483 +       return sb->s_fs_info;
27484 +}
27485 +
27486 +/* ---------------------------------------------------------------------- */
27487 +
27488 +#ifdef CONFIG_AUFS_EXPORT
27489 +int au_test_nfsd(void);
27490 +void au_export_init(struct super_block *sb);
27491 +void au_xigen_inc(struct inode *inode);
27492 +int au_xigen_new(struct inode *inode);
27493 +int au_xigen_set(struct super_block *sb, struct file *base);
27494 +void au_xigen_clr(struct super_block *sb);
27495 +
27496 +static inline int au_busy_or_stale(void)
27497 +{
27498 +       if (!au_test_nfsd())
27499 +               return -EBUSY;
27500 +       return -ESTALE;
27501 +}
27502 +#else
27503 +AuStubInt0(au_test_nfsd, void)
27504 +AuStubVoid(au_export_init, struct super_block *sb)
27505 +AuStubVoid(au_xigen_inc, struct inode *inode)
27506 +AuStubInt0(au_xigen_new, struct inode *inode)
27507 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
27508 +AuStubVoid(au_xigen_clr, struct super_block *sb)
27509 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
27510 +#endif /* CONFIG_AUFS_EXPORT */
27511 +
27512 +/* ---------------------------------------------------------------------- */
27513 +
27514 +#ifdef CONFIG_AUFS_SBILIST
27515 +/* module.c */
27516 +extern struct au_splhead au_sbilist;
27517 +
27518 +static inline void au_sbilist_init(void)
27519 +{
27520 +       au_spl_init(&au_sbilist);
27521 +}
27522 +
27523 +static inline void au_sbilist_add(struct super_block *sb)
27524 +{
27525 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
27526 +}
27527 +
27528 +static inline void au_sbilist_del(struct super_block *sb)
27529 +{
27530 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
27531 +}
27532 +
27533 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
27534 +static inline void au_sbilist_lock(void)
27535 +{
27536 +       spin_lock(&au_sbilist.spin);
27537 +}
27538 +
27539 +static inline void au_sbilist_unlock(void)
27540 +{
27541 +       spin_unlock(&au_sbilist.spin);
27542 +}
27543 +#define AuGFP_SBILIST  GFP_ATOMIC
27544 +#else
27545 +AuStubVoid(au_sbilist_lock, void)
27546 +AuStubVoid(au_sbilist_unlock, void)
27547 +#define AuGFP_SBILIST  GFP_NOFS
27548 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
27549 +#else
27550 +AuStubVoid(au_sbilist_init, void)
27551 +AuStubVoid(au_sbilist_add, struct super_block *sb)
27552 +AuStubVoid(au_sbilist_del, struct super_block *sb)
27553 +AuStubVoid(au_sbilist_lock, void)
27554 +AuStubVoid(au_sbilist_unlock, void)
27555 +#define AuGFP_SBILIST  GFP_NOFS
27556 +#endif
27557 +
27558 +/* ---------------------------------------------------------------------- */
27559 +
27560 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
27561 +{
27562 +       /*
27563 +        * This function is a dynamic '__init' function actually,
27564 +        * so the tiny check for si_rwsem is unnecessary.
27565 +        */
27566 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
27567 +#ifdef CONFIG_DEBUG_FS
27568 +       sbinfo->si_dbgaufs = NULL;
27569 +       sbinfo->si_dbgaufs_plink = NULL;
27570 +       sbinfo->si_dbgaufs_xib = NULL;
27571 +#ifdef CONFIG_AUFS_EXPORT
27572 +       sbinfo->si_dbgaufs_xigen = NULL;
27573 +#endif
27574 +#endif
27575 +}
27576 +
27577 +/* ---------------------------------------------------------------------- */
27578 +
27579 +static inline pid_t si_pid_bit(void)
27580 +{
27581 +       /* the origin of pid is 1, but the bitmap's is 0 */
27582 +       return current->pid - 1;
27583 +}
27584 +
27585 +static inline int si_pid_test(struct super_block *sb)
27586 +{
27587 +       pid_t bit;
27588 +
27589 +       bit = si_pid_bit();
27590 +       if (bit < PID_MAX_DEFAULT)
27591 +               return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
27592 +       return si_pid_test_slow(sb);
27593 +}
27594 +
27595 +static inline void si_pid_set(struct super_block *sb)
27596 +{
27597 +       pid_t bit;
27598 +
27599 +       bit = si_pid_bit();
27600 +       if (bit < PID_MAX_DEFAULT) {
27601 +               AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
27602 +               set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
27603 +               /* smp_mb(); */
27604 +       } else
27605 +               si_pid_set_slow(sb);
27606 +}
27607 +
27608 +static inline void si_pid_clr(struct super_block *sb)
27609 +{
27610 +       pid_t bit;
27611 +
27612 +       bit = si_pid_bit();
27613 +       if (bit < PID_MAX_DEFAULT) {
27614 +               AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
27615 +               clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
27616 +               /* smp_mb(); */
27617 +       } else
27618 +               si_pid_clr_slow(sb);
27619 +}
27620 +
27621 +/* ---------------------------------------------------------------------- */
27622 +
27623 +/* lock superblock. mainly for entry point functions */
27624 +/*
27625 + * __si_read_lock, __si_write_lock,
27626 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
27627 + */
27628 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
27629 +
27630 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
27631 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
27632 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
27633 +
27634 +static inline void si_noflush_read_lock(struct super_block *sb)
27635 +{
27636 +       __si_read_lock(sb);
27637 +       si_pid_set(sb);
27638 +}
27639 +
27640 +static inline int si_noflush_read_trylock(struct super_block *sb)
27641 +{
27642 +       int locked;
27643 +
27644 +       locked = __si_read_trylock(sb);
27645 +       if (locked)
27646 +               si_pid_set(sb);
27647 +       return locked;
27648 +}
27649 +
27650 +static inline void si_noflush_write_lock(struct super_block *sb)
27651 +{
27652 +       __si_write_lock(sb);
27653 +       si_pid_set(sb);
27654 +}
27655 +
27656 +static inline int si_noflush_write_trylock(struct super_block *sb)
27657 +{
27658 +       int locked;
27659 +
27660 +       locked = __si_write_trylock(sb);
27661 +       if (locked)
27662 +               si_pid_set(sb);
27663 +       return locked;
27664 +}
27665 +
27666 +#if 0 /* reserved */
27667 +static inline int si_read_trylock(struct super_block *sb, int flags)
27668 +{
27669 +       if (au_ftest_lock(flags, FLUSH))
27670 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
27671 +       return si_noflush_read_trylock(sb);
27672 +}
27673 +#endif
27674 +
27675 +static inline void si_read_unlock(struct super_block *sb)
27676 +{
27677 +       si_pid_clr(sb);
27678 +       __si_read_unlock(sb);
27679 +}
27680 +
27681 +#if 0 /* reserved */
27682 +static inline int si_write_trylock(struct super_block *sb, int flags)
27683 +{
27684 +       if (au_ftest_lock(flags, FLUSH))
27685 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
27686 +       return si_noflush_write_trylock(sb);
27687 +}
27688 +#endif
27689 +
27690 +static inline void si_write_unlock(struct super_block *sb)
27691 +{
27692 +       si_pid_clr(sb);
27693 +       __si_write_unlock(sb);
27694 +}
27695 +
27696 +#if 0 /* reserved */
27697 +static inline void si_downgrade_lock(struct super_block *sb)
27698 +{
27699 +       __si_downgrade_lock(sb);
27700 +}
27701 +#endif
27702 +
27703 +/* ---------------------------------------------------------------------- */
27704 +
27705 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
27706 +{
27707 +       SiMustAnyLock(sb);
27708 +       return au_sbi(sb)->si_bend;
27709 +}
27710 +
27711 +static inline unsigned int au_mntflags(struct super_block *sb)
27712 +{
27713 +       SiMustAnyLock(sb);
27714 +       return au_sbi(sb)->si_mntflags;
27715 +}
27716 +
27717 +static inline unsigned int au_sigen(struct super_block *sb)
27718 +{
27719 +       SiMustAnyLock(sb);
27720 +       return au_sbi(sb)->si_generation;
27721 +}
27722 +
27723 +static inline void au_ninodes_inc(struct super_block *sb)
27724 +{
27725 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
27726 +}
27727 +
27728 +static inline void au_ninodes_dec(struct super_block *sb)
27729 +{
27730 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
27731 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
27732 +}
27733 +
27734 +static inline void au_nfiles_inc(struct super_block *sb)
27735 +{
27736 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
27737 +}
27738 +
27739 +static inline void au_nfiles_dec(struct super_block *sb)
27740 +{
27741 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
27742 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
27743 +}
27744 +
27745 +static inline struct au_branch *au_sbr(struct super_block *sb,
27746 +                                      aufs_bindex_t bindex)
27747 +{
27748 +       SiMustAnyLock(sb);
27749 +       return au_sbi(sb)->si_branch[0 + bindex];
27750 +}
27751 +
27752 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
27753 +{
27754 +       SiMustWriteLock(sb);
27755 +       au_sbi(sb)->si_xino_brid = brid;
27756 +}
27757 +
27758 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
27759 +{
27760 +       SiMustAnyLock(sb);
27761 +       return au_sbi(sb)->si_xino_brid;
27762 +}
27763 +
27764 +#endif /* __KERNEL__ */
27765 +#endif /* __AUFS_SUPER_H__ */
27766 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
27767 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
27768 +++ linux/fs/aufs/sysaufs.c     2015-04-13 15:10:20.790157026 +0200
27769 @@ -0,0 +1,104 @@
27770 +/*
27771 + * Copyright (C) 2005-2015 Junjiro R. Okajima
27772 + *
27773 + * This program, aufs is free software; you can redistribute it and/or modify
27774 + * it under the terms of the GNU General Public License as published by
27775 + * the Free Software Foundation; either version 2 of the License, or
27776 + * (at your option) any later version.
27777 + *
27778 + * This program is distributed in the hope that it will be useful,
27779 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27780 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27781 + * GNU General Public License for more details.
27782 + *
27783 + * You should have received a copy of the GNU General Public License
27784 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27785 + */
27786 +
27787 +/*
27788 + * sysfs interface and lifetime management
27789 + * they are necessary regardless sysfs is disabled.
27790 + */
27791 +
27792 +#include <linux/random.h>
27793 +#include "aufs.h"
27794 +
27795 +unsigned long sysaufs_si_mask;
27796 +struct kset *sysaufs_kset;
27797 +
27798 +#define AuSiAttr(_name) { \
27799 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
27800 +       .show   = sysaufs_si_##_name,                           \
27801 +}
27802 +
27803 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
27804 +struct attribute *sysaufs_si_attrs[] = {
27805 +       &sysaufs_si_attr_xi_path.attr,
27806 +       NULL,
27807 +};
27808 +
27809 +static const struct sysfs_ops au_sbi_ops = {
27810 +       .show   = sysaufs_si_show
27811 +};
27812 +
27813 +static struct kobj_type au_sbi_ktype = {
27814 +       .release        = au_si_free,
27815 +       .sysfs_ops      = &au_sbi_ops,
27816 +       .default_attrs  = sysaufs_si_attrs
27817 +};
27818 +
27819 +/* ---------------------------------------------------------------------- */
27820 +
27821 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
27822 +{
27823 +       int err;
27824 +
27825 +       sbinfo->si_kobj.kset = sysaufs_kset;
27826 +       /* cf. sysaufs_name() */
27827 +       err = kobject_init_and_add
27828 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
27829 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
27830 +
27831 +       dbgaufs_si_null(sbinfo);
27832 +       if (!err) {
27833 +               err = dbgaufs_si_init(sbinfo);
27834 +               if (unlikely(err))
27835 +                       kobject_put(&sbinfo->si_kobj);
27836 +       }
27837 +       return err;
27838 +}
27839 +
27840 +void sysaufs_fin(void)
27841 +{
27842 +       dbgaufs_fin();
27843 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
27844 +       kset_unregister(sysaufs_kset);
27845 +}
27846 +
27847 +int __init sysaufs_init(void)
27848 +{
27849 +       int err;
27850 +
27851 +       do {
27852 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
27853 +       } while (!sysaufs_si_mask);
27854 +
27855 +       err = -EINVAL;
27856 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
27857 +       if (unlikely(!sysaufs_kset))
27858 +               goto out;
27859 +       err = PTR_ERR(sysaufs_kset);
27860 +       if (IS_ERR(sysaufs_kset))
27861 +               goto out;
27862 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
27863 +       if (unlikely(err)) {
27864 +               kset_unregister(sysaufs_kset);
27865 +               goto out;
27866 +       }
27867 +
27868 +       err = dbgaufs_init();
27869 +       if (unlikely(err))
27870 +               sysaufs_fin();
27871 +out:
27872 +       return err;
27873 +}
27874 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
27875 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
27876 +++ linux/fs/aufs/sysaufs.h     2015-04-13 15:10:20.790157026 +0200
27877 @@ -0,0 +1,101 @@
27878 +/*
27879 + * Copyright (C) 2005-2015 Junjiro R. Okajima
27880 + *
27881 + * This program, aufs is free software; you can redistribute it and/or modify
27882 + * it under the terms of the GNU General Public License as published by
27883 + * the Free Software Foundation; either version 2 of the License, or
27884 + * (at your option) any later version.
27885 + *
27886 + * This program is distributed in the hope that it will be useful,
27887 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27888 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27889 + * GNU General Public License for more details.
27890 + *
27891 + * You should have received a copy of the GNU General Public License
27892 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27893 + */
27894 +
27895 +/*
27896 + * sysfs interface and mount lifetime management
27897 + */
27898 +
27899 +#ifndef __SYSAUFS_H__
27900 +#define __SYSAUFS_H__
27901 +
27902 +#ifdef __KERNEL__
27903 +
27904 +#include <linux/sysfs.h>
27905 +#include "module.h"
27906 +
27907 +struct super_block;
27908 +struct au_sbinfo;
27909 +
27910 +struct sysaufs_si_attr {
27911 +       struct attribute attr;
27912 +       int (*show)(struct seq_file *seq, struct super_block *sb);
27913 +};
27914 +
27915 +/* ---------------------------------------------------------------------- */
27916 +
27917 +/* sysaufs.c */
27918 +extern unsigned long sysaufs_si_mask;
27919 +extern struct kset *sysaufs_kset;
27920 +extern struct attribute *sysaufs_si_attrs[];
27921 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
27922 +int __init sysaufs_init(void);
27923 +void sysaufs_fin(void);
27924 +
27925 +/* ---------------------------------------------------------------------- */
27926 +
27927 +/* some people doesn't like to show a pointer in kernel */
27928 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
27929 +{
27930 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
27931 +}
27932 +
27933 +#define SysaufsSiNamePrefix    "si_"
27934 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
27935 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
27936 +{
27937 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
27938 +                sysaufs_si_id(sbinfo));
27939 +}
27940 +
27941 +struct au_branch;
27942 +#ifdef CONFIG_SYSFS
27943 +/* sysfs.c */
27944 +extern struct attribute_group *sysaufs_attr_group;
27945 +
27946 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
27947 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
27948 +                        char *buf);
27949 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
27950 +#ifdef CONFIG_COMPAT
27951 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
27952 +#endif
27953 +
27954 +void sysaufs_br_init(struct au_branch *br);
27955 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
27956 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
27957 +
27958 +#define sysaufs_brs_init()     do {} while (0)
27959 +
27960 +#else
27961 +#define sysaufs_attr_group     NULL
27962 +
27963 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
27964 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
27965 +       struct attribute *attr, char *buf)
27966 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
27967 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
27968 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
27969 +
27970 +static inline void sysaufs_brs_init(void)
27971 +{
27972 +       sysaufs_brs = 0;
27973 +}
27974 +
27975 +#endif /* CONFIG_SYSFS */
27976 +
27977 +#endif /* __KERNEL__ */
27978 +#endif /* __SYSAUFS_H__ */
27979 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
27980 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
27981 +++ linux/fs/aufs/sysfs.c       2015-04-13 15:10:20.790157026 +0200
27982 @@ -0,0 +1,372 @@
27983 +/*
27984 + * Copyright (C) 2005-2015 Junjiro R. Okajima
27985 + *
27986 + * This program, aufs is free software; you can redistribute it and/or modify
27987 + * it under the terms of the GNU General Public License as published by
27988 + * the Free Software Foundation; either version 2 of the License, or
27989 + * (at your option) any later version.
27990 + *
27991 + * This program is distributed in the hope that it will be useful,
27992 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27993 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27994 + * GNU General Public License for more details.
27995 + *
27996 + * You should have received a copy of the GNU General Public License
27997 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27998 + */
27999 +
28000 +/*
28001 + * sysfs interface
28002 + */
28003 +
28004 +#include <linux/compat.h>
28005 +#include <linux/seq_file.h>
28006 +#include "aufs.h"
28007 +
28008 +#ifdef CONFIG_AUFS_FS_MODULE
28009 +/* this entry violates the "one line per file" policy of sysfs */
28010 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
28011 +                          char *buf)
28012 +{
28013 +       ssize_t err;
28014 +       static char *conf =
28015 +/* this file is generated at compiling */
28016 +#include "conf.str"
28017 +               ;
28018 +
28019 +       err = snprintf(buf, PAGE_SIZE, conf);
28020 +       if (unlikely(err >= PAGE_SIZE))
28021 +               err = -EFBIG;
28022 +       return err;
28023 +}
28024 +
28025 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
28026 +#endif
28027 +
28028 +static struct attribute *au_attr[] = {
28029 +#ifdef CONFIG_AUFS_FS_MODULE
28030 +       &au_config_attr.attr,
28031 +#endif
28032 +       NULL,   /* need to NULL terminate the list of attributes */
28033 +};
28034 +
28035 +static struct attribute_group sysaufs_attr_group_body = {
28036 +       .attrs = au_attr
28037 +};
28038 +
28039 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
28040 +
28041 +/* ---------------------------------------------------------------------- */
28042 +
28043 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
28044 +{
28045 +       int err;
28046 +
28047 +       SiMustAnyLock(sb);
28048 +
28049 +       err = 0;
28050 +       if (au_opt_test(au_mntflags(sb), XINO)) {
28051 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
28052 +               seq_putc(seq, '\n');
28053 +       }
28054 +       return err;
28055 +}
28056 +
28057 +/*
28058 + * the lifetime of branch is independent from the entry under sysfs.
28059 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
28060 + * unlinked.
28061 + */
28062 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
28063 +                        aufs_bindex_t bindex, int idx)
28064 +{
28065 +       int err;
28066 +       struct path path;
28067 +       struct dentry *root;
28068 +       struct au_branch *br;
28069 +       au_br_perm_str_t perm;
28070 +
28071 +       AuDbg("b%d\n", bindex);
28072 +
28073 +       err = 0;
28074 +       root = sb->s_root;
28075 +       di_read_lock_parent(root, !AuLock_IR);
28076 +       br = au_sbr(sb, bindex);
28077 +
28078 +       switch (idx) {
28079 +       case AuBrSysfs_BR:
28080 +               path.mnt = au_br_mnt(br);
28081 +               path.dentry = au_h_dptr(root, bindex);
28082 +               au_seq_path(seq, &path);
28083 +               au_optstr_br_perm(&perm, br->br_perm);
28084 +               err = seq_printf(seq, "=%s\n", perm.a);
28085 +               break;
28086 +       case AuBrSysfs_BRID:
28087 +               err = seq_printf(seq, "%d\n", br->br_id);
28088 +               break;
28089 +       }
28090 +       di_read_unlock(root, !AuLock_IR);
28091 +       if (err == -1)
28092 +               err = -E2BIG;
28093 +
28094 +       return err;
28095 +}
28096 +
28097 +/* ---------------------------------------------------------------------- */
28098 +
28099 +static struct seq_file *au_seq(char *p, ssize_t len)
28100 +{
28101 +       struct seq_file *seq;
28102 +
28103 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
28104 +       if (seq) {
28105 +               /* mutex_init(&seq.lock); */
28106 +               seq->buf = p;
28107 +               seq->size = len;
28108 +               return seq; /* success */
28109 +       }
28110 +
28111 +       seq = ERR_PTR(-ENOMEM);
28112 +       return seq;
28113 +}
28114 +
28115 +#define SysaufsBr_PREFIX       "br"
28116 +#define SysaufsBrid_PREFIX     "brid"
28117 +
28118 +/* todo: file size may exceed PAGE_SIZE */
28119 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28120 +                       char *buf)
28121 +{
28122 +       ssize_t err;
28123 +       int idx;
28124 +       long l;
28125 +       aufs_bindex_t bend;
28126 +       struct au_sbinfo *sbinfo;
28127 +       struct super_block *sb;
28128 +       struct seq_file *seq;
28129 +       char *name;
28130 +       struct attribute **cattr;
28131 +
28132 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
28133 +       sb = sbinfo->si_sb;
28134 +
28135 +       /*
28136 +        * prevent a race condition between sysfs and aufs.
28137 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
28138 +        * prohibits maintaining the sysfs entries.
28139 +        * hew we acquire read lock after sysfs_get_active_two().
28140 +        * on the other hand, the remount process may maintain the sysfs/aufs
28141 +        * entries after acquiring write lock.
28142 +        * it can cause a deadlock.
28143 +        * simply we gave up processing read here.
28144 +        */
28145 +       err = -EBUSY;
28146 +       if (unlikely(!si_noflush_read_trylock(sb)))
28147 +               goto out;
28148 +
28149 +       seq = au_seq(buf, PAGE_SIZE);
28150 +       err = PTR_ERR(seq);
28151 +       if (IS_ERR(seq))
28152 +               goto out_unlock;
28153 +
28154 +       name = (void *)attr->name;
28155 +       cattr = sysaufs_si_attrs;
28156 +       while (*cattr) {
28157 +               if (!strcmp(name, (*cattr)->name)) {
28158 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
28159 +                               ->show(seq, sb);
28160 +                       goto out_seq;
28161 +               }
28162 +               cattr++;
28163 +       }
28164 +
28165 +       if (!strncmp(name, SysaufsBrid_PREFIX,
28166 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
28167 +               idx = AuBrSysfs_BRID;
28168 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
28169 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
28170 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
28171 +               idx = AuBrSysfs_BR;
28172 +               name += sizeof(SysaufsBr_PREFIX) - 1;
28173 +       } else
28174 +                 BUG();
28175 +
28176 +       err = kstrtol(name, 10, &l);
28177 +       if (!err) {
28178 +               bend = au_sbend(sb);
28179 +               if (l <= bend)
28180 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
28181 +               else
28182 +                       err = -ENOENT;
28183 +       }
28184 +
28185 +out_seq:
28186 +       if (!err) {
28187 +               err = seq->count;
28188 +               /* sysfs limit */
28189 +               if (unlikely(err == PAGE_SIZE))
28190 +                       err = -EFBIG;
28191 +       }
28192 +       kfree(seq);
28193 +out_unlock:
28194 +       si_read_unlock(sb);
28195 +out:
28196 +       return err;
28197 +}
28198 +
28199 +/* ---------------------------------------------------------------------- */
28200 +
28201 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
28202 +{
28203 +       int err;
28204 +       int16_t brid;
28205 +       aufs_bindex_t bindex, bend;
28206 +       size_t sz;
28207 +       char *buf;
28208 +       struct seq_file *seq;
28209 +       struct au_branch *br;
28210 +
28211 +       si_read_lock(sb, AuLock_FLUSH);
28212 +       bend = au_sbend(sb);
28213 +       err = bend + 1;
28214 +       if (!arg)
28215 +               goto out;
28216 +
28217 +       err = -ENOMEM;
28218 +       buf = (void *)__get_free_page(GFP_NOFS);
28219 +       if (unlikely(!buf))
28220 +               goto out;
28221 +
28222 +       seq = au_seq(buf, PAGE_SIZE);
28223 +       err = PTR_ERR(seq);
28224 +       if (IS_ERR(seq))
28225 +               goto out_buf;
28226 +
28227 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
28228 +       for (bindex = 0; bindex <= bend; bindex++, arg++) {
28229 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
28230 +               if (unlikely(err))
28231 +                       break;
28232 +
28233 +               br = au_sbr(sb, bindex);
28234 +               brid = br->br_id;
28235 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
28236 +               err = __put_user(brid, &arg->id);
28237 +               if (unlikely(err))
28238 +                       break;
28239 +
28240 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
28241 +               err = __put_user(br->br_perm, &arg->perm);
28242 +               if (unlikely(err))
28243 +                       break;
28244 +
28245 +               au_seq_path(seq, &br->br_path);
28246 +               err = seq_putc(seq, '\0');
28247 +               if (!err && seq->count <= sz) {
28248 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
28249 +                       seq->count = 0;
28250 +                       if (unlikely(err))
28251 +                               break;
28252 +               } else {
28253 +                       err = -E2BIG;
28254 +                       goto out_seq;
28255 +               }
28256 +       }
28257 +       if (unlikely(err))
28258 +               err = -EFAULT;
28259 +
28260 +out_seq:
28261 +       kfree(seq);
28262 +out_buf:
28263 +       free_page((unsigned long)buf);
28264 +out:
28265 +       si_read_unlock(sb);
28266 +       return err;
28267 +}
28268 +
28269 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
28270 +{
28271 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
28272 +}
28273 +
28274 +#ifdef CONFIG_COMPAT
28275 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
28276 +{
28277 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
28278 +}
28279 +#endif
28280 +
28281 +/* ---------------------------------------------------------------------- */
28282 +
28283 +void sysaufs_br_init(struct au_branch *br)
28284 +{
28285 +       int i;
28286 +       struct au_brsysfs *br_sysfs;
28287 +       struct attribute *attr;
28288 +
28289 +       br_sysfs = br->br_sysfs;
28290 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28291 +               attr = &br_sysfs->attr;
28292 +               sysfs_attr_init(attr);
28293 +               attr->name = br_sysfs->name;
28294 +               attr->mode = S_IRUGO;
28295 +               br_sysfs++;
28296 +       }
28297 +}
28298 +
28299 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
28300 +{
28301 +       struct au_branch *br;
28302 +       struct kobject *kobj;
28303 +       struct au_brsysfs *br_sysfs;
28304 +       int i;
28305 +       aufs_bindex_t bend;
28306 +
28307 +       dbgaufs_brs_del(sb, bindex);
28308 +
28309 +       if (!sysaufs_brs)
28310 +               return;
28311 +
28312 +       kobj = &au_sbi(sb)->si_kobj;
28313 +       bend = au_sbend(sb);
28314 +       for (; bindex <= bend; bindex++) {
28315 +               br = au_sbr(sb, bindex);
28316 +               br_sysfs = br->br_sysfs;
28317 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28318 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
28319 +                       br_sysfs++;
28320 +               }
28321 +       }
28322 +}
28323 +
28324 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
28325 +{
28326 +       int err, i;
28327 +       aufs_bindex_t bend;
28328 +       struct kobject *kobj;
28329 +       struct au_branch *br;
28330 +       struct au_brsysfs *br_sysfs;
28331 +
28332 +       dbgaufs_brs_add(sb, bindex);
28333 +
28334 +       if (!sysaufs_brs)
28335 +               return;
28336 +
28337 +       kobj = &au_sbi(sb)->si_kobj;
28338 +       bend = au_sbend(sb);
28339 +       for (; bindex <= bend; bindex++) {
28340 +               br = au_sbr(sb, bindex);
28341 +               br_sysfs = br->br_sysfs;
28342 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
28343 +                        SysaufsBr_PREFIX "%d", bindex);
28344 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
28345 +                        SysaufsBrid_PREFIX "%d", bindex);
28346 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28347 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
28348 +                       if (unlikely(err))
28349 +                               pr_warn("failed %s under sysfs(%d)\n",
28350 +                                       br_sysfs->name, err);
28351 +                       br_sysfs++;
28352 +               }
28353 +       }
28354 +}
28355 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
28356 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
28357 +++ linux/fs/aufs/sysrq.c       2015-04-13 15:10:20.790157026 +0200
28358 @@ -0,0 +1,157 @@
28359 +/*
28360 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28361 + *
28362 + * This program, aufs is free software; you can redistribute it and/or modify
28363 + * it under the terms of the GNU General Public License as published by
28364 + * the Free Software Foundation; either version 2 of the License, or
28365 + * (at your option) any later version.
28366 + *
28367 + * This program is distributed in the hope that it will be useful,
28368 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28369 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28370 + * GNU General Public License for more details.
28371 + *
28372 + * You should have received a copy of the GNU General Public License
28373 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28374 + */
28375 +
28376 +/*
28377 + * magic sysrq hanlder
28378 + */
28379 +
28380 +/* #include <linux/sysrq.h> */
28381 +#include <linux/writeback.h>
28382 +#include "aufs.h"
28383 +
28384 +/* ---------------------------------------------------------------------- */
28385 +
28386 +static void sysrq_sb(struct super_block *sb)
28387 +{
28388 +       char *plevel;
28389 +       struct au_sbinfo *sbinfo;
28390 +       struct file *file;
28391 +       struct au_sphlhead *files;
28392 +       struct au_finfo *finfo;
28393 +
28394 +       plevel = au_plevel;
28395 +       au_plevel = KERN_WARNING;
28396 +
28397 +       /* since we define pr_fmt, call printk directly */
28398 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
28399 +
28400 +       sbinfo = au_sbi(sb);
28401 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
28402 +       pr("superblock\n");
28403 +       au_dpri_sb(sb);
28404 +
28405 +#if 0
28406 +       pr("root dentry\n");
28407 +       au_dpri_dentry(sb->s_root);
28408 +       pr("root inode\n");
28409 +       au_dpri_inode(sb->s_root->d_inode);
28410 +#endif
28411 +
28412 +#if 0
28413 +       do {
28414 +               int err, i, j, ndentry;
28415 +               struct au_dcsub_pages dpages;
28416 +               struct au_dpage *dpage;
28417 +
28418 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
28419 +               if (unlikely(err))
28420 +                       break;
28421 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
28422 +               if (!err)
28423 +                       for (i = 0; i < dpages.ndpage; i++) {
28424 +                               dpage = dpages.dpages + i;
28425 +                               ndentry = dpage->ndentry;
28426 +                               for (j = 0; j < ndentry; j++)
28427 +                                       au_dpri_dentry(dpage->dentries[j]);
28428 +                       }
28429 +               au_dpages_free(&dpages);
28430 +       } while (0);
28431 +#endif
28432 +
28433 +#if 1
28434 +       {
28435 +               struct inode *i;
28436 +
28437 +               pr("isolated inode\n");
28438 +               spin_lock(&inode_sb_list_lock);
28439 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
28440 +                       spin_lock(&i->i_lock);
28441 +                       if (1 || hlist_empty(&i->i_dentry))
28442 +                               au_dpri_inode(i);
28443 +                       spin_unlock(&i->i_lock);
28444 +               }
28445 +               spin_unlock(&inode_sb_list_lock);
28446 +       }
28447 +#endif
28448 +       pr("files\n");
28449 +       files = &au_sbi(sb)->si_files;
28450 +       spin_lock(&files->spin);
28451 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
28452 +               umode_t mode;
28453 +
28454 +               file = finfo->fi_file;
28455 +               mode = file_inode(file)->i_mode;
28456 +               if (!special_file(mode))
28457 +                       au_dpri_file(file);
28458 +       }
28459 +       spin_unlock(&files->spin);
28460 +       pr("done\n");
28461 +
28462 +#undef pr
28463 +       au_plevel = plevel;
28464 +}
28465 +
28466 +/* ---------------------------------------------------------------------- */
28467 +
28468 +/* module parameter */
28469 +static char *aufs_sysrq_key = "a";
28470 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
28471 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
28472 +
28473 +static void au_sysrq(int key __maybe_unused)
28474 +{
28475 +       struct au_sbinfo *sbinfo;
28476 +
28477 +       lockdep_off();
28478 +       au_sbilist_lock();
28479 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
28480 +               sysrq_sb(sbinfo->si_sb);
28481 +       au_sbilist_unlock();
28482 +       lockdep_on();
28483 +}
28484 +
28485 +static struct sysrq_key_op au_sysrq_op = {
28486 +       .handler        = au_sysrq,
28487 +       .help_msg       = "Aufs",
28488 +       .action_msg     = "Aufs",
28489 +       .enable_mask    = SYSRQ_ENABLE_DUMP
28490 +};
28491 +
28492 +/* ---------------------------------------------------------------------- */
28493 +
28494 +int __init au_sysrq_init(void)
28495 +{
28496 +       int err;
28497 +       char key;
28498 +
28499 +       err = -1;
28500 +       key = *aufs_sysrq_key;
28501 +       if ('a' <= key && key <= 'z')
28502 +               err = register_sysrq_key(key, &au_sysrq_op);
28503 +       if (unlikely(err))
28504 +               pr_err("err %d, sysrq=%c\n", err, key);
28505 +       return err;
28506 +}
28507 +
28508 +void au_sysrq_fin(void)
28509 +{
28510 +       int err;
28511 +
28512 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
28513 +       if (unlikely(err))
28514 +               pr_err("err %d (ignored)\n", err);
28515 +}
28516 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
28517 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
28518 +++ linux/fs/aufs/vdir.c        2015-04-13 15:10:20.790157026 +0200
28519 @@ -0,0 +1,889 @@
28520 +/*
28521 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28522 + *
28523 + * This program, aufs is free software; you can redistribute it and/or modify
28524 + * it under the terms of the GNU General Public License as published by
28525 + * the Free Software Foundation; either version 2 of the License, or
28526 + * (at your option) any later version.
28527 + *
28528 + * This program is distributed in the hope that it will be useful,
28529 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28530 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28531 + * GNU General Public License for more details.
28532 + *
28533 + * You should have received a copy of the GNU General Public License
28534 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28535 + */
28536 +
28537 +/*
28538 + * virtual or vertical directory
28539 + */
28540 +
28541 +#include "aufs.h"
28542 +
28543 +static unsigned int calc_size(int nlen)
28544 +{
28545 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
28546 +}
28547 +
28548 +static int set_deblk_end(union au_vdir_deblk_p *p,
28549 +                        union au_vdir_deblk_p *deblk_end)
28550 +{
28551 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
28552 +               p->de->de_str.len = 0;
28553 +               /* smp_mb(); */
28554 +               return 0;
28555 +       }
28556 +       return -1; /* error */
28557 +}
28558 +
28559 +/* returns true or false */
28560 +static int is_deblk_end(union au_vdir_deblk_p *p,
28561 +                       union au_vdir_deblk_p *deblk_end)
28562 +{
28563 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
28564 +               return !p->de->de_str.len;
28565 +       return 1;
28566 +}
28567 +
28568 +static unsigned char *last_deblk(struct au_vdir *vdir)
28569 +{
28570 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
28571 +}
28572 +
28573 +/* ---------------------------------------------------------------------- */
28574 +
28575 +/* estimate the apropriate size for name hash table */
28576 +unsigned int au_rdhash_est(loff_t sz)
28577 +{
28578 +       unsigned int n;
28579 +
28580 +       n = UINT_MAX;
28581 +       sz >>= 10;
28582 +       if (sz < n)
28583 +               n = sz;
28584 +       if (sz < AUFS_RDHASH_DEF)
28585 +               n = AUFS_RDHASH_DEF;
28586 +       /* pr_info("n %u\n", n); */
28587 +       return n;
28588 +}
28589 +
28590 +/*
28591 + * the allocated memory has to be freed by
28592 + * au_nhash_wh_free() or au_nhash_de_free().
28593 + */
28594 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
28595 +{
28596 +       struct hlist_head *head;
28597 +       unsigned int u;
28598 +       size_t sz;
28599 +
28600 +       sz = sizeof(*nhash->nh_head) * num_hash;
28601 +       head = kmalloc(sz, gfp);
28602 +       if (head) {
28603 +               nhash->nh_num = num_hash;
28604 +               nhash->nh_head = head;
28605 +               for (u = 0; u < num_hash; u++)
28606 +                       INIT_HLIST_HEAD(head++);
28607 +               return 0; /* success */
28608 +       }
28609 +
28610 +       return -ENOMEM;
28611 +}
28612 +
28613 +static void nhash_count(struct hlist_head *head)
28614 +{
28615 +#if 0
28616 +       unsigned long n;
28617 +       struct hlist_node *pos;
28618 +
28619 +       n = 0;
28620 +       hlist_for_each(pos, head)
28621 +               n++;
28622 +       pr_info("%lu\n", n);
28623 +#endif
28624 +}
28625 +
28626 +static void au_nhash_wh_do_free(struct hlist_head *head)
28627 +{
28628 +       struct au_vdir_wh *pos;
28629 +       struct hlist_node *node;
28630 +
28631 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
28632 +               kfree(pos);
28633 +}
28634 +
28635 +static void au_nhash_de_do_free(struct hlist_head *head)
28636 +{
28637 +       struct au_vdir_dehstr *pos;
28638 +       struct hlist_node *node;
28639 +
28640 +       hlist_for_each_entry_safe(pos, node, head, hash)
28641 +               au_cache_free_vdir_dehstr(pos);
28642 +}
28643 +
28644 +static void au_nhash_do_free(struct au_nhash *nhash,
28645 +                            void (*free)(struct hlist_head *head))
28646 +{
28647 +       unsigned int n;
28648 +       struct hlist_head *head;
28649 +
28650 +       n = nhash->nh_num;
28651 +       if (!n)
28652 +               return;
28653 +
28654 +       head = nhash->nh_head;
28655 +       while (n-- > 0) {
28656 +               nhash_count(head);
28657 +               free(head++);
28658 +       }
28659 +       kfree(nhash->nh_head);
28660 +}
28661 +
28662 +void au_nhash_wh_free(struct au_nhash *whlist)
28663 +{
28664 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
28665 +}
28666 +
28667 +static void au_nhash_de_free(struct au_nhash *delist)
28668 +{
28669 +       au_nhash_do_free(delist, au_nhash_de_do_free);
28670 +}
28671 +
28672 +/* ---------------------------------------------------------------------- */
28673 +
28674 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
28675 +                           int limit)
28676 +{
28677 +       int num;
28678 +       unsigned int u, n;
28679 +       struct hlist_head *head;
28680 +       struct au_vdir_wh *pos;
28681 +
28682 +       num = 0;
28683 +       n = whlist->nh_num;
28684 +       head = whlist->nh_head;
28685 +       for (u = 0; u < n; u++, head++)
28686 +               hlist_for_each_entry(pos, head, wh_hash)
28687 +                       if (pos->wh_bindex == btgt && ++num > limit)
28688 +                               return 1;
28689 +       return 0;
28690 +}
28691 +
28692 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
28693 +                                      unsigned char *name,
28694 +                                      unsigned int len)
28695 +{
28696 +       unsigned int v;
28697 +       /* const unsigned int magic_bit = 12; */
28698 +
28699 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
28700 +
28701 +       v = 0;
28702 +       while (len--)
28703 +               v += *name++;
28704 +       /* v = hash_long(v, magic_bit); */
28705 +       v %= nhash->nh_num;
28706 +       return nhash->nh_head + v;
28707 +}
28708 +
28709 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
28710 +                             int nlen)
28711 +{
28712 +       return str->len == nlen && !memcmp(str->name, name, nlen);
28713 +}
28714 +
28715 +/* returns found or not */
28716 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
28717 +{
28718 +       struct hlist_head *head;
28719 +       struct au_vdir_wh *pos;
28720 +       struct au_vdir_destr *str;
28721 +
28722 +       head = au_name_hash(whlist, name, nlen);
28723 +       hlist_for_each_entry(pos, head, wh_hash) {
28724 +               str = &pos->wh_str;
28725 +               AuDbg("%.*s\n", str->len, str->name);
28726 +               if (au_nhash_test_name(str, name, nlen))
28727 +                       return 1;
28728 +       }
28729 +       return 0;
28730 +}
28731 +
28732 +/* returns found(true) or not */
28733 +static int test_known(struct au_nhash *delist, char *name, int nlen)
28734 +{
28735 +       struct hlist_head *head;
28736 +       struct au_vdir_dehstr *pos;
28737 +       struct au_vdir_destr *str;
28738 +
28739 +       head = au_name_hash(delist, name, nlen);
28740 +       hlist_for_each_entry(pos, head, hash) {
28741 +               str = pos->str;
28742 +               AuDbg("%.*s\n", str->len, str->name);
28743 +               if (au_nhash_test_name(str, name, nlen))
28744 +                       return 1;
28745 +       }
28746 +       return 0;
28747 +}
28748 +
28749 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
28750 +                           unsigned char d_type)
28751 +{
28752 +#ifdef CONFIG_AUFS_SHWH
28753 +       wh->wh_ino = ino;
28754 +       wh->wh_type = d_type;
28755 +#endif
28756 +}
28757 +
28758 +/* ---------------------------------------------------------------------- */
28759 +
28760 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
28761 +                      unsigned int d_type, aufs_bindex_t bindex,
28762 +                      unsigned char shwh)
28763 +{
28764 +       int err;
28765 +       struct au_vdir_destr *str;
28766 +       struct au_vdir_wh *wh;
28767 +
28768 +       AuDbg("%.*s\n", nlen, name);
28769 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
28770 +
28771 +       err = -ENOMEM;
28772 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
28773 +       if (unlikely(!wh))
28774 +               goto out;
28775 +
28776 +       err = 0;
28777 +       wh->wh_bindex = bindex;
28778 +       if (shwh)
28779 +               au_shwh_init_wh(wh, ino, d_type);
28780 +       str = &wh->wh_str;
28781 +       str->len = nlen;
28782 +       memcpy(str->name, name, nlen);
28783 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
28784 +       /* smp_mb(); */
28785 +
28786 +out:
28787 +       return err;
28788 +}
28789 +
28790 +static int append_deblk(struct au_vdir *vdir)
28791 +{
28792 +       int err;
28793 +       unsigned long ul;
28794 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
28795 +       union au_vdir_deblk_p p, deblk_end;
28796 +       unsigned char **o;
28797 +
28798 +       err = -ENOMEM;
28799 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
28800 +                    GFP_NOFS);
28801 +       if (unlikely(!o))
28802 +               goto out;
28803 +
28804 +       vdir->vd_deblk = o;
28805 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
28806 +       if (p.deblk) {
28807 +               ul = vdir->vd_nblk++;
28808 +               vdir->vd_deblk[ul] = p.deblk;
28809 +               vdir->vd_last.ul = ul;
28810 +               vdir->vd_last.p.deblk = p.deblk;
28811 +               deblk_end.deblk = p.deblk + deblk_sz;
28812 +               err = set_deblk_end(&p, &deblk_end);
28813 +       }
28814 +
28815 +out:
28816 +       return err;
28817 +}
28818 +
28819 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
28820 +                    unsigned int d_type, struct au_nhash *delist)
28821 +{
28822 +       int err;
28823 +       unsigned int sz;
28824 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
28825 +       union au_vdir_deblk_p p, *room, deblk_end;
28826 +       struct au_vdir_dehstr *dehstr;
28827 +
28828 +       p.deblk = last_deblk(vdir);
28829 +       deblk_end.deblk = p.deblk + deblk_sz;
28830 +       room = &vdir->vd_last.p;
28831 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
28832 +                 || !is_deblk_end(room, &deblk_end));
28833 +
28834 +       sz = calc_size(nlen);
28835 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
28836 +               err = append_deblk(vdir);
28837 +               if (unlikely(err))
28838 +                       goto out;
28839 +
28840 +               p.deblk = last_deblk(vdir);
28841 +               deblk_end.deblk = p.deblk + deblk_sz;
28842 +               /* smp_mb(); */
28843 +               AuDebugOn(room->deblk != p.deblk);
28844 +       }
28845 +
28846 +       err = -ENOMEM;
28847 +       dehstr = au_cache_alloc_vdir_dehstr();
28848 +       if (unlikely(!dehstr))
28849 +               goto out;
28850 +
28851 +       dehstr->str = &room->de->de_str;
28852 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
28853 +       room->de->de_ino = ino;
28854 +       room->de->de_type = d_type;
28855 +       room->de->de_str.len = nlen;
28856 +       memcpy(room->de->de_str.name, name, nlen);
28857 +
28858 +       err = 0;
28859 +       room->deblk += sz;
28860 +       if (unlikely(set_deblk_end(room, &deblk_end)))
28861 +               err = append_deblk(vdir);
28862 +       /* smp_mb(); */
28863 +
28864 +out:
28865 +       return err;
28866 +}
28867 +
28868 +/* ---------------------------------------------------------------------- */
28869 +
28870 +void au_vdir_free(struct au_vdir *vdir)
28871 +{
28872 +       unsigned char **deblk;
28873 +
28874 +       deblk = vdir->vd_deblk;
28875 +       while (vdir->vd_nblk--)
28876 +               kfree(*deblk++);
28877 +       kfree(vdir->vd_deblk);
28878 +       au_cache_free_vdir(vdir);
28879 +}
28880 +
28881 +static struct au_vdir *alloc_vdir(struct file *file)
28882 +{
28883 +       struct au_vdir *vdir;
28884 +       struct super_block *sb;
28885 +       int err;
28886 +
28887 +       sb = file->f_path.dentry->d_sb;
28888 +       SiMustAnyLock(sb);
28889 +
28890 +       err = -ENOMEM;
28891 +       vdir = au_cache_alloc_vdir();
28892 +       if (unlikely(!vdir))
28893 +               goto out;
28894 +
28895 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
28896 +       if (unlikely(!vdir->vd_deblk))
28897 +               goto out_free;
28898 +
28899 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
28900 +       if (!vdir->vd_deblk_sz) {
28901 +               /* estimate the apropriate size for deblk */
28902 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
28903 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
28904 +       }
28905 +       vdir->vd_nblk = 0;
28906 +       vdir->vd_version = 0;
28907 +       vdir->vd_jiffy = 0;
28908 +       err = append_deblk(vdir);
28909 +       if (!err)
28910 +               return vdir; /* success */
28911 +
28912 +       kfree(vdir->vd_deblk);
28913 +
28914 +out_free:
28915 +       au_cache_free_vdir(vdir);
28916 +out:
28917 +       vdir = ERR_PTR(err);
28918 +       return vdir;
28919 +}
28920 +
28921 +static int reinit_vdir(struct au_vdir *vdir)
28922 +{
28923 +       int err;
28924 +       union au_vdir_deblk_p p, deblk_end;
28925 +
28926 +       while (vdir->vd_nblk > 1) {
28927 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
28928 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
28929 +               vdir->vd_nblk--;
28930 +       }
28931 +       p.deblk = vdir->vd_deblk[0];
28932 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
28933 +       err = set_deblk_end(&p, &deblk_end);
28934 +       /* keep vd_dblk_sz */
28935 +       vdir->vd_last.ul = 0;
28936 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
28937 +       vdir->vd_version = 0;
28938 +       vdir->vd_jiffy = 0;
28939 +       /* smp_mb(); */
28940 +       return err;
28941 +}
28942 +
28943 +/* ---------------------------------------------------------------------- */
28944 +
28945 +#define AuFillVdir_CALLED      1
28946 +#define AuFillVdir_WHABLE      (1 << 1)
28947 +#define AuFillVdir_SHWH                (1 << 2)
28948 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
28949 +#define au_fset_fillvdir(flags, name) \
28950 +       do { (flags) |= AuFillVdir_##name; } while (0)
28951 +#define au_fclr_fillvdir(flags, name) \
28952 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
28953 +
28954 +#ifndef CONFIG_AUFS_SHWH
28955 +#undef AuFillVdir_SHWH
28956 +#define AuFillVdir_SHWH                0
28957 +#endif
28958 +
28959 +struct fillvdir_arg {
28960 +       struct dir_context      ctx;
28961 +       struct file             *file;
28962 +       struct au_vdir          *vdir;
28963 +       struct au_nhash         delist;
28964 +       struct au_nhash         whlist;
28965 +       aufs_bindex_t           bindex;
28966 +       unsigned int            flags;
28967 +       int                     err;
28968 +};
28969 +
28970 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
28971 +                   loff_t offset __maybe_unused, u64 h_ino,
28972 +                   unsigned int d_type)
28973 +{
28974 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
28975 +       char *name = (void *)__name;
28976 +       struct super_block *sb;
28977 +       ino_t ino;
28978 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
28979 +
28980 +       arg->err = 0;
28981 +       sb = arg->file->f_path.dentry->d_sb;
28982 +       au_fset_fillvdir(arg->flags, CALLED);
28983 +       /* smp_mb(); */
28984 +       if (nlen <= AUFS_WH_PFX_LEN
28985 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
28986 +               if (test_known(&arg->delist, name, nlen)
28987 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
28988 +                       goto out; /* already exists or whiteouted */
28989 +
28990 +               sb = arg->file->f_path.dentry->d_sb;
28991 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
28992 +               if (!arg->err) {
28993 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
28994 +                               d_type = DT_UNKNOWN;
28995 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
28996 +                                            d_type, &arg->delist);
28997 +               }
28998 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
28999 +               name += AUFS_WH_PFX_LEN;
29000 +               nlen -= AUFS_WH_PFX_LEN;
29001 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
29002 +                       goto out; /* already whiteouted */
29003 +
29004 +               if (shwh)
29005 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
29006 +                                            &ino);
29007 +               if (!arg->err) {
29008 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
29009 +                               d_type = DT_UNKNOWN;
29010 +                       arg->err = au_nhash_append_wh
29011 +                               (&arg->whlist, name, nlen, ino, d_type,
29012 +                                arg->bindex, shwh);
29013 +               }
29014 +       }
29015 +
29016 +out:
29017 +       if (!arg->err)
29018 +               arg->vdir->vd_jiffy = jiffies;
29019 +       /* smp_mb(); */
29020 +       AuTraceErr(arg->err);
29021 +       return arg->err;
29022 +}
29023 +
29024 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
29025 +                         struct au_nhash *whlist, struct au_nhash *delist)
29026 +{
29027 +#ifdef CONFIG_AUFS_SHWH
29028 +       int err;
29029 +       unsigned int nh, u;
29030 +       struct hlist_head *head;
29031 +       struct au_vdir_wh *pos;
29032 +       struct hlist_node *n;
29033 +       char *p, *o;
29034 +       struct au_vdir_destr *destr;
29035 +
29036 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
29037 +
29038 +       err = -ENOMEM;
29039 +       o = p = (void *)__get_free_page(GFP_NOFS);
29040 +       if (unlikely(!p))
29041 +               goto out;
29042 +
29043 +       err = 0;
29044 +       nh = whlist->nh_num;
29045 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29046 +       p += AUFS_WH_PFX_LEN;
29047 +       for (u = 0; u < nh; u++) {
29048 +               head = whlist->nh_head + u;
29049 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
29050 +                       destr = &pos->wh_str;
29051 +                       memcpy(p, destr->name, destr->len);
29052 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
29053 +                                       pos->wh_ino, pos->wh_type, delist);
29054 +                       if (unlikely(err))
29055 +                               break;
29056 +               }
29057 +       }
29058 +
29059 +       free_page((unsigned long)o);
29060 +
29061 +out:
29062 +       AuTraceErr(err);
29063 +       return err;
29064 +#else
29065 +       return 0;
29066 +#endif
29067 +}
29068 +
29069 +static int au_do_read_vdir(struct fillvdir_arg *arg)
29070 +{
29071 +       int err;
29072 +       unsigned int rdhash;
29073 +       loff_t offset;
29074 +       aufs_bindex_t bend, bindex, bstart;
29075 +       unsigned char shwh;
29076 +       struct file *hf, *file;
29077 +       struct super_block *sb;
29078 +
29079 +       file = arg->file;
29080 +       sb = file->f_path.dentry->d_sb;
29081 +       SiMustAnyLock(sb);
29082 +
29083 +       rdhash = au_sbi(sb)->si_rdhash;
29084 +       if (!rdhash)
29085 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
29086 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
29087 +       if (unlikely(err))
29088 +               goto out;
29089 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
29090 +       if (unlikely(err))
29091 +               goto out_delist;
29092 +
29093 +       err = 0;
29094 +       arg->flags = 0;
29095 +       shwh = 0;
29096 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
29097 +               shwh = 1;
29098 +               au_fset_fillvdir(arg->flags, SHWH);
29099 +       }
29100 +       bstart = au_fbstart(file);
29101 +       bend = au_fbend_dir(file);
29102 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
29103 +               hf = au_hf_dir(file, bindex);
29104 +               if (!hf)
29105 +                       continue;
29106 +
29107 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
29108 +               err = offset;
29109 +               if (unlikely(offset))
29110 +                       break;
29111 +
29112 +               arg->bindex = bindex;
29113 +               au_fclr_fillvdir(arg->flags, WHABLE);
29114 +               if (shwh
29115 +                   || (bindex != bend
29116 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
29117 +                       au_fset_fillvdir(arg->flags, WHABLE);
29118 +               do {
29119 +                       arg->err = 0;
29120 +                       au_fclr_fillvdir(arg->flags, CALLED);
29121 +                       /* smp_mb(); */
29122 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
29123 +                       if (err >= 0)
29124 +                               err = arg->err;
29125 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
29126 +
29127 +               /*
29128 +                * dir_relax() may be good for concurrency, but aufs should not
29129 +                * use it since it will cause a lockdep problem.
29130 +                */
29131 +       }
29132 +
29133 +       if (!err && shwh)
29134 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
29135 +
29136 +       au_nhash_wh_free(&arg->whlist);
29137 +
29138 +out_delist:
29139 +       au_nhash_de_free(&arg->delist);
29140 +out:
29141 +       return err;
29142 +}
29143 +
29144 +static int read_vdir(struct file *file, int may_read)
29145 +{
29146 +       int err;
29147 +       unsigned long expire;
29148 +       unsigned char do_read;
29149 +       struct fillvdir_arg arg = {
29150 +               .ctx = {
29151 +                       .actor = fillvdir
29152 +               }
29153 +       };
29154 +       struct inode *inode;
29155 +       struct au_vdir *vdir, *allocated;
29156 +
29157 +       err = 0;
29158 +       inode = file_inode(file);
29159 +       IMustLock(inode);
29160 +       SiMustAnyLock(inode->i_sb);
29161 +
29162 +       allocated = NULL;
29163 +       do_read = 0;
29164 +       expire = au_sbi(inode->i_sb)->si_rdcache;
29165 +       vdir = au_ivdir(inode);
29166 +       if (!vdir) {
29167 +               do_read = 1;
29168 +               vdir = alloc_vdir(file);
29169 +               err = PTR_ERR(vdir);
29170 +               if (IS_ERR(vdir))
29171 +                       goto out;
29172 +               err = 0;
29173 +               allocated = vdir;
29174 +       } else if (may_read
29175 +                  && (inode->i_version != vdir->vd_version
29176 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
29177 +               do_read = 1;
29178 +               err = reinit_vdir(vdir);
29179 +               if (unlikely(err))
29180 +                       goto out;
29181 +       }
29182 +
29183 +       if (!do_read)
29184 +               return 0; /* success */
29185 +
29186 +       arg.file = file;
29187 +       arg.vdir = vdir;
29188 +       err = au_do_read_vdir(&arg);
29189 +       if (!err) {
29190 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
29191 +               vdir->vd_version = inode->i_version;
29192 +               vdir->vd_last.ul = 0;
29193 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29194 +               if (allocated)
29195 +                       au_set_ivdir(inode, allocated);
29196 +       } else if (allocated)
29197 +               au_vdir_free(allocated);
29198 +
29199 +out:
29200 +       return err;
29201 +}
29202 +
29203 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
29204 +{
29205 +       int err, rerr;
29206 +       unsigned long ul, n;
29207 +       const unsigned int deblk_sz = src->vd_deblk_sz;
29208 +
29209 +       AuDebugOn(tgt->vd_nblk != 1);
29210 +
29211 +       err = -ENOMEM;
29212 +       if (tgt->vd_nblk < src->vd_nblk) {
29213 +               unsigned char **p;
29214 +
29215 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
29216 +                            GFP_NOFS);
29217 +               if (unlikely(!p))
29218 +                       goto out;
29219 +               tgt->vd_deblk = p;
29220 +       }
29221 +
29222 +       if (tgt->vd_deblk_sz != deblk_sz) {
29223 +               unsigned char *p;
29224 +
29225 +               tgt->vd_deblk_sz = deblk_sz;
29226 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
29227 +               if (unlikely(!p))
29228 +                       goto out;
29229 +               tgt->vd_deblk[0] = p;
29230 +       }
29231 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
29232 +       tgt->vd_version = src->vd_version;
29233 +       tgt->vd_jiffy = src->vd_jiffy;
29234 +
29235 +       n = src->vd_nblk;
29236 +       for (ul = 1; ul < n; ul++) {
29237 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
29238 +                                           GFP_NOFS);
29239 +               if (unlikely(!tgt->vd_deblk[ul]))
29240 +                       goto out;
29241 +               tgt->vd_nblk++;
29242 +       }
29243 +       tgt->vd_nblk = n;
29244 +       tgt->vd_last.ul = tgt->vd_last.ul;
29245 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
29246 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
29247 +               - src->vd_deblk[src->vd_last.ul];
29248 +       /* smp_mb(); */
29249 +       return 0; /* success */
29250 +
29251 +out:
29252 +       rerr = reinit_vdir(tgt);
29253 +       BUG_ON(rerr);
29254 +       return err;
29255 +}
29256 +
29257 +int au_vdir_init(struct file *file)
29258 +{
29259 +       int err;
29260 +       struct inode *inode;
29261 +       struct au_vdir *vdir_cache, *allocated;
29262 +
29263 +       /* test file->f_pos here instead of ctx->pos */
29264 +       err = read_vdir(file, !file->f_pos);
29265 +       if (unlikely(err))
29266 +               goto out;
29267 +
29268 +       allocated = NULL;
29269 +       vdir_cache = au_fvdir_cache(file);
29270 +       if (!vdir_cache) {
29271 +               vdir_cache = alloc_vdir(file);
29272 +               err = PTR_ERR(vdir_cache);
29273 +               if (IS_ERR(vdir_cache))
29274 +                       goto out;
29275 +               allocated = vdir_cache;
29276 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
29277 +               /* test file->f_pos here instead of ctx->pos */
29278 +               err = reinit_vdir(vdir_cache);
29279 +               if (unlikely(err))
29280 +                       goto out;
29281 +       } else
29282 +               return 0; /* success */
29283 +
29284 +       inode = file_inode(file);
29285 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
29286 +       if (!err) {
29287 +               file->f_version = inode->i_version;
29288 +               if (allocated)
29289 +                       au_set_fvdir_cache(file, allocated);
29290 +       } else if (allocated)
29291 +               au_vdir_free(allocated);
29292 +
29293 +out:
29294 +       return err;
29295 +}
29296 +
29297 +static loff_t calc_offset(struct au_vdir *vdir)
29298 +{
29299 +       loff_t offset;
29300 +       union au_vdir_deblk_p p;
29301 +
29302 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
29303 +       offset = vdir->vd_last.p.deblk - p.deblk;
29304 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
29305 +       return offset;
29306 +}
29307 +
29308 +/* returns true or false */
29309 +static int seek_vdir(struct file *file, struct dir_context *ctx)
29310 +{
29311 +       int valid;
29312 +       unsigned int deblk_sz;
29313 +       unsigned long ul, n;
29314 +       loff_t offset;
29315 +       union au_vdir_deblk_p p, deblk_end;
29316 +       struct au_vdir *vdir_cache;
29317 +
29318 +       valid = 1;
29319 +       vdir_cache = au_fvdir_cache(file);
29320 +       offset = calc_offset(vdir_cache);
29321 +       AuDbg("offset %lld\n", offset);
29322 +       if (ctx->pos == offset)
29323 +               goto out;
29324 +
29325 +       vdir_cache->vd_last.ul = 0;
29326 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
29327 +       if (!ctx->pos)
29328 +               goto out;
29329 +
29330 +       valid = 0;
29331 +       deblk_sz = vdir_cache->vd_deblk_sz;
29332 +       ul = div64_u64(ctx->pos, deblk_sz);
29333 +       AuDbg("ul %lu\n", ul);
29334 +       if (ul >= vdir_cache->vd_nblk)
29335 +               goto out;
29336 +
29337 +       n = vdir_cache->vd_nblk;
29338 +       for (; ul < n; ul++) {
29339 +               p.deblk = vdir_cache->vd_deblk[ul];
29340 +               deblk_end.deblk = p.deblk + deblk_sz;
29341 +               offset = ul;
29342 +               offset *= deblk_sz;
29343 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
29344 +                       unsigned int l;
29345 +
29346 +                       l = calc_size(p.de->de_str.len);
29347 +                       offset += l;
29348 +                       p.deblk += l;
29349 +               }
29350 +               if (!is_deblk_end(&p, &deblk_end)) {
29351 +                       valid = 1;
29352 +                       vdir_cache->vd_last.ul = ul;
29353 +                       vdir_cache->vd_last.p = p;
29354 +                       break;
29355 +               }
29356 +       }
29357 +
29358 +out:
29359 +       /* smp_mb(); */
29360 +       AuTraceErr(!valid);
29361 +       return valid;
29362 +}
29363 +
29364 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
29365 +{
29366 +       unsigned int l, deblk_sz;
29367 +       union au_vdir_deblk_p deblk_end;
29368 +       struct au_vdir *vdir_cache;
29369 +       struct au_vdir_de *de;
29370 +
29371 +       vdir_cache = au_fvdir_cache(file);
29372 +       if (!seek_vdir(file, ctx))
29373 +               return 0;
29374 +
29375 +       deblk_sz = vdir_cache->vd_deblk_sz;
29376 +       while (1) {
29377 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
29378 +               deblk_end.deblk += deblk_sz;
29379 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
29380 +                       de = vdir_cache->vd_last.p.de;
29381 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
29382 +                             de->de_str.len, de->de_str.name, ctx->pos,
29383 +                             (unsigned long)de->de_ino, de->de_type);
29384 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
29385 +                                              de->de_str.len, de->de_ino,
29386 +                                              de->de_type))) {
29387 +                               /* todo: ignore the error caused by udba? */
29388 +                               /* return err; */
29389 +                               return 0;
29390 +                       }
29391 +
29392 +                       l = calc_size(de->de_str.len);
29393 +                       vdir_cache->vd_last.p.deblk += l;
29394 +                       ctx->pos += l;
29395 +               }
29396 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
29397 +                       vdir_cache->vd_last.ul++;
29398 +                       vdir_cache->vd_last.p.deblk
29399 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
29400 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
29401 +                       continue;
29402 +               }
29403 +               break;
29404 +       }
29405 +
29406 +       /* smp_mb(); */
29407 +       return 0;
29408 +}
29409 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
29410 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
29411 +++ linux/fs/aufs/vfsub.c       2015-04-13 15:10:20.790157026 +0200
29412 @@ -0,0 +1,795 @@
29413 +/*
29414 + * Copyright (C) 2005-2015 Junjiro R. Okajima
29415 + *
29416 + * This program, aufs is free software; you can redistribute it and/or modify
29417 + * it under the terms of the GNU General Public License as published by
29418 + * the Free Software Foundation; either version 2 of the License, or
29419 + * (at your option) any later version.
29420 + *
29421 + * This program is distributed in the hope that it will be useful,
29422 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29423 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29424 + * GNU General Public License for more details.
29425 + *
29426 + * You should have received a copy of the GNU General Public License
29427 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29428 + */
29429 +
29430 +/*
29431 + * sub-routines for VFS
29432 + */
29433 +
29434 +#include <linux/namei.h>
29435 +#include <linux/security.h>
29436 +#include <linux/splice.h>
29437 +#include "aufs.h"
29438 +
29439 +int vfsub_update_h_iattr(struct path *h_path, int *did)
29440 +{
29441 +       int err;
29442 +       struct kstat st;
29443 +       struct super_block *h_sb;
29444 +
29445 +       /* for remote fs, leave work for its getattr or d_revalidate */
29446 +       /* for bad i_attr fs, handle them in aufs_getattr() */
29447 +       /* still some fs may acquire i_mutex. we need to skip them */
29448 +       err = 0;
29449 +       if (!did)
29450 +               did = &err;
29451 +       h_sb = h_path->dentry->d_sb;
29452 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
29453 +       if (*did)
29454 +               err = vfs_getattr(h_path, &st);
29455 +
29456 +       return err;
29457 +}
29458 +
29459 +/* ---------------------------------------------------------------------- */
29460 +
29461 +struct file *vfsub_dentry_open(struct path *path, int flags)
29462 +{
29463 +       struct file *file;
29464 +
29465 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
29466 +                          current_cred());
29467 +       if (!IS_ERR_OR_NULL(file)
29468 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
29469 +               i_readcount_inc(path->dentry->d_inode);
29470 +
29471 +       return file;
29472 +}
29473 +
29474 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
29475 +{
29476 +       struct file *file;
29477 +
29478 +       lockdep_off();
29479 +       file = filp_open(path,
29480 +                        oflags /* | __FMODE_NONOTIFY */,
29481 +                        mode);
29482 +       lockdep_on();
29483 +       if (IS_ERR(file))
29484 +               goto out;
29485 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
29486 +
29487 +out:
29488 +       return file;
29489 +}
29490 +
29491 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
29492 +{
29493 +       int err;
29494 +
29495 +       err = kern_path(name, flags, path);
29496 +       if (!err && path->dentry->d_inode)
29497 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
29498 +       return err;
29499 +}
29500 +
29501 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
29502 +                                   int len)
29503 +{
29504 +       struct path path = {
29505 +               .mnt = NULL
29506 +       };
29507 +
29508 +       /* VFS checks it too, but by WARN_ON_ONCE() */
29509 +       IMustLock(parent->d_inode);
29510 +
29511 +       path.dentry = lookup_one_len(name, parent, len);
29512 +       if (IS_ERR(path.dentry))
29513 +               goto out;
29514 +       if (path.dentry->d_inode)
29515 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
29516 +
29517 +out:
29518 +       AuTraceErrPtr(path.dentry);
29519 +       return path.dentry;
29520 +}
29521 +
29522 +void vfsub_call_lkup_one(void *args)
29523 +{
29524 +       struct vfsub_lkup_one_args *a = args;
29525 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
29526 +}
29527 +
29528 +/* ---------------------------------------------------------------------- */
29529 +
29530 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
29531 +                                struct dentry *d2, struct au_hinode *hdir2)
29532 +{
29533 +       struct dentry *d;
29534 +
29535 +       lockdep_off();
29536 +       d = lock_rename(d1, d2);
29537 +       lockdep_on();
29538 +       au_hn_suspend(hdir1);
29539 +       if (hdir1 != hdir2)
29540 +               au_hn_suspend(hdir2);
29541 +
29542 +       return d;
29543 +}
29544 +
29545 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
29546 +                        struct dentry *d2, struct au_hinode *hdir2)
29547 +{
29548 +       au_hn_resume(hdir1);
29549 +       if (hdir1 != hdir2)
29550 +               au_hn_resume(hdir2);
29551 +       lockdep_off();
29552 +       unlock_rename(d1, d2);
29553 +       lockdep_on();
29554 +}
29555 +
29556 +/* ---------------------------------------------------------------------- */
29557 +
29558 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
29559 +{
29560 +       int err;
29561 +       struct dentry *d;
29562 +
29563 +       IMustLock(dir);
29564 +
29565 +       d = path->dentry;
29566 +       path->dentry = d->d_parent;
29567 +       err = security_path_mknod(path, d, mode, 0);
29568 +       path->dentry = d;
29569 +       if (unlikely(err))
29570 +               goto out;
29571 +
29572 +       lockdep_off();
29573 +       err = vfs_create(dir, path->dentry, mode, want_excl);
29574 +       lockdep_on();
29575 +       if (!err) {
29576 +               struct path tmp = *path;
29577 +               int did;
29578 +
29579 +               vfsub_update_h_iattr(&tmp, &did);
29580 +               if (did) {
29581 +                       tmp.dentry = path->dentry->d_parent;
29582 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
29583 +               }
29584 +               /*ignore*/
29585 +       }
29586 +
29587 +out:
29588 +       return err;
29589 +}
29590 +
29591 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
29592 +{
29593 +       int err;
29594 +       struct dentry *d;
29595 +
29596 +       IMustLock(dir);
29597 +
29598 +       d = path->dentry;
29599 +       path->dentry = d->d_parent;
29600 +       err = security_path_symlink(path, d, symname);
29601 +       path->dentry = d;
29602 +       if (unlikely(err))
29603 +               goto out;
29604 +
29605 +       lockdep_off();
29606 +       err = vfs_symlink(dir, path->dentry, symname);
29607 +       lockdep_on();
29608 +       if (!err) {
29609 +               struct path tmp = *path;
29610 +               int did;
29611 +
29612 +               vfsub_update_h_iattr(&tmp, &did);
29613 +               if (did) {
29614 +                       tmp.dentry = path->dentry->d_parent;
29615 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
29616 +               }
29617 +               /*ignore*/
29618 +       }
29619 +
29620 +out:
29621 +       return err;
29622 +}
29623 +
29624 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
29625 +{
29626 +       int err;
29627 +       struct dentry *d;
29628 +
29629 +       IMustLock(dir);
29630 +
29631 +       d = path->dentry;
29632 +       path->dentry = d->d_parent;
29633 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
29634 +       path->dentry = d;
29635 +       if (unlikely(err))
29636 +               goto out;
29637 +
29638 +       lockdep_off();
29639 +       err = vfs_mknod(dir, path->dentry, mode, dev);
29640 +       lockdep_on();
29641 +       if (!err) {
29642 +               struct path tmp = *path;
29643 +               int did;
29644 +
29645 +               vfsub_update_h_iattr(&tmp, &did);
29646 +               if (did) {
29647 +                       tmp.dentry = path->dentry->d_parent;
29648 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
29649 +               }
29650 +               /*ignore*/
29651 +       }
29652 +
29653 +out:
29654 +       return err;
29655 +}
29656 +
29657 +static int au_test_nlink(struct inode *inode)
29658 +{
29659 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
29660 +
29661 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
29662 +           || inode->i_nlink < link_max)
29663 +               return 0;
29664 +       return -EMLINK;
29665 +}
29666 +
29667 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
29668 +              struct inode **delegated_inode)
29669 +{
29670 +       int err;
29671 +       struct dentry *d;
29672 +
29673 +       IMustLock(dir);
29674 +
29675 +       err = au_test_nlink(src_dentry->d_inode);
29676 +       if (unlikely(err))
29677 +               return err;
29678 +
29679 +       /* we don't call may_linkat() */
29680 +       d = path->dentry;
29681 +       path->dentry = d->d_parent;
29682 +       err = security_path_link(src_dentry, path, d);
29683 +       path->dentry = d;
29684 +       if (unlikely(err))
29685 +               goto out;
29686 +
29687 +       lockdep_off();
29688 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
29689 +       lockdep_on();
29690 +       if (!err) {
29691 +               struct path tmp = *path;
29692 +               int did;
29693 +
29694 +               /* fuse has different memory inode for the same inumber */
29695 +               vfsub_update_h_iattr(&tmp, &did);
29696 +               if (did) {
29697 +                       tmp.dentry = path->dentry->d_parent;
29698 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
29699 +                       tmp.dentry = src_dentry;
29700 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
29701 +               }
29702 +               /*ignore*/
29703 +       }
29704 +
29705 +out:
29706 +       return err;
29707 +}
29708 +
29709 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
29710 +                struct inode *dir, struct path *path,
29711 +                struct inode **delegated_inode)
29712 +{
29713 +       int err;
29714 +       struct path tmp = {
29715 +               .mnt    = path->mnt
29716 +       };
29717 +       struct dentry *d;
29718 +
29719 +       IMustLock(dir);
29720 +       IMustLock(src_dir);
29721 +
29722 +       d = path->dentry;
29723 +       path->dentry = d->d_parent;
29724 +       tmp.dentry = src_dentry->d_parent;
29725 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
29726 +       path->dentry = d;
29727 +       if (unlikely(err))
29728 +               goto out;
29729 +
29730 +       lockdep_off();
29731 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
29732 +                        delegated_inode, /*flags*/0);
29733 +       lockdep_on();
29734 +       if (!err) {
29735 +               int did;
29736 +
29737 +               tmp.dentry = d->d_parent;
29738 +               vfsub_update_h_iattr(&tmp, &did);
29739 +               if (did) {
29740 +                       tmp.dentry = src_dentry;
29741 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
29742 +                       tmp.dentry = src_dentry->d_parent;
29743 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
29744 +               }
29745 +               /*ignore*/
29746 +       }
29747 +
29748 +out:
29749 +       return err;
29750 +}
29751 +
29752 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
29753 +{
29754 +       int err;
29755 +       struct dentry *d;
29756 +
29757 +       IMustLock(dir);
29758 +
29759 +       d = path->dentry;
29760 +       path->dentry = d->d_parent;
29761 +       err = security_path_mkdir(path, d, mode);
29762 +       path->dentry = d;
29763 +       if (unlikely(err))
29764 +               goto out;
29765 +
29766 +       lockdep_off();
29767 +       err = vfs_mkdir(dir, path->dentry, mode);
29768 +       lockdep_on();
29769 +       if (!err) {
29770 +               struct path tmp = *path;
29771 +               int did;
29772 +
29773 +               vfsub_update_h_iattr(&tmp, &did);
29774 +               if (did) {
29775 +                       tmp.dentry = path->dentry->d_parent;
29776 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
29777 +               }
29778 +               /*ignore*/
29779 +       }
29780 +
29781 +out:
29782 +       return err;
29783 +}
29784 +
29785 +int vfsub_rmdir(struct inode *dir, struct path *path)
29786 +{
29787 +       int err;
29788 +       struct dentry *d;
29789 +
29790 +       IMustLock(dir);
29791 +
29792 +       d = path->dentry;
29793 +       path->dentry = d->d_parent;
29794 +       err = security_path_rmdir(path, d);
29795 +       path->dentry = d;
29796 +       if (unlikely(err))
29797 +               goto out;
29798 +
29799 +       lockdep_off();
29800 +       err = vfs_rmdir(dir, path->dentry);
29801 +       lockdep_on();
29802 +       if (!err) {
29803 +               struct path tmp = {
29804 +                       .dentry = path->dentry->d_parent,
29805 +                       .mnt    = path->mnt
29806 +               };
29807 +
29808 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
29809 +       }
29810 +
29811 +out:
29812 +       return err;
29813 +}
29814 +
29815 +/* ---------------------------------------------------------------------- */
29816 +
29817 +/* todo: support mmap_sem? */
29818 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
29819 +                    loff_t *ppos)
29820 +{
29821 +       ssize_t err;
29822 +
29823 +       lockdep_off();
29824 +       err = vfs_read(file, ubuf, count, ppos);
29825 +       lockdep_on();
29826 +       if (err >= 0)
29827 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
29828 +       return err;
29829 +}
29830 +
29831 +/* todo: kernel_read()? */
29832 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
29833 +                    loff_t *ppos)
29834 +{
29835 +       ssize_t err;
29836 +       mm_segment_t oldfs;
29837 +       union {
29838 +               void *k;
29839 +               char __user *u;
29840 +       } buf;
29841 +
29842 +       buf.k = kbuf;
29843 +       oldfs = get_fs();
29844 +       set_fs(KERNEL_DS);
29845 +       err = vfsub_read_u(file, buf.u, count, ppos);
29846 +       set_fs(oldfs);
29847 +       return err;
29848 +}
29849 +
29850 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
29851 +                     loff_t *ppos)
29852 +{
29853 +       ssize_t err;
29854 +
29855 +       lockdep_off();
29856 +       err = vfs_write(file, ubuf, count, ppos);
29857 +       lockdep_on();
29858 +       if (err >= 0)
29859 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
29860 +       return err;
29861 +}
29862 +
29863 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
29864 +{
29865 +       ssize_t err;
29866 +       mm_segment_t oldfs;
29867 +       union {
29868 +               void *k;
29869 +               const char __user *u;
29870 +       } buf;
29871 +
29872 +       buf.k = kbuf;
29873 +       oldfs = get_fs();
29874 +       set_fs(KERNEL_DS);
29875 +       err = vfsub_write_u(file, buf.u, count, ppos);
29876 +       set_fs(oldfs);
29877 +       return err;
29878 +}
29879 +
29880 +int vfsub_flush(struct file *file, fl_owner_t id)
29881 +{
29882 +       int err;
29883 +
29884 +       err = 0;
29885 +       if (file->f_op->flush) {
29886 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
29887 +                       err = file->f_op->flush(file, id);
29888 +               else {
29889 +                       lockdep_off();
29890 +                       err = file->f_op->flush(file, id);
29891 +                       lockdep_on();
29892 +               }
29893 +               if (!err)
29894 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
29895 +               /*ignore*/
29896 +       }
29897 +       return err;
29898 +}
29899 +
29900 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
29901 +{
29902 +       int err;
29903 +
29904 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
29905 +
29906 +       lockdep_off();
29907 +       err = iterate_dir(file, ctx);
29908 +       lockdep_on();
29909 +       if (err >= 0)
29910 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
29911 +       return err;
29912 +}
29913 +
29914 +long vfsub_splice_to(struct file *in, loff_t *ppos,
29915 +                    struct pipe_inode_info *pipe, size_t len,
29916 +                    unsigned int flags)
29917 +{
29918 +       long err;
29919 +
29920 +       lockdep_off();
29921 +       err = do_splice_to(in, ppos, pipe, len, flags);
29922 +       lockdep_on();
29923 +       file_accessed(in);
29924 +       if (err >= 0)
29925 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
29926 +       return err;
29927 +}
29928 +
29929 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
29930 +                      loff_t *ppos, size_t len, unsigned int flags)
29931 +{
29932 +       long err;
29933 +
29934 +       lockdep_off();
29935 +       err = do_splice_from(pipe, out, ppos, len, flags);
29936 +       lockdep_on();
29937 +       if (err >= 0)
29938 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
29939 +       return err;
29940 +}
29941 +
29942 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
29943 +{
29944 +       int err;
29945 +
29946 +       /* file can be NULL */
29947 +       lockdep_off();
29948 +       err = vfs_fsync(file, datasync);
29949 +       lockdep_on();
29950 +       if (!err) {
29951 +               if (!path) {
29952 +                       AuDebugOn(!file);
29953 +                       path = &file->f_path;
29954 +               }
29955 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
29956 +       }
29957 +       return err;
29958 +}
29959 +
29960 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
29961 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
29962 +               struct file *h_file)
29963 +{
29964 +       int err;
29965 +       struct inode *h_inode;
29966 +       struct super_block *h_sb;
29967 +
29968 +       if (!h_file) {
29969 +               err = vfsub_truncate(h_path, length);
29970 +               goto out;
29971 +       }
29972 +
29973 +       h_inode = h_path->dentry->d_inode;
29974 +       h_sb = h_inode->i_sb;
29975 +       lockdep_off();
29976 +       sb_start_write(h_sb);
29977 +       lockdep_on();
29978 +       err = locks_verify_truncate(h_inode, h_file, length);
29979 +       if (!err)
29980 +               err = security_path_truncate(h_path);
29981 +       if (!err) {
29982 +               lockdep_off();
29983 +               err = do_truncate(h_path->dentry, length, attr, h_file);
29984 +               lockdep_on();
29985 +       }
29986 +       lockdep_off();
29987 +       sb_end_write(h_sb);
29988 +       lockdep_on();
29989 +
29990 +out:
29991 +       return err;
29992 +}
29993 +
29994 +/* ---------------------------------------------------------------------- */
29995 +
29996 +struct au_vfsub_mkdir_args {
29997 +       int *errp;
29998 +       struct inode *dir;
29999 +       struct path *path;
30000 +       int mode;
30001 +};
30002 +
30003 +static void au_call_vfsub_mkdir(void *args)
30004 +{
30005 +       struct au_vfsub_mkdir_args *a = args;
30006 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
30007 +}
30008 +
30009 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
30010 +{
30011 +       int err, do_sio, wkq_err;
30012 +
30013 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30014 +       if (!do_sio) {
30015 +               lockdep_off();
30016 +               err = vfsub_mkdir(dir, path, mode);
30017 +               lockdep_on();
30018 +       } else {
30019 +               struct au_vfsub_mkdir_args args = {
30020 +                       .errp   = &err,
30021 +                       .dir    = dir,
30022 +                       .path   = path,
30023 +                       .mode   = mode
30024 +               };
30025 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
30026 +               if (unlikely(wkq_err))
30027 +                       err = wkq_err;
30028 +       }
30029 +
30030 +       return err;
30031 +}
30032 +
30033 +struct au_vfsub_rmdir_args {
30034 +       int *errp;
30035 +       struct inode *dir;
30036 +       struct path *path;
30037 +};
30038 +
30039 +static void au_call_vfsub_rmdir(void *args)
30040 +{
30041 +       struct au_vfsub_rmdir_args *a = args;
30042 +       *a->errp = vfsub_rmdir(a->dir, a->path);
30043 +}
30044 +
30045 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
30046 +{
30047 +       int err, do_sio, wkq_err;
30048 +
30049 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30050 +       if (!do_sio) {
30051 +               lockdep_off();
30052 +               err = vfsub_rmdir(dir, path);
30053 +               lockdep_on();
30054 +       } else {
30055 +               struct au_vfsub_rmdir_args args = {
30056 +                       .errp   = &err,
30057 +                       .dir    = dir,
30058 +                       .path   = path
30059 +               };
30060 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
30061 +               if (unlikely(wkq_err))
30062 +                       err = wkq_err;
30063 +       }
30064 +
30065 +       return err;
30066 +}
30067 +
30068 +/* ---------------------------------------------------------------------- */
30069 +
30070 +struct notify_change_args {
30071 +       int *errp;
30072 +       struct path *path;
30073 +       struct iattr *ia;
30074 +       struct inode **delegated_inode;
30075 +};
30076 +
30077 +static void call_notify_change(void *args)
30078 +{
30079 +       struct notify_change_args *a = args;
30080 +       struct inode *h_inode;
30081 +
30082 +       h_inode = a->path->dentry->d_inode;
30083 +       IMustLock(h_inode);
30084 +
30085 +       *a->errp = -EPERM;
30086 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
30087 +               lockdep_off();
30088 +               *a->errp = notify_change(a->path->dentry, a->ia,
30089 +                                        a->delegated_inode);
30090 +               lockdep_on();
30091 +               if (!*a->errp)
30092 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
30093 +       }
30094 +       AuTraceErr(*a->errp);
30095 +}
30096 +
30097 +int vfsub_notify_change(struct path *path, struct iattr *ia,
30098 +                       struct inode **delegated_inode)
30099 +{
30100 +       int err;
30101 +       struct notify_change_args args = {
30102 +               .errp                   = &err,
30103 +               .path                   = path,
30104 +               .ia                     = ia,
30105 +               .delegated_inode        = delegated_inode
30106 +       };
30107 +
30108 +       call_notify_change(&args);
30109 +
30110 +       return err;
30111 +}
30112 +
30113 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
30114 +                           struct inode **delegated_inode)
30115 +{
30116 +       int err, wkq_err;
30117 +       struct notify_change_args args = {
30118 +               .errp                   = &err,
30119 +               .path                   = path,
30120 +               .ia                     = ia,
30121 +               .delegated_inode        = delegated_inode
30122 +       };
30123 +
30124 +       wkq_err = au_wkq_wait(call_notify_change, &args);
30125 +       if (unlikely(wkq_err))
30126 +               err = wkq_err;
30127 +
30128 +       return err;
30129 +}
30130 +
30131 +/* ---------------------------------------------------------------------- */
30132 +
30133 +struct unlink_args {
30134 +       int *errp;
30135 +       struct inode *dir;
30136 +       struct path *path;
30137 +       struct inode **delegated_inode;
30138 +};
30139 +
30140 +static void call_unlink(void *args)
30141 +{
30142 +       struct unlink_args *a = args;
30143 +       struct dentry *d = a->path->dentry;
30144 +       struct inode *h_inode;
30145 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
30146 +                                     && au_dcount(d) == 1);
30147 +
30148 +       IMustLock(a->dir);
30149 +
30150 +       a->path->dentry = d->d_parent;
30151 +       *a->errp = security_path_unlink(a->path, d);
30152 +       a->path->dentry = d;
30153 +       if (unlikely(*a->errp))
30154 +               return;
30155 +
30156 +       if (!stop_sillyrename)
30157 +               dget(d);
30158 +       h_inode = d->d_inode;
30159 +       if (h_inode)
30160 +               ihold(h_inode);
30161 +
30162 +       lockdep_off();
30163 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
30164 +       lockdep_on();
30165 +       if (!*a->errp) {
30166 +               struct path tmp = {
30167 +                       .dentry = d->d_parent,
30168 +                       .mnt    = a->path->mnt
30169 +               };
30170 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30171 +       }
30172 +
30173 +       if (!stop_sillyrename)
30174 +               dput(d);
30175 +       if (h_inode)
30176 +               iput(h_inode);
30177 +
30178 +       AuTraceErr(*a->errp);
30179 +}
30180 +
30181 +/*
30182 + * @dir: must be locked.
30183 + * @dentry: target dentry.
30184 + */
30185 +int vfsub_unlink(struct inode *dir, struct path *path,
30186 +                struct inode **delegated_inode, int force)
30187 +{
30188 +       int err;
30189 +       struct unlink_args args = {
30190 +               .errp                   = &err,
30191 +               .dir                    = dir,
30192 +               .path                   = path,
30193 +               .delegated_inode        = delegated_inode
30194 +       };
30195 +
30196 +       if (!force)
30197 +               call_unlink(&args);
30198 +       else {
30199 +               int wkq_err;
30200 +
30201 +               wkq_err = au_wkq_wait(call_unlink, &args);
30202 +               if (unlikely(wkq_err))
30203 +                       err = wkq_err;
30204 +       }
30205 +
30206 +       return err;
30207 +}
30208 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
30209 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
30210 +++ linux/fs/aufs/vfsub.h       2015-04-13 15:10:20.790157026 +0200
30211 @@ -0,0 +1,276 @@
30212 +/*
30213 + * Copyright (C) 2005-2015 Junjiro R. Okajima
30214 + *
30215 + * This program, aufs is free software; you can redistribute it and/or modify
30216 + * it under the terms of the GNU General Public License as published by
30217 + * the Free Software Foundation; either version 2 of the License, or
30218 + * (at your option) any later version.
30219 + *
30220 + * This program is distributed in the hope that it will be useful,
30221 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30222 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30223 + * GNU General Public License for more details.
30224 + *
30225 + * You should have received a copy of the GNU General Public License
30226 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30227 + */
30228 +
30229 +/*
30230 + * sub-routines for VFS
30231 + */
30232 +
30233 +#ifndef __AUFS_VFSUB_H__
30234 +#define __AUFS_VFSUB_H__
30235 +
30236 +#ifdef __KERNEL__
30237 +
30238 +#include <linux/fs.h>
30239 +#include <linux/mount.h>
30240 +#include <linux/xattr.h>
30241 +#include "debug.h"
30242 +
30243 +/* copied from linux/fs/internal.h */
30244 +/* todo: BAD approach!! */
30245 +extern void __mnt_drop_write(struct vfsmount *);
30246 +extern spinlock_t inode_sb_list_lock;
30247 +
30248 +/* ---------------------------------------------------------------------- */
30249 +
30250 +/* lock subclass for lower inode */
30251 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
30252 +/* reduce? gave up. */
30253 +enum {
30254 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
30255 +       AuLsc_I_PARENT,         /* lower inode, parent first */
30256 +       AuLsc_I_PARENT2,        /* copyup dirs */
30257 +       AuLsc_I_PARENT3,        /* copyup wh */
30258 +       AuLsc_I_CHILD,
30259 +       AuLsc_I_CHILD2,
30260 +       AuLsc_I_End
30261 +};
30262 +
30263 +/* to debug easier, do not make them inlined functions */
30264 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
30265 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
30266 +
30267 +/* ---------------------------------------------------------------------- */
30268 +
30269 +static inline void vfsub_drop_nlink(struct inode *inode)
30270 +{
30271 +       AuDebugOn(!inode->i_nlink);
30272 +       drop_nlink(inode);
30273 +}
30274 +
30275 +static inline void vfsub_dead_dir(struct inode *inode)
30276 +{
30277 +       AuDebugOn(!S_ISDIR(inode->i_mode));
30278 +       inode->i_flags |= S_DEAD;
30279 +       clear_nlink(inode);
30280 +}
30281 +
30282 +static inline int vfsub_native_ro(struct inode *inode)
30283 +{
30284 +       return (inode->i_sb->s_flags & MS_RDONLY)
30285 +               || IS_RDONLY(inode)
30286 +               /* || IS_APPEND(inode) */
30287 +               || IS_IMMUTABLE(inode);
30288 +}
30289 +
30290 +/* ---------------------------------------------------------------------- */
30291 +
30292 +int vfsub_update_h_iattr(struct path *h_path, int *did);
30293 +struct file *vfsub_dentry_open(struct path *path, int flags);
30294 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
30295 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
30296 +
30297 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30298 +                                   int len);
30299 +
30300 +struct vfsub_lkup_one_args {
30301 +       struct dentry **errp;
30302 +       struct qstr *name;
30303 +       struct dentry *parent;
30304 +};
30305 +
30306 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
30307 +                                           struct dentry *parent)
30308 +{
30309 +       return vfsub_lookup_one_len(name->name, parent, name->len);
30310 +}
30311 +
30312 +void vfsub_call_lkup_one(void *args);
30313 +
30314 +/* ---------------------------------------------------------------------- */
30315 +
30316 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
30317 +{
30318 +       int err;
30319 +
30320 +       lockdep_off();
30321 +       err = mnt_want_write(mnt);
30322 +       lockdep_on();
30323 +       return err;
30324 +}
30325 +
30326 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
30327 +{
30328 +       lockdep_off();
30329 +       mnt_drop_write(mnt);
30330 +       lockdep_on();
30331 +}
30332 +
30333 +#if 0 /* reserved */
30334 +static inline void vfsub_mnt_drop_write_file(struct file *file)
30335 +{
30336 +       lockdep_off();
30337 +       mnt_drop_write_file(file);
30338 +       lockdep_on();
30339 +}
30340 +#endif
30341 +
30342 +/* ---------------------------------------------------------------------- */
30343 +
30344 +struct au_hinode;
30345 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30346 +                                struct dentry *d2, struct au_hinode *hdir2);
30347 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30348 +                        struct dentry *d2, struct au_hinode *hdir2);
30349 +
30350 +int vfsub_create(struct inode *dir, struct path *path, int mode,
30351 +                bool want_excl);
30352 +int vfsub_symlink(struct inode *dir, struct path *path,
30353 +                 const char *symname);
30354 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
30355 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
30356 +              struct path *path, struct inode **delegated_inode);
30357 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
30358 +                struct inode *hdir, struct path *path,
30359 +                struct inode **delegated_inode);
30360 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
30361 +int vfsub_rmdir(struct inode *dir, struct path *path);
30362 +
30363 +/* ---------------------------------------------------------------------- */
30364 +
30365 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
30366 +                    loff_t *ppos);
30367 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
30368 +                       loff_t *ppos);
30369 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
30370 +                     loff_t *ppos);
30371 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
30372 +                     loff_t *ppos);
30373 +int vfsub_flush(struct file *file, fl_owner_t id);
30374 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
30375 +
30376 +static inline loff_t vfsub_f_size_read(struct file *file)
30377 +{
30378 +       return i_size_read(file_inode(file));
30379 +}
30380 +
30381 +static inline unsigned int vfsub_file_flags(struct file *file)
30382 +{
30383 +       unsigned int flags;
30384 +
30385 +       spin_lock(&file->f_lock);
30386 +       flags = file->f_flags;
30387 +       spin_unlock(&file->f_lock);
30388 +
30389 +       return flags;
30390 +}
30391 +
30392 +#if 0 /* reserved */
30393 +static inline void vfsub_file_accessed(struct file *h_file)
30394 +{
30395 +       file_accessed(h_file);
30396 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
30397 +}
30398 +#endif
30399 +
30400 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
30401 +                                    struct dentry *h_dentry)
30402 +{
30403 +       struct path h_path = {
30404 +               .dentry = h_dentry,
30405 +               .mnt    = h_mnt
30406 +       };
30407 +       touch_atime(&h_path);
30408 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
30409 +}
30410 +
30411 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
30412 +                                   int flags)
30413 +{
30414 +       return generic_update_time(h_inode, ts, flags);
30415 +       /* no vfsub_update_h_iattr() since we don't have struct path */
30416 +}
30417 +
30418 +long vfsub_splice_to(struct file *in, loff_t *ppos,
30419 +                    struct pipe_inode_info *pipe, size_t len,
30420 +                    unsigned int flags);
30421 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
30422 +                      loff_t *ppos, size_t len, unsigned int flags);
30423 +
30424 +static inline long vfsub_truncate(struct path *path, loff_t length)
30425 +{
30426 +       long err;
30427 +
30428 +       lockdep_off();
30429 +       err = vfs_truncate(path, length);
30430 +       lockdep_on();
30431 +       return err;
30432 +}
30433 +
30434 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
30435 +               struct file *h_file);
30436 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
30437 +
30438 +/* ---------------------------------------------------------------------- */
30439 +
30440 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
30441 +{
30442 +       loff_t err;
30443 +
30444 +       lockdep_off();
30445 +       err = vfs_llseek(file, offset, origin);
30446 +       lockdep_on();
30447 +       return err;
30448 +}
30449 +
30450 +/* ---------------------------------------------------------------------- */
30451 +
30452 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
30453 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
30454 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
30455 +                           struct inode **delegated_inode);
30456 +int vfsub_notify_change(struct path *path, struct iattr *ia,
30457 +                       struct inode **delegated_inode);
30458 +int vfsub_unlink(struct inode *dir, struct path *path,
30459 +                struct inode **delegated_inode, int force);
30460 +
30461 +/* ---------------------------------------------------------------------- */
30462 +
30463 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
30464 +                                const void *value, size_t size, int flags)
30465 +{
30466 +       int err;
30467 +
30468 +       lockdep_off();
30469 +       err = vfs_setxattr(dentry, name, value, size, flags);
30470 +       lockdep_on();
30471 +
30472 +       return err;
30473 +}
30474 +
30475 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
30476 +{
30477 +       int err;
30478 +
30479 +       lockdep_off();
30480 +       err = vfs_removexattr(dentry, name);
30481 +       lockdep_on();
30482 +
30483 +       return err;
30484 +}
30485 +
30486 +#endif /* __KERNEL__ */
30487 +#endif /* __AUFS_VFSUB_H__ */
30488 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
30489 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
30490 +++ linux/fs/aufs/wbr_policy.c  2015-04-13 15:10:20.790157026 +0200
30491 @@ -0,0 +1,765 @@
30492 +/*
30493 + * Copyright (C) 2005-2015 Junjiro R. Okajima
30494 + *
30495 + * This program, aufs is free software; you can redistribute it and/or modify
30496 + * it under the terms of the GNU General Public License as published by
30497 + * the Free Software Foundation; either version 2 of the License, or
30498 + * (at your option) any later version.
30499 + *
30500 + * This program is distributed in the hope that it will be useful,
30501 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30502 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30503 + * GNU General Public License for more details.
30504 + *
30505 + * You should have received a copy of the GNU General Public License
30506 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30507 + */
30508 +
30509 +/*
30510 + * policies for selecting one among multiple writable branches
30511 + */
30512 +
30513 +#include <linux/statfs.h>
30514 +#include "aufs.h"
30515 +
30516 +/* subset of cpup_attr() */
30517 +static noinline_for_stack
30518 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
30519 +{
30520 +       int err, sbits;
30521 +       struct iattr ia;
30522 +       struct inode *h_isrc;
30523 +
30524 +       h_isrc = h_src->d_inode;
30525 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
30526 +       ia.ia_mode = h_isrc->i_mode;
30527 +       ia.ia_uid = h_isrc->i_uid;
30528 +       ia.ia_gid = h_isrc->i_gid;
30529 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
30530 +       au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc->i_flags);
30531 +       /* no delegation since it is just created */
30532 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
30533 +
30534 +       /* is this nfs only? */
30535 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
30536 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
30537 +               ia.ia_mode = h_isrc->i_mode;
30538 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
30539 +       }
30540 +
30541 +       return err;
30542 +}
30543 +
30544 +#define AuCpdown_PARENT_OPQ    1
30545 +#define AuCpdown_WHED          (1 << 1)
30546 +#define AuCpdown_MADE_DIR      (1 << 2)
30547 +#define AuCpdown_DIROPQ                (1 << 3)
30548 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
30549 +#define au_fset_cpdown(flags, name) \
30550 +       do { (flags) |= AuCpdown_##name; } while (0)
30551 +#define au_fclr_cpdown(flags, name) \
30552 +       do { (flags) &= ~AuCpdown_##name; } while (0)
30553 +
30554 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
30555 +                            unsigned int *flags)
30556 +{
30557 +       int err;
30558 +       struct dentry *opq_dentry;
30559 +
30560 +       opq_dentry = au_diropq_create(dentry, bdst);
30561 +       err = PTR_ERR(opq_dentry);
30562 +       if (IS_ERR(opq_dentry))
30563 +               goto out;
30564 +       dput(opq_dentry);
30565 +       au_fset_cpdown(*flags, DIROPQ);
30566 +
30567 +out:
30568 +       return err;
30569 +}
30570 +
30571 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
30572 +                           struct inode *dir, aufs_bindex_t bdst)
30573 +{
30574 +       int err;
30575 +       struct path h_path;
30576 +       struct au_branch *br;
30577 +
30578 +       br = au_sbr(dentry->d_sb, bdst);
30579 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
30580 +       err = PTR_ERR(h_path.dentry);
30581 +       if (IS_ERR(h_path.dentry))
30582 +               goto out;
30583 +
30584 +       err = 0;
30585 +       if (h_path.dentry->d_inode) {
30586 +               h_path.mnt = au_br_mnt(br);
30587 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
30588 +                                         dentry);
30589 +       }
30590 +       dput(h_path.dentry);
30591 +
30592 +out:
30593 +       return err;
30594 +}
30595 +
30596 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
30597 +                        struct au_pin *pin,
30598 +                        struct dentry *h_parent, void *arg)
30599 +{
30600 +       int err, rerr;
30601 +       aufs_bindex_t bopq, bstart;
30602 +       struct path h_path;
30603 +       struct dentry *parent;
30604 +       struct inode *h_dir, *h_inode, *inode, *dir;
30605 +       unsigned int *flags = arg;
30606 +
30607 +       bstart = au_dbstart(dentry);
30608 +       /* dentry is di-locked */
30609 +       parent = dget_parent(dentry);
30610 +       dir = parent->d_inode;
30611 +       h_dir = h_parent->d_inode;
30612 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
30613 +       IMustLock(h_dir);
30614 +
30615 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
30616 +       if (unlikely(err < 0))
30617 +               goto out;
30618 +       h_path.dentry = au_h_dptr(dentry, bdst);
30619 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
30620 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
30621 +                             S_IRWXU | S_IRUGO | S_IXUGO);
30622 +       if (unlikely(err))
30623 +               goto out_put;
30624 +       au_fset_cpdown(*flags, MADE_DIR);
30625 +
30626 +       bopq = au_dbdiropq(dentry);
30627 +       au_fclr_cpdown(*flags, WHED);
30628 +       au_fclr_cpdown(*flags, DIROPQ);
30629 +       if (au_dbwh(dentry) == bdst)
30630 +               au_fset_cpdown(*flags, WHED);
30631 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
30632 +               au_fset_cpdown(*flags, PARENT_OPQ);
30633 +       h_inode = h_path.dentry->d_inode;
30634 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
30635 +       if (au_ftest_cpdown(*flags, WHED)) {
30636 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
30637 +               if (unlikely(err)) {
30638 +                       mutex_unlock(&h_inode->i_mutex);
30639 +                       goto out_dir;
30640 +               }
30641 +       }
30642 +
30643 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
30644 +       mutex_unlock(&h_inode->i_mutex);
30645 +       if (unlikely(err))
30646 +               goto out_opq;
30647 +
30648 +       if (au_ftest_cpdown(*flags, WHED)) {
30649 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
30650 +               if (unlikely(err))
30651 +                       goto out_opq;
30652 +       }
30653 +
30654 +       inode = dentry->d_inode;
30655 +       if (au_ibend(inode) < bdst)
30656 +               au_set_ibend(inode, bdst);
30657 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
30658 +                     au_hi_flags(inode, /*isdir*/1));
30659 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
30660 +       goto out; /* success */
30661 +
30662 +       /* revert */
30663 +out_opq:
30664 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
30665 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
30666 +               rerr = au_diropq_remove(dentry, bdst);
30667 +               mutex_unlock(&h_inode->i_mutex);
30668 +               if (unlikely(rerr)) {
30669 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
30670 +                               dentry, bdst, rerr);
30671 +                       err = -EIO;
30672 +                       goto out;
30673 +               }
30674 +       }
30675 +out_dir:
30676 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
30677 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
30678 +               if (unlikely(rerr)) {
30679 +                       AuIOErr("failed removing %pd b%d (%d)\n",
30680 +                               dentry, bdst, rerr);
30681 +                       err = -EIO;
30682 +               }
30683 +       }
30684 +out_put:
30685 +       au_set_h_dptr(dentry, bdst, NULL);
30686 +       if (au_dbend(dentry) == bdst)
30687 +               au_update_dbend(dentry);
30688 +out:
30689 +       dput(parent);
30690 +       return err;
30691 +}
30692 +
30693 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
30694 +{
30695 +       int err;
30696 +       unsigned int flags;
30697 +
30698 +       flags = 0;
30699 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
30700 +
30701 +       return err;
30702 +}
30703 +
30704 +/* ---------------------------------------------------------------------- */
30705 +
30706 +/* policies for create */
30707 +
30708 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
30709 +{
30710 +       int err, i, j, ndentry;
30711 +       aufs_bindex_t bopq;
30712 +       struct au_dcsub_pages dpages;
30713 +       struct au_dpage *dpage;
30714 +       struct dentry **dentries, *parent, *d;
30715 +
30716 +       err = au_dpages_init(&dpages, GFP_NOFS);
30717 +       if (unlikely(err))
30718 +               goto out;
30719 +       parent = dget_parent(dentry);
30720 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
30721 +       if (unlikely(err))
30722 +               goto out_free;
30723 +
30724 +       err = bindex;
30725 +       for (i = 0; i < dpages.ndpage; i++) {
30726 +               dpage = dpages.dpages + i;
30727 +               dentries = dpage->dentries;
30728 +               ndentry = dpage->ndentry;
30729 +               for (j = 0; j < ndentry; j++) {
30730 +                       d = dentries[j];
30731 +                       di_read_lock_parent2(d, !AuLock_IR);
30732 +                       bopq = au_dbdiropq(d);
30733 +                       di_read_unlock(d, !AuLock_IR);
30734 +                       if (bopq >= 0 && bopq < err)
30735 +                               err = bopq;
30736 +               }
30737 +       }
30738 +
30739 +out_free:
30740 +       dput(parent);
30741 +       au_dpages_free(&dpages);
30742 +out:
30743 +       return err;
30744 +}
30745 +
30746 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
30747 +{
30748 +       for (; bindex >= 0; bindex--)
30749 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
30750 +                       return bindex;
30751 +       return -EROFS;
30752 +}
30753 +
30754 +/* top down parent */
30755 +static int au_wbr_create_tdp(struct dentry *dentry,
30756 +                            unsigned int flags __maybe_unused)
30757 +{
30758 +       int err;
30759 +       aufs_bindex_t bstart, bindex;
30760 +       struct super_block *sb;
30761 +       struct dentry *parent, *h_parent;
30762 +
30763 +       sb = dentry->d_sb;
30764 +       bstart = au_dbstart(dentry);
30765 +       err = bstart;
30766 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
30767 +               goto out;
30768 +
30769 +       err = -EROFS;
30770 +       parent = dget_parent(dentry);
30771 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
30772 +               h_parent = au_h_dptr(parent, bindex);
30773 +               if (!h_parent || !h_parent->d_inode)
30774 +                       continue;
30775 +
30776 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
30777 +                       err = bindex;
30778 +                       break;
30779 +               }
30780 +       }
30781 +       dput(parent);
30782 +
30783 +       /* bottom up here */
30784 +       if (unlikely(err < 0)) {
30785 +               err = au_wbr_bu(sb, bstart - 1);
30786 +               if (err >= 0)
30787 +                       err = au_wbr_nonopq(dentry, err);
30788 +       }
30789 +
30790 +out:
30791 +       AuDbg("b%d\n", err);
30792 +       return err;
30793 +}
30794 +
30795 +/* ---------------------------------------------------------------------- */
30796 +
30797 +/* an exception for the policy other than tdp */
30798 +static int au_wbr_create_exp(struct dentry *dentry)
30799 +{
30800 +       int err;
30801 +       aufs_bindex_t bwh, bdiropq;
30802 +       struct dentry *parent;
30803 +
30804 +       err = -1;
30805 +       bwh = au_dbwh(dentry);
30806 +       parent = dget_parent(dentry);
30807 +       bdiropq = au_dbdiropq(parent);
30808 +       if (bwh >= 0) {
30809 +               if (bdiropq >= 0)
30810 +                       err = min(bdiropq, bwh);
30811 +               else
30812 +                       err = bwh;
30813 +               AuDbg("%d\n", err);
30814 +       } else if (bdiropq >= 0) {
30815 +               err = bdiropq;
30816 +               AuDbg("%d\n", err);
30817 +       }
30818 +       dput(parent);
30819 +
30820 +       if (err >= 0)
30821 +               err = au_wbr_nonopq(dentry, err);
30822 +
30823 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
30824 +               err = -1;
30825 +
30826 +       AuDbg("%d\n", err);
30827 +       return err;
30828 +}
30829 +
30830 +/* ---------------------------------------------------------------------- */
30831 +
30832 +/* round robin */
30833 +static int au_wbr_create_init_rr(struct super_block *sb)
30834 +{
30835 +       int err;
30836 +
30837 +       err = au_wbr_bu(sb, au_sbend(sb));
30838 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
30839 +       /* smp_mb(); */
30840 +
30841 +       AuDbg("b%d\n", err);
30842 +       return err;
30843 +}
30844 +
30845 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
30846 +{
30847 +       int err, nbr;
30848 +       unsigned int u;
30849 +       aufs_bindex_t bindex, bend;
30850 +       struct super_block *sb;
30851 +       atomic_t *next;
30852 +
30853 +       err = au_wbr_create_exp(dentry);
30854 +       if (err >= 0)
30855 +               goto out;
30856 +
30857 +       sb = dentry->d_sb;
30858 +       next = &au_sbi(sb)->si_wbr_rr_next;
30859 +       bend = au_sbend(sb);
30860 +       nbr = bend + 1;
30861 +       for (bindex = 0; bindex <= bend; bindex++) {
30862 +               if (!au_ftest_wbr(flags, DIR)) {
30863 +                       err = atomic_dec_return(next) + 1;
30864 +                       /* modulo for 0 is meaningless */
30865 +                       if (unlikely(!err))
30866 +                               err = atomic_dec_return(next) + 1;
30867 +               } else
30868 +                       err = atomic_read(next);
30869 +               AuDbg("%d\n", err);
30870 +               u = err;
30871 +               err = u % nbr;
30872 +               AuDbg("%d\n", err);
30873 +               if (!au_br_rdonly(au_sbr(sb, err)))
30874 +                       break;
30875 +               err = -EROFS;
30876 +       }
30877 +
30878 +       if (err >= 0)
30879 +               err = au_wbr_nonopq(dentry, err);
30880 +
30881 +out:
30882 +       AuDbg("%d\n", err);
30883 +       return err;
30884 +}
30885 +
30886 +/* ---------------------------------------------------------------------- */
30887 +
30888 +/* most free space */
30889 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
30890 +{
30891 +       struct super_block *sb;
30892 +       struct au_branch *br;
30893 +       struct au_wbr_mfs *mfs;
30894 +       struct dentry *h_parent;
30895 +       aufs_bindex_t bindex, bend;
30896 +       int err;
30897 +       unsigned long long b, bavail;
30898 +       struct path h_path;
30899 +       /* reduce the stack usage */
30900 +       struct kstatfs *st;
30901 +
30902 +       st = kmalloc(sizeof(*st), GFP_NOFS);
30903 +       if (unlikely(!st)) {
30904 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
30905 +               return;
30906 +       }
30907 +
30908 +       bavail = 0;
30909 +       sb = dentry->d_sb;
30910 +       mfs = &au_sbi(sb)->si_wbr_mfs;
30911 +       MtxMustLock(&mfs->mfs_lock);
30912 +       mfs->mfs_bindex = -EROFS;
30913 +       mfs->mfsrr_bytes = 0;
30914 +       if (!parent) {
30915 +               bindex = 0;
30916 +               bend = au_sbend(sb);
30917 +       } else {
30918 +               bindex = au_dbstart(parent);
30919 +               bend = au_dbtaildir(parent);
30920 +       }
30921 +
30922 +       for (; bindex <= bend; bindex++) {
30923 +               if (parent) {
30924 +                       h_parent = au_h_dptr(parent, bindex);
30925 +                       if (!h_parent || !h_parent->d_inode)
30926 +                               continue;
30927 +               }
30928 +               br = au_sbr(sb, bindex);
30929 +               if (au_br_rdonly(br))
30930 +                       continue;
30931 +
30932 +               /* sb->s_root for NFS is unreliable */
30933 +               h_path.mnt = au_br_mnt(br);
30934 +               h_path.dentry = h_path.mnt->mnt_root;
30935 +               err = vfs_statfs(&h_path, st);
30936 +               if (unlikely(err)) {
30937 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
30938 +                       continue;
30939 +               }
30940 +
30941 +               /* when the available size is equal, select the lower one */
30942 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
30943 +                            || sizeof(b) < sizeof(st->f_bsize));
30944 +               b = st->f_bavail * st->f_bsize;
30945 +               br->br_wbr->wbr_bytes = b;
30946 +               if (b >= bavail) {
30947 +                       bavail = b;
30948 +                       mfs->mfs_bindex = bindex;
30949 +                       mfs->mfs_jiffy = jiffies;
30950 +               }
30951 +       }
30952 +
30953 +       mfs->mfsrr_bytes = bavail;
30954 +       AuDbg("b%d\n", mfs->mfs_bindex);
30955 +       kfree(st);
30956 +}
30957 +
30958 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
30959 +{
30960 +       int err;
30961 +       struct dentry *parent;
30962 +       struct super_block *sb;
30963 +       struct au_wbr_mfs *mfs;
30964 +
30965 +       err = au_wbr_create_exp(dentry);
30966 +       if (err >= 0)
30967 +               goto out;
30968 +
30969 +       sb = dentry->d_sb;
30970 +       parent = NULL;
30971 +       if (au_ftest_wbr(flags, PARENT))
30972 +               parent = dget_parent(dentry);
30973 +       mfs = &au_sbi(sb)->si_wbr_mfs;
30974 +       mutex_lock(&mfs->mfs_lock);
30975 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
30976 +           || mfs->mfs_bindex < 0
30977 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
30978 +               au_mfs(dentry, parent);
30979 +       mutex_unlock(&mfs->mfs_lock);
30980 +       err = mfs->mfs_bindex;
30981 +       dput(parent);
30982 +
30983 +       if (err >= 0)
30984 +               err = au_wbr_nonopq(dentry, err);
30985 +
30986 +out:
30987 +       AuDbg("b%d\n", err);
30988 +       return err;
30989 +}
30990 +
30991 +static int au_wbr_create_init_mfs(struct super_block *sb)
30992 +{
30993 +       struct au_wbr_mfs *mfs;
30994 +
30995 +       mfs = &au_sbi(sb)->si_wbr_mfs;
30996 +       mutex_init(&mfs->mfs_lock);
30997 +       mfs->mfs_jiffy = 0;
30998 +       mfs->mfs_bindex = -EROFS;
30999 +
31000 +       return 0;
31001 +}
31002 +
31003 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
31004 +{
31005 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
31006 +       return 0;
31007 +}
31008 +
31009 +/* ---------------------------------------------------------------------- */
31010 +
31011 +/* most free space and then round robin */
31012 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
31013 +{
31014 +       int err;
31015 +       struct au_wbr_mfs *mfs;
31016 +
31017 +       err = au_wbr_create_mfs(dentry, flags);
31018 +       if (err >= 0) {
31019 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
31020 +               mutex_lock(&mfs->mfs_lock);
31021 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
31022 +                       err = au_wbr_create_rr(dentry, flags);
31023 +               mutex_unlock(&mfs->mfs_lock);
31024 +       }
31025 +
31026 +       AuDbg("b%d\n", err);
31027 +       return err;
31028 +}
31029 +
31030 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
31031 +{
31032 +       int err;
31033 +
31034 +       au_wbr_create_init_mfs(sb); /* ignore */
31035 +       err = au_wbr_create_init_rr(sb);
31036 +
31037 +       return err;
31038 +}
31039 +
31040 +/* ---------------------------------------------------------------------- */
31041 +
31042 +/* top down parent and most free space */
31043 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
31044 +{
31045 +       int err, e2;
31046 +       unsigned long long b;
31047 +       aufs_bindex_t bindex, bstart, bend;
31048 +       struct super_block *sb;
31049 +       struct dentry *parent, *h_parent;
31050 +       struct au_branch *br;
31051 +
31052 +       err = au_wbr_create_tdp(dentry, flags);
31053 +       if (unlikely(err < 0))
31054 +               goto out;
31055 +       parent = dget_parent(dentry);
31056 +       bstart = au_dbstart(parent);
31057 +       bend = au_dbtaildir(parent);
31058 +       if (bstart == bend)
31059 +               goto out_parent; /* success */
31060 +
31061 +       e2 = au_wbr_create_mfs(dentry, flags);
31062 +       if (e2 < 0)
31063 +               goto out_parent; /* success */
31064 +
31065 +       /* when the available size is equal, select upper one */
31066 +       sb = dentry->d_sb;
31067 +       br = au_sbr(sb, err);
31068 +       b = br->br_wbr->wbr_bytes;
31069 +       AuDbg("b%d, %llu\n", err, b);
31070 +
31071 +       for (bindex = bstart; bindex <= bend; bindex++) {
31072 +               h_parent = au_h_dptr(parent, bindex);
31073 +               if (!h_parent || !h_parent->d_inode)
31074 +                       continue;
31075 +
31076 +               br = au_sbr(sb, bindex);
31077 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
31078 +                       b = br->br_wbr->wbr_bytes;
31079 +                       err = bindex;
31080 +                       AuDbg("b%d, %llu\n", err, b);
31081 +               }
31082 +       }
31083 +
31084 +       if (err >= 0)
31085 +               err = au_wbr_nonopq(dentry, err);
31086 +
31087 +out_parent:
31088 +       dput(parent);
31089 +out:
31090 +       AuDbg("b%d\n", err);
31091 +       return err;
31092 +}
31093 +
31094 +/* ---------------------------------------------------------------------- */
31095 +
31096 +/*
31097 + * - top down parent
31098 + * - most free space with parent
31099 + * - most free space round-robin regardless parent
31100 + */
31101 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
31102 +{
31103 +       int err;
31104 +       unsigned long long watermark;
31105 +       struct super_block *sb;
31106 +       struct au_branch *br;
31107 +       struct au_wbr_mfs *mfs;
31108 +
31109 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
31110 +       if (unlikely(err < 0))
31111 +               goto out;
31112 +
31113 +       sb = dentry->d_sb;
31114 +       br = au_sbr(sb, err);
31115 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31116 +       mutex_lock(&mfs->mfs_lock);
31117 +       watermark = mfs->mfsrr_watermark;
31118 +       mutex_unlock(&mfs->mfs_lock);
31119 +       if (br->br_wbr->wbr_bytes < watermark)
31120 +               /* regardless the parent dir */
31121 +               err = au_wbr_create_mfsrr(dentry, flags);
31122 +
31123 +out:
31124 +       AuDbg("b%d\n", err);
31125 +       return err;
31126 +}
31127 +
31128 +/* ---------------------------------------------------------------------- */
31129 +
31130 +/* policies for copyup */
31131 +
31132 +/* top down parent */
31133 +static int au_wbr_copyup_tdp(struct dentry *dentry)
31134 +{
31135 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
31136 +}
31137 +
31138 +/* bottom up parent */
31139 +static int au_wbr_copyup_bup(struct dentry *dentry)
31140 +{
31141 +       int err;
31142 +       aufs_bindex_t bindex, bstart;
31143 +       struct dentry *parent, *h_parent;
31144 +       struct super_block *sb;
31145 +
31146 +       err = -EROFS;
31147 +       sb = dentry->d_sb;
31148 +       parent = dget_parent(dentry);
31149 +       bstart = au_dbstart(parent);
31150 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
31151 +               h_parent = au_h_dptr(parent, bindex);
31152 +               if (!h_parent || !h_parent->d_inode)
31153 +                       continue;
31154 +
31155 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
31156 +                       err = bindex;
31157 +                       break;
31158 +               }
31159 +       }
31160 +       dput(parent);
31161 +
31162 +       /* bottom up here */
31163 +       if (unlikely(err < 0))
31164 +               err = au_wbr_bu(sb, bstart - 1);
31165 +
31166 +       AuDbg("b%d\n", err);
31167 +       return err;
31168 +}
31169 +
31170 +/* bottom up */
31171 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart)
31172 +{
31173 +       int err;
31174 +
31175 +       err = au_wbr_bu(dentry->d_sb, bstart);
31176 +       AuDbg("b%d\n", err);
31177 +       if (err > bstart)
31178 +               err = au_wbr_nonopq(dentry, err);
31179 +
31180 +       AuDbg("b%d\n", err);
31181 +       return err;
31182 +}
31183 +
31184 +static int au_wbr_copyup_bu(struct dentry *dentry)
31185 +{
31186 +       int err;
31187 +       aufs_bindex_t bstart;
31188 +
31189 +       bstart = au_dbstart(dentry);
31190 +       err = au_wbr_do_copyup_bu(dentry, bstart);
31191 +       return err;
31192 +}
31193 +
31194 +/* ---------------------------------------------------------------------- */
31195 +
31196 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
31197 +       [AuWbrCopyup_TDP] = {
31198 +               .copyup = au_wbr_copyup_tdp
31199 +       },
31200 +       [AuWbrCopyup_BUP] = {
31201 +               .copyup = au_wbr_copyup_bup
31202 +       },
31203 +       [AuWbrCopyup_BU] = {
31204 +               .copyup = au_wbr_copyup_bu
31205 +       }
31206 +};
31207 +
31208 +struct au_wbr_create_operations au_wbr_create_ops[] = {
31209 +       [AuWbrCreate_TDP] = {
31210 +               .create = au_wbr_create_tdp
31211 +       },
31212 +       [AuWbrCreate_RR] = {
31213 +               .create = au_wbr_create_rr,
31214 +               .init   = au_wbr_create_init_rr
31215 +       },
31216 +       [AuWbrCreate_MFS] = {
31217 +               .create = au_wbr_create_mfs,
31218 +               .init   = au_wbr_create_init_mfs,
31219 +               .fin    = au_wbr_create_fin_mfs
31220 +       },
31221 +       [AuWbrCreate_MFSV] = {
31222 +               .create = au_wbr_create_mfs,
31223 +               .init   = au_wbr_create_init_mfs,
31224 +               .fin    = au_wbr_create_fin_mfs
31225 +       },
31226 +       [AuWbrCreate_MFSRR] = {
31227 +               .create = au_wbr_create_mfsrr,
31228 +               .init   = au_wbr_create_init_mfsrr,
31229 +               .fin    = au_wbr_create_fin_mfs
31230 +       },
31231 +       [AuWbrCreate_MFSRRV] = {
31232 +               .create = au_wbr_create_mfsrr,
31233 +               .init   = au_wbr_create_init_mfsrr,
31234 +               .fin    = au_wbr_create_fin_mfs
31235 +       },
31236 +       [AuWbrCreate_PMFS] = {
31237 +               .create = au_wbr_create_pmfs,
31238 +               .init   = au_wbr_create_init_mfs,
31239 +               .fin    = au_wbr_create_fin_mfs
31240 +       },
31241 +       [AuWbrCreate_PMFSV] = {
31242 +               .create = au_wbr_create_pmfs,
31243 +               .init   = au_wbr_create_init_mfs,
31244 +               .fin    = au_wbr_create_fin_mfs
31245 +       },
31246 +       [AuWbrCreate_PMFSRR] = {
31247 +               .create = au_wbr_create_pmfsrr,
31248 +               .init   = au_wbr_create_init_mfsrr,
31249 +               .fin    = au_wbr_create_fin_mfs
31250 +       },
31251 +       [AuWbrCreate_PMFSRRV] = {
31252 +               .create = au_wbr_create_pmfsrr,
31253 +               .init   = au_wbr_create_init_mfsrr,
31254 +               .fin    = au_wbr_create_fin_mfs
31255 +       }
31256 +};
31257 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
31258 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
31259 +++ linux/fs/aufs/whout.c       2015-04-13 15:10:20.790157026 +0200
31260 @@ -0,0 +1,1064 @@
31261 +/*
31262 + * Copyright (C) 2005-2015 Junjiro R. Okajima
31263 + *
31264 + * This program, aufs is free software; you can redistribute it and/or modify
31265 + * it under the terms of the GNU General Public License as published by
31266 + * the Free Software Foundation; either version 2 of the License, or
31267 + * (at your option) any later version.
31268 + *
31269 + * This program is distributed in the hope that it will be useful,
31270 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31271 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31272 + * GNU General Public License for more details.
31273 + *
31274 + * You should have received a copy of the GNU General Public License
31275 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31276 + */
31277 +
31278 +/*
31279 + * whiteout for logical deletion and opaque directory
31280 + */
31281 +
31282 +#include "aufs.h"
31283 +
31284 +#define WH_MASK                        S_IRUGO
31285 +
31286 +/*
31287 + * If a directory contains this file, then it is opaque.  We start with the
31288 + * .wh. flag so that it is blocked by lookup.
31289 + */
31290 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
31291 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
31292 +
31293 +/*
31294 + * generate whiteout name, which is NOT terminated by NULL.
31295 + * @name: original d_name.name
31296 + * @len: original d_name.len
31297 + * @wh: whiteout qstr
31298 + * returns zero when succeeds, otherwise error.
31299 + * succeeded value as wh->name should be freed by kfree().
31300 + */
31301 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
31302 +{
31303 +       char *p;
31304 +
31305 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
31306 +               return -ENAMETOOLONG;
31307 +
31308 +       wh->len = name->len + AUFS_WH_PFX_LEN;
31309 +       p = kmalloc(wh->len, GFP_NOFS);
31310 +       wh->name = p;
31311 +       if (p) {
31312 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
31313 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
31314 +               /* smp_mb(); */
31315 +               return 0;
31316 +       }
31317 +       return -ENOMEM;
31318 +}
31319 +
31320 +/* ---------------------------------------------------------------------- */
31321 +
31322 +/*
31323 + * test if the @wh_name exists under @h_parent.
31324 + * @try_sio specifies the necessary of super-io.
31325 + */
31326 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
31327 +{
31328 +       int err;
31329 +       struct dentry *wh_dentry;
31330 +
31331 +       if (!try_sio)
31332 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
31333 +       else
31334 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
31335 +       err = PTR_ERR(wh_dentry);
31336 +       if (IS_ERR(wh_dentry)) {
31337 +               if (err == -ENAMETOOLONG)
31338 +                       err = 0;
31339 +               goto out;
31340 +       }
31341 +
31342 +       err = 0;
31343 +       if (!wh_dentry->d_inode)
31344 +               goto out_wh; /* success */
31345 +
31346 +       err = 1;
31347 +       if (d_is_reg(wh_dentry))
31348 +               goto out_wh; /* success */
31349 +
31350 +       err = -EIO;
31351 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
31352 +               wh_dentry, wh_dentry->d_inode->i_mode);
31353 +
31354 +out_wh:
31355 +       dput(wh_dentry);
31356 +out:
31357 +       return err;
31358 +}
31359 +
31360 +/*
31361 + * test if the @h_dentry sets opaque or not.
31362 + */
31363 +int au_diropq_test(struct dentry *h_dentry)
31364 +{
31365 +       int err;
31366 +       struct inode *h_dir;
31367 +
31368 +       h_dir = h_dentry->d_inode;
31369 +       err = au_wh_test(h_dentry, &diropq_name,
31370 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
31371 +       return err;
31372 +}
31373 +
31374 +/*
31375 + * returns a negative dentry whose name is unique and temporary.
31376 + */
31377 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
31378 +                            struct qstr *prefix)
31379 +{
31380 +       struct dentry *dentry;
31381 +       int i;
31382 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
31383 +               *name, *p;
31384 +       /* strict atomic_t is unnecessary here */
31385 +       static unsigned short cnt;
31386 +       struct qstr qs;
31387 +
31388 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
31389 +
31390 +       name = defname;
31391 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
31392 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
31393 +               dentry = ERR_PTR(-ENAMETOOLONG);
31394 +               if (unlikely(qs.len > NAME_MAX))
31395 +                       goto out;
31396 +               dentry = ERR_PTR(-ENOMEM);
31397 +               name = kmalloc(qs.len + 1, GFP_NOFS);
31398 +               if (unlikely(!name))
31399 +                       goto out;
31400 +       }
31401 +
31402 +       /* doubly whiteout-ed */
31403 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
31404 +       p = name + AUFS_WH_PFX_LEN * 2;
31405 +       memcpy(p, prefix->name, prefix->len);
31406 +       p += prefix->len;
31407 +       *p++ = '.';
31408 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
31409 +
31410 +       qs.name = name;
31411 +       for (i = 0; i < 3; i++) {
31412 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
31413 +               dentry = au_sio_lkup_one(&qs, h_parent);
31414 +               if (IS_ERR(dentry) || !dentry->d_inode)
31415 +                       goto out_name;
31416 +               dput(dentry);
31417 +       }
31418 +       /* pr_warn("could not get random name\n"); */
31419 +       dentry = ERR_PTR(-EEXIST);
31420 +       AuDbg("%.*s\n", AuLNPair(&qs));
31421 +       BUG();
31422 +
31423 +out_name:
31424 +       if (name != defname)
31425 +               kfree(name);
31426 +out:
31427 +       AuTraceErrPtr(dentry);
31428 +       return dentry;
31429 +}
31430 +
31431 +/*
31432 + * rename the @h_dentry on @br to the whiteouted temporary name.
31433 + */
31434 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
31435 +{
31436 +       int err;
31437 +       struct path h_path = {
31438 +               .mnt = au_br_mnt(br)
31439 +       };
31440 +       struct inode *h_dir, *delegated;
31441 +       struct dentry *h_parent;
31442 +
31443 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
31444 +       h_dir = h_parent->d_inode;
31445 +       IMustLock(h_dir);
31446 +
31447 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
31448 +       err = PTR_ERR(h_path.dentry);
31449 +       if (IS_ERR(h_path.dentry))
31450 +               goto out;
31451 +
31452 +       /* under the same dir, no need to lock_rename() */
31453 +       delegated = NULL;
31454 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
31455 +       AuTraceErr(err);
31456 +       if (unlikely(err == -EWOULDBLOCK)) {
31457 +               pr_warn("cannot retry for NFSv4 delegation"
31458 +                       " for an internal rename\n");
31459 +               iput(delegated);
31460 +       }
31461 +       dput(h_path.dentry);
31462 +
31463 +out:
31464 +       AuTraceErr(err);
31465 +       return err;
31466 +}
31467 +
31468 +/* ---------------------------------------------------------------------- */
31469 +/*
31470 + * functions for removing a whiteout
31471 + */
31472 +
31473 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
31474 +{
31475 +       int err, force;
31476 +       struct inode *delegated;
31477 +
31478 +       /*
31479 +        * forces superio when the dir has a sticky bit.
31480 +        * this may be a violation of unix fs semantics.
31481 +        */
31482 +       force = (h_dir->i_mode & S_ISVTX)
31483 +               && !uid_eq(current_fsuid(), h_path->dentry->d_inode->i_uid);
31484 +       delegated = NULL;
31485 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
31486 +       if (unlikely(err == -EWOULDBLOCK)) {
31487 +               pr_warn("cannot retry for NFSv4 delegation"
31488 +                       " for an internal unlink\n");
31489 +               iput(delegated);
31490 +       }
31491 +       return err;
31492 +}
31493 +
31494 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
31495 +                       struct dentry *dentry)
31496 +{
31497 +       int err;
31498 +
31499 +       err = do_unlink_wh(h_dir, h_path);
31500 +       if (!err && dentry)
31501 +               au_set_dbwh(dentry, -1);
31502 +
31503 +       return err;
31504 +}
31505 +
31506 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
31507 +                         struct au_branch *br)
31508 +{
31509 +       int err;
31510 +       struct path h_path = {
31511 +               .mnt = au_br_mnt(br)
31512 +       };
31513 +
31514 +       err = 0;
31515 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
31516 +       if (IS_ERR(h_path.dentry))
31517 +               err = PTR_ERR(h_path.dentry);
31518 +       else {
31519 +               if (h_path.dentry->d_inode
31520 +                   && d_is_reg(h_path.dentry))
31521 +                       err = do_unlink_wh(h_parent->d_inode, &h_path);
31522 +               dput(h_path.dentry);
31523 +       }
31524 +
31525 +       return err;
31526 +}
31527 +
31528 +/* ---------------------------------------------------------------------- */
31529 +/*
31530 + * initialize/clean whiteout for a branch
31531 + */
31532 +
31533 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
31534 +                       const int isdir)
31535 +{
31536 +       int err;
31537 +       struct inode *delegated;
31538 +
31539 +       if (!whpath->dentry->d_inode)
31540 +               return;
31541 +
31542 +       if (isdir)
31543 +               err = vfsub_rmdir(h_dir, whpath);
31544 +       else {
31545 +               delegated = NULL;
31546 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
31547 +               if (unlikely(err == -EWOULDBLOCK)) {
31548 +                       pr_warn("cannot retry for NFSv4 delegation"
31549 +                               " for an internal unlink\n");
31550 +                       iput(delegated);
31551 +               }
31552 +       }
31553 +       if (unlikely(err))
31554 +               pr_warn("failed removing %pd (%d), ignored.\n",
31555 +                       whpath->dentry, err);
31556 +}
31557 +
31558 +static int test_linkable(struct dentry *h_root)
31559 +{
31560 +       struct inode *h_dir = h_root->d_inode;
31561 +
31562 +       if (h_dir->i_op->link)
31563 +               return 0;
31564 +
31565 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
31566 +              h_root, au_sbtype(h_root->d_sb));
31567 +       return -ENOSYS;
31568 +}
31569 +
31570 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
31571 +static int au_whdir(struct inode *h_dir, struct path *path)
31572 +{
31573 +       int err;
31574 +
31575 +       err = -EEXIST;
31576 +       if (!path->dentry->d_inode) {
31577 +               int mode = S_IRWXU;
31578 +
31579 +               if (au_test_nfs(path->dentry->d_sb))
31580 +                       mode |= S_IXUGO;
31581 +               err = vfsub_mkdir(h_dir, path, mode);
31582 +       } else if (d_is_dir(path->dentry))
31583 +               err = 0;
31584 +       else
31585 +               pr_err("unknown %pd exists\n", path->dentry);
31586 +
31587 +       return err;
31588 +}
31589 +
31590 +struct au_wh_base {
31591 +       const struct qstr *name;
31592 +       struct dentry *dentry;
31593 +};
31594 +
31595 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
31596 +                         struct path *h_path)
31597 +{
31598 +       h_path->dentry = base[AuBrWh_BASE].dentry;
31599 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
31600 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
31601 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
31602 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
31603 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
31604 +}
31605 +
31606 +/*
31607 + * returns tri-state,
31608 + * minus: error, caller should print the message
31609 + * zero: succuess
31610 + * plus: error, caller should NOT print the message
31611 + */
31612 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
31613 +                               int do_plink, struct au_wh_base base[],
31614 +                               struct path *h_path)
31615 +{
31616 +       int err;
31617 +       struct inode *h_dir;
31618 +
31619 +       h_dir = h_root->d_inode;
31620 +       h_path->dentry = base[AuBrWh_BASE].dentry;
31621 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
31622 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
31623 +       if (do_plink) {
31624 +               err = test_linkable(h_root);
31625 +               if (unlikely(err)) {
31626 +                       err = 1;
31627 +                       goto out;
31628 +               }
31629 +
31630 +               err = au_whdir(h_dir, h_path);
31631 +               if (unlikely(err))
31632 +                       goto out;
31633 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
31634 +       } else
31635 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
31636 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
31637 +       err = au_whdir(h_dir, h_path);
31638 +       if (unlikely(err))
31639 +               goto out;
31640 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
31641 +
31642 +out:
31643 +       return err;
31644 +}
31645 +
31646 +/*
31647 + * for the moment, aufs supports the branch filesystem which does not support
31648 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
31649 + * copyup failed. finally, such filesystem will not be used as the writable
31650 + * branch.
31651 + *
31652 + * returns tri-state, see above.
31653 + */
31654 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
31655 +                        int do_plink, struct au_wh_base base[],
31656 +                        struct path *h_path)
31657 +{
31658 +       int err;
31659 +       struct inode *h_dir;
31660 +
31661 +       WbrWhMustWriteLock(wbr);
31662 +
31663 +       err = test_linkable(h_root);
31664 +       if (unlikely(err)) {
31665 +               err = 1;
31666 +               goto out;
31667 +       }
31668 +
31669 +       /*
31670 +        * todo: should this create be done in /sbin/mount.aufs helper?
31671 +        */
31672 +       err = -EEXIST;
31673 +       h_dir = h_root->d_inode;
31674 +       if (!base[AuBrWh_BASE].dentry->d_inode) {
31675 +               h_path->dentry = base[AuBrWh_BASE].dentry;
31676 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
31677 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
31678 +               err = 0;
31679 +       else
31680 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
31681 +       if (unlikely(err))
31682 +               goto out;
31683 +
31684 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
31685 +       if (do_plink) {
31686 +               err = au_whdir(h_dir, h_path);
31687 +               if (unlikely(err))
31688 +                       goto out;
31689 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
31690 +       } else
31691 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
31692 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
31693 +
31694 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
31695 +       err = au_whdir(h_dir, h_path);
31696 +       if (unlikely(err))
31697 +               goto out;
31698 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
31699 +
31700 +out:
31701 +       return err;
31702 +}
31703 +
31704 +/*
31705 + * initialize the whiteout base file/dir for @br.
31706 + */
31707 +int au_wh_init(struct au_branch *br, struct super_block *sb)
31708 +{
31709 +       int err, i;
31710 +       const unsigned char do_plink
31711 +               = !!au_opt_test(au_mntflags(sb), PLINK);
31712 +       struct inode *h_dir;
31713 +       struct path path = br->br_path;
31714 +       struct dentry *h_root = path.dentry;
31715 +       struct au_wbr *wbr = br->br_wbr;
31716 +       static const struct qstr base_name[] = {
31717 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
31718 +                                         sizeof(AUFS_BASE_NAME) - 1),
31719 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
31720 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
31721 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
31722 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
31723 +       };
31724 +       struct au_wh_base base[] = {
31725 +               [AuBrWh_BASE] = {
31726 +                       .name   = base_name + AuBrWh_BASE,
31727 +                       .dentry = NULL
31728 +               },
31729 +               [AuBrWh_PLINK] = {
31730 +                       .name   = base_name + AuBrWh_PLINK,
31731 +                       .dentry = NULL
31732 +               },
31733 +               [AuBrWh_ORPH] = {
31734 +                       .name   = base_name + AuBrWh_ORPH,
31735 +                       .dentry = NULL
31736 +               }
31737 +       };
31738 +
31739 +       if (wbr)
31740 +               WbrWhMustWriteLock(wbr);
31741 +
31742 +       for (i = 0; i < AuBrWh_Last; i++) {
31743 +               /* doubly whiteouted */
31744 +               struct dentry *d;
31745 +
31746 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
31747 +               err = PTR_ERR(d);
31748 +               if (IS_ERR(d))
31749 +                       goto out;
31750 +
31751 +               base[i].dentry = d;
31752 +               AuDebugOn(wbr
31753 +                         && wbr->wbr_wh[i]
31754 +                         && wbr->wbr_wh[i] != base[i].dentry);
31755 +       }
31756 +
31757 +       if (wbr)
31758 +               for (i = 0; i < AuBrWh_Last; i++) {
31759 +                       dput(wbr->wbr_wh[i]);
31760 +                       wbr->wbr_wh[i] = NULL;
31761 +               }
31762 +
31763 +       err = 0;
31764 +       if (!au_br_writable(br->br_perm)) {
31765 +               h_dir = h_root->d_inode;
31766 +               au_wh_init_ro(h_dir, base, &path);
31767 +       } else if (!au_br_wh_linkable(br->br_perm)) {
31768 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
31769 +               if (err > 0)
31770 +                       goto out;
31771 +               else if (err)
31772 +                       goto out_err;
31773 +       } else {
31774 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
31775 +               if (err > 0)
31776 +                       goto out;
31777 +               else if (err)
31778 +                       goto out_err;
31779 +       }
31780 +       goto out; /* success */
31781 +
31782 +out_err:
31783 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
31784 +              err, h_root, au_sbtype(h_root->d_sb));
31785 +out:
31786 +       for (i = 0; i < AuBrWh_Last; i++)
31787 +               dput(base[i].dentry);
31788 +       return err;
31789 +}
31790 +
31791 +/* ---------------------------------------------------------------------- */
31792 +/*
31793 + * whiteouts are all hard-linked usually.
31794 + * when its link count reaches a ceiling, we create a new whiteout base
31795 + * asynchronously.
31796 + */
31797 +
31798 +struct reinit_br_wh {
31799 +       struct super_block *sb;
31800 +       struct au_branch *br;
31801 +};
31802 +
31803 +static void reinit_br_wh(void *arg)
31804 +{
31805 +       int err;
31806 +       aufs_bindex_t bindex;
31807 +       struct path h_path;
31808 +       struct reinit_br_wh *a = arg;
31809 +       struct au_wbr *wbr;
31810 +       struct inode *dir, *delegated;
31811 +       struct dentry *h_root;
31812 +       struct au_hinode *hdir;
31813 +
31814 +       err = 0;
31815 +       wbr = a->br->br_wbr;
31816 +       /* big aufs lock */
31817 +       si_noflush_write_lock(a->sb);
31818 +       if (!au_br_writable(a->br->br_perm))
31819 +               goto out;
31820 +       bindex = au_br_index(a->sb, a->br->br_id);
31821 +       if (unlikely(bindex < 0))
31822 +               goto out;
31823 +
31824 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
31825 +       dir = a->sb->s_root->d_inode;
31826 +       hdir = au_hi(dir, bindex);
31827 +       h_root = au_h_dptr(a->sb->s_root, bindex);
31828 +       AuDebugOn(h_root != au_br_dentry(a->br));
31829 +
31830 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
31831 +       wbr_wh_write_lock(wbr);
31832 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
31833 +                         h_root, a->br);
31834 +       if (!err) {
31835 +               h_path.dentry = wbr->wbr_whbase;
31836 +               h_path.mnt = au_br_mnt(a->br);
31837 +               delegated = NULL;
31838 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
31839 +                                  /*force*/0);
31840 +               if (unlikely(err == -EWOULDBLOCK)) {
31841 +                       pr_warn("cannot retry for NFSv4 delegation"
31842 +                               " for an internal unlink\n");
31843 +                       iput(delegated);
31844 +               }
31845 +       } else {
31846 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
31847 +               err = 0;
31848 +       }
31849 +       dput(wbr->wbr_whbase);
31850 +       wbr->wbr_whbase = NULL;
31851 +       if (!err)
31852 +               err = au_wh_init(a->br, a->sb);
31853 +       wbr_wh_write_unlock(wbr);
31854 +       au_hn_imtx_unlock(hdir);
31855 +       di_read_unlock(a->sb->s_root, AuLock_IR);
31856 +       if (!err)
31857 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
31858 +
31859 +out:
31860 +       if (wbr)
31861 +               atomic_dec(&wbr->wbr_wh_running);
31862 +       atomic_dec(&a->br->br_count);
31863 +       si_write_unlock(a->sb);
31864 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
31865 +       kfree(arg);
31866 +       if (unlikely(err))
31867 +               AuIOErr("err %d\n", err);
31868 +}
31869 +
31870 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
31871 +{
31872 +       int do_dec, wkq_err;
31873 +       struct reinit_br_wh *arg;
31874 +
31875 +       do_dec = 1;
31876 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
31877 +               goto out;
31878 +
31879 +       /* ignore ENOMEM */
31880 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
31881 +       if (arg) {
31882 +               /*
31883 +                * dec(wh_running), kfree(arg) and dec(br_count)
31884 +                * in reinit function
31885 +                */
31886 +               arg->sb = sb;
31887 +               arg->br = br;
31888 +               atomic_inc(&br->br_count);
31889 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
31890 +               if (unlikely(wkq_err)) {
31891 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
31892 +                       atomic_dec(&br->br_count);
31893 +                       kfree(arg);
31894 +               }
31895 +               do_dec = 0;
31896 +       }
31897 +
31898 +out:
31899 +       if (do_dec)
31900 +               atomic_dec(&br->br_wbr->wbr_wh_running);
31901 +}
31902 +
31903 +/* ---------------------------------------------------------------------- */
31904 +
31905 +/*
31906 + * create the whiteout @wh.
31907 + */
31908 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
31909 +                            struct dentry *wh)
31910 +{
31911 +       int err;
31912 +       struct path h_path = {
31913 +               .dentry = wh
31914 +       };
31915 +       struct au_branch *br;
31916 +       struct au_wbr *wbr;
31917 +       struct dentry *h_parent;
31918 +       struct inode *h_dir, *delegated;
31919 +
31920 +       h_parent = wh->d_parent; /* dir inode is locked */
31921 +       h_dir = h_parent->d_inode;
31922 +       IMustLock(h_dir);
31923 +
31924 +       br = au_sbr(sb, bindex);
31925 +       h_path.mnt = au_br_mnt(br);
31926 +       wbr = br->br_wbr;
31927 +       wbr_wh_read_lock(wbr);
31928 +       if (wbr->wbr_whbase) {
31929 +               delegated = NULL;
31930 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
31931 +               if (unlikely(err == -EWOULDBLOCK)) {
31932 +                       pr_warn("cannot retry for NFSv4 delegation"
31933 +                               " for an internal link\n");
31934 +                       iput(delegated);
31935 +               }
31936 +               if (!err || err != -EMLINK)
31937 +                       goto out;
31938 +
31939 +               /* link count full. re-initialize br_whbase. */
31940 +               kick_reinit_br_wh(sb, br);
31941 +       }
31942 +
31943 +       /* return this error in this context */
31944 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
31945 +       if (!err)
31946 +               au_fhsm_wrote(sb, bindex, /*force*/0);
31947 +
31948 +out:
31949 +       wbr_wh_read_unlock(wbr);
31950 +       return err;
31951 +}
31952 +
31953 +/* ---------------------------------------------------------------------- */
31954 +
31955 +/*
31956 + * create or remove the diropq.
31957 + */
31958 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
31959 +                               unsigned int flags)
31960 +{
31961 +       struct dentry *opq_dentry, *h_dentry;
31962 +       struct super_block *sb;
31963 +       struct au_branch *br;
31964 +       int err;
31965 +
31966 +       sb = dentry->d_sb;
31967 +       br = au_sbr(sb, bindex);
31968 +       h_dentry = au_h_dptr(dentry, bindex);
31969 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
31970 +       if (IS_ERR(opq_dentry))
31971 +               goto out;
31972 +
31973 +       if (au_ftest_diropq(flags, CREATE)) {
31974 +               err = link_or_create_wh(sb, bindex, opq_dentry);
31975 +               if (!err) {
31976 +                       au_set_dbdiropq(dentry, bindex);
31977 +                       goto out; /* success */
31978 +               }
31979 +       } else {
31980 +               struct path tmp = {
31981 +                       .dentry = opq_dentry,
31982 +                       .mnt    = au_br_mnt(br)
31983 +               };
31984 +               err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
31985 +               if (!err)
31986 +                       au_set_dbdiropq(dentry, -1);
31987 +       }
31988 +       dput(opq_dentry);
31989 +       opq_dentry = ERR_PTR(err);
31990 +
31991 +out:
31992 +       return opq_dentry;
31993 +}
31994 +
31995 +struct do_diropq_args {
31996 +       struct dentry **errp;
31997 +       struct dentry *dentry;
31998 +       aufs_bindex_t bindex;
31999 +       unsigned int flags;
32000 +};
32001 +
32002 +static void call_do_diropq(void *args)
32003 +{
32004 +       struct do_diropq_args *a = args;
32005 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
32006 +}
32007 +
32008 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
32009 +                            unsigned int flags)
32010 +{
32011 +       struct dentry *diropq, *h_dentry;
32012 +
32013 +       h_dentry = au_h_dptr(dentry, bindex);
32014 +       if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
32015 +               diropq = do_diropq(dentry, bindex, flags);
32016 +       else {
32017 +               int wkq_err;
32018 +               struct do_diropq_args args = {
32019 +                       .errp           = &diropq,
32020 +                       .dentry         = dentry,
32021 +                       .bindex         = bindex,
32022 +                       .flags          = flags
32023 +               };
32024 +
32025 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
32026 +               if (unlikely(wkq_err))
32027 +                       diropq = ERR_PTR(wkq_err);
32028 +       }
32029 +
32030 +       return diropq;
32031 +}
32032 +
32033 +/* ---------------------------------------------------------------------- */
32034 +
32035 +/*
32036 + * lookup whiteout dentry.
32037 + * @h_parent: lower parent dentry which must exist and be locked
32038 + * @base_name: name of dentry which will be whiteouted
32039 + * returns dentry for whiteout.
32040 + */
32041 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
32042 +                         struct au_branch *br)
32043 +{
32044 +       int err;
32045 +       struct qstr wh_name;
32046 +       struct dentry *wh_dentry;
32047 +
32048 +       err = au_wh_name_alloc(&wh_name, base_name);
32049 +       wh_dentry = ERR_PTR(err);
32050 +       if (!err) {
32051 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
32052 +               kfree(wh_name.name);
32053 +       }
32054 +       return wh_dentry;
32055 +}
32056 +
32057 +/*
32058 + * link/create a whiteout for @dentry on @bindex.
32059 + */
32060 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
32061 +                           struct dentry *h_parent)
32062 +{
32063 +       struct dentry *wh_dentry;
32064 +       struct super_block *sb;
32065 +       int err;
32066 +
32067 +       sb = dentry->d_sb;
32068 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
32069 +       if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
32070 +               err = link_or_create_wh(sb, bindex, wh_dentry);
32071 +               if (!err) {
32072 +                       au_set_dbwh(dentry, bindex);
32073 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
32074 +               } else {
32075 +                       dput(wh_dentry);
32076 +                       wh_dentry = ERR_PTR(err);
32077 +               }
32078 +       }
32079 +
32080 +       return wh_dentry;
32081 +}
32082 +
32083 +/* ---------------------------------------------------------------------- */
32084 +
32085 +/* Delete all whiteouts in this directory on branch bindex. */
32086 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
32087 +                          aufs_bindex_t bindex, struct au_branch *br)
32088 +{
32089 +       int err;
32090 +       unsigned long ul, n;
32091 +       struct qstr wh_name;
32092 +       char *p;
32093 +       struct hlist_head *head;
32094 +       struct au_vdir_wh *pos;
32095 +       struct au_vdir_destr *str;
32096 +
32097 +       err = -ENOMEM;
32098 +       p = (void *)__get_free_page(GFP_NOFS);
32099 +       wh_name.name = p;
32100 +       if (unlikely(!wh_name.name))
32101 +               goto out;
32102 +
32103 +       err = 0;
32104 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32105 +       p += AUFS_WH_PFX_LEN;
32106 +       n = whlist->nh_num;
32107 +       head = whlist->nh_head;
32108 +       for (ul = 0; !err && ul < n; ul++, head++) {
32109 +               hlist_for_each_entry(pos, head, wh_hash) {
32110 +                       if (pos->wh_bindex != bindex)
32111 +                               continue;
32112 +
32113 +                       str = &pos->wh_str;
32114 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
32115 +                               memcpy(p, str->name, str->len);
32116 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
32117 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
32118 +                               if (!err)
32119 +                                       continue;
32120 +                               break;
32121 +                       }
32122 +                       AuIOErr("whiteout name too long %.*s\n",
32123 +                               str->len, str->name);
32124 +                       err = -EIO;
32125 +                       break;
32126 +               }
32127 +       }
32128 +       free_page((unsigned long)wh_name.name);
32129 +
32130 +out:
32131 +       return err;
32132 +}
32133 +
32134 +struct del_wh_children_args {
32135 +       int *errp;
32136 +       struct dentry *h_dentry;
32137 +       struct au_nhash *whlist;
32138 +       aufs_bindex_t bindex;
32139 +       struct au_branch *br;
32140 +};
32141 +
32142 +static void call_del_wh_children(void *args)
32143 +{
32144 +       struct del_wh_children_args *a = args;
32145 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
32146 +}
32147 +
32148 +/* ---------------------------------------------------------------------- */
32149 +
32150 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
32151 +{
32152 +       struct au_whtmp_rmdir *whtmp;
32153 +       int err;
32154 +       unsigned int rdhash;
32155 +
32156 +       SiMustAnyLock(sb);
32157 +
32158 +       whtmp = kmalloc(sizeof(*whtmp), gfp);
32159 +       if (unlikely(!whtmp)) {
32160 +               whtmp = ERR_PTR(-ENOMEM);
32161 +               goto out;
32162 +       }
32163 +
32164 +       whtmp->dir = NULL;
32165 +       whtmp->br = NULL;
32166 +       whtmp->wh_dentry = NULL;
32167 +       /* no estimation for dir size */
32168 +       rdhash = au_sbi(sb)->si_rdhash;
32169 +       if (!rdhash)
32170 +               rdhash = AUFS_RDHASH_DEF;
32171 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
32172 +       if (unlikely(err)) {
32173 +               kfree(whtmp);
32174 +               whtmp = ERR_PTR(err);
32175 +       }
32176 +
32177 +out:
32178 +       return whtmp;
32179 +}
32180 +
32181 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
32182 +{
32183 +       if (whtmp->br)
32184 +               atomic_dec(&whtmp->br->br_count);
32185 +       dput(whtmp->wh_dentry);
32186 +       iput(whtmp->dir);
32187 +       au_nhash_wh_free(&whtmp->whlist);
32188 +       kfree(whtmp);
32189 +}
32190 +
32191 +/*
32192 + * rmdir the whiteouted temporary named dir @h_dentry.
32193 + * @whlist: whiteouted children.
32194 + */
32195 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
32196 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
32197 +{
32198 +       int err;
32199 +       unsigned int h_nlink;
32200 +       struct path h_tmp;
32201 +       struct inode *wh_inode, *h_dir;
32202 +       struct au_branch *br;
32203 +
32204 +       h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
32205 +       IMustLock(h_dir);
32206 +
32207 +       br = au_sbr(dir->i_sb, bindex);
32208 +       wh_inode = wh_dentry->d_inode;
32209 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
32210 +
32211 +       /*
32212 +        * someone else might change some whiteouts while we were sleeping.
32213 +        * it means this whlist may have an obsoleted entry.
32214 +        */
32215 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
32216 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
32217 +       else {
32218 +               int wkq_err;
32219 +               struct del_wh_children_args args = {
32220 +                       .errp           = &err,
32221 +                       .h_dentry       = wh_dentry,
32222 +                       .whlist         = whlist,
32223 +                       .bindex         = bindex,
32224 +                       .br             = br
32225 +               };
32226 +
32227 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
32228 +               if (unlikely(wkq_err))
32229 +                       err = wkq_err;
32230 +       }
32231 +       mutex_unlock(&wh_inode->i_mutex);
32232 +
32233 +       if (!err) {
32234 +               h_tmp.dentry = wh_dentry;
32235 +               h_tmp.mnt = au_br_mnt(br);
32236 +               h_nlink = h_dir->i_nlink;
32237 +               err = vfsub_rmdir(h_dir, &h_tmp);
32238 +               /* some fs doesn't change the parent nlink in some cases */
32239 +               h_nlink -= h_dir->i_nlink;
32240 +       }
32241 +
32242 +       if (!err) {
32243 +               if (au_ibstart(dir) == bindex) {
32244 +                       /* todo: dir->i_mutex is necessary */
32245 +                       au_cpup_attr_timesizes(dir);
32246 +                       if (h_nlink)
32247 +                               vfsub_drop_nlink(dir);
32248 +               }
32249 +               return 0; /* success */
32250 +       }
32251 +
32252 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
32253 +       return err;
32254 +}
32255 +
32256 +static void call_rmdir_whtmp(void *args)
32257 +{
32258 +       int err;
32259 +       aufs_bindex_t bindex;
32260 +       struct au_whtmp_rmdir *a = args;
32261 +       struct super_block *sb;
32262 +       struct dentry *h_parent;
32263 +       struct inode *h_dir;
32264 +       struct au_hinode *hdir;
32265 +
32266 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
32267 +       /* mutex_lock(&a->dir->i_mutex); */
32268 +       err = -EROFS;
32269 +       sb = a->dir->i_sb;
32270 +       si_read_lock(sb, !AuLock_FLUSH);
32271 +       if (!au_br_writable(a->br->br_perm))
32272 +               goto out;
32273 +       bindex = au_br_index(sb, a->br->br_id);
32274 +       if (unlikely(bindex < 0))
32275 +               goto out;
32276 +
32277 +       err = -EIO;
32278 +       ii_write_lock_parent(a->dir);
32279 +       h_parent = dget_parent(a->wh_dentry);
32280 +       h_dir = h_parent->d_inode;
32281 +       hdir = au_hi(a->dir, bindex);
32282 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
32283 +       if (unlikely(err))
32284 +               goto out_mnt;
32285 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
32286 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
32287 +                         a->br);
32288 +       if (!err)
32289 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
32290 +       au_hn_imtx_unlock(hdir);
32291 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
32292 +
32293 +out_mnt:
32294 +       dput(h_parent);
32295 +       ii_write_unlock(a->dir);
32296 +out:
32297 +       /* mutex_unlock(&a->dir->i_mutex); */
32298 +       au_whtmp_rmdir_free(a);
32299 +       si_read_unlock(sb);
32300 +       au_nwt_done(&au_sbi(sb)->si_nowait);
32301 +       if (unlikely(err))
32302 +               AuIOErr("err %d\n", err);
32303 +}
32304 +
32305 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
32306 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
32307 +{
32308 +       int wkq_err;
32309 +       struct super_block *sb;
32310 +
32311 +       IMustLock(dir);
32312 +
32313 +       /* all post-process will be done in do_rmdir_whtmp(). */
32314 +       sb = dir->i_sb;
32315 +       args->dir = au_igrab(dir);
32316 +       args->br = au_sbr(sb, bindex);
32317 +       atomic_inc(&args->br->br_count);
32318 +       args->wh_dentry = dget(wh_dentry);
32319 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
32320 +       if (unlikely(wkq_err)) {
32321 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
32322 +               au_whtmp_rmdir_free(args);
32323 +       }
32324 +}
32325 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
32326 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
32327 +++ linux/fs/aufs/whout.h       2015-04-13 15:10:20.790157026 +0200
32328 @@ -0,0 +1,85 @@
32329 +/*
32330 + * Copyright (C) 2005-2015 Junjiro R. Okajima
32331 + *
32332 + * This program, aufs is free software; you can redistribute it and/or modify
32333 + * it under the terms of the GNU General Public License as published by
32334 + * the Free Software Foundation; either version 2 of the License, or
32335 + * (at your option) any later version.
32336 + *
32337 + * This program is distributed in the hope that it will be useful,
32338 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32339 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32340 + * GNU General Public License for more details.
32341 + *
32342 + * You should have received a copy of the GNU General Public License
32343 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32344 + */
32345 +
32346 +/*
32347 + * whiteout for logical deletion and opaque directory
32348 + */
32349 +
32350 +#ifndef __AUFS_WHOUT_H__
32351 +#define __AUFS_WHOUT_H__
32352 +
32353 +#ifdef __KERNEL__
32354 +
32355 +#include "dir.h"
32356 +
32357 +/* whout.c */
32358 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
32359 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
32360 +int au_diropq_test(struct dentry *h_dentry);
32361 +struct au_branch;
32362 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
32363 +                            struct qstr *prefix);
32364 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
32365 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32366 +                       struct dentry *dentry);
32367 +int au_wh_init(struct au_branch *br, struct super_block *sb);
32368 +
32369 +/* diropq flags */
32370 +#define AuDiropq_CREATE        1
32371 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
32372 +#define au_fset_diropq(flags, name) \
32373 +       do { (flags) |= AuDiropq_##name; } while (0)
32374 +#define au_fclr_diropq(flags, name) \
32375 +       do { (flags) &= ~AuDiropq_##name; } while (0)
32376 +
32377 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
32378 +                            unsigned int flags);
32379 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
32380 +                         struct au_branch *br);
32381 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
32382 +                           struct dentry *h_parent);
32383 +
32384 +/* real rmdir for the whiteout-ed dir */
32385 +struct au_whtmp_rmdir {
32386 +       struct inode *dir;
32387 +       struct au_branch *br;
32388 +       struct dentry *wh_dentry;
32389 +       struct au_nhash whlist;
32390 +};
32391 +
32392 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
32393 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
32394 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
32395 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
32396 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
32397 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
32398 +
32399 +/* ---------------------------------------------------------------------- */
32400 +
32401 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
32402 +                                             aufs_bindex_t bindex)
32403 +{
32404 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
32405 +}
32406 +
32407 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
32408 +{
32409 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
32410 +}
32411 +
32412 +#endif /* __KERNEL__ */
32413 +#endif /* __AUFS_WHOUT_H__ */
32414 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
32415 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
32416 +++ linux/fs/aufs/wkq.c 2015-04-13 15:10:20.790157026 +0200
32417 @@ -0,0 +1,213 @@
32418 +/*
32419 + * Copyright (C) 2005-2015 Junjiro R. Okajima
32420 + *
32421 + * This program, aufs is free software; you can redistribute it and/or modify
32422 + * it under the terms of the GNU General Public License as published by
32423 + * the Free Software Foundation; either version 2 of the License, or
32424 + * (at your option) any later version.
32425 + *
32426 + * This program is distributed in the hope that it will be useful,
32427 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32428 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32429 + * GNU General Public License for more details.
32430 + *
32431 + * You should have received a copy of the GNU General Public License
32432 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32433 + */
32434 +
32435 +/*
32436 + * workqueue for asynchronous/super-io operations
32437 + * todo: try new dredential scheme
32438 + */
32439 +
32440 +#include <linux/module.h>
32441 +#include "aufs.h"
32442 +
32443 +/* internal workqueue named AUFS_WKQ_NAME */
32444 +
32445 +static struct workqueue_struct *au_wkq;
32446 +
32447 +struct au_wkinfo {
32448 +       struct work_struct wk;
32449 +       struct kobject *kobj;
32450 +
32451 +       unsigned int flags; /* see wkq.h */
32452 +
32453 +       au_wkq_func_t func;
32454 +       void *args;
32455 +
32456 +       struct completion *comp;
32457 +};
32458 +
32459 +/* ---------------------------------------------------------------------- */
32460 +
32461 +static void wkq_func(struct work_struct *wk)
32462 +{
32463 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
32464 +
32465 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
32466 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
32467 +
32468 +       wkinfo->func(wkinfo->args);
32469 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
32470 +               complete(wkinfo->comp);
32471 +       else {
32472 +               kobject_put(wkinfo->kobj);
32473 +               module_put(THIS_MODULE); /* todo: ?? */
32474 +               kfree(wkinfo);
32475 +       }
32476 +}
32477 +
32478 +/*
32479 + * Since struct completion is large, try allocating it dynamically.
32480 + */
32481 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
32482 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
32483 +
32484 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
32485 +{
32486 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
32487 +       if (*comp) {
32488 +               init_completion(*comp);
32489 +               wkinfo->comp = *comp;
32490 +               return 0;
32491 +       }
32492 +       return -ENOMEM;
32493 +}
32494 +
32495 +static void au_wkq_comp_free(struct completion *comp)
32496 +{
32497 +       kfree(comp);
32498 +}
32499 +
32500 +#else
32501 +
32502 +/* no braces */
32503 +#define AuWkqCompDeclare(name) \
32504 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
32505 +       struct completion *comp = &_ ## name
32506 +
32507 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
32508 +{
32509 +       wkinfo->comp = *comp;
32510 +       return 0;
32511 +}
32512 +
32513 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
32514 +{
32515 +       /* empty */
32516 +}
32517 +#endif /* 4KSTACKS */
32518 +
32519 +static void au_wkq_run(struct au_wkinfo *wkinfo)
32520 +{
32521 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
32522 +               if (au_wkq_test()) {
32523 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
32524 +                               " due to a dead dir by UDBA?\n");
32525 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
32526 +               }
32527 +       } else
32528 +               au_dbg_verify_kthread();
32529 +
32530 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
32531 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
32532 +               queue_work(au_wkq, &wkinfo->wk);
32533 +       } else {
32534 +               INIT_WORK(&wkinfo->wk, wkq_func);
32535 +               schedule_work(&wkinfo->wk);
32536 +       }
32537 +}
32538 +
32539 +/*
32540 + * Be careful. It is easy to make deadlock happen.
32541 + * processA: lock, wkq and wait
32542 + * processB: wkq and wait, lock in wkq
32543 + * --> deadlock
32544 + */
32545 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
32546 +{
32547 +       int err;
32548 +       AuWkqCompDeclare(comp);
32549 +       struct au_wkinfo wkinfo = {
32550 +               .flags  = flags,
32551 +               .func   = func,
32552 +               .args   = args
32553 +       };
32554 +
32555 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
32556 +       if (!err) {
32557 +               au_wkq_run(&wkinfo);
32558 +               /* no timeout, no interrupt */
32559 +               wait_for_completion(wkinfo.comp);
32560 +               au_wkq_comp_free(comp);
32561 +               destroy_work_on_stack(&wkinfo.wk);
32562 +       }
32563 +
32564 +       return err;
32565 +
32566 +}
32567 +
32568 +/*
32569 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
32570 + * problem in a concurrent umounting.
32571 + */
32572 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
32573 +                 unsigned int flags)
32574 +{
32575 +       int err;
32576 +       struct au_wkinfo *wkinfo;
32577 +
32578 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
32579 +
32580 +       /*
32581 +        * wkq_func() must free this wkinfo.
32582 +        * it highly depends upon the implementation of workqueue.
32583 +        */
32584 +       err = 0;
32585 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
32586 +       if (wkinfo) {
32587 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
32588 +               wkinfo->flags = flags & ~AuWkq_WAIT;
32589 +               wkinfo->func = func;
32590 +               wkinfo->args = args;
32591 +               wkinfo->comp = NULL;
32592 +               kobject_get(wkinfo->kobj);
32593 +               __module_get(THIS_MODULE); /* todo: ?? */
32594 +
32595 +               au_wkq_run(wkinfo);
32596 +       } else {
32597 +               err = -ENOMEM;
32598 +               au_nwt_done(&au_sbi(sb)->si_nowait);
32599 +       }
32600 +
32601 +       return err;
32602 +}
32603 +
32604 +/* ---------------------------------------------------------------------- */
32605 +
32606 +void au_nwt_init(struct au_nowait_tasks *nwt)
32607 +{
32608 +       atomic_set(&nwt->nw_len, 0);
32609 +       /* smp_mb(); */ /* atomic_set */
32610 +       init_waitqueue_head(&nwt->nw_wq);
32611 +}
32612 +
32613 +void au_wkq_fin(void)
32614 +{
32615 +       destroy_workqueue(au_wkq);
32616 +}
32617 +
32618 +int __init au_wkq_init(void)
32619 +{
32620 +       int err;
32621 +
32622 +       err = 0;
32623 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
32624 +       if (IS_ERR(au_wkq))
32625 +               err = PTR_ERR(au_wkq);
32626 +       else if (!au_wkq)
32627 +               err = -ENOMEM;
32628 +
32629 +       return err;
32630 +}
32631 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
32632 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
32633 +++ linux/fs/aufs/wkq.h 2015-04-13 15:10:20.790157026 +0200
32634 @@ -0,0 +1,91 @@
32635 +/*
32636 + * Copyright (C) 2005-2015 Junjiro R. Okajima
32637 + *
32638 + * This program, aufs is free software; you can redistribute it and/or modify
32639 + * it under the terms of the GNU General Public License as published by
32640 + * the Free Software Foundation; either version 2 of the License, or
32641 + * (at your option) any later version.
32642 + *
32643 + * This program is distributed in the hope that it will be useful,
32644 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32645 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32646 + * GNU General Public License for more details.
32647 + *
32648 + * You should have received a copy of the GNU General Public License
32649 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32650 + */
32651 +
32652 +/*
32653 + * workqueue for asynchronous/super-io operations
32654 + * todo: try new credentials management scheme
32655 + */
32656 +
32657 +#ifndef __AUFS_WKQ_H__
32658 +#define __AUFS_WKQ_H__
32659 +
32660 +#ifdef __KERNEL__
32661 +
32662 +struct super_block;
32663 +
32664 +/* ---------------------------------------------------------------------- */
32665 +
32666 +/*
32667 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
32668 + */
32669 +struct au_nowait_tasks {
32670 +       atomic_t                nw_len;
32671 +       wait_queue_head_t       nw_wq;
32672 +};
32673 +
32674 +/* ---------------------------------------------------------------------- */
32675 +
32676 +typedef void (*au_wkq_func_t)(void *args);
32677 +
32678 +/* wkq flags */
32679 +#define AuWkq_WAIT     1
32680 +#define AuWkq_NEST     (1 << 1)
32681 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
32682 +#define au_fset_wkq(flags, name) \
32683 +       do { (flags) |= AuWkq_##name; } while (0)
32684 +#define au_fclr_wkq(flags, name) \
32685 +       do { (flags) &= ~AuWkq_##name; } while (0)
32686 +
32687 +#ifndef CONFIG_AUFS_HNOTIFY
32688 +#undef AuWkq_NEST
32689 +#define AuWkq_NEST     0
32690 +#endif
32691 +
32692 +/* wkq.c */
32693 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
32694 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
32695 +                 unsigned int flags);
32696 +void au_nwt_init(struct au_nowait_tasks *nwt);
32697 +int __init au_wkq_init(void);
32698 +void au_wkq_fin(void);
32699 +
32700 +/* ---------------------------------------------------------------------- */
32701 +
32702 +static inline int au_wkq_test(void)
32703 +{
32704 +       return current->flags & PF_WQ_WORKER;
32705 +}
32706 +
32707 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
32708 +{
32709 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
32710 +}
32711 +
32712 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
32713 +{
32714 +       if (atomic_dec_and_test(&nwt->nw_len))
32715 +               wake_up_all(&nwt->nw_wq);
32716 +}
32717 +
32718 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
32719 +{
32720 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
32721 +       return 0;
32722 +}
32723 +
32724 +#endif /* __KERNEL__ */
32725 +#endif /* __AUFS_WKQ_H__ */
32726 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
32727 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
32728 +++ linux/fs/aufs/xattr.c       2015-04-13 15:10:20.790157026 +0200
32729 @@ -0,0 +1,338 @@
32730 +/*
32731 + * Copyright (C) 2014-2015 Junjiro R. Okajima
32732 + *
32733 + * This program, aufs is free software; you can redistribute it and/or modify
32734 + * it under the terms of the GNU General Public License as published by
32735 + * the Free Software Foundation; either version 2 of the License, or
32736 + * (at your option) any later version.
32737 + *
32738 + * This program is distributed in the hope that it will be useful,
32739 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32740 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32741 + * GNU General Public License for more details.
32742 + *
32743 + * You should have received a copy of the GNU General Public License
32744 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32745 + */
32746 +
32747 +/*
32748 + * handling xattr functions
32749 + */
32750 +
32751 +#include <linux/xattr.h>
32752 +#include "aufs.h"
32753 +
32754 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
32755 +{
32756 +       if (!ignore_flags)
32757 +               goto out;
32758 +       switch (err) {
32759 +       case -ENOMEM:
32760 +       case -EDQUOT:
32761 +               goto out;
32762 +       }
32763 +
32764 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
32765 +               err = 0;
32766 +               goto out;
32767 +       }
32768 +
32769 +#define cmp(brattr, prefix) do {                                       \
32770 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
32771 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
32772 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
32773 +                               err = 0;                                \
32774 +                       goto out;                                       \
32775 +               }                                                       \
32776 +       } while (0)
32777 +
32778 +       cmp(SEC, SECURITY);
32779 +       cmp(SYS, SYSTEM);
32780 +       cmp(TR, TRUSTED);
32781 +       cmp(USR, USER);
32782 +#undef cmp
32783 +
32784 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
32785 +               err = 0;
32786 +
32787 +out:
32788 +       return err;
32789 +}
32790 +
32791 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
32792 +
32793 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
32794 +                           char *name, char **buf, unsigned int ignore_flags,
32795 +                           unsigned int verbose)
32796 +{
32797 +       int err;
32798 +       ssize_t ssz;
32799 +       struct inode *h_idst;
32800 +
32801 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
32802 +       err = ssz;
32803 +       if (unlikely(err <= 0)) {
32804 +               AuTraceErr(err);
32805 +               if (err == -ENODATA
32806 +                   || (err == -EOPNOTSUPP
32807 +                       && (ignore_flags & au_xattr_out_of_list)))
32808 +                       err = 0;
32809 +               goto out;
32810 +       }
32811 +
32812 +       /* unlock it temporary */
32813 +       h_idst = h_dst->d_inode;
32814 +       mutex_unlock(&h_idst->i_mutex);
32815 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
32816 +       mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
32817 +       if (unlikely(err)) {
32818 +               if (verbose || au_debug_test())
32819 +                       pr_err("%s, err %d\n", name, err);
32820 +               err = au_xattr_ignore(err, name, ignore_flags);
32821 +       }
32822 +
32823 +out:
32824 +       return err;
32825 +}
32826 +
32827 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
32828 +                 unsigned int verbose)
32829 +{
32830 +       int err, unlocked, acl_access, acl_default;
32831 +       ssize_t ssz;
32832 +       struct inode *h_isrc, *h_idst;
32833 +       char *value, *p, *o, *e;
32834 +
32835 +       /* try stopping to update the source inode while we are referencing */
32836 +       /* there should not be the parent-child relationship between them */
32837 +       h_isrc = h_src->d_inode;
32838 +       h_idst = h_dst->d_inode;
32839 +       mutex_unlock(&h_idst->i_mutex);
32840 +       mutex_lock_nested(&h_isrc->i_mutex, AuLsc_I_CHILD);
32841 +       mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
32842 +       unlocked = 0;
32843 +
32844 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
32845 +       ssz = vfs_listxattr(h_src, NULL, 0);
32846 +       err = ssz;
32847 +       if (unlikely(err < 0)) {
32848 +               AuTraceErr(err);
32849 +               if (err == -ENODATA
32850 +                   || err == -EOPNOTSUPP)
32851 +                       err = 0;        /* ignore */
32852 +               goto out;
32853 +       }
32854 +
32855 +       err = 0;
32856 +       p = NULL;
32857 +       o = NULL;
32858 +       if (ssz) {
32859 +               err = -ENOMEM;
32860 +               p = kmalloc(ssz, GFP_NOFS);
32861 +               o = p;
32862 +               if (unlikely(!p))
32863 +                       goto out;
32864 +               err = vfs_listxattr(h_src, p, ssz);
32865 +       }
32866 +       mutex_unlock(&h_isrc->i_mutex);
32867 +       unlocked = 1;
32868 +       AuDbg("err %d, ssz %zd\n", err, ssz);
32869 +       if (unlikely(err < 0))
32870 +               goto out_free;
32871 +
32872 +       err = 0;
32873 +       e = p + ssz;
32874 +       value = NULL;
32875 +       acl_access = 0;
32876 +       acl_default = 0;
32877 +       while (!err && p < e) {
32878 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
32879 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
32880 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
32881 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
32882 +                                       - 1);
32883 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
32884 +                                      verbose);
32885 +               p += strlen(p) + 1;
32886 +       }
32887 +       AuTraceErr(err);
32888 +       ignore_flags |= au_xattr_out_of_list;
32889 +       if (!err && !acl_access) {
32890 +               err = au_do_cpup_xattr(h_dst, h_src,
32891 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
32892 +                                      ignore_flags, verbose);
32893 +               AuTraceErr(err);
32894 +       }
32895 +       if (!err && !acl_default) {
32896 +               err = au_do_cpup_xattr(h_dst, h_src,
32897 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
32898 +                                      ignore_flags, verbose);
32899 +               AuTraceErr(err);
32900 +       }
32901 +
32902 +       kfree(value);
32903 +
32904 +out_free:
32905 +       kfree(o);
32906 +out:
32907 +       if (!unlocked)
32908 +               mutex_unlock(&h_isrc->i_mutex);
32909 +       AuTraceErr(err);
32910 +       return err;
32911 +}
32912 +
32913 +/* ---------------------------------------------------------------------- */
32914 +
32915 +enum {
32916 +       AU_XATTR_LIST,
32917 +       AU_XATTR_GET
32918 +};
32919 +
32920 +struct au_lgxattr {
32921 +       int type;
32922 +       union {
32923 +               struct {
32924 +                       char    *list;
32925 +                       size_t  size;
32926 +               } list;
32927 +               struct {
32928 +                       const char      *name;
32929 +                       void            *value;
32930 +                       size_t          size;
32931 +               } get;
32932 +       } u;
32933 +};
32934 +
32935 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
32936 +{
32937 +       ssize_t err;
32938 +       struct path h_path;
32939 +       struct super_block *sb;
32940 +
32941 +       sb = dentry->d_sb;
32942 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
32943 +       if (unlikely(err))
32944 +               goto out;
32945 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path);
32946 +       if (unlikely(err))
32947 +               goto out_si;
32948 +       if (unlikely(!h_path.dentry))
32949 +               /* illegally overlapped or something */
32950 +               goto out_di; /* pretending success */
32951 +
32952 +       /* always topmost entry only */
32953 +       switch (arg->type) {
32954 +       case AU_XATTR_LIST:
32955 +               err = vfs_listxattr(h_path.dentry,
32956 +                                   arg->u.list.list, arg->u.list.size);
32957 +               break;
32958 +       case AU_XATTR_GET:
32959 +               err = vfs_getxattr(h_path.dentry,
32960 +                                  arg->u.get.name, arg->u.get.value,
32961 +                                  arg->u.get.size);
32962 +               break;
32963 +       }
32964 +
32965 +out_di:
32966 +       di_read_unlock(dentry, AuLock_IR);
32967 +out_si:
32968 +       si_read_unlock(sb);
32969 +out:
32970 +       AuTraceErr(err);
32971 +       return err;
32972 +}
32973 +
32974 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
32975 +{
32976 +       struct au_lgxattr arg = {
32977 +               .type = AU_XATTR_LIST,
32978 +               .u.list = {
32979 +                       .list   = list,
32980 +                       .size   = size
32981 +               },
32982 +       };
32983 +
32984 +       return au_lgxattr(dentry, &arg);
32985 +}
32986 +
32987 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
32988 +                     size_t size)
32989 +{
32990 +       struct au_lgxattr arg = {
32991 +               .type = AU_XATTR_GET,
32992 +               .u.get = {
32993 +                       .name   = name,
32994 +                       .value  = value,
32995 +                       .size   = size
32996 +               },
32997 +       };
32998 +
32999 +       return au_lgxattr(dentry, &arg);
33000 +}
33001 +
33002 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
33003 +                 size_t size, int flags)
33004 +{
33005 +       struct au_srxattr arg = {
33006 +               .type = AU_XATTR_SET,
33007 +               .u.set = {
33008 +                       .name   = name,
33009 +                       .value  = value,
33010 +                       .size   = size,
33011 +                       .flags  = flags
33012 +               },
33013 +       };
33014 +
33015 +       return au_srxattr(dentry, &arg);
33016 +}
33017 +
33018 +int aufs_removexattr(struct dentry *dentry, const char *name)
33019 +{
33020 +       struct au_srxattr arg = {
33021 +               .type = AU_XATTR_REMOVE,
33022 +               .u.remove = {
33023 +                       .name   = name
33024 +               },
33025 +       };
33026 +
33027 +       return au_srxattr(dentry, &arg);
33028 +}
33029 +
33030 +/* ---------------------------------------------------------------------- */
33031 +
33032 +#if 0
33033 +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
33034 +                           const char *name, size_t name_len, int type)
33035 +{
33036 +       return aufs_listxattr(dentry, list, list_size);
33037 +}
33038 +
33039 +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
33040 +                       size_t size, int type)
33041 +{
33042 +       return aufs_getxattr(dentry, name, buffer, size);
33043 +}
33044 +
33045 +static int au_xattr_set(struct dentry *dentry, const char *name,
33046 +                       const void *value, size_t size, int flags, int type)
33047 +{
33048 +       return aufs_setxattr(dentry, name, value, size, flags);
33049 +}
33050 +
33051 +static const struct xattr_handler au_xattr_handler = {
33052 +       /* no prefix, no flags */
33053 +       .list   = au_xattr_list,
33054 +       .get    = au_xattr_get,
33055 +       .set    = au_xattr_set
33056 +       /* why no remove? */
33057 +};
33058 +
33059 +static const struct xattr_handler *au_xattr_handlers[] = {
33060 +       &au_xattr_handler
33061 +};
33062 +
33063 +void au_xattr_init(struct super_block *sb)
33064 +{
33065 +       /* sb->s_xattr = au_xattr_handlers; */
33066 +}
33067 +#endif
33068 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
33069 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
33070 +++ linux/fs/aufs/xino.c        2015-04-13 15:10:20.790157026 +0200
33071 @@ -0,0 +1,1318 @@
33072 +/*
33073 + * Copyright (C) 2005-2015 Junjiro R. Okajima
33074 + *
33075 + * This program, aufs is free software; you can redistribute it and/or modify
33076 + * it under the terms of the GNU General Public License as published by
33077 + * the Free Software Foundation; either version 2 of the License, or
33078 + * (at your option) any later version.
33079 + *
33080 + * This program is distributed in the hope that it will be useful,
33081 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33082 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33083 + * GNU General Public License for more details.
33084 + *
33085 + * You should have received a copy of the GNU General Public License
33086 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33087 + */
33088 +
33089 +/*
33090 + * external inode number translation table and bitmap
33091 + */
33092 +
33093 +#include <linux/seq_file.h>
33094 +#include <linux/statfs.h>
33095 +#include "aufs.h"
33096 +
33097 +/* todo: unnecessary to support mmap_sem since kernel-space? */
33098 +ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
33099 +                  loff_t *pos)
33100 +{
33101 +       ssize_t err;
33102 +       mm_segment_t oldfs;
33103 +       union {
33104 +               void *k;
33105 +               char __user *u;
33106 +       } buf;
33107 +
33108 +       buf.k = kbuf;
33109 +       oldfs = get_fs();
33110 +       set_fs(KERNEL_DS);
33111 +       do {
33112 +               /* todo: signal_pending? */
33113 +               err = func(file, buf.u, size, pos);
33114 +       } while (err == -EAGAIN || err == -EINTR);
33115 +       set_fs(oldfs);
33116 +
33117 +#if 0 /* reserved for future use */
33118 +       if (err > 0)
33119 +               fsnotify_access(file->f_path.dentry);
33120 +#endif
33121 +
33122 +       return err;
33123 +}
33124 +
33125 +/* ---------------------------------------------------------------------- */
33126 +
33127 +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
33128 +                             size_t size, loff_t *pos)
33129 +{
33130 +       ssize_t err;
33131 +       mm_segment_t oldfs;
33132 +       union {
33133 +               void *k;
33134 +               const char __user *u;
33135 +       } buf;
33136 +
33137 +       buf.k = kbuf;
33138 +       oldfs = get_fs();
33139 +       set_fs(KERNEL_DS);
33140 +       do {
33141 +               /* todo: signal_pending? */
33142 +               err = func(file, buf.u, size, pos);
33143 +       } while (err == -EAGAIN || err == -EINTR);
33144 +       set_fs(oldfs);
33145 +
33146 +#if 0 /* reserved for future use */
33147 +       if (err > 0)
33148 +               fsnotify_modify(file->f_path.dentry);
33149 +#endif
33150 +
33151 +       return err;
33152 +}
33153 +
33154 +struct do_xino_fwrite_args {
33155 +       ssize_t *errp;
33156 +       au_writef_t func;
33157 +       struct file *file;
33158 +       void *buf;
33159 +       size_t size;
33160 +       loff_t *pos;
33161 +};
33162 +
33163 +static void call_do_xino_fwrite(void *args)
33164 +{
33165 +       struct do_xino_fwrite_args *a = args;
33166 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
33167 +}
33168 +
33169 +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
33170 +                   loff_t *pos)
33171 +{
33172 +       ssize_t err;
33173 +
33174 +       /* todo: signal block and no wkq? */
33175 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
33176 +               lockdep_off();
33177 +               err = do_xino_fwrite(func, file, buf, size, pos);
33178 +               lockdep_on();
33179 +       } else {
33180 +               /*
33181 +                * it breaks RLIMIT_FSIZE and normal user's limit,
33182 +                * users should care about quota and real 'filesystem full.'
33183 +                */
33184 +               int wkq_err;
33185 +               struct do_xino_fwrite_args args = {
33186 +                       .errp   = &err,
33187 +                       .func   = func,
33188 +                       .file   = file,
33189 +                       .buf    = buf,
33190 +                       .size   = size,
33191 +                       .pos    = pos
33192 +               };
33193 +
33194 +               wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
33195 +               if (unlikely(wkq_err))
33196 +                       err = wkq_err;
33197 +       }
33198 +
33199 +       return err;
33200 +}
33201 +
33202 +/* ---------------------------------------------------------------------- */
33203 +
33204 +/*
33205 + * create a new xinofile at the same place/path as @base_file.
33206 + */
33207 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
33208 +{
33209 +       struct file *file;
33210 +       struct dentry *base, *parent;
33211 +       struct inode *dir, *delegated;
33212 +       struct qstr *name;
33213 +       struct path path;
33214 +       int err;
33215 +
33216 +       base = base_file->f_path.dentry;
33217 +       parent = base->d_parent; /* dir inode is locked */
33218 +       dir = parent->d_inode;
33219 +       IMustLock(dir);
33220 +
33221 +       file = ERR_PTR(-EINVAL);
33222 +       name = &base->d_name;
33223 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
33224 +       if (IS_ERR(path.dentry)) {
33225 +               file = (void *)path.dentry;
33226 +               pr_err("%pd lookup err %ld\n",
33227 +                      base, PTR_ERR(path.dentry));
33228 +               goto out;
33229 +       }
33230 +
33231 +       /* no need to mnt_want_write() since we call dentry_open() later */
33232 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
33233 +       if (unlikely(err)) {
33234 +               file = ERR_PTR(err);
33235 +               pr_err("%pd create err %d\n", base, err);
33236 +               goto out_dput;
33237 +       }
33238 +
33239 +       path.mnt = base_file->f_path.mnt;
33240 +       file = vfsub_dentry_open(&path,
33241 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
33242 +                                /* | __FMODE_NONOTIFY */);
33243 +       if (IS_ERR(file)) {
33244 +               pr_err("%pd open err %ld\n", base, PTR_ERR(file));
33245 +               goto out_dput;
33246 +       }
33247 +
33248 +       delegated = NULL;
33249 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
33250 +       if (unlikely(err == -EWOULDBLOCK)) {
33251 +               pr_warn("cannot retry for NFSv4 delegation"
33252 +                       " for an internal unlink\n");
33253 +               iput(delegated);
33254 +       }
33255 +       if (unlikely(err)) {
33256 +               pr_err("%pd unlink err %d\n", base, err);
33257 +               goto out_fput;
33258 +       }
33259 +
33260 +       if (copy_src) {
33261 +               /* no one can touch copy_src xino */
33262 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
33263 +               if (unlikely(err)) {
33264 +                       pr_err("%pd copy err %d\n", base, err);
33265 +                       goto out_fput;
33266 +               }
33267 +       }
33268 +       goto out_dput; /* success */
33269 +
33270 +out_fput:
33271 +       fput(file);
33272 +       file = ERR_PTR(err);
33273 +out_dput:
33274 +       dput(path.dentry);
33275 +out:
33276 +       return file;
33277 +}
33278 +
33279 +struct au_xino_lock_dir {
33280 +       struct au_hinode *hdir;
33281 +       struct dentry *parent;
33282 +       struct mutex *mtx;
33283 +};
33284 +
33285 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
33286 +                            struct au_xino_lock_dir *ldir)
33287 +{
33288 +       aufs_bindex_t brid, bindex;
33289 +
33290 +       ldir->hdir = NULL;
33291 +       bindex = -1;
33292 +       brid = au_xino_brid(sb);
33293 +       if (brid >= 0)
33294 +               bindex = au_br_index(sb, brid);
33295 +       if (bindex >= 0) {
33296 +               ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
33297 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
33298 +       } else {
33299 +               ldir->parent = dget_parent(xino->f_path.dentry);
33300 +               ldir->mtx = &ldir->parent->d_inode->i_mutex;
33301 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
33302 +       }
33303 +}
33304 +
33305 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
33306 +{
33307 +       if (ldir->hdir)
33308 +               au_hn_imtx_unlock(ldir->hdir);
33309 +       else {
33310 +               mutex_unlock(ldir->mtx);
33311 +               dput(ldir->parent);
33312 +       }
33313 +}
33314 +
33315 +/* ---------------------------------------------------------------------- */
33316 +
33317 +/* trucate xino files asynchronously */
33318 +
33319 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
33320 +{
33321 +       int err;
33322 +       unsigned long jiffy;
33323 +       blkcnt_t blocks;
33324 +       aufs_bindex_t bi, bend;
33325 +       struct kstatfs *st;
33326 +       struct au_branch *br;
33327 +       struct file *new_xino, *file;
33328 +       struct super_block *h_sb;
33329 +       struct au_xino_lock_dir ldir;
33330 +
33331 +       err = -ENOMEM;
33332 +       st = kzalloc(sizeof(*st), GFP_NOFS);
33333 +       if (unlikely(!st))
33334 +               goto out;
33335 +
33336 +       err = -EINVAL;
33337 +       bend = au_sbend(sb);
33338 +       if (unlikely(bindex < 0 || bend < bindex))
33339 +               goto out_st;
33340 +       br = au_sbr(sb, bindex);
33341 +       file = br->br_xino.xi_file;
33342 +       if (!file)
33343 +               goto out_st;
33344 +
33345 +       err = vfs_statfs(&file->f_path, st);
33346 +       if (unlikely(err))
33347 +               AuErr1("statfs err %d, ignored\n", err);
33348 +       jiffy = jiffies;
33349 +       blocks = file_inode(file)->i_blocks;
33350 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
33351 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
33352 +
33353 +       au_xino_lock_dir(sb, file, &ldir);
33354 +       /* mnt_want_write() is unnecessary here */
33355 +       new_xino = au_xino_create2(file, file);
33356 +       au_xino_unlock_dir(&ldir);
33357 +       err = PTR_ERR(new_xino);
33358 +       if (IS_ERR(new_xino)) {
33359 +               pr_err("err %d, ignored\n", err);
33360 +               goto out_st;
33361 +       }
33362 +       err = 0;
33363 +       fput(file);
33364 +       br->br_xino.xi_file = new_xino;
33365 +
33366 +       h_sb = au_br_sb(br);
33367 +       for (bi = 0; bi <= bend; bi++) {
33368 +               if (unlikely(bi == bindex))
33369 +                       continue;
33370 +               br = au_sbr(sb, bi);
33371 +               if (au_br_sb(br) != h_sb)
33372 +                       continue;
33373 +
33374 +               fput(br->br_xino.xi_file);
33375 +               br->br_xino.xi_file = new_xino;
33376 +               get_file(new_xino);
33377 +       }
33378 +
33379 +       err = vfs_statfs(&new_xino->f_path, st);
33380 +       if (!err) {
33381 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
33382 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
33383 +                       st->f_bfree, st->f_blocks);
33384 +               if (file_inode(new_xino)->i_blocks < blocks)
33385 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
33386 +       } else
33387 +               AuErr1("statfs err %d, ignored\n", err);
33388 +
33389 +out_st:
33390 +       kfree(st);
33391 +out:
33392 +       return err;
33393 +}
33394 +
33395 +struct xino_do_trunc_args {
33396 +       struct super_block *sb;
33397 +       struct au_branch *br;
33398 +};
33399 +
33400 +static void xino_do_trunc(void *_args)
33401 +{
33402 +       struct xino_do_trunc_args *args = _args;
33403 +       struct super_block *sb;
33404 +       struct au_branch *br;
33405 +       struct inode *dir;
33406 +       int err;
33407 +       aufs_bindex_t bindex;
33408 +
33409 +       err = 0;
33410 +       sb = args->sb;
33411 +       dir = sb->s_root->d_inode;
33412 +       br = args->br;
33413 +
33414 +       si_noflush_write_lock(sb);
33415 +       ii_read_lock_parent(dir);
33416 +       bindex = au_br_index(sb, br->br_id);
33417 +       err = au_xino_trunc(sb, bindex);
33418 +       ii_read_unlock(dir);
33419 +       if (unlikely(err))
33420 +               pr_warn("err b%d, (%d)\n", bindex, err);
33421 +       atomic_dec(&br->br_xino_running);
33422 +       atomic_dec(&br->br_count);
33423 +       si_write_unlock(sb);
33424 +       au_nwt_done(&au_sbi(sb)->si_nowait);
33425 +       kfree(args);
33426 +}
33427 +
33428 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
33429 +{
33430 +       int err;
33431 +       struct kstatfs st;
33432 +       struct au_sbinfo *sbinfo;
33433 +
33434 +       /* todo: si_xino_expire and the ratio should be customizable */
33435 +       sbinfo = au_sbi(sb);
33436 +       if (time_before(jiffies,
33437 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
33438 +               return 0;
33439 +
33440 +       /* truncation border */
33441 +       err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
33442 +       if (unlikely(err)) {
33443 +               AuErr1("statfs err %d, ignored\n", err);
33444 +               return 0;
33445 +       }
33446 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
33447 +               return 0;
33448 +
33449 +       return 1;
33450 +}
33451 +
33452 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
33453 +{
33454 +       struct xino_do_trunc_args *args;
33455 +       int wkq_err;
33456 +
33457 +       if (!xino_trunc_test(sb, br))
33458 +               return;
33459 +
33460 +       if (atomic_inc_return(&br->br_xino_running) > 1)
33461 +               goto out;
33462 +
33463 +       /* lock and kfree() will be called in trunc_xino() */
33464 +       args = kmalloc(sizeof(*args), GFP_NOFS);
33465 +       if (unlikely(!args)) {
33466 +               AuErr1("no memory\n");
33467 +               goto out_args;
33468 +       }
33469 +
33470 +       atomic_inc(&br->br_count);
33471 +       args->sb = sb;
33472 +       args->br = br;
33473 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
33474 +       if (!wkq_err)
33475 +               return; /* success */
33476 +
33477 +       pr_err("wkq %d\n", wkq_err);
33478 +       atomic_dec(&br->br_count);
33479 +
33480 +out_args:
33481 +       kfree(args);
33482 +out:
33483 +       atomic_dec(&br->br_xino_running);
33484 +}
33485 +
33486 +/* ---------------------------------------------------------------------- */
33487 +
33488 +static int au_xino_do_write(au_writef_t write, struct file *file,
33489 +                           ino_t h_ino, ino_t ino)
33490 +{
33491 +       loff_t pos;
33492 +       ssize_t sz;
33493 +
33494 +       pos = h_ino;
33495 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
33496 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
33497 +               return -EFBIG;
33498 +       }
33499 +       pos *= sizeof(ino);
33500 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
33501 +       if (sz == sizeof(ino))
33502 +               return 0; /* success */
33503 +
33504 +       AuIOErr("write failed (%zd)\n", sz);
33505 +       return -EIO;
33506 +}
33507 +
33508 +/*
33509 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
33510 + * at the position of @h_ino.
33511 + * even if @ino is zero, it is written to the xinofile and means no entry.
33512 + * if the size of the xino file on a specific filesystem exceeds the watermark,
33513 + * try truncating it.
33514 + */
33515 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
33516 +                 ino_t ino)
33517 +{
33518 +       int err;
33519 +       unsigned int mnt_flags;
33520 +       struct au_branch *br;
33521 +
33522 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
33523 +                    || ((loff_t)-1) > 0);
33524 +       SiMustAnyLock(sb);
33525 +
33526 +       mnt_flags = au_mntflags(sb);
33527 +       if (!au_opt_test(mnt_flags, XINO))
33528 +               return 0;
33529 +
33530 +       br = au_sbr(sb, bindex);
33531 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
33532 +                              h_ino, ino);
33533 +       if (!err) {
33534 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
33535 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
33536 +                       xino_try_trunc(sb, br);
33537 +               return 0; /* success */
33538 +       }
33539 +
33540 +       AuIOErr("write failed (%d)\n", err);
33541 +       return -EIO;
33542 +}
33543 +
33544 +/* ---------------------------------------------------------------------- */
33545 +
33546 +/* aufs inode number bitmap */
33547 +
33548 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
33549 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
33550 +{
33551 +       ino_t ino;
33552 +
33553 +       AuDebugOn(bit < 0 || page_bits <= bit);
33554 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
33555 +       return ino;
33556 +}
33557 +
33558 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
33559 +{
33560 +       AuDebugOn(ino < AUFS_FIRST_INO);
33561 +       ino -= AUFS_FIRST_INO;
33562 +       *pindex = ino / page_bits;
33563 +       *bit = ino % page_bits;
33564 +}
33565 +
33566 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
33567 +{
33568 +       int err;
33569 +       loff_t pos;
33570 +       ssize_t sz;
33571 +       struct au_sbinfo *sbinfo;
33572 +       struct file *xib;
33573 +       unsigned long *p;
33574 +
33575 +       sbinfo = au_sbi(sb);
33576 +       MtxMustLock(&sbinfo->si_xib_mtx);
33577 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
33578 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
33579 +
33580 +       if (pindex == sbinfo->si_xib_last_pindex)
33581 +               return 0;
33582 +
33583 +       xib = sbinfo->si_xib;
33584 +       p = sbinfo->si_xib_buf;
33585 +       pos = sbinfo->si_xib_last_pindex;
33586 +       pos *= PAGE_SIZE;
33587 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
33588 +       if (unlikely(sz != PAGE_SIZE))
33589 +               goto out;
33590 +
33591 +       pos = pindex;
33592 +       pos *= PAGE_SIZE;
33593 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
33594 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
33595 +       else {
33596 +               memset(p, 0, PAGE_SIZE);
33597 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
33598 +       }
33599 +       if (sz == PAGE_SIZE) {
33600 +               sbinfo->si_xib_last_pindex = pindex;
33601 +               return 0; /* success */
33602 +       }
33603 +
33604 +out:
33605 +       AuIOErr1("write failed (%zd)\n", sz);
33606 +       err = sz;
33607 +       if (sz >= 0)
33608 +               err = -EIO;
33609 +       return err;
33610 +}
33611 +
33612 +/* ---------------------------------------------------------------------- */
33613 +
33614 +static void au_xib_clear_bit(struct inode *inode)
33615 +{
33616 +       int err, bit;
33617 +       unsigned long pindex;
33618 +       struct super_block *sb;
33619 +       struct au_sbinfo *sbinfo;
33620 +
33621 +       AuDebugOn(inode->i_nlink);
33622 +
33623 +       sb = inode->i_sb;
33624 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
33625 +       AuDebugOn(page_bits <= bit);
33626 +       sbinfo = au_sbi(sb);
33627 +       mutex_lock(&sbinfo->si_xib_mtx);
33628 +       err = xib_pindex(sb, pindex);
33629 +       if (!err) {
33630 +               clear_bit(bit, sbinfo->si_xib_buf);
33631 +               sbinfo->si_xib_next_bit = bit;
33632 +       }
33633 +       mutex_unlock(&sbinfo->si_xib_mtx);
33634 +}
33635 +
33636 +/* for s_op->delete_inode() */
33637 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
33638 +{
33639 +       int err;
33640 +       unsigned int mnt_flags;
33641 +       aufs_bindex_t bindex, bend, bi;
33642 +       unsigned char try_trunc;
33643 +       struct au_iinfo *iinfo;
33644 +       struct super_block *sb;
33645 +       struct au_hinode *hi;
33646 +       struct inode *h_inode;
33647 +       struct au_branch *br;
33648 +       au_writef_t xwrite;
33649 +
33650 +       sb = inode->i_sb;
33651 +       mnt_flags = au_mntflags(sb);
33652 +       if (!au_opt_test(mnt_flags, XINO)
33653 +           || inode->i_ino == AUFS_ROOT_INO)
33654 +               return;
33655 +
33656 +       if (unlinked) {
33657 +               au_xigen_inc(inode);
33658 +               au_xib_clear_bit(inode);
33659 +       }
33660 +
33661 +       iinfo = au_ii(inode);
33662 +       if (!iinfo)
33663 +               return;
33664 +
33665 +       bindex = iinfo->ii_bstart;
33666 +       if (bindex < 0)
33667 +               return;
33668 +
33669 +       xwrite = au_sbi(sb)->si_xwrite;
33670 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
33671 +       hi = iinfo->ii_hinode + bindex;
33672 +       bend = iinfo->ii_bend;
33673 +       for (; bindex <= bend; bindex++, hi++) {
33674 +               h_inode = hi->hi_inode;
33675 +               if (!h_inode
33676 +                   || (!unlinked && h_inode->i_nlink))
33677 +                       continue;
33678 +
33679 +               /* inode may not be revalidated */
33680 +               bi = au_br_index(sb, hi->hi_id);
33681 +               if (bi < 0)
33682 +                       continue;
33683 +
33684 +               br = au_sbr(sb, bi);
33685 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
33686 +                                      h_inode->i_ino, /*ino*/0);
33687 +               if (!err && try_trunc
33688 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
33689 +                       xino_try_trunc(sb, br);
33690 +       }
33691 +}
33692 +
33693 +/* get an unused inode number from bitmap */
33694 +ino_t au_xino_new_ino(struct super_block *sb)
33695 +{
33696 +       ino_t ino;
33697 +       unsigned long *p, pindex, ul, pend;
33698 +       struct au_sbinfo *sbinfo;
33699 +       struct file *file;
33700 +       int free_bit, err;
33701 +
33702 +       if (!au_opt_test(au_mntflags(sb), XINO))
33703 +               return iunique(sb, AUFS_FIRST_INO);
33704 +
33705 +       sbinfo = au_sbi(sb);
33706 +       mutex_lock(&sbinfo->si_xib_mtx);
33707 +       p = sbinfo->si_xib_buf;
33708 +       free_bit = sbinfo->si_xib_next_bit;
33709 +       if (free_bit < page_bits && !test_bit(free_bit, p))
33710 +               goto out; /* success */
33711 +       free_bit = find_first_zero_bit(p, page_bits);
33712 +       if (free_bit < page_bits)
33713 +               goto out; /* success */
33714 +
33715 +       pindex = sbinfo->si_xib_last_pindex;
33716 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
33717 +               err = xib_pindex(sb, ul);
33718 +               if (unlikely(err))
33719 +                       goto out_err;
33720 +               free_bit = find_first_zero_bit(p, page_bits);
33721 +               if (free_bit < page_bits)
33722 +                       goto out; /* success */
33723 +       }
33724 +
33725 +       file = sbinfo->si_xib;
33726 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
33727 +       for (ul = pindex + 1; ul <= pend; ul++) {
33728 +               err = xib_pindex(sb, ul);
33729 +               if (unlikely(err))
33730 +                       goto out_err;
33731 +               free_bit = find_first_zero_bit(p, page_bits);
33732 +               if (free_bit < page_bits)
33733 +                       goto out; /* success */
33734 +       }
33735 +       BUG();
33736 +
33737 +out:
33738 +       set_bit(free_bit, p);
33739 +       sbinfo->si_xib_next_bit = free_bit + 1;
33740 +       pindex = sbinfo->si_xib_last_pindex;
33741 +       mutex_unlock(&sbinfo->si_xib_mtx);
33742 +       ino = xib_calc_ino(pindex, free_bit);
33743 +       AuDbg("i%lu\n", (unsigned long)ino);
33744 +       return ino;
33745 +out_err:
33746 +       mutex_unlock(&sbinfo->si_xib_mtx);
33747 +       AuDbg("i0\n");
33748 +       return 0;
33749 +}
33750 +
33751 +/*
33752 + * read @ino from xinofile for the specified branch{@sb, @bindex}
33753 + * at the position of @h_ino.
33754 + * if @ino does not exist and @do_new is true, get new one.
33755 + */
33756 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
33757 +                ino_t *ino)
33758 +{
33759 +       int err;
33760 +       ssize_t sz;
33761 +       loff_t pos;
33762 +       struct file *file;
33763 +       struct au_sbinfo *sbinfo;
33764 +
33765 +       *ino = 0;
33766 +       if (!au_opt_test(au_mntflags(sb), XINO))
33767 +               return 0; /* no xino */
33768 +
33769 +       err = 0;
33770 +       sbinfo = au_sbi(sb);
33771 +       pos = h_ino;
33772 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
33773 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
33774 +               return -EFBIG;
33775 +       }
33776 +       pos *= sizeof(*ino);
33777 +
33778 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
33779 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
33780 +               return 0; /* no ino */
33781 +
33782 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
33783 +       if (sz == sizeof(*ino))
33784 +               return 0; /* success */
33785 +
33786 +       err = sz;
33787 +       if (unlikely(sz >= 0)) {
33788 +               err = -EIO;
33789 +               AuIOErr("xino read error (%zd)\n", sz);
33790 +       }
33791 +
33792 +       return err;
33793 +}
33794 +
33795 +/* ---------------------------------------------------------------------- */
33796 +
33797 +/* create and set a new xino file */
33798 +
33799 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
33800 +{
33801 +       struct file *file;
33802 +       struct dentry *h_parent, *d;
33803 +       struct inode *h_dir;
33804 +       int err;
33805 +
33806 +       /*
33807 +        * at mount-time, and the xino file is the default path,
33808 +        * hnotify is disabled so we have no notify events to ignore.
33809 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
33810 +        */
33811 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
33812 +                              /* | __FMODE_NONOTIFY */,
33813 +                              S_IRUGO | S_IWUGO);
33814 +       if (IS_ERR(file)) {
33815 +               if (!silent)
33816 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
33817 +               return file;
33818 +       }
33819 +
33820 +       /* keep file count */
33821 +       h_parent = dget_parent(file->f_path.dentry);
33822 +       h_dir = h_parent->d_inode;
33823 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
33824 +       /* mnt_want_write() is unnecessary here */
33825 +       /* no delegation since it is just created */
33826 +       err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL, /*force*/0);
33827 +       mutex_unlock(&h_dir->i_mutex);
33828 +       dput(h_parent);
33829 +       if (unlikely(err)) {
33830 +               if (!silent)
33831 +                       pr_err("unlink %s(%d)\n", fname, err);
33832 +               goto out;
33833 +       }
33834 +
33835 +       err = -EINVAL;
33836 +       d = file->f_path.dentry;
33837 +       if (unlikely(sb == d->d_sb)) {
33838 +               if (!silent)
33839 +                       pr_err("%s must be outside\n", fname);
33840 +               goto out;
33841 +       }
33842 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
33843 +               if (!silent)
33844 +                       pr_err("xino doesn't support %s(%s)\n",
33845 +                              fname, au_sbtype(d->d_sb));
33846 +               goto out;
33847 +       }
33848 +       return file; /* success */
33849 +
33850 +out:
33851 +       fput(file);
33852 +       file = ERR_PTR(err);
33853 +       return file;
33854 +}
33855 +
33856 +/*
33857 + * find another branch who is on the same filesystem of the specified
33858 + * branch{@btgt}. search until @bend.
33859 + */
33860 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
33861 +                       aufs_bindex_t bend)
33862 +{
33863 +       aufs_bindex_t bindex;
33864 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
33865 +
33866 +       for (bindex = 0; bindex < btgt; bindex++)
33867 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
33868 +                       return bindex;
33869 +       for (bindex++; bindex <= bend; bindex++)
33870 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
33871 +                       return bindex;
33872 +       return -1;
33873 +}
33874 +
33875 +/* ---------------------------------------------------------------------- */
33876 +
33877 +/*
33878 + * initialize the xinofile for the specified branch @br
33879 + * at the place/path where @base_file indicates.
33880 + * test whether another branch is on the same filesystem or not,
33881 + * if @do_test is true.
33882 + */
33883 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
33884 +              struct file *base_file, int do_test)
33885 +{
33886 +       int err;
33887 +       ino_t ino;
33888 +       aufs_bindex_t bend, bindex;
33889 +       struct au_branch *shared_br, *b;
33890 +       struct file *file;
33891 +       struct super_block *tgt_sb;
33892 +
33893 +       shared_br = NULL;
33894 +       bend = au_sbend(sb);
33895 +       if (do_test) {
33896 +               tgt_sb = au_br_sb(br);
33897 +               for (bindex = 0; bindex <= bend; bindex++) {
33898 +                       b = au_sbr(sb, bindex);
33899 +                       if (tgt_sb == au_br_sb(b)) {
33900 +                               shared_br = b;
33901 +                               break;
33902 +                       }
33903 +               }
33904 +       }
33905 +
33906 +       if (!shared_br || !shared_br->br_xino.xi_file) {
33907 +               struct au_xino_lock_dir ldir;
33908 +
33909 +               au_xino_lock_dir(sb, base_file, &ldir);
33910 +               /* mnt_want_write() is unnecessary here */
33911 +               file = au_xino_create2(base_file, NULL);
33912 +               au_xino_unlock_dir(&ldir);
33913 +               err = PTR_ERR(file);
33914 +               if (IS_ERR(file))
33915 +                       goto out;
33916 +               br->br_xino.xi_file = file;
33917 +       } else {
33918 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
33919 +               get_file(br->br_xino.xi_file);
33920 +       }
33921 +
33922 +       ino = AUFS_ROOT_INO;
33923 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
33924 +                              h_ino, ino);
33925 +       if (unlikely(err)) {
33926 +               fput(br->br_xino.xi_file);
33927 +               br->br_xino.xi_file = NULL;
33928 +       }
33929 +
33930 +out:
33931 +       return err;
33932 +}
33933 +
33934 +/* ---------------------------------------------------------------------- */
33935 +
33936 +/* trucate a xino bitmap file */
33937 +
33938 +/* todo: slow */
33939 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
33940 +{
33941 +       int err, bit;
33942 +       ssize_t sz;
33943 +       unsigned long pindex;
33944 +       loff_t pos, pend;
33945 +       struct au_sbinfo *sbinfo;
33946 +       au_readf_t func;
33947 +       ino_t *ino;
33948 +       unsigned long *p;
33949 +
33950 +       err = 0;
33951 +       sbinfo = au_sbi(sb);
33952 +       MtxMustLock(&sbinfo->si_xib_mtx);
33953 +       p = sbinfo->si_xib_buf;
33954 +       func = sbinfo->si_xread;
33955 +       pend = vfsub_f_size_read(file);
33956 +       pos = 0;
33957 +       while (pos < pend) {
33958 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
33959 +               err = sz;
33960 +               if (unlikely(sz <= 0))
33961 +                       goto out;
33962 +
33963 +               err = 0;
33964 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
33965 +                       if (unlikely(*ino < AUFS_FIRST_INO))
33966 +                               continue;
33967 +
33968 +                       xib_calc_bit(*ino, &pindex, &bit);
33969 +                       AuDebugOn(page_bits <= bit);
33970 +                       err = xib_pindex(sb, pindex);
33971 +                       if (!err)
33972 +                               set_bit(bit, p);
33973 +                       else
33974 +                               goto out;
33975 +               }
33976 +       }
33977 +
33978 +out:
33979 +       return err;
33980 +}
33981 +
33982 +static int xib_restore(struct super_block *sb)
33983 +{
33984 +       int err;
33985 +       aufs_bindex_t bindex, bend;
33986 +       void *page;
33987 +
33988 +       err = -ENOMEM;
33989 +       page = (void *)__get_free_page(GFP_NOFS);
33990 +       if (unlikely(!page))
33991 +               goto out;
33992 +
33993 +       err = 0;
33994 +       bend = au_sbend(sb);
33995 +       for (bindex = 0; !err && bindex <= bend; bindex++)
33996 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
33997 +                       err = do_xib_restore
33998 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
33999 +               else
34000 +                       AuDbg("b%d\n", bindex);
34001 +       free_page((unsigned long)page);
34002 +
34003 +out:
34004 +       return err;
34005 +}
34006 +
34007 +int au_xib_trunc(struct super_block *sb)
34008 +{
34009 +       int err;
34010 +       ssize_t sz;
34011 +       loff_t pos;
34012 +       struct au_xino_lock_dir ldir;
34013 +       struct au_sbinfo *sbinfo;
34014 +       unsigned long *p;
34015 +       struct file *file;
34016 +
34017 +       SiMustWriteLock(sb);
34018 +
34019 +       err = 0;
34020 +       sbinfo = au_sbi(sb);
34021 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
34022 +               goto out;
34023 +
34024 +       file = sbinfo->si_xib;
34025 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
34026 +               goto out;
34027 +
34028 +       au_xino_lock_dir(sb, file, &ldir);
34029 +       /* mnt_want_write() is unnecessary here */
34030 +       file = au_xino_create2(sbinfo->si_xib, NULL);
34031 +       au_xino_unlock_dir(&ldir);
34032 +       err = PTR_ERR(file);
34033 +       if (IS_ERR(file))
34034 +               goto out;
34035 +       fput(sbinfo->si_xib);
34036 +       sbinfo->si_xib = file;
34037 +
34038 +       p = sbinfo->si_xib_buf;
34039 +       memset(p, 0, PAGE_SIZE);
34040 +       pos = 0;
34041 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
34042 +       if (unlikely(sz != PAGE_SIZE)) {
34043 +               err = sz;
34044 +               AuIOErr("err %d\n", err);
34045 +               if (sz >= 0)
34046 +                       err = -EIO;
34047 +               goto out;
34048 +       }
34049 +
34050 +       mutex_lock(&sbinfo->si_xib_mtx);
34051 +       /* mnt_want_write() is unnecessary here */
34052 +       err = xib_restore(sb);
34053 +       mutex_unlock(&sbinfo->si_xib_mtx);
34054 +
34055 +out:
34056 +       return err;
34057 +}
34058 +
34059 +/* ---------------------------------------------------------------------- */
34060 +
34061 +/*
34062 + * xino mount option handlers
34063 + */
34064 +static au_readf_t find_readf(struct file *h_file)
34065 +{
34066 +       const struct file_operations *fop = h_file->f_op;
34067 +
34068 +       if (fop->read)
34069 +               return fop->read;
34070 +       if (fop->aio_read)
34071 +               return do_sync_read;
34072 +       if (fop->read_iter)
34073 +               return new_sync_read;
34074 +       return ERR_PTR(-ENOSYS);
34075 +}
34076 +
34077 +static au_writef_t find_writef(struct file *h_file)
34078 +{
34079 +       const struct file_operations *fop = h_file->f_op;
34080 +
34081 +       if (fop->write)
34082 +               return fop->write;
34083 +       if (fop->aio_write)
34084 +               return do_sync_write;
34085 +       if (fop->write_iter)
34086 +               return new_sync_write;
34087 +       return ERR_PTR(-ENOSYS);
34088 +}
34089 +
34090 +/* xino bitmap */
34091 +static void xino_clear_xib(struct super_block *sb)
34092 +{
34093 +       struct au_sbinfo *sbinfo;
34094 +
34095 +       SiMustWriteLock(sb);
34096 +
34097 +       sbinfo = au_sbi(sb);
34098 +       sbinfo->si_xread = NULL;
34099 +       sbinfo->si_xwrite = NULL;
34100 +       if (sbinfo->si_xib)
34101 +               fput(sbinfo->si_xib);
34102 +       sbinfo->si_xib = NULL;
34103 +       free_page((unsigned long)sbinfo->si_xib_buf);
34104 +       sbinfo->si_xib_buf = NULL;
34105 +}
34106 +
34107 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
34108 +{
34109 +       int err;
34110 +       loff_t pos;
34111 +       struct au_sbinfo *sbinfo;
34112 +       struct file *file;
34113 +
34114 +       SiMustWriteLock(sb);
34115 +
34116 +       sbinfo = au_sbi(sb);
34117 +       file = au_xino_create2(base, sbinfo->si_xib);
34118 +       err = PTR_ERR(file);
34119 +       if (IS_ERR(file))
34120 +               goto out;
34121 +       if (sbinfo->si_xib)
34122 +               fput(sbinfo->si_xib);
34123 +       sbinfo->si_xib = file;
34124 +       sbinfo->si_xread = find_readf(file);
34125 +       sbinfo->si_xwrite = find_writef(file);
34126 +
34127 +       err = -ENOMEM;
34128 +       if (!sbinfo->si_xib_buf)
34129 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
34130 +       if (unlikely(!sbinfo->si_xib_buf))
34131 +               goto out_unset;
34132 +
34133 +       sbinfo->si_xib_last_pindex = 0;
34134 +       sbinfo->si_xib_next_bit = 0;
34135 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
34136 +               pos = 0;
34137 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
34138 +                                 PAGE_SIZE, &pos);
34139 +               if (unlikely(err != PAGE_SIZE))
34140 +                       goto out_free;
34141 +       }
34142 +       err = 0;
34143 +       goto out; /* success */
34144 +
34145 +out_free:
34146 +       free_page((unsigned long)sbinfo->si_xib_buf);
34147 +       sbinfo->si_xib_buf = NULL;
34148 +       if (err >= 0)
34149 +               err = -EIO;
34150 +out_unset:
34151 +       fput(sbinfo->si_xib);
34152 +       sbinfo->si_xib = NULL;
34153 +       sbinfo->si_xread = NULL;
34154 +       sbinfo->si_xwrite = NULL;
34155 +out:
34156 +       return err;
34157 +}
34158 +
34159 +/* xino for each branch */
34160 +static void xino_clear_br(struct super_block *sb)
34161 +{
34162 +       aufs_bindex_t bindex, bend;
34163 +       struct au_branch *br;
34164 +
34165 +       bend = au_sbend(sb);
34166 +       for (bindex = 0; bindex <= bend; bindex++) {
34167 +               br = au_sbr(sb, bindex);
34168 +               if (!br || !br->br_xino.xi_file)
34169 +                       continue;
34170 +
34171 +               fput(br->br_xino.xi_file);
34172 +               br->br_xino.xi_file = NULL;
34173 +       }
34174 +}
34175 +
34176 +static int au_xino_set_br(struct super_block *sb, struct file *base)
34177 +{
34178 +       int err;
34179 +       ino_t ino;
34180 +       aufs_bindex_t bindex, bend, bshared;
34181 +       struct {
34182 +               struct file *old, *new;
34183 +       } *fpair, *p;
34184 +       struct au_branch *br;
34185 +       struct inode *inode;
34186 +       au_writef_t writef;
34187 +
34188 +       SiMustWriteLock(sb);
34189 +
34190 +       err = -ENOMEM;
34191 +       bend = au_sbend(sb);
34192 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
34193 +       if (unlikely(!fpair))
34194 +               goto out;
34195 +
34196 +       inode = sb->s_root->d_inode;
34197 +       ino = AUFS_ROOT_INO;
34198 +       writef = au_sbi(sb)->si_xwrite;
34199 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
34200 +               br = au_sbr(sb, bindex);
34201 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
34202 +               if (bshared >= 0) {
34203 +                       /* shared xino */
34204 +                       *p = fpair[bshared];
34205 +                       get_file(p->new);
34206 +               }
34207 +
34208 +               if (!p->new) {
34209 +                       /* new xino */
34210 +                       p->old = br->br_xino.xi_file;
34211 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
34212 +                       err = PTR_ERR(p->new);
34213 +                       if (IS_ERR(p->new)) {
34214 +                               p->new = NULL;
34215 +                               goto out_pair;
34216 +                       }
34217 +               }
34218 +
34219 +               err = au_xino_do_write(writef, p->new,
34220 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
34221 +               if (unlikely(err))
34222 +                       goto out_pair;
34223 +       }
34224 +
34225 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
34226 +               br = au_sbr(sb, bindex);
34227 +               if (br->br_xino.xi_file)
34228 +                       fput(br->br_xino.xi_file);
34229 +               get_file(p->new);
34230 +               br->br_xino.xi_file = p->new;
34231 +       }
34232 +
34233 +out_pair:
34234 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
34235 +               if (p->new)
34236 +                       fput(p->new);
34237 +               else
34238 +                       break;
34239 +       kfree(fpair);
34240 +out:
34241 +       return err;
34242 +}
34243 +
34244 +void au_xino_clr(struct super_block *sb)
34245 +{
34246 +       struct au_sbinfo *sbinfo;
34247 +
34248 +       au_xigen_clr(sb);
34249 +       xino_clear_xib(sb);
34250 +       xino_clear_br(sb);
34251 +       sbinfo = au_sbi(sb);
34252 +       /* lvalue, do not call au_mntflags() */
34253 +       au_opt_clr(sbinfo->si_mntflags, XINO);
34254 +}
34255 +
34256 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
34257 +{
34258 +       int err, skip;
34259 +       struct dentry *parent, *cur_parent;
34260 +       struct qstr *dname, *cur_name;
34261 +       struct file *cur_xino;
34262 +       struct inode *dir;
34263 +       struct au_sbinfo *sbinfo;
34264 +
34265 +       SiMustWriteLock(sb);
34266 +
34267 +       err = 0;
34268 +       sbinfo = au_sbi(sb);
34269 +       parent = dget_parent(xino->file->f_path.dentry);
34270 +       if (remount) {
34271 +               skip = 0;
34272 +               dname = &xino->file->f_path.dentry->d_name;
34273 +               cur_xino = sbinfo->si_xib;
34274 +               if (cur_xino) {
34275 +                       cur_parent = dget_parent(cur_xino->f_path.dentry);
34276 +                       cur_name = &cur_xino->f_path.dentry->d_name;
34277 +                       skip = (cur_parent == parent
34278 +                               && au_qstreq(dname, cur_name));
34279 +                       dput(cur_parent);
34280 +               }
34281 +               if (skip)
34282 +                       goto out;
34283 +       }
34284 +
34285 +       au_opt_set(sbinfo->si_mntflags, XINO);
34286 +       dir = parent->d_inode;
34287 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
34288 +       /* mnt_want_write() is unnecessary here */
34289 +       err = au_xino_set_xib(sb, xino->file);
34290 +       if (!err)
34291 +               err = au_xigen_set(sb, xino->file);
34292 +       if (!err)
34293 +               err = au_xino_set_br(sb, xino->file);
34294 +       mutex_unlock(&dir->i_mutex);
34295 +       if (!err)
34296 +               goto out; /* success */
34297 +
34298 +       /* reset all */
34299 +       AuIOErr("failed creating xino(%d).\n", err);
34300 +       au_xigen_clr(sb);
34301 +       xino_clear_xib(sb);
34302 +
34303 +out:
34304 +       dput(parent);
34305 +       return err;
34306 +}
34307 +
34308 +/* ---------------------------------------------------------------------- */
34309 +
34310 +/*
34311 + * create a xinofile at the default place/path.
34312 + */
34313 +struct file *au_xino_def(struct super_block *sb)
34314 +{
34315 +       struct file *file;
34316 +       char *page, *p;
34317 +       struct au_branch *br;
34318 +       struct super_block *h_sb;
34319 +       struct path path;
34320 +       aufs_bindex_t bend, bindex, bwr;
34321 +
34322 +       br = NULL;
34323 +       bend = au_sbend(sb);
34324 +       bwr = -1;
34325 +       for (bindex = 0; bindex <= bend; bindex++) {
34326 +               br = au_sbr(sb, bindex);
34327 +               if (au_br_writable(br->br_perm)
34328 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
34329 +                       bwr = bindex;
34330 +                       break;
34331 +               }
34332 +       }
34333 +
34334 +       if (bwr >= 0) {
34335 +               file = ERR_PTR(-ENOMEM);
34336 +               page = (void *)__get_free_page(GFP_NOFS);
34337 +               if (unlikely(!page))
34338 +                       goto out;
34339 +               path.mnt = au_br_mnt(br);
34340 +               path.dentry = au_h_dptr(sb->s_root, bwr);
34341 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
34342 +               file = (void *)p;
34343 +               if (!IS_ERR(p)) {
34344 +                       strcat(p, "/" AUFS_XINO_FNAME);
34345 +                       AuDbg("%s\n", p);
34346 +                       file = au_xino_create(sb, p, /*silent*/0);
34347 +                       if (!IS_ERR(file))
34348 +                               au_xino_brid_set(sb, br->br_id);
34349 +               }
34350 +               free_page((unsigned long)page);
34351 +       } else {
34352 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
34353 +               if (IS_ERR(file))
34354 +                       goto out;
34355 +               h_sb = file->f_path.dentry->d_sb;
34356 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
34357 +                       pr_err("xino doesn't support %s(%s)\n",
34358 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
34359 +                       fput(file);
34360 +                       file = ERR_PTR(-EINVAL);
34361 +               }
34362 +               if (!IS_ERR(file))
34363 +                       au_xino_brid_set(sb, -1);
34364 +       }
34365 +
34366 +out:
34367 +       return file;
34368 +}
34369 +
34370 +/* ---------------------------------------------------------------------- */
34371 +
34372 +int au_xino_path(struct seq_file *seq, struct file *file)
34373 +{
34374 +       int err;
34375 +
34376 +       err = au_seq_path(seq, &file->f_path);
34377 +       if (unlikely(err < 0))
34378 +               goto out;
34379 +
34380 +       err = 0;
34381 +#define Deleted "\\040(deleted)"
34382 +       seq->count -= sizeof(Deleted) - 1;
34383 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
34384 +                        sizeof(Deleted) - 1));
34385 +#undef Deleted
34386 +
34387 +out:
34388 +       return err;
34389 +}
34390 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
34391 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
34392 +++ linux/include/uapi/linux/aufs_type.h        2015-04-13 15:10:20.790157026 +0200
34393 @@ -0,0 +1,419 @@
34394 +/*
34395 + * Copyright (C) 2005-2015 Junjiro R. Okajima
34396 + *
34397 + * This program, aufs is free software; you can redistribute it and/or modify
34398 + * it under the terms of the GNU General Public License as published by
34399 + * the Free Software Foundation; either version 2 of the License, or
34400 + * (at your option) any later version.
34401 + *
34402 + * This program is distributed in the hope that it will be useful,
34403 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34404 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34405 + * GNU General Public License for more details.
34406 + *
34407 + * You should have received a copy of the GNU General Public License
34408 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34409 + */
34410 +
34411 +#ifndef __AUFS_TYPE_H__
34412 +#define __AUFS_TYPE_H__
34413 +
34414 +#define AUFS_NAME      "aufs"
34415 +
34416 +#ifdef __KERNEL__
34417 +/*
34418 + * define it before including all other headers.
34419 + * sched.h may use pr_* macros before defining "current", so define the
34420 + * no-current version first, and re-define later.
34421 + */
34422 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
34423 +#include <linux/sched.h>
34424 +#undef pr_fmt
34425 +#define pr_fmt(fmt) \
34426 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
34427 +               (int)sizeof(current->comm), current->comm, current->pid
34428 +#else
34429 +#include <stdint.h>
34430 +#include <sys/types.h>
34431 +#endif /* __KERNEL__ */
34432 +
34433 +#include <linux/limits.h>
34434 +
34435 +#define AUFS_VERSION   "3.x-rcN-20150406"
34436 +
34437 +/* todo? move this to linux-2.6.19/include/magic.h */
34438 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
34439 +
34440 +/* ---------------------------------------------------------------------- */
34441 +
34442 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
34443 +typedef int8_t aufs_bindex_t;
34444 +#define AUFS_BRANCH_MAX 127
34445 +#else
34446 +typedef int16_t aufs_bindex_t;
34447 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
34448 +#define AUFS_BRANCH_MAX 511
34449 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
34450 +#define AUFS_BRANCH_MAX 1023
34451 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
34452 +#define AUFS_BRANCH_MAX 32767
34453 +#endif
34454 +#endif
34455 +
34456 +#ifdef __KERNEL__
34457 +#ifndef AUFS_BRANCH_MAX
34458 +#error unknown CONFIG_AUFS_BRANCH_MAX value
34459 +#endif
34460 +#endif /* __KERNEL__ */
34461 +
34462 +/* ---------------------------------------------------------------------- */
34463 +
34464 +#define AUFS_FSTYPE            AUFS_NAME
34465 +
34466 +#define AUFS_ROOT_INO          2
34467 +#define AUFS_FIRST_INO         11
34468 +
34469 +#define AUFS_WH_PFX            ".wh."
34470 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
34471 +#define AUFS_WH_TMP_LEN                4
34472 +/* a limit for rmdir/rename a dir and copyup */
34473 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
34474 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
34475 +                               - 1                     /* dot */\
34476 +                               - AUFS_WH_TMP_LEN)      /* hex */
34477 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
34478 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
34479 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
34480 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
34481 +#define AUFS_DIRWH_DEF         3
34482 +#define AUFS_RDCACHE_DEF       10 /* seconds */
34483 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
34484 +#define AUFS_RDBLK_DEF         512 /* bytes */
34485 +#define AUFS_RDHASH_DEF                32
34486 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
34487 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
34488 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
34489 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
34490 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
34491 +
34492 +/* pseudo-link maintenace under /proc */
34493 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
34494 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
34495 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
34496 +
34497 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
34498 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
34499 +
34500 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
34501 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
34502 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
34503 +
34504 +/* doubly whiteouted */
34505 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
34506 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
34507 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
34508 +
34509 +/* branch permissions and attributes */
34510 +#define AUFS_BRPERM_RW         "rw"
34511 +#define AUFS_BRPERM_RO         "ro"
34512 +#define AUFS_BRPERM_RR         "rr"
34513 +#define AUFS_BRATTR_COO_REG    "coo_reg"
34514 +#define AUFS_BRATTR_COO_ALL    "coo_all"
34515 +#define AUFS_BRATTR_FHSM       "fhsm"
34516 +#define AUFS_BRATTR_UNPIN      "unpin"
34517 +#define AUFS_BRATTR_ICEX       "icex"
34518 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
34519 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
34520 +#define AUFS_BRATTR_ICEX_TR    "icextr"
34521 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
34522 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
34523 +#define AUFS_BRRATTR_WH                "wh"
34524 +#define AUFS_BRWATTR_NLWH      "nolwh"
34525 +#define AUFS_BRWATTR_MOO       "moo"
34526 +
34527 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
34528 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
34529 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
34530 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
34531 +
34532 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
34533 +#define AuBrAttr_COO_ALL       (1 << 4)
34534 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
34535 +
34536 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
34537 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
34538 +                                                  branch. meaningless since
34539 +                                                  linux-3.18-rc1 */
34540 +
34541 +/* ignore error in copying XATTR */
34542 +#define AuBrAttr_ICEX_SEC      (1 << 7)
34543 +#define AuBrAttr_ICEX_SYS      (1 << 8)
34544 +#define AuBrAttr_ICEX_TR       (1 << 9)
34545 +#define AuBrAttr_ICEX_USR      (1 << 10)
34546 +#define AuBrAttr_ICEX_OTH      (1 << 11)
34547 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
34548 +                                | AuBrAttr_ICEX_SYS    \
34549 +                                | AuBrAttr_ICEX_TR     \
34550 +                                | AuBrAttr_ICEX_USR    \
34551 +                                | AuBrAttr_ICEX_OTH)
34552 +
34553 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
34554 +#define AuBrRAttr_Mask         AuBrRAttr_WH
34555 +
34556 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
34557 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
34558 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
34559 +
34560 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
34561 +
34562 +/* #warning test userspace */
34563 +#ifdef __KERNEL__
34564 +#ifndef CONFIG_AUFS_FHSM
34565 +#undef AuBrAttr_FHSM
34566 +#define AuBrAttr_FHSM          0
34567 +#endif
34568 +#ifndef CONFIG_AUFS_XATTR
34569 +#undef AuBrAttr_ICEX
34570 +#define AuBrAttr_ICEX          0
34571 +#undef AuBrAttr_ICEX_SEC
34572 +#define AuBrAttr_ICEX_SEC      0
34573 +#undef AuBrAttr_ICEX_SYS
34574 +#define AuBrAttr_ICEX_SYS      0
34575 +#undef AuBrAttr_ICEX_TR
34576 +#define AuBrAttr_ICEX_TR       0
34577 +#undef AuBrAttr_ICEX_USR
34578 +#define AuBrAttr_ICEX_USR      0
34579 +#undef AuBrAttr_ICEX_OTH
34580 +#define AuBrAttr_ICEX_OTH      0
34581 +#endif
34582 +#endif
34583 +
34584 +/* the longest combination */
34585 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
34586 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
34587 +                              "+" AUFS_BRATTR_COO_REG          \
34588 +                              "+" AUFS_BRATTR_FHSM             \
34589 +                              "+" AUFS_BRATTR_UNPIN            \
34590 +                              "+" AUFS_BRATTR_ICEX_SEC         \
34591 +                              "+" AUFS_BRATTR_ICEX_SYS         \
34592 +                              "+" AUFS_BRATTR_ICEX_USR         \
34593 +                              "+" AUFS_BRATTR_ICEX_OTH         \
34594 +                              "+" AUFS_BRWATTR_NLWH)
34595 +
34596 +typedef struct {
34597 +       char a[AuBrPermStrSz];
34598 +} au_br_perm_str_t;
34599 +
34600 +static inline int au_br_writable(int brperm)
34601 +{
34602 +       return brperm & AuBrPerm_RW;
34603 +}
34604 +
34605 +static inline int au_br_whable(int brperm)
34606 +{
34607 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
34608 +}
34609 +
34610 +static inline int au_br_wh_linkable(int brperm)
34611 +{
34612 +       return !(brperm & AuBrWAttr_NoLinkWH);
34613 +}
34614 +
34615 +static inline int au_br_cmoo(int brperm)
34616 +{
34617 +       return brperm & AuBrAttr_CMOO_Mask;
34618 +}
34619 +
34620 +static inline int au_br_fhsm(int brperm)
34621 +{
34622 +       return brperm & AuBrAttr_FHSM;
34623 +}
34624 +
34625 +/* ---------------------------------------------------------------------- */
34626 +
34627 +/* ioctl */
34628 +enum {
34629 +       /* readdir in userspace */
34630 +       AuCtl_RDU,
34631 +       AuCtl_RDU_INO,
34632 +
34633 +       AuCtl_WBR_FD,   /* pathconf wrapper */
34634 +       AuCtl_IBUSY,    /* busy inode */
34635 +       AuCtl_MVDOWN,   /* move-down */
34636 +       AuCtl_BR,       /* info about branches */
34637 +       AuCtl_FHSM_FD   /* connection for fhsm */
34638 +};
34639 +
34640 +/* borrowed from linux/include/linux/kernel.h */
34641 +#ifndef ALIGN
34642 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
34643 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
34644 +#endif
34645 +
34646 +/* borrowed from linux/include/linux/compiler-gcc3.h */
34647 +#ifndef __aligned
34648 +#define __aligned(x)                   __attribute__((aligned(x)))
34649 +#endif
34650 +
34651 +#ifdef __KERNEL__
34652 +#ifndef __packed
34653 +#define __packed                       __attribute__((packed))
34654 +#endif
34655 +#endif
34656 +
34657 +struct au_rdu_cookie {
34658 +       uint64_t        h_pos;
34659 +       int16_t         bindex;
34660 +       uint8_t         flags;
34661 +       uint8_t         pad;
34662 +       uint32_t        generation;
34663 +} __aligned(8);
34664 +
34665 +struct au_rdu_ent {
34666 +       uint64_t        ino;
34667 +       int16_t         bindex;
34668 +       uint8_t         type;
34669 +       uint8_t         nlen;
34670 +       uint8_t         wh;
34671 +       char            name[0];
34672 +} __aligned(8);
34673 +
34674 +static inline int au_rdu_len(int nlen)
34675 +{
34676 +       /* include the terminating NULL */
34677 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
34678 +                    sizeof(uint64_t));
34679 +}
34680 +
34681 +union au_rdu_ent_ul {
34682 +       struct au_rdu_ent __user        *e;
34683 +       uint64_t                        ul;
34684 +};
34685 +
34686 +enum {
34687 +       AufsCtlRduV_SZ,
34688 +       AufsCtlRduV_End
34689 +};
34690 +
34691 +struct aufs_rdu {
34692 +       /* input */
34693 +       union {
34694 +               uint64_t        sz;     /* AuCtl_RDU */
34695 +               uint64_t        nent;   /* AuCtl_RDU_INO */
34696 +       };
34697 +       union au_rdu_ent_ul     ent;
34698 +       uint16_t                verify[AufsCtlRduV_End];
34699 +
34700 +       /* input/output */
34701 +       uint32_t                blk;
34702 +
34703 +       /* output */
34704 +       union au_rdu_ent_ul     tail;
34705 +       /* number of entries which were added in a single call */
34706 +       uint64_t                rent;
34707 +       uint8_t                 full;
34708 +       uint8_t                 shwh;
34709 +
34710 +       struct au_rdu_cookie    cookie;
34711 +} __aligned(8);
34712 +
34713 +/* ---------------------------------------------------------------------- */
34714 +
34715 +struct aufs_wbr_fd {
34716 +       uint32_t        oflags;
34717 +       int16_t         brid;
34718 +} __aligned(8);
34719 +
34720 +/* ---------------------------------------------------------------------- */
34721 +
34722 +struct aufs_ibusy {
34723 +       uint64_t        ino, h_ino;
34724 +       int16_t         bindex;
34725 +} __aligned(8);
34726 +
34727 +/* ---------------------------------------------------------------------- */
34728 +
34729 +/* error code for move-down */
34730 +/* the actual message strings are implemented in aufs-util.git */
34731 +enum {
34732 +       EAU_MVDOWN_OPAQUE = 1,
34733 +       EAU_MVDOWN_WHITEOUT,
34734 +       EAU_MVDOWN_UPPER,
34735 +       EAU_MVDOWN_BOTTOM,
34736 +       EAU_MVDOWN_NOUPPER,
34737 +       EAU_MVDOWN_NOLOWERBR,
34738 +       EAU_Last
34739 +};
34740 +
34741 +/* flags for move-down */
34742 +#define AUFS_MVDOWN_DMSG       1
34743 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
34744 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
34745 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
34746 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
34747 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
34748 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
34749 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
34750 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
34751 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
34752 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
34753 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
34754 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
34755 +
34756 +/* index for move-down */
34757 +enum {
34758 +       AUFS_MVDOWN_UPPER,
34759 +       AUFS_MVDOWN_LOWER,
34760 +       AUFS_MVDOWN_NARRAY
34761 +};
34762 +
34763 +/*
34764 + * additional info of move-down
34765 + * number of free blocks and inodes.
34766 + * subset of struct kstatfs, but smaller and always 64bit.
34767 + */
34768 +struct aufs_stfs {
34769 +       uint64_t        f_blocks;
34770 +       uint64_t        f_bavail;
34771 +       uint64_t        f_files;
34772 +       uint64_t        f_ffree;
34773 +};
34774 +
34775 +struct aufs_stbr {
34776 +       int16_t                 brid;   /* optional input */
34777 +       int16_t                 bindex; /* output */
34778 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
34779 +} __aligned(8);
34780 +
34781 +struct aufs_mvdown {
34782 +       uint32_t                flags;                  /* input/output */
34783 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
34784 +       int8_t                  au_errno;               /* output */
34785 +} __aligned(8);
34786 +
34787 +/* ---------------------------------------------------------------------- */
34788 +
34789 +union aufs_brinfo {
34790 +       /* PATH_MAX may differ between kernel-space and user-space */
34791 +       char    _spacer[4096];
34792 +       struct {
34793 +               int16_t id;
34794 +               int     perm;
34795 +               char    path[0];
34796 +       };
34797 +} __aligned(8);
34798 +
34799 +/* ---------------------------------------------------------------------- */
34800 +
34801 +#define AuCtlType              'A'
34802 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
34803 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
34804 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
34805 +                                    struct aufs_wbr_fd)
34806 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
34807 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
34808 +                                     struct aufs_mvdown)
34809 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
34810 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
34811 +
34812 +#endif /* __AUFS_TYPE_H__ */
34813 aufs3.x-rcN loopback patch
34814
34815 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
34816 index a0da519..ffef6c0 100644
34817 --- a/drivers/block/loop.c
34818 +++ b/drivers/block/loop.c
34819 @@ -471,7 +471,7 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
34820  }
34821  
34822  struct switch_request {
34823 -       struct file *file;
34824 +       struct file *file, *virt_file;
34825         struct completion wait;
34826  };
34827  
34828 @@ -491,6 +491,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
34829         mapping = file->f_mapping;
34830         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
34831         lo->lo_backing_file = file;
34832 +       lo->lo_backing_virt_file = p->virt_file;
34833         lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
34834                 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
34835         lo->old_gfp_mask = mapping_gfp_mask(mapping);
34836 @@ -502,11 +503,13 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
34837   * First it needs to flush existing IO, it does this by sending a magic
34838   * BIO down the pipe. The completion of this BIO does the actual switch.
34839   */
34840 -static int loop_switch(struct loop_device *lo, struct file *file)
34841 +static int loop_switch(struct loop_device *lo, struct file *file,
34842 +                      struct file *virt_file)
34843  {
34844         struct switch_request w;
34845  
34846         w.file = file;
34847 +       w.virt_file = virt_file;
34848  
34849         /* freeze queue and wait for completion of scheduled requests */
34850         blk_mq_freeze_queue(lo->lo_queue);
34851 @@ -525,7 +528,16 @@ static int loop_switch(struct loop_device *lo, struct file *file)
34852   */
34853  static int loop_flush(struct loop_device *lo)
34854  {
34855 -       return loop_switch(lo, NULL);
34856 +       return loop_switch(lo, NULL, NULL);
34857 +}
34858 +
34859 +static struct file *loop_real_file(struct file *file)
34860 +{
34861 +       struct file *f = NULL;
34862 +
34863 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
34864 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
34865 +       return f;
34866  }
34867  
34868  /*
34869 @@ -540,6 +552,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
34870                           unsigned int arg)
34871  {
34872         struct file     *file, *old_file;
34873 +       struct file     *f, *virt_file = NULL, *old_virt_file;
34874         struct inode    *inode;
34875         int             error;
34876  
34877 @@ -556,9 +569,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
34878         file = fget(arg);
34879         if (!file)
34880                 goto out;
34881 +       f = loop_real_file(file);
34882 +       if (f) {
34883 +               virt_file = file;
34884 +               file = f;
34885 +               get_file(file);
34886 +       }
34887  
34888         inode = file->f_mapping->host;
34889         old_file = lo->lo_backing_file;
34890 +       old_virt_file = lo->lo_backing_virt_file;
34891  
34892         error = -EINVAL;
34893  
34894 @@ -570,17 +590,21 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
34895                 goto out_putf;
34896  
34897         /* and ... switch */
34898 -       error = loop_switch(lo, file);
34899 +       error = loop_switch(lo, file, virt_file);
34900         if (error)
34901                 goto out_putf;
34902  
34903         fput(old_file);
34904 +       if (old_virt_file)
34905 +               fput(old_virt_file);
34906         if (lo->lo_flags & LO_FLAGS_PARTSCAN)
34907                 ioctl_by_bdev(bdev, BLKRRPART, 0);
34908         return 0;
34909  
34910   out_putf:
34911         fput(file);
34912 +       if (virt_file)
34913 +               fput(virt_file);
34914   out:
34915         return error;
34916  }
34917 @@ -741,7 +765,7 @@ static void loop_config_discard(struct loop_device *lo)
34918  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
34919                        struct block_device *bdev, unsigned int arg)
34920  {
34921 -       struct file     *file, *f;
34922 +       struct file     *file, *f, *virt_file = NULL;
34923         struct inode    *inode;
34924         struct address_space *mapping;
34925         unsigned lo_blocksize;
34926 @@ -756,6 +780,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
34927         file = fget(arg);
34928         if (!file)
34929                 goto out;
34930 +       f = loop_real_file(file);
34931 +       if (f) {
34932 +               virt_file = file;
34933 +               file = f;
34934 +               get_file(file);
34935 +       }
34936  
34937         error = -EBUSY;
34938         if (lo->lo_state != Lo_unbound)
34939 @@ -804,6 +834,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
34940         lo->lo_device = bdev;
34941         lo->lo_flags = lo_flags;
34942         lo->lo_backing_file = file;
34943 +       lo->lo_backing_virt_file = virt_file;
34944         lo->transfer = transfer_none;
34945         lo->ioctl = NULL;
34946         lo->lo_sizelimit = 0;
34947 @@ -835,6 +866,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
34948  
34949   out_putf:
34950         fput(file);
34951 +       if (virt_file)
34952 +               fput(virt_file);
34953   out:
34954         /* This is safe: open() is still holding a reference. */
34955         module_put(THIS_MODULE);
34956 @@ -881,6 +914,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
34957  static int loop_clr_fd(struct loop_device *lo)
34958  {
34959         struct file *filp = lo->lo_backing_file;
34960 +       struct file *virt_filp = lo->lo_backing_virt_file;
34961         gfp_t gfp = lo->old_gfp_mask;
34962         struct block_device *bdev = lo->lo_device;
34963  
34964 @@ -909,6 +943,7 @@ static int loop_clr_fd(struct loop_device *lo)
34965         spin_lock_irq(&lo->lo_lock);
34966         lo->lo_state = Lo_rundown;
34967         lo->lo_backing_file = NULL;
34968 +       lo->lo_backing_virt_file = NULL;
34969         spin_unlock_irq(&lo->lo_lock);
34970  
34971         loop_release_xfer(lo);
34972 @@ -950,6 +985,8 @@ static int loop_clr_fd(struct loop_device *lo)
34973          * bd_mutex which is usually taken before lo_ctl_mutex.
34974          */
34975         fput(filp);
34976 +       if (virt_filp)
34977 +               fput(virt_filp);
34978         return 0;
34979  }
34980  
34981 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
34982 index 301c27f..df84aa0 100644
34983 --- a/drivers/block/loop.h
34984 +++ b/drivers/block/loop.h
34985 @@ -46,7 +46,7 @@ struct loop_device {
34986         int             (*ioctl)(struct loop_device *, int cmd, 
34987                                  unsigned long arg); 
34988  
34989 -       struct file *   lo_backing_file;
34990 +       struct file *   lo_backing_file, *lo_backing_virt_file;
34991         struct block_device *lo_device;
34992         unsigned        lo_blocksize;
34993         void            *key_data; 
34994 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
34995 index 41c4c4a..6a68365 100644
34996 --- a/fs/aufs/f_op.c
34997 +++ b/fs/aufs/f_op.c
34998 @@ -368,7 +368,7 @@ static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
34999         err = -EINVAL;
35000         h_file = au_hf_top(file);
35001         get_file(h_file);
35002 -       if (au_test_loopback_kthread()) {
35003 +       if (0 && au_test_loopback_kthread()) {
35004                 au_warn_loopback(h_file->f_path.dentry->d_sb);
35005                 if (file->f_mapping != h_file->f_mapping) {
35006                         file->f_mapping = h_file->f_mapping;
35007 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
35008 index 69f7e96..7941063 100644
35009 --- a/fs/aufs/loop.c
35010 +++ b/fs/aufs/loop.c
35011 @@ -130,3 +130,19 @@ void au_loopback_fin(void)
35012         symbol_put(loop_backing_file);
35013         kfree(au_warn_loopback_array);
35014  }
35015 +
35016 +/* ---------------------------------------------------------------------- */
35017 +
35018 +/* support the loopback block device insude aufs */
35019 +
35020 +struct file *aufs_real_loop(struct file *file)
35021 +{
35022 +       struct file *f;
35023 +
35024 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
35025 +       fi_read_lock(file);
35026 +       f = au_hf_top(file);
35027 +       fi_read_unlock(file);
35028 +       AuDebugOn(!f);
35029 +       return f;
35030 +}
35031 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
35032 index 6d9864d..3322557 100644
35033 --- a/fs/aufs/loop.h
35034 +++ b/fs/aufs/loop.h
35035 @@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
35036  
35037  int au_loopback_init(void);
35038  void au_loopback_fin(void);
35039 +
35040 +struct file *aufs_real_loop(struct file *file);
35041  #else
35042 +AuStub(struct file *, loop_backing_file, return NULL)
35043 +
35044  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
35045            struct dentry *h_adding)
35046  AuStubInt0(au_test_loopback_kthread, void)
35047 @@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
35048  
35049  AuStubInt0(au_loopback_init, void)
35050  AuStubVoid(au_loopback_fin, void)
35051 +
35052 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
35053  #endif /* BLK_DEV_LOOP */
35054  
35055  #endif /* __KERNEL__ */
35056 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
35057 index 98dc945..5da1392 100644
35058 --- a/fs/aufs/super.c
35059 +++ b/fs/aufs/super.c
35060 @@ -810,7 +810,10 @@ static const struct super_operations aufs_sop = {
35061         .statfs         = aufs_statfs,
35062         .put_super      = aufs_put_super,
35063         .sync_fs        = aufs_sync_fs,
35064 -       .remount_fs     = aufs_remount_fs
35065 +       .remount_fs     = aufs_remount_fs,
35066 +#ifdef CONFIG_AUFS_BDEV_LOOP
35067 +       .real_loop      = aufs_real_loop
35068 +#endif
35069  };
35070  
35071  /* ---------------------------------------------------------------------- */
35072 diff --git a/include/linux/fs.h b/include/linux/fs.h
35073 index b4d71b5..328c21d 100644
35074 --- a/include/linux/fs.h
35075 +++ b/include/linux/fs.h
35076 @@ -1655,6 +1655,10 @@ struct super_operations {
35077                                   struct shrink_control *);
35078         long (*free_cached_objects)(struct super_block *,
35079                                     struct shrink_control *);
35080 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
35081 +       /* and aufs */
35082 +       struct file *(*real_loop)(struct file *);
35083 +#endif
35084  };
35085  
35086  /*
This page took 2.920678 seconds and 3 git commands to generate.